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