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)
79 unsigned int delay_total;
80 unsigned int delay_us;
81 boolean_t aborted __rte_unused;
84 delay_us = SFC_EFX_MCDI_POLL_INTERVAL_MIN_US;
88 boolean_t poll_completed;
90 poll_completed = (proxy) ? sfc_efx_mcdi_proxy_event_available(sa) :
91 efx_mcdi_request_poll(enp);
95 if (delay_total > SFC_EFX_MCDI_WATCHDOG_INTERVAL_US) {
97 aborted = efx_mcdi_request_abort(enp);
99 sfc_efx_mcdi_timeout(sa);
105 rte_delay_us(delay_us);
107 delay_total += delay_us;
109 /* Exponentially back off the poll frequency */
110 RTE_BUILD_BUG_ON(SFC_EFX_MCDI_POLL_INTERVAL_MAX_US >
113 if (delay_us > SFC_EFX_MCDI_POLL_INTERVAL_MAX_US)
114 delay_us = SFC_EFX_MCDI_POLL_INTERVAL_MAX_US;
120 sfc_efx_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
122 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
123 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
124 uint32_t proxy_handle;
126 rte_spinlock_lock(&mcdi->lock);
128 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
130 efx_mcdi_request_start(sa->nic, emrp, B_FALSE);
131 sfc_efx_mcdi_poll(sa, B_FALSE);
133 if (efx_mcdi_get_proxy_handle(sa->nic, emrp, &proxy_handle) == 0) {
135 * Authorization is required for the MCDI request;
136 * wait for an MCDI proxy response event to bring
137 * a non-zero proxy handle (should be the same as
138 * the value obtained above) and operation status
140 sfc_efx_mcdi_poll(sa, B_TRUE);
142 if ((mcdi->proxy_handle != 0) &&
143 (mcdi->proxy_handle != proxy_handle)) {
144 sfc_efx_mcdi_err(mcdi, "Unexpected MCDI proxy event");
145 emrp->emr_rc = EFAULT;
146 } else if (mcdi->proxy_result == 0) {
148 * Authorization succeeded; re-issue the original
149 * request and poll for an ordinary MCDI response
151 efx_mcdi_request_start(sa->nic, emrp, B_FALSE);
152 sfc_efx_mcdi_poll(sa, B_FALSE);
154 emrp->emr_rc = mcdi->proxy_result;
155 sfc_efx_mcdi_err(mcdi,
156 "MCDI proxy authorization failed (handle=%08x, result=%d)",
157 proxy_handle, mcdi->proxy_result);
161 rte_spinlock_unlock(&mcdi->lock);
165 sfc_efx_mcdi_ev_cpl(void *arg)
167 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
168 struct sfc_efx_mcdi *mcdi __rte_unused;
171 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
173 /* MCDI is polled, completions are not expected */
178 sfc_efx_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
180 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
181 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
183 sfc_efx_mcdi_warn(mcdi, "MC %s",
184 (eme == EFX_MCDI_EXCEPTION_MC_REBOOT) ? "REBOOT" :
185 (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT) ? "BADASSERT" : "UNKNOWN");
187 sfc_schedule_restart(sa);
190 #define SFC_MCDI_LOG_BUF_SIZE 128
193 sfc_efx_mcdi_do_log(const struct sfc_efx_mcdi *mcdi,
194 char *buffer, void *data, size_t data_size,
195 size_t pfxsize, size_t position)
197 uint32_t *words = data;
198 /* Space separator plus 2 characters per byte */
199 const size_t word_str_space = 1 + 2 * sizeof(*words);
202 for (i = 0; i < data_size; i += sizeof(*words)) {
203 if (position + word_str_space >=
204 SFC_MCDI_LOG_BUF_SIZE) {
205 /* Flush at SFC_MCDI_LOG_BUF_SIZE with backslash
206 * at the end which is required by netlogdecode.
208 buffer[position] = '\0';
209 sfc_efx_log_mcdi(mcdi, "%s \\", buffer);
210 /* Preserve prefix for the next log message */
213 position += snprintf(buffer + position,
214 SFC_MCDI_LOG_BUF_SIZE - position,
222 sfc_efx_mcdi_logger(void *arg, efx_log_msg_t type,
223 void *header, size_t header_size,
224 void *data, size_t data_size)
226 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
227 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
228 char buffer[SFC_MCDI_LOG_BUF_SIZE];
233 * Unlike the other cases, MCDI logging implies more onerous work
234 * needed to produce a message. If the dynamic log level prevents
235 * the end result from being printed, the CPU time will be wasted.
237 * To avoid wasting time, the actual level is examined in advance.
239 if (rte_log_get_level(mcdi->logtype) < (int)SFC_EFX_LOG_LEVEL_MCDI)
242 /* The format including prefix added by sfc_efx_log_mcdi() is the
243 * format consumed by the Solarflare netlogdecode tool.
245 pfxsize = snprintf(buffer, sizeof(buffer), "MCDI RPC %s:",
246 type == EFX_LOG_MCDI_REQUEST ? "REQ" :
247 type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???");
248 start = sfc_efx_mcdi_do_log(mcdi, buffer, header, header_size,
250 start = sfc_efx_mcdi_do_log(mcdi, buffer, data, data_size,
252 if (start != pfxsize) {
253 buffer[start] = '\0';
254 sfc_efx_log_mcdi(mcdi, "%s", buffer);
259 sfc_efx_mcdi_ev_proxy_response(void *arg, uint32_t handle, efx_rc_t result)
261 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
262 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
264 mcdi->proxy_handle = handle;
265 mcdi->proxy_result = result;
269 sfc_efx_mcdi_init(struct sfc_adapter *sa, struct sfc_efx_mcdi *mcdi,
270 uint32_t logtype, const char *log_prefix)
273 efx_mcdi_transport_t *emtp;
276 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_UNINITIALIZED);
278 rte_spinlock_init(&mcdi->lock);
280 mcdi->state = SFC_EFX_MCDI_INITIALIZED;
282 mcdi->logtype = logtype;
283 mcdi->log_prefix = log_prefix;
285 max_msg_size = sizeof(uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
286 rc = sfc_dma_alloc(sa, "mcdi", 0, max_msg_size, sa->socket_id,
291 emtp = &mcdi->transport;
292 emtp->emt_context = sa;
293 emtp->emt_dma_mem = &mcdi->mem;
294 emtp->emt_execute = sfc_efx_mcdi_execute;
295 emtp->emt_ev_cpl = sfc_efx_mcdi_ev_cpl;
296 emtp->emt_exception = sfc_efx_mcdi_exception;
297 emtp->emt_logger = sfc_efx_mcdi_logger;
298 emtp->emt_ev_proxy_response = sfc_efx_mcdi_ev_proxy_response;
300 sfc_efx_mcdi_info(mcdi, "init MCDI");
301 rc = efx_mcdi_init(sa->nic, emtp);
308 memset(emtp, 0, sizeof(*emtp));
309 sfc_dma_free(sa, &mcdi->mem);
312 mcdi->state = SFC_EFX_MCDI_UNINITIALIZED;
317 sfc_efx_mcdi_fini(struct sfc_adapter *sa, struct sfc_efx_mcdi *mcdi)
319 efx_mcdi_transport_t *emtp;
321 emtp = &mcdi->transport;
323 rte_spinlock_lock(&mcdi->lock);
325 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
326 mcdi->state = SFC_EFX_MCDI_UNINITIALIZED;
328 sfc_efx_mcdi_info(mcdi, "fini MCDI");
329 efx_mcdi_fini(sa->nic);
330 memset(emtp, 0, sizeof(*emtp));
332 rte_spinlock_unlock(&mcdi->lock);
334 sfc_dma_free(sa, &mcdi->mem);
338 sfc_mcdi_init(struct sfc_adapter *sa)
342 sfc_log_init(sa, "entry");
344 logtype = sfc_register_logtype(&sa->priv.shared->pci_addr,
345 SFC_LOGTYPE_MCDI_STR,
348 return sfc_efx_mcdi_init(sa, &sa->mcdi, logtype,
349 sa->priv.shared->log_prefix);
353 sfc_mcdi_fini(struct sfc_adapter *sa)
355 sfc_log_init(sa, "entry");
356 sfc_efx_mcdi_fini(sa, &sa->mcdi);