xref: /linux/drivers/infiniband/sw/rxe/rxe_comp.c (revision 0e9ab8e4d44ae9d9aaf213bfd2c90bbe7289337b)
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 #include "rxe_task.h"
13 
14 enum comp_state {
15 	COMPST_GET_ACK,
16 	COMPST_GET_WQE,
17 	COMPST_COMP_WQE,
18 	COMPST_COMP_ACK,
19 	COMPST_CHECK_PSN,
20 	COMPST_CHECK_ACK,
21 	COMPST_READ,
22 	COMPST_ATOMIC,
23 	COMPST_WRITE_SEND,
24 	COMPST_UPDATE_COMP,
25 	COMPST_ERROR_RETRY,
26 	COMPST_RNR_RETRY,
27 	COMPST_ERROR,
28 	COMPST_EXIT, /* We have an issue, and we want to rerun the completer */
29 	COMPST_DONE, /* The completer finished successflly */
30 };
31 
32 static char *comp_state_name[] =  {
33 	[COMPST_GET_ACK]		= "GET ACK",
34 	[COMPST_GET_WQE]		= "GET WQE",
35 	[COMPST_COMP_WQE]		= "COMP WQE",
36 	[COMPST_COMP_ACK]		= "COMP ACK",
37 	[COMPST_CHECK_PSN]		= "CHECK PSN",
38 	[COMPST_CHECK_ACK]		= "CHECK ACK",
39 	[COMPST_READ]			= "READ",
40 	[COMPST_ATOMIC]			= "ATOMIC",
41 	[COMPST_WRITE_SEND]		= "WRITE/SEND",
42 	[COMPST_UPDATE_COMP]		= "UPDATE COMP",
43 	[COMPST_ERROR_RETRY]		= "ERROR RETRY",
44 	[COMPST_RNR_RETRY]		= "RNR RETRY",
45 	[COMPST_ERROR]			= "ERROR",
46 	[COMPST_EXIT]			= "EXIT",
47 	[COMPST_DONE]			= "DONE",
48 };
49 
50 static unsigned long rnrnak_usec[32] = {
51 	[IB_RNR_TIMER_655_36] = 655360,
52 	[IB_RNR_TIMER_000_01] = 10,
53 	[IB_RNR_TIMER_000_02] = 20,
54 	[IB_RNR_TIMER_000_03] = 30,
55 	[IB_RNR_TIMER_000_04] = 40,
56 	[IB_RNR_TIMER_000_06] = 60,
57 	[IB_RNR_TIMER_000_08] = 80,
58 	[IB_RNR_TIMER_000_12] = 120,
59 	[IB_RNR_TIMER_000_16] = 160,
60 	[IB_RNR_TIMER_000_24] = 240,
61 	[IB_RNR_TIMER_000_32] = 320,
62 	[IB_RNR_TIMER_000_48] = 480,
63 	[IB_RNR_TIMER_000_64] = 640,
64 	[IB_RNR_TIMER_000_96] = 960,
65 	[IB_RNR_TIMER_001_28] = 1280,
66 	[IB_RNR_TIMER_001_92] = 1920,
67 	[IB_RNR_TIMER_002_56] = 2560,
68 	[IB_RNR_TIMER_003_84] = 3840,
69 	[IB_RNR_TIMER_005_12] = 5120,
70 	[IB_RNR_TIMER_007_68] = 7680,
71 	[IB_RNR_TIMER_010_24] = 10240,
72 	[IB_RNR_TIMER_015_36] = 15360,
73 	[IB_RNR_TIMER_020_48] = 20480,
74 	[IB_RNR_TIMER_030_72] = 30720,
75 	[IB_RNR_TIMER_040_96] = 40960,
76 	[IB_RNR_TIMER_061_44] = 61410,
77 	[IB_RNR_TIMER_081_92] = 81920,
78 	[IB_RNR_TIMER_122_88] = 122880,
79 	[IB_RNR_TIMER_163_84] = 163840,
80 	[IB_RNR_TIMER_245_76] = 245760,
81 	[IB_RNR_TIMER_327_68] = 327680,
82 	[IB_RNR_TIMER_491_52] = 491520,
83 };
84 
85 static inline unsigned long rnrnak_jiffies(u8 timeout)
86 {
87 	return max_t(unsigned long,
88 		usecs_to_jiffies(rnrnak_usec[timeout]), 1);
89 }
90 
91 static enum ib_wc_opcode wr_to_wc_opcode(enum ib_wr_opcode opcode)
92 {
93 	switch (opcode) {
94 	case IB_WR_RDMA_WRITE:			return IB_WC_RDMA_WRITE;
95 	case IB_WR_RDMA_WRITE_WITH_IMM:		return IB_WC_RDMA_WRITE;
96 	case IB_WR_SEND:			return IB_WC_SEND;
97 	case IB_WR_SEND_WITH_IMM:		return IB_WC_SEND;
98 	case IB_WR_RDMA_READ:			return IB_WC_RDMA_READ;
99 	case IB_WR_ATOMIC_CMP_AND_SWP:		return IB_WC_COMP_SWAP;
100 	case IB_WR_ATOMIC_FETCH_AND_ADD:	return IB_WC_FETCH_ADD;
101 	case IB_WR_LSO:				return IB_WC_LSO;
102 	case IB_WR_SEND_WITH_INV:		return IB_WC_SEND;
103 	case IB_WR_RDMA_READ_WITH_INV:		return IB_WC_RDMA_READ;
104 	case IB_WR_LOCAL_INV:			return IB_WC_LOCAL_INV;
105 	case IB_WR_REG_MR:			return IB_WC_REG_MR;
106 	case IB_WR_BIND_MW:			return IB_WC_BIND_MW;
107 	case IB_WR_ATOMIC_WRITE:		return IB_WC_ATOMIC_WRITE;
108 	case IB_WR_FLUSH:			return IB_WC_FLUSH;
109 
110 	default:
111 		return 0xff;
112 	}
113 }
114 
115 void retransmit_timer(struct timer_list *t)
116 {
117 	struct rxe_qp *qp = from_timer(qp, t, retrans_timer);
118 
119 	rxe_dbg_qp(qp, "retransmit timer fired\n");
120 
121 	if (qp->valid) {
122 		qp->comp.timeout = 1;
123 		rxe_sched_task(&qp->comp.task);
124 	}
125 }
126 
127 void rxe_comp_queue_pkt(struct rxe_qp *qp, struct sk_buff *skb)
128 {
129 	int must_sched;
130 
131 	skb_queue_tail(&qp->resp_pkts, skb);
132 
133 	must_sched = skb_queue_len(&qp->resp_pkts) > 1;
134 	if (must_sched != 0)
135 		rxe_counter_inc(SKB_TO_PKT(skb)->rxe, RXE_CNT_COMPLETER_SCHED);
136 
137 	if (must_sched)
138 		rxe_sched_task(&qp->comp.task);
139 	else
140 		rxe_run_task(&qp->comp.task);
141 }
142 
143 static inline enum comp_state get_wqe(struct rxe_qp *qp,
144 				      struct rxe_pkt_info *pkt,
145 				      struct rxe_send_wqe **wqe_p)
146 {
147 	struct rxe_send_wqe *wqe;
148 
149 	/* we come here whether or not we found a response packet to see if
150 	 * there are any posted WQEs
151 	 */
152 	wqe = queue_head(qp->sq.queue, QUEUE_TYPE_FROM_CLIENT);
153 	*wqe_p = wqe;
154 
155 	/* no WQE or requester has not started it yet */
156 	if (!wqe || wqe->state == wqe_state_posted)
157 		return pkt ? COMPST_DONE : COMPST_EXIT;
158 
159 	/* WQE does not require an ack */
160 	if (wqe->state == wqe_state_done)
161 		return COMPST_COMP_WQE;
162 
163 	/* WQE caused an error */
164 	if (wqe->state == wqe_state_error)
165 		return COMPST_ERROR;
166 
167 	/* we have a WQE, if we also have an ack check its PSN */
168 	return pkt ? COMPST_CHECK_PSN : COMPST_EXIT;
169 }
170 
171 static inline void reset_retry_counters(struct rxe_qp *qp)
172 {
173 	qp->comp.retry_cnt = qp->attr.retry_cnt;
174 	qp->comp.rnr_retry = qp->attr.rnr_retry;
175 	qp->comp.started_retry = 0;
176 }
177 
178 static inline enum comp_state check_psn(struct rxe_qp *qp,
179 					struct rxe_pkt_info *pkt,
180 					struct rxe_send_wqe *wqe)
181 {
182 	s32 diff;
183 
184 	/* check to see if response is past the oldest WQE. if it is, complete
185 	 * send/write or error read/atomic
186 	 */
187 	diff = psn_compare(pkt->psn, wqe->last_psn);
188 	if (diff > 0) {
189 		if (wqe->state == wqe_state_pending) {
190 			if (wqe->mask & WR_ATOMIC_OR_READ_MASK)
191 				return COMPST_ERROR_RETRY;
192 
193 			reset_retry_counters(qp);
194 			return COMPST_COMP_WQE;
195 		} else {
196 			return COMPST_DONE;
197 		}
198 	}
199 
200 	/* compare response packet to expected response */
201 	diff = psn_compare(pkt->psn, qp->comp.psn);
202 	if (diff < 0) {
203 		/* response is most likely a retried packet if it matches an
204 		 * uncompleted WQE go complete it else ignore it
205 		 */
206 		if (pkt->psn == wqe->last_psn)
207 			return COMPST_COMP_ACK;
208 		else if (pkt->opcode == IB_OPCODE_RC_ACKNOWLEDGE &&
209 			 (qp->comp.opcode == IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST ||
210 			  qp->comp.opcode == IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE))
211 			return COMPST_CHECK_ACK;
212 		else
213 			return COMPST_DONE;
214 	} else if ((diff > 0) && (wqe->mask & WR_ATOMIC_OR_READ_MASK)) {
215 		return COMPST_DONE;
216 	} else {
217 		return COMPST_CHECK_ACK;
218 	}
219 }
220 
221 static inline enum comp_state check_ack(struct rxe_qp *qp,
222 					struct rxe_pkt_info *pkt,
223 					struct rxe_send_wqe *wqe)
224 {
225 	unsigned int mask = pkt->mask;
226 	u8 syn;
227 	struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
228 
229 	/* Check the sequence only */
230 	switch (qp->comp.opcode) {
231 	case -1:
232 		/* Will catch all *_ONLY cases. */
233 		if (!(mask & RXE_START_MASK))
234 			return COMPST_ERROR;
235 
236 		break;
237 
238 	case IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST:
239 	case IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE:
240 		/* Check NAK code to handle a remote error */
241 		if (pkt->opcode == IB_OPCODE_RC_ACKNOWLEDGE)
242 			break;
243 
244 		if (pkt->opcode != IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE &&
245 		    pkt->opcode != IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST) {
246 			/* read retries of partial data may restart from
247 			 * read response first or response only.
248 			 */
249 			if ((pkt->psn == wqe->first_psn &&
250 			     pkt->opcode ==
251 			     IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST) ||
252 			    (wqe->first_psn == wqe->last_psn &&
253 			     pkt->opcode ==
254 			     IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY))
255 				break;
256 
257 			return COMPST_ERROR;
258 		}
259 		break;
260 	default:
261 		WARN_ON_ONCE(1);
262 	}
263 
264 	/* Check operation validity. */
265 	switch (pkt->opcode) {
266 	case IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST:
267 	case IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST:
268 	case IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY:
269 		syn = aeth_syn(pkt);
270 
271 		if ((syn & AETH_TYPE_MASK) != AETH_ACK)
272 			return COMPST_ERROR;
273 
274 		if (wqe->wr.opcode == IB_WR_ATOMIC_WRITE)
275 			return COMPST_WRITE_SEND;
276 
277 		fallthrough;
278 		/* (IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE doesn't have an AETH)
279 		 */
280 	case IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE:
281 		if (wqe->wr.opcode != IB_WR_RDMA_READ &&
282 		    wqe->wr.opcode != IB_WR_RDMA_READ_WITH_INV &&
283 		    wqe->wr.opcode != IB_WR_FLUSH) {
284 			wqe->status = IB_WC_FATAL_ERR;
285 			return COMPST_ERROR;
286 		}
287 		reset_retry_counters(qp);
288 		return COMPST_READ;
289 
290 	case IB_OPCODE_RC_ATOMIC_ACKNOWLEDGE:
291 		syn = aeth_syn(pkt);
292 
293 		if ((syn & AETH_TYPE_MASK) != AETH_ACK)
294 			return COMPST_ERROR;
295 
296 		if (wqe->wr.opcode != IB_WR_ATOMIC_CMP_AND_SWP &&
297 		    wqe->wr.opcode != IB_WR_ATOMIC_FETCH_AND_ADD)
298 			return COMPST_ERROR;
299 		reset_retry_counters(qp);
300 		return COMPST_ATOMIC;
301 
302 	case IB_OPCODE_RC_ACKNOWLEDGE:
303 		syn = aeth_syn(pkt);
304 		switch (syn & AETH_TYPE_MASK) {
305 		case AETH_ACK:
306 			reset_retry_counters(qp);
307 			return COMPST_WRITE_SEND;
308 
309 		case AETH_RNR_NAK:
310 			rxe_counter_inc(rxe, RXE_CNT_RCV_RNR);
311 			return COMPST_RNR_RETRY;
312 
313 		case AETH_NAK:
314 			switch (syn) {
315 			case AETH_NAK_PSN_SEQ_ERROR:
316 				/* a nak implicitly acks all packets with psns
317 				 * before
318 				 */
319 				if (psn_compare(pkt->psn, qp->comp.psn) > 0) {
320 					rxe_counter_inc(rxe,
321 							RXE_CNT_RCV_SEQ_ERR);
322 					qp->comp.psn = pkt->psn;
323 					if (qp->req.wait_psn) {
324 						qp->req.wait_psn = 0;
325 						rxe_run_task(&qp->req.task);
326 					}
327 				}
328 				return COMPST_ERROR_RETRY;
329 
330 			case AETH_NAK_INVALID_REQ:
331 				wqe->status = IB_WC_REM_INV_REQ_ERR;
332 				return COMPST_ERROR;
333 
334 			case AETH_NAK_REM_ACC_ERR:
335 				wqe->status = IB_WC_REM_ACCESS_ERR;
336 				return COMPST_ERROR;
337 
338 			case AETH_NAK_REM_OP_ERR:
339 				wqe->status = IB_WC_REM_OP_ERR;
340 				return COMPST_ERROR;
341 
342 			default:
343 				rxe_dbg_qp(qp, "unexpected nak %x\n", syn);
344 				wqe->status = IB_WC_REM_OP_ERR;
345 				return COMPST_ERROR;
346 			}
347 
348 		default:
349 			return COMPST_ERROR;
350 		}
351 		break;
352 
353 	default:
354 		rxe_dbg_qp(qp, "unexpected opcode\n");
355 	}
356 
357 	return COMPST_ERROR;
358 }
359 
360 static inline enum comp_state do_read(struct rxe_qp *qp,
361 				      struct rxe_pkt_info *pkt,
362 				      struct rxe_send_wqe *wqe)
363 {
364 	int ret;
365 
366 	ret = copy_data(qp->pd, IB_ACCESS_LOCAL_WRITE,
367 			&wqe->dma, payload_addr(pkt),
368 			payload_size(pkt), RXE_TO_MR_OBJ);
369 	if (ret) {
370 		wqe->status = IB_WC_LOC_PROT_ERR;
371 		return COMPST_ERROR;
372 	}
373 
374 	if (wqe->dma.resid == 0 && (pkt->mask & RXE_END_MASK))
375 		return COMPST_COMP_ACK;
376 
377 	return COMPST_UPDATE_COMP;
378 }
379 
380 static inline enum comp_state do_atomic(struct rxe_qp *qp,
381 					struct rxe_pkt_info *pkt,
382 					struct rxe_send_wqe *wqe)
383 {
384 	int ret;
385 
386 	u64 atomic_orig = atmack_orig(pkt);
387 
388 	ret = copy_data(qp->pd, IB_ACCESS_LOCAL_WRITE,
389 			&wqe->dma, &atomic_orig,
390 			sizeof(u64), RXE_TO_MR_OBJ);
391 	if (ret) {
392 		wqe->status = IB_WC_LOC_PROT_ERR;
393 		return COMPST_ERROR;
394 	}
395 
396 	return COMPST_COMP_ACK;
397 }
398 
399 static void make_send_cqe(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
400 			  struct rxe_cqe *cqe)
401 {
402 	struct ib_wc *wc = &cqe->ibwc;
403 	struct ib_uverbs_wc *uwc = &cqe->uibwc;
404 
405 	memset(cqe, 0, sizeof(*cqe));
406 
407 	if (!qp->is_user) {
408 		wc->wr_id = wqe->wr.wr_id;
409 		wc->status = wqe->status;
410 		wc->qp = &qp->ibqp;
411 	} else {
412 		uwc->wr_id = wqe->wr.wr_id;
413 		uwc->status = wqe->status;
414 		uwc->qp_num = qp->ibqp.qp_num;
415 	}
416 
417 	if (wqe->status == IB_WC_SUCCESS) {
418 		if (!qp->is_user) {
419 			wc->opcode = wr_to_wc_opcode(wqe->wr.opcode);
420 			if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
421 			    wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
422 				wc->wc_flags = IB_WC_WITH_IMM;
423 			wc->byte_len = wqe->dma.length;
424 		} else {
425 			uwc->opcode = wr_to_wc_opcode(wqe->wr.opcode);
426 			if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
427 			    wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
428 				uwc->wc_flags = IB_WC_WITH_IMM;
429 			uwc->byte_len = wqe->dma.length;
430 		}
431 	}
432 }
433 
434 /*
435  * IBA Spec. Section 10.7.3.1 SIGNALED COMPLETIONS
436  * ---------8<---------8<-------------
437  * ...Note that if a completion error occurs, a Work Completion
438  * will always be generated, even if the signaling
439  * indicator requests an Unsignaled Completion.
440  * ---------8<---------8<-------------
441  */
442 static void do_complete(struct rxe_qp *qp, struct rxe_send_wqe *wqe)
443 {
444 	struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
445 	struct rxe_cqe cqe;
446 	bool post;
447 
448 	/* do we need to post a completion */
449 	post = ((qp->sq_sig_type == IB_SIGNAL_ALL_WR) ||
450 			(wqe->wr.send_flags & IB_SEND_SIGNALED) ||
451 			wqe->status != IB_WC_SUCCESS);
452 
453 	if (post)
454 		make_send_cqe(qp, wqe, &cqe);
455 
456 	queue_advance_consumer(qp->sq.queue, QUEUE_TYPE_FROM_CLIENT);
457 
458 	if (post)
459 		rxe_cq_post(qp->scq, &cqe, 0);
460 
461 	if (wqe->wr.opcode == IB_WR_SEND ||
462 	    wqe->wr.opcode == IB_WR_SEND_WITH_IMM ||
463 	    wqe->wr.opcode == IB_WR_SEND_WITH_INV)
464 		rxe_counter_inc(rxe, RXE_CNT_RDMA_SEND);
465 
466 	/*
467 	 * we completed something so let req run again
468 	 * if it is trying to fence
469 	 */
470 	if (qp->req.wait_fence) {
471 		qp->req.wait_fence = 0;
472 		rxe_run_task(&qp->req.task);
473 	}
474 }
475 
476 static inline enum comp_state complete_ack(struct rxe_qp *qp,
477 					   struct rxe_pkt_info *pkt,
478 					   struct rxe_send_wqe *wqe)
479 {
480 	if (wqe->has_rd_atomic) {
481 		wqe->has_rd_atomic = 0;
482 		atomic_inc(&qp->req.rd_atomic);
483 		if (qp->req.need_rd_atomic) {
484 			qp->comp.timeout_retry = 0;
485 			qp->req.need_rd_atomic = 0;
486 			rxe_run_task(&qp->req.task);
487 		}
488 	}
489 
490 	if (unlikely(qp->req.state == QP_STATE_DRAIN)) {
491 		/* state_lock used by requester & completer */
492 		spin_lock_bh(&qp->state_lock);
493 		if ((qp->req.state == QP_STATE_DRAIN) &&
494 		    (qp->comp.psn == qp->req.psn)) {
495 			qp->req.state = QP_STATE_DRAINED;
496 			spin_unlock_bh(&qp->state_lock);
497 
498 			if (qp->ibqp.event_handler) {
499 				struct ib_event ev;
500 
501 				ev.device = qp->ibqp.device;
502 				ev.element.qp = &qp->ibqp;
503 				ev.event = IB_EVENT_SQ_DRAINED;
504 				qp->ibqp.event_handler(&ev,
505 					qp->ibqp.qp_context);
506 			}
507 		} else {
508 			spin_unlock_bh(&qp->state_lock);
509 		}
510 	}
511 
512 	do_complete(qp, wqe);
513 
514 	if (psn_compare(pkt->psn, qp->comp.psn) >= 0)
515 		return COMPST_UPDATE_COMP;
516 	else
517 		return COMPST_DONE;
518 }
519 
520 static inline enum comp_state complete_wqe(struct rxe_qp *qp,
521 					   struct rxe_pkt_info *pkt,
522 					   struct rxe_send_wqe *wqe)
523 {
524 	if (pkt && wqe->state == wqe_state_pending) {
525 		if (psn_compare(wqe->last_psn, qp->comp.psn) >= 0) {
526 			qp->comp.psn = (wqe->last_psn + 1) & BTH_PSN_MASK;
527 			qp->comp.opcode = -1;
528 		}
529 
530 		if (qp->req.wait_psn) {
531 			qp->req.wait_psn = 0;
532 			rxe_sched_task(&qp->req.task);
533 		}
534 	}
535 
536 	do_complete(qp, wqe);
537 
538 	return COMPST_GET_WQE;
539 }
540 
541 static void rxe_drain_resp_pkts(struct rxe_qp *qp, bool notify)
542 {
543 	struct sk_buff *skb;
544 	struct rxe_send_wqe *wqe;
545 	struct rxe_queue *q = qp->sq.queue;
546 
547 	while ((skb = skb_dequeue(&qp->resp_pkts))) {
548 		rxe_put(qp);
549 		kfree_skb(skb);
550 		ib_device_put(qp->ibqp.device);
551 	}
552 
553 	while ((wqe = queue_head(q, q->type))) {
554 		if (notify) {
555 			wqe->status = IB_WC_WR_FLUSH_ERR;
556 			do_complete(qp, wqe);
557 		} else {
558 			queue_advance_consumer(q, q->type);
559 		}
560 	}
561 }
562 
563 static void free_pkt(struct rxe_pkt_info *pkt)
564 {
565 	struct sk_buff *skb = PKT_TO_SKB(pkt);
566 	struct rxe_qp *qp = pkt->qp;
567 	struct ib_device *dev = qp->ibqp.device;
568 
569 	kfree_skb(skb);
570 	rxe_put(qp);
571 	ib_device_put(dev);
572 }
573 
574 int rxe_completer(void *arg)
575 {
576 	struct rxe_qp *qp = (struct rxe_qp *)arg;
577 	struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
578 	struct rxe_send_wqe *wqe = NULL;
579 	struct sk_buff *skb = NULL;
580 	struct rxe_pkt_info *pkt = NULL;
581 	enum comp_state state;
582 	int ret;
583 
584 	if (!rxe_get(qp))
585 		return -EAGAIN;
586 
587 	if (!qp->valid || qp->comp.state == QP_STATE_ERROR ||
588 	    qp->comp.state == QP_STATE_RESET) {
589 		rxe_drain_resp_pkts(qp, qp->valid &&
590 				    qp->comp.state == QP_STATE_ERROR);
591 		goto exit;
592 	}
593 
594 	if (qp->comp.timeout) {
595 		qp->comp.timeout_retry = 1;
596 		qp->comp.timeout = 0;
597 	} else {
598 		qp->comp.timeout_retry = 0;
599 	}
600 
601 	if (qp->req.need_retry)
602 		goto exit;
603 
604 	state = COMPST_GET_ACK;
605 
606 	while (1) {
607 		rxe_dbg_qp(qp, "state = %s\n", comp_state_name[state]);
608 		switch (state) {
609 		case COMPST_GET_ACK:
610 			skb = skb_dequeue(&qp->resp_pkts);
611 			if (skb) {
612 				pkt = SKB_TO_PKT(skb);
613 				qp->comp.timeout_retry = 0;
614 			}
615 			state = COMPST_GET_WQE;
616 			break;
617 
618 		case COMPST_GET_WQE:
619 			state = get_wqe(qp, pkt, &wqe);
620 			break;
621 
622 		case COMPST_CHECK_PSN:
623 			state = check_psn(qp, pkt, wqe);
624 			break;
625 
626 		case COMPST_CHECK_ACK:
627 			state = check_ack(qp, pkt, wqe);
628 			break;
629 
630 		case COMPST_READ:
631 			state = do_read(qp, pkt, wqe);
632 			break;
633 
634 		case COMPST_ATOMIC:
635 			state = do_atomic(qp, pkt, wqe);
636 			break;
637 
638 		case COMPST_WRITE_SEND:
639 			if (wqe->state == wqe_state_pending &&
640 			    wqe->last_psn == pkt->psn)
641 				state = COMPST_COMP_ACK;
642 			else
643 				state = COMPST_UPDATE_COMP;
644 			break;
645 
646 		case COMPST_COMP_ACK:
647 			state = complete_ack(qp, pkt, wqe);
648 			break;
649 
650 		case COMPST_COMP_WQE:
651 			state = complete_wqe(qp, pkt, wqe);
652 			break;
653 
654 		case COMPST_UPDATE_COMP:
655 			if (pkt->mask & RXE_END_MASK)
656 				qp->comp.opcode = -1;
657 			else
658 				qp->comp.opcode = pkt->opcode;
659 
660 			if (psn_compare(pkt->psn, qp->comp.psn) >= 0)
661 				qp->comp.psn = (pkt->psn + 1) & BTH_PSN_MASK;
662 
663 			if (qp->req.wait_psn) {
664 				qp->req.wait_psn = 0;
665 				rxe_sched_task(&qp->req.task);
666 			}
667 
668 			state = COMPST_DONE;
669 			break;
670 
671 		case COMPST_DONE:
672 			goto done;
673 
674 		case COMPST_EXIT:
675 			if (qp->comp.timeout_retry && wqe) {
676 				state = COMPST_ERROR_RETRY;
677 				break;
678 			}
679 
680 			/* re reset the timeout counter if
681 			 * (1) QP is type RC
682 			 * (2) the QP is alive
683 			 * (3) there is a packet sent by the requester that
684 			 *     might be acked (we still might get spurious
685 			 *     timeouts but try to keep them as few as possible)
686 			 * (4) the timeout parameter is set
687 			 */
688 			if ((qp_type(qp) == IB_QPT_RC) &&
689 			    (qp->req.state == QP_STATE_READY) &&
690 			    (psn_compare(qp->req.psn, qp->comp.psn) > 0) &&
691 			    qp->qp_timeout_jiffies)
692 				mod_timer(&qp->retrans_timer,
693 					  jiffies + qp->qp_timeout_jiffies);
694 			goto exit;
695 
696 		case COMPST_ERROR_RETRY:
697 			/* we come here if the retry timer fired and we did
698 			 * not receive a response packet. try to retry the send
699 			 * queue if that makes sense and the limits have not
700 			 * been exceeded. remember that some timeouts are
701 			 * spurious since we do not reset the timer but kick
702 			 * it down the road or let it expire
703 			 */
704 
705 			/* there is nothing to retry in this case */
706 			if (!wqe || (wqe->state == wqe_state_posted))
707 				goto exit;
708 
709 			/* if we've started a retry, don't start another
710 			 * retry sequence, unless this is a timeout.
711 			 */
712 			if (qp->comp.started_retry &&
713 			    !qp->comp.timeout_retry)
714 				goto done;
715 
716 			if (qp->comp.retry_cnt > 0) {
717 				if (qp->comp.retry_cnt != 7)
718 					qp->comp.retry_cnt--;
719 
720 				/* no point in retrying if we have already
721 				 * seen the last ack that the requester could
722 				 * have caused
723 				 */
724 				if (psn_compare(qp->req.psn,
725 						qp->comp.psn) > 0) {
726 					/* tell the requester to retry the
727 					 * send queue next time around
728 					 */
729 					rxe_counter_inc(rxe,
730 							RXE_CNT_COMP_RETRY);
731 					qp->req.need_retry = 1;
732 					qp->comp.started_retry = 1;
733 					rxe_run_task(&qp->req.task);
734 				}
735 				goto done;
736 
737 			} else {
738 				rxe_counter_inc(rxe, RXE_CNT_RETRY_EXCEEDED);
739 				wqe->status = IB_WC_RETRY_EXC_ERR;
740 				state = COMPST_ERROR;
741 			}
742 			break;
743 
744 		case COMPST_RNR_RETRY:
745 			/* we come here if we received an RNR NAK */
746 			if (qp->comp.rnr_retry > 0) {
747 				if (qp->comp.rnr_retry != 7)
748 					qp->comp.rnr_retry--;
749 
750 				/* don't start a retry flow until the
751 				 * rnr timer has fired
752 				 */
753 				qp->req.wait_for_rnr_timer = 1;
754 				rxe_dbg_qp(qp, "set rnr nak timer\n");
755 				mod_timer(&qp->rnr_nak_timer,
756 					  jiffies + rnrnak_jiffies(aeth_syn(pkt)
757 						& ~AETH_TYPE_MASK));
758 				goto exit;
759 			} else {
760 				rxe_counter_inc(rxe,
761 						RXE_CNT_RNR_RETRY_EXCEEDED);
762 				wqe->status = IB_WC_RNR_RETRY_EXC_ERR;
763 				state = COMPST_ERROR;
764 			}
765 			break;
766 
767 		case COMPST_ERROR:
768 			WARN_ON_ONCE(wqe->status == IB_WC_SUCCESS);
769 			do_complete(qp, wqe);
770 			rxe_qp_error(qp);
771 			goto exit;
772 		}
773 	}
774 
775 	/* A non-zero return value will cause rxe_do_task to
776 	 * exit its loop and end the tasklet. A zero return
777 	 * will continue looping and return to rxe_completer
778 	 */
779 done:
780 	ret = 0;
781 	goto out;
782 exit:
783 	ret = -EAGAIN;
784 out:
785 	if (pkt)
786 		free_pkt(pkt);
787 	rxe_put(qp);
788 
789 	return ret;
790 }
791