xref: /linux/drivers/infiniband/sw/rxe/rxe_mad.c (revision 9611c0ce215a66770ccbe5c126bf57ba8c31bcad)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2026 zhenwei pi <zhenwei.pi@linux.dev>
4  */
5 
6 #include <rdma/ib_pma.h>
7 #include "rxe.h"
8 #include "rxe_hw_counters.h"
9 
10 static int rxe_get_pma_info(struct ib_mad *out)
11 {
12 	struct ib_class_port_info cpi = {};
13 
14 	cpi.capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
15 	memcpy((out->data + 40), &cpi, sizeof(cpi));
16 
17 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
18 }
19 
20 static int rxe_get_pma_counters(struct rxe_dev *rxe, struct ib_mad *out)
21 {
22 	struct ib_pma_portcounters *pma_cnt = (struct ib_pma_portcounters *)(out->data + 40);
23 	s64 val;
24 
25 	/* IBA release 1.8, 16.1.3.5: During operation, instead of overflowing, they shall stop
26 	 * at all ones.
27 	 */
28 	val = atomic64_read(&rxe->stats_counters[RXE_CNT_LINK_DOWNED]);
29 	pma_cnt->link_downed_counter = clamp(val, 0, U8_MAX);
30 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
31 }
32 
33 static int rxe_get_pma_counters_ext(struct rxe_dev *rxe, struct ib_mad *out)
34 {
35 	struct ib_pma_portcounters_ext *pma_cnt_ext =
36 		(struct ib_pma_portcounters_ext *)(out->data + 40);
37 	s64 val;
38 
39 	val = atomic64_read(&rxe->stats_counters[RXE_CNT_SENT_BYTES]);
40 	pma_cnt_ext->port_xmit_data = cpu_to_be64(val >> 2);
41 
42 	val = atomic64_read(&rxe->stats_counters[RXE_CNT_RCVD_BYTES]);
43 	pma_cnt_ext->port_rcv_data = cpu_to_be64(val >> 2);
44 
45 	val = atomic64_read(&rxe->stats_counters[RXE_CNT_SENT_PKTS]);
46 	pma_cnt_ext->port_xmit_packets = cpu_to_be64(val);
47 
48 	val = atomic64_read(&rxe->stats_counters[RXE_CNT_RCVD_PKTS]);
49 	pma_cnt_ext->port_rcv_packets = cpu_to_be64(val);
50 
51 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
52 }
53 
54 static int rxe_get_perf_mgmt(struct rxe_dev *rxe, const struct ib_mad *in, struct ib_mad *out)
55 {
56 	switch (in->mad_hdr.attr_id) {
57 	case IB_PMA_CLASS_PORT_INFO:
58 		return rxe_get_pma_info(out);
59 
60 	case IB_PMA_PORT_COUNTERS:
61 		return rxe_get_pma_counters(rxe, out);
62 
63 	case IB_PMA_PORT_COUNTERS_EXT:
64 		return rxe_get_pma_counters_ext(rxe, out);
65 
66 	default:
67 		out->mad_hdr.status = cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB);
68 		return IB_MAD_RESULT_SUCCESS;
69 	}
70 }
71 
72 int rxe_process_mad(struct ib_device *ibdev, int mad_flags, u32 port_num,
73 		    const struct ib_wc *in_wc, const struct ib_grh *in_grh,
74 		    const struct ib_mad *in, struct ib_mad *out,
75 		    size_t *out_mad_size, u16 *out_mad_pkey_index)
76 {
77 	struct rxe_dev *rxe = to_rdev(ibdev);
78 	u8 mgmt_class = in->mad_hdr.mgmt_class;
79 	u8 method = in->mad_hdr.method;
80 
81 	if (port_num != 1)
82 		return IB_MAD_RESULT_FAILURE;
83 
84 	memset(out, 0, sizeof(*out));
85 	switch (mgmt_class) {
86 	case IB_MGMT_CLASS_PERF_MGMT:
87 		if (method == IB_MGMT_METHOD_GET)
88 			return rxe_get_perf_mgmt(rxe, in, out);
89 		break;
90 
91 	default:
92 		out->mad_hdr.status = cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD);
93 		return IB_MAD_RESULT_SUCCESS;
94 	}
95 
96 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
97 }
98