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 */
27 sfc_efx_mcdi_timeout(struct sfc_adapter *sa)
29 sfc_warn(sa, "MC TIMEOUT");
31 sfc_panic(sa, "MCDI timeout handling is not implemented\n");
34 static inline boolean_t
35 sfc_efx_mcdi_proxy_event_available(struct sfc_adapter *sa)
37 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
39 mcdi->proxy_handle = 0;
40 mcdi->proxy_result = ETIMEDOUT;
41 sfc_ev_mgmt_qpoll(sa);
42 if (mcdi->proxy_result != ETIMEDOUT)
49 sfc_efx_mcdi_poll(struct sfc_adapter *sa, boolean_t proxy)
52 unsigned int delay_total;
53 unsigned int delay_us;
54 boolean_t aborted __rte_unused;
57 delay_us = SFC_EFX_MCDI_POLL_INTERVAL_MIN_US;
61 boolean_t poll_completed;
63 poll_completed = (proxy) ? sfc_efx_mcdi_proxy_event_available(sa) :
64 efx_mcdi_request_poll(enp);
68 if (delay_total > SFC_EFX_MCDI_WATCHDOG_INTERVAL_US) {
70 aborted = efx_mcdi_request_abort(enp);
72 sfc_efx_mcdi_timeout(sa);
78 rte_delay_us(delay_us);
80 delay_total += delay_us;
82 /* Exponentially back off the poll frequency */
83 RTE_BUILD_BUG_ON(SFC_EFX_MCDI_POLL_INTERVAL_MAX_US >
86 if (delay_us > SFC_EFX_MCDI_POLL_INTERVAL_MAX_US)
87 delay_us = SFC_EFX_MCDI_POLL_INTERVAL_MAX_US;
93 sfc_efx_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
95 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
96 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
97 uint32_t proxy_handle;
99 rte_spinlock_lock(&mcdi->lock);
101 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
103 efx_mcdi_request_start(sa->nic, emrp, B_FALSE);
104 sfc_efx_mcdi_poll(sa, B_FALSE);
106 if (efx_mcdi_get_proxy_handle(sa->nic, emrp, &proxy_handle) == 0) {
108 * Authorization is required for the MCDI request;
109 * wait for an MCDI proxy response event to bring
110 * a non-zero proxy handle (should be the same as
111 * the value obtained above) and operation status
113 sfc_efx_mcdi_poll(sa, B_TRUE);
115 if ((mcdi->proxy_handle != 0) &&
116 (mcdi->proxy_handle != proxy_handle)) {
117 sfc_err(sa, "Unexpected MCDI proxy event");
118 emrp->emr_rc = EFAULT;
119 } else if (mcdi->proxy_result == 0) {
121 * Authorization succeeded; re-issue the original
122 * request and poll for an ordinary MCDI response
124 efx_mcdi_request_start(sa->nic, emrp, B_FALSE);
125 sfc_efx_mcdi_poll(sa, B_FALSE);
127 emrp->emr_rc = mcdi->proxy_result;
128 sfc_err(sa, "MCDI proxy authorization failed "
129 "(handle=%08x, result=%d)",
130 proxy_handle, mcdi->proxy_result);
134 rte_spinlock_unlock(&mcdi->lock);
138 sfc_efx_mcdi_ev_cpl(void *arg)
140 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
141 struct sfc_efx_mcdi *mcdi __rte_unused;
144 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
146 /* MCDI is polled, completions are not expected */
151 sfc_efx_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
153 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
155 sfc_warn(sa, "MC %s",
156 (eme == EFX_MCDI_EXCEPTION_MC_REBOOT) ? "REBOOT" :
157 (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT) ? "BADASSERT" : "UNKNOWN");
159 sfc_schedule_restart(sa);
162 #define SFC_MCDI_LOG_BUF_SIZE 128
165 sfc_efx_mcdi_do_log(const struct sfc_adapter *sa,
166 char *buffer, void *data, size_t data_size,
167 size_t pfxsize, size_t position)
169 uint32_t *words = data;
170 /* Space separator plus 2 characters per byte */
171 const size_t word_str_space = 1 + 2 * sizeof(*words);
174 for (i = 0; i < data_size; i += sizeof(*words)) {
175 if (position + word_str_space >=
176 SFC_MCDI_LOG_BUF_SIZE) {
177 /* Flush at SFC_MCDI_LOG_BUF_SIZE with backslash
178 * at the end which is required by netlogdecode.
180 buffer[position] = '\0';
181 sfc_log_mcdi(sa, "%s \\", buffer);
182 /* Preserve prefix for the next log message */
185 position += snprintf(buffer + position,
186 SFC_MCDI_LOG_BUF_SIZE - position,
194 sfc_efx_mcdi_logger(void *arg, efx_log_msg_t type,
195 void *header, size_t header_size,
196 void *data, size_t data_size)
198 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
199 char buffer[SFC_MCDI_LOG_BUF_SIZE];
204 * Unlike the other cases, MCDI logging implies more onerous work
205 * needed to produce a message. If the dynamic log level prevents
206 * the end result from being printed, the CPU time will be wasted.
208 * To avoid wasting time, the actual level is examined in advance.
210 if (rte_log_get_level(sa->mcdi.logtype) < (int)SFC_LOG_LEVEL_MCDI)
213 /* The format including prefix added by sfc_log_mcdi() is the format
214 * consumed by the Solarflare netlogdecode tool.
216 pfxsize = snprintf(buffer, sizeof(buffer), "MCDI RPC %s:",
217 type == EFX_LOG_MCDI_REQUEST ? "REQ" :
218 type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???");
219 start = sfc_efx_mcdi_do_log(sa, buffer, header, header_size,
221 start = sfc_efx_mcdi_do_log(sa, buffer, data, data_size,
223 if (start != pfxsize) {
224 buffer[start] = '\0';
225 sfc_log_mcdi(sa, "%s", buffer);
230 sfc_efx_mcdi_ev_proxy_response(void *arg, uint32_t handle, efx_rc_t result)
232 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
233 struct sfc_efx_mcdi *mcdi = &sa->mcdi;
235 mcdi->proxy_handle = handle;
236 mcdi->proxy_result = result;
240 sfc_efx_mcdi_init(struct sfc_adapter *sa)
242 struct sfc_efx_mcdi *mcdi;
244 efx_mcdi_transport_t *emtp;
247 sfc_log_init(sa, "entry");
251 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_UNINITIALIZED);
253 rte_spinlock_init(&mcdi->lock);
255 mcdi->state = SFC_EFX_MCDI_INITIALIZED;
257 max_msg_size = sizeof(uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
258 rc = sfc_dma_alloc(sa, "mcdi", 0, max_msg_size, sa->socket_id,
263 mcdi->logtype = sfc_register_logtype(&sa->priv.shared->pci_addr,
264 SFC_LOGTYPE_MCDI_STR,
267 emtp = &mcdi->transport;
268 emtp->emt_context = sa;
269 emtp->emt_dma_mem = &mcdi->mem;
270 emtp->emt_execute = sfc_efx_mcdi_execute;
271 emtp->emt_ev_cpl = sfc_efx_mcdi_ev_cpl;
272 emtp->emt_exception = sfc_efx_mcdi_exception;
273 emtp->emt_logger = sfc_efx_mcdi_logger;
274 emtp->emt_ev_proxy_response = sfc_efx_mcdi_ev_proxy_response;
276 sfc_log_init(sa, "init MCDI");
277 rc = efx_mcdi_init(sa->nic, emtp);
284 memset(emtp, 0, sizeof(*emtp));
285 sfc_dma_free(sa, &mcdi->mem);
288 mcdi->state = SFC_EFX_MCDI_UNINITIALIZED;
293 sfc_efx_mcdi_fini(struct sfc_adapter *sa)
295 struct sfc_efx_mcdi *mcdi;
296 efx_mcdi_transport_t *emtp;
298 sfc_log_init(sa, "entry");
301 emtp = &mcdi->transport;
303 rte_spinlock_lock(&mcdi->lock);
305 SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
306 mcdi->state = SFC_EFX_MCDI_UNINITIALIZED;
308 sfc_log_init(sa, "fini MCDI");
309 efx_mcdi_fini(sa->nic);
310 memset(emtp, 0, sizeof(*emtp));
312 rte_spinlock_unlock(&mcdi->lock);
314 sfc_dma_free(sa, &mcdi->mem);
318 sfc_mcdi_init(struct sfc_adapter *sa)
320 return sfc_efx_mcdi_init(sa);
324 sfc_mcdi_fini(struct sfc_adapter *sa)
326 sfc_efx_mcdi_fini(sa);