17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*cab28b2dStaylor  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/scsi/scsi.h>
307c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #define	A_TO_TRAN(ap)	((ap)->a_hba_tran)
347c478bd9Sstevel@tonic-gate #define	P_TO_TRAN(pkt)	((pkt)->pkt_address.a_hba_tran)
357c478bd9Sstevel@tonic-gate #define	P_TO_ADDR(pkt)	(&((pkt)->pkt_address))
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * Callback id
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate uintptr_t scsi_callback_id = 0;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate struct buf *
457c478bd9Sstevel@tonic-gate scsi_alloc_consistent_buf(struct scsi_address *ap,
467c478bd9Sstevel@tonic-gate     struct buf *in_bp, size_t datalen, uint_t bflags,
477c478bd9Sstevel@tonic-gate     int (*callback)(caddr_t), caddr_t callback_arg)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip;
507c478bd9Sstevel@tonic-gate 	struct		buf *bp;
517c478bd9Sstevel@tonic-gate 	int		kmflag;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_SCSI_RES, TR_SCSI_ALLOC_CONSISTENT_BUF_START,
547c478bd9Sstevel@tonic-gate 		"scsi_alloc_consistent_buf_start");
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if (!in_bp) {
577c478bd9Sstevel@tonic-gate 		kmflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
587c478bd9Sstevel@tonic-gate 		if ((bp = getrbuf(kmflag)) == NULL) {
597c478bd9Sstevel@tonic-gate 			goto no_resource;
607c478bd9Sstevel@tonic-gate 		}
617c478bd9Sstevel@tonic-gate 	} else {
627c478bd9Sstevel@tonic-gate 		bp = in_bp;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 		/* we are establishing a new buffer memory association */
657c478bd9Sstevel@tonic-gate 		bp->b_flags &= ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW);
667c478bd9Sstevel@tonic-gate 		bp->b_proc = NULL;
677c478bd9Sstevel@tonic-gate 		bp->b_pages = NULL;
687c478bd9Sstevel@tonic-gate 		bp->b_shadow = NULL;
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/* limit bits that can be set by bflags argument */
727c478bd9Sstevel@tonic-gate 	ASSERT(!(bflags & ~(B_READ | B_WRITE)));
737c478bd9Sstevel@tonic-gate 	bflags &= (B_READ | B_WRITE);
747c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = 0;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (datalen) {
777c478bd9Sstevel@tonic-gate 		pdip = (A_TO_TRAN(ap))->tran_hba_dip;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 		while (ddi_iopb_alloc(pdip, (ddi_dma_lim_t *)0, datalen,
807c478bd9Sstevel@tonic-gate 		    &bp->b_un.b_addr)) {
817c478bd9Sstevel@tonic-gate 			if (callback == SLEEP_FUNC) {
827c478bd9Sstevel@tonic-gate 				delay(drv_usectohz(10000));
837c478bd9Sstevel@tonic-gate 			} else {
847c478bd9Sstevel@tonic-gate 				if (!in_bp)
857c478bd9Sstevel@tonic-gate 					freerbuf(bp);
867c478bd9Sstevel@tonic-gate 				goto no_resource;
877c478bd9Sstevel@tonic-gate 			}
887c478bd9Sstevel@tonic-gate 		}
897c478bd9Sstevel@tonic-gate 		bp->b_flags |= bflags;
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 	bp->b_bcount = datalen;
927c478bd9Sstevel@tonic-gate 	bp->b_resid = 0;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_SCSI_RES, TR_SCSI_ALLOC_CONSISTENT_BUF_END,
957c478bd9Sstevel@tonic-gate 		"scsi_alloc_consistent_buf_end");
967c478bd9Sstevel@tonic-gate 	return (bp);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate no_resource:
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (callback != NULL_FUNC && callback != SLEEP_FUNC) {
1017c478bd9Sstevel@tonic-gate 		ddi_set_callback(callback, callback_arg,
1027c478bd9Sstevel@tonic-gate 			&scsi_callback_id);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_SCSI_RES,
1057c478bd9Sstevel@tonic-gate 	    TR_SCSI_ALLOC_CONSISTENT_BUF_RETURN1_END,
1067c478bd9Sstevel@tonic-gate 	    "scsi_alloc_consistent_buf_end (return1)");
1077c478bd9Sstevel@tonic-gate 	return (NULL);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate void
1117c478bd9Sstevel@tonic-gate scsi_free_consistent_buf(struct buf *bp)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_SCSI_RES, TR_SCSI_FREE_CONSISTENT_BUF_START,
1147c478bd9Sstevel@tonic-gate 		"scsi_free_consistent_buf_start");
1157c478bd9Sstevel@tonic-gate 	if (!bp)
1167c478bd9Sstevel@tonic-gate 		return;
1177c478bd9Sstevel@tonic-gate 	if (bp->b_un.b_addr)
1187c478bd9Sstevel@tonic-gate 		ddi_iopb_free((caddr_t)bp->b_un.b_addr);
1197c478bd9Sstevel@tonic-gate 	freerbuf(bp);
1207c478bd9Sstevel@tonic-gate 	if (scsi_callback_id != 0) {
1217c478bd9Sstevel@tonic-gate 		ddi_run_callback(&scsi_callback_id);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_SCSI_RES, TR_SCSI_FREE_CONSISTENT_BUF_END,
1247c478bd9Sstevel@tonic-gate 		"scsi_free_consistent_buf_end");
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
127*cab28b2dStaylor void scsi_free_cache_pkt(struct scsi_address *, struct scsi_pkt *);
128*cab28b2dStaylor 
129*cab28b2dStaylor struct scsi_pkt *
130*cab28b2dStaylor scsi_init_cache_pkt(struct scsi_address *ap, struct scsi_pkt *in_pktp,
131*cab28b2dStaylor     struct buf *bp, int cmdlen, int statuslen, int pplen,
132*cab28b2dStaylor     int flags, int (*callback)(caddr_t), caddr_t callback_arg)
133*cab28b2dStaylor {
134*cab28b2dStaylor 	struct scsi_pkt_cache_wrapper *pktw;
135*cab28b2dStaylor 	scsi_hba_tran_t *tranp = ap->a_hba_tran;
136*cab28b2dStaylor 	int		(*func)(caddr_t);
137*cab28b2dStaylor 
138*cab28b2dStaylor 	func = (callback == SLEEP_FUNC) ? SLEEP_FUNC : NULL_FUNC;
139*cab28b2dStaylor 
140*cab28b2dStaylor 	if (in_pktp == NULL) {
141*cab28b2dStaylor 		int kf;
142*cab28b2dStaylor 
143*cab28b2dStaylor 		if (callback == SLEEP_FUNC)
144*cab28b2dStaylor 			kf = KM_SLEEP;
145*cab28b2dStaylor 		else
146*cab28b2dStaylor 			kf = KM_NOSLEEP;
147*cab28b2dStaylor 		pktw = kmem_cache_alloc(tranp->tran_pkt_cache_ptr,
148*cab28b2dStaylor 			    kf);
149*cab28b2dStaylor 		if (pktw == NULL)
150*cab28b2dStaylor 			goto fail1;
151*cab28b2dStaylor 
152*cab28b2dStaylor 		pktw->pcw_kmflags = 0;
153*cab28b2dStaylor 		in_pktp = &(pktw->pcw_pkt);
154*cab28b2dStaylor 		/*
155*cab28b2dStaylor 		 * target driver initializes pkt_comp, pkt_flags,
156*cab28b2dStaylor 		 * and pkt_time
157*cab28b2dStaylor 		 */
158*cab28b2dStaylor 		in_pktp->pkt_address = *ap;
159*cab28b2dStaylor 		in_pktp->pkt_resid = 0;
160*cab28b2dStaylor 		in_pktp->pkt_state = 0;
161*cab28b2dStaylor 		in_pktp->pkt_statistics = 0;
162*cab28b2dStaylor 		in_pktp->pkt_reason = 0;
163*cab28b2dStaylor 
164*cab28b2dStaylor 		in_pktp->pkt_cdblen = cmdlen;
165*cab28b2dStaylor 		if ((tranp->tran_hba_flags & SCSI_HBA_TRAN_CDB) &&
166*cab28b2dStaylor 		    (cmdlen > DEFAULT_CDBLEN)) {
167*cab28b2dStaylor 			pktw->pcw_kmflags |= NEED_EXT_CDB;
168*cab28b2dStaylor 			in_pktp->pkt_cdbp = kmem_zalloc(cmdlen, kf);
169*cab28b2dStaylor 			if (in_pktp->pkt_cdbp == NULL)
170*cab28b2dStaylor 				goto fail2;
171*cab28b2dStaylor 		}
172*cab28b2dStaylor 		in_pktp->pkt_tgtlen = pplen;
173*cab28b2dStaylor 		if (pplen > DEFAULT_PRIVLEN) {
174*cab28b2dStaylor 			pktw->pcw_kmflags |= NEED_EXT_TGT;
175*cab28b2dStaylor 			in_pktp->pkt_private = kmem_zalloc(pplen, kf);
176*cab28b2dStaylor 			if (in_pktp->pkt_private == NULL)
177*cab28b2dStaylor 				goto fail3;
178*cab28b2dStaylor 		}
179*cab28b2dStaylor 		in_pktp->pkt_scblen = statuslen;
180*cab28b2dStaylor 		if ((tranp->tran_hba_flags & SCSI_HBA_TRAN_SCB) &&
181*cab28b2dStaylor 		    (statuslen > DEFAULT_SCBLEN)) {
182*cab28b2dStaylor 			pktw->pcw_kmflags |= NEED_EXT_SCB;
183*cab28b2dStaylor 			in_pktp->pkt_scbp = kmem_zalloc(statuslen, kf);
184*cab28b2dStaylor 			if (in_pktp->pkt_scbp == NULL)
185*cab28b2dStaylor 				goto fail4;
186*cab28b2dStaylor 		}
187*cab28b2dStaylor 		if ((*tranp->tran_setup_pkt) (in_pktp,
188*cab28b2dStaylor 			func, NULL) == -1) {
189*cab28b2dStaylor 				goto fail5;
190*cab28b2dStaylor 		}
191*cab28b2dStaylor 	}
192*cab28b2dStaylor 	if (bp && bp->b_bcount) {
193*cab28b2dStaylor 		if ((*tranp->tran_setup_bp) (in_pktp, bp,
194*cab28b2dStaylor 		    flags, func, NULL) == -1) {
195*cab28b2dStaylor 			scsi_free_cache_pkt(ap, in_pktp);
196*cab28b2dStaylor 			in_pktp = NULL;
197*cab28b2dStaylor 		}
198*cab28b2dStaylor 	}
199*cab28b2dStaylor 	return (in_pktp);
200*cab28b2dStaylor 
201*cab28b2dStaylor fail5:
202*cab28b2dStaylor 	if (pktw->pcw_kmflags & NEED_EXT_SCB) {
203*cab28b2dStaylor 		kmem_free(in_pktp->pkt_scbp, statuslen);
204*cab28b2dStaylor 		in_pktp->pkt_scbp = (opaque_t)((char *)in_pktp +
205*cab28b2dStaylor 		    tranp->tran_hba_len + DEFAULT_PRIVLEN +
206*cab28b2dStaylor 		    sizeof (struct scsi_pkt));
207*cab28b2dStaylor 		if ((A_TO_TRAN(ap))->tran_hba_flags & SCSI_HBA_TRAN_CDB)
208*cab28b2dStaylor 			in_pktp->pkt_scbp = (opaque_t)((in_pktp->pkt_scbp) +
209*cab28b2dStaylor 				DEFAULT_CDBLEN);
210*cab28b2dStaylor 		in_pktp->pkt_scblen = 0;
211*cab28b2dStaylor 	}
212*cab28b2dStaylor fail4:
213*cab28b2dStaylor 	if (pktw->pcw_kmflags & NEED_EXT_TGT) {
214*cab28b2dStaylor 		kmem_free(in_pktp->pkt_private, pplen);
215*cab28b2dStaylor 		in_pktp->pkt_tgtlen = 0;
216*cab28b2dStaylor 		in_pktp->pkt_private = NULL;
217*cab28b2dStaylor 	}
218*cab28b2dStaylor fail3:
219*cab28b2dStaylor 	if (pktw->pcw_kmflags & NEED_EXT_CDB) {
220*cab28b2dStaylor 		kmem_free(in_pktp->pkt_cdbp, cmdlen);
221*cab28b2dStaylor 		in_pktp->pkt_cdbp = (opaque_t)((char *)in_pktp +
222*cab28b2dStaylor 		    tranp->tran_hba_len +
223*cab28b2dStaylor 		    sizeof (struct scsi_pkt));
224*cab28b2dStaylor 		in_pktp->pkt_cdblen = 0;
225*cab28b2dStaylor 	}
226*cab28b2dStaylor 	pktw->pcw_kmflags &=
227*cab28b2dStaylor 	    ~(NEED_EXT_CDB|NEED_EXT_TGT|NEED_EXT_SCB);
228*cab28b2dStaylor fail2:
229*cab28b2dStaylor 	kmem_cache_free(tranp->tran_pkt_cache_ptr, pktw);
230*cab28b2dStaylor fail1:
231*cab28b2dStaylor 	if (callback != NULL_FUNC && callback != SLEEP_FUNC) {
232*cab28b2dStaylor 		ddi_set_callback(callback, callback_arg,
233*cab28b2dStaylor 			&scsi_callback_id);
234*cab28b2dStaylor 	}
235*cab28b2dStaylor 
236*cab28b2dStaylor 	return (NULL);
237*cab28b2dStaylor }
238*cab28b2dStaylor 
239*cab28b2dStaylor void
240*cab28b2dStaylor scsi_free_cache_pkt(struct scsi_address *ap, struct scsi_pkt *pktp)
241*cab28b2dStaylor {
242*cab28b2dStaylor 	struct scsi_pkt_cache_wrapper *pktw;
243*cab28b2dStaylor 
244*cab28b2dStaylor 	(*A_TO_TRAN(ap)->tran_teardown_pkt)(pktp);
245*cab28b2dStaylor 	pktw = (struct scsi_pkt_cache_wrapper *)pktp;
246*cab28b2dStaylor 
247*cab28b2dStaylor 	/*
248*cab28b2dStaylor 	 * if we allocated memory for anything that wouldn't fit, free
249*cab28b2dStaylor 	 * the memory and restore the pointers
250*cab28b2dStaylor 	 */
251*cab28b2dStaylor 	if (pktw->pcw_kmflags & NEED_EXT_SCB) {
252*cab28b2dStaylor 		kmem_free(pktp->pkt_scbp, pktp->pkt_scblen);
253*cab28b2dStaylor 		pktp->pkt_scbp = (opaque_t)((char *)pktp +
254*cab28b2dStaylor 		    (A_TO_TRAN(ap))->tran_hba_len +
255*cab28b2dStaylor 		    DEFAULT_PRIVLEN + sizeof (struct scsi_pkt_cache_wrapper));
256*cab28b2dStaylor 		if ((A_TO_TRAN(ap))->tran_hba_flags & SCSI_HBA_TRAN_CDB)
257*cab28b2dStaylor 			pktp->pkt_scbp = (opaque_t)((pktp->pkt_scbp) +
258*cab28b2dStaylor 				DEFAULT_CDBLEN);
259*cab28b2dStaylor 		pktp->pkt_scblen = 0;
260*cab28b2dStaylor 	}
261*cab28b2dStaylor 	if (pktw->pcw_kmflags & NEED_EXT_TGT) {
262*cab28b2dStaylor 		kmem_free(pktp->pkt_private, pktp->pkt_tgtlen);
263*cab28b2dStaylor 		pktp->pkt_tgtlen = 0;
264*cab28b2dStaylor 		pktp->pkt_private = NULL;
265*cab28b2dStaylor 	}
266*cab28b2dStaylor 	if (pktw->pcw_kmflags & NEED_EXT_CDB) {
267*cab28b2dStaylor 		kmem_free(pktp->pkt_cdbp, pktp->pkt_cdblen);
268*cab28b2dStaylor 		pktp->pkt_cdbp = (opaque_t)((char *)pktp +
269*cab28b2dStaylor 		    (A_TO_TRAN(ap))->tran_hba_len +
270*cab28b2dStaylor 		    sizeof (struct scsi_pkt_cache_wrapper));
271*cab28b2dStaylor 		pktp->pkt_cdblen = 0;
272*cab28b2dStaylor 	}
273*cab28b2dStaylor 	pktw->pcw_kmflags &=
274*cab28b2dStaylor 	    ~(NEED_EXT_CDB|NEED_EXT_TGT|NEED_EXT_SCB);
275*cab28b2dStaylor 	ASSERT(pktw->pcw_kmflags == 0);
276*cab28b2dStaylor 	kmem_cache_free(A_TO_TRAN(ap)->tran_pkt_cache_ptr, pktw);
277*cab28b2dStaylor 
278*cab28b2dStaylor 	if (scsi_callback_id != 0) {
279*cab28b2dStaylor 		ddi_run_callback(&scsi_callback_id);
280*cab28b2dStaylor 	}
281*cab28b2dStaylor 
282*cab28b2dStaylor }
283*cab28b2dStaylor 
2847c478bd9Sstevel@tonic-gate struct scsi_pkt *
2857c478bd9Sstevel@tonic-gate scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *in_pktp,
2867c478bd9Sstevel@tonic-gate     struct buf *bp, int cmdlen, int statuslen, int pplen,
2877c478bd9Sstevel@tonic-gate     int flags, int (*callback)(caddr_t), caddr_t callback_arg)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	struct scsi_pkt *pktp;
2907c478bd9Sstevel@tonic-gate 	scsi_hba_tran_t *tranp = ap->a_hba_tran;
2917c478bd9Sstevel@tonic-gate 	int		(*func)(caddr_t);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	TRACE_5(TR_FAC_SCSI_RES, TR_SCSI_INIT_PKT_START,
2947c478bd9Sstevel@tonic-gate "scsi_init_pkt_start: addr %p in_pktp %p cmdlen %d statuslen %d pplen %d",
2957c478bd9Sstevel@tonic-gate 	    ap, in_pktp, cmdlen, statuslen, pplen);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
2987c478bd9Sstevel@tonic-gate 	if (flags & PKT_CONSISTENT_OLD) {
2997c478bd9Sstevel@tonic-gate 		flags &= ~PKT_CONSISTENT_OLD;
3007c478bd9Sstevel@tonic-gate 		flags |= PKT_CONSISTENT;
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate #endif
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	func = (callback == SLEEP_FUNC) ? SLEEP_FUNC : NULL_FUNC;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	pktp = (*tranp->tran_init_pkt) (ap, in_pktp, bp, cmdlen,
3077c478bd9Sstevel@tonic-gate 		statuslen, pplen, flags, func, NULL);
3087c478bd9Sstevel@tonic-gate 	if (pktp == NULL) {
3097c478bd9Sstevel@tonic-gate 		if (callback != NULL_FUNC && callback != SLEEP_FUNC) {
3107c478bd9Sstevel@tonic-gate 			ddi_set_callback(callback, callback_arg,
3117c478bd9Sstevel@tonic-gate 				&scsi_callback_id);
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_SCSI_RES, TR_SCSI_INIT_PKT_END,
3167c478bd9Sstevel@tonic-gate 		"scsi_init_pkt_end: pktp %p", pktp);
3177c478bd9Sstevel@tonic-gate 	return (pktp);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate void
3217c478bd9Sstevel@tonic-gate scsi_destroy_pkt(struct scsi_pkt *pkt)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	struct scsi_address	*ap = P_TO_ADDR(pkt);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_SCSI_RES, TR_SCSI_DESTROY_PKT_START,
3267c478bd9Sstevel@tonic-gate 		"scsi_destroy_pkt_start: pkt %p", pkt);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	(*A_TO_TRAN(ap)->tran_destroy_pkt)(ap, pkt);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (scsi_callback_id != 0) {
3317c478bd9Sstevel@tonic-gate 		ddi_run_callback(&scsi_callback_id);
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_SCSI_RES, TR_SCSI_DESTROY_PKT_END,
3357c478bd9Sstevel@tonic-gate 		"scsi_destroy_pkt_end");
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  *	Generic Resource Allocation Routines
3417c478bd9Sstevel@tonic-gate  */
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate struct scsi_pkt *
3447c478bd9Sstevel@tonic-gate scsi_resalloc(struct scsi_address *ap, int cmdlen, int statuslen,
3457c478bd9Sstevel@tonic-gate     opaque_t dmatoken, int (*callback)())
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	register struct	scsi_pkt *pkt;
3487c478bd9Sstevel@tonic-gate 	register scsi_hba_tran_t *tranp = ap->a_hba_tran;
3497c478bd9Sstevel@tonic-gate 	register int			(*func)(caddr_t);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	func = (callback == SLEEP_FUNC) ? SLEEP_FUNC : NULL_FUNC;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	pkt = (*tranp->tran_init_pkt) (ap, NULL, (struct buf *)dmatoken,
3547c478bd9Sstevel@tonic-gate 		cmdlen, statuslen, 0, 0, func, NULL);
3557c478bd9Sstevel@tonic-gate 	if (pkt == NULL) {
3567c478bd9Sstevel@tonic-gate 		if (callback != NULL_FUNC && callback != SLEEP_FUNC) {
3577c478bd9Sstevel@tonic-gate 			ddi_set_callback(callback, NULL, &scsi_callback_id);
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	return (pkt);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate struct scsi_pkt *
3657c478bd9Sstevel@tonic-gate scsi_pktalloc(struct scsi_address *ap, int cmdlen, int statuslen,
3667c478bd9Sstevel@tonic-gate     int (*callback)())
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 	struct scsi_pkt		*pkt;
3697c478bd9Sstevel@tonic-gate 	struct scsi_hba_tran	*tran = ap->a_hba_tran;
3707c478bd9Sstevel@tonic-gate 	register int			(*func)(caddr_t);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	func = (callback == SLEEP_FUNC) ? SLEEP_FUNC : NULL_FUNC;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	pkt = (*tran->tran_init_pkt) (ap, NULL, NULL, cmdlen,
3757c478bd9Sstevel@tonic-gate 		statuslen, 0, 0, func, NULL);
3767c478bd9Sstevel@tonic-gate 	if (pkt == NULL) {
3777c478bd9Sstevel@tonic-gate 		if (callback != NULL_FUNC && callback != SLEEP_FUNC) {
3787c478bd9Sstevel@tonic-gate 			ddi_set_callback(callback, NULL, &scsi_callback_id);
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	return (pkt);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate struct scsi_pkt *
3867c478bd9Sstevel@tonic-gate scsi_dmaget(struct scsi_pkt *pkt, opaque_t dmatoken, int (*callback)())
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	struct scsi_pkt		*new_pkt;
3897c478bd9Sstevel@tonic-gate 	register int		(*func)(caddr_t);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	func = (callback == SLEEP_FUNC) ? SLEEP_FUNC : NULL_FUNC;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	new_pkt = (*P_TO_TRAN(pkt)->tran_init_pkt) (&pkt->pkt_address,
3947c478bd9Sstevel@tonic-gate 		pkt, (struct buf *)dmatoken,
3957c478bd9Sstevel@tonic-gate 		0, 0, 0, 0, func, NULL);
3967c478bd9Sstevel@tonic-gate 	ASSERT(new_pkt == pkt || new_pkt == NULL);
3977c478bd9Sstevel@tonic-gate 	if (new_pkt == NULL) {
3987c478bd9Sstevel@tonic-gate 		if (callback != NULL_FUNC && callback != SLEEP_FUNC) {
3997c478bd9Sstevel@tonic-gate 			ddi_set_callback(callback, NULL, &scsi_callback_id);
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	return (new_pkt);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate  *	Generic Resource Deallocation Routines
4097c478bd9Sstevel@tonic-gate  */
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate void
4127c478bd9Sstevel@tonic-gate scsi_dmafree(struct scsi_pkt *pkt)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	register struct scsi_address	*ap = P_TO_ADDR(pkt);
4157c478bd9Sstevel@tonic-gate 	(*A_TO_TRAN(ap)->tran_dmafree)(ap, pkt);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (scsi_callback_id != 0) {
4187c478bd9Sstevel@tonic-gate 		ddi_run_callback(&scsi_callback_id);
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate void
4237c478bd9Sstevel@tonic-gate scsi_sync_pkt(struct scsi_pkt *pkt)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	register struct scsi_address	*ap = P_TO_ADDR(pkt);
4267c478bd9Sstevel@tonic-gate 	(*A_TO_TRAN(ap)->tran_sync_pkt)(ap, pkt);
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate void
4307c478bd9Sstevel@tonic-gate scsi_resfree(struct scsi_pkt *pkt)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	register struct scsi_address	*ap = P_TO_ADDR(pkt);
4337c478bd9Sstevel@tonic-gate 	(*A_TO_TRAN(ap)->tran_destroy_pkt)(ap, pkt);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	if (scsi_callback_id != 0) {
4367c478bd9Sstevel@tonic-gate 		ddi_run_callback(&scsi_callback_id);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate }
439