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