xref: /onnv-gate/usr/src/uts/common/io/ath/ath_osdep.c (revision 1000:dd54117d55b1)
1*1000Sxc151355 /*
2*1000Sxc151355  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*1000Sxc151355  * Use is subject to license terms.
4*1000Sxc151355  */
5*1000Sxc151355 
6*1000Sxc151355 /*
7*1000Sxc151355  * Redistribution and use in source and binary forms, with or without
8*1000Sxc151355  * modification, are permitted provided that the following conditions
9*1000Sxc151355  * are met:
10*1000Sxc151355  * 1. Redistributions of source code must retain the above copyright
11*1000Sxc151355  * notice, this list of conditions and the following disclaimer,
12*1000Sxc151355  * without modification.
13*1000Sxc151355  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14*1000Sxc151355  * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15*1000Sxc151355  * redistribution must be conditioned upon including a substantially
16*1000Sxc151355  * similar Disclaimer requirement for further binary redistribution.
17*1000Sxc151355  * 3. Neither the names of the above-listed copyright holders nor the names
18*1000Sxc151355  * of any contributors may be used to endorse or promote products derived
19*1000Sxc151355  * from this software without specific prior written permission.
20*1000Sxc151355  *
21*1000Sxc151355  * NO WARRANTY
22*1000Sxc151355  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23*1000Sxc151355  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24*1000Sxc151355  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
25*1000Sxc151355  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26*1000Sxc151355  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
27*1000Sxc151355  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28*1000Sxc151355  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29*1000Sxc151355  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
30*1000Sxc151355  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31*1000Sxc151355  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32*1000Sxc151355  * THE POSSIBILITY OF SUCH DAMAGES.
33*1000Sxc151355  *
34*1000Sxc151355  */
35*1000Sxc151355 
36*1000Sxc151355 #pragma ident	"%Z%%M%	%I%	%E% SMI"
37*1000Sxc151355 
38*1000Sxc151355 #include <sys/param.h>
39*1000Sxc151355 #include <sys/types.h>
40*1000Sxc151355 #include <sys/cmn_err.h>
41*1000Sxc151355 #include <sys/kmem.h>
42*1000Sxc151355 #include <sys/ddi.h>
43*1000Sxc151355 #include <sys/sunddi.h>
44*1000Sxc151355 #include <sys/varargs.h>
45*1000Sxc151355 #include "ath_hal.h"
46*1000Sxc151355 #include "ath_ieee80211.h"
47*1000Sxc151355 #include "ath_impl.h"
48*1000Sxc151355 
49*1000Sxc151355 struct ath_halfix {
50*1000Sxc151355 	void *p;
51*1000Sxc151355 	size_t size;
52*1000Sxc151355 	int malloced;
53*1000Sxc151355 	int freed;
54*1000Sxc151355 };
55*1000Sxc151355 
56*1000Sxc151355 static struct ath_halfix ath_halfix[32];
57*1000Sxc151355 
58*1000Sxc151355 /* HAL layer needs these definitions */
59*1000Sxc151355 int ath_hal_dma_beacon_response_time = 2;	/* in TU's */
60*1000Sxc151355 int ath_hal_sw_beacon_response_time = 10;	/* in TU's */
61*1000Sxc151355 int ath_hal_additional_swba_backoff = 0;	/* in TU's */
62*1000Sxc151355 
63*1000Sxc151355 /*
64*1000Sxc151355  * Print/log message support.
65*1000Sxc151355  */
66*1000Sxc151355 
67*1000Sxc151355 void
68*1000Sxc151355 ath_hal_printf(struct ath_hal *ah, const char *fmt, ...)
69*1000Sxc151355 {
70*1000Sxc151355 	va_list ap;
71*1000Sxc151355 
72*1000Sxc151355 	_NOTE(ARGUNUSED(ah))
73*1000Sxc151355 	va_start(ap, fmt);
74*1000Sxc151355 	vcmn_err(CE_CONT, fmt, ap);
75*1000Sxc151355 	va_end(ap);
76*1000Sxc151355 }
77*1000Sxc151355 
78*1000Sxc151355 /*
79*1000Sxc151355  * Delay n microseconds.
80*1000Sxc151355  */
81*1000Sxc151355 void
82*1000Sxc151355 ath_hal_delay(int n)
83*1000Sxc151355 {
84*1000Sxc151355 	drv_usecwait(n);
85*1000Sxc151355 }
86*1000Sxc151355 
87*1000Sxc151355 /*
88*1000Sxc151355  * ath_hal_malloc() and ath_hal_free() are called
89*1000Sxc151355  * within ath_hal.o. We must record the size of
90*1000Sxc151355  * the memory alloced, so ath_hal_free() can get
91*1000Sxc151355  * the size and then calls kmem_free().
92*1000Sxc151355  */
93*1000Sxc151355 void *
94*1000Sxc151355 ath_hal_malloc(size_t size)
95*1000Sxc151355 {
96*1000Sxc151355 	void *p;
97*1000Sxc151355 	int i;
98*1000Sxc151355 
99*1000Sxc151355 	/* support 16 devices(max leakage of one device is 8) */
100*1000Sxc151355 	for (i = 0; i < 32; i++) {
101*1000Sxc151355 		if (ath_halfix[i].malloced == 0)
102*1000Sxc151355 			break;
103*1000Sxc151355 	}
104*1000Sxc151355 	if (i >= 32) {
105*1000Sxc151355 		ath_problem("ath: ath_hal_malloc(): too many malloc\n");
106*1000Sxc151355 		return (NULL);
107*1000Sxc151355 	}
108*1000Sxc151355 	p = kmem_zalloc(size, KM_SLEEP);
109*1000Sxc151355 	ath_halfix[i].p = p;
110*1000Sxc151355 	ath_halfix[i].size = size;
111*1000Sxc151355 	ath_halfix[i].malloced = 1;
112*1000Sxc151355 	ath_halfix[i].freed = 0;
113*1000Sxc151355 	ATH_DEBUG((ATH_DBG_OSDEP, "ath: ath_hal_malloc(): "
114*1000Sxc151355 	    "%d: p=%p, size=%d\n", i, p, size));
115*1000Sxc151355 	return (p);
116*1000Sxc151355 }
117*1000Sxc151355 
118*1000Sxc151355 void
119*1000Sxc151355 ath_hal_free(void* p)
120*1000Sxc151355 {
121*1000Sxc151355 	int i;
122*1000Sxc151355 	for (i = 0; i < 32; i++) {
123*1000Sxc151355 		if (ath_halfix[i].p == p)
124*1000Sxc151355 			break;
125*1000Sxc151355 	}
126*1000Sxc151355 	if (i >= 32) {
127*1000Sxc151355 		ath_problem("ath: ath_hal_free(): no record for %p\n", p);
128*1000Sxc151355 		return;
129*1000Sxc151355 	}
130*1000Sxc151355 	kmem_free(p, ath_halfix[i].size);
131*1000Sxc151355 	ath_halfix[i].malloced = 0;
132*1000Sxc151355 	ath_halfix[i].freed = 1;
133*1000Sxc151355 	ATH_DEBUG((ATH_DBG_OSDEP, "ath: ath_hal_free(): %d: p=%p, size=%d\n",
134*1000Sxc151355 	    i, p, ath_halfix[i].size));
135*1000Sxc151355 }
136*1000Sxc151355 
137*1000Sxc151355 void *
138*1000Sxc151355 ath_hal_memcpy(void *dst, const void *src, size_t n)
139*1000Sxc151355 {
140*1000Sxc151355 	bcopy(src, dst, n);
141*1000Sxc151355 	return (dst);
142*1000Sxc151355 }
143*1000Sxc151355 
144*1000Sxc151355 void
145*1000Sxc151355 ath_hal_memzero(void *dst, size_t n)
146*1000Sxc151355 {
147*1000Sxc151355 	bzero(dst, n);
148*1000Sxc151355 }
149*1000Sxc151355 
150*1000Sxc151355 /*
151*1000Sxc151355  * So far as I know and test, hal.o has a bug that when attaching,
152*1000Sxc151355  * it calls ath_hal_malloc() four times while detaching it calls
153*1000Sxc151355  * ath_hal_free() only 3 times, so everytime when a pair of driver
154*1000Sxc151355  * load/unload is done, a memory leak occurs. The function
155*1000Sxc151355  * free_hal_leaked_mem() just help free the memory that alloced by
156*1000Sxc151355  * hal.o but not freed by it. In fact, when attaching, hal.o only
157*1000Sxc151355  * call ath_hal_alloc() four times, here assuming a maximum times of
158*1000Sxc151355  * 8 just considers some special cases, we have no source after all!
159*1000Sxc151355  */
160*1000Sxc151355 void
161*1000Sxc151355 ath_halfix_init(void)
162*1000Sxc151355 {
163*1000Sxc151355 	int i;
164*1000Sxc151355 
165*1000Sxc151355 	for (i = 0; i < 32; i++) {
166*1000Sxc151355 		ath_halfix[i].malloced = 0;
167*1000Sxc151355 	}
168*1000Sxc151355 }
169*1000Sxc151355 
170*1000Sxc151355 void
171*1000Sxc151355 ath_halfix_finit(void)
172*1000Sxc151355 {
173*1000Sxc151355 	int i;
174*1000Sxc151355 
175*1000Sxc151355 	for (i = 0; i < 32; i++) {
176*1000Sxc151355 		if ((ath_halfix[i].malloced == 1) &&
177*1000Sxc151355 		    (ath_halfix[i].freed == 0)) {
178*1000Sxc151355 			kmem_free(ath_halfix[i].p, ath_halfix[i].size);
179*1000Sxc151355 		}
180*1000Sxc151355 	}
181*1000Sxc151355 }
182