1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2016-2018 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"
20 #define SFC_MCDI_POLL_INTERVAL_MIN_US 10 /* 10us in 1us units */
21 #define SFC_MCDI_POLL_INTERVAL_MAX_US (US_PER_S / 10) /* 100ms in 1us units */
22 #define SFC_MCDI_WATCHDOG_INTERVAL_US (10 * US_PER_S) /* 10s in 1us units */
25 sfc_mcdi_timeout(struct sfc_adapter *sa)
27 sfc_warn(sa, "MC TIMEOUT");
29 sfc_panic(sa, "MCDI timeout handling is not implemented\n");
32 static inline boolean_t
33 sfc_mcdi_proxy_event_available(struct sfc_adapter *sa)
35 struct sfc_mcdi *mcdi = &sa->mcdi;
37 mcdi->proxy_handle = 0;
38 mcdi->proxy_result = ETIMEDOUT;
39 sfc_ev_mgmt_qpoll(sa);
40 if (mcdi->proxy_result != ETIMEDOUT)
47 sfc_mcdi_poll(struct sfc_adapter *sa, boolean_t proxy)
50 unsigned int delay_total;
51 unsigned int delay_us;
52 boolean_t aborted __rte_unused;
55 delay_us = SFC_MCDI_POLL_INTERVAL_MIN_US;
59 boolean_t poll_completed;
61 poll_completed = (proxy) ? sfc_mcdi_proxy_event_available(sa) :
62 efx_mcdi_request_poll(enp);
66 if (delay_total > SFC_MCDI_WATCHDOG_INTERVAL_US) {
68 aborted = efx_mcdi_request_abort(enp);
76 rte_delay_us(delay_us);
78 delay_total += delay_us;
80 /* Exponentially back off the poll frequency */
81 RTE_BUILD_BUG_ON(SFC_MCDI_POLL_INTERVAL_MAX_US > UINT_MAX / 2);
83 if (delay_us > SFC_MCDI_POLL_INTERVAL_MAX_US)
84 delay_us = SFC_MCDI_POLL_INTERVAL_MAX_US;
90 sfc_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
92 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
93 struct sfc_mcdi *mcdi = &sa->mcdi;
94 uint32_t proxy_handle;
96 rte_spinlock_lock(&mcdi->lock);
98 SFC_ASSERT(mcdi->state == SFC_MCDI_INITIALIZED);
100 efx_mcdi_request_start(sa->nic, emrp, B_FALSE);
101 sfc_mcdi_poll(sa, B_FALSE);
103 if (efx_mcdi_get_proxy_handle(sa->nic, emrp, &proxy_handle) == 0) {
105 * Authorization is required for the MCDI request;
106 * wait for an MCDI proxy response event to bring
107 * a non-zero proxy handle (should be the same as
108 * the value obtained above) and operation status
110 sfc_mcdi_poll(sa, B_TRUE);
112 if ((mcdi->proxy_handle != 0) &&
113 (mcdi->proxy_handle != proxy_handle)) {
114 sfc_err(sa, "Unexpected MCDI proxy event");
115 emrp->emr_rc = EFAULT;
116 } else if (mcdi->proxy_result == 0) {
118 * Authorization succeeded; re-issue the original
119 * request and poll for an ordinary MCDI response
121 efx_mcdi_request_start(sa->nic, emrp, B_FALSE);
122 sfc_mcdi_poll(sa, B_FALSE);
124 emrp->emr_rc = mcdi->proxy_result;
125 sfc_err(sa, "MCDI proxy authorization failed "
126 "(handle=%08x, result=%d)",
127 proxy_handle, mcdi->proxy_result);
131 rte_spinlock_unlock(&mcdi->lock);
135 sfc_mcdi_ev_cpl(void *arg)
137 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
138 struct sfc_mcdi *mcdi __rte_unused;
141 SFC_ASSERT(mcdi->state == SFC_MCDI_INITIALIZED);
143 /* MCDI is polled, completions are not expected */
148 sfc_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
150 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
152 sfc_warn(sa, "MC %s",
153 (eme == EFX_MCDI_EXCEPTION_MC_REBOOT) ? "REBOOT" :
154 (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT) ? "BADASSERT" : "UNKNOWN");
156 sfc_schedule_restart(sa);
159 #define SFC_MCDI_LOG_BUF_SIZE 128
162 sfc_mcdi_do_log(const struct sfc_adapter *sa,
163 char *buffer, void *data, size_t data_size,
164 size_t pfxsize, size_t position)
166 uint32_t *words = data;
167 /* Space separator plus 2 characters per byte */
168 const size_t word_str_space = 1 + 2 * sizeof(*words);
171 for (i = 0; i < data_size; i += sizeof(*words)) {
172 if (position + word_str_space >=
173 SFC_MCDI_LOG_BUF_SIZE) {
174 /* Flush at SFC_MCDI_LOG_BUF_SIZE with backslash
175 * at the end which is required by netlogdecode.
177 buffer[position] = '\0';
178 sfc_log_mcdi(sa, "%s \\", buffer);
179 /* Preserve prefix for the next log message */
182 position += snprintf(buffer + position,
183 SFC_MCDI_LOG_BUF_SIZE - position,
191 sfc_mcdi_logger(void *arg, efx_log_msg_t type,
192 void *header, size_t header_size,
193 void *data, size_t data_size)
195 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
196 char buffer[SFC_MCDI_LOG_BUF_SIZE];
201 * Unlike the other cases, MCDI logging implies more onerous work
202 * needed to produce a message. If the dynamic log level prevents
203 * the end result from being printed, the CPU time will be wasted.
205 * To avoid wasting time, the actual level is examined in advance.
207 if (rte_log_get_level(sa->mcdi.logtype) < (int)SFC_LOG_LEVEL_MCDI)
210 /* The format including prefix added by sfc_log_mcdi() is the format
211 * consumed by the Solarflare netlogdecode tool.
213 pfxsize = snprintf(buffer, sizeof(buffer), "MCDI RPC %s:",
214 type == EFX_LOG_MCDI_REQUEST ? "REQ" :
215 type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???");
216 start = sfc_mcdi_do_log(sa, buffer, header, header_size,
218 start = sfc_mcdi_do_log(sa, buffer, data, data_size, pfxsize, start);
219 if (start != pfxsize) {
220 buffer[start] = '\0';
221 sfc_log_mcdi(sa, "%s", buffer);
226 sfc_mcdi_ev_proxy_response(void *arg, uint32_t handle, efx_rc_t result)
228 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
229 struct sfc_mcdi *mcdi = &sa->mcdi;
231 mcdi->proxy_handle = handle;
232 mcdi->proxy_result = result;
236 sfc_mcdi_init(struct sfc_adapter *sa)
238 struct sfc_mcdi *mcdi;
240 efx_mcdi_transport_t *emtp;
243 sfc_log_init(sa, "entry");
247 SFC_ASSERT(mcdi->state == SFC_MCDI_UNINITIALIZED);
249 rte_spinlock_init(&mcdi->lock);
251 mcdi->state = SFC_MCDI_INITIALIZED;
253 max_msg_size = sizeof(uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
254 rc = sfc_dma_alloc(sa, "mcdi", 0, max_msg_size, sa->socket_id,
259 mcdi->logtype = sfc_register_logtype(sa, SFC_LOGTYPE_MCDI_STR,
262 emtp = &mcdi->transport;
263 emtp->emt_context = sa;
264 emtp->emt_dma_mem = &mcdi->mem;
265 emtp->emt_execute = sfc_mcdi_execute;
266 emtp->emt_ev_cpl = sfc_mcdi_ev_cpl;
267 emtp->emt_exception = sfc_mcdi_exception;
268 emtp->emt_logger = sfc_mcdi_logger;
269 emtp->emt_ev_proxy_response = sfc_mcdi_ev_proxy_response;
271 sfc_log_init(sa, "init MCDI");
272 rc = efx_mcdi_init(sa->nic, emtp);
279 memset(emtp, 0, sizeof(*emtp));
280 sfc_dma_free(sa, &mcdi->mem);
283 mcdi->state = SFC_MCDI_UNINITIALIZED;
288 sfc_mcdi_fini(struct sfc_adapter *sa)
290 struct sfc_mcdi *mcdi;
291 efx_mcdi_transport_t *emtp;
293 sfc_log_init(sa, "entry");
296 emtp = &mcdi->transport;
298 rte_spinlock_lock(&mcdi->lock);
300 SFC_ASSERT(mcdi->state == SFC_MCDI_INITIALIZED);
301 mcdi->state = SFC_MCDI_UNINITIALIZED;
303 sfc_log_init(sa, "fini MCDI");
304 efx_mcdi_fini(sa->nic);
305 memset(emtp, 0, sizeof(*emtp));
307 rte_spinlock_unlock(&mcdi->lock);
309 sfc_dma_free(sa, &mcdi->mem);