/*1* Copyright (c) 2006 Oracle. All rights reserved.2*3* This software is available to you under a choice of one of two4* licenses. You may choose to be licensed under the terms of the GNU5* General Public License (GPL) Version 2, available from the file6* COPYING in the main directory of this source tree, or the7* OpenIB.org BSD license below:8*9* Redistribution and use in source and binary forms, with or10* without modification, are permitted provided that the following11* conditions are met:12*13* - Redistributions of source code must retain the above14* copyright notice, this list of conditions and the following15* disclaimer.16*17* - Redistributions in binary form must reproduce the above18* copyright notice, this list of conditions and the following19* disclaimer in the documentation and/or other materials20* provided with the distribution.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND25* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS26* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN27* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN28* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE29* SOFTWARE.30*31*/32#include <linux/kernel.h>3334#include "rds.h"35#include "iw.h"3637/*38* Locking for IB rings.39* We assume that allocation is always protected by a mutex40* in the caller (this is a valid assumption for the current41* implementation).42*43* Freeing always happens in an interrupt, and hence only44* races with allocations, but not with other free()s.45*46* The interaction between allocation and freeing is that47* the alloc code has to determine the number of free entries.48* To this end, we maintain two counters; an allocation counter49* and a free counter. Both are allowed to run freely, and wrap50* around.51* The number of used entries is always (alloc_ctr - free_ctr) % NR.52*53* The current implementation makes free_ctr atomic. When the54* caller finds an allocation fails, it should set an "alloc fail"55* bit and retry the allocation. The "alloc fail" bit essentially tells56* the CQ completion handlers to wake it up after freeing some57* more entries.58*/5960/*61* This only happens on shutdown.62*/63DECLARE_WAIT_QUEUE_HEAD(rds_iw_ring_empty_wait);6465void rds_iw_ring_init(struct rds_iw_work_ring *ring, u32 nr)66{67memset(ring, 0, sizeof(*ring));68ring->w_nr = nr;69rdsdebug("ring %p nr %u\n", ring, ring->w_nr);70}7172static inline u32 __rds_iw_ring_used(struct rds_iw_work_ring *ring)73{74u32 diff;7576/* This assumes that atomic_t has at least as many bits as u32 */77diff = ring->w_alloc_ctr - (u32) atomic_read(&ring->w_free_ctr);78BUG_ON(diff > ring->w_nr);7980return diff;81}8283void rds_iw_ring_resize(struct rds_iw_work_ring *ring, u32 nr)84{85/* We only ever get called from the connection setup code,86* prior to creating the QP. */87BUG_ON(__rds_iw_ring_used(ring));88ring->w_nr = nr;89}9091static int __rds_iw_ring_empty(struct rds_iw_work_ring *ring)92{93return __rds_iw_ring_used(ring) == 0;94}9596u32 rds_iw_ring_alloc(struct rds_iw_work_ring *ring, u32 val, u32 *pos)97{98u32 ret = 0, avail;99100avail = ring->w_nr - __rds_iw_ring_used(ring);101102rdsdebug("ring %p val %u next %u free %u\n", ring, val,103ring->w_alloc_ptr, avail);104105if (val && avail) {106ret = min(val, avail);107*pos = ring->w_alloc_ptr;108109ring->w_alloc_ptr = (ring->w_alloc_ptr + ret) % ring->w_nr;110ring->w_alloc_ctr += ret;111}112113return ret;114}115116void rds_iw_ring_free(struct rds_iw_work_ring *ring, u32 val)117{118ring->w_free_ptr = (ring->w_free_ptr + val) % ring->w_nr;119atomic_add(val, &ring->w_free_ctr);120121if (__rds_iw_ring_empty(ring) &&122waitqueue_active(&rds_iw_ring_empty_wait))123wake_up(&rds_iw_ring_empty_wait);124}125126void rds_iw_ring_unalloc(struct rds_iw_work_ring *ring, u32 val)127{128ring->w_alloc_ptr = (ring->w_alloc_ptr - val) % ring->w_nr;129ring->w_alloc_ctr -= val;130}131132int rds_iw_ring_empty(struct rds_iw_work_ring *ring)133{134return __rds_iw_ring_empty(ring);135}136137int rds_iw_ring_low(struct rds_iw_work_ring *ring)138{139return __rds_iw_ring_used(ring) <= (ring->w_nr >> 1);140}141142143/*144* returns the oldest alloced ring entry. This will be the next one145* freed. This can't be called if there are none allocated.146*/147u32 rds_iw_ring_oldest(struct rds_iw_work_ring *ring)148{149return ring->w_free_ptr;150}151152/*153* returns the number of completed work requests.154*/155156u32 rds_iw_ring_completed(struct rds_iw_work_ring *ring, u32 wr_id, u32 oldest)157{158u32 ret;159160if (oldest <= (unsigned long long)wr_id)161ret = (unsigned long long)wr_id - oldest + 1;162else163ret = ring->w_nr - oldest + (unsigned long long)wr_id + 1;164165rdsdebug("ring %p ret %u wr_id %u oldest %u\n", ring, ret,166wr_id, oldest);167return ret;168}169170171