18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2016 Oracle.  All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
58c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
118c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
128c2ecf20Sopenharmony_ci *     conditions are met:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
158c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
168c2ecf20Sopenharmony_ci *        disclaimer.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
198c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
208c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
218c2ecf20Sopenharmony_ci *        provided with the distribution.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
308c2ecf20Sopenharmony_ci * SOFTWARE.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci#ifndef _RDS_IB_MR_H
338c2ecf20Sopenharmony_ci#define _RDS_IB_MR_H
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <linux/kernel.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include "rds.h"
388c2ecf20Sopenharmony_ci#include "ib.h"
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define RDS_MR_1M_POOL_SIZE		(8192 / 2)
418c2ecf20Sopenharmony_ci#define RDS_MR_1M_MSG_SIZE		256
428c2ecf20Sopenharmony_ci#define RDS_MR_8K_MSG_SIZE		2
438c2ecf20Sopenharmony_ci#define RDS_MR_8K_SCALE			(256 / (RDS_MR_8K_MSG_SIZE + 1))
448c2ecf20Sopenharmony_ci#define RDS_MR_8K_POOL_SIZE		(RDS_MR_8K_SCALE * (8192 / 2))
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cienum rds_ib_fr_state {
478c2ecf20Sopenharmony_ci	FRMR_IS_FREE,	/* mr invalidated & ready for use */
488c2ecf20Sopenharmony_ci	FRMR_IS_INUSE,	/* mr is in use or used & can be invalidated */
498c2ecf20Sopenharmony_ci	FRMR_IS_STALE,	/* Stale MR and needs to be dropped  */
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistruct rds_ib_frmr {
538c2ecf20Sopenharmony_ci	struct ib_mr		*mr;
548c2ecf20Sopenharmony_ci	enum rds_ib_fr_state	fr_state;
558c2ecf20Sopenharmony_ci	bool			fr_inv;
568c2ecf20Sopenharmony_ci	wait_queue_head_t	fr_inv_done;
578c2ecf20Sopenharmony_ci	bool			fr_reg;
588c2ecf20Sopenharmony_ci	wait_queue_head_t	fr_reg_done;
598c2ecf20Sopenharmony_ci	struct ib_send_wr	fr_wr;
608c2ecf20Sopenharmony_ci	unsigned int		dma_npages;
618c2ecf20Sopenharmony_ci	unsigned int		sg_byte_len;
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/* This is stored as mr->r_trans_private. */
658c2ecf20Sopenharmony_cistruct rds_ib_mr {
668c2ecf20Sopenharmony_ci	struct delayed_work		work;
678c2ecf20Sopenharmony_ci	struct rds_ib_device		*device;
688c2ecf20Sopenharmony_ci	struct rds_ib_mr_pool		*pool;
698c2ecf20Sopenharmony_ci	struct rds_ib_connection	*ic;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	struct llist_node		llnode;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	/* unmap_list is for freeing */
748c2ecf20Sopenharmony_ci	struct list_head		unmap_list;
758c2ecf20Sopenharmony_ci	unsigned int			remap_count;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	struct scatterlist		*sg;
788c2ecf20Sopenharmony_ci	unsigned int			sg_len;
798c2ecf20Sopenharmony_ci	int				sg_dma_len;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	u8				odp:1;
828c2ecf20Sopenharmony_ci	union {
838c2ecf20Sopenharmony_ci		struct rds_ib_frmr	frmr;
848c2ecf20Sopenharmony_ci		struct ib_mr		*mr;
858c2ecf20Sopenharmony_ci	} u;
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/* Our own little MR pool */
898c2ecf20Sopenharmony_cistruct rds_ib_mr_pool {
908c2ecf20Sopenharmony_ci	unsigned int            pool_type;
918c2ecf20Sopenharmony_ci	struct mutex		flush_lock;	/* serialize fmr invalidate */
928c2ecf20Sopenharmony_ci	struct delayed_work	flush_worker;	/* flush worker */
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	atomic_t		item_count;	/* total # of MRs */
958c2ecf20Sopenharmony_ci	atomic_t		dirty_count;	/* # dirty of MRs */
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	struct llist_head	drop_list;	/* MRs not reached max_maps */
988c2ecf20Sopenharmony_ci	struct llist_head	free_list;	/* unused MRs */
998c2ecf20Sopenharmony_ci	struct llist_head	clean_list;	/* unused & unmapped MRs */
1008c2ecf20Sopenharmony_ci	wait_queue_head_t	flush_wait;
1018c2ecf20Sopenharmony_ci	spinlock_t		clean_lock;	/* "clean_list" concurrency */
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	atomic_t		free_pinned;	/* memory pinned by free MRs */
1048c2ecf20Sopenharmony_ci	unsigned long		max_items;
1058c2ecf20Sopenharmony_ci	unsigned long		max_items_soft;
1068c2ecf20Sopenharmony_ci	unsigned long		max_free_pinned;
1078c2ecf20Sopenharmony_ci	unsigned int		max_pages;
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciextern struct workqueue_struct *rds_ib_mr_wq;
1118c2ecf20Sopenharmony_ciextern bool prefer_frmr;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistruct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *rds_dev,
1148c2ecf20Sopenharmony_ci					     int npages);
1158c2ecf20Sopenharmony_civoid rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev,
1168c2ecf20Sopenharmony_ci			struct rds_info_rdma_connection *iinfo);
1178c2ecf20Sopenharmony_civoid rds6_ib_get_mr_info(struct rds_ib_device *rds_ibdev,
1188c2ecf20Sopenharmony_ci			 struct rds6_info_rdma_connection *iinfo6);
1198c2ecf20Sopenharmony_civoid rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *);
1208c2ecf20Sopenharmony_civoid *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,
1218c2ecf20Sopenharmony_ci		    struct rds_sock *rs, u32 *key_ret,
1228c2ecf20Sopenharmony_ci		    struct rds_connection *conn, u64 start, u64 length,
1238c2ecf20Sopenharmony_ci		    int need_odp);
1248c2ecf20Sopenharmony_civoid rds_ib_sync_mr(void *trans_private, int dir);
1258c2ecf20Sopenharmony_civoid rds_ib_free_mr(void *trans_private, int invalidate);
1268c2ecf20Sopenharmony_civoid rds_ib_flush_mrs(void);
1278c2ecf20Sopenharmony_ciint rds_ib_mr_init(void);
1288c2ecf20Sopenharmony_civoid rds_ib_mr_exit(void);
1298c2ecf20Sopenharmony_ciu32 rds_ib_get_lkey(void *trans_private);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_civoid __rds_ib_teardown_mr(struct rds_ib_mr *);
1328c2ecf20Sopenharmony_civoid rds_ib_teardown_mr(struct rds_ib_mr *);
1338c2ecf20Sopenharmony_cistruct rds_ib_mr *rds_ib_reuse_mr(struct rds_ib_mr_pool *);
1348c2ecf20Sopenharmony_ciint rds_ib_flush_mr_pool(struct rds_ib_mr_pool *, int, struct rds_ib_mr **);
1358c2ecf20Sopenharmony_cistruct rds_ib_mr *rds_ib_try_reuse_ibmr(struct rds_ib_mr_pool *);
1368c2ecf20Sopenharmony_cistruct rds_ib_mr *rds_ib_reg_frmr(struct rds_ib_device *rds_ibdev,
1378c2ecf20Sopenharmony_ci				  struct rds_ib_connection *ic,
1388c2ecf20Sopenharmony_ci				  struct scatterlist *sg,
1398c2ecf20Sopenharmony_ci				  unsigned long nents, u32 *key);
1408c2ecf20Sopenharmony_civoid rds_ib_unreg_frmr(struct list_head *list, unsigned int *nfreed,
1418c2ecf20Sopenharmony_ci		       unsigned long *unpinned, unsigned int goal);
1428c2ecf20Sopenharmony_civoid rds_ib_free_frmr_list(struct rds_ib_mr *);
1438c2ecf20Sopenharmony_ci#endif
144