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 	err = rxe_mr_copy(mr, res->read.va, payload_addr(&ack_pkt),
813 			  payload, RXE_FROM_MR_OBJ);
814 	if (err)
815 		pr_err("Failed copying memory\n");
816 	if (mr)
817 		rxe_put(mr);
818 
819 	if (bth_pad(&ack_pkt)) {
820 		u8 *pad = payload_addr(&ack_pkt) + payload;
821 
822 		memset(pad, 0, bth_pad(&ack_pkt));
823 	}
824 
825 	err = rxe_xmit_packet(qp, &ack_pkt, skb);
826 	if (err) {
827 		pr_err("Failed sending RDMA reply.\n");
828 		return RESPST_ERR_RNR;
829 	}
830 
831 	res->read.va += payload;
832 	res->read.resid -= payload;
833 	res->cur_psn = (res->cur_psn + 1) & BTH_PSN_MASK;
834 
835 	if (res->read.resid > 0) {
836 		state = RESPST_DONE;
837 	} else {
838 		qp->resp.res = NULL;
839 		if (!res->replay)
840 			qp->resp.opcode = -1;
841 		if (psn_compare(res->cur_psn, qp->resp.psn) >= 0)
842 			qp->resp.psn = res->cur_psn;
843 		state = RESPST_CLEANUP;
844 	}
845 
846 	return state;
847 }
848 
849 static int invalidate_rkey(struct rxe_qp *qp, u32 rkey)
850 {
851 	if (rkey_is_mw(rkey))
852 		return rxe_invalidate_mw(qp, rkey);
853 	else
854 		return rxe_invalidate_mr(qp, rkey);
855 }
856 
857 /* Executes a new request. A retried request never reach that function (send
858  * and writes are discarded, and reads and atomics are retried elsewhere.
859  */
860 static enum resp_states execute(struct rxe_qp *qp, struct rxe_pkt_info *pkt)
861 {
862 	enum resp_states err;
863 	struct sk_buff *skb = PKT_TO_SKB(pkt);
864 	union rdma_network_hdr hdr;
865 
866 	if (pkt->mask & RXE_SEND_MASK) {
867 		if (qp_type(qp) == IB_QPT_UD ||
868 		    qp_type(qp) == IB_QPT_GSI) {
869 			if (skb->protocol == htons(ETH_P_IP)) {
870 				memset(&hdr.reserved, 0,
871 						sizeof(hdr.reserved));
872 				memcpy(&hdr.roce4grh, ip_hdr(skb),
873 						sizeof(hdr.roce4grh));
874 				err = send_data_in(qp, &hdr, sizeof(hdr));
875 			} else {
876 				err = send_data_in(qp, ipv6_hdr(skb),
877 						sizeof(hdr));
878 			}
879 			if (err)
880 				return err;
881 		}
882 		err = send_data_in(qp, payload_addr(pkt), payload_size(pkt));
883 		if (err)
884 			return err;
885 	} else if (pkt->mask & RXE_WRITE_MASK) {
886 		err = write_data_in(qp, pkt);
887 		if (err)
888 			return err;
889 	} else if (pkt->mask & RXE_READ_MASK) {
890 		/* For RDMA Read we can increment the msn now. See C9-148. */
891 		qp->resp.msn++;
892 		return RESPST_READ_REPLY;
893 	} else if (pkt->mask & RXE_ATOMIC_MASK) {
894 		return RESPST_ATOMIC_REPLY;
895 	} else {
896 		/* Unreachable */
897 		WARN_ON_ONCE(1);
898 	}
899 
900 	if (pkt->mask & RXE_IETH_MASK) {
901 		u32 rkey = ieth_rkey(pkt);
902 
903 		err = invalidate_rkey(qp, rkey);
904 		if (err)
905 			return RESPST_ERR_INVALIDATE_RKEY;
906 	}
907 
908 	if (pkt->mask & RXE_END_MASK)
909 		/* We successfully processed this new request. */
910 		qp->resp.msn++;
911 
912 	/* next expected psn, read handles this separately */
913 	qp->resp.psn = (pkt->psn + 1) & BTH_PSN_MASK;
914 	qp->resp.ack_psn = qp->resp.psn;
915 
916 	qp->resp.opcode = pkt->opcode;
917 	qp->resp.status = IB_WC_SUCCESS;
918 
919 	if (pkt->mask & RXE_COMP_MASK)
920 		return RESPST_COMPLETE;
921 	else if (qp_type(qp) == IB_QPT_RC)
922 		return RESPST_ACKNOWLEDGE;
923 	else
924 		return RESPST_CLEANUP;
925 }
926 
927 static enum resp_states do_complete(struct rxe_qp *qp,
928 				    struct rxe_pkt_info *pkt)
929 {
930 	struct rxe_cqe cqe;
931 	struct ib_wc *wc = &cqe.ibwc;
932 	struct ib_uverbs_wc *uwc = &cqe.uibwc;
933 	struct rxe_recv_wqe *wqe = qp->resp.wqe;
934 	struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
935 
936 	if (!wqe)
937 		goto finish;
938 
939 	memset(&cqe, 0, sizeof(cqe));
940 
941 	if (qp->rcq->is_user) {
942 		uwc->status		= qp->resp.status;
943 		uwc->qp_num		= qp->ibqp.qp_num;
944 		uwc->wr_id		= wqe->wr_id;
945 	} else {
946 		wc->status		= qp->resp.status;
947 		wc->qp			= &qp->ibqp;
948 		wc->wr_id		= wqe->wr_id;
949 	}
950 
951 	if (wc->status == IB_WC_SUCCESS) {
952 		rxe_counter_inc(rxe, RXE_CNT_RDMA_RECV);
953 		wc->opcode = (pkt->mask & RXE_IMMDT_MASK &&
954 				pkt->mask & RXE_WRITE_MASK) ?
955 					IB_WC_RECV_RDMA_WITH_IMM : IB_WC_RECV;
956 		wc->byte_len = (pkt->mask & RXE_IMMDT_MASK &&
957 				pkt->mask & RXE_WRITE_MASK) ?
958 					qp->resp.length : wqe->dma.length - wqe->dma.resid;
959 
960 		/* fields after byte_len are different between kernel and user
961 		 * space
962 		 */
963 		if (qp->rcq->is_user) {
964 			uwc->wc_flags = IB_WC_GRH;
965 
966 			if (pkt->mask & RXE_IMMDT_MASK) {
967 				uwc->wc_flags |= IB_WC_WITH_IMM;
968 				uwc->ex.imm_data = immdt_imm(pkt);
969 			}
970 
971 			if (pkt->mask & RXE_IETH_MASK) {
972 				uwc->wc_flags |= IB_WC_WITH_INVALIDATE;
973 				uwc->ex.invalidate_rkey = ieth_rkey(pkt);
974 			}
975 
976 			if (pkt->mask & RXE_DETH_MASK)
977 				uwc->src_qp = deth_sqp(pkt);
978 
979 			uwc->port_num		= qp->attr.port_num;
980 		} else {
981 			struct sk_buff *skb = PKT_TO_SKB(pkt);
982 
983 			wc->wc_flags = IB_WC_GRH | IB_WC_WITH_NETWORK_HDR_TYPE;
984 			if (skb->protocol == htons(ETH_P_IP))
985 				wc->network_hdr_type = RDMA_NETWORK_IPV4;
986 			else
987 				wc->network_hdr_type = RDMA_NETWORK_IPV6;
988 
989 			if (is_vlan_dev(skb->dev)) {
990 				wc->wc_flags |= IB_WC_WITH_VLAN;
991 				wc->vlan_id = vlan_dev_vlan_id(skb->dev);
992 			}
993 
994 			if (pkt->mask & RXE_IMMDT_MASK) {
995 				wc->wc_flags |= IB_WC_WITH_IMM;
996 				wc->ex.imm_data = immdt_imm(pkt);
997 			}
998 
999 			if (pkt->mask & RXE_IETH_MASK) {
1000 				wc->wc_flags |= IB_WC_WITH_INVALIDATE;
1001 				wc->ex.invalidate_rkey = ieth_rkey(pkt);
1002 			}
1003 
1004 			if (pkt->mask & RXE_DETH_MASK)
1005 				wc->src_qp = deth_sqp(pkt);
1006 
1007 			wc->port_num		= qp->attr.port_num;
1008 		}
1009 	}
1010 
1011 	/* have copy for srq and reference for !srq */
1012 	if (!qp->srq)
1013 		queue_advance_consumer(qp->rq.queue, QUEUE_TYPE_FROM_CLIENT);
1014 
1015 	qp->resp.wqe = NULL;
1016 
1017 	if (rxe_cq_post(qp->rcq, &cqe, pkt ? bth_se(pkt) : 1))
1018 		return RESPST_ERR_CQ_OVERFLOW;
1019 
1020 finish:
1021 	if (unlikely(qp->resp.state == QP_STATE_ERROR))
1022 		return RESPST_CHK_RESOURCE;
1023 	if (unlikely(!pkt))
1024 		return RESPST_DONE;
1025 	if (qp_type(qp) == IB_QPT_RC)
1026 		return RESPST_ACKNOWLEDGE;
1027 	else
1028 		return RESPST_CLEANUP;
1029 }
1030 
1031 static int send_ack(struct rxe_qp *qp, u8 syndrome, u32 psn)
1032 {
1033 	int err = 0;
1034 	struct rxe_pkt_info ack_pkt;
1035 	struct sk_buff *skb;
1036 
1037 	skb = prepare_ack_packet(qp, &ack_pkt, IB_OPCODE_RC_ACKNOWLEDGE,
1038 				 0, psn, syndrome);
1039 	if (!skb) {
1040 		err = -ENOMEM;
1041 		goto err1;
1042 	}
1043 
1044 	err = rxe_xmit_packet(qp, &ack_pkt, skb);
1045 	if (err)
1046 		pr_err_ratelimited("Failed sending ack\n");
1047 
1048 err1:
1049 	return err;
1050 }
1051 
1052 static int send_atomic_ack(struct rxe_qp *qp, u8 syndrome, u32 psn)
1053 {
1054 	int err = 0;
1055 	struct rxe_pkt_info ack_pkt;
1056 	struct sk_buff *skb;
1057 
1058 	skb = prepare_ack_packet(qp, &ack_pkt, IB_OPCODE_RC_ATOMIC_ACKNOWLEDGE,
1059 				 0, psn, syndrome);
1060 	if (!skb) {
1061 		err = -ENOMEM;
1062 		goto out;
1063 	}
1064 
1065 	err = rxe_xmit_packet(qp, &ack_pkt, skb);
1066 	if (err)
1067 		pr_err_ratelimited("Failed sending atomic ack\n");
1068 
1069 	/* have to clear this since it is used to trigger
1070 	 * long read replies
1071 	 */
1072 	qp->resp.res = NULL;
1073 out:
1074 	return err;
1075 }
1076 
1077 static enum resp_states acknowledge(struct rxe_qp *qp,
1078 				    struct rxe_pkt_info *pkt)
1079 {
1080 	if (qp_type(qp) != IB_QPT_RC)
1081 		return RESPST_CLEANUP;
1082 
1083 	if (qp->resp.aeth_syndrome != AETH_ACK_UNLIMITED)
1084 		send_ack(qp, qp->resp.aeth_syndrome, pkt->psn);
1085 	else if (pkt->mask & RXE_ATOMIC_MASK)
1086 		send_atomic_ack(qp, AETH_ACK_UNLIMITED, pkt->psn);
1087 	else if (bth_ack(pkt))
1088 		send_ack(qp, AETH_ACK_UNLIMITED, pkt->psn);
1089 
1090 	return RESPST_CLEANUP;
1091 }
1092 
1093 static enum resp_states cleanup(struct rxe_qp *qp,
1094 				struct rxe_pkt_info *pkt)
1095 {
1096 	struct sk_buff *skb;
1097 
1098 	if (pkt) {
1099 		skb = skb_dequeue(&qp->req_pkts);
1100 		rxe_put(qp);
1101 		kfree_skb(skb);
1102 		ib_device_put(qp->ibqp.device);
1103 	}
1104 
1105 	if (qp->resp.mr) {
1106 		rxe_put(qp->resp.mr);
1107 		qp->resp.mr = NULL;
1108 	}
1109 
1110 	return RESPST_DONE;
1111 }
1112 
1113 static struct resp_res *find_resource(struct rxe_qp *qp, u32 psn)
1114 {
1115 	int i;
1116 
1117 	for (i = 0; i < qp->attr.max_dest_rd_atomic; i++) {
1118 		struct resp_res *res = &qp->resp.resources[i];
1119 
1120 		if (res->type == 0)
1121 			continue;
1122 
1123 		if (psn_compare(psn, res->first_psn) >= 0 &&
1124 		    psn_compare(psn, res->last_psn) <= 0) {
1125 			return res;
1126 		}
1127 	}
1128 
1129 	return NULL;
1130 }
1131 
1132 static enum resp_states duplicate_request(struct rxe_qp *qp,
1133 					  struct rxe_pkt_info *pkt)
1134 {
1135 	enum resp_states rc;
1136 	u32 prev_psn = (qp->resp.ack_psn - 1) & BTH_PSN_MASK;
1137 
1138 	if (pkt->mask & RXE_SEND_MASK ||
1139 	    pkt->mask & RXE_WRITE_MASK) {
1140 		/* SEND. Ack again and cleanup. C9-105. */
1141 		send_ack(qp, AETH_ACK_UNLIMITED, prev_psn);
1142 		return RESPST_CLEANUP;
1143 	} else if (pkt->mask & RXE_READ_MASK) {
1144 		struct resp_res *res;
1145 
1146 		res = find_resource(qp, pkt->psn);
1147 		if (!res) {
1148 			/* Resource not found. Class D error.  Drop the
1149 			 * request.
1150 			 */
1151 			rc = RESPST_CLEANUP;
1152 			goto out;
1153 		} else {
1154 			/* Ensure this new request is the same as the previous
1155 			 * one or a subset of it.
1156 			 */
1157 			u64 iova = reth_va(pkt);
1158 			u32 resid = reth_len(pkt);
1159 
1160 			if (iova < res->read.va_org ||
1161 			    resid > res->read.length ||
1162 			    (iova + resid) > (res->read.va_org +
1163 					      res->read.length)) {
1164 				rc = RESPST_CLEANUP;
1165 				goto out;
1166 			}
1167 
1168 			if (reth_rkey(pkt) != res->read.rkey) {
1169 				rc = RESPST_CLEANUP;
1170 				goto out;
1171 			}
1172 
1173 			res->cur_psn = pkt->psn;
1174 			res->state = (pkt->psn == res->first_psn) ?
1175 					rdatm_res_state_new :
1176 					rdatm_res_state_replay;
1177 			res->replay = 1;
1178 
1179 			/* Reset the resource, except length. */
1180 			res->read.va_org = iova;
1181 			res->read.va = iova;
1182 			res->read.resid = resid;
1183 
1184 			/* Replay the RDMA read reply. */
1185 			qp->resp.res = res;
1186 			rc = RESPST_READ_REPLY;
1187 			goto out;
1188 		}
1189 	} else {
1190 		struct resp_res *res;
1191 
1192 		/* Find the operation in our list of responder resources. */
1193 		res = find_resource(qp, pkt->psn);
1194 		if (res) {
1195 			res->replay = 1;
1196 			res->cur_psn = pkt->psn;
1197 			qp->resp.res = res;
1198 			rc = RESPST_ATOMIC_REPLY;
1199 			goto out;
1200 		}
1201 
1202 		/* Resource not found. Class D error. Drop the request. */
1203 		rc = RESPST_CLEANUP;
1204 		goto out;
1205 	}
1206 out:
1207 	return rc;
1208 }
1209 
1210 /* Process a class A or C. Both are treated the same in this implementation. */
1211 static void do_class_ac_error(struct rxe_qp *qp, u8 syndrome,
1212 			      enum ib_wc_status status)
1213 {
1214 	qp->resp.aeth_syndrome	= syndrome;
1215 	qp->resp.status		= status;
1216 
1217 	/* indicate that we should go through the ERROR state */
1218 	qp->resp.goto_error	= 1;
1219 }
1220 
1221 static enum resp_states do_class_d1e_error(struct rxe_qp *qp)
1222 {
1223 	/* UC */
1224 	if (qp->srq) {
1225 		/* Class E */
1226 		qp->resp.drop_msg = 1;
1227 		if (qp->resp.wqe) {
1228 			qp->resp.status = IB_WC_REM_INV_REQ_ERR;
1229 			return RESPST_COMPLETE;
1230 		} else {
1231 			return RESPST_CLEANUP;
1232 		}
1233 	} else {
1234 		/* Class D1. This packet may be the start of a
1235 		 * new message and could be valid. The previous
1236 		 * message is invalid and ignored. reset the
1237 		 * recv wr to its original state
1238 		 */
1239 		if (qp->resp.wqe) {
1240 			qp->resp.wqe->dma.resid = qp->resp.wqe->dma.length;
1241 			qp->resp.wqe->dma.cur_sge = 0;
1242 			qp->resp.wqe->dma.sge_offset = 0;
1243 			qp->resp.opcode = -1;
1244 		}
1245 
1246 		if (qp->resp.mr) {
1247 			rxe_put(qp->resp.mr);
1248 			qp->resp.mr = NULL;
1249 		}
1250 
1251 		return RESPST_CLEANUP;
1252 	}
1253 }
1254 
1255 static void rxe_drain_req_pkts(struct rxe_qp *qp, bool notify)
1256 {
1257 	struct sk_buff *skb;
1258 	struct rxe_queue *q = qp->rq.queue;
1259 
1260 	while ((skb = skb_dequeue(&qp->req_pkts))) {
1261 		rxe_put(qp);
1262 		kfree_skb(skb);
1263 		ib_device_put(qp->ibqp.device);
1264 	}
1265 
1266 	if (notify)
1267 		return;
1268 
1269 	while (!qp->srq && q && queue_head(q, q->type))
1270 		queue_advance_consumer(q, q->type);
1271 }
1272 
1273 int rxe_responder(void *arg)
1274 {
1275 	struct rxe_qp *qp = (struct rxe_qp *)arg;
1276 	struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
1277 	enum resp_states state;
1278 	struct rxe_pkt_info *pkt = NULL;
1279 	int ret;
1280 
1281 	if (!rxe_get(qp))
1282 		return -EAGAIN;
1283 
1284 	qp->resp.aeth_syndrome = AETH_ACK_UNLIMITED;
1285 
1286 	if (!qp->valid)
1287 		goto exit;
1288 
1289 	switch (qp->resp.state) {
1290 	case QP_STATE_RESET:
1291 		state = RESPST_RESET;
1292 		break;
1293 
1294 	default:
1295 		state = RESPST_GET_REQ;
1296 		break;
1297 	}
1298 
1299 	while (1) {
1300 		pr_debug("qp#%d state = %s\n", qp_num(qp),
1301 			 resp_state_name[state]);
1302 		switch (state) {
1303 		case RESPST_GET_REQ:
1304 			state = get_req(qp, &pkt);
1305 			break;
1306 		case RESPST_CHK_PSN:
1307 			state = check_psn(qp, pkt);
1308 			break;
1309 		case RESPST_CHK_OP_SEQ:
1310 			state = check_op_seq(qp, pkt);
1311 			break;
1312 		case RESPST_CHK_OP_VALID:
1313 			state = check_op_valid(qp, pkt);
1314 			break;
1315 		case RESPST_CHK_RESOURCE:
1316 			state = check_resource(qp, pkt);
1317 			break;
1318 		case RESPST_CHK_LENGTH:
1319 			state = check_length(qp, pkt);
1320 			break;
1321 		case RESPST_CHK_RKEY:
1322 			state = check_rkey(qp, pkt);
1323 			break;
1324 		case RESPST_EXECUTE:
1325 			state = execute(qp, pkt);
1326 			break;
1327 		case RESPST_COMPLETE:
1328 			state = do_complete(qp, pkt);
1329 			break;
1330 		case RESPST_READ_REPLY:
1331 			state = read_reply(qp, pkt);
1332 			break;
1333 		case RESPST_ATOMIC_REPLY:
1334 			state = atomic_reply(qp, pkt);
1335 			break;
1336 		case RESPST_ACKNOWLEDGE:
1337 			state = acknowledge(qp, pkt);
1338 			break;
1339 		case RESPST_CLEANUP:
1340 			state = cleanup(qp, pkt);
1341 			break;
1342 		case RESPST_DUPLICATE_REQUEST:
1343 			state = duplicate_request(qp, pkt);
1344 			break;
1345 		case RESPST_ERR_PSN_OUT_OF_SEQ:
1346 			/* RC only - Class B. Drop packet. */
1347 			send_ack(qp, AETH_NAK_PSN_SEQ_ERROR, qp->resp.psn);
1348 			state = RESPST_CLEANUP;
1349 			break;
1350 
1351 		case RESPST_ERR_TOO_MANY_RDMA_ATM_REQ:
1352 		case RESPST_ERR_MISSING_OPCODE_FIRST:
1353 		case RESPST_ERR_MISSING_OPCODE_LAST_C:
1354 		case RESPST_ERR_UNSUPPORTED_OPCODE:
1355 		case RESPST_ERR_MISALIGNED_ATOMIC:
1356 			/* RC Only - Class C. */
1357 			do_class_ac_error(qp, AETH_NAK_INVALID_REQ,
1358 					  IB_WC_REM_INV_REQ_ERR);
1359 			state = RESPST_COMPLETE;
1360 			break;
1361 
1362 		case RESPST_ERR_MISSING_OPCODE_LAST_D1E:
1363 			state = do_class_d1e_error(qp);
1364 			break;
1365 		case RESPST_ERR_RNR:
1366 			if (qp_type(qp) == IB_QPT_RC) {
1367 				rxe_counter_inc(rxe, RXE_CNT_SND_RNR);
1368 				/* RC - class B */
1369 				send_ack(qp, AETH_RNR_NAK |
1370 					 (~AETH_TYPE_MASK &
1371 					 qp->attr.min_rnr_timer),
1372 					 pkt->psn);
1373 			} else {
1374 				/* UD/UC - class D */
1375 				qp->resp.drop_msg = 1;
1376 			}
1377 			state = RESPST_CLEANUP;
1378 			break;
1379 
1380 		case RESPST_ERR_RKEY_VIOLATION:
1381 			if (qp_type(qp) == IB_QPT_RC) {
1382 				/* Class C */
1383 				do_class_ac_error(qp, AETH_NAK_REM_ACC_ERR,
1384 						  IB_WC_REM_ACCESS_ERR);
1385 				state = RESPST_COMPLETE;
1386 			} else {
1387 				qp->resp.drop_msg = 1;
1388 				if (qp->srq) {
1389 					/* UC/SRQ Class D */
1390 					qp->resp.status = IB_WC_REM_ACCESS_ERR;
1391 					state = RESPST_COMPLETE;
1392 				} else {
1393 					/* UC/non-SRQ Class E. */
1394 					state = RESPST_CLEANUP;
1395 				}
1396 			}
1397 			break;
1398 
1399 		case RESPST_ERR_INVALIDATE_RKEY:
1400 			/* RC - Class J. */
1401 			qp->resp.goto_error = 1;
1402 			qp->resp.status = IB_WC_REM_INV_REQ_ERR;
1403 			state = RESPST_COMPLETE;
1404 			break;
1405 
1406 		case RESPST_ERR_LENGTH:
1407 			if (qp_type(qp) == IB_QPT_RC) {
1408 				/* Class C */
1409 				do_class_ac_error(qp, AETH_NAK_INVALID_REQ,
1410 						  IB_WC_REM_INV_REQ_ERR);
1411 				state = RESPST_COMPLETE;
1412 			} else if (qp->srq) {
1413 				/* UC/UD - class E */
1414 				qp->resp.status = IB_WC_REM_INV_REQ_ERR;
1415 				state = RESPST_COMPLETE;
1416 			} else {
1417 				/* UC/UD - class D */
1418 				qp->resp.drop_msg = 1;
1419 				state = RESPST_CLEANUP;
1420 			}
1421 			break;
1422 
1423 		case RESPST_ERR_MALFORMED_WQE:
1424 			/* All, Class A. */
1425 			do_class_ac_error(qp, AETH_NAK_REM_OP_ERR,
1426 					  IB_WC_LOC_QP_OP_ERR);
1427 			state = RESPST_COMPLETE;
1428 			break;
1429 
1430 		case RESPST_ERR_CQ_OVERFLOW:
1431 			/* All - Class G */
1432 			state = RESPST_ERROR;
1433 			break;
1434 
1435 		case RESPST_DONE:
1436 			if (qp->resp.goto_error) {
1437 				state = RESPST_ERROR;
1438 				break;
1439 			}
1440 
1441 			goto done;
1442 
1443 		case RESPST_EXIT:
1444 			if (qp->resp.goto_error) {
1445 				state = RESPST_ERROR;
1446 				break;
1447 			}
1448 
1449 			goto exit;
1450 
1451 		case RESPST_RESET:
1452 			rxe_drain_req_pkts(qp, false);
1453 			qp->resp.wqe = NULL;
1454 			goto exit;
1455 
1456 		case RESPST_ERROR:
1457 			qp->resp.goto_error = 0;
1458 			pr_debug("qp#%d moved to error state\n", qp_num(qp));
1459 			rxe_qp_error(qp);
1460 			goto exit;
1461 
1462 		default:
1463 			WARN_ON_ONCE(1);
1464 		}
1465 	}
1466 
1467 	/* A non-zero return value will cause rxe_do_task to
1468 	 * exit its loop and end the tasklet. A zero return
1469 	 * will continue looping and return to rxe_responder
1470 	 */
1471 done:
1472 	ret = 0;
1473 	goto out;
1474 exit:
1475 	ret = -EAGAIN;
1476 out:
1477 	rxe_put(qp);
1478 	return ret;
1479 }
1480