blob: f9b8cd2354d1ba6f6b077aea249f00b2f3a8e8bb [file] [log] [blame]
Ralph Campbellf9315512010-05-23 21:44:54 -07001/*
Mike Marciniszynf7cf9a62013-06-15 17:06:58 -04002 * Copyright (c) 2012, 2013 Intel Corporation. All rights reserved.
Mike Marciniszyn1fb9fed2012-07-16 17:11:06 +00003 * Copyright (c) 2006 - 2012 QLogic Corporation. * All rights reserved.
Ralph Campbellf9315512010-05-23 21:44:54 -07004 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35#include <linux/err.h>
36#include <linux/vmalloc.h>
Dennis Dalessandro869a2a92016-01-22 12:45:02 -080037#include <rdma/rdma_vt.h>
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -040038#ifdef CONFIG_DEBUG_FS
39#include <linux/seq_file.h>
40#endif
Ralph Campbellf9315512010-05-23 21:44:54 -070041
42#include "qib.h"
43
Harish Chegondi898fa522016-01-22 12:56:27 -080044/*
45 * mask field which was present in now deleted qib_qpn_table
46 * is not present in rvt_qpn_table. Defining the same field
47 * as qpt_mask here instead of adding the mask field to
48 * rvt_qpn_table.
49 */
Harish Chegondi47c7ea62016-01-22 12:56:52 -080050u16 qpt_mask;
Ralph Campbellf9315512010-05-23 21:44:54 -070051
Harish Chegondi898fa522016-01-22 12:56:27 -080052static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
53 struct rvt_qpn_map *map, unsigned off)
Ralph Campbellf9315512010-05-23 21:44:54 -070054{
Harish Chegondi898fa522016-01-22 12:56:27 -080055 return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
Ralph Campbellf9315512010-05-23 21:44:54 -070056}
57
Harish Chegondi898fa522016-01-22 12:56:27 -080058static inline unsigned find_next_offset(struct rvt_qpn_table *qpt,
59 struct rvt_qpn_map *map, unsigned off,
Mike Marciniszyn2528ea62011-01-10 17:42:21 -080060 unsigned n)
Ralph Campbellf9315512010-05-23 21:44:54 -070061{
Harish Chegondi898fa522016-01-22 12:56:27 -080062 if (qpt_mask) {
Ralph Campbellf9315512010-05-23 21:44:54 -070063 off++;
Harish Chegondi898fa522016-01-22 12:56:27 -080064 if (((off & qpt_mask) >> 1) >= n)
65 off = (off | qpt_mask) + 2;
66 } else {
67 off = find_next_zero_bit(map->page, RVT_BITS_PER_PAGE, off);
68 }
Ralph Campbellf9315512010-05-23 21:44:54 -070069 return off;
70}
71
72/*
73 * Convert the AETH credit code into the number of credits.
74 */
75static u32 credit_table[31] = {
76 0, /* 0 */
77 1, /* 1 */
78 2, /* 2 */
79 3, /* 3 */
80 4, /* 4 */
81 6, /* 5 */
82 8, /* 6 */
83 12, /* 7 */
84 16, /* 8 */
85 24, /* 9 */
86 32, /* A */
87 48, /* B */
88 64, /* C */
89 96, /* D */
90 128, /* E */
91 192, /* F */
92 256, /* 10 */
93 384, /* 11 */
94 512, /* 12 */
95 768, /* 13 */
96 1024, /* 14 */
97 1536, /* 15 */
98 2048, /* 16 */
99 3072, /* 17 */
100 4096, /* 18 */
101 6144, /* 19 */
102 8192, /* 1A */
103 12288, /* 1B */
104 16384, /* 1C */
105 24576, /* 1D */
106 32768 /* 1E */
107};
108
Mike Marciniszyn9ec4faa2016-07-01 16:02:18 -0700109const struct rvt_operation_params qib_post_parms[RVT_OPERATION_MAX] = {
110[IB_WR_RDMA_WRITE] = {
111 .length = sizeof(struct ib_rdma_wr),
112 .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
113},
114
115[IB_WR_RDMA_READ] = {
116 .length = sizeof(struct ib_rdma_wr),
117 .qpt_support = BIT(IB_QPT_RC),
118 .flags = RVT_OPERATION_ATOMIC,
119},
120
121[IB_WR_ATOMIC_CMP_AND_SWP] = {
122 .length = sizeof(struct ib_atomic_wr),
123 .qpt_support = BIT(IB_QPT_RC),
124 .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
125},
126
127[IB_WR_ATOMIC_FETCH_AND_ADD] = {
128 .length = sizeof(struct ib_atomic_wr),
129 .qpt_support = BIT(IB_QPT_RC),
130 .flags = RVT_OPERATION_ATOMIC | RVT_OPERATION_ATOMIC_SGE,
131},
132
133[IB_WR_RDMA_WRITE_WITH_IMM] = {
134 .length = sizeof(struct ib_rdma_wr),
135 .qpt_support = BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
136},
137
138[IB_WR_SEND] = {
139 .length = sizeof(struct ib_send_wr),
140 .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
141 BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
142},
143
144[IB_WR_SEND_WITH_IMM] = {
145 .length = sizeof(struct ib_send_wr),
146 .qpt_support = BIT(IB_QPT_UD) | BIT(IB_QPT_SMI) | BIT(IB_QPT_GSI) |
147 BIT(IB_QPT_UC) | BIT(IB_QPT_RC),
148},
149
150};
151
Harish Chegondi898fa522016-01-22 12:56:27 -0800152static void get_map_page(struct rvt_qpn_table *qpt, struct rvt_qpn_map *map,
Vinit Agnihotrifbbeb862016-01-11 12:57:25 -0500153 gfp_t gfp)
Ralph Campbellf9315512010-05-23 21:44:54 -0700154{
Vinit Agnihotrifbbeb862016-01-11 12:57:25 -0500155 unsigned long page = get_zeroed_page(gfp);
Ralph Campbellf9315512010-05-23 21:44:54 -0700156
157 /*
158 * Free the page if someone raced with us installing it.
159 */
160
161 spin_lock(&qpt->lock);
162 if (map->page)
163 free_page(page);
164 else
165 map->page = (void *)page;
166 spin_unlock(&qpt->lock);
167}
168
169/*
170 * Allocate the next available QPN or
171 * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI.
172 */
Harish Chegondi20f333b2016-02-14 12:09:55 -0800173int qib_alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
174 enum ib_qp_type type, u8 port, gfp_t gfp)
Ralph Campbellf9315512010-05-23 21:44:54 -0700175{
176 u32 i, offset, max_scan, qpn;
Harish Chegondi898fa522016-01-22 12:56:27 -0800177 struct rvt_qpn_map *map;
Ralph Campbellf9315512010-05-23 21:44:54 -0700178 u32 ret;
Harish Chegondi47c7ea62016-01-22 12:56:52 -0800179 struct qib_ibdev *verbs_dev = container_of(rdi, struct qib_ibdev, rdi);
180 struct qib_devdata *dd = container_of(verbs_dev, struct qib_devdata,
181 verbs_dev);
Ralph Campbellf9315512010-05-23 21:44:54 -0700182
183 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
184 unsigned n;
185
186 ret = type == IB_QPT_GSI;
187 n = 1 << (ret + 2 * (port - 1));
188 spin_lock(&qpt->lock);
189 if (qpt->flags & n)
190 ret = -EINVAL;
191 else
192 qpt->flags |= n;
193 spin_unlock(&qpt->lock);
194 goto bail;
195 }
196
Mike Marciniszyn7c3edd32011-01-10 17:42:22 -0800197 qpn = qpt->last + 2;
Harish Chegondi898fa522016-01-22 12:56:27 -0800198 if (qpn >= RVT_QPN_MAX)
Ralph Campbellf9315512010-05-23 21:44:54 -0700199 qpn = 2;
Harish Chegondi898fa522016-01-22 12:56:27 -0800200 if (qpt_mask && ((qpn & qpt_mask) >> 1) >= dd->n_krcv_queues)
201 qpn = (qpn | qpt_mask) + 2;
202 offset = qpn & RVT_BITS_PER_PAGE_MASK;
203 map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
Ralph Campbellf9315512010-05-23 21:44:54 -0700204 max_scan = qpt->nmaps - !offset;
205 for (i = 0;;) {
206 if (unlikely(!map->page)) {
Vinit Agnihotrifbbeb862016-01-11 12:57:25 -0500207 get_map_page(qpt, map, gfp);
Ralph Campbellf9315512010-05-23 21:44:54 -0700208 if (unlikely(!map->page))
209 break;
210 }
211 do {
212 if (!test_and_set_bit(offset, map->page)) {
213 qpt->last = qpn;
214 ret = qpn;
215 goto bail;
216 }
Mike Marciniszyn2528ea62011-01-10 17:42:21 -0800217 offset = find_next_offset(qpt, map, offset,
218 dd->n_krcv_queues);
Ralph Campbellf9315512010-05-23 21:44:54 -0700219 qpn = mk_qpn(qpt, map, offset);
220 /*
221 * This test differs from alloc_pidmap().
222 * If find_next_offset() does find a zero
223 * bit, we don't need to check for QPN
224 * wrapping around past our starting QPN.
225 * We just need to be sure we don't loop
226 * forever.
227 */
Harish Chegondi898fa522016-01-22 12:56:27 -0800228 } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
Ralph Campbellf9315512010-05-23 21:44:54 -0700229 /*
230 * In order to keep the number of pages allocated to a
231 * minimum, we scan the all existing pages before increasing
232 * the size of the bitmap table.
233 */
234 if (++i > max_scan) {
Harish Chegondi898fa522016-01-22 12:56:27 -0800235 if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
Ralph Campbellf9315512010-05-23 21:44:54 -0700236 break;
237 map = &qpt->map[qpt->nmaps++];
Mike Marciniszyn2528ea62011-01-10 17:42:21 -0800238 offset = 0;
Ralph Campbellf9315512010-05-23 21:44:54 -0700239 } else if (map < &qpt->map[qpt->nmaps]) {
240 ++map;
Mike Marciniszyn2528ea62011-01-10 17:42:21 -0800241 offset = 0;
Ralph Campbellf9315512010-05-23 21:44:54 -0700242 } else {
243 map = &qpt->map[0];
Mike Marciniszyn2528ea62011-01-10 17:42:21 -0800244 offset = 2;
Ralph Campbellf9315512010-05-23 21:44:54 -0700245 }
246 qpn = mk_qpn(qpt, map, offset);
247 }
248
249 ret = -ENOMEM;
250
251bail:
252 return ret;
253}
254
Ralph Campbellf9315512010-05-23 21:44:54 -0700255/**
256 * qib_free_all_qps - check for QPs still in use
Ralph Campbellf9315512010-05-23 21:44:54 -0700257 */
Harish Chegondi47c7ea62016-01-22 12:56:52 -0800258unsigned qib_free_all_qps(struct rvt_dev_info *rdi)
Ralph Campbellf9315512010-05-23 21:44:54 -0700259{
Harish Chegondi47c7ea62016-01-22 12:56:52 -0800260 struct qib_ibdev *verbs_dev = container_of(rdi, struct qib_ibdev, rdi);
261 struct qib_devdata *dd = container_of(verbs_dev, struct qib_devdata,
262 verbs_dev);
Ralph Campbellf9315512010-05-23 21:44:54 -0700263 unsigned n, qp_inuse = 0;
264
265 for (n = 0; n < dd->num_pports; n++) {
266 struct qib_ibport *ibp = &dd->pport[n].ibport_data;
267
Mike Marciniszynaf061a62011-09-23 13:16:44 -0400268 rcu_read_lock();
Harish Chegondif24a6d42016-01-22 12:56:02 -0800269 if (rcu_dereference(ibp->rvp.qp[0]))
Ralph Campbellf9315512010-05-23 21:44:54 -0700270 qp_inuse++;
Harish Chegondif24a6d42016-01-22 12:56:02 -0800271 if (rcu_dereference(ibp->rvp.qp[1]))
Ralph Campbellf9315512010-05-23 21:44:54 -0700272 qp_inuse++;
Mike Marciniszynaf061a62011-09-23 13:16:44 -0400273 rcu_read_unlock();
Ralph Campbellf9315512010-05-23 21:44:54 -0700274 }
Ralph Campbellf9315512010-05-23 21:44:54 -0700275 return qp_inuse;
276}
277
Harish Chegondi20f333b2016-02-14 12:09:55 -0800278void qib_notify_qp_reset(struct rvt_qp *qp)
Ralph Campbellf9315512010-05-23 21:44:54 -0700279{
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800280 struct qib_qp_priv *priv = qp->priv;
Harish Chegondi47c7ea62016-01-22 12:56:52 -0800281
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800282 atomic_set(&priv->s_dma_busy, 0);
Ralph Campbellf9315512010-05-23 21:44:54 -0700283}
284
Harish Chegondi20f333b2016-02-14 12:09:55 -0800285void qib_notify_error_qp(struct rvt_qp *qp)
Ralph Campbellf9315512010-05-23 21:44:54 -0700286{
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800287 struct qib_qp_priv *priv = qp->priv;
Ralph Campbellf9315512010-05-23 21:44:54 -0700288 struct qib_ibdev *dev = to_idev(qp->ibqp.device);
Mike Marciniszyn16028f22011-01-10 17:42:20 -0800289
Harish Chegondicd182012016-01-22 12:56:14 -0800290 spin_lock(&dev->rdi.pending_lock);
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800291 if (!list_empty(&priv->iowait) && !(qp->s_flags & RVT_S_BUSY)) {
292 qp->s_flags &= ~RVT_S_ANY_WAIT_IO;
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800293 list_del_init(&priv->iowait);
Ralph Campbellf9315512010-05-23 21:44:54 -0700294 }
Harish Chegondicd182012016-01-22 12:56:14 -0800295 spin_unlock(&dev->rdi.pending_lock);
Ralph Campbellf9315512010-05-23 21:44:54 -0700296
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800297 if (!(qp->s_flags & RVT_S_BUSY)) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700298 qp->s_hdrwords = 0;
299 if (qp->s_rdma_mr) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800300 rvt_put_mr(qp->s_rdma_mr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700301 qp->s_rdma_mr = NULL;
302 }
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800303 if (priv->s_tx) {
304 qib_put_txreq(priv->s_tx);
305 priv->s_tx = NULL;
Ralph Campbellf9315512010-05-23 21:44:54 -0700306 }
307 }
Ralph Campbellf9315512010-05-23 21:44:54 -0700308}
309
Harish Chegondi70696ea2016-02-03 14:20:27 -0800310static int mtu_to_enum(u32 mtu)
Ralph Campbellf9315512010-05-23 21:44:54 -0700311{
Harish Chegondi70696ea2016-02-03 14:20:27 -0800312 int enum_mtu;
Ralph Campbellf9315512010-05-23 21:44:54 -0700313
Harish Chegondi70696ea2016-02-03 14:20:27 -0800314 switch (mtu) {
315 case 4096:
316 enum_mtu = IB_MTU_4096;
Ralph Campbellf9315512010-05-23 21:44:54 -0700317 break;
Harish Chegondi70696ea2016-02-03 14:20:27 -0800318 case 2048:
319 enum_mtu = IB_MTU_2048;
Ralph Campbellf9315512010-05-23 21:44:54 -0700320 break;
Harish Chegondi70696ea2016-02-03 14:20:27 -0800321 case 1024:
322 enum_mtu = IB_MTU_1024;
Ralph Campbellf9315512010-05-23 21:44:54 -0700323 break;
Harish Chegondi70696ea2016-02-03 14:20:27 -0800324 case 512:
325 enum_mtu = IB_MTU_512;
Ralph Campbellf9315512010-05-23 21:44:54 -0700326 break;
Harish Chegondi70696ea2016-02-03 14:20:27 -0800327 case 256:
328 enum_mtu = IB_MTU_256;
Ralph Campbellf9315512010-05-23 21:44:54 -0700329 break;
Ralph Campbellf9315512010-05-23 21:44:54 -0700330 default:
Harish Chegondi70696ea2016-02-03 14:20:27 -0800331 enum_mtu = IB_MTU_2048;
Ralph Campbellf9315512010-05-23 21:44:54 -0700332 }
Harish Chegondi70696ea2016-02-03 14:20:27 -0800333 return enum_mtu;
334}
Ralph Campbellf9315512010-05-23 21:44:54 -0700335
Harish Chegondi20f333b2016-02-14 12:09:55 -0800336int qib_get_pmtu_from_attr(struct rvt_dev_info *rdi, struct rvt_qp *qp,
337 struct ib_qp_attr *attr)
Harish Chegondi70696ea2016-02-03 14:20:27 -0800338{
339 int mtu, pmtu, pidx = qp->port_num - 1;
340 struct qib_ibdev *verbs_dev = container_of(rdi, struct qib_ibdev, rdi);
341 struct qib_devdata *dd = container_of(verbs_dev, struct qib_devdata,
342 verbs_dev);
343 mtu = ib_mtu_enum_to_int(attr->path_mtu);
344 if (mtu == -1)
345 return -EINVAL;
Ralph Campbellf9315512010-05-23 21:44:54 -0700346
Harish Chegondi70696ea2016-02-03 14:20:27 -0800347 if (mtu > dd->pport[pidx].ibmtu)
348 pmtu = mtu_to_enum(dd->pport[pidx].ibmtu);
349 else
350 pmtu = attr->path_mtu;
351 return pmtu;
352}
Ralph Campbellf9315512010-05-23 21:44:54 -0700353
Harish Chegondi20f333b2016-02-14 12:09:55 -0800354int qib_mtu_to_path_mtu(u32 mtu)
Harish Chegondi70696ea2016-02-03 14:20:27 -0800355{
356 return mtu_to_enum(mtu);
357}
Ralph Campbellf9315512010-05-23 21:44:54 -0700358
Harish Chegondi20f333b2016-02-14 12:09:55 -0800359u32 qib_mtu_from_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, u32 pmtu)
Harish Chegondi70696ea2016-02-03 14:20:27 -0800360{
361 return ib_mtu_enum_to_int(pmtu);
Ralph Campbellf9315512010-05-23 21:44:54 -0700362}
363
Ralph Campbellf9315512010-05-23 21:44:54 -0700364/**
365 * qib_compute_aeth - compute the AETH (syndrome + MSN)
366 * @qp: the queue pair to compute the AETH for
367 *
368 * Returns the AETH.
369 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800370__be32 qib_compute_aeth(struct rvt_qp *qp)
Ralph Campbellf9315512010-05-23 21:44:54 -0700371{
372 u32 aeth = qp->r_msn & QIB_MSN_MASK;
373
374 if (qp->ibqp.srq) {
375 /*
376 * Shared receive queues don't generate credits.
377 * Set the credit field to the invalid value.
378 */
379 aeth |= QIB_AETH_CREDIT_INVAL << QIB_AETH_CREDIT_SHIFT;
380 } else {
381 u32 min, max, x;
382 u32 credits;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800383 struct rvt_rwq *wq = qp->r_rq.wq;
Ralph Campbellf9315512010-05-23 21:44:54 -0700384 u32 head;
385 u32 tail;
386
387 /* sanity check pointers before trusting them */
388 head = wq->head;
389 if (head >= qp->r_rq.size)
390 head = 0;
391 tail = wq->tail;
392 if (tail >= qp->r_rq.size)
393 tail = 0;
394 /*
395 * Compute the number of credits available (RWQEs).
396 * XXX Not holding the r_rq.lock here so there is a small
397 * chance that the pair of reads are not atomic.
398 */
399 credits = head - tail;
400 if ((int)credits < 0)
401 credits += qp->r_rq.size;
402 /*
403 * Binary search the credit table to find the code to
404 * use.
405 */
406 min = 0;
407 max = 31;
408 for (;;) {
409 x = (min + max) / 2;
410 if (credit_table[x] == credits)
411 break;
412 if (credit_table[x] > credits)
413 max = x;
414 else if (min == x)
415 break;
416 else
417 min = x;
418 }
419 aeth |= x << QIB_AETH_CREDIT_SHIFT;
420 }
421 return cpu_to_be32(aeth);
422}
423
Harish Chegondi20f333b2016-02-14 12:09:55 -0800424void *qib_qp_priv_alloc(struct rvt_dev_info *rdi, struct rvt_qp *qp, gfp_t gfp)
Ralph Campbellf9315512010-05-23 21:44:54 -0700425{
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800426 struct qib_qp_priv *priv;
Ralph Campbellf9315512010-05-23 21:44:54 -0700427
Harish Chegondi47c7ea62016-01-22 12:56:52 -0800428 priv = kzalloc(sizeof(*priv), gfp);
429 if (!priv)
430 return ERR_PTR(-ENOMEM);
431 priv->owner = qp;
Vinit Agnihotrifbbeb862016-01-11 12:57:25 -0500432
Harish Chegondi47c7ea62016-01-22 12:56:52 -0800433 priv->s_hdr = kzalloc(sizeof(*priv->s_hdr), gfp);
434 if (!priv->s_hdr) {
435 kfree(priv);
436 return ERR_PTR(-ENOMEM);
Ralph Campbellf9315512010-05-23 21:44:54 -0700437 }
Harish Chegondi47c7ea62016-01-22 12:56:52 -0800438 init_waitqueue_head(&priv->wait_dma);
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800439 INIT_WORK(&priv->s_work, _qib_do_send);
Harish Chegondi47c7ea62016-01-22 12:56:52 -0800440 INIT_LIST_HEAD(&priv->iowait);
Ralph Campbellf9315512010-05-23 21:44:54 -0700441
Harish Chegondi47c7ea62016-01-22 12:56:52 -0800442 return priv;
443}
Ralph Campbellf9315512010-05-23 21:44:54 -0700444
Harish Chegondi20f333b2016-02-14 12:09:55 -0800445void qib_qp_priv_free(struct rvt_dev_info *rdi, struct rvt_qp *qp)
Harish Chegondi47c7ea62016-01-22 12:56:52 -0800446{
447 struct qib_qp_priv *priv = qp->priv;
Vinit Agnihotrifbbeb862016-01-11 12:57:25 -0500448
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800449 kfree(priv->s_hdr);
450 kfree(priv);
Ralph Campbellf9315512010-05-23 21:44:54 -0700451}
452
Harish Chegondi20f333b2016-02-14 12:09:55 -0800453void qib_stop_send_queue(struct rvt_qp *qp)
Harish Chegondi70696ea2016-02-03 14:20:27 -0800454{
455 struct qib_qp_priv *priv = qp->priv;
456
457 cancel_work_sync(&priv->s_work);
Mike Marciniszyn08279d52016-02-04 10:59:36 -0800458 del_timer_sync(&qp->s_timer);
Harish Chegondi70696ea2016-02-03 14:20:27 -0800459}
460
Harish Chegondi20f333b2016-02-14 12:09:55 -0800461void qib_quiesce_qp(struct rvt_qp *qp)
Harish Chegondi70696ea2016-02-03 14:20:27 -0800462{
463 struct qib_qp_priv *priv = qp->priv;
464
465 wait_event(priv->wait_dma, !atomic_read(&priv->s_dma_busy));
466 if (priv->s_tx) {
467 qib_put_txreq(priv->s_tx);
468 priv->s_tx = NULL;
469 }
470}
471
Harish Chegondi20f333b2016-02-14 12:09:55 -0800472void qib_flush_qp_waiters(struct rvt_qp *qp)
Harish Chegondi70696ea2016-02-03 14:20:27 -0800473{
474 struct qib_qp_priv *priv = qp->priv;
475 struct qib_ibdev *dev = to_idev(qp->ibqp.device);
476
477 spin_lock(&dev->rdi.pending_lock);
478 if (!list_empty(&priv->iowait))
479 list_del_init(&priv->iowait);
480 spin_unlock(&dev->rdi.pending_lock);
481}
482
Ralph Campbellf9315512010-05-23 21:44:54 -0700483/**
Ralph Campbellf9315512010-05-23 21:44:54 -0700484 * qib_get_credit - flush the send work queue of a QP
485 * @qp: the qp who's send work queue to flush
486 * @aeth: the Acknowledge Extended Transport Header
487 *
488 * The QP s_lock should be held.
489 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800490void qib_get_credit(struct rvt_qp *qp, u32 aeth)
Ralph Campbellf9315512010-05-23 21:44:54 -0700491{
492 u32 credit = (aeth >> QIB_AETH_CREDIT_SHIFT) & QIB_AETH_CREDIT_MASK;
493
494 /*
495 * If the credit is invalid, we can send
496 * as many packets as we like. Otherwise, we have to
497 * honor the credit field.
498 */
499 if (credit == QIB_AETH_CREDIT_INVAL) {
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800500 if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) {
501 qp->s_flags |= RVT_S_UNLIMITED_CREDIT;
502 if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) {
503 qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT;
Ralph Campbellf9315512010-05-23 21:44:54 -0700504 qib_schedule_send(qp);
505 }
506 }
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800507 } else if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700508 /* Compute new LSN (i.e., MSN + credit) */
509 credit = (aeth + credit_table[credit]) & QIB_MSN_MASK;
510 if (qib_cmp24(credit, qp->s_lsn) > 0) {
511 qp->s_lsn = credit;
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800512 if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) {
513 qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT;
Ralph Campbellf9315512010-05-23 21:44:54 -0700514 qib_schedule_send(qp);
515 }
516 }
517 }
518}
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -0400519
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800520/**
521 * qib_check_send_wqe - validate wr/wqe
522 * @qp - The qp
523 * @wqe - The built wqe
524 *
525 * validate wr/wqe. This is called
526 * prior to inserting the wqe into
527 * the ring but after the wqe has been
528 * setup.
529 *
Mike Marciniszyn91702b42016-02-14 12:45:44 -0800530 * Returns 1 to force direct progress, 0 otherwise, -EINVAL on failure
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800531 */
532int qib_check_send_wqe(struct rvt_qp *qp,
533 struct rvt_swqe *wqe)
534{
535 struct rvt_ah *ah;
Mike Marciniszyn91702b42016-02-14 12:45:44 -0800536 int ret = 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800537
538 switch (qp->ibqp.qp_type) {
539 case IB_QPT_RC:
540 case IB_QPT_UC:
541 if (wqe->length > 0x80000000U)
542 return -EINVAL;
543 break;
544 case IB_QPT_SMI:
545 case IB_QPT_GSI:
546 case IB_QPT_UD:
547 ah = ibah_to_rvtah(wqe->ud_wr.ah);
548 if (wqe->length > (1 << ah->log_pmtu))
549 return -EINVAL;
Mike Marciniszyn91702b42016-02-14 12:45:44 -0800550 /* progress hint */
551 ret = 1;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800552 break;
553 default:
554 break;
555 }
Mike Marciniszyn91702b42016-02-14 12:45:44 -0800556 return ret;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800557}
558
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -0400559#ifdef CONFIG_DEBUG_FS
560
561struct qib_qp_iter {
562 struct qib_ibdev *dev;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800563 struct rvt_qp *qp;
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -0400564 int n;
565};
566
567struct qib_qp_iter *qib_qp_iter_init(struct qib_ibdev *dev)
568{
569 struct qib_qp_iter *iter;
570
571 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
572 if (!iter)
573 return NULL;
574
575 iter->dev = dev;
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -0400576
577 return iter;
578}
579
580int qib_qp_iter_next(struct qib_qp_iter *iter)
581{
582 struct qib_ibdev *dev = iter->dev;
583 int n = iter->n;
584 int ret = 1;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800585 struct rvt_qp *pqp = iter->qp;
586 struct rvt_qp *qp;
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -0400587
Harish Chegondi898fa522016-01-22 12:56:27 -0800588 for (; n < dev->rdi.qp_dev->qp_table_size; n++) {
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -0400589 if (pqp)
590 qp = rcu_dereference(pqp->next);
591 else
Harish Chegondi898fa522016-01-22 12:56:27 -0800592 qp = rcu_dereference(dev->rdi.qp_dev->qp_table[n]);
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -0400593 pqp = qp;
594 if (qp) {
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -0400595 iter->qp = qp;
596 iter->n = n;
597 return 0;
598 }
599 }
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -0400600 return ret;
601}
602
603static const char * const qp_type_str[] = {
604 "SMI", "GSI", "RC", "UC", "UD",
605};
606
607void qib_qp_iter_print(struct seq_file *s, struct qib_qp_iter *iter)
608{
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800609 struct rvt_swqe *wqe;
610 struct rvt_qp *qp = iter->qp;
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800611 struct qib_qp_priv *priv = qp->priv;
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -0400612
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800613 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -0400614 seq_printf(s,
615 "N %d QP%u %s %u %u %u f=%x %u %u %u %u %u PSN %x %x %x %x %x (%u %u %u %u %u %u) QP%u LID %x\n",
616 iter->n,
617 qp->ibqp.qp_num,
618 qp_type_str[qp->ibqp.qp_type],
619 qp->state,
620 wqe->wr.opcode,
621 qp->s_hdrwords,
622 qp->s_flags,
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800623 atomic_read(&priv->s_dma_busy),
624 !list_empty(&priv->iowait),
Mike Marciniszyn1dd173b2013-06-15 17:07:14 -0400625 qp->timeout,
626 wqe->ssn,
627 qp->s_lsn,
628 qp->s_last_psn,
629 qp->s_psn, qp->s_next_psn,
630 qp->s_sending_psn, qp->s_sending_hpsn,
631 qp->s_last, qp->s_acked, qp->s_cur,
632 qp->s_tail, qp->s_head, qp->s_size,
633 qp->remote_qpn,
634 qp->remote_ah_attr.dlid);
635}
636
637#endif