vserver 1.9.5.x5
[linux-2.6.git] / drivers / scsi / sym53c8xx_2 / sym_malloc.c
1 /*
2  * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family 
3  * of PCI-SCSI IO processors.
4  *
5  * Copyright (C) 1999-2001  Gerard Roudier <groudier@free.fr>
6  *
7  * This driver is derived from the Linux sym53c8xx driver.
8  * Copyright (C) 1998-2000  Gerard Roudier
9  *
10  * The sym53c8xx driver is derived from the ncr53c8xx driver that had been 
11  * a port of the FreeBSD ncr driver to Linux-1.2.13.
12  *
13  * The original ncr driver has been written for 386bsd and FreeBSD by
14  *         Wolfgang Stanglmeier        <wolf@cologne.de>
15  *         Stefan Esser                <se@mi.Uni-Koeln.de>
16  * Copyright (C) 1994  Wolfgang Stanglmeier
17  *
18  * Other major contributions:
19  *
20  * NVRAM detection and reading.
21  * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
22  *
23  *-----------------------------------------------------------------------------
24  *
25  * This program is free software; you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation; either version 2 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program; if not, write to the Free Software
37  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38  */
39
40 #ifdef __FreeBSD__
41 #include <dev/sym/sym_glue.h>
42 #else
43 #include "sym_glue.h"
44 #endif
45
46 /*
47  *  Simple power of two buddy-like generic allocator.
48  *  Provides naturally aligned memory chunks.
49  *
50  *  This simple code is not intended to be fast, but to 
51  *  provide power of 2 aligned memory allocations.
52  *  Since the SCRIPTS processor only supplies 8 bit arithmetic, 
53  *  this allocator allows simple and fast address calculations  
54  *  from the SCRIPTS code. In addition, cache line alignment 
55  *  is guaranteed for power of 2 cache line size.
56  *
57  *  This allocator has been developped for the Linux sym53c8xx  
58  *  driver, since this O/S does not provide naturally aligned 
59  *  allocations.
60  *  It has the advantage of allowing the driver to use private 
61  *  pages of memory that will be useful if we ever need to deal 
62  *  with IO MMUs for PCI.
63  */
64 static void *___sym_malloc(m_pool_p mp, int size)
65 {
66         int i = 0;
67         int s = (1 << SYM_MEM_SHIFT);
68         int j;
69         m_addr_t a;
70         m_link_p h = mp->h;
71
72         if (size > SYM_MEM_CLUSTER_SIZE)
73                 return NULL;
74
75         while (size > s) {
76                 s <<= 1;
77                 ++i;
78         }
79
80         j = i;
81         while (!h[j].next) {
82                 if (s == SYM_MEM_CLUSTER_SIZE) {
83                         h[j].next = (m_link_p) M_GET_MEM_CLUSTER();
84                         if (h[j].next)
85                                 h[j].next->next = NULL;
86                         break;
87                 }
88                 ++j;
89                 s <<= 1;
90         }
91         a = (m_addr_t) h[j].next;
92         if (a) {
93                 h[j].next = h[j].next->next;
94                 while (j > i) {
95                         j -= 1;
96                         s >>= 1;
97                         h[j].next = (m_link_p) (a+s);
98                         h[j].next->next = NULL;
99                 }
100         }
101 #ifdef DEBUG
102         printf("___sym_malloc(%d) = %p\n", size, (void *) a);
103 #endif
104         return (void *) a;
105 }
106
107 /*
108  *  Counter-part of the generic allocator.
109  */
110 static void ___sym_mfree(m_pool_p mp, void *ptr, int size)
111 {
112         int i = 0;
113         int s = (1 << SYM_MEM_SHIFT);
114         m_link_p q;
115         m_addr_t a, b;
116         m_link_p h = mp->h;
117
118 #ifdef DEBUG
119         printf("___sym_mfree(%p, %d)\n", ptr, size);
120 #endif
121
122         if (size > SYM_MEM_CLUSTER_SIZE)
123                 return;
124
125         while (size > s) {
126                 s <<= 1;
127                 ++i;
128         }
129
130         a = (m_addr_t) ptr;
131
132         while (1) {
133                 if (s == SYM_MEM_CLUSTER_SIZE) {
134 #ifdef SYM_MEM_FREE_UNUSED
135                         M_FREE_MEM_CLUSTER(a);
136 #else
137                         ((m_link_p) a)->next = h[i].next;
138                         h[i].next = (m_link_p) a;
139 #endif
140                         break;
141                 }
142                 b = a ^ s;
143                 q = &h[i];
144                 while (q->next && q->next != (m_link_p) b) {
145                         q = q->next;
146                 }
147                 if (!q->next) {
148                         ((m_link_p) a)->next = h[i].next;
149                         h[i].next = (m_link_p) a;
150                         break;
151                 }
152                 q->next = q->next->next;
153                 a = a & b;
154                 s <<= 1;
155                 ++i;
156         }
157 }
158
159 /*
160  *  Verbose and zeroing allocator that wrapps to the generic allocator.
161  */
162 static void *__sym_calloc2(m_pool_p mp, int size, char *name, int uflags)
163 {
164         void *p;
165
166         p = ___sym_malloc(mp, size);
167
168         if (DEBUG_FLAGS & DEBUG_ALLOC) {
169                 printf ("new %-10s[%4d] @%p.\n", name, size, p);
170         }
171
172         if (p)
173                 memset(p, 0, size);
174         else if (uflags & SYM_MEM_WARN)
175                 printf ("__sym_calloc2: failed to allocate %s[%d]\n", name, size);
176         return p;
177 }
178 #define __sym_calloc(mp, s, n)  __sym_calloc2(mp, s, n, SYM_MEM_WARN)
179
180 /*
181  *  Its counter-part.
182  */
183 static void __sym_mfree(m_pool_p mp, void *ptr, int size, char *name)
184 {
185         if (DEBUG_FLAGS & DEBUG_ALLOC)
186                 printf ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
187
188         ___sym_mfree(mp, ptr, size);
189 }
190
191 /*
192  *  Default memory pool we donnot need to involve in DMA.
193  *
194  *  With DMA abstraction, we use functions (methods), to 
195  *  distinguish between non DMAable memory and DMAable memory.
196  */
197 static m_addr_t ___mp0_get_mem_cluster(m_pool_p mp)
198 {
199         m_addr_t m = (m_addr_t) sym_get_mem_cluster();
200         if (m)
201                 ++mp->nump;
202         return m;
203 }
204
205 #ifdef  SYM_MEM_FREE_UNUSED
206 static void ___mp0_free_mem_cluster(m_pool_p mp, m_addr_t m)
207 {
208         sym_free_mem_cluster(m);
209         --mp->nump;
210 }
211 #endif
212
213 #ifdef  SYM_MEM_FREE_UNUSED
214 static struct sym_m_pool mp0 =
215         {NULL, ___mp0_get_mem_cluster, ___mp0_free_mem_cluster};
216 #else
217 static struct sym_m_pool mp0 =
218         {NULL, ___mp0_get_mem_cluster};
219 #endif
220
221 /*
222  * Actual memory allocation routine for non-DMAed memory.
223  */
224 void *sym_calloc_unlocked(int size, char *name)
225 {
226         void *m;
227         m = __sym_calloc(&mp0, size, name);
228         return m;
229 }
230
231 /*
232  *  Its counter-part.
233  */
234 void sym_mfree_unlocked(void *ptr, int size, char *name)
235 {
236         __sym_mfree(&mp0, ptr, size, name);
237 }
238
239 /*
240  *  Methods that maintains DMAable pools according to user allocations.
241  *  New pools are created on the fly when a new pool id is provided.
242  *  They are deleted on the fly when they get emptied.
243  */
244 /* Get a memory cluster that matches the DMA contraints of a given pool */
245 static m_addr_t ___get_dma_mem_cluster(m_pool_p mp)
246 {
247         m_vtob_p vbp;
248         m_addr_t vaddr;
249
250         vbp = __sym_calloc(&mp0, sizeof(*vbp), "VTOB");
251         if (!vbp)
252                 goto out_err;
253
254         vaddr = sym_m_get_dma_mem_cluster(mp, vbp);
255         if (vaddr) {
256                 int hc = VTOB_HASH_CODE(vaddr);
257                 vbp->next = mp->vtob[hc];
258                 mp->vtob[hc] = vbp;
259                 ++mp->nump;
260                 return (m_addr_t) vaddr;
261         }
262         return vaddr;
263 out_err:
264         return 0;
265 }
266
267 #ifdef  SYM_MEM_FREE_UNUSED
268 /* Free a memory cluster and associated resources for DMA */
269 static void ___free_dma_mem_cluster(m_pool_p mp, m_addr_t m)
270 {
271         m_vtob_p *vbpp, vbp;
272         int hc = VTOB_HASH_CODE(m);
273
274         vbpp = &mp->vtob[hc];
275         while (*vbpp && (*vbpp)->vaddr != m)
276                 vbpp = &(*vbpp)->next;
277         if (*vbpp) {
278                 vbp = *vbpp;
279                 *vbpp = (*vbpp)->next;
280                 sym_m_free_dma_mem_cluster(mp, vbp);
281                 __sym_mfree(&mp0, vbp, sizeof(*vbp), "VTOB");
282                 --mp->nump;
283         }
284 }
285 #endif
286
287 /* Fetch the memory pool for a given pool id (i.e. DMA constraints) */
288 static __inline m_pool_p ___get_dma_pool(m_pool_ident_t dev_dmat)
289 {
290         m_pool_p mp;
291         for (mp = mp0.next;
292                 mp && !sym_m_pool_match(mp->dev_dmat, dev_dmat);
293                         mp = mp->next);
294         return mp;
295 }
296
297 /* Create a new memory DMAable pool (when fetch failed) */
298 static m_pool_p ___cre_dma_pool(m_pool_ident_t dev_dmat)
299 {
300         m_pool_p mp = NULL;
301
302         mp = __sym_calloc(&mp0, sizeof(*mp), "MPOOL");
303         if (mp) {
304                 mp->dev_dmat = dev_dmat;
305                 if (!sym_m_create_dma_mem_tag(mp)) {
306                         mp->get_mem_cluster = ___get_dma_mem_cluster;
307 #ifdef  SYM_MEM_FREE_UNUSED
308                         mp->free_mem_cluster = ___free_dma_mem_cluster;
309 #endif
310                         mp->next = mp0.next;
311                         mp0.next = mp;
312                         return mp;
313                 }
314         }
315         if (mp)
316                 __sym_mfree(&mp0, mp, sizeof(*mp), "MPOOL");
317         return NULL;
318 }
319
320 #ifdef  SYM_MEM_FREE_UNUSED
321 /* Destroy a DMAable memory pool (when got emptied) */
322 static void ___del_dma_pool(m_pool_p p)
323 {
324         m_pool_p *pp = &mp0.next;
325
326         while (*pp && *pp != p)
327                 pp = &(*pp)->next;
328         if (*pp) {
329                 *pp = (*pp)->next;
330                 sym_m_delete_dma_mem_tag(p);
331                 __sym_mfree(&mp0, p, sizeof(*p), "MPOOL");
332         }
333 }
334 #endif
335
336 /*
337  *  Actual allocator for DMAable memory.
338  */
339 void *__sym_calloc_dma_unlocked(m_pool_ident_t dev_dmat, int size, char *name)
340 {
341         m_pool_p mp;
342         void *m = NULL;
343
344         mp = ___get_dma_pool(dev_dmat);
345         if (!mp)
346                 mp = ___cre_dma_pool(dev_dmat);
347         if (mp)
348                 m = __sym_calloc(mp, size, name);
349 #ifdef  SYM_MEM_FREE_UNUSED
350         if (mp && !mp->nump)
351                 ___del_dma_pool(mp);
352 #endif
353
354         return m;
355 }
356
357 /*
358  *  Its counter-part.
359  */
360 void 
361 __sym_mfree_dma_unlocked(m_pool_ident_t dev_dmat, void *m, int size, char *name)
362 {
363         m_pool_p mp;
364
365         mp = ___get_dma_pool(dev_dmat);
366         if (mp)
367                 __sym_mfree(mp, m, size, name);
368 #ifdef  SYM_MEM_FREE_UNUSED
369         if (mp && !mp->nump)
370                 ___del_dma_pool(mp);
371 #endif
372 }
373
374 /*
375  *  Actual virtual to bus physical address translator 
376  *  for 32 bit addressable DMAable memory.
377  */
378 u32 __vtobus_unlocked(m_pool_ident_t dev_dmat, void *m)
379 {
380         m_pool_p mp;
381         int hc = VTOB_HASH_CODE(m);
382         m_vtob_p vp = NULL;
383         m_addr_t a = ((m_addr_t) m) & ~SYM_MEM_CLUSTER_MASK;
384
385         mp = ___get_dma_pool(dev_dmat);
386         if (mp) {
387                 vp = mp->vtob[hc];
388                 while (vp && (m_addr_t) vp->vaddr != a)
389                         vp = vp->next;
390         }
391         if (!vp)
392                 panic("sym: VTOBUS FAILED!\n");
393         return (u32)(vp ? vp->baddr + (((m_addr_t) m) - a) : 0);
394 }