1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright(c) 2019-2020 Xilinx, Inc.
4 * Copyright(c) 2016-2019 Solarflare Communications Inc.
6 * This software was jointly developed between OKTET Labs (under contract
7 * for Solarflare) and Solarflare Communications, Inc.
10 #include <rte_cycles.h>
14 #include "efx_regs_mcdi.h"
18 #include "sfc_debug.h"
22 #define SFC_EFX_MCDI_POLL_INTERVAL_MIN_US 10 /* 10us */
23 #define SFC_EFX_MCDI_POLL_INTERVAL_MAX_US (US_PER_S / 10) /* 100ms */
24 #define SFC_EFX_MCDI_WATCHDOG_INTERVAL_US (10 * US_PER_S) /* 10s */
26 #define sfc_efx_mcdi_log(mcdi, level, ...) \
28 const struct sfc_efx_mcdi *_mcdi = (mcdi); \
30 rte_log(level, _mcdi->logtype, \
31 RTE_FMT("%s" RTE_FMT_HEAD(__VA_ARGS__ ,) "\n", \
33 RTE_FMT_TAIL(__VA_ARGS__,))); \
36 #define sfc_efx_mcdi_err(mcdi, ...) \
37 sfc_efx_mcdi_log(mcdi, RTE_LOG_ERR, __VA_ARGS__)
39 #define sfc_efx_mcdi_warn(mcdi, ...) \
40 sfc_efx_mcdi_log(mcdi, RTE_LOG_WARNING, __VA_ARGS__)
42 #define sfc_efx_mcdi_info(mcdi, ...) \
43 sfc_efx_mcdi_log(mcdi, RTE_LOG_INFO, __VA_ARGS__)
45 /** Level value used by MCDI log statements */
46 #define SFC_EFX_LOG_LEVEL_MCDI RTE_LOG_INFO
48 #define sfc_efx_log_mcdi(mcdi, ...) \
49 sfc_efx_mcdi_log(mcdi, SFC_EFX_LOG_LEVEL_MCDI, __VA_ARGS__)
52 sfc_efx_mcdi_timeout(struct sfc_adapter *sa)
54 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
56 sfc_efx_mcdi_warn(mcdi, "MC TIMEOUT");
58 sfc_panic(sa, "MCDI timeout handling is not implemented\n");
61 static inline boolean_t
62 sfc_efx_mcdi_proxy_event_available(struct sfc_adapter *sa)
64 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
66 mcdi->proxy_handle = 0;
67 mcdi->proxy_result = ETIMEDOUT;
68 sfc_ev_mgmt_qpoll(sa);
69 if (mcdi->proxy_result != ETIMEDOUT)
76 sfc_efx_mcdi_poll(struct sfc_adapter *sa, boolean_t proxy)
78 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
80 unsigned int delay_total;
81 unsigned int delay_us;
82 boolean_t aborted __rte_unused;
85 delay_us = SFC_EFX_MCDI_POLL_INTERVAL_MIN_US;
89 boolean_t poll_completed;
91 poll_completed = (proxy) ? sfc_efx_mcdi_proxy_event_available(sa) :
92 efx_mcdi_request_poll(enp);
96 if (delay_total > SFC_EFX_MCDI_WATCHDOG_INTERVAL_US) {
98 aborted = efx_mcdi_request_abort(enp);
100 sfc_efx_mcdi_timeout(sa);
106 rte_delay_us(delay_us);
108 delay_total += delay_us;
110 /* Exponentially back off the poll frequency */
111 RTE_BUILD_BUG_ON(SFC_EFX_MCDI_POLL_INTERVAL_MAX_US >
114 if (delay_us > SFC_EFX_MCDI_POLL_INTERVAL_MAX_US)
115 delay_us = SFC_EFX_MCDI_POLL_INTERVAL_MAX_US;
121 sfc_efx_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
123 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
124 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
125 uint32_t proxy_handle;
127 rte_spinlock_lock(&mcdi->lock);
129 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
131 efx_mcdi_request_start(mcdi->nic, emrp, B_FALSE);
132 sfc_efx_mcdi_poll(sa, B_FALSE);
134 if (efx_mcdi_get_proxy_handle(mcdi->nic, emrp, &proxy_handle) == 0) {
136 * Authorization is required for the MCDI request;
137 * wait for an MCDI proxy response event to bring
138 * a non-zero proxy handle (should be the same as
139 * the value obtained above) and operation status
141 sfc_efx_mcdi_poll(sa, B_TRUE);
143 if ((mcdi->proxy_handle != 0) &&
144 (mcdi->proxy_handle != proxy_handle)) {
145 sfc_efx_mcdi_err(mcdi, "Unexpected MCDI proxy event");
146 emrp->emr_rc = EFAULT;
147 } else if (mcdi->proxy_result == 0) {
149 * Authorization succeeded; re-issue the original
150 * request and poll for an ordinary MCDI response
152 efx_mcdi_request_start(mcdi->nic, emrp, B_FALSE);
153 sfc_efx_mcdi_poll(sa, B_FALSE);
155 emrp->emr_rc = mcdi->proxy_result;
156 sfc_efx_mcdi_err(mcdi,
157 "MCDI proxy authorization failed (handle=%08x, result=%d)",
158 proxy_handle, mcdi->proxy_result);
162 rte_spinlock_unlock(&mcdi->lock);
166 sfc_efx_mcdi_ev_cpl(void *arg)
168 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
169 struct sfc_efx_mcdi *mcdi __rte_unused;
172 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
174 /* MCDI is polled, completions are not expected */
179 sfc_efx_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
181 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
182 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
184 sfc_efx_mcdi_warn(mcdi, "MC %s",
185 (eme == EFX_MCDI_EXCEPTION_MC_REBOOT) ? "REBOOT" :
186 (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT) ? "BADASSERT" : "UNKNOWN");
188 sfc_schedule_restart(sa);
191 #define SFC_MCDI_LOG_BUF_SIZE 128
194 sfc_efx_mcdi_do_log(const struct sfc_efx_mcdi *mcdi,
195 char *buffer, void *data, size_t data_size,
196 size_t pfxsize, size_t position)
198 uint32_t *words = data;
199 /* Space separator plus 2 characters per byte */
200 const size_t word_str_space = 1 + 2 * sizeof(*words);
203 for (i = 0; i < data_size; i += sizeof(*words)) {
204 if (position + word_str_space >=
205 SFC_MCDI_LOG_BUF_SIZE) {
206 /* Flush at SFC_MCDI_LOG_BUF_SIZE with backslash
207 * at the end which is required by netlogdecode.
209 buffer[position] = '\0';
210 sfc_efx_log_mcdi(mcdi, "%s \\", buffer);
211 /* Preserve prefix for the next log message */
214 position += snprintf(buffer + position,
215 SFC_MCDI_LOG_BUF_SIZE - position,
223 sfc_efx_mcdi_logger(void *arg, efx_log_msg_t type,
224 void *header, size_t header_size,
225 void *data, size_t data_size)
227 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
228 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
229 char buffer[SFC_MCDI_LOG_BUF_SIZE];
234 * Unlike the other cases, MCDI logging implies more onerous work
235 * needed to produce a message. If the dynamic log level prevents
236 * the end result from being printed, the CPU time will be wasted.
238 * To avoid wasting time, the actual level is examined in advance.
240 if (rte_log_get_level(mcdi->logtype) < (int)SFC_EFX_LOG_LEVEL_MCDI)
243 /* The format including prefix added by sfc_efx_log_mcdi() is the
244 * format consumed by the Solarflare netlogdecode tool.
246 pfxsize = snprintf(buffer, sizeof(buffer), "MCDI RPC %s:",
247 type == EFX_LOG_MCDI_REQUEST ? "REQ" :
248 type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???");
249 start = sfc_efx_mcdi_do_log(mcdi, buffer, header, header_size,
251 start = sfc_efx_mcdi_do_log(mcdi, buffer, data, data_size,
253 if (start != pfxsize) {
254 buffer[start] = '\0';
255 sfc_efx_log_mcdi(mcdi, "%s", buffer);
260 sfc_efx_mcdi_ev_proxy_response(void *arg, uint32_t handle, efx_rc_t result)
262 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
263 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
265 mcdi->proxy_handle = handle;
266 mcdi->proxy_result = result;
270 sfc_efx_mcdi_init(struct sfc_adapter *sa, struct sfc_efx_mcdi *mcdi,
271 uint32_t logtype, const char *log_prefix, efx_nic_t *nic)
274 efx_mcdi_transport_t *emtp;
277 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_UNINITIALIZED);
279 rte_spinlock_init(&mcdi->lock);
283 mcdi->state = SFC_EFX_MCDI_INITIALIZED;
285 mcdi->logtype = logtype;
286 mcdi->log_prefix = log_prefix;
288 max_msg_size = sizeof(uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
289 rc = sfc_dma_alloc(sa, "mcdi", 0, max_msg_size, sa->socket_id,
294 emtp = &mcdi->transport;
295 emtp->emt_context = sa;
296 emtp->emt_dma_mem = &mcdi->mem;
297 emtp->emt_execute = sfc_efx_mcdi_execute;
298 emtp->emt_ev_cpl = sfc_efx_mcdi_ev_cpl;
299 emtp->emt_exception = sfc_efx_mcdi_exception;
300 emtp->emt_logger = sfc_efx_mcdi_logger;
301 emtp->emt_ev_proxy_response = sfc_efx_mcdi_ev_proxy_response;
303 sfc_efx_mcdi_info(mcdi, "init MCDI");
304 rc = efx_mcdi_init(mcdi->nic, emtp);
311 memset(emtp, 0, sizeof(*emtp));
312 sfc_dma_free(sa, &mcdi->mem);
315 mcdi->state = SFC_EFX_MCDI_UNINITIALIZED;
320 sfc_efx_mcdi_fini(struct sfc_adapter *sa, struct sfc_efx_mcdi *mcdi)
322 efx_mcdi_transport_t *emtp;
324 emtp = &mcdi->transport;
326 rte_spinlock_lock(&mcdi->lock);
328 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
329 mcdi->state = SFC_EFX_MCDI_UNINITIALIZED;
331 sfc_efx_mcdi_info(mcdi, "fini MCDI");
332 efx_mcdi_fini(mcdi->nic);
333 memset(emtp, 0, sizeof(*emtp));
335 rte_spinlock_unlock(&mcdi->lock);
337 sfc_dma_free(sa, &mcdi->mem);
341 sfc_mcdi_init(struct sfc_adapter *sa)
345 sfc_log_init(sa, "entry");
347 logtype = sfc_register_logtype(&sa->priv.shared->pci_addr,
348 SFC_LOGTYPE_MCDI_STR,
351 return sfc_efx_mcdi_init(sa, &sa->mcdi, logtype,
352 sa->priv.shared->log_prefix, sa->nic);
356 sfc_mcdi_fini(struct sfc_adapter *sa)
358 sfc_log_init(sa, "entry");
359 sfc_efx_mcdi_fini(sa, &sa->mcdi);