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 #include <crypto/hash.h>
9 
10 #include "rxe.h"
11 #include "rxe_loc.h"
12 #include "rxe_queue.h"
13 
14 static int next_opcode(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
15 		       u32 opcode);
16 
17 static inline void retry_first_write_send(struct rxe_qp *qp,
18 					  struct rxe_send_wqe *wqe,
19 					  unsigned int mask, int npsn)
20 {
21 	int i;
22 
23 	for (i = 0; i < npsn; i++) {
24 		int to_send = (wqe->dma.resid > qp->mtu) ?
25 				qp->mtu : wqe->dma.resid;
26 
27 		qp->req.opcode = next_opcode(qp, wqe,
28 					     wqe->wr.opcode);
29 
30 		if (wqe->wr.send_flags & IB_SEND_INLINE) {
31 			wqe->dma.resid -= to_send;
32 			wqe->dma.sge_offset += to_send;
33 		} else {
34 			advance_dma_data(&wqe->dma, to_send);
35 		}
36 		if (mask & WR_WRITE_MASK)
37 			wqe->iova += qp->mtu;
38 	}
39 }
40 
41 static void req_retry(struct rxe_qp *qp)
42 {
43 	struct rxe_send_wqe *wqe;
44 	unsigned int wqe_index;
45 	unsigned int mask;
46 	int npsn;
47 	int first = 1;
48 	struct rxe_queue *q = qp->sq.queue;
49 	unsigned int cons;
50 	unsigned int prod;
51 
52 	cons = queue_get_consumer(q, QUEUE_TYPE_FROM_CLIENT);
53 	prod = queue_get_producer(q, QUEUE_TYPE_FROM_CLIENT);
54 
55 	qp->req.wqe_index	= cons;
56 	qp->req.psn		= qp->comp.psn;
57 	qp->req.opcode		= -1;
58 
59 	for (wqe_index = cons; wqe_index != prod;
60 			wqe_index = queue_next_index(q, wqe_index)) {
61 		wqe = queue_addr_from_index(qp->sq.queue, wqe_index);
62 		mask = wr_opcode_mask(wqe->wr.opcode, qp);
63 
64 		if (wqe->state == wqe_state_posted)
65 			break;
66 
67 		if (wqe->state == wqe_state_done)
68 			continue;
69 
70 		wqe->iova = (mask & WR_ATOMIC_MASK) ?
71 			     wqe->wr.wr.atomic.remote_addr :
72 			     (mask & WR_READ_OR_WRITE_MASK) ?
73 			     wqe->wr.wr.rdma.remote_addr :
74 			     0;
75 
76 		if (!first || (mask & WR_READ_MASK) == 0) {
77 			wqe->dma.resid = wqe->dma.length;
78 			wqe->dma.cur_sge = 0;
79 			wqe->dma.sge_offset = 0;
80 		}
81 
82 		if (first) {
83 			first = 0;
84 
85 			if (mask & WR_WRITE_OR_SEND_MASK) {
86 				npsn = (qp->comp.psn - wqe->first_psn) &
87 					BTH_PSN_MASK;
88 				retry_first_write_send(qp, wqe, mask, npsn);
89 			}
90 
91 			if (mask & WR_READ_MASK) {
92 				npsn = (wqe->dma.length - wqe->dma.resid) /
93 					qp->mtu;
94 				wqe->iova += npsn * qp->mtu;
95 			}
96 		}
97 
98 		wqe->state = wqe_state_posted;
99 	}
100 }
101 
102 void rnr_nak_timer(struct timer_list *t)
103 {
104 	struct rxe_qp *qp = from_timer(qp, t, rnr_nak_timer);
105 
106 	pr_debug("qp#%d rnr nak timer fired\n", qp_num(qp));
107 	rxe_run_task(&qp->req.task, 1);
108 }
109 
110 static struct rxe_send_wqe *req_next_wqe(struct rxe_qp *qp)
111 {
112 	struct rxe_send_wqe *wqe;
113 	struct rxe_queue *q = qp->sq.queue;
114 	unsigned int index = qp->req.wqe_index;
115 	unsigned int cons;
116 	unsigned int prod;
117 
118 	wqe = queue_head(q, QUEUE_TYPE_FROM_CLIENT);
119 	cons = queue_get_consumer(q, QUEUE_TYPE_FROM_CLIENT);
120 	prod = queue_get_producer(q, QUEUE_TYPE_FROM_CLIENT);
121 
122 	if (unlikely(qp->req.state == QP_STATE_DRAIN)) {
123 		/* check to see if we are drained;
124 		 * state_lock used by requester and completer
125 		 */
126 		spin_lock_bh(&qp->state_lock);
127 		do {
128 			if (qp->req.state != QP_STATE_DRAIN) {
129 				/* comp just finished */
130 				spin_unlock_bh(&qp->state_lock);
131 				break;
132 			}
133 
134 			if (wqe && ((index != cons) ||
135 				(wqe->state != wqe_state_posted))) {
136 				/* comp not done yet */
137 				spin_unlock_bh(&qp->state_lock);
138 				break;
139 			}
140 
141 			qp->req.state = QP_STATE_DRAINED;
142 			spin_unlock_bh(&qp->state_lock);
143 
144 			if (qp->ibqp.event_handler) {
145 				struct ib_event ev;
146 
147 				ev.device = qp->ibqp.device;
148 				ev.element.qp = &qp->ibqp;
149 				ev.event = IB_EVENT_SQ_DRAINED;
150 				qp->ibqp.event_handler(&ev,
151 					qp->ibqp.qp_context);
152 			}
153 		} while (0);
154 	}
155 
156 	if (index == prod)
157 		return NULL;
158 
159 	wqe = queue_addr_from_index(q, index);
160 
161 	if (unlikely((qp->req.state == QP_STATE_DRAIN ||
162 		      qp->req.state == QP_STATE_DRAINED) &&
163 		     (wqe->state != wqe_state_processing)))
164 		return NULL;
165 
166 	if (unlikely((wqe->wr.send_flags & IB_SEND_FENCE) &&
167 						     (index != cons))) {
168 		qp->req.wait_fence = 1;
169 		return NULL;
170 	}
171 
172 	wqe->mask = wr_opcode_mask(wqe->wr.opcode, qp);
173 	return wqe;
174 }
175 
176 static int next_opcode_rc(struct rxe_qp *qp, u32 opcode, int fits)
177 {
178 	switch (opcode) {
179 	case IB_WR_RDMA_WRITE:
180 		if (qp->req.opcode == IB_OPCODE_RC_RDMA_WRITE_FIRST ||
181 		    qp->req.opcode == IB_OPCODE_RC_RDMA_WRITE_MIDDLE)
182 			return fits ?
183 				IB_OPCODE_RC_RDMA_WRITE_LAST :
184 				IB_OPCODE_RC_RDMA_WRITE_MIDDLE;
185 		else
186 			return fits ?
187 				IB_OPCODE_RC_RDMA_WRITE_ONLY :
188 				IB_OPCODE_RC_RDMA_WRITE_FIRST;
189 
190 	case IB_WR_RDMA_WRITE_WITH_IMM:
191 		if (qp->req.opcode == IB_OPCODE_RC_RDMA_WRITE_FIRST ||
192 		    qp->req.opcode == IB_OPCODE_RC_RDMA_WRITE_MIDDLE)
193 			return fits ?
194 				IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE :
195 				IB_OPCODE_RC_RDMA_WRITE_MIDDLE;
196 		else
197 			return fits ?
198 				IB_OPCODE_RC_RDMA_WRITE_ONLY_WITH_IMMEDIATE :
199 				IB_OPCODE_RC_RDMA_WRITE_FIRST;
200 
201 	case IB_WR_SEND:
202 		if (qp->req.opcode == IB_OPCODE_RC_SEND_FIRST ||
203 		    qp->req.opcode == IB_OPCODE_RC_SEND_MIDDLE)
204 			return fits ?
205 				IB_OPCODE_RC_SEND_LAST :
206 				IB_OPCODE_RC_SEND_MIDDLE;
207 		else
208 			return fits ?
209 				IB_OPCODE_RC_SEND_ONLY :
210 				IB_OPCODE_RC_SEND_FIRST;
211 
212 	case IB_WR_SEND_WITH_IMM:
213 		if (qp->req.opcode == IB_OPCODE_RC_SEND_FIRST ||
214 		    qp->req.opcode == IB_OPCODE_RC_SEND_MIDDLE)
215 			return fits ?
216 				IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE :
217 				IB_OPCODE_RC_SEND_MIDDLE;
218 		else
219 			return fits ?
220 				IB_OPCODE_RC_SEND_ONLY_WITH_IMMEDIATE :
221 				IB_OPCODE_RC_SEND_FIRST;
222 
223 	case IB_WR_RDMA_READ:
224 		return IB_OPCODE_RC_RDMA_READ_REQUEST;
225 
226 	case IB_WR_ATOMIC_CMP_AND_SWP:
227 		return IB_OPCODE_RC_COMPARE_SWAP;
228 
229 	case IB_WR_ATOMIC_FETCH_AND_ADD:
230 		return IB_OPCODE_RC_FETCH_ADD;
231 
232 	case IB_WR_SEND_WITH_INV:
233 		if (qp->req.opcode == IB_OPCODE_RC_SEND_FIRST ||
234 		    qp->req.opcode == IB_OPCODE_RC_SEND_MIDDLE)
235 			return fits ? IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE :
236 				IB_OPCODE_RC_SEND_MIDDLE;
237 		else
238 			return fits ? IB_OPCODE_RC_SEND_ONLY_WITH_INVALIDATE :
239 				IB_OPCODE_RC_SEND_FIRST;
240 	case IB_WR_REG_MR:
241 	case IB_WR_LOCAL_INV:
242 		return opcode;
243 	}
244 
245 	return -EINVAL;
246 }
247 
248 static int next_opcode_uc(struct rxe_qp *qp, u32 opcode, int fits)
249 {
250 	switch (opcode) {
251 	case IB_WR_RDMA_WRITE:
252 		if (qp->req.opcode == IB_OPCODE_UC_RDMA_WRITE_FIRST ||
253 		    qp->req.opcode == IB_OPCODE_UC_RDMA_WRITE_MIDDLE)
254 			return fits ?
255 				IB_OPCODE_UC_RDMA_WRITE_LAST :
256 				IB_OPCODE_UC_RDMA_WRITE_MIDDLE;
257 		else
258 			return fits ?
259 				IB_OPCODE_UC_RDMA_WRITE_ONLY :
260 				IB_OPCODE_UC_RDMA_WRITE_FIRST;
261 
262 	case IB_WR_RDMA_WRITE_WITH_IMM:
263 		if (qp->req.opcode == IB_OPCODE_UC_RDMA_WRITE_FIRST ||
264 		    qp->req.opcode == IB_OPCODE_UC_RDMA_WRITE_MIDDLE)
265 			return fits ?
266 				IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE :
267 				IB_OPCODE_UC_RDMA_WRITE_MIDDLE;
268 		else
269 			return fits ?
270 				IB_OPCODE_UC_RDMA_WRITE_ONLY_WITH_IMMEDIATE :
271 				IB_OPCODE_UC_RDMA_WRITE_FIRST;
272 
273 	case IB_WR_SEND:
274 		if (qp->req.opcode == IB_OPCODE_UC_SEND_FIRST ||
275 		    qp->req.opcode == IB_OPCODE_UC_SEND_MIDDLE)
276 			return fits ?
277 				IB_OPCODE_UC_SEND_LAST :
278 				IB_OPCODE_UC_SEND_MIDDLE;
279 		else
280 			return fits ?
281 				IB_OPCODE_UC_SEND_ONLY :
282 				IB_OPCODE_UC_SEND_FIRST;
283 
284 	case IB_WR_SEND_WITH_IMM:
285 		if (qp->req.opcode == IB_OPCODE_UC_SEND_FIRST ||
286 		    qp->req.opcode == IB_OPCODE_UC_SEND_MIDDLE)
287 			return fits ?
288 				IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE :
289 				IB_OPCODE_UC_SEND_MIDDLE;
290 		else
291 			return fits ?
292 				IB_OPCODE_UC_SEND_ONLY_WITH_IMMEDIATE :
293 				IB_OPCODE_UC_SEND_FIRST;
294 	}
295 
296 	return -EINVAL;
297 }
298 
299 static int next_opcode(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
300 		       u32 opcode)
301 {
302 	int fits = (wqe->dma.resid <= qp->mtu);
303 
304 	switch (qp_type(qp)) {
305 	case IB_QPT_RC:
306 		return next_opcode_rc(qp, opcode, fits);
307 
308 	case IB_QPT_UC:
309 		return next_opcode_uc(qp, opcode, fits);
310 
311 	case IB_QPT_SMI:
312 	case IB_QPT_UD:
313 	case IB_QPT_GSI:
314 		switch (opcode) {
315 		case IB_WR_SEND:
316 			return IB_OPCODE_UD_SEND_ONLY;
317 
318 		case IB_WR_SEND_WITH_IMM:
319 			return IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
320 		}
321 		break;
322 
323 	default:
324 		break;
325 	}
326 
327 	return -EINVAL;
328 }
329 
330 static inline int check_init_depth(struct rxe_qp *qp, struct rxe_send_wqe *wqe)
331 {
332 	int depth;
333 
334 	if (wqe->has_rd_atomic)
335 		return 0;
336 
337 	qp->req.need_rd_atomic = 1;
338 	depth = atomic_dec_return(&qp->req.rd_atomic);
339 
340 	if (depth >= 0) {
341 		qp->req.need_rd_atomic = 0;
342 		wqe->has_rd_atomic = 1;
343 		return 0;
344 	}
345 
346 	atomic_inc(&qp->req.rd_atomic);
347 	return -EAGAIN;
348 }
349 
350 static inline int get_mtu(struct rxe_qp *qp)
351 {
352 	struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
353 
354 	if ((qp_type(qp) == IB_QPT_RC) || (qp_type(qp) == IB_QPT_UC))
355 		return qp->mtu;
356 
357 	return rxe->port.mtu_cap;
358 }
359 
360 static struct sk_buff *init_req_packet(struct rxe_qp *qp,
361 				       struct rxe_send_wqe *wqe,
362 				       int opcode, int payload,
363 				       struct rxe_pkt_info *pkt)
364 {
365 	struct rxe_dev		*rxe = to_rdev(qp->ibqp.device);
366 	struct sk_buff		*skb;
367 	struct rxe_send_wr	*ibwr = &wqe->wr;
368 	struct rxe_av		*av;
369 	int			pad = (-payload) & 0x3;
370 	int			paylen;
371 	int			solicited;
372 	u32			qp_num;
373 	int			ack_req;
374 
375 	/* length from start of bth to end of icrc */
376 	paylen = rxe_opcode[opcode].length + payload + pad + RXE_ICRC_SIZE;
377 
378 	/* pkt->hdr, port_num and mask are initialized in ifc layer */
379 	pkt->rxe	= rxe;
380 	pkt->opcode	= opcode;
381 	pkt->qp		= qp;
382 	pkt->psn	= qp->req.psn;
383 	pkt->mask	= rxe_opcode[opcode].mask;
384 	pkt->paylen	= paylen;
385 	pkt->wqe	= wqe;
386 
387 	/* init skb */
388 	av = rxe_get_av(pkt);
389 	if (!av)
390 		return NULL;
391 
392 	skb = rxe_init_packet(rxe, av, paylen, pkt);
393 	if (unlikely(!skb))
394 		return NULL;
395 
396 	/* init bth */
397 	solicited = (ibwr->send_flags & IB_SEND_SOLICITED) &&
398 			(pkt->mask & RXE_END_MASK) &&
399 			((pkt->mask & (RXE_SEND_MASK)) ||
400 			(pkt->mask & (RXE_WRITE_MASK | RXE_IMMDT_MASK)) ==
401 			(RXE_WRITE_MASK | RXE_IMMDT_MASK));
402 
403 	qp_num = (pkt->mask & RXE_DETH_MASK) ? ibwr->wr.ud.remote_qpn :
404 					 qp->attr.dest_qp_num;
405 
406 	ack_req = ((pkt->mask & RXE_END_MASK) ||
407 		(qp->req.noack_pkts++ > RXE_MAX_PKT_PER_ACK));
408 	if (ack_req)
409 		qp->req.noack_pkts = 0;
410 
411 	bth_init(pkt, pkt->opcode, solicited, 0, pad, IB_DEFAULT_PKEY_FULL, qp_num,
412 		 ack_req, pkt->psn);
413 
414 	/* init optional headers */
415 	if (pkt->mask & RXE_RETH_MASK) {
416 		reth_set_rkey(pkt, ibwr->wr.rdma.rkey);
417 		reth_set_va(pkt, wqe->iova);
418 		reth_set_len(pkt, wqe->dma.resid);
419 	}
420 
421 	if (pkt->mask & RXE_IMMDT_MASK)
422 		immdt_set_imm(pkt, ibwr->ex.imm_data);
423 
424 	if (pkt->mask & RXE_IETH_MASK)
425 		ieth_set_rkey(pkt, ibwr->ex.invalidate_rkey);
426 
427 	if (pkt->mask & RXE_ATMETH_MASK) {
428 		atmeth_set_va(pkt, wqe->iova);
429 		if (opcode == IB_OPCODE_RC_COMPARE_SWAP ||
430 		    opcode == IB_OPCODE_RD_COMPARE_SWAP) {
431 			atmeth_set_swap_add(pkt, ibwr->wr.atomic.swap);
432 			atmeth_set_comp(pkt, ibwr->wr.atomic.compare_add);
433 		} else {
434 			atmeth_set_swap_add(pkt, ibwr->wr.atomic.compare_add);
435 		}
436 		atmeth_set_rkey(pkt, ibwr->wr.atomic.rkey);
437 	}
438 
439 	if (pkt->mask & RXE_DETH_MASK) {
440 		if (qp->ibqp.qp_num == 1)
441 			deth_set_qkey(pkt, GSI_QKEY);
442 		else
443 			deth_set_qkey(pkt, ibwr->wr.ud.remote_qkey);
444 		deth_set_sqp(pkt, qp->ibqp.qp_num);
445 	}
446 
447 	return skb;
448 }
449 
450 static int finish_packet(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
451 		       struct rxe_pkt_info *pkt, struct sk_buff *skb,
452 		       int paylen)
453 {
454 	int err;
455 
456 	err = rxe_prepare(pkt, skb);
457 	if (err)
458 		return err;
459 
460 	if (pkt->mask & RXE_WRITE_OR_SEND_MASK) {
461 		if (wqe->wr.send_flags & IB_SEND_INLINE) {
462 			u8 *tmp = &wqe->dma.inline_data[wqe->dma.sge_offset];
463 
464 			memcpy(payload_addr(pkt), tmp, paylen);
465 
466 			wqe->dma.resid -= paylen;
467 			wqe->dma.sge_offset += paylen;
468 		} else {
469 			err = copy_data(qp->pd, 0, &wqe->dma,
470 					payload_addr(pkt), paylen,
471 					RXE_FROM_MR_OBJ);
472 			if (err)
473 				return err;
474 		}
475 		if (bth_pad(pkt)) {
476 			u8 *pad = payload_addr(pkt) + paylen;
477 
478 			memset(pad, 0, bth_pad(pkt));
479 		}
480 	}
481 
482 	return 0;
483 }
484 
485 static void update_wqe_state(struct rxe_qp *qp,
486 		struct rxe_send_wqe *wqe,
487 		struct rxe_pkt_info *pkt)
488 {
489 	if (pkt->mask & RXE_END_MASK) {
490 		if (qp_type(qp) == IB_QPT_RC)
491 			wqe->state = wqe_state_pending;
492 	} else {
493 		wqe->state = wqe_state_processing;
494 	}
495 }
496 
497 static void update_wqe_psn(struct rxe_qp *qp,
498 			   struct rxe_send_wqe *wqe,
499 			   struct rxe_pkt_info *pkt,
500 			   int payload)
501 {
502 	/* number of packets left to send including current one */
503 	int num_pkt = (wqe->dma.resid + payload + qp->mtu - 1) / qp->mtu;
504 
505 	/* handle zero length packet case */
506 	if (num_pkt == 0)
507 		num_pkt = 1;
508 
509 	if (pkt->mask & RXE_START_MASK) {
510 		wqe->first_psn = qp->req.psn;
511 		wqe->last_psn = (qp->req.psn + num_pkt - 1) & BTH_PSN_MASK;
512 	}
513 
514 	if (pkt->mask & RXE_READ_MASK)
515 		qp->req.psn = (wqe->first_psn + num_pkt) & BTH_PSN_MASK;
516 	else
517 		qp->req.psn = (qp->req.psn + 1) & BTH_PSN_MASK;
518 }
519 
520 static void save_state(struct rxe_send_wqe *wqe,
521 		       struct rxe_qp *qp,
522 		       struct rxe_send_wqe *rollback_wqe,
523 		       u32 *rollback_psn)
524 {
525 	rollback_wqe->state     = wqe->state;
526 	rollback_wqe->first_psn = wqe->first_psn;
527 	rollback_wqe->last_psn  = wqe->last_psn;
528 	*rollback_psn		= qp->req.psn;
529 }
530 
531 static void rollback_state(struct rxe_send_wqe *wqe,
532 			   struct rxe_qp *qp,
533 			   struct rxe_send_wqe *rollback_wqe,
534 			   u32 rollback_psn)
535 {
536 	wqe->state     = rollback_wqe->state;
537 	wqe->first_psn = rollback_wqe->first_psn;
538 	wqe->last_psn  = rollback_wqe->last_psn;
539 	qp->req.psn    = rollback_psn;
540 }
541 
542 static void update_state(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
543 			 struct rxe_pkt_info *pkt, int payload)
544 {
545 	qp->req.opcode = pkt->opcode;
546 
547 	if (pkt->mask & RXE_END_MASK)
548 		qp->req.wqe_index = queue_next_index(qp->sq.queue,
549 						     qp->req.wqe_index);
550 
551 	qp->need_req_skb = 0;
552 
553 	if (qp->qp_timeout_jiffies && !timer_pending(&qp->retrans_timer))
554 		mod_timer(&qp->retrans_timer,
555 			  jiffies + qp->qp_timeout_jiffies);
556 }
557 
558 static int rxe_do_local_ops(struct rxe_qp *qp, struct rxe_send_wqe *wqe)
559 {
560 	u8 opcode = wqe->wr.opcode;
561 	u32 rkey;
562 	int ret;
563 
564 	switch (opcode) {
565 	case IB_WR_LOCAL_INV:
566 		rkey = wqe->wr.ex.invalidate_rkey;
567 		if (rkey_is_mw(rkey))
568 			ret = rxe_invalidate_mw(qp, rkey);
569 		else
570 			ret = rxe_invalidate_mr(qp, rkey);
571 
572 		if (unlikely(ret)) {
573 			wqe->status = IB_WC_LOC_QP_OP_ERR;
574 			return ret;
575 		}
576 		break;
577 	case IB_WR_REG_MR:
578 		ret = rxe_reg_fast_mr(qp, wqe);
579 		if (unlikely(ret)) {
580 			wqe->status = IB_WC_LOC_QP_OP_ERR;
581 			return ret;
582 		}
583 		break;
584 	case IB_WR_BIND_MW:
585 		ret = rxe_bind_mw(qp, wqe);
586 		if (unlikely(ret)) {
587 			wqe->status = IB_WC_MW_BIND_ERR;
588 			return ret;
589 		}
590 		break;
591 	default:
592 		pr_err("Unexpected send wqe opcode %d\n", opcode);
593 		wqe->status = IB_WC_LOC_QP_OP_ERR;
594 		return -EINVAL;
595 	}
596 
597 	wqe->state = wqe_state_done;
598 	wqe->status = IB_WC_SUCCESS;
599 	qp->req.wqe_index = queue_next_index(qp->sq.queue, qp->req.wqe_index);
600 
601 	if ((wqe->wr.send_flags & IB_SEND_SIGNALED) ||
602 	    qp->sq_sig_type == IB_SIGNAL_ALL_WR)
603 		rxe_run_task(&qp->comp.task, 1);
604 
605 	return 0;
606 }
607 
608 int rxe_requester(void *arg)
609 {
610 	struct rxe_qp *qp = (struct rxe_qp *)arg;
611 	struct rxe_pkt_info pkt;
612 	struct sk_buff *skb;
613 	struct rxe_send_wqe *wqe;
614 	enum rxe_hdr_mask mask;
615 	int payload;
616 	int mtu;
617 	int opcode;
618 	int ret;
619 	struct rxe_send_wqe rollback_wqe;
620 	u32 rollback_psn;
621 	struct rxe_queue *q = qp->sq.queue;
622 
623 	rxe_add_ref(qp);
624 
625 next_wqe:
626 	if (unlikely(!qp->valid || qp->req.state == QP_STATE_ERROR))
627 		goto exit;
628 
629 	if (unlikely(qp->req.state == QP_STATE_RESET)) {
630 		qp->req.wqe_index = queue_get_consumer(q,
631 						QUEUE_TYPE_FROM_CLIENT);
632 		qp->req.opcode = -1;
633 		qp->req.need_rd_atomic = 0;
634 		qp->req.wait_psn = 0;
635 		qp->req.need_retry = 0;
636 		goto exit;
637 	}
638 
639 	if (unlikely(qp->req.need_retry)) {
640 		req_retry(qp);
641 		qp->req.need_retry = 0;
642 	}
643 
644 	wqe = req_next_wqe(qp);
645 	if (unlikely(!wqe))
646 		goto exit;
647 
648 	if (wqe->mask & WR_LOCAL_OP_MASK) {
649 		ret = rxe_do_local_ops(qp, wqe);
650 		if (unlikely(ret))
651 			goto err;
652 		else
653 			goto next_wqe;
654 	}
655 
656 	if (unlikely(qp_type(qp) == IB_QPT_RC &&
657 		psn_compare(qp->req.psn, (qp->comp.psn +
658 				RXE_MAX_UNACKED_PSNS)) > 0)) {
659 		qp->req.wait_psn = 1;
660 		goto exit;
661 	}
662 
663 	/* Limit the number of inflight SKBs per QP */
664 	if (unlikely(atomic_read(&qp->skb_out) >
665 		     RXE_INFLIGHT_SKBS_PER_QP_HIGH)) {
666 		qp->need_req_skb = 1;
667 		goto exit;
668 	}
669 
670 	opcode = next_opcode(qp, wqe, wqe->wr.opcode);
671 	if (unlikely(opcode < 0)) {
672 		wqe->status = IB_WC_LOC_QP_OP_ERR;
673 		goto exit;
674 	}
675 
676 	mask = rxe_opcode[opcode].mask;
677 	if (unlikely(mask & RXE_READ_OR_ATOMIC_MASK)) {
678 		if (check_init_depth(qp, wqe))
679 			goto exit;
680 	}
681 
682 	mtu = get_mtu(qp);
683 	payload = (mask & RXE_WRITE_OR_SEND_MASK) ? wqe->dma.resid : 0;
684 	if (payload > mtu) {
685 		if (qp_type(qp) == IB_QPT_UD) {
686 			/* C10-93.1.1: If the total sum of all the buffer lengths specified for a
687 			 * UD message exceeds the MTU of the port as returned by QueryHCA, the CI
688 			 * shall not emit any packets for this message. Further, the CI shall not
689 			 * generate an error due to this condition.
690 			 */
691 
692 			/* fake a successful UD send */
693 			wqe->first_psn = qp->req.psn;
694 			wqe->last_psn = qp->req.psn;
695 			qp->req.psn = (qp->req.psn + 1) & BTH_PSN_MASK;
696 			qp->req.opcode = IB_OPCODE_UD_SEND_ONLY;
697 			qp->req.wqe_index = queue_next_index(qp->sq.queue,
698 						       qp->req.wqe_index);
699 			wqe->state = wqe_state_done;
700 			wqe->status = IB_WC_SUCCESS;
701 			__rxe_do_task(&qp->comp.task);
702 			rxe_drop_ref(qp);
703 			return 0;
704 		}
705 		payload = mtu;
706 	}
707 
708 	skb = init_req_packet(qp, wqe, opcode, payload, &pkt);
709 	if (unlikely(!skb)) {
710 		pr_err("qp#%d Failed allocating skb\n", qp_num(qp));
711 		wqe->status = IB_WC_LOC_QP_OP_ERR;
712 		goto err;
713 	}
714 
715 	ret = finish_packet(qp, wqe, &pkt, skb, payload);
716 	if (unlikely(ret)) {
717 		pr_debug("qp#%d Error during finish packet\n", qp_num(qp));
718 		if (ret == -EFAULT)
719 			wqe->status = IB_WC_LOC_PROT_ERR;
720 		else
721 			wqe->status = IB_WC_LOC_QP_OP_ERR;
722 		kfree_skb(skb);
723 		goto err;
724 	}
725 
726 	/*
727 	 * To prevent a race on wqe access between requester and completer,
728 	 * wqe members state and psn need to be set before calling
729 	 * rxe_xmit_packet().
730 	 * Otherwise, completer might initiate an unjustified retry flow.
731 	 */
732 	save_state(wqe, qp, &rollback_wqe, &rollback_psn);
733 	update_wqe_state(qp, wqe, &pkt);
734 	update_wqe_psn(qp, wqe, &pkt, payload);
735 	ret = rxe_xmit_packet(qp, &pkt, skb);
736 	if (ret) {
737 		qp->need_req_skb = 1;
738 
739 		rollback_state(wqe, qp, &rollback_wqe, rollback_psn);
740 
741 		if (ret == -EAGAIN) {
742 			rxe_run_task(&qp->req.task, 1);
743 			goto exit;
744 		}
745 
746 		wqe->status = IB_WC_LOC_QP_OP_ERR;
747 		goto err;
748 	}
749 
750 	update_state(qp, wqe, &pkt, payload);
751 
752 	goto next_wqe;
753 
754 err:
755 	wqe->state = wqe_state_error;
756 	__rxe_do_task(&qp->comp.task);
757 
758 exit:
759 	rxe_drop_ref(qp);
760 	return -EAGAIN;
761 }
762