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