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