GNU Linux-libre 4.9.284-gnu1
[releases.git] / drivers / infiniband / sw / rdmavt / qp.c
1 /*
2  * Copyright(c) 2016 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 "qp.h"
55 #include "vt.h"
56 #include "trace.h"
57
58 /*
59  * Note that it is OK to post send work requests in the SQE and ERR
60  * states; rvt_do_send() will process them and generate error
61  * completions as per IB 1.2 C10-96.
62  */
63 const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
64         [IB_QPS_RESET] = 0,
65         [IB_QPS_INIT] = RVT_POST_RECV_OK,
66         [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
67         [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
68             RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
69             RVT_PROCESS_NEXT_SEND_OK,
70         [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
71             RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
72         [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
73             RVT_POST_SEND_OK | RVT_FLUSH_SEND,
74         [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
75             RVT_POST_SEND_OK | RVT_FLUSH_SEND,
76 };
77 EXPORT_SYMBOL(ib_rvt_state_ops);
78
79 static void get_map_page(struct rvt_qpn_table *qpt,
80                          struct rvt_qpn_map *map,
81                          gfp_t gfp)
82 {
83         unsigned long page = get_zeroed_page(gfp);
84
85         /*
86          * Free the page if someone raced with us installing it.
87          */
88
89         spin_lock(&qpt->lock);
90         if (map->page)
91                 free_page(page);
92         else
93                 map->page = (void *)page;
94         spin_unlock(&qpt->lock);
95 }
96
97 /**
98  * init_qpn_table - initialize the QP number table for a device
99  * @qpt: the QPN table
100  */
101 static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
102 {
103         u32 offset, i;
104         struct rvt_qpn_map *map;
105         int ret = 0;
106
107         if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
108                 return -EINVAL;
109
110         spin_lock_init(&qpt->lock);
111
112         qpt->last = rdi->dparms.qpn_start;
113         qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
114
115         /*
116          * Drivers may want some QPs beyond what we need for verbs let them use
117          * our qpn table. No need for two. Lets go ahead and mark the bitmaps
118          * for those. The reserved range must be *after* the range which verbs
119          * will pick from.
120          */
121
122         /* Figure out number of bit maps needed before reserved range */
123         qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
124
125         /* This should always be zero */
126         offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
127
128         /* Starting with the first reserved bit map */
129         map = &qpt->map[qpt->nmaps];
130
131         rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
132                     rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
133         for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
134                 if (!map->page) {
135                         get_map_page(qpt, map, GFP_KERNEL);
136                         if (!map->page) {
137                                 ret = -ENOMEM;
138                                 break;
139                         }
140                 }
141                 set_bit(offset, map->page);
142                 offset++;
143                 if (offset == RVT_BITS_PER_PAGE) {
144                         /* next page */
145                         qpt->nmaps++;
146                         map++;
147                         offset = 0;
148                 }
149         }
150         return ret;
151 }
152
153 /**
154  * free_qpn_table - free the QP number table for a device
155  * @qpt: the QPN table
156  */
157 static void free_qpn_table(struct rvt_qpn_table *qpt)
158 {
159         int i;
160
161         for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
162                 free_page((unsigned long)qpt->map[i].page);
163 }
164
165 /**
166  * rvt_driver_qp_init - Init driver qp resources
167  * @rdi: rvt dev strucutre
168  *
169  * Return: 0 on success
170  */
171 int rvt_driver_qp_init(struct rvt_dev_info *rdi)
172 {
173         int i;
174         int ret = -ENOMEM;
175
176         if (!rdi->dparms.qp_table_size)
177                 return -EINVAL;
178
179         /*
180          * If driver is not doing any QP allocation then make sure it is
181          * providing the necessary QP functions.
182          */
183         if (!rdi->driver_f.free_all_qps ||
184             !rdi->driver_f.qp_priv_alloc ||
185             !rdi->driver_f.qp_priv_free ||
186             !rdi->driver_f.notify_qp_reset)
187                 return -EINVAL;
188
189         /* allocate parent object */
190         rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
191                                    rdi->dparms.node);
192         if (!rdi->qp_dev)
193                 return -ENOMEM;
194
195         /* allocate hash table */
196         rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
197         rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
198         rdi->qp_dev->qp_table =
199                 kmalloc_node(rdi->qp_dev->qp_table_size *
200                              sizeof(*rdi->qp_dev->qp_table),
201                              GFP_KERNEL, rdi->dparms.node);
202         if (!rdi->qp_dev->qp_table)
203                 goto no_qp_table;
204
205         for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
206                 RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
207
208         spin_lock_init(&rdi->qp_dev->qpt_lock);
209
210         /* initialize qpn map */
211         if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
212                 goto fail_table;
213
214         spin_lock_init(&rdi->n_qps_lock);
215
216         return 0;
217
218 fail_table:
219         kfree(rdi->qp_dev->qp_table);
220         free_qpn_table(&rdi->qp_dev->qpn_table);
221
222 no_qp_table:
223         kfree(rdi->qp_dev);
224
225         return ret;
226 }
227
228 /**
229  * free_all_qps - check for QPs still in use
230  * @qpt: the QP table to empty
231  *
232  * There should not be any QPs still in use.
233  * Free memory for table.
234  */
235 static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
236 {
237         unsigned long flags;
238         struct rvt_qp *qp;
239         unsigned n, qp_inuse = 0;
240         spinlock_t *ql; /* work around too long line below */
241
242         if (rdi->driver_f.free_all_qps)
243                 qp_inuse = rdi->driver_f.free_all_qps(rdi);
244
245         qp_inuse += rvt_mcast_tree_empty(rdi);
246
247         if (!rdi->qp_dev)
248                 return qp_inuse;
249
250         ql = &rdi->qp_dev->qpt_lock;
251         spin_lock_irqsave(ql, flags);
252         for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
253                 qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
254                                                lockdep_is_held(ql));
255                 RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
256
257                 for (; qp; qp = rcu_dereference_protected(qp->next,
258                                                           lockdep_is_held(ql)))
259                         qp_inuse++;
260         }
261         spin_unlock_irqrestore(ql, flags);
262         synchronize_rcu();
263         return qp_inuse;
264 }
265
266 /**
267  * rvt_qp_exit - clean up qps on device exit
268  * @rdi: rvt dev structure
269  *
270  * Check for qp leaks and free resources.
271  */
272 void rvt_qp_exit(struct rvt_dev_info *rdi)
273 {
274         u32 qps_inuse = rvt_free_all_qps(rdi);
275
276         if (qps_inuse)
277                 rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
278                            qps_inuse);
279         if (!rdi->qp_dev)
280                 return;
281
282         kfree(rdi->qp_dev->qp_table);
283         free_qpn_table(&rdi->qp_dev->qpn_table);
284         kfree(rdi->qp_dev);
285 }
286
287 static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
288                               struct rvt_qpn_map *map, unsigned off)
289 {
290         return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
291 }
292
293 /**
294  * alloc_qpn - Allocate the next available qpn or zero/one for QP type
295  *             IB_QPT_SMI/IB_QPT_GSI
296  *@rdi: rvt device info structure
297  *@qpt: queue pair number table pointer
298  *@port_num: IB port number, 1 based, comes from core
299  *
300  * Return: The queue pair number
301  */
302 static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
303                      enum ib_qp_type type, u8 port_num, gfp_t gfp)
304 {
305         u32 i, offset, max_scan, qpn;
306         struct rvt_qpn_map *map;
307         u32 ret;
308
309         if (rdi->driver_f.alloc_qpn)
310                 return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num, gfp);
311
312         if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
313                 unsigned n;
314
315                 ret = type == IB_QPT_GSI;
316                 n = 1 << (ret + 2 * (port_num - 1));
317                 spin_lock(&qpt->lock);
318                 if (qpt->flags & n)
319                         ret = -EINVAL;
320                 else
321                         qpt->flags |= n;
322                 spin_unlock(&qpt->lock);
323                 goto bail;
324         }
325
326         qpn = qpt->last + qpt->incr;
327         if (qpn >= RVT_QPN_MAX)
328                 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
329         /* offset carries bit 0 */
330         offset = qpn & RVT_BITS_PER_PAGE_MASK;
331         map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
332         max_scan = qpt->nmaps - !offset;
333         for (i = 0;;) {
334                 if (unlikely(!map->page)) {
335                         get_map_page(qpt, map, gfp);
336                         if (unlikely(!map->page))
337                                 break;
338                 }
339                 do {
340                         if (!test_and_set_bit(offset, map->page)) {
341                                 qpt->last = qpn;
342                                 ret = qpn;
343                                 goto bail;
344                         }
345                         offset += qpt->incr;
346                         /*
347                          * This qpn might be bogus if offset >= BITS_PER_PAGE.
348                          * That is OK.   It gets re-assigned below
349                          */
350                         qpn = mk_qpn(qpt, map, offset);
351                 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
352                 /*
353                  * In order to keep the number of pages allocated to a
354                  * minimum, we scan the all existing pages before increasing
355                  * the size of the bitmap table.
356                  */
357                 if (++i > max_scan) {
358                         if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
359                                 break;
360                         map = &qpt->map[qpt->nmaps++];
361                         /* start at incr with current bit 0 */
362                         offset = qpt->incr | (offset & 1);
363                 } else if (map < &qpt->map[qpt->nmaps]) {
364                         ++map;
365                         /* start at incr with current bit 0 */
366                         offset = qpt->incr | (offset & 1);
367                 } else {
368                         map = &qpt->map[0];
369                         /* wrap to first map page, invert bit 0 */
370                         offset = qpt->incr | ((offset & 1) ^ 1);
371                 }
372                 /* there can be no set bits in low-order QoS bits */
373                 WARN_ON(rdi->dparms.qos_shift > 1 &&
374                         offset & ((BIT(rdi->dparms.qos_shift - 1) - 1) << 1));
375                 qpn = mk_qpn(qpt, map, offset);
376         }
377
378         ret = -ENOMEM;
379
380 bail:
381         return ret;
382 }
383
384 static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
385 {
386         struct rvt_qpn_map *map;
387
388         map = qpt->map + qpn / RVT_BITS_PER_PAGE;
389         if (map->page)
390                 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
391 }
392
393 /**
394  * rvt_clear_mr_refs - Drop help mr refs
395  * @qp: rvt qp data structure
396  * @clr_sends: If shoudl clear send side or not
397  */
398 static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
399 {
400         unsigned n;
401         struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
402
403         if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
404                 rvt_put_ss(&qp->s_rdma_read_sge);
405
406         rvt_put_ss(&qp->r_sge);
407
408         if (clr_sends) {
409                 while (qp->s_last != qp->s_head) {
410                         struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
411                         unsigned i;
412
413                         for (i = 0; i < wqe->wr.num_sge; i++) {
414                                 struct rvt_sge *sge = &wqe->sg_list[i];
415
416                                 rvt_put_mr(sge->mr);
417                         }
418                         if (qp->ibqp.qp_type == IB_QPT_UD ||
419                             qp->ibqp.qp_type == IB_QPT_SMI ||
420                             qp->ibqp.qp_type == IB_QPT_GSI)
421                                 atomic_dec(&ibah_to_rvtah(
422                                                 wqe->ud_wr.ah)->refcount);
423                         if (++qp->s_last >= qp->s_size)
424                                 qp->s_last = 0;
425                         smp_wmb(); /* see qp_set_savail */
426                 }
427                 if (qp->s_rdma_mr) {
428                         rvt_put_mr(qp->s_rdma_mr);
429                         qp->s_rdma_mr = NULL;
430                 }
431         }
432
433         if (qp->ibqp.qp_type != IB_QPT_RC)
434                 return;
435
436         for (n = 0; n < rvt_max_atomic(rdi); n++) {
437                 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
438
439                 if (e->rdma_sge.mr) {
440                         rvt_put_mr(e->rdma_sge.mr);
441                         e->rdma_sge.mr = NULL;
442                 }
443         }
444 }
445
446 /**
447  * rvt_remove_qp - remove qp form table
448  * @rdi: rvt dev struct
449  * @qp: qp to remove
450  *
451  * Remove the QP from the table so it can't be found asynchronously by
452  * the receive routine.
453  */
454 static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
455 {
456         struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
457         u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
458         unsigned long flags;
459         int removed = 1;
460
461         spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
462
463         if (rcu_dereference_protected(rvp->qp[0],
464                         lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
465                 RCU_INIT_POINTER(rvp->qp[0], NULL);
466         } else if (rcu_dereference_protected(rvp->qp[1],
467                         lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
468                 RCU_INIT_POINTER(rvp->qp[1], NULL);
469         } else {
470                 struct rvt_qp *q;
471                 struct rvt_qp __rcu **qpp;
472
473                 removed = 0;
474                 qpp = &rdi->qp_dev->qp_table[n];
475                 for (; (q = rcu_dereference_protected(*qpp,
476                         lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
477                         qpp = &q->next) {
478                         if (q == qp) {
479                                 RCU_INIT_POINTER(*qpp,
480                                      rcu_dereference_protected(qp->next,
481                                      lockdep_is_held(&rdi->qp_dev->qpt_lock)));
482                                 removed = 1;
483                                 trace_rvt_qpremove(qp, n);
484                                 break;
485                         }
486                 }
487         }
488
489         spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
490         if (removed) {
491                 synchronize_rcu();
492                 rvt_put_qp(qp);
493         }
494 }
495
496 /**
497  * rvt_init_qp - initialize the QP state to the reset state
498  * @qp: the QP to init or reinit
499  * @type: the QP type
500  *
501  * This function is called from both rvt_create_qp() and
502  * rvt_reset_qp().   The difference is that the reset
503  * patch the necessary locks to protect against concurent
504  * access.
505  */
506 static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
507                         enum ib_qp_type type)
508 {
509         qp->remote_qpn = 0;
510         qp->qkey = 0;
511         qp->qp_access_flags = 0;
512         qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
513         qp->s_hdrwords = 0;
514         qp->s_wqe = NULL;
515         qp->s_draining = 0;
516         qp->s_next_psn = 0;
517         qp->s_last_psn = 0;
518         qp->s_sending_psn = 0;
519         qp->s_sending_hpsn = 0;
520         qp->s_psn = 0;
521         qp->r_psn = 0;
522         qp->r_msn = 0;
523         if (type == IB_QPT_RC) {
524                 qp->s_state = IB_OPCODE_RC_SEND_LAST;
525                 qp->r_state = IB_OPCODE_RC_SEND_LAST;
526         } else {
527                 qp->s_state = IB_OPCODE_UC_SEND_LAST;
528                 qp->r_state = IB_OPCODE_UC_SEND_LAST;
529         }
530         qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
531         qp->r_nak_state = 0;
532         qp->r_aflags = 0;
533         qp->r_flags = 0;
534         qp->s_head = 0;
535         qp->s_tail = 0;
536         qp->s_cur = 0;
537         qp->s_acked = 0;
538         qp->s_last = 0;
539         qp->s_ssn = 1;
540         qp->s_lsn = 0;
541         qp->s_mig_state = IB_MIG_MIGRATED;
542         qp->r_head_ack_queue = 0;
543         qp->s_tail_ack_queue = 0;
544         qp->s_num_rd_atomic = 0;
545         if (qp->r_rq.wq) {
546                 qp->r_rq.wq->head = 0;
547                 qp->r_rq.wq->tail = 0;
548         }
549         qp->r_sge.num_sge = 0;
550         atomic_set(&qp->s_reserved_used, 0);
551 }
552
553 /**
554  * rvt_reset_qp - initialize the QP state to the reset state
555  * @qp: the QP to reset
556  * @type: the QP type
557  *
558  * r_lock, s_hlock, and s_lock are required to be held by the caller
559  */
560 static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
561                          enum ib_qp_type type)
562         __must_hold(&qp->s_lock)
563         __must_hold(&qp->s_hlock)
564         __must_hold(&qp->r_lock)
565 {
566         lockdep_assert_held(&qp->r_lock);
567         lockdep_assert_held(&qp->s_hlock);
568         lockdep_assert_held(&qp->s_lock);
569         if (qp->state != IB_QPS_RESET) {
570                 qp->state = IB_QPS_RESET;
571
572                 /* Let drivers flush their waitlist */
573                 rdi->driver_f.flush_qp_waiters(qp);
574                 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
575                 spin_unlock(&qp->s_lock);
576                 spin_unlock(&qp->s_hlock);
577                 spin_unlock_irq(&qp->r_lock);
578
579                 /* Stop the send queue and the retry timer */
580                 rdi->driver_f.stop_send_queue(qp);
581
582                 /* Wait for things to stop */
583                 rdi->driver_f.quiesce_qp(qp);
584
585                 /* take qp out the hash and wait for it to be unused */
586                 rvt_remove_qp(rdi, qp);
587                 wait_event(qp->wait, !atomic_read(&qp->refcount));
588
589                 /* grab the lock b/c it was locked at call time */
590                 spin_lock_irq(&qp->r_lock);
591                 spin_lock(&qp->s_hlock);
592                 spin_lock(&qp->s_lock);
593
594                 rvt_clear_mr_refs(qp, 1);
595                 /*
596                  * Let the driver do any tear down or re-init it needs to for
597                  * a qp that has been reset
598                  */
599                 rdi->driver_f.notify_qp_reset(qp);
600         }
601         rvt_init_qp(rdi, qp, type);
602         lockdep_assert_held(&qp->r_lock);
603         lockdep_assert_held(&qp->s_hlock);
604         lockdep_assert_held(&qp->s_lock);
605 }
606
607 /**
608  * rvt_create_qp - create a queue pair for a device
609  * @ibpd: the protection domain who's device we create the queue pair for
610  * @init_attr: the attributes of the queue pair
611  * @udata: user data for libibverbs.so
612  *
613  * Queue pair creation is mostly an rvt issue. However, drivers have their own
614  * unique idea of what queue pair numbers mean. For instance there is a reserved
615  * range for PSM.
616  *
617  * Return: the queue pair on success, otherwise returns an errno.
618  *
619  * Called by the ib_create_qp() core verbs function.
620  */
621 struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
622                             struct ib_qp_init_attr *init_attr,
623                             struct ib_udata *udata)
624 {
625         struct rvt_qp *qp;
626         int err;
627         struct rvt_swqe *swq = NULL;
628         size_t sz;
629         size_t sg_list_sz;
630         struct ib_qp *ret = ERR_PTR(-ENOMEM);
631         struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
632         void *priv = NULL;
633         gfp_t gfp;
634         size_t sqsize;
635
636         if (!rdi)
637                 return ERR_PTR(-EINVAL);
638
639         if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
640             init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
641             init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
642                 return ERR_PTR(-EINVAL);
643
644         /* GFP_NOIO is applicable to RC QP's only */
645
646         if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
647             init_attr->qp_type != IB_QPT_RC)
648                 return ERR_PTR(-EINVAL);
649
650         gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
651                                                 GFP_NOIO : GFP_KERNEL;
652
653         /* Check receive queue parameters if no SRQ is specified. */
654         if (!init_attr->srq) {
655                 if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
656                     init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
657                         return ERR_PTR(-EINVAL);
658
659                 if (init_attr->cap.max_send_sge +
660                     init_attr->cap.max_send_wr +
661                     init_attr->cap.max_recv_sge +
662                     init_attr->cap.max_recv_wr == 0)
663                         return ERR_PTR(-EINVAL);
664         }
665         sqsize =
666                 init_attr->cap.max_send_wr + 1 +
667                 rdi->dparms.reserved_operations;
668         switch (init_attr->qp_type) {
669         case IB_QPT_SMI:
670         case IB_QPT_GSI:
671                 if (init_attr->port_num == 0 ||
672                     init_attr->port_num > ibpd->device->phys_port_cnt)
673                         return ERR_PTR(-EINVAL);
674         case IB_QPT_UC:
675         case IB_QPT_RC:
676         case IB_QPT_UD:
677                 sz = sizeof(struct rvt_sge) *
678                         init_attr->cap.max_send_sge +
679                         sizeof(struct rvt_swqe);
680                 if (gfp == GFP_NOIO)
681                         swq = __vmalloc(
682                                 sqsize * sz,
683                                 gfp | __GFP_ZERO, PAGE_KERNEL);
684                 else
685                         swq = vzalloc_node(
686                                 sqsize * sz,
687                                 rdi->dparms.node);
688                 if (!swq)
689                         return ERR_PTR(-ENOMEM);
690
691                 sz = sizeof(*qp);
692                 sg_list_sz = 0;
693                 if (init_attr->srq) {
694                         struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
695
696                         if (srq->rq.max_sge > 1)
697                                 sg_list_sz = sizeof(*qp->r_sg_list) *
698                                         (srq->rq.max_sge - 1);
699                 } else if (init_attr->cap.max_recv_sge > 1)
700                         sg_list_sz = sizeof(*qp->r_sg_list) *
701                                 (init_attr->cap.max_recv_sge - 1);
702                 qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
703                 if (!qp)
704                         goto bail_swq;
705
706                 RCU_INIT_POINTER(qp->next, NULL);
707                 if (init_attr->qp_type == IB_QPT_RC) {
708                         qp->s_ack_queue =
709                                 kzalloc_node(
710                                         sizeof(*qp->s_ack_queue) *
711                                          rvt_max_atomic(rdi),
712                                         gfp,
713                                         rdi->dparms.node);
714                         if (!qp->s_ack_queue)
715                                 goto bail_qp;
716                 }
717
718                 /*
719                  * Driver needs to set up it's private QP structure and do any
720                  * initialization that is needed.
721                  */
722                 priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
723                 if (IS_ERR(priv)) {
724                         ret = priv;
725                         goto bail_qp;
726                 }
727                 qp->priv = priv;
728                 qp->timeout_jiffies =
729                         usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
730                                 1000UL);
731                 if (init_attr->srq) {
732                         sz = 0;
733                 } else {
734                         qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
735                         qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
736                         sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
737                                 sizeof(struct rvt_rwqe);
738                         if (udata)
739                                 qp->r_rq.wq = vmalloc_user(
740                                                 sizeof(struct rvt_rwq) +
741                                                 qp->r_rq.size * sz);
742                         else if (gfp == GFP_NOIO)
743                                 qp->r_rq.wq = __vmalloc(
744                                                 sizeof(struct rvt_rwq) +
745                                                 qp->r_rq.size * sz,
746                                                 gfp | __GFP_ZERO, PAGE_KERNEL);
747                         else
748                                 qp->r_rq.wq = vzalloc_node(
749                                                 sizeof(struct rvt_rwq) +
750                                                 qp->r_rq.size * sz,
751                                                 rdi->dparms.node);
752                         if (!qp->r_rq.wq)
753                                 goto bail_driver_priv;
754                 }
755
756                 /*
757                  * ib_create_qp() will initialize qp->ibqp
758                  * except for qp->ibqp.qp_num.
759                  */
760                 spin_lock_init(&qp->r_lock);
761                 spin_lock_init(&qp->s_hlock);
762                 spin_lock_init(&qp->s_lock);
763                 spin_lock_init(&qp->r_rq.lock);
764                 atomic_set(&qp->refcount, 0);
765                 atomic_set(&qp->local_ops_pending, 0);
766                 init_waitqueue_head(&qp->wait);
767                 init_timer(&qp->s_timer);
768                 qp->s_timer.data = (unsigned long)qp;
769                 INIT_LIST_HEAD(&qp->rspwait);
770                 qp->state = IB_QPS_RESET;
771                 qp->s_wq = swq;
772                 qp->s_size = sqsize;
773                 qp->s_avail = init_attr->cap.max_send_wr;
774                 qp->s_max_sge = init_attr->cap.max_send_sge;
775                 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
776                         qp->s_flags = RVT_S_SIGNAL_REQ_WR;
777
778                 err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
779                                 init_attr->qp_type,
780                                 init_attr->port_num, gfp);
781                 if (err < 0) {
782                         ret = ERR_PTR(err);
783                         goto bail_rq_wq;
784                 }
785                 qp->ibqp.qp_num = err;
786                 qp->port_num = init_attr->port_num;
787                 rvt_init_qp(rdi, qp, init_attr->qp_type);
788                 break;
789
790         default:
791                 /* Don't support raw QPs */
792                 return ERR_PTR(-EINVAL);
793         }
794
795         init_attr->cap.max_inline_data = 0;
796
797         /*
798          * Return the address of the RWQ as the offset to mmap.
799          * See rvt_mmap() for details.
800          */
801         if (udata && udata->outlen >= sizeof(__u64)) {
802                 if (!qp->r_rq.wq) {
803                         __u64 offset = 0;
804
805                         err = ib_copy_to_udata(udata, &offset,
806                                                sizeof(offset));
807                         if (err) {
808                                 ret = ERR_PTR(err);
809                                 goto bail_qpn;
810                         }
811                 } else {
812                         u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
813
814                         qp->ip = rvt_create_mmap_info(rdi, s,
815                                                       ibpd->uobject->context,
816                                                       qp->r_rq.wq);
817                         if (!qp->ip) {
818                                 ret = ERR_PTR(-ENOMEM);
819                                 goto bail_qpn;
820                         }
821
822                         err = ib_copy_to_udata(udata, &qp->ip->offset,
823                                                sizeof(qp->ip->offset));
824                         if (err) {
825                                 ret = ERR_PTR(err);
826                                 goto bail_ip;
827                         }
828                 }
829                 qp->pid = current->pid;
830         }
831
832         spin_lock(&rdi->n_qps_lock);
833         if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
834                 spin_unlock(&rdi->n_qps_lock);
835                 ret = ERR_PTR(-ENOMEM);
836                 goto bail_ip;
837         }
838
839         rdi->n_qps_allocated++;
840         /*
841          * Maintain a busy_jiffies variable that will be added to the timeout
842          * period in mod_retry_timer and add_retry_timer. This busy jiffies
843          * is scaled by the number of rc qps created for the device to reduce
844          * the number of timeouts occurring when there is a large number of
845          * qps. busy_jiffies is incremented every rc qp scaling interval.
846          * The scaling interval is selected based on extensive performance
847          * evaluation of targeted workloads.
848          */
849         if (init_attr->qp_type == IB_QPT_RC) {
850                 rdi->n_rc_qps++;
851                 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
852         }
853         spin_unlock(&rdi->n_qps_lock);
854
855         if (qp->ip) {
856                 spin_lock_irq(&rdi->pending_lock);
857                 list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
858                 spin_unlock_irq(&rdi->pending_lock);
859         }
860
861         ret = &qp->ibqp;
862
863         /*
864          * We have our QP and its good, now keep track of what types of opcodes
865          * can be processed on this QP. We do this by keeping track of what the
866          * 3 high order bits of the opcode are.
867          */
868         switch (init_attr->qp_type) {
869         case IB_QPT_SMI:
870         case IB_QPT_GSI:
871         case IB_QPT_UD:
872                 qp->allowed_ops = IB_OPCODE_UD;
873                 break;
874         case IB_QPT_RC:
875                 qp->allowed_ops = IB_OPCODE_RC;
876                 break;
877         case IB_QPT_UC:
878                 qp->allowed_ops = IB_OPCODE_UC;
879                 break;
880         default:
881                 ret = ERR_PTR(-EINVAL);
882                 goto bail_ip;
883         }
884
885         return ret;
886
887 bail_ip:
888         kref_put(&qp->ip->ref, rvt_release_mmap_info);
889
890 bail_qpn:
891         free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
892
893 bail_rq_wq:
894         if (!qp->ip)
895                 vfree(qp->r_rq.wq);
896
897 bail_driver_priv:
898         rdi->driver_f.qp_priv_free(rdi, qp);
899
900 bail_qp:
901         kfree(qp->s_ack_queue);
902         kfree(qp);
903
904 bail_swq:
905         vfree(swq);
906
907         return ret;
908 }
909
910 /**
911  * rvt_error_qp - put a QP into the error state
912  * @qp: the QP to put into the error state
913  * @err: the receive completion error to signal if a RWQE is active
914  *
915  * Flushes both send and receive work queues.
916  *
917  * Return: true if last WQE event should be generated.
918  * The QP r_lock and s_lock should be held and interrupts disabled.
919  * If we are already in error state, just return.
920  */
921 int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
922 {
923         struct ib_wc wc;
924         int ret = 0;
925         struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
926
927         lockdep_assert_held(&qp->r_lock);
928         lockdep_assert_held(&qp->s_lock);
929         if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
930                 goto bail;
931
932         qp->state = IB_QPS_ERR;
933
934         if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
935                 qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
936                 del_timer(&qp->s_timer);
937         }
938
939         if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
940                 qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
941
942         rdi->driver_f.notify_error_qp(qp);
943
944         /* Schedule the sending tasklet to drain the send work queue. */
945         if (ACCESS_ONCE(qp->s_last) != qp->s_head)
946                 rdi->driver_f.schedule_send(qp);
947
948         rvt_clear_mr_refs(qp, 0);
949
950         memset(&wc, 0, sizeof(wc));
951         wc.qp = &qp->ibqp;
952         wc.opcode = IB_WC_RECV;
953
954         if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
955                 wc.wr_id = qp->r_wr_id;
956                 wc.status = err;
957                 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
958         }
959         wc.status = IB_WC_WR_FLUSH_ERR;
960
961         if (qp->r_rq.wq) {
962                 struct rvt_rwq *wq;
963                 u32 head;
964                 u32 tail;
965
966                 spin_lock(&qp->r_rq.lock);
967
968                 /* sanity check pointers before trusting them */
969                 wq = qp->r_rq.wq;
970                 head = wq->head;
971                 if (head >= qp->r_rq.size)
972                         head = 0;
973                 tail = wq->tail;
974                 if (tail >= qp->r_rq.size)
975                         tail = 0;
976                 while (tail != head) {
977                         wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
978                         if (++tail >= qp->r_rq.size)
979                                 tail = 0;
980                         rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
981                 }
982                 wq->tail = tail;
983
984                 spin_unlock(&qp->r_rq.lock);
985         } else if (qp->ibqp.event_handler) {
986                 ret = 1;
987         }
988
989 bail:
990         return ret;
991 }
992 EXPORT_SYMBOL(rvt_error_qp);
993
994 /*
995  * Put the QP into the hash table.
996  * The hash table holds a reference to the QP.
997  */
998 static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
999 {
1000         struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
1001         unsigned long flags;
1002
1003         rvt_get_qp(qp);
1004         spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
1005
1006         if (qp->ibqp.qp_num <= 1) {
1007                 rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
1008         } else {
1009                 u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
1010
1011                 qp->next = rdi->qp_dev->qp_table[n];
1012                 rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
1013                 trace_rvt_qpinsert(qp, n);
1014         }
1015
1016         spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
1017 }
1018
1019 /**
1020  * rvt_modify_qp - modify the attributes of a queue pair
1021  * @ibqp: the queue pair who's attributes we're modifying
1022  * @attr: the new attributes
1023  * @attr_mask: the mask of attributes to modify
1024  * @udata: user data for libibverbs.so
1025  *
1026  * Return: 0 on success, otherwise returns an errno.
1027  */
1028 int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1029                   int attr_mask, struct ib_udata *udata)
1030 {
1031         struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1032         struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1033         enum ib_qp_state cur_state, new_state;
1034         struct ib_event ev;
1035         int lastwqe = 0;
1036         int mig = 0;
1037         int pmtu = 0; /* for gcc warning only */
1038         enum rdma_link_layer link;
1039
1040         link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
1041
1042         spin_lock_irq(&qp->r_lock);
1043         spin_lock(&qp->s_hlock);
1044         spin_lock(&qp->s_lock);
1045
1046         cur_state = attr_mask & IB_QP_CUR_STATE ?
1047                 attr->cur_qp_state : qp->state;
1048         new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1049
1050         if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1051                                 attr_mask, link))
1052                 goto inval;
1053
1054         if (rdi->driver_f.check_modify_qp &&
1055             rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
1056                 goto inval;
1057
1058         if (attr_mask & IB_QP_AV) {
1059                 if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
1060                         goto inval;
1061                 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
1062                         goto inval;
1063         }
1064
1065         if (attr_mask & IB_QP_ALT_PATH) {
1066                 if (attr->alt_ah_attr.dlid >=
1067                     be16_to_cpu(IB_MULTICAST_LID_BASE))
1068                         goto inval;
1069                 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
1070                         goto inval;
1071                 if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
1072                         goto inval;
1073         }
1074
1075         if (attr_mask & IB_QP_PKEY_INDEX)
1076                 if (attr->pkey_index >= rvt_get_npkeys(rdi))
1077                         goto inval;
1078
1079         if (attr_mask & IB_QP_MIN_RNR_TIMER)
1080                 if (attr->min_rnr_timer > 31)
1081                         goto inval;
1082
1083         if (attr_mask & IB_QP_PORT)
1084                 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1085                     qp->ibqp.qp_type == IB_QPT_GSI ||
1086                     attr->port_num == 0 ||
1087                     attr->port_num > ibqp->device->phys_port_cnt)
1088                         goto inval;
1089
1090         if (attr_mask & IB_QP_DEST_QPN)
1091                 if (attr->dest_qp_num > RVT_QPN_MASK)
1092                         goto inval;
1093
1094         if (attr_mask & IB_QP_RETRY_CNT)
1095                 if (attr->retry_cnt > 7)
1096                         goto inval;
1097
1098         if (attr_mask & IB_QP_RNR_RETRY)
1099                 if (attr->rnr_retry > 7)
1100                         goto inval;
1101
1102         /*
1103          * Don't allow invalid path_mtu values.  OK to set greater
1104          * than the active mtu (or even the max_cap, if we have tuned
1105          * that to a small mtu.  We'll set qp->path_mtu
1106          * to the lesser of requested attribute mtu and active,
1107          * for packetizing messages.
1108          * Note that the QP port has to be set in INIT and MTU in RTR.
1109          */
1110         if (attr_mask & IB_QP_PATH_MTU) {
1111                 pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
1112                 if (pmtu < 0)
1113                         goto inval;
1114         }
1115
1116         if (attr_mask & IB_QP_PATH_MIG_STATE) {
1117                 if (attr->path_mig_state == IB_MIG_REARM) {
1118                         if (qp->s_mig_state == IB_MIG_ARMED)
1119                                 goto inval;
1120                         if (new_state != IB_QPS_RTS)
1121                                 goto inval;
1122                 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
1123                         if (qp->s_mig_state == IB_MIG_REARM)
1124                                 goto inval;
1125                         if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
1126                                 goto inval;
1127                         if (qp->s_mig_state == IB_MIG_ARMED)
1128                                 mig = 1;
1129                 } else {
1130                         goto inval;
1131                 }
1132         }
1133
1134         if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1135                 if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
1136                         goto inval;
1137
1138         switch (new_state) {
1139         case IB_QPS_RESET:
1140                 if (qp->state != IB_QPS_RESET)
1141                         rvt_reset_qp(rdi, qp, ibqp->qp_type);
1142                 break;
1143
1144         case IB_QPS_RTR:
1145                 /* Allow event to re-trigger if QP set to RTR more than once */
1146                 qp->r_flags &= ~RVT_R_COMM_EST;
1147                 qp->state = new_state;
1148                 break;
1149
1150         case IB_QPS_SQD:
1151                 qp->s_draining = qp->s_last != qp->s_cur;
1152                 qp->state = new_state;
1153                 break;
1154
1155         case IB_QPS_SQE:
1156                 if (qp->ibqp.qp_type == IB_QPT_RC)
1157                         goto inval;
1158                 qp->state = new_state;
1159                 break;
1160
1161         case IB_QPS_ERR:
1162                 lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
1163                 break;
1164
1165         default:
1166                 qp->state = new_state;
1167                 break;
1168         }
1169
1170         if (attr_mask & IB_QP_PKEY_INDEX)
1171                 qp->s_pkey_index = attr->pkey_index;
1172
1173         if (attr_mask & IB_QP_PORT)
1174                 qp->port_num = attr->port_num;
1175
1176         if (attr_mask & IB_QP_DEST_QPN)
1177                 qp->remote_qpn = attr->dest_qp_num;
1178
1179         if (attr_mask & IB_QP_SQ_PSN) {
1180                 qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
1181                 qp->s_psn = qp->s_next_psn;
1182                 qp->s_sending_psn = qp->s_next_psn;
1183                 qp->s_last_psn = qp->s_next_psn - 1;
1184                 qp->s_sending_hpsn = qp->s_last_psn;
1185         }
1186
1187         if (attr_mask & IB_QP_RQ_PSN)
1188                 qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
1189
1190         if (attr_mask & IB_QP_ACCESS_FLAGS)
1191                 qp->qp_access_flags = attr->qp_access_flags;
1192
1193         if (attr_mask & IB_QP_AV) {
1194                 qp->remote_ah_attr = attr->ah_attr;
1195                 qp->s_srate = attr->ah_attr.static_rate;
1196                 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
1197         }
1198
1199         if (attr_mask & IB_QP_ALT_PATH) {
1200                 qp->alt_ah_attr = attr->alt_ah_attr;
1201                 qp->s_alt_pkey_index = attr->alt_pkey_index;
1202         }
1203
1204         if (attr_mask & IB_QP_PATH_MIG_STATE) {
1205                 qp->s_mig_state = attr->path_mig_state;
1206                 if (mig) {
1207                         qp->remote_ah_attr = qp->alt_ah_attr;
1208                         qp->port_num = qp->alt_ah_attr.port_num;
1209                         qp->s_pkey_index = qp->s_alt_pkey_index;
1210                 }
1211         }
1212
1213         if (attr_mask & IB_QP_PATH_MTU) {
1214                 qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
1215                 qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
1216                 qp->log_pmtu = ilog2(qp->pmtu);
1217         }
1218
1219         if (attr_mask & IB_QP_RETRY_CNT) {
1220                 qp->s_retry_cnt = attr->retry_cnt;
1221                 qp->s_retry = attr->retry_cnt;
1222         }
1223
1224         if (attr_mask & IB_QP_RNR_RETRY) {
1225                 qp->s_rnr_retry_cnt = attr->rnr_retry;
1226                 qp->s_rnr_retry = attr->rnr_retry;
1227         }
1228
1229         if (attr_mask & IB_QP_MIN_RNR_TIMER)
1230                 qp->r_min_rnr_timer = attr->min_rnr_timer;
1231
1232         if (attr_mask & IB_QP_TIMEOUT) {
1233                 qp->timeout = attr->timeout;
1234                 qp->timeout_jiffies =
1235                         usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1236                                 1000UL);
1237         }
1238
1239         if (attr_mask & IB_QP_QKEY)
1240                 qp->qkey = attr->qkey;
1241
1242         if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1243                 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
1244
1245         if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1246                 qp->s_max_rd_atomic = attr->max_rd_atomic;
1247
1248         if (rdi->driver_f.modify_qp)
1249                 rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
1250
1251         spin_unlock(&qp->s_lock);
1252         spin_unlock(&qp->s_hlock);
1253         spin_unlock_irq(&qp->r_lock);
1254
1255         if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1256                 rvt_insert_qp(rdi, qp);
1257
1258         if (lastwqe) {
1259                 ev.device = qp->ibqp.device;
1260                 ev.element.qp = &qp->ibqp;
1261                 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
1262                 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1263         }
1264         if (mig) {
1265                 ev.device = qp->ibqp.device;
1266                 ev.element.qp = &qp->ibqp;
1267                 ev.event = IB_EVENT_PATH_MIG;
1268                 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1269         }
1270         return 0;
1271
1272 inval:
1273         spin_unlock(&qp->s_lock);
1274         spin_unlock(&qp->s_hlock);
1275         spin_unlock_irq(&qp->r_lock);
1276         return -EINVAL;
1277 }
1278
1279 /** rvt_free_qpn - Free a qpn from the bit map
1280  * @qpt: QP table
1281  * @qpn: queue pair number to free
1282  */
1283 static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
1284 {
1285         struct rvt_qpn_map *map;
1286
1287         map = qpt->map + qpn / RVT_BITS_PER_PAGE;
1288         if (map->page)
1289                 clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
1290 }
1291
1292 /**
1293  * rvt_destroy_qp - destroy a queue pair
1294  * @ibqp: the queue pair to destroy
1295  *
1296  * Note that this can be called while the QP is actively sending or
1297  * receiving!
1298  *
1299  * Return: 0 on success.
1300  */
1301 int rvt_destroy_qp(struct ib_qp *ibqp)
1302 {
1303         struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1304         struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1305
1306         spin_lock_irq(&qp->r_lock);
1307         spin_lock(&qp->s_hlock);
1308         spin_lock(&qp->s_lock);
1309         rvt_reset_qp(rdi, qp, ibqp->qp_type);
1310         spin_unlock(&qp->s_lock);
1311         spin_unlock(&qp->s_hlock);
1312         spin_unlock_irq(&qp->r_lock);
1313
1314         /* qpn is now available for use again */
1315         rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
1316
1317         spin_lock(&rdi->n_qps_lock);
1318         rdi->n_qps_allocated--;
1319         if (qp->ibqp.qp_type == IB_QPT_RC) {
1320                 rdi->n_rc_qps--;
1321                 rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
1322         }
1323         spin_unlock(&rdi->n_qps_lock);
1324
1325         if (qp->ip)
1326                 kref_put(&qp->ip->ref, rvt_release_mmap_info);
1327         else
1328                 vfree(qp->r_rq.wq);
1329         vfree(qp->s_wq);
1330         rdi->driver_f.qp_priv_free(rdi, qp);
1331         kfree(qp->s_ack_queue);
1332         kfree(qp);
1333         return 0;
1334 }
1335
1336 /**
1337  * rvt_query_qp - query an ipbq
1338  * @ibqp: IB qp to query
1339  * @attr: attr struct to fill in
1340  * @attr_mask: attr mask ignored
1341  * @init_attr: struct to fill in
1342  *
1343  * Return: always 0
1344  */
1345 int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1346                  int attr_mask, struct ib_qp_init_attr *init_attr)
1347 {
1348         struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1349         struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1350
1351         attr->qp_state = qp->state;
1352         attr->cur_qp_state = attr->qp_state;
1353         attr->path_mtu = qp->path_mtu;
1354         attr->path_mig_state = qp->s_mig_state;
1355         attr->qkey = qp->qkey;
1356         attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
1357         attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
1358         attr->dest_qp_num = qp->remote_qpn;
1359         attr->qp_access_flags = qp->qp_access_flags;
1360         attr->cap.max_send_wr = qp->s_size - 1 -
1361                 rdi->dparms.reserved_operations;
1362         attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
1363         attr->cap.max_send_sge = qp->s_max_sge;
1364         attr->cap.max_recv_sge = qp->r_rq.max_sge;
1365         attr->cap.max_inline_data = 0;
1366         attr->ah_attr = qp->remote_ah_attr;
1367         attr->alt_ah_attr = qp->alt_ah_attr;
1368         attr->pkey_index = qp->s_pkey_index;
1369         attr->alt_pkey_index = qp->s_alt_pkey_index;
1370         attr->en_sqd_async_notify = 0;
1371         attr->sq_draining = qp->s_draining;
1372         attr->max_rd_atomic = qp->s_max_rd_atomic;
1373         attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
1374         attr->min_rnr_timer = qp->r_min_rnr_timer;
1375         attr->port_num = qp->port_num;
1376         attr->timeout = qp->timeout;
1377         attr->retry_cnt = qp->s_retry_cnt;
1378         attr->rnr_retry = qp->s_rnr_retry_cnt;
1379         attr->alt_port_num = qp->alt_ah_attr.port_num;
1380         attr->alt_timeout = qp->alt_timeout;
1381
1382         init_attr->event_handler = qp->ibqp.event_handler;
1383         init_attr->qp_context = qp->ibqp.qp_context;
1384         init_attr->send_cq = qp->ibqp.send_cq;
1385         init_attr->recv_cq = qp->ibqp.recv_cq;
1386         init_attr->srq = qp->ibqp.srq;
1387         init_attr->cap = attr->cap;
1388         if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
1389                 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
1390         else
1391                 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1392         init_attr->qp_type = qp->ibqp.qp_type;
1393         init_attr->port_num = qp->port_num;
1394         return 0;
1395 }
1396
1397 /**
1398  * rvt_post_receive - post a receive on a QP
1399  * @ibqp: the QP to post the receive on
1400  * @wr: the WR to post
1401  * @bad_wr: the first bad WR is put here
1402  *
1403  * This may be called from interrupt context.
1404  *
1405  * Return: 0 on success otherwise errno
1406  */
1407 int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1408                   struct ib_recv_wr **bad_wr)
1409 {
1410         struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1411         struct rvt_rwq *wq = qp->r_rq.wq;
1412         unsigned long flags;
1413         int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
1414                                 !qp->ibqp.srq;
1415
1416         /* Check that state is OK to post receive. */
1417         if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
1418                 *bad_wr = wr;
1419                 return -EINVAL;
1420         }
1421
1422         for (; wr; wr = wr->next) {
1423                 struct rvt_rwqe *wqe;
1424                 u32 next;
1425                 int i;
1426
1427                 if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
1428                         *bad_wr = wr;
1429                         return -EINVAL;
1430                 }
1431
1432                 spin_lock_irqsave(&qp->r_rq.lock, flags);
1433                 next = wq->head + 1;
1434                 if (next >= qp->r_rq.size)
1435                         next = 0;
1436                 if (next == wq->tail) {
1437                         spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1438                         *bad_wr = wr;
1439                         return -ENOMEM;
1440                 }
1441                 if (unlikely(qp_err_flush)) {
1442                         struct ib_wc wc;
1443
1444                         memset(&wc, 0, sizeof(wc));
1445                         wc.qp = &qp->ibqp;
1446                         wc.opcode = IB_WC_RECV;
1447                         wc.wr_id = wr->wr_id;
1448                         wc.status = IB_WC_WR_FLUSH_ERR;
1449                         rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
1450                 } else {
1451                         wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
1452                         wqe->wr_id = wr->wr_id;
1453                         wqe->num_sge = wr->num_sge;
1454                         for (i = 0; i < wr->num_sge; i++)
1455                                 wqe->sg_list[i] = wr->sg_list[i];
1456                         /*
1457                          * Make sure queue entry is written
1458                          * before the head index.
1459                          */
1460                         smp_wmb();
1461                         wq->head = next;
1462                 }
1463                 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
1464         }
1465         return 0;
1466 }
1467
1468 /**
1469  * rvt_qp_valid_operation - validate post send wr request
1470  * @qp - the qp
1471  * @post-parms - the post send table for the driver
1472  * @wr - the work request
1473  *
1474  * The routine validates the operation based on the
1475  * validation table an returns the length of the operation
1476  * which can extend beyond the ib_send_bw.  Operation
1477  * dependent flags key atomic operation validation.
1478  *
1479  * There is an exception for UD qps that validates the pd and
1480  * overrides the length to include the additional UD specific
1481  * length.
1482  *
1483  * Returns a negative error or the length of the work request
1484  * for building the swqe.
1485  */
1486 static inline int rvt_qp_valid_operation(
1487         struct rvt_qp *qp,
1488         const struct rvt_operation_params *post_parms,
1489         struct ib_send_wr *wr)
1490 {
1491         int len;
1492
1493         if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
1494                 return -EINVAL;
1495         if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
1496                 return -EINVAL;
1497         if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
1498             ibpd_to_rvtpd(qp->ibqp.pd)->user)
1499                 return -EINVAL;
1500         if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
1501             (wr->num_sge == 0 ||
1502              wr->sg_list[0].length < sizeof(u64) ||
1503              wr->sg_list[0].addr & (sizeof(u64) - 1)))
1504                 return -EINVAL;
1505         if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
1506             !qp->s_max_rd_atomic)
1507                 return -EINVAL;
1508         len = post_parms[wr->opcode].length;
1509         /* UD specific */
1510         if (qp->ibqp.qp_type != IB_QPT_UC &&
1511             qp->ibqp.qp_type != IB_QPT_RC) {
1512                 if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
1513                         return -EINVAL;
1514                 len = sizeof(struct ib_ud_wr);
1515         }
1516         return len;
1517 }
1518
1519 /**
1520  * rvt_qp_is_avail - determine queue capacity
1521  * @qp - the qp
1522  * @rdi - the rdmavt device
1523  * @reserved_op - is reserved operation
1524  *
1525  * This assumes the s_hlock is held but the s_last
1526  * qp variable is uncontrolled.
1527  *
1528  * For non reserved operations, the qp->s_avail
1529  * may be changed.
1530  *
1531  * The return value is zero or a -ENOMEM.
1532  */
1533 static inline int rvt_qp_is_avail(
1534         struct rvt_qp *qp,
1535         struct rvt_dev_info *rdi,
1536         bool reserved_op)
1537 {
1538         u32 slast;
1539         u32 avail;
1540         u32 reserved_used;
1541
1542         /* see rvt_qp_wqe_unreserve() */
1543         smp_mb__before_atomic();
1544         reserved_used = atomic_read(&qp->s_reserved_used);
1545         if (unlikely(reserved_op)) {
1546                 /* see rvt_qp_wqe_unreserve() */
1547                 smp_mb__before_atomic();
1548                 if (reserved_used >= rdi->dparms.reserved_operations)
1549                         return -ENOMEM;
1550                 return 0;
1551         }
1552         /* non-reserved operations */
1553         if (likely(qp->s_avail))
1554                 return 0;
1555         smp_read_barrier_depends(); /* see rc.c */
1556         slast = ACCESS_ONCE(qp->s_last);
1557         if (qp->s_head >= slast)
1558                 avail = qp->s_size - (qp->s_head - slast);
1559         else
1560                 avail = slast - qp->s_head;
1561
1562         /* see rvt_qp_wqe_unreserve() */
1563         smp_mb__before_atomic();
1564         reserved_used = atomic_read(&qp->s_reserved_used);
1565         avail =  avail - 1 -
1566                 (rdi->dparms.reserved_operations - reserved_used);
1567         /* insure we don't assign a negative s_avail */
1568         if ((s32)avail <= 0)
1569                 return -ENOMEM;
1570         qp->s_avail = avail;
1571         if (WARN_ON(qp->s_avail >
1572                     (qp->s_size - 1 - rdi->dparms.reserved_operations)))
1573                 rvt_pr_err(rdi,
1574                            "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
1575                            qp->ibqp.qp_num, qp->s_size, qp->s_avail,
1576                            qp->s_head, qp->s_tail, qp->s_cur,
1577                            qp->s_acked, qp->s_last);
1578         return 0;
1579 }
1580
1581 /**
1582  * rvt_post_one_wr - post one RC, UC, or UD send work request
1583  * @qp: the QP to post on
1584  * @wr: the work request to send
1585  */
1586 static int rvt_post_one_wr(struct rvt_qp *qp,
1587                            struct ib_send_wr *wr,
1588                            int *call_send)
1589 {
1590         struct rvt_swqe *wqe;
1591         u32 next;
1592         int i;
1593         int j;
1594         int acc;
1595         struct rvt_lkey_table *rkt;
1596         struct rvt_pd *pd;
1597         struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
1598         u8 log_pmtu;
1599         int ret;
1600         size_t cplen;
1601         bool reserved_op;
1602         int local_ops_delayed = 0;
1603
1604         BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
1605
1606         /* IB spec says that num_sge == 0 is OK. */
1607         if (unlikely(wr->num_sge > qp->s_max_sge))
1608                 return -EINVAL;
1609
1610         ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
1611         if (ret < 0)
1612                 return ret;
1613         cplen = ret;
1614
1615         /*
1616          * Local operations include fast register and local invalidate.
1617          * Fast register needs to be processed immediately because the
1618          * registered lkey may be used by following work requests and the
1619          * lkey needs to be valid at the time those requests are posted.
1620          * Local invalidate can be processed immediately if fencing is
1621          * not required and no previous local invalidate ops are pending.
1622          * Signaled local operations that have been processed immediately
1623          * need to have requests with "completion only" flags set posted
1624          * to the send queue in order to generate completions.
1625          */
1626         if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
1627                 switch (wr->opcode) {
1628                 case IB_WR_REG_MR:
1629                         ret = rvt_fast_reg_mr(qp,
1630                                               reg_wr(wr)->mr,
1631                                               reg_wr(wr)->key,
1632                                               reg_wr(wr)->access);
1633                         if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1634                                 return ret;
1635                         break;
1636                 case IB_WR_LOCAL_INV:
1637                         if ((wr->send_flags & IB_SEND_FENCE) ||
1638                             atomic_read(&qp->local_ops_pending)) {
1639                                 local_ops_delayed = 1;
1640                         } else {
1641                                 ret = rvt_invalidate_rkey(
1642                                         qp, wr->ex.invalidate_rkey);
1643                                 if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
1644                                         return ret;
1645                         }
1646                         break;
1647                 default:
1648                         return -EINVAL;
1649                 }
1650         }
1651
1652         reserved_op = rdi->post_parms[wr->opcode].flags &
1653                         RVT_OPERATION_USE_RESERVE;
1654         /* check for avail */
1655         ret = rvt_qp_is_avail(qp, rdi, reserved_op);
1656         if (ret)
1657                 return ret;
1658         next = qp->s_head + 1;
1659         if (next >= qp->s_size)
1660                 next = 0;
1661
1662         rkt = &rdi->lkey_table;
1663         pd = ibpd_to_rvtpd(qp->ibqp.pd);
1664         wqe = rvt_get_swqe_ptr(qp, qp->s_head);
1665
1666         /* cplen has length from above */
1667         memcpy(&wqe->wr, wr, cplen);
1668
1669         wqe->length = 0;
1670         j = 0;
1671         if (wr->num_sge) {
1672                 acc = wr->opcode >= IB_WR_RDMA_READ ?
1673                         IB_ACCESS_LOCAL_WRITE : 0;
1674                 for (i = 0; i < wr->num_sge; i++) {
1675                         u32 length = wr->sg_list[i].length;
1676                         int ok;
1677
1678                         if (length == 0)
1679                                 continue;
1680                         ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
1681                                          &wr->sg_list[i], acc);
1682                         if (!ok) {
1683                                 ret = -EINVAL;
1684                                 goto bail_inval_free;
1685                         }
1686                         wqe->length += length;
1687                         j++;
1688                 }
1689                 wqe->wr.num_sge = j;
1690         }
1691
1692         /* general part of wqe valid - allow for driver checks */
1693         if (rdi->driver_f.check_send_wqe) {
1694                 ret = rdi->driver_f.check_send_wqe(qp, wqe);
1695                 if (ret < 0)
1696                         goto bail_inval_free;
1697                 if (ret)
1698                         *call_send = ret;
1699         }
1700
1701         log_pmtu = qp->log_pmtu;
1702         if (qp->ibqp.qp_type != IB_QPT_UC &&
1703             qp->ibqp.qp_type != IB_QPT_RC) {
1704                 struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
1705
1706                 log_pmtu = ah->log_pmtu;
1707                 atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
1708         }
1709
1710         if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
1711                 if (local_ops_delayed)
1712                         atomic_inc(&qp->local_ops_pending);
1713                 else
1714                         wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
1715                 wqe->ssn = 0;
1716                 wqe->psn = 0;
1717                 wqe->lpsn = 0;
1718         } else {
1719                 wqe->ssn = qp->s_ssn++;
1720                 wqe->psn = qp->s_next_psn;
1721                 wqe->lpsn = wqe->psn +
1722                                 (wqe->length ?
1723                                         ((wqe->length - 1) >> log_pmtu) :
1724                                         0);
1725                 qp->s_next_psn = wqe->lpsn + 1;
1726         }
1727         trace_rvt_post_one_wr(qp, wqe);
1728         if (unlikely(reserved_op))
1729                 rvt_qp_wqe_reserve(qp, wqe);
1730         else
1731                 qp->s_avail--;
1732         smp_wmb(); /* see request builders */
1733         qp->s_head = next;
1734
1735         return 0;
1736
1737 bail_inval_free:
1738         /* release mr holds */
1739         while (j) {
1740                 struct rvt_sge *sge = &wqe->sg_list[--j];
1741
1742                 rvt_put_mr(sge->mr);
1743         }
1744         return ret;
1745 }
1746
1747 /**
1748  * rvt_post_send - post a send on a QP
1749  * @ibqp: the QP to post the send on
1750  * @wr: the list of work requests to post
1751  * @bad_wr: the first bad WR is put here
1752  *
1753  * This may be called from interrupt context.
1754  *
1755  * Return: 0 on success else errno
1756  */
1757 int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1758                   struct ib_send_wr **bad_wr)
1759 {
1760         struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
1761         struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
1762         unsigned long flags = 0;
1763         int call_send;
1764         unsigned nreq = 0;
1765         int err = 0;
1766
1767         spin_lock_irqsave(&qp->s_hlock, flags);
1768
1769         /*
1770          * Ensure QP state is such that we can send. If not bail out early,
1771          * there is no need to do this every time we post a send.
1772          */
1773         if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
1774                 spin_unlock_irqrestore(&qp->s_hlock, flags);
1775                 return -EINVAL;
1776         }
1777
1778         /*
1779          * If the send queue is empty, and we only have a single WR then just go
1780          * ahead and kick the send engine into gear. Otherwise we will always
1781          * just schedule the send to happen later.
1782          */
1783         call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
1784
1785         for (; wr; wr = wr->next) {
1786                 err = rvt_post_one_wr(qp, wr, &call_send);
1787                 if (unlikely(err)) {
1788                         *bad_wr = wr;
1789                         goto bail;
1790                 }
1791                 nreq++;
1792         }
1793 bail:
1794         spin_unlock_irqrestore(&qp->s_hlock, flags);
1795         if (nreq) {
1796                 if (call_send)
1797                         rdi->driver_f.do_send(qp);
1798                 else
1799                         rdi->driver_f.schedule_send_no_lock(qp);
1800         }
1801         return err;
1802 }
1803
1804 /**
1805  * rvt_post_srq_receive - post a receive on a shared receive queue
1806  * @ibsrq: the SRQ to post the receive on
1807  * @wr: the list of work requests to post
1808  * @bad_wr: A pointer to the first WR to cause a problem is put here
1809  *
1810  * This may be called from interrupt context.
1811  *
1812  * Return: 0 on success else errno
1813  */
1814 int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1815                       struct ib_recv_wr **bad_wr)
1816 {
1817         struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
1818         struct rvt_rwq *wq;
1819         unsigned long flags;
1820
1821         for (; wr; wr = wr->next) {
1822                 struct rvt_rwqe *wqe;
1823                 u32 next;
1824                 int i;
1825
1826                 if ((unsigned)wr->num_sge > srq->rq.max_sge) {
1827                         *bad_wr = wr;
1828                         return -EINVAL;
1829                 }
1830
1831                 spin_lock_irqsave(&srq->rq.lock, flags);
1832                 wq = srq->rq.wq;
1833                 next = wq->head + 1;
1834                 if (next >= srq->rq.size)
1835                         next = 0;
1836                 if (next == wq->tail) {
1837                         spin_unlock_irqrestore(&srq->rq.lock, flags);
1838                         *bad_wr = wr;
1839                         return -ENOMEM;
1840                 }
1841
1842                 wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
1843                 wqe->wr_id = wr->wr_id;
1844                 wqe->num_sge = wr->num_sge;
1845                 for (i = 0; i < wr->num_sge; i++)
1846                         wqe->sg_list[i] = wr->sg_list[i];
1847                 /* Make sure queue entry is written before the head index. */
1848                 smp_wmb();
1849                 wq->head = next;
1850                 spin_unlock_irqrestore(&srq->rq.lock, flags);
1851         }
1852         return 0;
1853 }