1*beec0239SBob Pearson // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2*beec0239SBob Pearson /* 3*beec0239SBob Pearson * Copyright (c) 2020 Hewlett Packard Enterprise, Inc. All rights reserved. 4*beec0239SBob Pearson */ 5*beec0239SBob Pearson 6*beec0239SBob Pearson #include "rxe.h" 7*beec0239SBob Pearson 8*beec0239SBob Pearson int rxe_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata) 9*beec0239SBob Pearson { 10*beec0239SBob Pearson struct rxe_mw *mw = to_rmw(ibmw); 11*beec0239SBob Pearson struct rxe_pd *pd = to_rpd(ibmw->pd); 12*beec0239SBob Pearson struct rxe_dev *rxe = to_rdev(ibmw->device); 13*beec0239SBob Pearson int ret; 14*beec0239SBob Pearson 15*beec0239SBob Pearson rxe_add_ref(pd); 16*beec0239SBob Pearson 17*beec0239SBob Pearson ret = rxe_add_to_pool(&rxe->mw_pool, mw); 18*beec0239SBob Pearson if (ret) { 19*beec0239SBob Pearson rxe_drop_ref(pd); 20*beec0239SBob Pearson return ret; 21*beec0239SBob Pearson } 22*beec0239SBob Pearson 23*beec0239SBob Pearson rxe_add_index(mw); 24*beec0239SBob Pearson ibmw->rkey = (mw->pelem.index << 8) | rxe_get_next_key(-1); 25*beec0239SBob Pearson mw->state = (mw->ibmw.type == IB_MW_TYPE_2) ? 26*beec0239SBob Pearson RXE_MW_STATE_FREE : RXE_MW_STATE_VALID; 27*beec0239SBob Pearson spin_lock_init(&mw->lock); 28*beec0239SBob Pearson 29*beec0239SBob Pearson return 0; 30*beec0239SBob Pearson } 31*beec0239SBob Pearson 32*beec0239SBob Pearson int rxe_dealloc_mw(struct ib_mw *ibmw) 33*beec0239SBob Pearson { 34*beec0239SBob Pearson struct rxe_mw *mw = to_rmw(ibmw); 35*beec0239SBob Pearson struct rxe_pd *pd = to_rpd(ibmw->pd); 36*beec0239SBob Pearson unsigned long flags; 37*beec0239SBob Pearson 38*beec0239SBob Pearson spin_lock_irqsave(&mw->lock, flags); 39*beec0239SBob Pearson mw->state = RXE_MW_STATE_INVALID; 40*beec0239SBob Pearson spin_unlock_irqrestore(&mw->lock, flags); 41*beec0239SBob Pearson 42*beec0239SBob Pearson rxe_drop_ref(mw); 43*beec0239SBob Pearson rxe_drop_ref(pd); 44*beec0239SBob Pearson 45*beec0239SBob Pearson return 0; 46*beec0239SBob Pearson } 47*beec0239SBob Pearson 48*beec0239SBob Pearson void rxe_mw_cleanup(struct rxe_pool_entry *elem) 49*beec0239SBob Pearson { 50*beec0239SBob Pearson struct rxe_mw *mw = container_of(elem, typeof(*mw), pelem); 51*beec0239SBob Pearson 52*beec0239SBob Pearson rxe_drop_index(mw); 53*beec0239SBob Pearson } 54