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