c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri * This file contains code imported from the OFED rds source file ib_ring.c
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri * Oracle elects to have and use the contents of ib_ring.c under and governed
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri * by the OpenIB.org BSD license (see below for full license text). However,
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri * the following notice accompanied the original version of this file:
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * Copyright (c) 2006 Oracle. All rights reserved.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * This software is available to you under a choice of one of two
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * licenses. You may choose to be licensed under the terms of the GNU
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * General Public License (GPL) Version 2, available from the file
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * COPYING in the main directory of this source tree, or the
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * OpenIB.org BSD license below:
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * Redistribution and use in source and binary forms, with or
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * without modification, are permitted provided that the following
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * conditions are met:
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * - Redistributions of source code must retain the above
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * copyright notice, this list of conditions and the following
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * disclaimer.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * - Redistributions in binary form must reproduce the above
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * copyright notice, this list of conditions and the following
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * disclaimer in the documentation and/or other materials
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * provided with the distribution.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * SOFTWARE.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/rds.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/ib/clients/rdsv3/rdsv3.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/ib/clients/rdsv3/ib.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/ib/clients/rdsv3/rdsv3_debug.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * Locking for IB rings.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * We assume that allocation is always protected by a mutex
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * in the caller (this is a valid assumption for the current
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * implementation).
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * Freeing always happens in an interrupt, and hence only
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * races with allocations, but not with other free()s.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * The interaction between allocation and freeing is that
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * the alloc code has to determine the number of free entries.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * To this end, we maintain two counters; an allocation counter
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * and a free counter. Both are allowed to run freely, and wrap
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * around.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * The number of used entries is always (alloc_ctr - free_ctr) % NR.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * The current implementation makes free_ctr atomic. When the
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * caller finds an allocation fails, it should set an "alloc fail"
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * bit and retry the allocation. The "alloc fail" bit essentially tells
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * the CQ completion handlers to wake it up after freeing some
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * more entries.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otavoid
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_ib_ring_init(struct rdsv3_ib_work_ring *ring, uint32_t nr)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota (void) memset(ring, 0, sizeof (*ring));
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ring->w_nr = nr;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota RDSV3_DPRINTF5("rdsv3_ib_ring_init", "ring %p nr %u", ring, ring->w_nr);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otastatic inline uint32_t
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota__rdsv3_ib_ring_used(struct rdsv3_ib_work_ring *ring)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota uint32_t diff;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota /* This assumes that atomic_t has at least as many bits as uint32_t */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota diff = ring->w_alloc_ctr - (uint32_t)atomic_get(&ring->w_free_ctr);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ASSERT(diff <= ring->w_nr);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (diff);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otavoid
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_ib_ring_resize(struct rdsv3_ib_work_ring *ring, uint32_t nr)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota /*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * We only ever get called from the connection setup code,
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * prior to creating the QP.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ASSERT(!__rdsv3_ib_ring_used(ring));
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ring->w_nr = nr;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otastatic int
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota__rdsv3_ib_ring_empty(struct rdsv3_ib_work_ring *ring)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (__rdsv3_ib_ring_used(ring) == 0);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otauint32_t
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_ib_ring_alloc(struct rdsv3_ib_work_ring *ring, uint32_t val,
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota uint32_t *pos)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota uint32_t ret = 0, avail;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota avail = ring->w_nr - __rdsv3_ib_ring_used(ring);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota RDSV3_DPRINTF5("rdsv3_ib_ring_alloc",
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota "ring %p val %u next %u free %u", ring, val,
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ring->w_alloc_ptr, avail);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota if (val && avail) {
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ret = min(val, avail);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *pos = ring->w_alloc_ptr;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ring->w_alloc_ptr = (ring->w_alloc_ptr + ret) % ring->w_nr;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ring->w_alloc_ctr += ret;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota }
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (ret);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otavoid
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_ib_ring_free(struct rdsv3_ib_work_ring *ring, uint32_t val)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ring->w_free_ptr = (ring->w_free_ptr + val) % ring->w_nr;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota atomic_add_32(&ring->w_free_ctr, val);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota if (__rdsv3_ib_ring_empty(ring))
5d5562f583b2b6affe19bdce0b3c8b1840d667a4Eiji Ota rdsv3_wake_up(&ring->w_empty_wait);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otavoid
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_ib_ring_unalloc(struct rdsv3_ib_work_ring *ring, uint32_t val)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ring->w_alloc_ptr = (ring->w_alloc_ptr - val) % ring->w_nr;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ring->w_alloc_ctr -= val;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otaint
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_ib_ring_empty(struct rdsv3_ib_work_ring *ring)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (__rdsv3_ib_ring_empty(ring));
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otaint
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_ib_ring_low(struct rdsv3_ib_work_ring *ring)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota return (__rdsv3_ib_ring_used(ring) <= (ring->w_nr >> 2));
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * returns the oldest alloced ring entry. This will be the next one
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * freed. This can't be called if there are none allocated.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otauint32_t
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_ib_ring_oldest(struct rdsv3_ib_work_ring *ring)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (ring->w_free_ptr);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * returns the number of completed work requests.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otauint32_t
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_ib_ring_completed(struct rdsv3_ib_work_ring *ring,
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota uint32_t wr_id, uint32_t oldest)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota uint32_t ret;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota if (oldest <= (unsigned long long)wr_id)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ret = (unsigned long long)wr_id - oldest + 1;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota else
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ret = ring->w_nr - oldest + (unsigned long long)wr_id + 1;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota RDSV3_DPRINTF5("rdsv3_ib_ring_completed",
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota "ring %p ret %u wr_id %u oldest %u", ring, ret, wr_id, oldest);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (ret);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}