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$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/pciio.h>
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <sys/bus_dma.h>
45 #include <sys/ioccom.h>
47 #include <sys/mutex.h>
48 #include <sys/rwlock.h>
49 #include <sys/linker.h>
50 #include <sys/firmware.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
56 #include <sys/queue.h>
57 #include <sys/taskqueue.h>
59 #include <sys/queue.h>
60 #include <sys/libkern.h>
62 #include <netinet/in.h>
64 #include <rdma/ib_verbs.h>
65 #include <rdma/ib_umem.h>
66 #include <rdma/ib_user_verbs.h>
67 #include <linux/idr.h>
68 #include <ulp/iw_cxgb/iw_cxgb_ib_intfc.h>
70 #include <cxgb_include.h>
71 #include <ulp/iw_cxgb/iw_cxgb_wr.h>
72 #include <ulp/iw_cxgb/iw_cxgb_hal.h>
73 #include <ulp/iw_cxgb/iw_cxgb_provider.h>
74 #include <ulp/iw_cxgb/iw_cxgb_cm.h>
75 #include <ulp/iw_cxgb/iw_cxgb.h>
76 #include <ulp/iw_cxgb/iw_cxgb_resource.h>
77 #include <ulp/iw_cxgb/iw_cxgb_user.h>
80 static struct buf_ring *rhdl_fifo;
81 static struct mtx rhdl_fifo_lock;
84 #define RANDOM_SIZE 16
86 static int __cxio_init_resource_fifo(struct buf_ring **fifo,
87 struct mtx *fifo_lock,
95 mtx_init(fifo_lock, "cxio fifo", NULL, MTX_DEF|MTX_DUPOK);
97 *fifo = buf_ring_alloc(nr, M_DEVBUF, M_NOWAIT, fifo_lock);
101 for (i = 0; i < skip_low + skip_high; i++) {
104 buf_ring_enqueue(*fifo, (uintptr_t) entry);
109 random_bytes = random();
110 for (i = 0; i < RANDOM_SIZE; i++)
111 rarray[i] = i + skip_low;
112 for (i = skip_low + RANDOM_SIZE; i < nr - skip_high; i++) {
113 if (j >= RANDOM_SIZE) {
115 random_bytes = random();
117 idx = (random_bytes >> (j * 2)) & 0xF;
118 buf_ring_enqueue(*fifo, (void *)(uintptr_t)rarray[idx]);
122 for (i = 0; i < RANDOM_SIZE; i++)
123 buf_ring_enqueue(*fifo, (void *) (uintptr_t)rarray[i]);
125 for (i = skip_low; i < nr - skip_high; i++)
126 buf_ring_enqueue(*fifo, (void *) (uintptr_t)i);
128 for (i = 0; i < skip_low + skip_high; i++)
129 buf_ring_dequeue_sc(*fifo);
134 static int cxio_init_resource_fifo(struct buf_ring **fifo, struct mtx * fifo_lock,
135 u32 nr, u32 skip_low, u32 skip_high)
137 return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
141 static int cxio_init_resource_fifo_random(struct buf_ring **fifo,
142 struct mtx * fifo_lock,
143 u32 nr, u32 skip_low, u32 skip_high)
146 return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
150 static int cxio_init_qpid_fifo(struct cxio_rdev *rdev_p)
154 mtx_init(&rdev_p->rscp->qpid_fifo_lock, "qpid fifo", NULL, MTX_DEF);
156 rdev_p->rscp->qpid_fifo = buf_ring_alloc(T3_MAX_NUM_QP, M_DEVBUF,
157 M_NOWAIT, &rdev_p->rscp->qpid_fifo_lock);
158 if (rdev_p->rscp->qpid_fifo == NULL)
161 for (i = 16; i < T3_MAX_NUM_QP; i++)
162 if (!(i & rdev_p->qpmask))
163 buf_ring_enqueue(rdev_p->rscp->qpid_fifo, (void *) (uintptr_t)i);
168 int cxio_hal_init_rhdl_resource(u32 nr_rhdl)
170 return cxio_init_resource_fifo(&rhdl_fifo, &rhdl_fifo_lock, nr_rhdl, 1,
174 void cxio_hal_destroy_rhdl_resource(void)
176 buf_ring_free(rhdl_fifo, M_DEVBUF);
180 /* nr_* must be power of 2 */
181 int cxio_hal_init_resource(struct cxio_rdev *rdev_p,
182 u32 nr_tpt, u32 nr_pbl,
183 u32 nr_rqt, u32 nr_qpid, u32 nr_cqid, u32 nr_pdid)
186 struct cxio_hal_resource *rscp;
188 rscp = malloc(sizeof(*rscp), M_DEVBUF, M_NOWAIT|M_ZERO);
192 err = cxio_init_resource_fifo_random(&rscp->tpt_fifo,
193 &rscp->tpt_fifo_lock,
197 err = cxio_init_qpid_fifo(rdev_p);
200 err = cxio_init_resource_fifo(&rscp->cqid_fifo, &rscp->cqid_fifo_lock,
204 err = cxio_init_resource_fifo(&rscp->pdid_fifo, &rscp->pdid_fifo_lock,
210 buf_ring_free(rscp->cqid_fifo, M_DEVBUF);
212 buf_ring_free(rscp->qpid_fifo, M_DEVBUF);
214 buf_ring_free(rscp->tpt_fifo, M_DEVBUF);
220 * returns 0 if no resource available
222 static u32 cxio_hal_get_resource(struct buf_ring *fifo, struct mtx *lock)
227 entry = (u32)(uintptr_t)buf_ring_dequeue_sc(fifo);
232 static void cxio_hal_put_resource(struct buf_ring *fifo, u32 entry, struct mtx *lock)
235 buf_ring_enqueue(fifo, (void *) (uintptr_t)entry);
239 u32 cxio_hal_get_stag(struct cxio_hal_resource *rscp)
241 return cxio_hal_get_resource(rscp->tpt_fifo, &rscp->tpt_fifo_lock);
244 void cxio_hal_put_stag(struct cxio_hal_resource *rscp, u32 stag)
246 cxio_hal_put_resource(rscp->tpt_fifo, stag, &rscp->tpt_fifo_lock);
249 u32 cxio_hal_get_qpid(struct cxio_hal_resource *rscp)
251 u32 qpid = cxio_hal_get_resource(rscp->qpid_fifo, &rscp->qpid_fifo_lock);
252 CTR2(KTR_IW_CXGB, "%s qpid 0x%x", __FUNCTION__, qpid);
256 void cxio_hal_put_qpid(struct cxio_hal_resource *rscp, u32 qpid)
258 CTR2(KTR_IW_CXGB, "%s qpid 0x%x", __FUNCTION__, qpid);
259 cxio_hal_put_resource(rscp->qpid_fifo, qpid, &rscp->qpid_fifo_lock);
262 u32 cxio_hal_get_cqid(struct cxio_hal_resource *rscp)
264 return cxio_hal_get_resource(rscp->cqid_fifo, &rscp->cqid_fifo_lock);
267 void cxio_hal_put_cqid(struct cxio_hal_resource *rscp, u32 cqid)
269 cxio_hal_put_resource(rscp->cqid_fifo, cqid, &rscp->cqid_fifo_lock);
272 u32 cxio_hal_get_pdid(struct cxio_hal_resource *rscp)
274 return cxio_hal_get_resource(rscp->pdid_fifo, &rscp->pdid_fifo_lock);
277 void cxio_hal_put_pdid(struct cxio_hal_resource *rscp, u32 pdid)
279 cxio_hal_put_resource(rscp->pdid_fifo, pdid, &rscp->pdid_fifo_lock);
282 void cxio_hal_destroy_resource(struct cxio_hal_resource *rscp)
284 buf_ring_free(rscp->tpt_fifo, M_DEVBUF);
285 buf_ring_free(rscp->cqid_fifo, M_DEVBUF);
286 buf_ring_free(rscp->qpid_fifo, M_DEVBUF);
287 buf_ring_free(rscp->pdid_fifo, M_DEVBUF);
288 free(rscp, M_DEVBUF);
292 * PBL Memory Manager. Uses Linux generic allocator.
295 #define MIN_PBL_SHIFT 8 /* 256B == min PBL size (32 entries) */
296 #define PBL_CHUNK 2*1024*1024
298 u32 cxio_hal_pblpool_alloc(struct cxio_rdev *rdev_p, int size)
300 unsigned long addr = gen_pool_alloc(rdev_p->pbl_pool, size);
301 CTR3(KTR_IW_CXGB, "%s addr 0x%x size %d", __FUNCTION__, (u32)addr, size);
305 void cxio_hal_pblpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
307 CTR3(KTR_IW_CXGB, "%s addr 0x%x size %d", __FUNCTION__, addr, size);
308 gen_pool_free(rdev_p->pbl_pool, (unsigned long)addr, size);
311 int cxio_hal_pblpool_create(struct cxio_rdev *rdev_p)
314 rdev_p->pbl_pool = gen_pool_create(rdev_p->rnic_info.pbl_base, MIN_PBL_SHIFT,
315 rdev_p->rnic_info.pbl_top - rdev_p->rnic_info.pbl_base);
317 if (rdev_p->pbl_pool) {
320 for (i = rdev_p->rnic_info.pbl_base;
321 i <= rdev_p->rnic_info.pbl_top - PBL_CHUNK + 1;
323 gen_pool_add(rdev_p->pbl_pool, i, PBL_CHUNK, -1);
326 return rdev_p->pbl_pool ? 0 : (-ENOMEM);
329 void cxio_hal_pblpool_destroy(struct cxio_rdev *rdev_p)
331 gen_pool_destroy(rdev_p->pbl_pool);
335 * RQT Memory Manager. Uses Linux generic allocator.
338 #define MIN_RQT_SHIFT 10 /* 1KB == mini RQT size (16 entries) */
339 #define RQT_CHUNK 2*1024*1024
341 u32 cxio_hal_rqtpool_alloc(struct cxio_rdev *rdev_p, int size)
343 unsigned long addr = gen_pool_alloc(rdev_p->rqt_pool, size << 6);
344 CTR3(KTR_IW_CXGB, "%s addr 0x%x size %d", __FUNCTION__, (u32)addr, size << 6);
348 void cxio_hal_rqtpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
350 CTR3(KTR_IW_CXGB, "%s addr 0x%x size %d", __FUNCTION__, addr, size << 6);
351 gen_pool_free(rdev_p->rqt_pool, (unsigned long)addr, size << 6);
354 int cxio_hal_rqtpool_create(struct cxio_rdev *rdev_p)
357 rdev_p->rqt_pool = gen_pool_create(rdev_p->rnic_info.rqt_base,
358 MIN_RQT_SHIFT, rdev_p->rnic_info.rqt_top - rdev_p->rnic_info.rqt_base);
360 if (rdev_p->rqt_pool) {
363 for (i = rdev_p->rnic_info.rqt_base;
364 i <= rdev_p->rnic_info.rqt_top - RQT_CHUNK + 1;
366 gen_pool_add(rdev_p->rqt_pool, i, RQT_CHUNK, -1);
369 return rdev_p->rqt_pool ? 0 : (-ENOMEM);
372 void cxio_hal_rqtpool_destroy(struct cxio_rdev *rdev_p)
374 gen_pool_destroy(rdev_p->rqt_pool);