2 * Copyright (C) 1997 Claus-Justus Heine
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19 * $Source: /homes/cvs/ftape-stacked/ftape/lowlevel/ftape-buffer.c,v $
21 * $Date: 1997/10/16 23:33:11 $
23 * This file contains the allocator/dealloctor for ftape's dynamic dma
27 #include <linux/slab.h>
29 #include <linux/mman.h>
32 #include <linux/ftape.h>
33 #include "../lowlevel/ftape-rw.h"
34 #include "../lowlevel/ftape-read.h"
35 #include "../lowlevel/ftape-tracing.h"
37 /* DMA'able memory allocation stuff.
40 static inline void *dmaalloc(size_t size
)
47 addr
= __get_dma_pages(GFP_KERNEL
, get_order(size
));
51 for (page
= virt_to_page(addr
); page
< virt_to_page(addr
+size
); page
++)
52 SetPageReserved(page
);
57 static inline void dmafree(void *addr
, size_t size
)
62 for (page
= virt_to_page((unsigned long)addr
);
63 page
< virt_to_page((unsigned long)addr
+size
); page
++)
64 ClearPageReserved(page
);
65 free_pages((unsigned long) addr
, get_order(size
));
69 static int add_one_buffer(void)
73 if (ft_nr_buffers
>= FT_MAX_NR_BUFFERS
) {
76 ft_buffer
[ft_nr_buffers
] = kmalloc(sizeof(buffer_struct
), GFP_KERNEL
);
77 if (ft_buffer
[ft_nr_buffers
] == NULL
) {
80 memset(ft_buffer
[ft_nr_buffers
], 0, sizeof(buffer_struct
));
81 ft_buffer
[ft_nr_buffers
]->address
= dmaalloc(FT_BUFF_SIZE
);
82 if (ft_buffer
[ft_nr_buffers
]->address
== NULL
) {
83 kfree(ft_buffer
[ft_nr_buffers
]);
84 ft_buffer
[ft_nr_buffers
] = NULL
;
88 TRACE(ft_t_info
, "buffer nr #%d @ %p, dma area @ %p",
90 ft_buffer
[ft_nr_buffers
-1],
91 ft_buffer
[ft_nr_buffers
-1]->address
);
95 static void del_one_buffer(void)
98 if (ft_nr_buffers
> 0) {
99 TRACE(ft_t_info
, "releasing buffer nr #%d @ %p, dma area @ %p",
101 ft_buffer
[ft_nr_buffers
-1],
102 ft_buffer
[ft_nr_buffers
-1]->address
);
104 dmafree(ft_buffer
[ft_nr_buffers
]->address
, FT_BUFF_SIZE
);
105 kfree(ft_buffer
[ft_nr_buffers
]);
106 ft_buffer
[ft_nr_buffers
] = NULL
;
111 int ftape_set_nr_buffers(int cnt
)
113 int delta
= cnt
- ft_nr_buffers
;
114 TRACE_FUN(ft_t_flow
);
118 if (add_one_buffer() < 0) {
122 } else if (delta
< 0) {
127 ftape_zap_read_buffers();