1beec0239SBob Pearson // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2beec0239SBob Pearson /* 3beec0239SBob Pearson * Copyright (c) 2020 Hewlett Packard Enterprise, Inc. All rights reserved. 4beec0239SBob Pearson */ 5beec0239SBob Pearson 6beec0239SBob Pearson #include "rxe.h" 7beec0239SBob Pearson 8beec0239SBob Pearson int rxe_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata) 9beec0239SBob Pearson { 10beec0239SBob Pearson struct rxe_mw *mw = to_rmw(ibmw); 11beec0239SBob Pearson struct rxe_pd *pd = to_rpd(ibmw->pd); 12beec0239SBob Pearson struct rxe_dev *rxe = to_rdev(ibmw->device); 13beec0239SBob Pearson int ret; 14beec0239SBob Pearson 15beec0239SBob Pearson rxe_add_ref(pd); 16beec0239SBob Pearson 17beec0239SBob Pearson ret = rxe_add_to_pool(&rxe->mw_pool, mw); 18beec0239SBob Pearson if (ret) { 19beec0239SBob Pearson rxe_drop_ref(pd); 20beec0239SBob Pearson return ret; 21beec0239SBob Pearson } 22beec0239SBob Pearson 23beec0239SBob Pearson rxe_add_index(mw); 24beec0239SBob Pearson ibmw->rkey = (mw->pelem.index << 8) | rxe_get_next_key(-1); 25beec0239SBob Pearson mw->state = (mw->ibmw.type == IB_MW_TYPE_2) ? 26beec0239SBob Pearson RXE_MW_STATE_FREE : RXE_MW_STATE_VALID; 27beec0239SBob Pearson spin_lock_init(&mw->lock); 28beec0239SBob Pearson 29beec0239SBob Pearson return 0; 30beec0239SBob Pearson } 31beec0239SBob Pearson 3232a577b4SBob Pearson static void rxe_do_dealloc_mw(struct rxe_mw *mw) 3332a577b4SBob Pearson { 3432a577b4SBob Pearson if (mw->mr) { 3532a577b4SBob Pearson struct rxe_mr *mr = mw->mr; 3632a577b4SBob Pearson 3732a577b4SBob Pearson mw->mr = NULL; 3832a577b4SBob Pearson atomic_dec(&mr->num_mw); 3932a577b4SBob Pearson rxe_drop_ref(mr); 4032a577b4SBob Pearson } 4132a577b4SBob Pearson 4232a577b4SBob Pearson if (mw->qp) { 4332a577b4SBob Pearson struct rxe_qp *qp = mw->qp; 4432a577b4SBob Pearson 4532a577b4SBob Pearson mw->qp = NULL; 4632a577b4SBob Pearson rxe_drop_ref(qp); 4732a577b4SBob Pearson } 4832a577b4SBob Pearson 4932a577b4SBob Pearson mw->access = 0; 5032a577b4SBob Pearson mw->addr = 0; 5132a577b4SBob Pearson mw->length = 0; 5232a577b4SBob Pearson mw->state = RXE_MW_STATE_INVALID; 5332a577b4SBob Pearson } 5432a577b4SBob Pearson 55beec0239SBob Pearson int rxe_dealloc_mw(struct ib_mw *ibmw) 56beec0239SBob Pearson { 57beec0239SBob Pearson struct rxe_mw *mw = to_rmw(ibmw); 58beec0239SBob Pearson struct rxe_pd *pd = to_rpd(ibmw->pd); 59beec0239SBob Pearson unsigned long flags; 60beec0239SBob Pearson 61beec0239SBob Pearson spin_lock_irqsave(&mw->lock, flags); 6232a577b4SBob Pearson rxe_do_dealloc_mw(mw); 63beec0239SBob Pearson spin_unlock_irqrestore(&mw->lock, flags); 64beec0239SBob Pearson 65beec0239SBob Pearson rxe_drop_ref(mw); 66beec0239SBob Pearson rxe_drop_ref(pd); 67beec0239SBob Pearson 68beec0239SBob Pearson return 0; 69beec0239SBob Pearson } 70beec0239SBob Pearson 7132a577b4SBob Pearson static int rxe_check_bind_mw(struct rxe_qp *qp, struct rxe_send_wqe *wqe, 7232a577b4SBob Pearson struct rxe_mw *mw, struct rxe_mr *mr) 7332a577b4SBob Pearson { 7432a577b4SBob Pearson if (mw->ibmw.type == IB_MW_TYPE_1) { 7532a577b4SBob Pearson if (unlikely(mw->state != RXE_MW_STATE_VALID)) { 7632a577b4SBob Pearson pr_err_once( 7732a577b4SBob Pearson "attempt to bind a type 1 MW not in the valid state\n"); 7832a577b4SBob Pearson return -EINVAL; 7932a577b4SBob Pearson } 8032a577b4SBob Pearson 8132a577b4SBob Pearson /* o10-36.2.2 */ 8232a577b4SBob Pearson if (unlikely((mw->access & IB_ZERO_BASED))) { 8332a577b4SBob Pearson pr_err_once("attempt to bind a zero based type 1 MW\n"); 8432a577b4SBob Pearson return -EINVAL; 8532a577b4SBob Pearson } 8632a577b4SBob Pearson } 8732a577b4SBob Pearson 8832a577b4SBob Pearson if (mw->ibmw.type == IB_MW_TYPE_2) { 8932a577b4SBob Pearson /* o10-37.2.30 */ 9032a577b4SBob Pearson if (unlikely(mw->state != RXE_MW_STATE_FREE)) { 9132a577b4SBob Pearson pr_err_once( 9232a577b4SBob Pearson "attempt to bind a type 2 MW not in the free state\n"); 9332a577b4SBob Pearson return -EINVAL; 9432a577b4SBob Pearson } 9532a577b4SBob Pearson 9632a577b4SBob Pearson /* C10-72 */ 9732a577b4SBob Pearson if (unlikely(qp->pd != to_rpd(mw->ibmw.pd))) { 9832a577b4SBob Pearson pr_err_once( 9932a577b4SBob Pearson "attempt to bind type 2 MW with qp with different PD\n"); 10032a577b4SBob Pearson return -EINVAL; 10132a577b4SBob Pearson } 10232a577b4SBob Pearson 10332a577b4SBob Pearson /* o10-37.2.40 */ 10432a577b4SBob Pearson if (unlikely(!mr || wqe->wr.wr.mw.length == 0)) { 10532a577b4SBob Pearson pr_err_once( 10632a577b4SBob Pearson "attempt to invalidate type 2 MW by binding with NULL or zero length MR\n"); 10732a577b4SBob Pearson return -EINVAL; 10832a577b4SBob Pearson } 10932a577b4SBob Pearson } 11032a577b4SBob Pearson 11132a577b4SBob Pearson if (unlikely((wqe->wr.wr.mw.rkey & 0xff) == (mw->ibmw.rkey & 0xff))) { 11232a577b4SBob Pearson pr_err_once("attempt to bind MW with same key\n"); 11332a577b4SBob Pearson return -EINVAL; 11432a577b4SBob Pearson } 11532a577b4SBob Pearson 11632a577b4SBob Pearson /* remaining checks only apply to a nonzero MR */ 11732a577b4SBob Pearson if (!mr) 11832a577b4SBob Pearson return 0; 11932a577b4SBob Pearson 12032a577b4SBob Pearson if (unlikely(mr->access & IB_ZERO_BASED)) { 12132a577b4SBob Pearson pr_err_once("attempt to bind MW to zero based MR\n"); 12232a577b4SBob Pearson return -EINVAL; 12332a577b4SBob Pearson } 12432a577b4SBob Pearson 12532a577b4SBob Pearson /* C10-73 */ 12632a577b4SBob Pearson if (unlikely(!(mr->access & IB_ACCESS_MW_BIND))) { 12732a577b4SBob Pearson pr_err_once( 12832a577b4SBob Pearson "attempt to bind an MW to an MR without bind access\n"); 12932a577b4SBob Pearson return -EINVAL; 13032a577b4SBob Pearson } 13132a577b4SBob Pearson 13232a577b4SBob Pearson /* C10-74 */ 13332a577b4SBob Pearson if (unlikely((mw->access & 13432a577b4SBob Pearson (IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_ATOMIC)) && 13532a577b4SBob Pearson !(mr->access & IB_ACCESS_LOCAL_WRITE))) { 13632a577b4SBob Pearson pr_err_once( 13732a577b4SBob Pearson "attempt to bind an writeable MW to an MR without local write access\n"); 13832a577b4SBob Pearson return -EINVAL; 13932a577b4SBob Pearson } 14032a577b4SBob Pearson 14132a577b4SBob Pearson /* C10-75 */ 14232a577b4SBob Pearson if (mw->access & IB_ZERO_BASED) { 14332a577b4SBob Pearson if (unlikely(wqe->wr.wr.mw.length > mr->length)) { 14432a577b4SBob Pearson pr_err_once( 14532a577b4SBob Pearson "attempt to bind a ZB MW outside of the MR\n"); 14632a577b4SBob Pearson return -EINVAL; 14732a577b4SBob Pearson } 14832a577b4SBob Pearson } else { 14932a577b4SBob Pearson if (unlikely((wqe->wr.wr.mw.addr < mr->iova) || 15032a577b4SBob Pearson ((wqe->wr.wr.mw.addr + wqe->wr.wr.mw.length) > 15132a577b4SBob Pearson (mr->iova + mr->length)))) { 15232a577b4SBob Pearson pr_err_once( 15332a577b4SBob Pearson "attempt to bind a VA MW outside of the MR\n"); 15432a577b4SBob Pearson return -EINVAL; 15532a577b4SBob Pearson } 15632a577b4SBob Pearson } 15732a577b4SBob Pearson 15832a577b4SBob Pearson return 0; 15932a577b4SBob Pearson } 16032a577b4SBob Pearson 16132a577b4SBob Pearson static void rxe_do_bind_mw(struct rxe_qp *qp, struct rxe_send_wqe *wqe, 16232a577b4SBob Pearson struct rxe_mw *mw, struct rxe_mr *mr) 16332a577b4SBob Pearson { 16432a577b4SBob Pearson u32 rkey; 16532a577b4SBob Pearson u32 new_rkey; 16632a577b4SBob Pearson 16732a577b4SBob Pearson rkey = mw->ibmw.rkey; 16832a577b4SBob Pearson new_rkey = (rkey & 0xffffff00) | (wqe->wr.wr.mw.rkey & 0x000000ff); 16932a577b4SBob Pearson 17032a577b4SBob Pearson mw->ibmw.rkey = new_rkey; 17132a577b4SBob Pearson mw->access = wqe->wr.wr.mw.access; 17232a577b4SBob Pearson mw->state = RXE_MW_STATE_VALID; 17332a577b4SBob Pearson mw->addr = wqe->wr.wr.mw.addr; 17432a577b4SBob Pearson mw->length = wqe->wr.wr.mw.length; 17532a577b4SBob Pearson 17632a577b4SBob Pearson if (mw->mr) { 17732a577b4SBob Pearson rxe_drop_ref(mw->mr); 17832a577b4SBob Pearson atomic_dec(&mw->mr->num_mw); 17932a577b4SBob Pearson mw->mr = NULL; 18032a577b4SBob Pearson } 18132a577b4SBob Pearson 18232a577b4SBob Pearson if (mw->length) { 18332a577b4SBob Pearson mw->mr = mr; 18432a577b4SBob Pearson atomic_inc(&mr->num_mw); 18532a577b4SBob Pearson rxe_add_ref(mr); 18632a577b4SBob Pearson } 18732a577b4SBob Pearson 18832a577b4SBob Pearson if (mw->ibmw.type == IB_MW_TYPE_2) { 18932a577b4SBob Pearson rxe_add_ref(qp); 19032a577b4SBob Pearson mw->qp = qp; 19132a577b4SBob Pearson } 19232a577b4SBob Pearson } 19332a577b4SBob Pearson 19432a577b4SBob Pearson int rxe_bind_mw(struct rxe_qp *qp, struct rxe_send_wqe *wqe) 19532a577b4SBob Pearson { 19632a577b4SBob Pearson int ret; 19732a577b4SBob Pearson struct rxe_mw *mw; 19832a577b4SBob Pearson struct rxe_mr *mr; 19932a577b4SBob Pearson struct rxe_dev *rxe = to_rdev(qp->ibqp.device); 20032a577b4SBob Pearson unsigned long flags; 20132a577b4SBob Pearson 20232a577b4SBob Pearson mw = rxe_pool_get_index(&rxe->mw_pool, 20332a577b4SBob Pearson wqe->wr.wr.mw.mw_rkey >> 8); 20432a577b4SBob Pearson if (unlikely(!mw)) { 20532a577b4SBob Pearson ret = -EINVAL; 20632a577b4SBob Pearson goto err; 20732a577b4SBob Pearson } 20832a577b4SBob Pearson 20932a577b4SBob Pearson if (unlikely(mw->ibmw.rkey != wqe->wr.wr.mw.mw_rkey)) { 21032a577b4SBob Pearson ret = -EINVAL; 21132a577b4SBob Pearson goto err_drop_mw; 21232a577b4SBob Pearson } 21332a577b4SBob Pearson 21432a577b4SBob Pearson if (likely(wqe->wr.wr.mw.length)) { 21532a577b4SBob Pearson mr = rxe_pool_get_index(&rxe->mr_pool, 21632a577b4SBob Pearson wqe->wr.wr.mw.mr_lkey >> 8); 21732a577b4SBob Pearson if (unlikely(!mr)) { 21832a577b4SBob Pearson ret = -EINVAL; 21932a577b4SBob Pearson goto err_drop_mw; 22032a577b4SBob Pearson } 22132a577b4SBob Pearson 22232a577b4SBob Pearson if (unlikely(mr->ibmr.lkey != wqe->wr.wr.mw.mr_lkey)) { 22332a577b4SBob Pearson ret = -EINVAL; 22432a577b4SBob Pearson goto err_drop_mr; 22532a577b4SBob Pearson } 22632a577b4SBob Pearson } else { 22732a577b4SBob Pearson mr = NULL; 22832a577b4SBob Pearson } 22932a577b4SBob Pearson 23032a577b4SBob Pearson spin_lock_irqsave(&mw->lock, flags); 23132a577b4SBob Pearson 23232a577b4SBob Pearson ret = rxe_check_bind_mw(qp, wqe, mw, mr); 23332a577b4SBob Pearson if (ret) 23432a577b4SBob Pearson goto err_unlock; 23532a577b4SBob Pearson 23632a577b4SBob Pearson rxe_do_bind_mw(qp, wqe, mw, mr); 23732a577b4SBob Pearson err_unlock: 23832a577b4SBob Pearson spin_unlock_irqrestore(&mw->lock, flags); 23932a577b4SBob Pearson err_drop_mr: 24032a577b4SBob Pearson if (mr) 24132a577b4SBob Pearson rxe_drop_ref(mr); 24232a577b4SBob Pearson err_drop_mw: 24332a577b4SBob Pearson rxe_drop_ref(mw); 24432a577b4SBob Pearson err: 24532a577b4SBob Pearson return ret; 24632a577b4SBob Pearson } 24732a577b4SBob Pearson 2483902b429SBob Pearson static int rxe_check_invalidate_mw(struct rxe_qp *qp, struct rxe_mw *mw) 2493902b429SBob Pearson { 2503902b429SBob Pearson if (unlikely(mw->state == RXE_MW_STATE_INVALID)) 2513902b429SBob Pearson return -EINVAL; 2523902b429SBob Pearson 2533902b429SBob Pearson /* o10-37.2.26 */ 2543902b429SBob Pearson if (unlikely(mw->ibmw.type == IB_MW_TYPE_1)) 2553902b429SBob Pearson return -EINVAL; 2563902b429SBob Pearson 2573902b429SBob Pearson return 0; 2583902b429SBob Pearson } 2593902b429SBob Pearson 2603902b429SBob Pearson static void rxe_do_invalidate_mw(struct rxe_mw *mw) 2613902b429SBob Pearson { 2623902b429SBob Pearson struct rxe_qp *qp; 2633902b429SBob Pearson struct rxe_mr *mr; 2643902b429SBob Pearson 2653902b429SBob Pearson /* valid type 2 MW will always have a QP pointer */ 2663902b429SBob Pearson qp = mw->qp; 2673902b429SBob Pearson mw->qp = NULL; 2683902b429SBob Pearson rxe_drop_ref(qp); 2693902b429SBob Pearson 2703902b429SBob Pearson /* valid type 2 MW will always have an MR pointer */ 2713902b429SBob Pearson mr = mw->mr; 2723902b429SBob Pearson mw->mr = NULL; 2733902b429SBob Pearson atomic_dec(&mr->num_mw); 2743902b429SBob Pearson rxe_drop_ref(mr); 2753902b429SBob Pearson 2763902b429SBob Pearson mw->access = 0; 2773902b429SBob Pearson mw->addr = 0; 2783902b429SBob Pearson mw->length = 0; 2793902b429SBob Pearson mw->state = RXE_MW_STATE_FREE; 2803902b429SBob Pearson } 2813902b429SBob Pearson 2823902b429SBob Pearson int rxe_invalidate_mw(struct rxe_qp *qp, u32 rkey) 2833902b429SBob Pearson { 2843902b429SBob Pearson struct rxe_dev *rxe = to_rdev(qp->ibqp.device); 2853902b429SBob Pearson unsigned long flags; 2863902b429SBob Pearson struct rxe_mw *mw; 2873902b429SBob Pearson int ret; 2883902b429SBob Pearson 2893902b429SBob Pearson mw = rxe_pool_get_index(&rxe->mw_pool, rkey >> 8); 2903902b429SBob Pearson if (!mw) { 2913902b429SBob Pearson ret = -EINVAL; 2923902b429SBob Pearson goto err; 2933902b429SBob Pearson } 2943902b429SBob Pearson 2953902b429SBob Pearson if (rkey != mw->ibmw.rkey) { 2963902b429SBob Pearson ret = -EINVAL; 2973902b429SBob Pearson goto err_drop_ref; 2983902b429SBob Pearson } 2993902b429SBob Pearson 3003902b429SBob Pearson spin_lock_irqsave(&mw->lock, flags); 3013902b429SBob Pearson 3023902b429SBob Pearson ret = rxe_check_invalidate_mw(qp, mw); 3033902b429SBob Pearson if (ret) 3043902b429SBob Pearson goto err_unlock; 3053902b429SBob Pearson 3063902b429SBob Pearson rxe_do_invalidate_mw(mw); 3073902b429SBob Pearson err_unlock: 3083902b429SBob Pearson spin_unlock_irqrestore(&mw->lock, flags); 3093902b429SBob Pearson err_drop_ref: 3103902b429SBob Pearson rxe_drop_ref(mw); 3113902b429SBob Pearson err: 3123902b429SBob Pearson return ret; 3133902b429SBob Pearson } 3143902b429SBob Pearson 315*cdd0b856SBob Pearson struct rxe_mw *rxe_lookup_mw(struct rxe_qp *qp, int access, u32 rkey) 316*cdd0b856SBob Pearson { 317*cdd0b856SBob Pearson struct rxe_dev *rxe = to_rdev(qp->ibqp.device); 318*cdd0b856SBob Pearson struct rxe_pd *pd = to_rpd(qp->ibqp.pd); 319*cdd0b856SBob Pearson struct rxe_mw *mw; 320*cdd0b856SBob Pearson int index = rkey >> 8; 321*cdd0b856SBob Pearson 322*cdd0b856SBob Pearson mw = rxe_pool_get_index(&rxe->mw_pool, index); 323*cdd0b856SBob Pearson if (!mw) 324*cdd0b856SBob Pearson return NULL; 325*cdd0b856SBob Pearson 326*cdd0b856SBob Pearson if (unlikely((rxe_mw_rkey(mw) != rkey) || rxe_mw_pd(mw) != pd || 327*cdd0b856SBob Pearson (mw->ibmw.type == IB_MW_TYPE_2 && mw->qp != qp) || 328*cdd0b856SBob Pearson (mw->length == 0) || 329*cdd0b856SBob Pearson (access && !(access & mw->access)) || 330*cdd0b856SBob Pearson mw->state != RXE_MW_STATE_VALID)) { 331*cdd0b856SBob Pearson rxe_drop_ref(mw); 332*cdd0b856SBob Pearson return NULL; 333*cdd0b856SBob Pearson } 334*cdd0b856SBob Pearson 335*cdd0b856SBob Pearson return mw; 336*cdd0b856SBob Pearson } 337*cdd0b856SBob Pearson 338beec0239SBob Pearson void rxe_mw_cleanup(struct rxe_pool_entry *elem) 339beec0239SBob Pearson { 340beec0239SBob Pearson struct rxe_mw *mw = container_of(elem, typeof(*mw), pelem); 341beec0239SBob Pearson 342beec0239SBob Pearson rxe_drop_index(mw); 343beec0239SBob Pearson } 344