1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/skbuff.h>
35
36#include "rxe.h"
37#include "rxe_loc.h"
38#include "rxe_queue.h"
39
40enum resp_states {
41 RESPST_NONE,
42 RESPST_GET_REQ,
43 RESPST_CHK_PSN,
44 RESPST_CHK_OP_SEQ,
45 RESPST_CHK_OP_VALID,
46 RESPST_CHK_RESOURCE,
47 RESPST_CHK_LENGTH,
48 RESPST_CHK_RKEY,
49 RESPST_EXECUTE,
50 RESPST_READ_REPLY,
51 RESPST_COMPLETE,
52 RESPST_ACKNOWLEDGE,
53 RESPST_CLEANUP,
54 RESPST_DUPLICATE_REQUEST,
55 RESPST_ERR_MALFORMED_WQE,
56 RESPST_ERR_UNSUPPORTED_OPCODE,
57 RESPST_ERR_MISALIGNED_ATOMIC,
58 RESPST_ERR_PSN_OUT_OF_SEQ,
59 RESPST_ERR_MISSING_OPCODE_FIRST,
60 RESPST_ERR_MISSING_OPCODE_LAST_C,
61 RESPST_ERR_MISSING_OPCODE_LAST_D1E,
62 RESPST_ERR_TOO_MANY_RDMA_ATM_REQ,
63 RESPST_ERR_RNR,
64 RESPST_ERR_RKEY_VIOLATION,
65 RESPST_ERR_LENGTH,
66 RESPST_ERR_CQ_OVERFLOW,
67 RESPST_ERROR,
68 RESPST_RESET,
69 RESPST_DONE,
70 RESPST_EXIT,
71};
72
73static char *resp_state_name[] = {
74 [RESPST_NONE] = "NONE",
75 [RESPST_GET_REQ] = "GET_REQ",
76 [RESPST_CHK_PSN] = "CHK_PSN",
77 [RESPST_CHK_OP_SEQ] = "CHK_OP_SEQ",
78 [RESPST_CHK_OP_VALID] = "CHK_OP_VALID",
79 [RESPST_CHK_RESOURCE] = "CHK_RESOURCE",
80 [RESPST_CHK_LENGTH] = "CHK_LENGTH",
81 [RESPST_CHK_RKEY] = "CHK_RKEY",
82 [RESPST_EXECUTE] = "EXECUTE",
83 [RESPST_READ_REPLY] = "READ_REPLY",
84 [RESPST_COMPLETE] = "COMPLETE",
85 [RESPST_ACKNOWLEDGE] = "ACKNOWLEDGE",
86 [RESPST_CLEANUP] = "CLEANUP",
87 [RESPST_DUPLICATE_REQUEST] = "DUPLICATE_REQUEST",
88 [RESPST_ERR_MALFORMED_WQE] = "ERR_MALFORMED_WQE",
89 [RESPST_ERR_UNSUPPORTED_OPCODE] = "ERR_UNSUPPORTED_OPCODE",
90 [RESPST_ERR_MISALIGNED_ATOMIC] = "ERR_MISALIGNED_ATOMIC",
91 [RESPST_ERR_PSN_OUT_OF_SEQ] = "ERR_PSN_OUT_OF_SEQ",
92 [RESPST_ERR_MISSING_OPCODE_FIRST] = "ERR_MISSING_OPCODE_FIRST",
93 [RESPST_ERR_MISSING_OPCODE_LAST_C] = "ERR_MISSING_OPCODE_LAST_C",
94 [RESPST_ERR_MISSING_OPCODE_LAST_D1E] = "ERR_MISSING_OPCODE_LAST_D1E",
95 [RESPST_ERR_TOO_MANY_RDMA_ATM_REQ] = "ERR_TOO_MANY_RDMA_ATM_REQ",
96 [RESPST_ERR_RNR] = "ERR_RNR",
97 [RESPST_ERR_RKEY_VIOLATION] = "ERR_RKEY_VIOLATION",
98 [RESPST_ERR_LENGTH] = "ERR_LENGTH",
99 [RESPST_ERR_CQ_OVERFLOW] = "ERR_CQ_OVERFLOW",
100 [RESPST_ERROR] = "ERROR",
101 [RESPST_RESET] = "RESET",
102 [RESPST_DONE] = "DONE",
103 [RESPST_EXIT] = "EXIT",
104};
105
106
107void rxe_resp_queue_pkt(struct rxe_dev *rxe, struct rxe_qp *qp,
108 struct sk_buff *skb)
109{
110 int must_sched;
111 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
112
113 skb_queue_tail(&qp->req_pkts, skb);
114
115 must_sched = (pkt->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST) ||
116 (skb_queue_len(&qp->req_pkts) > 1);
117
118 rxe_run_task(&qp->resp.task, must_sched);
119}
120
121static inline enum resp_states get_req(struct rxe_qp *qp,
122 struct rxe_pkt_info **pkt_p)
123{
124 struct sk_buff *skb;
125
126 if (qp->resp.state == QP_STATE_ERROR) {
127 skb = skb_dequeue(&qp->req_pkts);
128 if (skb) {
129
130 rxe_drop_ref(qp);
131 kfree_skb(skb);
132 return RESPST_GET_REQ;
133 }
134
135
136 return RESPST_CHK_RESOURCE;
137 }
138
139 skb = skb_peek(&qp->req_pkts);
140 if (!skb)
141 return RESPST_EXIT;
142
143 *pkt_p = SKB_TO_PKT(skb);
144
145 return (qp->resp.res) ? RESPST_READ_REPLY : RESPST_CHK_PSN;
146}
147
148static enum resp_states check_psn(struct rxe_qp *qp,
149 struct rxe_pkt_info *pkt)
150{
151 int diff = psn_compare(pkt->psn, qp->resp.psn);
152 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
153
154 switch (qp_type(qp)) {
155 case IB_QPT_RC:
156 if (diff > 0) {
157 if (qp->resp.sent_psn_nak)
158 return RESPST_CLEANUP;
159
160 qp->resp.sent_psn_nak = 1;
161 rxe_counter_inc(rxe, RXE_CNT_OUT_OF_SEQ_REQ);
162 return RESPST_ERR_PSN_OUT_OF_SEQ;
163
164 } else if (diff < 0) {
165 rxe_counter_inc(rxe, RXE_CNT_DUP_REQ);
166 return RESPST_DUPLICATE_REQUEST;
167 }
168
169 if (qp->resp.sent_psn_nak)
170 qp->resp.sent_psn_nak = 0;
171
172 break;
173
174 case IB_QPT_UC:
175 if (qp->resp.drop_msg || diff != 0) {
176 if (pkt->mask & RXE_START_MASK) {
177 qp->resp.drop_msg = 0;
178 return RESPST_CHK_OP_SEQ;
179 }
180
181 qp->resp.drop_msg = 1;
182 return RESPST_CLEANUP;
183 }
184 break;
185 default:
186 break;
187 }
188
189 return RESPST_CHK_OP_SEQ;
190}
191
192static enum resp_states check_op_seq(struct rxe_qp *qp,
193 struct rxe_pkt_info *pkt)
194{
195 switch (qp_type(qp)) {
196 case IB_QPT_RC:
197 switch (qp->resp.opcode) {
198 case IB_OPCODE_RC_SEND_FIRST:
199 case IB_OPCODE_RC_SEND_MIDDLE:
200 switch (pkt->opcode) {
201 case IB_OPCODE_RC_SEND_MIDDLE:
202 case IB_OPCODE_RC_SEND_LAST:
203 case IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE:
204 case IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE:
205 return RESPST_CHK_OP_VALID;
206 default:
207 return RESPST_ERR_MISSING_OPCODE_LAST_C;
208 }
209
210 case IB_OPCODE_RC_RDMA_WRITE_FIRST:
211 case IB_OPCODE_RC_RDMA_WRITE_MIDDLE:
212 switch (pkt->opcode) {
213 case IB_OPCODE_RC_RDMA_WRITE_MIDDLE:
214 case IB_OPCODE_RC_RDMA_WRITE_LAST:
215 case IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
216 return RESPST_CHK_OP_VALID;
217 default:
218 return RESPST_ERR_MISSING_OPCODE_LAST_C;
219 }
220
221 default:
222 switch (pkt->opcode) {
223 case IB_OPCODE_RC_SEND_MIDDLE:
224 case IB_OPCODE_RC_SEND_LAST:
225 case IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE:
226 case IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE:
227 case IB_OPCODE_RC_RDMA_WRITE_MIDDLE:
228 case IB_OPCODE_RC_RDMA_WRITE_LAST:
229 case IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
230 return RESPST_ERR_MISSING_OPCODE_FIRST;
231 default:
232 return RESPST_CHK_OP_VALID;
233 }
234 }
235 break;
236
237 case IB_QPT_UC:
238 switch (qp->resp.opcode) {
239 case IB_OPCODE_UC_SEND_FIRST:
240 case IB_OPCODE_UC_SEND_MIDDLE:
241 switch (pkt->opcode) {
242 case IB_OPCODE_UC_SEND_MIDDLE:
243 case IB_OPCODE_UC_SEND_LAST:
244 case IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE:
245 return RESPST_CHK_OP_VALID;
246 default:
247 return RESPST_ERR_MISSING_OPCODE_LAST_D1E;
248 }
249
250 case IB_OPCODE_UC_RDMA_WRITE_FIRST:
251 case IB_OPCODE_UC_RDMA_WRITE_MIDDLE:
252 switch (pkt->opcode) {
253 case IB_OPCODE_UC_RDMA_WRITE_MIDDLE:
254 case IB_OPCODE_UC_RDMA_WRITE_LAST:
255 case IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
256 return RESPST_CHK_OP_VALID;
257 default:
258 return RESPST_ERR_MISSING_OPCODE_LAST_D1E;
259 }
260
261 default:
262 switch (pkt->opcode) {
263 case IB_OPCODE_UC_SEND_MIDDLE:
264 case IB_OPCODE_UC_SEND_LAST:
265 case IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE:
266 case IB_OPCODE_UC_RDMA_WRITE_MIDDLE:
267 case IB_OPCODE_UC_RDMA_WRITE_LAST:
268 case IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
269 qp->resp.drop_msg = 1;
270 return RESPST_CLEANUP;
271 default:
272 return RESPST_CHK_OP_VALID;
273 }
274 }
275 break;
276
277 default:
278 return RESPST_CHK_OP_VALID;
279 }
280}
281
282static enum resp_states check_op_valid(struct rxe_qp *qp,
283 struct rxe_pkt_info *pkt)
284{
285 switch (qp_type(qp)) {
286 case IB_QPT_RC:
287 if (((pkt->mask & RXE_READ_MASK) &&
288 !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_READ)) ||
289 ((pkt->mask & RXE_WRITE_MASK) &&
290 !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_WRITE)) ||
291 ((pkt->mask & RXE_ATOMIC_MASK) &&
292 !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_ATOMIC))) {
293 return RESPST_ERR_UNSUPPORTED_OPCODE;
294 }
295
296 break;
297
298 case IB_QPT_UC:
299 if ((pkt->mask & RXE_WRITE_MASK) &&
300 !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_WRITE)) {
301 qp->resp.drop_msg = 1;
302 return RESPST_CLEANUP;
303 }
304
305 break;
306
307 case IB_QPT_UD:
308 case IB_QPT_SMI:
309 case IB_QPT_GSI:
310 break;
311
312 default:
313 WARN_ON_ONCE(1);
314 break;
315 }
316
317 return RESPST_CHK_RESOURCE;
318}
319
320static enum resp_states get_srq_wqe(struct rxe_qp *qp)
321{
322 struct rxe_srq *srq = qp->srq;
323 struct rxe_queue *q = srq->rq.queue;
324 struct rxe_recv_wqe *wqe;
325 struct ib_event ev;
326
327 if (srq->error)
328 return RESPST_ERR_RNR;
329
330 spin_lock_bh(&srq->rq.consumer_lock);
331
332 wqe = queue_head(q);
333 if (!wqe) {
334 spin_unlock_bh(&srq->rq.consumer_lock);
335 return RESPST_ERR_RNR;
336 }
337
338
339 memcpy(&qp->resp.srq_wqe, wqe, sizeof(qp->resp.srq_wqe));
340
341 qp->resp.wqe = &qp->resp.srq_wqe.wqe;
342 advance_consumer(q);
343
344 if (srq->limit && srq->ibsrq.event_handler &&
345 (queue_count(q) < srq->limit)) {
346 srq->limit = 0;
347 goto event;
348 }
349
350 spin_unlock_bh(&srq->rq.consumer_lock);
351 return RESPST_CHK_LENGTH;
352
353event:
354 spin_unlock_bh(&srq->rq.consumer_lock);
355 ev.device = qp->ibqp.device;
356 ev.element.srq = qp->ibqp.srq;
357 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
358 srq->ibsrq.event_handler(&ev, srq->ibsrq.srq_context);
359 return RESPST_CHK_LENGTH;
360}
361
362static enum resp_states check_resource(struct rxe_qp *qp,
363 struct rxe_pkt_info *pkt)
364{
365 struct rxe_srq *srq = qp->srq;
366
367 if (qp->resp.state == QP_STATE_ERROR) {
368 if (qp->resp.wqe) {
369 qp->resp.status = IB_WC_WR_FLUSH_ERR;
370 return RESPST_COMPLETE;
371 } else if (!srq) {
372 qp->resp.wqe = queue_head(qp->rq.queue);
373 if (qp->resp.wqe) {
374 qp->resp.status = IB_WC_WR_FLUSH_ERR;
375 return RESPST_COMPLETE;
376 } else {
377 return RESPST_EXIT;
378 }
379 } else {
380 return RESPST_EXIT;
381 }
382 }
383
384 if (pkt->mask & RXE_READ_OR_ATOMIC) {
385
386
387
388
389 if (likely(qp->attr.max_dest_rd_atomic > 0))
390 return RESPST_CHK_LENGTH;
391 else
392 return RESPST_ERR_TOO_MANY_RDMA_ATM_REQ;
393 }
394
395 if (pkt->mask & RXE_RWR_MASK) {
396 if (srq)
397 return get_srq_wqe(qp);
398
399 qp->resp.wqe = queue_head(qp->rq.queue);
400 return (qp->resp.wqe) ? RESPST_CHK_LENGTH : RESPST_ERR_RNR;
401 }
402
403 return RESPST_CHK_LENGTH;
404}
405
406static enum resp_states check_length(struct rxe_qp *qp,
407 struct rxe_pkt_info *pkt)
408{
409 switch (qp_type(qp)) {
410 case IB_QPT_RC:
411 return RESPST_CHK_RKEY;
412
413 case IB_QPT_UC:
414 return RESPST_CHK_RKEY;
415
416 default:
417 return RESPST_CHK_RKEY;
418 }
419}
420
421static enum resp_states check_rkey(struct rxe_qp *qp,
422 struct rxe_pkt_info *pkt)
423{
424 struct rxe_mem *mem = NULL;
425 u64 va;
426 u32 rkey;
427 u32 resid;
428 u32 pktlen;
429 int mtu = qp->mtu;
430 enum resp_states state;
431 int access;
432
433 if (pkt->mask & (RXE_READ_MASK | RXE_WRITE_MASK)) {
434 if (pkt->mask & RXE_RETH_MASK) {
435 qp->resp.va = reth_va(pkt);
436 qp->resp.rkey = reth_rkey(pkt);
437 qp->resp.resid = reth_len(pkt);
438 }
439 access = (pkt->mask & RXE_READ_MASK) ? IB_ACCESS_REMOTE_READ
440 : IB_ACCESS_REMOTE_WRITE;
441 } else if (pkt->mask & RXE_ATOMIC_MASK) {
442 qp->resp.va = atmeth_va(pkt);
443 qp->resp.rkey = atmeth_rkey(pkt);
444 qp->resp.resid = sizeof(u64);
445 access = IB_ACCESS_REMOTE_ATOMIC;
446 } else {
447 return RESPST_EXECUTE;
448 }
449
450
451 if ((pkt->mask & (RXE_READ_MASK | RXE_WRITE_OR_SEND)) &&
452 (pkt->mask & RXE_RETH_MASK) &&
453 reth_len(pkt) == 0) {
454 return RESPST_EXECUTE;
455 }
456
457 va = qp->resp.va;
458 rkey = qp->resp.rkey;
459 resid = qp->resp.resid;
460 pktlen = payload_size(pkt);
461
462 mem = lookup_mem(qp->pd, access, rkey, lookup_remote);
463 if (!mem) {
464 state = RESPST_ERR_RKEY_VIOLATION;
465 goto err;
466 }
467
468 if (unlikely(mem->state == RXE_MEM_STATE_FREE)) {
469 state = RESPST_ERR_RKEY_VIOLATION;
470 goto err;
471 }
472
473 if (mem_check_range(mem, va, resid)) {
474 state = RESPST_ERR_RKEY_VIOLATION;
475 goto err;
476 }
477
478 if (pkt->mask & RXE_WRITE_MASK) {
479 if (resid > mtu) {
480 if (pktlen != mtu || bth_pad(pkt)) {
481 state = RESPST_ERR_LENGTH;
482 goto err;
483 }
484 } else {
485 if (pktlen != resid) {
486 state = RESPST_ERR_LENGTH;
487 goto err;
488 }
489 if ((bth_pad(pkt) != (0x3 & (-resid)))) {
490
491
492
493 state = RESPST_ERR_LENGTH;
494 goto err;
495 }
496 }
497 }
498
499 WARN_ON_ONCE(qp->resp.mr);
500
501 qp->resp.mr = mem;
502 return RESPST_EXECUTE;
503
504err:
505 if (mem)
506 rxe_drop_ref(mem);
507 return state;
508}
509
510static enum resp_states send_data_in(struct rxe_qp *qp, void *data_addr,
511 int data_len)
512{
513 int err;
514
515 err = copy_data(qp->pd, IB_ACCESS_LOCAL_WRITE, &qp->resp.wqe->dma,
516 data_addr, data_len, to_mem_obj, NULL);
517 if (unlikely(err))
518 return (err == -ENOSPC) ? RESPST_ERR_LENGTH
519 : RESPST_ERR_MALFORMED_WQE;
520
521 return RESPST_NONE;
522}
523
524static enum resp_states write_data_in(struct rxe_qp *qp,
525 struct rxe_pkt_info *pkt)
526{
527 enum resp_states rc = RESPST_NONE;
528 int err;
529 int data_len = payload_size(pkt);
530
531 err = rxe_mem_copy(qp->resp.mr, qp->resp.va, payload_addr(pkt),
532 data_len, to_mem_obj, NULL);
533 if (err) {
534 rc = RESPST_ERR_RKEY_VIOLATION;
535 goto out;
536 }
537
538 qp->resp.va += data_len;
539 qp->resp.resid -= data_len;
540
541out:
542 return rc;
543}
544
545
546static DEFINE_SPINLOCK(atomic_ops_lock);
547
548static enum resp_states process_atomic(struct rxe_qp *qp,
549 struct rxe_pkt_info *pkt)
550{
551 u64 iova = atmeth_va(pkt);
552 u64 *vaddr;
553 enum resp_states ret;
554 struct rxe_mem *mr = qp->resp.mr;
555
556 if (mr->state != RXE_MEM_STATE_VALID) {
557 ret = RESPST_ERR_RKEY_VIOLATION;
558 goto out;
559 }
560
561 vaddr = iova_to_vaddr(mr, iova, sizeof(u64));
562
563
564 if (!vaddr || (uintptr_t)vaddr & 7) {
565 ret = RESPST_ERR_MISALIGNED_ATOMIC;
566 goto out;
567 }
568
569 spin_lock_bh(&atomic_ops_lock);
570
571 qp->resp.atomic_orig = *vaddr;
572
573 if (pkt->opcode == IB_OPCODE_RC_COMPARE_SWAP ||
574 pkt->opcode == IB_OPCODE_RD_COMPARE_SWAP) {
575 if (*vaddr == atmeth_comp(pkt))
576 *vaddr = atmeth_swap_add(pkt);
577 } else {
578 *vaddr += atmeth_swap_add(pkt);
579 }
580
581 spin_unlock_bh(&atomic_ops_lock);
582
583 ret = RESPST_NONE;
584out:
585 return ret;
586}
587
588static struct sk_buff *prepare_ack_packet(struct rxe_qp *qp,
589 struct rxe_pkt_info *pkt,
590 struct rxe_pkt_info *ack,
591 int opcode,
592 int payload,
593 u32 psn,
594 u8 syndrome,
595 u32 *crcp)
596{
597 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
598 struct sk_buff *skb;
599 u32 crc = 0;
600 u32 *p;
601 int paylen;
602 int pad;
603 int err;
604
605
606
607
608 pad = (-payload) & 0x3;
609 paylen = rxe_opcode[opcode].length + payload + pad + RXE_ICRC_SIZE;
610
611 skb = rxe_init_packet(rxe, &qp->pri_av, paylen, ack);
612 if (!skb)
613 return NULL;
614
615 ack->qp = qp;
616 ack->opcode = opcode;
617 ack->mask = rxe_opcode[opcode].mask;
618 ack->offset = pkt->offset;
619 ack->paylen = paylen;
620
621
622 memcpy(ack->hdr, pkt->hdr, pkt->offset + RXE_BTH_BYTES);
623
624 bth_set_opcode(ack, opcode);
625 bth_set_qpn(ack, qp->attr.dest_qp_num);
626 bth_set_pad(ack, pad);
627 bth_set_se(ack, 0);
628 bth_set_psn(ack, psn);
629 bth_set_ack(ack, 0);
630 ack->psn = psn;
631
632 if (ack->mask & RXE_AETH_MASK) {
633 aeth_set_syn(ack, syndrome);
634 aeth_set_msn(ack, qp->resp.msn);
635 }
636
637 if (ack->mask & RXE_ATMACK_MASK)
638 atmack_set_orig(ack, qp->resp.atomic_orig);
639
640 err = rxe_prepare(rxe, ack, skb, &crc);
641 if (err) {
642 kfree_skb(skb);
643 return NULL;
644 }
645
646 if (crcp) {
647
648 *crcp = crc;
649 } else {
650 p = payload_addr(ack) + payload + bth_pad(ack);
651 *p = ~crc;
652 }
653
654 return skb;
655}
656
657
658
659
660static enum resp_states read_reply(struct rxe_qp *qp,
661 struct rxe_pkt_info *req_pkt)
662{
663 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
664 struct rxe_pkt_info ack_pkt;
665 struct sk_buff *skb;
666 int mtu = qp->mtu;
667 enum resp_states state;
668 int payload;
669 int opcode;
670 int err;
671 struct resp_res *res = qp->resp.res;
672 u32 icrc;
673 u32 *p;
674
675 if (!res) {
676
677
678
679 res = &qp->resp.resources[qp->resp.res_head];
680
681 free_rd_atomic_resource(qp, res);
682 rxe_advance_resp_resource(qp);
683
684 res->type = RXE_READ_MASK;
685
686 res->read.va = qp->resp.va;
687 res->read.va_org = qp->resp.va;
688
689 res->first_psn = req_pkt->psn;
690
691 if (reth_len(req_pkt)) {
692 res->last_psn = (req_pkt->psn +
693 (reth_len(req_pkt) + mtu - 1) /
694 mtu - 1) & BTH_PSN_MASK;
695 } else {
696 res->last_psn = res->first_psn;
697 }
698 res->cur_psn = req_pkt->psn;
699
700 res->read.resid = qp->resp.resid;
701 res->read.length = qp->resp.resid;
702 res->read.rkey = qp->resp.rkey;
703
704
705 res->read.mr = qp->resp.mr;
706 qp->resp.mr = NULL;
707
708 qp->resp.res = res;
709 res->state = rdatm_res_state_new;
710 }
711
712 if (res->state == rdatm_res_state_new) {
713 if (res->read.resid <= mtu)
714 opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY;
715 else
716 opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST;
717 } else {
718 if (res->read.resid > mtu)
719 opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE;
720 else
721 opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST;
722 }
723
724 res->state = rdatm_res_state_next;
725
726 payload = min_t(int, res->read.resid, mtu);
727
728 skb = prepare_ack_packet(qp, req_pkt, &ack_pkt, opcode, payload,
729 res->cur_psn, AETH_ACK_UNLIMITED, &icrc);
730 if (!skb)
731 return RESPST_ERR_RNR;
732
733 err = rxe_mem_copy(res->read.mr, res->read.va, payload_addr(&ack_pkt),
734 payload, from_mem_obj, &icrc);
735 if (err)
736 pr_err("Failed copying memory\n");
737
738 p = payload_addr(&ack_pkt) + payload + bth_pad(&ack_pkt);
739 *p = ~icrc;
740
741 err = rxe_xmit_packet(rxe, qp, &ack_pkt, skb);
742 if (err) {
743 pr_err("Failed sending RDMA reply.\n");
744 return RESPST_ERR_RNR;
745 }
746
747 res->read.va += payload;
748 res->read.resid -= payload;
749 res->cur_psn = (res->cur_psn + 1) & BTH_PSN_MASK;
750
751 if (res->read.resid > 0) {
752 state = RESPST_DONE;
753 } else {
754 qp->resp.res = NULL;
755 qp->resp.opcode = -1;
756 if (psn_compare(res->cur_psn, qp->resp.psn) >= 0)
757 qp->resp.psn = res->cur_psn;
758 state = RESPST_CLEANUP;
759 }
760
761 return state;
762}
763
764static void build_rdma_network_hdr(union rdma_network_hdr *hdr,
765 struct rxe_pkt_info *pkt)
766{
767 struct sk_buff *skb = PKT_TO_SKB(pkt);
768
769 memset(hdr, 0, sizeof(*hdr));
770 if (skb->protocol == htons(ETH_P_IP))
771 memcpy(&hdr->roce4grh, ip_hdr(skb), sizeof(hdr->roce4grh));
772 else if (skb->protocol == htons(ETH_P_IPV6))
773 memcpy(&hdr->ibgrh, ipv6_hdr(skb), sizeof(hdr->ibgrh));
774}
775
776
777
778
779static enum resp_states execute(struct rxe_qp *qp, struct rxe_pkt_info *pkt)
780{
781 enum resp_states err;
782
783 if (pkt->mask & RXE_SEND_MASK) {
784 if (qp_type(qp) == IB_QPT_UD ||
785 qp_type(qp) == IB_QPT_SMI ||
786 qp_type(qp) == IB_QPT_GSI) {
787 union rdma_network_hdr hdr;
788
789 build_rdma_network_hdr(&hdr, pkt);
790
791 err = send_data_in(qp, &hdr, sizeof(hdr));
792 if (err)
793 return err;
794 }
795 err = send_data_in(qp, payload_addr(pkt), payload_size(pkt));
796 if (err)
797 return err;
798 } else if (pkt->mask & RXE_WRITE_MASK) {
799 err = write_data_in(qp, pkt);
800 if (err)
801 return err;
802 } else if (pkt->mask & RXE_READ_MASK) {
803
804 qp->resp.msn++;
805 return RESPST_READ_REPLY;
806 } else if (pkt->mask & RXE_ATOMIC_MASK) {
807 err = process_atomic(qp, pkt);
808 if (err)
809 return err;
810 } else {
811
812 WARN_ON_ONCE(1);
813 }
814
815
816 qp->resp.psn = (pkt->psn + 1) & BTH_PSN_MASK;
817
818 qp->resp.opcode = pkt->opcode;
819 qp->resp.status = IB_WC_SUCCESS;
820
821 if (pkt->mask & RXE_COMP_MASK) {
822
823 qp->resp.msn++;
824 return RESPST_COMPLETE;
825 } else if (qp_type(qp) == IB_QPT_RC)
826 return RESPST_ACKNOWLEDGE;
827 else
828 return RESPST_CLEANUP;
829}
830
831static enum resp_states do_complete(struct rxe_qp *qp,
832 struct rxe_pkt_info *pkt)
833{
834 struct rxe_cqe cqe;
835 struct ib_wc *wc = &cqe.ibwc;
836 struct ib_uverbs_wc *uwc = &cqe.uibwc;
837 struct rxe_recv_wqe *wqe = qp->resp.wqe;
838
839 if (unlikely(!wqe))
840 return RESPST_CLEANUP;
841
842 memset(&cqe, 0, sizeof(cqe));
843
844 wc->wr_id = wqe->wr_id;
845 wc->status = qp->resp.status;
846 wc->qp = &qp->ibqp;
847
848
849 if (wc->status == IB_WC_SUCCESS) {
850 wc->opcode = (pkt->mask & RXE_IMMDT_MASK &&
851 pkt->mask & RXE_WRITE_MASK) ?
852 IB_WC_RECV_RDMA_WITH_IMM : IB_WC_RECV;
853 wc->vendor_err = 0;
854 wc->byte_len = wqe->dma.length - wqe->dma.resid;
855
856
857
858
859 if (qp->rcq->is_user) {
860 uwc->wc_flags = IB_WC_GRH;
861
862 if (pkt->mask & RXE_IMMDT_MASK) {
863 uwc->wc_flags |= IB_WC_WITH_IMM;
864 uwc->ex.imm_data = immdt_imm(pkt);
865 }
866
867 if (pkt->mask & RXE_IETH_MASK) {
868 uwc->wc_flags |= IB_WC_WITH_INVALIDATE;
869 uwc->ex.invalidate_rkey = ieth_rkey(pkt);
870 }
871
872 uwc->qp_num = qp->ibqp.qp_num;
873
874 if (pkt->mask & RXE_DETH_MASK)
875 uwc->src_qp = deth_sqp(pkt);
876
877 uwc->port_num = qp->attr.port_num;
878 } else {
879 struct sk_buff *skb = PKT_TO_SKB(pkt);
880
881 wc->wc_flags = IB_WC_GRH | IB_WC_WITH_NETWORK_HDR_TYPE;
882 if (skb->protocol == htons(ETH_P_IP))
883 wc->network_hdr_type = RDMA_NETWORK_IPV4;
884 else
885 wc->network_hdr_type = RDMA_NETWORK_IPV6;
886
887 if (is_vlan_dev(skb->dev)) {
888 wc->wc_flags |= IB_WC_WITH_VLAN;
889 wc->vlan_id = vlan_dev_vlan_id(skb->dev);
890 }
891
892 if (pkt->mask & RXE_IMMDT_MASK) {
893 wc->wc_flags |= IB_WC_WITH_IMM;
894 wc->ex.imm_data = immdt_imm(pkt);
895 }
896
897 if (pkt->mask & RXE_IETH_MASK) {
898 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
899 struct rxe_mem *rmr;
900
901 wc->wc_flags |= IB_WC_WITH_INVALIDATE;
902 wc->ex.invalidate_rkey = ieth_rkey(pkt);
903
904 rmr = rxe_pool_get_index(&rxe->mr_pool,
905 wc->ex.invalidate_rkey >> 8);
906 if (unlikely(!rmr)) {
907 pr_err("Bad rkey %#x invalidation\n",
908 wc->ex.invalidate_rkey);
909 return RESPST_ERROR;
910 }
911 rmr->state = RXE_MEM_STATE_FREE;
912 rxe_drop_ref(rmr);
913 }
914
915 wc->qp = &qp->ibqp;
916
917 if (pkt->mask & RXE_DETH_MASK)
918 wc->src_qp = deth_sqp(pkt);
919
920 wc->port_num = qp->attr.port_num;
921 }
922 }
923
924
925 if (!qp->srq)
926 advance_consumer(qp->rq.queue);
927
928 qp->resp.wqe = NULL;
929
930 if (rxe_cq_post(qp->rcq, &cqe, pkt ? bth_se(pkt) : 1))
931 return RESPST_ERR_CQ_OVERFLOW;
932
933 if (qp->resp.state == QP_STATE_ERROR)
934 return RESPST_CHK_RESOURCE;
935
936 if (!pkt)
937 return RESPST_DONE;
938 else if (qp_type(qp) == IB_QPT_RC)
939 return RESPST_ACKNOWLEDGE;
940 else
941 return RESPST_CLEANUP;
942}
943
944static int send_ack(struct rxe_qp *qp, struct rxe_pkt_info *pkt,
945 u8 syndrome, u32 psn)
946{
947 int err = 0;
948 struct rxe_pkt_info ack_pkt;
949 struct sk_buff *skb;
950 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
951
952 skb = prepare_ack_packet(qp, pkt, &ack_pkt, IB_OPCODE_RC_ACKNOWLEDGE,
953 0, psn, syndrome, NULL);
954 if (!skb) {
955 err = -ENOMEM;
956 goto err1;
957 }
958
959 err = rxe_xmit_packet(rxe, qp, &ack_pkt, skb);
960 if (err)
961 pr_err_ratelimited("Failed sending ack\n");
962
963err1:
964 return err;
965}
966
967static int send_atomic_ack(struct rxe_qp *qp, struct rxe_pkt_info *pkt,
968 u8 syndrome)
969{
970 int rc = 0;
971 struct rxe_pkt_info ack_pkt;
972 struct sk_buff *skb;
973 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
974 struct resp_res *res;
975
976 skb = prepare_ack_packet(qp, pkt, &ack_pkt,
977 IB_OPCODE_RC_ATOMIC_ACKNOWLEDGE, 0, pkt->psn,
978 syndrome, NULL);
979 if (!skb) {
980 rc = -ENOMEM;
981 goto out;
982 }
983
984 rxe_add_ref(qp);
985
986 res = &qp->resp.resources[qp->resp.res_head];
987 free_rd_atomic_resource(qp, res);
988 rxe_advance_resp_resource(qp);
989
990 memcpy(SKB_TO_PKT(skb), &ack_pkt, sizeof(ack_pkt));
991 memset((unsigned char *)SKB_TO_PKT(skb) + sizeof(ack_pkt), 0,
992 sizeof(skb->cb) - sizeof(ack_pkt));
993
994 skb_get(skb);
995 res->type = RXE_ATOMIC_MASK;
996 res->atomic.skb = skb;
997 res->first_psn = ack_pkt.psn;
998 res->last_psn = ack_pkt.psn;
999 res->cur_psn = ack_pkt.psn;
1000
1001 rc = rxe_xmit_packet(rxe, qp, &ack_pkt, skb);
1002 if (rc) {
1003 pr_err_ratelimited("Failed sending ack\n");
1004 rxe_drop_ref(qp);
1005 }
1006out:
1007 return rc;
1008}
1009
1010static enum resp_states acknowledge(struct rxe_qp *qp,
1011 struct rxe_pkt_info *pkt)
1012{
1013 if (qp_type(qp) != IB_QPT_RC)
1014 return RESPST_CLEANUP;
1015
1016 if (qp->resp.aeth_syndrome != AETH_ACK_UNLIMITED)
1017 send_ack(qp, pkt, qp->resp.aeth_syndrome, pkt->psn);
1018 else if (pkt->mask & RXE_ATOMIC_MASK)
1019 send_atomic_ack(qp, pkt, AETH_ACK_UNLIMITED);
1020 else if (bth_ack(pkt))
1021 send_ack(qp, pkt, AETH_ACK_UNLIMITED, pkt->psn);
1022
1023 return RESPST_CLEANUP;
1024}
1025
1026static enum resp_states cleanup(struct rxe_qp *qp,
1027 struct rxe_pkt_info *pkt)
1028{
1029 struct sk_buff *skb;
1030
1031 if (pkt) {
1032 skb = skb_dequeue(&qp->req_pkts);
1033 rxe_drop_ref(qp);
1034 kfree_skb(skb);
1035 }
1036
1037 if (qp->resp.mr) {
1038 rxe_drop_ref(qp->resp.mr);
1039 qp->resp.mr = NULL;
1040 }
1041
1042 return RESPST_DONE;
1043}
1044
1045static struct resp_res *find_resource(struct rxe_qp *qp, u32 psn)
1046{
1047 int i;
1048
1049 for (i = 0; i < qp->attr.max_dest_rd_atomic; i++) {
1050 struct resp_res *res = &qp->resp.resources[i];
1051
1052 if (res->type == 0)
1053 continue;
1054
1055 if (psn_compare(psn, res->first_psn) >= 0 &&
1056 psn_compare(psn, res->last_psn) <= 0) {
1057 return res;
1058 }
1059 }
1060
1061 return NULL;
1062}
1063
1064static enum resp_states duplicate_request(struct rxe_qp *qp,
1065 struct rxe_pkt_info *pkt)
1066{
1067 enum resp_states rc;
1068 u32 prev_psn = (qp->resp.psn - 1) & BTH_PSN_MASK;
1069
1070 if (pkt->mask & RXE_SEND_MASK ||
1071 pkt->mask & RXE_WRITE_MASK) {
1072
1073 if (bth_ack(pkt))
1074 send_ack(qp, pkt, AETH_ACK_UNLIMITED, prev_psn);
1075 rc = RESPST_CLEANUP;
1076 goto out;
1077 } else if (pkt->mask & RXE_READ_MASK) {
1078 struct resp_res *res;
1079
1080 res = find_resource(qp, pkt->psn);
1081 if (!res) {
1082
1083
1084
1085 rc = RESPST_CLEANUP;
1086 goto out;
1087 } else {
1088
1089
1090
1091 u64 iova = reth_va(pkt);
1092 u32 resid = reth_len(pkt);
1093
1094 if (iova < res->read.va_org ||
1095 resid > res->read.length ||
1096 (iova + resid) > (res->read.va_org +
1097 res->read.length)) {
1098 rc = RESPST_CLEANUP;
1099 goto out;
1100 }
1101
1102 if (reth_rkey(pkt) != res->read.rkey) {
1103 rc = RESPST_CLEANUP;
1104 goto out;
1105 }
1106
1107 res->cur_psn = pkt->psn;
1108 res->state = (pkt->psn == res->first_psn) ?
1109 rdatm_res_state_new :
1110 rdatm_res_state_replay;
1111
1112
1113 res->read.va_org = iova;
1114 res->read.va = iova;
1115 res->read.resid = resid;
1116
1117
1118 qp->resp.res = res;
1119 rc = RESPST_READ_REPLY;
1120 goto out;
1121 }
1122 } else {
1123 struct resp_res *res;
1124
1125
1126 res = find_resource(qp, pkt->psn);
1127 if (res) {
1128 skb_get(res->atomic.skb);
1129
1130 rc = rxe_xmit_packet(to_rdev(qp->ibqp.device), qp,
1131 pkt, res->atomic.skb);
1132 if (rc) {
1133 pr_err("Failed resending result. This flow is not handled - skb ignored\n");
1134 rc = RESPST_CLEANUP;
1135 goto out;
1136 }
1137 }
1138
1139
1140 rc = RESPST_CLEANUP;
1141 goto out;
1142 }
1143out:
1144 return rc;
1145}
1146
1147
1148static void do_class_ac_error(struct rxe_qp *qp, u8 syndrome,
1149 enum ib_wc_status status)
1150{
1151 qp->resp.aeth_syndrome = syndrome;
1152 qp->resp.status = status;
1153
1154
1155 qp->resp.goto_error = 1;
1156}
1157
1158static enum resp_states do_class_d1e_error(struct rxe_qp *qp)
1159{
1160
1161 if (qp->srq) {
1162
1163 qp->resp.drop_msg = 1;
1164 if (qp->resp.wqe) {
1165 qp->resp.status = IB_WC_REM_INV_REQ_ERR;
1166 return RESPST_COMPLETE;
1167 } else {
1168 return RESPST_CLEANUP;
1169 }
1170 } else {
1171
1172
1173
1174
1175
1176 if (qp->resp.wqe) {
1177 qp->resp.wqe->dma.resid = qp->resp.wqe->dma.length;
1178 qp->resp.wqe->dma.cur_sge = 0;
1179 qp->resp.wqe->dma.sge_offset = 0;
1180 qp->resp.opcode = -1;
1181 }
1182
1183 if (qp->resp.mr) {
1184 rxe_drop_ref(qp->resp.mr);
1185 qp->resp.mr = NULL;
1186 }
1187
1188 return RESPST_CLEANUP;
1189 }
1190}
1191
1192static void rxe_drain_req_pkts(struct rxe_qp *qp, bool notify)
1193{
1194 struct sk_buff *skb;
1195
1196 while ((skb = skb_dequeue(&qp->req_pkts))) {
1197 rxe_drop_ref(qp);
1198 kfree_skb(skb);
1199 }
1200
1201 if (notify)
1202 return;
1203
1204 while (!qp->srq && qp->rq.queue && queue_head(qp->rq.queue))
1205 advance_consumer(qp->rq.queue);
1206}
1207
1208int rxe_responder(void *arg)
1209{
1210 struct rxe_qp *qp = (struct rxe_qp *)arg;
1211 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
1212 enum resp_states state;
1213 struct rxe_pkt_info *pkt = NULL;
1214 int ret = 0;
1215
1216 rxe_add_ref(qp);
1217
1218 qp->resp.aeth_syndrome = AETH_ACK_UNLIMITED;
1219
1220 if (!qp->valid) {
1221 ret = -EINVAL;
1222 goto done;
1223 }
1224
1225 switch (qp->resp.state) {
1226 case QP_STATE_RESET:
1227 state = RESPST_RESET;
1228 break;
1229
1230 default:
1231 state = RESPST_GET_REQ;
1232 break;
1233 }
1234
1235 while (1) {
1236 pr_debug("qp#%d state = %s\n", qp_num(qp),
1237 resp_state_name[state]);
1238 switch (state) {
1239 case RESPST_GET_REQ:
1240 state = get_req(qp, &pkt);
1241 break;
1242 case RESPST_CHK_PSN:
1243 state = check_psn(qp, pkt);
1244 break;
1245 case RESPST_CHK_OP_SEQ:
1246 state = check_op_seq(qp, pkt);
1247 break;
1248 case RESPST_CHK_OP_VALID:
1249 state = check_op_valid(qp, pkt);
1250 break;
1251 case RESPST_CHK_RESOURCE:
1252 state = check_resource(qp, pkt);
1253 break;
1254 case RESPST_CHK_LENGTH:
1255 state = check_length(qp, pkt);
1256 break;
1257 case RESPST_CHK_RKEY:
1258 state = check_rkey(qp, pkt);
1259 break;
1260 case RESPST_EXECUTE:
1261 state = execute(qp, pkt);
1262 break;
1263 case RESPST_COMPLETE:
1264 state = do_complete(qp, pkt);
1265 break;
1266 case RESPST_READ_REPLY:
1267 state = read_reply(qp, pkt);
1268 break;
1269 case RESPST_ACKNOWLEDGE:
1270 state = acknowledge(qp, pkt);
1271 break;
1272 case RESPST_CLEANUP:
1273 state = cleanup(qp, pkt);
1274 break;
1275 case RESPST_DUPLICATE_REQUEST:
1276 state = duplicate_request(qp, pkt);
1277 break;
1278 case RESPST_ERR_PSN_OUT_OF_SEQ:
1279
1280 send_ack(qp, pkt, AETH_NAK_PSN_SEQ_ERROR, qp->resp.psn);
1281 state = RESPST_CLEANUP;
1282 break;
1283
1284 case RESPST_ERR_TOO_MANY_RDMA_ATM_REQ:
1285 case RESPST_ERR_MISSING_OPCODE_FIRST:
1286 case RESPST_ERR_MISSING_OPCODE_LAST_C:
1287 case RESPST_ERR_UNSUPPORTED_OPCODE:
1288 case RESPST_ERR_MISALIGNED_ATOMIC:
1289
1290 do_class_ac_error(qp, AETH_NAK_INVALID_REQ,
1291 IB_WC_REM_INV_REQ_ERR);
1292 state = RESPST_COMPLETE;
1293 break;
1294
1295 case RESPST_ERR_MISSING_OPCODE_LAST_D1E:
1296 state = do_class_d1e_error(qp);
1297 break;
1298 case RESPST_ERR_RNR:
1299 if (qp_type(qp) == IB_QPT_RC) {
1300 rxe_counter_inc(rxe, RXE_CNT_SND_RNR);
1301
1302 send_ack(qp, pkt, AETH_RNR_NAK |
1303 (~AETH_TYPE_MASK &
1304 qp->attr.min_rnr_timer),
1305 pkt->psn);
1306 } else {
1307
1308 qp->resp.drop_msg = 1;
1309 }
1310 state = RESPST_CLEANUP;
1311 break;
1312
1313 case RESPST_ERR_RKEY_VIOLATION:
1314 if (qp_type(qp) == IB_QPT_RC) {
1315
1316 do_class_ac_error(qp, AETH_NAK_REM_ACC_ERR,
1317 IB_WC_REM_ACCESS_ERR);
1318 state = RESPST_COMPLETE;
1319 } else {
1320 qp->resp.drop_msg = 1;
1321 if (qp->srq) {
1322
1323 qp->resp.status = IB_WC_REM_ACCESS_ERR;
1324 state = RESPST_COMPLETE;
1325 } else {
1326
1327 state = RESPST_CLEANUP;
1328 }
1329 }
1330 break;
1331
1332 case RESPST_ERR_LENGTH:
1333 if (qp_type(qp) == IB_QPT_RC) {
1334
1335 do_class_ac_error(qp, AETH_NAK_INVALID_REQ,
1336 IB_WC_REM_INV_REQ_ERR);
1337 state = RESPST_COMPLETE;
1338 } else if (qp->srq) {
1339
1340 qp->resp.status = IB_WC_REM_INV_REQ_ERR;
1341 state = RESPST_COMPLETE;
1342 } else {
1343
1344 qp->resp.drop_msg = 1;
1345 state = RESPST_CLEANUP;
1346 }
1347 break;
1348
1349 case RESPST_ERR_MALFORMED_WQE:
1350
1351 do_class_ac_error(qp, AETH_NAK_REM_OP_ERR,
1352 IB_WC_LOC_QP_OP_ERR);
1353 state = RESPST_COMPLETE;
1354 break;
1355
1356 case RESPST_ERR_CQ_OVERFLOW:
1357
1358 state = RESPST_ERROR;
1359 break;
1360
1361 case RESPST_DONE:
1362 if (qp->resp.goto_error) {
1363 state = RESPST_ERROR;
1364 break;
1365 }
1366
1367 goto done;
1368
1369 case RESPST_EXIT:
1370 if (qp->resp.goto_error) {
1371 state = RESPST_ERROR;
1372 break;
1373 }
1374
1375 goto exit;
1376
1377 case RESPST_RESET:
1378 rxe_drain_req_pkts(qp, false);
1379 qp->resp.wqe = NULL;
1380 goto exit;
1381
1382 case RESPST_ERROR:
1383 qp->resp.goto_error = 0;
1384 pr_warn("qp#%d moved to error state\n", qp_num(qp));
1385 rxe_qp_error(qp);
1386 goto exit;
1387
1388 default:
1389 WARN_ON_ONCE(1);
1390 }
1391 }
1392
1393exit:
1394 ret = -EAGAIN;
1395done:
1396 rxe_drop_ref(qp);
1397 return ret;
1398}
1399