2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
6 * This file contains code imported from the OFED rds source file ib_ring.c
7 * Oracle elects to have and use the contents of ib_ring.c under and governed
8 * by the OpenIB.org BSD license (see below for full license text). However,
9 * the following notice accompanied the original version of this file:
13 * Copyright (c) 2006 Oracle. All rights reserved.
15 * This software is available to you under a choice of one of two
16 * licenses. You may choose to be licensed under the terms of the GNU
17 * General Public License (GPL) Version 2, available from the file
18 * COPYING in the main directory of this source tree, or the
19 * OpenIB.org BSD license below:
21 * Redistribution and use in source and binary forms, with or
22 * without modification, are permitted provided that the following
25 * - Redistributions of source code must retain the above
26 * copyright notice, this list of conditions and the following
29 * - Redistributions in binary form must reproduce the above
30 * copyright notice, this list of conditions and the following
31 * disclaimer in the documentation and/or other materials
32 * provided with the distribution.
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
37 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
38 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
39 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
40 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
46 #include <sys/ib/clients/rdsv3/rdsv3.h>
47 #include <sys/ib/clients/rdsv3/ib.h>
48 #include <sys/ib/clients/rdsv3/rdsv3_debug.h>
51 * Locking for IB rings.
52 * We assume that allocation is always protected by a mutex
53 * in the caller (this is a valid assumption for the current
56 * Freeing always happens in an interrupt, and hence only
57 * races with allocations, but not with other free()s.
59 * The interaction between allocation and freeing is that
60 * the alloc code has to determine the number of free entries.
61 * To this end, we maintain two counters; an allocation counter
62 * and a free counter. Both are allowed to run freely, and wrap
64 * The number of used entries is always (alloc_ctr - free_ctr) % NR.
66 * The current implementation makes free_ctr atomic. When the
67 * caller finds an allocation fails, it should set an "alloc fail"
68 * bit and retry the allocation. The "alloc fail" bit essentially tells
69 * the CQ completion handlers to wake it up after freeing some
74 rdsv3_ib_ring_init(struct rdsv3_ib_work_ring
*ring
, uint32_t nr
)
76 (void) memset(ring
, 0, sizeof (*ring
));
78 RDSV3_DPRINTF5("rdsv3_ib_ring_init", "ring %p nr %u", ring
, ring
->w_nr
);
81 static inline uint32_t
82 __rdsv3_ib_ring_used(struct rdsv3_ib_work_ring
*ring
)
86 /* This assumes that atomic_t has at least as many bits as uint32_t */
87 diff
= ring
->w_alloc_ctr
- (uint32_t)atomic_get(&ring
->w_free_ctr
);
88 ASSERT(diff
<= ring
->w_nr
);
94 rdsv3_ib_ring_resize(struct rdsv3_ib_work_ring
*ring
, uint32_t nr
)
97 * We only ever get called from the connection setup code,
98 * prior to creating the QP.
100 ASSERT(!__rdsv3_ib_ring_used(ring
));
105 __rdsv3_ib_ring_empty(struct rdsv3_ib_work_ring
*ring
)
107 return (__rdsv3_ib_ring_used(ring
) == 0);
111 rdsv3_ib_ring_alloc(struct rdsv3_ib_work_ring
*ring
, uint32_t val
,
114 uint32_t ret
= 0, avail
;
116 avail
= ring
->w_nr
- __rdsv3_ib_ring_used(ring
);
118 RDSV3_DPRINTF5("rdsv3_ib_ring_alloc",
119 "ring %p val %u next %u free %u", ring
, val
,
120 ring
->w_alloc_ptr
, avail
);
123 ret
= min(val
, avail
);
124 *pos
= ring
->w_alloc_ptr
;
126 ring
->w_alloc_ptr
= (ring
->w_alloc_ptr
+ ret
) % ring
->w_nr
;
127 ring
->w_alloc_ctr
+= ret
;
134 rdsv3_ib_ring_free(struct rdsv3_ib_work_ring
*ring
, uint32_t val
)
136 ring
->w_free_ptr
= (ring
->w_free_ptr
+ val
) % ring
->w_nr
;
137 atomic_add_32(&ring
->w_free_ctr
, val
);
139 if (__rdsv3_ib_ring_empty(ring
))
140 rdsv3_wake_up(&ring
->w_empty_wait
);
144 rdsv3_ib_ring_unalloc(struct rdsv3_ib_work_ring
*ring
, uint32_t val
)
146 ring
->w_alloc_ptr
= (ring
->w_alloc_ptr
- val
) % ring
->w_nr
;
147 ring
->w_alloc_ctr
-= val
;
151 rdsv3_ib_ring_empty(struct rdsv3_ib_work_ring
*ring
)
153 return (__rdsv3_ib_ring_empty(ring
));
157 rdsv3_ib_ring_low(struct rdsv3_ib_work_ring
*ring
)
159 return (__rdsv3_ib_ring_used(ring
) <= (ring
->w_nr
>> 2));
163 * returns the oldest alloced ring entry. This will be the next one
164 * freed. This can't be called if there are none allocated.
167 rdsv3_ib_ring_oldest(struct rdsv3_ib_work_ring
*ring
)
169 return (ring
->w_free_ptr
);
173 * returns the number of completed work requests.
177 rdsv3_ib_ring_completed(struct rdsv3_ib_work_ring
*ring
,
178 uint32_t wr_id
, uint32_t oldest
)
182 if (oldest
<= (unsigned long long)wr_id
)
183 ret
= (unsigned long long)wr_id
- oldest
+ 1;
185 ret
= ring
->w_nr
- oldest
+ (unsigned long long)wr_id
+ 1;
187 RDSV3_DPRINTF5("rdsv3_ib_ring_completed",
188 "ring %p ret %u wr_id %u oldest %u", ring
, ret
, wr_id
, oldest
);