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