35107b8f76df0a6bec1fe946055f11f2868c647b
[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 int mlx5_fw_tracer_create_log_buf(struct mlx5_fw_tracer *tracer)
123 {
124         struct mlx5_core_dev *dev = tracer->dev;
125         struct device *ddev = &dev->pdev->dev;
126         dma_addr_t dma;
127         void *buff;
128         gfp_t gfp;
129         int err;
130
131         tracer->buff.size = TRACE_BUFFER_SIZE_BYTE;
132
133         gfp = GFP_KERNEL | __GFP_ZERO;
134         buff = (void *)__get_free_pages(gfp,
135                                         get_order(tracer->buff.size));
136         if (!buff) {
137                 err = -ENOMEM;
138                 mlx5_core_warn(dev, "FWTracer: Failed to allocate pages, %d\n", err);
139                 return err;
140         }
141         tracer->buff.log_buf = buff;
142
143         dma = dma_map_single(ddev, buff, tracer->buff.size, DMA_FROM_DEVICE);
144         if (dma_mapping_error(ddev, dma)) {
145                 mlx5_core_warn(dev, "FWTracer: Unable to map DMA: %d\n",
146                                dma_mapping_error(ddev, dma));
147                 err = -ENOMEM;
148                 goto free_pages;
149         }
150         tracer->buff.dma = dma;
151
152         return 0;
153
154 free_pages:
155         free_pages((unsigned long)tracer->buff.log_buf, get_order(tracer->buff.size));
156
157         return err;
158 }
159
160 static void mlx5_fw_tracer_destroy_log_buf(struct mlx5_fw_tracer *tracer)
161 {
162         struct mlx5_core_dev *dev = tracer->dev;
163         struct device *ddev = &dev->pdev->dev;
164
165         if (!tracer->buff.log_buf)
166                 return;
167
168         dma_unmap_single(ddev, tracer->buff.dma, tracer->buff.size, DMA_FROM_DEVICE);
169         free_pages((unsigned long)tracer->buff.log_buf, get_order(tracer->buff.size));
170 }
171
172 static void mlx5_fw_tracer_free_strings_db(struct mlx5_fw_tracer *tracer)
173 {
174         u32 num_string_db = tracer->str_db.num_string_db;
175         int i;
176
177         for (i = 0; i < num_string_db; i++) {
178                 kfree(tracer->str_db.buffer[i]);
179                 tracer->str_db.buffer[i] = NULL;
180         }
181 }
182
183 static int mlx5_fw_tracer_allocate_strings_db(struct mlx5_fw_tracer *tracer)
184 {
185         u32 *string_db_size_out = tracer->str_db.size_out;
186         u32 num_string_db = tracer->str_db.num_string_db;
187         int i;
188
189         for (i = 0; i < num_string_db; i++) {
190                 tracer->str_db.buffer[i] = kzalloc(string_db_size_out[i], GFP_KERNEL);
191                 if (!tracer->str_db.buffer[i])
192                         goto free_strings_db;
193         }
194
195         return 0;
196
197 free_strings_db:
198         mlx5_fw_tracer_free_strings_db(tracer);
199         return -ENOMEM;
200 }
201
202 static void mlx5_tracer_read_strings_db(struct work_struct *work)
203 {
204         struct mlx5_fw_tracer *tracer = container_of(work, struct mlx5_fw_tracer,
205                                                      read_fw_strings_work);
206         u32 num_of_reads, num_string_db = tracer->str_db.num_string_db;
207         struct mlx5_core_dev *dev = tracer->dev;
208         u32 in[MLX5_ST_SZ_DW(mtrc_cap)] = {0};
209         u32 leftovers, offset;
210         int err = 0, i, j;
211         u32 *out, outlen;
212         void *out_value;
213
214         outlen = MLX5_ST_SZ_BYTES(mtrc_stdb) + STRINGS_DB_READ_SIZE_BYTES;
215         out = kzalloc(outlen, GFP_KERNEL);
216         if (!out) {
217                 err = -ENOMEM;
218                 goto out;
219         }
220
221         for (i = 0; i < num_string_db; i++) {
222                 offset = 0;
223                 MLX5_SET(mtrc_stdb, in, string_db_index, i);
224                 num_of_reads = tracer->str_db.size_out[i] /
225                                 STRINGS_DB_READ_SIZE_BYTES;
226                 leftovers = (tracer->str_db.size_out[i] %
227                                 STRINGS_DB_READ_SIZE_BYTES) /
228                                         STRINGS_DB_LEFTOVER_SIZE_BYTES;
229
230                 MLX5_SET(mtrc_stdb, in, read_size, STRINGS_DB_READ_SIZE_BYTES);
231                 for (j = 0; j < num_of_reads; j++) {
232                         MLX5_SET(mtrc_stdb, in, start_offset, offset);
233
234                         err = mlx5_core_access_reg(dev, in, sizeof(in), out,
235                                                    outlen, MLX5_REG_MTRC_STDB,
236                                                    0, 1);
237                         if (err) {
238                                 mlx5_core_dbg(dev, "FWTracer: Failed to read strings DB %d\n",
239                                               err);
240                                 goto out_free;
241                         }
242
243                         out_value = MLX5_ADDR_OF(mtrc_stdb, out, string_db_data);
244                         memcpy(tracer->str_db.buffer[i] + offset, out_value,
245                                STRINGS_DB_READ_SIZE_BYTES);
246                         offset += STRINGS_DB_READ_SIZE_BYTES;
247                 }
248
249                 /* Strings database is aligned to 64, need to read leftovers*/
250                 MLX5_SET(mtrc_stdb, in, read_size,
251                          STRINGS_DB_LEFTOVER_SIZE_BYTES);
252                 for (j = 0; j < leftovers; j++) {
253                         MLX5_SET(mtrc_stdb, in, start_offset, offset);
254
255                         err = mlx5_core_access_reg(dev, in, sizeof(in), out,
256                                                    outlen, MLX5_REG_MTRC_STDB,
257                                                    0, 1);
258                         if (err) {
259                                 mlx5_core_dbg(dev, "FWTracer: Failed to read strings DB %d\n",
260                                               err);
261                                 goto out_free;
262                         }
263
264                         out_value = MLX5_ADDR_OF(mtrc_stdb, out, string_db_data);
265                         memcpy(tracer->str_db.buffer[i] + offset, out_value,
266                                STRINGS_DB_LEFTOVER_SIZE_BYTES);
267                         offset += STRINGS_DB_LEFTOVER_SIZE_BYTES;
268                 }
269         }
270
271         tracer->str_db.loaded = true;
272
273 out_free:
274         kfree(out);
275 out:
276         return;
277 }
278
279 static void mlx5_fw_tracer_ownership_change(struct work_struct *work)
280 {
281         struct mlx5_fw_tracer *tracer = container_of(work, struct mlx5_fw_tracer,
282                                                      ownership_change_work);
283         struct mlx5_core_dev *dev = tracer->dev;
284         int err;
285
286         if (tracer->owner) {
287                 mlx5_fw_tracer_ownership_release(tracer);
288                 return;
289         }
290
291         err = mlx5_fw_tracer_ownership_acquire(tracer);
292         if (err) {
293                 mlx5_core_dbg(dev, "FWTracer: Ownership was not granted %d\n", err);
294                 return;
295         }
296 }
297
298 struct mlx5_fw_tracer *mlx5_fw_tracer_create(struct mlx5_core_dev *dev)
299 {
300         struct mlx5_fw_tracer *tracer = NULL;
301         int err;
302
303         if (!MLX5_CAP_MCAM_REG(dev, tracer_registers)) {
304                 mlx5_core_dbg(dev, "FWTracer: Tracer capability not present\n");
305                 return NULL;
306         }
307
308         tracer = kzalloc(sizeof(*tracer), GFP_KERNEL);
309         if (!tracer)
310                 return ERR_PTR(-ENOMEM);
311
312         tracer->work_queue = create_singlethread_workqueue("mlx5_fw_tracer");
313         if (!tracer->work_queue) {
314                 err = -ENOMEM;
315                 goto free_tracer;
316         }
317
318         tracer->dev = dev;
319
320         INIT_WORK(&tracer->ownership_change_work, mlx5_fw_tracer_ownership_change);
321         INIT_WORK(&tracer->read_fw_strings_work, mlx5_tracer_read_strings_db);
322
323         err = mlx5_query_mtrc_caps(tracer);
324         if (err) {
325                 mlx5_core_dbg(dev, "FWTracer: Failed to query capabilities %d\n", err);
326                 goto destroy_workqueue;
327         }
328
329         err = mlx5_fw_tracer_create_log_buf(tracer);
330         if (err) {
331                 mlx5_core_warn(dev, "FWTracer: Create log buffer failed %d\n", err);
332                 goto destroy_workqueue;
333         }
334
335         err = mlx5_fw_tracer_allocate_strings_db(tracer);
336         if (err) {
337                 mlx5_core_warn(dev, "FWTracer: Allocate strings database failed %d\n", err);
338                 goto free_log_buf;
339         }
340
341         return tracer;
342
343 free_log_buf:
344         mlx5_fw_tracer_destroy_log_buf(tracer);
345 destroy_workqueue:
346         tracer->dev = NULL;
347         destroy_workqueue(tracer->work_queue);
348 free_tracer:
349         kfree(tracer);
350         return ERR_PTR(err);
351 }
352
353 int mlx5_fw_tracer_init(struct mlx5_fw_tracer *tracer)
354 {
355         struct mlx5_core_dev *dev;
356         int err;
357
358         if (IS_ERR_OR_NULL(tracer))
359                 return 0;
360
361         dev = tracer->dev;
362
363         if (!tracer->str_db.loaded)
364                 queue_work(tracer->work_queue, &tracer->read_fw_strings_work);
365
366         err = mlx5_fw_tracer_ownership_acquire(tracer);
367         if (err) {
368                 mlx5_core_dbg(dev, "FWTracer: Ownership was not granted %d\n", err);
369                 return 0; /* return 0 since ownership can be acquired on a later FW event */
370         }
371
372         return 0;
373 }
374
375 void mlx5_fw_tracer_cleanup(struct mlx5_fw_tracer *tracer)
376 {
377         if (IS_ERR_OR_NULL(tracer))
378                 return;
379
380         cancel_work_sync(&tracer->ownership_change_work);
381
382         if (tracer->owner)
383                 mlx5_fw_tracer_ownership_release(tracer);
384 }
385
386 void mlx5_fw_tracer_destroy(struct mlx5_fw_tracer *tracer)
387 {
388         if (IS_ERR_OR_NULL(tracer))
389                 return;
390
391         cancel_work_sync(&tracer->read_fw_strings_work);
392         mlx5_fw_tracer_free_strings_db(tracer);
393         mlx5_fw_tracer_destroy_log_buf(tracer);
394         flush_workqueue(tracer->work_queue);
395         destroy_workqueue(tracer->work_queue);
396         kfree(tracer);
397 }
398
399 EXPORT_TRACEPOINT_SYMBOL(mlx5_fw);