xref: /OK3568_Linux_fs/kernel/drivers/infiniband/hw/hfi1/tid_rdma.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright(c) 2018 - 2020 Intel Corporation.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include "hfi.h"
8*4882a593Smuzhiyun #include "qp.h"
9*4882a593Smuzhiyun #include "rc.h"
10*4882a593Smuzhiyun #include "verbs.h"
11*4882a593Smuzhiyun #include "tid_rdma.h"
12*4882a593Smuzhiyun #include "exp_rcv.h"
13*4882a593Smuzhiyun #include "trace.h"
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /**
16*4882a593Smuzhiyun  * DOC: TID RDMA READ protocol
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * This is an end-to-end protocol at the hfi1 level between two nodes that
19*4882a593Smuzhiyun  * improves performance by avoiding data copy on the requester side. It
20*4882a593Smuzhiyun  * converts a qualified RDMA READ request into a TID RDMA READ request on
21*4882a593Smuzhiyun  * the requester side and thereafter handles the request and response
22*4882a593Smuzhiyun  * differently. To be qualified, the RDMA READ request should meet the
23*4882a593Smuzhiyun  * following:
24*4882a593Smuzhiyun  * -- The total data length should be greater than 256K;
25*4882a593Smuzhiyun  * -- The total data length should be a multiple of 4K page size;
26*4882a593Smuzhiyun  * -- Each local scatter-gather entry should be 4K page aligned;
27*4882a593Smuzhiyun  * -- Each local scatter-gather entry should be a multiple of 4K page size;
28*4882a593Smuzhiyun  */
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define RCV_TID_FLOW_TABLE_CTRL_FLOW_VALID_SMASK BIT_ULL(32)
31*4882a593Smuzhiyun #define RCV_TID_FLOW_TABLE_CTRL_HDR_SUPP_EN_SMASK BIT_ULL(33)
32*4882a593Smuzhiyun #define RCV_TID_FLOW_TABLE_CTRL_KEEP_AFTER_SEQ_ERR_SMASK BIT_ULL(34)
33*4882a593Smuzhiyun #define RCV_TID_FLOW_TABLE_CTRL_KEEP_ON_GEN_ERR_SMASK BIT_ULL(35)
34*4882a593Smuzhiyun #define RCV_TID_FLOW_TABLE_STATUS_SEQ_MISMATCH_SMASK BIT_ULL(37)
35*4882a593Smuzhiyun #define RCV_TID_FLOW_TABLE_STATUS_GEN_MISMATCH_SMASK BIT_ULL(38)
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /* Maximum number of packets within a flow generation. */
38*4882a593Smuzhiyun #define MAX_TID_FLOW_PSN BIT(HFI1_KDETH_BTH_SEQ_SHIFT)
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define GENERATION_MASK 0xFFFFF
41*4882a593Smuzhiyun 
mask_generation(u32 a)42*4882a593Smuzhiyun static u32 mask_generation(u32 a)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	return a & GENERATION_MASK;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /* Reserved generation value to set to unused flows for kernel contexts */
48*4882a593Smuzhiyun #define KERN_GENERATION_RESERVED mask_generation(U32_MAX)
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun  * J_KEY for kernel contexts when TID RDMA is used.
52*4882a593Smuzhiyun  * See generate_jkey() in hfi.h for more information.
53*4882a593Smuzhiyun  */
54*4882a593Smuzhiyun #define TID_RDMA_JKEY                   32
55*4882a593Smuzhiyun #define HFI1_KERNEL_MIN_JKEY HFI1_ADMIN_JKEY_RANGE
56*4882a593Smuzhiyun #define HFI1_KERNEL_MAX_JKEY (2 * HFI1_ADMIN_JKEY_RANGE - 1)
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /* Maximum number of segments in flight per QP request. */
59*4882a593Smuzhiyun #define TID_RDMA_MAX_READ_SEGS_PER_REQ  6
60*4882a593Smuzhiyun #define TID_RDMA_MAX_WRITE_SEGS_PER_REQ 4
61*4882a593Smuzhiyun #define MAX_REQ max_t(u16, TID_RDMA_MAX_READ_SEGS_PER_REQ, \
62*4882a593Smuzhiyun 			TID_RDMA_MAX_WRITE_SEGS_PER_REQ)
63*4882a593Smuzhiyun #define MAX_FLOWS roundup_pow_of_two(MAX_REQ + 1)
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun #define MAX_EXPECTED_PAGES     (MAX_EXPECTED_BUFFER / PAGE_SIZE)
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun #define TID_RDMA_DESTQP_FLOW_SHIFT      11
68*4882a593Smuzhiyun #define TID_RDMA_DESTQP_FLOW_MASK       0x1f
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #define TID_OPFN_QP_CTXT_MASK 0xff
71*4882a593Smuzhiyun #define TID_OPFN_QP_CTXT_SHIFT 56
72*4882a593Smuzhiyun #define TID_OPFN_QP_KDETH_MASK 0xff
73*4882a593Smuzhiyun #define TID_OPFN_QP_KDETH_SHIFT 48
74*4882a593Smuzhiyun #define TID_OPFN_MAX_LEN_MASK 0x7ff
75*4882a593Smuzhiyun #define TID_OPFN_MAX_LEN_SHIFT 37
76*4882a593Smuzhiyun #define TID_OPFN_TIMEOUT_MASK 0x1f
77*4882a593Smuzhiyun #define TID_OPFN_TIMEOUT_SHIFT 32
78*4882a593Smuzhiyun #define TID_OPFN_RESERVED_MASK 0x3f
79*4882a593Smuzhiyun #define TID_OPFN_RESERVED_SHIFT 26
80*4882a593Smuzhiyun #define TID_OPFN_URG_MASK 0x1
81*4882a593Smuzhiyun #define TID_OPFN_URG_SHIFT 25
82*4882a593Smuzhiyun #define TID_OPFN_VER_MASK 0x7
83*4882a593Smuzhiyun #define TID_OPFN_VER_SHIFT 22
84*4882a593Smuzhiyun #define TID_OPFN_JKEY_MASK 0x3f
85*4882a593Smuzhiyun #define TID_OPFN_JKEY_SHIFT 16
86*4882a593Smuzhiyun #define TID_OPFN_MAX_READ_MASK 0x3f
87*4882a593Smuzhiyun #define TID_OPFN_MAX_READ_SHIFT 10
88*4882a593Smuzhiyun #define TID_OPFN_MAX_WRITE_MASK 0x3f
89*4882a593Smuzhiyun #define TID_OPFN_MAX_WRITE_SHIFT 4
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun  * OPFN TID layout
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * 63               47               31               15
95*4882a593Smuzhiyun  * NNNNNNNNKKKKKKKK MMMMMMMMMMMTTTTT DDDDDDUVVVJJJJJJ RRRRRRWWWWWWCCCC
96*4882a593Smuzhiyun  * 3210987654321098 7654321098765432 1098765432109876 5432109876543210
97*4882a593Smuzhiyun  * N - the context Number
98*4882a593Smuzhiyun  * K - the Kdeth_qp
99*4882a593Smuzhiyun  * M - Max_len
100*4882a593Smuzhiyun  * T - Timeout
101*4882a593Smuzhiyun  * D - reserveD
102*4882a593Smuzhiyun  * V - version
103*4882a593Smuzhiyun  * U - Urg capable
104*4882a593Smuzhiyun  * J - Jkey
105*4882a593Smuzhiyun  * R - max_Read
106*4882a593Smuzhiyun  * W - max_Write
107*4882a593Smuzhiyun  * C - Capcode
108*4882a593Smuzhiyun  */
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun static void tid_rdma_trigger_resume(struct work_struct *work);
111*4882a593Smuzhiyun static void hfi1_kern_exp_rcv_free_flows(struct tid_rdma_request *req);
112*4882a593Smuzhiyun static int hfi1_kern_exp_rcv_alloc_flows(struct tid_rdma_request *req,
113*4882a593Smuzhiyun 					 gfp_t gfp);
114*4882a593Smuzhiyun static void hfi1_init_trdma_req(struct rvt_qp *qp,
115*4882a593Smuzhiyun 				struct tid_rdma_request *req);
116*4882a593Smuzhiyun static void hfi1_tid_write_alloc_resources(struct rvt_qp *qp, bool intr_ctx);
117*4882a593Smuzhiyun static void hfi1_tid_timeout(struct timer_list *t);
118*4882a593Smuzhiyun static void hfi1_add_tid_reap_timer(struct rvt_qp *qp);
119*4882a593Smuzhiyun static void hfi1_mod_tid_reap_timer(struct rvt_qp *qp);
120*4882a593Smuzhiyun static void hfi1_mod_tid_retry_timer(struct rvt_qp *qp);
121*4882a593Smuzhiyun static int hfi1_stop_tid_retry_timer(struct rvt_qp *qp);
122*4882a593Smuzhiyun static void hfi1_tid_retry_timeout(struct timer_list *t);
123*4882a593Smuzhiyun static int make_tid_rdma_ack(struct rvt_qp *qp,
124*4882a593Smuzhiyun 			     struct ib_other_headers *ohdr,
125*4882a593Smuzhiyun 			     struct hfi1_pkt_state *ps);
126*4882a593Smuzhiyun static void hfi1_do_tid_send(struct rvt_qp *qp);
127*4882a593Smuzhiyun static u32 read_r_next_psn(struct hfi1_devdata *dd, u8 ctxt, u8 fidx);
128*4882a593Smuzhiyun static void tid_rdma_rcv_err(struct hfi1_packet *packet,
129*4882a593Smuzhiyun 			     struct ib_other_headers *ohdr,
130*4882a593Smuzhiyun 			     struct rvt_qp *qp, u32 psn, int diff, bool fecn);
131*4882a593Smuzhiyun static void update_r_next_psn_fecn(struct hfi1_packet *packet,
132*4882a593Smuzhiyun 				   struct hfi1_qp_priv *priv,
133*4882a593Smuzhiyun 				   struct hfi1_ctxtdata *rcd,
134*4882a593Smuzhiyun 				   struct tid_rdma_flow *flow,
135*4882a593Smuzhiyun 				   bool fecn);
136*4882a593Smuzhiyun 
validate_r_tid_ack(struct hfi1_qp_priv * priv)137*4882a593Smuzhiyun static void validate_r_tid_ack(struct hfi1_qp_priv *priv)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	if (priv->r_tid_ack == HFI1_QP_WQE_INVALID)
140*4882a593Smuzhiyun 		priv->r_tid_ack = priv->r_tid_tail;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
tid_rdma_schedule_ack(struct rvt_qp * qp)143*4882a593Smuzhiyun static void tid_rdma_schedule_ack(struct rvt_qp *qp)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	priv->s_flags |= RVT_S_ACK_PENDING;
148*4882a593Smuzhiyun 	hfi1_schedule_tid_send(qp);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
tid_rdma_trigger_ack(struct rvt_qp * qp)151*4882a593Smuzhiyun static void tid_rdma_trigger_ack(struct rvt_qp *qp)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	validate_r_tid_ack(qp->priv);
154*4882a593Smuzhiyun 	tid_rdma_schedule_ack(qp);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
tid_rdma_opfn_encode(struct tid_rdma_params * p)157*4882a593Smuzhiyun static u64 tid_rdma_opfn_encode(struct tid_rdma_params *p)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun 	return
160*4882a593Smuzhiyun 		(((u64)p->qp & TID_OPFN_QP_CTXT_MASK) <<
161*4882a593Smuzhiyun 			TID_OPFN_QP_CTXT_SHIFT) |
162*4882a593Smuzhiyun 		((((u64)p->qp >> 16) & TID_OPFN_QP_KDETH_MASK) <<
163*4882a593Smuzhiyun 			TID_OPFN_QP_KDETH_SHIFT) |
164*4882a593Smuzhiyun 		(((u64)((p->max_len >> PAGE_SHIFT) - 1) &
165*4882a593Smuzhiyun 			TID_OPFN_MAX_LEN_MASK) << TID_OPFN_MAX_LEN_SHIFT) |
166*4882a593Smuzhiyun 		(((u64)p->timeout & TID_OPFN_TIMEOUT_MASK) <<
167*4882a593Smuzhiyun 			TID_OPFN_TIMEOUT_SHIFT) |
168*4882a593Smuzhiyun 		(((u64)p->urg & TID_OPFN_URG_MASK) << TID_OPFN_URG_SHIFT) |
169*4882a593Smuzhiyun 		(((u64)p->jkey & TID_OPFN_JKEY_MASK) << TID_OPFN_JKEY_SHIFT) |
170*4882a593Smuzhiyun 		(((u64)p->max_read & TID_OPFN_MAX_READ_MASK) <<
171*4882a593Smuzhiyun 			TID_OPFN_MAX_READ_SHIFT) |
172*4882a593Smuzhiyun 		(((u64)p->max_write & TID_OPFN_MAX_WRITE_MASK) <<
173*4882a593Smuzhiyun 			TID_OPFN_MAX_WRITE_SHIFT);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
tid_rdma_opfn_decode(struct tid_rdma_params * p,u64 data)176*4882a593Smuzhiyun static void tid_rdma_opfn_decode(struct tid_rdma_params *p, u64 data)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	p->max_len = (((data >> TID_OPFN_MAX_LEN_SHIFT) &
179*4882a593Smuzhiyun 		TID_OPFN_MAX_LEN_MASK) + 1) << PAGE_SHIFT;
180*4882a593Smuzhiyun 	p->jkey = (data >> TID_OPFN_JKEY_SHIFT) & TID_OPFN_JKEY_MASK;
181*4882a593Smuzhiyun 	p->max_write = (data >> TID_OPFN_MAX_WRITE_SHIFT) &
182*4882a593Smuzhiyun 		TID_OPFN_MAX_WRITE_MASK;
183*4882a593Smuzhiyun 	p->max_read = (data >> TID_OPFN_MAX_READ_SHIFT) &
184*4882a593Smuzhiyun 		TID_OPFN_MAX_READ_MASK;
185*4882a593Smuzhiyun 	p->qp =
186*4882a593Smuzhiyun 		((((data >> TID_OPFN_QP_KDETH_SHIFT) & TID_OPFN_QP_KDETH_MASK)
187*4882a593Smuzhiyun 			<< 16) |
188*4882a593Smuzhiyun 		((data >> TID_OPFN_QP_CTXT_SHIFT) & TID_OPFN_QP_CTXT_MASK));
189*4882a593Smuzhiyun 	p->urg = (data >> TID_OPFN_URG_SHIFT) & TID_OPFN_URG_MASK;
190*4882a593Smuzhiyun 	p->timeout = (data >> TID_OPFN_TIMEOUT_SHIFT) & TID_OPFN_TIMEOUT_MASK;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
tid_rdma_opfn_init(struct rvt_qp * qp,struct tid_rdma_params * p)193*4882a593Smuzhiyun void tid_rdma_opfn_init(struct rvt_qp *qp, struct tid_rdma_params *p)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	p->qp = (RVT_KDETH_QP_PREFIX << 16) | priv->rcd->ctxt;
198*4882a593Smuzhiyun 	p->max_len = TID_RDMA_MAX_SEGMENT_SIZE;
199*4882a593Smuzhiyun 	p->jkey = priv->rcd->jkey;
200*4882a593Smuzhiyun 	p->max_read = TID_RDMA_MAX_READ_SEGS_PER_REQ;
201*4882a593Smuzhiyun 	p->max_write = TID_RDMA_MAX_WRITE_SEGS_PER_REQ;
202*4882a593Smuzhiyun 	p->timeout = qp->timeout;
203*4882a593Smuzhiyun 	p->urg = is_urg_masked(priv->rcd);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
tid_rdma_conn_req(struct rvt_qp * qp,u64 * data)206*4882a593Smuzhiyun bool tid_rdma_conn_req(struct rvt_qp *qp, u64 *data)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	*data = tid_rdma_opfn_encode(&priv->tid_rdma.local);
211*4882a593Smuzhiyun 	return true;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun 
tid_rdma_conn_reply(struct rvt_qp * qp,u64 data)214*4882a593Smuzhiyun bool tid_rdma_conn_reply(struct rvt_qp *qp, u64 data)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
217*4882a593Smuzhiyun 	struct tid_rdma_params *remote, *old;
218*4882a593Smuzhiyun 	bool ret = true;
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	old = rcu_dereference_protected(priv->tid_rdma.remote,
221*4882a593Smuzhiyun 					lockdep_is_held(&priv->opfn.lock));
222*4882a593Smuzhiyun 	data &= ~0xfULL;
223*4882a593Smuzhiyun 	/*
224*4882a593Smuzhiyun 	 * If data passed in is zero, return true so as not to continue the
225*4882a593Smuzhiyun 	 * negotiation process
226*4882a593Smuzhiyun 	 */
227*4882a593Smuzhiyun 	if (!data || !HFI1_CAP_IS_KSET(TID_RDMA))
228*4882a593Smuzhiyun 		goto null;
229*4882a593Smuzhiyun 	/*
230*4882a593Smuzhiyun 	 * If kzalloc fails, return false. This will result in:
231*4882a593Smuzhiyun 	 * * at the requester a new OPFN request being generated to retry
232*4882a593Smuzhiyun 	 *   the negotiation
233*4882a593Smuzhiyun 	 * * at the responder, 0 being returned to the requester so as to
234*4882a593Smuzhiyun 	 *   disable TID RDMA at both the requester and the responder
235*4882a593Smuzhiyun 	 */
236*4882a593Smuzhiyun 	remote = kzalloc(sizeof(*remote), GFP_ATOMIC);
237*4882a593Smuzhiyun 	if (!remote) {
238*4882a593Smuzhiyun 		ret = false;
239*4882a593Smuzhiyun 		goto null;
240*4882a593Smuzhiyun 	}
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	tid_rdma_opfn_decode(remote, data);
243*4882a593Smuzhiyun 	priv->tid_timer_timeout_jiffies =
244*4882a593Smuzhiyun 		usecs_to_jiffies((((4096UL * (1UL << remote->timeout)) /
245*4882a593Smuzhiyun 				   1000UL) << 3) * 7);
246*4882a593Smuzhiyun 	trace_hfi1_opfn_param(qp, 0, &priv->tid_rdma.local);
247*4882a593Smuzhiyun 	trace_hfi1_opfn_param(qp, 1, remote);
248*4882a593Smuzhiyun 	rcu_assign_pointer(priv->tid_rdma.remote, remote);
249*4882a593Smuzhiyun 	/*
250*4882a593Smuzhiyun 	 * A TID RDMA READ request's segment size is not equal to
251*4882a593Smuzhiyun 	 * remote->max_len only when the request's data length is smaller
252*4882a593Smuzhiyun 	 * than remote->max_len. In that case, there will be only one segment.
253*4882a593Smuzhiyun 	 * Therefore, when priv->pkts_ps is used to calculate req->cur_seg
254*4882a593Smuzhiyun 	 * during retry, it will lead to req->cur_seg = 0, which is exactly
255*4882a593Smuzhiyun 	 * what is expected.
256*4882a593Smuzhiyun 	 */
257*4882a593Smuzhiyun 	priv->pkts_ps = (u16)rvt_div_mtu(qp, remote->max_len);
258*4882a593Smuzhiyun 	priv->timeout_shift = ilog2(priv->pkts_ps - 1) + 1;
259*4882a593Smuzhiyun 	goto free;
260*4882a593Smuzhiyun null:
261*4882a593Smuzhiyun 	RCU_INIT_POINTER(priv->tid_rdma.remote, NULL);
262*4882a593Smuzhiyun 	priv->timeout_shift = 0;
263*4882a593Smuzhiyun free:
264*4882a593Smuzhiyun 	if (old)
265*4882a593Smuzhiyun 		kfree_rcu(old, rcu_head);
266*4882a593Smuzhiyun 	return ret;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun 
tid_rdma_conn_resp(struct rvt_qp * qp,u64 * data)269*4882a593Smuzhiyun bool tid_rdma_conn_resp(struct rvt_qp *qp, u64 *data)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun 	bool ret;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	ret = tid_rdma_conn_reply(qp, *data);
274*4882a593Smuzhiyun 	*data = 0;
275*4882a593Smuzhiyun 	/*
276*4882a593Smuzhiyun 	 * If tid_rdma_conn_reply() returns error, set *data as 0 to indicate
277*4882a593Smuzhiyun 	 * TID RDMA could not be enabled. This will result in TID RDMA being
278*4882a593Smuzhiyun 	 * disabled at the requester too.
279*4882a593Smuzhiyun 	 */
280*4882a593Smuzhiyun 	if (ret)
281*4882a593Smuzhiyun 		(void)tid_rdma_conn_req(qp, data);
282*4882a593Smuzhiyun 	return ret;
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun 
tid_rdma_conn_error(struct rvt_qp * qp)285*4882a593Smuzhiyun void tid_rdma_conn_error(struct rvt_qp *qp)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
288*4882a593Smuzhiyun 	struct tid_rdma_params *old;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	old = rcu_dereference_protected(priv->tid_rdma.remote,
291*4882a593Smuzhiyun 					lockdep_is_held(&priv->opfn.lock));
292*4882a593Smuzhiyun 	RCU_INIT_POINTER(priv->tid_rdma.remote, NULL);
293*4882a593Smuzhiyun 	if (old)
294*4882a593Smuzhiyun 		kfree_rcu(old, rcu_head);
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun /* This is called at context initialization time */
hfi1_kern_exp_rcv_init(struct hfi1_ctxtdata * rcd,int reinit)298*4882a593Smuzhiyun int hfi1_kern_exp_rcv_init(struct hfi1_ctxtdata *rcd, int reinit)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun 	if (reinit)
301*4882a593Smuzhiyun 		return 0;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	BUILD_BUG_ON(TID_RDMA_JKEY < HFI1_KERNEL_MIN_JKEY);
304*4882a593Smuzhiyun 	BUILD_BUG_ON(TID_RDMA_JKEY > HFI1_KERNEL_MAX_JKEY);
305*4882a593Smuzhiyun 	rcd->jkey = TID_RDMA_JKEY;
306*4882a593Smuzhiyun 	hfi1_set_ctxt_jkey(rcd->dd, rcd, rcd->jkey);
307*4882a593Smuzhiyun 	return hfi1_alloc_ctxt_rcv_groups(rcd);
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun /**
311*4882a593Smuzhiyun  * qp_to_rcd - determine the receive context used by a qp
312*4882a593Smuzhiyun  * @qp - the qp
313*4882a593Smuzhiyun  *
314*4882a593Smuzhiyun  * This routine returns the receive context associated
315*4882a593Smuzhiyun  * with a a qp's qpn.
316*4882a593Smuzhiyun  *
317*4882a593Smuzhiyun  * Returns the context.
318*4882a593Smuzhiyun  */
qp_to_rcd(struct rvt_dev_info * rdi,struct rvt_qp * qp)319*4882a593Smuzhiyun static struct hfi1_ctxtdata *qp_to_rcd(struct rvt_dev_info *rdi,
320*4882a593Smuzhiyun 				       struct rvt_qp *qp)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	struct hfi1_ibdev *verbs_dev = container_of(rdi,
323*4882a593Smuzhiyun 						    struct hfi1_ibdev,
324*4882a593Smuzhiyun 						    rdi);
325*4882a593Smuzhiyun 	struct hfi1_devdata *dd = container_of(verbs_dev,
326*4882a593Smuzhiyun 					       struct hfi1_devdata,
327*4882a593Smuzhiyun 					       verbs_dev);
328*4882a593Smuzhiyun 	unsigned int ctxt;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	if (qp->ibqp.qp_num == 0)
331*4882a593Smuzhiyun 		ctxt = 0;
332*4882a593Smuzhiyun 	else
333*4882a593Smuzhiyun 		ctxt = hfi1_get_qp_map(dd, qp->ibqp.qp_num >> dd->qos_shift);
334*4882a593Smuzhiyun 	return dd->rcd[ctxt];
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun 
hfi1_qp_priv_init(struct rvt_dev_info * rdi,struct rvt_qp * qp,struct ib_qp_init_attr * init_attr)337*4882a593Smuzhiyun int hfi1_qp_priv_init(struct rvt_dev_info *rdi, struct rvt_qp *qp,
338*4882a593Smuzhiyun 		      struct ib_qp_init_attr *init_attr)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
341*4882a593Smuzhiyun 	int i, ret;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	qpriv->rcd = qp_to_rcd(rdi, qp);
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	spin_lock_init(&qpriv->opfn.lock);
346*4882a593Smuzhiyun 	INIT_WORK(&qpriv->opfn.opfn_work, opfn_send_conn_request);
347*4882a593Smuzhiyun 	INIT_WORK(&qpriv->tid_rdma.trigger_work, tid_rdma_trigger_resume);
348*4882a593Smuzhiyun 	qpriv->flow_state.psn = 0;
349*4882a593Smuzhiyun 	qpriv->flow_state.index = RXE_NUM_TID_FLOWS;
350*4882a593Smuzhiyun 	qpriv->flow_state.last_index = RXE_NUM_TID_FLOWS;
351*4882a593Smuzhiyun 	qpriv->flow_state.generation = KERN_GENERATION_RESERVED;
352*4882a593Smuzhiyun 	qpriv->s_state = TID_OP(WRITE_RESP);
353*4882a593Smuzhiyun 	qpriv->s_tid_cur = HFI1_QP_WQE_INVALID;
354*4882a593Smuzhiyun 	qpriv->s_tid_head = HFI1_QP_WQE_INVALID;
355*4882a593Smuzhiyun 	qpriv->s_tid_tail = HFI1_QP_WQE_INVALID;
356*4882a593Smuzhiyun 	qpriv->rnr_nak_state = TID_RNR_NAK_INIT;
357*4882a593Smuzhiyun 	qpriv->r_tid_head = HFI1_QP_WQE_INVALID;
358*4882a593Smuzhiyun 	qpriv->r_tid_tail = HFI1_QP_WQE_INVALID;
359*4882a593Smuzhiyun 	qpriv->r_tid_ack = HFI1_QP_WQE_INVALID;
360*4882a593Smuzhiyun 	qpriv->r_tid_alloc = HFI1_QP_WQE_INVALID;
361*4882a593Smuzhiyun 	atomic_set(&qpriv->n_requests, 0);
362*4882a593Smuzhiyun 	atomic_set(&qpriv->n_tid_requests, 0);
363*4882a593Smuzhiyun 	timer_setup(&qpriv->s_tid_timer, hfi1_tid_timeout, 0);
364*4882a593Smuzhiyun 	timer_setup(&qpriv->s_tid_retry_timer, hfi1_tid_retry_timeout, 0);
365*4882a593Smuzhiyun 	INIT_LIST_HEAD(&qpriv->tid_wait);
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	if (init_attr->qp_type == IB_QPT_RC && HFI1_CAP_IS_KSET(TID_RDMA)) {
368*4882a593Smuzhiyun 		struct hfi1_devdata *dd = qpriv->rcd->dd;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 		qpriv->pages = kzalloc_node(TID_RDMA_MAX_PAGES *
371*4882a593Smuzhiyun 						sizeof(*qpriv->pages),
372*4882a593Smuzhiyun 					    GFP_KERNEL, dd->node);
373*4882a593Smuzhiyun 		if (!qpriv->pages)
374*4882a593Smuzhiyun 			return -ENOMEM;
375*4882a593Smuzhiyun 		for (i = 0; i < qp->s_size; i++) {
376*4882a593Smuzhiyun 			struct hfi1_swqe_priv *priv;
377*4882a593Smuzhiyun 			struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, i);
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 			priv = kzalloc_node(sizeof(*priv), GFP_KERNEL,
380*4882a593Smuzhiyun 					    dd->node);
381*4882a593Smuzhiyun 			if (!priv)
382*4882a593Smuzhiyun 				return -ENOMEM;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 			hfi1_init_trdma_req(qp, &priv->tid_req);
385*4882a593Smuzhiyun 			priv->tid_req.e.swqe = wqe;
386*4882a593Smuzhiyun 			wqe->priv = priv;
387*4882a593Smuzhiyun 		}
388*4882a593Smuzhiyun 		for (i = 0; i < rvt_max_atomic(rdi); i++) {
389*4882a593Smuzhiyun 			struct hfi1_ack_priv *priv;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 			priv = kzalloc_node(sizeof(*priv), GFP_KERNEL,
392*4882a593Smuzhiyun 					    dd->node);
393*4882a593Smuzhiyun 			if (!priv)
394*4882a593Smuzhiyun 				return -ENOMEM;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 			hfi1_init_trdma_req(qp, &priv->tid_req);
397*4882a593Smuzhiyun 			priv->tid_req.e.ack = &qp->s_ack_queue[i];
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 			ret = hfi1_kern_exp_rcv_alloc_flows(&priv->tid_req,
400*4882a593Smuzhiyun 							    GFP_KERNEL);
401*4882a593Smuzhiyun 			if (ret) {
402*4882a593Smuzhiyun 				kfree(priv);
403*4882a593Smuzhiyun 				return ret;
404*4882a593Smuzhiyun 			}
405*4882a593Smuzhiyun 			qp->s_ack_queue[i].priv = priv;
406*4882a593Smuzhiyun 		}
407*4882a593Smuzhiyun 	}
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	return 0;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
hfi1_qp_priv_tid_free(struct rvt_dev_info * rdi,struct rvt_qp * qp)412*4882a593Smuzhiyun void hfi1_qp_priv_tid_free(struct rvt_dev_info *rdi, struct rvt_qp *qp)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
415*4882a593Smuzhiyun 	struct rvt_swqe *wqe;
416*4882a593Smuzhiyun 	u32 i;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	if (qp->ibqp.qp_type == IB_QPT_RC && HFI1_CAP_IS_KSET(TID_RDMA)) {
419*4882a593Smuzhiyun 		for (i = 0; i < qp->s_size; i++) {
420*4882a593Smuzhiyun 			wqe = rvt_get_swqe_ptr(qp, i);
421*4882a593Smuzhiyun 			kfree(wqe->priv);
422*4882a593Smuzhiyun 			wqe->priv = NULL;
423*4882a593Smuzhiyun 		}
424*4882a593Smuzhiyun 		for (i = 0; i < rvt_max_atomic(rdi); i++) {
425*4882a593Smuzhiyun 			struct hfi1_ack_priv *priv = qp->s_ack_queue[i].priv;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 			if (priv)
428*4882a593Smuzhiyun 				hfi1_kern_exp_rcv_free_flows(&priv->tid_req);
429*4882a593Smuzhiyun 			kfree(priv);
430*4882a593Smuzhiyun 			qp->s_ack_queue[i].priv = NULL;
431*4882a593Smuzhiyun 		}
432*4882a593Smuzhiyun 		cancel_work_sync(&qpriv->opfn.opfn_work);
433*4882a593Smuzhiyun 		kfree(qpriv->pages);
434*4882a593Smuzhiyun 		qpriv->pages = NULL;
435*4882a593Smuzhiyun 	}
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun /* Flow and tid waiter functions */
439*4882a593Smuzhiyun /**
440*4882a593Smuzhiyun  * DOC: lock ordering
441*4882a593Smuzhiyun  *
442*4882a593Smuzhiyun  * There are two locks involved with the queuing
443*4882a593Smuzhiyun  * routines: the qp s_lock and the exp_lock.
444*4882a593Smuzhiyun  *
445*4882a593Smuzhiyun  * Since the tid space allocation is called from
446*4882a593Smuzhiyun  * the send engine, the qp s_lock is already held.
447*4882a593Smuzhiyun  *
448*4882a593Smuzhiyun  * The allocation routines will get the exp_lock.
449*4882a593Smuzhiyun  *
450*4882a593Smuzhiyun  * The first_qp() call is provided to allow the head of
451*4882a593Smuzhiyun  * the rcd wait queue to be fetched under the exp_lock and
452*4882a593Smuzhiyun  * followed by a drop of the exp_lock.
453*4882a593Smuzhiyun  *
454*4882a593Smuzhiyun  * Any qp in the wait list will have the qp reference count held
455*4882a593Smuzhiyun  * to hold the qp in memory.
456*4882a593Smuzhiyun  */
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun /*
459*4882a593Smuzhiyun  * return head of rcd wait list
460*4882a593Smuzhiyun  *
461*4882a593Smuzhiyun  * Must hold the exp_lock.
462*4882a593Smuzhiyun  *
463*4882a593Smuzhiyun  * Get a reference to the QP to hold the QP in memory.
464*4882a593Smuzhiyun  *
465*4882a593Smuzhiyun  * The caller must release the reference when the local
466*4882a593Smuzhiyun  * is no longer being used.
467*4882a593Smuzhiyun  */
first_qp(struct hfi1_ctxtdata * rcd,struct tid_queue * queue)468*4882a593Smuzhiyun static struct rvt_qp *first_qp(struct hfi1_ctxtdata *rcd,
469*4882a593Smuzhiyun 			       struct tid_queue *queue)
470*4882a593Smuzhiyun 	__must_hold(&rcd->exp_lock)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	lockdep_assert_held(&rcd->exp_lock);
475*4882a593Smuzhiyun 	priv = list_first_entry_or_null(&queue->queue_head,
476*4882a593Smuzhiyun 					struct hfi1_qp_priv,
477*4882a593Smuzhiyun 					tid_wait);
478*4882a593Smuzhiyun 	if (!priv)
479*4882a593Smuzhiyun 		return NULL;
480*4882a593Smuzhiyun 	rvt_get_qp(priv->owner);
481*4882a593Smuzhiyun 	return priv->owner;
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun /**
485*4882a593Smuzhiyun  * kernel_tid_waiters - determine rcd wait
486*4882a593Smuzhiyun  * @rcd: the receive context
487*4882a593Smuzhiyun  * @qp: the head of the qp being processed
488*4882a593Smuzhiyun  *
489*4882a593Smuzhiyun  * This routine will return false IFF
490*4882a593Smuzhiyun  * the list is NULL or the head of the
491*4882a593Smuzhiyun  * list is the indicated qp.
492*4882a593Smuzhiyun  *
493*4882a593Smuzhiyun  * Must hold the qp s_lock and the exp_lock.
494*4882a593Smuzhiyun  *
495*4882a593Smuzhiyun  * Return:
496*4882a593Smuzhiyun  * false if either of the conditions below are satisfied:
497*4882a593Smuzhiyun  * 1. The list is empty or
498*4882a593Smuzhiyun  * 2. The indicated qp is at the head of the list and the
499*4882a593Smuzhiyun  *    HFI1_S_WAIT_TID_SPACE bit is set in qp->s_flags.
500*4882a593Smuzhiyun  * true is returned otherwise.
501*4882a593Smuzhiyun  */
kernel_tid_waiters(struct hfi1_ctxtdata * rcd,struct tid_queue * queue,struct rvt_qp * qp)502*4882a593Smuzhiyun static bool kernel_tid_waiters(struct hfi1_ctxtdata *rcd,
503*4882a593Smuzhiyun 			       struct tid_queue *queue, struct rvt_qp *qp)
504*4882a593Smuzhiyun 	__must_hold(&rcd->exp_lock) __must_hold(&qp->s_lock)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun 	struct rvt_qp *fqp;
507*4882a593Smuzhiyun 	bool ret = true;
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
510*4882a593Smuzhiyun 	lockdep_assert_held(&rcd->exp_lock);
511*4882a593Smuzhiyun 	fqp = first_qp(rcd, queue);
512*4882a593Smuzhiyun 	if (!fqp || (fqp == qp && (qp->s_flags & HFI1_S_WAIT_TID_SPACE)))
513*4882a593Smuzhiyun 		ret = false;
514*4882a593Smuzhiyun 	rvt_put_qp(fqp);
515*4882a593Smuzhiyun 	return ret;
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun /**
519*4882a593Smuzhiyun  * dequeue_tid_waiter - dequeue the qp from the list
520*4882a593Smuzhiyun  * @qp - the qp to remove the wait list
521*4882a593Smuzhiyun  *
522*4882a593Smuzhiyun  * This routine removes the indicated qp from the
523*4882a593Smuzhiyun  * wait list if it is there.
524*4882a593Smuzhiyun  *
525*4882a593Smuzhiyun  * This should be done after the hardware flow and
526*4882a593Smuzhiyun  * tid array resources have been allocated.
527*4882a593Smuzhiyun  *
528*4882a593Smuzhiyun  * Must hold the qp s_lock and the rcd exp_lock.
529*4882a593Smuzhiyun  *
530*4882a593Smuzhiyun  * It assumes the s_lock to protect the s_flags
531*4882a593Smuzhiyun  * field and to reliably test the HFI1_S_WAIT_TID_SPACE flag.
532*4882a593Smuzhiyun  */
dequeue_tid_waiter(struct hfi1_ctxtdata * rcd,struct tid_queue * queue,struct rvt_qp * qp)533*4882a593Smuzhiyun static void dequeue_tid_waiter(struct hfi1_ctxtdata *rcd,
534*4882a593Smuzhiyun 			       struct tid_queue *queue, struct rvt_qp *qp)
535*4882a593Smuzhiyun 	__must_hold(&rcd->exp_lock) __must_hold(&qp->s_lock)
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
540*4882a593Smuzhiyun 	lockdep_assert_held(&rcd->exp_lock);
541*4882a593Smuzhiyun 	if (list_empty(&priv->tid_wait))
542*4882a593Smuzhiyun 		return;
543*4882a593Smuzhiyun 	list_del_init(&priv->tid_wait);
544*4882a593Smuzhiyun 	qp->s_flags &= ~HFI1_S_WAIT_TID_SPACE;
545*4882a593Smuzhiyun 	queue->dequeue++;
546*4882a593Smuzhiyun 	rvt_put_qp(qp);
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun /**
550*4882a593Smuzhiyun  * queue_qp_for_tid_wait - suspend QP on tid space
551*4882a593Smuzhiyun  * @rcd: the receive context
552*4882a593Smuzhiyun  * @qp: the qp
553*4882a593Smuzhiyun  *
554*4882a593Smuzhiyun  * The qp is inserted at the tail of the rcd
555*4882a593Smuzhiyun  * wait queue and the HFI1_S_WAIT_TID_SPACE s_flag is set.
556*4882a593Smuzhiyun  *
557*4882a593Smuzhiyun  * Must hold the qp s_lock and the exp_lock.
558*4882a593Smuzhiyun  */
queue_qp_for_tid_wait(struct hfi1_ctxtdata * rcd,struct tid_queue * queue,struct rvt_qp * qp)559*4882a593Smuzhiyun static void queue_qp_for_tid_wait(struct hfi1_ctxtdata *rcd,
560*4882a593Smuzhiyun 				  struct tid_queue *queue, struct rvt_qp *qp)
561*4882a593Smuzhiyun 	__must_hold(&rcd->exp_lock) __must_hold(&qp->s_lock)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
566*4882a593Smuzhiyun 	lockdep_assert_held(&rcd->exp_lock);
567*4882a593Smuzhiyun 	if (list_empty(&priv->tid_wait)) {
568*4882a593Smuzhiyun 		qp->s_flags |= HFI1_S_WAIT_TID_SPACE;
569*4882a593Smuzhiyun 		list_add_tail(&priv->tid_wait, &queue->queue_head);
570*4882a593Smuzhiyun 		priv->tid_enqueue = ++queue->enqueue;
571*4882a593Smuzhiyun 		rcd->dd->verbs_dev.n_tidwait++;
572*4882a593Smuzhiyun 		trace_hfi1_qpsleep(qp, HFI1_S_WAIT_TID_SPACE);
573*4882a593Smuzhiyun 		rvt_get_qp(qp);
574*4882a593Smuzhiyun 	}
575*4882a593Smuzhiyun }
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun /**
578*4882a593Smuzhiyun  * __trigger_tid_waiter - trigger tid waiter
579*4882a593Smuzhiyun  * @qp: the qp
580*4882a593Smuzhiyun  *
581*4882a593Smuzhiyun  * This is a private entrance to schedule the qp
582*4882a593Smuzhiyun  * assuming the caller is holding the qp->s_lock.
583*4882a593Smuzhiyun  */
__trigger_tid_waiter(struct rvt_qp * qp)584*4882a593Smuzhiyun static void __trigger_tid_waiter(struct rvt_qp *qp)
585*4882a593Smuzhiyun 	__must_hold(&qp->s_lock)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
588*4882a593Smuzhiyun 	if (!(qp->s_flags & HFI1_S_WAIT_TID_SPACE))
589*4882a593Smuzhiyun 		return;
590*4882a593Smuzhiyun 	trace_hfi1_qpwakeup(qp, HFI1_S_WAIT_TID_SPACE);
591*4882a593Smuzhiyun 	hfi1_schedule_send(qp);
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun /**
595*4882a593Smuzhiyun  * tid_rdma_schedule_tid_wakeup - schedule wakeup for a qp
596*4882a593Smuzhiyun  * @qp - the qp
597*4882a593Smuzhiyun  *
598*4882a593Smuzhiyun  * trigger a schedule or a waiting qp in a deadlock
599*4882a593Smuzhiyun  * safe manner.  The qp reference is held prior
600*4882a593Smuzhiyun  * to this call via first_qp().
601*4882a593Smuzhiyun  *
602*4882a593Smuzhiyun  * If the qp trigger was already scheduled (!rval)
603*4882a593Smuzhiyun  * the the reference is dropped, otherwise the resume
604*4882a593Smuzhiyun  * or the destroy cancel will dispatch the reference.
605*4882a593Smuzhiyun  */
tid_rdma_schedule_tid_wakeup(struct rvt_qp * qp)606*4882a593Smuzhiyun static void tid_rdma_schedule_tid_wakeup(struct rvt_qp *qp)
607*4882a593Smuzhiyun {
608*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv;
609*4882a593Smuzhiyun 	struct hfi1_ibport *ibp;
610*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd;
611*4882a593Smuzhiyun 	struct hfi1_devdata *dd;
612*4882a593Smuzhiyun 	bool rval;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	if (!qp)
615*4882a593Smuzhiyun 		return;
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	priv = qp->priv;
618*4882a593Smuzhiyun 	ibp = to_iport(qp->ibqp.device, qp->port_num);
619*4882a593Smuzhiyun 	ppd = ppd_from_ibp(ibp);
620*4882a593Smuzhiyun 	dd = dd_from_ibdev(qp->ibqp.device);
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	rval = queue_work_on(priv->s_sde ?
623*4882a593Smuzhiyun 			     priv->s_sde->cpu :
624*4882a593Smuzhiyun 			     cpumask_first(cpumask_of_node(dd->node)),
625*4882a593Smuzhiyun 			     ppd->hfi1_wq,
626*4882a593Smuzhiyun 			     &priv->tid_rdma.trigger_work);
627*4882a593Smuzhiyun 	if (!rval)
628*4882a593Smuzhiyun 		rvt_put_qp(qp);
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun /**
632*4882a593Smuzhiyun  * tid_rdma_trigger_resume - field a trigger work request
633*4882a593Smuzhiyun  * @work - the work item
634*4882a593Smuzhiyun  *
635*4882a593Smuzhiyun  * Complete the off qp trigger processing by directly
636*4882a593Smuzhiyun  * calling the progress routine.
637*4882a593Smuzhiyun  */
tid_rdma_trigger_resume(struct work_struct * work)638*4882a593Smuzhiyun static void tid_rdma_trigger_resume(struct work_struct *work)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun 	struct tid_rdma_qp_params *tr;
641*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv;
642*4882a593Smuzhiyun 	struct rvt_qp *qp;
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	tr = container_of(work, struct tid_rdma_qp_params, trigger_work);
645*4882a593Smuzhiyun 	priv = container_of(tr, struct hfi1_qp_priv, tid_rdma);
646*4882a593Smuzhiyun 	qp = priv->owner;
647*4882a593Smuzhiyun 	spin_lock_irq(&qp->s_lock);
648*4882a593Smuzhiyun 	if (qp->s_flags & HFI1_S_WAIT_TID_SPACE) {
649*4882a593Smuzhiyun 		spin_unlock_irq(&qp->s_lock);
650*4882a593Smuzhiyun 		hfi1_do_send(priv->owner, true);
651*4882a593Smuzhiyun 	} else {
652*4882a593Smuzhiyun 		spin_unlock_irq(&qp->s_lock);
653*4882a593Smuzhiyun 	}
654*4882a593Smuzhiyun 	rvt_put_qp(qp);
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun /**
658*4882a593Smuzhiyun  * tid_rdma_flush_wait - unwind any tid space wait
659*4882a593Smuzhiyun  *
660*4882a593Smuzhiyun  * This is called when resetting a qp to
661*4882a593Smuzhiyun  * allow a destroy or reset to get rid
662*4882a593Smuzhiyun  * of any tid space linkage and reference counts.
663*4882a593Smuzhiyun  */
_tid_rdma_flush_wait(struct rvt_qp * qp,struct tid_queue * queue)664*4882a593Smuzhiyun static void _tid_rdma_flush_wait(struct rvt_qp *qp, struct tid_queue *queue)
665*4882a593Smuzhiyun 	__must_hold(&qp->s_lock)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	if (!qp)
670*4882a593Smuzhiyun 		return;
671*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
672*4882a593Smuzhiyun 	priv = qp->priv;
673*4882a593Smuzhiyun 	qp->s_flags &= ~HFI1_S_WAIT_TID_SPACE;
674*4882a593Smuzhiyun 	spin_lock(&priv->rcd->exp_lock);
675*4882a593Smuzhiyun 	if (!list_empty(&priv->tid_wait)) {
676*4882a593Smuzhiyun 		list_del_init(&priv->tid_wait);
677*4882a593Smuzhiyun 		qp->s_flags &= ~HFI1_S_WAIT_TID_SPACE;
678*4882a593Smuzhiyun 		queue->dequeue++;
679*4882a593Smuzhiyun 		rvt_put_qp(qp);
680*4882a593Smuzhiyun 	}
681*4882a593Smuzhiyun 	spin_unlock(&priv->rcd->exp_lock);
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun 
hfi1_tid_rdma_flush_wait(struct rvt_qp * qp)684*4882a593Smuzhiyun void hfi1_tid_rdma_flush_wait(struct rvt_qp *qp)
685*4882a593Smuzhiyun 	__must_hold(&qp->s_lock)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	_tid_rdma_flush_wait(qp, &priv->rcd->flow_queue);
690*4882a593Smuzhiyun 	_tid_rdma_flush_wait(qp, &priv->rcd->rarr_queue);
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun /* Flow functions */
694*4882a593Smuzhiyun /**
695*4882a593Smuzhiyun  * kern_reserve_flow - allocate a hardware flow
696*4882a593Smuzhiyun  * @rcd - the context to use for allocation
697*4882a593Smuzhiyun  * @last - the index of the preferred flow. Use RXE_NUM_TID_FLOWS to
698*4882a593Smuzhiyun  *         signify "don't care".
699*4882a593Smuzhiyun  *
700*4882a593Smuzhiyun  * Use a bit mask based allocation to reserve a hardware
701*4882a593Smuzhiyun  * flow for use in receiving KDETH data packets. If a preferred flow is
702*4882a593Smuzhiyun  * specified the function will attempt to reserve that flow again, if
703*4882a593Smuzhiyun  * available.
704*4882a593Smuzhiyun  *
705*4882a593Smuzhiyun  * The exp_lock must be held.
706*4882a593Smuzhiyun  *
707*4882a593Smuzhiyun  * Return:
708*4882a593Smuzhiyun  * On success: a value postive value between 0 and RXE_NUM_TID_FLOWS - 1
709*4882a593Smuzhiyun  * On failure: -EAGAIN
710*4882a593Smuzhiyun  */
kern_reserve_flow(struct hfi1_ctxtdata * rcd,int last)711*4882a593Smuzhiyun static int kern_reserve_flow(struct hfi1_ctxtdata *rcd, int last)
712*4882a593Smuzhiyun 	__must_hold(&rcd->exp_lock)
713*4882a593Smuzhiyun {
714*4882a593Smuzhiyun 	int nr;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	/* Attempt to reserve the preferred flow index */
717*4882a593Smuzhiyun 	if (last >= 0 && last < RXE_NUM_TID_FLOWS &&
718*4882a593Smuzhiyun 	    !test_and_set_bit(last, &rcd->flow_mask))
719*4882a593Smuzhiyun 		return last;
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	nr = ffz(rcd->flow_mask);
722*4882a593Smuzhiyun 	BUILD_BUG_ON(RXE_NUM_TID_FLOWS >=
723*4882a593Smuzhiyun 		     (sizeof(rcd->flow_mask) * BITS_PER_BYTE));
724*4882a593Smuzhiyun 	if (nr > (RXE_NUM_TID_FLOWS - 1))
725*4882a593Smuzhiyun 		return -EAGAIN;
726*4882a593Smuzhiyun 	set_bit(nr, &rcd->flow_mask);
727*4882a593Smuzhiyun 	return nr;
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
kern_set_hw_flow(struct hfi1_ctxtdata * rcd,u32 generation,u32 flow_idx)730*4882a593Smuzhiyun static void kern_set_hw_flow(struct hfi1_ctxtdata *rcd, u32 generation,
731*4882a593Smuzhiyun 			     u32 flow_idx)
732*4882a593Smuzhiyun {
733*4882a593Smuzhiyun 	u64 reg;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	reg = ((u64)generation << HFI1_KDETH_BTH_SEQ_SHIFT) |
736*4882a593Smuzhiyun 		RCV_TID_FLOW_TABLE_CTRL_FLOW_VALID_SMASK |
737*4882a593Smuzhiyun 		RCV_TID_FLOW_TABLE_CTRL_KEEP_AFTER_SEQ_ERR_SMASK |
738*4882a593Smuzhiyun 		RCV_TID_FLOW_TABLE_CTRL_KEEP_ON_GEN_ERR_SMASK |
739*4882a593Smuzhiyun 		RCV_TID_FLOW_TABLE_STATUS_SEQ_MISMATCH_SMASK |
740*4882a593Smuzhiyun 		RCV_TID_FLOW_TABLE_STATUS_GEN_MISMATCH_SMASK;
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	if (generation != KERN_GENERATION_RESERVED)
743*4882a593Smuzhiyun 		reg |= RCV_TID_FLOW_TABLE_CTRL_HDR_SUPP_EN_SMASK;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	write_uctxt_csr(rcd->dd, rcd->ctxt,
746*4882a593Smuzhiyun 			RCV_TID_FLOW_TABLE + 8 * flow_idx, reg);
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun 
kern_setup_hw_flow(struct hfi1_ctxtdata * rcd,u32 flow_idx)749*4882a593Smuzhiyun static u32 kern_setup_hw_flow(struct hfi1_ctxtdata *rcd, u32 flow_idx)
750*4882a593Smuzhiyun 	__must_hold(&rcd->exp_lock)
751*4882a593Smuzhiyun {
752*4882a593Smuzhiyun 	u32 generation = rcd->flows[flow_idx].generation;
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	kern_set_hw_flow(rcd, generation, flow_idx);
755*4882a593Smuzhiyun 	return generation;
756*4882a593Smuzhiyun }
757*4882a593Smuzhiyun 
kern_flow_generation_next(u32 gen)758*4882a593Smuzhiyun static u32 kern_flow_generation_next(u32 gen)
759*4882a593Smuzhiyun {
760*4882a593Smuzhiyun 	u32 generation = mask_generation(gen + 1);
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	if (generation == KERN_GENERATION_RESERVED)
763*4882a593Smuzhiyun 		generation = mask_generation(generation + 1);
764*4882a593Smuzhiyun 	return generation;
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun 
kern_clear_hw_flow(struct hfi1_ctxtdata * rcd,u32 flow_idx)767*4882a593Smuzhiyun static void kern_clear_hw_flow(struct hfi1_ctxtdata *rcd, u32 flow_idx)
768*4882a593Smuzhiyun 	__must_hold(&rcd->exp_lock)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun 	rcd->flows[flow_idx].generation =
771*4882a593Smuzhiyun 		kern_flow_generation_next(rcd->flows[flow_idx].generation);
772*4882a593Smuzhiyun 	kern_set_hw_flow(rcd, KERN_GENERATION_RESERVED, flow_idx);
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun 
hfi1_kern_setup_hw_flow(struct hfi1_ctxtdata * rcd,struct rvt_qp * qp)775*4882a593Smuzhiyun int hfi1_kern_setup_hw_flow(struct hfi1_ctxtdata *rcd, struct rvt_qp *qp)
776*4882a593Smuzhiyun {
777*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = (struct hfi1_qp_priv *)qp->priv;
778*4882a593Smuzhiyun 	struct tid_flow_state *fs = &qpriv->flow_state;
779*4882a593Smuzhiyun 	struct rvt_qp *fqp;
780*4882a593Smuzhiyun 	unsigned long flags;
781*4882a593Smuzhiyun 	int ret = 0;
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	/* The QP already has an allocated flow */
784*4882a593Smuzhiyun 	if (fs->index != RXE_NUM_TID_FLOWS)
785*4882a593Smuzhiyun 		return ret;
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	spin_lock_irqsave(&rcd->exp_lock, flags);
788*4882a593Smuzhiyun 	if (kernel_tid_waiters(rcd, &rcd->flow_queue, qp))
789*4882a593Smuzhiyun 		goto queue;
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	ret = kern_reserve_flow(rcd, fs->last_index);
792*4882a593Smuzhiyun 	if (ret < 0)
793*4882a593Smuzhiyun 		goto queue;
794*4882a593Smuzhiyun 	fs->index = ret;
795*4882a593Smuzhiyun 	fs->last_index = fs->index;
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	/* Generation received in a RESYNC overrides default flow generation */
798*4882a593Smuzhiyun 	if (fs->generation != KERN_GENERATION_RESERVED)
799*4882a593Smuzhiyun 		rcd->flows[fs->index].generation = fs->generation;
800*4882a593Smuzhiyun 	fs->generation = kern_setup_hw_flow(rcd, fs->index);
801*4882a593Smuzhiyun 	fs->psn = 0;
802*4882a593Smuzhiyun 	dequeue_tid_waiter(rcd, &rcd->flow_queue, qp);
803*4882a593Smuzhiyun 	/* get head before dropping lock */
804*4882a593Smuzhiyun 	fqp = first_qp(rcd, &rcd->flow_queue);
805*4882a593Smuzhiyun 	spin_unlock_irqrestore(&rcd->exp_lock, flags);
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	tid_rdma_schedule_tid_wakeup(fqp);
808*4882a593Smuzhiyun 	return 0;
809*4882a593Smuzhiyun queue:
810*4882a593Smuzhiyun 	queue_qp_for_tid_wait(rcd, &rcd->flow_queue, qp);
811*4882a593Smuzhiyun 	spin_unlock_irqrestore(&rcd->exp_lock, flags);
812*4882a593Smuzhiyun 	return -EAGAIN;
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun 
hfi1_kern_clear_hw_flow(struct hfi1_ctxtdata * rcd,struct rvt_qp * qp)815*4882a593Smuzhiyun void hfi1_kern_clear_hw_flow(struct hfi1_ctxtdata *rcd, struct rvt_qp *qp)
816*4882a593Smuzhiyun {
817*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = (struct hfi1_qp_priv *)qp->priv;
818*4882a593Smuzhiyun 	struct tid_flow_state *fs = &qpriv->flow_state;
819*4882a593Smuzhiyun 	struct rvt_qp *fqp;
820*4882a593Smuzhiyun 	unsigned long flags;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	if (fs->index >= RXE_NUM_TID_FLOWS)
823*4882a593Smuzhiyun 		return;
824*4882a593Smuzhiyun 	spin_lock_irqsave(&rcd->exp_lock, flags);
825*4882a593Smuzhiyun 	kern_clear_hw_flow(rcd, fs->index);
826*4882a593Smuzhiyun 	clear_bit(fs->index, &rcd->flow_mask);
827*4882a593Smuzhiyun 	fs->index = RXE_NUM_TID_FLOWS;
828*4882a593Smuzhiyun 	fs->psn = 0;
829*4882a593Smuzhiyun 	fs->generation = KERN_GENERATION_RESERVED;
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	/* get head before dropping lock */
832*4882a593Smuzhiyun 	fqp = first_qp(rcd, &rcd->flow_queue);
833*4882a593Smuzhiyun 	spin_unlock_irqrestore(&rcd->exp_lock, flags);
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	if (fqp == qp) {
836*4882a593Smuzhiyun 		__trigger_tid_waiter(fqp);
837*4882a593Smuzhiyun 		rvt_put_qp(fqp);
838*4882a593Smuzhiyun 	} else {
839*4882a593Smuzhiyun 		tid_rdma_schedule_tid_wakeup(fqp);
840*4882a593Smuzhiyun 	}
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun 
hfi1_kern_init_ctxt_generations(struct hfi1_ctxtdata * rcd)843*4882a593Smuzhiyun void hfi1_kern_init_ctxt_generations(struct hfi1_ctxtdata *rcd)
844*4882a593Smuzhiyun {
845*4882a593Smuzhiyun 	int i;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	for (i = 0; i < RXE_NUM_TID_FLOWS; i++) {
848*4882a593Smuzhiyun 		rcd->flows[i].generation = mask_generation(prandom_u32());
849*4882a593Smuzhiyun 		kern_set_hw_flow(rcd, KERN_GENERATION_RESERVED, i);
850*4882a593Smuzhiyun 	}
851*4882a593Smuzhiyun }
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun /* TID allocation functions */
trdma_pset_order(struct tid_rdma_pageset * s)854*4882a593Smuzhiyun static u8 trdma_pset_order(struct tid_rdma_pageset *s)
855*4882a593Smuzhiyun {
856*4882a593Smuzhiyun 	u8 count = s->count;
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	return ilog2(count) + 1;
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun /**
862*4882a593Smuzhiyun  * tid_rdma_find_phys_blocks_4k - get groups base on mr info
863*4882a593Smuzhiyun  * @npages - number of pages
864*4882a593Smuzhiyun  * @pages - pointer to an array of page structs
865*4882a593Smuzhiyun  * @list - page set array to return
866*4882a593Smuzhiyun  *
867*4882a593Smuzhiyun  * This routine returns the number of groups associated with
868*4882a593Smuzhiyun  * the current sge information.  This implementation is based
869*4882a593Smuzhiyun  * on the expected receive find_phys_blocks() adjusted to
870*4882a593Smuzhiyun  * use the MR information vs. the pfn.
871*4882a593Smuzhiyun  *
872*4882a593Smuzhiyun  * Return:
873*4882a593Smuzhiyun  * the number of RcvArray entries
874*4882a593Smuzhiyun  */
tid_rdma_find_phys_blocks_4k(struct tid_rdma_flow * flow,struct page ** pages,u32 npages,struct tid_rdma_pageset * list)875*4882a593Smuzhiyun static u32 tid_rdma_find_phys_blocks_4k(struct tid_rdma_flow *flow,
876*4882a593Smuzhiyun 					struct page **pages,
877*4882a593Smuzhiyun 					u32 npages,
878*4882a593Smuzhiyun 					struct tid_rdma_pageset *list)
879*4882a593Smuzhiyun {
880*4882a593Smuzhiyun 	u32 pagecount, pageidx, setcount = 0, i;
881*4882a593Smuzhiyun 	void *vaddr, *this_vaddr;
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	if (!npages)
884*4882a593Smuzhiyun 		return 0;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	/*
887*4882a593Smuzhiyun 	 * Look for sets of physically contiguous pages in the user buffer.
888*4882a593Smuzhiyun 	 * This will allow us to optimize Expected RcvArray entry usage by
889*4882a593Smuzhiyun 	 * using the bigger supported sizes.
890*4882a593Smuzhiyun 	 */
891*4882a593Smuzhiyun 	vaddr = page_address(pages[0]);
892*4882a593Smuzhiyun 	trace_hfi1_tid_flow_page(flow->req->qp, flow, 0, 0, 0, vaddr);
893*4882a593Smuzhiyun 	for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) {
894*4882a593Smuzhiyun 		this_vaddr = i < npages ? page_address(pages[i]) : NULL;
895*4882a593Smuzhiyun 		trace_hfi1_tid_flow_page(flow->req->qp, flow, i, 0, 0,
896*4882a593Smuzhiyun 					 this_vaddr);
897*4882a593Smuzhiyun 		/*
898*4882a593Smuzhiyun 		 * If the vaddr's are not sequential, pages are not physically
899*4882a593Smuzhiyun 		 * contiguous.
900*4882a593Smuzhiyun 		 */
901*4882a593Smuzhiyun 		if (this_vaddr != (vaddr + PAGE_SIZE)) {
902*4882a593Smuzhiyun 			/*
903*4882a593Smuzhiyun 			 * At this point we have to loop over the set of
904*4882a593Smuzhiyun 			 * physically contiguous pages and break them down it
905*4882a593Smuzhiyun 			 * sizes supported by the HW.
906*4882a593Smuzhiyun 			 * There are two main constraints:
907*4882a593Smuzhiyun 			 *     1. The max buffer size is MAX_EXPECTED_BUFFER.
908*4882a593Smuzhiyun 			 *        If the total set size is bigger than that
909*4882a593Smuzhiyun 			 *        program only a MAX_EXPECTED_BUFFER chunk.
910*4882a593Smuzhiyun 			 *     2. The buffer size has to be a power of two. If
911*4882a593Smuzhiyun 			 *        it is not, round down to the closes power of
912*4882a593Smuzhiyun 			 *        2 and program that size.
913*4882a593Smuzhiyun 			 */
914*4882a593Smuzhiyun 			while (pagecount) {
915*4882a593Smuzhiyun 				int maxpages = pagecount;
916*4882a593Smuzhiyun 				u32 bufsize = pagecount * PAGE_SIZE;
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 				if (bufsize > MAX_EXPECTED_BUFFER)
919*4882a593Smuzhiyun 					maxpages =
920*4882a593Smuzhiyun 						MAX_EXPECTED_BUFFER >>
921*4882a593Smuzhiyun 						PAGE_SHIFT;
922*4882a593Smuzhiyun 				else if (!is_power_of_2(bufsize))
923*4882a593Smuzhiyun 					maxpages =
924*4882a593Smuzhiyun 						rounddown_pow_of_two(bufsize) >>
925*4882a593Smuzhiyun 						PAGE_SHIFT;
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 				list[setcount].idx = pageidx;
928*4882a593Smuzhiyun 				list[setcount].count = maxpages;
929*4882a593Smuzhiyun 				trace_hfi1_tid_pageset(flow->req->qp, setcount,
930*4882a593Smuzhiyun 						       list[setcount].idx,
931*4882a593Smuzhiyun 						       list[setcount].count);
932*4882a593Smuzhiyun 				pagecount -= maxpages;
933*4882a593Smuzhiyun 				pageidx += maxpages;
934*4882a593Smuzhiyun 				setcount++;
935*4882a593Smuzhiyun 			}
936*4882a593Smuzhiyun 			pageidx = i;
937*4882a593Smuzhiyun 			pagecount = 1;
938*4882a593Smuzhiyun 			vaddr = this_vaddr;
939*4882a593Smuzhiyun 		} else {
940*4882a593Smuzhiyun 			vaddr += PAGE_SIZE;
941*4882a593Smuzhiyun 			pagecount++;
942*4882a593Smuzhiyun 		}
943*4882a593Smuzhiyun 	}
944*4882a593Smuzhiyun 	/* insure we always return an even number of sets */
945*4882a593Smuzhiyun 	if (setcount & 1)
946*4882a593Smuzhiyun 		list[setcount++].count = 0;
947*4882a593Smuzhiyun 	return setcount;
948*4882a593Smuzhiyun }
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun /**
951*4882a593Smuzhiyun  * tid_flush_pages - dump out pages into pagesets
952*4882a593Smuzhiyun  * @list - list of pagesets
953*4882a593Smuzhiyun  * @idx - pointer to current page index
954*4882a593Smuzhiyun  * @pages - number of pages to dump
955*4882a593Smuzhiyun  * @sets - current number of pagesset
956*4882a593Smuzhiyun  *
957*4882a593Smuzhiyun  * This routine flushes out accumuated pages.
958*4882a593Smuzhiyun  *
959*4882a593Smuzhiyun  * To insure an even number of sets the
960*4882a593Smuzhiyun  * code may add a filler.
961*4882a593Smuzhiyun  *
962*4882a593Smuzhiyun  * This can happen with when pages is not
963*4882a593Smuzhiyun  * a power of 2 or pages is a power of 2
964*4882a593Smuzhiyun  * less than the maximum pages.
965*4882a593Smuzhiyun  *
966*4882a593Smuzhiyun  * Return:
967*4882a593Smuzhiyun  * The new number of sets
968*4882a593Smuzhiyun  */
969*4882a593Smuzhiyun 
tid_flush_pages(struct tid_rdma_pageset * list,u32 * idx,u32 pages,u32 sets)970*4882a593Smuzhiyun static u32 tid_flush_pages(struct tid_rdma_pageset *list,
971*4882a593Smuzhiyun 			   u32 *idx, u32 pages, u32 sets)
972*4882a593Smuzhiyun {
973*4882a593Smuzhiyun 	while (pages) {
974*4882a593Smuzhiyun 		u32 maxpages = pages;
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 		if (maxpages > MAX_EXPECTED_PAGES)
977*4882a593Smuzhiyun 			maxpages = MAX_EXPECTED_PAGES;
978*4882a593Smuzhiyun 		else if (!is_power_of_2(maxpages))
979*4882a593Smuzhiyun 			maxpages = rounddown_pow_of_two(maxpages);
980*4882a593Smuzhiyun 		list[sets].idx = *idx;
981*4882a593Smuzhiyun 		list[sets++].count = maxpages;
982*4882a593Smuzhiyun 		*idx += maxpages;
983*4882a593Smuzhiyun 		pages -= maxpages;
984*4882a593Smuzhiyun 	}
985*4882a593Smuzhiyun 	/* might need a filler */
986*4882a593Smuzhiyun 	if (sets & 1)
987*4882a593Smuzhiyun 		list[sets++].count = 0;
988*4882a593Smuzhiyun 	return sets;
989*4882a593Smuzhiyun }
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun /**
992*4882a593Smuzhiyun  * tid_rdma_find_phys_blocks_8k - get groups base on mr info
993*4882a593Smuzhiyun  * @pages - pointer to an array of page structs
994*4882a593Smuzhiyun  * @npages - number of pages
995*4882a593Smuzhiyun  * @list - page set array to return
996*4882a593Smuzhiyun  *
997*4882a593Smuzhiyun  * This routine parses an array of pages to compute pagesets
998*4882a593Smuzhiyun  * in an 8k compatible way.
999*4882a593Smuzhiyun  *
1000*4882a593Smuzhiyun  * pages are tested two at a time, i, i + 1 for contiguous
1001*4882a593Smuzhiyun  * pages and i - 1 and i contiguous pages.
1002*4882a593Smuzhiyun  *
1003*4882a593Smuzhiyun  * If any condition is false, any accumlated pages are flushed and
1004*4882a593Smuzhiyun  * v0,v1 are emitted as separate PAGE_SIZE pagesets
1005*4882a593Smuzhiyun  *
1006*4882a593Smuzhiyun  * Otherwise, the current 8k is totaled for a future flush.
1007*4882a593Smuzhiyun  *
1008*4882a593Smuzhiyun  * Return:
1009*4882a593Smuzhiyun  * The number of pagesets
1010*4882a593Smuzhiyun  * list set with the returned number of pagesets
1011*4882a593Smuzhiyun  *
1012*4882a593Smuzhiyun  */
tid_rdma_find_phys_blocks_8k(struct tid_rdma_flow * flow,struct page ** pages,u32 npages,struct tid_rdma_pageset * list)1013*4882a593Smuzhiyun static u32 tid_rdma_find_phys_blocks_8k(struct tid_rdma_flow *flow,
1014*4882a593Smuzhiyun 					struct page **pages,
1015*4882a593Smuzhiyun 					u32 npages,
1016*4882a593Smuzhiyun 					struct tid_rdma_pageset *list)
1017*4882a593Smuzhiyun {
1018*4882a593Smuzhiyun 	u32 idx, sets = 0, i;
1019*4882a593Smuzhiyun 	u32 pagecnt = 0;
1020*4882a593Smuzhiyun 	void *v0, *v1, *vm1;
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 	if (!npages)
1023*4882a593Smuzhiyun 		return 0;
1024*4882a593Smuzhiyun 	for (idx = 0, i = 0, vm1 = NULL; i < npages; i += 2) {
1025*4882a593Smuzhiyun 		/* get a new v0 */
1026*4882a593Smuzhiyun 		v0 = page_address(pages[i]);
1027*4882a593Smuzhiyun 		trace_hfi1_tid_flow_page(flow->req->qp, flow, i, 1, 0, v0);
1028*4882a593Smuzhiyun 		v1 = i + 1 < npages ?
1029*4882a593Smuzhiyun 				page_address(pages[i + 1]) : NULL;
1030*4882a593Smuzhiyun 		trace_hfi1_tid_flow_page(flow->req->qp, flow, i, 1, 1, v1);
1031*4882a593Smuzhiyun 		/* compare i, i + 1 vaddr */
1032*4882a593Smuzhiyun 		if (v1 != (v0 + PAGE_SIZE)) {
1033*4882a593Smuzhiyun 			/* flush out pages */
1034*4882a593Smuzhiyun 			sets = tid_flush_pages(list, &idx, pagecnt, sets);
1035*4882a593Smuzhiyun 			/* output v0,v1 as two pagesets */
1036*4882a593Smuzhiyun 			list[sets].idx = idx++;
1037*4882a593Smuzhiyun 			list[sets++].count = 1;
1038*4882a593Smuzhiyun 			if (v1) {
1039*4882a593Smuzhiyun 				list[sets].count = 1;
1040*4882a593Smuzhiyun 				list[sets++].idx = idx++;
1041*4882a593Smuzhiyun 			} else {
1042*4882a593Smuzhiyun 				list[sets++].count = 0;
1043*4882a593Smuzhiyun 			}
1044*4882a593Smuzhiyun 			vm1 = NULL;
1045*4882a593Smuzhiyun 			pagecnt = 0;
1046*4882a593Smuzhiyun 			continue;
1047*4882a593Smuzhiyun 		}
1048*4882a593Smuzhiyun 		/* i,i+1 consecutive, look at i-1,i */
1049*4882a593Smuzhiyun 		if (vm1 && v0 != (vm1 + PAGE_SIZE)) {
1050*4882a593Smuzhiyun 			/* flush out pages */
1051*4882a593Smuzhiyun 			sets = tid_flush_pages(list, &idx, pagecnt, sets);
1052*4882a593Smuzhiyun 			pagecnt = 0;
1053*4882a593Smuzhiyun 		}
1054*4882a593Smuzhiyun 		/* pages will always be a multiple of 8k */
1055*4882a593Smuzhiyun 		pagecnt += 2;
1056*4882a593Smuzhiyun 		/* save i-1 */
1057*4882a593Smuzhiyun 		vm1 = v1;
1058*4882a593Smuzhiyun 		/* move to next pair */
1059*4882a593Smuzhiyun 	}
1060*4882a593Smuzhiyun 	/* dump residual pages at end */
1061*4882a593Smuzhiyun 	sets = tid_flush_pages(list, &idx, npages - idx, sets);
1062*4882a593Smuzhiyun 	/* by design cannot be odd sets */
1063*4882a593Smuzhiyun 	WARN_ON(sets & 1);
1064*4882a593Smuzhiyun 	return sets;
1065*4882a593Smuzhiyun }
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun /**
1068*4882a593Smuzhiyun  * Find pages for one segment of a sge array represented by @ss. The function
1069*4882a593Smuzhiyun  * does not check the sge, the sge must have been checked for alignment with a
1070*4882a593Smuzhiyun  * prior call to hfi1_kern_trdma_ok. Other sge checking is done as part of
1071*4882a593Smuzhiyun  * rvt_lkey_ok and rvt_rkey_ok. Also, the function only modifies the local sge
1072*4882a593Smuzhiyun  * copy maintained in @ss->sge, the original sge is not modified.
1073*4882a593Smuzhiyun  *
1074*4882a593Smuzhiyun  * Unlike IB RDMA WRITE, we can't decrement ss->num_sge here because we are not
1075*4882a593Smuzhiyun  * releasing the MR reference count at the same time. Otherwise, we'll "leak"
1076*4882a593Smuzhiyun  * references to the MR. This difference requires that we keep track of progress
1077*4882a593Smuzhiyun  * into the sg_list. This is done by the cur_seg cursor in the tid_rdma_request
1078*4882a593Smuzhiyun  * structure.
1079*4882a593Smuzhiyun  */
kern_find_pages(struct tid_rdma_flow * flow,struct page ** pages,struct rvt_sge_state * ss,bool * last)1080*4882a593Smuzhiyun static u32 kern_find_pages(struct tid_rdma_flow *flow,
1081*4882a593Smuzhiyun 			   struct page **pages,
1082*4882a593Smuzhiyun 			   struct rvt_sge_state *ss, bool *last)
1083*4882a593Smuzhiyun {
1084*4882a593Smuzhiyun 	struct tid_rdma_request *req = flow->req;
1085*4882a593Smuzhiyun 	struct rvt_sge *sge = &ss->sge;
1086*4882a593Smuzhiyun 	u32 length = flow->req->seg_len;
1087*4882a593Smuzhiyun 	u32 len = PAGE_SIZE;
1088*4882a593Smuzhiyun 	u32 i = 0;
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	while (length && req->isge < ss->num_sge) {
1091*4882a593Smuzhiyun 		pages[i++] = virt_to_page(sge->vaddr);
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 		sge->vaddr += len;
1094*4882a593Smuzhiyun 		sge->length -= len;
1095*4882a593Smuzhiyun 		sge->sge_length -= len;
1096*4882a593Smuzhiyun 		if (!sge->sge_length) {
1097*4882a593Smuzhiyun 			if (++req->isge < ss->num_sge)
1098*4882a593Smuzhiyun 				*sge = ss->sg_list[req->isge - 1];
1099*4882a593Smuzhiyun 		} else if (sge->length == 0 && sge->mr->lkey) {
1100*4882a593Smuzhiyun 			if (++sge->n >= RVT_SEGSZ) {
1101*4882a593Smuzhiyun 				++sge->m;
1102*4882a593Smuzhiyun 				sge->n = 0;
1103*4882a593Smuzhiyun 			}
1104*4882a593Smuzhiyun 			sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
1105*4882a593Smuzhiyun 			sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
1106*4882a593Smuzhiyun 		}
1107*4882a593Smuzhiyun 		length -= len;
1108*4882a593Smuzhiyun 	}
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	flow->length = flow->req->seg_len - length;
1111*4882a593Smuzhiyun 	*last = req->isge == ss->num_sge ? false : true;
1112*4882a593Smuzhiyun 	return i;
1113*4882a593Smuzhiyun }
1114*4882a593Smuzhiyun 
dma_unmap_flow(struct tid_rdma_flow * flow)1115*4882a593Smuzhiyun static void dma_unmap_flow(struct tid_rdma_flow *flow)
1116*4882a593Smuzhiyun {
1117*4882a593Smuzhiyun 	struct hfi1_devdata *dd;
1118*4882a593Smuzhiyun 	int i;
1119*4882a593Smuzhiyun 	struct tid_rdma_pageset *pset;
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	dd = flow->req->rcd->dd;
1122*4882a593Smuzhiyun 	for (i = 0, pset = &flow->pagesets[0]; i < flow->npagesets;
1123*4882a593Smuzhiyun 			i++, pset++) {
1124*4882a593Smuzhiyun 		if (pset->count && pset->addr) {
1125*4882a593Smuzhiyun 			dma_unmap_page(&dd->pcidev->dev,
1126*4882a593Smuzhiyun 				       pset->addr,
1127*4882a593Smuzhiyun 				       PAGE_SIZE * pset->count,
1128*4882a593Smuzhiyun 				       DMA_FROM_DEVICE);
1129*4882a593Smuzhiyun 			pset->mapped = 0;
1130*4882a593Smuzhiyun 		}
1131*4882a593Smuzhiyun 	}
1132*4882a593Smuzhiyun }
1133*4882a593Smuzhiyun 
dma_map_flow(struct tid_rdma_flow * flow,struct page ** pages)1134*4882a593Smuzhiyun static int dma_map_flow(struct tid_rdma_flow *flow, struct page **pages)
1135*4882a593Smuzhiyun {
1136*4882a593Smuzhiyun 	int i;
1137*4882a593Smuzhiyun 	struct hfi1_devdata *dd = flow->req->rcd->dd;
1138*4882a593Smuzhiyun 	struct tid_rdma_pageset *pset;
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	for (i = 0, pset = &flow->pagesets[0]; i < flow->npagesets;
1141*4882a593Smuzhiyun 			i++, pset++) {
1142*4882a593Smuzhiyun 		if (pset->count) {
1143*4882a593Smuzhiyun 			pset->addr = dma_map_page(&dd->pcidev->dev,
1144*4882a593Smuzhiyun 						  pages[pset->idx],
1145*4882a593Smuzhiyun 						  0,
1146*4882a593Smuzhiyun 						  PAGE_SIZE * pset->count,
1147*4882a593Smuzhiyun 						  DMA_FROM_DEVICE);
1148*4882a593Smuzhiyun 
1149*4882a593Smuzhiyun 			if (dma_mapping_error(&dd->pcidev->dev, pset->addr)) {
1150*4882a593Smuzhiyun 				dma_unmap_flow(flow);
1151*4882a593Smuzhiyun 				return -ENOMEM;
1152*4882a593Smuzhiyun 			}
1153*4882a593Smuzhiyun 			pset->mapped = 1;
1154*4882a593Smuzhiyun 		}
1155*4882a593Smuzhiyun 	}
1156*4882a593Smuzhiyun 	return 0;
1157*4882a593Smuzhiyun }
1158*4882a593Smuzhiyun 
dma_mapped(struct tid_rdma_flow * flow)1159*4882a593Smuzhiyun static inline bool dma_mapped(struct tid_rdma_flow *flow)
1160*4882a593Smuzhiyun {
1161*4882a593Smuzhiyun 	return !!flow->pagesets[0].mapped;
1162*4882a593Smuzhiyun }
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun /*
1165*4882a593Smuzhiyun  * Get pages pointers and identify contiguous physical memory chunks for a
1166*4882a593Smuzhiyun  * segment. All segments are of length flow->req->seg_len.
1167*4882a593Smuzhiyun  */
kern_get_phys_blocks(struct tid_rdma_flow * flow,struct page ** pages,struct rvt_sge_state * ss,bool * last)1168*4882a593Smuzhiyun static int kern_get_phys_blocks(struct tid_rdma_flow *flow,
1169*4882a593Smuzhiyun 				struct page **pages,
1170*4882a593Smuzhiyun 				struct rvt_sge_state *ss, bool *last)
1171*4882a593Smuzhiyun {
1172*4882a593Smuzhiyun 	u8 npages;
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 	/* Reuse previously computed pagesets, if any */
1175*4882a593Smuzhiyun 	if (flow->npagesets) {
1176*4882a593Smuzhiyun 		trace_hfi1_tid_flow_alloc(flow->req->qp, flow->req->setup_head,
1177*4882a593Smuzhiyun 					  flow);
1178*4882a593Smuzhiyun 		if (!dma_mapped(flow))
1179*4882a593Smuzhiyun 			return dma_map_flow(flow, pages);
1180*4882a593Smuzhiyun 		return 0;
1181*4882a593Smuzhiyun 	}
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	npages = kern_find_pages(flow, pages, ss, last);
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun 	if (flow->req->qp->pmtu == enum_to_mtu(OPA_MTU_4096))
1186*4882a593Smuzhiyun 		flow->npagesets =
1187*4882a593Smuzhiyun 			tid_rdma_find_phys_blocks_4k(flow, pages, npages,
1188*4882a593Smuzhiyun 						     flow->pagesets);
1189*4882a593Smuzhiyun 	else
1190*4882a593Smuzhiyun 		flow->npagesets =
1191*4882a593Smuzhiyun 			tid_rdma_find_phys_blocks_8k(flow, pages, npages,
1192*4882a593Smuzhiyun 						     flow->pagesets);
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 	return dma_map_flow(flow, pages);
1195*4882a593Smuzhiyun }
1196*4882a593Smuzhiyun 
kern_add_tid_node(struct tid_rdma_flow * flow,struct hfi1_ctxtdata * rcd,char * s,struct tid_group * grp,u8 cnt)1197*4882a593Smuzhiyun static inline void kern_add_tid_node(struct tid_rdma_flow *flow,
1198*4882a593Smuzhiyun 				     struct hfi1_ctxtdata *rcd, char *s,
1199*4882a593Smuzhiyun 				     struct tid_group *grp, u8 cnt)
1200*4882a593Smuzhiyun {
1201*4882a593Smuzhiyun 	struct kern_tid_node *node = &flow->tnode[flow->tnode_cnt++];
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 	WARN_ON_ONCE(flow->tnode_cnt >=
1204*4882a593Smuzhiyun 		     (TID_RDMA_MAX_SEGMENT_SIZE >> PAGE_SHIFT));
1205*4882a593Smuzhiyun 	if (WARN_ON_ONCE(cnt & 1))
1206*4882a593Smuzhiyun 		dd_dev_err(rcd->dd,
1207*4882a593Smuzhiyun 			   "unexpected odd allocation cnt %u map 0x%x used %u",
1208*4882a593Smuzhiyun 			   cnt, grp->map, grp->used);
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	node->grp = grp;
1211*4882a593Smuzhiyun 	node->map = grp->map;
1212*4882a593Smuzhiyun 	node->cnt = cnt;
1213*4882a593Smuzhiyun 	trace_hfi1_tid_node_add(flow->req->qp, s, flow->tnode_cnt - 1,
1214*4882a593Smuzhiyun 				grp->base, grp->map, grp->used, cnt);
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun /*
1218*4882a593Smuzhiyun  * Try to allocate pageset_count TID's from TID groups for a context
1219*4882a593Smuzhiyun  *
1220*4882a593Smuzhiyun  * This function allocates TID's without moving groups between lists or
1221*4882a593Smuzhiyun  * modifying grp->map. This is done as follows, being cogizant of the lists
1222*4882a593Smuzhiyun  * between which the TID groups will move:
1223*4882a593Smuzhiyun  * 1. First allocate complete groups of 8 TID's since this is more efficient,
1224*4882a593Smuzhiyun  *    these groups will move from group->full without affecting used
1225*4882a593Smuzhiyun  * 2. If more TID's are needed allocate from used (will move from used->full or
1226*4882a593Smuzhiyun  *    stay in used)
1227*4882a593Smuzhiyun  * 3. If we still don't have the required number of TID's go back and look again
1228*4882a593Smuzhiyun  *    at a complete group (will move from group->used)
1229*4882a593Smuzhiyun  */
kern_alloc_tids(struct tid_rdma_flow * flow)1230*4882a593Smuzhiyun static int kern_alloc_tids(struct tid_rdma_flow *flow)
1231*4882a593Smuzhiyun {
1232*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = flow->req->rcd;
1233*4882a593Smuzhiyun 	struct hfi1_devdata *dd = rcd->dd;
1234*4882a593Smuzhiyun 	u32 ngroups, pageidx = 0;
1235*4882a593Smuzhiyun 	struct tid_group *group = NULL, *used;
1236*4882a593Smuzhiyun 	u8 use;
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun 	flow->tnode_cnt = 0;
1239*4882a593Smuzhiyun 	ngroups = flow->npagesets / dd->rcv_entries.group_size;
1240*4882a593Smuzhiyun 	if (!ngroups)
1241*4882a593Smuzhiyun 		goto used_list;
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 	/* First look at complete groups */
1244*4882a593Smuzhiyun 	list_for_each_entry(group,  &rcd->tid_group_list.list, list) {
1245*4882a593Smuzhiyun 		kern_add_tid_node(flow, rcd, "complete groups", group,
1246*4882a593Smuzhiyun 				  group->size);
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun 		pageidx += group->size;
1249*4882a593Smuzhiyun 		if (!--ngroups)
1250*4882a593Smuzhiyun 			break;
1251*4882a593Smuzhiyun 	}
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun 	if (pageidx >= flow->npagesets)
1254*4882a593Smuzhiyun 		goto ok;
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun used_list:
1257*4882a593Smuzhiyun 	/* Now look at partially used groups */
1258*4882a593Smuzhiyun 	list_for_each_entry(used, &rcd->tid_used_list.list, list) {
1259*4882a593Smuzhiyun 		use = min_t(u32, flow->npagesets - pageidx,
1260*4882a593Smuzhiyun 			    used->size - used->used);
1261*4882a593Smuzhiyun 		kern_add_tid_node(flow, rcd, "used groups", used, use);
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun 		pageidx += use;
1264*4882a593Smuzhiyun 		if (pageidx >= flow->npagesets)
1265*4882a593Smuzhiyun 			goto ok;
1266*4882a593Smuzhiyun 	}
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	/*
1269*4882a593Smuzhiyun 	 * Look again at a complete group, continuing from where we left.
1270*4882a593Smuzhiyun 	 * However, if we are at the head, we have reached the end of the
1271*4882a593Smuzhiyun 	 * complete groups list from the first loop above
1272*4882a593Smuzhiyun 	 */
1273*4882a593Smuzhiyun 	if (group && &group->list == &rcd->tid_group_list.list)
1274*4882a593Smuzhiyun 		goto bail_eagain;
1275*4882a593Smuzhiyun 	group = list_prepare_entry(group, &rcd->tid_group_list.list,
1276*4882a593Smuzhiyun 				   list);
1277*4882a593Smuzhiyun 	if (list_is_last(&group->list, &rcd->tid_group_list.list))
1278*4882a593Smuzhiyun 		goto bail_eagain;
1279*4882a593Smuzhiyun 	group = list_next_entry(group, list);
1280*4882a593Smuzhiyun 	use = min_t(u32, flow->npagesets - pageidx, group->size);
1281*4882a593Smuzhiyun 	kern_add_tid_node(flow, rcd, "complete continue", group, use);
1282*4882a593Smuzhiyun 	pageidx += use;
1283*4882a593Smuzhiyun 	if (pageidx >= flow->npagesets)
1284*4882a593Smuzhiyun 		goto ok;
1285*4882a593Smuzhiyun bail_eagain:
1286*4882a593Smuzhiyun 	trace_hfi1_msg_alloc_tids(flow->req->qp, " insufficient tids: needed ",
1287*4882a593Smuzhiyun 				  (u64)flow->npagesets);
1288*4882a593Smuzhiyun 	return -EAGAIN;
1289*4882a593Smuzhiyun ok:
1290*4882a593Smuzhiyun 	return 0;
1291*4882a593Smuzhiyun }
1292*4882a593Smuzhiyun 
kern_program_rcv_group(struct tid_rdma_flow * flow,int grp_num,u32 * pset_idx)1293*4882a593Smuzhiyun static void kern_program_rcv_group(struct tid_rdma_flow *flow, int grp_num,
1294*4882a593Smuzhiyun 				   u32 *pset_idx)
1295*4882a593Smuzhiyun {
1296*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = flow->req->rcd;
1297*4882a593Smuzhiyun 	struct hfi1_devdata *dd = rcd->dd;
1298*4882a593Smuzhiyun 	struct kern_tid_node *node = &flow->tnode[grp_num];
1299*4882a593Smuzhiyun 	struct tid_group *grp = node->grp;
1300*4882a593Smuzhiyun 	struct tid_rdma_pageset *pset;
1301*4882a593Smuzhiyun 	u32 pmtu_pg = flow->req->qp->pmtu >> PAGE_SHIFT;
1302*4882a593Smuzhiyun 	u32 rcventry, npages = 0, pair = 0, tidctrl;
1303*4882a593Smuzhiyun 	u8 i, cnt = 0;
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun 	for (i = 0; i < grp->size; i++) {
1306*4882a593Smuzhiyun 		rcventry = grp->base + i;
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 		if (node->map & BIT(i) || cnt >= node->cnt) {
1309*4882a593Smuzhiyun 			rcv_array_wc_fill(dd, rcventry);
1310*4882a593Smuzhiyun 			continue;
1311*4882a593Smuzhiyun 		}
1312*4882a593Smuzhiyun 		pset = &flow->pagesets[(*pset_idx)++];
1313*4882a593Smuzhiyun 		if (pset->count) {
1314*4882a593Smuzhiyun 			hfi1_put_tid(dd, rcventry, PT_EXPECTED,
1315*4882a593Smuzhiyun 				     pset->addr, trdma_pset_order(pset));
1316*4882a593Smuzhiyun 		} else {
1317*4882a593Smuzhiyun 			hfi1_put_tid(dd, rcventry, PT_INVALID, 0, 0);
1318*4882a593Smuzhiyun 		}
1319*4882a593Smuzhiyun 		npages += pset->count;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 		rcventry -= rcd->expected_base;
1322*4882a593Smuzhiyun 		tidctrl = pair ? 0x3 : rcventry & 0x1 ? 0x2 : 0x1;
1323*4882a593Smuzhiyun 		/*
1324*4882a593Smuzhiyun 		 * A single TID entry will be used to use a rcvarr pair (with
1325*4882a593Smuzhiyun 		 * tidctrl 0x3), if ALL these are true (a) the bit pos is even
1326*4882a593Smuzhiyun 		 * (b) the group map shows current and the next bits as free
1327*4882a593Smuzhiyun 		 * indicating two consecutive rcvarry entries are available (c)
1328*4882a593Smuzhiyun 		 * we actually need 2 more entries
1329*4882a593Smuzhiyun 		 */
1330*4882a593Smuzhiyun 		pair = !(i & 0x1) && !((node->map >> i) & 0x3) &&
1331*4882a593Smuzhiyun 			node->cnt >= cnt + 2;
1332*4882a593Smuzhiyun 		if (!pair) {
1333*4882a593Smuzhiyun 			if (!pset->count)
1334*4882a593Smuzhiyun 				tidctrl = 0x1;
1335*4882a593Smuzhiyun 			flow->tid_entry[flow->tidcnt++] =
1336*4882a593Smuzhiyun 				EXP_TID_SET(IDX, rcventry >> 1) |
1337*4882a593Smuzhiyun 				EXP_TID_SET(CTRL, tidctrl) |
1338*4882a593Smuzhiyun 				EXP_TID_SET(LEN, npages);
1339*4882a593Smuzhiyun 			trace_hfi1_tid_entry_alloc(/* entry */
1340*4882a593Smuzhiyun 			   flow->req->qp, flow->tidcnt - 1,
1341*4882a593Smuzhiyun 			   flow->tid_entry[flow->tidcnt - 1]);
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 			/* Efficient DIV_ROUND_UP(npages, pmtu_pg) */
1344*4882a593Smuzhiyun 			flow->npkts += (npages + pmtu_pg - 1) >> ilog2(pmtu_pg);
1345*4882a593Smuzhiyun 			npages = 0;
1346*4882a593Smuzhiyun 		}
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun 		if (grp->used == grp->size - 1)
1349*4882a593Smuzhiyun 			tid_group_move(grp, &rcd->tid_used_list,
1350*4882a593Smuzhiyun 				       &rcd->tid_full_list);
1351*4882a593Smuzhiyun 		else if (!grp->used)
1352*4882a593Smuzhiyun 			tid_group_move(grp, &rcd->tid_group_list,
1353*4882a593Smuzhiyun 				       &rcd->tid_used_list);
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 		grp->used++;
1356*4882a593Smuzhiyun 		grp->map |= BIT(i);
1357*4882a593Smuzhiyun 		cnt++;
1358*4882a593Smuzhiyun 	}
1359*4882a593Smuzhiyun }
1360*4882a593Smuzhiyun 
kern_unprogram_rcv_group(struct tid_rdma_flow * flow,int grp_num)1361*4882a593Smuzhiyun static void kern_unprogram_rcv_group(struct tid_rdma_flow *flow, int grp_num)
1362*4882a593Smuzhiyun {
1363*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = flow->req->rcd;
1364*4882a593Smuzhiyun 	struct hfi1_devdata *dd = rcd->dd;
1365*4882a593Smuzhiyun 	struct kern_tid_node *node = &flow->tnode[grp_num];
1366*4882a593Smuzhiyun 	struct tid_group *grp = node->grp;
1367*4882a593Smuzhiyun 	u32 rcventry;
1368*4882a593Smuzhiyun 	u8 i, cnt = 0;
1369*4882a593Smuzhiyun 
1370*4882a593Smuzhiyun 	for (i = 0; i < grp->size; i++) {
1371*4882a593Smuzhiyun 		rcventry = grp->base + i;
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun 		if (node->map & BIT(i) || cnt >= node->cnt) {
1374*4882a593Smuzhiyun 			rcv_array_wc_fill(dd, rcventry);
1375*4882a593Smuzhiyun 			continue;
1376*4882a593Smuzhiyun 		}
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun 		hfi1_put_tid(dd, rcventry, PT_INVALID, 0, 0);
1379*4882a593Smuzhiyun 
1380*4882a593Smuzhiyun 		grp->used--;
1381*4882a593Smuzhiyun 		grp->map &= ~BIT(i);
1382*4882a593Smuzhiyun 		cnt++;
1383*4882a593Smuzhiyun 
1384*4882a593Smuzhiyun 		if (grp->used == grp->size - 1)
1385*4882a593Smuzhiyun 			tid_group_move(grp, &rcd->tid_full_list,
1386*4882a593Smuzhiyun 				       &rcd->tid_used_list);
1387*4882a593Smuzhiyun 		else if (!grp->used)
1388*4882a593Smuzhiyun 			tid_group_move(grp, &rcd->tid_used_list,
1389*4882a593Smuzhiyun 				       &rcd->tid_group_list);
1390*4882a593Smuzhiyun 	}
1391*4882a593Smuzhiyun 	if (WARN_ON_ONCE(cnt & 1)) {
1392*4882a593Smuzhiyun 		struct hfi1_ctxtdata *rcd = flow->req->rcd;
1393*4882a593Smuzhiyun 		struct hfi1_devdata *dd = rcd->dd;
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun 		dd_dev_err(dd, "unexpected odd free cnt %u map 0x%x used %u",
1396*4882a593Smuzhiyun 			   cnt, grp->map, grp->used);
1397*4882a593Smuzhiyun 	}
1398*4882a593Smuzhiyun }
1399*4882a593Smuzhiyun 
kern_program_rcvarray(struct tid_rdma_flow * flow)1400*4882a593Smuzhiyun static void kern_program_rcvarray(struct tid_rdma_flow *flow)
1401*4882a593Smuzhiyun {
1402*4882a593Smuzhiyun 	u32 pset_idx = 0;
1403*4882a593Smuzhiyun 	int i;
1404*4882a593Smuzhiyun 
1405*4882a593Smuzhiyun 	flow->npkts = 0;
1406*4882a593Smuzhiyun 	flow->tidcnt = 0;
1407*4882a593Smuzhiyun 	for (i = 0; i < flow->tnode_cnt; i++)
1408*4882a593Smuzhiyun 		kern_program_rcv_group(flow, i, &pset_idx);
1409*4882a593Smuzhiyun 	trace_hfi1_tid_flow_alloc(flow->req->qp, flow->req->setup_head, flow);
1410*4882a593Smuzhiyun }
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun /**
1413*4882a593Smuzhiyun  * hfi1_kern_exp_rcv_setup() - setup TID's and flow for one segment of a
1414*4882a593Smuzhiyun  * TID RDMA request
1415*4882a593Smuzhiyun  *
1416*4882a593Smuzhiyun  * @req: TID RDMA request for which the segment/flow is being set up
1417*4882a593Smuzhiyun  * @ss: sge state, maintains state across successive segments of a sge
1418*4882a593Smuzhiyun  * @last: set to true after the last sge segment has been processed
1419*4882a593Smuzhiyun  *
1420*4882a593Smuzhiyun  * This function
1421*4882a593Smuzhiyun  * (1) finds a free flow entry in the flow circular buffer
1422*4882a593Smuzhiyun  * (2) finds pages and continuous physical chunks constituing one segment
1423*4882a593Smuzhiyun  *     of an sge
1424*4882a593Smuzhiyun  * (3) allocates TID group entries for those chunks
1425*4882a593Smuzhiyun  * (4) programs rcvarray entries in the hardware corresponding to those
1426*4882a593Smuzhiyun  *     TID's
1427*4882a593Smuzhiyun  * (5) computes a tidarray with formatted TID entries which can be sent
1428*4882a593Smuzhiyun  *     to the sender
1429*4882a593Smuzhiyun  * (6) Reserves and programs HW flows.
1430*4882a593Smuzhiyun  * (7) It also manages queing the QP when TID/flow resources are not
1431*4882a593Smuzhiyun  *     available.
1432*4882a593Smuzhiyun  *
1433*4882a593Smuzhiyun  * @req points to struct tid_rdma_request of which the segments are a part. The
1434*4882a593Smuzhiyun  * function uses qp, rcd and seg_len members of @req. In the absence of errors,
1435*4882a593Smuzhiyun  * req->flow_idx is the index of the flow which has been prepared in this
1436*4882a593Smuzhiyun  * invocation of function call. With flow = &req->flows[req->flow_idx],
1437*4882a593Smuzhiyun  * flow->tid_entry contains the TID array which the sender can use for TID RDMA
1438*4882a593Smuzhiyun  * sends and flow->npkts contains number of packets required to send the
1439*4882a593Smuzhiyun  * segment.
1440*4882a593Smuzhiyun  *
1441*4882a593Smuzhiyun  * hfi1_check_sge_align should be called prior to calling this function and if
1442*4882a593Smuzhiyun  * it signals error TID RDMA cannot be used for this sge and this function
1443*4882a593Smuzhiyun  * should not be called.
1444*4882a593Smuzhiyun  *
1445*4882a593Smuzhiyun  * For the queuing, caller must hold the flow->req->qp s_lock from the send
1446*4882a593Smuzhiyun  * engine and the function will procure the exp_lock.
1447*4882a593Smuzhiyun  *
1448*4882a593Smuzhiyun  * Return:
1449*4882a593Smuzhiyun  * The function returns -EAGAIN if sufficient number of TID/flow resources to
1450*4882a593Smuzhiyun  * map the segment could not be allocated. In this case the function should be
1451*4882a593Smuzhiyun  * called again with previous arguments to retry the TID allocation. There are
1452*4882a593Smuzhiyun  * no other error returns. The function returns 0 on success.
1453*4882a593Smuzhiyun  */
hfi1_kern_exp_rcv_setup(struct tid_rdma_request * req,struct rvt_sge_state * ss,bool * last)1454*4882a593Smuzhiyun int hfi1_kern_exp_rcv_setup(struct tid_rdma_request *req,
1455*4882a593Smuzhiyun 			    struct rvt_sge_state *ss, bool *last)
1456*4882a593Smuzhiyun 	__must_hold(&req->qp->s_lock)
1457*4882a593Smuzhiyun {
1458*4882a593Smuzhiyun 	struct tid_rdma_flow *flow = &req->flows[req->setup_head];
1459*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = req->rcd;
1460*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = req->qp->priv;
1461*4882a593Smuzhiyun 	unsigned long flags;
1462*4882a593Smuzhiyun 	struct rvt_qp *fqp;
1463*4882a593Smuzhiyun 	u16 clear_tail = req->clear_tail;
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 	lockdep_assert_held(&req->qp->s_lock);
1466*4882a593Smuzhiyun 	/*
1467*4882a593Smuzhiyun 	 * We return error if either (a) we don't have space in the flow
1468*4882a593Smuzhiyun 	 * circular buffer, or (b) we already have max entries in the buffer.
1469*4882a593Smuzhiyun 	 * Max entries depend on the type of request we are processing and the
1470*4882a593Smuzhiyun 	 * negotiated TID RDMA parameters.
1471*4882a593Smuzhiyun 	 */
1472*4882a593Smuzhiyun 	if (!CIRC_SPACE(req->setup_head, clear_tail, MAX_FLOWS) ||
1473*4882a593Smuzhiyun 	    CIRC_CNT(req->setup_head, clear_tail, MAX_FLOWS) >=
1474*4882a593Smuzhiyun 	    req->n_flows)
1475*4882a593Smuzhiyun 		return -EINVAL;
1476*4882a593Smuzhiyun 
1477*4882a593Smuzhiyun 	/*
1478*4882a593Smuzhiyun 	 * Get pages, identify contiguous physical memory chunks for the segment
1479*4882a593Smuzhiyun 	 * If we can not determine a DMA address mapping we will treat it just
1480*4882a593Smuzhiyun 	 * like if we ran out of space above.
1481*4882a593Smuzhiyun 	 */
1482*4882a593Smuzhiyun 	if (kern_get_phys_blocks(flow, qpriv->pages, ss, last)) {
1483*4882a593Smuzhiyun 		hfi1_wait_kmem(flow->req->qp);
1484*4882a593Smuzhiyun 		return -ENOMEM;
1485*4882a593Smuzhiyun 	}
1486*4882a593Smuzhiyun 
1487*4882a593Smuzhiyun 	spin_lock_irqsave(&rcd->exp_lock, flags);
1488*4882a593Smuzhiyun 	if (kernel_tid_waiters(rcd, &rcd->rarr_queue, flow->req->qp))
1489*4882a593Smuzhiyun 		goto queue;
1490*4882a593Smuzhiyun 
1491*4882a593Smuzhiyun 	/*
1492*4882a593Smuzhiyun 	 * At this point we know the number of pagesets and hence the number of
1493*4882a593Smuzhiyun 	 * TID's to map the segment. Allocate the TID's from the TID groups. If
1494*4882a593Smuzhiyun 	 * we cannot allocate the required number we exit and try again later
1495*4882a593Smuzhiyun 	 */
1496*4882a593Smuzhiyun 	if (kern_alloc_tids(flow))
1497*4882a593Smuzhiyun 		goto queue;
1498*4882a593Smuzhiyun 	/*
1499*4882a593Smuzhiyun 	 * Finally program the TID entries with the pagesets, compute the
1500*4882a593Smuzhiyun 	 * tidarray and enable the HW flow
1501*4882a593Smuzhiyun 	 */
1502*4882a593Smuzhiyun 	kern_program_rcvarray(flow);
1503*4882a593Smuzhiyun 
1504*4882a593Smuzhiyun 	/*
1505*4882a593Smuzhiyun 	 * Setup the flow state with relevant information.
1506*4882a593Smuzhiyun 	 * This information is used for tracking the sequence of data packets
1507*4882a593Smuzhiyun 	 * for the segment.
1508*4882a593Smuzhiyun 	 * The flow is setup here as this is the most accurate time and place
1509*4882a593Smuzhiyun 	 * to do so. Doing at a later time runs the risk of the flow data in
1510*4882a593Smuzhiyun 	 * qpriv getting out of sync.
1511*4882a593Smuzhiyun 	 */
1512*4882a593Smuzhiyun 	memset(&flow->flow_state, 0x0, sizeof(flow->flow_state));
1513*4882a593Smuzhiyun 	flow->idx = qpriv->flow_state.index;
1514*4882a593Smuzhiyun 	flow->flow_state.generation = qpriv->flow_state.generation;
1515*4882a593Smuzhiyun 	flow->flow_state.spsn = qpriv->flow_state.psn;
1516*4882a593Smuzhiyun 	flow->flow_state.lpsn = flow->flow_state.spsn + flow->npkts - 1;
1517*4882a593Smuzhiyun 	flow->flow_state.r_next_psn =
1518*4882a593Smuzhiyun 		full_flow_psn(flow, flow->flow_state.spsn);
1519*4882a593Smuzhiyun 	qpriv->flow_state.psn += flow->npkts;
1520*4882a593Smuzhiyun 
1521*4882a593Smuzhiyun 	dequeue_tid_waiter(rcd, &rcd->rarr_queue, flow->req->qp);
1522*4882a593Smuzhiyun 	/* get head before dropping lock */
1523*4882a593Smuzhiyun 	fqp = first_qp(rcd, &rcd->rarr_queue);
1524*4882a593Smuzhiyun 	spin_unlock_irqrestore(&rcd->exp_lock, flags);
1525*4882a593Smuzhiyun 	tid_rdma_schedule_tid_wakeup(fqp);
1526*4882a593Smuzhiyun 
1527*4882a593Smuzhiyun 	req->setup_head = (req->setup_head + 1) & (MAX_FLOWS - 1);
1528*4882a593Smuzhiyun 	return 0;
1529*4882a593Smuzhiyun queue:
1530*4882a593Smuzhiyun 	queue_qp_for_tid_wait(rcd, &rcd->rarr_queue, flow->req->qp);
1531*4882a593Smuzhiyun 	spin_unlock_irqrestore(&rcd->exp_lock, flags);
1532*4882a593Smuzhiyun 	return -EAGAIN;
1533*4882a593Smuzhiyun }
1534*4882a593Smuzhiyun 
hfi1_tid_rdma_reset_flow(struct tid_rdma_flow * flow)1535*4882a593Smuzhiyun static void hfi1_tid_rdma_reset_flow(struct tid_rdma_flow *flow)
1536*4882a593Smuzhiyun {
1537*4882a593Smuzhiyun 	flow->npagesets = 0;
1538*4882a593Smuzhiyun }
1539*4882a593Smuzhiyun 
1540*4882a593Smuzhiyun /*
1541*4882a593Smuzhiyun  * This function is called after one segment has been successfully sent to
1542*4882a593Smuzhiyun  * release the flow and TID HW/SW resources for that segment. The segments for a
1543*4882a593Smuzhiyun  * TID RDMA request are setup and cleared in FIFO order which is managed using a
1544*4882a593Smuzhiyun  * circular buffer.
1545*4882a593Smuzhiyun  */
hfi1_kern_exp_rcv_clear(struct tid_rdma_request * req)1546*4882a593Smuzhiyun int hfi1_kern_exp_rcv_clear(struct tid_rdma_request *req)
1547*4882a593Smuzhiyun 	__must_hold(&req->qp->s_lock)
1548*4882a593Smuzhiyun {
1549*4882a593Smuzhiyun 	struct tid_rdma_flow *flow = &req->flows[req->clear_tail];
1550*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = req->rcd;
1551*4882a593Smuzhiyun 	unsigned long flags;
1552*4882a593Smuzhiyun 	int i;
1553*4882a593Smuzhiyun 	struct rvt_qp *fqp;
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun 	lockdep_assert_held(&req->qp->s_lock);
1556*4882a593Smuzhiyun 	/* Exit if we have nothing in the flow circular buffer */
1557*4882a593Smuzhiyun 	if (!CIRC_CNT(req->setup_head, req->clear_tail, MAX_FLOWS))
1558*4882a593Smuzhiyun 		return -EINVAL;
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun 	spin_lock_irqsave(&rcd->exp_lock, flags);
1561*4882a593Smuzhiyun 
1562*4882a593Smuzhiyun 	for (i = 0; i < flow->tnode_cnt; i++)
1563*4882a593Smuzhiyun 		kern_unprogram_rcv_group(flow, i);
1564*4882a593Smuzhiyun 	/* To prevent double unprogramming */
1565*4882a593Smuzhiyun 	flow->tnode_cnt = 0;
1566*4882a593Smuzhiyun 	/* get head before dropping lock */
1567*4882a593Smuzhiyun 	fqp = first_qp(rcd, &rcd->rarr_queue);
1568*4882a593Smuzhiyun 	spin_unlock_irqrestore(&rcd->exp_lock, flags);
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun 	dma_unmap_flow(flow);
1571*4882a593Smuzhiyun 
1572*4882a593Smuzhiyun 	hfi1_tid_rdma_reset_flow(flow);
1573*4882a593Smuzhiyun 	req->clear_tail = (req->clear_tail + 1) & (MAX_FLOWS - 1);
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 	if (fqp == req->qp) {
1576*4882a593Smuzhiyun 		__trigger_tid_waiter(fqp);
1577*4882a593Smuzhiyun 		rvt_put_qp(fqp);
1578*4882a593Smuzhiyun 	} else {
1579*4882a593Smuzhiyun 		tid_rdma_schedule_tid_wakeup(fqp);
1580*4882a593Smuzhiyun 	}
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun 	return 0;
1583*4882a593Smuzhiyun }
1584*4882a593Smuzhiyun 
1585*4882a593Smuzhiyun /*
1586*4882a593Smuzhiyun  * This function is called to release all the tid entries for
1587*4882a593Smuzhiyun  * a request.
1588*4882a593Smuzhiyun  */
hfi1_kern_exp_rcv_clear_all(struct tid_rdma_request * req)1589*4882a593Smuzhiyun void hfi1_kern_exp_rcv_clear_all(struct tid_rdma_request *req)
1590*4882a593Smuzhiyun 	__must_hold(&req->qp->s_lock)
1591*4882a593Smuzhiyun {
1592*4882a593Smuzhiyun 	/* Use memory barrier for proper ordering */
1593*4882a593Smuzhiyun 	while (CIRC_CNT(req->setup_head, req->clear_tail, MAX_FLOWS)) {
1594*4882a593Smuzhiyun 		if (hfi1_kern_exp_rcv_clear(req))
1595*4882a593Smuzhiyun 			break;
1596*4882a593Smuzhiyun 	}
1597*4882a593Smuzhiyun }
1598*4882a593Smuzhiyun 
1599*4882a593Smuzhiyun /**
1600*4882a593Smuzhiyun  * hfi1_kern_exp_rcv_free_flows - free priviously allocated flow information
1601*4882a593Smuzhiyun  * @req - the tid rdma request to be cleaned
1602*4882a593Smuzhiyun  */
hfi1_kern_exp_rcv_free_flows(struct tid_rdma_request * req)1603*4882a593Smuzhiyun static void hfi1_kern_exp_rcv_free_flows(struct tid_rdma_request *req)
1604*4882a593Smuzhiyun {
1605*4882a593Smuzhiyun 	kfree(req->flows);
1606*4882a593Smuzhiyun 	req->flows = NULL;
1607*4882a593Smuzhiyun }
1608*4882a593Smuzhiyun 
1609*4882a593Smuzhiyun /**
1610*4882a593Smuzhiyun  * __trdma_clean_swqe - clean up for large sized QPs
1611*4882a593Smuzhiyun  * @qp: the queue patch
1612*4882a593Smuzhiyun  * @wqe: the send wqe
1613*4882a593Smuzhiyun  */
__trdma_clean_swqe(struct rvt_qp * qp,struct rvt_swqe * wqe)1614*4882a593Smuzhiyun void __trdma_clean_swqe(struct rvt_qp *qp, struct rvt_swqe *wqe)
1615*4882a593Smuzhiyun {
1616*4882a593Smuzhiyun 	struct hfi1_swqe_priv *p = wqe->priv;
1617*4882a593Smuzhiyun 
1618*4882a593Smuzhiyun 	hfi1_kern_exp_rcv_free_flows(&p->tid_req);
1619*4882a593Smuzhiyun }
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun /*
1622*4882a593Smuzhiyun  * This can be called at QP create time or in the data path.
1623*4882a593Smuzhiyun  */
hfi1_kern_exp_rcv_alloc_flows(struct tid_rdma_request * req,gfp_t gfp)1624*4882a593Smuzhiyun static int hfi1_kern_exp_rcv_alloc_flows(struct tid_rdma_request *req,
1625*4882a593Smuzhiyun 					 gfp_t gfp)
1626*4882a593Smuzhiyun {
1627*4882a593Smuzhiyun 	struct tid_rdma_flow *flows;
1628*4882a593Smuzhiyun 	int i;
1629*4882a593Smuzhiyun 
1630*4882a593Smuzhiyun 	if (likely(req->flows))
1631*4882a593Smuzhiyun 		return 0;
1632*4882a593Smuzhiyun 	flows = kmalloc_node(MAX_FLOWS * sizeof(*flows), gfp,
1633*4882a593Smuzhiyun 			     req->rcd->numa_id);
1634*4882a593Smuzhiyun 	if (!flows)
1635*4882a593Smuzhiyun 		return -ENOMEM;
1636*4882a593Smuzhiyun 	/* mini init */
1637*4882a593Smuzhiyun 	for (i = 0; i < MAX_FLOWS; i++) {
1638*4882a593Smuzhiyun 		flows[i].req = req;
1639*4882a593Smuzhiyun 		flows[i].npagesets = 0;
1640*4882a593Smuzhiyun 		flows[i].pagesets[0].mapped =  0;
1641*4882a593Smuzhiyun 		flows[i].resync_npkts = 0;
1642*4882a593Smuzhiyun 	}
1643*4882a593Smuzhiyun 	req->flows = flows;
1644*4882a593Smuzhiyun 	return 0;
1645*4882a593Smuzhiyun }
1646*4882a593Smuzhiyun 
hfi1_init_trdma_req(struct rvt_qp * qp,struct tid_rdma_request * req)1647*4882a593Smuzhiyun static void hfi1_init_trdma_req(struct rvt_qp *qp,
1648*4882a593Smuzhiyun 				struct tid_rdma_request *req)
1649*4882a593Smuzhiyun {
1650*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun 	/*
1653*4882a593Smuzhiyun 	 * Initialize various TID RDMA request variables.
1654*4882a593Smuzhiyun 	 * These variables are "static", which is why they
1655*4882a593Smuzhiyun 	 * can be pre-initialized here before the WRs has
1656*4882a593Smuzhiyun 	 * even been submitted.
1657*4882a593Smuzhiyun 	 * However, non-NULL values for these variables do not
1658*4882a593Smuzhiyun 	 * imply that this WQE has been enabled for TID RDMA.
1659*4882a593Smuzhiyun 	 * Drivers should check the WQE's opcode to determine
1660*4882a593Smuzhiyun 	 * if a request is a TID RDMA one or not.
1661*4882a593Smuzhiyun 	 */
1662*4882a593Smuzhiyun 	req->qp = qp;
1663*4882a593Smuzhiyun 	req->rcd = qpriv->rcd;
1664*4882a593Smuzhiyun }
1665*4882a593Smuzhiyun 
hfi1_access_sw_tid_wait(const struct cntr_entry * entry,void * context,int vl,int mode,u64 data)1666*4882a593Smuzhiyun u64 hfi1_access_sw_tid_wait(const struct cntr_entry *entry,
1667*4882a593Smuzhiyun 			    void *context, int vl, int mode, u64 data)
1668*4882a593Smuzhiyun {
1669*4882a593Smuzhiyun 	struct hfi1_devdata *dd = context;
1670*4882a593Smuzhiyun 
1671*4882a593Smuzhiyun 	return dd->verbs_dev.n_tidwait;
1672*4882a593Smuzhiyun }
1673*4882a593Smuzhiyun 
find_flow_ib(struct tid_rdma_request * req,u32 psn,u16 * fidx)1674*4882a593Smuzhiyun static struct tid_rdma_flow *find_flow_ib(struct tid_rdma_request *req,
1675*4882a593Smuzhiyun 					  u32 psn, u16 *fidx)
1676*4882a593Smuzhiyun {
1677*4882a593Smuzhiyun 	u16 head, tail;
1678*4882a593Smuzhiyun 	struct tid_rdma_flow *flow;
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 	head = req->setup_head;
1681*4882a593Smuzhiyun 	tail = req->clear_tail;
1682*4882a593Smuzhiyun 	for ( ; CIRC_CNT(head, tail, MAX_FLOWS);
1683*4882a593Smuzhiyun 	     tail = CIRC_NEXT(tail, MAX_FLOWS)) {
1684*4882a593Smuzhiyun 		flow = &req->flows[tail];
1685*4882a593Smuzhiyun 		if (cmp_psn(psn, flow->flow_state.ib_spsn) >= 0 &&
1686*4882a593Smuzhiyun 		    cmp_psn(psn, flow->flow_state.ib_lpsn) <= 0) {
1687*4882a593Smuzhiyun 			if (fidx)
1688*4882a593Smuzhiyun 				*fidx = tail;
1689*4882a593Smuzhiyun 			return flow;
1690*4882a593Smuzhiyun 		}
1691*4882a593Smuzhiyun 	}
1692*4882a593Smuzhiyun 	return NULL;
1693*4882a593Smuzhiyun }
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun /* TID RDMA READ functions */
hfi1_build_tid_rdma_read_packet(struct rvt_swqe * wqe,struct ib_other_headers * ohdr,u32 * bth1,u32 * bth2,u32 * len)1696*4882a593Smuzhiyun u32 hfi1_build_tid_rdma_read_packet(struct rvt_swqe *wqe,
1697*4882a593Smuzhiyun 				    struct ib_other_headers *ohdr, u32 *bth1,
1698*4882a593Smuzhiyun 				    u32 *bth2, u32 *len)
1699*4882a593Smuzhiyun {
1700*4882a593Smuzhiyun 	struct tid_rdma_request *req = wqe_to_tid_req(wqe);
1701*4882a593Smuzhiyun 	struct tid_rdma_flow *flow = &req->flows[req->flow_idx];
1702*4882a593Smuzhiyun 	struct rvt_qp *qp = req->qp;
1703*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
1704*4882a593Smuzhiyun 	struct hfi1_swqe_priv *wpriv = wqe->priv;
1705*4882a593Smuzhiyun 	struct tid_rdma_read_req *rreq = &ohdr->u.tid_rdma.r_req;
1706*4882a593Smuzhiyun 	struct tid_rdma_params *remote;
1707*4882a593Smuzhiyun 	u32 req_len = 0;
1708*4882a593Smuzhiyun 	void *req_addr = NULL;
1709*4882a593Smuzhiyun 
1710*4882a593Smuzhiyun 	/* This is the IB psn used to send the request */
1711*4882a593Smuzhiyun 	*bth2 = mask_psn(flow->flow_state.ib_spsn + flow->pkt);
1712*4882a593Smuzhiyun 	trace_hfi1_tid_flow_build_read_pkt(qp, req->flow_idx, flow);
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun 	/* TID Entries for TID RDMA READ payload */
1715*4882a593Smuzhiyun 	req_addr = &flow->tid_entry[flow->tid_idx];
1716*4882a593Smuzhiyun 	req_len = sizeof(*flow->tid_entry) *
1717*4882a593Smuzhiyun 			(flow->tidcnt - flow->tid_idx);
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun 	memset(&ohdr->u.tid_rdma.r_req, 0, sizeof(ohdr->u.tid_rdma.r_req));
1720*4882a593Smuzhiyun 	wpriv->ss.sge.vaddr = req_addr;
1721*4882a593Smuzhiyun 	wpriv->ss.sge.sge_length = req_len;
1722*4882a593Smuzhiyun 	wpriv->ss.sge.length = wpriv->ss.sge.sge_length;
1723*4882a593Smuzhiyun 	/*
1724*4882a593Smuzhiyun 	 * We can safely zero these out. Since the first SGE covers the
1725*4882a593Smuzhiyun 	 * entire packet, nothing else should even look at the MR.
1726*4882a593Smuzhiyun 	 */
1727*4882a593Smuzhiyun 	wpriv->ss.sge.mr = NULL;
1728*4882a593Smuzhiyun 	wpriv->ss.sge.m = 0;
1729*4882a593Smuzhiyun 	wpriv->ss.sge.n = 0;
1730*4882a593Smuzhiyun 
1731*4882a593Smuzhiyun 	wpriv->ss.sg_list = NULL;
1732*4882a593Smuzhiyun 	wpriv->ss.total_len = wpriv->ss.sge.sge_length;
1733*4882a593Smuzhiyun 	wpriv->ss.num_sge = 1;
1734*4882a593Smuzhiyun 
1735*4882a593Smuzhiyun 	/* Construct the TID RDMA READ REQ packet header */
1736*4882a593Smuzhiyun 	rcu_read_lock();
1737*4882a593Smuzhiyun 	remote = rcu_dereference(qpriv->tid_rdma.remote);
1738*4882a593Smuzhiyun 
1739*4882a593Smuzhiyun 	KDETH_RESET(rreq->kdeth0, KVER, 0x1);
1740*4882a593Smuzhiyun 	KDETH_RESET(rreq->kdeth1, JKEY, remote->jkey);
1741*4882a593Smuzhiyun 	rreq->reth.vaddr = cpu_to_be64(wqe->rdma_wr.remote_addr +
1742*4882a593Smuzhiyun 			   req->cur_seg * req->seg_len + flow->sent);
1743*4882a593Smuzhiyun 	rreq->reth.rkey = cpu_to_be32(wqe->rdma_wr.rkey);
1744*4882a593Smuzhiyun 	rreq->reth.length = cpu_to_be32(*len);
1745*4882a593Smuzhiyun 	rreq->tid_flow_psn =
1746*4882a593Smuzhiyun 		cpu_to_be32((flow->flow_state.generation <<
1747*4882a593Smuzhiyun 			     HFI1_KDETH_BTH_SEQ_SHIFT) |
1748*4882a593Smuzhiyun 			    ((flow->flow_state.spsn + flow->pkt) &
1749*4882a593Smuzhiyun 			     HFI1_KDETH_BTH_SEQ_MASK));
1750*4882a593Smuzhiyun 	rreq->tid_flow_qp =
1751*4882a593Smuzhiyun 		cpu_to_be32(qpriv->tid_rdma.local.qp |
1752*4882a593Smuzhiyun 			    ((flow->idx & TID_RDMA_DESTQP_FLOW_MASK) <<
1753*4882a593Smuzhiyun 			     TID_RDMA_DESTQP_FLOW_SHIFT) |
1754*4882a593Smuzhiyun 			    qpriv->rcd->ctxt);
1755*4882a593Smuzhiyun 	rreq->verbs_qp = cpu_to_be32(qp->remote_qpn);
1756*4882a593Smuzhiyun 	*bth1 &= ~RVT_QPN_MASK;
1757*4882a593Smuzhiyun 	*bth1 |= remote->qp;
1758*4882a593Smuzhiyun 	*bth2 |= IB_BTH_REQ_ACK;
1759*4882a593Smuzhiyun 	rcu_read_unlock();
1760*4882a593Smuzhiyun 
1761*4882a593Smuzhiyun 	/* We are done with this segment */
1762*4882a593Smuzhiyun 	flow->sent += *len;
1763*4882a593Smuzhiyun 	req->cur_seg++;
1764*4882a593Smuzhiyun 	qp->s_state = TID_OP(READ_REQ);
1765*4882a593Smuzhiyun 	req->ack_pending++;
1766*4882a593Smuzhiyun 	req->flow_idx = (req->flow_idx + 1) & (MAX_FLOWS - 1);
1767*4882a593Smuzhiyun 	qpriv->pending_tid_r_segs++;
1768*4882a593Smuzhiyun 	qp->s_num_rd_atomic++;
1769*4882a593Smuzhiyun 
1770*4882a593Smuzhiyun 	/* Set the TID RDMA READ request payload size */
1771*4882a593Smuzhiyun 	*len = req_len;
1772*4882a593Smuzhiyun 
1773*4882a593Smuzhiyun 	return sizeof(ohdr->u.tid_rdma.r_req) / sizeof(u32);
1774*4882a593Smuzhiyun }
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun /*
1777*4882a593Smuzhiyun  * @len: contains the data length to read upon entry and the read request
1778*4882a593Smuzhiyun  *       payload length upon exit.
1779*4882a593Smuzhiyun  */
hfi1_build_tid_rdma_read_req(struct rvt_qp * qp,struct rvt_swqe * wqe,struct ib_other_headers * ohdr,u32 * bth1,u32 * bth2,u32 * len)1780*4882a593Smuzhiyun u32 hfi1_build_tid_rdma_read_req(struct rvt_qp *qp, struct rvt_swqe *wqe,
1781*4882a593Smuzhiyun 				 struct ib_other_headers *ohdr, u32 *bth1,
1782*4882a593Smuzhiyun 				 u32 *bth2, u32 *len)
1783*4882a593Smuzhiyun 	__must_hold(&qp->s_lock)
1784*4882a593Smuzhiyun {
1785*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
1786*4882a593Smuzhiyun 	struct tid_rdma_request *req = wqe_to_tid_req(wqe);
1787*4882a593Smuzhiyun 	struct tid_rdma_flow *flow = NULL;
1788*4882a593Smuzhiyun 	u32 hdwords = 0;
1789*4882a593Smuzhiyun 	bool last;
1790*4882a593Smuzhiyun 	bool retry = true;
1791*4882a593Smuzhiyun 	u32 npkts = rvt_div_round_up_mtu(qp, *len);
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun 	trace_hfi1_tid_req_build_read_req(qp, 0, wqe->wr.opcode, wqe->psn,
1794*4882a593Smuzhiyun 					  wqe->lpsn, req);
1795*4882a593Smuzhiyun 	/*
1796*4882a593Smuzhiyun 	 * Check sync conditions. Make sure that there are no pending
1797*4882a593Smuzhiyun 	 * segments before freeing the flow.
1798*4882a593Smuzhiyun 	 */
1799*4882a593Smuzhiyun sync_check:
1800*4882a593Smuzhiyun 	if (req->state == TID_REQUEST_SYNC) {
1801*4882a593Smuzhiyun 		if (qpriv->pending_tid_r_segs)
1802*4882a593Smuzhiyun 			goto done;
1803*4882a593Smuzhiyun 
1804*4882a593Smuzhiyun 		hfi1_kern_clear_hw_flow(req->rcd, qp);
1805*4882a593Smuzhiyun 		qpriv->s_flags &= ~HFI1_R_TID_SW_PSN;
1806*4882a593Smuzhiyun 		req->state = TID_REQUEST_ACTIVE;
1807*4882a593Smuzhiyun 	}
1808*4882a593Smuzhiyun 
1809*4882a593Smuzhiyun 	/*
1810*4882a593Smuzhiyun 	 * If the request for this segment is resent, the tid resources should
1811*4882a593Smuzhiyun 	 * have been allocated before. In this case, req->flow_idx should
1812*4882a593Smuzhiyun 	 * fall behind req->setup_head.
1813*4882a593Smuzhiyun 	 */
1814*4882a593Smuzhiyun 	if (req->flow_idx == req->setup_head) {
1815*4882a593Smuzhiyun 		retry = false;
1816*4882a593Smuzhiyun 		if (req->state == TID_REQUEST_RESEND) {
1817*4882a593Smuzhiyun 			/*
1818*4882a593Smuzhiyun 			 * This is the first new segment for a request whose
1819*4882a593Smuzhiyun 			 * earlier segments have been re-sent. We need to
1820*4882a593Smuzhiyun 			 * set up the sge pointer correctly.
1821*4882a593Smuzhiyun 			 */
1822*4882a593Smuzhiyun 			restart_sge(&qp->s_sge, wqe, req->s_next_psn,
1823*4882a593Smuzhiyun 				    qp->pmtu);
1824*4882a593Smuzhiyun 			req->isge = 0;
1825*4882a593Smuzhiyun 			req->state = TID_REQUEST_ACTIVE;
1826*4882a593Smuzhiyun 		}
1827*4882a593Smuzhiyun 
1828*4882a593Smuzhiyun 		/*
1829*4882a593Smuzhiyun 		 * Check sync. The last PSN of each generation is reserved for
1830*4882a593Smuzhiyun 		 * RESYNC.
1831*4882a593Smuzhiyun 		 */
1832*4882a593Smuzhiyun 		if ((qpriv->flow_state.psn + npkts) > MAX_TID_FLOW_PSN - 1) {
1833*4882a593Smuzhiyun 			req->state = TID_REQUEST_SYNC;
1834*4882a593Smuzhiyun 			goto sync_check;
1835*4882a593Smuzhiyun 		}
1836*4882a593Smuzhiyun 
1837*4882a593Smuzhiyun 		/* Allocate the flow if not yet */
1838*4882a593Smuzhiyun 		if (hfi1_kern_setup_hw_flow(qpriv->rcd, qp))
1839*4882a593Smuzhiyun 			goto done;
1840*4882a593Smuzhiyun 
1841*4882a593Smuzhiyun 		/*
1842*4882a593Smuzhiyun 		 * The following call will advance req->setup_head after
1843*4882a593Smuzhiyun 		 * allocating the tid entries.
1844*4882a593Smuzhiyun 		 */
1845*4882a593Smuzhiyun 		if (hfi1_kern_exp_rcv_setup(req, &qp->s_sge, &last)) {
1846*4882a593Smuzhiyun 			req->state = TID_REQUEST_QUEUED;
1847*4882a593Smuzhiyun 
1848*4882a593Smuzhiyun 			/*
1849*4882a593Smuzhiyun 			 * We don't have resources for this segment. The QP has
1850*4882a593Smuzhiyun 			 * already been queued.
1851*4882a593Smuzhiyun 			 */
1852*4882a593Smuzhiyun 			goto done;
1853*4882a593Smuzhiyun 		}
1854*4882a593Smuzhiyun 	}
1855*4882a593Smuzhiyun 
1856*4882a593Smuzhiyun 	/* req->flow_idx should only be one slot behind req->setup_head */
1857*4882a593Smuzhiyun 	flow = &req->flows[req->flow_idx];
1858*4882a593Smuzhiyun 	flow->pkt = 0;
1859*4882a593Smuzhiyun 	flow->tid_idx = 0;
1860*4882a593Smuzhiyun 	flow->sent = 0;
1861*4882a593Smuzhiyun 	if (!retry) {
1862*4882a593Smuzhiyun 		/* Set the first and last IB PSN for the flow in use.*/
1863*4882a593Smuzhiyun 		flow->flow_state.ib_spsn = req->s_next_psn;
1864*4882a593Smuzhiyun 		flow->flow_state.ib_lpsn =
1865*4882a593Smuzhiyun 			flow->flow_state.ib_spsn + flow->npkts - 1;
1866*4882a593Smuzhiyun 	}
1867*4882a593Smuzhiyun 
1868*4882a593Smuzhiyun 	/* Calculate the next segment start psn.*/
1869*4882a593Smuzhiyun 	req->s_next_psn += flow->npkts;
1870*4882a593Smuzhiyun 
1871*4882a593Smuzhiyun 	/* Build the packet header */
1872*4882a593Smuzhiyun 	hdwords = hfi1_build_tid_rdma_read_packet(wqe, ohdr, bth1, bth2, len);
1873*4882a593Smuzhiyun done:
1874*4882a593Smuzhiyun 	return hdwords;
1875*4882a593Smuzhiyun }
1876*4882a593Smuzhiyun 
1877*4882a593Smuzhiyun /*
1878*4882a593Smuzhiyun  * Validate and accept the TID RDMA READ request parameters.
1879*4882a593Smuzhiyun  * Return 0 if the request is accepted successfully;
1880*4882a593Smuzhiyun  * Return 1 otherwise.
1881*4882a593Smuzhiyun  */
tid_rdma_rcv_read_request(struct rvt_qp * qp,struct rvt_ack_entry * e,struct hfi1_packet * packet,struct ib_other_headers * ohdr,u32 bth0,u32 psn,u64 vaddr,u32 len)1882*4882a593Smuzhiyun static int tid_rdma_rcv_read_request(struct rvt_qp *qp,
1883*4882a593Smuzhiyun 				     struct rvt_ack_entry *e,
1884*4882a593Smuzhiyun 				     struct hfi1_packet *packet,
1885*4882a593Smuzhiyun 				     struct ib_other_headers *ohdr,
1886*4882a593Smuzhiyun 				     u32 bth0, u32 psn, u64 vaddr, u32 len)
1887*4882a593Smuzhiyun {
1888*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
1889*4882a593Smuzhiyun 	struct tid_rdma_request *req;
1890*4882a593Smuzhiyun 	struct tid_rdma_flow *flow;
1891*4882a593Smuzhiyun 	u32 flow_psn, i, tidlen = 0, pktlen, tlen;
1892*4882a593Smuzhiyun 
1893*4882a593Smuzhiyun 	req = ack_to_tid_req(e);
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun 	/* Validate the payload first */
1896*4882a593Smuzhiyun 	flow = &req->flows[req->setup_head];
1897*4882a593Smuzhiyun 
1898*4882a593Smuzhiyun 	/* payload length = packet length - (header length + ICRC length) */
1899*4882a593Smuzhiyun 	pktlen = packet->tlen - (packet->hlen + 4);
1900*4882a593Smuzhiyun 	if (pktlen > sizeof(flow->tid_entry))
1901*4882a593Smuzhiyun 		return 1;
1902*4882a593Smuzhiyun 	memcpy(flow->tid_entry, packet->ebuf, pktlen);
1903*4882a593Smuzhiyun 	flow->tidcnt = pktlen / sizeof(*flow->tid_entry);
1904*4882a593Smuzhiyun 
1905*4882a593Smuzhiyun 	/*
1906*4882a593Smuzhiyun 	 * Walk the TID_ENTRY list to make sure we have enough space for a
1907*4882a593Smuzhiyun 	 * complete segment. Also calculate the number of required packets.
1908*4882a593Smuzhiyun 	 */
1909*4882a593Smuzhiyun 	flow->npkts = rvt_div_round_up_mtu(qp, len);
1910*4882a593Smuzhiyun 	for (i = 0; i < flow->tidcnt; i++) {
1911*4882a593Smuzhiyun 		trace_hfi1_tid_entry_rcv_read_req(qp, i,
1912*4882a593Smuzhiyun 						  flow->tid_entry[i]);
1913*4882a593Smuzhiyun 		tlen = EXP_TID_GET(flow->tid_entry[i], LEN);
1914*4882a593Smuzhiyun 		if (!tlen)
1915*4882a593Smuzhiyun 			return 1;
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun 		/*
1918*4882a593Smuzhiyun 		 * For tid pair (tidctr == 3), the buffer size of the pair
1919*4882a593Smuzhiyun 		 * should be the sum of the buffer size described by each
1920*4882a593Smuzhiyun 		 * tid entry. However, only the first entry needs to be
1921*4882a593Smuzhiyun 		 * specified in the request (see WFR HAS Section 8.5.7.1).
1922*4882a593Smuzhiyun 		 */
1923*4882a593Smuzhiyun 		tidlen += tlen;
1924*4882a593Smuzhiyun 	}
1925*4882a593Smuzhiyun 	if (tidlen * PAGE_SIZE < len)
1926*4882a593Smuzhiyun 		return 1;
1927*4882a593Smuzhiyun 
1928*4882a593Smuzhiyun 	/* Empty the flow array */
1929*4882a593Smuzhiyun 	req->clear_tail = req->setup_head;
1930*4882a593Smuzhiyun 	flow->pkt = 0;
1931*4882a593Smuzhiyun 	flow->tid_idx = 0;
1932*4882a593Smuzhiyun 	flow->tid_offset = 0;
1933*4882a593Smuzhiyun 	flow->sent = 0;
1934*4882a593Smuzhiyun 	flow->tid_qpn = be32_to_cpu(ohdr->u.tid_rdma.r_req.tid_flow_qp);
1935*4882a593Smuzhiyun 	flow->idx = (flow->tid_qpn >> TID_RDMA_DESTQP_FLOW_SHIFT) &
1936*4882a593Smuzhiyun 		    TID_RDMA_DESTQP_FLOW_MASK;
1937*4882a593Smuzhiyun 	flow_psn = mask_psn(be32_to_cpu(ohdr->u.tid_rdma.r_req.tid_flow_psn));
1938*4882a593Smuzhiyun 	flow->flow_state.generation = flow_psn >> HFI1_KDETH_BTH_SEQ_SHIFT;
1939*4882a593Smuzhiyun 	flow->flow_state.spsn = flow_psn & HFI1_KDETH_BTH_SEQ_MASK;
1940*4882a593Smuzhiyun 	flow->length = len;
1941*4882a593Smuzhiyun 
1942*4882a593Smuzhiyun 	flow->flow_state.lpsn = flow->flow_state.spsn +
1943*4882a593Smuzhiyun 		flow->npkts - 1;
1944*4882a593Smuzhiyun 	flow->flow_state.ib_spsn = psn;
1945*4882a593Smuzhiyun 	flow->flow_state.ib_lpsn = flow->flow_state.ib_spsn + flow->npkts - 1;
1946*4882a593Smuzhiyun 
1947*4882a593Smuzhiyun 	trace_hfi1_tid_flow_rcv_read_req(qp, req->setup_head, flow);
1948*4882a593Smuzhiyun 	/* Set the initial flow index to the current flow. */
1949*4882a593Smuzhiyun 	req->flow_idx = req->setup_head;
1950*4882a593Smuzhiyun 
1951*4882a593Smuzhiyun 	/* advance circular buffer head */
1952*4882a593Smuzhiyun 	req->setup_head = (req->setup_head + 1) & (MAX_FLOWS - 1);
1953*4882a593Smuzhiyun 
1954*4882a593Smuzhiyun 	/*
1955*4882a593Smuzhiyun 	 * Compute last PSN for request.
1956*4882a593Smuzhiyun 	 */
1957*4882a593Smuzhiyun 	e->opcode = (bth0 >> 24) & 0xff;
1958*4882a593Smuzhiyun 	e->psn = psn;
1959*4882a593Smuzhiyun 	e->lpsn = psn + flow->npkts - 1;
1960*4882a593Smuzhiyun 	e->sent = 0;
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 	req->n_flows = qpriv->tid_rdma.local.max_read;
1963*4882a593Smuzhiyun 	req->state = TID_REQUEST_ACTIVE;
1964*4882a593Smuzhiyun 	req->cur_seg = 0;
1965*4882a593Smuzhiyun 	req->comp_seg = 0;
1966*4882a593Smuzhiyun 	req->ack_seg = 0;
1967*4882a593Smuzhiyun 	req->isge = 0;
1968*4882a593Smuzhiyun 	req->seg_len = qpriv->tid_rdma.local.max_len;
1969*4882a593Smuzhiyun 	req->total_len = len;
1970*4882a593Smuzhiyun 	req->total_segs = 1;
1971*4882a593Smuzhiyun 	req->r_flow_psn = e->psn;
1972*4882a593Smuzhiyun 
1973*4882a593Smuzhiyun 	trace_hfi1_tid_req_rcv_read_req(qp, 0, e->opcode, e->psn, e->lpsn,
1974*4882a593Smuzhiyun 					req);
1975*4882a593Smuzhiyun 	return 0;
1976*4882a593Smuzhiyun }
1977*4882a593Smuzhiyun 
tid_rdma_rcv_error(struct hfi1_packet * packet,struct ib_other_headers * ohdr,struct rvt_qp * qp,u32 psn,int diff)1978*4882a593Smuzhiyun static int tid_rdma_rcv_error(struct hfi1_packet *packet,
1979*4882a593Smuzhiyun 			      struct ib_other_headers *ohdr,
1980*4882a593Smuzhiyun 			      struct rvt_qp *qp, u32 psn, int diff)
1981*4882a593Smuzhiyun {
1982*4882a593Smuzhiyun 	struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
1983*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = ((struct hfi1_qp_priv *)qp->priv)->rcd;
1984*4882a593Smuzhiyun 	struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
1985*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
1986*4882a593Smuzhiyun 	struct rvt_ack_entry *e;
1987*4882a593Smuzhiyun 	struct tid_rdma_request *req;
1988*4882a593Smuzhiyun 	unsigned long flags;
1989*4882a593Smuzhiyun 	u8 prev;
1990*4882a593Smuzhiyun 	bool old_req;
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun 	trace_hfi1_rsp_tid_rcv_error(qp, psn);
1993*4882a593Smuzhiyun 	trace_hfi1_tid_rdma_rcv_err(qp, 0, psn, diff);
1994*4882a593Smuzhiyun 	if (diff > 0) {
1995*4882a593Smuzhiyun 		/* sequence error */
1996*4882a593Smuzhiyun 		if (!qp->r_nak_state) {
1997*4882a593Smuzhiyun 			ibp->rvp.n_rc_seqnak++;
1998*4882a593Smuzhiyun 			qp->r_nak_state = IB_NAK_PSN_ERROR;
1999*4882a593Smuzhiyun 			qp->r_ack_psn = qp->r_psn;
2000*4882a593Smuzhiyun 			rc_defered_ack(rcd, qp);
2001*4882a593Smuzhiyun 		}
2002*4882a593Smuzhiyun 		goto done;
2003*4882a593Smuzhiyun 	}
2004*4882a593Smuzhiyun 
2005*4882a593Smuzhiyun 	ibp->rvp.n_rc_dupreq++;
2006*4882a593Smuzhiyun 
2007*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->s_lock, flags);
2008*4882a593Smuzhiyun 	e = find_prev_entry(qp, psn, &prev, NULL, &old_req);
2009*4882a593Smuzhiyun 	if (!e || (e->opcode != TID_OP(READ_REQ) &&
2010*4882a593Smuzhiyun 		   e->opcode != TID_OP(WRITE_REQ)))
2011*4882a593Smuzhiyun 		goto unlock;
2012*4882a593Smuzhiyun 
2013*4882a593Smuzhiyun 	req = ack_to_tid_req(e);
2014*4882a593Smuzhiyun 	req->r_flow_psn = psn;
2015*4882a593Smuzhiyun 	trace_hfi1_tid_req_rcv_err(qp, 0, e->opcode, e->psn, e->lpsn, req);
2016*4882a593Smuzhiyun 	if (e->opcode == TID_OP(READ_REQ)) {
2017*4882a593Smuzhiyun 		struct ib_reth *reth;
2018*4882a593Smuzhiyun 		u32 len;
2019*4882a593Smuzhiyun 		u32 rkey;
2020*4882a593Smuzhiyun 		u64 vaddr;
2021*4882a593Smuzhiyun 		int ok;
2022*4882a593Smuzhiyun 		u32 bth0;
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun 		reth = &ohdr->u.tid_rdma.r_req.reth;
2025*4882a593Smuzhiyun 		/*
2026*4882a593Smuzhiyun 		 * The requester always restarts from the start of the original
2027*4882a593Smuzhiyun 		 * request.
2028*4882a593Smuzhiyun 		 */
2029*4882a593Smuzhiyun 		len = be32_to_cpu(reth->length);
2030*4882a593Smuzhiyun 		if (psn != e->psn || len != req->total_len)
2031*4882a593Smuzhiyun 			goto unlock;
2032*4882a593Smuzhiyun 
2033*4882a593Smuzhiyun 		release_rdma_sge_mr(e);
2034*4882a593Smuzhiyun 
2035*4882a593Smuzhiyun 		rkey = be32_to_cpu(reth->rkey);
2036*4882a593Smuzhiyun 		vaddr = get_ib_reth_vaddr(reth);
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun 		qp->r_len = len;
2039*4882a593Smuzhiyun 		ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
2040*4882a593Smuzhiyun 				 IB_ACCESS_REMOTE_READ);
2041*4882a593Smuzhiyun 		if (unlikely(!ok))
2042*4882a593Smuzhiyun 			goto unlock;
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun 		/*
2045*4882a593Smuzhiyun 		 * If all the response packets for the current request have
2046*4882a593Smuzhiyun 		 * been sent out and this request is complete (old_request
2047*4882a593Smuzhiyun 		 * == false) and the TID flow may be unusable (the
2048*4882a593Smuzhiyun 		 * req->clear_tail is advanced). However, when an earlier
2049*4882a593Smuzhiyun 		 * request is received, this request will not be complete any
2050*4882a593Smuzhiyun 		 * more (qp->s_tail_ack_queue is moved back, see below).
2051*4882a593Smuzhiyun 		 * Consequently, we need to update the TID flow info everytime
2052*4882a593Smuzhiyun 		 * a duplicate request is received.
2053*4882a593Smuzhiyun 		 */
2054*4882a593Smuzhiyun 		bth0 = be32_to_cpu(ohdr->bth[0]);
2055*4882a593Smuzhiyun 		if (tid_rdma_rcv_read_request(qp, e, packet, ohdr, bth0, psn,
2056*4882a593Smuzhiyun 					      vaddr, len))
2057*4882a593Smuzhiyun 			goto unlock;
2058*4882a593Smuzhiyun 
2059*4882a593Smuzhiyun 		/*
2060*4882a593Smuzhiyun 		 * True if the request is already scheduled (between
2061*4882a593Smuzhiyun 		 * qp->s_tail_ack_queue and qp->r_head_ack_queue);
2062*4882a593Smuzhiyun 		 */
2063*4882a593Smuzhiyun 		if (old_req)
2064*4882a593Smuzhiyun 			goto unlock;
2065*4882a593Smuzhiyun 	} else {
2066*4882a593Smuzhiyun 		struct flow_state *fstate;
2067*4882a593Smuzhiyun 		bool schedule = false;
2068*4882a593Smuzhiyun 		u8 i;
2069*4882a593Smuzhiyun 
2070*4882a593Smuzhiyun 		if (req->state == TID_REQUEST_RESEND) {
2071*4882a593Smuzhiyun 			req->state = TID_REQUEST_RESEND_ACTIVE;
2072*4882a593Smuzhiyun 		} else if (req->state == TID_REQUEST_INIT_RESEND) {
2073*4882a593Smuzhiyun 			req->state = TID_REQUEST_INIT;
2074*4882a593Smuzhiyun 			schedule = true;
2075*4882a593Smuzhiyun 		}
2076*4882a593Smuzhiyun 
2077*4882a593Smuzhiyun 		/*
2078*4882a593Smuzhiyun 		 * True if the request is already scheduled (between
2079*4882a593Smuzhiyun 		 * qp->s_tail_ack_queue and qp->r_head_ack_queue).
2080*4882a593Smuzhiyun 		 * Also, don't change requests, which are at the SYNC
2081*4882a593Smuzhiyun 		 * point and haven't generated any responses yet.
2082*4882a593Smuzhiyun 		 * There is nothing to retransmit for them yet.
2083*4882a593Smuzhiyun 		 */
2084*4882a593Smuzhiyun 		if (old_req || req->state == TID_REQUEST_INIT ||
2085*4882a593Smuzhiyun 		    (req->state == TID_REQUEST_SYNC && !req->cur_seg)) {
2086*4882a593Smuzhiyun 			for (i = prev + 1; ; i++) {
2087*4882a593Smuzhiyun 				if (i > rvt_size_atomic(&dev->rdi))
2088*4882a593Smuzhiyun 					i = 0;
2089*4882a593Smuzhiyun 				if (i == qp->r_head_ack_queue)
2090*4882a593Smuzhiyun 					break;
2091*4882a593Smuzhiyun 				e = &qp->s_ack_queue[i];
2092*4882a593Smuzhiyun 				req = ack_to_tid_req(e);
2093*4882a593Smuzhiyun 				if (e->opcode == TID_OP(WRITE_REQ) &&
2094*4882a593Smuzhiyun 				    req->state == TID_REQUEST_INIT)
2095*4882a593Smuzhiyun 					req->state = TID_REQUEST_INIT_RESEND;
2096*4882a593Smuzhiyun 			}
2097*4882a593Smuzhiyun 			/*
2098*4882a593Smuzhiyun 			 * If the state of the request has been changed,
2099*4882a593Smuzhiyun 			 * the first leg needs to get scheduled in order to
2100*4882a593Smuzhiyun 			 * pick up the change. Otherwise, normal response
2101*4882a593Smuzhiyun 			 * processing should take care of it.
2102*4882a593Smuzhiyun 			 */
2103*4882a593Smuzhiyun 			if (!schedule)
2104*4882a593Smuzhiyun 				goto unlock;
2105*4882a593Smuzhiyun 		}
2106*4882a593Smuzhiyun 
2107*4882a593Smuzhiyun 		/*
2108*4882a593Smuzhiyun 		 * If there is no more allocated segment, just schedule the qp
2109*4882a593Smuzhiyun 		 * without changing any state.
2110*4882a593Smuzhiyun 		 */
2111*4882a593Smuzhiyun 		if (req->clear_tail == req->setup_head)
2112*4882a593Smuzhiyun 			goto schedule;
2113*4882a593Smuzhiyun 		/*
2114*4882a593Smuzhiyun 		 * If this request has sent responses for segments, which have
2115*4882a593Smuzhiyun 		 * not received data yet (flow_idx != clear_tail), the flow_idx
2116*4882a593Smuzhiyun 		 * pointer needs to be adjusted so the same responses can be
2117*4882a593Smuzhiyun 		 * re-sent.
2118*4882a593Smuzhiyun 		 */
2119*4882a593Smuzhiyun 		if (CIRC_CNT(req->flow_idx, req->clear_tail, MAX_FLOWS)) {
2120*4882a593Smuzhiyun 			fstate = &req->flows[req->clear_tail].flow_state;
2121*4882a593Smuzhiyun 			qpriv->pending_tid_w_segs -=
2122*4882a593Smuzhiyun 				CIRC_CNT(req->flow_idx, req->clear_tail,
2123*4882a593Smuzhiyun 					 MAX_FLOWS);
2124*4882a593Smuzhiyun 			req->flow_idx =
2125*4882a593Smuzhiyun 				CIRC_ADD(req->clear_tail,
2126*4882a593Smuzhiyun 					 delta_psn(psn, fstate->resp_ib_psn),
2127*4882a593Smuzhiyun 					 MAX_FLOWS);
2128*4882a593Smuzhiyun 			qpriv->pending_tid_w_segs +=
2129*4882a593Smuzhiyun 				delta_psn(psn, fstate->resp_ib_psn);
2130*4882a593Smuzhiyun 			/*
2131*4882a593Smuzhiyun 			 * When flow_idx == setup_head, we've gotten a duplicate
2132*4882a593Smuzhiyun 			 * request for a segment, which has not been allocated
2133*4882a593Smuzhiyun 			 * yet. In that case, don't adjust this request.
2134*4882a593Smuzhiyun 			 * However, we still want to go through the loop below
2135*4882a593Smuzhiyun 			 * to adjust all subsequent requests.
2136*4882a593Smuzhiyun 			 */
2137*4882a593Smuzhiyun 			if (CIRC_CNT(req->setup_head, req->flow_idx,
2138*4882a593Smuzhiyun 				     MAX_FLOWS)) {
2139*4882a593Smuzhiyun 				req->cur_seg = delta_psn(psn, e->psn);
2140*4882a593Smuzhiyun 				req->state = TID_REQUEST_RESEND_ACTIVE;
2141*4882a593Smuzhiyun 			}
2142*4882a593Smuzhiyun 		}
2143*4882a593Smuzhiyun 
2144*4882a593Smuzhiyun 		for (i = prev + 1; ; i++) {
2145*4882a593Smuzhiyun 			/*
2146*4882a593Smuzhiyun 			 * Look at everything up to and including
2147*4882a593Smuzhiyun 			 * s_tail_ack_queue
2148*4882a593Smuzhiyun 			 */
2149*4882a593Smuzhiyun 			if (i > rvt_size_atomic(&dev->rdi))
2150*4882a593Smuzhiyun 				i = 0;
2151*4882a593Smuzhiyun 			if (i == qp->r_head_ack_queue)
2152*4882a593Smuzhiyun 				break;
2153*4882a593Smuzhiyun 			e = &qp->s_ack_queue[i];
2154*4882a593Smuzhiyun 			req = ack_to_tid_req(e);
2155*4882a593Smuzhiyun 			trace_hfi1_tid_req_rcv_err(qp, 0, e->opcode, e->psn,
2156*4882a593Smuzhiyun 						   e->lpsn, req);
2157*4882a593Smuzhiyun 			if (e->opcode != TID_OP(WRITE_REQ) ||
2158*4882a593Smuzhiyun 			    req->cur_seg == req->comp_seg ||
2159*4882a593Smuzhiyun 			    req->state == TID_REQUEST_INIT ||
2160*4882a593Smuzhiyun 			    req->state == TID_REQUEST_INIT_RESEND) {
2161*4882a593Smuzhiyun 				if (req->state == TID_REQUEST_INIT)
2162*4882a593Smuzhiyun 					req->state = TID_REQUEST_INIT_RESEND;
2163*4882a593Smuzhiyun 				continue;
2164*4882a593Smuzhiyun 			}
2165*4882a593Smuzhiyun 			qpriv->pending_tid_w_segs -=
2166*4882a593Smuzhiyun 				CIRC_CNT(req->flow_idx,
2167*4882a593Smuzhiyun 					 req->clear_tail,
2168*4882a593Smuzhiyun 					 MAX_FLOWS);
2169*4882a593Smuzhiyun 			req->flow_idx = req->clear_tail;
2170*4882a593Smuzhiyun 			req->state = TID_REQUEST_RESEND;
2171*4882a593Smuzhiyun 			req->cur_seg = req->comp_seg;
2172*4882a593Smuzhiyun 		}
2173*4882a593Smuzhiyun 		qpriv->s_flags &= ~HFI1_R_TID_WAIT_INTERLCK;
2174*4882a593Smuzhiyun 	}
2175*4882a593Smuzhiyun 	/* Re-process old requests.*/
2176*4882a593Smuzhiyun 	if (qp->s_acked_ack_queue == qp->s_tail_ack_queue)
2177*4882a593Smuzhiyun 		qp->s_acked_ack_queue = prev;
2178*4882a593Smuzhiyun 	qp->s_tail_ack_queue = prev;
2179*4882a593Smuzhiyun 	/*
2180*4882a593Smuzhiyun 	 * Since the qp->s_tail_ack_queue is modified, the
2181*4882a593Smuzhiyun 	 * qp->s_ack_state must be changed to re-initialize
2182*4882a593Smuzhiyun 	 * qp->s_ack_rdma_sge; Otherwise, we will end up in
2183*4882a593Smuzhiyun 	 * wrong memory region.
2184*4882a593Smuzhiyun 	 */
2185*4882a593Smuzhiyun 	qp->s_ack_state = OP(ACKNOWLEDGE);
2186*4882a593Smuzhiyun schedule:
2187*4882a593Smuzhiyun 	/*
2188*4882a593Smuzhiyun 	 * It's possible to receive a retry psn that is earlier than an RNRNAK
2189*4882a593Smuzhiyun 	 * psn. In this case, the rnrnak state should be cleared.
2190*4882a593Smuzhiyun 	 */
2191*4882a593Smuzhiyun 	if (qpriv->rnr_nak_state) {
2192*4882a593Smuzhiyun 		qp->s_nak_state = 0;
2193*4882a593Smuzhiyun 		qpriv->rnr_nak_state = TID_RNR_NAK_INIT;
2194*4882a593Smuzhiyun 		qp->r_psn = e->lpsn + 1;
2195*4882a593Smuzhiyun 		hfi1_tid_write_alloc_resources(qp, true);
2196*4882a593Smuzhiyun 	}
2197*4882a593Smuzhiyun 
2198*4882a593Smuzhiyun 	qp->r_state = e->opcode;
2199*4882a593Smuzhiyun 	qp->r_nak_state = 0;
2200*4882a593Smuzhiyun 	qp->s_flags |= RVT_S_RESP_PENDING;
2201*4882a593Smuzhiyun 	hfi1_schedule_send(qp);
2202*4882a593Smuzhiyun unlock:
2203*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, flags);
2204*4882a593Smuzhiyun done:
2205*4882a593Smuzhiyun 	return 1;
2206*4882a593Smuzhiyun }
2207*4882a593Smuzhiyun 
hfi1_rc_rcv_tid_rdma_read_req(struct hfi1_packet * packet)2208*4882a593Smuzhiyun void hfi1_rc_rcv_tid_rdma_read_req(struct hfi1_packet *packet)
2209*4882a593Smuzhiyun {
2210*4882a593Smuzhiyun 	/* HANDLER FOR TID RDMA READ REQUEST packet (Responder side)*/
2211*4882a593Smuzhiyun 
2212*4882a593Smuzhiyun 	/*
2213*4882a593Smuzhiyun 	 * 1. Verify TID RDMA READ REQ as per IB_OPCODE_RC_RDMA_READ
2214*4882a593Smuzhiyun 	 *    (see hfi1_rc_rcv())
2215*4882a593Smuzhiyun 	 * 2. Put TID RDMA READ REQ into the response queueu (s_ack_queue)
2216*4882a593Smuzhiyun 	 *     - Setup struct tid_rdma_req with request info
2217*4882a593Smuzhiyun 	 *     - Initialize struct tid_rdma_flow info;
2218*4882a593Smuzhiyun 	 *     - Copy TID entries;
2219*4882a593Smuzhiyun 	 * 3. Set the qp->s_ack_state.
2220*4882a593Smuzhiyun 	 * 4. Set RVT_S_RESP_PENDING in s_flags.
2221*4882a593Smuzhiyun 	 * 5. Kick the send engine (hfi1_schedule_send())
2222*4882a593Smuzhiyun 	 */
2223*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = packet->rcd;
2224*4882a593Smuzhiyun 	struct rvt_qp *qp = packet->qp;
2225*4882a593Smuzhiyun 	struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
2226*4882a593Smuzhiyun 	struct ib_other_headers *ohdr = packet->ohdr;
2227*4882a593Smuzhiyun 	struct rvt_ack_entry *e;
2228*4882a593Smuzhiyun 	unsigned long flags;
2229*4882a593Smuzhiyun 	struct ib_reth *reth;
2230*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
2231*4882a593Smuzhiyun 	u32 bth0, psn, len, rkey;
2232*4882a593Smuzhiyun 	bool fecn;
2233*4882a593Smuzhiyun 	u8 next;
2234*4882a593Smuzhiyun 	u64 vaddr;
2235*4882a593Smuzhiyun 	int diff;
2236*4882a593Smuzhiyun 	u8 nack_state = IB_NAK_INVALID_REQUEST;
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun 	bth0 = be32_to_cpu(ohdr->bth[0]);
2239*4882a593Smuzhiyun 	if (hfi1_ruc_check_hdr(ibp, packet))
2240*4882a593Smuzhiyun 		return;
2241*4882a593Smuzhiyun 
2242*4882a593Smuzhiyun 	fecn = process_ecn(qp, packet);
2243*4882a593Smuzhiyun 	psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
2244*4882a593Smuzhiyun 	trace_hfi1_rsp_rcv_tid_read_req(qp, psn);
2245*4882a593Smuzhiyun 
2246*4882a593Smuzhiyun 	if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
2247*4882a593Smuzhiyun 		rvt_comm_est(qp);
2248*4882a593Smuzhiyun 
2249*4882a593Smuzhiyun 	if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
2250*4882a593Smuzhiyun 		goto nack_inv;
2251*4882a593Smuzhiyun 
2252*4882a593Smuzhiyun 	reth = &ohdr->u.tid_rdma.r_req.reth;
2253*4882a593Smuzhiyun 	vaddr = be64_to_cpu(reth->vaddr);
2254*4882a593Smuzhiyun 	len = be32_to_cpu(reth->length);
2255*4882a593Smuzhiyun 	/* The length needs to be in multiples of PAGE_SIZE */
2256*4882a593Smuzhiyun 	if (!len || len & ~PAGE_MASK || len > qpriv->tid_rdma.local.max_len)
2257*4882a593Smuzhiyun 		goto nack_inv;
2258*4882a593Smuzhiyun 
2259*4882a593Smuzhiyun 	diff = delta_psn(psn, qp->r_psn);
2260*4882a593Smuzhiyun 	if (unlikely(diff)) {
2261*4882a593Smuzhiyun 		tid_rdma_rcv_err(packet, ohdr, qp, psn, diff, fecn);
2262*4882a593Smuzhiyun 		return;
2263*4882a593Smuzhiyun 	}
2264*4882a593Smuzhiyun 
2265*4882a593Smuzhiyun 	/* We've verified the request, insert it into the ack queue. */
2266*4882a593Smuzhiyun 	next = qp->r_head_ack_queue + 1;
2267*4882a593Smuzhiyun 	if (next > rvt_size_atomic(ib_to_rvt(qp->ibqp.device)))
2268*4882a593Smuzhiyun 		next = 0;
2269*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->s_lock, flags);
2270*4882a593Smuzhiyun 	if (unlikely(next == qp->s_tail_ack_queue)) {
2271*4882a593Smuzhiyun 		if (!qp->s_ack_queue[next].sent) {
2272*4882a593Smuzhiyun 			nack_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
2273*4882a593Smuzhiyun 			goto nack_inv_unlock;
2274*4882a593Smuzhiyun 		}
2275*4882a593Smuzhiyun 		update_ack_queue(qp, next);
2276*4882a593Smuzhiyun 	}
2277*4882a593Smuzhiyun 	e = &qp->s_ack_queue[qp->r_head_ack_queue];
2278*4882a593Smuzhiyun 	release_rdma_sge_mr(e);
2279*4882a593Smuzhiyun 
2280*4882a593Smuzhiyun 	rkey = be32_to_cpu(reth->rkey);
2281*4882a593Smuzhiyun 	qp->r_len = len;
2282*4882a593Smuzhiyun 
2283*4882a593Smuzhiyun 	if (unlikely(!rvt_rkey_ok(qp, &e->rdma_sge, qp->r_len, vaddr,
2284*4882a593Smuzhiyun 				  rkey, IB_ACCESS_REMOTE_READ)))
2285*4882a593Smuzhiyun 		goto nack_acc;
2286*4882a593Smuzhiyun 
2287*4882a593Smuzhiyun 	/* Accept the request parameters */
2288*4882a593Smuzhiyun 	if (tid_rdma_rcv_read_request(qp, e, packet, ohdr, bth0, psn, vaddr,
2289*4882a593Smuzhiyun 				      len))
2290*4882a593Smuzhiyun 		goto nack_inv_unlock;
2291*4882a593Smuzhiyun 
2292*4882a593Smuzhiyun 	qp->r_state = e->opcode;
2293*4882a593Smuzhiyun 	qp->r_nak_state = 0;
2294*4882a593Smuzhiyun 	/*
2295*4882a593Smuzhiyun 	 * We need to increment the MSN here instead of when we
2296*4882a593Smuzhiyun 	 * finish sending the result since a duplicate request would
2297*4882a593Smuzhiyun 	 * increment it more than once.
2298*4882a593Smuzhiyun 	 */
2299*4882a593Smuzhiyun 	qp->r_msn++;
2300*4882a593Smuzhiyun 	qp->r_psn += e->lpsn - e->psn + 1;
2301*4882a593Smuzhiyun 
2302*4882a593Smuzhiyun 	qp->r_head_ack_queue = next;
2303*4882a593Smuzhiyun 
2304*4882a593Smuzhiyun 	/*
2305*4882a593Smuzhiyun 	 * For all requests other than TID WRITE which are added to the ack
2306*4882a593Smuzhiyun 	 * queue, qpriv->r_tid_alloc follows qp->r_head_ack_queue. It is ok to
2307*4882a593Smuzhiyun 	 * do this because of interlocks between these and TID WRITE
2308*4882a593Smuzhiyun 	 * requests. The same change has also been made in hfi1_rc_rcv().
2309*4882a593Smuzhiyun 	 */
2310*4882a593Smuzhiyun 	qpriv->r_tid_alloc = qp->r_head_ack_queue;
2311*4882a593Smuzhiyun 
2312*4882a593Smuzhiyun 	/* Schedule the send tasklet. */
2313*4882a593Smuzhiyun 	qp->s_flags |= RVT_S_RESP_PENDING;
2314*4882a593Smuzhiyun 	if (fecn)
2315*4882a593Smuzhiyun 		qp->s_flags |= RVT_S_ECN;
2316*4882a593Smuzhiyun 	hfi1_schedule_send(qp);
2317*4882a593Smuzhiyun 
2318*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, flags);
2319*4882a593Smuzhiyun 	return;
2320*4882a593Smuzhiyun 
2321*4882a593Smuzhiyun nack_inv_unlock:
2322*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, flags);
2323*4882a593Smuzhiyun nack_inv:
2324*4882a593Smuzhiyun 	rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
2325*4882a593Smuzhiyun 	qp->r_nak_state = nack_state;
2326*4882a593Smuzhiyun 	qp->r_ack_psn = qp->r_psn;
2327*4882a593Smuzhiyun 	/* Queue NAK for later */
2328*4882a593Smuzhiyun 	rc_defered_ack(rcd, qp);
2329*4882a593Smuzhiyun 	return;
2330*4882a593Smuzhiyun nack_acc:
2331*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, flags);
2332*4882a593Smuzhiyun 	rvt_rc_error(qp, IB_WC_LOC_PROT_ERR);
2333*4882a593Smuzhiyun 	qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
2334*4882a593Smuzhiyun 	qp->r_ack_psn = qp->r_psn;
2335*4882a593Smuzhiyun }
2336*4882a593Smuzhiyun 
hfi1_build_tid_rdma_read_resp(struct rvt_qp * qp,struct rvt_ack_entry * e,struct ib_other_headers * ohdr,u32 * bth0,u32 * bth1,u32 * bth2,u32 * len,bool * last)2337*4882a593Smuzhiyun u32 hfi1_build_tid_rdma_read_resp(struct rvt_qp *qp, struct rvt_ack_entry *e,
2338*4882a593Smuzhiyun 				  struct ib_other_headers *ohdr, u32 *bth0,
2339*4882a593Smuzhiyun 				  u32 *bth1, u32 *bth2, u32 *len, bool *last)
2340*4882a593Smuzhiyun {
2341*4882a593Smuzhiyun 	struct hfi1_ack_priv *epriv = e->priv;
2342*4882a593Smuzhiyun 	struct tid_rdma_request *req = &epriv->tid_req;
2343*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
2344*4882a593Smuzhiyun 	struct tid_rdma_flow *flow = &req->flows[req->clear_tail];
2345*4882a593Smuzhiyun 	u32 tidentry = flow->tid_entry[flow->tid_idx];
2346*4882a593Smuzhiyun 	u32 tidlen = EXP_TID_GET(tidentry, LEN) << PAGE_SHIFT;
2347*4882a593Smuzhiyun 	struct tid_rdma_read_resp *resp = &ohdr->u.tid_rdma.r_rsp;
2348*4882a593Smuzhiyun 	u32 next_offset, om = KDETH_OM_LARGE;
2349*4882a593Smuzhiyun 	bool last_pkt;
2350*4882a593Smuzhiyun 	u32 hdwords = 0;
2351*4882a593Smuzhiyun 	struct tid_rdma_params *remote;
2352*4882a593Smuzhiyun 
2353*4882a593Smuzhiyun 	*len = min_t(u32, qp->pmtu, tidlen - flow->tid_offset);
2354*4882a593Smuzhiyun 	flow->sent += *len;
2355*4882a593Smuzhiyun 	next_offset = flow->tid_offset + *len;
2356*4882a593Smuzhiyun 	last_pkt = (flow->sent >= flow->length);
2357*4882a593Smuzhiyun 
2358*4882a593Smuzhiyun 	trace_hfi1_tid_entry_build_read_resp(qp, flow->tid_idx, tidentry);
2359*4882a593Smuzhiyun 	trace_hfi1_tid_flow_build_read_resp(qp, req->clear_tail, flow);
2360*4882a593Smuzhiyun 
2361*4882a593Smuzhiyun 	rcu_read_lock();
2362*4882a593Smuzhiyun 	remote = rcu_dereference(qpriv->tid_rdma.remote);
2363*4882a593Smuzhiyun 	if (!remote) {
2364*4882a593Smuzhiyun 		rcu_read_unlock();
2365*4882a593Smuzhiyun 		goto done;
2366*4882a593Smuzhiyun 	}
2367*4882a593Smuzhiyun 	KDETH_RESET(resp->kdeth0, KVER, 0x1);
2368*4882a593Smuzhiyun 	KDETH_SET(resp->kdeth0, SH, !last_pkt);
2369*4882a593Smuzhiyun 	KDETH_SET(resp->kdeth0, INTR, !!(!last_pkt && remote->urg));
2370*4882a593Smuzhiyun 	KDETH_SET(resp->kdeth0, TIDCTRL, EXP_TID_GET(tidentry, CTRL));
2371*4882a593Smuzhiyun 	KDETH_SET(resp->kdeth0, TID, EXP_TID_GET(tidentry, IDX));
2372*4882a593Smuzhiyun 	KDETH_SET(resp->kdeth0, OM, om == KDETH_OM_LARGE);
2373*4882a593Smuzhiyun 	KDETH_SET(resp->kdeth0, OFFSET, flow->tid_offset / om);
2374*4882a593Smuzhiyun 	KDETH_RESET(resp->kdeth1, JKEY, remote->jkey);
2375*4882a593Smuzhiyun 	resp->verbs_qp = cpu_to_be32(qp->remote_qpn);
2376*4882a593Smuzhiyun 	rcu_read_unlock();
2377*4882a593Smuzhiyun 
2378*4882a593Smuzhiyun 	resp->aeth = rvt_compute_aeth(qp);
2379*4882a593Smuzhiyun 	resp->verbs_psn = cpu_to_be32(mask_psn(flow->flow_state.ib_spsn +
2380*4882a593Smuzhiyun 					       flow->pkt));
2381*4882a593Smuzhiyun 
2382*4882a593Smuzhiyun 	*bth0 = TID_OP(READ_RESP) << 24;
2383*4882a593Smuzhiyun 	*bth1 = flow->tid_qpn;
2384*4882a593Smuzhiyun 	*bth2 = mask_psn(((flow->flow_state.spsn + flow->pkt++) &
2385*4882a593Smuzhiyun 			  HFI1_KDETH_BTH_SEQ_MASK) |
2386*4882a593Smuzhiyun 			 (flow->flow_state.generation <<
2387*4882a593Smuzhiyun 			  HFI1_KDETH_BTH_SEQ_SHIFT));
2388*4882a593Smuzhiyun 	*last = last_pkt;
2389*4882a593Smuzhiyun 	if (last_pkt)
2390*4882a593Smuzhiyun 		/* Advance to next flow */
2391*4882a593Smuzhiyun 		req->clear_tail = (req->clear_tail + 1) &
2392*4882a593Smuzhiyun 				  (MAX_FLOWS - 1);
2393*4882a593Smuzhiyun 
2394*4882a593Smuzhiyun 	if (next_offset >= tidlen) {
2395*4882a593Smuzhiyun 		flow->tid_offset = 0;
2396*4882a593Smuzhiyun 		flow->tid_idx++;
2397*4882a593Smuzhiyun 	} else {
2398*4882a593Smuzhiyun 		flow->tid_offset = next_offset;
2399*4882a593Smuzhiyun 	}
2400*4882a593Smuzhiyun 
2401*4882a593Smuzhiyun 	hdwords = sizeof(ohdr->u.tid_rdma.r_rsp) / sizeof(u32);
2402*4882a593Smuzhiyun 
2403*4882a593Smuzhiyun done:
2404*4882a593Smuzhiyun 	return hdwords;
2405*4882a593Smuzhiyun }
2406*4882a593Smuzhiyun 
2407*4882a593Smuzhiyun static inline struct tid_rdma_request *
find_tid_request(struct rvt_qp * qp,u32 psn,enum ib_wr_opcode opcode)2408*4882a593Smuzhiyun find_tid_request(struct rvt_qp *qp, u32 psn, enum ib_wr_opcode opcode)
2409*4882a593Smuzhiyun 	__must_hold(&qp->s_lock)
2410*4882a593Smuzhiyun {
2411*4882a593Smuzhiyun 	struct rvt_swqe *wqe;
2412*4882a593Smuzhiyun 	struct tid_rdma_request *req = NULL;
2413*4882a593Smuzhiyun 	u32 i, end;
2414*4882a593Smuzhiyun 
2415*4882a593Smuzhiyun 	end = qp->s_cur + 1;
2416*4882a593Smuzhiyun 	if (end == qp->s_size)
2417*4882a593Smuzhiyun 		end = 0;
2418*4882a593Smuzhiyun 	for (i = qp->s_acked; i != end;) {
2419*4882a593Smuzhiyun 		wqe = rvt_get_swqe_ptr(qp, i);
2420*4882a593Smuzhiyun 		if (cmp_psn(psn, wqe->psn) >= 0 &&
2421*4882a593Smuzhiyun 		    cmp_psn(psn, wqe->lpsn) <= 0) {
2422*4882a593Smuzhiyun 			if (wqe->wr.opcode == opcode)
2423*4882a593Smuzhiyun 				req = wqe_to_tid_req(wqe);
2424*4882a593Smuzhiyun 			break;
2425*4882a593Smuzhiyun 		}
2426*4882a593Smuzhiyun 		if (++i == qp->s_size)
2427*4882a593Smuzhiyun 			i = 0;
2428*4882a593Smuzhiyun 	}
2429*4882a593Smuzhiyun 
2430*4882a593Smuzhiyun 	return req;
2431*4882a593Smuzhiyun }
2432*4882a593Smuzhiyun 
hfi1_rc_rcv_tid_rdma_read_resp(struct hfi1_packet * packet)2433*4882a593Smuzhiyun void hfi1_rc_rcv_tid_rdma_read_resp(struct hfi1_packet *packet)
2434*4882a593Smuzhiyun {
2435*4882a593Smuzhiyun 	/* HANDLER FOR TID RDMA READ RESPONSE packet (Requestor side */
2436*4882a593Smuzhiyun 
2437*4882a593Smuzhiyun 	/*
2438*4882a593Smuzhiyun 	 * 1. Find matching SWQE
2439*4882a593Smuzhiyun 	 * 2. Check that the entire segment has been read.
2440*4882a593Smuzhiyun 	 * 3. Remove HFI1_S_WAIT_TID_RESP from s_flags.
2441*4882a593Smuzhiyun 	 * 4. Free the TID flow resources.
2442*4882a593Smuzhiyun 	 * 5. Kick the send engine (hfi1_schedule_send())
2443*4882a593Smuzhiyun 	 */
2444*4882a593Smuzhiyun 	struct ib_other_headers *ohdr = packet->ohdr;
2445*4882a593Smuzhiyun 	struct rvt_qp *qp = packet->qp;
2446*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
2447*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = packet->rcd;
2448*4882a593Smuzhiyun 	struct tid_rdma_request *req;
2449*4882a593Smuzhiyun 	struct tid_rdma_flow *flow;
2450*4882a593Smuzhiyun 	u32 opcode, aeth;
2451*4882a593Smuzhiyun 	bool fecn;
2452*4882a593Smuzhiyun 	unsigned long flags;
2453*4882a593Smuzhiyun 	u32 kpsn, ipsn;
2454*4882a593Smuzhiyun 
2455*4882a593Smuzhiyun 	trace_hfi1_sender_rcv_tid_read_resp(qp);
2456*4882a593Smuzhiyun 	fecn = process_ecn(qp, packet);
2457*4882a593Smuzhiyun 	kpsn = mask_psn(be32_to_cpu(ohdr->bth[2]));
2458*4882a593Smuzhiyun 	aeth = be32_to_cpu(ohdr->u.tid_rdma.r_rsp.aeth);
2459*4882a593Smuzhiyun 	opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0xff;
2460*4882a593Smuzhiyun 
2461*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->s_lock, flags);
2462*4882a593Smuzhiyun 	ipsn = mask_psn(be32_to_cpu(ohdr->u.tid_rdma.r_rsp.verbs_psn));
2463*4882a593Smuzhiyun 	req = find_tid_request(qp, ipsn, IB_WR_TID_RDMA_READ);
2464*4882a593Smuzhiyun 	if (unlikely(!req))
2465*4882a593Smuzhiyun 		goto ack_op_err;
2466*4882a593Smuzhiyun 
2467*4882a593Smuzhiyun 	flow = &req->flows[req->clear_tail];
2468*4882a593Smuzhiyun 	/* When header suppression is disabled */
2469*4882a593Smuzhiyun 	if (cmp_psn(ipsn, flow->flow_state.ib_lpsn)) {
2470*4882a593Smuzhiyun 		update_r_next_psn_fecn(packet, priv, rcd, flow, fecn);
2471*4882a593Smuzhiyun 
2472*4882a593Smuzhiyun 		if (cmp_psn(kpsn, flow->flow_state.r_next_psn))
2473*4882a593Smuzhiyun 			goto ack_done;
2474*4882a593Smuzhiyun 		flow->flow_state.r_next_psn = mask_psn(kpsn + 1);
2475*4882a593Smuzhiyun 		/*
2476*4882a593Smuzhiyun 		 * Copy the payload to destination buffer if this packet is
2477*4882a593Smuzhiyun 		 * delivered as an eager packet due to RSM rule and FECN.
2478*4882a593Smuzhiyun 		 * The RSM rule selects FECN bit in BTH and SH bit in
2479*4882a593Smuzhiyun 		 * KDETH header and therefore will not match the last
2480*4882a593Smuzhiyun 		 * packet of each segment that has SH bit cleared.
2481*4882a593Smuzhiyun 		 */
2482*4882a593Smuzhiyun 		if (fecn && packet->etype == RHF_RCV_TYPE_EAGER) {
2483*4882a593Smuzhiyun 			struct rvt_sge_state ss;
2484*4882a593Smuzhiyun 			u32 len;
2485*4882a593Smuzhiyun 			u32 tlen = packet->tlen;
2486*4882a593Smuzhiyun 			u16 hdrsize = packet->hlen;
2487*4882a593Smuzhiyun 			u8 pad = packet->pad;
2488*4882a593Smuzhiyun 			u8 extra_bytes = pad + packet->extra_byte +
2489*4882a593Smuzhiyun 				(SIZE_OF_CRC << 2);
2490*4882a593Smuzhiyun 			u32 pmtu = qp->pmtu;
2491*4882a593Smuzhiyun 
2492*4882a593Smuzhiyun 			if (unlikely(tlen != (hdrsize + pmtu + extra_bytes)))
2493*4882a593Smuzhiyun 				goto ack_op_err;
2494*4882a593Smuzhiyun 			len = restart_sge(&ss, req->e.swqe, ipsn, pmtu);
2495*4882a593Smuzhiyun 			if (unlikely(len < pmtu))
2496*4882a593Smuzhiyun 				goto ack_op_err;
2497*4882a593Smuzhiyun 			rvt_copy_sge(qp, &ss, packet->payload, pmtu, false,
2498*4882a593Smuzhiyun 				     false);
2499*4882a593Smuzhiyun 			/* Raise the sw sequence check flag for next packet */
2500*4882a593Smuzhiyun 			priv->s_flags |= HFI1_R_TID_SW_PSN;
2501*4882a593Smuzhiyun 		}
2502*4882a593Smuzhiyun 
2503*4882a593Smuzhiyun 		goto ack_done;
2504*4882a593Smuzhiyun 	}
2505*4882a593Smuzhiyun 	flow->flow_state.r_next_psn = mask_psn(kpsn + 1);
2506*4882a593Smuzhiyun 	req->ack_pending--;
2507*4882a593Smuzhiyun 	priv->pending_tid_r_segs--;
2508*4882a593Smuzhiyun 	qp->s_num_rd_atomic--;
2509*4882a593Smuzhiyun 	if ((qp->s_flags & RVT_S_WAIT_FENCE) &&
2510*4882a593Smuzhiyun 	    !qp->s_num_rd_atomic) {
2511*4882a593Smuzhiyun 		qp->s_flags &= ~(RVT_S_WAIT_FENCE |
2512*4882a593Smuzhiyun 				 RVT_S_WAIT_ACK);
2513*4882a593Smuzhiyun 		hfi1_schedule_send(qp);
2514*4882a593Smuzhiyun 	}
2515*4882a593Smuzhiyun 	if (qp->s_flags & RVT_S_WAIT_RDMAR) {
2516*4882a593Smuzhiyun 		qp->s_flags &= ~(RVT_S_WAIT_RDMAR | RVT_S_WAIT_ACK);
2517*4882a593Smuzhiyun 		hfi1_schedule_send(qp);
2518*4882a593Smuzhiyun 	}
2519*4882a593Smuzhiyun 
2520*4882a593Smuzhiyun 	trace_hfi1_ack(qp, ipsn);
2521*4882a593Smuzhiyun 	trace_hfi1_tid_req_rcv_read_resp(qp, 0, req->e.swqe->wr.opcode,
2522*4882a593Smuzhiyun 					 req->e.swqe->psn, req->e.swqe->lpsn,
2523*4882a593Smuzhiyun 					 req);
2524*4882a593Smuzhiyun 	trace_hfi1_tid_flow_rcv_read_resp(qp, req->clear_tail, flow);
2525*4882a593Smuzhiyun 
2526*4882a593Smuzhiyun 	/* Release the tid resources */
2527*4882a593Smuzhiyun 	hfi1_kern_exp_rcv_clear(req);
2528*4882a593Smuzhiyun 
2529*4882a593Smuzhiyun 	if (!do_rc_ack(qp, aeth, ipsn, opcode, 0, rcd))
2530*4882a593Smuzhiyun 		goto ack_done;
2531*4882a593Smuzhiyun 
2532*4882a593Smuzhiyun 	/* If not done yet, build next read request */
2533*4882a593Smuzhiyun 	if (++req->comp_seg >= req->total_segs) {
2534*4882a593Smuzhiyun 		priv->tid_r_comp++;
2535*4882a593Smuzhiyun 		req->state = TID_REQUEST_COMPLETE;
2536*4882a593Smuzhiyun 	}
2537*4882a593Smuzhiyun 
2538*4882a593Smuzhiyun 	/*
2539*4882a593Smuzhiyun 	 * Clear the hw flow under two conditions:
2540*4882a593Smuzhiyun 	 * 1. This request is a sync point and it is complete;
2541*4882a593Smuzhiyun 	 * 2. Current request is completed and there are no more requests.
2542*4882a593Smuzhiyun 	 */
2543*4882a593Smuzhiyun 	if ((req->state == TID_REQUEST_SYNC &&
2544*4882a593Smuzhiyun 	     req->comp_seg == req->cur_seg) ||
2545*4882a593Smuzhiyun 	    priv->tid_r_comp == priv->tid_r_reqs) {
2546*4882a593Smuzhiyun 		hfi1_kern_clear_hw_flow(priv->rcd, qp);
2547*4882a593Smuzhiyun 		priv->s_flags &= ~HFI1_R_TID_SW_PSN;
2548*4882a593Smuzhiyun 		if (req->state == TID_REQUEST_SYNC)
2549*4882a593Smuzhiyun 			req->state = TID_REQUEST_ACTIVE;
2550*4882a593Smuzhiyun 	}
2551*4882a593Smuzhiyun 
2552*4882a593Smuzhiyun 	hfi1_schedule_send(qp);
2553*4882a593Smuzhiyun 	goto ack_done;
2554*4882a593Smuzhiyun 
2555*4882a593Smuzhiyun ack_op_err:
2556*4882a593Smuzhiyun 	/*
2557*4882a593Smuzhiyun 	 * The test indicates that the send engine has finished its cleanup
2558*4882a593Smuzhiyun 	 * after sending the request and it's now safe to put the QP into error
2559*4882a593Smuzhiyun 	 * state. However, if the wqe queue is empty (qp->s_acked == qp->s_tail
2560*4882a593Smuzhiyun 	 * == qp->s_head), it would be unsafe to complete the wqe pointed by
2561*4882a593Smuzhiyun 	 * qp->s_acked here. Putting the qp into error state will safely flush
2562*4882a593Smuzhiyun 	 * all remaining requests.
2563*4882a593Smuzhiyun 	 */
2564*4882a593Smuzhiyun 	if (qp->s_last == qp->s_acked)
2565*4882a593Smuzhiyun 		rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
2566*4882a593Smuzhiyun 
2567*4882a593Smuzhiyun ack_done:
2568*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, flags);
2569*4882a593Smuzhiyun }
2570*4882a593Smuzhiyun 
hfi1_kern_read_tid_flow_free(struct rvt_qp * qp)2571*4882a593Smuzhiyun void hfi1_kern_read_tid_flow_free(struct rvt_qp *qp)
2572*4882a593Smuzhiyun 	__must_hold(&qp->s_lock)
2573*4882a593Smuzhiyun {
2574*4882a593Smuzhiyun 	u32 n = qp->s_acked;
2575*4882a593Smuzhiyun 	struct rvt_swqe *wqe;
2576*4882a593Smuzhiyun 	struct tid_rdma_request *req;
2577*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
2578*4882a593Smuzhiyun 
2579*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
2580*4882a593Smuzhiyun 	/* Free any TID entries */
2581*4882a593Smuzhiyun 	while (n != qp->s_tail) {
2582*4882a593Smuzhiyun 		wqe = rvt_get_swqe_ptr(qp, n);
2583*4882a593Smuzhiyun 		if (wqe->wr.opcode == IB_WR_TID_RDMA_READ) {
2584*4882a593Smuzhiyun 			req = wqe_to_tid_req(wqe);
2585*4882a593Smuzhiyun 			hfi1_kern_exp_rcv_clear_all(req);
2586*4882a593Smuzhiyun 		}
2587*4882a593Smuzhiyun 
2588*4882a593Smuzhiyun 		if (++n == qp->s_size)
2589*4882a593Smuzhiyun 			n = 0;
2590*4882a593Smuzhiyun 	}
2591*4882a593Smuzhiyun 	/* Free flow */
2592*4882a593Smuzhiyun 	hfi1_kern_clear_hw_flow(priv->rcd, qp);
2593*4882a593Smuzhiyun }
2594*4882a593Smuzhiyun 
tid_rdma_tid_err(struct hfi1_packet * packet,u8 rcv_type)2595*4882a593Smuzhiyun static bool tid_rdma_tid_err(struct hfi1_packet *packet, u8 rcv_type)
2596*4882a593Smuzhiyun {
2597*4882a593Smuzhiyun 	struct rvt_qp *qp = packet->qp;
2598*4882a593Smuzhiyun 
2599*4882a593Smuzhiyun 	if (rcv_type >= RHF_RCV_TYPE_IB)
2600*4882a593Smuzhiyun 		goto done;
2601*4882a593Smuzhiyun 
2602*4882a593Smuzhiyun 	spin_lock(&qp->s_lock);
2603*4882a593Smuzhiyun 
2604*4882a593Smuzhiyun 	/*
2605*4882a593Smuzhiyun 	 * We've ran out of space in the eager buffer.
2606*4882a593Smuzhiyun 	 * Eagerly received KDETH packets which require space in the
2607*4882a593Smuzhiyun 	 * Eager buffer (packet that have payload) are TID RDMA WRITE
2608*4882a593Smuzhiyun 	 * response packets. In this case, we have to re-transmit the
2609*4882a593Smuzhiyun 	 * TID RDMA WRITE request.
2610*4882a593Smuzhiyun 	 */
2611*4882a593Smuzhiyun 	if (rcv_type == RHF_RCV_TYPE_EAGER) {
2612*4882a593Smuzhiyun 		hfi1_restart_rc(qp, qp->s_last_psn + 1, 1);
2613*4882a593Smuzhiyun 		hfi1_schedule_send(qp);
2614*4882a593Smuzhiyun 	}
2615*4882a593Smuzhiyun 
2616*4882a593Smuzhiyun 	/* Since no payload is delivered, just drop the packet */
2617*4882a593Smuzhiyun 	spin_unlock(&qp->s_lock);
2618*4882a593Smuzhiyun done:
2619*4882a593Smuzhiyun 	return true;
2620*4882a593Smuzhiyun }
2621*4882a593Smuzhiyun 
restart_tid_rdma_read_req(struct hfi1_ctxtdata * rcd,struct rvt_qp * qp,struct rvt_swqe * wqe)2622*4882a593Smuzhiyun static void restart_tid_rdma_read_req(struct hfi1_ctxtdata *rcd,
2623*4882a593Smuzhiyun 				      struct rvt_qp *qp, struct rvt_swqe *wqe)
2624*4882a593Smuzhiyun {
2625*4882a593Smuzhiyun 	struct tid_rdma_request *req;
2626*4882a593Smuzhiyun 	struct tid_rdma_flow *flow;
2627*4882a593Smuzhiyun 
2628*4882a593Smuzhiyun 	/* Start from the right segment */
2629*4882a593Smuzhiyun 	qp->r_flags |= RVT_R_RDMAR_SEQ;
2630*4882a593Smuzhiyun 	req = wqe_to_tid_req(wqe);
2631*4882a593Smuzhiyun 	flow = &req->flows[req->clear_tail];
2632*4882a593Smuzhiyun 	hfi1_restart_rc(qp, flow->flow_state.ib_spsn, 0);
2633*4882a593Smuzhiyun 	if (list_empty(&qp->rspwait)) {
2634*4882a593Smuzhiyun 		qp->r_flags |= RVT_R_RSP_SEND;
2635*4882a593Smuzhiyun 		rvt_get_qp(qp);
2636*4882a593Smuzhiyun 		list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
2637*4882a593Smuzhiyun 	}
2638*4882a593Smuzhiyun }
2639*4882a593Smuzhiyun 
2640*4882a593Smuzhiyun /*
2641*4882a593Smuzhiyun  * Handle the KDETH eflags for TID RDMA READ response.
2642*4882a593Smuzhiyun  *
2643*4882a593Smuzhiyun  * Return true if the last packet for a segment has been received and it is
2644*4882a593Smuzhiyun  * time to process the response normally; otherwise, return true.
2645*4882a593Smuzhiyun  *
2646*4882a593Smuzhiyun  * The caller must hold the packet->qp->r_lock and the rcu_read_lock.
2647*4882a593Smuzhiyun  */
handle_read_kdeth_eflags(struct hfi1_ctxtdata * rcd,struct hfi1_packet * packet,u8 rcv_type,u8 rte,u32 psn,u32 ibpsn)2648*4882a593Smuzhiyun static bool handle_read_kdeth_eflags(struct hfi1_ctxtdata *rcd,
2649*4882a593Smuzhiyun 				     struct hfi1_packet *packet, u8 rcv_type,
2650*4882a593Smuzhiyun 				     u8 rte, u32 psn, u32 ibpsn)
2651*4882a593Smuzhiyun 	__must_hold(&packet->qp->r_lock) __must_hold(RCU)
2652*4882a593Smuzhiyun {
2653*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd = rcd->ppd;
2654*4882a593Smuzhiyun 	struct hfi1_devdata *dd = ppd->dd;
2655*4882a593Smuzhiyun 	struct hfi1_ibport *ibp;
2656*4882a593Smuzhiyun 	struct rvt_swqe *wqe;
2657*4882a593Smuzhiyun 	struct tid_rdma_request *req;
2658*4882a593Smuzhiyun 	struct tid_rdma_flow *flow;
2659*4882a593Smuzhiyun 	u32 ack_psn;
2660*4882a593Smuzhiyun 	struct rvt_qp *qp = packet->qp;
2661*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
2662*4882a593Smuzhiyun 	bool ret = true;
2663*4882a593Smuzhiyun 	int diff = 0;
2664*4882a593Smuzhiyun 	u32 fpsn;
2665*4882a593Smuzhiyun 
2666*4882a593Smuzhiyun 	lockdep_assert_held(&qp->r_lock);
2667*4882a593Smuzhiyun 	trace_hfi1_rsp_read_kdeth_eflags(qp, ibpsn);
2668*4882a593Smuzhiyun 	trace_hfi1_sender_read_kdeth_eflags(qp);
2669*4882a593Smuzhiyun 	trace_hfi1_tid_read_sender_kdeth_eflags(qp, 0);
2670*4882a593Smuzhiyun 	spin_lock(&qp->s_lock);
2671*4882a593Smuzhiyun 	/* If the psn is out of valid range, drop the packet */
2672*4882a593Smuzhiyun 	if (cmp_psn(ibpsn, qp->s_last_psn) < 0 ||
2673*4882a593Smuzhiyun 	    cmp_psn(ibpsn, qp->s_psn) > 0)
2674*4882a593Smuzhiyun 		goto s_unlock;
2675*4882a593Smuzhiyun 
2676*4882a593Smuzhiyun 	/*
2677*4882a593Smuzhiyun 	 * Note that NAKs implicitly ACK outstanding SEND and RDMA write
2678*4882a593Smuzhiyun 	 * requests and implicitly NAK RDMA read and atomic requests issued
2679*4882a593Smuzhiyun 	 * before the NAK'ed request.
2680*4882a593Smuzhiyun 	 */
2681*4882a593Smuzhiyun 	ack_psn = ibpsn - 1;
2682*4882a593Smuzhiyun 	wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
2683*4882a593Smuzhiyun 	ibp = to_iport(qp->ibqp.device, qp->port_num);
2684*4882a593Smuzhiyun 
2685*4882a593Smuzhiyun 	/* Complete WQEs that the PSN finishes. */
2686*4882a593Smuzhiyun 	while ((int)delta_psn(ack_psn, wqe->lpsn) >= 0) {
2687*4882a593Smuzhiyun 		/*
2688*4882a593Smuzhiyun 		 * If this request is a RDMA read or atomic, and the NACK is
2689*4882a593Smuzhiyun 		 * for a later operation, this NACK NAKs the RDMA read or
2690*4882a593Smuzhiyun 		 * atomic.
2691*4882a593Smuzhiyun 		 */
2692*4882a593Smuzhiyun 		if (wqe->wr.opcode == IB_WR_RDMA_READ ||
2693*4882a593Smuzhiyun 		    wqe->wr.opcode == IB_WR_TID_RDMA_READ ||
2694*4882a593Smuzhiyun 		    wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
2695*4882a593Smuzhiyun 		    wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
2696*4882a593Smuzhiyun 			/* Retry this request. */
2697*4882a593Smuzhiyun 			if (!(qp->r_flags & RVT_R_RDMAR_SEQ)) {
2698*4882a593Smuzhiyun 				qp->r_flags |= RVT_R_RDMAR_SEQ;
2699*4882a593Smuzhiyun 				if (wqe->wr.opcode == IB_WR_TID_RDMA_READ) {
2700*4882a593Smuzhiyun 					restart_tid_rdma_read_req(rcd, qp,
2701*4882a593Smuzhiyun 								  wqe);
2702*4882a593Smuzhiyun 				} else {
2703*4882a593Smuzhiyun 					hfi1_restart_rc(qp, qp->s_last_psn + 1,
2704*4882a593Smuzhiyun 							0);
2705*4882a593Smuzhiyun 					if (list_empty(&qp->rspwait)) {
2706*4882a593Smuzhiyun 						qp->r_flags |= RVT_R_RSP_SEND;
2707*4882a593Smuzhiyun 						rvt_get_qp(qp);
2708*4882a593Smuzhiyun 						list_add_tail(/* wait */
2709*4882a593Smuzhiyun 						   &qp->rspwait,
2710*4882a593Smuzhiyun 						   &rcd->qp_wait_list);
2711*4882a593Smuzhiyun 					}
2712*4882a593Smuzhiyun 				}
2713*4882a593Smuzhiyun 			}
2714*4882a593Smuzhiyun 			/*
2715*4882a593Smuzhiyun 			 * No need to process the NAK since we are
2716*4882a593Smuzhiyun 			 * restarting an earlier request.
2717*4882a593Smuzhiyun 			 */
2718*4882a593Smuzhiyun 			break;
2719*4882a593Smuzhiyun 		}
2720*4882a593Smuzhiyun 
2721*4882a593Smuzhiyun 		wqe = do_rc_completion(qp, wqe, ibp);
2722*4882a593Smuzhiyun 		if (qp->s_acked == qp->s_tail)
2723*4882a593Smuzhiyun 			goto s_unlock;
2724*4882a593Smuzhiyun 	}
2725*4882a593Smuzhiyun 
2726*4882a593Smuzhiyun 	if (qp->s_acked == qp->s_tail)
2727*4882a593Smuzhiyun 		goto s_unlock;
2728*4882a593Smuzhiyun 
2729*4882a593Smuzhiyun 	/* Handle the eflags for the request */
2730*4882a593Smuzhiyun 	if (wqe->wr.opcode != IB_WR_TID_RDMA_READ)
2731*4882a593Smuzhiyun 		goto s_unlock;
2732*4882a593Smuzhiyun 
2733*4882a593Smuzhiyun 	req = wqe_to_tid_req(wqe);
2734*4882a593Smuzhiyun 	trace_hfi1_tid_req_read_kdeth_eflags(qp, 0, wqe->wr.opcode, wqe->psn,
2735*4882a593Smuzhiyun 					     wqe->lpsn, req);
2736*4882a593Smuzhiyun 	switch (rcv_type) {
2737*4882a593Smuzhiyun 	case RHF_RCV_TYPE_EXPECTED:
2738*4882a593Smuzhiyun 		switch (rte) {
2739*4882a593Smuzhiyun 		case RHF_RTE_EXPECTED_FLOW_SEQ_ERR:
2740*4882a593Smuzhiyun 			/*
2741*4882a593Smuzhiyun 			 * On the first occurrence of a Flow Sequence error,
2742*4882a593Smuzhiyun 			 * the flag TID_FLOW_SW_PSN is set.
2743*4882a593Smuzhiyun 			 *
2744*4882a593Smuzhiyun 			 * After that, the flow is *not* reprogrammed and the
2745*4882a593Smuzhiyun 			 * protocol falls back to SW PSN checking. This is done
2746*4882a593Smuzhiyun 			 * to prevent continuous Flow Sequence errors for any
2747*4882a593Smuzhiyun 			 * packets that could be still in the fabric.
2748*4882a593Smuzhiyun 			 */
2749*4882a593Smuzhiyun 			flow = &req->flows[req->clear_tail];
2750*4882a593Smuzhiyun 			trace_hfi1_tid_flow_read_kdeth_eflags(qp,
2751*4882a593Smuzhiyun 							      req->clear_tail,
2752*4882a593Smuzhiyun 							      flow);
2753*4882a593Smuzhiyun 			if (priv->s_flags & HFI1_R_TID_SW_PSN) {
2754*4882a593Smuzhiyun 				diff = cmp_psn(psn,
2755*4882a593Smuzhiyun 					       flow->flow_state.r_next_psn);
2756*4882a593Smuzhiyun 				if (diff > 0) {
2757*4882a593Smuzhiyun 					/* Drop the packet.*/
2758*4882a593Smuzhiyun 					goto s_unlock;
2759*4882a593Smuzhiyun 				} else if (diff < 0) {
2760*4882a593Smuzhiyun 					/*
2761*4882a593Smuzhiyun 					 * If a response packet for a restarted
2762*4882a593Smuzhiyun 					 * request has come back, reset the
2763*4882a593Smuzhiyun 					 * restart flag.
2764*4882a593Smuzhiyun 					 */
2765*4882a593Smuzhiyun 					if (qp->r_flags & RVT_R_RDMAR_SEQ)
2766*4882a593Smuzhiyun 						qp->r_flags &=
2767*4882a593Smuzhiyun 							~RVT_R_RDMAR_SEQ;
2768*4882a593Smuzhiyun 
2769*4882a593Smuzhiyun 					/* Drop the packet.*/
2770*4882a593Smuzhiyun 					goto s_unlock;
2771*4882a593Smuzhiyun 				}
2772*4882a593Smuzhiyun 
2773*4882a593Smuzhiyun 				/*
2774*4882a593Smuzhiyun 				 * If SW PSN verification is successful and
2775*4882a593Smuzhiyun 				 * this is the last packet in the segment, tell
2776*4882a593Smuzhiyun 				 * the caller to process it as a normal packet.
2777*4882a593Smuzhiyun 				 */
2778*4882a593Smuzhiyun 				fpsn = full_flow_psn(flow,
2779*4882a593Smuzhiyun 						     flow->flow_state.lpsn);
2780*4882a593Smuzhiyun 				if (cmp_psn(fpsn, psn) == 0) {
2781*4882a593Smuzhiyun 					ret = false;
2782*4882a593Smuzhiyun 					if (qp->r_flags & RVT_R_RDMAR_SEQ)
2783*4882a593Smuzhiyun 						qp->r_flags &=
2784*4882a593Smuzhiyun 							~RVT_R_RDMAR_SEQ;
2785*4882a593Smuzhiyun 				}
2786*4882a593Smuzhiyun 				flow->flow_state.r_next_psn =
2787*4882a593Smuzhiyun 					mask_psn(psn + 1);
2788*4882a593Smuzhiyun 			} else {
2789*4882a593Smuzhiyun 				u32 last_psn;
2790*4882a593Smuzhiyun 
2791*4882a593Smuzhiyun 				last_psn = read_r_next_psn(dd, rcd->ctxt,
2792*4882a593Smuzhiyun 							   flow->idx);
2793*4882a593Smuzhiyun 				flow->flow_state.r_next_psn = last_psn;
2794*4882a593Smuzhiyun 				priv->s_flags |= HFI1_R_TID_SW_PSN;
2795*4882a593Smuzhiyun 				/*
2796*4882a593Smuzhiyun 				 * If no request has been restarted yet,
2797*4882a593Smuzhiyun 				 * restart the current one.
2798*4882a593Smuzhiyun 				 */
2799*4882a593Smuzhiyun 				if (!(qp->r_flags & RVT_R_RDMAR_SEQ))
2800*4882a593Smuzhiyun 					restart_tid_rdma_read_req(rcd, qp,
2801*4882a593Smuzhiyun 								  wqe);
2802*4882a593Smuzhiyun 			}
2803*4882a593Smuzhiyun 
2804*4882a593Smuzhiyun 			break;
2805*4882a593Smuzhiyun 
2806*4882a593Smuzhiyun 		case RHF_RTE_EXPECTED_FLOW_GEN_ERR:
2807*4882a593Smuzhiyun 			/*
2808*4882a593Smuzhiyun 			 * Since the TID flow is able to ride through
2809*4882a593Smuzhiyun 			 * generation mismatch, drop this stale packet.
2810*4882a593Smuzhiyun 			 */
2811*4882a593Smuzhiyun 			break;
2812*4882a593Smuzhiyun 
2813*4882a593Smuzhiyun 		default:
2814*4882a593Smuzhiyun 			break;
2815*4882a593Smuzhiyun 		}
2816*4882a593Smuzhiyun 		break;
2817*4882a593Smuzhiyun 
2818*4882a593Smuzhiyun 	case RHF_RCV_TYPE_ERROR:
2819*4882a593Smuzhiyun 		switch (rte) {
2820*4882a593Smuzhiyun 		case RHF_RTE_ERROR_OP_CODE_ERR:
2821*4882a593Smuzhiyun 		case RHF_RTE_ERROR_KHDR_MIN_LEN_ERR:
2822*4882a593Smuzhiyun 		case RHF_RTE_ERROR_KHDR_HCRC_ERR:
2823*4882a593Smuzhiyun 		case RHF_RTE_ERROR_KHDR_KVER_ERR:
2824*4882a593Smuzhiyun 		case RHF_RTE_ERROR_CONTEXT_ERR:
2825*4882a593Smuzhiyun 		case RHF_RTE_ERROR_KHDR_TID_ERR:
2826*4882a593Smuzhiyun 		default:
2827*4882a593Smuzhiyun 			break;
2828*4882a593Smuzhiyun 		}
2829*4882a593Smuzhiyun 	default:
2830*4882a593Smuzhiyun 		break;
2831*4882a593Smuzhiyun 	}
2832*4882a593Smuzhiyun s_unlock:
2833*4882a593Smuzhiyun 	spin_unlock(&qp->s_lock);
2834*4882a593Smuzhiyun 	return ret;
2835*4882a593Smuzhiyun }
2836*4882a593Smuzhiyun 
hfi1_handle_kdeth_eflags(struct hfi1_ctxtdata * rcd,struct hfi1_pportdata * ppd,struct hfi1_packet * packet)2837*4882a593Smuzhiyun bool hfi1_handle_kdeth_eflags(struct hfi1_ctxtdata *rcd,
2838*4882a593Smuzhiyun 			      struct hfi1_pportdata *ppd,
2839*4882a593Smuzhiyun 			      struct hfi1_packet *packet)
2840*4882a593Smuzhiyun {
2841*4882a593Smuzhiyun 	struct hfi1_ibport *ibp = &ppd->ibport_data;
2842*4882a593Smuzhiyun 	struct hfi1_devdata *dd = ppd->dd;
2843*4882a593Smuzhiyun 	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
2844*4882a593Smuzhiyun 	u8 rcv_type = rhf_rcv_type(packet->rhf);
2845*4882a593Smuzhiyun 	u8 rte = rhf_rcv_type_err(packet->rhf);
2846*4882a593Smuzhiyun 	struct ib_header *hdr = packet->hdr;
2847*4882a593Smuzhiyun 	struct ib_other_headers *ohdr = NULL;
2848*4882a593Smuzhiyun 	int lnh = be16_to_cpu(hdr->lrh[0]) & 3;
2849*4882a593Smuzhiyun 	u16 lid  = be16_to_cpu(hdr->lrh[1]);
2850*4882a593Smuzhiyun 	u8 opcode;
2851*4882a593Smuzhiyun 	u32 qp_num, psn, ibpsn;
2852*4882a593Smuzhiyun 	struct rvt_qp *qp;
2853*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv;
2854*4882a593Smuzhiyun 	unsigned long flags;
2855*4882a593Smuzhiyun 	bool ret = true;
2856*4882a593Smuzhiyun 	struct rvt_ack_entry *e;
2857*4882a593Smuzhiyun 	struct tid_rdma_request *req;
2858*4882a593Smuzhiyun 	struct tid_rdma_flow *flow;
2859*4882a593Smuzhiyun 	int diff = 0;
2860*4882a593Smuzhiyun 
2861*4882a593Smuzhiyun 	trace_hfi1_msg_handle_kdeth_eflags(NULL, "Kdeth error: rhf ",
2862*4882a593Smuzhiyun 					   packet->rhf);
2863*4882a593Smuzhiyun 	if (packet->rhf & RHF_ICRC_ERR)
2864*4882a593Smuzhiyun 		return ret;
2865*4882a593Smuzhiyun 
2866*4882a593Smuzhiyun 	packet->ohdr = &hdr->u.oth;
2867*4882a593Smuzhiyun 	ohdr = packet->ohdr;
2868*4882a593Smuzhiyun 	trace_input_ibhdr(rcd->dd, packet, !!(rhf_dc_info(packet->rhf)));
2869*4882a593Smuzhiyun 
2870*4882a593Smuzhiyun 	/* Get the destination QP number. */
2871*4882a593Smuzhiyun 	qp_num = be32_to_cpu(ohdr->u.tid_rdma.r_rsp.verbs_qp) &
2872*4882a593Smuzhiyun 		RVT_QPN_MASK;
2873*4882a593Smuzhiyun 	if (lid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
2874*4882a593Smuzhiyun 		goto drop;
2875*4882a593Smuzhiyun 
2876*4882a593Smuzhiyun 	psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
2877*4882a593Smuzhiyun 	opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0xff;
2878*4882a593Smuzhiyun 
2879*4882a593Smuzhiyun 	rcu_read_lock();
2880*4882a593Smuzhiyun 	qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
2881*4882a593Smuzhiyun 	if (!qp)
2882*4882a593Smuzhiyun 		goto rcu_unlock;
2883*4882a593Smuzhiyun 
2884*4882a593Smuzhiyun 	packet->qp = qp;
2885*4882a593Smuzhiyun 
2886*4882a593Smuzhiyun 	/* Check for valid receive state. */
2887*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->r_lock, flags);
2888*4882a593Smuzhiyun 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
2889*4882a593Smuzhiyun 		ibp->rvp.n_pkt_drops++;
2890*4882a593Smuzhiyun 		goto r_unlock;
2891*4882a593Smuzhiyun 	}
2892*4882a593Smuzhiyun 
2893*4882a593Smuzhiyun 	if (packet->rhf & RHF_TID_ERR) {
2894*4882a593Smuzhiyun 		/* For TIDERR and RC QPs preemptively schedule a NAK */
2895*4882a593Smuzhiyun 		u32 tlen = rhf_pkt_len(packet->rhf); /* in bytes */
2896*4882a593Smuzhiyun 
2897*4882a593Smuzhiyun 		/* Sanity check packet */
2898*4882a593Smuzhiyun 		if (tlen < 24)
2899*4882a593Smuzhiyun 			goto r_unlock;
2900*4882a593Smuzhiyun 
2901*4882a593Smuzhiyun 		/*
2902*4882a593Smuzhiyun 		 * Check for GRH. We should never get packets with GRH in this
2903*4882a593Smuzhiyun 		 * path.
2904*4882a593Smuzhiyun 		 */
2905*4882a593Smuzhiyun 		if (lnh == HFI1_LRH_GRH)
2906*4882a593Smuzhiyun 			goto r_unlock;
2907*4882a593Smuzhiyun 
2908*4882a593Smuzhiyun 		if (tid_rdma_tid_err(packet, rcv_type))
2909*4882a593Smuzhiyun 			goto r_unlock;
2910*4882a593Smuzhiyun 	}
2911*4882a593Smuzhiyun 
2912*4882a593Smuzhiyun 	/* handle TID RDMA READ */
2913*4882a593Smuzhiyun 	if (opcode == TID_OP(READ_RESP)) {
2914*4882a593Smuzhiyun 		ibpsn = be32_to_cpu(ohdr->u.tid_rdma.r_rsp.verbs_psn);
2915*4882a593Smuzhiyun 		ibpsn = mask_psn(ibpsn);
2916*4882a593Smuzhiyun 		ret = handle_read_kdeth_eflags(rcd, packet, rcv_type, rte, psn,
2917*4882a593Smuzhiyun 					       ibpsn);
2918*4882a593Smuzhiyun 		goto r_unlock;
2919*4882a593Smuzhiyun 	}
2920*4882a593Smuzhiyun 
2921*4882a593Smuzhiyun 	/*
2922*4882a593Smuzhiyun 	 * qp->s_tail_ack_queue points to the rvt_ack_entry currently being
2923*4882a593Smuzhiyun 	 * processed. These a completed sequentially so we can be sure that
2924*4882a593Smuzhiyun 	 * the pointer will not change until the entire request has completed.
2925*4882a593Smuzhiyun 	 */
2926*4882a593Smuzhiyun 	spin_lock(&qp->s_lock);
2927*4882a593Smuzhiyun 	qpriv = qp->priv;
2928*4882a593Smuzhiyun 	if (qpriv->r_tid_tail == HFI1_QP_WQE_INVALID ||
2929*4882a593Smuzhiyun 	    qpriv->r_tid_tail == qpriv->r_tid_head)
2930*4882a593Smuzhiyun 		goto unlock;
2931*4882a593Smuzhiyun 	e = &qp->s_ack_queue[qpriv->r_tid_tail];
2932*4882a593Smuzhiyun 	if (e->opcode != TID_OP(WRITE_REQ))
2933*4882a593Smuzhiyun 		goto unlock;
2934*4882a593Smuzhiyun 	req = ack_to_tid_req(e);
2935*4882a593Smuzhiyun 	if (req->comp_seg == req->cur_seg)
2936*4882a593Smuzhiyun 		goto unlock;
2937*4882a593Smuzhiyun 	flow = &req->flows[req->clear_tail];
2938*4882a593Smuzhiyun 	trace_hfi1_eflags_err_write(qp, rcv_type, rte, psn);
2939*4882a593Smuzhiyun 	trace_hfi1_rsp_handle_kdeth_eflags(qp, psn);
2940*4882a593Smuzhiyun 	trace_hfi1_tid_write_rsp_handle_kdeth_eflags(qp);
2941*4882a593Smuzhiyun 	trace_hfi1_tid_req_handle_kdeth_eflags(qp, 0, e->opcode, e->psn,
2942*4882a593Smuzhiyun 					       e->lpsn, req);
2943*4882a593Smuzhiyun 	trace_hfi1_tid_flow_handle_kdeth_eflags(qp, req->clear_tail, flow);
2944*4882a593Smuzhiyun 
2945*4882a593Smuzhiyun 	switch (rcv_type) {
2946*4882a593Smuzhiyun 	case RHF_RCV_TYPE_EXPECTED:
2947*4882a593Smuzhiyun 		switch (rte) {
2948*4882a593Smuzhiyun 		case RHF_RTE_EXPECTED_FLOW_SEQ_ERR:
2949*4882a593Smuzhiyun 			if (!(qpriv->s_flags & HFI1_R_TID_SW_PSN)) {
2950*4882a593Smuzhiyun 				qpriv->s_flags |= HFI1_R_TID_SW_PSN;
2951*4882a593Smuzhiyun 				flow->flow_state.r_next_psn =
2952*4882a593Smuzhiyun 					read_r_next_psn(dd, rcd->ctxt,
2953*4882a593Smuzhiyun 							flow->idx);
2954*4882a593Smuzhiyun 				qpriv->r_next_psn_kdeth =
2955*4882a593Smuzhiyun 					flow->flow_state.r_next_psn;
2956*4882a593Smuzhiyun 				goto nak_psn;
2957*4882a593Smuzhiyun 			} else {
2958*4882a593Smuzhiyun 				/*
2959*4882a593Smuzhiyun 				 * If the received PSN does not match the next
2960*4882a593Smuzhiyun 				 * expected PSN, NAK the packet.
2961*4882a593Smuzhiyun 				 * However, only do that if we know that the a
2962*4882a593Smuzhiyun 				 * NAK has already been sent. Otherwise, this
2963*4882a593Smuzhiyun 				 * mismatch could be due to packets that were
2964*4882a593Smuzhiyun 				 * already in flight.
2965*4882a593Smuzhiyun 				 */
2966*4882a593Smuzhiyun 				diff = cmp_psn(psn,
2967*4882a593Smuzhiyun 					       flow->flow_state.r_next_psn);
2968*4882a593Smuzhiyun 				if (diff > 0)
2969*4882a593Smuzhiyun 					goto nak_psn;
2970*4882a593Smuzhiyun 				else if (diff < 0)
2971*4882a593Smuzhiyun 					break;
2972*4882a593Smuzhiyun 
2973*4882a593Smuzhiyun 				qpriv->s_nak_state = 0;
2974*4882a593Smuzhiyun 				/*
2975*4882a593Smuzhiyun 				 * If SW PSN verification is successful and this
2976*4882a593Smuzhiyun 				 * is the last packet in the segment, tell the
2977*4882a593Smuzhiyun 				 * caller to process it as a normal packet.
2978*4882a593Smuzhiyun 				 */
2979*4882a593Smuzhiyun 				if (psn == full_flow_psn(flow,
2980*4882a593Smuzhiyun 							 flow->flow_state.lpsn))
2981*4882a593Smuzhiyun 					ret = false;
2982*4882a593Smuzhiyun 				flow->flow_state.r_next_psn =
2983*4882a593Smuzhiyun 					mask_psn(psn + 1);
2984*4882a593Smuzhiyun 				qpriv->r_next_psn_kdeth =
2985*4882a593Smuzhiyun 					flow->flow_state.r_next_psn;
2986*4882a593Smuzhiyun 			}
2987*4882a593Smuzhiyun 			break;
2988*4882a593Smuzhiyun 
2989*4882a593Smuzhiyun 		case RHF_RTE_EXPECTED_FLOW_GEN_ERR:
2990*4882a593Smuzhiyun 			goto nak_psn;
2991*4882a593Smuzhiyun 
2992*4882a593Smuzhiyun 		default:
2993*4882a593Smuzhiyun 			break;
2994*4882a593Smuzhiyun 		}
2995*4882a593Smuzhiyun 		break;
2996*4882a593Smuzhiyun 
2997*4882a593Smuzhiyun 	case RHF_RCV_TYPE_ERROR:
2998*4882a593Smuzhiyun 		switch (rte) {
2999*4882a593Smuzhiyun 		case RHF_RTE_ERROR_OP_CODE_ERR:
3000*4882a593Smuzhiyun 		case RHF_RTE_ERROR_KHDR_MIN_LEN_ERR:
3001*4882a593Smuzhiyun 		case RHF_RTE_ERROR_KHDR_HCRC_ERR:
3002*4882a593Smuzhiyun 		case RHF_RTE_ERROR_KHDR_KVER_ERR:
3003*4882a593Smuzhiyun 		case RHF_RTE_ERROR_CONTEXT_ERR:
3004*4882a593Smuzhiyun 		case RHF_RTE_ERROR_KHDR_TID_ERR:
3005*4882a593Smuzhiyun 		default:
3006*4882a593Smuzhiyun 			break;
3007*4882a593Smuzhiyun 		}
3008*4882a593Smuzhiyun 	default:
3009*4882a593Smuzhiyun 		break;
3010*4882a593Smuzhiyun 	}
3011*4882a593Smuzhiyun 
3012*4882a593Smuzhiyun unlock:
3013*4882a593Smuzhiyun 	spin_unlock(&qp->s_lock);
3014*4882a593Smuzhiyun r_unlock:
3015*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->r_lock, flags);
3016*4882a593Smuzhiyun rcu_unlock:
3017*4882a593Smuzhiyun 	rcu_read_unlock();
3018*4882a593Smuzhiyun drop:
3019*4882a593Smuzhiyun 	return ret;
3020*4882a593Smuzhiyun nak_psn:
3021*4882a593Smuzhiyun 	ibp->rvp.n_rc_seqnak++;
3022*4882a593Smuzhiyun 	if (!qpriv->s_nak_state) {
3023*4882a593Smuzhiyun 		qpriv->s_nak_state = IB_NAK_PSN_ERROR;
3024*4882a593Smuzhiyun 		/* We are NAK'ing the next expected PSN */
3025*4882a593Smuzhiyun 		qpriv->s_nak_psn = mask_psn(flow->flow_state.r_next_psn);
3026*4882a593Smuzhiyun 		tid_rdma_trigger_ack(qp);
3027*4882a593Smuzhiyun 	}
3028*4882a593Smuzhiyun 	goto unlock;
3029*4882a593Smuzhiyun }
3030*4882a593Smuzhiyun 
3031*4882a593Smuzhiyun /*
3032*4882a593Smuzhiyun  * "Rewind" the TID request information.
3033*4882a593Smuzhiyun  * This means that we reset the state back to ACTIVE,
3034*4882a593Smuzhiyun  * find the proper flow, set the flow index to that flow,
3035*4882a593Smuzhiyun  * and reset the flow information.
3036*4882a593Smuzhiyun  */
hfi1_tid_rdma_restart_req(struct rvt_qp * qp,struct rvt_swqe * wqe,u32 * bth2)3037*4882a593Smuzhiyun void hfi1_tid_rdma_restart_req(struct rvt_qp *qp, struct rvt_swqe *wqe,
3038*4882a593Smuzhiyun 			       u32 *bth2)
3039*4882a593Smuzhiyun {
3040*4882a593Smuzhiyun 	struct tid_rdma_request *req = wqe_to_tid_req(wqe);
3041*4882a593Smuzhiyun 	struct tid_rdma_flow *flow;
3042*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
3043*4882a593Smuzhiyun 	int diff, delta_pkts;
3044*4882a593Smuzhiyun 	u32 tididx = 0, i;
3045*4882a593Smuzhiyun 	u16 fidx;
3046*4882a593Smuzhiyun 
3047*4882a593Smuzhiyun 	if (wqe->wr.opcode == IB_WR_TID_RDMA_READ) {
3048*4882a593Smuzhiyun 		*bth2 = mask_psn(qp->s_psn);
3049*4882a593Smuzhiyun 		flow = find_flow_ib(req, *bth2, &fidx);
3050*4882a593Smuzhiyun 		if (!flow) {
3051*4882a593Smuzhiyun 			trace_hfi1_msg_tid_restart_req(/* msg */
3052*4882a593Smuzhiyun 			   qp, "!!!!!! Could not find flow to restart: bth2 ",
3053*4882a593Smuzhiyun 			   (u64)*bth2);
3054*4882a593Smuzhiyun 			trace_hfi1_tid_req_restart_req(qp, 0, wqe->wr.opcode,
3055*4882a593Smuzhiyun 						       wqe->psn, wqe->lpsn,
3056*4882a593Smuzhiyun 						       req);
3057*4882a593Smuzhiyun 			return;
3058*4882a593Smuzhiyun 		}
3059*4882a593Smuzhiyun 	} else {
3060*4882a593Smuzhiyun 		fidx = req->acked_tail;
3061*4882a593Smuzhiyun 		flow = &req->flows[fidx];
3062*4882a593Smuzhiyun 		*bth2 = mask_psn(req->r_ack_psn);
3063*4882a593Smuzhiyun 	}
3064*4882a593Smuzhiyun 
3065*4882a593Smuzhiyun 	if (wqe->wr.opcode == IB_WR_TID_RDMA_READ)
3066*4882a593Smuzhiyun 		delta_pkts = delta_psn(*bth2, flow->flow_state.ib_spsn);
3067*4882a593Smuzhiyun 	else
3068*4882a593Smuzhiyun 		delta_pkts = delta_psn(*bth2,
3069*4882a593Smuzhiyun 				       full_flow_psn(flow,
3070*4882a593Smuzhiyun 						     flow->flow_state.spsn));
3071*4882a593Smuzhiyun 
3072*4882a593Smuzhiyun 	trace_hfi1_tid_flow_restart_req(qp, fidx, flow);
3073*4882a593Smuzhiyun 	diff = delta_pkts + flow->resync_npkts;
3074*4882a593Smuzhiyun 
3075*4882a593Smuzhiyun 	flow->sent = 0;
3076*4882a593Smuzhiyun 	flow->pkt = 0;
3077*4882a593Smuzhiyun 	flow->tid_idx = 0;
3078*4882a593Smuzhiyun 	flow->tid_offset = 0;
3079*4882a593Smuzhiyun 	if (diff) {
3080*4882a593Smuzhiyun 		for (tididx = 0; tididx < flow->tidcnt; tididx++) {
3081*4882a593Smuzhiyun 			u32 tidentry = flow->tid_entry[tididx], tidlen,
3082*4882a593Smuzhiyun 				tidnpkts, npkts;
3083*4882a593Smuzhiyun 
3084*4882a593Smuzhiyun 			flow->tid_offset = 0;
3085*4882a593Smuzhiyun 			tidlen = EXP_TID_GET(tidentry, LEN) * PAGE_SIZE;
3086*4882a593Smuzhiyun 			tidnpkts = rvt_div_round_up_mtu(qp, tidlen);
3087*4882a593Smuzhiyun 			npkts = min_t(u32, diff, tidnpkts);
3088*4882a593Smuzhiyun 			flow->pkt += npkts;
3089*4882a593Smuzhiyun 			flow->sent += (npkts == tidnpkts ? tidlen :
3090*4882a593Smuzhiyun 				       npkts * qp->pmtu);
3091*4882a593Smuzhiyun 			flow->tid_offset += npkts * qp->pmtu;
3092*4882a593Smuzhiyun 			diff -= npkts;
3093*4882a593Smuzhiyun 			if (!diff)
3094*4882a593Smuzhiyun 				break;
3095*4882a593Smuzhiyun 		}
3096*4882a593Smuzhiyun 	}
3097*4882a593Smuzhiyun 	if (wqe->wr.opcode == IB_WR_TID_RDMA_WRITE) {
3098*4882a593Smuzhiyun 		rvt_skip_sge(&qpriv->tid_ss, (req->cur_seg * req->seg_len) +
3099*4882a593Smuzhiyun 			     flow->sent, 0);
3100*4882a593Smuzhiyun 		/*
3101*4882a593Smuzhiyun 		 * Packet PSN is based on flow_state.spsn + flow->pkt. However,
3102*4882a593Smuzhiyun 		 * during a RESYNC, the generation is incremented and the
3103*4882a593Smuzhiyun 		 * sequence is reset to 0. Since we've adjusted the npkts in the
3104*4882a593Smuzhiyun 		 * flow and the SGE has been sufficiently advanced, we have to
3105*4882a593Smuzhiyun 		 * adjust flow->pkt in order to calculate the correct PSN.
3106*4882a593Smuzhiyun 		 */
3107*4882a593Smuzhiyun 		flow->pkt -= flow->resync_npkts;
3108*4882a593Smuzhiyun 	}
3109*4882a593Smuzhiyun 
3110*4882a593Smuzhiyun 	if (flow->tid_offset ==
3111*4882a593Smuzhiyun 	    EXP_TID_GET(flow->tid_entry[tididx], LEN) * PAGE_SIZE) {
3112*4882a593Smuzhiyun 		tididx++;
3113*4882a593Smuzhiyun 		flow->tid_offset = 0;
3114*4882a593Smuzhiyun 	}
3115*4882a593Smuzhiyun 	flow->tid_idx = tididx;
3116*4882a593Smuzhiyun 	if (wqe->wr.opcode == IB_WR_TID_RDMA_READ)
3117*4882a593Smuzhiyun 		/* Move flow_idx to correct index */
3118*4882a593Smuzhiyun 		req->flow_idx = fidx;
3119*4882a593Smuzhiyun 	else
3120*4882a593Smuzhiyun 		req->clear_tail = fidx;
3121*4882a593Smuzhiyun 
3122*4882a593Smuzhiyun 	trace_hfi1_tid_flow_restart_req(qp, fidx, flow);
3123*4882a593Smuzhiyun 	trace_hfi1_tid_req_restart_req(qp, 0, wqe->wr.opcode, wqe->psn,
3124*4882a593Smuzhiyun 				       wqe->lpsn, req);
3125*4882a593Smuzhiyun 	req->state = TID_REQUEST_ACTIVE;
3126*4882a593Smuzhiyun 	if (wqe->wr.opcode == IB_WR_TID_RDMA_WRITE) {
3127*4882a593Smuzhiyun 		/* Reset all the flows that we are going to resend */
3128*4882a593Smuzhiyun 		fidx = CIRC_NEXT(fidx, MAX_FLOWS);
3129*4882a593Smuzhiyun 		i = qpriv->s_tid_tail;
3130*4882a593Smuzhiyun 		do {
3131*4882a593Smuzhiyun 			for (; CIRC_CNT(req->setup_head, fidx, MAX_FLOWS);
3132*4882a593Smuzhiyun 			      fidx = CIRC_NEXT(fidx, MAX_FLOWS)) {
3133*4882a593Smuzhiyun 				req->flows[fidx].sent = 0;
3134*4882a593Smuzhiyun 				req->flows[fidx].pkt = 0;
3135*4882a593Smuzhiyun 				req->flows[fidx].tid_idx = 0;
3136*4882a593Smuzhiyun 				req->flows[fidx].tid_offset = 0;
3137*4882a593Smuzhiyun 				req->flows[fidx].resync_npkts = 0;
3138*4882a593Smuzhiyun 			}
3139*4882a593Smuzhiyun 			if (i == qpriv->s_tid_cur)
3140*4882a593Smuzhiyun 				break;
3141*4882a593Smuzhiyun 			do {
3142*4882a593Smuzhiyun 				i = (++i == qp->s_size ? 0 : i);
3143*4882a593Smuzhiyun 				wqe = rvt_get_swqe_ptr(qp, i);
3144*4882a593Smuzhiyun 			} while (wqe->wr.opcode != IB_WR_TID_RDMA_WRITE);
3145*4882a593Smuzhiyun 			req = wqe_to_tid_req(wqe);
3146*4882a593Smuzhiyun 			req->cur_seg = req->ack_seg;
3147*4882a593Smuzhiyun 			fidx = req->acked_tail;
3148*4882a593Smuzhiyun 			/* Pull req->clear_tail back */
3149*4882a593Smuzhiyun 			req->clear_tail = fidx;
3150*4882a593Smuzhiyun 		} while (1);
3151*4882a593Smuzhiyun 	}
3152*4882a593Smuzhiyun }
3153*4882a593Smuzhiyun 
hfi1_qp_kern_exp_rcv_clear_all(struct rvt_qp * qp)3154*4882a593Smuzhiyun void hfi1_qp_kern_exp_rcv_clear_all(struct rvt_qp *qp)
3155*4882a593Smuzhiyun {
3156*4882a593Smuzhiyun 	int i, ret;
3157*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
3158*4882a593Smuzhiyun 	struct tid_flow_state *fs;
3159*4882a593Smuzhiyun 
3160*4882a593Smuzhiyun 	if (qp->ibqp.qp_type != IB_QPT_RC || !HFI1_CAP_IS_KSET(TID_RDMA))
3161*4882a593Smuzhiyun 		return;
3162*4882a593Smuzhiyun 
3163*4882a593Smuzhiyun 	/*
3164*4882a593Smuzhiyun 	 * First, clear the flow to help prevent any delayed packets from
3165*4882a593Smuzhiyun 	 * being delivered.
3166*4882a593Smuzhiyun 	 */
3167*4882a593Smuzhiyun 	fs = &qpriv->flow_state;
3168*4882a593Smuzhiyun 	if (fs->index != RXE_NUM_TID_FLOWS)
3169*4882a593Smuzhiyun 		hfi1_kern_clear_hw_flow(qpriv->rcd, qp);
3170*4882a593Smuzhiyun 
3171*4882a593Smuzhiyun 	for (i = qp->s_acked; i != qp->s_head;) {
3172*4882a593Smuzhiyun 		struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, i);
3173*4882a593Smuzhiyun 
3174*4882a593Smuzhiyun 		if (++i == qp->s_size)
3175*4882a593Smuzhiyun 			i = 0;
3176*4882a593Smuzhiyun 		/* Free only locally allocated TID entries */
3177*4882a593Smuzhiyun 		if (wqe->wr.opcode != IB_WR_TID_RDMA_READ)
3178*4882a593Smuzhiyun 			continue;
3179*4882a593Smuzhiyun 		do {
3180*4882a593Smuzhiyun 			struct hfi1_swqe_priv *priv = wqe->priv;
3181*4882a593Smuzhiyun 
3182*4882a593Smuzhiyun 			ret = hfi1_kern_exp_rcv_clear(&priv->tid_req);
3183*4882a593Smuzhiyun 		} while (!ret);
3184*4882a593Smuzhiyun 	}
3185*4882a593Smuzhiyun 	for (i = qp->s_acked_ack_queue; i != qp->r_head_ack_queue;) {
3186*4882a593Smuzhiyun 		struct rvt_ack_entry *e = &qp->s_ack_queue[i];
3187*4882a593Smuzhiyun 
3188*4882a593Smuzhiyun 		if (++i == rvt_max_atomic(ib_to_rvt(qp->ibqp.device)))
3189*4882a593Smuzhiyun 			i = 0;
3190*4882a593Smuzhiyun 		/* Free only locally allocated TID entries */
3191*4882a593Smuzhiyun 		if (e->opcode != TID_OP(WRITE_REQ))
3192*4882a593Smuzhiyun 			continue;
3193*4882a593Smuzhiyun 		do {
3194*4882a593Smuzhiyun 			struct hfi1_ack_priv *priv = e->priv;
3195*4882a593Smuzhiyun 
3196*4882a593Smuzhiyun 			ret = hfi1_kern_exp_rcv_clear(&priv->tid_req);
3197*4882a593Smuzhiyun 		} while (!ret);
3198*4882a593Smuzhiyun 	}
3199*4882a593Smuzhiyun }
3200*4882a593Smuzhiyun 
hfi1_tid_rdma_wqe_interlock(struct rvt_qp * qp,struct rvt_swqe * wqe)3201*4882a593Smuzhiyun bool hfi1_tid_rdma_wqe_interlock(struct rvt_qp *qp, struct rvt_swqe *wqe)
3202*4882a593Smuzhiyun {
3203*4882a593Smuzhiyun 	struct rvt_swqe *prev;
3204*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
3205*4882a593Smuzhiyun 	u32 s_prev;
3206*4882a593Smuzhiyun 	struct tid_rdma_request *req;
3207*4882a593Smuzhiyun 
3208*4882a593Smuzhiyun 	s_prev = (qp->s_cur == 0 ? qp->s_size : qp->s_cur) - 1;
3209*4882a593Smuzhiyun 	prev = rvt_get_swqe_ptr(qp, s_prev);
3210*4882a593Smuzhiyun 
3211*4882a593Smuzhiyun 	switch (wqe->wr.opcode) {
3212*4882a593Smuzhiyun 	case IB_WR_SEND:
3213*4882a593Smuzhiyun 	case IB_WR_SEND_WITH_IMM:
3214*4882a593Smuzhiyun 	case IB_WR_SEND_WITH_INV:
3215*4882a593Smuzhiyun 	case IB_WR_ATOMIC_CMP_AND_SWP:
3216*4882a593Smuzhiyun 	case IB_WR_ATOMIC_FETCH_AND_ADD:
3217*4882a593Smuzhiyun 	case IB_WR_RDMA_WRITE:
3218*4882a593Smuzhiyun 	case IB_WR_RDMA_WRITE_WITH_IMM:
3219*4882a593Smuzhiyun 		switch (prev->wr.opcode) {
3220*4882a593Smuzhiyun 		case IB_WR_TID_RDMA_WRITE:
3221*4882a593Smuzhiyun 			req = wqe_to_tid_req(prev);
3222*4882a593Smuzhiyun 			if (req->ack_seg != req->total_segs)
3223*4882a593Smuzhiyun 				goto interlock;
3224*4882a593Smuzhiyun 		default:
3225*4882a593Smuzhiyun 			break;
3226*4882a593Smuzhiyun 		}
3227*4882a593Smuzhiyun 		break;
3228*4882a593Smuzhiyun 	case IB_WR_RDMA_READ:
3229*4882a593Smuzhiyun 		if (prev->wr.opcode != IB_WR_TID_RDMA_WRITE)
3230*4882a593Smuzhiyun 			break;
3231*4882a593Smuzhiyun 		fallthrough;
3232*4882a593Smuzhiyun 	case IB_WR_TID_RDMA_READ:
3233*4882a593Smuzhiyun 		switch (prev->wr.opcode) {
3234*4882a593Smuzhiyun 		case IB_WR_RDMA_READ:
3235*4882a593Smuzhiyun 			if (qp->s_acked != qp->s_cur)
3236*4882a593Smuzhiyun 				goto interlock;
3237*4882a593Smuzhiyun 			break;
3238*4882a593Smuzhiyun 		case IB_WR_TID_RDMA_WRITE:
3239*4882a593Smuzhiyun 			req = wqe_to_tid_req(prev);
3240*4882a593Smuzhiyun 			if (req->ack_seg != req->total_segs)
3241*4882a593Smuzhiyun 				goto interlock;
3242*4882a593Smuzhiyun 		default:
3243*4882a593Smuzhiyun 			break;
3244*4882a593Smuzhiyun 		}
3245*4882a593Smuzhiyun 	default:
3246*4882a593Smuzhiyun 		break;
3247*4882a593Smuzhiyun 	}
3248*4882a593Smuzhiyun 	return false;
3249*4882a593Smuzhiyun 
3250*4882a593Smuzhiyun interlock:
3251*4882a593Smuzhiyun 	priv->s_flags |= HFI1_S_TID_WAIT_INTERLCK;
3252*4882a593Smuzhiyun 	return true;
3253*4882a593Smuzhiyun }
3254*4882a593Smuzhiyun 
3255*4882a593Smuzhiyun /* Does @sge meet the alignment requirements for tid rdma? */
hfi1_check_sge_align(struct rvt_qp * qp,struct rvt_sge * sge,int num_sge)3256*4882a593Smuzhiyun static inline bool hfi1_check_sge_align(struct rvt_qp *qp,
3257*4882a593Smuzhiyun 					struct rvt_sge *sge, int num_sge)
3258*4882a593Smuzhiyun {
3259*4882a593Smuzhiyun 	int i;
3260*4882a593Smuzhiyun 
3261*4882a593Smuzhiyun 	for (i = 0; i < num_sge; i++, sge++) {
3262*4882a593Smuzhiyun 		trace_hfi1_sge_check_align(qp, i, sge);
3263*4882a593Smuzhiyun 		if ((u64)sge->vaddr & ~PAGE_MASK ||
3264*4882a593Smuzhiyun 		    sge->sge_length & ~PAGE_MASK)
3265*4882a593Smuzhiyun 			return false;
3266*4882a593Smuzhiyun 	}
3267*4882a593Smuzhiyun 	return true;
3268*4882a593Smuzhiyun }
3269*4882a593Smuzhiyun 
setup_tid_rdma_wqe(struct rvt_qp * qp,struct rvt_swqe * wqe)3270*4882a593Smuzhiyun void setup_tid_rdma_wqe(struct rvt_qp *qp, struct rvt_swqe *wqe)
3271*4882a593Smuzhiyun {
3272*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = (struct hfi1_qp_priv *)qp->priv;
3273*4882a593Smuzhiyun 	struct hfi1_swqe_priv *priv = wqe->priv;
3274*4882a593Smuzhiyun 	struct tid_rdma_params *remote;
3275*4882a593Smuzhiyun 	enum ib_wr_opcode new_opcode;
3276*4882a593Smuzhiyun 	bool do_tid_rdma = false;
3277*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd = qpriv->rcd->ppd;
3278*4882a593Smuzhiyun 
3279*4882a593Smuzhiyun 	if ((rdma_ah_get_dlid(&qp->remote_ah_attr) & ~((1 << ppd->lmc) - 1)) ==
3280*4882a593Smuzhiyun 				ppd->lid)
3281*4882a593Smuzhiyun 		return;
3282*4882a593Smuzhiyun 	if (qpriv->hdr_type != HFI1_PKT_TYPE_9B)
3283*4882a593Smuzhiyun 		return;
3284*4882a593Smuzhiyun 
3285*4882a593Smuzhiyun 	rcu_read_lock();
3286*4882a593Smuzhiyun 	remote = rcu_dereference(qpriv->tid_rdma.remote);
3287*4882a593Smuzhiyun 	/*
3288*4882a593Smuzhiyun 	 * If TID RDMA is disabled by the negotiation, don't
3289*4882a593Smuzhiyun 	 * use it.
3290*4882a593Smuzhiyun 	 */
3291*4882a593Smuzhiyun 	if (!remote)
3292*4882a593Smuzhiyun 		goto exit;
3293*4882a593Smuzhiyun 
3294*4882a593Smuzhiyun 	if (wqe->wr.opcode == IB_WR_RDMA_READ) {
3295*4882a593Smuzhiyun 		if (hfi1_check_sge_align(qp, &wqe->sg_list[0],
3296*4882a593Smuzhiyun 					 wqe->wr.num_sge)) {
3297*4882a593Smuzhiyun 			new_opcode = IB_WR_TID_RDMA_READ;
3298*4882a593Smuzhiyun 			do_tid_rdma = true;
3299*4882a593Smuzhiyun 		}
3300*4882a593Smuzhiyun 	} else if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
3301*4882a593Smuzhiyun 		/*
3302*4882a593Smuzhiyun 		 * TID RDMA is enabled for this RDMA WRITE request iff:
3303*4882a593Smuzhiyun 		 *   1. The remote address is page-aligned,
3304*4882a593Smuzhiyun 		 *   2. The length is larger than the minimum segment size,
3305*4882a593Smuzhiyun 		 *   3. The length is page-multiple.
3306*4882a593Smuzhiyun 		 */
3307*4882a593Smuzhiyun 		if (!(wqe->rdma_wr.remote_addr & ~PAGE_MASK) &&
3308*4882a593Smuzhiyun 		    !(wqe->length & ~PAGE_MASK)) {
3309*4882a593Smuzhiyun 			new_opcode = IB_WR_TID_RDMA_WRITE;
3310*4882a593Smuzhiyun 			do_tid_rdma = true;
3311*4882a593Smuzhiyun 		}
3312*4882a593Smuzhiyun 	}
3313*4882a593Smuzhiyun 
3314*4882a593Smuzhiyun 	if (do_tid_rdma) {
3315*4882a593Smuzhiyun 		if (hfi1_kern_exp_rcv_alloc_flows(&priv->tid_req, GFP_ATOMIC))
3316*4882a593Smuzhiyun 			goto exit;
3317*4882a593Smuzhiyun 		wqe->wr.opcode = new_opcode;
3318*4882a593Smuzhiyun 		priv->tid_req.seg_len =
3319*4882a593Smuzhiyun 			min_t(u32, remote->max_len, wqe->length);
3320*4882a593Smuzhiyun 		priv->tid_req.total_segs =
3321*4882a593Smuzhiyun 			DIV_ROUND_UP(wqe->length, priv->tid_req.seg_len);
3322*4882a593Smuzhiyun 		/* Compute the last PSN of the request */
3323*4882a593Smuzhiyun 		wqe->lpsn = wqe->psn;
3324*4882a593Smuzhiyun 		if (wqe->wr.opcode == IB_WR_TID_RDMA_READ) {
3325*4882a593Smuzhiyun 			priv->tid_req.n_flows = remote->max_read;
3326*4882a593Smuzhiyun 			qpriv->tid_r_reqs++;
3327*4882a593Smuzhiyun 			wqe->lpsn += rvt_div_round_up_mtu(qp, wqe->length) - 1;
3328*4882a593Smuzhiyun 		} else {
3329*4882a593Smuzhiyun 			wqe->lpsn += priv->tid_req.total_segs - 1;
3330*4882a593Smuzhiyun 			atomic_inc(&qpriv->n_requests);
3331*4882a593Smuzhiyun 		}
3332*4882a593Smuzhiyun 
3333*4882a593Smuzhiyun 		priv->tid_req.cur_seg = 0;
3334*4882a593Smuzhiyun 		priv->tid_req.comp_seg = 0;
3335*4882a593Smuzhiyun 		priv->tid_req.ack_seg = 0;
3336*4882a593Smuzhiyun 		priv->tid_req.state = TID_REQUEST_INACTIVE;
3337*4882a593Smuzhiyun 		/*
3338*4882a593Smuzhiyun 		 * Reset acked_tail.
3339*4882a593Smuzhiyun 		 * TID RDMA READ does not have ACKs so it does not
3340*4882a593Smuzhiyun 		 * update the pointer. We have to reset it so TID RDMA
3341*4882a593Smuzhiyun 		 * WRITE does not get confused.
3342*4882a593Smuzhiyun 		 */
3343*4882a593Smuzhiyun 		priv->tid_req.acked_tail = priv->tid_req.setup_head;
3344*4882a593Smuzhiyun 		trace_hfi1_tid_req_setup_tid_wqe(qp, 1, wqe->wr.opcode,
3345*4882a593Smuzhiyun 						 wqe->psn, wqe->lpsn,
3346*4882a593Smuzhiyun 						 &priv->tid_req);
3347*4882a593Smuzhiyun 	}
3348*4882a593Smuzhiyun exit:
3349*4882a593Smuzhiyun 	rcu_read_unlock();
3350*4882a593Smuzhiyun }
3351*4882a593Smuzhiyun 
3352*4882a593Smuzhiyun /* TID RDMA WRITE functions */
3353*4882a593Smuzhiyun 
hfi1_build_tid_rdma_write_req(struct rvt_qp * qp,struct rvt_swqe * wqe,struct ib_other_headers * ohdr,u32 * bth1,u32 * bth2,u32 * len)3354*4882a593Smuzhiyun u32 hfi1_build_tid_rdma_write_req(struct rvt_qp *qp, struct rvt_swqe *wqe,
3355*4882a593Smuzhiyun 				  struct ib_other_headers *ohdr,
3356*4882a593Smuzhiyun 				  u32 *bth1, u32 *bth2, u32 *len)
3357*4882a593Smuzhiyun {
3358*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
3359*4882a593Smuzhiyun 	struct tid_rdma_request *req = wqe_to_tid_req(wqe);
3360*4882a593Smuzhiyun 	struct tid_rdma_params *remote;
3361*4882a593Smuzhiyun 
3362*4882a593Smuzhiyun 	rcu_read_lock();
3363*4882a593Smuzhiyun 	remote = rcu_dereference(qpriv->tid_rdma.remote);
3364*4882a593Smuzhiyun 	/*
3365*4882a593Smuzhiyun 	 * Set the number of flow to be used based on negotiated
3366*4882a593Smuzhiyun 	 * parameters.
3367*4882a593Smuzhiyun 	 */
3368*4882a593Smuzhiyun 	req->n_flows = remote->max_write;
3369*4882a593Smuzhiyun 	req->state = TID_REQUEST_ACTIVE;
3370*4882a593Smuzhiyun 
3371*4882a593Smuzhiyun 	KDETH_RESET(ohdr->u.tid_rdma.w_req.kdeth0, KVER, 0x1);
3372*4882a593Smuzhiyun 	KDETH_RESET(ohdr->u.tid_rdma.w_req.kdeth1, JKEY, remote->jkey);
3373*4882a593Smuzhiyun 	ohdr->u.tid_rdma.w_req.reth.vaddr =
3374*4882a593Smuzhiyun 		cpu_to_be64(wqe->rdma_wr.remote_addr + (wqe->length - *len));
3375*4882a593Smuzhiyun 	ohdr->u.tid_rdma.w_req.reth.rkey =
3376*4882a593Smuzhiyun 		cpu_to_be32(wqe->rdma_wr.rkey);
3377*4882a593Smuzhiyun 	ohdr->u.tid_rdma.w_req.reth.length = cpu_to_be32(*len);
3378*4882a593Smuzhiyun 	ohdr->u.tid_rdma.w_req.verbs_qp = cpu_to_be32(qp->remote_qpn);
3379*4882a593Smuzhiyun 	*bth1 &= ~RVT_QPN_MASK;
3380*4882a593Smuzhiyun 	*bth1 |= remote->qp;
3381*4882a593Smuzhiyun 	qp->s_state = TID_OP(WRITE_REQ);
3382*4882a593Smuzhiyun 	qp->s_flags |= HFI1_S_WAIT_TID_RESP;
3383*4882a593Smuzhiyun 	*bth2 |= IB_BTH_REQ_ACK;
3384*4882a593Smuzhiyun 	*len = 0;
3385*4882a593Smuzhiyun 
3386*4882a593Smuzhiyun 	rcu_read_unlock();
3387*4882a593Smuzhiyun 	return sizeof(ohdr->u.tid_rdma.w_req) / sizeof(u32);
3388*4882a593Smuzhiyun }
3389*4882a593Smuzhiyun 
hfi1_compute_tid_rdma_flow_wt(struct rvt_qp * qp)3390*4882a593Smuzhiyun static u32 hfi1_compute_tid_rdma_flow_wt(struct rvt_qp *qp)
3391*4882a593Smuzhiyun {
3392*4882a593Smuzhiyun 	/*
3393*4882a593Smuzhiyun 	 * Heuristic for computing the RNR timeout when waiting on the flow
3394*4882a593Smuzhiyun 	 * queue. Rather than a computationaly expensive exact estimate of when
3395*4882a593Smuzhiyun 	 * a flow will be available, we assume that if a QP is at position N in
3396*4882a593Smuzhiyun 	 * the flow queue it has to wait approximately (N + 1) * (number of
3397*4882a593Smuzhiyun 	 * segments between two sync points). The rationale for this is that
3398*4882a593Smuzhiyun 	 * flows are released and recycled at each sync point.
3399*4882a593Smuzhiyun 	 */
3400*4882a593Smuzhiyun 	return (MAX_TID_FLOW_PSN * qp->pmtu) >> TID_RDMA_SEGMENT_SHIFT;
3401*4882a593Smuzhiyun }
3402*4882a593Smuzhiyun 
position_in_queue(struct hfi1_qp_priv * qpriv,struct tid_queue * queue)3403*4882a593Smuzhiyun static u32 position_in_queue(struct hfi1_qp_priv *qpriv,
3404*4882a593Smuzhiyun 			     struct tid_queue *queue)
3405*4882a593Smuzhiyun {
3406*4882a593Smuzhiyun 	return qpriv->tid_enqueue - queue->dequeue;
3407*4882a593Smuzhiyun }
3408*4882a593Smuzhiyun 
3409*4882a593Smuzhiyun /*
3410*4882a593Smuzhiyun  * @qp: points to rvt_qp context.
3411*4882a593Smuzhiyun  * @to_seg: desired RNR timeout in segments.
3412*4882a593Smuzhiyun  * Return: index of the next highest timeout in the ib_hfi1_rnr_table[]
3413*4882a593Smuzhiyun  */
hfi1_compute_tid_rnr_timeout(struct rvt_qp * qp,u32 to_seg)3414*4882a593Smuzhiyun static u32 hfi1_compute_tid_rnr_timeout(struct rvt_qp *qp, u32 to_seg)
3415*4882a593Smuzhiyun {
3416*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
3417*4882a593Smuzhiyun 	u64 timeout;
3418*4882a593Smuzhiyun 	u32 bytes_per_us;
3419*4882a593Smuzhiyun 	u8 i;
3420*4882a593Smuzhiyun 
3421*4882a593Smuzhiyun 	bytes_per_us = active_egress_rate(qpriv->rcd->ppd) / 8;
3422*4882a593Smuzhiyun 	timeout = (to_seg * TID_RDMA_MAX_SEGMENT_SIZE) / bytes_per_us;
3423*4882a593Smuzhiyun 	/*
3424*4882a593Smuzhiyun 	 * Find the next highest value in the RNR table to the required
3425*4882a593Smuzhiyun 	 * timeout. This gives the responder some padding.
3426*4882a593Smuzhiyun 	 */
3427*4882a593Smuzhiyun 	for (i = 1; i <= IB_AETH_CREDIT_MASK; i++)
3428*4882a593Smuzhiyun 		if (rvt_rnr_tbl_to_usec(i) >= timeout)
3429*4882a593Smuzhiyun 			return i;
3430*4882a593Smuzhiyun 	return 0;
3431*4882a593Smuzhiyun }
3432*4882a593Smuzhiyun 
3433*4882a593Smuzhiyun /**
3434*4882a593Smuzhiyun  * Central place for resource allocation at TID write responder,
3435*4882a593Smuzhiyun  * is called from write_req and write_data interrupt handlers as
3436*4882a593Smuzhiyun  * well as the send thread when a queued QP is scheduled for
3437*4882a593Smuzhiyun  * resource allocation.
3438*4882a593Smuzhiyun  *
3439*4882a593Smuzhiyun  * Iterates over (a) segments of a request and then (b) queued requests
3440*4882a593Smuzhiyun  * themselves to allocate resources for up to local->max_write
3441*4882a593Smuzhiyun  * segments across multiple requests. Stop allocating when we
3442*4882a593Smuzhiyun  * hit a sync point, resume allocating after data packets at
3443*4882a593Smuzhiyun  * sync point have been received.
3444*4882a593Smuzhiyun  *
3445*4882a593Smuzhiyun  * Resource allocation and sending of responses is decoupled. The
3446*4882a593Smuzhiyun  * request/segment which are being allocated and sent are as follows.
3447*4882a593Smuzhiyun  * Resources are allocated for:
3448*4882a593Smuzhiyun  *     [request: qpriv->r_tid_alloc, segment: req->alloc_seg]
3449*4882a593Smuzhiyun  * The send thread sends:
3450*4882a593Smuzhiyun  *     [request: qp->s_tail_ack_queue, segment:req->cur_seg]
3451*4882a593Smuzhiyun  */
hfi1_tid_write_alloc_resources(struct rvt_qp * qp,bool intr_ctx)3452*4882a593Smuzhiyun static void hfi1_tid_write_alloc_resources(struct rvt_qp *qp, bool intr_ctx)
3453*4882a593Smuzhiyun {
3454*4882a593Smuzhiyun 	struct tid_rdma_request *req;
3455*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
3456*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = qpriv->rcd;
3457*4882a593Smuzhiyun 	struct tid_rdma_params *local = &qpriv->tid_rdma.local;
3458*4882a593Smuzhiyun 	struct rvt_ack_entry *e;
3459*4882a593Smuzhiyun 	u32 npkts, to_seg;
3460*4882a593Smuzhiyun 	bool last;
3461*4882a593Smuzhiyun 	int ret = 0;
3462*4882a593Smuzhiyun 
3463*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
3464*4882a593Smuzhiyun 
3465*4882a593Smuzhiyun 	while (1) {
3466*4882a593Smuzhiyun 		trace_hfi1_rsp_tid_write_alloc_res(qp, 0);
3467*4882a593Smuzhiyun 		trace_hfi1_tid_write_rsp_alloc_res(qp);
3468*4882a593Smuzhiyun 		/*
3469*4882a593Smuzhiyun 		 * Don't allocate more segments if a RNR NAK has already been
3470*4882a593Smuzhiyun 		 * scheduled to avoid messing up qp->r_psn: the RNR NAK will
3471*4882a593Smuzhiyun 		 * be sent only when all allocated segments have been sent.
3472*4882a593Smuzhiyun 		 * However, if more segments are allocated before that, TID RDMA
3473*4882a593Smuzhiyun 		 * WRITE RESP packets will be sent out for these new segments
3474*4882a593Smuzhiyun 		 * before the RNR NAK packet. When the requester receives the
3475*4882a593Smuzhiyun 		 * RNR NAK packet, it will restart with qp->s_last_psn + 1,
3476*4882a593Smuzhiyun 		 * which does not match qp->r_psn and will be dropped.
3477*4882a593Smuzhiyun 		 * Consequently, the requester will exhaust its retries and
3478*4882a593Smuzhiyun 		 * put the qp into error state.
3479*4882a593Smuzhiyun 		 */
3480*4882a593Smuzhiyun 		if (qpriv->rnr_nak_state == TID_RNR_NAK_SEND)
3481*4882a593Smuzhiyun 			break;
3482*4882a593Smuzhiyun 
3483*4882a593Smuzhiyun 		/* No requests left to process */
3484*4882a593Smuzhiyun 		if (qpriv->r_tid_alloc == qpriv->r_tid_head) {
3485*4882a593Smuzhiyun 			/* If all data has been received, clear the flow */
3486*4882a593Smuzhiyun 			if (qpriv->flow_state.index < RXE_NUM_TID_FLOWS &&
3487*4882a593Smuzhiyun 			    !qpriv->alloc_w_segs) {
3488*4882a593Smuzhiyun 				hfi1_kern_clear_hw_flow(rcd, qp);
3489*4882a593Smuzhiyun 				qpriv->s_flags &= ~HFI1_R_TID_SW_PSN;
3490*4882a593Smuzhiyun 			}
3491*4882a593Smuzhiyun 			break;
3492*4882a593Smuzhiyun 		}
3493*4882a593Smuzhiyun 
3494*4882a593Smuzhiyun 		e = &qp->s_ack_queue[qpriv->r_tid_alloc];
3495*4882a593Smuzhiyun 		if (e->opcode != TID_OP(WRITE_REQ))
3496*4882a593Smuzhiyun 			goto next_req;
3497*4882a593Smuzhiyun 		req = ack_to_tid_req(e);
3498*4882a593Smuzhiyun 		trace_hfi1_tid_req_write_alloc_res(qp, 0, e->opcode, e->psn,
3499*4882a593Smuzhiyun 						   e->lpsn, req);
3500*4882a593Smuzhiyun 		/* Finished allocating for all segments of this request */
3501*4882a593Smuzhiyun 		if (req->alloc_seg >= req->total_segs)
3502*4882a593Smuzhiyun 			goto next_req;
3503*4882a593Smuzhiyun 
3504*4882a593Smuzhiyun 		/* Can allocate only a maximum of local->max_write for a QP */
3505*4882a593Smuzhiyun 		if (qpriv->alloc_w_segs >= local->max_write)
3506*4882a593Smuzhiyun 			break;
3507*4882a593Smuzhiyun 
3508*4882a593Smuzhiyun 		/* Don't allocate at a sync point with data packets pending */
3509*4882a593Smuzhiyun 		if (qpriv->sync_pt && qpriv->alloc_w_segs)
3510*4882a593Smuzhiyun 			break;
3511*4882a593Smuzhiyun 
3512*4882a593Smuzhiyun 		/* All data received at the sync point, continue */
3513*4882a593Smuzhiyun 		if (qpriv->sync_pt && !qpriv->alloc_w_segs) {
3514*4882a593Smuzhiyun 			hfi1_kern_clear_hw_flow(rcd, qp);
3515*4882a593Smuzhiyun 			qpriv->sync_pt = false;
3516*4882a593Smuzhiyun 			qpriv->s_flags &= ~HFI1_R_TID_SW_PSN;
3517*4882a593Smuzhiyun 		}
3518*4882a593Smuzhiyun 
3519*4882a593Smuzhiyun 		/* Allocate flow if we don't have one */
3520*4882a593Smuzhiyun 		if (qpriv->flow_state.index >= RXE_NUM_TID_FLOWS) {
3521*4882a593Smuzhiyun 			ret = hfi1_kern_setup_hw_flow(qpriv->rcd, qp);
3522*4882a593Smuzhiyun 			if (ret) {
3523*4882a593Smuzhiyun 				to_seg = hfi1_compute_tid_rdma_flow_wt(qp) *
3524*4882a593Smuzhiyun 					position_in_queue(qpriv,
3525*4882a593Smuzhiyun 							  &rcd->flow_queue);
3526*4882a593Smuzhiyun 				break;
3527*4882a593Smuzhiyun 			}
3528*4882a593Smuzhiyun 		}
3529*4882a593Smuzhiyun 
3530*4882a593Smuzhiyun 		npkts = rvt_div_round_up_mtu(qp, req->seg_len);
3531*4882a593Smuzhiyun 
3532*4882a593Smuzhiyun 		/*
3533*4882a593Smuzhiyun 		 * We are at a sync point if we run out of KDETH PSN space.
3534*4882a593Smuzhiyun 		 * Last PSN of every generation is reserved for RESYNC.
3535*4882a593Smuzhiyun 		 */
3536*4882a593Smuzhiyun 		if (qpriv->flow_state.psn + npkts > MAX_TID_FLOW_PSN - 1) {
3537*4882a593Smuzhiyun 			qpriv->sync_pt = true;
3538*4882a593Smuzhiyun 			break;
3539*4882a593Smuzhiyun 		}
3540*4882a593Smuzhiyun 
3541*4882a593Smuzhiyun 		/*
3542*4882a593Smuzhiyun 		 * If overtaking req->acked_tail, send an RNR NAK. Because the
3543*4882a593Smuzhiyun 		 * QP is not queued in this case, and the issue can only be
3544*4882a593Smuzhiyun 		 * caused by a delay in scheduling the second leg which we
3545*4882a593Smuzhiyun 		 * cannot estimate, we use a rather arbitrary RNR timeout of
3546*4882a593Smuzhiyun 		 * (MAX_FLOWS / 2) segments
3547*4882a593Smuzhiyun 		 */
3548*4882a593Smuzhiyun 		if (!CIRC_SPACE(req->setup_head, req->acked_tail,
3549*4882a593Smuzhiyun 				MAX_FLOWS)) {
3550*4882a593Smuzhiyun 			ret = -EAGAIN;
3551*4882a593Smuzhiyun 			to_seg = MAX_FLOWS >> 1;
3552*4882a593Smuzhiyun 			tid_rdma_trigger_ack(qp);
3553*4882a593Smuzhiyun 			break;
3554*4882a593Smuzhiyun 		}
3555*4882a593Smuzhiyun 
3556*4882a593Smuzhiyun 		/* Try to allocate rcv array / TID entries */
3557*4882a593Smuzhiyun 		ret = hfi1_kern_exp_rcv_setup(req, &req->ss, &last);
3558*4882a593Smuzhiyun 		if (ret == -EAGAIN)
3559*4882a593Smuzhiyun 			to_seg = position_in_queue(qpriv, &rcd->rarr_queue);
3560*4882a593Smuzhiyun 		if (ret)
3561*4882a593Smuzhiyun 			break;
3562*4882a593Smuzhiyun 
3563*4882a593Smuzhiyun 		qpriv->alloc_w_segs++;
3564*4882a593Smuzhiyun 		req->alloc_seg++;
3565*4882a593Smuzhiyun 		continue;
3566*4882a593Smuzhiyun next_req:
3567*4882a593Smuzhiyun 		/* Begin processing the next request */
3568*4882a593Smuzhiyun 		if (++qpriv->r_tid_alloc >
3569*4882a593Smuzhiyun 		    rvt_size_atomic(ib_to_rvt(qp->ibqp.device)))
3570*4882a593Smuzhiyun 			qpriv->r_tid_alloc = 0;
3571*4882a593Smuzhiyun 	}
3572*4882a593Smuzhiyun 
3573*4882a593Smuzhiyun 	/*
3574*4882a593Smuzhiyun 	 * Schedule an RNR NAK to be sent if (a) flow or rcv array allocation
3575*4882a593Smuzhiyun 	 * has failed (b) we are called from the rcv handler interrupt context
3576*4882a593Smuzhiyun 	 * (c) an RNR NAK has not already been scheduled
3577*4882a593Smuzhiyun 	 */
3578*4882a593Smuzhiyun 	if (ret == -EAGAIN && intr_ctx && !qp->r_nak_state)
3579*4882a593Smuzhiyun 		goto send_rnr_nak;
3580*4882a593Smuzhiyun 
3581*4882a593Smuzhiyun 	return;
3582*4882a593Smuzhiyun 
3583*4882a593Smuzhiyun send_rnr_nak:
3584*4882a593Smuzhiyun 	lockdep_assert_held(&qp->r_lock);
3585*4882a593Smuzhiyun 
3586*4882a593Smuzhiyun 	/* Set r_nak_state to prevent unrelated events from generating NAK's */
3587*4882a593Smuzhiyun 	qp->r_nak_state = hfi1_compute_tid_rnr_timeout(qp, to_seg) | IB_RNR_NAK;
3588*4882a593Smuzhiyun 
3589*4882a593Smuzhiyun 	/* Pull back r_psn to the segment being RNR NAK'd */
3590*4882a593Smuzhiyun 	qp->r_psn = e->psn + req->alloc_seg;
3591*4882a593Smuzhiyun 	qp->r_ack_psn = qp->r_psn;
3592*4882a593Smuzhiyun 	/*
3593*4882a593Smuzhiyun 	 * Pull back r_head_ack_queue to the ack entry following the request
3594*4882a593Smuzhiyun 	 * being RNR NAK'd. This allows resources to be allocated to the request
3595*4882a593Smuzhiyun 	 * if the queued QP is scheduled.
3596*4882a593Smuzhiyun 	 */
3597*4882a593Smuzhiyun 	qp->r_head_ack_queue = qpriv->r_tid_alloc + 1;
3598*4882a593Smuzhiyun 	if (qp->r_head_ack_queue > rvt_size_atomic(ib_to_rvt(qp->ibqp.device)))
3599*4882a593Smuzhiyun 		qp->r_head_ack_queue = 0;
3600*4882a593Smuzhiyun 	qpriv->r_tid_head = qp->r_head_ack_queue;
3601*4882a593Smuzhiyun 	/*
3602*4882a593Smuzhiyun 	 * These send side fields are used in make_rc_ack(). They are set in
3603*4882a593Smuzhiyun 	 * hfi1_send_rc_ack() but must be set here before dropping qp->s_lock
3604*4882a593Smuzhiyun 	 * for consistency
3605*4882a593Smuzhiyun 	 */
3606*4882a593Smuzhiyun 	qp->s_nak_state = qp->r_nak_state;
3607*4882a593Smuzhiyun 	qp->s_ack_psn = qp->r_ack_psn;
3608*4882a593Smuzhiyun 	/*
3609*4882a593Smuzhiyun 	 * Clear the ACK PENDING flag to prevent unwanted ACK because we
3610*4882a593Smuzhiyun 	 * have modified qp->s_ack_psn here.
3611*4882a593Smuzhiyun 	 */
3612*4882a593Smuzhiyun 	qp->s_flags &= ~(RVT_S_ACK_PENDING);
3613*4882a593Smuzhiyun 
3614*4882a593Smuzhiyun 	trace_hfi1_rsp_tid_write_alloc_res(qp, qp->r_psn);
3615*4882a593Smuzhiyun 	/*
3616*4882a593Smuzhiyun 	 * qpriv->rnr_nak_state is used to determine when the scheduled RNR NAK
3617*4882a593Smuzhiyun 	 * has actually been sent. qp->s_flags RVT_S_ACK_PENDING bit cannot be
3618*4882a593Smuzhiyun 	 * used for this because qp->s_lock is dropped before calling
3619*4882a593Smuzhiyun 	 * hfi1_send_rc_ack() leading to inconsistency between the receive
3620*4882a593Smuzhiyun 	 * interrupt handlers and the send thread in make_rc_ack()
3621*4882a593Smuzhiyun 	 */
3622*4882a593Smuzhiyun 	qpriv->rnr_nak_state = TID_RNR_NAK_SEND;
3623*4882a593Smuzhiyun 
3624*4882a593Smuzhiyun 	/*
3625*4882a593Smuzhiyun 	 * Schedule RNR NAK to be sent. RNR NAK's are scheduled from the receive
3626*4882a593Smuzhiyun 	 * interrupt handlers but will be sent from the send engine behind any
3627*4882a593Smuzhiyun 	 * previous responses that may have been scheduled
3628*4882a593Smuzhiyun 	 */
3629*4882a593Smuzhiyun 	rc_defered_ack(rcd, qp);
3630*4882a593Smuzhiyun }
3631*4882a593Smuzhiyun 
hfi1_rc_rcv_tid_rdma_write_req(struct hfi1_packet * packet)3632*4882a593Smuzhiyun void hfi1_rc_rcv_tid_rdma_write_req(struct hfi1_packet *packet)
3633*4882a593Smuzhiyun {
3634*4882a593Smuzhiyun 	/* HANDLER FOR TID RDMA WRITE REQUEST packet (Responder side)*/
3635*4882a593Smuzhiyun 
3636*4882a593Smuzhiyun 	/*
3637*4882a593Smuzhiyun 	 * 1. Verify TID RDMA WRITE REQ as per IB_OPCODE_RC_RDMA_WRITE_FIRST
3638*4882a593Smuzhiyun 	 *    (see hfi1_rc_rcv())
3639*4882a593Smuzhiyun 	 *     - Don't allow 0-length requests.
3640*4882a593Smuzhiyun 	 * 2. Put TID RDMA WRITE REQ into the response queueu (s_ack_queue)
3641*4882a593Smuzhiyun 	 *     - Setup struct tid_rdma_req with request info
3642*4882a593Smuzhiyun 	 *     - Prepare struct tid_rdma_flow array?
3643*4882a593Smuzhiyun 	 * 3. Set the qp->s_ack_state as state diagram in design doc.
3644*4882a593Smuzhiyun 	 * 4. Set RVT_S_RESP_PENDING in s_flags.
3645*4882a593Smuzhiyun 	 * 5. Kick the send engine (hfi1_schedule_send())
3646*4882a593Smuzhiyun 	 */
3647*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = packet->rcd;
3648*4882a593Smuzhiyun 	struct rvt_qp *qp = packet->qp;
3649*4882a593Smuzhiyun 	struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
3650*4882a593Smuzhiyun 	struct ib_other_headers *ohdr = packet->ohdr;
3651*4882a593Smuzhiyun 	struct rvt_ack_entry *e;
3652*4882a593Smuzhiyun 	unsigned long flags;
3653*4882a593Smuzhiyun 	struct ib_reth *reth;
3654*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
3655*4882a593Smuzhiyun 	struct tid_rdma_request *req;
3656*4882a593Smuzhiyun 	u32 bth0, psn, len, rkey, num_segs;
3657*4882a593Smuzhiyun 	bool fecn;
3658*4882a593Smuzhiyun 	u8 next;
3659*4882a593Smuzhiyun 	u64 vaddr;
3660*4882a593Smuzhiyun 	int diff;
3661*4882a593Smuzhiyun 
3662*4882a593Smuzhiyun 	bth0 = be32_to_cpu(ohdr->bth[0]);
3663*4882a593Smuzhiyun 	if (hfi1_ruc_check_hdr(ibp, packet))
3664*4882a593Smuzhiyun 		return;
3665*4882a593Smuzhiyun 
3666*4882a593Smuzhiyun 	fecn = process_ecn(qp, packet);
3667*4882a593Smuzhiyun 	psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
3668*4882a593Smuzhiyun 	trace_hfi1_rsp_rcv_tid_write_req(qp, psn);
3669*4882a593Smuzhiyun 
3670*4882a593Smuzhiyun 	if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
3671*4882a593Smuzhiyun 		rvt_comm_est(qp);
3672*4882a593Smuzhiyun 
3673*4882a593Smuzhiyun 	if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
3674*4882a593Smuzhiyun 		goto nack_inv;
3675*4882a593Smuzhiyun 
3676*4882a593Smuzhiyun 	reth = &ohdr->u.tid_rdma.w_req.reth;
3677*4882a593Smuzhiyun 	vaddr = be64_to_cpu(reth->vaddr);
3678*4882a593Smuzhiyun 	len = be32_to_cpu(reth->length);
3679*4882a593Smuzhiyun 
3680*4882a593Smuzhiyun 	num_segs = DIV_ROUND_UP(len, qpriv->tid_rdma.local.max_len);
3681*4882a593Smuzhiyun 	diff = delta_psn(psn, qp->r_psn);
3682*4882a593Smuzhiyun 	if (unlikely(diff)) {
3683*4882a593Smuzhiyun 		tid_rdma_rcv_err(packet, ohdr, qp, psn, diff, fecn);
3684*4882a593Smuzhiyun 		return;
3685*4882a593Smuzhiyun 	}
3686*4882a593Smuzhiyun 
3687*4882a593Smuzhiyun 	/*
3688*4882a593Smuzhiyun 	 * The resent request which was previously RNR NAK'd is inserted at the
3689*4882a593Smuzhiyun 	 * location of the original request, which is one entry behind
3690*4882a593Smuzhiyun 	 * r_head_ack_queue
3691*4882a593Smuzhiyun 	 */
3692*4882a593Smuzhiyun 	if (qpriv->rnr_nak_state)
3693*4882a593Smuzhiyun 		qp->r_head_ack_queue = qp->r_head_ack_queue ?
3694*4882a593Smuzhiyun 			qp->r_head_ack_queue - 1 :
3695*4882a593Smuzhiyun 			rvt_size_atomic(ib_to_rvt(qp->ibqp.device));
3696*4882a593Smuzhiyun 
3697*4882a593Smuzhiyun 	/* We've verified the request, insert it into the ack queue. */
3698*4882a593Smuzhiyun 	next = qp->r_head_ack_queue + 1;
3699*4882a593Smuzhiyun 	if (next > rvt_size_atomic(ib_to_rvt(qp->ibqp.device)))
3700*4882a593Smuzhiyun 		next = 0;
3701*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->s_lock, flags);
3702*4882a593Smuzhiyun 	if (unlikely(next == qp->s_acked_ack_queue)) {
3703*4882a593Smuzhiyun 		if (!qp->s_ack_queue[next].sent)
3704*4882a593Smuzhiyun 			goto nack_inv_unlock;
3705*4882a593Smuzhiyun 		update_ack_queue(qp, next);
3706*4882a593Smuzhiyun 	}
3707*4882a593Smuzhiyun 	e = &qp->s_ack_queue[qp->r_head_ack_queue];
3708*4882a593Smuzhiyun 	req = ack_to_tid_req(e);
3709*4882a593Smuzhiyun 
3710*4882a593Smuzhiyun 	/* Bring previously RNR NAK'd request back to life */
3711*4882a593Smuzhiyun 	if (qpriv->rnr_nak_state) {
3712*4882a593Smuzhiyun 		qp->r_nak_state = 0;
3713*4882a593Smuzhiyun 		qp->s_nak_state = 0;
3714*4882a593Smuzhiyun 		qpriv->rnr_nak_state = TID_RNR_NAK_INIT;
3715*4882a593Smuzhiyun 		qp->r_psn = e->lpsn + 1;
3716*4882a593Smuzhiyun 		req->state = TID_REQUEST_INIT;
3717*4882a593Smuzhiyun 		goto update_head;
3718*4882a593Smuzhiyun 	}
3719*4882a593Smuzhiyun 
3720*4882a593Smuzhiyun 	release_rdma_sge_mr(e);
3721*4882a593Smuzhiyun 
3722*4882a593Smuzhiyun 	/* The length needs to be in multiples of PAGE_SIZE */
3723*4882a593Smuzhiyun 	if (!len || len & ~PAGE_MASK)
3724*4882a593Smuzhiyun 		goto nack_inv_unlock;
3725*4882a593Smuzhiyun 
3726*4882a593Smuzhiyun 	rkey = be32_to_cpu(reth->rkey);
3727*4882a593Smuzhiyun 	qp->r_len = len;
3728*4882a593Smuzhiyun 
3729*4882a593Smuzhiyun 	if (e->opcode == TID_OP(WRITE_REQ) &&
3730*4882a593Smuzhiyun 	    (req->setup_head != req->clear_tail ||
3731*4882a593Smuzhiyun 	     req->clear_tail != req->acked_tail))
3732*4882a593Smuzhiyun 		goto nack_inv_unlock;
3733*4882a593Smuzhiyun 
3734*4882a593Smuzhiyun 	if (unlikely(!rvt_rkey_ok(qp, &e->rdma_sge, qp->r_len, vaddr,
3735*4882a593Smuzhiyun 				  rkey, IB_ACCESS_REMOTE_WRITE)))
3736*4882a593Smuzhiyun 		goto nack_acc;
3737*4882a593Smuzhiyun 
3738*4882a593Smuzhiyun 	qp->r_psn += num_segs - 1;
3739*4882a593Smuzhiyun 
3740*4882a593Smuzhiyun 	e->opcode = (bth0 >> 24) & 0xff;
3741*4882a593Smuzhiyun 	e->psn = psn;
3742*4882a593Smuzhiyun 	e->lpsn = qp->r_psn;
3743*4882a593Smuzhiyun 	e->sent = 0;
3744*4882a593Smuzhiyun 
3745*4882a593Smuzhiyun 	req->n_flows = min_t(u16, num_segs, qpriv->tid_rdma.local.max_write);
3746*4882a593Smuzhiyun 	req->state = TID_REQUEST_INIT;
3747*4882a593Smuzhiyun 	req->cur_seg = 0;
3748*4882a593Smuzhiyun 	req->comp_seg = 0;
3749*4882a593Smuzhiyun 	req->ack_seg = 0;
3750*4882a593Smuzhiyun 	req->alloc_seg = 0;
3751*4882a593Smuzhiyun 	req->isge = 0;
3752*4882a593Smuzhiyun 	req->seg_len = qpriv->tid_rdma.local.max_len;
3753*4882a593Smuzhiyun 	req->total_len = len;
3754*4882a593Smuzhiyun 	req->total_segs = num_segs;
3755*4882a593Smuzhiyun 	req->r_flow_psn = e->psn;
3756*4882a593Smuzhiyun 	req->ss.sge = e->rdma_sge;
3757*4882a593Smuzhiyun 	req->ss.num_sge = 1;
3758*4882a593Smuzhiyun 
3759*4882a593Smuzhiyun 	req->flow_idx = req->setup_head;
3760*4882a593Smuzhiyun 	req->clear_tail = req->setup_head;
3761*4882a593Smuzhiyun 	req->acked_tail = req->setup_head;
3762*4882a593Smuzhiyun 
3763*4882a593Smuzhiyun 	qp->r_state = e->opcode;
3764*4882a593Smuzhiyun 	qp->r_nak_state = 0;
3765*4882a593Smuzhiyun 	/*
3766*4882a593Smuzhiyun 	 * We need to increment the MSN here instead of when we
3767*4882a593Smuzhiyun 	 * finish sending the result since a duplicate request would
3768*4882a593Smuzhiyun 	 * increment it more than once.
3769*4882a593Smuzhiyun 	 */
3770*4882a593Smuzhiyun 	qp->r_msn++;
3771*4882a593Smuzhiyun 	qp->r_psn++;
3772*4882a593Smuzhiyun 
3773*4882a593Smuzhiyun 	trace_hfi1_tid_req_rcv_write_req(qp, 0, e->opcode, e->psn, e->lpsn,
3774*4882a593Smuzhiyun 					 req);
3775*4882a593Smuzhiyun 
3776*4882a593Smuzhiyun 	if (qpriv->r_tid_tail == HFI1_QP_WQE_INVALID) {
3777*4882a593Smuzhiyun 		qpriv->r_tid_tail = qp->r_head_ack_queue;
3778*4882a593Smuzhiyun 	} else if (qpriv->r_tid_tail == qpriv->r_tid_head) {
3779*4882a593Smuzhiyun 		struct tid_rdma_request *ptr;
3780*4882a593Smuzhiyun 
3781*4882a593Smuzhiyun 		e = &qp->s_ack_queue[qpriv->r_tid_tail];
3782*4882a593Smuzhiyun 		ptr = ack_to_tid_req(e);
3783*4882a593Smuzhiyun 
3784*4882a593Smuzhiyun 		if (e->opcode != TID_OP(WRITE_REQ) ||
3785*4882a593Smuzhiyun 		    ptr->comp_seg == ptr->total_segs) {
3786*4882a593Smuzhiyun 			if (qpriv->r_tid_tail == qpriv->r_tid_ack)
3787*4882a593Smuzhiyun 				qpriv->r_tid_ack = qp->r_head_ack_queue;
3788*4882a593Smuzhiyun 			qpriv->r_tid_tail = qp->r_head_ack_queue;
3789*4882a593Smuzhiyun 		}
3790*4882a593Smuzhiyun 	}
3791*4882a593Smuzhiyun update_head:
3792*4882a593Smuzhiyun 	qp->r_head_ack_queue = next;
3793*4882a593Smuzhiyun 	qpriv->r_tid_head = qp->r_head_ack_queue;
3794*4882a593Smuzhiyun 
3795*4882a593Smuzhiyun 	hfi1_tid_write_alloc_resources(qp, true);
3796*4882a593Smuzhiyun 	trace_hfi1_tid_write_rsp_rcv_req(qp);
3797*4882a593Smuzhiyun 
3798*4882a593Smuzhiyun 	/* Schedule the send tasklet. */
3799*4882a593Smuzhiyun 	qp->s_flags |= RVT_S_RESP_PENDING;
3800*4882a593Smuzhiyun 	if (fecn)
3801*4882a593Smuzhiyun 		qp->s_flags |= RVT_S_ECN;
3802*4882a593Smuzhiyun 	hfi1_schedule_send(qp);
3803*4882a593Smuzhiyun 
3804*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, flags);
3805*4882a593Smuzhiyun 	return;
3806*4882a593Smuzhiyun 
3807*4882a593Smuzhiyun nack_inv_unlock:
3808*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, flags);
3809*4882a593Smuzhiyun nack_inv:
3810*4882a593Smuzhiyun 	rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
3811*4882a593Smuzhiyun 	qp->r_nak_state = IB_NAK_INVALID_REQUEST;
3812*4882a593Smuzhiyun 	qp->r_ack_psn = qp->r_psn;
3813*4882a593Smuzhiyun 	/* Queue NAK for later */
3814*4882a593Smuzhiyun 	rc_defered_ack(rcd, qp);
3815*4882a593Smuzhiyun 	return;
3816*4882a593Smuzhiyun nack_acc:
3817*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, flags);
3818*4882a593Smuzhiyun 	rvt_rc_error(qp, IB_WC_LOC_PROT_ERR);
3819*4882a593Smuzhiyun 	qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
3820*4882a593Smuzhiyun 	qp->r_ack_psn = qp->r_psn;
3821*4882a593Smuzhiyun }
3822*4882a593Smuzhiyun 
hfi1_build_tid_rdma_write_resp(struct rvt_qp * qp,struct rvt_ack_entry * e,struct ib_other_headers * ohdr,u32 * bth1,u32 bth2,u32 * len,struct rvt_sge_state ** ss)3823*4882a593Smuzhiyun u32 hfi1_build_tid_rdma_write_resp(struct rvt_qp *qp, struct rvt_ack_entry *e,
3824*4882a593Smuzhiyun 				   struct ib_other_headers *ohdr, u32 *bth1,
3825*4882a593Smuzhiyun 				   u32 bth2, u32 *len,
3826*4882a593Smuzhiyun 				   struct rvt_sge_state **ss)
3827*4882a593Smuzhiyun {
3828*4882a593Smuzhiyun 	struct hfi1_ack_priv *epriv = e->priv;
3829*4882a593Smuzhiyun 	struct tid_rdma_request *req = &epriv->tid_req;
3830*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
3831*4882a593Smuzhiyun 	struct tid_rdma_flow *flow = NULL;
3832*4882a593Smuzhiyun 	u32 resp_len = 0, hdwords = 0;
3833*4882a593Smuzhiyun 	void *resp_addr = NULL;
3834*4882a593Smuzhiyun 	struct tid_rdma_params *remote;
3835*4882a593Smuzhiyun 
3836*4882a593Smuzhiyun 	trace_hfi1_tid_req_build_write_resp(qp, 0, e->opcode, e->psn, e->lpsn,
3837*4882a593Smuzhiyun 					    req);
3838*4882a593Smuzhiyun 	trace_hfi1_tid_write_rsp_build_resp(qp);
3839*4882a593Smuzhiyun 	trace_hfi1_rsp_build_tid_write_resp(qp, bth2);
3840*4882a593Smuzhiyun 	flow = &req->flows[req->flow_idx];
3841*4882a593Smuzhiyun 	switch (req->state) {
3842*4882a593Smuzhiyun 	default:
3843*4882a593Smuzhiyun 		/*
3844*4882a593Smuzhiyun 		 * Try to allocate resources here in case QP was queued and was
3845*4882a593Smuzhiyun 		 * later scheduled when resources became available
3846*4882a593Smuzhiyun 		 */
3847*4882a593Smuzhiyun 		hfi1_tid_write_alloc_resources(qp, false);
3848*4882a593Smuzhiyun 
3849*4882a593Smuzhiyun 		/* We've already sent everything which is ready */
3850*4882a593Smuzhiyun 		if (req->cur_seg >= req->alloc_seg)
3851*4882a593Smuzhiyun 			goto done;
3852*4882a593Smuzhiyun 
3853*4882a593Smuzhiyun 		/*
3854*4882a593Smuzhiyun 		 * Resources can be assigned but responses cannot be sent in
3855*4882a593Smuzhiyun 		 * rnr_nak state, till the resent request is received
3856*4882a593Smuzhiyun 		 */
3857*4882a593Smuzhiyun 		if (qpriv->rnr_nak_state == TID_RNR_NAK_SENT)
3858*4882a593Smuzhiyun 			goto done;
3859*4882a593Smuzhiyun 
3860*4882a593Smuzhiyun 		req->state = TID_REQUEST_ACTIVE;
3861*4882a593Smuzhiyun 		trace_hfi1_tid_flow_build_write_resp(qp, req->flow_idx, flow);
3862*4882a593Smuzhiyun 		req->flow_idx = CIRC_NEXT(req->flow_idx, MAX_FLOWS);
3863*4882a593Smuzhiyun 		hfi1_add_tid_reap_timer(qp);
3864*4882a593Smuzhiyun 		break;
3865*4882a593Smuzhiyun 
3866*4882a593Smuzhiyun 	case TID_REQUEST_RESEND_ACTIVE:
3867*4882a593Smuzhiyun 	case TID_REQUEST_RESEND:
3868*4882a593Smuzhiyun 		trace_hfi1_tid_flow_build_write_resp(qp, req->flow_idx, flow);
3869*4882a593Smuzhiyun 		req->flow_idx = CIRC_NEXT(req->flow_idx, MAX_FLOWS);
3870*4882a593Smuzhiyun 		if (!CIRC_CNT(req->setup_head, req->flow_idx, MAX_FLOWS))
3871*4882a593Smuzhiyun 			req->state = TID_REQUEST_ACTIVE;
3872*4882a593Smuzhiyun 
3873*4882a593Smuzhiyun 		hfi1_mod_tid_reap_timer(qp);
3874*4882a593Smuzhiyun 		break;
3875*4882a593Smuzhiyun 	}
3876*4882a593Smuzhiyun 	flow->flow_state.resp_ib_psn = bth2;
3877*4882a593Smuzhiyun 	resp_addr = (void *)flow->tid_entry;
3878*4882a593Smuzhiyun 	resp_len = sizeof(*flow->tid_entry) * flow->tidcnt;
3879*4882a593Smuzhiyun 	req->cur_seg++;
3880*4882a593Smuzhiyun 
3881*4882a593Smuzhiyun 	memset(&ohdr->u.tid_rdma.w_rsp, 0, sizeof(ohdr->u.tid_rdma.w_rsp));
3882*4882a593Smuzhiyun 	epriv->ss.sge.vaddr = resp_addr;
3883*4882a593Smuzhiyun 	epriv->ss.sge.sge_length = resp_len;
3884*4882a593Smuzhiyun 	epriv->ss.sge.length = epriv->ss.sge.sge_length;
3885*4882a593Smuzhiyun 	/*
3886*4882a593Smuzhiyun 	 * We can safely zero these out. Since the first SGE covers the
3887*4882a593Smuzhiyun 	 * entire packet, nothing else should even look at the MR.
3888*4882a593Smuzhiyun 	 */
3889*4882a593Smuzhiyun 	epriv->ss.sge.mr = NULL;
3890*4882a593Smuzhiyun 	epriv->ss.sge.m = 0;
3891*4882a593Smuzhiyun 	epriv->ss.sge.n = 0;
3892*4882a593Smuzhiyun 
3893*4882a593Smuzhiyun 	epriv->ss.sg_list = NULL;
3894*4882a593Smuzhiyun 	epriv->ss.total_len = epriv->ss.sge.sge_length;
3895*4882a593Smuzhiyun 	epriv->ss.num_sge = 1;
3896*4882a593Smuzhiyun 
3897*4882a593Smuzhiyun 	*ss = &epriv->ss;
3898*4882a593Smuzhiyun 	*len = epriv->ss.total_len;
3899*4882a593Smuzhiyun 
3900*4882a593Smuzhiyun 	/* Construct the TID RDMA WRITE RESP packet header */
3901*4882a593Smuzhiyun 	rcu_read_lock();
3902*4882a593Smuzhiyun 	remote = rcu_dereference(qpriv->tid_rdma.remote);
3903*4882a593Smuzhiyun 
3904*4882a593Smuzhiyun 	KDETH_RESET(ohdr->u.tid_rdma.w_rsp.kdeth0, KVER, 0x1);
3905*4882a593Smuzhiyun 	KDETH_RESET(ohdr->u.tid_rdma.w_rsp.kdeth1, JKEY, remote->jkey);
3906*4882a593Smuzhiyun 	ohdr->u.tid_rdma.w_rsp.aeth = rvt_compute_aeth(qp);
3907*4882a593Smuzhiyun 	ohdr->u.tid_rdma.w_rsp.tid_flow_psn =
3908*4882a593Smuzhiyun 		cpu_to_be32((flow->flow_state.generation <<
3909*4882a593Smuzhiyun 			     HFI1_KDETH_BTH_SEQ_SHIFT) |
3910*4882a593Smuzhiyun 			    (flow->flow_state.spsn &
3911*4882a593Smuzhiyun 			     HFI1_KDETH_BTH_SEQ_MASK));
3912*4882a593Smuzhiyun 	ohdr->u.tid_rdma.w_rsp.tid_flow_qp =
3913*4882a593Smuzhiyun 		cpu_to_be32(qpriv->tid_rdma.local.qp |
3914*4882a593Smuzhiyun 			    ((flow->idx & TID_RDMA_DESTQP_FLOW_MASK) <<
3915*4882a593Smuzhiyun 			     TID_RDMA_DESTQP_FLOW_SHIFT) |
3916*4882a593Smuzhiyun 			    qpriv->rcd->ctxt);
3917*4882a593Smuzhiyun 	ohdr->u.tid_rdma.w_rsp.verbs_qp = cpu_to_be32(qp->remote_qpn);
3918*4882a593Smuzhiyun 	*bth1 = remote->qp;
3919*4882a593Smuzhiyun 	rcu_read_unlock();
3920*4882a593Smuzhiyun 	hdwords = sizeof(ohdr->u.tid_rdma.w_rsp) / sizeof(u32);
3921*4882a593Smuzhiyun 	qpriv->pending_tid_w_segs++;
3922*4882a593Smuzhiyun done:
3923*4882a593Smuzhiyun 	return hdwords;
3924*4882a593Smuzhiyun }
3925*4882a593Smuzhiyun 
hfi1_add_tid_reap_timer(struct rvt_qp * qp)3926*4882a593Smuzhiyun static void hfi1_add_tid_reap_timer(struct rvt_qp *qp)
3927*4882a593Smuzhiyun {
3928*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
3929*4882a593Smuzhiyun 
3930*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
3931*4882a593Smuzhiyun 	if (!(qpriv->s_flags & HFI1_R_TID_RSC_TIMER)) {
3932*4882a593Smuzhiyun 		qpriv->s_flags |= HFI1_R_TID_RSC_TIMER;
3933*4882a593Smuzhiyun 		qpriv->s_tid_timer.expires = jiffies +
3934*4882a593Smuzhiyun 			qpriv->tid_timer_timeout_jiffies;
3935*4882a593Smuzhiyun 		add_timer(&qpriv->s_tid_timer);
3936*4882a593Smuzhiyun 	}
3937*4882a593Smuzhiyun }
3938*4882a593Smuzhiyun 
hfi1_mod_tid_reap_timer(struct rvt_qp * qp)3939*4882a593Smuzhiyun static void hfi1_mod_tid_reap_timer(struct rvt_qp *qp)
3940*4882a593Smuzhiyun {
3941*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
3942*4882a593Smuzhiyun 
3943*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
3944*4882a593Smuzhiyun 	qpriv->s_flags |= HFI1_R_TID_RSC_TIMER;
3945*4882a593Smuzhiyun 	mod_timer(&qpriv->s_tid_timer, jiffies +
3946*4882a593Smuzhiyun 		  qpriv->tid_timer_timeout_jiffies);
3947*4882a593Smuzhiyun }
3948*4882a593Smuzhiyun 
hfi1_stop_tid_reap_timer(struct rvt_qp * qp)3949*4882a593Smuzhiyun static int hfi1_stop_tid_reap_timer(struct rvt_qp *qp)
3950*4882a593Smuzhiyun {
3951*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
3952*4882a593Smuzhiyun 	int rval = 0;
3953*4882a593Smuzhiyun 
3954*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
3955*4882a593Smuzhiyun 	if (qpriv->s_flags & HFI1_R_TID_RSC_TIMER) {
3956*4882a593Smuzhiyun 		rval = del_timer(&qpriv->s_tid_timer);
3957*4882a593Smuzhiyun 		qpriv->s_flags &= ~HFI1_R_TID_RSC_TIMER;
3958*4882a593Smuzhiyun 	}
3959*4882a593Smuzhiyun 	return rval;
3960*4882a593Smuzhiyun }
3961*4882a593Smuzhiyun 
hfi1_del_tid_reap_timer(struct rvt_qp * qp)3962*4882a593Smuzhiyun void hfi1_del_tid_reap_timer(struct rvt_qp *qp)
3963*4882a593Smuzhiyun {
3964*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
3965*4882a593Smuzhiyun 
3966*4882a593Smuzhiyun 	del_timer_sync(&qpriv->s_tid_timer);
3967*4882a593Smuzhiyun 	qpriv->s_flags &= ~HFI1_R_TID_RSC_TIMER;
3968*4882a593Smuzhiyun }
3969*4882a593Smuzhiyun 
hfi1_tid_timeout(struct timer_list * t)3970*4882a593Smuzhiyun static void hfi1_tid_timeout(struct timer_list *t)
3971*4882a593Smuzhiyun {
3972*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = from_timer(qpriv, t, s_tid_timer);
3973*4882a593Smuzhiyun 	struct rvt_qp *qp = qpriv->owner;
3974*4882a593Smuzhiyun 	struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
3975*4882a593Smuzhiyun 	unsigned long flags;
3976*4882a593Smuzhiyun 	u32 i;
3977*4882a593Smuzhiyun 
3978*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->r_lock, flags);
3979*4882a593Smuzhiyun 	spin_lock(&qp->s_lock);
3980*4882a593Smuzhiyun 	if (qpriv->s_flags & HFI1_R_TID_RSC_TIMER) {
3981*4882a593Smuzhiyun 		dd_dev_warn(dd_from_ibdev(qp->ibqp.device), "[QP%u] %s %d\n",
3982*4882a593Smuzhiyun 			    qp->ibqp.qp_num, __func__, __LINE__);
3983*4882a593Smuzhiyun 		trace_hfi1_msg_tid_timeout(/* msg */
3984*4882a593Smuzhiyun 			qp, "resource timeout = ",
3985*4882a593Smuzhiyun 			(u64)qpriv->tid_timer_timeout_jiffies);
3986*4882a593Smuzhiyun 		hfi1_stop_tid_reap_timer(qp);
3987*4882a593Smuzhiyun 		/*
3988*4882a593Smuzhiyun 		 * Go though the entire ack queue and clear any outstanding
3989*4882a593Smuzhiyun 		 * HW flow and RcvArray resources.
3990*4882a593Smuzhiyun 		 */
3991*4882a593Smuzhiyun 		hfi1_kern_clear_hw_flow(qpriv->rcd, qp);
3992*4882a593Smuzhiyun 		for (i = 0; i < rvt_max_atomic(rdi); i++) {
3993*4882a593Smuzhiyun 			struct tid_rdma_request *req =
3994*4882a593Smuzhiyun 				ack_to_tid_req(&qp->s_ack_queue[i]);
3995*4882a593Smuzhiyun 
3996*4882a593Smuzhiyun 			hfi1_kern_exp_rcv_clear_all(req);
3997*4882a593Smuzhiyun 		}
3998*4882a593Smuzhiyun 		spin_unlock(&qp->s_lock);
3999*4882a593Smuzhiyun 		if (qp->ibqp.event_handler) {
4000*4882a593Smuzhiyun 			struct ib_event ev;
4001*4882a593Smuzhiyun 
4002*4882a593Smuzhiyun 			ev.device = qp->ibqp.device;
4003*4882a593Smuzhiyun 			ev.element.qp = &qp->ibqp;
4004*4882a593Smuzhiyun 			ev.event = IB_EVENT_QP_FATAL;
4005*4882a593Smuzhiyun 			qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
4006*4882a593Smuzhiyun 		}
4007*4882a593Smuzhiyun 		rvt_rc_error(qp, IB_WC_RESP_TIMEOUT_ERR);
4008*4882a593Smuzhiyun 		goto unlock_r_lock;
4009*4882a593Smuzhiyun 	}
4010*4882a593Smuzhiyun 	spin_unlock(&qp->s_lock);
4011*4882a593Smuzhiyun unlock_r_lock:
4012*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->r_lock, flags);
4013*4882a593Smuzhiyun }
4014*4882a593Smuzhiyun 
hfi1_rc_rcv_tid_rdma_write_resp(struct hfi1_packet * packet)4015*4882a593Smuzhiyun void hfi1_rc_rcv_tid_rdma_write_resp(struct hfi1_packet *packet)
4016*4882a593Smuzhiyun {
4017*4882a593Smuzhiyun 	/* HANDLER FOR TID RDMA WRITE RESPONSE packet (Requestor side */
4018*4882a593Smuzhiyun 
4019*4882a593Smuzhiyun 	/*
4020*4882a593Smuzhiyun 	 * 1. Find matching SWQE
4021*4882a593Smuzhiyun 	 * 2. Check that TIDENTRY array has enough space for a complete
4022*4882a593Smuzhiyun 	 *    segment. If not, put QP in error state.
4023*4882a593Smuzhiyun 	 * 3. Save response data in struct tid_rdma_req and struct tid_rdma_flow
4024*4882a593Smuzhiyun 	 * 4. Remove HFI1_S_WAIT_TID_RESP from s_flags.
4025*4882a593Smuzhiyun 	 * 5. Set qp->s_state
4026*4882a593Smuzhiyun 	 * 6. Kick the send engine (hfi1_schedule_send())
4027*4882a593Smuzhiyun 	 */
4028*4882a593Smuzhiyun 	struct ib_other_headers *ohdr = packet->ohdr;
4029*4882a593Smuzhiyun 	struct rvt_qp *qp = packet->qp;
4030*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
4031*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = packet->rcd;
4032*4882a593Smuzhiyun 	struct rvt_swqe *wqe;
4033*4882a593Smuzhiyun 	struct tid_rdma_request *req;
4034*4882a593Smuzhiyun 	struct tid_rdma_flow *flow;
4035*4882a593Smuzhiyun 	enum ib_wc_status status;
4036*4882a593Smuzhiyun 	u32 opcode, aeth, psn, flow_psn, i, tidlen = 0, pktlen;
4037*4882a593Smuzhiyun 	bool fecn;
4038*4882a593Smuzhiyun 	unsigned long flags;
4039*4882a593Smuzhiyun 
4040*4882a593Smuzhiyun 	fecn = process_ecn(qp, packet);
4041*4882a593Smuzhiyun 	psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
4042*4882a593Smuzhiyun 	aeth = be32_to_cpu(ohdr->u.tid_rdma.w_rsp.aeth);
4043*4882a593Smuzhiyun 	opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0xff;
4044*4882a593Smuzhiyun 
4045*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->s_lock, flags);
4046*4882a593Smuzhiyun 
4047*4882a593Smuzhiyun 	/* Ignore invalid responses */
4048*4882a593Smuzhiyun 	if (cmp_psn(psn, qp->s_next_psn) >= 0)
4049*4882a593Smuzhiyun 		goto ack_done;
4050*4882a593Smuzhiyun 
4051*4882a593Smuzhiyun 	/* Ignore duplicate responses. */
4052*4882a593Smuzhiyun 	if (unlikely(cmp_psn(psn, qp->s_last_psn) <= 0))
4053*4882a593Smuzhiyun 		goto ack_done;
4054*4882a593Smuzhiyun 
4055*4882a593Smuzhiyun 	if (unlikely(qp->s_acked == qp->s_tail))
4056*4882a593Smuzhiyun 		goto ack_done;
4057*4882a593Smuzhiyun 
4058*4882a593Smuzhiyun 	/*
4059*4882a593Smuzhiyun 	 * If we are waiting for a particular packet sequence number
4060*4882a593Smuzhiyun 	 * due to a request being resent, check for it. Otherwise,
4061*4882a593Smuzhiyun 	 * ensure that we haven't missed anything.
4062*4882a593Smuzhiyun 	 */
4063*4882a593Smuzhiyun 	if (qp->r_flags & RVT_R_RDMAR_SEQ) {
4064*4882a593Smuzhiyun 		if (cmp_psn(psn, qp->s_last_psn + 1) != 0)
4065*4882a593Smuzhiyun 			goto ack_done;
4066*4882a593Smuzhiyun 		qp->r_flags &= ~RVT_R_RDMAR_SEQ;
4067*4882a593Smuzhiyun 	}
4068*4882a593Smuzhiyun 
4069*4882a593Smuzhiyun 	wqe = rvt_get_swqe_ptr(qp, qpriv->s_tid_cur);
4070*4882a593Smuzhiyun 	if (unlikely(wqe->wr.opcode != IB_WR_TID_RDMA_WRITE))
4071*4882a593Smuzhiyun 		goto ack_op_err;
4072*4882a593Smuzhiyun 
4073*4882a593Smuzhiyun 	req = wqe_to_tid_req(wqe);
4074*4882a593Smuzhiyun 	/*
4075*4882a593Smuzhiyun 	 * If we've lost ACKs and our acked_tail pointer is too far
4076*4882a593Smuzhiyun 	 * behind, don't overwrite segments. Just drop the packet and
4077*4882a593Smuzhiyun 	 * let the reliability protocol take care of it.
4078*4882a593Smuzhiyun 	 */
4079*4882a593Smuzhiyun 	if (!CIRC_SPACE(req->setup_head, req->acked_tail, MAX_FLOWS))
4080*4882a593Smuzhiyun 		goto ack_done;
4081*4882a593Smuzhiyun 
4082*4882a593Smuzhiyun 	/*
4083*4882a593Smuzhiyun 	 * The call to do_rc_ack() should be last in the chain of
4084*4882a593Smuzhiyun 	 * packet checks because it will end up updating the QP state.
4085*4882a593Smuzhiyun 	 * Therefore, anything that would prevent the packet from
4086*4882a593Smuzhiyun 	 * being accepted as a successful response should be prior
4087*4882a593Smuzhiyun 	 * to it.
4088*4882a593Smuzhiyun 	 */
4089*4882a593Smuzhiyun 	if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
4090*4882a593Smuzhiyun 		goto ack_done;
4091*4882a593Smuzhiyun 
4092*4882a593Smuzhiyun 	trace_hfi1_ack(qp, psn);
4093*4882a593Smuzhiyun 
4094*4882a593Smuzhiyun 	flow = &req->flows[req->setup_head];
4095*4882a593Smuzhiyun 	flow->pkt = 0;
4096*4882a593Smuzhiyun 	flow->tid_idx = 0;
4097*4882a593Smuzhiyun 	flow->tid_offset = 0;
4098*4882a593Smuzhiyun 	flow->sent = 0;
4099*4882a593Smuzhiyun 	flow->resync_npkts = 0;
4100*4882a593Smuzhiyun 	flow->tid_qpn = be32_to_cpu(ohdr->u.tid_rdma.w_rsp.tid_flow_qp);
4101*4882a593Smuzhiyun 	flow->idx = (flow->tid_qpn >> TID_RDMA_DESTQP_FLOW_SHIFT) &
4102*4882a593Smuzhiyun 		TID_RDMA_DESTQP_FLOW_MASK;
4103*4882a593Smuzhiyun 	flow_psn = mask_psn(be32_to_cpu(ohdr->u.tid_rdma.w_rsp.tid_flow_psn));
4104*4882a593Smuzhiyun 	flow->flow_state.generation = flow_psn >> HFI1_KDETH_BTH_SEQ_SHIFT;
4105*4882a593Smuzhiyun 	flow->flow_state.spsn = flow_psn & HFI1_KDETH_BTH_SEQ_MASK;
4106*4882a593Smuzhiyun 	flow->flow_state.resp_ib_psn = psn;
4107*4882a593Smuzhiyun 	flow->length = min_t(u32, req->seg_len,
4108*4882a593Smuzhiyun 			     (wqe->length - (req->comp_seg * req->seg_len)));
4109*4882a593Smuzhiyun 
4110*4882a593Smuzhiyun 	flow->npkts = rvt_div_round_up_mtu(qp, flow->length);
4111*4882a593Smuzhiyun 	flow->flow_state.lpsn = flow->flow_state.spsn +
4112*4882a593Smuzhiyun 		flow->npkts - 1;
4113*4882a593Smuzhiyun 	/* payload length = packet length - (header length + ICRC length) */
4114*4882a593Smuzhiyun 	pktlen = packet->tlen - (packet->hlen + 4);
4115*4882a593Smuzhiyun 	if (pktlen > sizeof(flow->tid_entry)) {
4116*4882a593Smuzhiyun 		status = IB_WC_LOC_LEN_ERR;
4117*4882a593Smuzhiyun 		goto ack_err;
4118*4882a593Smuzhiyun 	}
4119*4882a593Smuzhiyun 	memcpy(flow->tid_entry, packet->ebuf, pktlen);
4120*4882a593Smuzhiyun 	flow->tidcnt = pktlen / sizeof(*flow->tid_entry);
4121*4882a593Smuzhiyun 	trace_hfi1_tid_flow_rcv_write_resp(qp, req->setup_head, flow);
4122*4882a593Smuzhiyun 
4123*4882a593Smuzhiyun 	req->comp_seg++;
4124*4882a593Smuzhiyun 	trace_hfi1_tid_write_sender_rcv_resp(qp, 0);
4125*4882a593Smuzhiyun 	/*
4126*4882a593Smuzhiyun 	 * Walk the TID_ENTRY list to make sure we have enough space for a
4127*4882a593Smuzhiyun 	 * complete segment.
4128*4882a593Smuzhiyun 	 */
4129*4882a593Smuzhiyun 	for (i = 0; i < flow->tidcnt; i++) {
4130*4882a593Smuzhiyun 		trace_hfi1_tid_entry_rcv_write_resp(/* entry */
4131*4882a593Smuzhiyun 			qp, i, flow->tid_entry[i]);
4132*4882a593Smuzhiyun 		if (!EXP_TID_GET(flow->tid_entry[i], LEN)) {
4133*4882a593Smuzhiyun 			status = IB_WC_LOC_LEN_ERR;
4134*4882a593Smuzhiyun 			goto ack_err;
4135*4882a593Smuzhiyun 		}
4136*4882a593Smuzhiyun 		tidlen += EXP_TID_GET(flow->tid_entry[i], LEN);
4137*4882a593Smuzhiyun 	}
4138*4882a593Smuzhiyun 	if (tidlen * PAGE_SIZE < flow->length) {
4139*4882a593Smuzhiyun 		status = IB_WC_LOC_LEN_ERR;
4140*4882a593Smuzhiyun 		goto ack_err;
4141*4882a593Smuzhiyun 	}
4142*4882a593Smuzhiyun 
4143*4882a593Smuzhiyun 	trace_hfi1_tid_req_rcv_write_resp(qp, 0, wqe->wr.opcode, wqe->psn,
4144*4882a593Smuzhiyun 					  wqe->lpsn, req);
4145*4882a593Smuzhiyun 	/*
4146*4882a593Smuzhiyun 	 * If this is the first response for this request, set the initial
4147*4882a593Smuzhiyun 	 * flow index to the current flow.
4148*4882a593Smuzhiyun 	 */
4149*4882a593Smuzhiyun 	if (!cmp_psn(psn, wqe->psn)) {
4150*4882a593Smuzhiyun 		req->r_last_acked = mask_psn(wqe->psn - 1);
4151*4882a593Smuzhiyun 		/* Set acked flow index to head index */
4152*4882a593Smuzhiyun 		req->acked_tail = req->setup_head;
4153*4882a593Smuzhiyun 	}
4154*4882a593Smuzhiyun 
4155*4882a593Smuzhiyun 	/* advance circular buffer head */
4156*4882a593Smuzhiyun 	req->setup_head = CIRC_NEXT(req->setup_head, MAX_FLOWS);
4157*4882a593Smuzhiyun 	req->state = TID_REQUEST_ACTIVE;
4158*4882a593Smuzhiyun 
4159*4882a593Smuzhiyun 	/*
4160*4882a593Smuzhiyun 	 * If all responses for this TID RDMA WRITE request have been received
4161*4882a593Smuzhiyun 	 * advance the pointer to the next one.
4162*4882a593Smuzhiyun 	 * Since TID RDMA requests could be mixed in with regular IB requests,
4163*4882a593Smuzhiyun 	 * they might not appear sequentially in the queue. Therefore, the
4164*4882a593Smuzhiyun 	 * next request needs to be "found".
4165*4882a593Smuzhiyun 	 */
4166*4882a593Smuzhiyun 	if (qpriv->s_tid_cur != qpriv->s_tid_head &&
4167*4882a593Smuzhiyun 	    req->comp_seg == req->total_segs) {
4168*4882a593Smuzhiyun 		for (i = qpriv->s_tid_cur + 1; ; i++) {
4169*4882a593Smuzhiyun 			if (i == qp->s_size)
4170*4882a593Smuzhiyun 				i = 0;
4171*4882a593Smuzhiyun 			wqe = rvt_get_swqe_ptr(qp, i);
4172*4882a593Smuzhiyun 			if (i == qpriv->s_tid_head)
4173*4882a593Smuzhiyun 				break;
4174*4882a593Smuzhiyun 			if (wqe->wr.opcode == IB_WR_TID_RDMA_WRITE)
4175*4882a593Smuzhiyun 				break;
4176*4882a593Smuzhiyun 		}
4177*4882a593Smuzhiyun 		qpriv->s_tid_cur = i;
4178*4882a593Smuzhiyun 	}
4179*4882a593Smuzhiyun 	qp->s_flags &= ~HFI1_S_WAIT_TID_RESP;
4180*4882a593Smuzhiyun 	hfi1_schedule_tid_send(qp);
4181*4882a593Smuzhiyun 	goto ack_done;
4182*4882a593Smuzhiyun 
4183*4882a593Smuzhiyun ack_op_err:
4184*4882a593Smuzhiyun 	status = IB_WC_LOC_QP_OP_ERR;
4185*4882a593Smuzhiyun ack_err:
4186*4882a593Smuzhiyun 	rvt_error_qp(qp, status);
4187*4882a593Smuzhiyun ack_done:
4188*4882a593Smuzhiyun 	if (fecn)
4189*4882a593Smuzhiyun 		qp->s_flags |= RVT_S_ECN;
4190*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, flags);
4191*4882a593Smuzhiyun }
4192*4882a593Smuzhiyun 
hfi1_build_tid_rdma_packet(struct rvt_swqe * wqe,struct ib_other_headers * ohdr,u32 * bth1,u32 * bth2,u32 * len)4193*4882a593Smuzhiyun bool hfi1_build_tid_rdma_packet(struct rvt_swqe *wqe,
4194*4882a593Smuzhiyun 				struct ib_other_headers *ohdr,
4195*4882a593Smuzhiyun 				u32 *bth1, u32 *bth2, u32 *len)
4196*4882a593Smuzhiyun {
4197*4882a593Smuzhiyun 	struct tid_rdma_request *req = wqe_to_tid_req(wqe);
4198*4882a593Smuzhiyun 	struct tid_rdma_flow *flow = &req->flows[req->clear_tail];
4199*4882a593Smuzhiyun 	struct tid_rdma_params *remote;
4200*4882a593Smuzhiyun 	struct rvt_qp *qp = req->qp;
4201*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
4202*4882a593Smuzhiyun 	u32 tidentry = flow->tid_entry[flow->tid_idx];
4203*4882a593Smuzhiyun 	u32 tidlen = EXP_TID_GET(tidentry, LEN) << PAGE_SHIFT;
4204*4882a593Smuzhiyun 	struct tid_rdma_write_data *wd = &ohdr->u.tid_rdma.w_data;
4205*4882a593Smuzhiyun 	u32 next_offset, om = KDETH_OM_LARGE;
4206*4882a593Smuzhiyun 	bool last_pkt;
4207*4882a593Smuzhiyun 
4208*4882a593Smuzhiyun 	if (!tidlen) {
4209*4882a593Smuzhiyun 		hfi1_trdma_send_complete(qp, wqe, IB_WC_REM_INV_RD_REQ_ERR);
4210*4882a593Smuzhiyun 		rvt_error_qp(qp, IB_WC_REM_INV_RD_REQ_ERR);
4211*4882a593Smuzhiyun 	}
4212*4882a593Smuzhiyun 
4213*4882a593Smuzhiyun 	*len = min_t(u32, qp->pmtu, tidlen - flow->tid_offset);
4214*4882a593Smuzhiyun 	flow->sent += *len;
4215*4882a593Smuzhiyun 	next_offset = flow->tid_offset + *len;
4216*4882a593Smuzhiyun 	last_pkt = (flow->tid_idx == (flow->tidcnt - 1) &&
4217*4882a593Smuzhiyun 		    next_offset >= tidlen) || (flow->sent >= flow->length);
4218*4882a593Smuzhiyun 	trace_hfi1_tid_entry_build_write_data(qp, flow->tid_idx, tidentry);
4219*4882a593Smuzhiyun 	trace_hfi1_tid_flow_build_write_data(qp, req->clear_tail, flow);
4220*4882a593Smuzhiyun 
4221*4882a593Smuzhiyun 	rcu_read_lock();
4222*4882a593Smuzhiyun 	remote = rcu_dereference(qpriv->tid_rdma.remote);
4223*4882a593Smuzhiyun 	KDETH_RESET(wd->kdeth0, KVER, 0x1);
4224*4882a593Smuzhiyun 	KDETH_SET(wd->kdeth0, SH, !last_pkt);
4225*4882a593Smuzhiyun 	KDETH_SET(wd->kdeth0, INTR, !!(!last_pkt && remote->urg));
4226*4882a593Smuzhiyun 	KDETH_SET(wd->kdeth0, TIDCTRL, EXP_TID_GET(tidentry, CTRL));
4227*4882a593Smuzhiyun 	KDETH_SET(wd->kdeth0, TID, EXP_TID_GET(tidentry, IDX));
4228*4882a593Smuzhiyun 	KDETH_SET(wd->kdeth0, OM, om == KDETH_OM_LARGE);
4229*4882a593Smuzhiyun 	KDETH_SET(wd->kdeth0, OFFSET, flow->tid_offset / om);
4230*4882a593Smuzhiyun 	KDETH_RESET(wd->kdeth1, JKEY, remote->jkey);
4231*4882a593Smuzhiyun 	wd->verbs_qp = cpu_to_be32(qp->remote_qpn);
4232*4882a593Smuzhiyun 	rcu_read_unlock();
4233*4882a593Smuzhiyun 
4234*4882a593Smuzhiyun 	*bth1 = flow->tid_qpn;
4235*4882a593Smuzhiyun 	*bth2 = mask_psn(((flow->flow_state.spsn + flow->pkt++) &
4236*4882a593Smuzhiyun 			 HFI1_KDETH_BTH_SEQ_MASK) |
4237*4882a593Smuzhiyun 			 (flow->flow_state.generation <<
4238*4882a593Smuzhiyun 			  HFI1_KDETH_BTH_SEQ_SHIFT));
4239*4882a593Smuzhiyun 	if (last_pkt) {
4240*4882a593Smuzhiyun 		/* PSNs are zero-based, so +1 to count number of packets */
4241*4882a593Smuzhiyun 		if (flow->flow_state.lpsn + 1 +
4242*4882a593Smuzhiyun 		    rvt_div_round_up_mtu(qp, req->seg_len) >
4243*4882a593Smuzhiyun 		    MAX_TID_FLOW_PSN)
4244*4882a593Smuzhiyun 			req->state = TID_REQUEST_SYNC;
4245*4882a593Smuzhiyun 		*bth2 |= IB_BTH_REQ_ACK;
4246*4882a593Smuzhiyun 	}
4247*4882a593Smuzhiyun 
4248*4882a593Smuzhiyun 	if (next_offset >= tidlen) {
4249*4882a593Smuzhiyun 		flow->tid_offset = 0;
4250*4882a593Smuzhiyun 		flow->tid_idx++;
4251*4882a593Smuzhiyun 	} else {
4252*4882a593Smuzhiyun 		flow->tid_offset = next_offset;
4253*4882a593Smuzhiyun 	}
4254*4882a593Smuzhiyun 	return last_pkt;
4255*4882a593Smuzhiyun }
4256*4882a593Smuzhiyun 
hfi1_rc_rcv_tid_rdma_write_data(struct hfi1_packet * packet)4257*4882a593Smuzhiyun void hfi1_rc_rcv_tid_rdma_write_data(struct hfi1_packet *packet)
4258*4882a593Smuzhiyun {
4259*4882a593Smuzhiyun 	struct rvt_qp *qp = packet->qp;
4260*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
4261*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = priv->rcd;
4262*4882a593Smuzhiyun 	struct ib_other_headers *ohdr = packet->ohdr;
4263*4882a593Smuzhiyun 	struct rvt_ack_entry *e;
4264*4882a593Smuzhiyun 	struct tid_rdma_request *req;
4265*4882a593Smuzhiyun 	struct tid_rdma_flow *flow;
4266*4882a593Smuzhiyun 	struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
4267*4882a593Smuzhiyun 	unsigned long flags;
4268*4882a593Smuzhiyun 	u32 psn, next;
4269*4882a593Smuzhiyun 	u8 opcode;
4270*4882a593Smuzhiyun 	bool fecn;
4271*4882a593Smuzhiyun 
4272*4882a593Smuzhiyun 	fecn = process_ecn(qp, packet);
4273*4882a593Smuzhiyun 	psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
4274*4882a593Smuzhiyun 	opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0xff;
4275*4882a593Smuzhiyun 
4276*4882a593Smuzhiyun 	/*
4277*4882a593Smuzhiyun 	 * All error handling should be done by now. If we are here, the packet
4278*4882a593Smuzhiyun 	 * is either good or been accepted by the error handler.
4279*4882a593Smuzhiyun 	 */
4280*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->s_lock, flags);
4281*4882a593Smuzhiyun 	e = &qp->s_ack_queue[priv->r_tid_tail];
4282*4882a593Smuzhiyun 	req = ack_to_tid_req(e);
4283*4882a593Smuzhiyun 	flow = &req->flows[req->clear_tail];
4284*4882a593Smuzhiyun 	if (cmp_psn(psn, full_flow_psn(flow, flow->flow_state.lpsn))) {
4285*4882a593Smuzhiyun 		update_r_next_psn_fecn(packet, priv, rcd, flow, fecn);
4286*4882a593Smuzhiyun 
4287*4882a593Smuzhiyun 		if (cmp_psn(psn, flow->flow_state.r_next_psn))
4288*4882a593Smuzhiyun 			goto send_nak;
4289*4882a593Smuzhiyun 
4290*4882a593Smuzhiyun 		flow->flow_state.r_next_psn = mask_psn(psn + 1);
4291*4882a593Smuzhiyun 		/*
4292*4882a593Smuzhiyun 		 * Copy the payload to destination buffer if this packet is
4293*4882a593Smuzhiyun 		 * delivered as an eager packet due to RSM rule and FECN.
4294*4882a593Smuzhiyun 		 * The RSM rule selects FECN bit in BTH and SH bit in
4295*4882a593Smuzhiyun 		 * KDETH header and therefore will not match the last
4296*4882a593Smuzhiyun 		 * packet of each segment that has SH bit cleared.
4297*4882a593Smuzhiyun 		 */
4298*4882a593Smuzhiyun 		if (fecn && packet->etype == RHF_RCV_TYPE_EAGER) {
4299*4882a593Smuzhiyun 			struct rvt_sge_state ss;
4300*4882a593Smuzhiyun 			u32 len;
4301*4882a593Smuzhiyun 			u32 tlen = packet->tlen;
4302*4882a593Smuzhiyun 			u16 hdrsize = packet->hlen;
4303*4882a593Smuzhiyun 			u8 pad = packet->pad;
4304*4882a593Smuzhiyun 			u8 extra_bytes = pad + packet->extra_byte +
4305*4882a593Smuzhiyun 				(SIZE_OF_CRC << 2);
4306*4882a593Smuzhiyun 			u32 pmtu = qp->pmtu;
4307*4882a593Smuzhiyun 
4308*4882a593Smuzhiyun 			if (unlikely(tlen != (hdrsize + pmtu + extra_bytes)))
4309*4882a593Smuzhiyun 				goto send_nak;
4310*4882a593Smuzhiyun 			len = req->comp_seg * req->seg_len;
4311*4882a593Smuzhiyun 			len += delta_psn(psn,
4312*4882a593Smuzhiyun 				full_flow_psn(flow, flow->flow_state.spsn)) *
4313*4882a593Smuzhiyun 				pmtu;
4314*4882a593Smuzhiyun 			if (unlikely(req->total_len - len < pmtu))
4315*4882a593Smuzhiyun 				goto send_nak;
4316*4882a593Smuzhiyun 
4317*4882a593Smuzhiyun 			/*
4318*4882a593Smuzhiyun 			 * The e->rdma_sge field is set when TID RDMA WRITE REQ
4319*4882a593Smuzhiyun 			 * is first received and is never modified thereafter.
4320*4882a593Smuzhiyun 			 */
4321*4882a593Smuzhiyun 			ss.sge = e->rdma_sge;
4322*4882a593Smuzhiyun 			ss.sg_list = NULL;
4323*4882a593Smuzhiyun 			ss.num_sge = 1;
4324*4882a593Smuzhiyun 			ss.total_len = req->total_len;
4325*4882a593Smuzhiyun 			rvt_skip_sge(&ss, len, false);
4326*4882a593Smuzhiyun 			rvt_copy_sge(qp, &ss, packet->payload, pmtu, false,
4327*4882a593Smuzhiyun 				     false);
4328*4882a593Smuzhiyun 			/* Raise the sw sequence check flag for next packet */
4329*4882a593Smuzhiyun 			priv->r_next_psn_kdeth = mask_psn(psn + 1);
4330*4882a593Smuzhiyun 			priv->s_flags |= HFI1_R_TID_SW_PSN;
4331*4882a593Smuzhiyun 		}
4332*4882a593Smuzhiyun 		goto exit;
4333*4882a593Smuzhiyun 	}
4334*4882a593Smuzhiyun 	flow->flow_state.r_next_psn = mask_psn(psn + 1);
4335*4882a593Smuzhiyun 	hfi1_kern_exp_rcv_clear(req);
4336*4882a593Smuzhiyun 	priv->alloc_w_segs--;
4337*4882a593Smuzhiyun 	rcd->flows[flow->idx].psn = psn & HFI1_KDETH_BTH_SEQ_MASK;
4338*4882a593Smuzhiyun 	req->comp_seg++;
4339*4882a593Smuzhiyun 	priv->s_nak_state = 0;
4340*4882a593Smuzhiyun 
4341*4882a593Smuzhiyun 	/*
4342*4882a593Smuzhiyun 	 * Release the flow if one of the following conditions has been met:
4343*4882a593Smuzhiyun 	 *  - The request has reached a sync point AND all outstanding
4344*4882a593Smuzhiyun 	 *    segments have been completed, or
4345*4882a593Smuzhiyun 	 *  - The entire request is complete and there are no more requests
4346*4882a593Smuzhiyun 	 *    (of any kind) in the queue.
4347*4882a593Smuzhiyun 	 */
4348*4882a593Smuzhiyun 	trace_hfi1_rsp_rcv_tid_write_data(qp, psn);
4349*4882a593Smuzhiyun 	trace_hfi1_tid_req_rcv_write_data(qp, 0, e->opcode, e->psn, e->lpsn,
4350*4882a593Smuzhiyun 					  req);
4351*4882a593Smuzhiyun 	trace_hfi1_tid_write_rsp_rcv_data(qp);
4352*4882a593Smuzhiyun 	validate_r_tid_ack(priv);
4353*4882a593Smuzhiyun 
4354*4882a593Smuzhiyun 	if (opcode == TID_OP(WRITE_DATA_LAST)) {
4355*4882a593Smuzhiyun 		release_rdma_sge_mr(e);
4356*4882a593Smuzhiyun 		for (next = priv->r_tid_tail + 1; ; next++) {
4357*4882a593Smuzhiyun 			if (next > rvt_size_atomic(&dev->rdi))
4358*4882a593Smuzhiyun 				next = 0;
4359*4882a593Smuzhiyun 			if (next == priv->r_tid_head)
4360*4882a593Smuzhiyun 				break;
4361*4882a593Smuzhiyun 			e = &qp->s_ack_queue[next];
4362*4882a593Smuzhiyun 			if (e->opcode == TID_OP(WRITE_REQ))
4363*4882a593Smuzhiyun 				break;
4364*4882a593Smuzhiyun 		}
4365*4882a593Smuzhiyun 		priv->r_tid_tail = next;
4366*4882a593Smuzhiyun 		if (++qp->s_acked_ack_queue > rvt_size_atomic(&dev->rdi))
4367*4882a593Smuzhiyun 			qp->s_acked_ack_queue = 0;
4368*4882a593Smuzhiyun 	}
4369*4882a593Smuzhiyun 
4370*4882a593Smuzhiyun 	hfi1_tid_write_alloc_resources(qp, true);
4371*4882a593Smuzhiyun 
4372*4882a593Smuzhiyun 	/*
4373*4882a593Smuzhiyun 	 * If we need to generate more responses, schedule the
4374*4882a593Smuzhiyun 	 * send engine.
4375*4882a593Smuzhiyun 	 */
4376*4882a593Smuzhiyun 	if (req->cur_seg < req->total_segs ||
4377*4882a593Smuzhiyun 	    qp->s_tail_ack_queue != qp->r_head_ack_queue) {
4378*4882a593Smuzhiyun 		qp->s_flags |= RVT_S_RESP_PENDING;
4379*4882a593Smuzhiyun 		hfi1_schedule_send(qp);
4380*4882a593Smuzhiyun 	}
4381*4882a593Smuzhiyun 
4382*4882a593Smuzhiyun 	priv->pending_tid_w_segs--;
4383*4882a593Smuzhiyun 	if (priv->s_flags & HFI1_R_TID_RSC_TIMER) {
4384*4882a593Smuzhiyun 		if (priv->pending_tid_w_segs)
4385*4882a593Smuzhiyun 			hfi1_mod_tid_reap_timer(req->qp);
4386*4882a593Smuzhiyun 		else
4387*4882a593Smuzhiyun 			hfi1_stop_tid_reap_timer(req->qp);
4388*4882a593Smuzhiyun 	}
4389*4882a593Smuzhiyun 
4390*4882a593Smuzhiyun done:
4391*4882a593Smuzhiyun 	tid_rdma_schedule_ack(qp);
4392*4882a593Smuzhiyun exit:
4393*4882a593Smuzhiyun 	priv->r_next_psn_kdeth = flow->flow_state.r_next_psn;
4394*4882a593Smuzhiyun 	if (fecn)
4395*4882a593Smuzhiyun 		qp->s_flags |= RVT_S_ECN;
4396*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, flags);
4397*4882a593Smuzhiyun 	return;
4398*4882a593Smuzhiyun 
4399*4882a593Smuzhiyun send_nak:
4400*4882a593Smuzhiyun 	if (!priv->s_nak_state) {
4401*4882a593Smuzhiyun 		priv->s_nak_state = IB_NAK_PSN_ERROR;
4402*4882a593Smuzhiyun 		priv->s_nak_psn = flow->flow_state.r_next_psn;
4403*4882a593Smuzhiyun 		tid_rdma_trigger_ack(qp);
4404*4882a593Smuzhiyun 	}
4405*4882a593Smuzhiyun 	goto done;
4406*4882a593Smuzhiyun }
4407*4882a593Smuzhiyun 
hfi1_tid_rdma_is_resync_psn(u32 psn)4408*4882a593Smuzhiyun static bool hfi1_tid_rdma_is_resync_psn(u32 psn)
4409*4882a593Smuzhiyun {
4410*4882a593Smuzhiyun 	return (bool)((psn & HFI1_KDETH_BTH_SEQ_MASK) ==
4411*4882a593Smuzhiyun 		      HFI1_KDETH_BTH_SEQ_MASK);
4412*4882a593Smuzhiyun }
4413*4882a593Smuzhiyun 
hfi1_build_tid_rdma_write_ack(struct rvt_qp * qp,struct rvt_ack_entry * e,struct ib_other_headers * ohdr,u16 iflow,u32 * bth1,u32 * bth2)4414*4882a593Smuzhiyun u32 hfi1_build_tid_rdma_write_ack(struct rvt_qp *qp, struct rvt_ack_entry *e,
4415*4882a593Smuzhiyun 				  struct ib_other_headers *ohdr, u16 iflow,
4416*4882a593Smuzhiyun 				  u32 *bth1, u32 *bth2)
4417*4882a593Smuzhiyun {
4418*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
4419*4882a593Smuzhiyun 	struct tid_flow_state *fs = &qpriv->flow_state;
4420*4882a593Smuzhiyun 	struct tid_rdma_request *req = ack_to_tid_req(e);
4421*4882a593Smuzhiyun 	struct tid_rdma_flow *flow = &req->flows[iflow];
4422*4882a593Smuzhiyun 	struct tid_rdma_params *remote;
4423*4882a593Smuzhiyun 
4424*4882a593Smuzhiyun 	rcu_read_lock();
4425*4882a593Smuzhiyun 	remote = rcu_dereference(qpriv->tid_rdma.remote);
4426*4882a593Smuzhiyun 	KDETH_RESET(ohdr->u.tid_rdma.ack.kdeth1, JKEY, remote->jkey);
4427*4882a593Smuzhiyun 	ohdr->u.tid_rdma.ack.verbs_qp = cpu_to_be32(qp->remote_qpn);
4428*4882a593Smuzhiyun 	*bth1 = remote->qp;
4429*4882a593Smuzhiyun 	rcu_read_unlock();
4430*4882a593Smuzhiyun 
4431*4882a593Smuzhiyun 	if (qpriv->resync) {
4432*4882a593Smuzhiyun 		*bth2 = mask_psn((fs->generation <<
4433*4882a593Smuzhiyun 				  HFI1_KDETH_BTH_SEQ_SHIFT) - 1);
4434*4882a593Smuzhiyun 		ohdr->u.tid_rdma.ack.aeth = rvt_compute_aeth(qp);
4435*4882a593Smuzhiyun 	} else if (qpriv->s_nak_state) {
4436*4882a593Smuzhiyun 		*bth2 = mask_psn(qpriv->s_nak_psn);
4437*4882a593Smuzhiyun 		ohdr->u.tid_rdma.ack.aeth =
4438*4882a593Smuzhiyun 			cpu_to_be32((qp->r_msn & IB_MSN_MASK) |
4439*4882a593Smuzhiyun 				    (qpriv->s_nak_state <<
4440*4882a593Smuzhiyun 				     IB_AETH_CREDIT_SHIFT));
4441*4882a593Smuzhiyun 	} else {
4442*4882a593Smuzhiyun 		*bth2 = full_flow_psn(flow, flow->flow_state.lpsn);
4443*4882a593Smuzhiyun 		ohdr->u.tid_rdma.ack.aeth = rvt_compute_aeth(qp);
4444*4882a593Smuzhiyun 	}
4445*4882a593Smuzhiyun 	KDETH_RESET(ohdr->u.tid_rdma.ack.kdeth0, KVER, 0x1);
4446*4882a593Smuzhiyun 	ohdr->u.tid_rdma.ack.tid_flow_qp =
4447*4882a593Smuzhiyun 		cpu_to_be32(qpriv->tid_rdma.local.qp |
4448*4882a593Smuzhiyun 			    ((flow->idx & TID_RDMA_DESTQP_FLOW_MASK) <<
4449*4882a593Smuzhiyun 			     TID_RDMA_DESTQP_FLOW_SHIFT) |
4450*4882a593Smuzhiyun 			    qpriv->rcd->ctxt);
4451*4882a593Smuzhiyun 
4452*4882a593Smuzhiyun 	ohdr->u.tid_rdma.ack.tid_flow_psn = 0;
4453*4882a593Smuzhiyun 	ohdr->u.tid_rdma.ack.verbs_psn =
4454*4882a593Smuzhiyun 		cpu_to_be32(flow->flow_state.resp_ib_psn);
4455*4882a593Smuzhiyun 
4456*4882a593Smuzhiyun 	if (qpriv->resync) {
4457*4882a593Smuzhiyun 		/*
4458*4882a593Smuzhiyun 		 * If the PSN before the current expect KDETH PSN is the
4459*4882a593Smuzhiyun 		 * RESYNC PSN, then we never received a good TID RDMA WRITE
4460*4882a593Smuzhiyun 		 * DATA packet after a previous RESYNC.
4461*4882a593Smuzhiyun 		 * In this case, the next expected KDETH PSN stays the same.
4462*4882a593Smuzhiyun 		 */
4463*4882a593Smuzhiyun 		if (hfi1_tid_rdma_is_resync_psn(qpriv->r_next_psn_kdeth - 1)) {
4464*4882a593Smuzhiyun 			ohdr->u.tid_rdma.ack.tid_flow_psn =
4465*4882a593Smuzhiyun 				cpu_to_be32(qpriv->r_next_psn_kdeth_save);
4466*4882a593Smuzhiyun 		} else {
4467*4882a593Smuzhiyun 			/*
4468*4882a593Smuzhiyun 			 * Because the KDETH PSNs jump during a RESYNC, it's
4469*4882a593Smuzhiyun 			 * not possible to infer (or compute) the previous value
4470*4882a593Smuzhiyun 			 * of r_next_psn_kdeth in the case of back-to-back
4471*4882a593Smuzhiyun 			 * RESYNC packets. Therefore, we save it.
4472*4882a593Smuzhiyun 			 */
4473*4882a593Smuzhiyun 			qpriv->r_next_psn_kdeth_save =
4474*4882a593Smuzhiyun 				qpriv->r_next_psn_kdeth - 1;
4475*4882a593Smuzhiyun 			ohdr->u.tid_rdma.ack.tid_flow_psn =
4476*4882a593Smuzhiyun 				cpu_to_be32(qpriv->r_next_psn_kdeth_save);
4477*4882a593Smuzhiyun 			qpriv->r_next_psn_kdeth = mask_psn(*bth2 + 1);
4478*4882a593Smuzhiyun 		}
4479*4882a593Smuzhiyun 		qpriv->resync = false;
4480*4882a593Smuzhiyun 	}
4481*4882a593Smuzhiyun 
4482*4882a593Smuzhiyun 	return sizeof(ohdr->u.tid_rdma.ack) / sizeof(u32);
4483*4882a593Smuzhiyun }
4484*4882a593Smuzhiyun 
hfi1_rc_rcv_tid_rdma_ack(struct hfi1_packet * packet)4485*4882a593Smuzhiyun void hfi1_rc_rcv_tid_rdma_ack(struct hfi1_packet *packet)
4486*4882a593Smuzhiyun {
4487*4882a593Smuzhiyun 	struct ib_other_headers *ohdr = packet->ohdr;
4488*4882a593Smuzhiyun 	struct rvt_qp *qp = packet->qp;
4489*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
4490*4882a593Smuzhiyun 	struct rvt_swqe *wqe;
4491*4882a593Smuzhiyun 	struct tid_rdma_request *req;
4492*4882a593Smuzhiyun 	struct tid_rdma_flow *flow;
4493*4882a593Smuzhiyun 	u32 aeth, psn, req_psn, ack_psn, flpsn, resync_psn, ack_kpsn;
4494*4882a593Smuzhiyun 	unsigned long flags;
4495*4882a593Smuzhiyun 	u16 fidx;
4496*4882a593Smuzhiyun 
4497*4882a593Smuzhiyun 	trace_hfi1_tid_write_sender_rcv_tid_ack(qp, 0);
4498*4882a593Smuzhiyun 	process_ecn(qp, packet);
4499*4882a593Smuzhiyun 	psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
4500*4882a593Smuzhiyun 	aeth = be32_to_cpu(ohdr->u.tid_rdma.ack.aeth);
4501*4882a593Smuzhiyun 	req_psn = mask_psn(be32_to_cpu(ohdr->u.tid_rdma.ack.verbs_psn));
4502*4882a593Smuzhiyun 	resync_psn = mask_psn(be32_to_cpu(ohdr->u.tid_rdma.ack.tid_flow_psn));
4503*4882a593Smuzhiyun 
4504*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->s_lock, flags);
4505*4882a593Smuzhiyun 	trace_hfi1_rcv_tid_ack(qp, aeth, psn, req_psn, resync_psn);
4506*4882a593Smuzhiyun 
4507*4882a593Smuzhiyun 	/* If we are waiting for an ACK to RESYNC, drop any other packets */
4508*4882a593Smuzhiyun 	if ((qp->s_flags & HFI1_S_WAIT_HALT) &&
4509*4882a593Smuzhiyun 	    cmp_psn(psn, qpriv->s_resync_psn))
4510*4882a593Smuzhiyun 		goto ack_op_err;
4511*4882a593Smuzhiyun 
4512*4882a593Smuzhiyun 	ack_psn = req_psn;
4513*4882a593Smuzhiyun 	if (hfi1_tid_rdma_is_resync_psn(psn))
4514*4882a593Smuzhiyun 		ack_kpsn = resync_psn;
4515*4882a593Smuzhiyun 	else
4516*4882a593Smuzhiyun 		ack_kpsn = psn;
4517*4882a593Smuzhiyun 	if (aeth >> 29) {
4518*4882a593Smuzhiyun 		ack_psn--;
4519*4882a593Smuzhiyun 		ack_kpsn--;
4520*4882a593Smuzhiyun 	}
4521*4882a593Smuzhiyun 
4522*4882a593Smuzhiyun 	if (unlikely(qp->s_acked == qp->s_tail))
4523*4882a593Smuzhiyun 		goto ack_op_err;
4524*4882a593Smuzhiyun 
4525*4882a593Smuzhiyun 	wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
4526*4882a593Smuzhiyun 
4527*4882a593Smuzhiyun 	if (wqe->wr.opcode != IB_WR_TID_RDMA_WRITE)
4528*4882a593Smuzhiyun 		goto ack_op_err;
4529*4882a593Smuzhiyun 
4530*4882a593Smuzhiyun 	req = wqe_to_tid_req(wqe);
4531*4882a593Smuzhiyun 	trace_hfi1_tid_req_rcv_tid_ack(qp, 0, wqe->wr.opcode, wqe->psn,
4532*4882a593Smuzhiyun 				       wqe->lpsn, req);
4533*4882a593Smuzhiyun 	flow = &req->flows[req->acked_tail];
4534*4882a593Smuzhiyun 	trace_hfi1_tid_flow_rcv_tid_ack(qp, req->acked_tail, flow);
4535*4882a593Smuzhiyun 
4536*4882a593Smuzhiyun 	/* Drop stale ACK/NAK */
4537*4882a593Smuzhiyun 	if (cmp_psn(psn, full_flow_psn(flow, flow->flow_state.spsn)) < 0 ||
4538*4882a593Smuzhiyun 	    cmp_psn(req_psn, flow->flow_state.resp_ib_psn) < 0)
4539*4882a593Smuzhiyun 		goto ack_op_err;
4540*4882a593Smuzhiyun 
4541*4882a593Smuzhiyun 	while (cmp_psn(ack_kpsn,
4542*4882a593Smuzhiyun 		       full_flow_psn(flow, flow->flow_state.lpsn)) >= 0 &&
4543*4882a593Smuzhiyun 	       req->ack_seg < req->cur_seg) {
4544*4882a593Smuzhiyun 		req->ack_seg++;
4545*4882a593Smuzhiyun 		/* advance acked segment pointer */
4546*4882a593Smuzhiyun 		req->acked_tail = CIRC_NEXT(req->acked_tail, MAX_FLOWS);
4547*4882a593Smuzhiyun 		req->r_last_acked = flow->flow_state.resp_ib_psn;
4548*4882a593Smuzhiyun 		trace_hfi1_tid_req_rcv_tid_ack(qp, 0, wqe->wr.opcode, wqe->psn,
4549*4882a593Smuzhiyun 					       wqe->lpsn, req);
4550*4882a593Smuzhiyun 		if (req->ack_seg == req->total_segs) {
4551*4882a593Smuzhiyun 			req->state = TID_REQUEST_COMPLETE;
4552*4882a593Smuzhiyun 			wqe = do_rc_completion(qp, wqe,
4553*4882a593Smuzhiyun 					       to_iport(qp->ibqp.device,
4554*4882a593Smuzhiyun 							qp->port_num));
4555*4882a593Smuzhiyun 			trace_hfi1_sender_rcv_tid_ack(qp);
4556*4882a593Smuzhiyun 			atomic_dec(&qpriv->n_tid_requests);
4557*4882a593Smuzhiyun 			if (qp->s_acked == qp->s_tail)
4558*4882a593Smuzhiyun 				break;
4559*4882a593Smuzhiyun 			if (wqe->wr.opcode != IB_WR_TID_RDMA_WRITE)
4560*4882a593Smuzhiyun 				break;
4561*4882a593Smuzhiyun 			req = wqe_to_tid_req(wqe);
4562*4882a593Smuzhiyun 		}
4563*4882a593Smuzhiyun 		flow = &req->flows[req->acked_tail];
4564*4882a593Smuzhiyun 		trace_hfi1_tid_flow_rcv_tid_ack(qp, req->acked_tail, flow);
4565*4882a593Smuzhiyun 	}
4566*4882a593Smuzhiyun 
4567*4882a593Smuzhiyun 	trace_hfi1_tid_req_rcv_tid_ack(qp, 0, wqe->wr.opcode, wqe->psn,
4568*4882a593Smuzhiyun 				       wqe->lpsn, req);
4569*4882a593Smuzhiyun 	switch (aeth >> 29) {
4570*4882a593Smuzhiyun 	case 0:         /* ACK */
4571*4882a593Smuzhiyun 		if (qpriv->s_flags & RVT_S_WAIT_ACK)
4572*4882a593Smuzhiyun 			qpriv->s_flags &= ~RVT_S_WAIT_ACK;
4573*4882a593Smuzhiyun 		if (!hfi1_tid_rdma_is_resync_psn(psn)) {
4574*4882a593Smuzhiyun 			/* Check if there is any pending TID ACK */
4575*4882a593Smuzhiyun 			if (wqe->wr.opcode == IB_WR_TID_RDMA_WRITE &&
4576*4882a593Smuzhiyun 			    req->ack_seg < req->cur_seg)
4577*4882a593Smuzhiyun 				hfi1_mod_tid_retry_timer(qp);
4578*4882a593Smuzhiyun 			else
4579*4882a593Smuzhiyun 				hfi1_stop_tid_retry_timer(qp);
4580*4882a593Smuzhiyun 			hfi1_schedule_send(qp);
4581*4882a593Smuzhiyun 		} else {
4582*4882a593Smuzhiyun 			u32 spsn, fpsn, last_acked, generation;
4583*4882a593Smuzhiyun 			struct tid_rdma_request *rptr;
4584*4882a593Smuzhiyun 
4585*4882a593Smuzhiyun 			/* ACK(RESYNC) */
4586*4882a593Smuzhiyun 			hfi1_stop_tid_retry_timer(qp);
4587*4882a593Smuzhiyun 			/* Allow new requests (see hfi1_make_tid_rdma_pkt) */
4588*4882a593Smuzhiyun 			qp->s_flags &= ~HFI1_S_WAIT_HALT;
4589*4882a593Smuzhiyun 			/*
4590*4882a593Smuzhiyun 			 * Clear RVT_S_SEND_ONE flag in case that the TID RDMA
4591*4882a593Smuzhiyun 			 * ACK is received after the TID retry timer is fired
4592*4882a593Smuzhiyun 			 * again. In this case, do not send any more TID
4593*4882a593Smuzhiyun 			 * RESYNC request or wait for any more TID ACK packet.
4594*4882a593Smuzhiyun 			 */
4595*4882a593Smuzhiyun 			qpriv->s_flags &= ~RVT_S_SEND_ONE;
4596*4882a593Smuzhiyun 			hfi1_schedule_send(qp);
4597*4882a593Smuzhiyun 
4598*4882a593Smuzhiyun 			if ((qp->s_acked == qpriv->s_tid_tail &&
4599*4882a593Smuzhiyun 			     req->ack_seg == req->total_segs) ||
4600*4882a593Smuzhiyun 			    qp->s_acked == qp->s_tail) {
4601*4882a593Smuzhiyun 				qpriv->s_state = TID_OP(WRITE_DATA_LAST);
4602*4882a593Smuzhiyun 				goto done;
4603*4882a593Smuzhiyun 			}
4604*4882a593Smuzhiyun 
4605*4882a593Smuzhiyun 			if (req->ack_seg == req->comp_seg) {
4606*4882a593Smuzhiyun 				qpriv->s_state = TID_OP(WRITE_DATA);
4607*4882a593Smuzhiyun 				goto done;
4608*4882a593Smuzhiyun 			}
4609*4882a593Smuzhiyun 
4610*4882a593Smuzhiyun 			/*
4611*4882a593Smuzhiyun 			 * The PSN to start with is the next PSN after the
4612*4882a593Smuzhiyun 			 * RESYNC PSN.
4613*4882a593Smuzhiyun 			 */
4614*4882a593Smuzhiyun 			psn = mask_psn(psn + 1);
4615*4882a593Smuzhiyun 			generation = psn >> HFI1_KDETH_BTH_SEQ_SHIFT;
4616*4882a593Smuzhiyun 			spsn = 0;
4617*4882a593Smuzhiyun 
4618*4882a593Smuzhiyun 			/*
4619*4882a593Smuzhiyun 			 * Update to the correct WQE when we get an ACK(RESYNC)
4620*4882a593Smuzhiyun 			 * in the middle of a request.
4621*4882a593Smuzhiyun 			 */
4622*4882a593Smuzhiyun 			if (delta_psn(ack_psn, wqe->lpsn))
4623*4882a593Smuzhiyun 				wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
4624*4882a593Smuzhiyun 			req = wqe_to_tid_req(wqe);
4625*4882a593Smuzhiyun 			flow = &req->flows[req->acked_tail];
4626*4882a593Smuzhiyun 			/*
4627*4882a593Smuzhiyun 			 * RESYNC re-numbers the PSN ranges of all remaining
4628*4882a593Smuzhiyun 			 * segments. Also, PSN's start from 0 in the middle of a
4629*4882a593Smuzhiyun 			 * segment and the first segment size is less than the
4630*4882a593Smuzhiyun 			 * default number of packets. flow->resync_npkts is used
4631*4882a593Smuzhiyun 			 * to track the number of packets from the start of the
4632*4882a593Smuzhiyun 			 * real segment to the point of 0 PSN after the RESYNC
4633*4882a593Smuzhiyun 			 * in order to later correctly rewind the SGE.
4634*4882a593Smuzhiyun 			 */
4635*4882a593Smuzhiyun 			fpsn = full_flow_psn(flow, flow->flow_state.spsn);
4636*4882a593Smuzhiyun 			req->r_ack_psn = psn;
4637*4882a593Smuzhiyun 			/*
4638*4882a593Smuzhiyun 			 * If resync_psn points to the last flow PSN for a
4639*4882a593Smuzhiyun 			 * segment and the new segment (likely from a new
4640*4882a593Smuzhiyun 			 * request) starts with a new generation number, we
4641*4882a593Smuzhiyun 			 * need to adjust resync_psn accordingly.
4642*4882a593Smuzhiyun 			 */
4643*4882a593Smuzhiyun 			if (flow->flow_state.generation !=
4644*4882a593Smuzhiyun 			    (resync_psn >> HFI1_KDETH_BTH_SEQ_SHIFT))
4645*4882a593Smuzhiyun 				resync_psn = mask_psn(fpsn - 1);
4646*4882a593Smuzhiyun 			flow->resync_npkts +=
4647*4882a593Smuzhiyun 				delta_psn(mask_psn(resync_psn + 1), fpsn);
4648*4882a593Smuzhiyun 			/*
4649*4882a593Smuzhiyun 			 * Renumber all packet sequence number ranges
4650*4882a593Smuzhiyun 			 * based on the new generation.
4651*4882a593Smuzhiyun 			 */
4652*4882a593Smuzhiyun 			last_acked = qp->s_acked;
4653*4882a593Smuzhiyun 			rptr = req;
4654*4882a593Smuzhiyun 			while (1) {
4655*4882a593Smuzhiyun 				/* start from last acked segment */
4656*4882a593Smuzhiyun 				for (fidx = rptr->acked_tail;
4657*4882a593Smuzhiyun 				     CIRC_CNT(rptr->setup_head, fidx,
4658*4882a593Smuzhiyun 					      MAX_FLOWS);
4659*4882a593Smuzhiyun 				     fidx = CIRC_NEXT(fidx, MAX_FLOWS)) {
4660*4882a593Smuzhiyun 					u32 lpsn;
4661*4882a593Smuzhiyun 					u32 gen;
4662*4882a593Smuzhiyun 
4663*4882a593Smuzhiyun 					flow = &rptr->flows[fidx];
4664*4882a593Smuzhiyun 					gen = flow->flow_state.generation;
4665*4882a593Smuzhiyun 					if (WARN_ON(gen == generation &&
4666*4882a593Smuzhiyun 						    flow->flow_state.spsn !=
4667*4882a593Smuzhiyun 						     spsn))
4668*4882a593Smuzhiyun 						continue;
4669*4882a593Smuzhiyun 					lpsn = flow->flow_state.lpsn;
4670*4882a593Smuzhiyun 					lpsn = full_flow_psn(flow, lpsn);
4671*4882a593Smuzhiyun 					flow->npkts =
4672*4882a593Smuzhiyun 						delta_psn(lpsn,
4673*4882a593Smuzhiyun 							  mask_psn(resync_psn)
4674*4882a593Smuzhiyun 							  );
4675*4882a593Smuzhiyun 					flow->flow_state.generation =
4676*4882a593Smuzhiyun 						generation;
4677*4882a593Smuzhiyun 					flow->flow_state.spsn = spsn;
4678*4882a593Smuzhiyun 					flow->flow_state.lpsn =
4679*4882a593Smuzhiyun 						flow->flow_state.spsn +
4680*4882a593Smuzhiyun 						flow->npkts - 1;
4681*4882a593Smuzhiyun 					flow->pkt = 0;
4682*4882a593Smuzhiyun 					spsn += flow->npkts;
4683*4882a593Smuzhiyun 					resync_psn += flow->npkts;
4684*4882a593Smuzhiyun 					trace_hfi1_tid_flow_rcv_tid_ack(qp,
4685*4882a593Smuzhiyun 									fidx,
4686*4882a593Smuzhiyun 									flow);
4687*4882a593Smuzhiyun 				}
4688*4882a593Smuzhiyun 				if (++last_acked == qpriv->s_tid_cur + 1)
4689*4882a593Smuzhiyun 					break;
4690*4882a593Smuzhiyun 				if (last_acked == qp->s_size)
4691*4882a593Smuzhiyun 					last_acked = 0;
4692*4882a593Smuzhiyun 				wqe = rvt_get_swqe_ptr(qp, last_acked);
4693*4882a593Smuzhiyun 				rptr = wqe_to_tid_req(wqe);
4694*4882a593Smuzhiyun 			}
4695*4882a593Smuzhiyun 			req->cur_seg = req->ack_seg;
4696*4882a593Smuzhiyun 			qpriv->s_tid_tail = qp->s_acked;
4697*4882a593Smuzhiyun 			qpriv->s_state = TID_OP(WRITE_REQ);
4698*4882a593Smuzhiyun 			hfi1_schedule_tid_send(qp);
4699*4882a593Smuzhiyun 		}
4700*4882a593Smuzhiyun done:
4701*4882a593Smuzhiyun 		qpriv->s_retry = qp->s_retry_cnt;
4702*4882a593Smuzhiyun 		break;
4703*4882a593Smuzhiyun 
4704*4882a593Smuzhiyun 	case 3:         /* NAK */
4705*4882a593Smuzhiyun 		hfi1_stop_tid_retry_timer(qp);
4706*4882a593Smuzhiyun 		switch ((aeth >> IB_AETH_CREDIT_SHIFT) &
4707*4882a593Smuzhiyun 			IB_AETH_CREDIT_MASK) {
4708*4882a593Smuzhiyun 		case 0: /* PSN sequence error */
4709*4882a593Smuzhiyun 			if (!req->flows)
4710*4882a593Smuzhiyun 				break;
4711*4882a593Smuzhiyun 			flow = &req->flows[req->acked_tail];
4712*4882a593Smuzhiyun 			flpsn = full_flow_psn(flow, flow->flow_state.lpsn);
4713*4882a593Smuzhiyun 			if (cmp_psn(psn, flpsn) > 0)
4714*4882a593Smuzhiyun 				break;
4715*4882a593Smuzhiyun 			trace_hfi1_tid_flow_rcv_tid_ack(qp, req->acked_tail,
4716*4882a593Smuzhiyun 							flow);
4717*4882a593Smuzhiyun 			req->r_ack_psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
4718*4882a593Smuzhiyun 			req->cur_seg = req->ack_seg;
4719*4882a593Smuzhiyun 			qpriv->s_tid_tail = qp->s_acked;
4720*4882a593Smuzhiyun 			qpriv->s_state = TID_OP(WRITE_REQ);
4721*4882a593Smuzhiyun 			qpriv->s_retry = qp->s_retry_cnt;
4722*4882a593Smuzhiyun 			hfi1_schedule_tid_send(qp);
4723*4882a593Smuzhiyun 			break;
4724*4882a593Smuzhiyun 
4725*4882a593Smuzhiyun 		default:
4726*4882a593Smuzhiyun 			break;
4727*4882a593Smuzhiyun 		}
4728*4882a593Smuzhiyun 		break;
4729*4882a593Smuzhiyun 
4730*4882a593Smuzhiyun 	default:
4731*4882a593Smuzhiyun 		break;
4732*4882a593Smuzhiyun 	}
4733*4882a593Smuzhiyun 
4734*4882a593Smuzhiyun ack_op_err:
4735*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, flags);
4736*4882a593Smuzhiyun }
4737*4882a593Smuzhiyun 
hfi1_add_tid_retry_timer(struct rvt_qp * qp)4738*4882a593Smuzhiyun void hfi1_add_tid_retry_timer(struct rvt_qp *qp)
4739*4882a593Smuzhiyun {
4740*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
4741*4882a593Smuzhiyun 	struct ib_qp *ibqp = &qp->ibqp;
4742*4882a593Smuzhiyun 	struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
4743*4882a593Smuzhiyun 
4744*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
4745*4882a593Smuzhiyun 	if (!(priv->s_flags & HFI1_S_TID_RETRY_TIMER)) {
4746*4882a593Smuzhiyun 		priv->s_flags |= HFI1_S_TID_RETRY_TIMER;
4747*4882a593Smuzhiyun 		priv->s_tid_retry_timer.expires = jiffies +
4748*4882a593Smuzhiyun 			priv->tid_retry_timeout_jiffies + rdi->busy_jiffies;
4749*4882a593Smuzhiyun 		add_timer(&priv->s_tid_retry_timer);
4750*4882a593Smuzhiyun 	}
4751*4882a593Smuzhiyun }
4752*4882a593Smuzhiyun 
hfi1_mod_tid_retry_timer(struct rvt_qp * qp)4753*4882a593Smuzhiyun static void hfi1_mod_tid_retry_timer(struct rvt_qp *qp)
4754*4882a593Smuzhiyun {
4755*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
4756*4882a593Smuzhiyun 	struct ib_qp *ibqp = &qp->ibqp;
4757*4882a593Smuzhiyun 	struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
4758*4882a593Smuzhiyun 
4759*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
4760*4882a593Smuzhiyun 	priv->s_flags |= HFI1_S_TID_RETRY_TIMER;
4761*4882a593Smuzhiyun 	mod_timer(&priv->s_tid_retry_timer, jiffies +
4762*4882a593Smuzhiyun 		  priv->tid_retry_timeout_jiffies + rdi->busy_jiffies);
4763*4882a593Smuzhiyun }
4764*4882a593Smuzhiyun 
hfi1_stop_tid_retry_timer(struct rvt_qp * qp)4765*4882a593Smuzhiyun static int hfi1_stop_tid_retry_timer(struct rvt_qp *qp)
4766*4882a593Smuzhiyun {
4767*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
4768*4882a593Smuzhiyun 	int rval = 0;
4769*4882a593Smuzhiyun 
4770*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
4771*4882a593Smuzhiyun 	if (priv->s_flags & HFI1_S_TID_RETRY_TIMER) {
4772*4882a593Smuzhiyun 		rval = del_timer(&priv->s_tid_retry_timer);
4773*4882a593Smuzhiyun 		priv->s_flags &= ~HFI1_S_TID_RETRY_TIMER;
4774*4882a593Smuzhiyun 	}
4775*4882a593Smuzhiyun 	return rval;
4776*4882a593Smuzhiyun }
4777*4882a593Smuzhiyun 
hfi1_del_tid_retry_timer(struct rvt_qp * qp)4778*4882a593Smuzhiyun void hfi1_del_tid_retry_timer(struct rvt_qp *qp)
4779*4882a593Smuzhiyun {
4780*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
4781*4882a593Smuzhiyun 
4782*4882a593Smuzhiyun 	del_timer_sync(&priv->s_tid_retry_timer);
4783*4882a593Smuzhiyun 	priv->s_flags &= ~HFI1_S_TID_RETRY_TIMER;
4784*4882a593Smuzhiyun }
4785*4882a593Smuzhiyun 
hfi1_tid_retry_timeout(struct timer_list * t)4786*4882a593Smuzhiyun static void hfi1_tid_retry_timeout(struct timer_list *t)
4787*4882a593Smuzhiyun {
4788*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = from_timer(priv, t, s_tid_retry_timer);
4789*4882a593Smuzhiyun 	struct rvt_qp *qp = priv->owner;
4790*4882a593Smuzhiyun 	struct rvt_swqe *wqe;
4791*4882a593Smuzhiyun 	unsigned long flags;
4792*4882a593Smuzhiyun 	struct tid_rdma_request *req;
4793*4882a593Smuzhiyun 
4794*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->r_lock, flags);
4795*4882a593Smuzhiyun 	spin_lock(&qp->s_lock);
4796*4882a593Smuzhiyun 	trace_hfi1_tid_write_sender_retry_timeout(qp, 0);
4797*4882a593Smuzhiyun 	if (priv->s_flags & HFI1_S_TID_RETRY_TIMER) {
4798*4882a593Smuzhiyun 		hfi1_stop_tid_retry_timer(qp);
4799*4882a593Smuzhiyun 		if (!priv->s_retry) {
4800*4882a593Smuzhiyun 			trace_hfi1_msg_tid_retry_timeout(/* msg */
4801*4882a593Smuzhiyun 				qp,
4802*4882a593Smuzhiyun 				"Exhausted retries. Tid retry timeout = ",
4803*4882a593Smuzhiyun 				(u64)priv->tid_retry_timeout_jiffies);
4804*4882a593Smuzhiyun 
4805*4882a593Smuzhiyun 			wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
4806*4882a593Smuzhiyun 			hfi1_trdma_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
4807*4882a593Smuzhiyun 			rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
4808*4882a593Smuzhiyun 		} else {
4809*4882a593Smuzhiyun 			wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
4810*4882a593Smuzhiyun 			req = wqe_to_tid_req(wqe);
4811*4882a593Smuzhiyun 			trace_hfi1_tid_req_tid_retry_timeout(/* req */
4812*4882a593Smuzhiyun 			   qp, 0, wqe->wr.opcode, wqe->psn, wqe->lpsn, req);
4813*4882a593Smuzhiyun 
4814*4882a593Smuzhiyun 			priv->s_flags &= ~RVT_S_WAIT_ACK;
4815*4882a593Smuzhiyun 			/* Only send one packet (the RESYNC) */
4816*4882a593Smuzhiyun 			priv->s_flags |= RVT_S_SEND_ONE;
4817*4882a593Smuzhiyun 			/*
4818*4882a593Smuzhiyun 			 * No additional request shall be made by this QP until
4819*4882a593Smuzhiyun 			 * the RESYNC has been complete.
4820*4882a593Smuzhiyun 			 */
4821*4882a593Smuzhiyun 			qp->s_flags |= HFI1_S_WAIT_HALT;
4822*4882a593Smuzhiyun 			priv->s_state = TID_OP(RESYNC);
4823*4882a593Smuzhiyun 			priv->s_retry--;
4824*4882a593Smuzhiyun 			hfi1_schedule_tid_send(qp);
4825*4882a593Smuzhiyun 		}
4826*4882a593Smuzhiyun 	}
4827*4882a593Smuzhiyun 	spin_unlock(&qp->s_lock);
4828*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->r_lock, flags);
4829*4882a593Smuzhiyun }
4830*4882a593Smuzhiyun 
hfi1_build_tid_rdma_resync(struct rvt_qp * qp,struct rvt_swqe * wqe,struct ib_other_headers * ohdr,u32 * bth1,u32 * bth2,u16 fidx)4831*4882a593Smuzhiyun u32 hfi1_build_tid_rdma_resync(struct rvt_qp *qp, struct rvt_swqe *wqe,
4832*4882a593Smuzhiyun 			       struct ib_other_headers *ohdr, u32 *bth1,
4833*4882a593Smuzhiyun 			       u32 *bth2, u16 fidx)
4834*4882a593Smuzhiyun {
4835*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
4836*4882a593Smuzhiyun 	struct tid_rdma_params *remote;
4837*4882a593Smuzhiyun 	struct tid_rdma_request *req = wqe_to_tid_req(wqe);
4838*4882a593Smuzhiyun 	struct tid_rdma_flow *flow = &req->flows[fidx];
4839*4882a593Smuzhiyun 	u32 generation;
4840*4882a593Smuzhiyun 
4841*4882a593Smuzhiyun 	rcu_read_lock();
4842*4882a593Smuzhiyun 	remote = rcu_dereference(qpriv->tid_rdma.remote);
4843*4882a593Smuzhiyun 	KDETH_RESET(ohdr->u.tid_rdma.ack.kdeth1, JKEY, remote->jkey);
4844*4882a593Smuzhiyun 	ohdr->u.tid_rdma.ack.verbs_qp = cpu_to_be32(qp->remote_qpn);
4845*4882a593Smuzhiyun 	*bth1 = remote->qp;
4846*4882a593Smuzhiyun 	rcu_read_unlock();
4847*4882a593Smuzhiyun 
4848*4882a593Smuzhiyun 	generation = kern_flow_generation_next(flow->flow_state.generation);
4849*4882a593Smuzhiyun 	*bth2 = mask_psn((generation << HFI1_KDETH_BTH_SEQ_SHIFT) - 1);
4850*4882a593Smuzhiyun 	qpriv->s_resync_psn = *bth2;
4851*4882a593Smuzhiyun 	*bth2 |= IB_BTH_REQ_ACK;
4852*4882a593Smuzhiyun 	KDETH_RESET(ohdr->u.tid_rdma.ack.kdeth0, KVER, 0x1);
4853*4882a593Smuzhiyun 
4854*4882a593Smuzhiyun 	return sizeof(ohdr->u.tid_rdma.resync) / sizeof(u32);
4855*4882a593Smuzhiyun }
4856*4882a593Smuzhiyun 
hfi1_rc_rcv_tid_rdma_resync(struct hfi1_packet * packet)4857*4882a593Smuzhiyun void hfi1_rc_rcv_tid_rdma_resync(struct hfi1_packet *packet)
4858*4882a593Smuzhiyun {
4859*4882a593Smuzhiyun 	struct ib_other_headers *ohdr = packet->ohdr;
4860*4882a593Smuzhiyun 	struct rvt_qp *qp = packet->qp;
4861*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
4862*4882a593Smuzhiyun 	struct hfi1_ctxtdata *rcd = qpriv->rcd;
4863*4882a593Smuzhiyun 	struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
4864*4882a593Smuzhiyun 	struct rvt_ack_entry *e;
4865*4882a593Smuzhiyun 	struct tid_rdma_request *req;
4866*4882a593Smuzhiyun 	struct tid_rdma_flow *flow;
4867*4882a593Smuzhiyun 	struct tid_flow_state *fs = &qpriv->flow_state;
4868*4882a593Smuzhiyun 	u32 psn, generation, idx, gen_next;
4869*4882a593Smuzhiyun 	bool fecn;
4870*4882a593Smuzhiyun 	unsigned long flags;
4871*4882a593Smuzhiyun 
4872*4882a593Smuzhiyun 	fecn = process_ecn(qp, packet);
4873*4882a593Smuzhiyun 	psn = mask_psn(be32_to_cpu(ohdr->bth[2]));
4874*4882a593Smuzhiyun 
4875*4882a593Smuzhiyun 	generation = mask_psn(psn + 1) >> HFI1_KDETH_BTH_SEQ_SHIFT;
4876*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->s_lock, flags);
4877*4882a593Smuzhiyun 
4878*4882a593Smuzhiyun 	gen_next = (fs->generation == KERN_GENERATION_RESERVED) ?
4879*4882a593Smuzhiyun 		generation : kern_flow_generation_next(fs->generation);
4880*4882a593Smuzhiyun 	/*
4881*4882a593Smuzhiyun 	 * RESYNC packet contains the "next" generation and can only be
4882*4882a593Smuzhiyun 	 * from the current or previous generations
4883*4882a593Smuzhiyun 	 */
4884*4882a593Smuzhiyun 	if (generation != mask_generation(gen_next - 1) &&
4885*4882a593Smuzhiyun 	    generation != gen_next)
4886*4882a593Smuzhiyun 		goto bail;
4887*4882a593Smuzhiyun 	/* Already processing a resync */
4888*4882a593Smuzhiyun 	if (qpriv->resync)
4889*4882a593Smuzhiyun 		goto bail;
4890*4882a593Smuzhiyun 
4891*4882a593Smuzhiyun 	spin_lock(&rcd->exp_lock);
4892*4882a593Smuzhiyun 	if (fs->index >= RXE_NUM_TID_FLOWS) {
4893*4882a593Smuzhiyun 		/*
4894*4882a593Smuzhiyun 		 * If we don't have a flow, save the generation so it can be
4895*4882a593Smuzhiyun 		 * applied when a new flow is allocated
4896*4882a593Smuzhiyun 		 */
4897*4882a593Smuzhiyun 		fs->generation = generation;
4898*4882a593Smuzhiyun 	} else {
4899*4882a593Smuzhiyun 		/* Reprogram the QP flow with new generation */
4900*4882a593Smuzhiyun 		rcd->flows[fs->index].generation = generation;
4901*4882a593Smuzhiyun 		fs->generation = kern_setup_hw_flow(rcd, fs->index);
4902*4882a593Smuzhiyun 	}
4903*4882a593Smuzhiyun 	fs->psn = 0;
4904*4882a593Smuzhiyun 	/*
4905*4882a593Smuzhiyun 	 * Disable SW PSN checking since a RESYNC is equivalent to a
4906*4882a593Smuzhiyun 	 * sync point and the flow has/will be reprogrammed
4907*4882a593Smuzhiyun 	 */
4908*4882a593Smuzhiyun 	qpriv->s_flags &= ~HFI1_R_TID_SW_PSN;
4909*4882a593Smuzhiyun 	trace_hfi1_tid_write_rsp_rcv_resync(qp);
4910*4882a593Smuzhiyun 
4911*4882a593Smuzhiyun 	/*
4912*4882a593Smuzhiyun 	 * Reset all TID flow information with the new generation.
4913*4882a593Smuzhiyun 	 * This is done for all requests and segments after the
4914*4882a593Smuzhiyun 	 * last received segment
4915*4882a593Smuzhiyun 	 */
4916*4882a593Smuzhiyun 	for (idx = qpriv->r_tid_tail; ; idx++) {
4917*4882a593Smuzhiyun 		u16 flow_idx;
4918*4882a593Smuzhiyun 
4919*4882a593Smuzhiyun 		if (idx > rvt_size_atomic(&dev->rdi))
4920*4882a593Smuzhiyun 			idx = 0;
4921*4882a593Smuzhiyun 		e = &qp->s_ack_queue[idx];
4922*4882a593Smuzhiyun 		if (e->opcode == TID_OP(WRITE_REQ)) {
4923*4882a593Smuzhiyun 			req = ack_to_tid_req(e);
4924*4882a593Smuzhiyun 			trace_hfi1_tid_req_rcv_resync(qp, 0, e->opcode, e->psn,
4925*4882a593Smuzhiyun 						      e->lpsn, req);
4926*4882a593Smuzhiyun 
4927*4882a593Smuzhiyun 			/* start from last unacked segment */
4928*4882a593Smuzhiyun 			for (flow_idx = req->clear_tail;
4929*4882a593Smuzhiyun 			     CIRC_CNT(req->setup_head, flow_idx,
4930*4882a593Smuzhiyun 				      MAX_FLOWS);
4931*4882a593Smuzhiyun 			     flow_idx = CIRC_NEXT(flow_idx, MAX_FLOWS)) {
4932*4882a593Smuzhiyun 				u32 lpsn;
4933*4882a593Smuzhiyun 				u32 next;
4934*4882a593Smuzhiyun 
4935*4882a593Smuzhiyun 				flow = &req->flows[flow_idx];
4936*4882a593Smuzhiyun 				lpsn = full_flow_psn(flow,
4937*4882a593Smuzhiyun 						     flow->flow_state.lpsn);
4938*4882a593Smuzhiyun 				next = flow->flow_state.r_next_psn;
4939*4882a593Smuzhiyun 				flow->npkts = delta_psn(lpsn, next - 1);
4940*4882a593Smuzhiyun 				flow->flow_state.generation = fs->generation;
4941*4882a593Smuzhiyun 				flow->flow_state.spsn = fs->psn;
4942*4882a593Smuzhiyun 				flow->flow_state.lpsn =
4943*4882a593Smuzhiyun 					flow->flow_state.spsn + flow->npkts - 1;
4944*4882a593Smuzhiyun 				flow->flow_state.r_next_psn =
4945*4882a593Smuzhiyun 					full_flow_psn(flow,
4946*4882a593Smuzhiyun 						      flow->flow_state.spsn);
4947*4882a593Smuzhiyun 				fs->psn += flow->npkts;
4948*4882a593Smuzhiyun 				trace_hfi1_tid_flow_rcv_resync(qp, flow_idx,
4949*4882a593Smuzhiyun 							       flow);
4950*4882a593Smuzhiyun 			}
4951*4882a593Smuzhiyun 		}
4952*4882a593Smuzhiyun 		if (idx == qp->s_tail_ack_queue)
4953*4882a593Smuzhiyun 			break;
4954*4882a593Smuzhiyun 	}
4955*4882a593Smuzhiyun 
4956*4882a593Smuzhiyun 	spin_unlock(&rcd->exp_lock);
4957*4882a593Smuzhiyun 	qpriv->resync = true;
4958*4882a593Smuzhiyun 	/* RESYNC request always gets a TID RDMA ACK. */
4959*4882a593Smuzhiyun 	qpriv->s_nak_state = 0;
4960*4882a593Smuzhiyun 	tid_rdma_trigger_ack(qp);
4961*4882a593Smuzhiyun bail:
4962*4882a593Smuzhiyun 	if (fecn)
4963*4882a593Smuzhiyun 		qp->s_flags |= RVT_S_ECN;
4964*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, flags);
4965*4882a593Smuzhiyun }
4966*4882a593Smuzhiyun 
4967*4882a593Smuzhiyun /*
4968*4882a593Smuzhiyun  * Call this function when the last TID RDMA WRITE DATA packet for a request
4969*4882a593Smuzhiyun  * is built.
4970*4882a593Smuzhiyun  */
update_tid_tail(struct rvt_qp * qp)4971*4882a593Smuzhiyun static void update_tid_tail(struct rvt_qp *qp)
4972*4882a593Smuzhiyun 	__must_hold(&qp->s_lock)
4973*4882a593Smuzhiyun {
4974*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
4975*4882a593Smuzhiyun 	u32 i;
4976*4882a593Smuzhiyun 	struct rvt_swqe *wqe;
4977*4882a593Smuzhiyun 
4978*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
4979*4882a593Smuzhiyun 	/* Can't move beyond s_tid_cur */
4980*4882a593Smuzhiyun 	if (priv->s_tid_tail == priv->s_tid_cur)
4981*4882a593Smuzhiyun 		return;
4982*4882a593Smuzhiyun 	for (i = priv->s_tid_tail + 1; ; i++) {
4983*4882a593Smuzhiyun 		if (i == qp->s_size)
4984*4882a593Smuzhiyun 			i = 0;
4985*4882a593Smuzhiyun 
4986*4882a593Smuzhiyun 		if (i == priv->s_tid_cur)
4987*4882a593Smuzhiyun 			break;
4988*4882a593Smuzhiyun 		wqe = rvt_get_swqe_ptr(qp, i);
4989*4882a593Smuzhiyun 		if (wqe->wr.opcode == IB_WR_TID_RDMA_WRITE)
4990*4882a593Smuzhiyun 			break;
4991*4882a593Smuzhiyun 	}
4992*4882a593Smuzhiyun 	priv->s_tid_tail = i;
4993*4882a593Smuzhiyun 	priv->s_state = TID_OP(WRITE_RESP);
4994*4882a593Smuzhiyun }
4995*4882a593Smuzhiyun 
hfi1_make_tid_rdma_pkt(struct rvt_qp * qp,struct hfi1_pkt_state * ps)4996*4882a593Smuzhiyun int hfi1_make_tid_rdma_pkt(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
4997*4882a593Smuzhiyun 	__must_hold(&qp->s_lock)
4998*4882a593Smuzhiyun {
4999*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
5000*4882a593Smuzhiyun 	struct rvt_swqe *wqe;
5001*4882a593Smuzhiyun 	u32 bth1 = 0, bth2 = 0, hwords = 5, len, middle = 0;
5002*4882a593Smuzhiyun 	struct ib_other_headers *ohdr;
5003*4882a593Smuzhiyun 	struct rvt_sge_state *ss = &qp->s_sge;
5004*4882a593Smuzhiyun 	struct rvt_ack_entry *e = &qp->s_ack_queue[qp->s_tail_ack_queue];
5005*4882a593Smuzhiyun 	struct tid_rdma_request *req = ack_to_tid_req(e);
5006*4882a593Smuzhiyun 	bool last = false;
5007*4882a593Smuzhiyun 	u8 opcode = TID_OP(WRITE_DATA);
5008*4882a593Smuzhiyun 
5009*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
5010*4882a593Smuzhiyun 	trace_hfi1_tid_write_sender_make_tid_pkt(qp, 0);
5011*4882a593Smuzhiyun 	/*
5012*4882a593Smuzhiyun 	 * Prioritize the sending of the requests and responses over the
5013*4882a593Smuzhiyun 	 * sending of the TID RDMA data packets.
5014*4882a593Smuzhiyun 	 */
5015*4882a593Smuzhiyun 	if (((atomic_read(&priv->n_tid_requests) < HFI1_TID_RDMA_WRITE_CNT) &&
5016*4882a593Smuzhiyun 	     atomic_read(&priv->n_requests) &&
5017*4882a593Smuzhiyun 	     !(qp->s_flags & (RVT_S_BUSY | RVT_S_WAIT_ACK |
5018*4882a593Smuzhiyun 			     HFI1_S_ANY_WAIT_IO))) ||
5019*4882a593Smuzhiyun 	    (e->opcode == TID_OP(WRITE_REQ) && req->cur_seg < req->alloc_seg &&
5020*4882a593Smuzhiyun 	     !(qp->s_flags & (RVT_S_BUSY | HFI1_S_ANY_WAIT_IO)))) {
5021*4882a593Smuzhiyun 		struct iowait_work *iowork;
5022*4882a593Smuzhiyun 
5023*4882a593Smuzhiyun 		iowork = iowait_get_ib_work(&priv->s_iowait);
5024*4882a593Smuzhiyun 		ps->s_txreq = get_waiting_verbs_txreq(iowork);
5025*4882a593Smuzhiyun 		if (ps->s_txreq || hfi1_make_rc_req(qp, ps)) {
5026*4882a593Smuzhiyun 			priv->s_flags |= HFI1_S_TID_BUSY_SET;
5027*4882a593Smuzhiyun 			return 1;
5028*4882a593Smuzhiyun 		}
5029*4882a593Smuzhiyun 	}
5030*4882a593Smuzhiyun 
5031*4882a593Smuzhiyun 	ps->s_txreq = get_txreq(ps->dev, qp);
5032*4882a593Smuzhiyun 	if (!ps->s_txreq)
5033*4882a593Smuzhiyun 		goto bail_no_tx;
5034*4882a593Smuzhiyun 
5035*4882a593Smuzhiyun 	ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
5036*4882a593Smuzhiyun 
5037*4882a593Smuzhiyun 	if ((priv->s_flags & RVT_S_ACK_PENDING) &&
5038*4882a593Smuzhiyun 	    make_tid_rdma_ack(qp, ohdr, ps))
5039*4882a593Smuzhiyun 		return 1;
5040*4882a593Smuzhiyun 
5041*4882a593Smuzhiyun 	/*
5042*4882a593Smuzhiyun 	 * Bail out if we can't send data.
5043*4882a593Smuzhiyun 	 * Be reminded that this check must been done after the call to
5044*4882a593Smuzhiyun 	 * make_tid_rdma_ack() because the responding QP could be in
5045*4882a593Smuzhiyun 	 * RTR state where it can send TID RDMA ACK, not TID RDMA WRITE DATA.
5046*4882a593Smuzhiyun 	 */
5047*4882a593Smuzhiyun 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK))
5048*4882a593Smuzhiyun 		goto bail;
5049*4882a593Smuzhiyun 
5050*4882a593Smuzhiyun 	if (priv->s_flags & RVT_S_WAIT_ACK)
5051*4882a593Smuzhiyun 		goto bail;
5052*4882a593Smuzhiyun 
5053*4882a593Smuzhiyun 	/* Check whether there is anything to do. */
5054*4882a593Smuzhiyun 	if (priv->s_tid_tail == HFI1_QP_WQE_INVALID)
5055*4882a593Smuzhiyun 		goto bail;
5056*4882a593Smuzhiyun 	wqe = rvt_get_swqe_ptr(qp, priv->s_tid_tail);
5057*4882a593Smuzhiyun 	req = wqe_to_tid_req(wqe);
5058*4882a593Smuzhiyun 	trace_hfi1_tid_req_make_tid_pkt(qp, 0, wqe->wr.opcode, wqe->psn,
5059*4882a593Smuzhiyun 					wqe->lpsn, req);
5060*4882a593Smuzhiyun 	switch (priv->s_state) {
5061*4882a593Smuzhiyun 	case TID_OP(WRITE_REQ):
5062*4882a593Smuzhiyun 	case TID_OP(WRITE_RESP):
5063*4882a593Smuzhiyun 		priv->tid_ss.sge = wqe->sg_list[0];
5064*4882a593Smuzhiyun 		priv->tid_ss.sg_list = wqe->sg_list + 1;
5065*4882a593Smuzhiyun 		priv->tid_ss.num_sge = wqe->wr.num_sge;
5066*4882a593Smuzhiyun 		priv->tid_ss.total_len = wqe->length;
5067*4882a593Smuzhiyun 
5068*4882a593Smuzhiyun 		if (priv->s_state == TID_OP(WRITE_REQ))
5069*4882a593Smuzhiyun 			hfi1_tid_rdma_restart_req(qp, wqe, &bth2);
5070*4882a593Smuzhiyun 		priv->s_state = TID_OP(WRITE_DATA);
5071*4882a593Smuzhiyun 		fallthrough;
5072*4882a593Smuzhiyun 
5073*4882a593Smuzhiyun 	case TID_OP(WRITE_DATA):
5074*4882a593Smuzhiyun 		/*
5075*4882a593Smuzhiyun 		 * 1. Check whether TID RDMA WRITE RESP available.
5076*4882a593Smuzhiyun 		 * 2. If no:
5077*4882a593Smuzhiyun 		 *    2.1 If have more segments and no TID RDMA WRITE RESP,
5078*4882a593Smuzhiyun 		 *        set HFI1_S_WAIT_TID_RESP
5079*4882a593Smuzhiyun 		 *    2.2 Return indicating no progress made.
5080*4882a593Smuzhiyun 		 * 3. If yes:
5081*4882a593Smuzhiyun 		 *    3.1 Build TID RDMA WRITE DATA packet.
5082*4882a593Smuzhiyun 		 *    3.2 If last packet in segment:
5083*4882a593Smuzhiyun 		 *        3.2.1 Change KDETH header bits
5084*4882a593Smuzhiyun 		 *        3.2.2 Advance RESP pointers.
5085*4882a593Smuzhiyun 		 *    3.3 Return indicating progress made.
5086*4882a593Smuzhiyun 		 */
5087*4882a593Smuzhiyun 		trace_hfi1_sender_make_tid_pkt(qp);
5088*4882a593Smuzhiyun 		trace_hfi1_tid_write_sender_make_tid_pkt(qp, 0);
5089*4882a593Smuzhiyun 		wqe = rvt_get_swqe_ptr(qp, priv->s_tid_tail);
5090*4882a593Smuzhiyun 		req = wqe_to_tid_req(wqe);
5091*4882a593Smuzhiyun 		len = wqe->length;
5092*4882a593Smuzhiyun 
5093*4882a593Smuzhiyun 		if (!req->comp_seg || req->cur_seg == req->comp_seg)
5094*4882a593Smuzhiyun 			goto bail;
5095*4882a593Smuzhiyun 
5096*4882a593Smuzhiyun 		trace_hfi1_tid_req_make_tid_pkt(qp, 0, wqe->wr.opcode,
5097*4882a593Smuzhiyun 						wqe->psn, wqe->lpsn, req);
5098*4882a593Smuzhiyun 		last = hfi1_build_tid_rdma_packet(wqe, ohdr, &bth1, &bth2,
5099*4882a593Smuzhiyun 						  &len);
5100*4882a593Smuzhiyun 
5101*4882a593Smuzhiyun 		if (last) {
5102*4882a593Smuzhiyun 			/* move pointer to next flow */
5103*4882a593Smuzhiyun 			req->clear_tail = CIRC_NEXT(req->clear_tail,
5104*4882a593Smuzhiyun 						    MAX_FLOWS);
5105*4882a593Smuzhiyun 			if (++req->cur_seg < req->total_segs) {
5106*4882a593Smuzhiyun 				if (!CIRC_CNT(req->setup_head, req->clear_tail,
5107*4882a593Smuzhiyun 					      MAX_FLOWS))
5108*4882a593Smuzhiyun 					qp->s_flags |= HFI1_S_WAIT_TID_RESP;
5109*4882a593Smuzhiyun 			} else {
5110*4882a593Smuzhiyun 				priv->s_state = TID_OP(WRITE_DATA_LAST);
5111*4882a593Smuzhiyun 				opcode = TID_OP(WRITE_DATA_LAST);
5112*4882a593Smuzhiyun 
5113*4882a593Smuzhiyun 				/* Advance the s_tid_tail now */
5114*4882a593Smuzhiyun 				update_tid_tail(qp);
5115*4882a593Smuzhiyun 			}
5116*4882a593Smuzhiyun 		}
5117*4882a593Smuzhiyun 		hwords += sizeof(ohdr->u.tid_rdma.w_data) / sizeof(u32);
5118*4882a593Smuzhiyun 		ss = &priv->tid_ss;
5119*4882a593Smuzhiyun 		break;
5120*4882a593Smuzhiyun 
5121*4882a593Smuzhiyun 	case TID_OP(RESYNC):
5122*4882a593Smuzhiyun 		trace_hfi1_sender_make_tid_pkt(qp);
5123*4882a593Smuzhiyun 		/* Use generation from the most recently received response */
5124*4882a593Smuzhiyun 		wqe = rvt_get_swqe_ptr(qp, priv->s_tid_cur);
5125*4882a593Smuzhiyun 		req = wqe_to_tid_req(wqe);
5126*4882a593Smuzhiyun 		/* If no responses for this WQE look at the previous one */
5127*4882a593Smuzhiyun 		if (!req->comp_seg) {
5128*4882a593Smuzhiyun 			wqe = rvt_get_swqe_ptr(qp,
5129*4882a593Smuzhiyun 					       (!priv->s_tid_cur ? qp->s_size :
5130*4882a593Smuzhiyun 						priv->s_tid_cur) - 1);
5131*4882a593Smuzhiyun 			req = wqe_to_tid_req(wqe);
5132*4882a593Smuzhiyun 		}
5133*4882a593Smuzhiyun 		hwords += hfi1_build_tid_rdma_resync(qp, wqe, ohdr, &bth1,
5134*4882a593Smuzhiyun 						     &bth2,
5135*4882a593Smuzhiyun 						     CIRC_PREV(req->setup_head,
5136*4882a593Smuzhiyun 							       MAX_FLOWS));
5137*4882a593Smuzhiyun 		ss = NULL;
5138*4882a593Smuzhiyun 		len = 0;
5139*4882a593Smuzhiyun 		opcode = TID_OP(RESYNC);
5140*4882a593Smuzhiyun 		break;
5141*4882a593Smuzhiyun 
5142*4882a593Smuzhiyun 	default:
5143*4882a593Smuzhiyun 		goto bail;
5144*4882a593Smuzhiyun 	}
5145*4882a593Smuzhiyun 	if (priv->s_flags & RVT_S_SEND_ONE) {
5146*4882a593Smuzhiyun 		priv->s_flags &= ~RVT_S_SEND_ONE;
5147*4882a593Smuzhiyun 		priv->s_flags |= RVT_S_WAIT_ACK;
5148*4882a593Smuzhiyun 		bth2 |= IB_BTH_REQ_ACK;
5149*4882a593Smuzhiyun 	}
5150*4882a593Smuzhiyun 	qp->s_len -= len;
5151*4882a593Smuzhiyun 	ps->s_txreq->hdr_dwords = hwords;
5152*4882a593Smuzhiyun 	ps->s_txreq->sde = priv->s_sde;
5153*4882a593Smuzhiyun 	ps->s_txreq->ss = ss;
5154*4882a593Smuzhiyun 	ps->s_txreq->s_cur_size = len;
5155*4882a593Smuzhiyun 	hfi1_make_ruc_header(qp, ohdr, (opcode << 24), bth1, bth2,
5156*4882a593Smuzhiyun 			     middle, ps);
5157*4882a593Smuzhiyun 	return 1;
5158*4882a593Smuzhiyun bail:
5159*4882a593Smuzhiyun 	hfi1_put_txreq(ps->s_txreq);
5160*4882a593Smuzhiyun bail_no_tx:
5161*4882a593Smuzhiyun 	ps->s_txreq = NULL;
5162*4882a593Smuzhiyun 	priv->s_flags &= ~RVT_S_BUSY;
5163*4882a593Smuzhiyun 	/*
5164*4882a593Smuzhiyun 	 * If we didn't get a txreq, the QP will be woken up later to try
5165*4882a593Smuzhiyun 	 * again, set the flags to the the wake up which work item to wake
5166*4882a593Smuzhiyun 	 * up.
5167*4882a593Smuzhiyun 	 * (A better algorithm should be found to do this and generalize the
5168*4882a593Smuzhiyun 	 * sleep/wakeup flags.)
5169*4882a593Smuzhiyun 	 */
5170*4882a593Smuzhiyun 	iowait_set_flag(&priv->s_iowait, IOWAIT_PENDING_TID);
5171*4882a593Smuzhiyun 	return 0;
5172*4882a593Smuzhiyun }
5173*4882a593Smuzhiyun 
make_tid_rdma_ack(struct rvt_qp * qp,struct ib_other_headers * ohdr,struct hfi1_pkt_state * ps)5174*4882a593Smuzhiyun static int make_tid_rdma_ack(struct rvt_qp *qp,
5175*4882a593Smuzhiyun 			     struct ib_other_headers *ohdr,
5176*4882a593Smuzhiyun 			     struct hfi1_pkt_state *ps)
5177*4882a593Smuzhiyun {
5178*4882a593Smuzhiyun 	struct rvt_ack_entry *e;
5179*4882a593Smuzhiyun 	struct hfi1_qp_priv *qpriv = qp->priv;
5180*4882a593Smuzhiyun 	struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
5181*4882a593Smuzhiyun 	u32 hwords, next;
5182*4882a593Smuzhiyun 	u32 len = 0;
5183*4882a593Smuzhiyun 	u32 bth1 = 0, bth2 = 0;
5184*4882a593Smuzhiyun 	int middle = 0;
5185*4882a593Smuzhiyun 	u16 flow;
5186*4882a593Smuzhiyun 	struct tid_rdma_request *req, *nreq;
5187*4882a593Smuzhiyun 
5188*4882a593Smuzhiyun 	trace_hfi1_tid_write_rsp_make_tid_ack(qp);
5189*4882a593Smuzhiyun 	/* Don't send an ACK if we aren't supposed to. */
5190*4882a593Smuzhiyun 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
5191*4882a593Smuzhiyun 		goto bail;
5192*4882a593Smuzhiyun 
5193*4882a593Smuzhiyun 	/* header size in 32-bit words LRH+BTH = (8+12)/4. */
5194*4882a593Smuzhiyun 	hwords = 5;
5195*4882a593Smuzhiyun 
5196*4882a593Smuzhiyun 	e = &qp->s_ack_queue[qpriv->r_tid_ack];
5197*4882a593Smuzhiyun 	req = ack_to_tid_req(e);
5198*4882a593Smuzhiyun 	/*
5199*4882a593Smuzhiyun 	 * In the RESYNC case, we are exactly one segment past the
5200*4882a593Smuzhiyun 	 * previously sent ack or at the previously sent NAK. So to send
5201*4882a593Smuzhiyun 	 * the resync ack, we go back one segment (which might be part of
5202*4882a593Smuzhiyun 	 * the previous request) and let the do-while loop execute again.
5203*4882a593Smuzhiyun 	 * The advantage of executing the do-while loop is that any data
5204*4882a593Smuzhiyun 	 * received after the previous ack is automatically acked in the
5205*4882a593Smuzhiyun 	 * RESYNC ack. It turns out that for the do-while loop we only need
5206*4882a593Smuzhiyun 	 * to pull back qpriv->r_tid_ack, not the segment
5207*4882a593Smuzhiyun 	 * indices/counters. The scheme works even if the previous request
5208*4882a593Smuzhiyun 	 * was not a TID WRITE request.
5209*4882a593Smuzhiyun 	 */
5210*4882a593Smuzhiyun 	if (qpriv->resync) {
5211*4882a593Smuzhiyun 		if (!req->ack_seg || req->ack_seg == req->total_segs)
5212*4882a593Smuzhiyun 			qpriv->r_tid_ack = !qpriv->r_tid_ack ?
5213*4882a593Smuzhiyun 				rvt_size_atomic(&dev->rdi) :
5214*4882a593Smuzhiyun 				qpriv->r_tid_ack - 1;
5215*4882a593Smuzhiyun 		e = &qp->s_ack_queue[qpriv->r_tid_ack];
5216*4882a593Smuzhiyun 		req = ack_to_tid_req(e);
5217*4882a593Smuzhiyun 	}
5218*4882a593Smuzhiyun 
5219*4882a593Smuzhiyun 	trace_hfi1_rsp_make_tid_ack(qp, e->psn);
5220*4882a593Smuzhiyun 	trace_hfi1_tid_req_make_tid_ack(qp, 0, e->opcode, e->psn, e->lpsn,
5221*4882a593Smuzhiyun 					req);
5222*4882a593Smuzhiyun 	/*
5223*4882a593Smuzhiyun 	 * If we've sent all the ACKs that we can, we are done
5224*4882a593Smuzhiyun 	 * until we get more segments...
5225*4882a593Smuzhiyun 	 */
5226*4882a593Smuzhiyun 	if (!qpriv->s_nak_state && !qpriv->resync &&
5227*4882a593Smuzhiyun 	    req->ack_seg == req->comp_seg)
5228*4882a593Smuzhiyun 		goto bail;
5229*4882a593Smuzhiyun 
5230*4882a593Smuzhiyun 	do {
5231*4882a593Smuzhiyun 		/*
5232*4882a593Smuzhiyun 		 * To deal with coalesced ACKs, the acked_tail pointer
5233*4882a593Smuzhiyun 		 * into the flow array is used. The distance between it
5234*4882a593Smuzhiyun 		 * and the clear_tail is the number of flows that are
5235*4882a593Smuzhiyun 		 * being ACK'ed.
5236*4882a593Smuzhiyun 		 */
5237*4882a593Smuzhiyun 		req->ack_seg +=
5238*4882a593Smuzhiyun 			/* Get up-to-date value */
5239*4882a593Smuzhiyun 			CIRC_CNT(req->clear_tail, req->acked_tail,
5240*4882a593Smuzhiyun 				 MAX_FLOWS);
5241*4882a593Smuzhiyun 		/* Advance acked index */
5242*4882a593Smuzhiyun 		req->acked_tail = req->clear_tail;
5243*4882a593Smuzhiyun 
5244*4882a593Smuzhiyun 		/*
5245*4882a593Smuzhiyun 		 * req->clear_tail points to the segment currently being
5246*4882a593Smuzhiyun 		 * received. So, when sending an ACK, the previous
5247*4882a593Smuzhiyun 		 * segment is being ACK'ed.
5248*4882a593Smuzhiyun 		 */
5249*4882a593Smuzhiyun 		flow = CIRC_PREV(req->acked_tail, MAX_FLOWS);
5250*4882a593Smuzhiyun 		if (req->ack_seg != req->total_segs)
5251*4882a593Smuzhiyun 			break;
5252*4882a593Smuzhiyun 		req->state = TID_REQUEST_COMPLETE;
5253*4882a593Smuzhiyun 
5254*4882a593Smuzhiyun 		next = qpriv->r_tid_ack + 1;
5255*4882a593Smuzhiyun 		if (next > rvt_size_atomic(&dev->rdi))
5256*4882a593Smuzhiyun 			next = 0;
5257*4882a593Smuzhiyun 		qpriv->r_tid_ack = next;
5258*4882a593Smuzhiyun 		if (qp->s_ack_queue[next].opcode != TID_OP(WRITE_REQ))
5259*4882a593Smuzhiyun 			break;
5260*4882a593Smuzhiyun 		nreq = ack_to_tid_req(&qp->s_ack_queue[next]);
5261*4882a593Smuzhiyun 		if (!nreq->comp_seg || nreq->ack_seg == nreq->comp_seg)
5262*4882a593Smuzhiyun 			break;
5263*4882a593Smuzhiyun 
5264*4882a593Smuzhiyun 		/* Move to the next ack entry now */
5265*4882a593Smuzhiyun 		e = &qp->s_ack_queue[qpriv->r_tid_ack];
5266*4882a593Smuzhiyun 		req = ack_to_tid_req(e);
5267*4882a593Smuzhiyun 	} while (1);
5268*4882a593Smuzhiyun 
5269*4882a593Smuzhiyun 	/*
5270*4882a593Smuzhiyun 	 * At this point qpriv->r_tid_ack == qpriv->r_tid_tail but e and
5271*4882a593Smuzhiyun 	 * req could be pointing at the previous ack queue entry
5272*4882a593Smuzhiyun 	 */
5273*4882a593Smuzhiyun 	if (qpriv->s_nak_state ||
5274*4882a593Smuzhiyun 	    (qpriv->resync &&
5275*4882a593Smuzhiyun 	     !hfi1_tid_rdma_is_resync_psn(qpriv->r_next_psn_kdeth - 1) &&
5276*4882a593Smuzhiyun 	     (cmp_psn(qpriv->r_next_psn_kdeth - 1,
5277*4882a593Smuzhiyun 		      full_flow_psn(&req->flows[flow],
5278*4882a593Smuzhiyun 				    req->flows[flow].flow_state.lpsn)) > 0))) {
5279*4882a593Smuzhiyun 		/*
5280*4882a593Smuzhiyun 		 * A NAK will implicitly acknowledge all previous TID RDMA
5281*4882a593Smuzhiyun 		 * requests. Therefore, we NAK with the req->acked_tail
5282*4882a593Smuzhiyun 		 * segment for the request at qpriv->r_tid_ack (same at
5283*4882a593Smuzhiyun 		 * this point as the req->clear_tail segment for the
5284*4882a593Smuzhiyun 		 * qpriv->r_tid_tail request)
5285*4882a593Smuzhiyun 		 */
5286*4882a593Smuzhiyun 		e = &qp->s_ack_queue[qpriv->r_tid_ack];
5287*4882a593Smuzhiyun 		req = ack_to_tid_req(e);
5288*4882a593Smuzhiyun 		flow = req->acked_tail;
5289*4882a593Smuzhiyun 	} else if (req->ack_seg == req->total_segs &&
5290*4882a593Smuzhiyun 		   qpriv->s_flags & HFI1_R_TID_WAIT_INTERLCK)
5291*4882a593Smuzhiyun 		qpriv->s_flags &= ~HFI1_R_TID_WAIT_INTERLCK;
5292*4882a593Smuzhiyun 
5293*4882a593Smuzhiyun 	trace_hfi1_tid_write_rsp_make_tid_ack(qp);
5294*4882a593Smuzhiyun 	trace_hfi1_tid_req_make_tid_ack(qp, 0, e->opcode, e->psn, e->lpsn,
5295*4882a593Smuzhiyun 					req);
5296*4882a593Smuzhiyun 	hwords += hfi1_build_tid_rdma_write_ack(qp, e, ohdr, flow, &bth1,
5297*4882a593Smuzhiyun 						&bth2);
5298*4882a593Smuzhiyun 	len = 0;
5299*4882a593Smuzhiyun 	qpriv->s_flags &= ~RVT_S_ACK_PENDING;
5300*4882a593Smuzhiyun 	ps->s_txreq->hdr_dwords = hwords;
5301*4882a593Smuzhiyun 	ps->s_txreq->sde = qpriv->s_sde;
5302*4882a593Smuzhiyun 	ps->s_txreq->s_cur_size = len;
5303*4882a593Smuzhiyun 	ps->s_txreq->ss = NULL;
5304*4882a593Smuzhiyun 	hfi1_make_ruc_header(qp, ohdr, (TID_OP(ACK) << 24), bth1, bth2, middle,
5305*4882a593Smuzhiyun 			     ps);
5306*4882a593Smuzhiyun 	ps->s_txreq->txreq.flags |= SDMA_TXREQ_F_VIP;
5307*4882a593Smuzhiyun 	return 1;
5308*4882a593Smuzhiyun bail:
5309*4882a593Smuzhiyun 	/*
5310*4882a593Smuzhiyun 	 * Ensure s_rdma_ack_cnt changes are committed prior to resetting
5311*4882a593Smuzhiyun 	 * RVT_S_RESP_PENDING
5312*4882a593Smuzhiyun 	 */
5313*4882a593Smuzhiyun 	smp_wmb();
5314*4882a593Smuzhiyun 	qpriv->s_flags &= ~RVT_S_ACK_PENDING;
5315*4882a593Smuzhiyun 	return 0;
5316*4882a593Smuzhiyun }
5317*4882a593Smuzhiyun 
hfi1_send_tid_ok(struct rvt_qp * qp)5318*4882a593Smuzhiyun static int hfi1_send_tid_ok(struct rvt_qp *qp)
5319*4882a593Smuzhiyun {
5320*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
5321*4882a593Smuzhiyun 
5322*4882a593Smuzhiyun 	return !(priv->s_flags & RVT_S_BUSY ||
5323*4882a593Smuzhiyun 		 qp->s_flags & HFI1_S_ANY_WAIT_IO) &&
5324*4882a593Smuzhiyun 		(verbs_txreq_queued(iowait_get_tid_work(&priv->s_iowait)) ||
5325*4882a593Smuzhiyun 		 (priv->s_flags & RVT_S_RESP_PENDING) ||
5326*4882a593Smuzhiyun 		 !(qp->s_flags & HFI1_S_ANY_TID_WAIT_SEND));
5327*4882a593Smuzhiyun }
5328*4882a593Smuzhiyun 
_hfi1_do_tid_send(struct work_struct * work)5329*4882a593Smuzhiyun void _hfi1_do_tid_send(struct work_struct *work)
5330*4882a593Smuzhiyun {
5331*4882a593Smuzhiyun 	struct iowait_work *w = container_of(work, struct iowait_work, iowork);
5332*4882a593Smuzhiyun 	struct rvt_qp *qp = iowait_to_qp(w->iow);
5333*4882a593Smuzhiyun 
5334*4882a593Smuzhiyun 	hfi1_do_tid_send(qp);
5335*4882a593Smuzhiyun }
5336*4882a593Smuzhiyun 
hfi1_do_tid_send(struct rvt_qp * qp)5337*4882a593Smuzhiyun static void hfi1_do_tid_send(struct rvt_qp *qp)
5338*4882a593Smuzhiyun {
5339*4882a593Smuzhiyun 	struct hfi1_pkt_state ps;
5340*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
5341*4882a593Smuzhiyun 
5342*4882a593Smuzhiyun 	ps.dev = to_idev(qp->ibqp.device);
5343*4882a593Smuzhiyun 	ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
5344*4882a593Smuzhiyun 	ps.ppd = ppd_from_ibp(ps.ibp);
5345*4882a593Smuzhiyun 	ps.wait = iowait_get_tid_work(&priv->s_iowait);
5346*4882a593Smuzhiyun 	ps.in_thread = false;
5347*4882a593Smuzhiyun 	ps.timeout_int = qp->timeout_jiffies / 8;
5348*4882a593Smuzhiyun 
5349*4882a593Smuzhiyun 	trace_hfi1_rc_do_tid_send(qp, false);
5350*4882a593Smuzhiyun 	spin_lock_irqsave(&qp->s_lock, ps.flags);
5351*4882a593Smuzhiyun 
5352*4882a593Smuzhiyun 	/* Return if we are already busy processing a work request. */
5353*4882a593Smuzhiyun 	if (!hfi1_send_tid_ok(qp)) {
5354*4882a593Smuzhiyun 		if (qp->s_flags & HFI1_S_ANY_WAIT_IO)
5355*4882a593Smuzhiyun 			iowait_set_flag(&priv->s_iowait, IOWAIT_PENDING_TID);
5356*4882a593Smuzhiyun 		spin_unlock_irqrestore(&qp->s_lock, ps.flags);
5357*4882a593Smuzhiyun 		return;
5358*4882a593Smuzhiyun 	}
5359*4882a593Smuzhiyun 
5360*4882a593Smuzhiyun 	priv->s_flags |= RVT_S_BUSY;
5361*4882a593Smuzhiyun 
5362*4882a593Smuzhiyun 	ps.timeout = jiffies + ps.timeout_int;
5363*4882a593Smuzhiyun 	ps.cpu = priv->s_sde ? priv->s_sde->cpu :
5364*4882a593Smuzhiyun 		cpumask_first(cpumask_of_node(ps.ppd->dd->node));
5365*4882a593Smuzhiyun 	ps.pkts_sent = false;
5366*4882a593Smuzhiyun 
5367*4882a593Smuzhiyun 	/* insure a pre-built packet is handled  */
5368*4882a593Smuzhiyun 	ps.s_txreq = get_waiting_verbs_txreq(ps.wait);
5369*4882a593Smuzhiyun 	do {
5370*4882a593Smuzhiyun 		/* Check for a constructed packet to be sent. */
5371*4882a593Smuzhiyun 		if (ps.s_txreq) {
5372*4882a593Smuzhiyun 			if (priv->s_flags & HFI1_S_TID_BUSY_SET) {
5373*4882a593Smuzhiyun 				qp->s_flags |= RVT_S_BUSY;
5374*4882a593Smuzhiyun 				ps.wait = iowait_get_ib_work(&priv->s_iowait);
5375*4882a593Smuzhiyun 			}
5376*4882a593Smuzhiyun 			spin_unlock_irqrestore(&qp->s_lock, ps.flags);
5377*4882a593Smuzhiyun 
5378*4882a593Smuzhiyun 			/*
5379*4882a593Smuzhiyun 			 * If the packet cannot be sent now, return and
5380*4882a593Smuzhiyun 			 * the send tasklet will be woken up later.
5381*4882a593Smuzhiyun 			 */
5382*4882a593Smuzhiyun 			if (hfi1_verbs_send(qp, &ps))
5383*4882a593Smuzhiyun 				return;
5384*4882a593Smuzhiyun 
5385*4882a593Smuzhiyun 			/* allow other tasks to run */
5386*4882a593Smuzhiyun 			if (hfi1_schedule_send_yield(qp, &ps, true))
5387*4882a593Smuzhiyun 				return;
5388*4882a593Smuzhiyun 
5389*4882a593Smuzhiyun 			spin_lock_irqsave(&qp->s_lock, ps.flags);
5390*4882a593Smuzhiyun 			if (priv->s_flags & HFI1_S_TID_BUSY_SET) {
5391*4882a593Smuzhiyun 				qp->s_flags &= ~RVT_S_BUSY;
5392*4882a593Smuzhiyun 				priv->s_flags &= ~HFI1_S_TID_BUSY_SET;
5393*4882a593Smuzhiyun 				ps.wait = iowait_get_tid_work(&priv->s_iowait);
5394*4882a593Smuzhiyun 				if (iowait_flag_set(&priv->s_iowait,
5395*4882a593Smuzhiyun 						    IOWAIT_PENDING_IB))
5396*4882a593Smuzhiyun 					hfi1_schedule_send(qp);
5397*4882a593Smuzhiyun 			}
5398*4882a593Smuzhiyun 		}
5399*4882a593Smuzhiyun 	} while (hfi1_make_tid_rdma_pkt(qp, &ps));
5400*4882a593Smuzhiyun 	iowait_starve_clear(ps.pkts_sent, &priv->s_iowait);
5401*4882a593Smuzhiyun 	spin_unlock_irqrestore(&qp->s_lock, ps.flags);
5402*4882a593Smuzhiyun }
5403*4882a593Smuzhiyun 
_hfi1_schedule_tid_send(struct rvt_qp * qp)5404*4882a593Smuzhiyun static bool _hfi1_schedule_tid_send(struct rvt_qp *qp)
5405*4882a593Smuzhiyun {
5406*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
5407*4882a593Smuzhiyun 	struct hfi1_ibport *ibp =
5408*4882a593Smuzhiyun 		to_iport(qp->ibqp.device, qp->port_num);
5409*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
5410*4882a593Smuzhiyun 	struct hfi1_devdata *dd = ppd->dd;
5411*4882a593Smuzhiyun 
5412*4882a593Smuzhiyun 	if ((dd->flags & HFI1_SHUTDOWN))
5413*4882a593Smuzhiyun 		return true;
5414*4882a593Smuzhiyun 
5415*4882a593Smuzhiyun 	return iowait_tid_schedule(&priv->s_iowait, ppd->hfi1_wq,
5416*4882a593Smuzhiyun 				   priv->s_sde ?
5417*4882a593Smuzhiyun 				   priv->s_sde->cpu :
5418*4882a593Smuzhiyun 				   cpumask_first(cpumask_of_node(dd->node)));
5419*4882a593Smuzhiyun }
5420*4882a593Smuzhiyun 
5421*4882a593Smuzhiyun /**
5422*4882a593Smuzhiyun  * hfi1_schedule_tid_send - schedule progress on TID RDMA state machine
5423*4882a593Smuzhiyun  * @qp: the QP
5424*4882a593Smuzhiyun  *
5425*4882a593Smuzhiyun  * This schedules qp progress on the TID RDMA state machine. Caller
5426*4882a593Smuzhiyun  * should hold the s_lock.
5427*4882a593Smuzhiyun  * Unlike hfi1_schedule_send(), this cannot use hfi1_send_ok() because
5428*4882a593Smuzhiyun  * the two state machines can step on each other with respect to the
5429*4882a593Smuzhiyun  * RVT_S_BUSY flag.
5430*4882a593Smuzhiyun  * Therefore, a modified test is used.
5431*4882a593Smuzhiyun  * @return true if the second leg is scheduled;
5432*4882a593Smuzhiyun  *  false if the second leg is not scheduled.
5433*4882a593Smuzhiyun  */
hfi1_schedule_tid_send(struct rvt_qp * qp)5434*4882a593Smuzhiyun bool hfi1_schedule_tid_send(struct rvt_qp *qp)
5435*4882a593Smuzhiyun {
5436*4882a593Smuzhiyun 	lockdep_assert_held(&qp->s_lock);
5437*4882a593Smuzhiyun 	if (hfi1_send_tid_ok(qp)) {
5438*4882a593Smuzhiyun 		/*
5439*4882a593Smuzhiyun 		 * The following call returns true if the qp is not on the
5440*4882a593Smuzhiyun 		 * queue and false if the qp is already on the queue before
5441*4882a593Smuzhiyun 		 * this call. Either way, the qp will be on the queue when the
5442*4882a593Smuzhiyun 		 * call returns.
5443*4882a593Smuzhiyun 		 */
5444*4882a593Smuzhiyun 		_hfi1_schedule_tid_send(qp);
5445*4882a593Smuzhiyun 		return true;
5446*4882a593Smuzhiyun 	}
5447*4882a593Smuzhiyun 	if (qp->s_flags & HFI1_S_ANY_WAIT_IO)
5448*4882a593Smuzhiyun 		iowait_set_flag(&((struct hfi1_qp_priv *)qp->priv)->s_iowait,
5449*4882a593Smuzhiyun 				IOWAIT_PENDING_TID);
5450*4882a593Smuzhiyun 	return false;
5451*4882a593Smuzhiyun }
5452*4882a593Smuzhiyun 
hfi1_tid_rdma_ack_interlock(struct rvt_qp * qp,struct rvt_ack_entry * e)5453*4882a593Smuzhiyun bool hfi1_tid_rdma_ack_interlock(struct rvt_qp *qp, struct rvt_ack_entry *e)
5454*4882a593Smuzhiyun {
5455*4882a593Smuzhiyun 	struct rvt_ack_entry *prev;
5456*4882a593Smuzhiyun 	struct tid_rdma_request *req;
5457*4882a593Smuzhiyun 	struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
5458*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv = qp->priv;
5459*4882a593Smuzhiyun 	u32 s_prev;
5460*4882a593Smuzhiyun 
5461*4882a593Smuzhiyun 	s_prev = qp->s_tail_ack_queue == 0 ? rvt_size_atomic(&dev->rdi) :
5462*4882a593Smuzhiyun 		(qp->s_tail_ack_queue - 1);
5463*4882a593Smuzhiyun 	prev = &qp->s_ack_queue[s_prev];
5464*4882a593Smuzhiyun 
5465*4882a593Smuzhiyun 	if ((e->opcode == TID_OP(READ_REQ) ||
5466*4882a593Smuzhiyun 	     e->opcode == OP(RDMA_READ_REQUEST)) &&
5467*4882a593Smuzhiyun 	    prev->opcode == TID_OP(WRITE_REQ)) {
5468*4882a593Smuzhiyun 		req = ack_to_tid_req(prev);
5469*4882a593Smuzhiyun 		if (req->ack_seg != req->total_segs) {
5470*4882a593Smuzhiyun 			priv->s_flags |= HFI1_R_TID_WAIT_INTERLCK;
5471*4882a593Smuzhiyun 			return true;
5472*4882a593Smuzhiyun 		}
5473*4882a593Smuzhiyun 	}
5474*4882a593Smuzhiyun 	return false;
5475*4882a593Smuzhiyun }
5476*4882a593Smuzhiyun 
read_r_next_psn(struct hfi1_devdata * dd,u8 ctxt,u8 fidx)5477*4882a593Smuzhiyun static u32 read_r_next_psn(struct hfi1_devdata *dd, u8 ctxt, u8 fidx)
5478*4882a593Smuzhiyun {
5479*4882a593Smuzhiyun 	u64 reg;
5480*4882a593Smuzhiyun 
5481*4882a593Smuzhiyun 	/*
5482*4882a593Smuzhiyun 	 * The only sane way to get the amount of
5483*4882a593Smuzhiyun 	 * progress is to read the HW flow state.
5484*4882a593Smuzhiyun 	 */
5485*4882a593Smuzhiyun 	reg = read_uctxt_csr(dd, ctxt, RCV_TID_FLOW_TABLE + (8 * fidx));
5486*4882a593Smuzhiyun 	return mask_psn(reg);
5487*4882a593Smuzhiyun }
5488*4882a593Smuzhiyun 
tid_rdma_rcv_err(struct hfi1_packet * packet,struct ib_other_headers * ohdr,struct rvt_qp * qp,u32 psn,int diff,bool fecn)5489*4882a593Smuzhiyun static void tid_rdma_rcv_err(struct hfi1_packet *packet,
5490*4882a593Smuzhiyun 			     struct ib_other_headers *ohdr,
5491*4882a593Smuzhiyun 			     struct rvt_qp *qp, u32 psn, int diff, bool fecn)
5492*4882a593Smuzhiyun {
5493*4882a593Smuzhiyun 	unsigned long flags;
5494*4882a593Smuzhiyun 
5495*4882a593Smuzhiyun 	tid_rdma_rcv_error(packet, ohdr, qp, psn, diff);
5496*4882a593Smuzhiyun 	if (fecn) {
5497*4882a593Smuzhiyun 		spin_lock_irqsave(&qp->s_lock, flags);
5498*4882a593Smuzhiyun 		qp->s_flags |= RVT_S_ECN;
5499*4882a593Smuzhiyun 		spin_unlock_irqrestore(&qp->s_lock, flags);
5500*4882a593Smuzhiyun 	}
5501*4882a593Smuzhiyun }
5502*4882a593Smuzhiyun 
update_r_next_psn_fecn(struct hfi1_packet * packet,struct hfi1_qp_priv * priv,struct hfi1_ctxtdata * rcd,struct tid_rdma_flow * flow,bool fecn)5503*4882a593Smuzhiyun static void update_r_next_psn_fecn(struct hfi1_packet *packet,
5504*4882a593Smuzhiyun 				   struct hfi1_qp_priv *priv,
5505*4882a593Smuzhiyun 				   struct hfi1_ctxtdata *rcd,
5506*4882a593Smuzhiyun 				   struct tid_rdma_flow *flow,
5507*4882a593Smuzhiyun 				   bool fecn)
5508*4882a593Smuzhiyun {
5509*4882a593Smuzhiyun 	/*
5510*4882a593Smuzhiyun 	 * If a start/middle packet is delivered here due to
5511*4882a593Smuzhiyun 	 * RSM rule and FECN, we need to update the r_next_psn.
5512*4882a593Smuzhiyun 	 */
5513*4882a593Smuzhiyun 	if (fecn && packet->etype == RHF_RCV_TYPE_EAGER &&
5514*4882a593Smuzhiyun 	    !(priv->s_flags & HFI1_R_TID_SW_PSN)) {
5515*4882a593Smuzhiyun 		struct hfi1_devdata *dd = rcd->dd;
5516*4882a593Smuzhiyun 
5517*4882a593Smuzhiyun 		flow->flow_state.r_next_psn =
5518*4882a593Smuzhiyun 			read_r_next_psn(dd, rcd->ctxt, flow->idx);
5519*4882a593Smuzhiyun 	}
5520*4882a593Smuzhiyun }
5521