463ef5813a5957a84a0861866db5655302884f46
[platform/kernel/linux-starfive.git] / drivers / infiniband / hw / cxgb4 / restrack.c
1 /*
2  * Copyright (c) 2018 Chelsio, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <rdma/rdma_cm.h>
34
35 #include "iw_cxgb4.h"
36 #include <rdma/restrack.h>
37 #include <uapi/rdma/rdma_netlink.h>
38
39 static int fill_sq(struct sk_buff *msg, struct t4_wq *wq)
40 {
41         /* WQ+SQ */
42         if (rdma_nl_put_driver_u32(msg, "sqid", wq->sq.qid))
43                 goto err;
44         if (rdma_nl_put_driver_u32(msg, "flushed", wq->flushed))
45                 goto err;
46         if (rdma_nl_put_driver_u32(msg, "memsize", wq->sq.memsize))
47                 goto err;
48         if (rdma_nl_put_driver_u32(msg, "cidx", wq->sq.cidx))
49                 goto err;
50         if (rdma_nl_put_driver_u32(msg, "pidx", wq->sq.pidx))
51                 goto err;
52         if (rdma_nl_put_driver_u32(msg, "wq_pidx", wq->sq.wq_pidx))
53                 goto err;
54         if (rdma_nl_put_driver_u32(msg, "flush_cidx", wq->sq.flush_cidx))
55                 goto err;
56         if (rdma_nl_put_driver_u32(msg, "in_use", wq->sq.in_use))
57                 goto err;
58         if (rdma_nl_put_driver_u32(msg, "size", wq->sq.size))
59                 goto err;
60         if (rdma_nl_put_driver_u32_hex(msg, "flags", wq->sq.flags))
61                 goto err;
62         return 0;
63 err:
64         return -EMSGSIZE;
65 }
66
67 static int fill_rq(struct sk_buff *msg, struct t4_wq *wq)
68 {
69         /* RQ */
70         if (rdma_nl_put_driver_u32(msg, "rqid", wq->rq.qid))
71                 goto err;
72         if (rdma_nl_put_driver_u32(msg, "memsize", wq->rq.memsize))
73                 goto err;
74         if (rdma_nl_put_driver_u32(msg, "cidx", wq->rq.cidx))
75                 goto err;
76         if (rdma_nl_put_driver_u32(msg, "pidx", wq->rq.pidx))
77                 goto err;
78         if (rdma_nl_put_driver_u32(msg, "wq_pidx", wq->rq.wq_pidx))
79                 goto err;
80         if (rdma_nl_put_driver_u32(msg, "msn", wq->rq.msn))
81                 goto err;
82         if (rdma_nl_put_driver_u32_hex(msg, "rqt_hwaddr", wq->rq.rqt_hwaddr))
83                 goto err;
84         if (rdma_nl_put_driver_u32(msg, "rqt_size", wq->rq.rqt_size))
85                 goto err;
86         if (rdma_nl_put_driver_u32(msg, "in_use", wq->rq.in_use))
87                 goto err;
88         if (rdma_nl_put_driver_u32(msg, "size", wq->rq.size))
89                 goto err;
90         return 0;
91 err:
92         return -EMSGSIZE;
93 }
94
95 static int fill_swsqe(struct sk_buff *msg, struct t4_sq *sq, u16 idx,
96                       struct t4_swsqe *sqe)
97 {
98         if (rdma_nl_put_driver_u32(msg, "idx", idx))
99                 goto err;
100         if (rdma_nl_put_driver_u32(msg, "opcode", sqe->opcode))
101                 goto err;
102         if (rdma_nl_put_driver_u32(msg, "complete", sqe->complete))
103                 goto err;
104         if (sqe->complete &&
105             rdma_nl_put_driver_u32(msg, "cqe_status", CQE_STATUS(&sqe->cqe)))
106                 goto err;
107         if (rdma_nl_put_driver_u32(msg, "signaled", sqe->signaled))
108                 goto err;
109         if (rdma_nl_put_driver_u32(msg, "flushed", sqe->flushed))
110                 goto err;
111         return 0;
112 err:
113         return -EMSGSIZE;
114 }
115
116 /*
117  * Dump the first and last pending sqes.
118  */
119 static int fill_swsqes(struct sk_buff *msg, struct t4_sq *sq,
120                        u16 first_idx, struct t4_swsqe *first_sqe,
121                        u16 last_idx, struct t4_swsqe *last_sqe)
122 {
123         if (!first_sqe)
124                 goto out;
125         if (fill_swsqe(msg, sq, first_idx, first_sqe))
126                 goto err;
127         if (!last_sqe)
128                 goto out;
129         if (fill_swsqe(msg, sq, last_idx, last_sqe))
130                 goto err;
131 out:
132         return 0;
133 err:
134         return -EMSGSIZE;
135 }
136
137 static int fill_res_qp_entry(struct sk_buff *msg,
138                              struct rdma_restrack_entry *res)
139 {
140         struct ib_qp *ibqp = container_of(res, struct ib_qp, res);
141         struct t4_swsqe *fsp = NULL, *lsp = NULL;
142         struct c4iw_qp *qhp = to_c4iw_qp(ibqp);
143         u16 first_sq_idx = 0, last_sq_idx = 0;
144         struct t4_swsqe first_sqe, last_sqe;
145         struct nlattr *table_attr;
146         struct t4_wq wq;
147
148         /* User qp state is not available, so don't dump user qps */
149         if (qhp->ucontext)
150                 return 0;
151
152         table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
153         if (!table_attr)
154                 goto err;
155
156         /* Get a consistent snapshot */
157         spin_lock_irq(&qhp->lock);
158         wq = qhp->wq;
159
160         /* If there are any pending sqes, copy the first and last */
161         if (wq.sq.cidx != wq.sq.pidx) {
162                 first_sq_idx = wq.sq.cidx;
163                 first_sqe = qhp->wq.sq.sw_sq[first_sq_idx];
164                 fsp = &first_sqe;
165                 last_sq_idx = wq.sq.pidx;
166                 if (last_sq_idx-- == 0)
167                         last_sq_idx = wq.sq.size - 1;
168                 if (last_sq_idx != first_sq_idx) {
169                         last_sqe = qhp->wq.sq.sw_sq[last_sq_idx];
170                         lsp = &last_sqe;
171                 }
172         }
173         spin_unlock_irq(&qhp->lock);
174
175         if (fill_sq(msg, &wq))
176                 goto err_cancel_table;
177
178         if (fill_swsqes(msg, &wq.sq, first_sq_idx, fsp, last_sq_idx, lsp))
179                 goto err_cancel_table;
180
181         if (fill_rq(msg, &wq))
182                 goto err_cancel_table;
183
184         nla_nest_end(msg, table_attr);
185         return 0;
186
187 err_cancel_table:
188         nla_nest_cancel(msg, table_attr);
189 err:
190         return -EMSGSIZE;
191 }
192
193 union union_ep {
194         struct c4iw_listen_ep lep;
195         struct c4iw_ep ep;
196 };
197
198 static int fill_res_ep_entry(struct sk_buff *msg,
199                              struct rdma_restrack_entry *res)
200 {
201         struct rdma_cm_id *cm_id = rdma_res_to_id(res);
202         struct nlattr *table_attr;
203         struct c4iw_ep_common *epcp;
204         struct c4iw_listen_ep *listen_ep = NULL;
205         struct c4iw_ep *ep = NULL;
206         struct iw_cm_id *iw_cm_id;
207         union union_ep *uep;
208
209         iw_cm_id = rdma_iw_cm_id(cm_id);
210         if (!iw_cm_id)
211                 return 0;
212         epcp = (struct c4iw_ep_common *)iw_cm_id->provider_data;
213         if (!epcp)
214                 return 0;
215         uep = kcalloc(1, sizeof(*uep), GFP_KERNEL);
216         if (!uep)
217                 return 0;
218
219         table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
220         if (!table_attr)
221                 goto err_free_uep;
222
223         /* Get a consistent snapshot */
224         mutex_lock(&epcp->mutex);
225         if (epcp->state == LISTEN) {
226                 uep->lep = *(struct c4iw_listen_ep *)epcp;
227                 mutex_unlock(&epcp->mutex);
228                 listen_ep = &uep->lep;
229                 epcp = &listen_ep->com;
230         } else {
231                 uep->ep = *(struct c4iw_ep *)epcp;
232                 mutex_unlock(&epcp->mutex);
233                 ep = &uep->ep;
234                 epcp = &ep->com;
235         }
236
237         if (rdma_nl_put_driver_u32(msg, "state", epcp->state))
238                 goto err_cancel_table;
239         if (rdma_nl_put_driver_u64_hex(msg, "flags", epcp->flags))
240                 goto err_cancel_table;
241         if (rdma_nl_put_driver_u64_hex(msg, "history", epcp->history))
242                 goto err_cancel_table;
243
244         if (epcp->state == LISTEN) {
245                 if (rdma_nl_put_driver_u32(msg, "stid", listen_ep->stid))
246                         goto err_cancel_table;
247                 if (rdma_nl_put_driver_u32(msg, "backlog", listen_ep->backlog))
248                         goto err_cancel_table;
249         } else {
250                 if (rdma_nl_put_driver_u32(msg, "hwtid", ep->hwtid))
251                         goto err_cancel_table;
252                 if (rdma_nl_put_driver_u32(msg, "ord", ep->ord))
253                         goto err_cancel_table;
254                 if (rdma_nl_put_driver_u32(msg, "ird", ep->ird))
255                         goto err_cancel_table;
256                 if (rdma_nl_put_driver_u32(msg, "emss", ep->emss))
257                         goto err_cancel_table;
258
259                 if (!ep->parent_ep && rdma_nl_put_driver_u32(msg, "atid",
260                                                              ep->atid))
261                         goto err_cancel_table;
262         }
263         nla_nest_end(msg, table_attr);
264         kfree(uep);
265         return 0;
266
267 err_cancel_table:
268         nla_nest_cancel(msg, table_attr);
269 err_free_uep:
270         kfree(uep);
271         return -EMSGSIZE;
272 }
273
274 c4iw_restrack_func *c4iw_restrack_funcs[RDMA_RESTRACK_MAX] = {
275         [RDMA_RESTRACK_QP]      = fill_res_qp_entry,
276         [RDMA_RESTRACK_CM_ID]   = fill_res_ep_entry,
277 };