162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2016 Oracle.  All rights reserved.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * This software is available to you under a choice of one of two
562306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
662306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
762306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
862306a36Sopenharmony_ci * OpenIB.org BSD license below:
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1162306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1262306a36Sopenharmony_ci *     conditions are met:
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1562306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1662306a36Sopenharmony_ci *        disclaimer.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
1962306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2062306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2162306a36Sopenharmony_ci *        provided with the distribution.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2462306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2562306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2662306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2762306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2862306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2962306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3062306a36Sopenharmony_ci * SOFTWARE.
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_ci#ifndef _RDS_IB_MR_H
3362306a36Sopenharmony_ci#define _RDS_IB_MR_H
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#include <linux/kernel.h>
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#include "rds.h"
3862306a36Sopenharmony_ci#include "ib.h"
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#define RDS_MR_1M_POOL_SIZE		(8192 / 2)
4162306a36Sopenharmony_ci#define RDS_MR_1M_MSG_SIZE		256
4262306a36Sopenharmony_ci#define RDS_MR_8K_MSG_SIZE		2
4362306a36Sopenharmony_ci#define RDS_MR_8K_SCALE			(256 / (RDS_MR_8K_MSG_SIZE + 1))
4462306a36Sopenharmony_ci#define RDS_MR_8K_POOL_SIZE		(RDS_MR_8K_SCALE * (8192 / 2))
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cienum rds_ib_fr_state {
4762306a36Sopenharmony_ci	FRMR_IS_FREE,	/* mr invalidated & ready for use */
4862306a36Sopenharmony_ci	FRMR_IS_INUSE,	/* mr is in use or used & can be invalidated */
4962306a36Sopenharmony_ci	FRMR_IS_STALE,	/* Stale MR and needs to be dropped  */
5062306a36Sopenharmony_ci};
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_cistruct rds_ib_frmr {
5362306a36Sopenharmony_ci	struct ib_mr		*mr;
5462306a36Sopenharmony_ci	enum rds_ib_fr_state	fr_state;
5562306a36Sopenharmony_ci	bool			fr_inv;
5662306a36Sopenharmony_ci	wait_queue_head_t	fr_inv_done;
5762306a36Sopenharmony_ci	bool			fr_reg;
5862306a36Sopenharmony_ci	wait_queue_head_t	fr_reg_done;
5962306a36Sopenharmony_ci	struct ib_send_wr	fr_wr;
6062306a36Sopenharmony_ci	unsigned int		dma_npages;
6162306a36Sopenharmony_ci	unsigned int		sg_byte_len;
6262306a36Sopenharmony_ci};
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci/* This is stored as mr->r_trans_private. */
6562306a36Sopenharmony_cistruct rds_ib_mr {
6662306a36Sopenharmony_ci	struct delayed_work		work;
6762306a36Sopenharmony_ci	struct rds_ib_device		*device;
6862306a36Sopenharmony_ci	struct rds_ib_mr_pool		*pool;
6962306a36Sopenharmony_ci	struct rds_ib_connection	*ic;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	struct llist_node		llnode;
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	/* unmap_list is for freeing */
7462306a36Sopenharmony_ci	struct list_head		unmap_list;
7562306a36Sopenharmony_ci	unsigned int			remap_count;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	struct scatterlist		*sg;
7862306a36Sopenharmony_ci	unsigned int			sg_len;
7962306a36Sopenharmony_ci	int				sg_dma_len;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	u8				odp:1;
8262306a36Sopenharmony_ci	union {
8362306a36Sopenharmony_ci		struct rds_ib_frmr	frmr;
8462306a36Sopenharmony_ci		struct ib_mr		*mr;
8562306a36Sopenharmony_ci	} u;
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/* Our own little MR pool */
8962306a36Sopenharmony_cistruct rds_ib_mr_pool {
9062306a36Sopenharmony_ci	unsigned int            pool_type;
9162306a36Sopenharmony_ci	struct mutex		flush_lock;	/* serialize fmr invalidate */
9262306a36Sopenharmony_ci	struct delayed_work	flush_worker;	/* flush worker */
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	atomic_t		item_count;	/* total # of MRs */
9562306a36Sopenharmony_ci	atomic_t		dirty_count;	/* # dirty of MRs */
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	struct llist_head	drop_list;	/* MRs not reached max_maps */
9862306a36Sopenharmony_ci	struct llist_head	free_list;	/* unused MRs */
9962306a36Sopenharmony_ci	struct llist_head	clean_list;	/* unused & unmapped MRs */
10062306a36Sopenharmony_ci	wait_queue_head_t	flush_wait;
10162306a36Sopenharmony_ci	spinlock_t		clean_lock;	/* "clean_list" concurrency */
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	atomic_t		free_pinned;	/* memory pinned by free MRs */
10462306a36Sopenharmony_ci	unsigned long		max_items;
10562306a36Sopenharmony_ci	unsigned long		max_items_soft;
10662306a36Sopenharmony_ci	unsigned long		max_free_pinned;
10762306a36Sopenharmony_ci	unsigned int		max_pages;
10862306a36Sopenharmony_ci};
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciextern struct workqueue_struct *rds_ib_mr_wq;
11162306a36Sopenharmony_ciextern bool prefer_frmr;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistruct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *rds_dev,
11462306a36Sopenharmony_ci					     int npages);
11562306a36Sopenharmony_civoid rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev,
11662306a36Sopenharmony_ci			struct rds_info_rdma_connection *iinfo);
11762306a36Sopenharmony_civoid rds6_ib_get_mr_info(struct rds_ib_device *rds_ibdev,
11862306a36Sopenharmony_ci			 struct rds6_info_rdma_connection *iinfo6);
11962306a36Sopenharmony_civoid rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *);
12062306a36Sopenharmony_civoid *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,
12162306a36Sopenharmony_ci		    struct rds_sock *rs, u32 *key_ret,
12262306a36Sopenharmony_ci		    struct rds_connection *conn, u64 start, u64 length,
12362306a36Sopenharmony_ci		    int need_odp);
12462306a36Sopenharmony_civoid rds_ib_sync_mr(void *trans_private, int dir);
12562306a36Sopenharmony_civoid rds_ib_free_mr(void *trans_private, int invalidate);
12662306a36Sopenharmony_civoid rds_ib_flush_mrs(void);
12762306a36Sopenharmony_ciint rds_ib_mr_init(void);
12862306a36Sopenharmony_civoid rds_ib_mr_exit(void);
12962306a36Sopenharmony_ciu32 rds_ib_get_lkey(void *trans_private);
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_civoid __rds_ib_teardown_mr(struct rds_ib_mr *);
13262306a36Sopenharmony_civoid rds_ib_teardown_mr(struct rds_ib_mr *);
13362306a36Sopenharmony_cistruct rds_ib_mr *rds_ib_reuse_mr(struct rds_ib_mr_pool *);
13462306a36Sopenharmony_ciint rds_ib_flush_mr_pool(struct rds_ib_mr_pool *, int, struct rds_ib_mr **);
13562306a36Sopenharmony_cistruct rds_ib_mr *rds_ib_try_reuse_ibmr(struct rds_ib_mr_pool *);
13662306a36Sopenharmony_cistruct rds_ib_mr *rds_ib_reg_frmr(struct rds_ib_device *rds_ibdev,
13762306a36Sopenharmony_ci				  struct rds_ib_connection *ic,
13862306a36Sopenharmony_ci				  struct scatterlist *sg,
13962306a36Sopenharmony_ci				  unsigned long nents, u32 *key);
14062306a36Sopenharmony_civoid rds_ib_unreg_frmr(struct list_head *list, unsigned int *nfreed,
14162306a36Sopenharmony_ci		       unsigned long *unpinned, unsigned int goal);
14262306a36Sopenharmony_civoid rds_ib_free_frmr_list(struct rds_ib_mr *);
14362306a36Sopenharmony_ci#endif
144