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 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. The name of the author may not be used to endorse or promote products
31 * derived from this software without specific prior written permission.
33 * Where this Software is combined with software released under the terms of
34 * the GNU Public License ("GPL") and the terms of the GPL would require the
35 * combined work to also be released under the terms of the GPL, the terms
36 * and conditions of this License will apply in addition to those of the
37 * GPL with the exception of any terms or conditions of this License that
38 * conflict with, or are expressly prohibited by, the GPL.
40 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
44 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 #include <dev/sym/sym_glue.h>
60 * Simple power of two buddy-like generic allocator.
61 * Provides naturally aligned memory chunks.
63 * This simple code is not intended to be fast, but to
64 * provide power of 2 aligned memory allocations.
65 * Since the SCRIPTS processor only supplies 8 bit arithmetic,
66 * this allocator allows simple and fast address calculations
67 * from the SCRIPTS code. In addition, cache line alignment
68 * is guaranteed for power of 2 cache line size.
70 * This allocator has been developped for the Linux sym53c8xx
71 * driver, since this O/S does not provide naturally aligned
73 * It has the advantage of allowing the driver to use private
74 * pages of memory that will be useful if we ever need to deal
75 * with IO MMUs for PCI.
77 static void *___sym_malloc(m_pool_p mp
, int size
)
80 int s
= (1 << SYM_MEM_SHIFT
);
85 if (size
> SYM_MEM_CLUSTER_SIZE
)
95 if (s
== SYM_MEM_CLUSTER_SIZE
) {
96 h
[j
].next
= (m_link_p
) M_GET_MEM_CLUSTER();
98 h
[j
].next
->next
= NULL
;
104 a
= (m_addr_t
) h
[j
].next
;
106 h
[j
].next
= h
[j
].next
->next
;
110 h
[j
].next
= (m_link_p
) (a
+s
);
111 h
[j
].next
->next
= NULL
;
115 printf("___sym_malloc(%d) = %p\n", size
, (void *) a
);
121 * Counter-part of the generic allocator.
123 static void ___sym_mfree(m_pool_p mp
, void *ptr
, int size
)
126 int s
= (1 << SYM_MEM_SHIFT
);
132 printf("___sym_mfree(%p, %d)\n", ptr
, size
);
135 if (size
> SYM_MEM_CLUSTER_SIZE
)
146 if (s
== SYM_MEM_CLUSTER_SIZE
) {
147 #ifdef SYM_MEM_FREE_UNUSED
148 M_FREE_MEM_CLUSTER(a
);
150 ((m_link_p
) a
)->next
= h
[i
].next
;
151 h
[i
].next
= (m_link_p
) a
;
157 while (q
->next
&& q
->next
!= (m_link_p
) b
) {
161 ((m_link_p
) a
)->next
= h
[i
].next
;
162 h
[i
].next
= (m_link_p
) a
;
165 q
->next
= q
->next
->next
;
173 * Verbose and zeroing allocator that wrapps to the generic allocator.
175 static void *__sym_calloc2(m_pool_p mp
, int size
, char *name
, int uflags
)
179 p
= ___sym_malloc(mp
, size
);
181 if (DEBUG_FLAGS
& DEBUG_ALLOC
) {
182 printf ("new %-10s[%4d] @%p.\n", name
, size
, p
);
187 else if (uflags
& SYM_MEM_WARN
)
188 printf ("__sym_calloc2: failed to allocate %s[%d]\n", name
, size
);
191 #define __sym_calloc(mp, s, n) __sym_calloc2(mp, s, n, SYM_MEM_WARN)
196 static void __sym_mfree(m_pool_p mp
, void *ptr
, int size
, char *name
)
198 if (DEBUG_FLAGS
& DEBUG_ALLOC
)
199 printf ("freeing %-10s[%4d] @%p.\n", name
, size
, ptr
);
201 ___sym_mfree(mp
, ptr
, size
);
205 * Default memory pool we donnot need to involve in DMA.
207 * With DMA abstraction, we use functions (methods), to
208 * distinguish between non DMAable memory and DMAable memory.
210 static m_addr_t
___mp0_get_mem_cluster(m_pool_p mp
)
212 m_addr_t m
= (m_addr_t
) sym_get_mem_cluster();
218 #ifdef SYM_MEM_FREE_UNUSED
219 static void ___mp0_free_mem_cluster(m_pool_p mp
, m_addr_t m
)
221 sym_free_mem_cluster(m
);
226 #ifdef SYM_MEM_FREE_UNUSED
227 static struct sym_m_pool mp0
=
228 {NULL
, ___mp0_get_mem_cluster
, ___mp0_free_mem_cluster
};
230 static struct sym_m_pool mp0
=
231 {NULL
, ___mp0_get_mem_cluster
};
235 * Actual memory allocation routine for non-DMAed memory.
237 void *sym_calloc_unlocked(int size
, char *name
)
240 m
= __sym_calloc(&mp0
, size
, name
);
247 void sym_mfree_unlocked(void *ptr
, int size
, char *name
)
249 __sym_mfree(&mp0
, ptr
, size
, name
);
253 * Methods that maintains DMAable pools according to user allocations.
254 * New pools are created on the fly when a new pool id is provided.
255 * They are deleted on the fly when they get emptied.
257 /* Get a memory cluster that matches the DMA contraints of a given pool */
258 static m_addr_t
___get_dma_mem_cluster(m_pool_p mp
)
263 vbp
= __sym_calloc(&mp0
, sizeof(*vbp
), "VTOB");
267 vaddr
= sym_m_get_dma_mem_cluster(mp
, vbp
);
269 int hc
= VTOB_HASH_CODE(vaddr
);
270 vbp
->next
= mp
->vtob
[hc
];
273 return (m_addr_t
) vaddr
;
280 #ifdef SYM_MEM_FREE_UNUSED
281 /* Free a memory cluster and associated resources for DMA */
282 static void ___free_dma_mem_cluster(m_pool_p mp
, m_addr_t m
)
285 int hc
= VTOB_HASH_CODE(m
);
287 vbpp
= &mp
->vtob
[hc
];
288 while (*vbpp
&& (*vbpp
)->vaddr
!= m
)
289 vbpp
= &(*vbpp
)->next
;
292 *vbpp
= (*vbpp
)->next
;
293 sym_m_free_dma_mem_cluster(mp
, vbp
);
294 __sym_mfree(&mp0
, vbp
, sizeof(*vbp
), "VTOB");
300 /* Fetch the memory pool for a given pool id (i.e. DMA constraints) */
301 static __inline m_pool_p
___get_dma_pool(m_pool_ident_t dev_dmat
)
305 mp
&& !sym_m_pool_match(mp
->dev_dmat
, dev_dmat
);
310 /* Create a new memory DMAable pool (when fetch failed) */
311 static m_pool_p
___cre_dma_pool(m_pool_ident_t dev_dmat
)
315 mp
= __sym_calloc(&mp0
, sizeof(*mp
), "MPOOL");
317 mp
->dev_dmat
= dev_dmat
;
318 if (!sym_m_create_dma_mem_tag(mp
)) {
319 mp
->get_mem_cluster
= ___get_dma_mem_cluster
;
320 #ifdef SYM_MEM_FREE_UNUSED
321 mp
->free_mem_cluster
= ___free_dma_mem_cluster
;
329 __sym_mfree(&mp0
, mp
, sizeof(*mp
), "MPOOL");
333 #ifdef SYM_MEM_FREE_UNUSED
334 /* Destroy a DMAable memory pool (when got emptied) */
335 static void ___del_dma_pool(m_pool_p p
)
337 m_pool_p
*pp
= &mp0
.next
;
339 while (*pp
&& *pp
!= p
)
343 sym_m_delete_dma_mem_tag(p
);
344 __sym_mfree(&mp0
, p
, sizeof(*p
), "MPOOL");
350 * Actual allocator for DMAable memory.
352 void *__sym_calloc_dma_unlocked(m_pool_ident_t dev_dmat
, int size
, char *name
)
357 mp
= ___get_dma_pool(dev_dmat
);
359 mp
= ___cre_dma_pool(dev_dmat
);
361 m
= __sym_calloc(mp
, size
, name
);
362 #ifdef SYM_MEM_FREE_UNUSED
374 __sym_mfree_dma_unlocked(m_pool_ident_t dev_dmat
, void *m
, int size
, char *name
)
378 mp
= ___get_dma_pool(dev_dmat
);
380 __sym_mfree(mp
, m
, size
, name
);
381 #ifdef SYM_MEM_FREE_UNUSED
388 * Actual virtual to bus physical address translator
389 * for 32 bit addressable DMAable memory.
391 u32
__vtobus_unlocked(m_pool_ident_t dev_dmat
, void *m
)
394 int hc
= VTOB_HASH_CODE(m
);
396 m_addr_t a
= ((m_addr_t
) m
) & ~SYM_MEM_CLUSTER_MASK
;
398 mp
= ___get_dma_pool(dev_dmat
);
401 while (vp
&& (m_addr_t
) vp
->vaddr
!= a
)
405 panic("sym: VTOBUS FAILED!\n");
406 return (u32
)(vp
? vp
->baddr
+ (((m_addr_t
) m
) - a
) : 0);