1 /**************************************************************************
3 Copyright (c) 2007, Chelsio Inc.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
12 2. Neither the name of the Chelsio Corporation nor the names of its
13 contributors may be used to endorse or promote products derived from
14 this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
28 ***************************************************************************/
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/pciio.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/bus_dma.h>
43 #include <sys/ioccom.h>
45 #include <sys/mutex.h>
46 #include <sys/rwlock.h>
47 #include <sys/linker.h>
48 #include <sys/firmware.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/queue.h>
55 #include <sys/taskqueue.h>
57 #include <sys/queue.h>
58 #include <sys/libkern.h>
60 #include <netinet/in.h>
62 #include <contrib/rdma/ib_verbs.h>
63 #include <contrib/rdma/ib_umem.h>
64 #include <contrib/rdma/ib_user_verbs.h>
66 #include <cxgb_include.h>
67 #include <ulp/iw_cxgb/iw_cxgb_wr.h>
68 #include <ulp/iw_cxgb/iw_cxgb_hal.h>
69 #include <ulp/iw_cxgb/iw_cxgb_provider.h>
70 #include <ulp/iw_cxgb/iw_cxgb_cm.h>
71 #include <ulp/iw_cxgb/iw_cxgb.h>
72 #include <ulp/iw_cxgb/iw_cxgb_resource.h>
73 #include <ulp/iw_cxgb/iw_cxgb_user.h>
76 static struct buf_ring *rhdl_fifo;
77 static struct mtx rhdl_fifo_lock;
80 #define RANDOM_SIZE 16
82 static int __cxio_init_resource_fifo(struct buf_ring **fifo,
83 struct mtx *fifo_lock,
91 mtx_init(fifo_lock, "cxio fifo", NULL, MTX_DEF|MTX_DUPOK);
93 *fifo = buf_ring_alloc(nr, M_DEVBUF, M_NOWAIT, fifo_lock);
97 for (i = 0; i < skip_low + skip_high; i++) {
100 buf_ring_enqueue(*fifo, (uintptr_t) entry);
105 random_bytes = random();
106 for (i = 0; i < RANDOM_SIZE; i++)
107 rarray[i] = i + skip_low;
108 for (i = skip_low + RANDOM_SIZE; i < nr - skip_high; i++) {
109 if (j >= RANDOM_SIZE) {
111 random_bytes = random();
113 idx = (random_bytes >> (j * 2)) & 0xF;
114 buf_ring_enqueue(*fifo, (void *)(uintptr_t)rarray[idx]);
118 for (i = 0; i < RANDOM_SIZE; i++)
119 buf_ring_enqueue(*fifo, (void *) (uintptr_t)rarray[i]);
121 for (i = skip_low; i < nr - skip_high; i++)
122 buf_ring_enqueue(*fifo, (void *) (uintptr_t)i);
124 for (i = 0; i < skip_low + skip_high; i++)
125 buf_ring_dequeue_sc(*fifo);
130 static int cxio_init_resource_fifo(struct buf_ring **fifo, struct mtx * fifo_lock,
131 u32 nr, u32 skip_low, u32 skip_high)
133 return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
137 static int cxio_init_resource_fifo_random(struct buf_ring **fifo,
138 struct mtx * fifo_lock,
139 u32 nr, u32 skip_low, u32 skip_high)
142 return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
146 static int cxio_init_qpid_fifo(struct cxio_rdev *rdev_p)
150 mtx_init(&rdev_p->rscp->qpid_fifo_lock, "qpid fifo", NULL, MTX_DEF);
152 rdev_p->rscp->qpid_fifo = buf_ring_alloc(T3_MAX_NUM_QP, M_DEVBUF,
153 M_NOWAIT, &rdev_p->rscp->qpid_fifo_lock);
154 if (rdev_p->rscp->qpid_fifo == NULL)
157 for (i = 16; i < T3_MAX_NUM_QP; i++)
158 if (!(i & rdev_p->qpmask))
159 buf_ring_enqueue(rdev_p->rscp->qpid_fifo, (void *) (uintptr_t)i);
164 int cxio_hal_init_rhdl_resource(u32 nr_rhdl)
166 return cxio_init_resource_fifo(&rhdl_fifo, &rhdl_fifo_lock, nr_rhdl, 1,
170 void cxio_hal_destroy_rhdl_resource(void)
172 buf_ring_free(rhdl_fifo, M_DEVBUF);
176 /* nr_* must be power of 2 */
177 int cxio_hal_init_resource(struct cxio_rdev *rdev_p,
178 u32 nr_tpt, u32 nr_pbl,
179 u32 nr_rqt, u32 nr_qpid, u32 nr_cqid, u32 nr_pdid)
182 struct cxio_hal_resource *rscp;
184 rscp = malloc(sizeof(*rscp), M_DEVBUF, M_NOWAIT|M_ZERO);
188 err = cxio_init_resource_fifo_random(&rscp->tpt_fifo,
189 &rscp->tpt_fifo_lock,
193 err = cxio_init_qpid_fifo(rdev_p);
196 err = cxio_init_resource_fifo(&rscp->cqid_fifo, &rscp->cqid_fifo_lock,
200 err = cxio_init_resource_fifo(&rscp->pdid_fifo, &rscp->pdid_fifo_lock,
206 buf_ring_free(rscp->cqid_fifo, M_DEVBUF);
208 buf_ring_free(rscp->qpid_fifo, M_DEVBUF);
210 buf_ring_free(rscp->tpt_fifo, M_DEVBUF);
216 * returns 0 if no resource available
218 static u32 cxio_hal_get_resource(struct buf_ring *fifo, struct mtx *lock)
223 entry = (u32)(uintptr_t)buf_ring_dequeue_sc(fifo);
228 static void cxio_hal_put_resource(struct buf_ring *fifo, u32 entry, struct mtx *lock)
231 buf_ring_enqueue(fifo, (void *) (uintptr_t)entry);
235 u32 cxio_hal_get_stag(struct cxio_hal_resource *rscp)
237 return cxio_hal_get_resource(rscp->tpt_fifo, &rscp->tpt_fifo_lock);
240 void cxio_hal_put_stag(struct cxio_hal_resource *rscp, u32 stag)
242 cxio_hal_put_resource(rscp->tpt_fifo, stag, &rscp->tpt_fifo_lock);
245 u32 cxio_hal_get_qpid(struct cxio_hal_resource *rscp)
247 u32 qpid = cxio_hal_get_resource(rscp->qpid_fifo, &rscp->qpid_fifo_lock);
248 CTR2(KTR_IW_CXGB, "%s qpid 0x%x", __FUNCTION__, qpid);
252 void cxio_hal_put_qpid(struct cxio_hal_resource *rscp, u32 qpid)
254 CTR2(KTR_IW_CXGB, "%s qpid 0x%x", __FUNCTION__, qpid);
255 cxio_hal_put_resource(rscp->qpid_fifo, qpid, &rscp->qpid_fifo_lock);
258 u32 cxio_hal_get_cqid(struct cxio_hal_resource *rscp)
260 return cxio_hal_get_resource(rscp->cqid_fifo, &rscp->cqid_fifo_lock);
263 void cxio_hal_put_cqid(struct cxio_hal_resource *rscp, u32 cqid)
265 cxio_hal_put_resource(rscp->cqid_fifo, cqid, &rscp->cqid_fifo_lock);
268 u32 cxio_hal_get_pdid(struct cxio_hal_resource *rscp)
270 return cxio_hal_get_resource(rscp->pdid_fifo, &rscp->pdid_fifo_lock);
273 void cxio_hal_put_pdid(struct cxio_hal_resource *rscp, u32 pdid)
275 cxio_hal_put_resource(rscp->pdid_fifo, pdid, &rscp->pdid_fifo_lock);
278 void cxio_hal_destroy_resource(struct cxio_hal_resource *rscp)
280 buf_ring_free(rscp->tpt_fifo, M_DEVBUF);
281 buf_ring_free(rscp->cqid_fifo, M_DEVBUF);
282 buf_ring_free(rscp->qpid_fifo, M_DEVBUF);
283 buf_ring_free(rscp->pdid_fifo, M_DEVBUF);
284 free(rscp, M_DEVBUF);
288 * PBL Memory Manager. Uses Linux generic allocator.
291 #define MIN_PBL_SHIFT 8 /* 256B == min PBL size (32 entries) */
292 #define PBL_CHUNK 2*1024*1024
294 u32 cxio_hal_pblpool_alloc(struct cxio_rdev *rdev_p, int size)
296 unsigned long addr = gen_pool_alloc(rdev_p->pbl_pool, size);
297 CTR3(KTR_IW_CXGB, "%s addr 0x%x size %d", __FUNCTION__, (u32)addr, size);
301 void cxio_hal_pblpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
303 CTR3(KTR_IW_CXGB, "%s addr 0x%x size %d", __FUNCTION__, addr, size);
304 gen_pool_free(rdev_p->pbl_pool, (unsigned long)addr, size);
307 int cxio_hal_pblpool_create(struct cxio_rdev *rdev_p)
310 rdev_p->pbl_pool = gen_pool_create(rdev_p->rnic_info.pbl_base, MIN_PBL_SHIFT,
311 rdev_p->rnic_info.pbl_top - rdev_p->rnic_info.pbl_base);
313 if (rdev_p->pbl_pool) {
316 for (i = rdev_p->rnic_info.pbl_base;
317 i <= rdev_p->rnic_info.pbl_top - PBL_CHUNK + 1;
319 gen_pool_add(rdev_p->pbl_pool, i, PBL_CHUNK, -1);
322 return rdev_p->pbl_pool ? 0 : (-ENOMEM);
325 void cxio_hal_pblpool_destroy(struct cxio_rdev *rdev_p)
327 gen_pool_destroy(rdev_p->pbl_pool);
331 * RQT Memory Manager. Uses Linux generic allocator.
334 #define MIN_RQT_SHIFT 10 /* 1KB == mini RQT size (16 entries) */
335 #define RQT_CHUNK 2*1024*1024
337 u32 cxio_hal_rqtpool_alloc(struct cxio_rdev *rdev_p, int size)
339 unsigned long addr = gen_pool_alloc(rdev_p->rqt_pool, size << 6);
340 CTR3(KTR_IW_CXGB, "%s addr 0x%x size %d", __FUNCTION__, (u32)addr, size << 6);
344 void cxio_hal_rqtpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
346 CTR3(KTR_IW_CXGB, "%s addr 0x%x size %d", __FUNCTION__, addr, size << 6);
347 gen_pool_free(rdev_p->rqt_pool, (unsigned long)addr, size << 6);
350 int cxio_hal_rqtpool_create(struct cxio_rdev *rdev_p)
353 rdev_p->rqt_pool = gen_pool_create(rdev_p->rnic_info.rqt_base,
354 MIN_RQT_SHIFT, rdev_p->rnic_info.rqt_top - rdev_p->rnic_info.rqt_base);
356 if (rdev_p->rqt_pool) {
359 for (i = rdev_p->rnic_info.rqt_base;
360 i <= rdev_p->rnic_info.rqt_top - RQT_CHUNK + 1;
362 gen_pool_add(rdev_p->rqt_pool, i, RQT_CHUNK, -1);
365 return rdev_p->rqt_pool ? 0 : (-ENOMEM);
368 void cxio_hal_rqtpool_destroy(struct cxio_rdev *rdev_p)
370 gen_pool_destroy(rdev_p->rqt_pool);