1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * ISSUES
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <sys/scsi/scsi.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/note.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/scsi/adapters/fasreg.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/scsi/adapters/fasvar.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/scsi/adapters/fascmd.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #ifdef	FASDEBUG
41*7c478bd9Sstevel@tonic-gate extern int  fasdebug;
42*7c478bd9Sstevel@tonic-gate extern int  fasdebug_instance; /* debug all instances */
43*7c478bd9Sstevel@tonic-gate #endif	/* FASDEBUG */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate void fas_complete_arq_pkt(struct scsi_pkt *pkt);
46*7c478bd9Sstevel@tonic-gate void fas_call_pkt_comp(register struct fas *fas,
47*7c478bd9Sstevel@tonic-gate     register struct fas_cmd *sp);
48*7c478bd9Sstevel@tonic-gate void fas_empty_callbackQ(struct fas *fas);
49*7c478bd9Sstevel@tonic-gate int fas_init_callbacks(struct fas *fas);
50*7c478bd9Sstevel@tonic-gate void fas_destroy_callbacks(struct fas *fas);
51*7c478bd9Sstevel@tonic-gate void fas_printf(struct fas *fas, const char *fmt, ...);
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate int
fas_init_callbacks(struct fas * fas)54*7c478bd9Sstevel@tonic-gate fas_init_callbacks(struct fas *fas)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	mutex_init(&fas->f_c_mutex, NULL, MUTEX_DRIVER, fas->f_iblock);
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	return (0);
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate void
fas_destroy_callbacks(struct fas * fas)62*7c478bd9Sstevel@tonic-gate fas_destroy_callbacks(struct fas *fas)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&fas->f_c_mutex);
65*7c478bd9Sstevel@tonic-gate }
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate void
fas_empty_callbackQ(struct fas * fas)68*7c478bd9Sstevel@tonic-gate fas_empty_callbackQ(struct fas *fas)
69*7c478bd9Sstevel@tonic-gate {
70*7c478bd9Sstevel@tonic-gate 	register struct fas_cmd *sp;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_SCSI, TR_FAS_EMPTY_CALLBACKQ_START,
73*7c478bd9Sstevel@tonic-gate 	    "fas_empty_callbackQ_start");
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fas->f_c_mutex);
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	/*
78*7c478bd9Sstevel@tonic-gate 	 * don't recurse into calling back: the target driver
79*7c478bd9Sstevel@tonic-gate 	 * may call scsi_transport() again which may call
80*7c478bd9Sstevel@tonic-gate 	 * fas_empty_callbackQ again
81*7c478bd9Sstevel@tonic-gate 	 */
82*7c478bd9Sstevel@tonic-gate 	if (fas->f_c_in_callback) {
83*7c478bd9Sstevel@tonic-gate 		goto done;
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 	fas->f_c_in_callback = 1;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	while (fas->f_c_qf) {
88*7c478bd9Sstevel@tonic-gate 		register struct fas_cmd *qf = fas->f_c_qf;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 		fas->f_c_qf = fas->f_c_qb = NULL;
91*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fas->f_c_mutex);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 		while (qf) {
94*7c478bd9Sstevel@tonic-gate 			sp = qf;
95*7c478bd9Sstevel@tonic-gate 			qf =   sp->cmd_forw;
96*7c478bd9Sstevel@tonic-gate 			(*sp->cmd_pkt->pkt_comp)(sp->cmd_pkt);
97*7c478bd9Sstevel@tonic-gate 		}
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 		mutex_enter(&fas->f_c_mutex);
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	fas->f_c_in_callback = 0;
103*7c478bd9Sstevel@tonic-gate done:
104*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fas->f_c_mutex);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_SCSI, TR_FAS_EMPTY_CALLBACKQ_END,
107*7c478bd9Sstevel@tonic-gate 	    "fas_empty_callbackQ_end");
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /*
112*7c478bd9Sstevel@tonic-gate  * fas_call_pkt_comp does sanity checking to ensure that we don't
113*7c478bd9Sstevel@tonic-gate  * call completion twice on the same packet or a packet that has been freed.
114*7c478bd9Sstevel@tonic-gate  * if there is a completion function specified, the packet is queued
115*7c478bd9Sstevel@tonic-gate  * up and it is left to the fas_callback thread to empty the queue at
116*7c478bd9Sstevel@tonic-gate  * a lower priority; note that there is one callback queue per fas
117*7c478bd9Sstevel@tonic-gate  *
118*7c478bd9Sstevel@tonic-gate  * we use a separate thread for calling back into the target driver
119*7c478bd9Sstevel@tonic-gate  * this thread unqueues packets from the callback queue
120*7c478bd9Sstevel@tonic-gate  */
121*7c478bd9Sstevel@tonic-gate void
fas_call_pkt_comp(register struct fas * fas,register struct fas_cmd * sp)122*7c478bd9Sstevel@tonic-gate fas_call_pkt_comp(register struct fas *fas, register struct fas_cmd *sp)
123*7c478bd9Sstevel@tonic-gate {
124*7c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_SCSI, TR_FAS_CALL_PKT_COMP_START,
125*7c478bd9Sstevel@tonic-gate 	    "fas_call_pkt_comp_start");
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	ASSERT(sp != 0);
128*7c478bd9Sstevel@tonic-gate 	ASSERT((sp->cmd_flags & CFLAG_COMPLETED) == 0);
129*7c478bd9Sstevel@tonic-gate 	ASSERT((sp->cmd_flags & CFLAG_FREE) == 0);
130*7c478bd9Sstevel@tonic-gate 	ASSERT(sp->cmd_flags & CFLAG_FINISHED);
131*7c478bd9Sstevel@tonic-gate 	ASSERT(fas->f_ncmds >= fas->f_ndisc);
132*7c478bd9Sstevel@tonic-gate 	ASSERT((sp->cmd_flags & CFLAG_CMDDISC) == 0);
133*7c478bd9Sstevel@tonic-gate 	ASSERT(sp != fas->f_current_sp);
134*7c478bd9Sstevel@tonic-gate 	ASSERT(sp != fas->f_active[sp->cmd_slot]->f_slot[sp->cmd_tag[1]]);
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	sp->cmd_flags &= ~CFLAG_IN_TRANSPORT;
137*7c478bd9Sstevel@tonic-gate 	sp->cmd_flags |= CFLAG_COMPLETED;
138*7c478bd9Sstevel@tonic-gate 	sp->cmd_qfull_retries = 0;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	/*
141*7c478bd9Sstevel@tonic-gate 	 * if this was an auto request sense, complete immediately to free
142*7c478bd9Sstevel@tonic-gate 	 * the arq pkt
143*7c478bd9Sstevel@tonic-gate 	 */
144*7c478bd9Sstevel@tonic-gate 	if (sp->cmd_pkt->pkt_comp && !(sp->cmd_flags & CFLAG_CMDARQ)) {
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 		if (sp->cmd_pkt->pkt_reason != CMD_CMPLT) {
147*7c478bd9Sstevel@tonic-gate 			IPRINTF6("completion for %d.%d, sp=0x%p, "
148*7c478bd9Sstevel@tonic-gate 			    "reason=%s, stats=%x, state=%x\n",
149*7c478bd9Sstevel@tonic-gate 				Tgt(sp), Lun(sp), (void *)sp,
150*7c478bd9Sstevel@tonic-gate 				scsi_rname(sp->cmd_pkt->pkt_reason),
151*7c478bd9Sstevel@tonic-gate 				sp->cmd_pkt->pkt_statistics,
152*7c478bd9Sstevel@tonic-gate 				sp->cmd_pkt->pkt_state);
153*7c478bd9Sstevel@tonic-gate 		} else {
154*7c478bd9Sstevel@tonic-gate 			EPRINTF2("completion queued for %d.%dn",
155*7c478bd9Sstevel@tonic-gate 				Tgt(sp), Lun(sp));
156*7c478bd9Sstevel@tonic-gate 		}
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 		/*
159*7c478bd9Sstevel@tonic-gate 		 * append the packet or start a new queue
160*7c478bd9Sstevel@tonic-gate 		 */
161*7c478bd9Sstevel@tonic-gate 		mutex_enter(&fas->f_c_mutex);
162*7c478bd9Sstevel@tonic-gate 		if (fas->f_c_qf) {
163*7c478bd9Sstevel@tonic-gate 			/*
164*7c478bd9Sstevel@tonic-gate 			 * add to tail
165*7c478bd9Sstevel@tonic-gate 			 */
166*7c478bd9Sstevel@tonic-gate 			register struct fas_cmd *dp = fas->f_c_qb;
167*7c478bd9Sstevel@tonic-gate 			ASSERT(dp != NULL);
168*7c478bd9Sstevel@tonic-gate 			fas->f_c_qb =	sp;
169*7c478bd9Sstevel@tonic-gate 			sp->cmd_forw = NULL;
170*7c478bd9Sstevel@tonic-gate 			dp->cmd_forw = sp;
171*7c478bd9Sstevel@tonic-gate 		} else {
172*7c478bd9Sstevel@tonic-gate 			/*
173*7c478bd9Sstevel@tonic-gate 			 * start new queue
174*7c478bd9Sstevel@tonic-gate 			 */
175*7c478bd9Sstevel@tonic-gate 			fas->f_c_qf = fas->f_c_qb = sp;
176*7c478bd9Sstevel@tonic-gate 			sp->cmd_forw = NULL;
177*7c478bd9Sstevel@tonic-gate 		}
178*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fas->f_c_mutex);
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	} else if ((sp->cmd_flags & CFLAG_CMDARQ) && sp->cmd_pkt->pkt_comp) {
181*7c478bd9Sstevel@tonic-gate 		/*
182*7c478bd9Sstevel@tonic-gate 		 * pkt_comp may be NULL when we are aborting/resetting but then
183*7c478bd9Sstevel@tonic-gate 		 * the callback will be redone later
184*7c478bd9Sstevel@tonic-gate 		 */
185*7c478bd9Sstevel@tonic-gate 		fas_complete_arq_pkt(sp->cmd_pkt);
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	} else	{
188*7c478bd9Sstevel@tonic-gate 		EPRINTF2("No completion routine for 0x%p reason %x\n",
189*7c478bd9Sstevel@tonic-gate 		    (void *)sp, sp->cmd_pkt->pkt_reason);
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_SCSI, TR_FAS_CALL_PKT_COMP_END,
192*7c478bd9Sstevel@tonic-gate 	    "fas_call_pkt_comp_end");
193*7c478bd9Sstevel@tonic-gate }
194