2 * Copyright(c) 2015 - 2018 Intel Corporation.
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 #include "verbs_txreq.h"
52 /* cut down ridiculously long IB macro names */
53 #define OP(x) UC_OP(x)
56 * hfi1_make_uc_req - construct a request packet (SEND, RDMA write)
57 * @qp: a pointer to the QP
59 * Assume s_lock is held.
61 * Return 1 if constructed; otherwise, return 0.
63 int hfi1_make_uc_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
65 struct hfi1_qp_priv *priv = qp->priv;
66 struct ib_other_headers *ohdr;
74 ps->s_txreq = get_txreq(ps->dev, qp);
78 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
79 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
81 /* We are in the error state, flush the work request. */
82 smp_read_barrier_depends(); /* see post_one_send() */
83 if (qp->s_last == ACCESS_ONCE(qp->s_head))
85 /* If DMAs are in progress, we can't flush immediately. */
86 if (iowait_sdma_pending(&priv->s_iowait)) {
87 qp->s_flags |= RVT_S_WAIT_DMA;
91 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
92 hfi1_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
96 ohdr = &ps->s_txreq->phdr.hdr.u.oth;
97 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
98 ohdr = &ps->s_txreq->phdr.hdr.u.l.oth;
100 /* Get the next send request. */
101 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
103 switch (qp->s_state) {
105 if (!(ib_rvt_state_ops[qp->state] &
106 RVT_PROCESS_NEXT_SEND_OK))
108 /* Check if send work queue is empty. */
109 smp_read_barrier_depends(); /* see post_one_send() */
110 if (qp->s_cur == ACCESS_ONCE(qp->s_head)) {
115 * Local operations are processed immediately
116 * after all prior requests have completed.
118 if (wqe->wr.opcode == IB_WR_REG_MR ||
119 wqe->wr.opcode == IB_WR_LOCAL_INV) {
123 if (qp->s_last != qp->s_cur)
125 if (++qp->s_cur == qp->s_size)
127 if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
128 err = rvt_invalidate_rkey(
129 qp, wqe->wr.ex.invalidate_rkey);
132 hfi1_send_complete(qp, wqe, err ? IB_WC_LOC_PROT_ERR
135 atomic_dec(&qp->local_ops_pending);
140 * Start a new request.
142 qp->s_psn = wqe->psn;
143 qp->s_sge.sge = wqe->sg_list[0];
144 qp->s_sge.sg_list = wqe->sg_list + 1;
145 qp->s_sge.num_sge = wqe->wr.num_sge;
146 qp->s_sge.total_len = wqe->length;
149 switch (wqe->wr.opcode) {
151 case IB_WR_SEND_WITH_IMM:
153 qp->s_state = OP(SEND_FIRST);
157 if (wqe->wr.opcode == IB_WR_SEND) {
158 qp->s_state = OP(SEND_ONLY);
161 OP(SEND_ONLY_WITH_IMMEDIATE);
162 /* Immediate data comes after the BTH */
163 ohdr->u.imm_data = wqe->wr.ex.imm_data;
166 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
167 bth0 |= IB_BTH_SOLICITED;
169 if (++qp->s_cur >= qp->s_size)
173 case IB_WR_RDMA_WRITE:
174 case IB_WR_RDMA_WRITE_WITH_IMM:
175 ohdr->u.rc.reth.vaddr =
176 cpu_to_be64(wqe->rdma_wr.remote_addr);
177 ohdr->u.rc.reth.rkey =
178 cpu_to_be32(wqe->rdma_wr.rkey);
179 ohdr->u.rc.reth.length = cpu_to_be32(len);
180 hwords += sizeof(struct ib_reth) / 4;
182 qp->s_state = OP(RDMA_WRITE_FIRST);
186 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
187 qp->s_state = OP(RDMA_WRITE_ONLY);
190 OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
191 /* Immediate data comes after the RETH */
192 ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
194 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
195 bth0 |= IB_BTH_SOLICITED;
198 if (++qp->s_cur >= qp->s_size)
208 qp->s_state = OP(SEND_MIDDLE);
210 case OP(SEND_MIDDLE):
214 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
217 if (wqe->wr.opcode == IB_WR_SEND) {
218 qp->s_state = OP(SEND_LAST);
220 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
221 /* Immediate data comes after the BTH */
222 ohdr->u.imm_data = wqe->wr.ex.imm_data;
225 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
226 bth0 |= IB_BTH_SOLICITED;
228 if (++qp->s_cur >= qp->s_size)
232 case OP(RDMA_WRITE_FIRST):
233 qp->s_state = OP(RDMA_WRITE_MIDDLE);
235 case OP(RDMA_WRITE_MIDDLE):
239 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
242 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
243 qp->s_state = OP(RDMA_WRITE_LAST);
246 OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
247 /* Immediate data comes after the BTH */
248 ohdr->u.imm_data = wqe->wr.ex.imm_data;
250 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
251 bth0 |= IB_BTH_SOLICITED;
254 if (++qp->s_cur >= qp->s_size)
259 qp->s_hdrwords = hwords;
260 ps->s_txreq->sde = priv->s_sde;
261 qp->s_cur_sge = &qp->s_sge;
262 qp->s_cur_size = len;
263 hfi1_make_ruc_header(qp, ohdr, bth0 | (qp->s_state << 24),
264 mask_psn(qp->s_psn++), middle, ps);
266 ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
270 hfi1_put_txreq(ps->s_txreq);
275 hfi1_put_txreq(ps->s_txreq);
279 qp->s_flags &= ~RVT_S_BUSY;
285 * hfi1_uc_rcv - handle an incoming UC packet
286 * @ibp: the port the packet came in on
287 * @hdr: the header of the packet
288 * @rcv_flags: flags relevant to rcv processing
289 * @data: the packet data
290 * @tlen: the length of the packet
291 * @qp: the QP for this packet.
293 * This is called from qp_rcv() to process an incoming UC packet
295 * Called at interrupt level.
297 void hfi1_uc_rcv(struct hfi1_packet *packet)
299 struct hfi1_ibport *ibp = &packet->rcd->ppd->ibport_data;
300 struct ib_header *hdr = packet->hdr;
301 u32 rcv_flags = packet->rcv_flags;
302 void *data = packet->ebuf;
303 u32 tlen = packet->tlen;
304 struct rvt_qp *qp = packet->qp;
305 struct ib_other_headers *ohdr = packet->ohdr;
307 u32 hdrsize = packet->hlen;
312 struct ib_reth *reth;
313 int has_grh = rcv_flags & HFI1_HAS_GRH;
316 bth0 = be32_to_cpu(ohdr->bth[0]);
317 if (hfi1_ruc_check_hdr(ibp, hdr, has_grh, qp, bth0))
320 process_ecn(qp, packet, true);
322 psn = be32_to_cpu(ohdr->bth[2]);
323 opcode = (bth0 >> 24) & 0xff;
325 /* Compare the PSN verses the expected PSN. */
326 if (unlikely(cmp_psn(psn, qp->r_psn) != 0)) {
328 * Handle a sequence error.
329 * Silently drop any current message.
333 if (qp->r_state == OP(SEND_FIRST) ||
334 qp->r_state == OP(SEND_MIDDLE)) {
335 set_bit(RVT_R_REWIND_SGE, &qp->r_aflags);
336 qp->r_sge.num_sge = 0;
338 rvt_put_ss(&qp->r_sge);
340 qp->r_state = OP(SEND_LAST);
344 case OP(SEND_ONLY_WITH_IMMEDIATE):
347 case OP(RDMA_WRITE_FIRST):
348 case OP(RDMA_WRITE_ONLY):
349 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
357 /* Check for opcode sequence errors. */
358 switch (qp->r_state) {
360 case OP(SEND_MIDDLE):
361 if (opcode == OP(SEND_MIDDLE) ||
362 opcode == OP(SEND_LAST) ||
363 opcode == OP(SEND_LAST_WITH_IMMEDIATE))
367 case OP(RDMA_WRITE_FIRST):
368 case OP(RDMA_WRITE_MIDDLE):
369 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
370 opcode == OP(RDMA_WRITE_LAST) ||
371 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
376 if (opcode == OP(SEND_FIRST) ||
377 opcode == OP(SEND_ONLY) ||
378 opcode == OP(SEND_ONLY_WITH_IMMEDIATE) ||
379 opcode == OP(RDMA_WRITE_FIRST) ||
380 opcode == OP(RDMA_WRITE_ONLY) ||
381 opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
386 if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
389 /* OK, process the packet. */
393 case OP(SEND_ONLY_WITH_IMMEDIATE):
395 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags)) {
396 qp->r_sge = qp->s_rdma_read_sge;
398 ret = hfi1_rvt_get_rwqe(qp, 0);
404 * qp->s_rdma_read_sge will be the owner
405 * of the mr references.
407 qp->s_rdma_read_sge = qp->r_sge;
410 if (opcode == OP(SEND_ONLY))
411 goto no_immediate_data;
412 else if (opcode == OP(SEND_ONLY_WITH_IMMEDIATE))
415 case OP(SEND_MIDDLE):
416 /* Check for invalid length PMTU or posted rwqe len. */
417 if (unlikely(tlen != (hdrsize + pmtu + 4)))
419 qp->r_rcv_len += pmtu;
420 if (unlikely(qp->r_rcv_len > qp->r_len))
422 hfi1_copy_sge(&qp->r_sge, data, pmtu, 0, 0);
425 case OP(SEND_LAST_WITH_IMMEDIATE):
427 wc.ex.imm_data = ohdr->u.imm_data;
428 wc.wc_flags = IB_WC_WITH_IMM;
435 /* Get the number of bytes the message was padded by. */
436 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
437 /* Check for invalid length. */
438 /* LAST len should be >= 1 */
439 if (unlikely(tlen < (hdrsize + pad + 4)))
441 /* Don't count the CRC. */
442 tlen -= (hdrsize + pad + 4);
443 wc.byte_len = tlen + qp->r_rcv_len;
444 if (unlikely(wc.byte_len > qp->r_len))
446 wc.opcode = IB_WC_RECV;
447 hfi1_copy_sge(&qp->r_sge, data, tlen, 0, 0);
448 rvt_put_ss(&qp->s_rdma_read_sge);
450 wc.wr_id = qp->r_wr_id;
451 wc.status = IB_WC_SUCCESS;
453 wc.src_qp = qp->remote_qpn;
454 wc.slid = qp->remote_ah_attr.dlid;
456 * It seems that IB mandates the presence of an SL in a
457 * work completion only for the UD transport (see section
458 * 11.4.2 of IBTA Vol. 1).
460 * However, the way the SL is chosen below is consistent
461 * with the way that IB/qib works and is trying avoid
462 * introducing incompatibilities.
464 * See also OPA Vol. 1, section 9.7.6, and table 9-17.
466 wc.sl = qp->remote_ah_attr.sl;
467 /* zero fields that are N/A */
470 wc.dlid_path_bits = 0;
472 /* Signal completion event if the solicited bit is set. */
473 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
475 cpu_to_be32(IB_BTH_SOLICITED)) != 0);
478 case OP(RDMA_WRITE_FIRST):
479 case OP(RDMA_WRITE_ONLY):
480 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE): /* consume RWQE */
482 if (unlikely(!(qp->qp_access_flags &
483 IB_ACCESS_REMOTE_WRITE))) {
486 reth = &ohdr->u.rc.reth;
487 qp->r_len = be32_to_cpu(reth->length);
489 qp->r_sge.sg_list = NULL;
490 if (qp->r_len != 0) {
491 u32 rkey = be32_to_cpu(reth->rkey);
492 u64 vaddr = be64_to_cpu(reth->vaddr);
496 ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len,
497 vaddr, rkey, IB_ACCESS_REMOTE_WRITE);
500 qp->r_sge.num_sge = 1;
502 qp->r_sge.num_sge = 0;
503 qp->r_sge.sge.mr = NULL;
504 qp->r_sge.sge.vaddr = NULL;
505 qp->r_sge.sge.length = 0;
506 qp->r_sge.sge.sge_length = 0;
508 if (opcode == OP(RDMA_WRITE_ONLY)) {
510 } else if (opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE)) {
511 wc.ex.imm_data = ohdr->u.rc.imm_data;
515 case OP(RDMA_WRITE_MIDDLE):
516 /* Check for invalid length PMTU or posted rwqe len. */
517 if (unlikely(tlen != (hdrsize + pmtu + 4)))
519 qp->r_rcv_len += pmtu;
520 if (unlikely(qp->r_rcv_len > qp->r_len))
522 hfi1_copy_sge(&qp->r_sge, data, pmtu, 1, 0);
525 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
526 wc.ex.imm_data = ohdr->u.imm_data;
528 wc.wc_flags = IB_WC_WITH_IMM;
530 /* Get the number of bytes the message was padded by. */
531 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
532 /* Check for invalid length. */
533 /* LAST len should be >= 1 */
534 if (unlikely(tlen < (hdrsize + pad + 4)))
536 /* Don't count the CRC. */
537 tlen -= (hdrsize + pad + 4);
538 if (unlikely(tlen + qp->r_rcv_len != qp->r_len))
540 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags)) {
541 rvt_put_ss(&qp->s_rdma_read_sge);
543 ret = hfi1_rvt_get_rwqe(qp, 1);
549 wc.byte_len = qp->r_len;
550 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
551 hfi1_copy_sge(&qp->r_sge, data, tlen, 1, 0);
552 rvt_put_ss(&qp->r_sge);
555 case OP(RDMA_WRITE_LAST):
557 /* Get the number of bytes the message was padded by. */
558 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
559 /* Check for invalid length. */
560 /* LAST len should be >= 1 */
561 if (unlikely(tlen < (hdrsize + pad + 4)))
563 /* Don't count the CRC. */
564 tlen -= (hdrsize + pad + 4);
565 if (unlikely(tlen + qp->r_rcv_len != qp->r_len))
567 hfi1_copy_sge(&qp->r_sge, data, tlen, 1, 0);
568 rvt_put_ss(&qp->r_sge);
572 /* Drop packet for unknown opcodes. */
576 qp->r_state = opcode;
580 set_bit(RVT_R_REWIND_SGE, &qp->r_aflags);
581 qp->r_sge.num_sge = 0;
583 ibp->rvp.n_pkt_drops++;
587 hfi1_rc_error(qp, IB_WC_LOC_QP_OP_ERR);