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