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
69227b6ceSBob Pearson /*
79227b6ceSBob Pearson * The rdma_rxe driver supports type 1 or type 2B memory windows.
89227b6ceSBob Pearson * Type 1 MWs are created by ibv_alloc_mw() verbs calls and bound by
99227b6ceSBob Pearson * ibv_bind_mw() calls. Type 2 MWs are also created by ibv_alloc_mw()
109227b6ceSBob Pearson * but bound by bind_mw work requests. The ibv_bind_mw() call is converted
119227b6ceSBob Pearson * by libibverbs to a bind_mw work request.
129227b6ceSBob Pearson */
139227b6ceSBob Pearson
14beec0239SBob Pearson #include "rxe.h"
15beec0239SBob Pearson
rxe_alloc_mw(struct ib_mw * ibmw,struct ib_udata * udata)16beec0239SBob Pearson int rxe_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata)
17beec0239SBob Pearson {
18beec0239SBob Pearson struct rxe_mw *mw = to_rmw(ibmw);
19beec0239SBob Pearson struct rxe_pd *pd = to_rpd(ibmw->pd);
20beec0239SBob Pearson struct rxe_dev *rxe = to_rdev(ibmw->device);
21beec0239SBob Pearson int ret;
22beec0239SBob Pearson
233197706aSBob Pearson rxe_get(pd);
24beec0239SBob Pearson
25beec0239SBob Pearson ret = rxe_add_to_pool(&rxe->mw_pool, mw);
26beec0239SBob Pearson if (ret) {
273197706aSBob Pearson rxe_put(pd);
28beec0239SBob Pearson return ret;
29beec0239SBob Pearson }
30beec0239SBob Pearson
3102827b67SBob Pearson mw->rkey = ibmw->rkey = (mw->elem.index << 8) | rxe_get_next_key(-1);
32beec0239SBob Pearson mw->state = (mw->ibmw.type == IB_MW_TYPE_2) ?
33beec0239SBob Pearson RXE_MW_STATE_FREE : RXE_MW_STATE_VALID;
34beec0239SBob Pearson spin_lock_init(&mw->lock);
35beec0239SBob Pearson
36215d0a75SBob Pearson rxe_finalize(mw);
37215d0a75SBob Pearson
38beec0239SBob Pearson return 0;
39beec0239SBob Pearson }
40beec0239SBob Pearson
rxe_dealloc_mw(struct ib_mw * ibmw)41beec0239SBob Pearson int rxe_dealloc_mw(struct ib_mw *ibmw)
42beec0239SBob Pearson {
43beec0239SBob Pearson struct rxe_mw *mw = to_rmw(ibmw);
44beec0239SBob Pearson
45215d0a75SBob Pearson rxe_cleanup(mw);
46beec0239SBob Pearson
47beec0239SBob Pearson return 0;
48beec0239SBob Pearson }
49beec0239SBob Pearson
rxe_check_bind_mw(struct rxe_qp * qp,struct rxe_send_wqe * wqe,struct rxe_mw * mw,struct rxe_mr * mr,int access)5032a577b4SBob Pearson static int rxe_check_bind_mw(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
51425e1c90SBob Pearson struct rxe_mw *mw, struct rxe_mr *mr, int access)
5232a577b4SBob Pearson {
5332a577b4SBob Pearson if (mw->ibmw.type == IB_MW_TYPE_1) {
5432a577b4SBob Pearson if (unlikely(mw->state != RXE_MW_STATE_VALID)) {
55e8a87efdSBob Pearson rxe_dbg_mw(mw,
5632a577b4SBob Pearson "attempt to bind a type 1 MW not in the valid state\n");
5732a577b4SBob Pearson return -EINVAL;
5832a577b4SBob Pearson }
5932a577b4SBob Pearson
6032a577b4SBob Pearson /* o10-36.2.2 */
61425e1c90SBob Pearson if (unlikely((access & IB_ZERO_BASED))) {
62e8a87efdSBob Pearson rxe_dbg_mw(mw, "attempt to bind a zero based type 1 MW\n");
6332a577b4SBob Pearson return -EINVAL;
6432a577b4SBob Pearson }
6532a577b4SBob Pearson }
6632a577b4SBob Pearson
6732a577b4SBob Pearson if (mw->ibmw.type == IB_MW_TYPE_2) {
6832a577b4SBob Pearson /* o10-37.2.30 */
6932a577b4SBob Pearson if (unlikely(mw->state != RXE_MW_STATE_FREE)) {
70e8a87efdSBob Pearson rxe_dbg_mw(mw,
7132a577b4SBob Pearson "attempt to bind a type 2 MW not in the free state\n");
7232a577b4SBob Pearson return -EINVAL;
7332a577b4SBob Pearson }
7432a577b4SBob Pearson
7532a577b4SBob Pearson /* C10-72 */
7632a577b4SBob Pearson if (unlikely(qp->pd != to_rpd(mw->ibmw.pd))) {
77e8a87efdSBob Pearson rxe_dbg_mw(mw,
7832a577b4SBob Pearson "attempt to bind type 2 MW with qp with different PD\n");
7932a577b4SBob Pearson return -EINVAL;
8032a577b4SBob Pearson }
8132a577b4SBob Pearson
8232a577b4SBob Pearson /* o10-37.2.40 */
8332a577b4SBob Pearson if (unlikely(!mr || wqe->wr.wr.mw.length == 0)) {
84e8a87efdSBob Pearson rxe_dbg_mw(mw,
8532a577b4SBob Pearson "attempt to invalidate type 2 MW by binding with NULL or zero length MR\n");
8632a577b4SBob Pearson return -EINVAL;
8732a577b4SBob Pearson }
8832a577b4SBob Pearson }
8932a577b4SBob Pearson
9032a577b4SBob Pearson /* remaining checks only apply to a nonzero MR */
9132a577b4SBob Pearson if (!mr)
9232a577b4SBob Pearson return 0;
9332a577b4SBob Pearson
9432a577b4SBob Pearson if (unlikely(mr->access & IB_ZERO_BASED)) {
95e8a87efdSBob Pearson rxe_dbg_mw(mw, "attempt to bind MW to zero based MR\n");
9632a577b4SBob Pearson return -EINVAL;
9732a577b4SBob Pearson }
9832a577b4SBob Pearson
9932a577b4SBob Pearson /* C10-73 */
10032a577b4SBob Pearson if (unlikely(!(mr->access & IB_ACCESS_MW_BIND))) {
101e8a87efdSBob Pearson rxe_dbg_mw(mw,
10232a577b4SBob Pearson "attempt to bind an MW to an MR without bind access\n");
10332a577b4SBob Pearson return -EINVAL;
10432a577b4SBob Pearson }
10532a577b4SBob Pearson
10632a577b4SBob Pearson /* C10-74 */
107425e1c90SBob Pearson if (unlikely((access &
10832a577b4SBob Pearson (IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_ATOMIC)) &&
10932a577b4SBob Pearson !(mr->access & IB_ACCESS_LOCAL_WRITE))) {
110e8a87efdSBob Pearson rxe_dbg_mw(mw,
1115abb71b4SZhang Jiaming "attempt to bind an Writable MW to an MR without local write access\n");
11232a577b4SBob Pearson return -EINVAL;
11332a577b4SBob Pearson }
11432a577b4SBob Pearson
11532a577b4SBob Pearson /* C10-75 */
116425e1c90SBob Pearson if (access & IB_ZERO_BASED) {
117954afc5aSDaisuke Matsuda if (unlikely(wqe->wr.wr.mw.length > mr->ibmr.length)) {
118e8a87efdSBob Pearson rxe_dbg_mw(mw,
11932a577b4SBob Pearson "attempt to bind a ZB MW outside of the MR\n");
12032a577b4SBob Pearson return -EINVAL;
12132a577b4SBob Pearson }
12232a577b4SBob Pearson } else {
123954afc5aSDaisuke Matsuda if (unlikely((wqe->wr.wr.mw.addr < mr->ibmr.iova) ||
12432a577b4SBob Pearson ((wqe->wr.wr.mw.addr + wqe->wr.wr.mw.length) >
125954afc5aSDaisuke Matsuda (mr->ibmr.iova + mr->ibmr.length)))) {
126e8a87efdSBob Pearson rxe_dbg_mw(mw,
12732a577b4SBob Pearson "attempt to bind a VA MW outside of the MR\n");
12832a577b4SBob Pearson return -EINVAL;
12932a577b4SBob Pearson }
13032a577b4SBob Pearson }
13132a577b4SBob Pearson
13232a577b4SBob Pearson return 0;
13332a577b4SBob Pearson }
13432a577b4SBob Pearson
rxe_do_bind_mw(struct rxe_qp * qp,struct rxe_send_wqe * wqe,struct rxe_mw * mw,struct rxe_mr * mr,int access)13532a577b4SBob Pearson static void rxe_do_bind_mw(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
136425e1c90SBob Pearson struct rxe_mw *mw, struct rxe_mr *mr, int access)
13732a577b4SBob Pearson {
13800134533SBob Pearson u32 key = wqe->wr.wr.mw.rkey & 0xff;
13932a577b4SBob Pearson
14000134533SBob Pearson mw->rkey = (mw->rkey & ~0xff) | key;
141425e1c90SBob Pearson mw->access = access;
14232a577b4SBob Pearson mw->state = RXE_MW_STATE_VALID;
14332a577b4SBob Pearson mw->addr = wqe->wr.wr.mw.addr;
14432a577b4SBob Pearson mw->length = wqe->wr.wr.mw.length;
14532a577b4SBob Pearson
14632a577b4SBob Pearson if (mw->mr) {
1473197706aSBob Pearson rxe_put(mw->mr);
14832a577b4SBob Pearson atomic_dec(&mw->mr->num_mw);
14932a577b4SBob Pearson mw->mr = NULL;
15032a577b4SBob Pearson }
15132a577b4SBob Pearson
15232a577b4SBob Pearson if (mw->length) {
15332a577b4SBob Pearson mw->mr = mr;
15432a577b4SBob Pearson atomic_inc(&mr->num_mw);
1553197706aSBob Pearson rxe_get(mr);
15632a577b4SBob Pearson }
15732a577b4SBob Pearson
15832a577b4SBob Pearson if (mw->ibmw.type == IB_MW_TYPE_2) {
1593197706aSBob Pearson rxe_get(qp);
16032a577b4SBob Pearson mw->qp = qp;
16132a577b4SBob Pearson }
16232a577b4SBob Pearson }
16332a577b4SBob Pearson
rxe_bind_mw(struct rxe_qp * qp,struct rxe_send_wqe * wqe)16432a577b4SBob Pearson int rxe_bind_mw(struct rxe_qp *qp, struct rxe_send_wqe *wqe)
16532a577b4SBob Pearson {
16632a577b4SBob Pearson int ret;
16732a577b4SBob Pearson struct rxe_mw *mw;
16832a577b4SBob Pearson struct rxe_mr *mr;
16932a577b4SBob Pearson struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
17000134533SBob Pearson u32 mw_rkey = wqe->wr.wr.mw.mw_rkey;
17100134533SBob Pearson u32 mr_lkey = wqe->wr.wr.mw.mr_lkey;
172425e1c90SBob Pearson int access = wqe->wr.wr.mw.access;
17332a577b4SBob Pearson
17400134533SBob Pearson mw = rxe_pool_get_index(&rxe->mw_pool, mw_rkey >> 8);
17532a577b4SBob Pearson if (unlikely(!mw)) {
17632a577b4SBob Pearson ret = -EINVAL;
17732a577b4SBob Pearson goto err;
17832a577b4SBob Pearson }
17932a577b4SBob Pearson
18000134533SBob Pearson if (unlikely(mw->rkey != mw_rkey)) {
18132a577b4SBob Pearson ret = -EINVAL;
18232a577b4SBob Pearson goto err_drop_mw;
18332a577b4SBob Pearson }
18432a577b4SBob Pearson
18532a577b4SBob Pearson if (likely(wqe->wr.wr.mw.length)) {
18600134533SBob Pearson mr = rxe_pool_get_index(&rxe->mr_pool, mr_lkey >> 8);
18732a577b4SBob Pearson if (unlikely(!mr)) {
18832a577b4SBob Pearson ret = -EINVAL;
18932a577b4SBob Pearson goto err_drop_mw;
19032a577b4SBob Pearson }
19132a577b4SBob Pearson
19200134533SBob Pearson if (unlikely(mr->lkey != mr_lkey)) {
19332a577b4SBob Pearson ret = -EINVAL;
19432a577b4SBob Pearson goto err_drop_mr;
19532a577b4SBob Pearson }
19632a577b4SBob Pearson } else {
19732a577b4SBob Pearson mr = NULL;
19832a577b4SBob Pearson }
19932a577b4SBob Pearson
20002ed2537SBob Pearson if (access & ~RXE_ACCESS_SUPPORTED_MW) {
201*64827180SLi Zhijian rxe_err_mw(mw, "access %#x not supported\n", access);
2025c719d7aSChristophe JAILLET ret = -EOPNOTSUPP;
2035c719d7aSChristophe JAILLET goto err_drop_mr;
20402ed2537SBob Pearson }
20502ed2537SBob Pearson
20621adfa7aSBob Pearson spin_lock_bh(&mw->lock);
20732a577b4SBob Pearson
208425e1c90SBob Pearson ret = rxe_check_bind_mw(qp, wqe, mw, mr, access);
20932a577b4SBob Pearson if (ret)
21032a577b4SBob Pearson goto err_unlock;
21132a577b4SBob Pearson
212425e1c90SBob Pearson rxe_do_bind_mw(qp, wqe, mw, mr, access);
21332a577b4SBob Pearson err_unlock:
21421adfa7aSBob Pearson spin_unlock_bh(&mw->lock);
21532a577b4SBob Pearson err_drop_mr:
21632a577b4SBob Pearson if (mr)
2173197706aSBob Pearson rxe_put(mr);
21832a577b4SBob Pearson err_drop_mw:
2193197706aSBob Pearson rxe_put(mw);
22032a577b4SBob Pearson err:
22132a577b4SBob Pearson return ret;
22232a577b4SBob Pearson }
22332a577b4SBob Pearson
rxe_check_invalidate_mw(struct rxe_qp * qp,struct rxe_mw * mw)2243902b429SBob Pearson static int rxe_check_invalidate_mw(struct rxe_qp *qp, struct rxe_mw *mw)
2253902b429SBob Pearson {
2263902b429SBob Pearson if (unlikely(mw->state == RXE_MW_STATE_INVALID))
2273902b429SBob Pearson return -EINVAL;
2283902b429SBob Pearson
2293902b429SBob Pearson /* o10-37.2.26 */
2303902b429SBob Pearson if (unlikely(mw->ibmw.type == IB_MW_TYPE_1))
2313902b429SBob Pearson return -EINVAL;
2323902b429SBob Pearson
2333902b429SBob Pearson return 0;
2343902b429SBob Pearson }
2353902b429SBob Pearson
rxe_do_invalidate_mw(struct rxe_mw * mw)2363902b429SBob Pearson static void rxe_do_invalidate_mw(struct rxe_mw *mw)
2373902b429SBob Pearson {
2383902b429SBob Pearson struct rxe_qp *qp;
2393902b429SBob Pearson struct rxe_mr *mr;
2403902b429SBob Pearson
2413902b429SBob Pearson /* valid type 2 MW will always have a QP pointer */
2423902b429SBob Pearson qp = mw->qp;
2433902b429SBob Pearson mw->qp = NULL;
2443197706aSBob Pearson rxe_put(qp);
2453902b429SBob Pearson
2463902b429SBob Pearson /* valid type 2 MW will always have an MR pointer */
2473902b429SBob Pearson mr = mw->mr;
2483902b429SBob Pearson mw->mr = NULL;
2493902b429SBob Pearson atomic_dec(&mr->num_mw);
2503197706aSBob Pearson rxe_put(mr);
2513902b429SBob Pearson
2523902b429SBob Pearson mw->access = 0;
2533902b429SBob Pearson mw->addr = 0;
2543902b429SBob Pearson mw->length = 0;
2553902b429SBob Pearson mw->state = RXE_MW_STATE_FREE;
2563902b429SBob Pearson }
2573902b429SBob Pearson
rxe_invalidate_mw(struct rxe_qp * qp,u32 rkey)2583902b429SBob Pearson int rxe_invalidate_mw(struct rxe_qp *qp, u32 rkey)
2593902b429SBob Pearson {
2603902b429SBob Pearson struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
2613902b429SBob Pearson struct rxe_mw *mw;
2623902b429SBob Pearson int ret;
2633902b429SBob Pearson
2643902b429SBob Pearson mw = rxe_pool_get_index(&rxe->mw_pool, rkey >> 8);
2653902b429SBob Pearson if (!mw) {
2663902b429SBob Pearson ret = -EINVAL;
2673902b429SBob Pearson goto err;
2683902b429SBob Pearson }
2693902b429SBob Pearson
27000134533SBob Pearson if (rkey != mw->rkey) {
2713902b429SBob Pearson ret = -EINVAL;
2723902b429SBob Pearson goto err_drop_ref;
2733902b429SBob Pearson }
2743902b429SBob Pearson
27521adfa7aSBob Pearson spin_lock_bh(&mw->lock);
2763902b429SBob Pearson
2773902b429SBob Pearson ret = rxe_check_invalidate_mw(qp, mw);
2783902b429SBob Pearson if (ret)
2793902b429SBob Pearson goto err_unlock;
2803902b429SBob Pearson
2813902b429SBob Pearson rxe_do_invalidate_mw(mw);
2823902b429SBob Pearson err_unlock:
28321adfa7aSBob Pearson spin_unlock_bh(&mw->lock);
2843902b429SBob Pearson err_drop_ref:
2853197706aSBob Pearson rxe_put(mw);
2863902b429SBob Pearson err:
2873902b429SBob Pearson return ret;
2883902b429SBob Pearson }
2893902b429SBob Pearson
rxe_lookup_mw(struct rxe_qp * qp,int access,u32 rkey)290cdd0b856SBob Pearson struct rxe_mw *rxe_lookup_mw(struct rxe_qp *qp, int access, u32 rkey)
291cdd0b856SBob Pearson {
292cdd0b856SBob Pearson struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
293cdd0b856SBob Pearson struct rxe_pd *pd = to_rpd(qp->ibqp.pd);
294cdd0b856SBob Pearson struct rxe_mw *mw;
295cdd0b856SBob Pearson int index = rkey >> 8;
296cdd0b856SBob Pearson
297cdd0b856SBob Pearson mw = rxe_pool_get_index(&rxe->mw_pool, index);
298cdd0b856SBob Pearson if (!mw)
299cdd0b856SBob Pearson return NULL;
300cdd0b856SBob Pearson
30100134533SBob Pearson if (unlikely((mw->rkey != rkey) || rxe_mw_pd(mw) != pd ||
302cdd0b856SBob Pearson (mw->ibmw.type == IB_MW_TYPE_2 && mw->qp != qp) ||
303875ab4a8SLi Zhijian (mw->length == 0) || ((access & mw->access) != access) ||
304cdd0b856SBob Pearson mw->state != RXE_MW_STATE_VALID)) {
3053197706aSBob Pearson rxe_put(mw);
306cdd0b856SBob Pearson return NULL;
307cdd0b856SBob Pearson }
308cdd0b856SBob Pearson
309cdd0b856SBob Pearson return mw;
310cdd0b856SBob Pearson }
311cde3f5d6SBob Pearson
rxe_mw_cleanup(struct rxe_pool_elem * elem)312cde3f5d6SBob Pearson void rxe_mw_cleanup(struct rxe_pool_elem *elem)
313cde3f5d6SBob Pearson {
314cde3f5d6SBob Pearson struct rxe_mw *mw = container_of(elem, typeof(*mw), elem);
315cde3f5d6SBob Pearson struct rxe_pd *pd = to_rpd(mw->ibmw.pd);
316cde3f5d6SBob Pearson
317cde3f5d6SBob Pearson rxe_put(pd);
318cde3f5d6SBob Pearson
319cde3f5d6SBob Pearson if (mw->mr) {
320cde3f5d6SBob Pearson struct rxe_mr *mr = mw->mr;
321cde3f5d6SBob Pearson
322cde3f5d6SBob Pearson mw->mr = NULL;
323cde3f5d6SBob Pearson atomic_dec(&mr->num_mw);
324cde3f5d6SBob Pearson rxe_put(mr);
325cde3f5d6SBob Pearson }
326cde3f5d6SBob Pearson
327cde3f5d6SBob Pearson if (mw->qp) {
328cde3f5d6SBob Pearson struct rxe_qp *qp = mw->qp;
329cde3f5d6SBob Pearson
330cde3f5d6SBob Pearson mw->qp = NULL;
331cde3f5d6SBob Pearson rxe_put(qp);
332cde3f5d6SBob Pearson }
333cde3f5d6SBob Pearson
334cde3f5d6SBob Pearson mw->access = 0;
335cde3f5d6SBob Pearson mw->addr = 0;
336cde3f5d6SBob Pearson mw->length = 0;
337cde3f5d6SBob Pearson mw->state = RXE_MW_STATE_INVALID;
338cde3f5d6SBob Pearson }
339