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