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_crit(mcdi, ...) \
37 sfc_efx_mcdi_log(mcdi, RTE_LOG_CRIT, __VA_ARGS__)
39 #define sfc_efx_mcdi_err(mcdi, ...) \
40 sfc_efx_mcdi_log(mcdi, RTE_LOG_ERR, __VA_ARGS__)
42 #define sfc_efx_mcdi_warn(mcdi, ...) \
43 sfc_efx_mcdi_log(mcdi, RTE_LOG_WARNING, __VA_ARGS__)
45 #define sfc_efx_mcdi_info(mcdi, ...) \
46 sfc_efx_mcdi_log(mcdi, RTE_LOG_INFO, __VA_ARGS__)
48 /** Level value used by MCDI log statements */
49 #define SFC_EFX_LOG_LEVEL_MCDI RTE_LOG_INFO
51 #define sfc_efx_log_mcdi(mcdi, ...) \
52 sfc_efx_mcdi_log(mcdi, SFC_EFX_LOG_LEVEL_MCDI, __VA_ARGS__)
55 sfc_efx_mcdi_timeout(struct sfc_adapter *sa)
57 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
59 sfc_efx_mcdi_warn(mcdi, "MC TIMEOUT");
61 mcdi->state = SFC_EFX_MCDI_DEAD;
62 sfc_efx_mcdi_crit(mcdi,
63 "MCDI timeout handling is not implemented - NIC is unusable");
66 static inline boolean_t
67 sfc_efx_mcdi_proxy_event_available(struct sfc_adapter *sa)
69 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
71 mcdi->proxy_handle = 0;
72 mcdi->proxy_result = ETIMEDOUT;
73 mcdi->ops->mgmt_evq_poll(mcdi->ops_cookie);
74 if (mcdi->proxy_result != ETIMEDOUT)
81 sfc_efx_mcdi_poll(struct sfc_adapter *sa, boolean_t proxy)
83 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
85 unsigned int delay_total;
86 unsigned int delay_us;
87 boolean_t aborted __rte_unused;
90 delay_us = SFC_EFX_MCDI_POLL_INTERVAL_MIN_US;
94 boolean_t poll_completed;
96 poll_completed = (proxy) ? sfc_efx_mcdi_proxy_event_available(sa) :
97 efx_mcdi_request_poll(enp);
101 if (delay_total > SFC_EFX_MCDI_WATCHDOG_INTERVAL_US) {
103 aborted = efx_mcdi_request_abort(enp);
105 sfc_efx_mcdi_timeout(sa);
111 rte_delay_us(delay_us);
113 delay_total += delay_us;
115 /* Exponentially back off the poll frequency */
116 RTE_BUILD_BUG_ON(SFC_EFX_MCDI_POLL_INTERVAL_MAX_US >
119 if (delay_us > SFC_EFX_MCDI_POLL_INTERVAL_MAX_US)
120 delay_us = SFC_EFX_MCDI_POLL_INTERVAL_MAX_US;
126 sfc_efx_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
128 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
129 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
130 uint32_t proxy_handle;
132 if (mcdi->state == SFC_EFX_MCDI_DEAD) {
133 emrp->emr_rc = ENOEXEC;
137 rte_spinlock_lock(&mcdi->lock);
139 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
141 efx_mcdi_request_start(mcdi->nic, emrp, B_FALSE);
142 sfc_efx_mcdi_poll(sa, B_FALSE);
144 if (efx_mcdi_get_proxy_handle(mcdi->nic, emrp, &proxy_handle) == 0) {
146 * Authorization is required for the MCDI request;
147 * wait for an MCDI proxy response event to bring
148 * a non-zero proxy handle (should be the same as
149 * the value obtained above) and operation status
151 sfc_efx_mcdi_poll(sa, B_TRUE);
153 if ((mcdi->proxy_handle != 0) &&
154 (mcdi->proxy_handle != proxy_handle)) {
155 sfc_efx_mcdi_err(mcdi, "Unexpected MCDI proxy event");
156 emrp->emr_rc = EFAULT;
157 } else if (mcdi->proxy_result == 0) {
159 * Authorization succeeded; re-issue the original
160 * request and poll for an ordinary MCDI response
162 efx_mcdi_request_start(mcdi->nic, emrp, B_FALSE);
163 sfc_efx_mcdi_poll(sa, B_FALSE);
165 emrp->emr_rc = mcdi->proxy_result;
166 sfc_efx_mcdi_err(mcdi,
167 "MCDI proxy authorization failed (handle=%08x, result=%d)",
168 proxy_handle, mcdi->proxy_result);
172 rte_spinlock_unlock(&mcdi->lock);
176 sfc_efx_mcdi_ev_cpl(void *arg)
178 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
179 struct sfc_efx_mcdi *mcdi __rte_unused;
182 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
184 /* MCDI is polled, completions are not expected */
189 sfc_efx_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
191 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
192 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
194 sfc_efx_mcdi_warn(mcdi, "MC %s",
195 (eme == EFX_MCDI_EXCEPTION_MC_REBOOT) ? "REBOOT" :
196 (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT) ? "BADASSERT" : "UNKNOWN");
198 mcdi->ops->sched_restart(mcdi->ops_cookie);
201 #define SFC_MCDI_LOG_BUF_SIZE 128
204 sfc_efx_mcdi_do_log(const struct sfc_efx_mcdi *mcdi,
205 char *buffer, void *data, size_t data_size,
206 size_t pfxsize, size_t position)
208 uint32_t *words = data;
209 /* Space separator plus 2 characters per byte */
210 const size_t word_str_space = 1 + 2 * sizeof(*words);
213 for (i = 0; i < data_size; i += sizeof(*words)) {
214 if (position + word_str_space >=
215 SFC_MCDI_LOG_BUF_SIZE) {
216 /* Flush at SFC_MCDI_LOG_BUF_SIZE with backslash
217 * at the end which is required by netlogdecode.
219 buffer[position] = '\0';
220 sfc_efx_log_mcdi(mcdi, "%s \\", buffer);
221 /* Preserve prefix for the next log message */
224 position += snprintf(buffer + position,
225 SFC_MCDI_LOG_BUF_SIZE - position,
233 sfc_efx_mcdi_logger(void *arg, efx_log_msg_t type,
234 void *header, size_t header_size,
235 void *data, size_t data_size)
237 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
238 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
239 char buffer[SFC_MCDI_LOG_BUF_SIZE];
244 * Unlike the other cases, MCDI logging implies more onerous work
245 * needed to produce a message. If the dynamic log level prevents
246 * the end result from being printed, the CPU time will be wasted.
248 * To avoid wasting time, the actual level is examined in advance.
250 if (rte_log_get_level(mcdi->logtype) < (int)SFC_EFX_LOG_LEVEL_MCDI)
253 /* The format including prefix added by sfc_efx_log_mcdi() is the
254 * format consumed by the Solarflare netlogdecode tool.
256 pfxsize = snprintf(buffer, sizeof(buffer), "MCDI RPC %s:",
257 type == EFX_LOG_MCDI_REQUEST ? "REQ" :
258 type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???");
259 start = sfc_efx_mcdi_do_log(mcdi, buffer, header, header_size,
261 start = sfc_efx_mcdi_do_log(mcdi, buffer, data, data_size,
263 if (start != pfxsize) {
264 buffer[start] = '\0';
265 sfc_efx_log_mcdi(mcdi, "%s", buffer);
270 sfc_efx_mcdi_ev_proxy_response(void *arg, uint32_t handle, efx_rc_t result)
272 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
273 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
275 mcdi->proxy_handle = handle;
276 mcdi->proxy_result = result;
280 sfc_efx_mcdi_init(struct sfc_adapter *sa, struct sfc_efx_mcdi *mcdi,
281 uint32_t logtype, const char *log_prefix, efx_nic_t *nic,
282 const struct sfc_efx_mcdi_ops *ops, void *ops_cookie)
285 efx_mcdi_transport_t *emtp;
288 if (ops->dma_alloc == NULL || ops->dma_free == NULL ||
289 ops->sched_restart == NULL || ops->mgmt_evq_poll == NULL)
292 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_UNINITIALIZED);
294 rte_spinlock_init(&mcdi->lock);
297 mcdi->ops_cookie = ops_cookie;
300 mcdi->state = SFC_EFX_MCDI_INITIALIZED;
302 mcdi->logtype = logtype;
303 mcdi->log_prefix = log_prefix;
305 max_msg_size = sizeof(uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
306 rc = ops->dma_alloc(ops_cookie, "mcdi", max_msg_size, &mcdi->mem);
310 emtp = &mcdi->transport;
311 emtp->emt_context = sa;
312 emtp->emt_dma_mem = &mcdi->mem;
313 emtp->emt_execute = sfc_efx_mcdi_execute;
314 emtp->emt_ev_cpl = sfc_efx_mcdi_ev_cpl;
315 emtp->emt_exception = sfc_efx_mcdi_exception;
316 emtp->emt_logger = sfc_efx_mcdi_logger;
317 emtp->emt_ev_proxy_response = sfc_efx_mcdi_ev_proxy_response;
319 sfc_efx_mcdi_info(mcdi, "init MCDI");
320 rc = efx_mcdi_init(mcdi->nic, emtp);
327 memset(emtp, 0, sizeof(*emtp));
328 ops->dma_free(ops_cookie, &mcdi->mem);
331 mcdi->state = SFC_EFX_MCDI_UNINITIALIZED;
336 sfc_efx_mcdi_fini(struct sfc_efx_mcdi *mcdi)
338 efx_mcdi_transport_t *emtp;
340 emtp = &mcdi->transport;
342 rte_spinlock_lock(&mcdi->lock);
344 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED ||
345 mcdi->state == SFC_EFX_MCDI_DEAD);
346 mcdi->state = SFC_EFX_MCDI_UNINITIALIZED;
348 sfc_efx_mcdi_info(mcdi, "fini MCDI");
349 efx_mcdi_fini(mcdi->nic);
350 memset(emtp, 0, sizeof(*emtp));
352 rte_spinlock_unlock(&mcdi->lock);
354 mcdi->ops->dma_free(mcdi->ops_cookie, &mcdi->mem);
357 static sfc_efx_mcdi_dma_alloc_cb sfc_mcdi_dma_alloc;
359 sfc_mcdi_dma_alloc(void *cookie, const char *name, size_t len,
362 const struct sfc_adapter *sa = cookie;
364 return sfc_dma_alloc(sa, name, 0, len, sa->socket_id, esmp);
367 static sfc_efx_mcdi_dma_free_cb sfc_mcdi_dma_free;
369 sfc_mcdi_dma_free(void *cookie, efsys_mem_t *esmp)
371 const struct sfc_adapter *sa = cookie;
373 sfc_dma_free(sa, esmp);
376 static sfc_efx_mcdi_sched_restart_cb sfc_mcdi_sched_restart;
378 sfc_mcdi_sched_restart(void *cookie)
380 struct sfc_adapter *sa = cookie;
382 sfc_schedule_restart(sa);
385 static sfc_efx_mcdi_mgmt_evq_poll_cb sfc_mcdi_mgmt_evq_poll;
387 sfc_mcdi_mgmt_evq_poll(void *cookie)
389 struct sfc_adapter *sa = cookie;
391 sfc_ev_mgmt_qpoll(sa);
394 static const struct sfc_efx_mcdi_ops sfc_mcdi_ops = {
395 .dma_alloc = sfc_mcdi_dma_alloc,
396 .dma_free = sfc_mcdi_dma_free,
397 .sched_restart = sfc_mcdi_sched_restart,
398 .mgmt_evq_poll = sfc_mcdi_mgmt_evq_poll,
402 sfc_mcdi_init(struct sfc_adapter *sa)
406 sfc_log_init(sa, "entry");
408 logtype = sfc_register_logtype(&sa->priv.shared->pci_addr,
409 SFC_LOGTYPE_MCDI_STR,
412 return sfc_efx_mcdi_init(sa, &sa->mcdi, logtype,
413 sa->priv.shared->log_prefix, sa->nic,
418 sfc_mcdi_fini(struct sfc_adapter *sa)
420 sfc_log_init(sa, "entry");
421 sfc_efx_mcdi_fini(&sa->mcdi);