GNU Linux-libre 4.9.283-gnu1
[releases.git] / drivers / infiniband / sw / rxe / rxe_resp.c
1 /*
2  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/skbuff.h>
35
36 #include "rxe.h"
37 #include "rxe_loc.h"
38 #include "rxe_queue.h"
39
40 enum 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
73 static 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 /* rxe_recv calls here to add a request packet to the input queue */
107 void 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
121 static 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                         /* drain request packet queue */
130                         rxe_drop_ref(qp);
131                         kfree_skb(skb);
132                         return RESPST_GET_REQ;
133                 }
134
135                 /* go drain recv wr queue */
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
148 static 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
153         switch (qp_type(qp)) {
154         case IB_QPT_RC:
155                 if (diff > 0) {
156                         if (qp->resp.sent_psn_nak)
157                                 return RESPST_CLEANUP;
158
159                         qp->resp.sent_psn_nak = 1;
160                         return RESPST_ERR_PSN_OUT_OF_SEQ;
161
162                 } else if (diff < 0) {
163                         return RESPST_DUPLICATE_REQUEST;
164                 }
165
166                 if (qp->resp.sent_psn_nak)
167                         qp->resp.sent_psn_nak = 0;
168
169                 break;
170
171         case IB_QPT_UC:
172                 if (qp->resp.drop_msg || diff != 0) {
173                         if (pkt->mask & RXE_START_MASK) {
174                                 qp->resp.drop_msg = 0;
175                                 return RESPST_CHK_OP_SEQ;
176                         }
177
178                         qp->resp.drop_msg = 1;
179                         return RESPST_CLEANUP;
180                 }
181                 break;
182         default:
183                 break;
184         }
185
186         return RESPST_CHK_OP_SEQ;
187 }
188
189 static enum resp_states check_op_seq(struct rxe_qp *qp,
190                                      struct rxe_pkt_info *pkt)
191 {
192         switch (qp_type(qp)) {
193         case IB_QPT_RC:
194                 switch (qp->resp.opcode) {
195                 case IB_OPCODE_RC_SEND_FIRST:
196                 case IB_OPCODE_RC_SEND_MIDDLE:
197                         switch (pkt->opcode) {
198                         case IB_OPCODE_RC_SEND_MIDDLE:
199                         case IB_OPCODE_RC_SEND_LAST:
200                         case IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE:
201                         case IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE:
202                                 return RESPST_CHK_OP_VALID;
203                         default:
204                                 return RESPST_ERR_MISSING_OPCODE_LAST_C;
205                         }
206
207                 case IB_OPCODE_RC_RDMA_WRITE_FIRST:
208                 case IB_OPCODE_RC_RDMA_WRITE_MIDDLE:
209                         switch (pkt->opcode) {
210                         case IB_OPCODE_RC_RDMA_WRITE_MIDDLE:
211                         case IB_OPCODE_RC_RDMA_WRITE_LAST:
212                         case IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
213                                 return RESPST_CHK_OP_VALID;
214                         default:
215                                 return RESPST_ERR_MISSING_OPCODE_LAST_C;
216                         }
217
218                 default:
219                         switch (pkt->opcode) {
220                         case IB_OPCODE_RC_SEND_MIDDLE:
221                         case IB_OPCODE_RC_SEND_LAST:
222                         case IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE:
223                         case IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE:
224                         case IB_OPCODE_RC_RDMA_WRITE_MIDDLE:
225                         case IB_OPCODE_RC_RDMA_WRITE_LAST:
226                         case IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
227                                 return RESPST_ERR_MISSING_OPCODE_FIRST;
228                         default:
229                                 return RESPST_CHK_OP_VALID;
230                         }
231                 }
232                 break;
233
234         case IB_QPT_UC:
235                 switch (qp->resp.opcode) {
236                 case IB_OPCODE_UC_SEND_FIRST:
237                 case IB_OPCODE_UC_SEND_MIDDLE:
238                         switch (pkt->opcode) {
239                         case IB_OPCODE_UC_SEND_MIDDLE:
240                         case IB_OPCODE_UC_SEND_LAST:
241                         case IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE:
242                                 return RESPST_CHK_OP_VALID;
243                         default:
244                                 return RESPST_ERR_MISSING_OPCODE_LAST_D1E;
245                         }
246
247                 case IB_OPCODE_UC_RDMA_WRITE_FIRST:
248                 case IB_OPCODE_UC_RDMA_WRITE_MIDDLE:
249                         switch (pkt->opcode) {
250                         case IB_OPCODE_UC_RDMA_WRITE_MIDDLE:
251                         case IB_OPCODE_UC_RDMA_WRITE_LAST:
252                         case IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
253                                 return RESPST_CHK_OP_VALID;
254                         default:
255                                 return RESPST_ERR_MISSING_OPCODE_LAST_D1E;
256                         }
257
258                 default:
259                         switch (pkt->opcode) {
260                         case IB_OPCODE_UC_SEND_MIDDLE:
261                         case IB_OPCODE_UC_SEND_LAST:
262                         case IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE:
263                         case IB_OPCODE_UC_RDMA_WRITE_MIDDLE:
264                         case IB_OPCODE_UC_RDMA_WRITE_LAST:
265                         case IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE:
266                                 qp->resp.drop_msg = 1;
267                                 return RESPST_CLEANUP;
268                         default:
269                                 return RESPST_CHK_OP_VALID;
270                         }
271                 }
272                 break;
273
274         default:
275                 return RESPST_CHK_OP_VALID;
276         }
277 }
278
279 static enum resp_states check_op_valid(struct rxe_qp *qp,
280                                        struct rxe_pkt_info *pkt)
281 {
282         switch (qp_type(qp)) {
283         case IB_QPT_RC:
284                 if (((pkt->mask & RXE_READ_MASK) &&
285                      !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_READ)) ||
286                     ((pkt->mask & RXE_WRITE_MASK) &&
287                      !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_WRITE)) ||
288                     ((pkt->mask & RXE_ATOMIC_MASK) &&
289                      !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_ATOMIC))) {
290                         return RESPST_ERR_UNSUPPORTED_OPCODE;
291                 }
292
293                 break;
294
295         case IB_QPT_UC:
296                 if ((pkt->mask & RXE_WRITE_MASK) &&
297                     !(qp->attr.qp_access_flags & IB_ACCESS_REMOTE_WRITE)) {
298                         qp->resp.drop_msg = 1;
299                         return RESPST_CLEANUP;
300                 }
301
302                 break;
303
304         case IB_QPT_UD:
305         case IB_QPT_SMI:
306         case IB_QPT_GSI:
307                 break;
308
309         default:
310                 WARN_ON(1);
311                 break;
312         }
313
314         return RESPST_CHK_RESOURCE;
315 }
316
317 static enum resp_states get_srq_wqe(struct rxe_qp *qp)
318 {
319         struct rxe_srq *srq = qp->srq;
320         struct rxe_queue *q = srq->rq.queue;
321         struct rxe_recv_wqe *wqe;
322         struct ib_event ev;
323
324         if (srq->error)
325                 return RESPST_ERR_RNR;
326
327         spin_lock_bh(&srq->rq.consumer_lock);
328
329         wqe = queue_head(q);
330         if (!wqe) {
331                 spin_unlock_bh(&srq->rq.consumer_lock);
332                 return RESPST_ERR_RNR;
333         }
334
335         /* note kernel and user space recv wqes have same size */
336         memcpy(&qp->resp.srq_wqe, wqe, sizeof(qp->resp.srq_wqe));
337
338         qp->resp.wqe = &qp->resp.srq_wqe.wqe;
339         advance_consumer(q);
340
341         if (srq->limit && srq->ibsrq.event_handler &&
342             (queue_count(q) < srq->limit)) {
343                 srq->limit = 0;
344                 goto event;
345         }
346
347         spin_unlock_bh(&srq->rq.consumer_lock);
348         return RESPST_CHK_LENGTH;
349
350 event:
351         spin_unlock_bh(&srq->rq.consumer_lock);
352         ev.device = qp->ibqp.device;
353         ev.element.srq = qp->ibqp.srq;
354         ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
355         srq->ibsrq.event_handler(&ev, srq->ibsrq.srq_context);
356         return RESPST_CHK_LENGTH;
357 }
358
359 static enum resp_states check_resource(struct rxe_qp *qp,
360                                        struct rxe_pkt_info *pkt)
361 {
362         struct rxe_srq *srq = qp->srq;
363
364         if (qp->resp.state == QP_STATE_ERROR) {
365                 if (qp->resp.wqe) {
366                         qp->resp.status = IB_WC_WR_FLUSH_ERR;
367                         return RESPST_COMPLETE;
368                 } else if (!srq) {
369                         qp->resp.wqe = queue_head(qp->rq.queue);
370                         if (qp->resp.wqe) {
371                                 qp->resp.status = IB_WC_WR_FLUSH_ERR;
372                                 return RESPST_COMPLETE;
373                         } else {
374                                 return RESPST_EXIT;
375                         }
376                 } else {
377                         return RESPST_EXIT;
378                 }
379         }
380
381         if (pkt->mask & RXE_READ_OR_ATOMIC) {
382                 /* it is the requesters job to not send
383                  * too many read/atomic ops, we just
384                  * recycle the responder resource queue
385                  */
386                 if (likely(qp->attr.max_dest_rd_atomic > 0))
387                         return RESPST_CHK_LENGTH;
388                 else
389                         return RESPST_ERR_TOO_MANY_RDMA_ATM_REQ;
390         }
391
392         if (pkt->mask & RXE_RWR_MASK) {
393                 if (srq)
394                         return get_srq_wqe(qp);
395
396                 qp->resp.wqe = queue_head(qp->rq.queue);
397                 return (qp->resp.wqe) ? RESPST_CHK_LENGTH : RESPST_ERR_RNR;
398         }
399
400         return RESPST_CHK_LENGTH;
401 }
402
403 static enum resp_states check_length(struct rxe_qp *qp,
404                                      struct rxe_pkt_info *pkt)
405 {
406         switch (qp_type(qp)) {
407         case IB_QPT_RC:
408                 return RESPST_CHK_RKEY;
409
410         case IB_QPT_UC:
411                 return RESPST_CHK_RKEY;
412
413         default:
414                 return RESPST_CHK_RKEY;
415         }
416 }
417
418 static enum resp_states check_rkey(struct rxe_qp *qp,
419                                    struct rxe_pkt_info *pkt)
420 {
421         struct rxe_mem *mem = NULL;
422         u64 va;
423         u32 rkey;
424         u32 resid;
425         u32 pktlen;
426         int mtu = qp->mtu;
427         enum resp_states state;
428         int access;
429
430         if (pkt->mask & (RXE_READ_MASK | RXE_WRITE_MASK)) {
431                 if (pkt->mask & RXE_RETH_MASK) {
432                         qp->resp.va = reth_va(pkt);
433                         qp->resp.rkey = reth_rkey(pkt);
434                         qp->resp.resid = reth_len(pkt);
435                         qp->resp.length = reth_len(pkt);
436                 }
437                 access = (pkt->mask & RXE_READ_MASK) ? IB_ACCESS_REMOTE_READ
438                                                      : IB_ACCESS_REMOTE_WRITE;
439         } else if (pkt->mask & RXE_ATOMIC_MASK) {
440                 qp->resp.va = atmeth_va(pkt);
441                 qp->resp.rkey = atmeth_rkey(pkt);
442                 qp->resp.resid = sizeof(u64);
443                 access = IB_ACCESS_REMOTE_ATOMIC;
444         } else {
445                 return RESPST_EXECUTE;
446         }
447
448         va      = qp->resp.va;
449         rkey    = qp->resp.rkey;
450         resid   = qp->resp.resid;
451         pktlen  = payload_size(pkt);
452
453         mem = lookup_mem(qp->pd, access, rkey, lookup_remote);
454         if (!mem) {
455                 state = RESPST_ERR_RKEY_VIOLATION;
456                 goto err;
457         }
458
459         if (unlikely(mem->state == RXE_MEM_STATE_FREE)) {
460                 state = RESPST_ERR_RKEY_VIOLATION;
461                 goto err;
462         }
463
464         if (mem_check_range(mem, va, resid)) {
465                 state = RESPST_ERR_RKEY_VIOLATION;
466                 goto err;
467         }
468
469         if (pkt->mask & RXE_WRITE_MASK)  {
470                 if (resid > mtu) {
471                         if (pktlen != mtu || bth_pad(pkt)) {
472                                 state = RESPST_ERR_LENGTH;
473                                 goto err;
474                         }
475                 } else {
476                         if (pktlen != resid) {
477                                 state = RESPST_ERR_LENGTH;
478                                 goto err;
479                         }
480                         if ((bth_pad(pkt) != (0x3 & (-resid)))) {
481                                 /* This case may not be exactly that
482                                  * but nothing else fits.
483                                  */
484                                 state = RESPST_ERR_LENGTH;
485                                 goto err;
486                         }
487                 }
488         }
489
490         WARN_ON(qp->resp.mr);
491
492         qp->resp.mr = mem;
493         return RESPST_EXECUTE;
494
495 err:
496         if (mem)
497                 rxe_drop_ref(mem);
498         return state;
499 }
500
501 static enum resp_states send_data_in(struct rxe_qp *qp, void *data_addr,
502                                      int data_len)
503 {
504         int err;
505         struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
506
507         err = copy_data(rxe, qp->pd, IB_ACCESS_LOCAL_WRITE, &qp->resp.wqe->dma,
508                         data_addr, data_len, to_mem_obj, NULL);
509         if (unlikely(err))
510                 return (err == -ENOSPC) ? RESPST_ERR_LENGTH
511                                         : RESPST_ERR_MALFORMED_WQE;
512
513         return RESPST_NONE;
514 }
515
516 static enum resp_states write_data_in(struct rxe_qp *qp,
517                                       struct rxe_pkt_info *pkt)
518 {
519         enum resp_states rc = RESPST_NONE;
520         int     err;
521         int data_len = payload_size(pkt);
522
523         err = rxe_mem_copy(qp->resp.mr, qp->resp.va, payload_addr(pkt),
524                            data_len, to_mem_obj, NULL);
525         if (err) {
526                 rc = RESPST_ERR_RKEY_VIOLATION;
527                 goto out;
528         }
529
530         qp->resp.va += data_len;
531         qp->resp.resid -= data_len;
532
533 out:
534         return rc;
535 }
536
537 /* Guarantee atomicity of atomic operations at the machine level. */
538 static DEFINE_SPINLOCK(atomic_ops_lock);
539
540 static enum resp_states process_atomic(struct rxe_qp *qp,
541                                        struct rxe_pkt_info *pkt)
542 {
543         u64 iova = atmeth_va(pkt);
544         u64 *vaddr;
545         enum resp_states ret;
546         struct rxe_mem *mr = qp->resp.mr;
547
548         if (mr->state != RXE_MEM_STATE_VALID) {
549                 ret = RESPST_ERR_RKEY_VIOLATION;
550                 goto out;
551         }
552
553         vaddr = iova_to_vaddr(mr, iova, sizeof(u64));
554
555         /* check vaddr is 8 bytes aligned. */
556         if (!vaddr || (uintptr_t)vaddr & 7) {
557                 ret = RESPST_ERR_MISALIGNED_ATOMIC;
558                 goto out;
559         }
560
561         spin_lock_bh(&atomic_ops_lock);
562
563         qp->resp.atomic_orig = *vaddr;
564
565         if (pkt->opcode == IB_OPCODE_RC_COMPARE_SWAP ||
566             pkt->opcode == IB_OPCODE_RD_COMPARE_SWAP) {
567                 if (*vaddr == atmeth_comp(pkt))
568                         *vaddr = atmeth_swap_add(pkt);
569         } else {
570                 *vaddr += atmeth_swap_add(pkt);
571         }
572
573         spin_unlock_bh(&atomic_ops_lock);
574
575         ret = RESPST_NONE;
576 out:
577         return ret;
578 }
579
580 static struct sk_buff *prepare_ack_packet(struct rxe_qp *qp,
581                                           struct rxe_pkt_info *pkt,
582                                           struct rxe_pkt_info *ack,
583                                           int opcode,
584                                           int payload,
585                                           u32 psn,
586                                           u8 syndrome,
587                                           u32 *crcp)
588 {
589         struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
590         struct sk_buff *skb;
591         u32 crc = 0;
592         u32 *p;
593         int paylen;
594         int pad;
595         int err;
596
597         /*
598          * allocate packet
599          */
600         pad = (-payload) & 0x3;
601         paylen = rxe_opcode[opcode].length + payload + pad + RXE_ICRC_SIZE;
602
603         skb = rxe_init_packet(rxe, &qp->pri_av, paylen, ack);
604         if (!skb)
605                 return NULL;
606
607         ack->qp = qp;
608         ack->opcode = opcode;
609         ack->mask = rxe_opcode[opcode].mask;
610         ack->offset = pkt->offset;
611         ack->paylen = paylen;
612
613         /* fill in bth using the request packet headers */
614         memcpy(ack->hdr, pkt->hdr, pkt->offset + RXE_BTH_BYTES);
615
616         bth_set_opcode(ack, opcode);
617         bth_set_qpn(ack, qp->attr.dest_qp_num);
618         bth_set_pad(ack, pad);
619         bth_set_se(ack, 0);
620         bth_set_psn(ack, psn);
621         bth_set_ack(ack, 0);
622         ack->psn = psn;
623
624         if (ack->mask & RXE_AETH_MASK) {
625                 aeth_set_syn(ack, syndrome);
626                 aeth_set_msn(ack, qp->resp.msn);
627         }
628
629         if (ack->mask & RXE_ATMACK_MASK)
630                 atmack_set_orig(ack, qp->resp.atomic_orig);
631
632         err = rxe_prepare(rxe, ack, skb, &crc);
633         if (err) {
634                 kfree_skb(skb);
635                 return NULL;
636         }
637
638         if (crcp) {
639                 /* CRC computation will be continued by the caller */
640                 *crcp = crc;
641         } else {
642                 p = payload_addr(ack) + payload + bth_pad(ack);
643                 *p = ~crc;
644         }
645
646         return skb;
647 }
648
649 /* RDMA read response. If res is not NULL, then we have a current RDMA request
650  * being processed or replayed.
651  */
652 static enum resp_states read_reply(struct rxe_qp *qp,
653                                    struct rxe_pkt_info *req_pkt)
654 {
655         struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
656         struct rxe_pkt_info ack_pkt;
657         struct sk_buff *skb;
658         int mtu = qp->mtu;
659         enum resp_states state;
660         int payload;
661         int opcode;
662         int err;
663         struct resp_res *res = qp->resp.res;
664         u32 icrc;
665         u32 *p;
666
667         if (!res) {
668                 /* This is the first time we process that request. Get a
669                  * resource
670                  */
671                 res = &qp->resp.resources[qp->resp.res_head];
672
673                 free_rd_atomic_resource(qp, res);
674                 rxe_advance_resp_resource(qp);
675
676                 res->type               = RXE_READ_MASK;
677
678                 res->read.va            = qp->resp.va;
679                 res->read.va_org        = qp->resp.va;
680
681                 res->first_psn          = req_pkt->psn;
682                 res->last_psn           = req_pkt->psn +
683                                           (reth_len(req_pkt) + mtu - 1) /
684                                           mtu - 1;
685                 res->cur_psn            = req_pkt->psn;
686
687                 res->read.resid         = qp->resp.resid;
688                 res->read.length        = qp->resp.resid;
689                 res->read.rkey          = qp->resp.rkey;
690
691                 /* note res inherits the reference to mr from qp */
692                 res->read.mr            = qp->resp.mr;
693                 qp->resp.mr             = NULL;
694
695                 qp->resp.res            = res;
696                 res->state              = rdatm_res_state_new;
697         }
698
699         if (res->state == rdatm_res_state_new) {
700                 if (res->read.resid <= mtu)
701                         opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY;
702                 else
703                         opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST;
704         } else {
705                 if (res->read.resid > mtu)
706                         opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE;
707                 else
708                         opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST;
709         }
710
711         res->state = rdatm_res_state_next;
712
713         payload = min_t(int, res->read.resid, mtu);
714
715         skb = prepare_ack_packet(qp, req_pkt, &ack_pkt, opcode, payload,
716                                  res->cur_psn, AETH_ACK_UNLIMITED, &icrc);
717         if (!skb)
718                 return RESPST_ERR_RNR;
719
720         err = rxe_mem_copy(res->read.mr, res->read.va, payload_addr(&ack_pkt),
721                            payload, from_mem_obj, &icrc);
722         if (err)
723                 pr_err("Failed copying memory\n");
724
725         p = payload_addr(&ack_pkt) + payload + bth_pad(&ack_pkt);
726         *p = ~icrc;
727
728         err = rxe_xmit_packet(rxe, qp, &ack_pkt, skb);
729         if (err) {
730                 pr_err("Failed sending RDMA reply.\n");
731                 kfree_skb(skb);
732                 return RESPST_ERR_RNR;
733         }
734
735         res->read.va += payload;
736         res->read.resid -= payload;
737         res->cur_psn = (res->cur_psn + 1) & BTH_PSN_MASK;
738
739         if (res->read.resid > 0) {
740                 state = RESPST_DONE;
741         } else {
742                 qp->resp.res = NULL;
743                 qp->resp.opcode = -1;
744                 qp->resp.psn = res->cur_psn;
745                 state = RESPST_CLEANUP;
746         }
747
748         return state;
749 }
750
751 static void build_rdma_network_hdr(union rdma_network_hdr *hdr,
752                                    struct rxe_pkt_info *pkt)
753 {
754         struct sk_buff *skb = PKT_TO_SKB(pkt);
755
756         memset(hdr, 0, sizeof(*hdr));
757         if (skb->protocol == htons(ETH_P_IP))
758                 memcpy(&hdr->roce4grh, ip_hdr(skb), sizeof(hdr->roce4grh));
759         else if (skb->protocol == htons(ETH_P_IPV6))
760                 memcpy(&hdr->ibgrh, ipv6_hdr(skb), sizeof(hdr->ibgrh));
761 }
762
763 /* Executes a new request. A retried request never reach that function (send
764  * and writes are discarded, and reads and atomics are retried elsewhere.
765  */
766 static enum resp_states execute(struct rxe_qp *qp, struct rxe_pkt_info *pkt)
767 {
768         enum resp_states err;
769
770         if (pkt->mask & RXE_SEND_MASK) {
771                 if (qp_type(qp) == IB_QPT_UD ||
772                     qp_type(qp) == IB_QPT_SMI ||
773                     qp_type(qp) == IB_QPT_GSI) {
774                         union rdma_network_hdr hdr;
775
776                         build_rdma_network_hdr(&hdr, pkt);
777
778                         err = send_data_in(qp, &hdr, sizeof(hdr));
779                         if (err)
780                                 return err;
781                 }
782                 err = send_data_in(qp, payload_addr(pkt), payload_size(pkt));
783                 if (err)
784                         return err;
785         } else if (pkt->mask & RXE_WRITE_MASK) {
786                 err = write_data_in(qp, pkt);
787                 if (err)
788                         return err;
789         } else if (pkt->mask & RXE_READ_MASK) {
790                 /* For RDMA Read we can increment the msn now. See C9-148. */
791                 qp->resp.msn++;
792                 return RESPST_READ_REPLY;
793         } else if (pkt->mask & RXE_ATOMIC_MASK) {
794                 err = process_atomic(qp, pkt);
795                 if (err)
796                         return err;
797         } else
798                 /* Unreachable */
799                 WARN_ON(1);
800
801         /* next expected psn, read handles this separately */
802         qp->resp.psn = (pkt->psn + 1) & BTH_PSN_MASK;
803
804         qp->resp.opcode = pkt->opcode;
805         qp->resp.status = IB_WC_SUCCESS;
806
807         if (pkt->mask & RXE_COMP_MASK) {
808                 /* We successfully processed this new request. */
809                 qp->resp.msn++;
810                 return RESPST_COMPLETE;
811         } else if (qp_type(qp) == IB_QPT_RC)
812                 return RESPST_ACKNOWLEDGE;
813         else
814                 return RESPST_CLEANUP;
815 }
816
817 static enum resp_states do_complete(struct rxe_qp *qp,
818                                     struct rxe_pkt_info *pkt)
819 {
820         struct rxe_cqe cqe;
821         struct ib_wc *wc = &cqe.ibwc;
822         struct ib_uverbs_wc *uwc = &cqe.uibwc;
823         struct rxe_recv_wqe *wqe = qp->resp.wqe;
824
825         if (unlikely(!wqe))
826                 return RESPST_CLEANUP;
827
828         memset(&cqe, 0, sizeof(cqe));
829
830         if (qp->rcq->is_user) {
831                 uwc->status             = qp->resp.status;
832                 uwc->qp_num             = qp->ibqp.qp_num;
833                 uwc->wr_id              = wqe->wr_id;
834         } else {
835                 wc->status              = qp->resp.status;
836                 wc->qp                  = &qp->ibqp;
837                 wc->wr_id               = wqe->wr_id;
838         }
839
840         if (wc->status == IB_WC_SUCCESS) {
841                 wc->opcode = (pkt->mask & RXE_IMMDT_MASK &&
842                                 pkt->mask & RXE_WRITE_MASK) ?
843                                         IB_WC_RECV_RDMA_WITH_IMM : IB_WC_RECV;
844                 wc->vendor_err = 0;
845                 wc->byte_len = (pkt->mask & RXE_IMMDT_MASK &&
846                                 pkt->mask & RXE_WRITE_MASK) ?
847                                         qp->resp.length : wqe->dma.length - wqe->dma.resid;
848
849                 /* fields after byte_len are different between kernel and user
850                  * space
851                  */
852                 if (qp->rcq->is_user) {
853                         uwc->wc_flags = IB_WC_GRH;
854
855                         if (pkt->mask & RXE_IMMDT_MASK) {
856                                 uwc->wc_flags |= IB_WC_WITH_IMM;
857                                 uwc->ex.imm_data =
858                                         (__u32 __force)immdt_imm(pkt);
859                         }
860
861                         if (pkt->mask & RXE_IETH_MASK) {
862                                 uwc->wc_flags |= IB_WC_WITH_INVALIDATE;
863                                 uwc->ex.invalidate_rkey = ieth_rkey(pkt);
864                         }
865
866                         uwc->qp_num             = qp->ibqp.qp_num;
867
868                         if (pkt->mask & RXE_DETH_MASK)
869                                 uwc->src_qp = deth_sqp(pkt);
870
871                         uwc->port_num           = qp->attr.port_num;
872                 } else {
873                         struct sk_buff *skb = PKT_TO_SKB(pkt);
874
875                         wc->wc_flags = IB_WC_GRH | IB_WC_WITH_NETWORK_HDR_TYPE;
876                         if (skb->protocol == htons(ETH_P_IP))
877                                 wc->network_hdr_type = RDMA_NETWORK_IPV4;
878                         else
879                                 wc->network_hdr_type = RDMA_NETWORK_IPV6;
880
881                         if (pkt->mask & RXE_IMMDT_MASK) {
882                                 wc->wc_flags |= IB_WC_WITH_IMM;
883                                 wc->ex.imm_data = immdt_imm(pkt);
884                         }
885
886                         if (pkt->mask & RXE_IETH_MASK) {
887                                 struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
888                                 struct rxe_mem *rmr;
889
890                                 wc->wc_flags |= IB_WC_WITH_INVALIDATE;
891                                 wc->ex.invalidate_rkey = ieth_rkey(pkt);
892
893                                 rmr = rxe_pool_get_index(&rxe->mr_pool,
894                                                          wc->ex.invalidate_rkey >> 8);
895                                 if (unlikely(!rmr)) {
896                                         pr_err("Bad rkey %#x invalidation\n",
897                                                wc->ex.invalidate_rkey);
898                                         return RESPST_ERROR;
899                                 }
900                                 rmr->state = RXE_MEM_STATE_FREE;
901                                 rxe_drop_ref(rmr);
902                         }
903
904                         wc->qp                  = &qp->ibqp;
905
906                         if (pkt->mask & RXE_DETH_MASK)
907                                 wc->src_qp = deth_sqp(pkt);
908
909                         wc->port_num            = qp->attr.port_num;
910                 }
911         }
912
913         /* have copy for srq and reference for !srq */
914         if (!qp->srq)
915                 advance_consumer(qp->rq.queue);
916
917         qp->resp.wqe = NULL;
918
919         if (rxe_cq_post(qp->rcq, &cqe, pkt ? bth_se(pkt) : 1))
920                 return RESPST_ERR_CQ_OVERFLOW;
921
922         if (qp->resp.state == QP_STATE_ERROR)
923                 return RESPST_CHK_RESOURCE;
924
925         if (!pkt)
926                 return RESPST_DONE;
927         else if (qp_type(qp) == IB_QPT_RC)
928                 return RESPST_ACKNOWLEDGE;
929         else
930                 return RESPST_CLEANUP;
931 }
932
933 static int send_ack(struct rxe_qp *qp, struct rxe_pkt_info *pkt,
934                     u8 syndrome, u32 psn)
935 {
936         int err = 0;
937         struct rxe_pkt_info ack_pkt;
938         struct sk_buff *skb;
939         struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
940
941         skb = prepare_ack_packet(qp, pkt, &ack_pkt, IB_OPCODE_RC_ACKNOWLEDGE,
942                                  0, psn, syndrome, NULL);
943         if (!skb) {
944                 err = -ENOMEM;
945                 goto err1;
946         }
947
948         err = rxe_xmit_packet(rxe, qp, &ack_pkt, skb);
949         if (err) {
950                 pr_err_ratelimited("Failed sending ack\n");
951                 kfree_skb(skb);
952         }
953
954 err1:
955         return err;
956 }
957
958 static int send_atomic_ack(struct rxe_qp *qp, struct rxe_pkt_info *pkt,
959                            u8 syndrome)
960 {
961         int rc = 0;
962         struct rxe_pkt_info ack_pkt;
963         struct sk_buff *skb;
964         struct sk_buff *skb_copy;
965         struct rxe_dev *rxe = to_rdev(qp->ibqp.device);
966         struct resp_res *res;
967
968         skb = prepare_ack_packet(qp, pkt, &ack_pkt,
969                                  IB_OPCODE_RC_ATOMIC_ACKNOWLEDGE, 0, pkt->psn,
970                                  syndrome, NULL);
971         if (!skb) {
972                 rc = -ENOMEM;
973                 goto out;
974         }
975
976         skb_copy = skb_clone(skb, GFP_ATOMIC);
977         if (skb_copy)
978                 rxe_add_ref(qp); /* for the new SKB */
979         else {
980                 pr_warn("Could not clone atomic response\n");
981                 rc = -ENOMEM;
982                 goto out;
983         }
984
985         res = &qp->resp.resources[qp->resp.res_head];
986         free_rd_atomic_resource(qp, res);
987         rxe_advance_resp_resource(qp);
988
989         memcpy(SKB_TO_PKT(skb), &ack_pkt, sizeof(ack_pkt));
990         memset((unsigned char *)SKB_TO_PKT(skb) + sizeof(ack_pkt), 0,
991                sizeof(skb->cb) - sizeof(ack_pkt));
992
993         res->type = RXE_ATOMIC_MASK;
994         res->atomic.skb = skb;
995         res->first_psn = ack_pkt.psn;
996         res->last_psn  = ack_pkt.psn;
997         res->cur_psn   = ack_pkt.psn;
998
999         rc = rxe_xmit_packet(rxe, qp, &ack_pkt, skb_copy);
1000         if (rc) {
1001                 pr_err_ratelimited("Failed sending ack\n");
1002                 rxe_drop_ref(qp);
1003                 kfree_skb(skb_copy);
1004         }
1005
1006 out:
1007         return rc;
1008 }
1009
1010 static 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
1026 static 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
1045 static struct resp_res *find_resource(struct rxe_qp *qp, u32 psn)
1046 {
1047         int i;
1048
1049         for (i = 0; i < qp->attr.max_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
1064 static enum resp_states duplicate_request(struct rxe_qp *qp,
1065                                           struct rxe_pkt_info *pkt)
1066 {
1067         enum resp_states rc;
1068
1069         if (pkt->mask & RXE_SEND_MASK ||
1070             pkt->mask & RXE_WRITE_MASK) {
1071                 /* SEND. Ack again and cleanup. C9-105. */
1072                 if (bth_ack(pkt))
1073                         send_ack(qp, pkt, AETH_ACK_UNLIMITED, qp->resp.psn - 1);
1074                 rc = RESPST_CLEANUP;
1075                 goto out;
1076         } else if (pkt->mask & RXE_READ_MASK) {
1077                 struct resp_res *res;
1078
1079                 res = find_resource(qp, pkt->psn);
1080                 if (!res) {
1081                         /* Resource not found. Class D error.  Drop the
1082                          * request.
1083                          */
1084                         rc = RESPST_CLEANUP;
1085                         goto out;
1086                 } else {
1087                         /* Ensure this new request is the same as the previous
1088                          * one or a subset of it.
1089                          */
1090                         u64 iova = reth_va(pkt);
1091                         u32 resid = reth_len(pkt);
1092
1093                         if (iova < res->read.va_org ||
1094                             resid > res->read.length ||
1095                             (iova + resid) > (res->read.va_org +
1096                                               res->read.length)) {
1097                                 rc = RESPST_CLEANUP;
1098                                 goto out;
1099                         }
1100
1101                         if (reth_rkey(pkt) != res->read.rkey) {
1102                                 rc = RESPST_CLEANUP;
1103                                 goto out;
1104                         }
1105
1106                         res->cur_psn = pkt->psn;
1107                         res->state = (pkt->psn == res->first_psn) ?
1108                                         rdatm_res_state_new :
1109                                         rdatm_res_state_replay;
1110
1111                         /* Reset the resource, except length. */
1112                         res->read.va_org = iova;
1113                         res->read.va = iova;
1114                         res->read.resid = resid;
1115
1116                         /* Replay the RDMA read reply. */
1117                         qp->resp.res = res;
1118                         rc = RESPST_READ_REPLY;
1119                         goto out;
1120                 }
1121         } else {
1122                 struct resp_res *res;
1123
1124                 /* Find the operation in our list of responder resources. */
1125                 res = find_resource(qp, pkt->psn);
1126                 if (res) {
1127                         struct sk_buff *skb_copy;
1128
1129                         skb_copy = skb_clone(res->atomic.skb, GFP_ATOMIC);
1130                         if (skb_copy) {
1131                                 rxe_add_ref(qp); /* for the new SKB */
1132                         } else {
1133                                 pr_warn("Couldn't clone atomic resp\n");
1134                                 rc = RESPST_CLEANUP;
1135                                 goto out;
1136                         }
1137
1138                         /* Resend the result. */
1139                         rc = rxe_xmit_packet(to_rdev(qp->ibqp.device), qp,
1140                                              pkt, skb_copy);
1141                         if (rc) {
1142                                 pr_err("Failed resending result. This flow is not handled - skb ignored\n");
1143                                 kfree_skb(skb_copy);
1144                                 rc = RESPST_CLEANUP;
1145                                 goto out;
1146                         }
1147                 }
1148
1149                 /* Resource not found. Class D error. Drop the request. */
1150                 rc = RESPST_CLEANUP;
1151                 goto out;
1152         }
1153 out:
1154         return rc;
1155 }
1156
1157 /* Process a class A or C. Both are treated the same in this implementation. */
1158 static void do_class_ac_error(struct rxe_qp *qp, u8 syndrome,
1159                               enum ib_wc_status status)
1160 {
1161         qp->resp.aeth_syndrome  = syndrome;
1162         qp->resp.status         = status;
1163
1164         /* indicate that we should go through the ERROR state */
1165         qp->resp.goto_error     = 1;
1166 }
1167
1168 static enum resp_states do_class_d1e_error(struct rxe_qp *qp)
1169 {
1170         /* UC */
1171         if (qp->srq) {
1172                 /* Class E */
1173                 qp->resp.drop_msg = 1;
1174                 if (qp->resp.wqe) {
1175                         qp->resp.status = IB_WC_REM_INV_REQ_ERR;
1176                         return RESPST_COMPLETE;
1177                 } else {
1178                         return RESPST_CLEANUP;
1179                 }
1180         } else {
1181                 /* Class D1. This packet may be the start of a
1182                  * new message and could be valid. The previous
1183                  * message is invalid and ignored. reset the
1184                  * recv wr to its original state
1185                  */
1186                 if (qp->resp.wqe) {
1187                         qp->resp.wqe->dma.resid = qp->resp.wqe->dma.length;
1188                         qp->resp.wqe->dma.cur_sge = 0;
1189                         qp->resp.wqe->dma.sge_offset = 0;
1190                         qp->resp.opcode = -1;
1191                 }
1192
1193                 if (qp->resp.mr) {
1194                         rxe_drop_ref(qp->resp.mr);
1195                         qp->resp.mr = NULL;
1196                 }
1197
1198                 return RESPST_CLEANUP;
1199         }
1200 }
1201
1202 int rxe_responder(void *arg)
1203 {
1204         struct rxe_qp *qp = (struct rxe_qp *)arg;
1205         enum resp_states state;
1206         struct rxe_pkt_info *pkt = NULL;
1207         int ret = 0;
1208
1209         qp->resp.aeth_syndrome = AETH_ACK_UNLIMITED;
1210
1211         if (!qp->valid) {
1212                 ret = -EINVAL;
1213                 goto done;
1214         }
1215
1216         switch (qp->resp.state) {
1217         case QP_STATE_RESET:
1218                 state = RESPST_RESET;
1219                 break;
1220
1221         default:
1222                 state = RESPST_GET_REQ;
1223                 break;
1224         }
1225
1226         while (1) {
1227                 pr_debug("qp#%d state = %s\n", qp_num(qp),
1228                          resp_state_name[state]);
1229                 switch (state) {
1230                 case RESPST_GET_REQ:
1231                         state = get_req(qp, &pkt);
1232                         break;
1233                 case RESPST_CHK_PSN:
1234                         state = check_psn(qp, pkt);
1235                         break;
1236                 case RESPST_CHK_OP_SEQ:
1237                         state = check_op_seq(qp, pkt);
1238                         break;
1239                 case RESPST_CHK_OP_VALID:
1240                         state = check_op_valid(qp, pkt);
1241                         break;
1242                 case RESPST_CHK_RESOURCE:
1243                         state = check_resource(qp, pkt);
1244                         break;
1245                 case RESPST_CHK_LENGTH:
1246                         state = check_length(qp, pkt);
1247                         break;
1248                 case RESPST_CHK_RKEY:
1249                         state = check_rkey(qp, pkt);
1250                         break;
1251                 case RESPST_EXECUTE:
1252                         state = execute(qp, pkt);
1253                         break;
1254                 case RESPST_COMPLETE:
1255                         state = do_complete(qp, pkt);
1256                         break;
1257                 case RESPST_READ_REPLY:
1258                         state = read_reply(qp, pkt);
1259                         break;
1260                 case RESPST_ACKNOWLEDGE:
1261                         state = acknowledge(qp, pkt);
1262                         break;
1263                 case RESPST_CLEANUP:
1264                         state = cleanup(qp, pkt);
1265                         break;
1266                 case RESPST_DUPLICATE_REQUEST:
1267                         state = duplicate_request(qp, pkt);
1268                         break;
1269                 case RESPST_ERR_PSN_OUT_OF_SEQ:
1270                         /* RC only - Class B. Drop packet. */
1271                         send_ack(qp, pkt, AETH_NAK_PSN_SEQ_ERROR, qp->resp.psn);
1272                         state = RESPST_CLEANUP;
1273                         break;
1274
1275                 case RESPST_ERR_TOO_MANY_RDMA_ATM_REQ:
1276                 case RESPST_ERR_MISSING_OPCODE_FIRST:
1277                 case RESPST_ERR_MISSING_OPCODE_LAST_C:
1278                 case RESPST_ERR_UNSUPPORTED_OPCODE:
1279                 case RESPST_ERR_MISALIGNED_ATOMIC:
1280                         /* RC Only - Class C. */
1281                         do_class_ac_error(qp, AETH_NAK_INVALID_REQ,
1282                                           IB_WC_REM_INV_REQ_ERR);
1283                         state = RESPST_COMPLETE;
1284                         break;
1285
1286                 case RESPST_ERR_MISSING_OPCODE_LAST_D1E:
1287                         state = do_class_d1e_error(qp);
1288                         break;
1289                 case RESPST_ERR_RNR:
1290                         if (qp_type(qp) == IB_QPT_RC) {
1291                                 /* RC - class B */
1292                                 send_ack(qp, pkt, AETH_RNR_NAK |
1293                                          (~AETH_TYPE_MASK &
1294                                          qp->attr.min_rnr_timer),
1295                                          pkt->psn);
1296                         } else {
1297                                 /* UD/UC - class D */
1298                                 qp->resp.drop_msg = 1;
1299                         }
1300                         state = RESPST_CLEANUP;
1301                         break;
1302
1303                 case RESPST_ERR_RKEY_VIOLATION:
1304                         if (qp_type(qp) == IB_QPT_RC) {
1305                                 /* Class C */
1306                                 do_class_ac_error(qp, AETH_NAK_REM_ACC_ERR,
1307                                                   IB_WC_REM_ACCESS_ERR);
1308                                 state = RESPST_COMPLETE;
1309                         } else {
1310                                 qp->resp.drop_msg = 1;
1311                                 if (qp->srq) {
1312                                         /* UC/SRQ Class D */
1313                                         qp->resp.status = IB_WC_REM_ACCESS_ERR;
1314                                         state = RESPST_COMPLETE;
1315                                 } else {
1316                                         /* UC/non-SRQ Class E. */
1317                                         state = RESPST_CLEANUP;
1318                                 }
1319                         }
1320                         break;
1321
1322                 case RESPST_ERR_LENGTH:
1323                         if (qp_type(qp) == IB_QPT_RC) {
1324                                 /* Class C */
1325                                 do_class_ac_error(qp, AETH_NAK_INVALID_REQ,
1326                                                   IB_WC_REM_INV_REQ_ERR);
1327                                 state = RESPST_COMPLETE;
1328                         } else if (qp->srq) {
1329                                 /* UC/UD - class E */
1330                                 qp->resp.status = IB_WC_REM_INV_REQ_ERR;
1331                                 state = RESPST_COMPLETE;
1332                         } else {
1333                                 /* UC/UD - class D */
1334                                 qp->resp.drop_msg = 1;
1335                                 state = RESPST_CLEANUP;
1336                         }
1337                         break;
1338
1339                 case RESPST_ERR_MALFORMED_WQE:
1340                         /* All, Class A. */
1341                         do_class_ac_error(qp, AETH_NAK_REM_OP_ERR,
1342                                           IB_WC_LOC_QP_OP_ERR);
1343                         state = RESPST_COMPLETE;
1344                         break;
1345
1346                 case RESPST_ERR_CQ_OVERFLOW:
1347                         /* All - Class G */
1348                         state = RESPST_ERROR;
1349                         break;
1350
1351                 case RESPST_DONE:
1352                         if (qp->resp.goto_error) {
1353                                 state = RESPST_ERROR;
1354                                 break;
1355                         }
1356
1357                         goto done;
1358
1359                 case RESPST_EXIT:
1360                         if (qp->resp.goto_error) {
1361                                 state = RESPST_ERROR;
1362                                 break;
1363                         }
1364
1365                         goto exit;
1366
1367                 case RESPST_RESET: {
1368                         struct sk_buff *skb;
1369
1370                         while ((skb = skb_dequeue(&qp->req_pkts))) {
1371                                 rxe_drop_ref(qp);
1372                                 kfree_skb(skb);
1373                         }
1374
1375                         while (!qp->srq && qp->rq.queue &&
1376                                queue_head(qp->rq.queue))
1377                                 advance_consumer(qp->rq.queue);
1378
1379                         qp->resp.wqe = NULL;
1380                         goto exit;
1381                 }
1382
1383                 case RESPST_ERROR:
1384                         qp->resp.goto_error = 0;
1385                         pr_warn("qp#%d moved to error state\n", qp_num(qp));
1386                         rxe_qp_error(qp);
1387                         goto exit;
1388
1389                 default:
1390                         WARN_ON(1);
1391                 }
1392         }
1393
1394 exit:
1395         ret = -EAGAIN;
1396 done:
1397         return ret;
1398 }