aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/cxgb/sys/cxgb_support.c
blob: e911dfcbdac06dfe2e2fb5166070194f73bbad10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**************************************************************************

Copyright (c) 2007, Chelsio Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.

 2. Neither the name of the Chelsio Corporation nor the names of its
    contributors may be used to endorse or promote products derived from
    this software without specific prior written permission.
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

***************************************************************************/

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/queue.h>


#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/smp.h>
#include <sys/systm.h>
#include <vm/vm.h>
#include <vm/pmap.h>

#ifdef CONFIG_DEFINED
#include <cxgb_include.h>
#include <sys/mvec.h>
#else
#include <dev/cxgb/cxgb_include.h>
#include <dev/cxgb/sys/mvec.h>
#endif

extern int cxgb_use_16k_clusters;
int cxgb_pcpu_cache_enable = 1;

struct buf_stack {
	caddr_t            *bs_stack;
	volatile int        bs_head;
	int                 bs_size;
};

static __inline int
buf_stack_push(struct buf_stack *bs, caddr_t buf)
{
	if (bs->bs_head + 1 >= bs->bs_size)
		return (ENOSPC);

	bs->bs_stack[++(bs->bs_head)] = buf;
	return (0);
}

static __inline caddr_t
buf_stack_pop(struct buf_stack *bs)
{
	if (bs->bs_head < 0)
		return (NULL);

	return (bs->bs_stack[(bs->bs_head)--]);
}

/*
 * Stack is full
 *
 */
static __inline int
buf_stack_avail(struct buf_stack *bs)
{
	return (bs->bs_size - bs->bs_head - 1);
}

struct cxgb_cache_pcpu {
	struct buf_stack        ccp_jumbo_free;
	struct buf_stack        ccp_cluster_free;
	uma_zone_t              ccp_jumbo_zone;
};

struct cxgb_cache_system {
	struct cxgb_cache_pcpu ccs_array[0];
} *cxgb_caches;

static int
buf_stack_init(struct buf_stack *bs, int size)
{
	bs->bs_size = size;
	bs->bs_head = -1;
	if((bs->bs_stack = malloc(sizeof(caddr_t)*size, M_DEVBUF, M_NOWAIT)) == NULL)
		return (ENOMEM);

	return (0);
}

static void
buf_stack_deinit(struct buf_stack *bs)
{
	if (bs->bs_stack != NULL)
		free(bs->bs_stack, M_DEVBUF);
}

static int
cxgb_cache_pcpu_init(struct cxgb_cache_pcpu *ccp)
{
	int err;
	
	if ((err = buf_stack_init(&ccp->ccp_jumbo_free, (JUMBO_Q_SIZE >> 2))))
		return (err);
	
	if ((err = buf_stack_init(&ccp->ccp_cluster_free, (FL_Q_SIZE >> 2))))
		return (err);

#if __FreeBSD_version > 800000		
	if (cxgb_use_16k_clusters) 
		ccp->ccp_jumbo_zone = zone_jumbo16;
	else
		ccp->ccp_jumbo_zone = zone_jumbo9;
#else
		ccp->ccp_jumbo_zone = zone_jumbop;
#endif
	return (0);
}

static void
cxgb_cache_pcpu_deinit(struct cxgb_cache_pcpu *ccp)
{
	void *cl;

	while ((cl = buf_stack_pop(&ccp->ccp_jumbo_free)) != NULL)
		uma_zfree(ccp->ccp_jumbo_zone, cl);
	while ((cl = buf_stack_pop(&ccp->ccp_cluster_free)) != NULL)
		uma_zfree(zone_clust, cl);

	buf_stack_deinit(&ccp->ccp_jumbo_free);
	buf_stack_deinit(&ccp->ccp_cluster_free);
	
}

static int inited = 0;

int
cxgb_cache_init(void)
{
	int i, err;
	
	if (inited++ > 0)
		return (0);

	if ((cxgb_caches = malloc(sizeof(struct cxgb_cache_pcpu)*mp_ncpus, M_DEVBUF, M_WAITOK|M_ZERO)) == NULL)
		return (ENOMEM);
	
	for (i = 0; i < mp_ncpus; i++) 
		if ((err = cxgb_cache_pcpu_init(&cxgb_caches->ccs_array[i])))
			goto err;

	return (0);
err:
	cxgb_cache_flush();

	return (err);
}

void
cxgb_cache_flush(void)
{
	int i;
	
	if (--inited > 0) 
		return;

	if (cxgb_caches == NULL)
		return;
	
	for (i = 0; i < mp_ncpus; i++) 
		cxgb_cache_pcpu_deinit(&cxgb_caches->ccs_array[i]);

	free(cxgb_caches, M_DEVBUF);
	cxgb_caches = NULL;
}

caddr_t
cxgb_cache_get(uma_zone_t zone)
{
	caddr_t cl = NULL;
	struct cxgb_cache_pcpu *ccp;

	if (cxgb_pcpu_cache_enable) {
		critical_enter();
		ccp = &cxgb_caches->ccs_array[curcpu];
		if (zone == zone_clust) {
			cl = buf_stack_pop(&ccp->ccp_cluster_free);
		} else if (zone == ccp->ccp_jumbo_zone) {
			cl = buf_stack_pop(&ccp->ccp_jumbo_free);
		}
		critical_exit();
	}
	
	if (cl == NULL) 
		cl = uma_zalloc(zone, M_NOWAIT);
	else
		cxgb_cached_allocations++;
	
	return (cl);
}

void
cxgb_cache_put(uma_zone_t zone, void *cl)
{
	struct cxgb_cache_pcpu *ccp;
	int err = ENOSPC;

	if (cxgb_pcpu_cache_enable) {
		critical_enter();
		ccp = &cxgb_caches->ccs_array[curcpu];
		if (zone == zone_clust) {
			err = buf_stack_push(&ccp->ccp_cluster_free, cl);
		} else if (zone == ccp->ccp_jumbo_zone){
			err = buf_stack_push(&ccp->ccp_jumbo_free, cl);
		}
		critical_exit();
	}
	
	if (err)
		uma_zfree(zone, cl);
	else
		cxgb_cached++;
}

void
cxgb_cache_refill(void)
{
	struct cxgb_cache_pcpu *ccp;
	caddr_t vec[8];
	uma_zone_t zone;
	int i, count;


	return;
restart:
	critical_enter();
	ccp = &cxgb_caches->ccs_array[curcpu];
	zone = ccp->ccp_jumbo_zone;
	if (!buf_stack_avail(&ccp->ccp_jumbo_free) &&
	    !buf_stack_avail(&ccp->ccp_cluster_free)) {
		critical_exit();
		return;
	}
	critical_exit();


	
	for (i = 0; i < 8; i++)
		if ((vec[i] = uma_zalloc(zone, M_NOWAIT)) == NULL) 
			goto free;

	critical_enter();
	ccp = &cxgb_caches->ccs_array[curcpu];
	for (i = 0; i < 8 && buf_stack_avail(&ccp->ccp_jumbo_free); i++)
		if (buf_stack_push(&ccp->ccp_jumbo_free, vec[i]))
			break;
	critical_exit();

	for (; i < 8; i++)
		uma_zfree(zone, vec[i]);


	
	zone = zone_clust;
	for (i = 0; i < 8; i++)
		if ((vec[i] = uma_zalloc(zone, M_NOWAIT)) == NULL) 
			goto free;

	critical_enter();
	ccp = &cxgb_caches->ccs_array[curcpu];
	for (i = 0; i < 8 && buf_stack_avail(&ccp->ccp_cluster_free); i++)
		if (buf_stack_push(&ccp->ccp_cluster_free, vec[i]))
			break;
	critical_exit();
	
	for (; i < 8; i++)
		uma_zfree(zone, vec[i]);

	goto restart;


free:
	count = i;
	for (; i < count; i++)
		uma_zfree(zone, vec[i]);
}
	
struct buf_ring *
buf_ring_alloc(int count, int flags)
{
	struct buf_ring *br;

	KASSERT(powerof2(count), ("buf ring must be size power of 2"));
	
	br = malloc(sizeof(struct buf_ring), M_DEVBUF, flags|M_ZERO);
	if (br == NULL)
		return (NULL);
	
	br->br_ring = malloc(sizeof(caddr_t)*count, M_DEVBUF, flags|M_ZERO);
	if (br->br_ring == NULL) {
		free(br, M_DEVBUF);
		return (NULL);
	}
	
	mtx_init(&br->br_lock, "buf ring", NULL, MTX_DUPOK|MTX_DEF);
	br->br_size = count;
	br->br_prod = br->br_cons = 0;

	return (br);
}

void
buf_ring_free(struct buf_ring *br)
{
	free(br->br_ring, M_DEVBUF);
	free(br, M_DEVBUF);
}