blob: 19c44bf4e39160deeaea80cb71f5ca781d022ab3 [file] [log] [blame]
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001/*
2 * Shared Memory Communications over RDMA (SMC-R) and RoCE
3 *
4 * Definitions for SMC Connections, Link Groups and Links
5 *
6 * Copyright IBM Corp. 2016
7 *
8 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
9 */
10
11#ifndef _SMC_CORE_H
12#define _SMC_CORE_H
13
Ursula Braunf38ba1792017-01-09 16:55:19 +010014#include <linux/atomic.h>
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010015#include <rdma/ib_verbs.h>
16
17#include "smc.h"
18#include "smc_ib.h"
19
Ursula Brauncd6851f2017-01-09 16:55:18 +010020#define SMC_RMBS_PER_LGR_MAX 255 /* max. # of RMBs per link group */
21
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010022struct smc_lgr_list { /* list of link group definition */
23 struct list_head list;
24 spinlock_t lock; /* protects list of link groups */
25};
26
27extern struct smc_lgr_list smc_lgr_list; /* list of link groups */
28
29enum smc_lgr_role { /* possible roles of a link group */
30 SMC_CLNT, /* client */
31 SMC_SERV /* server */
32};
33
Ursula Braunf38ba1792017-01-09 16:55:19 +010034#define SMC_WR_BUF_SIZE 48 /* size of work request buffer */
35
36struct smc_wr_buf {
37 u8 raw[SMC_WR_BUF_SIZE];
38};
39
Ursula Braun652a1e42017-07-28 13:56:17 +020040#define SMC_WR_REG_MR_WAIT_TIME (5 * HZ)/* wait time for ib_wr_reg_mr result */
41
42enum smc_wr_reg_state {
43 POSTED, /* ib_wr_reg_mr request posted */
44 CONFIRMED, /* ib_wr_reg_mr response: successful */
45 FAILED /* ib_wr_reg_mr response: failure */
46};
47
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010048struct smc_link {
49 struct smc_ib_device *smcibdev; /* ib-device */
50 u8 ibport; /* port - values 1 | 2 */
Ursula Braunf38ba1792017-01-09 16:55:19 +010051 struct ib_pd *roce_pd; /* IB protection domain,
52 * unique for every RoCE QP
53 */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010054 struct ib_qp *roce_qp; /* IB queue pair */
55 struct ib_qp_attr qp_attr; /* IB queue pair attributes */
Ursula Braunf38ba1792017-01-09 16:55:19 +010056
57 struct smc_wr_buf *wr_tx_bufs; /* WR send payload buffers */
58 struct ib_send_wr *wr_tx_ibs; /* WR send meta data */
59 struct ib_sge *wr_tx_sges; /* WR send gather meta data */
60 struct smc_wr_tx_pend *wr_tx_pends; /* WR send waiting for CQE */
61 /* above four vectors have wr_tx_cnt elements and use the same index */
62 dma_addr_t wr_tx_dma_addr; /* DMA address of wr_tx_bufs */
63 atomic_long_t wr_tx_id; /* seq # of last sent WR */
64 unsigned long *wr_tx_mask; /* bit mask of used indexes */
65 u32 wr_tx_cnt; /* number of WR send buffers */
66 wait_queue_head_t wr_tx_wait; /* wait for free WR send buf */
67
68 struct smc_wr_buf *wr_rx_bufs; /* WR recv payload buffers */
69 struct ib_recv_wr *wr_rx_ibs; /* WR recv meta data */
70 struct ib_sge *wr_rx_sges; /* WR recv scatter meta data */
71 /* above three vectors have wr_rx_cnt elements and use the same index */
72 dma_addr_t wr_rx_dma_addr; /* DMA address of wr_rx_bufs */
73 u64 wr_rx_id; /* seq # of last recv WR */
74 u32 wr_rx_cnt; /* number of WR recv buffers */
75
Ursula Braun652a1e42017-07-28 13:56:17 +020076 struct ib_reg_wr wr_reg; /* WR register memory region */
77 wait_queue_head_t wr_reg_wait; /* wait for wr_reg result */
78 enum smc_wr_reg_state wr_reg_state; /* state of wr_reg request */
79
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010080 union ib_gid gid; /* gid matching used vlan id */
81 u32 peer_qpn; /* QP number of peer */
82 enum ib_mtu path_mtu; /* used mtu */
83 enum ib_mtu peer_mtu; /* mtu size of peer */
84 u32 psn_initial; /* QP tx initial packet seqno */
85 u32 peer_psn; /* QP rx initial packet seqno */
86 u8 peer_mac[ETH_ALEN]; /* = gid[8:10||13:15] */
87 u8 peer_gid[sizeof(union ib_gid)]; /* gid of peer*/
Ursula Braun9bf9abe2017-01-09 16:55:21 +010088 u8 link_id; /* unique # within link group */
89 struct completion llc_confirm; /* wait for rx of conf link */
90 struct completion llc_confirm_resp; /* wait 4 rx of cnf lnk rsp */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010091};
92
93/* For now we just allow one parallel link per link group. The SMC protocol
94 * allows more (up to 8).
95 */
96#define SMC_LINKS_PER_LGR_MAX 1
97#define SMC_SINGLE_LINK 0
98
99#define SMC_FIRST_CONTACT 1 /* first contact to a peer */
100#define SMC_REUSE_CONTACT 0 /* follow-on contact to a peer*/
101
Ursula Brauncd6851f2017-01-09 16:55:18 +0100102/* tx/rx buffer list element for sndbufs list and rmbs list of a lgr */
103struct smc_buf_desc {
104 struct list_head list;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100105 void *cpu_addr; /* virtual address of buffer */
Ursula Brauna3fe3d02017-07-28 13:56:15 +0200106 struct sg_table sgt[SMC_LINKS_PER_LGR_MAX];/* virtual buffer */
Ursula Braun897e1c22017-07-28 13:56:16 +0200107 struct ib_mr *mr_rx[SMC_LINKS_PER_LGR_MAX];
108 /* for rmb only: memory region
109 * incl. rkey provided to peer
110 */
Ursula Brauna3fe3d02017-07-28 13:56:15 +0200111 u32 order; /* allocation order */
Ursula Brauncd6851f2017-01-09 16:55:18 +0100112 u32 used; /* currently used / unused */
Ursula Braun897e1c22017-07-28 13:56:16 +0200113 bool reused; /* new created / reused */
Ursula Brauncd6851f2017-01-09 16:55:18 +0100114};
115
Ursula Braunbd4ad572017-01-09 16:55:20 +0100116struct smc_rtoken { /* address/key of remote RMB */
117 u64 dma_addr;
118 u32 rkey;
119};
120
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100121#define SMC_LGR_ID_SIZE 4
122
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100123struct smc_link_group {
124 struct list_head list;
125 enum smc_lgr_role role; /* client or server */
126 __be32 daddr; /* destination ip address */
127 struct smc_link lnk[SMC_LINKS_PER_LGR_MAX]; /* smc link */
128 char peer_systemid[SMC_SYSTEMID_LEN];
129 /* unique system_id of peer */
130 struct rb_root conns_all; /* connection tree */
131 rwlock_t conns_lock; /* protects conns_all */
132 unsigned int conns_num; /* current # of connections */
133 unsigned short vlan_id; /* vlan id of link group */
Ursula Brauncd6851f2017-01-09 16:55:18 +0100134
135 struct list_head sndbufs[SMC_RMBE_SIZES];/* tx buffers */
136 rwlock_t sndbufs_lock; /* protects tx buffers */
137 struct list_head rmbs[SMC_RMBE_SIZES]; /* rx buffers */
138 rwlock_t rmbs_lock; /* protects rx buffers */
Ursula Braunbd4ad572017-01-09 16:55:20 +0100139 struct smc_rtoken rtokens[SMC_RMBS_PER_LGR_MAX]
140 [SMC_LINKS_PER_LGR_MAX];
141 /* remote addr/key pairs */
142 unsigned long rtokens_used_mask[BITS_TO_LONGS(
143 SMC_RMBS_PER_LGR_MAX)];
144 /* used rtoken elements */
145
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100146 u8 id[SMC_LGR_ID_SIZE]; /* unique lgr id */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100147 struct delayed_work free_work; /* delayed freeing of an lgr */
148 bool sync_err; /* lgr no longer fits to peer */
149};
150
151/* Find the connection associated with the given alert token in the link group.
152 * To use rbtrees we have to implement our own search core.
153 * Requires @conns_lock
154 * @token alert token to search for
155 * @lgr link group to search in
156 * Returns connection associated with token if found, NULL otherwise.
157 */
158static inline struct smc_connection *smc_lgr_find_conn(
159 u32 token, struct smc_link_group *lgr)
160{
161 struct smc_connection *res = NULL;
162 struct rb_node *node;
163
164 node = lgr->conns_all.rb_node;
165 while (node) {
166 struct smc_connection *cur = rb_entry(node,
167 struct smc_connection, alert_node);
168
169 if (cur->alert_token_local > token) {
170 node = node->rb_left;
171 } else {
172 if (cur->alert_token_local < token) {
173 node = node->rb_right;
174 } else {
175 res = cur;
176 break;
177 }
178 }
179 }
180
181 return res;
182}
183
Ursula Brauncd6851f2017-01-09 16:55:18 +0100184struct smc_sock;
185struct smc_clc_msg_accept_confirm;
186
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100187void smc_lgr_free(struct smc_link_group *lgr);
188void smc_lgr_terminate(struct smc_link_group *lgr);
Ursula Braun3e034722017-07-28 13:56:20 +0200189int smc_buf_create(struct smc_sock *smc);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100190int smc_rmb_rtoken_handling(struct smc_connection *conn,
191 struct smc_clc_msg_accept_confirm *clc);
Ursula Braun10428dd2017-07-28 13:56:22 +0200192void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn);
193void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn);
194void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn);
195void smc_rmb_sync_sg_for_device(struct smc_connection *conn);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100196#endif