2 * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
3 * of PCI-SCSI IO processors.
5 * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
7 * This driver is derived from the Linux sym53c8xx driver.
8 * Copyright (C) 1998-2000 Gerard Roudier
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.
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
18 * Other major contributions:
20 * NVRAM detection and reading.
21 * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
23 *-----------------------------------------------------------------------------
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.
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.
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
43 * Simple power of two buddy-like generic allocator.
44 * Provides naturally aligned memory chunks.
46 * This simple code is not intended to be fast, but to
47 * provide power of 2 aligned memory allocations.
48 * Since the SCRIPTS processor only supplies 8 bit arithmetic,
49 * this allocator allows simple and fast address calculations
50 * from the SCRIPTS code. In addition, cache line alignment
51 * is guaranteed for power of 2 cache line size.
53 * This allocator has been developped for the Linux sym53c8xx
54 * driver, since this O/S does not provide naturally aligned
56 * It has the advantage of allowing the driver to use private
57 * pages of memory that will be useful if we ever need to deal
58 * with IO MMUs for PCI.
60 static void *___sym_malloc(m_pool_p mp
, int size
)
63 int s
= (1 << SYM_MEM_SHIFT
);
68 if (size
> SYM_MEM_CLUSTER_SIZE
)
78 if (s
== SYM_MEM_CLUSTER_SIZE
) {
79 h
[j
].next
= (m_link_p
) M_GET_MEM_CLUSTER();
81 h
[j
].next
->next
= NULL
;
89 h
[j
].next
= h
[j
].next
->next
;
93 h
[j
].next
= (m_link_p
) (a
+s
);
94 h
[j
].next
->next
= NULL
;
98 printf("___sym_malloc(%d) = %p\n", size
, (void *) a
);
104 * Counter-part of the generic allocator.
106 static void ___sym_mfree(m_pool_p mp
, void *ptr
, int size
)
109 int s
= (1 << SYM_MEM_SHIFT
);
115 printf("___sym_mfree(%p, %d)\n", ptr
, size
);
118 if (size
> SYM_MEM_CLUSTER_SIZE
)
126 a
= (unsigned long)ptr
;
129 if (s
== SYM_MEM_CLUSTER_SIZE
) {
130 #ifdef SYM_MEM_FREE_UNUSED
131 M_FREE_MEM_CLUSTER((void *)a
);
133 ((m_link_p
) a
)->next
= h
[i
].next
;
134 h
[i
].next
= (m_link_p
) a
;
140 while (q
->next
&& q
->next
!= (m_link_p
) b
) {
144 ((m_link_p
) a
)->next
= h
[i
].next
;
145 h
[i
].next
= (m_link_p
) a
;
148 q
->next
= q
->next
->next
;
156 * Verbose and zeroing allocator that wrapps to the generic allocator.
158 static void *__sym_calloc2(m_pool_p mp
, int size
, char *name
, int uflags
)
162 p
= ___sym_malloc(mp
, size
);
164 if (DEBUG_FLAGS
& DEBUG_ALLOC
) {
165 printf ("new %-10s[%4d] @%p.\n", name
, size
, p
);
170 else if (uflags
& SYM_MEM_WARN
)
171 printf ("__sym_calloc2: failed to allocate %s[%d]\n", name
, size
);
174 #define __sym_calloc(mp, s, n) __sym_calloc2(mp, s, n, SYM_MEM_WARN)
179 static void __sym_mfree(m_pool_p mp
, void *ptr
, int size
, char *name
)
181 if (DEBUG_FLAGS
& DEBUG_ALLOC
)
182 printf ("freeing %-10s[%4d] @%p.\n", name
, size
, ptr
);
184 ___sym_mfree(mp
, ptr
, size
);
188 * Default memory pool we donnot need to involve in DMA.
190 * With DMA abstraction, we use functions (methods), to
191 * distinguish between non DMAable memory and DMAable memory.
193 static void *___mp0_get_mem_cluster(m_pool_p mp
)
195 void *m
= sym_get_mem_cluster();
201 #ifdef SYM_MEM_FREE_UNUSED
202 static void ___mp0_free_mem_cluster(m_pool_p mp
, void *m
)
204 sym_free_mem_cluster(m
);
208 #define ___mp0_free_mem_cluster NULL
211 static struct sym_m_pool mp0
= {
213 ___mp0_get_mem_cluster
,
214 ___mp0_free_mem_cluster
218 * Methods that maintains DMAable pools according to user allocations.
219 * New pools are created on the fly when a new pool id is provided.
220 * They are deleted on the fly when they get emptied.
222 /* Get a memory cluster that matches the DMA constraints of a given pool */
223 static void * ___get_dma_mem_cluster(m_pool_p mp
)
228 vbp
= __sym_calloc(&mp0
, sizeof(*vbp
), "VTOB");
232 vaddr
= sym_m_get_dma_mem_cluster(mp
, vbp
);
234 int hc
= VTOB_HASH_CODE(vaddr
);
235 vbp
->next
= mp
->vtob
[hc
];
244 #ifdef SYM_MEM_FREE_UNUSED
245 /* Free a memory cluster and associated resources for DMA */
246 static void ___free_dma_mem_cluster(m_pool_p mp
, void *m
)
249 int hc
= VTOB_HASH_CODE(m
);
251 vbpp
= &mp
->vtob
[hc
];
252 while (*vbpp
&& (*vbpp
)->vaddr
!= m
)
253 vbpp
= &(*vbpp
)->next
;
256 *vbpp
= (*vbpp
)->next
;
257 sym_m_free_dma_mem_cluster(mp
, vbp
);
258 __sym_mfree(&mp0
, vbp
, sizeof(*vbp
), "VTOB");
264 /* Fetch the memory pool for a given pool id (i.e. DMA constraints) */
265 static inline m_pool_p
___get_dma_pool(m_pool_ident_t dev_dmat
)
269 mp
&& !sym_m_pool_match(mp
->dev_dmat
, dev_dmat
);
274 /* Create a new memory DMAable pool (when fetch failed) */
275 static m_pool_p
___cre_dma_pool(m_pool_ident_t dev_dmat
)
277 m_pool_p mp
= __sym_calloc(&mp0
, sizeof(*mp
), "MPOOL");
279 mp
->dev_dmat
= dev_dmat
;
280 mp
->get_mem_cluster
= ___get_dma_mem_cluster
;
281 #ifdef SYM_MEM_FREE_UNUSED
282 mp
->free_mem_cluster
= ___free_dma_mem_cluster
;
291 #ifdef SYM_MEM_FREE_UNUSED
292 /* Destroy a DMAable memory pool (when got emptied) */
293 static void ___del_dma_pool(m_pool_p p
)
295 m_pool_p
*pp
= &mp0
.next
;
297 while (*pp
&& *pp
!= p
)
301 __sym_mfree(&mp0
, p
, sizeof(*p
), "MPOOL");
306 /* This lock protects only the memory allocation/free. */
307 static DEFINE_SPINLOCK(sym53c8xx_lock
);
310 * Actual allocator for DMAable memory.
312 void *__sym_calloc_dma(m_pool_ident_t dev_dmat
, int size
, char *name
)
318 spin_lock_irqsave(&sym53c8xx_lock
, flags
);
319 mp
= ___get_dma_pool(dev_dmat
);
321 mp
= ___cre_dma_pool(dev_dmat
);
324 m
= __sym_calloc(mp
, size
, name
);
325 #ifdef SYM_MEM_FREE_UNUSED
331 spin_unlock_irqrestore(&sym53c8xx_lock
, flags
);
335 void __sym_mfree_dma(m_pool_ident_t dev_dmat
, void *m
, int size
, char *name
)
340 spin_lock_irqsave(&sym53c8xx_lock
, flags
);
341 mp
= ___get_dma_pool(dev_dmat
);
344 __sym_mfree(mp
, m
, size
, name
);
345 #ifdef SYM_MEM_FREE_UNUSED
350 spin_unlock_irqrestore(&sym53c8xx_lock
, flags
);
354 * Actual virtual to bus physical address translator
355 * for 32 bit addressable DMAable memory.
357 dma_addr_t
__vtobus(m_pool_ident_t dev_dmat
, void *m
)
361 int hc
= VTOB_HASH_CODE(m
);
363 void *a
= (void *)((unsigned long)m
& ~SYM_MEM_CLUSTER_MASK
);
366 spin_lock_irqsave(&sym53c8xx_lock
, flags
);
367 mp
= ___get_dma_pool(dev_dmat
);
370 while (vp
&& vp
->vaddr
!= a
)
374 panic("sym: VTOBUS FAILED!\n");
375 b
= vp
->baddr
+ (m
- a
);
376 spin_unlock_irqrestore(&sym53c8xx_lock
, flags
);