#ifndef DEF_RDMAVT_INCMR_H1#define DEF_RDMAVT_INCMR_H23/*-4* SPDX-License-Identifier: BSD-2-Clause OR GPL-2.05*6* Copyright(c) 2016 Intel Corporation.7*8* This file is provided under a dual BSD/GPLv2 license. When using or9* redistributing this file, you may do so under either license.10*11* GPL LICENSE SUMMARY12*13* This program is free software; you can redistribute it and/or modify14* it under the terms of version 2 of the GNU General Public License as15* published by the Free Software Foundation.16*17* This program is distributed in the hope that it will be useful, but18* WITHOUT ANY WARRANTY; without even the implied warranty of19* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU20* General Public License for more details.21*22* BSD LICENSE23*24* Redistribution and use in source and binary forms, with or without25* modification, are permitted provided that the following conditions26* are met:27*28* - Redistributions of source code must retain the above copyright29* notice, this list of conditions and the following disclaimer.30* - Redistributions in binary form must reproduce the above copyright31* notice, this list of conditions and the following disclaimer in32* the documentation and/or other materials provided with the33* distribution.34* - Neither the name of Intel Corporation nor the names of its35* contributors may be used to endorse or promote products derived36* from this software without specific prior written permission.37*38* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS39* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT40* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR41* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT42* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,43* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT44* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,45* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY46* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT47* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE48* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.49*/5051/*52* For Memory Regions. This stuff should probably be moved into rdmavt/mr.h once53* drivers no longer need access to the MR directly.54*/5556/*57* A segment is a linear region of low physical memory.58* Used by the verbs layer.59*/60struct rvt_seg {61void *vaddr;62size_t length;63};6465/* The number of rvt_segs that fit in a page. */66#define RVT_SEGSZ (PAGE_SIZE / sizeof(struct rvt_seg))6768struct rvt_segarray {69struct rvt_seg segs[RVT_SEGSZ];70};7172struct rvt_mregion {73struct ib_pd *pd; /* shares refcnt of ibmr.pd */74u64 user_base; /* User's address for this region */75u64 iova; /* IB start address of this region */76size_t length;77u32 lkey;78u32 offset; /* offset (bytes) to start of region */79int access_flags;80u32 max_segs; /* number of rvt_segs in all the arrays */81u32 mapsz; /* size of the map array */82u8 page_shift; /* 0 - non unform/non powerof2 sizes */83u8 lkey_published; /* in global table */84atomic_t lkey_invalid; /* true if current lkey is invalid */85struct completion comp; /* complete when refcount goes to zero */86atomic_t refcount;87struct rvt_segarray *map[0]; /* the segments */88};8990#define RVT_MAX_LKEY_TABLE_BITS 239192struct rvt_lkey_table {93spinlock_t lock; /* protect changes in this struct */94u32 next; /* next unused index (speeds search) */95u32 gen; /* generation count */96u32 max; /* size of the table */97struct rvt_mregion __rcu **table;98};99100/*101* These keep track of the copy progress within a memory region.102* Used by the verbs layer.103*/104struct rvt_sge {105struct rvt_mregion *mr;106void *vaddr; /* kernel virtual address of segment */107u32 sge_length; /* length of the SGE */108u32 length; /* remaining length of the segment */109u16 m; /* current index: mr->map[m] */110u16 n; /* current index: mr->map[m]->segs[n] */111};112113struct rvt_sge_state {114struct rvt_sge *sg_list; /* next SGE to be used if any */115struct rvt_sge sge; /* progress state for the current SGE */116u32 total_len;117u8 num_sge;118};119120static inline void rvt_put_mr(struct rvt_mregion *mr)121{122if (unlikely(atomic_dec_and_test(&mr->refcount)))123complete(&mr->comp);124}125126static inline void rvt_get_mr(struct rvt_mregion *mr)127{128atomic_inc(&mr->refcount);129}130131static inline void rvt_put_ss(struct rvt_sge_state *ss)132{133while (ss->num_sge) {134rvt_put_mr(ss->sge.mr);135if (--ss->num_sge)136ss->sge = *ss->sg_list++;137}138}139140#endif /* DEF_RDMAVT_INCMRH */141142143