staging/rdma/hfi1: Remove srq from hfi1
[deliverable/linux.git] / drivers / staging / rdma / hfi1 / qp.c
CommitLineData
77241056
MM
1/*
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2015 Intel Corporation.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * BSD LICENSE
20 *
21 * Copyright(c) 2015 Intel Corporation.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 *
27 * - Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * - Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
32 * distribution.
33 * - Neither the name of Intel Corporation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 *
49 */
50
51#include <linux/err.h>
52#include <linux/vmalloc.h>
53#include <linux/hash.h>
54#include <linux/module.h>
55#include <linux/random.h>
56#include <linux/seq_file.h>
57
58#include "hfi.h"
59#include "qp.h"
60#include "trace.h"
61#include "sdma.h"
62
63#define BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE)
64#define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1)
65
66static unsigned int hfi1_qp_table_size = 256;
67module_param_named(qp_table_size, hfi1_qp_table_size, uint, S_IRUGO);
68MODULE_PARM_DESC(qp_table_size, "QP table size");
69
895420dd 70static void flush_tx_list(struct rvt_qp *qp);
77241056
MM
71static int iowait_sleep(
72 struct sdma_engine *sde,
73 struct iowait *wait,
74 struct sdma_txreq *stx,
75 unsigned seq);
76static void iowait_wakeup(struct iowait *wait, int reason);
77
78static inline unsigned mk_qpn(struct hfi1_qpn_table *qpt,
79 struct qpn_map *map, unsigned off)
80{
81 return (map - qpt->map) * BITS_PER_PAGE + off;
82}
83
84/*
85 * Convert the AETH credit code into the number of credits.
86 */
87static const u16 credit_table[31] = {
88 0, /* 0 */
89 1, /* 1 */
90 2, /* 2 */
91 3, /* 3 */
92 4, /* 4 */
93 6, /* 5 */
94 8, /* 6 */
95 12, /* 7 */
96 16, /* 8 */
97 24, /* 9 */
98 32, /* A */
99 48, /* B */
100 64, /* C */
101 96, /* D */
102 128, /* E */
103 192, /* F */
104 256, /* 10 */
105 384, /* 11 */
106 512, /* 12 */
107 768, /* 13 */
108 1024, /* 14 */
109 1536, /* 15 */
110 2048, /* 16 */
111 3072, /* 17 */
112 4096, /* 18 */
113 6144, /* 19 */
114 8192, /* 1A */
115 12288, /* 1B */
116 16384, /* 1C */
117 24576, /* 1D */
118 32768 /* 1E */
119};
120
121static void get_map_page(struct hfi1_qpn_table *qpt, struct qpn_map *map)
122{
123 unsigned long page = get_zeroed_page(GFP_KERNEL);
124
125 /*
126 * Free the page if someone raced with us installing it.
127 */
128
129 spin_lock(&qpt->lock);
130 if (map->page)
131 free_page(page);
132 else
133 map->page = (void *)page;
134 spin_unlock(&qpt->lock);
135}
136
137/*
138 * Allocate the next available QPN or
139 * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI.
140 */
141static int alloc_qpn(struct hfi1_devdata *dd, struct hfi1_qpn_table *qpt,
142 enum ib_qp_type type, u8 port)
143{
144 u32 i, offset, max_scan, qpn;
145 struct qpn_map *map;
146 u32 ret;
147
148 if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
149 unsigned n;
150
151 ret = type == IB_QPT_GSI;
152 n = 1 << (ret + 2 * (port - 1));
153 spin_lock(&qpt->lock);
154 if (qpt->flags & n)
155 ret = -EINVAL;
156 else
157 qpt->flags |= n;
158 spin_unlock(&qpt->lock);
159 goto bail;
160 }
161
162 qpn = qpt->last + qpt->incr;
163 if (qpn >= QPN_MAX)
164 qpn = qpt->incr | ((qpt->last & 1) ^ 1);
165 /* offset carries bit 0 */
166 offset = qpn & BITS_PER_PAGE_MASK;
167 map = &qpt->map[qpn / BITS_PER_PAGE];
168 max_scan = qpt->nmaps - !offset;
169 for (i = 0;;) {
170 if (unlikely(!map->page)) {
171 get_map_page(qpt, map);
172 if (unlikely(!map->page))
173 break;
174 }
175 do {
176 if (!test_and_set_bit(offset, map->page)) {
177 qpt->last = qpn;
178 ret = qpn;
179 goto bail;
180 }
181 offset += qpt->incr;
182 /*
183 * This qpn might be bogus if offset >= BITS_PER_PAGE.
184 * That is OK. It gets re-assigned below
185 */
186 qpn = mk_qpn(qpt, map, offset);
187 } while (offset < BITS_PER_PAGE && qpn < QPN_MAX);
188 /*
189 * In order to keep the number of pages allocated to a
190 * minimum, we scan the all existing pages before increasing
191 * the size of the bitmap table.
192 */
193 if (++i > max_scan) {
194 if (qpt->nmaps == QPNMAP_ENTRIES)
195 break;
196 map = &qpt->map[qpt->nmaps++];
197 /* start at incr with current bit 0 */
198 offset = qpt->incr | (offset & 1);
199 } else if (map < &qpt->map[qpt->nmaps]) {
200 ++map;
201 /* start at incr with current bit 0 */
202 offset = qpt->incr | (offset & 1);
203 } else {
204 map = &qpt->map[0];
205 /* wrap to first map page, invert bit 0 */
206 offset = qpt->incr | ((offset & 1) ^ 1);
207 }
208 /* there can be no bits at shift and below */
209 WARN_ON(offset & (dd->qos_shift - 1));
210 qpn = mk_qpn(qpt, map, offset);
211 }
212
213 ret = -ENOMEM;
214
215bail:
216 return ret;
217}
218
219static void free_qpn(struct hfi1_qpn_table *qpt, u32 qpn)
220{
221 struct qpn_map *map;
222
223 map = qpt->map + qpn / BITS_PER_PAGE;
224 if (map->page)
225 clear_bit(qpn & BITS_PER_PAGE_MASK, map->page);
226}
227
228/*
229 * Put the QP into the hash table.
230 * The hash table holds a reference to the QP.
231 */
895420dd 232static void insert_qp(struct hfi1_ibdev *dev, struct rvt_qp *qp)
77241056
MM
233{
234 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
235 unsigned long flags;
236
237 atomic_inc(&qp->refcount);
238 spin_lock_irqsave(&dev->qp_dev->qpt_lock, flags);
239
240 if (qp->ibqp.qp_num <= 1) {
241 rcu_assign_pointer(ibp->qp[qp->ibqp.qp_num], qp);
242 } else {
243 u32 n = qpn_hash(dev->qp_dev, qp->ibqp.qp_num);
244
245 qp->next = dev->qp_dev->qp_table[n];
246 rcu_assign_pointer(dev->qp_dev->qp_table[n], qp);
247 trace_hfi1_qpinsert(qp, n);
248 }
249
250 spin_unlock_irqrestore(&dev->qp_dev->qpt_lock, flags);
251}
252
253/*
254 * Remove the QP from the table so it can't be found asynchronously by
255 * the receive interrupt routine.
256 */
895420dd 257static void remove_qp(struct hfi1_ibdev *dev, struct rvt_qp *qp)
77241056
MM
258{
259 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
260 u32 n = qpn_hash(dev->qp_dev, qp->ibqp.qp_num);
261 unsigned long flags;
262 int removed = 1;
263
264 spin_lock_irqsave(&dev->qp_dev->qpt_lock, flags);
265
266 if (rcu_dereference_protected(ibp->qp[0],
267 lockdep_is_held(&dev->qp_dev->qpt_lock)) == qp) {
268 RCU_INIT_POINTER(ibp->qp[0], NULL);
269 } else if (rcu_dereference_protected(ibp->qp[1],
270 lockdep_is_held(&dev->qp_dev->qpt_lock)) == qp) {
271 RCU_INIT_POINTER(ibp->qp[1], NULL);
272 } else {
895420dd
DD
273 struct rvt_qp *q;
274 struct rvt_qp __rcu **qpp;
77241056
MM
275
276 removed = 0;
277 qpp = &dev->qp_dev->qp_table[n];
278 for (; (q = rcu_dereference_protected(*qpp,
279 lockdep_is_held(&dev->qp_dev->qpt_lock)))
280 != NULL;
281 qpp = &q->next)
282 if (q == qp) {
283 RCU_INIT_POINTER(*qpp,
284 rcu_dereference_protected(qp->next,
285 lockdep_is_held(&dev->qp_dev->qpt_lock)));
286 removed = 1;
287 trace_hfi1_qpremove(qp, n);
288 break;
289 }
290 }
291
292 spin_unlock_irqrestore(&dev->qp_dev->qpt_lock, flags);
293 if (removed) {
294 synchronize_rcu();
295 if (atomic_dec_and_test(&qp->refcount))
296 wake_up(&qp->wait);
297 }
298}
299
300/**
301 * free_all_qps - check for QPs still in use
302 * @qpt: the QP table to empty
303 *
304 * There should not be any QPs still in use.
305 * Free memory for table.
306 */
307static unsigned free_all_qps(struct hfi1_devdata *dd)
308{
309 struct hfi1_ibdev *dev = &dd->verbs_dev;
310 unsigned long flags;
895420dd 311 struct rvt_qp *qp;
77241056
MM
312 unsigned n, qp_inuse = 0;
313
314 for (n = 0; n < dd->num_pports; n++) {
315 struct hfi1_ibport *ibp = &dd->pport[n].ibport_data;
316
317 if (!hfi1_mcast_tree_empty(ibp))
318 qp_inuse++;
319 rcu_read_lock();
320 if (rcu_dereference(ibp->qp[0]))
321 qp_inuse++;
322 if (rcu_dereference(ibp->qp[1]))
323 qp_inuse++;
324 rcu_read_unlock();
325 }
326
327 if (!dev->qp_dev)
328 goto bail;
329 spin_lock_irqsave(&dev->qp_dev->qpt_lock, flags);
330 for (n = 0; n < dev->qp_dev->qp_table_size; n++) {
331 qp = rcu_dereference_protected(dev->qp_dev->qp_table[n],
332 lockdep_is_held(&dev->qp_dev->qpt_lock));
333 RCU_INIT_POINTER(dev->qp_dev->qp_table[n], NULL);
334
335 for (; qp; qp = rcu_dereference_protected(qp->next,
336 lockdep_is_held(&dev->qp_dev->qpt_lock)))
337 qp_inuse++;
338 }
339 spin_unlock_irqrestore(&dev->qp_dev->qpt_lock, flags);
340 synchronize_rcu();
341bail:
342 return qp_inuse;
343}
344
345/**
346 * reset_qp - initialize the QP state to the reset state
347 * @qp: the QP to reset
348 * @type: the QP type
349 */
895420dd 350static void reset_qp(struct rvt_qp *qp, enum ib_qp_type type)
77241056 351{
4c6829c5 352 struct hfi1_qp_priv *priv = qp->priv;
77241056
MM
353 qp->remote_qpn = 0;
354 qp->qkey = 0;
355 qp->qp_access_flags = 0;
356 iowait_init(
4c6829c5 357 &priv->s_iowait,
77241056
MM
358 1,
359 hfi1_do_send,
360 iowait_sleep,
361 iowait_wakeup);
362 qp->s_flags &= HFI1_S_SIGNAL_REQ_WR;
363 qp->s_hdrwords = 0;
364 qp->s_wqe = NULL;
365 qp->s_draining = 0;
366 qp->s_next_psn = 0;
367 qp->s_last_psn = 0;
368 qp->s_sending_psn = 0;
369 qp->s_sending_hpsn = 0;
370 qp->s_psn = 0;
371 qp->r_psn = 0;
372 qp->r_msn = 0;
373 if (type == IB_QPT_RC) {
374 qp->s_state = IB_OPCODE_RC_SEND_LAST;
375 qp->r_state = IB_OPCODE_RC_SEND_LAST;
376 } else {
377 qp->s_state = IB_OPCODE_UC_SEND_LAST;
378 qp->r_state = IB_OPCODE_UC_SEND_LAST;
379 }
380 qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
381 qp->r_nak_state = 0;
4c6829c5 382 priv->r_adefered = 0;
77241056
MM
383 qp->r_aflags = 0;
384 qp->r_flags = 0;
385 qp->s_head = 0;
386 qp->s_tail = 0;
387 qp->s_cur = 0;
388 qp->s_acked = 0;
389 qp->s_last = 0;
390 qp->s_ssn = 1;
391 qp->s_lsn = 0;
392 clear_ahg(qp);
393 qp->s_mig_state = IB_MIG_MIGRATED;
394 memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
395 qp->r_head_ack_queue = 0;
396 qp->s_tail_ack_queue = 0;
397 qp->s_num_rd_atomic = 0;
398 if (qp->r_rq.wq) {
399 qp->r_rq.wq->head = 0;
400 qp->r_rq.wq->tail = 0;
401 }
402 qp->r_sge.num_sge = 0;
403}
404
895420dd 405static void clear_mr_refs(struct rvt_qp *qp, int clr_sends)
77241056
MM
406{
407 unsigned n;
408
409 if (test_and_clear_bit(HFI1_R_REWIND_SGE, &qp->r_aflags))
410 hfi1_put_ss(&qp->s_rdma_read_sge);
411
412 hfi1_put_ss(&qp->r_sge);
413
414 if (clr_sends) {
415 while (qp->s_last != qp->s_head) {
895420dd 416 struct rvt_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
77241056
MM
417 unsigned i;
418
419 for (i = 0; i < wqe->wr.num_sge; i++) {
895420dd 420 struct rvt_sge *sge = &wqe->sg_list[i];
77241056 421
895420dd 422 rvt_put_mr(sge->mr);
77241056
MM
423 }
424 if (qp->ibqp.qp_type == IB_QPT_UD ||
425 qp->ibqp.qp_type == IB_QPT_SMI ||
426 qp->ibqp.qp_type == IB_QPT_GSI)
15723f06 427 atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
77241056
MM
428 if (++qp->s_last >= qp->s_size)
429 qp->s_last = 0;
430 }
431 if (qp->s_rdma_mr) {
895420dd 432 rvt_put_mr(qp->s_rdma_mr);
77241056
MM
433 qp->s_rdma_mr = NULL;
434 }
435 }
436
437 if (qp->ibqp.qp_type != IB_QPT_RC)
438 return;
439
440 for (n = 0; n < ARRAY_SIZE(qp->s_ack_queue); n++) {
895420dd 441 struct rvt_ack_entry *e = &qp->s_ack_queue[n];
77241056
MM
442
443 if (e->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST &&
444 e->rdma_sge.mr) {
895420dd 445 rvt_put_mr(e->rdma_sge.mr);
77241056
MM
446 e->rdma_sge.mr = NULL;
447 }
448 }
449}
450
451/**
452 * hfi1_error_qp - put a QP into the error state
453 * @qp: the QP to put into the error state
454 * @err: the receive completion error to signal if a RWQE is active
455 *
456 * Flushes both send and receive work queues.
457 * Returns true if last WQE event should be generated.
458 * The QP r_lock and s_lock should be held and interrupts disabled.
459 * If we are already in error state, just return.
460 */
895420dd 461int hfi1_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
77241056
MM
462{
463 struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
4c6829c5 464 struct hfi1_qp_priv *priv = qp->priv;
77241056
MM
465 struct ib_wc wc;
466 int ret = 0;
467
468 if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
469 goto bail;
470
471 qp->state = IB_QPS_ERR;
472
473 if (qp->s_flags & (HFI1_S_TIMER | HFI1_S_WAIT_RNR)) {
474 qp->s_flags &= ~(HFI1_S_TIMER | HFI1_S_WAIT_RNR);
475 del_timer(&qp->s_timer);
476 }
477
478 if (qp->s_flags & HFI1_S_ANY_WAIT_SEND)
479 qp->s_flags &= ~HFI1_S_ANY_WAIT_SEND;
480
481 write_seqlock(&dev->iowait_lock);
4c6829c5 482 if (!list_empty(&priv->s_iowait.list) && !(qp->s_flags & HFI1_S_BUSY)) {
77241056 483 qp->s_flags &= ~HFI1_S_ANY_WAIT_IO;
4c6829c5 484 list_del_init(&priv->s_iowait.list);
77241056
MM
485 if (atomic_dec_and_test(&qp->refcount))
486 wake_up(&qp->wait);
487 }
488 write_sequnlock(&dev->iowait_lock);
489
490 if (!(qp->s_flags & HFI1_S_BUSY)) {
491 qp->s_hdrwords = 0;
492 if (qp->s_rdma_mr) {
895420dd 493 rvt_put_mr(qp->s_rdma_mr);
77241056
MM
494 qp->s_rdma_mr = NULL;
495 }
496 flush_tx_list(qp);
497 }
498
499 /* Schedule the sending tasklet to drain the send work queue. */
500 if (qp->s_last != qp->s_head)
501 hfi1_schedule_send(qp);
502
503 clear_mr_refs(qp, 0);
504
505 memset(&wc, 0, sizeof(wc));
506 wc.qp = &qp->ibqp;
507 wc.opcode = IB_WC_RECV;
508
509 if (test_and_clear_bit(HFI1_R_WRID_VALID, &qp->r_aflags)) {
510 wc.wr_id = qp->r_wr_id;
511 wc.status = err;
512 hfi1_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
513 }
514 wc.status = IB_WC_WR_FLUSH_ERR;
515
516 if (qp->r_rq.wq) {
895420dd 517 struct rvt_rwq *wq;
77241056
MM
518 u32 head;
519 u32 tail;
520
521 spin_lock(&qp->r_rq.lock);
522
523 /* sanity check pointers before trusting them */
524 wq = qp->r_rq.wq;
525 head = wq->head;
526 if (head >= qp->r_rq.size)
527 head = 0;
528 tail = wq->tail;
529 if (tail >= qp->r_rq.size)
530 tail = 0;
531 while (tail != head) {
532 wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
533 if (++tail >= qp->r_rq.size)
534 tail = 0;
535 hfi1_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
536 }
537 wq->tail = tail;
538
539 spin_unlock(&qp->r_rq.lock);
540 } else if (qp->ibqp.event_handler)
541 ret = 1;
542
543bail:
544 return ret;
545}
546
895420dd 547static void flush_tx_list(struct rvt_qp *qp)
77241056 548{
4c6829c5
DD
549 struct hfi1_qp_priv *priv = qp->priv;
550
551 while (!list_empty(&priv->s_iowait.tx_head)) {
77241056
MM
552 struct sdma_txreq *tx;
553
554 tx = list_first_entry(
4c6829c5 555 &priv->s_iowait.tx_head,
77241056
MM
556 struct sdma_txreq,
557 list);
558 list_del_init(&tx->list);
559 hfi1_put_txreq(
560 container_of(tx, struct verbs_txreq, txreq));
561 }
562}
563
895420dd 564static void flush_iowait(struct rvt_qp *qp)
77241056 565{
4c6829c5 566 struct hfi1_qp_priv *priv = qp->priv;
77241056
MM
567 struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
568 unsigned long flags;
569
570 write_seqlock_irqsave(&dev->iowait_lock, flags);
4c6829c5
DD
571 if (!list_empty(&priv->s_iowait.list)) {
572 list_del_init(&priv->s_iowait.list);
77241056
MM
573 if (atomic_dec_and_test(&qp->refcount))
574 wake_up(&qp->wait);
575 }
576 write_sequnlock_irqrestore(&dev->iowait_lock, flags);
577}
578
579static inline int opa_mtu_enum_to_int(int mtu)
580{
581 switch (mtu) {
582 case OPA_MTU_8192: return 8192;
583 case OPA_MTU_10240: return 10240;
584 default: return -1;
585 }
586}
587
588/**
589 * This function is what we would push to the core layer if we wanted to be a
590 * "first class citizen". Instead we hide this here and rely on Verbs ULPs
591 * to blindly pass the MTU enum value from the PathRecord to us.
592 *
593 * The actual flag used to determine "8k MTU" will change and is currently
594 * unknown.
595 */
596static inline int verbs_mtu_enum_to_int(struct ib_device *dev, enum ib_mtu mtu)
597{
598 int val = opa_mtu_enum_to_int((int)mtu);
599
600 if (val > 0)
601 return val;
602 return ib_mtu_enum_to_int(mtu);
603}
604
605
606/**
607 * hfi1_modify_qp - modify the attributes of a queue pair
608 * @ibqp: the queue pair who's attributes we're modifying
609 * @attr: the new attributes
610 * @attr_mask: the mask of attributes to modify
611 * @udata: user data for libibverbs.so
612 *
613 * Returns 0 on success, otherwise returns an errno.
614 */
615int hfi1_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
616 int attr_mask, struct ib_udata *udata)
617{
618 struct hfi1_ibdev *dev = to_idev(ibqp->device);
895420dd 619 struct rvt_qp *qp = to_iqp(ibqp);
4c6829c5 620 struct hfi1_qp_priv *priv = qp->priv;
77241056
MM
621 enum ib_qp_state cur_state, new_state;
622 struct ib_event ev;
623 int lastwqe = 0;
624 int mig = 0;
625 int ret;
626 u32 pmtu = 0; /* for gcc warning only */
d7b8ba51 627 struct hfi1_devdata *dd = dd_from_dev(dev);
77241056
MM
628
629 spin_lock_irq(&qp->r_lock);
630 spin_lock(&qp->s_lock);
631
632 cur_state = attr_mask & IB_QP_CUR_STATE ?
633 attr->cur_qp_state : qp->state;
634 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
635
636 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
637 attr_mask, IB_LINK_LAYER_UNSPECIFIED))
638 goto inval;
639
640 if (attr_mask & IB_QP_AV) {
d7b8ba51
MM
641 u8 sc;
642
8859b4a6 643 if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
77241056 644 goto inval;
15723f06 645 if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
77241056 646 goto inval;
d7b8ba51
MM
647 sc = ah_to_sc(ibqp->device, &attr->ah_attr);
648 if (!qp_to_sdma_engine(qp, sc) &&
649 dd->flags & HFI1_HAS_SEND_DMA)
650 goto inval;
77241056
MM
651 }
652
653 if (attr_mask & IB_QP_ALT_PATH) {
d7b8ba51
MM
654 u8 sc;
655
8859b4a6
DD
656 if (attr->alt_ah_attr.dlid >=
657 be16_to_cpu(IB_MULTICAST_LID_BASE))
77241056 658 goto inval;
15723f06 659 if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
77241056 660 goto inval;
d7b8ba51
MM
661 if (attr->alt_pkey_index >= hfi1_get_npkeys(dd))
662 goto inval;
663 sc = ah_to_sc(ibqp->device, &attr->alt_ah_attr);
664 if (!qp_to_sdma_engine(qp, sc) &&
665 dd->flags & HFI1_HAS_SEND_DMA)
77241056
MM
666 goto inval;
667 }
668
669 if (attr_mask & IB_QP_PKEY_INDEX)
d7b8ba51 670 if (attr->pkey_index >= hfi1_get_npkeys(dd))
77241056
MM
671 goto inval;
672
673 if (attr_mask & IB_QP_MIN_RNR_TIMER)
674 if (attr->min_rnr_timer > 31)
675 goto inval;
676
677 if (attr_mask & IB_QP_PORT)
678 if (qp->ibqp.qp_type == IB_QPT_SMI ||
679 qp->ibqp.qp_type == IB_QPT_GSI ||
680 attr->port_num == 0 ||
681 attr->port_num > ibqp->device->phys_port_cnt)
682 goto inval;
683
684 if (attr_mask & IB_QP_DEST_QPN)
685 if (attr->dest_qp_num > HFI1_QPN_MASK)
686 goto inval;
687
688 if (attr_mask & IB_QP_RETRY_CNT)
689 if (attr->retry_cnt > 7)
690 goto inval;
691
692 if (attr_mask & IB_QP_RNR_RETRY)
693 if (attr->rnr_retry > 7)
694 goto inval;
695
696 /*
697 * Don't allow invalid path_mtu values. OK to set greater
698 * than the active mtu (or even the max_cap, if we have tuned
699 * that to a small mtu. We'll set qp->path_mtu
700 * to the lesser of requested attribute mtu and active,
701 * for packetizing messages.
702 * Note that the QP port has to be set in INIT and MTU in RTR.
703 */
704 if (attr_mask & IB_QP_PATH_MTU) {
705 int mtu, pidx = qp->port_num - 1;
706
707 dd = dd_from_dev(dev);
708 mtu = verbs_mtu_enum_to_int(ibqp->device, attr->path_mtu);
709 if (mtu == -1)
710 goto inval;
711
712 if (mtu > dd->pport[pidx].ibmtu)
713 pmtu = mtu_to_enum(dd->pport[pidx].ibmtu, IB_MTU_2048);
714 else
715 pmtu = attr->path_mtu;
716 }
717
718 if (attr_mask & IB_QP_PATH_MIG_STATE) {
719 if (attr->path_mig_state == IB_MIG_REARM) {
720 if (qp->s_mig_state == IB_MIG_ARMED)
721 goto inval;
722 if (new_state != IB_QPS_RTS)
723 goto inval;
724 } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
725 if (qp->s_mig_state == IB_MIG_REARM)
726 goto inval;
727 if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
728 goto inval;
729 if (qp->s_mig_state == IB_MIG_ARMED)
730 mig = 1;
731 } else
732 goto inval;
733 }
734
735 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
736 if (attr->max_dest_rd_atomic > HFI1_MAX_RDMA_ATOMIC)
737 goto inval;
738
739 switch (new_state) {
740 case IB_QPS_RESET:
741 if (qp->state != IB_QPS_RESET) {
742 qp->state = IB_QPS_RESET;
743 flush_iowait(qp);
744 qp->s_flags &= ~(HFI1_S_TIMER | HFI1_S_ANY_WAIT);
745 spin_unlock(&qp->s_lock);
746 spin_unlock_irq(&qp->r_lock);
747 /* Stop the sending work queue and retry timer */
4c6829c5 748 cancel_work_sync(&priv->s_iowait.iowork);
77241056 749 del_timer_sync(&qp->s_timer);
4c6829c5 750 iowait_sdma_drain(&priv->s_iowait);
77241056
MM
751 flush_tx_list(qp);
752 remove_qp(dev, qp);
753 wait_event(qp->wait, !atomic_read(&qp->refcount));
754 spin_lock_irq(&qp->r_lock);
755 spin_lock(&qp->s_lock);
756 clear_mr_refs(qp, 1);
757 clear_ahg(qp);
758 reset_qp(qp, ibqp->qp_type);
759 }
760 break;
761
762 case IB_QPS_RTR:
763 /* Allow event to re-trigger if QP set to RTR more than once */
764 qp->r_flags &= ~HFI1_R_COMM_EST;
765 qp->state = new_state;
766 break;
767
768 case IB_QPS_SQD:
769 qp->s_draining = qp->s_last != qp->s_cur;
770 qp->state = new_state;
771 break;
772
773 case IB_QPS_SQE:
774 if (qp->ibqp.qp_type == IB_QPT_RC)
775 goto inval;
776 qp->state = new_state;
777 break;
778
779 case IB_QPS_ERR:
780 lastwqe = hfi1_error_qp(qp, IB_WC_WR_FLUSH_ERR);
781 break;
782
783 default:
784 qp->state = new_state;
785 break;
786 }
787
788 if (attr_mask & IB_QP_PKEY_INDEX)
789 qp->s_pkey_index = attr->pkey_index;
790
791 if (attr_mask & IB_QP_PORT)
792 qp->port_num = attr->port_num;
793
794 if (attr_mask & IB_QP_DEST_QPN)
795 qp->remote_qpn = attr->dest_qp_num;
796
797 if (attr_mask & IB_QP_SQ_PSN) {
798 qp->s_next_psn = attr->sq_psn & PSN_MODIFY_MASK;
799 qp->s_psn = qp->s_next_psn;
800 qp->s_sending_psn = qp->s_next_psn;
801 qp->s_last_psn = qp->s_next_psn - 1;
802 qp->s_sending_hpsn = qp->s_last_psn;
803 }
804
805 if (attr_mask & IB_QP_RQ_PSN)
806 qp->r_psn = attr->rq_psn & PSN_MODIFY_MASK;
807
808 if (attr_mask & IB_QP_ACCESS_FLAGS)
809 qp->qp_access_flags = attr->qp_access_flags;
810
811 if (attr_mask & IB_QP_AV) {
812 qp->remote_ah_attr = attr->ah_attr;
813 qp->s_srate = attr->ah_attr.static_rate;
814 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
4c6829c5
DD
815 priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr);
816 priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
77241056
MM
817 }
818
819 if (attr_mask & IB_QP_ALT_PATH) {
820 qp->alt_ah_attr = attr->alt_ah_attr;
821 qp->s_alt_pkey_index = attr->alt_pkey_index;
822 }
823
824 if (attr_mask & IB_QP_PATH_MIG_STATE) {
825 qp->s_mig_state = attr->path_mig_state;
826 if (mig) {
827 qp->remote_ah_attr = qp->alt_ah_attr;
828 qp->port_num = qp->alt_ah_attr.port_num;
829 qp->s_pkey_index = qp->s_alt_pkey_index;
830 qp->s_flags |= HFI1_S_AHG_CLEAR;
4c6829c5
DD
831 priv->s_sc = ah_to_sc(ibqp->device, &qp->remote_ah_attr);
832 priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
77241056
MM
833 }
834 }
835
836 if (attr_mask & IB_QP_PATH_MTU) {
837 struct hfi1_ibport *ibp;
838 u8 sc, vl;
839 u32 mtu;
840
841 dd = dd_from_dev(dev);
842 ibp = &dd->pport[qp->port_num - 1].ibport_data;
843
844 sc = ibp->sl_to_sc[qp->remote_ah_attr.sl];
845 vl = sc_to_vlt(dd, sc);
846
847 mtu = verbs_mtu_enum_to_int(ibqp->device, pmtu);
848 if (vl < PER_VL_SEND_CONTEXTS)
849 mtu = min_t(u32, mtu, dd->vld[vl].mtu);
850 pmtu = mtu_to_enum(mtu, OPA_MTU_8192);
851
852 qp->path_mtu = pmtu;
853 qp->pmtu = mtu;
854 }
855
856 if (attr_mask & IB_QP_RETRY_CNT) {
857 qp->s_retry_cnt = attr->retry_cnt;
858 qp->s_retry = attr->retry_cnt;
859 }
860
861 if (attr_mask & IB_QP_RNR_RETRY) {
862 qp->s_rnr_retry_cnt = attr->rnr_retry;
863 qp->s_rnr_retry = attr->rnr_retry;
864 }
865
866 if (attr_mask & IB_QP_MIN_RNR_TIMER)
867 qp->r_min_rnr_timer = attr->min_rnr_timer;
868
869 if (attr_mask & IB_QP_TIMEOUT) {
870 qp->timeout = attr->timeout;
871 qp->timeout_jiffies =
872 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
873 1000UL);
874 }
875
876 if (attr_mask & IB_QP_QKEY)
877 qp->qkey = attr->qkey;
878
879 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
880 qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
881
882 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
883 qp->s_max_rd_atomic = attr->max_rd_atomic;
884
885 spin_unlock(&qp->s_lock);
886 spin_unlock_irq(&qp->r_lock);
887
888 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
889 insert_qp(dev, qp);
890
891 if (lastwqe) {
892 ev.device = qp->ibqp.device;
893 ev.element.qp = &qp->ibqp;
894 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
895 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
896 }
897 if (mig) {
898 ev.device = qp->ibqp.device;
899 ev.element.qp = &qp->ibqp;
900 ev.event = IB_EVENT_PATH_MIG;
901 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
902 }
903 ret = 0;
904 goto bail;
905
906inval:
907 spin_unlock(&qp->s_lock);
908 spin_unlock_irq(&qp->r_lock);
909 ret = -EINVAL;
910
911bail:
912 return ret;
913}
914
915int hfi1_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
916 int attr_mask, struct ib_qp_init_attr *init_attr)
917{
895420dd 918 struct rvt_qp *qp = to_iqp(ibqp);
77241056
MM
919
920 attr->qp_state = qp->state;
921 attr->cur_qp_state = attr->qp_state;
922 attr->path_mtu = qp->path_mtu;
923 attr->path_mig_state = qp->s_mig_state;
924 attr->qkey = qp->qkey;
925 attr->rq_psn = mask_psn(qp->r_psn);
926 attr->sq_psn = mask_psn(qp->s_next_psn);
927 attr->dest_qp_num = qp->remote_qpn;
928 attr->qp_access_flags = qp->qp_access_flags;
929 attr->cap.max_send_wr = qp->s_size - 1;
930 attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
931 attr->cap.max_send_sge = qp->s_max_sge;
932 attr->cap.max_recv_sge = qp->r_rq.max_sge;
933 attr->cap.max_inline_data = 0;
934 attr->ah_attr = qp->remote_ah_attr;
935 attr->alt_ah_attr = qp->alt_ah_attr;
936 attr->pkey_index = qp->s_pkey_index;
937 attr->alt_pkey_index = qp->s_alt_pkey_index;
938 attr->en_sqd_async_notify = 0;
939 attr->sq_draining = qp->s_draining;
940 attr->max_rd_atomic = qp->s_max_rd_atomic;
941 attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
942 attr->min_rnr_timer = qp->r_min_rnr_timer;
943 attr->port_num = qp->port_num;
944 attr->timeout = qp->timeout;
945 attr->retry_cnt = qp->s_retry_cnt;
946 attr->rnr_retry = qp->s_rnr_retry_cnt;
947 attr->alt_port_num = qp->alt_ah_attr.port_num;
948 attr->alt_timeout = qp->alt_timeout;
949
950 init_attr->event_handler = qp->ibqp.event_handler;
951 init_attr->qp_context = qp->ibqp.qp_context;
952 init_attr->send_cq = qp->ibqp.send_cq;
953 init_attr->recv_cq = qp->ibqp.recv_cq;
954 init_attr->srq = qp->ibqp.srq;
955 init_attr->cap = attr->cap;
956 if (qp->s_flags & HFI1_S_SIGNAL_REQ_WR)
957 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
958 else
959 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
960 init_attr->qp_type = qp->ibqp.qp_type;
961 init_attr->port_num = qp->port_num;
962 return 0;
963}
964
965/**
966 * hfi1_compute_aeth - compute the AETH (syndrome + MSN)
967 * @qp: the queue pair to compute the AETH for
968 *
969 * Returns the AETH.
970 */
895420dd 971__be32 hfi1_compute_aeth(struct rvt_qp *qp)
77241056
MM
972{
973 u32 aeth = qp->r_msn & HFI1_MSN_MASK;
974
975 if (qp->ibqp.srq) {
976 /*
977 * Shared receive queues don't generate credits.
978 * Set the credit field to the invalid value.
979 */
980 aeth |= HFI1_AETH_CREDIT_INVAL << HFI1_AETH_CREDIT_SHIFT;
981 } else {
982 u32 min, max, x;
983 u32 credits;
895420dd 984 struct rvt_rwq *wq = qp->r_rq.wq;
77241056
MM
985 u32 head;
986 u32 tail;
987
988 /* sanity check pointers before trusting them */
989 head = wq->head;
990 if (head >= qp->r_rq.size)
991 head = 0;
992 tail = wq->tail;
993 if (tail >= qp->r_rq.size)
994 tail = 0;
995 /*
996 * Compute the number of credits available (RWQEs).
997 * There is a small chance that the pair of reads are
998 * not atomic, which is OK, since the fuzziness is
999 * resolved as further ACKs go out.
1000 */
1001 credits = head - tail;
1002 if ((int)credits < 0)
1003 credits += qp->r_rq.size;
1004 /*
1005 * Binary search the credit table to find the code to
1006 * use.
1007 */
1008 min = 0;
1009 max = 31;
1010 for (;;) {
1011 x = (min + max) / 2;
1012 if (credit_table[x] == credits)
1013 break;
1014 if (credit_table[x] > credits)
1015 max = x;
1016 else if (min == x)
1017 break;
1018 else
1019 min = x;
1020 }
1021 aeth |= x << HFI1_AETH_CREDIT_SHIFT;
1022 }
1023 return cpu_to_be32(aeth);
1024}
1025
1026/**
1027 * hfi1_create_qp - create a queue pair for a device
1028 * @ibpd: the protection domain who's device we create the queue pair for
1029 * @init_attr: the attributes of the queue pair
1030 * @udata: user data for libibverbs.so
1031 *
1032 * Returns the queue pair on success, otherwise returns an errno.
1033 *
1034 * Called by the ib_create_qp() core verbs function.
1035 */
1036struct ib_qp *hfi1_create_qp(struct ib_pd *ibpd,
1037 struct ib_qp_init_attr *init_attr,
1038 struct ib_udata *udata)
1039{
895420dd 1040 struct rvt_qp *qp;
4c6829c5 1041 struct hfi1_qp_priv *priv;
77241056 1042 int err;
895420dd 1043 struct rvt_swqe *swq = NULL;
77241056
MM
1044 struct hfi1_ibdev *dev;
1045 struct hfi1_devdata *dd;
1046 size_t sz;
1047 size_t sg_list_sz;
1048 struct ib_qp *ret;
1049
1050 if (init_attr->cap.max_send_sge > hfi1_max_sges ||
1051 init_attr->cap.max_send_wr > hfi1_max_qp_wrs ||
1052 init_attr->create_flags) {
1053 ret = ERR_PTR(-EINVAL);
1054 goto bail;
1055 }
1056
1057 /* Check receive queue parameters if no SRQ is specified. */
1058 if (!init_attr->srq) {
1059 if (init_attr->cap.max_recv_sge > hfi1_max_sges ||
1060 init_attr->cap.max_recv_wr > hfi1_max_qp_wrs) {
1061 ret = ERR_PTR(-EINVAL);
1062 goto bail;
1063 }
1064 if (init_attr->cap.max_send_sge +
1065 init_attr->cap.max_send_wr +
1066 init_attr->cap.max_recv_sge +
1067 init_attr->cap.max_recv_wr == 0) {
1068 ret = ERR_PTR(-EINVAL);
1069 goto bail;
1070 }
1071 }
1072
1073 switch (init_attr->qp_type) {
1074 case IB_QPT_SMI:
1075 case IB_QPT_GSI:
1076 if (init_attr->port_num == 0 ||
1077 init_attr->port_num > ibpd->device->phys_port_cnt) {
1078 ret = ERR_PTR(-EINVAL);
1079 goto bail;
1080 }
1081 case IB_QPT_UC:
1082 case IB_QPT_RC:
1083 case IB_QPT_UD:
895420dd 1084 sz = sizeof(struct rvt_sge) *
77241056 1085 init_attr->cap.max_send_sge +
895420dd 1086 sizeof(struct rvt_swqe);
77241056
MM
1087 swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
1088 if (swq == NULL) {
1089 ret = ERR_PTR(-ENOMEM);
1090 goto bail;
1091 }
1092 sz = sizeof(*qp);
1093 sg_list_sz = 0;
1094 if (init_attr->srq) {
39db3e66 1095 struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
77241056
MM
1096
1097 if (srq->rq.max_sge > 1)
1098 sg_list_sz = sizeof(*qp->r_sg_list) *
1099 (srq->rq.max_sge - 1);
1100 } else if (init_attr->cap.max_recv_sge > 1)
1101 sg_list_sz = sizeof(*qp->r_sg_list) *
1102 (init_attr->cap.max_recv_sge - 1);
1103 qp = kzalloc(sz + sg_list_sz, GFP_KERNEL);
1104 if (!qp) {
1105 ret = ERR_PTR(-ENOMEM);
1106 goto bail_swq;
1107 }
1108 RCU_INIT_POINTER(qp->next, NULL);
4c6829c5
DD
1109 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1110 if (!priv) {
1111 ret = ERR_PTR(-ENOMEM);
1112 goto bail_qp_priv;
1113 }
1114 priv->owner = qp;
1115 priv->s_hdr = kzalloc(sizeof(*priv->s_hdr), GFP_KERNEL);
1116 if (!priv->s_hdr) {
77241056
MM
1117 ret = ERR_PTR(-ENOMEM);
1118 goto bail_qp;
1119 }
4c6829c5 1120 qp->priv = priv;
77241056
MM
1121 qp->timeout_jiffies =
1122 usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
1123 1000UL);
1124 if (init_attr->srq)
1125 sz = 0;
1126 else {
1127 qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
1128 qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
1129 sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
895420dd
DD
1130 sizeof(struct rvt_rwqe);
1131 qp->r_rq.wq = vmalloc_user(sizeof(struct rvt_rwq) +
77241056
MM
1132 qp->r_rq.size * sz);
1133 if (!qp->r_rq.wq) {
1134 ret = ERR_PTR(-ENOMEM);
1135 goto bail_qp;
1136 }
1137 }
1138
1139 /*
1140 * ib_create_qp() will initialize qp->ibqp
1141 * except for qp->ibqp.qp_num.
1142 */
1143 spin_lock_init(&qp->r_lock);
1144 spin_lock_init(&qp->s_lock);
1145 spin_lock_init(&qp->r_rq.lock);
1146 atomic_set(&qp->refcount, 0);
1147 init_waitqueue_head(&qp->wait);
1148 init_timer(&qp->s_timer);
1149 qp->s_timer.data = (unsigned long)qp;
1150 INIT_LIST_HEAD(&qp->rspwait);
1151 qp->state = IB_QPS_RESET;
1152 qp->s_wq = swq;
1153 qp->s_size = init_attr->cap.max_send_wr + 1;
1154 qp->s_max_sge = init_attr->cap.max_send_sge;
1155 if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
1156 qp->s_flags = HFI1_S_SIGNAL_REQ_WR;
1157 dev = to_idev(ibpd->device);
1158 dd = dd_from_dev(dev);
1159 err = alloc_qpn(dd, &dev->qp_dev->qpn_table, init_attr->qp_type,
1160 init_attr->port_num);
1161 if (err < 0) {
1162 ret = ERR_PTR(err);
1163 vfree(qp->r_rq.wq);
1164 goto bail_qp;
1165 }
1166 qp->ibqp.qp_num = err;
1167 qp->port_num = init_attr->port_num;
1168 reset_qp(qp, init_attr->qp_type);
1169
1170 break;
1171
1172 default:
1173 /* Don't support raw QPs */
1174 ret = ERR_PTR(-ENOSYS);
1175 goto bail;
1176 }
1177
1178 init_attr->cap.max_inline_data = 0;
1179
1180 /*
1181 * Return the address of the RWQ as the offset to mmap.
1182 * See hfi1_mmap() for details.
1183 */
1184 if (udata && udata->outlen >= sizeof(__u64)) {
1185 if (!qp->r_rq.wq) {
1186 __u64 offset = 0;
1187
1188 err = ib_copy_to_udata(udata, &offset,
1189 sizeof(offset));
1190 if (err) {
1191 ret = ERR_PTR(err);
1192 goto bail_ip;
1193 }
1194 } else {
895420dd 1195 u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
77241056
MM
1196
1197 qp->ip = hfi1_create_mmap_info(dev, s,
1198 ibpd->uobject->context,
1199 qp->r_rq.wq);
1200 if (!qp->ip) {
1201 ret = ERR_PTR(-ENOMEM);
1202 goto bail_ip;
1203 }
1204
1205 err = ib_copy_to_udata(udata, &(qp->ip->offset),
1206 sizeof(qp->ip->offset));
1207 if (err) {
1208 ret = ERR_PTR(err);
1209 goto bail_ip;
1210 }
1211 }
1212 }
1213
1214 spin_lock(&dev->n_qps_lock);
1215 if (dev->n_qps_allocated == hfi1_max_qps) {
1216 spin_unlock(&dev->n_qps_lock);
1217 ret = ERR_PTR(-ENOMEM);
1218 goto bail_ip;
1219 }
1220
1221 dev->n_qps_allocated++;
1222 spin_unlock(&dev->n_qps_lock);
1223
1224 if (qp->ip) {
1225 spin_lock_irq(&dev->pending_lock);
1226 list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps);
1227 spin_unlock_irq(&dev->pending_lock);
1228 }
1229
1230 ret = &qp->ibqp;
1231
1232 /*
1233 * We have our QP and its good, now keep track of what types of opcodes
1234 * can be processed on this QP. We do this by keeping track of what the
1235 * 3 high order bits of the opcode are.
1236 */
1237 switch (init_attr->qp_type) {
1238 case IB_QPT_SMI:
1239 case IB_QPT_GSI:
1240 case IB_QPT_UD:
1241 qp->allowed_ops = IB_OPCODE_UD_SEND_ONLY & OPCODE_QP_MASK;
1242 break;
1243 case IB_QPT_RC:
1244 qp->allowed_ops = IB_OPCODE_RC_SEND_ONLY & OPCODE_QP_MASK;
1245 break;
1246 case IB_QPT_UC:
1247 qp->allowed_ops = IB_OPCODE_UC_SEND_ONLY & OPCODE_QP_MASK;
1248 break;
1249 default:
1250 ret = ERR_PTR(-EINVAL);
1251 goto bail_ip;
1252 }
1253
1254 goto bail;
1255
1256bail_ip:
1257 if (qp->ip)
1258 kref_put(&qp->ip->ref, hfi1_release_mmap_info);
1259 else
1260 vfree(qp->r_rq.wq);
1261 free_qpn(&dev->qp_dev->qpn_table, qp->ibqp.qp_num);
1262bail_qp:
4c6829c5
DD
1263 kfree(priv->s_hdr);
1264 kfree(priv);
1265bail_qp_priv:
77241056
MM
1266 kfree(qp);
1267bail_swq:
1268 vfree(swq);
1269bail:
1270 return ret;
1271}
1272
1273/**
1274 * hfi1_destroy_qp - destroy a queue pair
1275 * @ibqp: the queue pair to destroy
1276 *
1277 * Returns 0 on success.
1278 *
1279 * Note that this can be called while the QP is actively sending or
1280 * receiving!
1281 */
1282int hfi1_destroy_qp(struct ib_qp *ibqp)
1283{
895420dd 1284 struct rvt_qp *qp = to_iqp(ibqp);
77241056 1285 struct hfi1_ibdev *dev = to_idev(ibqp->device);
4c6829c5 1286 struct hfi1_qp_priv *priv = qp->priv;
77241056
MM
1287
1288 /* Make sure HW and driver activity is stopped. */
1289 spin_lock_irq(&qp->r_lock);
1290 spin_lock(&qp->s_lock);
1291 if (qp->state != IB_QPS_RESET) {
1292 qp->state = IB_QPS_RESET;
1293 flush_iowait(qp);
1294 qp->s_flags &= ~(HFI1_S_TIMER | HFI1_S_ANY_WAIT);
1295 spin_unlock(&qp->s_lock);
1296 spin_unlock_irq(&qp->r_lock);
4c6829c5 1297 cancel_work_sync(&priv->s_iowait.iowork);
77241056 1298 del_timer_sync(&qp->s_timer);
4c6829c5 1299 iowait_sdma_drain(&priv->s_iowait);
77241056
MM
1300 flush_tx_list(qp);
1301 remove_qp(dev, qp);
1302 wait_event(qp->wait, !atomic_read(&qp->refcount));
1303 spin_lock_irq(&qp->r_lock);
1304 spin_lock(&qp->s_lock);
1305 clear_mr_refs(qp, 1);
1306 clear_ahg(qp);
1307 }
1308 spin_unlock(&qp->s_lock);
1309 spin_unlock_irq(&qp->r_lock);
1310
1311 /* all user's cleaned up, mark it available */
1312 free_qpn(&dev->qp_dev->qpn_table, qp->ibqp.qp_num);
1313 spin_lock(&dev->n_qps_lock);
1314 dev->n_qps_allocated--;
1315 spin_unlock(&dev->n_qps_lock);
1316
1317 if (qp->ip)
1318 kref_put(&qp->ip->ref, hfi1_release_mmap_info);
1319 else
1320 vfree(qp->r_rq.wq);
1321 vfree(qp->s_wq);
4c6829c5
DD
1322 kfree(priv->s_hdr);
1323 kfree(priv);
77241056
MM
1324 kfree(qp);
1325 return 0;
1326}
1327
1328/**
1329 * init_qpn_table - initialize the QP number table for a device
1330 * @qpt: the QPN table
1331 */
1332static int init_qpn_table(struct hfi1_devdata *dd, struct hfi1_qpn_table *qpt)
1333{
1334 u32 offset, qpn, i;
1335 struct qpn_map *map;
1336 int ret = 0;
1337
1338 spin_lock_init(&qpt->lock);
1339
1340 qpt->last = 0;
1341 qpt->incr = 1 << dd->qos_shift;
1342
1343 /* insure we don't assign QPs from KDETH 64K window */
1344 qpn = kdeth_qp << 16;
1345 qpt->nmaps = qpn / BITS_PER_PAGE;
1346 /* This should always be zero */
1347 offset = qpn & BITS_PER_PAGE_MASK;
1348 map = &qpt->map[qpt->nmaps];
1349 dd_dev_info(dd, "Reserving QPNs for KDETH window from 0x%x to 0x%x\n",
1350 qpn, qpn + 65535);
1351 for (i = 0; i < 65536; i++) {
1352 if (!map->page) {
1353 get_map_page(qpt, map);
1354 if (!map->page) {
1355 ret = -ENOMEM;
1356 break;
1357 }
1358 }
1359 set_bit(offset, map->page);
1360 offset++;
1361 if (offset == BITS_PER_PAGE) {
1362 /* next page */
1363 qpt->nmaps++;
1364 map++;
1365 offset = 0;
1366 }
1367 }
1368 return ret;
1369}
1370
1371/**
1372 * free_qpn_table - free the QP number table for a device
1373 * @qpt: the QPN table
1374 */
1375static void free_qpn_table(struct hfi1_qpn_table *qpt)
1376{
1377 int i;
1378
1379 for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
1380 free_page((unsigned long) qpt->map[i].page);
1381}
1382
1383/**
1384 * hfi1_get_credit - flush the send work queue of a QP
1385 * @qp: the qp who's send work queue to flush
1386 * @aeth: the Acknowledge Extended Transport Header
1387 *
1388 * The QP s_lock should be held.
1389 */
895420dd 1390void hfi1_get_credit(struct rvt_qp *qp, u32 aeth)
77241056
MM
1391{
1392 u32 credit = (aeth >> HFI1_AETH_CREDIT_SHIFT) & HFI1_AETH_CREDIT_MASK;
1393
1394 /*
1395 * If the credit is invalid, we can send
1396 * as many packets as we like. Otherwise, we have to
1397 * honor the credit field.
1398 */
1399 if (credit == HFI1_AETH_CREDIT_INVAL) {
1400 if (!(qp->s_flags & HFI1_S_UNLIMITED_CREDIT)) {
1401 qp->s_flags |= HFI1_S_UNLIMITED_CREDIT;
1402 if (qp->s_flags & HFI1_S_WAIT_SSN_CREDIT) {
1403 qp->s_flags &= ~HFI1_S_WAIT_SSN_CREDIT;
1404 hfi1_schedule_send(qp);
1405 }
1406 }
1407 } else if (!(qp->s_flags & HFI1_S_UNLIMITED_CREDIT)) {
1408 /* Compute new LSN (i.e., MSN + credit) */
1409 credit = (aeth + credit_table[credit]) & HFI1_MSN_MASK;
1410 if (cmp_msn(credit, qp->s_lsn) > 0) {
1411 qp->s_lsn = credit;
1412 if (qp->s_flags & HFI1_S_WAIT_SSN_CREDIT) {
1413 qp->s_flags &= ~HFI1_S_WAIT_SSN_CREDIT;
1414 hfi1_schedule_send(qp);
1415 }
1416 }
1417 }
1418}
1419
895420dd 1420void hfi1_qp_wakeup(struct rvt_qp *qp, u32 flag)
77241056
MM
1421{
1422 unsigned long flags;
1423
1424 spin_lock_irqsave(&qp->s_lock, flags);
1425 if (qp->s_flags & flag) {
1426 qp->s_flags &= ~flag;
1427 trace_hfi1_qpwakeup(qp, flag);
1428 hfi1_schedule_send(qp);
1429 }
1430 spin_unlock_irqrestore(&qp->s_lock, flags);
1431 /* Notify hfi1_destroy_qp() if it is waiting. */
1432 if (atomic_dec_and_test(&qp->refcount))
1433 wake_up(&qp->wait);
1434}
1435
1436static int iowait_sleep(
1437 struct sdma_engine *sde,
1438 struct iowait *wait,
1439 struct sdma_txreq *stx,
1440 unsigned seq)
1441{
1442 struct verbs_txreq *tx = container_of(stx, struct verbs_txreq, txreq);
895420dd 1443 struct rvt_qp *qp;
4c6829c5 1444 struct hfi1_qp_priv *priv;
77241056
MM
1445 unsigned long flags;
1446 int ret = 0;
1447 struct hfi1_ibdev *dev;
1448
1449 qp = tx->qp;
4c6829c5 1450 priv = qp->priv;
77241056
MM
1451
1452 spin_lock_irqsave(&qp->s_lock, flags);
1453 if (ib_hfi1_state_ops[qp->state] & HFI1_PROCESS_RECV_OK) {
1454
1455 /*
1456 * If we couldn't queue the DMA request, save the info
1457 * and try again later rather than destroying the
1458 * buffer and undoing the side effects of the copy.
1459 */
1460 /* Make a common routine? */
1461 dev = &sde->dd->verbs_dev;
1462 list_add_tail(&stx->list, &wait->tx_head);
1463 write_seqlock(&dev->iowait_lock);
1464 if (sdma_progress(sde, seq, stx))
1465 goto eagain;
4c6829c5 1466 if (list_empty(&priv->s_iowait.list)) {
77241056
MM
1467 struct hfi1_ibport *ibp =
1468 to_iport(qp->ibqp.device, qp->port_num);
1469
1470 ibp->n_dmawait++;
1471 qp->s_flags |= HFI1_S_WAIT_DMA_DESC;
4c6829c5 1472 list_add_tail(&priv->s_iowait.list, &sde->dmawait);
77241056
MM
1473 trace_hfi1_qpsleep(qp, HFI1_S_WAIT_DMA_DESC);
1474 atomic_inc(&qp->refcount);
1475 }
1476 write_sequnlock(&dev->iowait_lock);
1477 qp->s_flags &= ~HFI1_S_BUSY;
1478 spin_unlock_irqrestore(&qp->s_lock, flags);
1479 ret = -EBUSY;
1480 } else {
1481 spin_unlock_irqrestore(&qp->s_lock, flags);
1482 hfi1_put_txreq(tx);
1483 }
1484 return ret;
1485eagain:
1486 write_sequnlock(&dev->iowait_lock);
1487 spin_unlock_irqrestore(&qp->s_lock, flags);
1488 list_del_init(&stx->list);
1489 return -EAGAIN;
1490}
1491
1492static void iowait_wakeup(struct iowait *wait, int reason)
1493{
895420dd 1494 struct rvt_qp *qp = iowait_to_qp(wait);
77241056
MM
1495
1496 WARN_ON(reason != SDMA_AVAIL_REASON);
1497 hfi1_qp_wakeup(qp, HFI1_S_WAIT_DMA_DESC);
1498}
1499
1500int hfi1_qp_init(struct hfi1_ibdev *dev)
1501{
1502 struct hfi1_devdata *dd = dd_from_dev(dev);
1503 int i;
1504 int ret = -ENOMEM;
1505
1506 /* allocate parent object */
1507 dev->qp_dev = kzalloc(sizeof(*dev->qp_dev), GFP_KERNEL);
1508 if (!dev->qp_dev)
1509 goto nomem;
1510 /* allocate hash table */
1511 dev->qp_dev->qp_table_size = hfi1_qp_table_size;
1512 dev->qp_dev->qp_table_bits = ilog2(hfi1_qp_table_size);
1513 dev->qp_dev->qp_table =
1514 kmalloc(dev->qp_dev->qp_table_size *
1515 sizeof(*dev->qp_dev->qp_table),
1516 GFP_KERNEL);
1517 if (!dev->qp_dev->qp_table)
1518 goto nomem;
1519 for (i = 0; i < dev->qp_dev->qp_table_size; i++)
1520 RCU_INIT_POINTER(dev->qp_dev->qp_table[i], NULL);
1521 spin_lock_init(&dev->qp_dev->qpt_lock);
1522 /* initialize qpn map */
1523 ret = init_qpn_table(dd, &dev->qp_dev->qpn_table);
1524 if (ret)
1525 goto nomem;
1526 return ret;
1527nomem:
1528 if (dev->qp_dev) {
1529 kfree(dev->qp_dev->qp_table);
1530 free_qpn_table(&dev->qp_dev->qpn_table);
1531 kfree(dev->qp_dev);
1532 }
1533 return ret;
1534}
1535
1536void hfi1_qp_exit(struct hfi1_ibdev *dev)
1537{
1538 struct hfi1_devdata *dd = dd_from_dev(dev);
1539 u32 qps_inuse;
1540
1541 qps_inuse = free_all_qps(dd);
1542 if (qps_inuse)
1543 dd_dev_err(dd, "QP memory leak! %u still in use\n",
1544 qps_inuse);
1545 if (dev->qp_dev) {
1546 kfree(dev->qp_dev->qp_table);
1547 free_qpn_table(&dev->qp_dev->qpn_table);
1548 kfree(dev->qp_dev);
1549 }
1550}
1551
1552/**
1553 *
1554 * qp_to_sdma_engine - map a qp to a send engine
1555 * @qp: the QP
1556 * @sc5: the 5 bit sc
1557 *
1558 * Return:
1559 * A send engine for the qp or NULL for SMI type qp.
1560 */
895420dd 1561struct sdma_engine *qp_to_sdma_engine(struct rvt_qp *qp, u8 sc5)
77241056
MM
1562{
1563 struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
1564 struct sdma_engine *sde;
1565
1566 if (!(dd->flags & HFI1_HAS_SEND_DMA))
1567 return NULL;
1568 switch (qp->ibqp.qp_type) {
77241056
MM
1569 case IB_QPT_SMI:
1570 return NULL;
1571 default:
1572 break;
1573 }
1574 sde = sdma_select_engine_sc(dd, qp->ibqp.qp_num >> dd->qos_shift, sc5);
1575 return sde;
1576}
1577
1578struct qp_iter {
1579 struct hfi1_ibdev *dev;
895420dd 1580 struct rvt_qp *qp;
77241056
MM
1581 int specials;
1582 int n;
1583};
1584
1585struct qp_iter *qp_iter_init(struct hfi1_ibdev *dev)
1586{
1587 struct qp_iter *iter;
1588
1589 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1590 if (!iter)
1591 return NULL;
1592
1593 iter->dev = dev;
ec3f2c12 1594 iter->specials = dev->rdi.ibdev.phys_port_cnt * 2;
77241056
MM
1595 if (qp_iter_next(iter)) {
1596 kfree(iter);
1597 return NULL;
1598 }
1599
1600 return iter;
1601}
1602
1603int qp_iter_next(struct qp_iter *iter)
1604{
1605 struct hfi1_ibdev *dev = iter->dev;
1606 int n = iter->n;
1607 int ret = 1;
895420dd
DD
1608 struct rvt_qp *pqp = iter->qp;
1609 struct rvt_qp *qp;
77241056
MM
1610
1611 /*
1612 * The approach is to consider the special qps
1613 * as an additional table entries before the
1614 * real hash table. Since the qp code sets
1615 * the qp->next hash link to NULL, this works just fine.
1616 *
1617 * iter->specials is 2 * # ports
1618 *
1619 * n = 0..iter->specials is the special qp indices
1620 *
1621 * n = iter->specials..dev->qp_dev->qp_table_size+iter->specials are
1622 * the potential hash bucket entries
1623 *
1624 */
1625 for (; n < dev->qp_dev->qp_table_size + iter->specials; n++) {
1626 if (pqp) {
1627 qp = rcu_dereference(pqp->next);
1628 } else {
1629 if (n < iter->specials) {
1630 struct hfi1_pportdata *ppd;
1631 struct hfi1_ibport *ibp;
1632 int pidx;
1633
ec3f2c12 1634 pidx = n % dev->rdi.ibdev.phys_port_cnt;
77241056
MM
1635 ppd = &dd_from_dev(dev)->pport[pidx];
1636 ibp = &ppd->ibport_data;
1637
1638 if (!(n & 1))
1639 qp = rcu_dereference(ibp->qp[0]);
1640 else
1641 qp = rcu_dereference(ibp->qp[1]);
1642 } else {
1643 qp = rcu_dereference(
1644 dev->qp_dev->qp_table[
1645 (n - iter->specials)]);
1646 }
1647 }
1648 pqp = qp;
1649 if (qp) {
1650 iter->qp = qp;
1651 iter->n = n;
1652 return 0;
1653 }
1654 }
1655 return ret;
1656}
1657
1658static const char * const qp_type_str[] = {
1659 "SMI", "GSI", "RC", "UC", "UD",
1660};
1661
895420dd 1662static int qp_idle(struct rvt_qp *qp)
77241056
MM
1663{
1664 return
1665 qp->s_last == qp->s_acked &&
1666 qp->s_acked == qp->s_cur &&
1667 qp->s_cur == qp->s_tail &&
1668 qp->s_tail == qp->s_head;
1669}
1670
1671void qp_iter_print(struct seq_file *s, struct qp_iter *iter)
1672{
895420dd
DD
1673 struct rvt_swqe *wqe;
1674 struct rvt_qp *qp = iter->qp;
4c6829c5 1675 struct hfi1_qp_priv *priv = qp->priv;
77241056
MM
1676 struct sdma_engine *sde;
1677
4c6829c5 1678 sde = qp_to_sdma_engine(qp, priv->s_sc);
77241056
MM
1679 wqe = get_swqe_ptr(qp, qp->s_last);
1680 seq_printf(s,
1681 "N %d %s QP%u R %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 SL %u MTU %d %u %u %u SDE %p,%u\n",
1682 iter->n,
1683 qp_idle(qp) ? "I" : "B",
1684 qp->ibqp.qp_num,
1685 atomic_read(&qp->refcount),
1686 qp_type_str[qp->ibqp.qp_type],
1687 qp->state,
1688 wqe ? wqe->wr.opcode : 0,
1689 qp->s_hdrwords,
1690 qp->s_flags,
4c6829c5
DD
1691 atomic_read(&priv->s_iowait.sdma_busy),
1692 !list_empty(&priv->s_iowait.list),
77241056
MM
1693 qp->timeout,
1694 wqe ? wqe->ssn : 0,
1695 qp->s_lsn,
1696 qp->s_last_psn,
1697 qp->s_psn, qp->s_next_psn,
1698 qp->s_sending_psn, qp->s_sending_hpsn,
1699 qp->s_last, qp->s_acked, qp->s_cur,
1700 qp->s_tail, qp->s_head, qp->s_size,
1701 qp->remote_qpn,
1702 qp->remote_ah_attr.dlid,
1703 qp->remote_ah_attr.sl,
1704 qp->pmtu,
1705 qp->s_retry_cnt,
1706 qp->timeout,
1707 qp->s_rnr_retry_cnt,
1708 sde,
1709 sde ? sde->this_idx : 0);
1710}
1711
895420dd 1712void qp_comm_est(struct rvt_qp *qp)
77241056
MM
1713{
1714 qp->r_flags |= HFI1_R_COMM_EST;
1715 if (qp->ibqp.event_handler) {
1716 struct ib_event ev;
1717
1718 ev.device = qp->ibqp.device;
1719 ev.element.qp = &qp->ibqp;
1720 ev.event = IB_EVENT_COMM_EST;
1721 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1722 }
1723}
c2f3ffb0
MM
1724
1725/*
1726 * Switch to alternate path.
1727 * The QP s_lock should be held and interrupts disabled.
1728 */
895420dd 1729void hfi1_migrate_qp(struct rvt_qp *qp)
c2f3ffb0 1730{
4c6829c5 1731 struct hfi1_qp_priv *priv = qp->priv;
c2f3ffb0
MM
1732 struct ib_event ev;
1733
1734 qp->s_mig_state = IB_MIG_MIGRATED;
1735 qp->remote_ah_attr = qp->alt_ah_attr;
1736 qp->port_num = qp->alt_ah_attr.port_num;
1737 qp->s_pkey_index = qp->s_alt_pkey_index;
1738 qp->s_flags |= HFI1_S_AHG_CLEAR;
4c6829c5
DD
1739 priv->s_sc = ah_to_sc(qp->ibqp.device, &qp->remote_ah_attr);
1740 priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
c2f3ffb0
MM
1741
1742 ev.device = qp->ibqp.device;
1743 ev.element.qp = &qp->ibqp;
1744 ev.event = IB_EVENT_PATH_MIG;
1745 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
1746}
This page took 0.137127 seconds and 5 git commands to generate.