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