These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / staging / rdma / hfi1 / qp.h
1 #ifndef _QP_H
2 #define _QP_H
3 /*
4  *
5  * This file is provided under a dual BSD/GPLv2 license.  When using or
6  * redistributing this file, you may do so under either license.
7  *
8  * GPL LICENSE SUMMARY
9  *
10  * Copyright(c) 2015 Intel Corporation.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * BSD LICENSE
22  *
23  * Copyright(c) 2015 Intel Corporation.
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  *
29  *  - Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  *  - Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in
33  *    the documentation and/or other materials provided with the
34  *    distribution.
35  *  - Neither the name of Intel Corporation nor the names of its
36  *    contributors may be used to endorse or promote products derived
37  *    from this software without specific prior written permission.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
40  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
41  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
42  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
43  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
46  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
47  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
48  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  *
51  */
52
53 #include <linux/hash.h>
54 #include "verbs.h"
55 #include "sdma.h"
56
57 #define QPN_MAX                 (1 << 24)
58 #define QPNMAP_ENTRIES          (QPN_MAX / PAGE_SIZE / BITS_PER_BYTE)
59
60 /*
61  * QPN-map pages start out as NULL, they get allocated upon
62  * first use and are never deallocated. This way,
63  * large bitmaps are not allocated unless large numbers of QPs are used.
64  */
65 struct qpn_map {
66         void *page;
67 };
68
69 struct hfi1_qpn_table {
70         spinlock_t lock; /* protect changes in this struct */
71         unsigned flags;         /* flags for QP0/1 allocated for each port */
72         u32 last;               /* last QP number allocated */
73         u32 nmaps;              /* size of the map table */
74         u16 limit;
75         u8  incr;
76         /* bit map of free QP numbers other than 0/1 */
77         struct qpn_map map[QPNMAP_ENTRIES];
78 };
79
80 struct hfi1_qp_ibdev {
81         u32 qp_table_size;
82         u32 qp_table_bits;
83         struct hfi1_qp __rcu **qp_table;
84         spinlock_t qpt_lock;
85         struct hfi1_qpn_table qpn_table;
86 };
87
88 static inline u32 qpn_hash(struct hfi1_qp_ibdev *dev, u32 qpn)
89 {
90         return hash_32(qpn, dev->qp_table_bits);
91 }
92
93 /**
94  * hfi1_lookup_qpn - return the QP with the given QPN
95  * @ibp: the ibport
96  * @qpn: the QP number to look up
97  *
98  * The caller must hold the rcu_read_lock(), and keep the lock until
99  * the returned qp is no longer in use.
100  */
101 static inline struct hfi1_qp *hfi1_lookup_qpn(struct hfi1_ibport *ibp,
102                                 u32 qpn) __must_hold(RCU)
103 {
104         struct hfi1_qp *qp = NULL;
105
106         if (unlikely(qpn <= 1)) {
107                 qp = rcu_dereference(ibp->qp[qpn]);
108         } else {
109                 struct hfi1_ibdev *dev = &ppd_from_ibp(ibp)->dd->verbs_dev;
110                 u32 n = qpn_hash(dev->qp_dev, qpn);
111
112                 for (qp = rcu_dereference(dev->qp_dev->qp_table[n]); qp;
113                         qp = rcu_dereference(qp->next))
114                         if (qp->ibqp.qp_num == qpn)
115                                 break;
116         }
117         return qp;
118 }
119
120 /**
121  * clear_ahg - reset ahg status in qp
122  * @qp - qp pointer
123  */
124 static inline void clear_ahg(struct hfi1_qp *qp)
125 {
126         qp->s_hdr->ahgcount = 0;
127         qp->s_flags &= ~(HFI1_S_AHG_VALID | HFI1_S_AHG_CLEAR);
128         if (qp->s_sde && qp->s_ahgidx >= 0)
129                 sdma_ahg_free(qp->s_sde, qp->s_ahgidx);
130         qp->s_ahgidx = -1;
131         qp->s_sde = NULL;
132 }
133
134 /**
135  * hfi1_error_qp - put a QP into the error state
136  * @qp: the QP to put into the error state
137  * @err: the receive completion error to signal if a RWQE is active
138  *
139  * Flushes both send and receive work queues.
140  * Returns true if last WQE event should be generated.
141  * The QP r_lock and s_lock should be held and interrupts disabled.
142  * If we are already in error state, just return.
143  */
144 int hfi1_error_qp(struct hfi1_qp *qp, enum ib_wc_status err);
145
146 /**
147  * hfi1_modify_qp - modify the attributes of a queue pair
148  * @ibqp: the queue pair who's attributes we're modifying
149  * @attr: the new attributes
150  * @attr_mask: the mask of attributes to modify
151  * @udata: user data for libibverbs.so
152  *
153  * Returns 0 on success, otherwise returns an errno.
154  */
155 int hfi1_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
156                    int attr_mask, struct ib_udata *udata);
157
158 int hfi1_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
159                   int attr_mask, struct ib_qp_init_attr *init_attr);
160
161 /**
162  * hfi1_compute_aeth - compute the AETH (syndrome + MSN)
163  * @qp: the queue pair to compute the AETH for
164  *
165  * Returns the AETH.
166  */
167 __be32 hfi1_compute_aeth(struct hfi1_qp *qp);
168
169 /**
170  * hfi1_create_qp - create a queue pair for a device
171  * @ibpd: the protection domain who's device we create the queue pair for
172  * @init_attr: the attributes of the queue pair
173  * @udata: user data for libibverbs.so
174  *
175  * Returns the queue pair on success, otherwise returns an errno.
176  *
177  * Called by the ib_create_qp() core verbs function.
178  */
179 struct ib_qp *hfi1_create_qp(struct ib_pd *ibpd,
180                              struct ib_qp_init_attr *init_attr,
181                              struct ib_udata *udata);
182 /**
183  * hfi1_destroy_qp - destroy a queue pair
184  * @ibqp: the queue pair to destroy
185  *
186  * Returns 0 on success.
187  *
188  * Note that this can be called while the QP is actively sending or
189  * receiving!
190  */
191 int hfi1_destroy_qp(struct ib_qp *ibqp);
192
193 /**
194  * hfi1_get_credit - flush the send work queue of a QP
195  * @qp: the qp who's send work queue to flush
196  * @aeth: the Acknowledge Extended Transport Header
197  *
198  * The QP s_lock should be held.
199  */
200 void hfi1_get_credit(struct hfi1_qp *qp, u32 aeth);
201
202 /**
203  * hfi1_qp_init - allocate QP tables
204  * @dev: a pointer to the hfi1_ibdev
205  */
206 int hfi1_qp_init(struct hfi1_ibdev *dev);
207
208 /**
209  * hfi1_qp_exit - free the QP related structures
210  * @dev: a pointer to the hfi1_ibdev
211  */
212 void hfi1_qp_exit(struct hfi1_ibdev *dev);
213
214 /**
215  * hfi1_qp_waitup - wake up on the indicated event
216  * @qp: the QP
217  * @flag: flag the qp on which the qp is stalled
218  */
219 void hfi1_qp_wakeup(struct hfi1_qp *qp, u32 flag);
220
221 struct sdma_engine *qp_to_sdma_engine(struct hfi1_qp *qp, u8 sc5);
222
223 struct qp_iter;
224
225 /**
226  * qp_iter_init - wake up on the indicated event
227  * @dev: the hfi1_ibdev
228  */
229 struct qp_iter *qp_iter_init(struct hfi1_ibdev *dev);
230
231 /**
232  * qp_iter_next - wakeup on the indicated event
233  * @iter: the iterator for the qp hash list
234  */
235 int qp_iter_next(struct qp_iter *iter);
236
237 /**
238  * qp_iter_next - wake up on the indicated event
239  * @s: the seq_file to emit the qp information on
240  * @iter: the iterator for the qp hash list
241  */
242 void qp_iter_print(struct seq_file *s, struct qp_iter *iter);
243
244 /**
245  * qp_comm_est - handle trap with QP established
246  * @qp: the QP
247  */
248 void qp_comm_est(struct hfi1_qp *qp);
249
250 #endif /* _QP_H */