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
58 * @ps: the current packet state
60 * Assume s_lock is held.
62 * Return 1 if constructed; otherwise, return 0.
64 int hfi1_make_uc_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
66 struct hfi1_qp_priv *priv = qp->priv;
67 struct ib_other_headers *ohdr;
75 ps->s_txreq = get_txreq(ps->dev, qp);
79 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
80 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
82 /* We are in the error state, flush the work request. */
83 if (qp->s_last == READ_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 rvt_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
96 if (priv->hdr_type == HFI1_PKT_TYPE_9B) {
97 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
99 if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)
100 ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
102 ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
104 /* header size in 32-bit words 16B LRH+BTH = (16+12)/4. */
106 if ((rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH) &&
107 (hfi1_check_mcast(rdma_ah_get_dlid(&qp->remote_ah_attr))))
108 ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth;
110 ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth;
113 /* Get the next send request. */
114 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
116 switch (qp->s_state) {
118 if (!(ib_rvt_state_ops[qp->state] &
119 RVT_PROCESS_NEXT_SEND_OK))
121 /* Check if send work queue is empty. */
122 if (qp->s_cur == READ_ONCE(qp->s_head)) {
127 * Local operations are processed immediately
128 * after all prior requests have completed.
130 if (wqe->wr.opcode == IB_WR_REG_MR ||
131 wqe->wr.opcode == IB_WR_LOCAL_INV) {
135 if (qp->s_last != qp->s_cur)
137 if (++qp->s_cur == qp->s_size)
139 if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
140 err = rvt_invalidate_rkey(
141 qp, wqe->wr.ex.invalidate_rkey);
144 rvt_send_complete(qp, wqe, err ? IB_WC_LOC_PROT_ERR
147 atomic_dec(&qp->local_ops_pending);
151 * Start a new request.
153 qp->s_psn = wqe->psn;
154 qp->s_sge.sge = wqe->sg_list[0];
155 qp->s_sge.sg_list = wqe->sg_list + 1;
156 qp->s_sge.num_sge = wqe->wr.num_sge;
157 qp->s_sge.total_len = wqe->length;
160 switch (wqe->wr.opcode) {
162 case IB_WR_SEND_WITH_IMM:
164 qp->s_state = OP(SEND_FIRST);
168 if (wqe->wr.opcode == IB_WR_SEND) {
169 qp->s_state = OP(SEND_ONLY);
172 OP(SEND_ONLY_WITH_IMMEDIATE);
173 /* Immediate data comes after the BTH */
174 ohdr->u.imm_data = wqe->wr.ex.imm_data;
177 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
178 bth0 |= IB_BTH_SOLICITED;
180 if (++qp->s_cur >= qp->s_size)
184 case IB_WR_RDMA_WRITE:
185 case IB_WR_RDMA_WRITE_WITH_IMM:
186 ohdr->u.rc.reth.vaddr =
187 cpu_to_be64(wqe->rdma_wr.remote_addr);
188 ohdr->u.rc.reth.rkey =
189 cpu_to_be32(wqe->rdma_wr.rkey);
190 ohdr->u.rc.reth.length = cpu_to_be32(len);
191 hwords += sizeof(struct ib_reth) / 4;
193 qp->s_state = OP(RDMA_WRITE_FIRST);
197 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
198 qp->s_state = OP(RDMA_WRITE_ONLY);
201 OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
202 /* Immediate data comes after the RETH */
203 ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
205 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
206 bth0 |= IB_BTH_SOLICITED;
209 if (++qp->s_cur >= qp->s_size)
219 qp->s_state = OP(SEND_MIDDLE);
221 case OP(SEND_MIDDLE):
225 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
228 if (wqe->wr.opcode == IB_WR_SEND) {
229 qp->s_state = OP(SEND_LAST);
231 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
232 /* Immediate data comes after the BTH */
233 ohdr->u.imm_data = wqe->wr.ex.imm_data;
236 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
237 bth0 |= IB_BTH_SOLICITED;
239 if (++qp->s_cur >= qp->s_size)
243 case OP(RDMA_WRITE_FIRST):
244 qp->s_state = OP(RDMA_WRITE_MIDDLE);
246 case OP(RDMA_WRITE_MIDDLE):
250 middle = HFI1_CAP_IS_KSET(SDMA_AHG);
253 if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
254 qp->s_state = OP(RDMA_WRITE_LAST);
257 OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
258 /* Immediate data comes after the BTH */
259 ohdr->u.imm_data = wqe->wr.ex.imm_data;
261 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
262 bth0 |= IB_BTH_SOLICITED;
265 if (++qp->s_cur >= qp->s_size)
270 ps->s_txreq->hdr_dwords = hwords;
271 ps->s_txreq->sde = priv->s_sde;
272 ps->s_txreq->ss = &qp->s_sge;
273 ps->s_txreq->s_cur_size = len;
274 hfi1_make_ruc_header(qp, ohdr, bth0 | (qp->s_state << 24),
275 qp->remote_qpn, mask_psn(qp->s_psn++),
280 hfi1_put_txreq(ps->s_txreq);
285 hfi1_put_txreq(ps->s_txreq);
289 qp->s_flags &= ~RVT_S_BUSY;
294 * hfi1_uc_rcv - handle an incoming UC packet
295 * @packet: the packet structure
297 * This is called from qp_rcv() to process an incoming UC packet
299 * Called at interrupt level.
301 void hfi1_uc_rcv(struct hfi1_packet *packet)
303 struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
304 void *data = packet->payload;
305 u32 tlen = packet->tlen;
306 struct rvt_qp *qp = packet->qp;
307 struct ib_other_headers *ohdr = packet->ohdr;
308 u32 opcode = packet->opcode;
309 u32 hdrsize = packet->hlen;
311 u32 pad = packet->pad;
314 struct ib_reth *reth;
316 u8 extra_bytes = pad + packet->extra_byte + (SIZE_OF_CRC << 2);
318 if (hfi1_ruc_check_hdr(ibp, packet))
321 process_ecn(qp, packet);
323 psn = ib_bth_get_psn(ohdr);
324 /* Compare the PSN verses the expected PSN. */
325 if (unlikely(cmp_psn(psn, qp->r_psn) != 0)) {
327 * Handle a sequence error.
328 * Silently drop any current message.
332 if (qp->r_state == OP(SEND_FIRST) ||
333 qp->r_state == OP(SEND_MIDDLE)) {
334 set_bit(RVT_R_REWIND_SGE, &qp->r_aflags);
335 qp->r_sge.num_sge = 0;
337 rvt_put_ss(&qp->r_sge);
339 qp->r_state = OP(SEND_LAST);
343 case OP(SEND_ONLY_WITH_IMMEDIATE):
346 case OP(RDMA_WRITE_FIRST):
347 case OP(RDMA_WRITE_ONLY):
348 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
356 /* Check for opcode sequence errors. */
357 switch (qp->r_state) {
359 case OP(SEND_MIDDLE):
360 if (opcode == OP(SEND_MIDDLE) ||
361 opcode == OP(SEND_LAST) ||
362 opcode == OP(SEND_LAST_WITH_IMMEDIATE))
366 case OP(RDMA_WRITE_FIRST):
367 case OP(RDMA_WRITE_MIDDLE):
368 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
369 opcode == OP(RDMA_WRITE_LAST) ||
370 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
375 if (opcode == OP(SEND_FIRST) ||
376 opcode == OP(SEND_ONLY) ||
377 opcode == OP(SEND_ONLY_WITH_IMMEDIATE) ||
378 opcode == OP(RDMA_WRITE_FIRST) ||
379 opcode == OP(RDMA_WRITE_ONLY) ||
380 opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
385 if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
388 /* OK, process the packet. */
392 case OP(SEND_ONLY_WITH_IMMEDIATE):
394 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags)) {
395 qp->r_sge = qp->s_rdma_read_sge;
397 ret = rvt_get_rwqe(qp, false);
403 * qp->s_rdma_read_sge will be the owner
404 * of the mr references.
406 qp->s_rdma_read_sge = qp->r_sge;
409 if (opcode == OP(SEND_ONLY))
410 goto no_immediate_data;
411 else if (opcode == OP(SEND_ONLY_WITH_IMMEDIATE))
414 case OP(SEND_MIDDLE):
415 /* Check for invalid length PMTU or posted rwqe len. */
417 * There will be no padding for 9B packet but 16B packets
418 * will come in with some padding since we always add
419 * CRC and LT bytes which will need to be flit aligned
421 if (unlikely(tlen != (hdrsize + pmtu + extra_bytes)))
423 qp->r_rcv_len += pmtu;
424 if (unlikely(qp->r_rcv_len > qp->r_len))
426 rvt_copy_sge(qp, &qp->r_sge, data, pmtu, false, false);
429 case OP(SEND_LAST_WITH_IMMEDIATE):
431 wc.ex.imm_data = ohdr->u.imm_data;
432 wc.wc_flags = IB_WC_WITH_IMM;
439 /* Check for invalid length. */
440 /* LAST len should be >= 1 */
441 if (unlikely(tlen < (hdrsize + extra_bytes)))
443 /* Don't count the CRC. */
444 tlen -= (hdrsize + extra_bytes);
445 wc.byte_len = tlen + qp->r_rcv_len;
446 if (unlikely(wc.byte_len > qp->r_len))
448 wc.opcode = IB_WC_RECV;
449 rvt_copy_sge(qp, &qp->r_sge, data, tlen, false, false);
450 rvt_put_ss(&qp->s_rdma_read_sge);
452 wc.wr_id = qp->r_wr_id;
453 wc.status = IB_WC_SUCCESS;
455 wc.src_qp = qp->remote_qpn;
456 wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX;
458 * It seems that IB mandates the presence of an SL in a
459 * work completion only for the UD transport (see section
460 * 11.4.2 of IBTA Vol. 1).
462 * However, the way the SL is chosen below is consistent
463 * with the way that IB/qib works and is trying avoid
464 * introducing incompatibilities.
466 * See also OPA Vol. 1, section 9.7.6, and table 9-17.
468 wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
469 /* zero fields that are N/A */
472 wc.dlid_path_bits = 0;
474 /* Signal completion event if the solicited bit is set. */
475 rvt_recv_cq(qp, &wc, ib_bth_is_solicited(ohdr));
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 rvt_copy_sge(qp, &qp->r_sge, data, pmtu, true, false);
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 /* Check for invalid length. */
531 /* LAST len should be >= 1 */
532 if (unlikely(tlen < (hdrsize + pad + 4)))
534 /* Don't count the CRC. */
535 tlen -= (hdrsize + extra_bytes);
536 if (unlikely(tlen + qp->r_rcv_len != qp->r_len))
538 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags)) {
539 rvt_put_ss(&qp->s_rdma_read_sge);
541 ret = rvt_get_rwqe(qp, true);
547 wc.byte_len = qp->r_len;
548 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
549 rvt_copy_sge(qp, &qp->r_sge, data, tlen, true, false);
550 rvt_put_ss(&qp->r_sge);
553 case OP(RDMA_WRITE_LAST):
555 /* Check for invalid length. */
556 /* LAST len should be >= 1 */
557 if (unlikely(tlen < (hdrsize + pad + 4)))
559 /* Don't count the CRC. */
560 tlen -= (hdrsize + extra_bytes);
561 if (unlikely(tlen + qp->r_rcv_len != qp->r_len))
563 rvt_copy_sge(qp, &qp->r_sge, data, tlen, true, false);
564 rvt_put_ss(&qp->r_sge);
568 /* Drop packet for unknown opcodes. */
572 qp->r_state = opcode;
576 set_bit(RVT_R_REWIND_SGE, &qp->r_aflags);
577 qp->r_sge.num_sge = 0;
579 ibp->rvp.n_pkt_drops++;
583 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);