Merge master.kernel.org:/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[pandora-kernel.git] / drivers / infiniband / hw / cxgb3 / cxio_resource.c
1 /*
2  * Copyright (c) 2006 Chelsio, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 /* Crude resource management */
33 #include <linux/kernel.h>
34 #include <linux/random.h>
35 #include <linux/slab.h>
36 #include <linux/kfifo.h>
37 #include <linux/spinlock.h>
38 #include <linux/errno.h>
39 #include "cxio_resource.h"
40 #include "cxio_hal.h"
41
42 static struct kfifo *rhdl_fifo;
43 static spinlock_t rhdl_fifo_lock;
44
45 #define RANDOM_SIZE 16
46
47 static int __cxio_init_resource_fifo(struct kfifo **fifo,
48                                    spinlock_t *fifo_lock,
49                                    u32 nr, u32 skip_low,
50                                    u32 skip_high,
51                                    int random)
52 {
53         u32 i, j, entry = 0, idx;
54         u32 random_bytes;
55         u32 rarray[16];
56         spin_lock_init(fifo_lock);
57
58         *fifo = kfifo_alloc(nr * sizeof(u32), GFP_KERNEL, fifo_lock);
59         if (IS_ERR(*fifo))
60                 return -ENOMEM;
61
62         for (i = 0; i < skip_low + skip_high; i++)
63                 __kfifo_put(*fifo, (unsigned char *) &entry, sizeof(u32));
64         if (random) {
65                 j = 0;
66                 random_bytes = random32();
67                 for (i = 0; i < RANDOM_SIZE; i++)
68                         rarray[i] = i + skip_low;
69                 for (i = skip_low + RANDOM_SIZE; i < nr - skip_high; i++) {
70                         if (j >= RANDOM_SIZE) {
71                                 j = 0;
72                                 random_bytes = random32();
73                         }
74                         idx = (random_bytes >> (j * 2)) & 0xF;
75                         __kfifo_put(*fifo,
76                                 (unsigned char *) &rarray[idx],
77                                 sizeof(u32));
78                         rarray[idx] = i;
79                         j++;
80                 }
81                 for (i = 0; i < RANDOM_SIZE; i++)
82                         __kfifo_put(*fifo,
83                                 (unsigned char *) &rarray[i],
84                                 sizeof(u32));
85         } else
86                 for (i = skip_low; i < nr - skip_high; i++)
87                         __kfifo_put(*fifo, (unsigned char *) &i, sizeof(u32));
88
89         for (i = 0; i < skip_low + skip_high; i++)
90                 kfifo_get(*fifo, (unsigned char *) &entry, sizeof(u32));
91         return 0;
92 }
93
94 static int cxio_init_resource_fifo(struct kfifo **fifo, spinlock_t * fifo_lock,
95                                    u32 nr, u32 skip_low, u32 skip_high)
96 {
97         return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
98                                           skip_high, 0));
99 }
100
101 static int cxio_init_resource_fifo_random(struct kfifo **fifo,
102                                    spinlock_t * fifo_lock,
103                                    u32 nr, u32 skip_low, u32 skip_high)
104 {
105
106         return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
107                                           skip_high, 1));
108 }
109
110 static int cxio_init_qpid_fifo(struct cxio_rdev *rdev_p)
111 {
112         u32 i;
113
114         spin_lock_init(&rdev_p->rscp->qpid_fifo_lock);
115
116         rdev_p->rscp->qpid_fifo = kfifo_alloc(T3_MAX_NUM_QP * sizeof(u32),
117                                               GFP_KERNEL,
118                                               &rdev_p->rscp->qpid_fifo_lock);
119         if (IS_ERR(rdev_p->rscp->qpid_fifo))
120                 return -ENOMEM;
121
122         for (i = 16; i < T3_MAX_NUM_QP; i++)
123                 if (!(i & rdev_p->qpmask))
124                         __kfifo_put(rdev_p->rscp->qpid_fifo,
125                                     (unsigned char *) &i, sizeof(u32));
126         return 0;
127 }
128
129 int cxio_hal_init_rhdl_resource(u32 nr_rhdl)
130 {
131         return cxio_init_resource_fifo(&rhdl_fifo, &rhdl_fifo_lock, nr_rhdl, 1,
132                                        0);
133 }
134
135 void cxio_hal_destroy_rhdl_resource(void)
136 {
137         kfifo_free(rhdl_fifo);
138 }
139
140 /* nr_* must be power of 2 */
141 int cxio_hal_init_resource(struct cxio_rdev *rdev_p,
142                            u32 nr_tpt, u32 nr_pbl,
143                            u32 nr_rqt, u32 nr_qpid, u32 nr_cqid, u32 nr_pdid)
144 {
145         int err = 0;
146         struct cxio_hal_resource *rscp;
147
148         rscp = kmalloc(sizeof(*rscp), GFP_KERNEL);
149         if (!rscp)
150                 return -ENOMEM;
151         rdev_p->rscp = rscp;
152         err = cxio_init_resource_fifo_random(&rscp->tpt_fifo,
153                                       &rscp->tpt_fifo_lock,
154                                       nr_tpt, 1, 0);
155         if (err)
156                 goto tpt_err;
157         err = cxio_init_qpid_fifo(rdev_p);
158         if (err)
159                 goto qpid_err;
160         err = cxio_init_resource_fifo(&rscp->cqid_fifo, &rscp->cqid_fifo_lock,
161                                       nr_cqid, 1, 0);
162         if (err)
163                 goto cqid_err;
164         err = cxio_init_resource_fifo(&rscp->pdid_fifo, &rscp->pdid_fifo_lock,
165                                       nr_pdid, 1, 0);
166         if (err)
167                 goto pdid_err;
168         return 0;
169 pdid_err:
170         kfifo_free(rscp->cqid_fifo);
171 cqid_err:
172         kfifo_free(rscp->qpid_fifo);
173 qpid_err:
174         kfifo_free(rscp->tpt_fifo);
175 tpt_err:
176         return -ENOMEM;
177 }
178
179 /*
180  * returns 0 if no resource available
181  */
182 static inline u32 cxio_hal_get_resource(struct kfifo *fifo)
183 {
184         u32 entry;
185         if (kfifo_get(fifo, (unsigned char *) &entry, sizeof(u32)))
186                 return entry;
187         else
188                 return 0;       /* fifo emptry */
189 }
190
191 static inline void cxio_hal_put_resource(struct kfifo *fifo, u32 entry)
192 {
193         BUG_ON(kfifo_put(fifo, (unsigned char *) &entry, sizeof(u32)) == 0);
194 }
195
196 u32 cxio_hal_get_rhdl(void)
197 {
198         return cxio_hal_get_resource(rhdl_fifo);
199 }
200
201 void cxio_hal_put_rhdl(u32 rhdl)
202 {
203         cxio_hal_put_resource(rhdl_fifo, rhdl);
204 }
205
206 u32 cxio_hal_get_stag(struct cxio_hal_resource *rscp)
207 {
208         return cxio_hal_get_resource(rscp->tpt_fifo);
209 }
210
211 void cxio_hal_put_stag(struct cxio_hal_resource *rscp, u32 stag)
212 {
213         cxio_hal_put_resource(rscp->tpt_fifo, stag);
214 }
215
216 u32 cxio_hal_get_qpid(struct cxio_hal_resource *rscp)
217 {
218         u32 qpid = cxio_hal_get_resource(rscp->qpid_fifo);
219         PDBG("%s qpid 0x%x\n", __FUNCTION__, qpid);
220         return qpid;
221 }
222
223 void cxio_hal_put_qpid(struct cxio_hal_resource *rscp, u32 qpid)
224 {
225         PDBG("%s qpid 0x%x\n", __FUNCTION__, qpid);
226         cxio_hal_put_resource(rscp->qpid_fifo, qpid);
227 }
228
229 u32 cxio_hal_get_cqid(struct cxio_hal_resource *rscp)
230 {
231         return cxio_hal_get_resource(rscp->cqid_fifo);
232 }
233
234 void cxio_hal_put_cqid(struct cxio_hal_resource *rscp, u32 cqid)
235 {
236         cxio_hal_put_resource(rscp->cqid_fifo, cqid);
237 }
238
239 u32 cxio_hal_get_pdid(struct cxio_hal_resource *rscp)
240 {
241         return cxio_hal_get_resource(rscp->pdid_fifo);
242 }
243
244 void cxio_hal_put_pdid(struct cxio_hal_resource *rscp, u32 pdid)
245 {
246         cxio_hal_put_resource(rscp->pdid_fifo, pdid);
247 }
248
249 void cxio_hal_destroy_resource(struct cxio_hal_resource *rscp)
250 {
251         kfifo_free(rscp->tpt_fifo);
252         kfifo_free(rscp->cqid_fifo);
253         kfifo_free(rscp->qpid_fifo);
254         kfifo_free(rscp->pdid_fifo);
255         kfree(rscp);
256 }
257
258 /*
259  * PBL Memory Manager.  Uses Linux generic allocator.
260  */
261
262 #define MIN_PBL_SHIFT 8                 /* 256B == min PBL size (32 entries) */
263 #define PBL_CHUNK 2*1024*1024
264
265 u32 cxio_hal_pblpool_alloc(struct cxio_rdev *rdev_p, int size)
266 {
267         unsigned long addr = gen_pool_alloc(rdev_p->pbl_pool, size);
268         PDBG("%s addr 0x%x size %d\n", __FUNCTION__, (u32)addr, size);
269         return (u32)addr;
270 }
271
272 void cxio_hal_pblpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
273 {
274         PDBG("%s addr 0x%x size %d\n", __FUNCTION__, addr, size);
275         gen_pool_free(rdev_p->pbl_pool, (unsigned long)addr, size);
276 }
277
278 int cxio_hal_pblpool_create(struct cxio_rdev *rdev_p)
279 {
280         unsigned long i;
281         rdev_p->pbl_pool = gen_pool_create(MIN_PBL_SHIFT, -1);
282         if (rdev_p->pbl_pool)
283                 for (i = rdev_p->rnic_info.pbl_base;
284                      i <= rdev_p->rnic_info.pbl_top - PBL_CHUNK + 1;
285                      i += PBL_CHUNK)
286                         gen_pool_add(rdev_p->pbl_pool, i, PBL_CHUNK, -1);
287         return rdev_p->pbl_pool ? 0 : -ENOMEM;
288 }
289
290 void cxio_hal_pblpool_destroy(struct cxio_rdev *rdev_p)
291 {
292         gen_pool_destroy(rdev_p->pbl_pool);
293 }
294
295 /*
296  * RQT Memory Manager.  Uses Linux generic allocator.
297  */
298
299 #define MIN_RQT_SHIFT 10        /* 1KB == mini RQT size (16 entries) */
300 #define RQT_CHUNK 2*1024*1024
301
302 u32 cxio_hal_rqtpool_alloc(struct cxio_rdev *rdev_p, int size)
303 {
304         unsigned long addr = gen_pool_alloc(rdev_p->rqt_pool, size << 6);
305         PDBG("%s addr 0x%x size %d\n", __FUNCTION__, (u32)addr, size << 6);
306         return (u32)addr;
307 }
308
309 void cxio_hal_rqtpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
310 {
311         PDBG("%s addr 0x%x size %d\n", __FUNCTION__, addr, size << 6);
312         gen_pool_free(rdev_p->rqt_pool, (unsigned long)addr, size << 6);
313 }
314
315 int cxio_hal_rqtpool_create(struct cxio_rdev *rdev_p)
316 {
317         unsigned long i;
318         rdev_p->rqt_pool = gen_pool_create(MIN_RQT_SHIFT, -1);
319         if (rdev_p->rqt_pool)
320                 for (i = rdev_p->rnic_info.rqt_base;
321                      i <= rdev_p->rnic_info.rqt_top - RQT_CHUNK + 1;
322                      i += RQT_CHUNK)
323                         gen_pool_add(rdev_p->rqt_pool, i, RQT_CHUNK, -1);
324         return rdev_p->rqt_pool ? 0 : -ENOMEM;
325 }
326
327 void cxio_hal_rqtpool_destroy(struct cxio_rdev *rdev_p)
328 {
329         gen_pool_destroy(rdev_p->rqt_pool);
330 }