xref: /linux/drivers/infiniband/sw/rxe/rxe_resp.c (revision c79c3c34f75d72a066e292b10aa50fc758c97c89)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
5  */
6 
7 #include <linux/skbuff.h>
8 
9 #include "rxe.h"
10 #include "rxe_loc.h"
11 #include "rxe_queue.h"
12 
13 enum resp_states {
14 	RESPST_NONE,
15 	RESPST_GET_REQ,
16 	RESPST_CHK_PSN,
17 	RESPST_CHK_OP_SEQ,
18 	RESPST_CHK_OP_VALID,
19 	RESPST_CHK_RESOURCE,
20 	RESPST_CHK_LENGTH,
21 	RESPST_CHK_RKEY,
22 	RESPST_EXECUTE,
23 	RESPST_READ_REPLY,
24 	RESPST_COMPLETE,
25 	RESPST_ACKNOWLEDGE,
26 	RESPST_CLEANUP,
27 	RESPST_DUPLICATE_REQUEST,
28 	RESPST_ERR_MALFORMED_WQE,
29 	RESPST_ERR_UNSUPPORTED_OPCODE,
30 	RESPST_ERR_MISALIGNED_ATOMIC,
31 	RESPST_ERR_PSN_OUT_OF_SEQ,
32 	RESPST_ERR_MISSING_OPCODE_FIRST,
33 	RESPST_ERR_MISSING_OPCODE_LAST_C,
34 	RESPST_ERR_MISSING_OPCODE_LAST_D1E,
35 	RESPST_ERR_TOO_MANY_RDMA_ATM_REQ,
36 	RESPST_ERR_RNR,
37 	RESPST_ERR_RKEY_VIOLATION,
38 	RESPST_ERR_LENGTH,
39 	RESPST_ERR_CQ_OVERFLOW,
40 	RESPST_ERROR,
41 	RESPST_RESET,
42 	RESPST_DONE,
43 	RESPST_EXIT,
44 };
45 
46 static char *resp_state_name[] = {
47 	[RESPST_NONE]				= "NONE",
48 	[RESPST_GET_REQ]			= "GET_REQ",
49 	[RESPST_CHK_PSN]			= "CHK_PSN",
50 	[RESPST_CHK_OP_SEQ]			= "CHK_OP_SEQ",
51 	[RESPST_CHK_OP_VALID]			= "CHK_OP_VALID",
52 	[RESPST_CHK_RESOURCE]			= "CHK_RESOURCE",
53 	[RESPST_CHK_LENGTH]			= "CHK_LENGTH",
54 	[RESPST_CHK_RKEY]			= "CHK_RKEY",
55 	[RESPST_EXECUTE]			= "EXECUTE",
56 	[RESPST_READ_REPLY]			= "READ_REPLY",
57 	[RESPST_COMPLETE]			= "COMPLETE",
58 	[RESPST_ACKNOWLEDGE]			= "ACKNOWLEDGE",
59 	[RESPST_CLEANUP]			= "CLEANUP",
60 	[RESPST_DUPLICATE_REQUEST]		= "DUPLICATE_REQUEST",
61 	[RESPST_ERR_MALFORMED_WQE]		= "ERR_MALFORMED_WQE",
62 	[RESPST_ERR_UNSUPPORTED_OPCODE]		= "ERR_UNSUPPORTED_OPCODE",
63 	[RESPST_ERR_MISALIGNED_ATOMIC]		= "ERR_MISALIGNED_ATOMIC",
64 	[RESPST_ERR_PSN_OUT_OF_SEQ]		= "ERR_PSN_OUT_OF_SEQ",
65 	[RESPST_ERR_MISSING_OPCODE_FIRST]	= "ERR_MISSING_OPCODE_FIRST",
66 	[RESPST_ERR_MISSING_OPCODE_LAST_C]	= "ERR_MISSING_OPCODE_LAST_C",
67 	[RESPST_ERR_MISSING_OPCODE_LAST_D1E]	= "ERR_MISSING_OPCODE_LAST_D1E",
68 	[RESPST_ERR_TOO_MANY_RDMA_ATM_REQ]	= "ERR_TOO_MANY_RDMA_ATM_REQ",
69 	[RESPST_ERR_RNR]			= "ERR_RNR",
70 	[RESPST_ERR_RKEY_VIOLATION]		= "ERR_RKEY_VIOLATION",
71 	[RESPST_ERR_LENGTH]			= "ERR_LENGTH",
72 	[RESPST_ERR_CQ_OVERFLOW]		= "ERR_CQ_OVERFLOW",
73 	[RESPST_ERROR]				= "ERROR",
74 	[RESPST_RESET]				= "RESET",
75 	[RESPST_DONE]				= "DONE",
76 	[RESPST_EXIT]				= "EXIT",
77 };
78 
79 /* rxe_recv calls here to add a request packet to the input queue */
80 void rxe_resp_queue_pkt(struct rxe_qp *qp, struct sk_buff *skb)
81 {
82 	int must_sched;
83 	struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
84 
85 	skb_queue_tail(&qp->req_pkts, skb);
86 
87 	must_sched = (pkt->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST) ||
88 			(skb_queue_len(&qp->req_pkts) > 1);
89 
90 	rxe_run_task(&qp->resp.task, must_sched);
91 }
92 
93 static inline enum resp_states get_req(struct rxe_qp *qp,
94 				       struct rxe_pkt_info **pkt_p)
95 {
96 	struct sk_buff *skb;
97 
98 	if (qp->resp.state == QP_STATE_ERROR) {
99 		while ((skb = skb_dequeue(&qp->req_pkts))) {
100 			rxe_drop_ref(qp);
101 			kfree_skb(skb);
102 			ib_device_put(qp->ibqp.device);
103 		}
104 
105 		/* go drain recv wr queue */
106 		return RESPST_CHK_RESOURCE;
107 	}
108 
109 	skb = skb_peek(&qp->req_pkts);
110 	if (!skb)
111 		return RESPST_EXIT;
112 
113 	*pkt_p = SKB_TO_PKT(skb);
114 
115 	return (qp->resp.res) ? RESPST_READ_REPLY : RESPST_CHK_PSN;
116 }
117 
118 static enum resp_states check_psn(struct rxe_qp *qp,
119 				  struct rxe_pkt_info *pkt)
120 {
121 	int diff = psn_compare(pkt->psn, qp->resp.psn);
122 	struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
123 
124 	switch (qp_type(qp)) {
125 	case IB_QPT_RC:
126 		if (diff > 0) {
127 			if (qp->resp.sent_psn_nak)
128 				return RESPST_CLEANUP;
129 
130 			qp->resp.sent_psn_nak = 1;
131 			rxe_counter_inc(rxe, RXE_CNT_OUT_OF_SEQ_REQ);
132 			return RESPST_ERR_PSN_OUT_OF_SEQ;
133 
134 		} else if (diff < 0) {
135 			rxe_counter_inc(rxe, RXE_CNT_DUP_REQ);
136 			return RESPST_DUPLICATE_REQUEST;
137 		}
138 
139 		if (qp->resp.sent_psn_nak)
140 			qp->resp.sent_psn_nak = 0;
141 
142 		break;
143 
144 	case IB_QPT_UC:
145 		if (qp->resp.drop_msg || diff != 0) {
146 			if (pkt->mask & RXE_START_MASK) {
147 				qp->resp.drop_msg = 0;
148 				return RESPST_CHK_OP_SEQ;
149 			}
150 
151 			qp->resp.drop_msg = 1;
152 			return RESPST_CLEANUP;
153 		}
154 		break;
155 	default:
156 		break;
157 	}
158 
159 	return RESPST_CHK_OP_SEQ;
160 }
161 
162 static enum resp_states check_op_seq(struct rxe_qp *qp,
163 				     struct rxe_pkt_info *pkt)
164 {
165 	switch (qp_type(qp)) {
166 	case IB_QPT_RC:
167 		switch (qp->resp.opcode) {
168 		case IB_OPCODE_RC_SEND_FIRST:
169 		case IB_OPCODE_RC_SEND_MIDDLE:
170 			switch (pkt->opcode) {
171 			case IB_OPCODE_RC_SEND_MIDDLE:
172 			case IB_OPCODE_RC_SEND_LAST:
173 			case IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE:
174 			case IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE:
175 				return RESPST_CHK_OP_VALID;
176 			default:
177 				return RESPST_ERR_MISSING_OPCODE_LAST_C;
178 			}
179 
180 		case IB_OPCODE_RC_RDMA_WRITE_FIRST:
181 		case IB_OPCODE_RC_RDMA_WRITE_MIDDLE:
182 			switch (pkt->opcode) {
183 			case IB_OPCODE_RC_RDMA_WRITE_MIDDLE:
184 			case IB_OPCODE_RC_RDMA_WRITE_LAST:
185 			case IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
186 				return RESPST_CHK_OP_VALID;
187 			default:
188 				return RESPST_ERR_MISSING_OPCODE_LAST_C;
189 			}
190 
191 		default:
192 			switch (pkt->opcode) {
193 			case IB_OPCODE_RC_SEND_MIDDLE:
194 			case IB_OPCODE_RC_SEND_LAST:
195 			case IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE:
196 			case IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE:
197 			case IB_OPCODE_RC_RDMA_WRITE_MIDDLE:
198 			case IB_OPCODE_RC_RDMA_WRITE_LAST:
199 			case IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
200 				return RESPST_ERR_MISSING_OPCODE_FIRST;
201 			default:
202 				return RESPST_CHK_OP_VALID;
203 			}
204 		}
205 		break;
206 
207 	case IB_QPT_UC:
208 		switch (qp->resp.opcode) {
209 		case IB_OPCODE_UC_SEND_FIRST:
210 		case IB_OPCODE_UC_SEND_MIDDLE:
211 			switch (pkt->opcode) {
212 			case IB_OPCODE_UC_SEND_MIDDLE:
213 			case IB_OPCODE_UC_SEND_LAST:
214 			case IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE:
215 				return RESPST_CHK_OP_VALID;
216 			default:
217 				return RESPST_ERR_MISSING_OPCODE_LAST_D1E;
218 			}
219 
220 		case IB_OPCODE_UC_RDMA_WRITE_FIRST:
221 		case IB_OPCODE_UC_RDMA_WRITE_MIDDLE:
222 			switch (pkt->opcode) {
223 			case IB_OPCODE_UC_RDMA_WRITE_MIDDLE:
224 			case IB_OPCODE_UC_RDMA_WRITE_LAST:
225 			case IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
226 				return RESPST_CHK_OP_VALID;
227 			default:
228 				return RESPST_ERR_MISSING_OPCODE_LAST_D1E;
229 			}
230 
231 		default:
232 			switch (pkt->opcode) {
233 			case IB_OPCODE_UC_SEND_MIDDLE:
234 			case IB_OPCODE_UC_SEND_LAST:
235 			case IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE:
236 			case IB_OPCODE_UC_RDMA_WRITE_MIDDLE:
237 			case IB_OPCODE_UC_RDMA_WRITE_LAST:
238 			case IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
239 				qp->resp.drop_msg = 1;
240 				return RESPST_CLEANUP;
241 			default:
242 				return RESPST_CHK_OP_VALID;
243 			}
244 		}
245 		break;
246 
247 	default:
248 		return RESPST_CHK_OP_VALID;
249 	}
250 }
251 
252 static enum resp_states check_op_valid(struct rxe_qp *qp,
253 				       struct rxe_pkt_info *pkt)
254 {
255 	switch (qp_type(qp)) {
256 	case IB_QPT_RC:
257 		if (((pkt->mask & RXE_READ_MASK) &&
258 		     !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_READ)) ||
259 		    ((pkt->mask & RXE_WRITE_MASK) &&
260 		     !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_WRITE)) ||
261 		    ((pkt->mask & RXE_ATOMIC_MASK) &&
262 		     !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_ATOMIC))) {
263 			return RESPST_ERR_UNSUPPORTED_OPCODE;
264 		}
265 
266 		break;
267 
268 	case IB_QPT_UC:
269 		if ((pkt->mask & RXE_WRITE_MASK) &&
270 		    !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_WRITE)) {
271 			qp->resp.drop_msg = 1;
272 			return RESPST_CLEANUP;
273 		}
274 
275 		break;
276 
277 	case IB_QPT_UD:
278 	case IB_QPT_SMI:
279 	case IB_QPT_GSI:
280 		break;
281 
282 	default:
283 		WARN_ON_ONCE(1);
284 		break;
285 	}
286 
287 	return RESPST_CHK_RESOURCE;
288 }
289 
290 static enum resp_states get_srq_wqe(struct rxe_qp *qp)
291 {
292 	struct rxe_srq *srq = qp->srq;
293 	struct rxe_queue *q = srq->rq.queue;
294 	struct rxe_recv_wqe *wqe;
295 	struct ib_event ev;
296 
297 	if (srq->error)
298 		return RESPST_ERR_RNR;
299 
300 	spin_lock_bh(&srq->rq.consumer_lock);
301 
302 	wqe = queue_head(q);
303 	if (!wqe) {
304 		spin_unlock_bh(&srq->rq.consumer_lock);
305 		return RESPST_ERR_RNR;
306 	}
307 
308 	/* note kernel and user space recv wqes have same size */
309 	memcpy(&qp->resp.srq_wqe, wqe, sizeof(qp->resp.srq_wqe));
310 
311 	qp->resp.wqe = &qp->resp.srq_wqe.wqe;
312 	advance_consumer(q);
313 
314 	if (srq->limit && srq->ibsrq.event_handler &&
315 	    (queue_count(q) < srq->limit)) {
316 		srq->limit = 0;
317 		goto event;
318 	}
319 
320 	spin_unlock_bh(&srq->rq.consumer_lock);
321 	return RESPST_CHK_LENGTH;
322 
323 event:
324 	spin_unlock_bh(&srq->rq.consumer_lock);
325 	ev.device = qp->ibqp.device;
326 	ev.element.srq = qp->ibqp.srq;
327 	ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
328 	srq->ibsrq.event_handler(&ev, srq->ibsrq.srq_context);
329 	return RESPST_CHK_LENGTH;
330 }
331 
332 static enum resp_states check_resource(struct rxe_qp *qp,
333 				       struct rxe_pkt_info *pkt)
334 {
335 	struct rxe_srq *srq = qp->srq;
336 
337 	if (qp->resp.state == QP_STATE_ERROR) {
338 		if (qp->resp.wqe) {
339 			qp->resp.status = IB_WC_WR_FLUSH_ERR;
340 			return RESPST_COMPLETE;
341 		} else if (!srq) {
342 			qp->resp.wqe = queue_head(qp->rq.queue);
343 			if (qp->resp.wqe) {
344 				qp->resp.status = IB_WC_WR_FLUSH_ERR;
345 				return RESPST_COMPLETE;
346 			} else {
347 				return RESPST_EXIT;
348 			}
349 		} else {
350 			return RESPST_EXIT;
351 		}
352 	}
353 
354 	if (pkt->mask & RXE_READ_OR_ATOMIC) {
355 		/* it is the requesters job to not send
356 		 * too many read/atomic ops, we just
357 		 * recycle the responder resource queue
358 		 */
359 		if (likely(qp->attr.max_dest_rd_atomic > 0))
360 			return RESPST_CHK_LENGTH;
361 		else
362 			return RESPST_ERR_TOO_MANY_RDMA_ATM_REQ;
363 	}
364 
365 	if (pkt->mask & RXE_RWR_MASK) {
366 		if (srq)
367 			return get_srq_wqe(qp);
368 
369 		qp->resp.wqe = queue_head(qp->rq.queue);
370 		return (qp->resp.wqe) ? RESPST_CHK_LENGTH : RESPST_ERR_RNR;
371 	}
372 
373 	return RESPST_CHK_LENGTH;
374 }
375 
376 static enum resp_states check_length(struct rxe_qp *qp,
377 				     struct rxe_pkt_info *pkt)
378 {
379 	switch (qp_type(qp)) {
380 	case IB_QPT_RC:
381 		return RESPST_CHK_RKEY;
382 
383 	case IB_QPT_UC:
384 		return RESPST_CHK_RKEY;
385 
386 	default:
387 		return RESPST_CHK_RKEY;
388 	}
389 }
390 
391 static enum resp_states check_rkey(struct rxe_qp *qp,
392 				   struct rxe_pkt_info *pkt)
393 {
394 	struct rxe_mem *mem = NULL;
395 	u64 va;
396 	u32 rkey;
397 	u32 resid;
398 	u32 pktlen;
399 	int mtu = qp->mtu;
400 	enum resp_states state;
401 	int access;
402 
403 	if (pkt->mask & (RXE_READ_MASK | RXE_WRITE_MASK)) {
404 		if (pkt->mask & RXE_RETH_MASK) {
405 			qp->resp.va = reth_va(pkt);
406 			qp->resp.rkey = reth_rkey(pkt);
407 			qp->resp.resid = reth_len(pkt);
408 			qp->resp.length = reth_len(pkt);
409 		}
410 		access = (pkt->mask & RXE_READ_MASK) ? IB_ACCESS_REMOTE_READ
411 						     : IB_ACCESS_REMOTE_WRITE;
412 	} else if (pkt->mask & RXE_ATOMIC_MASK) {
413 		qp->resp.va = atmeth_va(pkt);
414 		qp->resp.rkey = atmeth_rkey(pkt);
415 		qp->resp.resid = sizeof(u64);
416 		access = IB_ACCESS_REMOTE_ATOMIC;
417 	} else {
418 		return RESPST_EXECUTE;
419 	}
420 
421 	/* A zero-byte op is not required to set an addr or rkey. */
422 	if ((pkt->mask & (RXE_READ_MASK | RXE_WRITE_OR_SEND)) &&
423 	    (pkt->mask & RXE_RETH_MASK) &&
424 	    reth_len(pkt) == 0) {
425 		return RESPST_EXECUTE;
426 	}
427 
428 	va	= qp->resp.va;
429 	rkey	= qp->resp.rkey;
430 	resid	= qp->resp.resid;
431 	pktlen	= payload_size(pkt);
432 
433 	mem = lookup_mem(qp->pd, access, rkey, lookup_remote);
434 	if (!mem) {
435 		state = RESPST_ERR_RKEY_VIOLATION;
436 		goto err;
437 	}
438 
439 	if (unlikely(mem->state == RXE_MEM_STATE_FREE)) {
440 		state = RESPST_ERR_RKEY_VIOLATION;
441 		goto err;
442 	}
443 
444 	if (mem_check_range(mem, va, resid)) {
445 		state = RESPST_ERR_RKEY_VIOLATION;
446 		goto err;
447 	}
448 
449 	if (pkt->mask & RXE_WRITE_MASK)	 {
450 		if (resid > mtu) {
451 			if (pktlen != mtu || bth_pad(pkt)) {
452 				state = RESPST_ERR_LENGTH;
453 				goto err;
454 			}
455 		} else {
456 			if (pktlen != resid) {
457 				state = RESPST_ERR_LENGTH;
458 				goto err;
459 			}
460 			if ((bth_pad(pkt) != (0x3 & (-resid)))) {
461 				/* This case may not be exactly that
462 				 * but nothing else fits.
463 				 */
464 				state = RESPST_ERR_LENGTH;
465 				goto err;
466 			}
467 		}
468 	}
469 
470 	WARN_ON_ONCE(qp->resp.mr);
471 
472 	qp->resp.mr = mem;
473 	return RESPST_EXECUTE;
474 
475 err:
476 	if (mem)
477 		rxe_drop_ref(mem);
478 	return state;
479 }
480 
481 static enum resp_states send_data_in(struct rxe_qp *qp, void *data_addr,
482 				     int data_len)
483 {
484 	int err;
485 
486 	err = copy_data(qp->pd, IB_ACCESS_LOCAL_WRITE, &qp->resp.wqe->dma,
487 			data_addr, data_len, to_mem_obj, NULL);
488 	if (unlikely(err))
489 		return (err == -ENOSPC) ? RESPST_ERR_LENGTH
490 					: RESPST_ERR_MALFORMED_WQE;
491 
492 	return RESPST_NONE;
493 }
494 
495 static enum resp_states write_data_in(struct rxe_qp *qp,
496 				      struct rxe_pkt_info *pkt)
497 {
498 	enum resp_states rc = RESPST_NONE;
499 	int	err;
500 	int data_len = payload_size(pkt);
501 
502 	err = rxe_mem_copy(qp->resp.mr, qp->resp.va, payload_addr(pkt),
503 			   data_len, to_mem_obj, NULL);
504 	if (err) {
505 		rc = RESPST_ERR_RKEY_VIOLATION;
506 		goto out;
507 	}
508 
509 	qp->resp.va += data_len;
510 	qp->resp.resid -= data_len;
511 
512 out:
513 	return rc;
514 }
515 
516 /* Guarantee atomicity of atomic operations at the machine level. */
517 static DEFINE_SPINLOCK(atomic_ops_lock);
518 
519 static enum resp_states process_atomic(struct rxe_qp *qp,
520 				       struct rxe_pkt_info *pkt)
521 {
522 	u64 iova = atmeth_va(pkt);
523 	u64 *vaddr;
524 	enum resp_states ret;
525 	struct rxe_mem *mr = qp->resp.mr;
526 
527 	if (mr->state != RXE_MEM_STATE_VALID) {
528 		ret = RESPST_ERR_RKEY_VIOLATION;
529 		goto out;
530 	}
531 
532 	vaddr = iova_to_vaddr(mr, iova, sizeof(u64));
533 
534 	/* check vaddr is 8 bytes aligned. */
535 	if (!vaddr || (uintptr_t)vaddr & 7) {
536 		ret = RESPST_ERR_MISALIGNED_ATOMIC;
537 		goto out;
538 	}
539 
540 	spin_lock_bh(&atomic_ops_lock);
541 
542 	qp->resp.atomic_orig = *vaddr;
543 
544 	if (pkt->opcode == IB_OPCODE_RC_COMPARE_SWAP ||
545 	    pkt->opcode == IB_OPCODE_RD_COMPARE_SWAP) {
546 		if (*vaddr == atmeth_comp(pkt))
547 			*vaddr = atmeth_swap_add(pkt);
548 	} else {
549 		*vaddr += atmeth_swap_add(pkt);
550 	}
551 
552 	spin_unlock_bh(&atomic_ops_lock);
553 
554 	ret = RESPST_NONE;
555 out:
556 	return ret;
557 }
558 
559 static struct sk_buff *prepare_ack_packet(struct rxe_qp *qp,
560 					  struct rxe_pkt_info *pkt,
561 					  struct rxe_pkt_info *ack,
562 					  int opcode,
563 					  int payload,
564 					  u32 psn,
565 					  u8 syndrome,
566 					  u32 *crcp)
567 {
568 	struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
569 	struct sk_buff *skb;
570 	u32 crc = 0;
571 	u32 *p;
572 	int paylen;
573 	int pad;
574 	int err;
575 
576 	/*
577 	 * allocate packet
578 	 */
579 	pad = (-payload) & 0x3;
580 	paylen = rxe_opcode[opcode].length + payload + pad + RXE_ICRC_SIZE;
581 
582 	skb = rxe_init_packet(rxe, &qp->pri_av, paylen, ack);
583 	if (!skb)
584 		return NULL;
585 
586 	ack->qp = qp;
587 	ack->opcode = opcode;
588 	ack->mask = rxe_opcode[opcode].mask;
589 	ack->paylen = paylen;
590 
591 	/* fill in bth using the request packet headers */
592 	memcpy(ack->hdr, pkt->hdr, RXE_BTH_BYTES);
593 
594 	bth_set_opcode(ack, opcode);
595 	bth_set_qpn(ack, qp->attr.dest_qp_num);
596 	bth_set_pad(ack, pad);
597 	bth_set_se(ack, 0);
598 	bth_set_psn(ack, psn);
599 	bth_set_ack(ack, 0);
600 	ack->psn = psn;
601 
602 	if (ack->mask & RXE_AETH_MASK) {
603 		aeth_set_syn(ack, syndrome);
604 		aeth_set_msn(ack, qp->resp.msn);
605 	}
606 
607 	if (ack->mask & RXE_ATMACK_MASK)
608 		atmack_set_orig(ack, qp->resp.atomic_orig);
609 
610 	err = rxe_prepare(ack, skb, &crc);
611 	if (err) {
612 		kfree_skb(skb);
613 		return NULL;
614 	}
615 
616 	if (crcp) {
617 		/* CRC computation will be continued by the caller */
618 		*crcp = crc;
619 	} else {
620 		p = payload_addr(ack) + payload + bth_pad(ack);
621 		*p = ~crc;
622 	}
623 
624 	return skb;
625 }
626 
627 /* RDMA read response. If res is not NULL, then we have a current RDMA request
628  * being processed or replayed.
629  */
630 static enum resp_states read_reply(struct rxe_qp *qp,
631 				   struct rxe_pkt_info *req_pkt)
632 {
633 	struct rxe_pkt_info ack_pkt;
634 	struct sk_buff *skb;
635 	int mtu = qp->mtu;
636 	enum resp_states state;
637 	int payload;
638 	int opcode;
639 	int err;
640 	struct resp_res *res = qp->resp.res;
641 	u32 icrc;
642 	u32 *p;
643 
644 	if (!res) {
645 		/* This is the first time we process that request. Get a
646 		 * resource
647 		 */
648 		res = &qp->resp.resources[qp->resp.res_head];
649 
650 		free_rd_atomic_resource(qp, res);
651 		rxe_advance_resp_resource(qp);
652 
653 		res->type		= RXE_READ_MASK;
654 		res->replay		= 0;
655 
656 		res->read.va		= qp->resp.va;
657 		res->read.va_org	= qp->resp.va;
658 
659 		res->first_psn		= req_pkt->psn;
660 
661 		if (reth_len(req_pkt)) {
662 			res->last_psn	= (req_pkt->psn +
663 					   (reth_len(req_pkt) + mtu - 1) /
664 					   mtu - 1) & BTH_PSN_MASK;
665 		} else {
666 			res->last_psn	= res->first_psn;
667 		}
668 		res->cur_psn		= req_pkt->psn;
669 
670 		res->read.resid		= qp->resp.resid;
671 		res->read.length	= qp->resp.resid;
672 		res->read.rkey		= qp->resp.rkey;
673 
674 		/* note res inherits the reference to mr from qp */
675 		res->read.mr		= qp->resp.mr;
676 		qp->resp.mr		= NULL;
677 
678 		qp->resp.res		= res;
679 		res->state		= rdatm_res_state_new;
680 	}
681 
682 	if (res->state == rdatm_res_state_new) {
683 		if (res->read.resid <= mtu)
684 			opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY;
685 		else
686 			opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST;
687 	} else {
688 		if (res->read.resid > mtu)
689 			opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE;
690 		else
691 			opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST;
692 	}
693 
694 	res->state = rdatm_res_state_next;
695 
696 	payload = min_t(int, res->read.resid, mtu);
697 
698 	skb = prepare_ack_packet(qp, req_pkt, &ack_pkt, opcode, payload,
699 				 res->cur_psn, AETH_ACK_UNLIMITED, &icrc);
700 	if (!skb)
701 		return RESPST_ERR_RNR;
702 
703 	err = rxe_mem_copy(res->read.mr, res->read.va, payload_addr(&ack_pkt),
704 			   payload, from_mem_obj, &icrc);
705 	if (err)
706 		pr_err("Failed copying memory\n");
707 
708 	if (bth_pad(&ack_pkt)) {
709 		struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
710 		u8 *pad = payload_addr(&ack_pkt) + payload;
711 
712 		memset(pad, 0, bth_pad(&ack_pkt));
713 		icrc = rxe_crc32(rxe, icrc, pad, bth_pad(&ack_pkt));
714 	}
715 	p = payload_addr(&ack_pkt) + payload + bth_pad(&ack_pkt);
716 	*p = ~icrc;
717 
718 	err = rxe_xmit_packet(qp, &ack_pkt, skb);
719 	if (err) {
720 		pr_err("Failed sending RDMA reply.\n");
721 		return RESPST_ERR_RNR;
722 	}
723 
724 	res->read.va += payload;
725 	res->read.resid -= payload;
726 	res->cur_psn = (res->cur_psn + 1) & BTH_PSN_MASK;
727 
728 	if (res->read.resid > 0) {
729 		state = RESPST_DONE;
730 	} else {
731 		qp->resp.res = NULL;
732 		if (!res->replay)
733 			qp->resp.opcode = -1;
734 		if (psn_compare(res->cur_psn, qp->resp.psn) >= 0)
735 			qp->resp.psn = res->cur_psn;
736 		state = RESPST_CLEANUP;
737 	}
738 
739 	return state;
740 }
741 
742 static void build_rdma_network_hdr(union rdma_network_hdr *hdr,
743 				   struct rxe_pkt_info *pkt)
744 {
745 	struct sk_buff *skb = PKT_TO_SKB(pkt);
746 
747 	memset(hdr, 0, sizeof(*hdr));
748 	if (skb->protocol == htons(ETH_P_IP))
749 		memcpy(&hdr->roce4grh, ip_hdr(skb), sizeof(hdr->roce4grh));
750 	else if (skb->protocol == htons(ETH_P_IPV6))
751 		memcpy(&hdr->ibgrh, ipv6_hdr(skb), sizeof(hdr->ibgrh));
752 }
753 
754 /* Executes a new request. A retried request never reach that function (send
755  * and writes are discarded, and reads and atomics are retried elsewhere.
756  */
757 static enum resp_states execute(struct rxe_qp *qp, struct rxe_pkt_info *pkt)
758 {
759 	enum resp_states err;
760 
761 	if (pkt->mask & RXE_SEND_MASK) {
762 		if (qp_type(qp) == IB_QPT_UD ||
763 		    qp_type(qp) == IB_QPT_SMI ||
764 		    qp_type(qp) == IB_QPT_GSI) {
765 			union rdma_network_hdr hdr;
766 
767 			build_rdma_network_hdr(&hdr, pkt);
768 
769 			err = send_data_in(qp, &hdr, sizeof(hdr));
770 			if (err)
771 				return err;
772 		}
773 		err = send_data_in(qp, payload_addr(pkt), payload_size(pkt));
774 		if (err)
775 			return err;
776 	} else if (pkt->mask & RXE_WRITE_MASK) {
777 		err = write_data_in(qp, pkt);
778 		if (err)
779 			return err;
780 	} else if (pkt->mask & RXE_READ_MASK) {
781 		/* For RDMA Read we can increment the msn now. See C9-148. */
782 		qp->resp.msn++;
783 		return RESPST_READ_REPLY;
784 	} else if (pkt->mask & RXE_ATOMIC_MASK) {
785 		err = process_atomic(qp, pkt);
786 		if (err)
787 			return err;
788 	} else {
789 		/* Unreachable */
790 		WARN_ON_ONCE(1);
791 	}
792 
793 	/* next expected psn, read handles this separately */
794 	qp->resp.psn = (pkt->psn + 1) & BTH_PSN_MASK;
795 	qp->resp.ack_psn = qp->resp.psn;
796 
797 	qp->resp.opcode = pkt->opcode;
798 	qp->resp.status = IB_WC_SUCCESS;
799 
800 	if (pkt->mask & RXE_COMP_MASK) {
801 		/* We successfully processed this new request. */
802 		qp->resp.msn++;
803 		return RESPST_COMPLETE;
804 	} else if (qp_type(qp) == IB_QPT_RC)
805 		return RESPST_ACKNOWLEDGE;
806 	else
807 		return RESPST_CLEANUP;
808 }
809 
810 static enum resp_states do_complete(struct rxe_qp *qp,
811 				    struct rxe_pkt_info *pkt)
812 {
813 	struct rxe_cqe cqe;
814 	struct ib_wc *wc = &cqe.ibwc;
815 	struct ib_uverbs_wc *uwc = &cqe.uibwc;
816 	struct rxe_recv_wqe *wqe = qp->resp.wqe;
817 	struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
818 
819 	if (unlikely(!wqe))
820 		return RESPST_CLEANUP;
821 
822 	memset(&cqe, 0, sizeof(cqe));
823 
824 	if (qp->rcq->is_user) {
825 		uwc->status             = qp->resp.status;
826 		uwc->qp_num             = qp->ibqp.qp_num;
827 		uwc->wr_id              = wqe->wr_id;
828 	} else {
829 		wc->status              = qp->resp.status;
830 		wc->qp                  = &qp->ibqp;
831 		wc->wr_id               = wqe->wr_id;
832 	}
833 
834 	if (wc->status == IB_WC_SUCCESS) {
835 		rxe_counter_inc(rxe, RXE_CNT_RDMA_RECV);
836 		wc->opcode = (pkt->mask & RXE_IMMDT_MASK &&
837 				pkt->mask & RXE_WRITE_MASK) ?
838 					IB_WC_RECV_RDMA_WITH_IMM : IB_WC_RECV;
839 		wc->vendor_err = 0;
840 		wc->byte_len = (pkt->mask & RXE_IMMDT_MASK &&
841 				pkt->mask & RXE_WRITE_MASK) ?
842 					qp->resp.length : wqe->dma.length - wqe->dma.resid;
843 
844 		/* fields after byte_len are different between kernel and user
845 		 * space
846 		 */
847 		if (qp->rcq->is_user) {
848 			uwc->wc_flags = IB_WC_GRH;
849 
850 			if (pkt->mask & RXE_IMMDT_MASK) {
851 				uwc->wc_flags |= IB_WC_WITH_IMM;
852 				uwc->ex.imm_data = immdt_imm(pkt);
853 			}
854 
855 			if (pkt->mask & RXE_IETH_MASK) {
856 				uwc->wc_flags |= IB_WC_WITH_INVALIDATE;
857 				uwc->ex.invalidate_rkey = ieth_rkey(pkt);
858 			}
859 
860 			uwc->qp_num		= qp->ibqp.qp_num;
861 
862 			if (pkt->mask & RXE_DETH_MASK)
863 				uwc->src_qp = deth_sqp(pkt);
864 
865 			uwc->port_num		= qp->attr.port_num;
866 		} else {
867 			struct sk_buff *skb = PKT_TO_SKB(pkt);
868 
869 			wc->wc_flags = IB_WC_GRH | IB_WC_WITH_NETWORK_HDR_TYPE;
870 			if (skb->protocol == htons(ETH_P_IP))
871 				wc->network_hdr_type = RDMA_NETWORK_IPV4;
872 			else
873 				wc->network_hdr_type = RDMA_NETWORK_IPV6;
874 
875 			if (is_vlan_dev(skb->dev)) {
876 				wc->wc_flags |= IB_WC_WITH_VLAN;
877 				wc->vlan_id = vlan_dev_vlan_id(skb->dev);
878 			}
879 
880 			if (pkt->mask & RXE_IMMDT_MASK) {
881 				wc->wc_flags |= IB_WC_WITH_IMM;
882 				wc->ex.imm_data = immdt_imm(pkt);
883 			}
884 
885 			if (pkt->mask & RXE_IETH_MASK) {
886 				struct rxe_mem *rmr;
887 
888 				wc->wc_flags |= IB_WC_WITH_INVALIDATE;
889 				wc->ex.invalidate_rkey = ieth_rkey(pkt);
890 
891 				rmr = rxe_pool_get_index(&rxe->mr_pool,
892 							 wc->ex.invalidate_rkey >> 8);
893 				if (unlikely(!rmr)) {
894 					pr_err("Bad rkey %#x invalidation\n",
895 					       wc->ex.invalidate_rkey);
896 					return RESPST_ERROR;
897 				}
898 				rmr->state = RXE_MEM_STATE_FREE;
899 				rxe_drop_ref(rmr);
900 			}
901 
902 			wc->qp			= &qp->ibqp;
903 
904 			if (pkt->mask & RXE_DETH_MASK)
905 				wc->src_qp = deth_sqp(pkt);
906 
907 			wc->port_num		= qp->attr.port_num;
908 		}
909 	}
910 
911 	/* have copy for srq and reference for !srq */
912 	if (!qp->srq)
913 		advance_consumer(qp->rq.queue);
914 
915 	qp->resp.wqe = NULL;
916 
917 	if (rxe_cq_post(qp->rcq, &cqe, pkt ? bth_se(pkt) : 1))
918 		return RESPST_ERR_CQ_OVERFLOW;
919 
920 	if (qp->resp.state == QP_STATE_ERROR)
921 		return RESPST_CHK_RESOURCE;
922 
923 	if (!pkt)
924 		return RESPST_DONE;
925 	else if (qp_type(qp) == IB_QPT_RC)
926 		return RESPST_ACKNOWLEDGE;
927 	else
928 		return RESPST_CLEANUP;
929 }
930 
931 static int send_ack(struct rxe_qp *qp, struct rxe_pkt_info *pkt,
932 		    u8 syndrome, u32 psn)
933 {
934 	int err = 0;
935 	struct rxe_pkt_info ack_pkt;
936 	struct sk_buff *skb;
937 
938 	skb = prepare_ack_packet(qp, pkt, &ack_pkt, IB_OPCODE_RC_ACKNOWLEDGE,
939 				 0, psn, syndrome, NULL);
940 	if (!skb) {
941 		err = -ENOMEM;
942 		goto err1;
943 	}
944 
945 	err = rxe_xmit_packet(qp, &ack_pkt, skb);
946 	if (err)
947 		pr_err_ratelimited("Failed sending ack\n");
948 
949 err1:
950 	return err;
951 }
952 
953 static int send_atomic_ack(struct rxe_qp *qp, struct rxe_pkt_info *pkt,
954 			   u8 syndrome)
955 {
956 	int rc = 0;
957 	struct rxe_pkt_info ack_pkt;
958 	struct sk_buff *skb;
959 	struct resp_res *res;
960 
961 	skb = prepare_ack_packet(qp, pkt, &ack_pkt,
962 				 IB_OPCODE_RC_ATOMIC_ACKNOWLEDGE, 0, pkt->psn,
963 				 syndrome, NULL);
964 	if (!skb) {
965 		rc = -ENOMEM;
966 		goto out;
967 	}
968 
969 	rxe_add_ref(qp);
970 
971 	res = &qp->resp.resources[qp->resp.res_head];
972 	free_rd_atomic_resource(qp, res);
973 	rxe_advance_resp_resource(qp);
974 
975 	memcpy(SKB_TO_PKT(skb), &ack_pkt, sizeof(ack_pkt));
976 	memset((unsigned char *)SKB_TO_PKT(skb) + sizeof(ack_pkt), 0,
977 	       sizeof(skb->cb) - sizeof(ack_pkt));
978 
979 	skb_get(skb);
980 	res->type = RXE_ATOMIC_MASK;
981 	res->atomic.skb = skb;
982 	res->first_psn = ack_pkt.psn;
983 	res->last_psn  = ack_pkt.psn;
984 	res->cur_psn   = ack_pkt.psn;
985 
986 	rc = rxe_xmit_packet(qp, &ack_pkt, skb);
987 	if (rc) {
988 		pr_err_ratelimited("Failed sending ack\n");
989 		rxe_drop_ref(qp);
990 	}
991 out:
992 	return rc;
993 }
994 
995 static enum resp_states acknowledge(struct rxe_qp *qp,
996 				    struct rxe_pkt_info *pkt)
997 {
998 	if (qp_type(qp) != IB_QPT_RC)
999 		return RESPST_CLEANUP;
1000 
1001 	if (qp->resp.aeth_syndrome != AETH_ACK_UNLIMITED)
1002 		send_ack(qp, pkt, qp->resp.aeth_syndrome, pkt->psn);
1003 	else if (pkt->mask & RXE_ATOMIC_MASK)
1004 		send_atomic_ack(qp, pkt, AETH_ACK_UNLIMITED);
1005 	else if (bth_ack(pkt))
1006 		send_ack(qp, pkt, AETH_ACK_UNLIMITED, pkt->psn);
1007 
1008 	return RESPST_CLEANUP;
1009 }
1010 
1011 static enum resp_states cleanup(struct rxe_qp *qp,
1012 				struct rxe_pkt_info *pkt)
1013 {
1014 	struct sk_buff *skb;
1015 
1016 	if (pkt) {
1017 		skb = skb_dequeue(&qp->req_pkts);
1018 		rxe_drop_ref(qp);
1019 		kfree_skb(skb);
1020 		ib_device_put(qp->ibqp.device);
1021 	}
1022 
1023 	if (qp->resp.mr) {
1024 		rxe_drop_ref(qp->resp.mr);
1025 		qp->resp.mr = NULL;
1026 	}
1027 
1028 	return RESPST_DONE;
1029 }
1030 
1031 static struct resp_res *find_resource(struct rxe_qp *qp, u32 psn)
1032 {
1033 	int i;
1034 
1035 	for (i = 0; i < qp->attr.max_dest_rd_atomic; i++) {
1036 		struct resp_res *res = &qp->resp.resources[i];
1037 
1038 		if (res->type == 0)
1039 			continue;
1040 
1041 		if (psn_compare(psn, res->first_psn) >= 0 &&
1042 		    psn_compare(psn, res->last_psn) <= 0) {
1043 			return res;
1044 		}
1045 	}
1046 
1047 	return NULL;
1048 }
1049 
1050 static enum resp_states duplicate_request(struct rxe_qp *qp,
1051 					  struct rxe_pkt_info *pkt)
1052 {
1053 	enum resp_states rc;
1054 	u32 prev_psn = (qp->resp.ack_psn - 1) & BTH_PSN_MASK;
1055 
1056 	if (pkt->mask & RXE_SEND_MASK ||
1057 	    pkt->mask & RXE_WRITE_MASK) {
1058 		/* SEND. Ack again and cleanup. C9-105. */
1059 		if (bth_ack(pkt))
1060 			send_ack(qp, pkt, AETH_ACK_UNLIMITED, prev_psn);
1061 		rc = RESPST_CLEANUP;
1062 		goto out;
1063 	} else if (pkt->mask & RXE_READ_MASK) {
1064 		struct resp_res *res;
1065 
1066 		res = find_resource(qp, pkt->psn);
1067 		if (!res) {
1068 			/* Resource not found. Class D error.  Drop the
1069 			 * request.
1070 			 */
1071 			rc = RESPST_CLEANUP;
1072 			goto out;
1073 		} else {
1074 			/* Ensure this new request is the same as the previous
1075 			 * one or a subset of it.
1076 			 */
1077 			u64 iova = reth_va(pkt);
1078 			u32 resid = reth_len(pkt);
1079 
1080 			if (iova < res->read.va_org ||
1081 			    resid > res->read.length ||
1082 			    (iova + resid) > (res->read.va_org +
1083 					      res->read.length)) {
1084 				rc = RESPST_CLEANUP;
1085 				goto out;
1086 			}
1087 
1088 			if (reth_rkey(pkt) != res->read.rkey) {
1089 				rc = RESPST_CLEANUP;
1090 				goto out;
1091 			}
1092 
1093 			res->cur_psn = pkt->psn;
1094 			res->state = (pkt->psn == res->first_psn) ?
1095 					rdatm_res_state_new :
1096 					rdatm_res_state_replay;
1097 			res->replay = 1;
1098 
1099 			/* Reset the resource, except length. */
1100 			res->read.va_org = iova;
1101 			res->read.va = iova;
1102 			res->read.resid = resid;
1103 
1104 			/* Replay the RDMA read reply. */
1105 			qp->resp.res = res;
1106 			rc = RESPST_READ_REPLY;
1107 			goto out;
1108 		}
1109 	} else {
1110 		struct resp_res *res;
1111 
1112 		/* Find the operation in our list of responder resources. */
1113 		res = find_resource(qp, pkt->psn);
1114 		if (res) {
1115 			skb_get(res->atomic.skb);
1116 			/* Resend the result. */
1117 			rc = rxe_xmit_packet(qp, pkt, res->atomic.skb);
1118 			if (rc) {
1119 				pr_err("Failed resending result. This flow is not handled - skb ignored\n");
1120 				rc = RESPST_CLEANUP;
1121 				goto out;
1122 			}
1123 		}
1124 
1125 		/* Resource not found. Class D error. Drop the request. */
1126 		rc = RESPST_CLEANUP;
1127 		goto out;
1128 	}
1129 out:
1130 	return rc;
1131 }
1132 
1133 /* Process a class A or C. Both are treated the same in this implementation. */
1134 static void do_class_ac_error(struct rxe_qp *qp, u8 syndrome,
1135 			      enum ib_wc_status status)
1136 {
1137 	qp->resp.aeth_syndrome	= syndrome;
1138 	qp->resp.status		= status;
1139 
1140 	/* indicate that we should go through the ERROR state */
1141 	qp->resp.goto_error	= 1;
1142 }
1143 
1144 static enum resp_states do_class_d1e_error(struct rxe_qp *qp)
1145 {
1146 	/* UC */
1147 	if (qp->srq) {
1148 		/* Class E */
1149 		qp->resp.drop_msg = 1;
1150 		if (qp->resp.wqe) {
1151 			qp->resp.status = IB_WC_REM_INV_REQ_ERR;
1152 			return RESPST_COMPLETE;
1153 		} else {
1154 			return RESPST_CLEANUP;
1155 		}
1156 	} else {
1157 		/* Class D1. This packet may be the start of a
1158 		 * new message and could be valid. The previous
1159 		 * message is invalid and ignored. reset the
1160 		 * recv wr to its original state
1161 		 */
1162 		if (qp->resp.wqe) {
1163 			qp->resp.wqe->dma.resid = qp->resp.wqe->dma.length;
1164 			qp->resp.wqe->dma.cur_sge = 0;
1165 			qp->resp.wqe->dma.sge_offset = 0;
1166 			qp->resp.opcode = -1;
1167 		}
1168 
1169 		if (qp->resp.mr) {
1170 			rxe_drop_ref(qp->resp.mr);
1171 			qp->resp.mr = NULL;
1172 		}
1173 
1174 		return RESPST_CLEANUP;
1175 	}
1176 }
1177 
1178 static void rxe_drain_req_pkts(struct rxe_qp *qp, bool notify)
1179 {
1180 	struct sk_buff *skb;
1181 
1182 	while ((skb = skb_dequeue(&qp->req_pkts))) {
1183 		rxe_drop_ref(qp);
1184 		kfree_skb(skb);
1185 		ib_device_put(qp->ibqp.device);
1186 	}
1187 
1188 	if (notify)
1189 		return;
1190 
1191 	while (!qp->srq && qp->rq.queue && queue_head(qp->rq.queue))
1192 		advance_consumer(qp->rq.queue);
1193 }
1194 
1195 int rxe_responder(void *arg)
1196 {
1197 	struct rxe_qp *qp = (struct rxe_qp *)arg;
1198 	struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
1199 	enum resp_states state;
1200 	struct rxe_pkt_info *pkt = NULL;
1201 	int ret = 0;
1202 
1203 	rxe_add_ref(qp);
1204 
1205 	qp->resp.aeth_syndrome = AETH_ACK_UNLIMITED;
1206 
1207 	if (!qp->valid) {
1208 		ret = -EINVAL;
1209 		goto done;
1210 	}
1211 
1212 	switch (qp->resp.state) {
1213 	case QP_STATE_RESET:
1214 		state = RESPST_RESET;
1215 		break;
1216 
1217 	default:
1218 		state = RESPST_GET_REQ;
1219 		break;
1220 	}
1221 
1222 	while (1) {
1223 		pr_debug("qp#%d state = %s\n", qp_num(qp),
1224 			 resp_state_name[state]);
1225 		switch (state) {
1226 		case RESPST_GET_REQ:
1227 			state = get_req(qp, &pkt);
1228 			break;
1229 		case RESPST_CHK_PSN:
1230 			state = check_psn(qp, pkt);
1231 			break;
1232 		case RESPST_CHK_OP_SEQ:
1233 			state = check_op_seq(qp, pkt);
1234 			break;
1235 		case RESPST_CHK_OP_VALID:
1236 			state = check_op_valid(qp, pkt);
1237 			break;
1238 		case RESPST_CHK_RESOURCE:
1239 			state = check_resource(qp, pkt);
1240 			break;
1241 		case RESPST_CHK_LENGTH:
1242 			state = check_length(qp, pkt);
1243 			break;
1244 		case RESPST_CHK_RKEY:
1245 			state = check_rkey(qp, pkt);
1246 			break;
1247 		case RESPST_EXECUTE:
1248 			state = execute(qp, pkt);
1249 			break;
1250 		case RESPST_COMPLETE:
1251 			state = do_complete(qp, pkt);
1252 			break;
1253 		case RESPST_READ_REPLY:
1254 			state = read_reply(qp, pkt);
1255 			break;
1256 		case RESPST_ACKNOWLEDGE:
1257 			state = acknowledge(qp, pkt);
1258 			break;
1259 		case RESPST_CLEANUP:
1260 			state = cleanup(qp, pkt);
1261 			break;
1262 		case RESPST_DUPLICATE_REQUEST:
1263 			state = duplicate_request(qp, pkt);
1264 			break;
1265 		case RESPST_ERR_PSN_OUT_OF_SEQ:
1266 			/* RC only - Class B. Drop packet. */
1267 			send_ack(qp, pkt, AETH_NAK_PSN_SEQ_ERROR, qp->resp.psn);
1268 			state = RESPST_CLEANUP;
1269 			break;
1270 
1271 		case RESPST_ERR_TOO_MANY_RDMA_ATM_REQ:
1272 		case RESPST_ERR_MISSING_OPCODE_FIRST:
1273 		case RESPST_ERR_MISSING_OPCODE_LAST_C:
1274 		case RESPST_ERR_UNSUPPORTED_OPCODE:
1275 		case RESPST_ERR_MISALIGNED_ATOMIC:
1276 			/* RC Only - Class C. */
1277 			do_class_ac_error(qp, AETH_NAK_INVALID_REQ,
1278 					  IB_WC_REM_INV_REQ_ERR);
1279 			state = RESPST_COMPLETE;
1280 			break;
1281 
1282 		case RESPST_ERR_MISSING_OPCODE_LAST_D1E:
1283 			state = do_class_d1e_error(qp);
1284 			break;
1285 		case RESPST_ERR_RNR:
1286 			if (qp_type(qp) == IB_QPT_RC) {
1287 				rxe_counter_inc(rxe, RXE_CNT_SND_RNR);
1288 				/* RC - class B */
1289 				send_ack(qp, pkt, AETH_RNR_NAK |
1290 					 (~AETH_TYPE_MASK &
1291 					 qp->attr.min_rnr_timer),
1292 					 pkt->psn);
1293 			} else {
1294 				/* UD/UC - class D */
1295 				qp->resp.drop_msg = 1;
1296 			}
1297 			state = RESPST_CLEANUP;
1298 			break;
1299 
1300 		case RESPST_ERR_RKEY_VIOLATION:
1301 			if (qp_type(qp) == IB_QPT_RC) {
1302 				/* Class C */
1303 				do_class_ac_error(qp, AETH_NAK_REM_ACC_ERR,
1304 						  IB_WC_REM_ACCESS_ERR);
1305 				state = RESPST_COMPLETE;
1306 			} else {
1307 				qp->resp.drop_msg = 1;
1308 				if (qp->srq) {
1309 					/* UC/SRQ Class D */
1310 					qp->resp.status = IB_WC_REM_ACCESS_ERR;
1311 					state = RESPST_COMPLETE;
1312 				} else {
1313 					/* UC/non-SRQ Class E. */
1314 					state = RESPST_CLEANUP;
1315 				}
1316 			}
1317 			break;
1318 
1319 		case RESPST_ERR_LENGTH:
1320 			if (qp_type(qp) == IB_QPT_RC) {
1321 				/* Class C */
1322 				do_class_ac_error(qp, AETH_NAK_INVALID_REQ,
1323 						  IB_WC_REM_INV_REQ_ERR);
1324 				state = RESPST_COMPLETE;
1325 			} else if (qp->srq) {
1326 				/* UC/UD - class E */
1327 				qp->resp.status = IB_WC_REM_INV_REQ_ERR;
1328 				state = RESPST_COMPLETE;
1329 			} else {
1330 				/* UC/UD - class D */
1331 				qp->resp.drop_msg = 1;
1332 				state = RESPST_CLEANUP;
1333 			}
1334 			break;
1335 
1336 		case RESPST_ERR_MALFORMED_WQE:
1337 			/* All, Class A. */
1338 			do_class_ac_error(qp, AETH_NAK_REM_OP_ERR,
1339 					  IB_WC_LOC_QP_OP_ERR);
1340 			state = RESPST_COMPLETE;
1341 			break;
1342 
1343 		case RESPST_ERR_CQ_OVERFLOW:
1344 			/* All - Class G */
1345 			state = RESPST_ERROR;
1346 			break;
1347 
1348 		case RESPST_DONE:
1349 			if (qp->resp.goto_error) {
1350 				state = RESPST_ERROR;
1351 				break;
1352 			}
1353 
1354 			goto done;
1355 
1356 		case RESPST_EXIT:
1357 			if (qp->resp.goto_error) {
1358 				state = RESPST_ERROR;
1359 				break;
1360 			}
1361 
1362 			goto exit;
1363 
1364 		case RESPST_RESET:
1365 			rxe_drain_req_pkts(qp, false);
1366 			qp->resp.wqe = NULL;
1367 			goto exit;
1368 
1369 		case RESPST_ERROR:
1370 			qp->resp.goto_error = 0;
1371 			pr_warn("qp#%d moved to error state\n", qp_num(qp));
1372 			rxe_qp_error(qp);
1373 			goto exit;
1374 
1375 		default:
1376 			WARN_ON_ONCE(1);
1377 		}
1378 	}
1379 
1380 exit:
1381 	ret = -EAGAIN;
1382 done:
1383 	rxe_drop_ref(qp);
1384 	return ret;
1385 }
1386