2 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/skbuff.h>
38 #include "rxe_queue.h"
54 RESPST_DUPLICATE_REQUEST,
55 RESPST_ERR_MALFORMED_WQE,
56 RESPST_ERR_UNSUPPORTED_OPCODE,
57 RESPST_ERR_MISALIGNED_ATOMIC,
58 RESPST_ERR_PSN_OUT_OF_SEQ,
59 RESPST_ERR_MISSING_OPCODE_FIRST,
60 RESPST_ERR_MISSING_OPCODE_LAST_C,
61 RESPST_ERR_MISSING_OPCODE_LAST_D1E,
62 RESPST_ERR_TOO_MANY_RDMA_ATM_REQ,
64 RESPST_ERR_RKEY_VIOLATION,
66 RESPST_ERR_CQ_OVERFLOW,
73 static char *resp_state_name[] = {
74 [RESPST_NONE] = "NONE",
75 [RESPST_GET_REQ] = "GET_REQ",
76 [RESPST_CHK_PSN] = "CHK_PSN",
77 [RESPST_CHK_OP_SEQ] = "CHK_OP_SEQ",
78 [RESPST_CHK_OP_VALID] = "CHK_OP_VALID",
79 [RESPST_CHK_RESOURCE] = "CHK_RESOURCE",
80 [RESPST_CHK_LENGTH] = "CHK_LENGTH",
81 [RESPST_CHK_RKEY] = "CHK_RKEY",
82 [RESPST_EXECUTE] = "EXECUTE",
83 [RESPST_READ_REPLY] = "READ_REPLY",
84 [RESPST_COMPLETE] = "COMPLETE",
85 [RESPST_ACKNOWLEDGE] = "ACKNOWLEDGE",
86 [RESPST_CLEANUP] = "CLEANUP",
87 [RESPST_DUPLICATE_REQUEST] = "DUPLICATE_REQUEST",
88 [RESPST_ERR_MALFORMED_WQE] = "ERR_MALFORMED_WQE",
89 [RESPST_ERR_UNSUPPORTED_OPCODE] = "ERR_UNSUPPORTED_OPCODE",
90 [RESPST_ERR_MISALIGNED_ATOMIC] = "ERR_MISALIGNED_ATOMIC",
91 [RESPST_ERR_PSN_OUT_OF_SEQ] = "ERR_PSN_OUT_OF_SEQ",
92 [RESPST_ERR_MISSING_OPCODE_FIRST] = "ERR_MISSING_OPCODE_FIRST",
93 [RESPST_ERR_MISSING_OPCODE_LAST_C] = "ERR_MISSING_OPCODE_LAST_C",
94 [RESPST_ERR_MISSING_OPCODE_LAST_D1E] = "ERR_MISSING_OPCODE_LAST_D1E",
95 [RESPST_ERR_TOO_MANY_RDMA_ATM_REQ] = "ERR_TOO_MANY_RDMA_ATM_REQ",
96 [RESPST_ERR_RNR] = "ERR_RNR",
97 [RESPST_ERR_RKEY_VIOLATION] = "ERR_RKEY_VIOLATION",
98 [RESPST_ERR_LENGTH] = "ERR_LENGTH",
99 [RESPST_ERR_CQ_OVERFLOW] = "ERR_CQ_OVERFLOW",
100 [RESPST_ERROR] = "ERROR",
101 [RESPST_RESET] = "RESET",
102 [RESPST_DONE] = "DONE",
103 [RESPST_EXIT] = "EXIT",
106 /* rxe_recv calls here to add a request packet to the input queue */
107 void rxe_resp_queue_pkt(struct rxe_dev *rxe, struct rxe_qp *qp,
111 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
113 skb_queue_tail(&qp->req_pkts, skb);
115 must_sched = (pkt->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST) ||
116 (skb_queue_len(&qp->req_pkts) > 1);
118 rxe_run_task(&qp->resp.task, must_sched);
121 static inline enum resp_states get_req(struct rxe_qp *qp,
122 struct rxe_pkt_info **pkt_p)
126 if (qp->resp.state == QP_STATE_ERROR) {
127 skb = skb_dequeue(&qp->req_pkts);
129 /* drain request packet queue */
132 return RESPST_GET_REQ;
135 /* go drain recv wr queue */
136 return RESPST_CHK_RESOURCE;
139 skb = skb_peek(&qp->req_pkts);
143 *pkt_p = SKB_TO_PKT(skb);
145 return (qp->resp.res) ? RESPST_READ_REPLY : RESPST_CHK_PSN;
148 static enum resp_states check_psn(struct rxe_qp *qp,
149 struct rxe_pkt_info *pkt)
151 int diff = psn_compare(pkt->psn, qp->resp.psn);
153 switch (qp_type(qp)) {
156 if (qp->resp.sent_psn_nak)
157 return RESPST_CLEANUP;
159 qp->resp.sent_psn_nak = 1;
160 return RESPST_ERR_PSN_OUT_OF_SEQ;
162 } else if (diff < 0) {
163 return RESPST_DUPLICATE_REQUEST;
166 if (qp->resp.sent_psn_nak)
167 qp->resp.sent_psn_nak = 0;
172 if (qp->resp.drop_msg || diff != 0) {
173 if (pkt->mask & RXE_START_MASK) {
174 qp->resp.drop_msg = 0;
175 return RESPST_CHK_OP_SEQ;
178 qp->resp.drop_msg = 1;
179 return RESPST_CLEANUP;
186 return RESPST_CHK_OP_SEQ;
189 static enum resp_states check_op_seq(struct rxe_qp *qp,
190 struct rxe_pkt_info *pkt)
192 switch (qp_type(qp)) {
194 switch (qp->resp.opcode) {
195 case IB_OPCODE_RC_SEND_FIRST:
196 case IB_OPCODE_RC_SEND_MIDDLE:
197 switch (pkt->opcode) {
198 case IB_OPCODE_RC_SEND_MIDDLE:
199 case IB_OPCODE_RC_SEND_LAST:
200 case IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE:
201 case IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE:
202 return RESPST_CHK_OP_VALID;
204 return RESPST_ERR_MISSING_OPCODE_LAST_C;
207 case IB_OPCODE_RC_RDMA_WRITE_FIRST:
208 case IB_OPCODE_RC_RDMA_WRITE_MIDDLE:
209 switch (pkt->opcode) {
210 case IB_OPCODE_RC_RDMA_WRITE_MIDDLE:
211 case IB_OPCODE_RC_RDMA_WRITE_LAST:
212 case IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
213 return RESPST_CHK_OP_VALID;
215 return RESPST_ERR_MISSING_OPCODE_LAST_C;
219 switch (pkt->opcode) {
220 case IB_OPCODE_RC_SEND_MIDDLE:
221 case IB_OPCODE_RC_SEND_LAST:
222 case IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE:
223 case IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE:
224 case IB_OPCODE_RC_RDMA_WRITE_MIDDLE:
225 case IB_OPCODE_RC_RDMA_WRITE_LAST:
226 case IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
227 return RESPST_ERR_MISSING_OPCODE_FIRST;
229 return RESPST_CHK_OP_VALID;
235 switch (qp->resp.opcode) {
236 case IB_OPCODE_UC_SEND_FIRST:
237 case IB_OPCODE_UC_SEND_MIDDLE:
238 switch (pkt->opcode) {
239 case IB_OPCODE_UC_SEND_MIDDLE:
240 case IB_OPCODE_UC_SEND_LAST:
241 case IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE:
242 return RESPST_CHK_OP_VALID;
244 return RESPST_ERR_MISSING_OPCODE_LAST_D1E;
247 case IB_OPCODE_UC_RDMA_WRITE_FIRST:
248 case IB_OPCODE_UC_RDMA_WRITE_MIDDLE:
249 switch (pkt->opcode) {
250 case IB_OPCODE_UC_RDMA_WRITE_MIDDLE:
251 case IB_OPCODE_UC_RDMA_WRITE_LAST:
252 case IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
253 return RESPST_CHK_OP_VALID;
255 return RESPST_ERR_MISSING_OPCODE_LAST_D1E;
259 switch (pkt->opcode) {
260 case IB_OPCODE_UC_SEND_MIDDLE:
261 case IB_OPCODE_UC_SEND_LAST:
262 case IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE:
263 case IB_OPCODE_UC_RDMA_WRITE_MIDDLE:
264 case IB_OPCODE_UC_RDMA_WRITE_LAST:
265 case IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
266 qp->resp.drop_msg = 1;
267 return RESPST_CLEANUP;
269 return RESPST_CHK_OP_VALID;
275 return RESPST_CHK_OP_VALID;
279 static enum resp_states check_op_valid(struct rxe_qp *qp,
280 struct rxe_pkt_info *pkt)
282 switch (qp_type(qp)) {
284 if (((pkt->mask & RXE_READ_MASK) &&
285 !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_READ)) ||
286 ((pkt->mask & RXE_WRITE_MASK) &&
287 !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_WRITE)) ||
288 ((pkt->mask & RXE_ATOMIC_MASK) &&
289 !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_ATOMIC))) {
290 return RESPST_ERR_UNSUPPORTED_OPCODE;
296 if ((pkt->mask & RXE_WRITE_MASK) &&
297 !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_WRITE)) {
298 qp->resp.drop_msg = 1;
299 return RESPST_CLEANUP;
314 return RESPST_CHK_RESOURCE;
317 static enum resp_states get_srq_wqe(struct rxe_qp *qp)
319 struct rxe_srq *srq = qp->srq;
320 struct rxe_queue *q = srq->rq.queue;
321 struct rxe_recv_wqe *wqe;
325 return RESPST_ERR_RNR;
327 spin_lock_bh(&srq->rq.consumer_lock);
331 spin_unlock_bh(&srq->rq.consumer_lock);
332 return RESPST_ERR_RNR;
335 /* note kernel and user space recv wqes have same size */
336 memcpy(&qp->resp.srq_wqe, wqe, sizeof(qp->resp.srq_wqe));
338 qp->resp.wqe = &qp->resp.srq_wqe.wqe;
341 if (srq->limit && srq->ibsrq.event_handler &&
342 (queue_count(q) < srq->limit)) {
347 spin_unlock_bh(&srq->rq.consumer_lock);
348 return RESPST_CHK_LENGTH;
351 spin_unlock_bh(&srq->rq.consumer_lock);
352 ev.device = qp->ibqp.device;
353 ev.element.srq = qp->ibqp.srq;
354 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
355 srq->ibsrq.event_handler(&ev, srq->ibsrq.srq_context);
356 return RESPST_CHK_LENGTH;
359 static enum resp_states check_resource(struct rxe_qp *qp,
360 struct rxe_pkt_info *pkt)
362 struct rxe_srq *srq = qp->srq;
364 if (qp->resp.state == QP_STATE_ERROR) {
366 qp->resp.status = IB_WC_WR_FLUSH_ERR;
367 return RESPST_COMPLETE;
369 qp->resp.wqe = queue_head(qp->rq.queue);
371 qp->resp.status = IB_WC_WR_FLUSH_ERR;
372 return RESPST_COMPLETE;
381 if (pkt->mask & RXE_READ_OR_ATOMIC) {
382 /* it is the requesters job to not send
383 * too many read/atomic ops, we just
384 * recycle the responder resource queue
386 if (likely(qp->attr.max_dest_rd_atomic > 0))
387 return RESPST_CHK_LENGTH;
389 return RESPST_ERR_TOO_MANY_RDMA_ATM_REQ;
392 if (pkt->mask & RXE_RWR_MASK) {
394 return get_srq_wqe(qp);
396 qp->resp.wqe = queue_head(qp->rq.queue);
397 return (qp->resp.wqe) ? RESPST_CHK_LENGTH : RESPST_ERR_RNR;
400 return RESPST_CHK_LENGTH;
403 static enum resp_states check_length(struct rxe_qp *qp,
404 struct rxe_pkt_info *pkt)
406 switch (qp_type(qp)) {
408 return RESPST_CHK_RKEY;
411 return RESPST_CHK_RKEY;
414 return RESPST_CHK_RKEY;
418 static enum resp_states check_rkey(struct rxe_qp *qp,
419 struct rxe_pkt_info *pkt)
421 struct rxe_mem *mem = NULL;
427 enum resp_states state;
430 if (pkt->mask & (RXE_READ_MASK | RXE_WRITE_MASK)) {
431 if (pkt->mask & RXE_RETH_MASK) {
432 qp->resp.va = reth_va(pkt);
433 qp->resp.rkey = reth_rkey(pkt);
434 qp->resp.resid = reth_len(pkt);
435 qp->resp.length = reth_len(pkt);
437 access = (pkt->mask & RXE_READ_MASK) ? IB_ACCESS_REMOTE_READ
438 : IB_ACCESS_REMOTE_WRITE;
439 } else if (pkt->mask & RXE_ATOMIC_MASK) {
440 qp->resp.va = atmeth_va(pkt);
441 qp->resp.rkey = atmeth_rkey(pkt);
442 qp->resp.resid = sizeof(u64);
443 access = IB_ACCESS_REMOTE_ATOMIC;
445 return RESPST_EXECUTE;
449 rkey = qp->resp.rkey;
450 resid = qp->resp.resid;
451 pktlen = payload_size(pkt);
453 mem = lookup_mem(qp->pd, access, rkey, lookup_remote);
455 state = RESPST_ERR_RKEY_VIOLATION;
459 if (unlikely(mem->state == RXE_MEM_STATE_FREE)) {
460 state = RESPST_ERR_RKEY_VIOLATION;
464 if (mem_check_range(mem, va, resid)) {
465 state = RESPST_ERR_RKEY_VIOLATION;
469 if (pkt->mask & RXE_WRITE_MASK) {
471 if (pktlen != mtu || bth_pad(pkt)) {
472 state = RESPST_ERR_LENGTH;
476 if (pktlen != resid) {
477 state = RESPST_ERR_LENGTH;
480 if ((bth_pad(pkt) != (0x3 & (-resid)))) {
481 /* This case may not be exactly that
482 * but nothing else fits.
484 state = RESPST_ERR_LENGTH;
490 WARN_ON(qp->resp.mr);
493 return RESPST_EXECUTE;
501 static enum resp_states send_data_in(struct rxe_qp *qp, void *data_addr,
505 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
507 err = copy_data(rxe, qp->pd, IB_ACCESS_LOCAL_WRITE, &qp->resp.wqe->dma,
508 data_addr, data_len, to_mem_obj, NULL);
510 return (err == -ENOSPC) ? RESPST_ERR_LENGTH
511 : RESPST_ERR_MALFORMED_WQE;
516 static enum resp_states write_data_in(struct rxe_qp *qp,
517 struct rxe_pkt_info *pkt)
519 enum resp_states rc = RESPST_NONE;
521 int data_len = payload_size(pkt);
523 err = rxe_mem_copy(qp->resp.mr, qp->resp.va, payload_addr(pkt),
524 data_len, to_mem_obj, NULL);
526 rc = RESPST_ERR_RKEY_VIOLATION;
530 qp->resp.va += data_len;
531 qp->resp.resid -= data_len;
537 /* Guarantee atomicity of atomic operations at the machine level. */
538 static DEFINE_SPINLOCK(atomic_ops_lock);
540 static enum resp_states process_atomic(struct rxe_qp *qp,
541 struct rxe_pkt_info *pkt)
543 u64 iova = atmeth_va(pkt);
545 enum resp_states ret;
546 struct rxe_mem *mr = qp->resp.mr;
548 if (mr->state != RXE_MEM_STATE_VALID) {
549 ret = RESPST_ERR_RKEY_VIOLATION;
553 vaddr = iova_to_vaddr(mr, iova, sizeof(u64));
555 /* check vaddr is 8 bytes aligned. */
556 if (!vaddr || (uintptr_t)vaddr & 7) {
557 ret = RESPST_ERR_MISALIGNED_ATOMIC;
561 spin_lock_bh(&atomic_ops_lock);
563 qp->resp.atomic_orig = *vaddr;
565 if (pkt->opcode == IB_OPCODE_RC_COMPARE_SWAP ||
566 pkt->opcode == IB_OPCODE_RD_COMPARE_SWAP) {
567 if (*vaddr == atmeth_comp(pkt))
568 *vaddr = atmeth_swap_add(pkt);
570 *vaddr += atmeth_swap_add(pkt);
573 spin_unlock_bh(&atomic_ops_lock);
580 static struct sk_buff *prepare_ack_packet(struct rxe_qp *qp,
581 struct rxe_pkt_info *pkt,
582 struct rxe_pkt_info *ack,
589 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
600 pad = (-payload) & 0x3;
601 paylen = rxe_opcode[opcode].length + payload + pad + RXE_ICRC_SIZE;
603 skb = rxe_init_packet(rxe, &qp->pri_av, paylen, ack);
608 ack->opcode = opcode;
609 ack->mask = rxe_opcode[opcode].mask;
610 ack->offset = pkt->offset;
611 ack->paylen = paylen;
613 /* fill in bth using the request packet headers */
614 memcpy(ack->hdr, pkt->hdr, pkt->offset + RXE_BTH_BYTES);
616 bth_set_opcode(ack, opcode);
617 bth_set_qpn(ack, qp->attr.dest_qp_num);
618 bth_set_pad(ack, pad);
620 bth_set_psn(ack, psn);
624 if (ack->mask & RXE_AETH_MASK) {
625 aeth_set_syn(ack, syndrome);
626 aeth_set_msn(ack, qp->resp.msn);
629 if (ack->mask & RXE_ATMACK_MASK)
630 atmack_set_orig(ack, qp->resp.atomic_orig);
632 err = rxe_prepare(rxe, ack, skb, &crc);
639 /* CRC computation will be continued by the caller */
642 p = payload_addr(ack) + payload + bth_pad(ack);
649 /* RDMA read response. If res is not NULL, then we have a current RDMA request
650 * being processed or replayed.
652 static enum resp_states read_reply(struct rxe_qp *qp,
653 struct rxe_pkt_info *req_pkt)
655 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
656 struct rxe_pkt_info ack_pkt;
659 enum resp_states state;
663 struct resp_res *res = qp->resp.res;
668 /* This is the first time we process that request. Get a
671 res = &qp->resp.resources[qp->resp.res_head];
673 free_rd_atomic_resource(qp, res);
674 rxe_advance_resp_resource(qp);
676 res->type = RXE_READ_MASK;
678 res->read.va = qp->resp.va;
679 res->read.va_org = qp->resp.va;
681 res->first_psn = req_pkt->psn;
682 res->last_psn = req_pkt->psn +
683 (reth_len(req_pkt) + mtu - 1) /
685 res->cur_psn = req_pkt->psn;
687 res->read.resid = qp->resp.resid;
688 res->read.length = qp->resp.resid;
689 res->read.rkey = qp->resp.rkey;
691 /* note res inherits the reference to mr from qp */
692 res->read.mr = qp->resp.mr;
696 res->state = rdatm_res_state_new;
699 if (res->state == rdatm_res_state_new) {
700 if (res->read.resid <= mtu)
701 opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY;
703 opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST;
705 if (res->read.resid > mtu)
706 opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE;
708 opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST;
711 res->state = rdatm_res_state_next;
713 payload = min_t(int, res->read.resid, mtu);
715 skb = prepare_ack_packet(qp, req_pkt, &ack_pkt, opcode, payload,
716 res->cur_psn, AETH_ACK_UNLIMITED, &icrc);
718 return RESPST_ERR_RNR;
720 err = rxe_mem_copy(res->read.mr, res->read.va, payload_addr(&ack_pkt),
721 payload, from_mem_obj, &icrc);
723 pr_err("Failed copying memory\n");
725 p = payload_addr(&ack_pkt) + payload + bth_pad(&ack_pkt);
728 err = rxe_xmit_packet(rxe, qp, &ack_pkt, skb);
730 pr_err("Failed sending RDMA reply.\n");
732 return RESPST_ERR_RNR;
735 res->read.va += payload;
736 res->read.resid -= payload;
737 res->cur_psn = (res->cur_psn + 1) & BTH_PSN_MASK;
739 if (res->read.resid > 0) {
743 qp->resp.opcode = -1;
744 qp->resp.psn = res->cur_psn;
745 state = RESPST_CLEANUP;
751 static void build_rdma_network_hdr(union rdma_network_hdr *hdr,
752 struct rxe_pkt_info *pkt)
754 struct sk_buff *skb = PKT_TO_SKB(pkt);
756 memset(hdr, 0, sizeof(*hdr));
757 if (skb->protocol == htons(ETH_P_IP))
758 memcpy(&hdr->roce4grh, ip_hdr(skb), sizeof(hdr->roce4grh));
759 else if (skb->protocol == htons(ETH_P_IPV6))
760 memcpy(&hdr->ibgrh, ipv6_hdr(skb), sizeof(hdr->ibgrh));
763 /* Executes a new request. A retried request never reach that function (send
764 * and writes are discarded, and reads and atomics are retried elsewhere.
766 static enum resp_states execute(struct rxe_qp *qp, struct rxe_pkt_info *pkt)
768 enum resp_states err;
770 if (pkt->mask & RXE_SEND_MASK) {
771 if (qp_type(qp) == IB_QPT_UD ||
772 qp_type(qp) == IB_QPT_SMI ||
773 qp_type(qp) == IB_QPT_GSI) {
774 union rdma_network_hdr hdr;
776 build_rdma_network_hdr(&hdr, pkt);
778 err = send_data_in(qp, &hdr, sizeof(hdr));
782 err = send_data_in(qp, payload_addr(pkt), payload_size(pkt));
785 } else if (pkt->mask & RXE_WRITE_MASK) {
786 err = write_data_in(qp, pkt);
789 } else if (pkt->mask & RXE_READ_MASK) {
790 /* For RDMA Read we can increment the msn now. See C9-148. */
792 return RESPST_READ_REPLY;
793 } else if (pkt->mask & RXE_ATOMIC_MASK) {
794 err = process_atomic(qp, pkt);
801 /* next expected psn, read handles this separately */
802 qp->resp.psn = (pkt->psn + 1) & BTH_PSN_MASK;
804 qp->resp.opcode = pkt->opcode;
805 qp->resp.status = IB_WC_SUCCESS;
807 if (pkt->mask & RXE_COMP_MASK) {
808 /* We successfully processed this new request. */
810 return RESPST_COMPLETE;
811 } else if (qp_type(qp) == IB_QPT_RC)
812 return RESPST_ACKNOWLEDGE;
814 return RESPST_CLEANUP;
817 static enum resp_states do_complete(struct rxe_qp *qp,
818 struct rxe_pkt_info *pkt)
821 struct ib_wc *wc = &cqe.ibwc;
822 struct ib_uverbs_wc *uwc = &cqe.uibwc;
823 struct rxe_recv_wqe *wqe = qp->resp.wqe;
826 return RESPST_CLEANUP;
828 memset(&cqe, 0, sizeof(cqe));
830 if (qp->rcq->is_user) {
831 uwc->status = qp->resp.status;
832 uwc->qp_num = qp->ibqp.qp_num;
833 uwc->wr_id = wqe->wr_id;
835 wc->status = qp->resp.status;
837 wc->wr_id = wqe->wr_id;
840 if (wc->status == IB_WC_SUCCESS) {
841 wc->opcode = (pkt->mask & RXE_IMMDT_MASK &&
842 pkt->mask & RXE_WRITE_MASK) ?
843 IB_WC_RECV_RDMA_WITH_IMM : IB_WC_RECV;
845 wc->byte_len = (pkt->mask & RXE_IMMDT_MASK &&
846 pkt->mask & RXE_WRITE_MASK) ?
847 qp->resp.length : wqe->dma.length - wqe->dma.resid;
849 /* fields after byte_len are different between kernel and user
852 if (qp->rcq->is_user) {
853 uwc->wc_flags = IB_WC_GRH;
855 if (pkt->mask & RXE_IMMDT_MASK) {
856 uwc->wc_flags |= IB_WC_WITH_IMM;
858 (__u32 __force)immdt_imm(pkt);
861 if (pkt->mask & RXE_IETH_MASK) {
862 uwc->wc_flags |= IB_WC_WITH_INVALIDATE;
863 uwc->ex.invalidate_rkey = ieth_rkey(pkt);
866 uwc->qp_num = qp->ibqp.qp_num;
868 if (pkt->mask & RXE_DETH_MASK)
869 uwc->src_qp = deth_sqp(pkt);
871 uwc->port_num = qp->attr.port_num;
873 struct sk_buff *skb = PKT_TO_SKB(pkt);
875 wc->wc_flags = IB_WC_GRH | IB_WC_WITH_NETWORK_HDR_TYPE;
876 if (skb->protocol == htons(ETH_P_IP))
877 wc->network_hdr_type = RDMA_NETWORK_IPV4;
879 wc->network_hdr_type = RDMA_NETWORK_IPV6;
881 if (pkt->mask & RXE_IMMDT_MASK) {
882 wc->wc_flags |= IB_WC_WITH_IMM;
883 wc->ex.imm_data = immdt_imm(pkt);
886 if (pkt->mask & RXE_IETH_MASK) {
887 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
890 wc->wc_flags |= IB_WC_WITH_INVALIDATE;
891 wc->ex.invalidate_rkey = ieth_rkey(pkt);
893 rmr = rxe_pool_get_index(&rxe->mr_pool,
894 wc->ex.invalidate_rkey >> 8);
895 if (unlikely(!rmr)) {
896 pr_err("Bad rkey %#x invalidation\n",
897 wc->ex.invalidate_rkey);
900 rmr->state = RXE_MEM_STATE_FREE;
906 if (pkt->mask & RXE_DETH_MASK)
907 wc->src_qp = deth_sqp(pkt);
909 wc->port_num = qp->attr.port_num;
913 /* have copy for srq and reference for !srq */
915 advance_consumer(qp->rq.queue);
919 if (rxe_cq_post(qp->rcq, &cqe, pkt ? bth_se(pkt) : 1))
920 return RESPST_ERR_CQ_OVERFLOW;
922 if (qp->resp.state == QP_STATE_ERROR)
923 return RESPST_CHK_RESOURCE;
927 else if (qp_type(qp) == IB_QPT_RC)
928 return RESPST_ACKNOWLEDGE;
930 return RESPST_CLEANUP;
933 static int send_ack(struct rxe_qp *qp, struct rxe_pkt_info *pkt,
934 u8 syndrome, u32 psn)
937 struct rxe_pkt_info ack_pkt;
939 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
941 skb = prepare_ack_packet(qp, pkt, &ack_pkt, IB_OPCODE_RC_ACKNOWLEDGE,
942 0, psn, syndrome, NULL);
948 err = rxe_xmit_packet(rxe, qp, &ack_pkt, skb);
950 pr_err_ratelimited("Failed sending ack\n");
958 static int send_atomic_ack(struct rxe_qp *qp, struct rxe_pkt_info *pkt,
962 struct rxe_pkt_info ack_pkt;
964 struct sk_buff *skb_copy;
965 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
966 struct resp_res *res;
968 skb = prepare_ack_packet(qp, pkt, &ack_pkt,
969 IB_OPCODE_RC_ATOMIC_ACKNOWLEDGE, 0, pkt->psn,
976 skb_copy = skb_clone(skb, GFP_ATOMIC);
978 rxe_add_ref(qp); /* for the new SKB */
980 pr_warn("Could not clone atomic response\n");
985 res = &qp->resp.resources[qp->resp.res_head];
986 free_rd_atomic_resource(qp, res);
987 rxe_advance_resp_resource(qp);
989 memcpy(SKB_TO_PKT(skb), &ack_pkt, sizeof(ack_pkt));
990 memset((unsigned char *)SKB_TO_PKT(skb) + sizeof(ack_pkt), 0,
991 sizeof(skb->cb) - sizeof(ack_pkt));
993 res->type = RXE_ATOMIC_MASK;
994 res->atomic.skb = skb;
995 res->first_psn = ack_pkt.psn;
996 res->last_psn = ack_pkt.psn;
997 res->cur_psn = ack_pkt.psn;
999 rc = rxe_xmit_packet(rxe, qp, &ack_pkt, skb_copy);
1001 pr_err_ratelimited("Failed sending ack\n");
1003 kfree_skb(skb_copy);
1010 static enum resp_states acknowledge(struct rxe_qp *qp,
1011 struct rxe_pkt_info *pkt)
1013 if (qp_type(qp) != IB_QPT_RC)
1014 return RESPST_CLEANUP;
1016 if (qp->resp.aeth_syndrome != AETH_ACK_UNLIMITED)
1017 send_ack(qp, pkt, qp->resp.aeth_syndrome, pkt->psn);
1018 else if (pkt->mask & RXE_ATOMIC_MASK)
1019 send_atomic_ack(qp, pkt, AETH_ACK_UNLIMITED);
1020 else if (bth_ack(pkt))
1021 send_ack(qp, pkt, AETH_ACK_UNLIMITED, pkt->psn);
1023 return RESPST_CLEANUP;
1026 static enum resp_states cleanup(struct rxe_qp *qp,
1027 struct rxe_pkt_info *pkt)
1029 struct sk_buff *skb;
1032 skb = skb_dequeue(&qp->req_pkts);
1038 rxe_drop_ref(qp->resp.mr);
1045 static struct resp_res *find_resource(struct rxe_qp *qp, u32 psn)
1049 for (i = 0; i < qp->attr.max_rd_atomic; i++) {
1050 struct resp_res *res = &qp->resp.resources[i];
1055 if (psn_compare(psn, res->first_psn) >= 0 &&
1056 psn_compare(psn, res->last_psn) <= 0) {
1064 static enum resp_states duplicate_request(struct rxe_qp *qp,
1065 struct rxe_pkt_info *pkt)
1067 enum resp_states rc;
1069 if (pkt->mask & RXE_SEND_MASK ||
1070 pkt->mask & RXE_WRITE_MASK) {
1071 /* SEND. Ack again and cleanup. C9-105. */
1073 send_ack(qp, pkt, AETH_ACK_UNLIMITED, qp->resp.psn - 1);
1074 rc = RESPST_CLEANUP;
1076 } else if (pkt->mask & RXE_READ_MASK) {
1077 struct resp_res *res;
1079 res = find_resource(qp, pkt->psn);
1081 /* Resource not found. Class D error. Drop the
1084 rc = RESPST_CLEANUP;
1087 /* Ensure this new request is the same as the previous
1088 * one or a subset of it.
1090 u64 iova = reth_va(pkt);
1091 u32 resid = reth_len(pkt);
1093 if (iova < res->read.va_org ||
1094 resid > res->read.length ||
1095 (iova + resid) > (res->read.va_org +
1096 res->read.length)) {
1097 rc = RESPST_CLEANUP;
1101 if (reth_rkey(pkt) != res->read.rkey) {
1102 rc = RESPST_CLEANUP;
1106 res->cur_psn = pkt->psn;
1107 res->state = (pkt->psn == res->first_psn) ?
1108 rdatm_res_state_new :
1109 rdatm_res_state_replay;
1111 /* Reset the resource, except length. */
1112 res->read.va_org = iova;
1113 res->read.va = iova;
1114 res->read.resid = resid;
1116 /* Replay the RDMA read reply. */
1118 rc = RESPST_READ_REPLY;
1122 struct resp_res *res;
1124 /* Find the operation in our list of responder resources. */
1125 res = find_resource(qp, pkt->psn);
1127 struct sk_buff *skb_copy;
1129 skb_copy = skb_clone(res->atomic.skb, GFP_ATOMIC);
1131 rxe_add_ref(qp); /* for the new SKB */
1133 pr_warn("Couldn't clone atomic resp\n");
1134 rc = RESPST_CLEANUP;
1138 /* Resend the result. */
1139 rc = rxe_xmit_packet(to_rdev(qp->ibqp.device), qp,
1142 pr_err("Failed resending result. This flow is not handled - skb ignored\n");
1143 kfree_skb(skb_copy);
1144 rc = RESPST_CLEANUP;
1149 /* Resource not found. Class D error. Drop the request. */
1150 rc = RESPST_CLEANUP;
1157 /* Process a class A or C. Both are treated the same in this implementation. */
1158 static void do_class_ac_error(struct rxe_qp *qp, u8 syndrome,
1159 enum ib_wc_status status)
1161 qp->resp.aeth_syndrome = syndrome;
1162 qp->resp.status = status;
1164 /* indicate that we should go through the ERROR state */
1165 qp->resp.goto_error = 1;
1168 static enum resp_states do_class_d1e_error(struct rxe_qp *qp)
1173 qp->resp.drop_msg = 1;
1175 qp->resp.status = IB_WC_REM_INV_REQ_ERR;
1176 return RESPST_COMPLETE;
1178 return RESPST_CLEANUP;
1181 /* Class D1. This packet may be the start of a
1182 * new message and could be valid. The previous
1183 * message is invalid and ignored. reset the
1184 * recv wr to its original state
1187 qp->resp.wqe->dma.resid = qp->resp.wqe->dma.length;
1188 qp->resp.wqe->dma.cur_sge = 0;
1189 qp->resp.wqe->dma.sge_offset = 0;
1190 qp->resp.opcode = -1;
1194 rxe_drop_ref(qp->resp.mr);
1198 return RESPST_CLEANUP;
1202 int rxe_responder(void *arg)
1204 struct rxe_qp *qp = (struct rxe_qp *)arg;
1205 enum resp_states state;
1206 struct rxe_pkt_info *pkt = NULL;
1209 qp->resp.aeth_syndrome = AETH_ACK_UNLIMITED;
1216 switch (qp->resp.state) {
1217 case QP_STATE_RESET:
1218 state = RESPST_RESET;
1222 state = RESPST_GET_REQ;
1227 pr_debug("qp#%d state = %s\n", qp_num(qp),
1228 resp_state_name[state]);
1230 case RESPST_GET_REQ:
1231 state = get_req(qp, &pkt);
1233 case RESPST_CHK_PSN:
1234 state = check_psn(qp, pkt);
1236 case RESPST_CHK_OP_SEQ:
1237 state = check_op_seq(qp, pkt);
1239 case RESPST_CHK_OP_VALID:
1240 state = check_op_valid(qp, pkt);
1242 case RESPST_CHK_RESOURCE:
1243 state = check_resource(qp, pkt);
1245 case RESPST_CHK_LENGTH:
1246 state = check_length(qp, pkt);
1248 case RESPST_CHK_RKEY:
1249 state = check_rkey(qp, pkt);
1251 case RESPST_EXECUTE:
1252 state = execute(qp, pkt);
1254 case RESPST_COMPLETE:
1255 state = do_complete(qp, pkt);
1257 case RESPST_READ_REPLY:
1258 state = read_reply(qp, pkt);
1260 case RESPST_ACKNOWLEDGE:
1261 state = acknowledge(qp, pkt);
1263 case RESPST_CLEANUP:
1264 state = cleanup(qp, pkt);
1266 case RESPST_DUPLICATE_REQUEST:
1267 state = duplicate_request(qp, pkt);
1269 case RESPST_ERR_PSN_OUT_OF_SEQ:
1270 /* RC only - Class B. Drop packet. */
1271 send_ack(qp, pkt, AETH_NAK_PSN_SEQ_ERROR, qp->resp.psn);
1272 state = RESPST_CLEANUP;
1275 case RESPST_ERR_TOO_MANY_RDMA_ATM_REQ:
1276 case RESPST_ERR_MISSING_OPCODE_FIRST:
1277 case RESPST_ERR_MISSING_OPCODE_LAST_C:
1278 case RESPST_ERR_UNSUPPORTED_OPCODE:
1279 case RESPST_ERR_MISALIGNED_ATOMIC:
1280 /* RC Only - Class C. */
1281 do_class_ac_error(qp, AETH_NAK_INVALID_REQ,
1282 IB_WC_REM_INV_REQ_ERR);
1283 state = RESPST_COMPLETE;
1286 case RESPST_ERR_MISSING_OPCODE_LAST_D1E:
1287 state = do_class_d1e_error(qp);
1289 case RESPST_ERR_RNR:
1290 if (qp_type(qp) == IB_QPT_RC) {
1292 send_ack(qp, pkt, AETH_RNR_NAK |
1294 qp->attr.min_rnr_timer),
1297 /* UD/UC - class D */
1298 qp->resp.drop_msg = 1;
1300 state = RESPST_CLEANUP;
1303 case RESPST_ERR_RKEY_VIOLATION:
1304 if (qp_type(qp) == IB_QPT_RC) {
1306 do_class_ac_error(qp, AETH_NAK_REM_ACC_ERR,
1307 IB_WC_REM_ACCESS_ERR);
1308 state = RESPST_COMPLETE;
1310 qp->resp.drop_msg = 1;
1312 /* UC/SRQ Class D */
1313 qp->resp.status = IB_WC_REM_ACCESS_ERR;
1314 state = RESPST_COMPLETE;
1316 /* UC/non-SRQ Class E. */
1317 state = RESPST_CLEANUP;
1322 case RESPST_ERR_LENGTH:
1323 if (qp_type(qp) == IB_QPT_RC) {
1325 do_class_ac_error(qp, AETH_NAK_INVALID_REQ,
1326 IB_WC_REM_INV_REQ_ERR);
1327 state = RESPST_COMPLETE;
1328 } else if (qp->srq) {
1329 /* UC/UD - class E */
1330 qp->resp.status = IB_WC_REM_INV_REQ_ERR;
1331 state = RESPST_COMPLETE;
1333 /* UC/UD - class D */
1334 qp->resp.drop_msg = 1;
1335 state = RESPST_CLEANUP;
1339 case RESPST_ERR_MALFORMED_WQE:
1341 do_class_ac_error(qp, AETH_NAK_REM_OP_ERR,
1342 IB_WC_LOC_QP_OP_ERR);
1343 state = RESPST_COMPLETE;
1346 case RESPST_ERR_CQ_OVERFLOW:
1348 state = RESPST_ERROR;
1352 if (qp->resp.goto_error) {
1353 state = RESPST_ERROR;
1360 if (qp->resp.goto_error) {
1361 state = RESPST_ERROR;
1367 case RESPST_RESET: {
1368 struct sk_buff *skb;
1370 while ((skb = skb_dequeue(&qp->req_pkts))) {
1375 while (!qp->srq && qp->rq.queue &&
1376 queue_head(qp->rq.queue))
1377 advance_consumer(qp->rq.queue);
1379 qp->resp.wqe = NULL;
1384 qp->resp.goto_error = 0;
1385 pr_warn("qp#%d moved to error state\n", qp_num(qp));