1f528efe2SAndy Grover /*
2f528efe2SAndy Grover * Copyright (c) 2006 Oracle. All rights reserved.
3f528efe2SAndy Grover *
4f528efe2SAndy Grover * This software is available to you under a choice of one of two
5f528efe2SAndy Grover * licenses. You may choose to be licensed under the terms of the GNU
6f528efe2SAndy Grover * General Public License (GPL) Version 2, available from the file
7f528efe2SAndy Grover * COPYING in the main directory of this source tree, or the
8f528efe2SAndy Grover * OpenIB.org BSD license below:
9f528efe2SAndy Grover *
10f528efe2SAndy Grover * Redistribution and use in source and binary forms, with or
11f528efe2SAndy Grover * without modification, are permitted provided that the following
12f528efe2SAndy Grover * conditions are met:
13f528efe2SAndy Grover *
14f528efe2SAndy Grover * - Redistributions of source code must retain the above
15f528efe2SAndy Grover * copyright notice, this list of conditions and the following
16f528efe2SAndy Grover * disclaimer.
17f528efe2SAndy Grover *
18f528efe2SAndy Grover * - Redistributions in binary form must reproduce the above
19f528efe2SAndy Grover * copyright notice, this list of conditions and the following
20f528efe2SAndy Grover * disclaimer in the documentation and/or other materials
21f528efe2SAndy Grover * provided with the distribution.
22f528efe2SAndy Grover *
23f528efe2SAndy Grover * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24f528efe2SAndy Grover * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25f528efe2SAndy Grover * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26f528efe2SAndy Grover * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27f528efe2SAndy Grover * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28f528efe2SAndy Grover * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29f528efe2SAndy Grover * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30f528efe2SAndy Grover * SOFTWARE.
31f528efe2SAndy Grover *
32f528efe2SAndy Grover */
33f528efe2SAndy Grover #include <linux/kernel.h>
34f528efe2SAndy Grover
35f528efe2SAndy Grover #include "rds.h"
36f528efe2SAndy Grover #include "ib.h"
37f528efe2SAndy Grover
38f528efe2SAndy Grover /*
39f528efe2SAndy Grover * Locking for IB rings.
40f528efe2SAndy Grover * We assume that allocation is always protected by a mutex
41f528efe2SAndy Grover * in the caller (this is a valid assumption for the current
42f528efe2SAndy Grover * implementation).
43f528efe2SAndy Grover *
44f528efe2SAndy Grover * Freeing always happens in an interrupt, and hence only
45f528efe2SAndy Grover * races with allocations, but not with other free()s.
46f528efe2SAndy Grover *
47f528efe2SAndy Grover * The interaction between allocation and freeing is that
48f528efe2SAndy Grover * the alloc code has to determine the number of free entries.
49f528efe2SAndy Grover * To this end, we maintain two counters; an allocation counter
50f528efe2SAndy Grover * and a free counter. Both are allowed to run freely, and wrap
51f528efe2SAndy Grover * around.
52f528efe2SAndy Grover * The number of used entries is always (alloc_ctr - free_ctr) % NR.
53f528efe2SAndy Grover *
54f528efe2SAndy Grover * The current implementation makes free_ctr atomic. When the
55f528efe2SAndy Grover * caller finds an allocation fails, it should set an "alloc fail"
56f528efe2SAndy Grover * bit and retry the allocation. The "alloc fail" bit essentially tells
57f528efe2SAndy Grover * the CQ completion handlers to wake it up after freeing some
58f528efe2SAndy Grover * more entries.
59f528efe2SAndy Grover */
60f528efe2SAndy Grover
61f528efe2SAndy Grover /*
62f528efe2SAndy Grover * This only happens on shutdown.
63f528efe2SAndy Grover */
64f528efe2SAndy Grover DECLARE_WAIT_QUEUE_HEAD(rds_ib_ring_empty_wait);
65f528efe2SAndy Grover
rds_ib_ring_init(struct rds_ib_work_ring * ring,u32 nr)66f528efe2SAndy Grover void rds_ib_ring_init(struct rds_ib_work_ring *ring, u32 nr)
67f528efe2SAndy Grover {
68f528efe2SAndy Grover memset(ring, 0, sizeof(*ring));
69f528efe2SAndy Grover ring->w_nr = nr;
70f528efe2SAndy Grover rdsdebug("ring %p nr %u\n", ring, ring->w_nr);
71f528efe2SAndy Grover }
72f528efe2SAndy Grover
__rds_ib_ring_used(struct rds_ib_work_ring * ring)73f528efe2SAndy Grover static inline u32 __rds_ib_ring_used(struct rds_ib_work_ring *ring)
74f528efe2SAndy Grover {
75f528efe2SAndy Grover u32 diff;
76f528efe2SAndy Grover
77f528efe2SAndy Grover /* This assumes that atomic_t has at least as many bits as u32 */
78f528efe2SAndy Grover diff = ring->w_alloc_ctr - (u32) atomic_read(&ring->w_free_ctr);
79f528efe2SAndy Grover BUG_ON(diff > ring->w_nr);
80f528efe2SAndy Grover
81f528efe2SAndy Grover return diff;
82f528efe2SAndy Grover }
83f528efe2SAndy Grover
rds_ib_ring_resize(struct rds_ib_work_ring * ring,u32 nr)84f528efe2SAndy Grover void rds_ib_ring_resize(struct rds_ib_work_ring *ring, u32 nr)
85f528efe2SAndy Grover {
86f528efe2SAndy Grover /* We only ever get called from the connection setup code,
87f528efe2SAndy Grover * prior to creating the QP. */
88f528efe2SAndy Grover BUG_ON(__rds_ib_ring_used(ring));
89f528efe2SAndy Grover ring->w_nr = nr;
90f528efe2SAndy Grover }
91f528efe2SAndy Grover
__rds_ib_ring_empty(struct rds_ib_work_ring * ring)92f528efe2SAndy Grover static int __rds_ib_ring_empty(struct rds_ib_work_ring *ring)
93f528efe2SAndy Grover {
94f528efe2SAndy Grover return __rds_ib_ring_used(ring) == 0;
95f528efe2SAndy Grover }
96f528efe2SAndy Grover
rds_ib_ring_alloc(struct rds_ib_work_ring * ring,u32 val,u32 * pos)97f528efe2SAndy Grover u32 rds_ib_ring_alloc(struct rds_ib_work_ring *ring, u32 val, u32 *pos)
98f528efe2SAndy Grover {
99f528efe2SAndy Grover u32 ret = 0, avail;
100f528efe2SAndy Grover
101f528efe2SAndy Grover avail = ring->w_nr - __rds_ib_ring_used(ring);
102f528efe2SAndy Grover
103f528efe2SAndy Grover rdsdebug("ring %p val %u next %u free %u\n", ring, val,
104f528efe2SAndy Grover ring->w_alloc_ptr, avail);
105f528efe2SAndy Grover
106f528efe2SAndy Grover if (val && avail) {
107f528efe2SAndy Grover ret = min(val, avail);
108f528efe2SAndy Grover *pos = ring->w_alloc_ptr;
109f528efe2SAndy Grover
110f528efe2SAndy Grover ring->w_alloc_ptr = (ring->w_alloc_ptr + ret) % ring->w_nr;
111f528efe2SAndy Grover ring->w_alloc_ctr += ret;
112f528efe2SAndy Grover }
113f528efe2SAndy Grover
114f528efe2SAndy Grover return ret;
115f528efe2SAndy Grover }
116f528efe2SAndy Grover
rds_ib_ring_free(struct rds_ib_work_ring * ring,u32 val)117f528efe2SAndy Grover void rds_ib_ring_free(struct rds_ib_work_ring *ring, u32 val)
118f528efe2SAndy Grover {
119f528efe2SAndy Grover ring->w_free_ptr = (ring->w_free_ptr + val) % ring->w_nr;
120f528efe2SAndy Grover atomic_add(val, &ring->w_free_ctr);
121f528efe2SAndy Grover
122f528efe2SAndy Grover if (__rds_ib_ring_empty(ring) &&
123f528efe2SAndy Grover waitqueue_active(&rds_ib_ring_empty_wait))
124f528efe2SAndy Grover wake_up(&rds_ib_ring_empty_wait);
125f528efe2SAndy Grover }
126f528efe2SAndy Grover
rds_ib_ring_unalloc(struct rds_ib_work_ring * ring,u32 val)127f528efe2SAndy Grover void rds_ib_ring_unalloc(struct rds_ib_work_ring *ring, u32 val)
128f528efe2SAndy Grover {
129f528efe2SAndy Grover ring->w_alloc_ptr = (ring->w_alloc_ptr - val) % ring->w_nr;
130f528efe2SAndy Grover ring->w_alloc_ctr -= val;
131f528efe2SAndy Grover }
132f528efe2SAndy Grover
rds_ib_ring_empty(struct rds_ib_work_ring * ring)133f528efe2SAndy Grover int rds_ib_ring_empty(struct rds_ib_work_ring *ring)
134f528efe2SAndy Grover {
135f528efe2SAndy Grover return __rds_ib_ring_empty(ring);
136f528efe2SAndy Grover }
137f528efe2SAndy Grover
rds_ib_ring_low(struct rds_ib_work_ring * ring)138f528efe2SAndy Grover int rds_ib_ring_low(struct rds_ib_work_ring *ring)
139f528efe2SAndy Grover {
1405cd2fe6dSSteve Wise return __rds_ib_ring_used(ring) <= (ring->w_nr >> 1);
141f528efe2SAndy Grover }
142f528efe2SAndy Grover
143f528efe2SAndy Grover /*
144*379aecbcSZheng Yongjun * returns the oldest allocated ring entry. This will be the next one
145f528efe2SAndy Grover * freed. This can't be called if there are none allocated.
146f528efe2SAndy Grover */
rds_ib_ring_oldest(struct rds_ib_work_ring * ring)147f528efe2SAndy Grover u32 rds_ib_ring_oldest(struct rds_ib_work_ring *ring)
148f528efe2SAndy Grover {
149f528efe2SAndy Grover return ring->w_free_ptr;
150f528efe2SAndy Grover }
151f528efe2SAndy Grover
152f528efe2SAndy Grover /*
153f528efe2SAndy Grover * returns the number of completed work requests.
154f528efe2SAndy Grover */
155f528efe2SAndy Grover
rds_ib_ring_completed(struct rds_ib_work_ring * ring,u32 wr_id,u32 oldest)156f528efe2SAndy Grover u32 rds_ib_ring_completed(struct rds_ib_work_ring *ring, u32 wr_id, u32 oldest)
157f528efe2SAndy Grover {
158f528efe2SAndy Grover u32 ret;
159f528efe2SAndy Grover
160f528efe2SAndy Grover if (oldest <= (unsigned long long)wr_id)
161f528efe2SAndy Grover ret = (unsigned long long)wr_id - oldest + 1;
162f528efe2SAndy Grover else
163f528efe2SAndy Grover ret = ring->w_nr - oldest + (unsigned long long)wr_id + 1;
164f528efe2SAndy Grover
165f528efe2SAndy Grover rdsdebug("ring %p ret %u wr_id %u oldest %u\n", ring, ret,
166f528efe2SAndy Grover wr_id, oldest);
167f528efe2SAndy Grover return ret;
168f528efe2SAndy Grover }
169