xref: /openbmc/linux/drivers/infiniband/hw/bnxt_re/hw_counters.c (revision 812f77b749a8ae11f58dacf0d3ed65e7ede47458)
1 /*
2  * Broadcom NetXtreme-E RoCE driver.
3  *
4  * Copyright (c) 2016 - 2017, Broadcom. All rights reserved.  The term
5  * Broadcom refers to Broadcom Limited and/or its subsidiaries.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * BSD license below:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in
21  *    the documentation and/or other materials provided with the
22  *    distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  * Description: Statistics
37  *
38  */
39 
40 #include <linux/interrupt.h>
41 #include <linux/types.h>
42 #include <linux/spinlock.h>
43 #include <linux/sched.h>
44 #include <linux/slab.h>
45 #include <linux/pci.h>
46 #include <linux/prefetch.h>
47 #include <linux/delay.h>
48 
49 #include <rdma/ib_addr.h>
50 
51 #include "bnxt_ulp.h"
52 #include "roce_hsi.h"
53 #include "qplib_res.h"
54 #include "qplib_sp.h"
55 #include "qplib_fp.h"
56 #include "qplib_rcfw.h"
57 #include "bnxt_re.h"
58 #include "hw_counters.h"
59 
60 static const char * const bnxt_re_stat_name[] = {
61 	[BNXT_RE_ACTIVE_QP]           =  "active_qps",
62 	[BNXT_RE_ACTIVE_SRQ]          =  "active_srqs",
63 	[BNXT_RE_ACTIVE_CQ]           =  "active_cqs",
64 	[BNXT_RE_ACTIVE_MR]           =  "active_mrs",
65 	[BNXT_RE_ACTIVE_MW]           =  "active_mws",
66 	[BNXT_RE_RX_PKTS]             =  "rx_pkts",
67 	[BNXT_RE_RX_BYTES]            =  "rx_bytes",
68 	[BNXT_RE_TX_PKTS]             =  "tx_pkts",
69 	[BNXT_RE_TX_BYTES]            =  "tx_bytes",
70 	[BNXT_RE_RECOVERABLE_ERRORS]  =  "recoverable_errors"
71 };
72 
73 int bnxt_re_ib_get_hw_stats(struct ib_device *ibdev,
74 			    struct rdma_hw_stats *stats,
75 			    u8 port, int index)
76 {
77 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
78 	struct ctx_hw_stats *bnxt_re_stats = rdev->qplib_ctx.stats.dma;
79 
80 	if (!port || !stats)
81 		return -EINVAL;
82 
83 	stats->value[BNXT_RE_ACTIVE_QP] = atomic_read(&rdev->qp_count);
84 	stats->value[BNXT_RE_ACTIVE_SRQ] = atomic_read(&rdev->srq_count);
85 	stats->value[BNXT_RE_ACTIVE_CQ] = atomic_read(&rdev->cq_count);
86 	stats->value[BNXT_RE_ACTIVE_MR] = atomic_read(&rdev->mr_count);
87 	stats->value[BNXT_RE_ACTIVE_MW] = atomic_read(&rdev->mw_count);
88 	if (bnxt_re_stats) {
89 		stats->value[BNXT_RE_RECOVERABLE_ERRORS] =
90 			le64_to_cpu(bnxt_re_stats->tx_bcast_pkts);
91 		stats->value[BNXT_RE_RX_PKTS] =
92 			le64_to_cpu(bnxt_re_stats->rx_ucast_pkts);
93 		stats->value[BNXT_RE_RX_BYTES] =
94 			le64_to_cpu(bnxt_re_stats->rx_ucast_bytes);
95 		stats->value[BNXT_RE_TX_PKTS] =
96 			le64_to_cpu(bnxt_re_stats->tx_ucast_pkts);
97 		stats->value[BNXT_RE_TX_BYTES] =
98 			le64_to_cpu(bnxt_re_stats->tx_ucast_bytes);
99 	}
100 	return ARRAY_SIZE(bnxt_re_stat_name);
101 }
102 
103 struct rdma_hw_stats *bnxt_re_ib_alloc_hw_stats(struct ib_device *ibdev,
104 						u8 port_num)
105 {
106 	BUILD_BUG_ON(ARRAY_SIZE(bnxt_re_stat_name) != BNXT_RE_NUM_COUNTERS);
107 	/* We support only per port stats */
108 	if (!port_num)
109 		return NULL;
110 
111 	return rdma_alloc_hw_stats_struct(bnxt_re_stat_name,
112 					  ARRAY_SIZE(bnxt_re_stat_name),
113 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
114 }
115