d6cc27b0ff34412f952406e9da75efa57b721996
[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 int mlx5_fw_tracer_create_mkey(struct mlx5_fw_tracer *tracer)
173 {
174         struct mlx5_core_dev *dev = tracer->dev;
175         int err, inlen, i;
176         __be64 *mtt;
177         void *mkc;
178         u32 *in;
179
180         inlen = MLX5_ST_SZ_BYTES(create_mkey_in) +
181                         sizeof(*mtt) * round_up(TRACER_BUFFER_PAGE_NUM, 2);
182
183         in = kvzalloc(inlen, GFP_KERNEL);
184         if (!in)
185                 return -ENOMEM;
186
187         MLX5_SET(create_mkey_in, in, translations_octword_actual_size,
188                  DIV_ROUND_UP(TRACER_BUFFER_PAGE_NUM, 2));
189         mtt = (u64 *)MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt);
190         for (i = 0 ; i < TRACER_BUFFER_PAGE_NUM ; i++)
191                 mtt[i] = cpu_to_be64(tracer->buff.dma + i * PAGE_SIZE);
192
193         mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
194         MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
195         MLX5_SET(mkc, mkc, lr, 1);
196         MLX5_SET(mkc, mkc, lw, 1);
197         MLX5_SET(mkc, mkc, pd, tracer->buff.pdn);
198         MLX5_SET(mkc, mkc, bsf_octword_size, 0);
199         MLX5_SET(mkc, mkc, qpn, 0xffffff);
200         MLX5_SET(mkc, mkc, log_page_size, PAGE_SHIFT);
201         MLX5_SET(mkc, mkc, translations_octword_size,
202                  DIV_ROUND_UP(TRACER_BUFFER_PAGE_NUM, 2));
203         MLX5_SET64(mkc, mkc, start_addr, tracer->buff.dma);
204         MLX5_SET64(mkc, mkc, len, tracer->buff.size);
205         err = mlx5_core_create_mkey(dev, &tracer->buff.mkey, in, inlen);
206         if (err)
207                 mlx5_core_warn(dev, "FWTracer: Failed to create mkey, %d\n", err);
208
209         kvfree(in);
210
211         return err;
212 }
213
214 static void mlx5_fw_tracer_free_strings_db(struct mlx5_fw_tracer *tracer)
215 {
216         u32 num_string_db = tracer->str_db.num_string_db;
217         int i;
218
219         for (i = 0; i < num_string_db; i++) {
220                 kfree(tracer->str_db.buffer[i]);
221                 tracer->str_db.buffer[i] = NULL;
222         }
223 }
224
225 static int mlx5_fw_tracer_allocate_strings_db(struct mlx5_fw_tracer *tracer)
226 {
227         u32 *string_db_size_out = tracer->str_db.size_out;
228         u32 num_string_db = tracer->str_db.num_string_db;
229         int i;
230
231         for (i = 0; i < num_string_db; i++) {
232                 tracer->str_db.buffer[i] = kzalloc(string_db_size_out[i], GFP_KERNEL);
233                 if (!tracer->str_db.buffer[i])
234                         goto free_strings_db;
235         }
236
237         return 0;
238
239 free_strings_db:
240         mlx5_fw_tracer_free_strings_db(tracer);
241         return -ENOMEM;
242 }
243
244 static void mlx5_tracer_read_strings_db(struct work_struct *work)
245 {
246         struct mlx5_fw_tracer *tracer = container_of(work, struct mlx5_fw_tracer,
247                                                      read_fw_strings_work);
248         u32 num_of_reads, num_string_db = tracer->str_db.num_string_db;
249         struct mlx5_core_dev *dev = tracer->dev;
250         u32 in[MLX5_ST_SZ_DW(mtrc_cap)] = {0};
251         u32 leftovers, offset;
252         int err = 0, i, j;
253         u32 *out, outlen;
254         void *out_value;
255
256         outlen = MLX5_ST_SZ_BYTES(mtrc_stdb) + STRINGS_DB_READ_SIZE_BYTES;
257         out = kzalloc(outlen, GFP_KERNEL);
258         if (!out) {
259                 err = -ENOMEM;
260                 goto out;
261         }
262
263         for (i = 0; i < num_string_db; i++) {
264                 offset = 0;
265                 MLX5_SET(mtrc_stdb, in, string_db_index, i);
266                 num_of_reads = tracer->str_db.size_out[i] /
267                                 STRINGS_DB_READ_SIZE_BYTES;
268                 leftovers = (tracer->str_db.size_out[i] %
269                                 STRINGS_DB_READ_SIZE_BYTES) /
270                                         STRINGS_DB_LEFTOVER_SIZE_BYTES;
271
272                 MLX5_SET(mtrc_stdb, in, read_size, STRINGS_DB_READ_SIZE_BYTES);
273                 for (j = 0; j < num_of_reads; j++) {
274                         MLX5_SET(mtrc_stdb, in, start_offset, offset);
275
276                         err = mlx5_core_access_reg(dev, in, sizeof(in), out,
277                                                    outlen, MLX5_REG_MTRC_STDB,
278                                                    0, 1);
279                         if (err) {
280                                 mlx5_core_dbg(dev, "FWTracer: Failed to read strings DB %d\n",
281                                               err);
282                                 goto out_free;
283                         }
284
285                         out_value = MLX5_ADDR_OF(mtrc_stdb, out, string_db_data);
286                         memcpy(tracer->str_db.buffer[i] + offset, out_value,
287                                STRINGS_DB_READ_SIZE_BYTES);
288                         offset += STRINGS_DB_READ_SIZE_BYTES;
289                 }
290
291                 /* Strings database is aligned to 64, need to read leftovers*/
292                 MLX5_SET(mtrc_stdb, in, read_size,
293                          STRINGS_DB_LEFTOVER_SIZE_BYTES);
294                 for (j = 0; j < leftovers; j++) {
295                         MLX5_SET(mtrc_stdb, in, start_offset, offset);
296
297                         err = mlx5_core_access_reg(dev, in, sizeof(in), out,
298                                                    outlen, MLX5_REG_MTRC_STDB,
299                                                    0, 1);
300                         if (err) {
301                                 mlx5_core_dbg(dev, "FWTracer: Failed to read strings DB %d\n",
302                                               err);
303                                 goto out_free;
304                         }
305
306                         out_value = MLX5_ADDR_OF(mtrc_stdb, out, string_db_data);
307                         memcpy(tracer->str_db.buffer[i] + offset, out_value,
308                                STRINGS_DB_LEFTOVER_SIZE_BYTES);
309                         offset += STRINGS_DB_LEFTOVER_SIZE_BYTES;
310                 }
311         }
312
313         tracer->str_db.loaded = true;
314
315 out_free:
316         kfree(out);
317 out:
318         return;
319 }
320
321 static void mlx5_fw_tracer_ownership_change(struct work_struct *work)
322 {
323         struct mlx5_fw_tracer *tracer = container_of(work, struct mlx5_fw_tracer,
324                                                      ownership_change_work);
325         struct mlx5_core_dev *dev = tracer->dev;
326         int err;
327
328         if (tracer->owner) {
329                 mlx5_fw_tracer_ownership_release(tracer);
330                 return;
331         }
332
333         err = mlx5_fw_tracer_ownership_acquire(tracer);
334         if (err) {
335                 mlx5_core_dbg(dev, "FWTracer: Ownership was not granted %d\n", err);
336                 return;
337         }
338 }
339
340 struct mlx5_fw_tracer *mlx5_fw_tracer_create(struct mlx5_core_dev *dev)
341 {
342         struct mlx5_fw_tracer *tracer = NULL;
343         int err;
344
345         if (!MLX5_CAP_MCAM_REG(dev, tracer_registers)) {
346                 mlx5_core_dbg(dev, "FWTracer: Tracer capability not present\n");
347                 return NULL;
348         }
349
350         tracer = kzalloc(sizeof(*tracer), GFP_KERNEL);
351         if (!tracer)
352                 return ERR_PTR(-ENOMEM);
353
354         tracer->work_queue = create_singlethread_workqueue("mlx5_fw_tracer");
355         if (!tracer->work_queue) {
356                 err = -ENOMEM;
357                 goto free_tracer;
358         }
359
360         tracer->dev = dev;
361
362         INIT_WORK(&tracer->ownership_change_work, mlx5_fw_tracer_ownership_change);
363         INIT_WORK(&tracer->read_fw_strings_work, mlx5_tracer_read_strings_db);
364
365         err = mlx5_query_mtrc_caps(tracer);
366         if (err) {
367                 mlx5_core_dbg(dev, "FWTracer: Failed to query capabilities %d\n", err);
368                 goto destroy_workqueue;
369         }
370
371         err = mlx5_fw_tracer_create_log_buf(tracer);
372         if (err) {
373                 mlx5_core_warn(dev, "FWTracer: Create log buffer failed %d\n", err);
374                 goto destroy_workqueue;
375         }
376
377         err = mlx5_fw_tracer_allocate_strings_db(tracer);
378         if (err) {
379                 mlx5_core_warn(dev, "FWTracer: Allocate strings database failed %d\n", err);
380                 goto free_log_buf;
381         }
382
383         return tracer;
384
385 free_log_buf:
386         mlx5_fw_tracer_destroy_log_buf(tracer);
387 destroy_workqueue:
388         tracer->dev = NULL;
389         destroy_workqueue(tracer->work_queue);
390 free_tracer:
391         kfree(tracer);
392         return ERR_PTR(err);
393 }
394
395 int mlx5_fw_tracer_init(struct mlx5_fw_tracer *tracer)
396 {
397         struct mlx5_core_dev *dev;
398         int err;
399
400         if (IS_ERR_OR_NULL(tracer))
401                 return 0;
402
403         dev = tracer->dev;
404
405         if (!tracer->str_db.loaded)
406                 queue_work(tracer->work_queue, &tracer->read_fw_strings_work);
407
408         err = mlx5_core_alloc_pd(dev, &tracer->buff.pdn);
409         if (err) {
410                 mlx5_core_warn(dev, "FWTracer: Failed to allocate PD %d\n", err);
411                 return err;
412         }
413
414         err = mlx5_fw_tracer_create_mkey(tracer);
415         if (err) {
416                 mlx5_core_warn(dev, "FWTracer: Failed to create mkey %d\n", err);
417                 goto err_dealloc_pd;
418         }
419
420         err = mlx5_fw_tracer_ownership_acquire(tracer);
421         if (err) /* Don't fail since ownership can be acquired on a later FW event */
422                 mlx5_core_dbg(dev, "FWTracer: Ownership was not granted %d\n", err);
423
424         return 0;
425 err_dealloc_pd:
426         mlx5_core_dealloc_pd(dev, tracer->buff.pdn);
427         return err;
428 }
429
430 void mlx5_fw_tracer_cleanup(struct mlx5_fw_tracer *tracer)
431 {
432         if (IS_ERR_OR_NULL(tracer))
433                 return;
434
435         cancel_work_sync(&tracer->ownership_change_work);
436
437         if (tracer->owner)
438                 mlx5_fw_tracer_ownership_release(tracer);
439
440         mlx5_core_destroy_mkey(tracer->dev, &tracer->buff.mkey);
441         mlx5_core_dealloc_pd(tracer->dev, tracer->buff.pdn);
442 }
443
444 void mlx5_fw_tracer_destroy(struct mlx5_fw_tracer *tracer)
445 {
446         if (IS_ERR_OR_NULL(tracer))
447                 return;
448
449         cancel_work_sync(&tracer->read_fw_strings_work);
450         mlx5_fw_tracer_free_strings_db(tracer);
451         mlx5_fw_tracer_destroy_log_buf(tracer);
452         flush_workqueue(tracer->work_queue);
453         destroy_workqueue(tracer->work_queue);
454         kfree(tracer);
455 }
456
457 EXPORT_TRACEPOINT_SYMBOL(mlx5_fw);