1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2023 Advanced Micro Devices, Inc */
3
4 #include "core.h"
5 #include <linux/pds/pds_auxbus.h>
6
7 static struct
pdsc_dl_find_viftype_by_id(struct pdsc * pdsc,enum devlink_param_type dl_id)8 pdsc_viftype *pdsc_dl_find_viftype_by_id(struct pdsc *pdsc,
9 enum devlink_param_type dl_id)
10 {
11 int vt;
12
13 if (!pdsc->viftype_status)
14 return NULL;
15
16 for (vt = 0; vt < PDS_DEV_TYPE_MAX; vt++) {
17 if (pdsc->viftype_status[vt].dl_id == dl_id)
18 return &pdsc->viftype_status[vt];
19 }
20
21 return NULL;
22 }
23
pdsc_dl_enable_get(struct devlink * dl,u32 id,struct devlink_param_gset_ctx * ctx)24 int pdsc_dl_enable_get(struct devlink *dl, u32 id,
25 struct devlink_param_gset_ctx *ctx)
26 {
27 struct pdsc *pdsc = devlink_priv(dl);
28 struct pdsc_viftype *vt_entry;
29
30 vt_entry = pdsc_dl_find_viftype_by_id(pdsc, id);
31 if (!vt_entry)
32 return -ENOENT;
33
34 ctx->val.vbool = vt_entry->enabled;
35
36 return 0;
37 }
38
pdsc_dl_enable_set(struct devlink * dl,u32 id,struct devlink_param_gset_ctx * ctx)39 int pdsc_dl_enable_set(struct devlink *dl, u32 id,
40 struct devlink_param_gset_ctx *ctx)
41 {
42 struct pdsc *pdsc = devlink_priv(dl);
43 struct pdsc_viftype *vt_entry;
44 int err = 0;
45 int vf_id;
46
47 vt_entry = pdsc_dl_find_viftype_by_id(pdsc, id);
48 if (!vt_entry || !vt_entry->supported)
49 return -EOPNOTSUPP;
50
51 if (vt_entry->enabled == ctx->val.vbool)
52 return 0;
53
54 vt_entry->enabled = ctx->val.vbool;
55 for (vf_id = 0; vf_id < pdsc->num_vfs; vf_id++) {
56 struct pdsc *vf = pdsc->vfs[vf_id].vf;
57
58 err = ctx->val.vbool ? pdsc_auxbus_dev_add(vf, pdsc) :
59 pdsc_auxbus_dev_del(vf, pdsc);
60 }
61
62 return err;
63 }
64
pdsc_dl_enable_validate(struct devlink * dl,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)65 int pdsc_dl_enable_validate(struct devlink *dl, u32 id,
66 union devlink_param_value val,
67 struct netlink_ext_ack *extack)
68 {
69 struct pdsc *pdsc = devlink_priv(dl);
70 struct pdsc_viftype *vt_entry;
71
72 vt_entry = pdsc_dl_find_viftype_by_id(pdsc, id);
73 if (!vt_entry || !vt_entry->supported)
74 return -EOPNOTSUPP;
75
76 if (!pdsc->viftype_status[vt_entry->vif_id].supported)
77 return -ENODEV;
78
79 return 0;
80 }
81
pdsc_dl_flash_update(struct devlink * dl,struct devlink_flash_update_params * params,struct netlink_ext_ack * extack)82 int pdsc_dl_flash_update(struct devlink *dl,
83 struct devlink_flash_update_params *params,
84 struct netlink_ext_ack *extack)
85 {
86 struct pdsc *pdsc = devlink_priv(dl);
87
88 return pdsc_firmware_update(pdsc, params->fw, extack);
89 }
90
91 static char *fw_slotnames[] = {
92 "fw.goldfw",
93 "fw.mainfwa",
94 "fw.mainfwb",
95 };
96
pdsc_dl_info_get(struct devlink * dl,struct devlink_info_req * req,struct netlink_ext_ack * extack)97 int pdsc_dl_info_get(struct devlink *dl, struct devlink_info_req *req,
98 struct netlink_ext_ack *extack)
99 {
100 union pds_core_dev_cmd cmd = {
101 .fw_control.opcode = PDS_CORE_CMD_FW_CONTROL,
102 .fw_control.oper = PDS_CORE_FW_GET_LIST,
103 };
104 struct pds_core_fw_list_info fw_list;
105 struct pdsc *pdsc = devlink_priv(dl);
106 union pds_core_dev_comp comp;
107 char buf[16];
108 int listlen;
109 int err;
110 int i;
111
112 mutex_lock(&pdsc->devcmd_lock);
113 err = pdsc_devcmd_locked(pdsc, &cmd, &comp, pdsc->devcmd_timeout * 2);
114 memcpy_fromio(&fw_list, pdsc->cmd_regs->data, sizeof(fw_list));
115 mutex_unlock(&pdsc->devcmd_lock);
116 if (err && err != -EIO)
117 return err;
118
119 listlen = fw_list.num_fw_slots;
120 for (i = 0; i < listlen; i++) {
121 if (i < ARRAY_SIZE(fw_slotnames))
122 strscpy(buf, fw_slotnames[i], sizeof(buf));
123 else
124 snprintf(buf, sizeof(buf), "fw.slot_%d", i);
125 err = devlink_info_version_stored_put(req, buf,
126 fw_list.fw_names[i].fw_version);
127 }
128
129 err = devlink_info_version_running_put(req,
130 DEVLINK_INFO_VERSION_GENERIC_FW,
131 pdsc->dev_info.fw_version);
132 if (err)
133 return err;
134
135 snprintf(buf, sizeof(buf), "0x%x", pdsc->dev_info.asic_type);
136 err = devlink_info_version_fixed_put(req,
137 DEVLINK_INFO_VERSION_GENERIC_ASIC_ID,
138 buf);
139 if (err)
140 return err;
141
142 snprintf(buf, sizeof(buf), "0x%x", pdsc->dev_info.asic_rev);
143 err = devlink_info_version_fixed_put(req,
144 DEVLINK_INFO_VERSION_GENERIC_ASIC_REV,
145 buf);
146 if (err)
147 return err;
148
149 return devlink_info_serial_number_put(req, pdsc->dev_info.serial_num);
150 }
151
pdsc_fw_reporter_diagnose(struct devlink_health_reporter * reporter,struct devlink_fmsg * fmsg,struct netlink_ext_ack * extack)152 int pdsc_fw_reporter_diagnose(struct devlink_health_reporter *reporter,
153 struct devlink_fmsg *fmsg,
154 struct netlink_ext_ack *extack)
155 {
156 struct pdsc *pdsc = devlink_health_reporter_priv(reporter);
157 int err;
158
159 mutex_lock(&pdsc->config_lock);
160
161 if (test_bit(PDSC_S_FW_DEAD, &pdsc->state))
162 err = devlink_fmsg_string_pair_put(fmsg, "Status", "dead");
163 else if (!pdsc_is_fw_good(pdsc))
164 err = devlink_fmsg_string_pair_put(fmsg, "Status", "unhealthy");
165 else
166 err = devlink_fmsg_string_pair_put(fmsg, "Status", "healthy");
167
168 mutex_unlock(&pdsc->config_lock);
169
170 if (err)
171 return err;
172
173 err = devlink_fmsg_u32_pair_put(fmsg, "State",
174 pdsc->fw_status &
175 ~PDS_CORE_FW_STS_F_GENERATION);
176 if (err)
177 return err;
178
179 err = devlink_fmsg_u32_pair_put(fmsg, "Generation",
180 pdsc->fw_generation >> 4);
181 if (err)
182 return err;
183
184 return devlink_fmsg_u32_pair_put(fmsg, "Recoveries",
185 pdsc->fw_recoveries);
186 }
187