18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2006 Oracle.  All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
58c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
118c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
128c2ecf20Sopenharmony_ci *     conditions are met:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
158c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
168c2ecf20Sopenharmony_ci *        disclaimer.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
198c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
208c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
218c2ecf20Sopenharmony_ci *        provided with the distribution.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
308c2ecf20Sopenharmony_ci * SOFTWARE.
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci#include <linux/kernel.h>
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include "rds.h"
368c2ecf20Sopenharmony_ci#include "ib.h"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/*
398c2ecf20Sopenharmony_ci * Locking for IB rings.
408c2ecf20Sopenharmony_ci * We assume that allocation is always protected by a mutex
418c2ecf20Sopenharmony_ci * in the caller (this is a valid assumption for the current
428c2ecf20Sopenharmony_ci * implementation).
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * Freeing always happens in an interrupt, and hence only
458c2ecf20Sopenharmony_ci * races with allocations, but not with other free()s.
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * The interaction between allocation and freeing is that
488c2ecf20Sopenharmony_ci * the alloc code has to determine the number of free entries.
498c2ecf20Sopenharmony_ci * To this end, we maintain two counters; an allocation counter
508c2ecf20Sopenharmony_ci * and a free counter. Both are allowed to run freely, and wrap
518c2ecf20Sopenharmony_ci * around.
528c2ecf20Sopenharmony_ci * The number of used entries is always (alloc_ctr - free_ctr) % NR.
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * The current implementation makes free_ctr atomic. When the
558c2ecf20Sopenharmony_ci * caller finds an allocation fails, it should set an "alloc fail"
568c2ecf20Sopenharmony_ci * bit and retry the allocation. The "alloc fail" bit essentially tells
578c2ecf20Sopenharmony_ci * the CQ completion handlers to wake it up after freeing some
588c2ecf20Sopenharmony_ci * more entries.
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/*
628c2ecf20Sopenharmony_ci * This only happens on shutdown.
638c2ecf20Sopenharmony_ci */
648c2ecf20Sopenharmony_ciDECLARE_WAIT_QUEUE_HEAD(rds_ib_ring_empty_wait);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_civoid rds_ib_ring_init(struct rds_ib_work_ring *ring, u32 nr)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	memset(ring, 0, sizeof(*ring));
698c2ecf20Sopenharmony_ci	ring->w_nr = nr;
708c2ecf20Sopenharmony_ci	rdsdebug("ring %p nr %u\n", ring, ring->w_nr);
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic inline u32 __rds_ib_ring_used(struct rds_ib_work_ring *ring)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	u32 diff;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* This assumes that atomic_t has at least as many bits as u32 */
788c2ecf20Sopenharmony_ci	diff = ring->w_alloc_ctr - (u32) atomic_read(&ring->w_free_ctr);
798c2ecf20Sopenharmony_ci	BUG_ON(diff > ring->w_nr);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return diff;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_civoid rds_ib_ring_resize(struct rds_ib_work_ring *ring, u32 nr)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	/* We only ever get called from the connection setup code,
878c2ecf20Sopenharmony_ci	 * prior to creating the QP. */
888c2ecf20Sopenharmony_ci	BUG_ON(__rds_ib_ring_used(ring));
898c2ecf20Sopenharmony_ci	ring->w_nr = nr;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic int __rds_ib_ring_empty(struct rds_ib_work_ring *ring)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	return __rds_ib_ring_used(ring) == 0;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ciu32 rds_ib_ring_alloc(struct rds_ib_work_ring *ring, u32 val, u32 *pos)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	u32 ret = 0, avail;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	avail = ring->w_nr - __rds_ib_ring_used(ring);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	rdsdebug("ring %p val %u next %u free %u\n", ring, val,
1048c2ecf20Sopenharmony_ci		 ring->w_alloc_ptr, avail);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	if (val && avail) {
1078c2ecf20Sopenharmony_ci		ret = min(val, avail);
1088c2ecf20Sopenharmony_ci		*pos = ring->w_alloc_ptr;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci		ring->w_alloc_ptr = (ring->w_alloc_ptr + ret) % ring->w_nr;
1118c2ecf20Sopenharmony_ci		ring->w_alloc_ctr += ret;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return ret;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_civoid rds_ib_ring_free(struct rds_ib_work_ring *ring, u32 val)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	ring->w_free_ptr = (ring->w_free_ptr + val) % ring->w_nr;
1208c2ecf20Sopenharmony_ci	atomic_add(val, &ring->w_free_ctr);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	if (__rds_ib_ring_empty(ring) &&
1238c2ecf20Sopenharmony_ci	    waitqueue_active(&rds_ib_ring_empty_wait))
1248c2ecf20Sopenharmony_ci		wake_up(&rds_ib_ring_empty_wait);
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_civoid rds_ib_ring_unalloc(struct rds_ib_work_ring *ring, u32 val)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	ring->w_alloc_ptr = (ring->w_alloc_ptr - val) % ring->w_nr;
1308c2ecf20Sopenharmony_ci	ring->w_alloc_ctr -= val;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ciint rds_ib_ring_empty(struct rds_ib_work_ring *ring)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	return __rds_ib_ring_empty(ring);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ciint rds_ib_ring_low(struct rds_ib_work_ring *ring)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	return __rds_ib_ring_used(ring) <= (ring->w_nr >> 1);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/*
1448c2ecf20Sopenharmony_ci * returns the oldest alloced ring entry.  This will be the next one
1458c2ecf20Sopenharmony_ci * freed.  This can't be called if there are none allocated.
1468c2ecf20Sopenharmony_ci */
1478c2ecf20Sopenharmony_ciu32 rds_ib_ring_oldest(struct rds_ib_work_ring *ring)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	return ring->w_free_ptr;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/*
1538c2ecf20Sopenharmony_ci * returns the number of completed work requests.
1548c2ecf20Sopenharmony_ci */
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciu32 rds_ib_ring_completed(struct rds_ib_work_ring *ring, u32 wr_id, u32 oldest)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	u32 ret;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	if (oldest <= (unsigned long long)wr_id)
1618c2ecf20Sopenharmony_ci		ret = (unsigned long long)wr_id - oldest + 1;
1628c2ecf20Sopenharmony_ci	else
1638c2ecf20Sopenharmony_ci		ret = ring->w_nr - oldest + (unsigned long long)wr_id + 1;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	rdsdebug("ring %p ret %u wr_id %u oldest %u\n", ring, ret,
1668c2ecf20Sopenharmony_ci		 wr_id, oldest);
1678c2ecf20Sopenharmony_ci	return ret;
1688c2ecf20Sopenharmony_ci}
169