GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / infiniband / sw / rdmavt / qp.c
1 /*
2  * Copyright(c) 2016, 2017 Intel Corporation.
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * BSD LICENSE
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  *  - Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  *  - Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *  - Neither the name of Intel Corporation nor the names of its
31  *    contributors may be used to endorse or promote products derived
32  *    from this software without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47
48 #include <linux/hash.h>
49 #include <linux/bitops.h>
50 #include <linux/lockdep.h>
51 #include <linux/vmalloc.h>
52 #include <linux/slab.h>
53 #include <rdma/ib_verbs.h>
54 #include <rdma/ib_hdrs.h>
55 #include <rdma/opa_addr.h>
56 #include "qp.h"
57 #include "vt.h"
58 #include "trace.h"
59
60 static void rvt_rc_timeout(unsigned long arg);
61
62 /*
63  * Convert the AETH RNR timeout code into the number of microseconds.
64  */
65 static const u32 ib_rvt_rnr_table[32] = {
66         655360, /* 00: 655.36 */
67         10,     /* 01:    .01 */
68         20,     /* 02     .02 */
69         30,     /* 03:    .03 */
70         40,     /* 04:    .04 */
71         60,     /* 05:    .06 */
72         80,     /* 06:    .08 */
73         120,    /* 07:    .12 */
74         160,    /* 08:    .16 */
75         240,    /* 09:    .24 */
76         320,    /* 0A:    .32 */
77         480,    /* 0B:    .48 */
78         640,    /* 0C:    .64 */
79         960,    /* 0D:    .96 */
80         1280,   /* 0E:   1.28 */
81         1920,   /* 0F:   1.92 */
82         2560,   /* 10:   2.56 */
83         3840,   /* 11:   3.84 */
84         5120,   /* 12:   5.12 */
85         7680,   /* 13:   7.68 */
86         10240,  /* 14:  10.24 */
87         15360,  /* 15:  15.36 */
88         20480,  /* 16:  20.48 */
89         30720,  /* 17:  30.72 */
90         40960,  /* 18:  40.96 */
91         61440,  /* 19:  61.44 */
92         81920,  /* 1A:  81.92 */
93         122880, /* 1B: 122.88 */
94         163840, /* 1C: 163.84 */
95         245760, /* 1D: 245.76 */
96         327680, /* 1E: 327.68 */
97         491520  /* 1F: 491.52 */
98 };
99
100 /*
101  * Note that it is OK to post send work requests in the SQE and ERR
102  * states; rvt_do_send() will process them and generate error
103  * completions as per IB 1.2 C10-96.
104  */
105 const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
106         [IB_QPS_RESET] = 0,
107         [IB_QPS_INIT] = RVT_POST_RECV_OK,
108         [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
109         [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
110             RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
111             RVT_PROCESS_NEXT_SEND_OK,
112         [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
113             RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
114         [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
115             RVT_POST_SEND_OK | RVT_FLUSH_SEND,
116         [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
117             RVT_POST_SEND_OK | RVT_FLUSH_SEND,
118 };
119 EXPORT_SYMBOL(ib_rvt_state_ops);
120
121 static void get_map_page(struct rvt_qpn_table *qpt,
122                          struct rvt_qpn_map *map)
123 {
124         unsigned long page = get_zeroed_page(GFP_KERNEL);
125
126         /*
127          * Free the page if someone raced with us installing it.
128          */
129
130         spin_lock(&qpt->lock);
131         if (map->page)
132                 free_page(page);
133         else
134                 map->page = (void *)page;
135         spin_unlock(&qpt->lock);
136 }
137
138 /**
139  * init_qpn_table - initialize the QP number table for a device
140  * @qpt: the QPN table
141  */
142 static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
143 {
144         u32 offset, i;
145         struct rvt_qpn_map *map;
146         int ret = 0;
147
148         if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
149                 return -EINVAL;
150
151         spin_lock_init(&qpt->lock);
152
153         qpt->last = rdi->dparms.qpn_start;
154         qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
155
156         /*
157          * Drivers may want some QPs beyond what we need for verbs let them use
158          * our qpn table. No need for two. Lets go ahead and mark the bitmaps
159          * for those. The reserved range must be *after* the range which verbs
160          * will pick from.
161          */
162
163         /* Figure out number of bit maps needed before reserved range */
164         qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
165
166         /* This should always be zero */
167         offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
168
169         /* Starting with the first reserved bit map */
170         map = &qpt->map[qpt->nmaps];
171
172         rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
173                     rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
174         for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
175                 if (!map->page) {
176                         get_map_page(qpt, map);
177                         if (!map->page) {
178                                 ret = -ENOMEM;
179                                 break;
180                         }
181                 }
182                 set_bit(offset, map->page);
183                 offset++;
184                 if (offset == RVT_BITS_PER_PAGE) {
185                         /* next page */
186                         qpt->nmaps++;
187                         map++;
188                         offset = 0;
189                 }
190         }
191         return ret;
192 }
193
194 /**
195  * free_qpn_table - free the QP number table for a device
196  * @qpt: the QPN table
197  */
198 static void free_qpn_table(struct rvt_qpn_table *qpt)
199 {
200         int i;
201
202         for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
203                 free_page((unsigned long)qpt->map[i].page);
204 }
205
206 /**
207  * rvt_driver_qp_init - Init driver qp resources
208  * @rdi: rvt dev strucutre
209  *
210  * Return: 0 on success
211  */
212 int rvt_driver_qp_init(struct rvt_dev_info *rdi)
213 {
214         int i;
215         int ret = -ENOMEM;
216
217         if (!rdi->dparms.qp_table_size)
218                 return -EINVAL;
219
220         /*
221          * If driver is not doing any QP allocation then make sure it is
222          * providing the necessary QP functions.
223          */
224         if (!rdi->driver_f.free_all_qps ||
225             !rdi->driver_f.qp_priv_alloc ||
226             !rdi->driver_f.qp_priv_free ||
227             !rdi->driver_f.notify_qp_reset ||
228             !rdi->driver_f.notify_restart_rc)
229                 return -EINVAL;
230
231         /* allocate parent object */
232         rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
233                                    rdi->dparms.node);
234         if (!rdi->qp_dev)
235                 return -ENOMEM;
236
237         /* allocate hash table */
238         rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
239         rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
240         rdi->qp_dev->qp_table =
241                 kmalloc_node(rdi->qp_dev->qp_table_size *
242                              sizeof(*rdi->qp_dev->qp_table),
243                              GFP_KERNEL, rdi->dparms.node);
244         if (!rdi->qp_dev->qp_table)
245                 goto no_qp_table;
246
247         for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
248                 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
249
250         spin_lock_init(&rdi->qp_dev->qpt_lock);
251
252         /* initialize qpn map */
253         if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
254                 goto fail_table;
255
256         spin_lock_init(&rdi->n_qps_lock);
257
258         return 0;
259
260 fail_table:
261         kfree(rdi->qp_dev->qp_table);
262         free_qpn_table(&rdi->qp_dev->qpn_table);
263
264 no_qp_table:
265         kfree(rdi->qp_dev);
266
267         return ret;
268 }
269
270 /**
271  * free_all_qps - check for QPs still in use
272  * @qpt: the QP table to empty
273  *
274  * There should not be any QPs still in use.
275  * Free memory for table.
276  */
277 static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
278 {
279         unsigned long flags;
280         struct rvt_qp *qp;
281         unsigned n, qp_inuse = 0;
282         spinlock_t *ql; /* work around too long line below */
283
284         if (rdi->driver_f.free_all_qps)
285                 qp_inuse = rdi->driver_f.free_all_qps(rdi);
286
287         qp_inuse += rvt_mcast_tree_empty(rdi);
288
289         if (!rdi->qp_dev)
290                 return qp_inuse;
291
292         ql = &rdi->qp_dev->qpt_lock;
293         spin_lock_irqsave(ql, flags);
294         for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
295                 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
296                                                lockdep_is_held(ql));
297                 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
298
299                 for (; qp; qp = rcu_dereference_protected(qp->next,
300                                                           lockdep_is_held(ql)))
301                         qp_inuse++;
302         }
303         spin_unlock_irqrestore(ql, flags);
304         synchronize_rcu();
305         return qp_inuse;
306 }
307
308 /**
309  * rvt_qp_exit - clean up qps on device exit
310  * @rdi: rvt dev structure
311  *
312  * Check for qp leaks and free resources.
313  */
314 void rvt_qp_exit(struct rvt_dev_info *rdi)
315 {
316         u32 qps_inuse = rvt_free_all_qps(rdi);
317
318         if (qps_inuse)
319                 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
320                            qps_inuse);
321
322         kfree(rdi->qp_dev->qp_table);
323         free_qpn_table(&rdi->qp_dev->qpn_table);
324         kfree(rdi->qp_dev);
325 }
326
327 static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
328                               struct rvt_qpn_map *map, unsigned off)
329 {
330         return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
331 }
332
333 /**
334  * alloc_qpn - Allocate the next available qpn or zero/one for QP type
335  *             IB_QPT_SMI/IB_QPT_GSI
336  *@rdi: rvt device info structure
337  *@qpt: queue pair number table pointer
338  *@port_num: IB port number, 1 based, comes from core
339  *
340  * Return: The queue pair number
341  */
342 static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
343                      enum ib_qp_type type, u8 port_num)
344 {
345         u32 i, offset, max_scan, qpn;
346         struct rvt_qpn_map *map;
347         u32 ret;
348
349         if (rdi->driver_f.alloc_qpn)
350                 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num);
351
352         if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
353                 unsigned n;
354
355                 ret = type == IB_QPT_GSI;
356                 n = 1 << (ret + 2 * (port_num - 1));
357                 spin_lock(&qpt->lock);
358                 if (qpt->flags & n)
359                         ret = -EINVAL;
360                 else
361                         qpt->flags |= n;
362                 spin_unlock(&qpt->lock);
363                 goto bail;
364         }
365
366         qpn = qpt->last + qpt->incr;
367         if (qpn >= RVT_QPN_MAX)
368                 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
369         /* offset carries bit 0 */
370         offset = qpn & RVT_BITS_PER_PAGE_MASK;
371         map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
372         max_scan = qpt->nmaps - !offset;
373         for (i = 0;;) {
374                 if (unlikely(!map->page)) {
375                         get_map_page(qpt, map);
376                         if (unlikely(!map->page))
377                                 break;
378                 }
379                 do {
380                         if (!test_and_set_bit(offset, map->page)) {
381                                 qpt->last = qpn;
382                                 ret = qpn;
383                                 goto bail;
384                         }
385                         offset += qpt->incr;
386                         /*
387                          * This qpn might be bogus if offset >= BITS_PER_PAGE.
388                          * That is OK.   It gets re-assigned below
389                          */
390                         qpn = mk_qpn(qpt, map, offset);
391                 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
392                 /*
393                  * In order to keep the number of pages allocated to a
394                  * minimum, we scan the all existing pages before increasing
395                  * the size of the bitmap table.
396                  */
397                 if (++i > max_scan) {
398                         if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
399                                 break;
400                         map = &qpt->map[qpt->nmaps++];
401                         /* start at incr with current bit 0 */
402                         offset = qpt->incr | (offset & 1);
403                 } else if (map < &qpt->map[qpt->nmaps]) {
404                         ++map;
405                         /* start at incr with current bit 0 */
406                         offset = qpt->incr | (offset & 1);
407                 } else {
408                         map = &qpt->map[0];
409                         /* wrap to first map page, invert bit 0 */
410                         offset = qpt->incr | ((offset & 1) ^ 1);
411                 }
412                 /* there can be no set bits in low-order QoS bits */
413                 WARN_ON(rdi->dparms.qos_shift > 1 &&
414                         offset & ((BIT(rdi->dparms.qos_shift - 1) - 1) << 1));
415                 qpn = mk_qpn(qpt, map, offset);
416         }
417
418         ret = -ENOMEM;
419
420 bail:
421         return ret;
422 }
423
424 /**
425  * rvt_clear_mr_refs - Drop help mr refs
426  * @qp: rvt qp data structure
427  * @clr_sends: If shoudl clear send side or not
428  */
429 static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
430 {
431         unsigned n;
432         struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
433
434         if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
435                 rvt_put_ss(&qp->s_rdma_read_sge);
436
437         rvt_put_ss(&qp->r_sge);
438
439         if (clr_sends) {
440                 while (qp->s_last != qp->s_head) {
441                         struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
442
443                         rvt_put_swqe(wqe);
444
445                         if (qp->ibqp.qp_type == IB_QPT_UD ||
446                             qp->ibqp.qp_type == IB_QPT_SMI ||
447                             qp->ibqp.qp_type == IB_QPT_GSI)
448                                 atomic_dec(&ibah_to_rvtah(
449                                                 wqe->ud_wr.ah)->refcount);
450                         if (++qp->s_last >= qp->s_size)
451                                 qp->s_last = 0;
452                         smp_wmb(); /* see qp_set_savail */
453                 }
454                 if (qp->s_rdma_mr) {
455                         rvt_put_mr(qp->s_rdma_mr);
456                         qp->s_rdma_mr = NULL;
457                 }
458         }
459
460         for (n = 0; qp->s_ack_queue && n < rvt_max_atomic(rdi); n++) {
461                 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
462
463                 if (e->rdma_sge.mr) {
464                         rvt_put_mr(e->rdma_sge.mr);
465                         e->rdma_sge.mr = NULL;
466                 }
467         }
468 }
469
470 /**
471  * rvt_swqe_has_lkey - return true if lkey is used by swqe
472  * @wqe - the send wqe
473  * @lkey - the lkey
474  *
475  * Test the swqe for using lkey
476  */
477 static bool rvt_swqe_has_lkey(struct rvt_swqe *wqe, u32 lkey)
478 {
479         int i;
480
481         for (i = 0; i < wqe->wr.num_sge; i++) {
482                 struct rvt_sge *sge = &wqe->sg_list[i];
483
484                 if (rvt_mr_has_lkey(sge->mr, lkey))
485                         return true;
486         }
487         return false;
488 }
489
490 /**
491  * rvt_qp_sends_has_lkey - return true is qp sends use lkey
492  * @qp - the rvt_qp
493  * @lkey - the lkey
494  */
495 static bool rvt_qp_sends_has_lkey(struct rvt_qp *qp, u32 lkey)
496 {
497         u32 s_last = qp->s_last;
498
499         while (s_last != qp->s_head) {
500                 struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, s_last);
501
502                 if (rvt_swqe_has_lkey(wqe, lkey))
503                         return true;
504
505                 if (++s_last >= qp->s_size)
506                         s_last = 0;
507         }
508         if (qp->s_rdma_mr)
509                 if (rvt_mr_has_lkey(qp->s_rdma_mr, lkey))
510                         return true;
511         return false;
512 }
513
514 /**
515  * rvt_qp_acks_has_lkey - return true if acks have lkey
516  * @qp - the qp
517  * @lkey - the lkey
518  */
519 static bool rvt_qp_acks_has_lkey(struct rvt_qp *qp, u32 lkey)
520 {
521         int i;
522         struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
523
524         for (i = 0; qp->s_ack_queue && i < rvt_max_atomic(rdi); i++) {
525                 struct rvt_ack_entry *e = &qp->s_ack_queue[i];
526
527                 if (rvt_mr_has_lkey(e->rdma_sge.mr, lkey))
528                         return true;
529         }
530         return false;
531 }
532
533 /*
534  * rvt_qp_mr_clean - clean up remote ops for lkey
535  * @qp - the qp
536  * @lkey - the lkey that is being de-registered
537  *
538  * This routine checks if the lkey is being used by
539  * the qp.
540  *
541  * If so, the qp is put into an error state to elminate
542  * any references from the qp.
543  */
544 void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey)
545 {
546         bool lastwqe = false;
547
548         if (qp->ibqp.qp_type == IB_QPT_SMI ||
549             qp->ibqp.qp_type == IB_QPT_GSI)
550                 /* avoid special QPs */
551                 return;
552         spin_lock_irq(&qp->r_lock);
553         spin_lock(&qp->s_hlock);
554         spin_lock(&qp->s_lock);
555
556         if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
557                 goto check_lwqe;
558
559         if (rvt_ss_has_lkey(&qp->r_sge, lkey) ||
560             rvt_qp_sends_has_lkey(qp, lkey) ||
561             rvt_qp_acks_has_lkey(qp, lkey))
562                 lastwqe = rvt_error_qp(qp, IB_WC_LOC_PROT_ERR);
563 check_lwqe:
564         spin_unlock(&qp->s_lock);
565         spin_unlock(&qp->s_hlock);
566         spin_unlock_irq(&qp->r_lock);
567         if (lastwqe) {
568                 struct ib_event ev;
569
570                 ev.device = qp->ibqp.device;
571                 ev.element.qp = &qp->ibqp;
572                 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
573                 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
574         }
575 }
576
577 /**
578  * rvt_remove_qp - remove qp form table
579  * @rdi: rvt dev struct
580  * @qp: qp to remove
581  *
582  * Remove the QP from the table so it can't be found asynchronously by
583  * the receive routine.
584  */
585 static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
586 {
587         struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
588         u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
589         unsigned long flags;
590         int removed = 1;
591
592         spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
593
594         if (rcu_dereference_protected(rvp->qp[0],
595                         lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
596                 RCU_INIT_POINTER(rvp->qp[0], NULL);
597         } else if (rcu_dereference_protected(rvp->qp[1],
598                         lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
599                 RCU_INIT_POINTER(rvp->qp[1], NULL);
600         } else {
601                 struct rvt_qp *q;
602                 struct rvt_qp __rcu **qpp;
603
604                 removed = 0;
605                 qpp = &rdi->qp_dev->qp_table[n];
606                 for (; (q = rcu_dereference_protected(*qpp,
607                         lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
608                         qpp = &q->next) {
609                         if (q == qp) {
610                                 RCU_INIT_POINTER(*qpp,
611                                      rcu_dereference_protected(qp->next,
612                                      lockdep_is_held(&rdi->qp_dev->qpt_lock)));
613                                 removed = 1;
614                                 trace_rvt_qpremove(qp, n);
615                                 break;
616                         }
617                 }
618         }
619
620         spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
621         if (removed) {
622                 synchronize_rcu();
623                 rvt_put_qp(qp);
624         }
625 }
626
627 /**
628  * rvt_init_qp - initialize the QP state to the reset state
629  * @qp: the QP to init or reinit
630  * @type: the QP type
631  *
632  * This function is called from both rvt_create_qp() and
633  * rvt_reset_qp().   The difference is that the reset
634  * patch the necessary locks to protect against concurent
635  * access.
636  */
637 static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
638                         enum ib_qp_type type)
639 {
640         qp->remote_qpn = 0;
641         qp->qkey = 0;
642         qp->qp_access_flags = 0;
643         qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
644         qp->s_hdrwords = 0;
645         qp->s_wqe = NULL;
646         qp->s_draining = 0;
647         qp->s_next_psn = 0;
648         qp->s_last_psn = 0;
649         qp->s_sending_psn = 0;
650         qp->s_sending_hpsn = 0;
651         qp->s_psn = 0;
652         qp->r_psn = 0;
653         qp->r_msn = 0;
654         if (type == IB_QPT_RC) {
655                 qp->s_state = IB_OPCODE_RC_SEND_LAST;
656                 qp->r_state = IB_OPCODE_RC_SEND_LAST;
657         } else {
658                 qp->s_state = IB_OPCODE_UC_SEND_LAST;
659                 qp->r_state = IB_OPCODE_UC_SEND_LAST;
660         }
661         qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
662         qp->r_nak_state = 0;
663         qp->r_aflags = 0;
664         qp->r_flags = 0;
665         qp->s_head = 0;
666         qp->s_tail = 0;
667         qp->s_cur = 0;
668         qp->s_acked = 0;
669         qp->s_last = 0;
670         qp->s_ssn = 1;
671         qp->s_lsn = 0;
672         qp->s_mig_state = IB_MIG_MIGRATED;
673         qp->r_head_ack_queue = 0;
674         qp->s_tail_ack_queue = 0;
675         qp->s_num_rd_atomic = 0;
676         if (qp->r_rq.wq) {
677                 qp->r_rq.wq->head = 0;
678                 qp->r_rq.wq->tail = 0;
679         }
680         qp->r_sge.num_sge = 0;
681         atomic_set(&qp->s_reserved_used, 0);
682 }
683
684 /**
685  * rvt_reset_qp - initialize the QP state to the reset state
686  * @qp: the QP to reset
687  * @type: the QP type
688  *
689  * r_lock, s_hlock, and s_lock are required to be held by the caller
690  */
691 static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
692                          enum ib_qp_type type)
693         __must_hold(&qp->s_lock)
694         __must_hold(&qp->s_hlock)
695         __must_hold(&qp->r_lock)
696 {
697         lockdep_assert_held(&qp->r_lock);
698         lockdep_assert_held(&qp->s_hlock);
699         lockdep_assert_held(&qp->s_lock);
700         if (qp->state != IB_QPS_RESET) {
701                 qp->state = IB_QPS_RESET;
702
703                 /* Let drivers flush their waitlist */
704                 rdi->driver_f.flush_qp_waiters(qp);
705                 rvt_stop_rc_timers(qp);
706                 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
707                 spin_unlock(&qp->s_lock);
708                 spin_unlock(&qp->s_hlock);
709                 spin_unlock_irq(&qp->r_lock);
710
711                 /* Stop the send queue and the retry timer */
712                 rdi->driver_f.stop_send_queue(qp);
713                 rvt_del_timers_sync(qp);
714                 /* Wait for things to stop */
715                 rdi->driver_f.quiesce_qp(qp);
716
717                 /* take qp out the hash and wait for it to be unused */
718                 rvt_remove_qp(rdi, qp);
719                 wait_event(qp->wait, !atomic_read(&qp->refcount));
720
721                 /* grab the lock b/c it was locked at call time */
722                 spin_lock_irq(&qp->r_lock);
723                 spin_lock(&qp->s_hlock);
724                 spin_lock(&qp->s_lock);
725
726                 rvt_clear_mr_refs(qp, 1);
727                 /*
728                  * Let the driver do any tear down or re-init it needs to for
729                  * a qp that has been reset
730                  */
731                 rdi->driver_f.notify_qp_reset(qp);
732         }
733         rvt_init_qp(rdi, qp, type);
734         lockdep_assert_held(&qp->r_lock);
735         lockdep_assert_held(&qp->s_hlock);
736         lockdep_assert_held(&qp->s_lock);
737 }
738
739 /** rvt_free_qpn - Free a qpn from the bit map
740  * @qpt: QP table
741  * @qpn: queue pair number to free
742  */
743 static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
744 {
745         struct rvt_qpn_map *map;
746
747         map = qpt->map + (qpn & RVT_QPN_MASK) / RVT_BITS_PER_PAGE;
748         if (map->page)
749                 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
750 }
751
752 /**
753  * rvt_create_qp - create a queue pair for a device
754  * @ibpd: the protection domain who's device we create the queue pair for
755  * @init_attr: the attributes of the queue pair
756  * @udata: user data for libibverbs.so
757  *
758  * Queue pair creation is mostly an rvt issue. However, drivers have their own
759  * unique idea of what queue pair numbers mean. For instance there is a reserved
760  * range for PSM.
761  *
762  * Return: the queue pair on success, otherwise returns an errno.
763  *
764  * Called by the ib_create_qp() core verbs function.
765  */
766 struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
767                             struct ib_qp_init_attr *init_attr,
768                             struct ib_udata *udata)
769 {
770         struct rvt_qp *qp;
771         int err;
772         struct rvt_swqe *swq = NULL;
773         size_t sz;
774         size_t sg_list_sz;
775         struct ib_qp *ret = ERR_PTR(-ENOMEM);
776         struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
777         void *priv = NULL;
778         size_t sqsize;
779
780         if (!rdi)
781                 return ERR_PTR(-EINVAL);
782
783         if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
784             init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
785             init_attr->create_flags)
786                 return ERR_PTR(-EINVAL);
787
788         /* Check receive queue parameters if no SRQ is specified. */
789         if (!init_attr->srq) {
790                 if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
791                     init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
792                         return ERR_PTR(-EINVAL);
793
794                 if (init_attr->cap.max_send_sge +
795                     init_attr->cap.max_send_wr +
796                     init_attr->cap.max_recv_sge +
797                     init_attr->cap.max_recv_wr == 0)
798                         return ERR_PTR(-EINVAL);
799         }
800         sqsize =
801                 init_attr->cap.max_send_wr + 1 +
802                 rdi->dparms.reserved_operations;
803         switch (init_attr->qp_type) {
804         case IB_QPT_SMI:
805         case IB_QPT_GSI:
806                 if (init_attr->port_num == 0 ||
807                     init_attr->port_num > ibpd->device->phys_port_cnt)
808                         return ERR_PTR(-EINVAL);
809         case IB_QPT_UC:
810         case IB_QPT_RC:
811         case IB_QPT_UD:
812                 sz = sizeof(struct rvt_sge) *
813                         init_attr->cap.max_send_sge +
814                         sizeof(struct rvt_swqe);
815                 swq = vzalloc_node(sqsize * sz, rdi->dparms.node);
816                 if (!swq)
817                         return ERR_PTR(-ENOMEM);
818
819                 sz = sizeof(*qp);
820                 sg_list_sz = 0;
821                 if (init_attr->srq) {
822                         struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
823
824                         if (srq->rq.max_sge > 1)
825                                 sg_list_sz = sizeof(*qp->r_sg_list) *
826                                         (srq->rq.max_sge - 1);
827                 } else if (init_attr->cap.max_recv_sge > 1)
828                         sg_list_sz = sizeof(*qp->r_sg_list) *
829                                 (init_attr->cap.max_recv_sge - 1);
830                 qp = kzalloc_node(sz + sg_list_sz, GFP_KERNEL,
831                                   rdi->dparms.node);
832                 if (!qp)
833                         goto bail_swq;
834
835                 RCU_INIT_POINTER(qp->next, NULL);
836                 if (init_attr->qp_type == IB_QPT_RC) {
837                         qp->s_ack_queue =
838                                 kzalloc_node(
839                                         sizeof(*qp->s_ack_queue) *
840                                          rvt_max_atomic(rdi),
841                                         GFP_KERNEL,
842                                         rdi->dparms.node);
843                         if (!qp->s_ack_queue)
844                                 goto bail_qp;
845                 }
846                 /* initialize timers needed for rc qp */
847                 setup_timer(&qp->s_timer, rvt_rc_timeout, (unsigned long)qp);
848                 hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
849                              HRTIMER_MODE_REL);
850                 qp->s_rnr_timer.function = rvt_rc_rnr_retry;
851
852                 /*
853                  * Driver needs to set up it's private QP structure and do any
854                  * initialization that is needed.
855                  */
856                 priv = rdi->driver_f.qp_priv_alloc(rdi, qp);
857                 if (IS_ERR(priv)) {
858                         ret = priv;
859                         goto bail_qp;
860                 }
861                 qp->priv = priv;
862                 qp->timeout_jiffies =
863                         usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
864                                 1000UL);
865                 if (init_attr->srq) {
866                         sz = 0;
867                 } else {
868                         qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
869                         qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
870                         sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
871                                 sizeof(struct rvt_rwqe);
872                         if (udata)
873                                 qp->r_rq.wq = vmalloc_user(
874                                                 sizeof(struct rvt_rwq) +
875                                                 qp->r_rq.size * sz);
876                         else
877                                 qp->r_rq.wq = vzalloc_node(
878                                                 sizeof(struct rvt_rwq) +
879                                                 qp->r_rq.size * sz,
880                                                 rdi->dparms.node);
881                         if (!qp->r_rq.wq)
882                                 goto bail_driver_priv;
883                 }
884
885                 /*
886                  * ib_create_qp() will initialize qp->ibqp
887                  * except for qp->ibqp.qp_num.
888                  */
889                 spin_lock_init(&qp->r_lock);
890                 spin_lock_init(&qp->s_hlock);
891                 spin_lock_init(&qp->s_lock);
892                 spin_lock_init(&qp->r_rq.lock);
893                 atomic_set(&qp->refcount, 0);
894                 atomic_set(&qp->local_ops_pending, 0);
895                 init_waitqueue_head(&qp->wait);
896                 init_timer(&qp->s_timer);
897                 qp->s_timer.data = (unsigned long)qp;
898                 INIT_LIST_HEAD(&qp->rspwait);
899                 qp->state = IB_QPS_RESET;
900                 qp->s_wq = swq;
901                 qp->s_size = sqsize;
902                 qp->s_avail = init_attr->cap.max_send_wr;
903                 qp->s_max_sge = init_attr->cap.max_send_sge;
904                 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
905                         qp->s_flags = RVT_S_SIGNAL_REQ_WR;
906
907                 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
908                                 init_attr->qp_type,
909                                 init_attr->port_num);
910                 if (err < 0) {
911                         ret = ERR_PTR(err);
912                         goto bail_rq_wq;
913                 }
914                 qp->ibqp.qp_num = err;
915                 qp->port_num = init_attr->port_num;
916                 rvt_init_qp(rdi, qp, init_attr->qp_type);
917                 break;
918
919         default:
920                 /* Don't support raw QPs */
921                 return ERR_PTR(-EINVAL);
922         }
923
924         init_attr->cap.max_inline_data = 0;
925
926         /*
927          * Return the address of the RWQ as the offset to mmap.
928          * See rvt_mmap() for details.
929          */
930         if (udata && udata->outlen >= sizeof(__u64)) {
931                 if (!qp->r_rq.wq) {
932                         __u64 offset = 0;
933
934                         err = ib_copy_to_udata(udata, &offset,
935                                                sizeof(offset));
936                         if (err) {
937                                 ret = ERR_PTR(err);
938                                 goto bail_qpn;
939                         }
940                 } else {
941                         u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
942
943                         qp->ip = rvt_create_mmap_info(rdi, s,
944                                                       ibpd->uobject->context,
945                                                       qp->r_rq.wq);
946                         if (!qp->ip) {
947                                 ret = ERR_PTR(-ENOMEM);
948                                 goto bail_qpn;
949                         }
950
951                         err = ib_copy_to_udata(udata, &qp->ip->offset,
952                                                sizeof(qp->ip->offset));
953                         if (err) {
954                                 ret = ERR_PTR(err);
955                                 goto bail_ip;
956                         }
957                 }
958                 qp->pid = current->pid;
959         }
960
961         spin_lock(&rdi->n_qps_lock);
962         if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
963                 spin_unlock(&rdi->n_qps_lock);
964                 ret = ERR_PTR(-ENOMEM);
965                 goto bail_ip;
966         }
967
968         rdi->n_qps_allocated++;
969         /*
970          * Maintain a busy_jiffies variable that will be added to the timeout
971          * period in mod_retry_timer and add_retry_timer. This busy jiffies
972          * is scaled by the number of rc qps created for the device to reduce
973          * the number of timeouts occurring when there is a large number of
974          * qps. busy_jiffies is incremented every rc qp scaling interval.
975          * The scaling interval is selected based on extensive performance
976          * evaluation of targeted workloads.
977          */
978         if (init_attr->qp_type == IB_QPT_RC) {
979                 rdi->n_rc_qps++;
980                 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
981         }
982         spin_unlock(&rdi->n_qps_lock);
983
984         if (qp->ip) {
985                 spin_lock_irq(&rdi->pending_lock);
986                 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
987                 spin_unlock_irq(&rdi->pending_lock);
988         }
989
990         ret = &qp->ibqp;
991
992         /*
993          * We have our QP and its good, now keep track of what types of opcodes
994          * can be processed on this QP. We do this by keeping track of what the
995          * 3 high order bits of the opcode are.
996          */
997         switch (init_attr->qp_type) {
998         case IB_QPT_SMI:
999         case IB_QPT_GSI:
1000         case IB_QPT_UD:
1001                 qp->allowed_ops = IB_OPCODE_UD;
1002                 break;
1003         case IB_QPT_RC:
1004                 qp->allowed_ops = IB_OPCODE_RC;
1005                 break;
1006         case IB_QPT_UC:
1007                 qp->allowed_ops = IB_OPCODE_UC;
1008                 break;
1009         default:
1010                 ret = ERR_PTR(-EINVAL);
1011                 goto bail_ip;
1012         }
1013
1014         return ret;
1015
1016 bail_ip:
1017         if (qp->ip)
1018                 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1019
1020 bail_qpn:
1021         rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1022
1023 bail_rq_wq:
1024         if (!qp->ip)
1025                 vfree(qp->r_rq.wq);
1026
1027 bail_driver_priv:
1028         rdi->driver_f.qp_priv_free(rdi, qp);
1029
1030 bail_qp:
1031         kfree(qp->s_ack_queue);
1032         kfree(qp);
1033
1034 bail_swq:
1035         vfree(swq);
1036
1037         return ret;
1038 }
1039
1040 /**
1041  * rvt_error_qp - put a QP into the error state
1042  * @qp: the QP to put into the error state
1043  * @err: the receive completion error to signal if a RWQE is active
1044  *
1045  * Flushes both send and receive work queues.
1046  *
1047  * Return: true if last WQE event should be generated.
1048  * The QP r_lock and s_lock should be held and interrupts disabled.
1049  * If we are already in error state, just return.
1050  */
1051 int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
1052 {
1053         struct ib_wc wc;
1054         int ret = 0;
1055         struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
1056
1057         lockdep_assert_held(&qp->r_lock);
1058         lockdep_assert_held(&qp->s_lock);
1059         if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
1060                 goto bail;
1061
1062         qp->state = IB_QPS_ERR;
1063
1064         if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
1065                 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
1066                 del_timer(&qp->s_timer);
1067         }
1068
1069         if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
1070                 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
1071
1072         rdi->driver_f.notify_error_qp(qp);
1073
1074         /* Schedule the sending tasklet to drain the send work queue. */
1075         if (ACCESS_ONCE(qp->s_last) != qp->s_head)
1076                 rdi->driver_f.schedule_send(qp);
1077
1078         rvt_clear_mr_refs(qp, 0);
1079
1080         memset(&wc, 0, sizeof(wc));
1081         wc.qp = &qp->ibqp;
1082         wc.opcode = IB_WC_RECV;
1083
1084         if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
1085                 wc.wr_id = qp->r_wr_id;
1086                 wc.status = err;
1087                 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1088         }
1089         wc.status = IB_WC_WR_FLUSH_ERR;
1090
1091         if (qp->r_rq.wq) {
1092                 struct rvt_rwq *wq;
1093                 u32 head;
1094                 u32 tail;
1095
1096                 spin_lock(&qp->r_rq.lock);
1097
1098                 /* sanity check pointers before trusting them */
1099                 wq = qp->r_rq.wq;
1100                 head = wq->head;
1101                 if (head >= qp->r_rq.size)
1102                         head = 0;
1103                 tail = wq->tail;
1104                 if (tail >= qp->r_rq.size)
1105                         tail = 0;
1106                 while (tail != head) {
1107                         wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
1108                         if (++tail >= qp->r_rq.size)
1109                                 tail = 0;
1110                         rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1111                 }
1112                 wq->tail = tail;
1113
1114                 spin_unlock(&qp->r_rq.lock);
1115         } else if (qp->ibqp.event_handler) {
1116                 ret = 1;
1117         }
1118
1119 bail:
1120         return ret;
1121 }
1122 EXPORT_SYMBOL(rvt_error_qp);
1123
1124 /*
1125  * Put the QP into the hash table.
1126  * The hash table holds a reference to the QP.
1127  */
1128 static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
1129 {
1130         struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
1131         unsigned long flags;
1132
1133         rvt_get_qp(qp);
1134         spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
1135
1136         if (qp->ibqp.qp_num <= 1) {
1137                 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
1138         } else {
1139                 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1140
1141                 qp->next = rdi->qp_dev->qp_table[n];
1142                 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1143                 trace_rvt_qpinsert(qp, n);
1144         }
1145
1146         spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1147 }
1148
1149 /**
1150  * rvt_modify_qp - modify the attributes of a queue pair
1151  * @ibqp: the queue pair who's attributes we're modifying
1152  * @attr: the new attributes
1153  * @attr_mask: the mask of attributes to modify
1154  * @udata: user data for libibverbs.so
1155  *
1156  * Return: 0 on success, otherwise returns an errno.
1157  */
1158 int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1159                   int attr_mask, struct ib_udata *udata)
1160 {
1161         struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1162         struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1163         enum ib_qp_state cur_state, new_state;
1164         struct ib_event ev;
1165         int lastwqe = 0;
1166         int mig = 0;
1167         int pmtu = 0; /* for gcc warning only */
1168         enum rdma_link_layer link;
1169         int opa_ah;
1170
1171         link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
1172
1173         spin_lock_irq(&qp->r_lock);
1174         spin_lock(&qp->s_hlock);
1175         spin_lock(&qp->s_lock);
1176
1177         cur_state = attr_mask & IB_QP_CUR_STATE ?
1178                 attr->cur_qp_state : qp->state;
1179         new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1180         opa_ah = rdma_cap_opa_ah(ibqp->device, qp->port_num);
1181
1182         if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1183                                 attr_mask, link))
1184                 goto inval;
1185
1186         if (rdi->driver_f.check_modify_qp &&
1187             rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1188                 goto inval;
1189
1190         if (attr_mask & IB_QP_AV) {
1191                 if (opa_ah) {
1192                         if (rdma_ah_get_dlid(&attr->ah_attr) >=
1193                                 opa_get_mcast_base(OPA_MCAST_NR))
1194                                 goto inval;
1195                 } else {
1196                         if (rdma_ah_get_dlid(&attr->ah_attr) >=
1197                                 be16_to_cpu(IB_MULTICAST_LID_BASE))
1198                                 goto inval;
1199                 }
1200
1201                 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1202                         goto inval;
1203         }
1204
1205         if (attr_mask & IB_QP_ALT_PATH) {
1206                 if (opa_ah) {
1207                         if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
1208                                 opa_get_mcast_base(OPA_MCAST_NR))
1209                                 goto inval;
1210                 } else {
1211                         if (rdma_ah_get_dlid(&attr->alt_ah_attr) >=
1212                                 be16_to_cpu(IB_MULTICAST_LID_BASE))
1213                                 goto inval;
1214                 }
1215
1216                 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1217                         goto inval;
1218                 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1219                         goto inval;
1220         }
1221
1222         if (attr_mask & IB_QP_PKEY_INDEX)
1223                 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1224                         goto inval;
1225
1226         if (attr_mask & IB_QP_MIN_RNR_TIMER)
1227                 if (attr->min_rnr_timer > 31)
1228                         goto inval;
1229
1230         if (attr_mask & IB_QP_PORT)
1231                 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1232                     qp->ibqp.qp_type == IB_QPT_GSI ||
1233                     attr->port_num == 0 ||
1234                     attr->port_num > ibqp->device->phys_port_cnt)
1235                         goto inval;
1236
1237         if (attr_mask & IB_QP_DEST_QPN)
1238                 if (attr->dest_qp_num > RVT_QPN_MASK)
1239                         goto inval;
1240
1241         if (attr_mask & IB_QP_RETRY_CNT)
1242                 if (attr->retry_cnt > 7)
1243                         goto inval;
1244
1245         if (attr_mask & IB_QP_RNR_RETRY)
1246                 if (attr->rnr_retry > 7)
1247                         goto inval;
1248
1249         /*
1250          * Don't allow invalid path_mtu values.  OK to set greater
1251          * than the active mtu (or even the max_cap, if we have tuned
1252          * that to a small mtu.  We'll set qp->path_mtu
1253          * to the lesser of requested attribute mtu and active,
1254          * for packetizing messages.
1255          * Note that the QP port has to be set in INIT and MTU in RTR.
1256          */
1257         if (attr_mask & IB_QP_PATH_MTU) {
1258                 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1259                 if (pmtu < 0)
1260                         goto inval;
1261         }
1262
1263         if (attr_mask & IB_QP_PATH_MIG_STATE) {
1264                 if (attr->path_mig_state == IB_MIG_REARM) {
1265                         if (qp->s_mig_state == IB_MIG_ARMED)
1266                                 goto inval;
1267                         if (new_state != IB_QPS_RTS)
1268                                 goto inval;
1269                 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1270                         if (qp->s_mig_state == IB_MIG_REARM)
1271                                 goto inval;
1272                         if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1273                                 goto inval;
1274                         if (qp->s_mig_state == IB_MIG_ARMED)
1275                                 mig = 1;
1276                 } else {
1277                         goto inval;
1278                 }
1279         }
1280
1281         if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1282                 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1283                         goto inval;
1284
1285         switch (new_state) {
1286         case IB_QPS_RESET:
1287                 if (qp->state != IB_QPS_RESET)
1288                         rvt_reset_qp(rdi, qp, ibqp->qp_type);
1289                 break;
1290
1291         case IB_QPS_RTR:
1292                 /* Allow event to re-trigger if QP set to RTR more than once */
1293                 qp->r_flags &= ~RVT_R_COMM_EST;
1294                 qp->state = new_state;
1295                 break;
1296
1297         case IB_QPS_SQD:
1298                 qp->s_draining = qp->s_last != qp->s_cur;
1299                 qp->state = new_state;
1300                 break;
1301
1302         case IB_QPS_SQE:
1303                 if (qp->ibqp.qp_type == IB_QPT_RC)
1304                         goto inval;
1305                 qp->state = new_state;
1306                 break;
1307
1308         case IB_QPS_ERR:
1309                 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1310                 break;
1311
1312         default:
1313                 qp->state = new_state;
1314                 break;
1315         }
1316
1317         if (attr_mask & IB_QP_PKEY_INDEX)
1318                 qp->s_pkey_index = attr->pkey_index;
1319
1320         if (attr_mask & IB_QP_PORT)
1321                 qp->port_num = attr->port_num;
1322
1323         if (attr_mask & IB_QP_DEST_QPN)
1324                 qp->remote_qpn = attr->dest_qp_num;
1325
1326         if (attr_mask & IB_QP_SQ_PSN) {
1327                 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1328                 qp->s_psn = qp->s_next_psn;
1329                 qp->s_sending_psn = qp->s_next_psn;
1330                 qp->s_last_psn = qp->s_next_psn - 1;
1331                 qp->s_sending_hpsn = qp->s_last_psn;
1332         }
1333
1334         if (attr_mask & IB_QP_RQ_PSN)
1335                 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1336
1337         if (attr_mask & IB_QP_ACCESS_FLAGS)
1338                 qp->qp_access_flags = attr->qp_access_flags;
1339
1340         if (attr_mask & IB_QP_AV) {
1341                 qp->remote_ah_attr = attr->ah_attr;
1342                 qp->s_srate = rdma_ah_get_static_rate(&attr->ah_attr);
1343                 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1344         }
1345
1346         if (attr_mask & IB_QP_ALT_PATH) {
1347                 qp->alt_ah_attr = attr->alt_ah_attr;
1348                 qp->s_alt_pkey_index = attr->alt_pkey_index;
1349         }
1350
1351         if (attr_mask & IB_QP_PATH_MIG_STATE) {
1352                 qp->s_mig_state = attr->path_mig_state;
1353                 if (mig) {
1354                         qp->remote_ah_attr = qp->alt_ah_attr;
1355                         qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
1356                         qp->s_pkey_index = qp->s_alt_pkey_index;
1357                 }
1358         }
1359
1360         if (attr_mask & IB_QP_PATH_MTU) {
1361                 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1362                 qp->log_pmtu = ilog2(qp->pmtu);
1363         }
1364
1365         if (attr_mask & IB_QP_RETRY_CNT) {
1366                 qp->s_retry_cnt = attr->retry_cnt;
1367                 qp->s_retry = attr->retry_cnt;
1368         }
1369
1370         if (attr_mask & IB_QP_RNR_RETRY) {
1371                 qp->s_rnr_retry_cnt = attr->rnr_retry;
1372                 qp->s_rnr_retry = attr->rnr_retry;
1373         }
1374
1375         if (attr_mask & IB_QP_MIN_RNR_TIMER)
1376                 qp->r_min_rnr_timer = attr->min_rnr_timer;
1377
1378         if (attr_mask & IB_QP_TIMEOUT) {
1379                 qp->timeout = attr->timeout;
1380                 qp->timeout_jiffies = rvt_timeout_to_jiffies(qp->timeout);
1381         }
1382
1383         if (attr_mask & IB_QP_QKEY)
1384                 qp->qkey = attr->qkey;
1385
1386         if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1387                 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1388
1389         if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1390                 qp->s_max_rd_atomic = attr->max_rd_atomic;
1391
1392         if (rdi->driver_f.modify_qp)
1393                 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1394
1395         spin_unlock(&qp->s_lock);
1396         spin_unlock(&qp->s_hlock);
1397         spin_unlock_irq(&qp->r_lock);
1398
1399         if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1400                 rvt_insert_qp(rdi, qp);
1401
1402         if (lastwqe) {
1403                 ev.device = qp->ibqp.device;
1404                 ev.element.qp = &qp->ibqp;
1405                 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1406                 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1407         }
1408         if (mig) {
1409                 ev.device = qp->ibqp.device;
1410                 ev.element.qp = &qp->ibqp;
1411                 ev.event = IB_EVENT_PATH_MIG;
1412                 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1413         }
1414         return 0;
1415
1416 inval:
1417         spin_unlock(&qp->s_lock);
1418         spin_unlock(&qp->s_hlock);
1419         spin_unlock_irq(&qp->r_lock);
1420         return -EINVAL;
1421 }
1422
1423 /**
1424  * rvt_destroy_qp - destroy a queue pair
1425  * @ibqp: the queue pair to destroy
1426  *
1427  * Note that this can be called while the QP is actively sending or
1428  * receiving!
1429  *
1430  * Return: 0 on success.
1431  */
1432 int rvt_destroy_qp(struct ib_qp *ibqp)
1433 {
1434         struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1435         struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1436
1437         spin_lock_irq(&qp->r_lock);
1438         spin_lock(&qp->s_hlock);
1439         spin_lock(&qp->s_lock);
1440         rvt_reset_qp(rdi, qp, ibqp->qp_type);
1441         spin_unlock(&qp->s_lock);
1442         spin_unlock(&qp->s_hlock);
1443         spin_unlock_irq(&qp->r_lock);
1444
1445         /* qpn is now available for use again */
1446         rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1447
1448         spin_lock(&rdi->n_qps_lock);
1449         rdi->n_qps_allocated--;
1450         if (qp->ibqp.qp_type == IB_QPT_RC) {
1451                 rdi->n_rc_qps--;
1452                 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1453         }
1454         spin_unlock(&rdi->n_qps_lock);
1455
1456         if (qp->ip)
1457                 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1458         else
1459                 vfree(qp->r_rq.wq);
1460         vfree(qp->s_wq);
1461         rdi->driver_f.qp_priv_free(rdi, qp);
1462         kfree(qp->s_ack_queue);
1463         kfree(qp);
1464         return 0;
1465 }
1466
1467 /**
1468  * rvt_query_qp - query an ipbq
1469  * @ibqp: IB qp to query
1470  * @attr: attr struct to fill in
1471  * @attr_mask: attr mask ignored
1472  * @init_attr: struct to fill in
1473  *
1474  * Return: always 0
1475  */
1476 int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1477                  int attr_mask, struct ib_qp_init_attr *init_attr)
1478 {
1479         struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1480         struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1481
1482         attr->qp_state = qp->state;
1483         attr->cur_qp_state = attr->qp_state;
1484         attr->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
1485         attr->path_mig_state = qp->s_mig_state;
1486         attr->qkey = qp->qkey;
1487         attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1488         attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1489         attr->dest_qp_num = qp->remote_qpn;
1490         attr->qp_access_flags = qp->qp_access_flags;
1491         attr->cap.max_send_wr = qp->s_size - 1 -
1492                 rdi->dparms.reserved_operations;
1493         attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1494         attr->cap.max_send_sge = qp->s_max_sge;
1495         attr->cap.max_recv_sge = qp->r_rq.max_sge;
1496         attr->cap.max_inline_data = 0;
1497         attr->ah_attr = qp->remote_ah_attr;
1498         attr->alt_ah_attr = qp->alt_ah_attr;
1499         attr->pkey_index = qp->s_pkey_index;
1500         attr->alt_pkey_index = qp->s_alt_pkey_index;
1501         attr->en_sqd_async_notify = 0;
1502         attr->sq_draining = qp->s_draining;
1503         attr->max_rd_atomic = qp->s_max_rd_atomic;
1504         attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1505         attr->min_rnr_timer = qp->r_min_rnr_timer;
1506         attr->port_num = qp->port_num;
1507         attr->timeout = qp->timeout;
1508         attr->retry_cnt = qp->s_retry_cnt;
1509         attr->rnr_retry = qp->s_rnr_retry_cnt;
1510         attr->alt_port_num =
1511                 rdma_ah_get_port_num(&qp->alt_ah_attr);
1512         attr->alt_timeout = qp->alt_timeout;
1513
1514         init_attr->event_handler = qp->ibqp.event_handler;
1515         init_attr->qp_context = qp->ibqp.qp_context;
1516         init_attr->send_cq = qp->ibqp.send_cq;
1517         init_attr->recv_cq = qp->ibqp.recv_cq;
1518         init_attr->srq = qp->ibqp.srq;
1519         init_attr->cap = attr->cap;
1520         if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1521                 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1522         else
1523                 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1524         init_attr->qp_type = qp->ibqp.qp_type;
1525         init_attr->port_num = qp->port_num;
1526         return 0;
1527 }
1528
1529 /**
1530  * rvt_post_receive - post a receive on a QP
1531  * @ibqp: the QP to post the receive on
1532  * @wr: the WR to post
1533  * @bad_wr: the first bad WR is put here
1534  *
1535  * This may be called from interrupt context.
1536  *
1537  * Return: 0 on success otherwise errno
1538  */
1539 int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1540                   struct ib_recv_wr **bad_wr)
1541 {
1542         struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1543         struct rvt_rwq *wq = qp->r_rq.wq;
1544         unsigned long flags;
1545         int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
1546                                 !qp->ibqp.srq;
1547
1548         /* Check that state is OK to post receive. */
1549         if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1550                 *bad_wr = wr;
1551                 return -EINVAL;
1552         }
1553
1554         for (; wr; wr = wr->next) {
1555                 struct rvt_rwqe *wqe;
1556                 u32 next;
1557                 int i;
1558
1559                 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1560                         *bad_wr = wr;
1561                         return -EINVAL;
1562                 }
1563
1564                 spin_lock_irqsave(&qp->r_rq.lock, flags);
1565                 next = wq->head + 1;
1566                 if (next >= qp->r_rq.size)
1567                         next = 0;
1568                 if (next == wq->tail) {
1569                         spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1570                         *bad_wr = wr;
1571                         return -ENOMEM;
1572                 }
1573                 if (unlikely(qp_err_flush)) {
1574                         struct ib_wc wc;
1575
1576                         memset(&wc, 0, sizeof(wc));
1577                         wc.qp = &qp->ibqp;
1578                         wc.opcode = IB_WC_RECV;
1579                         wc.wr_id = wr->wr_id;
1580                         wc.status = IB_WC_WR_FLUSH_ERR;
1581                         rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1582                 } else {
1583                         wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1584                         wqe->wr_id = wr->wr_id;
1585                         wqe->num_sge = wr->num_sge;
1586                         for (i = 0; i < wr->num_sge; i++)
1587                                 wqe->sg_list[i] = wr->sg_list[i];
1588                         /*
1589                          * Make sure queue entry is written
1590                          * before the head index.
1591                          */
1592                         smp_wmb();
1593                         wq->head = next;
1594                 }
1595                 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1596         }
1597         return 0;
1598 }
1599
1600 /**
1601  * rvt_qp_valid_operation - validate post send wr request
1602  * @qp - the qp
1603  * @post-parms - the post send table for the driver
1604  * @wr - the work request
1605  *
1606  * The routine validates the operation based on the
1607  * validation table an returns the length of the operation
1608  * which can extend beyond the ib_send_bw.  Operation
1609  * dependent flags key atomic operation validation.
1610  *
1611  * There is an exception for UD qps that validates the pd and
1612  * overrides the length to include the additional UD specific
1613  * length.
1614  *
1615  * Returns a negative error or the length of the work request
1616  * for building the swqe.
1617  */
1618 static inline int rvt_qp_valid_operation(
1619         struct rvt_qp *qp,
1620         const struct rvt_operation_params *post_parms,
1621         struct ib_send_wr *wr)
1622 {
1623         int len;
1624
1625         if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
1626                 return -EINVAL;
1627         if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
1628                 return -EINVAL;
1629         if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
1630             ibpd_to_rvtpd(qp->ibqp.pd)->user)
1631                 return -EINVAL;
1632         if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
1633             (wr->num_sge == 0 ||
1634              wr->sg_list[0].length < sizeof(u64) ||
1635              wr->sg_list[0].addr & (sizeof(u64) - 1)))
1636                 return -EINVAL;
1637         if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
1638             !qp->s_max_rd_atomic)
1639                 return -EINVAL;
1640         len = post_parms[wr->opcode].length;
1641         /* UD specific */
1642         if (qp->ibqp.qp_type != IB_QPT_UC &&
1643             qp->ibqp.qp_type != IB_QPT_RC) {
1644                 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1645                         return -EINVAL;
1646                 len = sizeof(struct ib_ud_wr);
1647         }
1648         return len;
1649 }
1650
1651 /**
1652  * rvt_qp_is_avail - determine queue capacity
1653  * @qp - the qp
1654  * @rdi - the rdmavt device
1655  * @reserved_op - is reserved operation
1656  *
1657  * This assumes the s_hlock is held but the s_last
1658  * qp variable is uncontrolled.
1659  *
1660  * For non reserved operations, the qp->s_avail
1661  * may be changed.
1662  *
1663  * The return value is zero or a -ENOMEM.
1664  */
1665 static inline int rvt_qp_is_avail(
1666         struct rvt_qp *qp,
1667         struct rvt_dev_info *rdi,
1668         bool reserved_op)
1669 {
1670         u32 slast;
1671         u32 avail;
1672         u32 reserved_used;
1673
1674         /* see rvt_qp_wqe_unreserve() */
1675         smp_mb__before_atomic();
1676         reserved_used = atomic_read(&qp->s_reserved_used);
1677         if (unlikely(reserved_op)) {
1678                 /* see rvt_qp_wqe_unreserve() */
1679                 smp_mb__before_atomic();
1680                 if (reserved_used >= rdi->dparms.reserved_operations)
1681                         return -ENOMEM;
1682                 return 0;
1683         }
1684         /* non-reserved operations */
1685         if (likely(qp->s_avail))
1686                 return 0;
1687         smp_read_barrier_depends(); /* see rc.c */
1688         slast = ACCESS_ONCE(qp->s_last);
1689         if (qp->s_head >= slast)
1690                 avail = qp->s_size - (qp->s_head - slast);
1691         else
1692                 avail = slast - qp->s_head;
1693
1694         /* see rvt_qp_wqe_unreserve() */
1695         smp_mb__before_atomic();
1696         reserved_used = atomic_read(&qp->s_reserved_used);
1697         avail =  avail - 1 -
1698                 (rdi->dparms.reserved_operations - reserved_used);
1699         /* insure we don't assign a negative s_avail */
1700         if ((s32)avail <= 0)
1701                 return -ENOMEM;
1702         qp->s_avail = avail;
1703         if (WARN_ON(qp->s_avail >
1704                     (qp->s_size - 1 - rdi->dparms.reserved_operations)))
1705                 rvt_pr_err(rdi,
1706                            "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1707                            qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1708                            qp->s_head, qp->s_tail, qp->s_cur,
1709                            qp->s_acked, qp->s_last);
1710         return 0;
1711 }
1712
1713 /**
1714  * rvt_post_one_wr - post one RC, UC, or UD send work request
1715  * @qp: the QP to post on
1716  * @wr: the work request to send
1717  */
1718 static int rvt_post_one_wr(struct rvt_qp *qp,
1719                            struct ib_send_wr *wr,
1720                            int *call_send)
1721 {
1722         struct rvt_swqe *wqe;
1723         u32 next;
1724         int i;
1725         int j;
1726         int acc;
1727         struct rvt_lkey_table *rkt;
1728         struct rvt_pd *pd;
1729         struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
1730         u8 log_pmtu;
1731         int ret;
1732         size_t cplen;
1733         bool reserved_op;
1734         int local_ops_delayed = 0;
1735
1736         BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
1737
1738         /* IB spec says that num_sge == 0 is OK. */
1739         if (unlikely(wr->num_sge > qp->s_max_sge))
1740                 return -EINVAL;
1741
1742         ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
1743         if (ret < 0)
1744                 return ret;
1745         cplen = ret;
1746
1747         /*
1748          * Local operations include fast register and local invalidate.
1749          * Fast register needs to be processed immediately because the
1750          * registered lkey may be used by following work requests and the
1751          * lkey needs to be valid at the time those requests are posted.
1752          * Local invalidate can be processed immediately if fencing is
1753          * not required and no previous local invalidate ops are pending.
1754          * Signaled local operations that have been processed immediately
1755          * need to have requests with "completion only" flags set posted
1756          * to the send queue in order to generate completions.
1757          */
1758         if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
1759                 switch (wr->opcode) {
1760                 case IB_WR_REG_MR:
1761                         ret = rvt_fast_reg_mr(qp,
1762                                               reg_wr(wr)->mr,
1763                                               reg_wr(wr)->key,
1764                                               reg_wr(wr)->access);
1765                         if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1766                                 return ret;
1767                         break;
1768                 case IB_WR_LOCAL_INV:
1769                         if ((wr->send_flags & IB_SEND_FENCE) ||
1770                             atomic_read(&qp->local_ops_pending)) {
1771                                 local_ops_delayed = 1;
1772                         } else {
1773                                 ret = rvt_invalidate_rkey(
1774                                         qp, wr->ex.invalidate_rkey);
1775                                 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1776                                         return ret;
1777                         }
1778                         break;
1779                 default:
1780                         return -EINVAL;
1781                 }
1782         }
1783
1784         reserved_op = rdi->post_parms[wr->opcode].flags &
1785                         RVT_OPERATION_USE_RESERVE;
1786         /* check for avail */
1787         ret = rvt_qp_is_avail(qp, rdi, reserved_op);
1788         if (ret)
1789                 return ret;
1790         next = qp->s_head + 1;
1791         if (next >= qp->s_size)
1792                 next = 0;
1793
1794         rkt = &rdi->lkey_table;
1795         pd = ibpd_to_rvtpd(qp->ibqp.pd);
1796         wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1797
1798         /* cplen has length from above */
1799         memcpy(&wqe->wr, wr, cplen);
1800
1801         wqe->length = 0;
1802         j = 0;
1803         if (wr->num_sge) {
1804                 struct rvt_sge *last_sge = NULL;
1805
1806                 acc = wr->opcode >= IB_WR_RDMA_READ ?
1807                         IB_ACCESS_LOCAL_WRITE : 0;
1808                 for (i = 0; i < wr->num_sge; i++) {
1809                         u32 length = wr->sg_list[i].length;
1810
1811                         if (length == 0)
1812                                 continue;
1813                         ret = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j], last_sge,
1814                                           &wr->sg_list[i], acc);
1815                         if (unlikely(ret < 0))
1816                                 goto bail_inval_free;
1817                         wqe->length += length;
1818                         if (ret)
1819                                 last_sge = &wqe->sg_list[j];
1820                         j += ret;
1821                 }
1822                 wqe->wr.num_sge = j;
1823         }
1824
1825         /* general part of wqe valid - allow for driver checks */
1826         if (rdi->driver_f.check_send_wqe) {
1827                 ret = rdi->driver_f.check_send_wqe(qp, wqe);
1828                 if (ret < 0)
1829                         goto bail_inval_free;
1830                 if (ret)
1831                         *call_send = ret;
1832         }
1833
1834         log_pmtu = qp->log_pmtu;
1835         if (qp->ibqp.qp_type != IB_QPT_UC &&
1836             qp->ibqp.qp_type != IB_QPT_RC) {
1837                 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1838
1839                 log_pmtu = ah->log_pmtu;
1840                 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1841         }
1842
1843         if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
1844                 if (local_ops_delayed)
1845                         atomic_inc(&qp->local_ops_pending);
1846                 else
1847                         wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
1848                 wqe->ssn = 0;
1849                 wqe->psn = 0;
1850                 wqe->lpsn = 0;
1851         } else {
1852                 wqe->ssn = qp->s_ssn++;
1853                 wqe->psn = qp->s_next_psn;
1854                 wqe->lpsn = wqe->psn +
1855                                 (wqe->length ?
1856                                         ((wqe->length - 1) >> log_pmtu) :
1857                                         0);
1858                 qp->s_next_psn = wqe->lpsn + 1;
1859         }
1860         if (unlikely(reserved_op)) {
1861                 wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;
1862                 rvt_qp_wqe_reserve(qp, wqe);
1863         } else {
1864                 wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;
1865                 qp->s_avail--;
1866         }
1867         trace_rvt_post_one_wr(qp, wqe, wr->num_sge);
1868         smp_wmb(); /* see request builders */
1869         qp->s_head = next;
1870
1871         return 0;
1872
1873 bail_inval_free:
1874         /* release mr holds */
1875         while (j) {
1876                 struct rvt_sge *sge = &wqe->sg_list[--j];
1877
1878                 rvt_put_mr(sge->mr);
1879         }
1880         return ret;
1881 }
1882
1883 /**
1884  * rvt_post_send - post a send on a QP
1885  * @ibqp: the QP to post the send on
1886  * @wr: the list of work requests to post
1887  * @bad_wr: the first bad WR is put here
1888  *
1889  * This may be called from interrupt context.
1890  *
1891  * Return: 0 on success else errno
1892  */
1893 int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1894                   struct ib_send_wr **bad_wr)
1895 {
1896         struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1897         struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1898         unsigned long flags = 0;
1899         int call_send;
1900         unsigned nreq = 0;
1901         int err = 0;
1902
1903         spin_lock_irqsave(&qp->s_hlock, flags);
1904
1905         /*
1906          * Ensure QP state is such that we can send. If not bail out early,
1907          * there is no need to do this every time we post a send.
1908          */
1909         if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
1910                 spin_unlock_irqrestore(&qp->s_hlock, flags);
1911                 return -EINVAL;
1912         }
1913
1914         /*
1915          * If the send queue is empty, and we only have a single WR then just go
1916          * ahead and kick the send engine into gear. Otherwise we will always
1917          * just schedule the send to happen later.
1918          */
1919         call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
1920
1921         for (; wr; wr = wr->next) {
1922                 err = rvt_post_one_wr(qp, wr, &call_send);
1923                 if (unlikely(err)) {
1924                         *bad_wr = wr;
1925                         goto bail;
1926                 }
1927                 nreq++;
1928         }
1929 bail:
1930         spin_unlock_irqrestore(&qp->s_hlock, flags);
1931         if (nreq) {
1932                 if (call_send)
1933                         rdi->driver_f.do_send(qp);
1934                 else
1935                         rdi->driver_f.schedule_send_no_lock(qp);
1936         }
1937         return err;
1938 }
1939
1940 /**
1941  * rvt_post_srq_receive - post a receive on a shared receive queue
1942  * @ibsrq: the SRQ to post the receive on
1943  * @wr: the list of work requests to post
1944  * @bad_wr: A pointer to the first WR to cause a problem is put here
1945  *
1946  * This may be called from interrupt context.
1947  *
1948  * Return: 0 on success else errno
1949  */
1950 int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1951                       struct ib_recv_wr **bad_wr)
1952 {
1953         struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1954         struct rvt_rwq *wq;
1955         unsigned long flags;
1956
1957         for (; wr; wr = wr->next) {
1958                 struct rvt_rwqe *wqe;
1959                 u32 next;
1960                 int i;
1961
1962                 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1963                         *bad_wr = wr;
1964                         return -EINVAL;
1965                 }
1966
1967                 spin_lock_irqsave(&srq->rq.lock, flags);
1968                 wq = srq->rq.wq;
1969                 next = wq->head + 1;
1970                 if (next >= srq->rq.size)
1971                         next = 0;
1972                 if (next == wq->tail) {
1973                         spin_unlock_irqrestore(&srq->rq.lock, flags);
1974                         *bad_wr = wr;
1975                         return -ENOMEM;
1976                 }
1977
1978                 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1979                 wqe->wr_id = wr->wr_id;
1980                 wqe->num_sge = wr->num_sge;
1981                 for (i = 0; i < wr->num_sge; i++)
1982                         wqe->sg_list[i] = wr->sg_list[i];
1983                 /* Make sure queue entry is written before the head index. */
1984                 smp_wmb();
1985                 wq->head = next;
1986                 spin_unlock_irqrestore(&srq->rq.lock, flags);
1987         }
1988         return 0;
1989 }
1990
1991 /**
1992  * qp_comm_est - handle trap with QP established
1993  * @qp: the QP
1994  */
1995 void rvt_comm_est(struct rvt_qp *qp)
1996 {
1997         qp->r_flags |= RVT_R_COMM_EST;
1998         if (qp->ibqp.event_handler) {
1999                 struct ib_event ev;
2000
2001                 ev.device = qp->ibqp.device;
2002                 ev.element.qp = &qp->ibqp;
2003                 ev.event = IB_EVENT_COMM_EST;
2004                 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2005         }
2006 }
2007 EXPORT_SYMBOL(rvt_comm_est);
2008
2009 void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
2010 {
2011         unsigned long flags;
2012         int lastwqe;
2013
2014         spin_lock_irqsave(&qp->s_lock, flags);
2015         lastwqe = rvt_error_qp(qp, err);
2016         spin_unlock_irqrestore(&qp->s_lock, flags);
2017
2018         if (lastwqe) {
2019                 struct ib_event ev;
2020
2021                 ev.device = qp->ibqp.device;
2022                 ev.element.qp = &qp->ibqp;
2023                 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
2024                 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
2025         }
2026 }
2027 EXPORT_SYMBOL(rvt_rc_error);
2028
2029 /*
2030  *  rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
2031  *  @index - the index
2032  *  return usec from an index into ib_rvt_rnr_table
2033  */
2034 unsigned long rvt_rnr_tbl_to_usec(u32 index)
2035 {
2036         return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
2037 }
2038 EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
2039
2040 static inline unsigned long rvt_aeth_to_usec(u32 aeth)
2041 {
2042         return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
2043                                   IB_AETH_CREDIT_MASK];
2044 }
2045
2046 /*
2047  *  rvt_add_retry_timer - add/start a retry timer
2048  *  @qp - the QP
2049  *  add a retry timer on the QP
2050  */
2051 void rvt_add_retry_timer(struct rvt_qp *qp)
2052 {
2053         struct ib_qp *ibqp = &qp->ibqp;
2054         struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
2055
2056         lockdep_assert_held(&qp->s_lock);
2057         qp->s_flags |= RVT_S_TIMER;
2058        /* 4.096 usec. * (1 << qp->timeout) */
2059         qp->s_timer.expires = jiffies + qp->timeout_jiffies +
2060                              rdi->busy_jiffies;
2061         add_timer(&qp->s_timer);
2062 }
2063 EXPORT_SYMBOL(rvt_add_retry_timer);
2064
2065 /**
2066  * rvt_add_rnr_timer - add/start an rnr timer
2067  * @qp - the QP
2068  * @aeth - aeth of RNR timeout, simulated aeth for loopback
2069  * add an rnr timer on the QP
2070  */
2071 void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
2072 {
2073         u32 to;
2074
2075         lockdep_assert_held(&qp->s_lock);
2076         qp->s_flags |= RVT_S_WAIT_RNR;
2077         to = rvt_aeth_to_usec(aeth);
2078         hrtimer_start(&qp->s_rnr_timer,
2079                       ns_to_ktime(1000 * to), HRTIMER_MODE_REL);
2080 }
2081 EXPORT_SYMBOL(rvt_add_rnr_timer);
2082
2083 /**
2084  * rvt_stop_rc_timers - stop all timers
2085  * @qp - the QP
2086  * stop any pending timers
2087  */
2088 void rvt_stop_rc_timers(struct rvt_qp *qp)
2089 {
2090         lockdep_assert_held(&qp->s_lock);
2091         /* Remove QP from all timers */
2092         if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
2093                 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
2094                 del_timer(&qp->s_timer);
2095                 hrtimer_try_to_cancel(&qp->s_rnr_timer);
2096         }
2097 }
2098 EXPORT_SYMBOL(rvt_stop_rc_timers);
2099
2100 /**
2101  * rvt_stop_rnr_timer - stop an rnr timer
2102  * @qp - the QP
2103  *
2104  * stop an rnr timer and return if the timer
2105  * had been pending.
2106  */
2107 static int rvt_stop_rnr_timer(struct rvt_qp *qp)
2108 {
2109         int rval = 0;
2110
2111         lockdep_assert_held(&qp->s_lock);
2112         /* Remove QP from rnr timer */
2113         if (qp->s_flags & RVT_S_WAIT_RNR) {
2114                 qp->s_flags &= ~RVT_S_WAIT_RNR;
2115                 rval = hrtimer_try_to_cancel(&qp->s_rnr_timer);
2116         }
2117         return rval;
2118 }
2119
2120 /**
2121  * rvt_del_timers_sync - wait for any timeout routines to exit
2122  * @qp - the QP
2123  */
2124 void rvt_del_timers_sync(struct rvt_qp *qp)
2125 {
2126         del_timer_sync(&qp->s_timer);
2127         hrtimer_cancel(&qp->s_rnr_timer);
2128 }
2129 EXPORT_SYMBOL(rvt_del_timers_sync);
2130
2131 /**
2132  * This is called from s_timer for missing responses.
2133  */
2134 static void rvt_rc_timeout(unsigned long arg)
2135 {
2136         struct rvt_qp *qp = (struct rvt_qp *)arg;
2137         struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2138         unsigned long flags;
2139
2140         spin_lock_irqsave(&qp->r_lock, flags);
2141         spin_lock(&qp->s_lock);
2142         if (qp->s_flags & RVT_S_TIMER) {
2143                 struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
2144
2145                 qp->s_flags &= ~RVT_S_TIMER;
2146                 rvp->n_rc_timeouts++;
2147                 del_timer(&qp->s_timer);
2148                 trace_rvt_rc_timeout(qp, qp->s_last_psn + 1);
2149                 if (rdi->driver_f.notify_restart_rc)
2150                         rdi->driver_f.notify_restart_rc(qp,
2151                                                         qp->s_last_psn + 1,
2152                                                         1);
2153                 rdi->driver_f.schedule_send(qp);
2154         }
2155         spin_unlock(&qp->s_lock);
2156         spin_unlock_irqrestore(&qp->r_lock, flags);
2157 }
2158
2159 /*
2160  * This is called from s_timer for RNR timeouts.
2161  */
2162 enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
2163 {
2164         struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
2165         struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
2166         unsigned long flags;
2167
2168         spin_lock_irqsave(&qp->s_lock, flags);
2169         rvt_stop_rnr_timer(qp);
2170         rdi->driver_f.schedule_send(qp);
2171         spin_unlock_irqrestore(&qp->s_lock, flags);
2172         return HRTIMER_NORESTART;
2173 }
2174 EXPORT_SYMBOL(rvt_rc_rnr_retry);
2175
2176 /**
2177  * rvt_qp_iter_init - initial for QP iteration
2178  * @rdi - rvt devinfo
2179  * @v - u64 value
2180  *
2181  * This returns an iterator suitable for iterating QPs
2182  * in the system.
2183  *
2184  * The @cb is a user defined callback and @v is a 64
2185  * bit value passed to and relevant for processing in the
2186  * @cb.  An example use case would be to alter QP processing
2187  * based on criteria not part of the rvt_qp.
2188  *
2189  * Use cases that require memory allocation to succeed
2190  * must preallocate appropriately.
2191  *
2192  * Return: a pointer to an rvt_qp_iter or NULL
2193  */
2194 struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
2195                                      u64 v,
2196                                      void (*cb)(struct rvt_qp *qp, u64 v))
2197 {
2198         struct rvt_qp_iter *i;
2199
2200         i = kzalloc(sizeof(*i), GFP_KERNEL);
2201         if (!i)
2202                 return NULL;
2203
2204         i->rdi = rdi;
2205         /* number of special QPs (SMI/GSI) for device */
2206         i->specials = rdi->ibdev.phys_port_cnt * 2;
2207         i->v = v;
2208         i->cb = cb;
2209
2210         return i;
2211 }
2212 EXPORT_SYMBOL(rvt_qp_iter_init);
2213
2214 /**
2215  * rvt_qp_iter_next - return the next QP in iter
2216  * @iter - the iterator
2217  *
2218  * Fine grained QP iterator suitable for use
2219  * with debugfs seq_file mechanisms.
2220  *
2221  * Updates iter->qp with the current QP when the return
2222  * value is 0.
2223  *
2224  * Return: 0 - iter->qp is valid 1 - no more QPs
2225  */
2226 int rvt_qp_iter_next(struct rvt_qp_iter *iter)
2227         __must_hold(RCU)
2228 {
2229         int n = iter->n;
2230         int ret = 1;
2231         struct rvt_qp *pqp = iter->qp;
2232         struct rvt_qp *qp;
2233         struct rvt_dev_info *rdi = iter->rdi;
2234
2235         /*
2236          * The approach is to consider the special qps
2237          * as additional table entries before the
2238          * real hash table.  Since the qp code sets
2239          * the qp->next hash link to NULL, this works just fine.
2240          *
2241          * iter->specials is 2 * # ports
2242          *
2243          * n = 0..iter->specials is the special qp indices
2244          *
2245          * n = iter->specials..rdi->qp_dev->qp_table_size+iter->specials are
2246          * the potential hash bucket entries
2247          *
2248          */
2249         for (; n <  rdi->qp_dev->qp_table_size + iter->specials; n++) {
2250                 if (pqp) {
2251                         qp = rcu_dereference(pqp->next);
2252                 } else {
2253                         if (n < iter->specials) {
2254                                 struct rvt_ibport *rvp;
2255                                 int pidx;
2256
2257                                 pidx = n % rdi->ibdev.phys_port_cnt;
2258                                 rvp = rdi->ports[pidx];
2259                                 qp = rcu_dereference(rvp->qp[n & 1]);
2260                         } else {
2261                                 qp = rcu_dereference(
2262                                         rdi->qp_dev->qp_table[
2263                                                 (n - iter->specials)]);
2264                         }
2265                 }
2266                 pqp = qp;
2267                 if (qp) {
2268                         iter->qp = qp;
2269                         iter->n = n;
2270                         return 0;
2271                 }
2272         }
2273         return ret;
2274 }
2275 EXPORT_SYMBOL(rvt_qp_iter_next);
2276
2277 /**
2278  * rvt_qp_iter - iterate all QPs
2279  * @rdi - rvt devinfo
2280  * @v - a 64 bit value
2281  * @cb - a callback
2282  *
2283  * This provides a way for iterating all QPs.
2284  *
2285  * The @cb is a user defined callback and @v is a 64
2286  * bit value passed to and relevant for processing in the
2287  * cb.  An example use case would be to alter QP processing
2288  * based on criteria not part of the rvt_qp.
2289  *
2290  * The code has an internal iterator to simplify
2291  * non seq_file use cases.
2292  */
2293 void rvt_qp_iter(struct rvt_dev_info *rdi,
2294                  u64 v,
2295                  void (*cb)(struct rvt_qp *qp, u64 v))
2296 {
2297         int ret;
2298         struct rvt_qp_iter i = {
2299                 .rdi = rdi,
2300                 .specials = rdi->ibdev.phys_port_cnt * 2,
2301                 .v = v,
2302                 .cb = cb
2303         };
2304
2305         rcu_read_lock();
2306         do {
2307                 ret = rvt_qp_iter_next(&i);
2308                 if (!ret) {
2309                         rvt_get_qp(i.qp);
2310                         rcu_read_unlock();
2311                         i.cb(i.qp, i.v);
2312                         rcu_read_lock();
2313                         rvt_put_qp(i.qp);
2314                 }
2315         } while (!ret);
2316         rcu_read_unlock();
2317 }
2318 EXPORT_SYMBOL(rvt_qp_iter);