3ecbf06b4d7177b51ad9c36d037aba3b39e9c6ea
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / diag / fw_tracer.c
1 /*
2  * Copyright (c) 2018, Mellanox Technologies. 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 "fw_tracer.h"
34
35 static int mlx5_query_mtrc_caps(struct mlx5_fw_tracer *tracer)
36 {
37         u32 *string_db_base_address_out = tracer->str_db.base_address_out;
38         u32 *string_db_size_out = tracer->str_db.size_out;
39         struct mlx5_core_dev *dev = tracer->dev;
40         u32 out[MLX5_ST_SZ_DW(mtrc_cap)] = {0};
41         u32 in[MLX5_ST_SZ_DW(mtrc_cap)] = {0};
42         void *mtrc_cap_sp;
43         int err, i;
44
45         err = mlx5_core_access_reg(dev, in, sizeof(in), out, sizeof(out),
46                                    MLX5_REG_MTRC_CAP, 0, 0);
47         if (err) {
48                 mlx5_core_warn(dev, "FWTracer: Error reading tracer caps %d\n",
49                                err);
50                 return err;
51         }
52
53         if (!MLX5_GET(mtrc_cap, out, trace_to_memory)) {
54                 mlx5_core_dbg(dev, "FWTracer: Device does not support logging traces to memory\n");
55                 return -ENOTSUPP;
56         }
57
58         tracer->trc_ver = MLX5_GET(mtrc_cap, out, trc_ver);
59         tracer->str_db.first_string_trace =
60                         MLX5_GET(mtrc_cap, out, first_string_trace);
61         tracer->str_db.num_string_trace =
62                         MLX5_GET(mtrc_cap, out, num_string_trace);
63         tracer->str_db.num_string_db = MLX5_GET(mtrc_cap, out, num_string_db);
64         tracer->owner = !!MLX5_GET(mtrc_cap, out, trace_owner);
65
66         for (i = 0; i < tracer->str_db.num_string_db; i++) {
67                 mtrc_cap_sp = MLX5_ADDR_OF(mtrc_cap, out, string_db_param[i]);
68                 string_db_base_address_out[i] = MLX5_GET(mtrc_string_db_param,
69                                                          mtrc_cap_sp,
70                                                          string_db_base_address);
71                 string_db_size_out[i] = MLX5_GET(mtrc_string_db_param,
72                                                  mtrc_cap_sp, string_db_size);
73         }
74
75         return err;
76 }
77
78 static int mlx5_set_mtrc_caps_trace_owner(struct mlx5_fw_tracer *tracer,
79                                           u32 *out, u32 out_size,
80                                           u8 trace_owner)
81 {
82         struct mlx5_core_dev *dev = tracer->dev;
83         u32 in[MLX5_ST_SZ_DW(mtrc_cap)] = {0};
84
85         MLX5_SET(mtrc_cap, in, trace_owner, trace_owner);
86
87         return mlx5_core_access_reg(dev, in, sizeof(in), out, out_size,
88                                     MLX5_REG_MTRC_CAP, 0, 1);
89 }
90
91 static int mlx5_fw_tracer_ownership_acquire(struct mlx5_fw_tracer *tracer)
92 {
93         struct mlx5_core_dev *dev = tracer->dev;
94         u32 out[MLX5_ST_SZ_DW(mtrc_cap)] = {0};
95         int err;
96
97         err = mlx5_set_mtrc_caps_trace_owner(tracer, out, sizeof(out),
98                                              MLX5_FW_TRACER_ACQUIRE_OWNERSHIP);
99         if (err) {
100                 mlx5_core_warn(dev, "FWTracer: Acquire tracer ownership failed %d\n",
101                                err);
102                 return err;
103         }
104
105         tracer->owner = !!MLX5_GET(mtrc_cap, out, trace_owner);
106
107         if (!tracer->owner)
108                 return -EBUSY;
109
110         return 0;
111 }
112
113 static void mlx5_fw_tracer_ownership_release(struct mlx5_fw_tracer *tracer)
114 {
115         u32 out[MLX5_ST_SZ_DW(mtrc_cap)] = {0};
116
117         mlx5_set_mtrc_caps_trace_owner(tracer, out, sizeof(out),
118                                        MLX5_FW_TRACER_RELEASE_OWNERSHIP);
119         tracer->owner = false;
120 }
121
122 static void mlx5_fw_tracer_ownership_change(struct work_struct *work)
123 {
124         struct mlx5_fw_tracer *tracer = container_of(work, struct mlx5_fw_tracer,
125                                                      ownership_change_work);
126         struct mlx5_core_dev *dev = tracer->dev;
127         int err;
128
129         if (tracer->owner) {
130                 mlx5_fw_tracer_ownership_release(tracer);
131                 return;
132         }
133
134         err = mlx5_fw_tracer_ownership_acquire(tracer);
135         if (err) {
136                 mlx5_core_dbg(dev, "FWTracer: Ownership was not granted %d\n", err);
137                 return;
138         }
139 }
140
141 struct mlx5_fw_tracer *mlx5_fw_tracer_create(struct mlx5_core_dev *dev)
142 {
143         struct mlx5_fw_tracer *tracer = NULL;
144         int err;
145
146         if (!MLX5_CAP_MCAM_REG(dev, tracer_registers)) {
147                 mlx5_core_dbg(dev, "FWTracer: Tracer capability not present\n");
148                 return NULL;
149         }
150
151         tracer = kzalloc(sizeof(*tracer), GFP_KERNEL);
152         if (!tracer)
153                 return ERR_PTR(-ENOMEM);
154
155         tracer->work_queue = create_singlethread_workqueue("mlx5_fw_tracer");
156         if (!tracer->work_queue) {
157                 err = -ENOMEM;
158                 goto free_tracer;
159         }
160
161         tracer->dev = dev;
162
163         INIT_WORK(&tracer->ownership_change_work, mlx5_fw_tracer_ownership_change);
164
165         err = mlx5_query_mtrc_caps(tracer);
166         if (err) {
167                 mlx5_core_dbg(dev, "FWTracer: Failed to query capabilities %d\n", err);
168                 goto destroy_workqueue;
169         }
170
171         mlx5_fw_tracer_ownership_change(&tracer->ownership_change_work);
172
173         return tracer;
174
175 destroy_workqueue:
176         tracer->dev = NULL;
177         destroy_workqueue(tracer->work_queue);
178 free_tracer:
179         kfree(tracer);
180         return ERR_PTR(err);
181 }
182
183 void mlx5_fw_tracer_destroy(struct mlx5_fw_tracer *tracer)
184 {
185         if (!tracer)
186                 return;
187
188         cancel_work_sync(&tracer->ownership_change_work);
189
190         if (tracer->owner)
191                 mlx5_fw_tracer_ownership_release(tracer);
192
193         flush_workqueue(tracer->work_queue);
194         destroy_workqueue(tracer->work_queue);
195         kfree(tracer);
196 }