blob: fe275fcaffbd919ef282d2197497509c6ba01369 [file] [log] [blame]
Bob Pearson63fa15d2020-08-27 09:54:40 -05001// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
Moni Shoua8700e3e2016-06-16 16:45:23 +03002/*
3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
Moni Shoua8700e3e2016-06-16 16:45:23 +03005 */
6
7#include <linux/skbuff.h>
yonatanccee26882017-04-20 20:55:55 +03008#include <crypto/hash.h>
Moni Shoua8700e3e2016-06-16 16:45:23 +03009
10#include "rxe.h"
11#include "rxe_loc.h"
12#include "rxe_queue.h"
13
14static int next_opcode(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
Parav Pandite404f942016-09-28 20:26:26 +000015 u32 opcode);
Moni Shoua8700e3e2016-06-16 16:45:23 +030016
17static inline void retry_first_write_send(struct rxe_qp *qp,
18 struct rxe_send_wqe *wqe,
Kamal Heibc498e822017-06-15 11:29:04 +030019 unsigned int mask, int npsn)
Moni Shoua8700e3e2016-06-16 16:45:23 +030020{
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
41static 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;
Bob Pearson5bcf5a52021-05-27 14:47:48 -050048 struct rxe_queue *q = qp->sq.queue;
49 unsigned int cons;
50 unsigned int prod;
Moni Shoua8700e3e2016-06-16 16:45:23 +030051
Bob Pearsonae6e8432021-09-14 11:42:03 -050052 cons = queue_get_consumer(q, QUEUE_TYPE_FROM_CLIENT);
53 prod = queue_get_producer(q, QUEUE_TYPE_FROM_CLIENT);
Bob Pearson5bcf5a52021-05-27 14:47:48 -050054
55 qp->req.wqe_index = cons;
Moni Shoua8700e3e2016-06-16 16:45:23 +030056 qp->req.psn = qp->comp.psn;
57 qp->req.opcode = -1;
58
Bob Pearson5bcf5a52021-05-27 14:47:48 -050059 for (wqe_index = cons; wqe_index != prod;
Bob Pearsonae6e8432021-09-14 11:42:03 -050060 wqe_index = queue_next_index(q, wqe_index)) {
61 wqe = queue_addr_from_index(qp->sq.queue, wqe_index);
Moni Shoua8700e3e2016-06-16 16:45:23 +030062 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
Vijay Immanuel030e46e2018-06-13 18:48:07 -070085 if (mask & WR_WRITE_OR_SEND_MASK) {
86 npsn = (qp->comp.psn - wqe->first_psn) &
87 BTH_PSN_MASK;
Moni Shoua8700e3e2016-06-16 16:45:23 +030088 retry_first_write_send(qp, wqe, mask, npsn);
Vijay Immanuel030e46e2018-06-13 18:48:07 -070089 }
Moni Shoua8700e3e2016-06-16 16:45:23 +030090
Vijay Immanuel030e46e2018-06-13 18:48:07 -070091 if (mask & WR_READ_MASK) {
92 npsn = (wqe->dma.length - wqe->dma.resid) /
93 qp->mtu;
Moni Shoua8700e3e2016-06-16 16:45:23 +030094 wqe->iova += npsn * qp->mtu;
Vijay Immanuel030e46e2018-06-13 18:48:07 -070095 }
Moni Shoua8700e3e2016-06-16 16:45:23 +030096 }
97
98 wqe->state = wqe_state_posted;
99 }
100}
101
Kees Cook3bfbea72017-10-24 03:28:27 -0700102void rnr_nak_timer(struct timer_list *t)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300103{
Kees Cook3bfbea72017-10-24 03:28:27 -0700104 struct rxe_qp *qp = from_timer(qp, t, rnr_nak_timer);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300105
Parav Pandite404f942016-09-28 20:26:26 +0000106 pr_debug("qp#%d rnr nak timer fired\n", qp_num(qp));
Moni Shoua8700e3e2016-06-16 16:45:23 +0300107 rxe_run_task(&qp->req.task, 1);
108}
109
110static struct rxe_send_wqe *req_next_wqe(struct rxe_qp *qp)
111{
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500112 struct rxe_send_wqe *wqe;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300113 unsigned long flags;
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500114 struct rxe_queue *q = qp->sq.queue;
115 unsigned int index = qp->req.wqe_index;
116 unsigned int cons;
117 unsigned int prod;
118
Bob Pearsonae6e8432021-09-14 11:42:03 -0500119 wqe = queue_head(q, QUEUE_TYPE_FROM_CLIENT);
120 cons = queue_get_consumer(q, QUEUE_TYPE_FROM_CLIENT);
121 prod = queue_get_producer(q, QUEUE_TYPE_FROM_CLIENT);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300122
123 if (unlikely(qp->req.state == QP_STATE_DRAIN)) {
124 /* check to see if we are drained;
125 * state_lock used by requester and completer
126 */
127 spin_lock_irqsave(&qp->state_lock, flags);
128 do {
129 if (qp->req.state != QP_STATE_DRAIN) {
130 /* comp just finished */
131 spin_unlock_irqrestore(&qp->state_lock,
132 flags);
133 break;
134 }
135
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500136 if (wqe && ((index != cons) ||
Moni Shoua8700e3e2016-06-16 16:45:23 +0300137 (wqe->state != wqe_state_posted))) {
138 /* comp not done yet */
139 spin_unlock_irqrestore(&qp->state_lock,
140 flags);
141 break;
142 }
143
144 qp->req.state = QP_STATE_DRAINED;
145 spin_unlock_irqrestore(&qp->state_lock, flags);
146
147 if (qp->ibqp.event_handler) {
148 struct ib_event ev;
149
150 ev.device = qp->ibqp.device;
151 ev.element.qp = &qp->ibqp;
152 ev.event = IB_EVENT_SQ_DRAINED;
153 qp->ibqp.event_handler(&ev,
154 qp->ibqp.qp_context);
155 }
156 } while (0);
157 }
158
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500159 if (index == prod)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300160 return NULL;
161
Bob Pearsonae6e8432021-09-14 11:42:03 -0500162 wqe = queue_addr_from_index(q, index);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300163
164 if (unlikely((qp->req.state == QP_STATE_DRAIN ||
165 qp->req.state == QP_STATE_DRAINED) &&
166 (wqe->state != wqe_state_processing)))
167 return NULL;
168
169 if (unlikely((wqe->wr.send_flags & IB_SEND_FENCE) &&
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500170 (index != cons))) {
Moni Shoua8700e3e2016-06-16 16:45:23 +0300171 qp->req.wait_fence = 1;
172 return NULL;
173 }
174
175 wqe->mask = wr_opcode_mask(wqe->wr.opcode, qp);
176 return wqe;
177}
178
Parav Pandite404f942016-09-28 20:26:26 +0000179static int next_opcode_rc(struct rxe_qp *qp, u32 opcode, int fits)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300180{
181 switch (opcode) {
182 case IB_WR_RDMA_WRITE:
183 if (qp->req.opcode == IB_OPCODE_RC_RDMA_WRITE_FIRST ||
184 qp->req.opcode == IB_OPCODE_RC_RDMA_WRITE_MIDDLE)
185 return fits ?
186 IB_OPCODE_RC_RDMA_WRITE_LAST :
187 IB_OPCODE_RC_RDMA_WRITE_MIDDLE;
188 else
189 return fits ?
190 IB_OPCODE_RC_RDMA_WRITE_ONLY :
191 IB_OPCODE_RC_RDMA_WRITE_FIRST;
192
193 case IB_WR_RDMA_WRITE_WITH_IMM:
194 if (qp->req.opcode == IB_OPCODE_RC_RDMA_WRITE_FIRST ||
195 qp->req.opcode == IB_OPCODE_RC_RDMA_WRITE_MIDDLE)
196 return fits ?
197 IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE :
198 IB_OPCODE_RC_RDMA_WRITE_MIDDLE;
199 else
200 return fits ?
201 IB_OPCODE_RC_RDMA_WRITE_ONLY_WITH_IMMEDIATE :
202 IB_OPCODE_RC_RDMA_WRITE_FIRST;
203
204 case IB_WR_SEND:
205 if (qp->req.opcode == IB_OPCODE_RC_SEND_FIRST ||
206 qp->req.opcode == IB_OPCODE_RC_SEND_MIDDLE)
207 return fits ?
208 IB_OPCODE_RC_SEND_LAST :
209 IB_OPCODE_RC_SEND_MIDDLE;
210 else
211 return fits ?
212 IB_OPCODE_RC_SEND_ONLY :
213 IB_OPCODE_RC_SEND_FIRST;
214
215 case IB_WR_SEND_WITH_IMM:
216 if (qp->req.opcode == IB_OPCODE_RC_SEND_FIRST ||
217 qp->req.opcode == IB_OPCODE_RC_SEND_MIDDLE)
218 return fits ?
219 IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE :
220 IB_OPCODE_RC_SEND_MIDDLE;
221 else
222 return fits ?
223 IB_OPCODE_RC_SEND_ONLY_WITH_IMMEDIATE :
224 IB_OPCODE_RC_SEND_FIRST;
225
226 case IB_WR_RDMA_READ:
227 return IB_OPCODE_RC_RDMA_READ_REQUEST;
228
229 case IB_WR_ATOMIC_CMP_AND_SWP:
230 return IB_OPCODE_RC_COMPARE_SWAP;
231
232 case IB_WR_ATOMIC_FETCH_AND_ADD:
233 return IB_OPCODE_RC_FETCH_ADD;
234
235 case IB_WR_SEND_WITH_INV:
236 if (qp->req.opcode == IB_OPCODE_RC_SEND_FIRST ||
237 qp->req.opcode == IB_OPCODE_RC_SEND_MIDDLE)
238 return fits ? IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE :
239 IB_OPCODE_RC_SEND_MIDDLE;
240 else
241 return fits ? IB_OPCODE_RC_SEND_ONLY_WITH_INVALIDATE :
242 IB_OPCODE_RC_SEND_FIRST;
243 case IB_WR_REG_MR:
244 case IB_WR_LOCAL_INV:
245 return opcode;
246 }
247
248 return -EINVAL;
249}
250
Parav Pandite404f942016-09-28 20:26:26 +0000251static int next_opcode_uc(struct rxe_qp *qp, u32 opcode, int fits)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300252{
253 switch (opcode) {
254 case IB_WR_RDMA_WRITE:
255 if (qp->req.opcode == IB_OPCODE_UC_RDMA_WRITE_FIRST ||
256 qp->req.opcode == IB_OPCODE_UC_RDMA_WRITE_MIDDLE)
257 return fits ?
258 IB_OPCODE_UC_RDMA_WRITE_LAST :
259 IB_OPCODE_UC_RDMA_WRITE_MIDDLE;
260 else
261 return fits ?
262 IB_OPCODE_UC_RDMA_WRITE_ONLY :
263 IB_OPCODE_UC_RDMA_WRITE_FIRST;
264
265 case IB_WR_RDMA_WRITE_WITH_IMM:
266 if (qp->req.opcode == IB_OPCODE_UC_RDMA_WRITE_FIRST ||
267 qp->req.opcode == IB_OPCODE_UC_RDMA_WRITE_MIDDLE)
268 return fits ?
269 IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE :
270 IB_OPCODE_UC_RDMA_WRITE_MIDDLE;
271 else
272 return fits ?
273 IB_OPCODE_UC_RDMA_WRITE_ONLY_WITH_IMMEDIATE :
274 IB_OPCODE_UC_RDMA_WRITE_FIRST;
275
276 case IB_WR_SEND:
277 if (qp->req.opcode == IB_OPCODE_UC_SEND_FIRST ||
278 qp->req.opcode == IB_OPCODE_UC_SEND_MIDDLE)
279 return fits ?
280 IB_OPCODE_UC_SEND_LAST :
281 IB_OPCODE_UC_SEND_MIDDLE;
282 else
283 return fits ?
284 IB_OPCODE_UC_SEND_ONLY :
285 IB_OPCODE_UC_SEND_FIRST;
286
287 case IB_WR_SEND_WITH_IMM:
288 if (qp->req.opcode == IB_OPCODE_UC_SEND_FIRST ||
289 qp->req.opcode == IB_OPCODE_UC_SEND_MIDDLE)
290 return fits ?
291 IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE :
292 IB_OPCODE_UC_SEND_MIDDLE;
293 else
294 return fits ?
295 IB_OPCODE_UC_SEND_ONLY_WITH_IMMEDIATE :
296 IB_OPCODE_UC_SEND_FIRST;
297 }
298
299 return -EINVAL;
300}
301
302static int next_opcode(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
Parav Pandite404f942016-09-28 20:26:26 +0000303 u32 opcode)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300304{
305 int fits = (wqe->dma.resid <= qp->mtu);
306
307 switch (qp_type(qp)) {
308 case IB_QPT_RC:
309 return next_opcode_rc(qp, opcode, fits);
310
311 case IB_QPT_UC:
312 return next_opcode_uc(qp, opcode, fits);
313
314 case IB_QPT_SMI:
315 case IB_QPT_UD:
316 case IB_QPT_GSI:
317 switch (opcode) {
318 case IB_WR_SEND:
319 return IB_OPCODE_UD_SEND_ONLY;
320
321 case IB_WR_SEND_WITH_IMM:
322 return IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
323 }
324 break;
325
326 default:
327 break;
328 }
329
330 return -EINVAL;
331}
332
333static inline int check_init_depth(struct rxe_qp *qp, struct rxe_send_wqe *wqe)
334{
335 int depth;
336
337 if (wqe->has_rd_atomic)
338 return 0;
339
340 qp->req.need_rd_atomic = 1;
341 depth = atomic_dec_return(&qp->req.rd_atomic);
342
343 if (depth >= 0) {
344 qp->req.need_rd_atomic = 0;
345 wqe->has_rd_atomic = 1;
346 return 0;
347 }
348
349 atomic_inc(&qp->req.rd_atomic);
350 return -EAGAIN;
351}
352
Bart Van Assche175f1242017-01-10 11:15:43 -0800353static inline int get_mtu(struct rxe_qp *qp)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300354{
355 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300356
357 if ((qp_type(qp) == IB_QPT_RC) || (qp_type(qp) == IB_QPT_UC))
358 return qp->mtu;
359
Bart Van Assche175f1242017-01-10 11:15:43 -0800360 return rxe->port.mtu_cap;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300361}
362
363static struct sk_buff *init_req_packet(struct rxe_qp *qp,
364 struct rxe_send_wqe *wqe,
365 int opcode, int payload,
366 struct rxe_pkt_info *pkt)
367{
368 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300369 struct sk_buff *skb;
370 struct rxe_send_wr *ibwr = &wqe->wr;
371 struct rxe_av *av;
372 int pad = (-payload) & 0x3;
373 int paylen;
374 int solicited;
375 u16 pkey;
376 u32 qp_num;
377 int ack_req;
378
379 /* length from start of bth to end of icrc */
380 paylen = rxe_opcode[opcode].length + payload + pad + RXE_ICRC_SIZE;
381
382 /* pkt->hdr, rxe, port_num and mask are initialized in ifc
383 * layer
384 */
385 pkt->opcode = opcode;
386 pkt->qp = qp;
387 pkt->psn = qp->req.psn;
388 pkt->mask = rxe_opcode[opcode].mask;
389 pkt->paylen = paylen;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300390 pkt->wqe = wqe;
391
392 /* init skb */
393 av = rxe_get_av(pkt);
Bart Van Assche839f5ac2017-01-10 11:15:53 -0800394 skb = rxe_init_packet(rxe, av, paylen, pkt);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300395 if (unlikely(!skb))
396 return NULL;
397
398 /* init bth */
399 solicited = (ibwr->send_flags & IB_SEND_SOLICITED) &&
400 (pkt->mask & RXE_END_MASK) &&
401 ((pkt->mask & (RXE_SEND_MASK)) ||
402 (pkt->mask & (RXE_WRITE_MASK | RXE_IMMDT_MASK)) ==
403 (RXE_WRITE_MASK | RXE_IMMDT_MASK));
404
Kamal Heib76251e12020-07-21 13:16:18 +0300405 pkey = IB_DEFAULT_PKEY_FULL;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300406
407 qp_num = (pkt->mask & RXE_DETH_MASK) ? ibwr->wr.ud.remote_qpn :
408 qp->attr.dest_qp_num;
409
410 ack_req = ((pkt->mask & RXE_END_MASK) ||
411 (qp->req.noack_pkts++ > RXE_MAX_PKT_PER_ACK));
412 if (ack_req)
413 qp->req.noack_pkts = 0;
414
415 bth_init(pkt, pkt->opcode, solicited, 0, pad, pkey, qp_num,
416 ack_req, pkt->psn);
417
418 /* init optional headers */
419 if (pkt->mask & RXE_RETH_MASK) {
420 reth_set_rkey(pkt, ibwr->wr.rdma.rkey);
421 reth_set_va(pkt, wqe->iova);
Vijay Immanuel030e46e2018-06-13 18:48:07 -0700422 reth_set_len(pkt, wqe->dma.resid);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300423 }
424
425 if (pkt->mask & RXE_IMMDT_MASK)
426 immdt_set_imm(pkt, ibwr->ex.imm_data);
427
428 if (pkt->mask & RXE_IETH_MASK)
429 ieth_set_rkey(pkt, ibwr->ex.invalidate_rkey);
430
431 if (pkt->mask & RXE_ATMETH_MASK) {
432 atmeth_set_va(pkt, wqe->iova);
433 if (opcode == IB_OPCODE_RC_COMPARE_SWAP ||
434 opcode == IB_OPCODE_RD_COMPARE_SWAP) {
435 atmeth_set_swap_add(pkt, ibwr->wr.atomic.swap);
436 atmeth_set_comp(pkt, ibwr->wr.atomic.compare_add);
437 } else {
438 atmeth_set_swap_add(pkt, ibwr->wr.atomic.compare_add);
439 }
440 atmeth_set_rkey(pkt, ibwr->wr.atomic.rkey);
441 }
442
443 if (pkt->mask & RXE_DETH_MASK) {
444 if (qp->ibqp.qp_num == 1)
445 deth_set_qkey(pkt, GSI_QKEY);
446 else
447 deth_set_qkey(pkt, ibwr->wr.ud.remote_qkey);
448 deth_set_sqp(pkt, qp->ibqp.qp_num);
449 }
450
451 return skb;
452}
453
Bob Pearsonc1a41122021-06-07 23:25:49 -0500454static int finish_packet(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
Moni Shoua8700e3e2016-06-16 16:45:23 +0300455 struct rxe_pkt_info *pkt, struct sk_buff *skb,
456 int paylen)
457{
Moni Shoua8700e3e2016-06-16 16:45:23 +0300458 int err;
459
Bob Pearson1117f262021-07-06 23:00:36 -0500460 err = rxe_prepare(pkt, skb);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300461 if (err)
462 return err;
463
Xiao Yang45216d62021-09-14 16:02:52 +0800464 if (pkt->mask & RXE_WRITE_OR_SEND_MASK) {
Moni Shoua8700e3e2016-06-16 16:45:23 +0300465 if (wqe->wr.send_flags & IB_SEND_INLINE) {
466 u8 *tmp = &wqe->dma.inline_data[wqe->dma.sge_offset];
467
Moni Shoua8700e3e2016-06-16 16:45:23 +0300468 memcpy(payload_addr(pkt), tmp, paylen);
469
470 wqe->dma.resid -= paylen;
471 wqe->dma.sge_offset += paylen;
472 } else {
Zhu Yanjune12ee8c2018-04-23 03:57:58 -0400473 err = copy_data(qp->pd, 0, &wqe->dma,
Moni Shoua8700e3e2016-06-16 16:45:23 +0300474 payload_addr(pkt), paylen,
Bob Pearson1117f262021-07-06 23:00:36 -0500475 RXE_FROM_MR_OBJ);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300476 if (err)
477 return err;
478 }
Steve Wise2030abd2019-12-02 20:03:20 -0600479 if (bth_pad(pkt)) {
480 u8 *pad = payload_addr(pkt) + paylen;
481
482 memset(pad, 0, bth_pad(pkt));
Steve Wise2030abd2019-12-02 20:03:20 -0600483 }
Moni Shoua8700e3e2016-06-16 16:45:23 +0300484 }
Moni Shoua8700e3e2016-06-16 16:45:23 +0300485
486 return 0;
487}
488
489static void update_wqe_state(struct rxe_qp *qp,
Yonatan Cohen3050b992016-09-07 14:04:06 +0300490 struct rxe_send_wqe *wqe,
491 struct rxe_pkt_info *pkt)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300492{
Moni Shoua8700e3e2016-06-16 16:45:23 +0300493 if (pkt->mask & RXE_END_MASK) {
494 if (qp_type(qp) == IB_QPT_RC)
495 wqe->state = wqe_state_pending;
496 } else {
497 wqe->state = wqe_state_processing;
498 }
Moni Shoua8700e3e2016-06-16 16:45:23 +0300499}
500
Yonatan Cohen3050b992016-09-07 14:04:06 +0300501static void update_wqe_psn(struct rxe_qp *qp,
502 struct rxe_send_wqe *wqe,
503 struct rxe_pkt_info *pkt,
504 int payload)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300505{
506 /* number of packets left to send including current one */
507 int num_pkt = (wqe->dma.resid + payload + qp->mtu - 1) / qp->mtu;
508
509 /* handle zero length packet case */
510 if (num_pkt == 0)
511 num_pkt = 1;
512
513 if (pkt->mask & RXE_START_MASK) {
514 wqe->first_psn = qp->req.psn;
515 wqe->last_psn = (qp->req.psn + num_pkt - 1) & BTH_PSN_MASK;
516 }
517
518 if (pkt->mask & RXE_READ_MASK)
519 qp->req.psn = (wqe->first_psn + num_pkt) & BTH_PSN_MASK;
520 else
521 qp->req.psn = (qp->req.psn + 1) & BTH_PSN_MASK;
Yonatan Cohen3050b992016-09-07 14:04:06 +0300522}
Moni Shoua8700e3e2016-06-16 16:45:23 +0300523
Yonatan Cohen3050b992016-09-07 14:04:06 +0300524static void save_state(struct rxe_send_wqe *wqe,
525 struct rxe_qp *qp,
526 struct rxe_send_wqe *rollback_wqe,
Arnd Bergmanna0fa7262016-09-19 13:57:26 +0200527 u32 *rollback_psn)
Yonatan Cohen3050b992016-09-07 14:04:06 +0300528{
529 rollback_wqe->state = wqe->state;
530 rollback_wqe->first_psn = wqe->first_psn;
531 rollback_wqe->last_psn = wqe->last_psn;
Arnd Bergmanna0fa7262016-09-19 13:57:26 +0200532 *rollback_psn = qp->req.psn;
Yonatan Cohen3050b992016-09-07 14:04:06 +0300533}
534
535static void rollback_state(struct rxe_send_wqe *wqe,
536 struct rxe_qp *qp,
537 struct rxe_send_wqe *rollback_wqe,
Arnd Bergmanna0fa7262016-09-19 13:57:26 +0200538 u32 rollback_psn)
Yonatan Cohen3050b992016-09-07 14:04:06 +0300539{
540 wqe->state = rollback_wqe->state;
541 wqe->first_psn = rollback_wqe->first_psn;
542 wqe->last_psn = rollback_wqe->last_psn;
Arnd Bergmanna0fa7262016-09-19 13:57:26 +0200543 qp->req.psn = rollback_psn;
Yonatan Cohen3050b992016-09-07 14:04:06 +0300544}
545
546static void update_state(struct rxe_qp *qp, struct rxe_send_wqe *wqe,
547 struct rxe_pkt_info *pkt, int payload)
548{
Moni Shoua8700e3e2016-06-16 16:45:23 +0300549 qp->req.opcode = pkt->opcode;
550
Moni Shoua8700e3e2016-06-16 16:45:23 +0300551 if (pkt->mask & RXE_END_MASK)
Bob Pearsonae6e8432021-09-14 11:42:03 -0500552 qp->req.wqe_index = queue_next_index(qp->sq.queue,
553 qp->req.wqe_index);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300554
555 qp->need_req_skb = 0;
556
557 if (qp->qp_timeout_jiffies && !timer_pending(&qp->retrans_timer))
558 mod_timer(&qp->retrans_timer,
559 jiffies + qp->qp_timeout_jiffies);
560}
561
Bob Pearsonc1a41122021-06-07 23:25:49 -0500562static int rxe_do_local_ops(struct rxe_qp *qp, struct rxe_send_wqe *wqe)
563{
564 u8 opcode = wqe->wr.opcode;
Bob Pearsonc1a41122021-06-07 23:25:49 -0500565 u32 rkey;
Bob Pearson32a577b2021-06-07 23:25:50 -0500566 int ret;
Bob Pearsonc1a41122021-06-07 23:25:49 -0500567
568 switch (opcode) {
569 case IB_WR_LOCAL_INV:
Bob Pearsonc1a41122021-06-07 23:25:49 -0500570 rkey = wqe->wr.ex.invalidate_rkey;
Bob Pearson3902b422021-06-07 23:25:51 -0500571 if (rkey_is_mw(rkey))
572 ret = rxe_invalidate_mw(qp, rkey);
573 else
574 ret = rxe_invalidate_mr(qp, rkey);
575
576 if (unlikely(ret)) {
Bob Pearsonc1a41122021-06-07 23:25:49 -0500577 wqe->status = IB_WC_LOC_QP_OP_ERR;
Bob Pearson3902b422021-06-07 23:25:51 -0500578 return ret;
Bob Pearsonc1a41122021-06-07 23:25:49 -0500579 }
Bob Pearsonc1a41122021-06-07 23:25:49 -0500580 break;
581 case IB_WR_REG_MR:
Bob Pearson00134532021-09-14 11:42:05 -0500582 ret = rxe_reg_fast_mr(qp, wqe);
583 if (unlikely(ret)) {
584 wqe->status = IB_WC_LOC_QP_OP_ERR;
585 return ret;
586 }
Bob Pearsonc1a41122021-06-07 23:25:49 -0500587 break;
Bob Pearson32a577b2021-06-07 23:25:50 -0500588 case IB_WR_BIND_MW:
589 ret = rxe_bind_mw(qp, wqe);
590 if (unlikely(ret)) {
591 wqe->status = IB_WC_MW_BIND_ERR;
592 return ret;
593 }
594 break;
Bob Pearsonc1a41122021-06-07 23:25:49 -0500595 default:
596 pr_err("Unexpected send wqe opcode %d\n", opcode);
597 wqe->status = IB_WC_LOC_QP_OP_ERR;
598 return -EINVAL;
599 }
600
601 wqe->state = wqe_state_done;
602 wqe->status = IB_WC_SUCCESS;
Bob Pearsonae6e8432021-09-14 11:42:03 -0500603 qp->req.wqe_index = queue_next_index(qp->sq.queue, qp->req.wqe_index);
Bob Pearsonc1a41122021-06-07 23:25:49 -0500604
605 if ((wqe->wr.send_flags & IB_SEND_SIGNALED) ||
606 qp->sq_sig_type == IB_SIGNAL_ALL_WR)
607 rxe_run_task(&qp->comp.task, 1);
608
609 return 0;
610}
611
Moni Shoua8700e3e2016-06-16 16:45:23 +0300612int rxe_requester(void *arg)
613{
614 struct rxe_qp *qp = (struct rxe_qp *)arg;
615 struct rxe_pkt_info pkt;
616 struct sk_buff *skb;
617 struct rxe_send_wqe *wqe;
Parav Pandite404f942016-09-28 20:26:26 +0000618 enum rxe_hdr_mask mask;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300619 int payload;
620 int mtu;
621 int opcode;
622 int ret;
Yonatan Cohen3050b992016-09-07 14:04:06 +0300623 struct rxe_send_wqe rollback_wqe;
Arnd Bergmanna0fa7262016-09-19 13:57:26 +0200624 u32 rollback_psn;
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500625 struct rxe_queue *q = qp->sq.queue;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300626
Andrew Boyer37f69f42016-12-05 08:43:21 -0500627 rxe_add_ref(qp);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300628
629next_wqe:
Bart Van Assche65567e42018-01-12 15:11:58 -0800630 if (unlikely(!qp->valid || qp->req.state == QP_STATE_ERROR))
Moni Shoua8700e3e2016-06-16 16:45:23 +0300631 goto exit;
Bart Van Assche18d34512017-01-10 11:15:50 -0800632
Moni Shoua8700e3e2016-06-16 16:45:23 +0300633 if (unlikely(qp->req.state == QP_STATE_RESET)) {
Bob Pearsonae6e8432021-09-14 11:42:03 -0500634 qp->req.wqe_index = queue_get_consumer(q,
635 QUEUE_TYPE_FROM_CLIENT);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300636 qp->req.opcode = -1;
637 qp->req.need_rd_atomic = 0;
638 qp->req.wait_psn = 0;
639 qp->req.need_retry = 0;
640 goto exit;
641 }
642
643 if (unlikely(qp->req.need_retry)) {
644 req_retry(qp);
645 qp->req.need_retry = 0;
646 }
647
648 wqe = req_next_wqe(qp);
649 if (unlikely(!wqe))
650 goto exit;
651
Bob Pearson886441f2021-06-07 23:25:48 -0500652 if (wqe->mask & WR_LOCAL_OP_MASK) {
Bob Pearsonc1a41122021-06-07 23:25:49 -0500653 ret = rxe_do_local_ops(qp, wqe);
654 if (unlikely(ret))
655 goto err;
656 else
657 goto next_wqe;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300658 }
659
660 if (unlikely(qp_type(qp) == IB_QPT_RC &&
Bob Pearsonbb3ab292020-10-13 12:07:42 -0500661 psn_compare(qp->req.psn, (qp->comp.psn +
662 RXE_MAX_UNACKED_PSNS)) > 0)) {
Moni Shoua8700e3e2016-06-16 16:45:23 +0300663 qp->req.wait_psn = 1;
664 goto exit;
665 }
666
667 /* Limit the number of inflight SKBs per QP */
668 if (unlikely(atomic_read(&qp->skb_out) >
669 RXE_INFLIGHT_SKBS_PER_QP_HIGH)) {
670 qp->need_req_skb = 1;
671 goto exit;
672 }
673
674 opcode = next_opcode(qp, wqe, wqe->wr.opcode);
675 if (unlikely(opcode < 0)) {
676 wqe->status = IB_WC_LOC_QP_OP_ERR;
677 goto exit;
678 }
679
680 mask = rxe_opcode[opcode].mask;
Xiao Yang45216d62021-09-14 16:02:52 +0800681 if (unlikely(mask & RXE_READ_OR_ATOMIC_MASK)) {
Moni Shoua8700e3e2016-06-16 16:45:23 +0300682 if (check_init_depth(qp, wqe))
683 goto exit;
684 }
685
Bart Van Assche175f1242017-01-10 11:15:43 -0800686 mtu = get_mtu(qp);
Xiao Yang45216d62021-09-14 16:02:52 +0800687 payload = (mask & RXE_WRITE_OR_SEND_MASK) ? wqe->dma.resid : 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300688 if (payload > mtu) {
689 if (qp_type(qp) == IB_QPT_UD) {
690 /* C10-93.1.1: If the total sum of all the buffer lengths specified for a
691 * UD message exceeds the MTU of the port as returned by QueryHCA, the CI
692 * shall not emit any packets for this message. Further, the CI shall not
693 * generate an error due to this condition.
694 */
695
696 /* fake a successful UD send */
697 wqe->first_psn = qp->req.psn;
698 wqe->last_psn = qp->req.psn;
699 qp->req.psn = (qp->req.psn + 1) & BTH_PSN_MASK;
700 qp->req.opcode = IB_OPCODE_UD_SEND_ONLY;
Bob Pearsonae6e8432021-09-14 11:42:03 -0500701 qp->req.wqe_index = queue_next_index(qp->sq.queue,
Moni Shoua8700e3e2016-06-16 16:45:23 +0300702 qp->req.wqe_index);
703 wqe->state = wqe_state_done;
704 wqe->status = IB_WC_SUCCESS;
Yonatan Cohen002e0622016-11-16 10:39:15 +0200705 __rxe_do_task(&qp->comp.task);
Linus Torvalds4d5b57e2016-12-15 12:03:32 -0800706 rxe_drop_ref(qp);
Yonatan Cohen002e0622016-11-16 10:39:15 +0200707 return 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300708 }
709 payload = mtu;
710 }
711
712 skb = init_req_packet(qp, wqe, opcode, payload, &pkt);
713 if (unlikely(!skb)) {
Parav Pandite404f942016-09-28 20:26:26 +0000714 pr_err("qp#%d Failed allocating skb\n", qp_num(qp));
Bob Pearsonc1a41122021-06-07 23:25:49 -0500715 wqe->status = IB_WC_LOC_QP_OP_ERR;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300716 goto err;
717 }
718
Bob Pearsonc1a41122021-06-07 23:25:49 -0500719 ret = finish_packet(qp, wqe, &pkt, skb, payload);
720 if (unlikely(ret)) {
721 pr_debug("qp#%d Error during finish packet\n", qp_num(qp));
722 if (ret == -EFAULT)
723 wqe->status = IB_WC_LOC_PROT_ERR;
724 else
725 wqe->status = IB_WC_LOC_QP_OP_ERR;
Zhu Yanjun828d8102018-06-07 02:32:52 -0400726 kfree_skb(skb);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300727 goto err;
728 }
729
Yonatan Cohen3050b992016-09-07 14:04:06 +0300730 /*
731 * To prevent a race on wqe access between requester and completer,
732 * wqe members state and psn need to be set before calling
733 * rxe_xmit_packet().
734 * Otherwise, completer might initiate an unjustified retry flow.
735 */
Arnd Bergmanna0fa7262016-09-19 13:57:26 +0200736 save_state(wqe, qp, &rollback_wqe, &rollback_psn);
Yonatan Cohen3050b992016-09-07 14:04:06 +0300737 update_wqe_state(qp, wqe, &pkt);
738 update_wqe_psn(qp, wqe, &pkt, payload);
Zhu Yanjuna854b1e2018-11-03 08:13:18 -0400739 ret = rxe_xmit_packet(qp, &pkt, skb);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300740 if (ret) {
741 qp->need_req_skb = 1;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300742
Arnd Bergmanna0fa7262016-09-19 13:57:26 +0200743 rollback_state(wqe, qp, &rollback_wqe, rollback_psn);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300744
745 if (ret == -EAGAIN) {
746 rxe_run_task(&qp->req.task, 1);
747 goto exit;
748 }
749
Bob Pearsonc1a41122021-06-07 23:25:49 -0500750 wqe->status = IB_WC_LOC_QP_OP_ERR;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300751 goto err;
752 }
753
754 update_state(qp, wqe, &pkt, payload);
755
756 goto next_wqe;
757
758err:
Moni Shoua8700e3e2016-06-16 16:45:23 +0300759 wqe->state = wqe_state_error;
Yonatan Cohen002e0622016-11-16 10:39:15 +0200760 __rxe_do_task(&qp->comp.task);
Bart Van Assche18d34512017-01-10 11:15:50 -0800761
Moni Shoua8700e3e2016-06-16 16:45:23 +0300762exit:
Andrew Boyer37f69f42016-12-05 08:43:21 -0500763 rxe_drop_ref(qp);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300764 return -EAGAIN;
765}