xref: /illumos-gate/usr/src/uts/sun/io/dada/impl/dcd_subr.c (revision 7c478bd9)
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 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/dada/dada.h>
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * Utility DADA routines
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate  * Polling support routines
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate extern uintptr_t	dcd_callback_id;
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #ifdef NOT_NEEDED
45*7c478bd9Sstevel@tonic-gate static int	dcd_poll_busycnt = DCD_POLL_TIMEOUT;
46*7c478bd9Sstevel@tonic-gate #endif
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate /*
50*7c478bd9Sstevel@tonic-gate  * Common buffer for dcd_lod
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate extern kmutex_t  dcd_log_mutex;
54*7c478bd9Sstevel@tonic-gate static char dcd_log_buffer[256];
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #define	A_TO_TRAN(ap) 	(ap->a_hba_tran)
58*7c478bd9Sstevel@tonic-gate #define	P_TO_TRAN(pkt)	((pkt)->pkt_address.a_hba_tran)
59*7c478bd9Sstevel@tonic-gate #define	P_TO_ADDR(pkt)	(&((pkt)->pkt_address))
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate #ifdef NOT_NEEDED
63*7c478bd9Sstevel@tonic-gate int
dcd_poll(struct dcd_pkt * pkt)64*7c478bd9Sstevel@tonic-gate dcd_poll(struct dcd_pkt *pkt)
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	register	busy_count, rval = -1, savef;
68*7c478bd9Sstevel@tonic-gate 	clock_t	savet;
69*7c478bd9Sstevel@tonic-gate 	void	(*savec)();
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	/*
73*7c478bd9Sstevel@tonic-gate 	 * Save old flags
74*7c478bd9Sstevel@tonic-gate 	 */
75*7c478bd9Sstevel@tonic-gate 	savef = pkt->pkt_flags;
76*7c478bd9Sstevel@tonic-gate 	savec = pkt->pkt_comp;
77*7c478bd9Sstevel@tonic-gate 	savet = pkt->pkt_time;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	pkt->pkt_flags |= FLAG_NOINTR;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	/*
83*7c478bd9Sstevel@tonic-gate 	 * Set the Pkt_comp to NULL
84*7c478bd9Sstevel@tonic-gate 	 */
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	pkt->pkt_comp = 0;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	/*
89*7c478bd9Sstevel@tonic-gate 	 * Set the Pkt time for the polled command
90*7c478bd9Sstevel@tonic-gate 	 */
91*7c478bd9Sstevel@tonic-gate 	if (pkt->pkt_time == 0) {
92*7c478bd9Sstevel@tonic-gate 		pkt->pkt_time = DCD_POLL_TIMEOUT;
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	/* Now transport the command */
97*7c478bd9Sstevel@tonic-gate 	for (busy_count = 0; busy_count < dcd_poll_busycnt; busy_count++) {
98*7c478bd9Sstevel@tonic-gate 		if (dcd_transport(pkt) != TRAN_ACCEPT) {
99*7c478bd9Sstevel@tonic-gate 			break;
100*7c478bd9Sstevel@tonic-gate 		}
101*7c478bd9Sstevel@tonic-gate 		if (pkt->pkt_reason == CMD_INCOMPLETE && pkt->pkt_state == 0) {
102*7c478bd9Sstevel@tonic-gate 			drv_usecwait(1000000);
103*7c478bd9Sstevel@tonic-gate 		} else if (pkt->pkt_reason  != CMD_CMPLT) {
104*7c478bd9Sstevel@tonic-gate 			break;
105*7c478bd9Sstevel@tonic-gate 		} else if (((*pkt->pkt_scbp) & STATUS_ATA_MASK)
106*7c478bd9Sstevel@tonic-gate 			    == STATUS_ATA_BUSY) {
107*7c478bd9Sstevel@tonic-gate 			drv_usecwait(1000000);
108*7c478bd9Sstevel@tonic-gate 		} else {
109*7c478bd9Sstevel@tonic-gate 			rval = 0;
110*7c478bd9Sstevel@tonic-gate 			break;
111*7c478bd9Sstevel@tonic-gate 		}
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	pkt->pkt_flags = savef;
115*7c478bd9Sstevel@tonic-gate 	pkt->pkt_comp = savec;
116*7c478bd9Sstevel@tonic-gate 	pkt->pkt_time = savet;
117*7c478bd9Sstevel@tonic-gate 	return (rval);
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate #endif
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE4*/
123*7c478bd9Sstevel@tonic-gate void
dcd_log(dev_info_t * dev,char * label,uint_t level,const char * fmt,...)124*7c478bd9Sstevel@tonic-gate dcd_log(dev_info_t *dev, char *label, uint_t level, const char *fmt, ...)
125*7c478bd9Sstevel@tonic-gate {
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	auto char name[256];
128*7c478bd9Sstevel@tonic-gate 	va_list	ap;
129*7c478bd9Sstevel@tonic-gate 	int	log_only = 0;
130*7c478bd9Sstevel@tonic-gate 	int	boot_only = 0;
131*7c478bd9Sstevel@tonic-gate 	int	console_only = 0;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	mutex_enter(&dcd_log_mutex);
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if (dev) {
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 		if (level == CE_PANIC || level == CE_WARN) {
140*7c478bd9Sstevel@tonic-gate 			(void) sprintf(name, "%s (%s%d):\n",
141*7c478bd9Sstevel@tonic-gate 				ddi_pathname(dev, dcd_log_buffer), label,
142*7c478bd9Sstevel@tonic-gate 				ddi_get_instance(dev));
143*7c478bd9Sstevel@tonic-gate 		} else if (level == CE_NOTE ||
144*7c478bd9Sstevel@tonic-gate 			level >= (uint_t)DCD_DEBUG) {
145*7c478bd9Sstevel@tonic-gate 			(void) sprintf(name, "%s%d:", label,
146*7c478bd9Sstevel@tonic-gate 						ddi_get_instance(dev));
147*7c478bd9Sstevel@tonic-gate 		} else if (level == CE_CONT) {
148*7c478bd9Sstevel@tonic-gate 			name[0] = '\0';
149*7c478bd9Sstevel@tonic-gate 		}
150*7c478bd9Sstevel@tonic-gate 	} else {
151*7c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "%s:", label);
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
156*7c478bd9Sstevel@tonic-gate 	(void) vsprintf(dcd_log_buffer, fmt, ap);
157*7c478bd9Sstevel@tonic-gate 	va_end(ap);
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	switch (dcd_log_buffer[0]) {
161*7c478bd9Sstevel@tonic-gate 	case '!':
162*7c478bd9Sstevel@tonic-gate 		log_only = 1;
163*7c478bd9Sstevel@tonic-gate 		break;
164*7c478bd9Sstevel@tonic-gate 	case '?':
165*7c478bd9Sstevel@tonic-gate 		boot_only = 1;
166*7c478bd9Sstevel@tonic-gate 		break;
167*7c478bd9Sstevel@tonic-gate 	case '^':
168*7c478bd9Sstevel@tonic-gate 		console_only = 1;
169*7c478bd9Sstevel@tonic-gate 		break;
170*7c478bd9Sstevel@tonic-gate 	}
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	switch (level) {
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	case CE_NOTE:
175*7c478bd9Sstevel@tonic-gate 		level = CE_CONT;
176*7c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
177*7c478bd9Sstevel@tonic-gate 	case CE_CONT:
178*7c478bd9Sstevel@tonic-gate 	case CE_WARN:
179*7c478bd9Sstevel@tonic-gate 	case CE_PANIC:
180*7c478bd9Sstevel@tonic-gate 		if (boot_only) {
181*7c478bd9Sstevel@tonic-gate 			cmn_err(level, "?%s\t%s", name,
182*7c478bd9Sstevel@tonic-gate 				&dcd_log_buffer[1]);
183*7c478bd9Sstevel@tonic-gate 		} else if (console_only) {
184*7c478bd9Sstevel@tonic-gate 			cmn_err(level, "^%s\t%s", name,
185*7c478bd9Sstevel@tonic-gate 				&dcd_log_buffer[1]);
186*7c478bd9Sstevel@tonic-gate 		} else if (log_only) {
187*7c478bd9Sstevel@tonic-gate 			cmn_err(level, "!%s\t%s", name,
188*7c478bd9Sstevel@tonic-gate 				&dcd_log_buffer[1]);
189*7c478bd9Sstevel@tonic-gate 		} else {
190*7c478bd9Sstevel@tonic-gate 			cmn_err(level, "%s\t%s", name,
191*7c478bd9Sstevel@tonic-gate 				dcd_log_buffer);
192*7c478bd9Sstevel@tonic-gate 		}
193*7c478bd9Sstevel@tonic-gate 		break;
194*7c478bd9Sstevel@tonic-gate 	case (uint_t)DCD_DEBUG:
195*7c478bd9Sstevel@tonic-gate 	default:
196*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "^DEBUG: %s\t%s\n", name,
197*7c478bd9Sstevel@tonic-gate 				dcd_log_buffer);
198*7c478bd9Sstevel@tonic-gate 		break;
199*7c478bd9Sstevel@tonic-gate 	}
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	mutex_exit(&dcd_log_mutex);
202*7c478bd9Sstevel@tonic-gate }
203