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