162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Shared Memory Communications over RDMA (SMC-R) and RoCE
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Work Requests exploiting Infiniband API
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright IBM Corp. 2016
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Author(s):  Steffen Maier <maier@linux.vnet.ibm.com>
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#ifndef SMC_WR_H
1362306a36Sopenharmony_ci#define SMC_WR_H
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <linux/atomic.h>
1662306a36Sopenharmony_ci#include <rdma/ib_verbs.h>
1762306a36Sopenharmony_ci#include <asm/div64.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#include "smc.h"
2062306a36Sopenharmony_ci#include "smc_core.h"
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define SMC_WR_BUF_CNT 16	/* # of ctrl buffers per link */
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define SMC_WR_TX_WAIT_FREE_SLOT_TIME	(10 * HZ)
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define SMC_WR_TX_SIZE 44 /* actual size of wr_send data (<=SMC_WR_BUF_SIZE) */
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define SMC_WR_TX_PEND_PRIV_SIZE 32
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_cistruct smc_wr_tx_pend_priv {
3162306a36Sopenharmony_ci	u8			priv[SMC_WR_TX_PEND_PRIV_SIZE];
3262306a36Sopenharmony_ci};
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_citypedef void (*smc_wr_tx_handler)(struct smc_wr_tx_pend_priv *,
3562306a36Sopenharmony_ci				  struct smc_link *,
3662306a36Sopenharmony_ci				  enum ib_wc_status);
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_citypedef bool (*smc_wr_tx_filter)(struct smc_wr_tx_pend_priv *,
3962306a36Sopenharmony_ci				 unsigned long);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_citypedef void (*smc_wr_tx_dismisser)(struct smc_wr_tx_pend_priv *);
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistruct smc_wr_rx_handler {
4462306a36Sopenharmony_ci	struct hlist_node	list;	/* hash table collision resolution */
4562306a36Sopenharmony_ci	void			(*handler)(struct ib_wc *, void *);
4662306a36Sopenharmony_ci	u8			type;
4762306a36Sopenharmony_ci};
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci/* Only used by RDMA write WRs.
5062306a36Sopenharmony_ci * All other WRs (CDC/LLC) use smc_wr_tx_send handling WR_ID implicitly
5162306a36Sopenharmony_ci */
5262306a36Sopenharmony_cistatic inline long smc_wr_tx_get_next_wr_id(struct smc_link *link)
5362306a36Sopenharmony_ci{
5462306a36Sopenharmony_ci	return atomic_long_inc_return(&link->wr_tx_id);
5562306a36Sopenharmony_ci}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistatic inline void smc_wr_tx_set_wr_id(atomic_long_t *wr_tx_id, long val)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	atomic_long_set(wr_tx_id, val);
6062306a36Sopenharmony_ci}
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_cistatic inline bool smc_wr_tx_link_hold(struct smc_link *link)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	if (!smc_link_sendable(link))
6562306a36Sopenharmony_ci		return false;
6662306a36Sopenharmony_ci	percpu_ref_get(&link->wr_tx_refs);
6762306a36Sopenharmony_ci	return true;
6862306a36Sopenharmony_ci}
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_cistatic inline void smc_wr_tx_link_put(struct smc_link *link)
7162306a36Sopenharmony_ci{
7262306a36Sopenharmony_ci	percpu_ref_put(&link->wr_tx_refs);
7362306a36Sopenharmony_ci}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cistatic inline void smc_wr_drain_cq(struct smc_link *lnk)
7662306a36Sopenharmony_ci{
7762306a36Sopenharmony_ci	wait_event(lnk->wr_rx_empty_wait, lnk->wr_rx_id_compl == lnk->wr_rx_id);
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistatic inline void smc_wr_wakeup_tx_wait(struct smc_link *lnk)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	wake_up_all(&lnk->wr_tx_wait);
8362306a36Sopenharmony_ci}
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_cistatic inline void smc_wr_wakeup_reg_wait(struct smc_link *lnk)
8662306a36Sopenharmony_ci{
8762306a36Sopenharmony_ci	wake_up(&lnk->wr_reg_wait);
8862306a36Sopenharmony_ci}
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci/* post a new receive work request to fill a completed old work request entry */
9162306a36Sopenharmony_cistatic inline int smc_wr_rx_post(struct smc_link *link)
9262306a36Sopenharmony_ci{
9362306a36Sopenharmony_ci	int rc;
9462306a36Sopenharmony_ci	u64 wr_id, temp_wr_id;
9562306a36Sopenharmony_ci	u32 index;
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	wr_id = ++link->wr_rx_id; /* tasklet context, thus not atomic */
9862306a36Sopenharmony_ci	temp_wr_id = wr_id;
9962306a36Sopenharmony_ci	index = do_div(temp_wr_id, link->wr_rx_cnt);
10062306a36Sopenharmony_ci	link->wr_rx_ibs[index].wr_id = wr_id;
10162306a36Sopenharmony_ci	rc = ib_post_recv(link->roce_qp, &link->wr_rx_ibs[index], NULL);
10262306a36Sopenharmony_ci	return rc;
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ciint smc_wr_create_link(struct smc_link *lnk);
10662306a36Sopenharmony_ciint smc_wr_alloc_link_mem(struct smc_link *lnk);
10762306a36Sopenharmony_ciint smc_wr_alloc_lgr_mem(struct smc_link_group *lgr);
10862306a36Sopenharmony_civoid smc_wr_free_link(struct smc_link *lnk);
10962306a36Sopenharmony_civoid smc_wr_free_link_mem(struct smc_link *lnk);
11062306a36Sopenharmony_civoid smc_wr_free_lgr_mem(struct smc_link_group *lgr);
11162306a36Sopenharmony_civoid smc_wr_remember_qp_attr(struct smc_link *lnk);
11262306a36Sopenharmony_civoid smc_wr_remove_dev(struct smc_ib_device *smcibdev);
11362306a36Sopenharmony_civoid smc_wr_add_dev(struct smc_ib_device *smcibdev);
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciint smc_wr_tx_get_free_slot(struct smc_link *link, smc_wr_tx_handler handler,
11662306a36Sopenharmony_ci			    struct smc_wr_buf **wr_buf,
11762306a36Sopenharmony_ci			    struct smc_rdma_wr **wrs,
11862306a36Sopenharmony_ci			    struct smc_wr_tx_pend_priv **wr_pend_priv);
11962306a36Sopenharmony_ciint smc_wr_tx_get_v2_slot(struct smc_link *link,
12062306a36Sopenharmony_ci			  smc_wr_tx_handler handler,
12162306a36Sopenharmony_ci			  struct smc_wr_v2_buf **wr_buf,
12262306a36Sopenharmony_ci			  struct smc_wr_tx_pend_priv **wr_pend_priv);
12362306a36Sopenharmony_ciint smc_wr_tx_put_slot(struct smc_link *link,
12462306a36Sopenharmony_ci		       struct smc_wr_tx_pend_priv *wr_pend_priv);
12562306a36Sopenharmony_ciint smc_wr_tx_send(struct smc_link *link,
12662306a36Sopenharmony_ci		   struct smc_wr_tx_pend_priv *wr_pend_priv);
12762306a36Sopenharmony_ciint smc_wr_tx_v2_send(struct smc_link *link,
12862306a36Sopenharmony_ci		      struct smc_wr_tx_pend_priv *priv, int len);
12962306a36Sopenharmony_ciint smc_wr_tx_send_wait(struct smc_link *link, struct smc_wr_tx_pend_priv *priv,
13062306a36Sopenharmony_ci			unsigned long timeout);
13162306a36Sopenharmony_civoid smc_wr_tx_cq_handler(struct ib_cq *ib_cq, void *cq_context);
13262306a36Sopenharmony_civoid smc_wr_tx_wait_no_pending_sends(struct smc_link *link);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ciint smc_wr_rx_register_handler(struct smc_wr_rx_handler *handler);
13562306a36Sopenharmony_ciint smc_wr_rx_post_init(struct smc_link *link);
13662306a36Sopenharmony_civoid smc_wr_rx_cq_handler(struct ib_cq *ib_cq, void *cq_context);
13762306a36Sopenharmony_ciint smc_wr_reg_send(struct smc_link *link, struct ib_mr *mr);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci#endif /* SMC_WR_H */
140