4 * Copyright (c) 2016-2017 Solarflare Communications Inc.
7 * This software was jointly developed between OKTET Labs (under contract
8 * for Solarflare) and Solarflare Communications, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <rte_cycles.h>
36 #include "efx_regs_mcdi.h"
40 #include "sfc_kvargs.h"
43 #define SFC_MCDI_POLL_INTERVAL_MIN_US 10 /* 10us in 1us units */
44 #define SFC_MCDI_POLL_INTERVAL_MAX_US (US_PER_S / 10) /* 100ms in 1us units */
45 #define SFC_MCDI_WATCHDOG_INTERVAL_US (10 * US_PER_S) /* 10s in 1us units */
48 sfc_mcdi_timeout(struct sfc_adapter *sa)
50 sfc_warn(sa, "MC TIMEOUT");
52 sfc_panic(sa, "MCDI timeout handling is not implemented\n");
55 static inline boolean_t
56 sfc_mcdi_proxy_event_available(struct sfc_adapter *sa)
58 struct sfc_mcdi *mcdi = &sa->mcdi;
60 mcdi->proxy_handle = 0;
61 mcdi->proxy_result = ETIMEDOUT;
62 sfc_ev_mgmt_qpoll(sa);
63 if (mcdi->proxy_result != ETIMEDOUT)
70 sfc_mcdi_poll(struct sfc_adapter *sa, boolean_t proxy)
73 unsigned int delay_total;
74 unsigned int delay_us;
75 boolean_t aborted __rte_unused;
78 delay_us = SFC_MCDI_POLL_INTERVAL_MIN_US;
82 boolean_t poll_completed;
84 poll_completed = (proxy) ? sfc_mcdi_proxy_event_available(sa) :
85 efx_mcdi_request_poll(enp);
89 if (delay_total > SFC_MCDI_WATCHDOG_INTERVAL_US) {
91 aborted = efx_mcdi_request_abort(enp);
99 rte_delay_us(delay_us);
101 delay_total += delay_us;
103 /* Exponentially back off the poll frequency */
104 RTE_BUILD_BUG_ON(SFC_MCDI_POLL_INTERVAL_MAX_US > UINT_MAX / 2);
106 if (delay_us > SFC_MCDI_POLL_INTERVAL_MAX_US)
107 delay_us = SFC_MCDI_POLL_INTERVAL_MAX_US;
113 sfc_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
115 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
116 struct sfc_mcdi *mcdi = &sa->mcdi;
117 uint32_t proxy_handle;
119 rte_spinlock_lock(&mcdi->lock);
121 SFC_ASSERT(mcdi->state == SFC_MCDI_INITIALIZED);
123 efx_mcdi_request_start(sa->nic, emrp, B_FALSE);
124 sfc_mcdi_poll(sa, B_FALSE);
126 if (efx_mcdi_get_proxy_handle(sa->nic, emrp, &proxy_handle) == 0) {
128 * Authorization is required for the MCDI request;
129 * wait for an MCDI proxy response event to bring
130 * a non-zero proxy handle (should be the same as
131 * the value obtained above) and operation status
133 sfc_mcdi_poll(sa, B_TRUE);
135 if ((mcdi->proxy_handle != 0) &&
136 (mcdi->proxy_handle != proxy_handle)) {
137 sfc_err(sa, "Unexpected MCDI proxy event");
138 emrp->emr_rc = EFAULT;
139 } else if (mcdi->proxy_result == 0) {
141 * Authorization succeeded; re-issue the original
142 * request and poll for an ordinary MCDI response
144 efx_mcdi_request_start(sa->nic, emrp, B_FALSE);
145 sfc_mcdi_poll(sa, B_FALSE);
147 emrp->emr_rc = mcdi->proxy_result;
148 sfc_err(sa, "MCDI proxy authorization failed "
149 "(handle=%08x, result=%d)",
150 proxy_handle, mcdi->proxy_result);
154 rte_spinlock_unlock(&mcdi->lock);
158 sfc_mcdi_ev_cpl(void *arg)
160 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
161 struct sfc_mcdi *mcdi __rte_unused;
164 SFC_ASSERT(mcdi->state == SFC_MCDI_INITIALIZED);
166 /* MCDI is polled, completions are not expected */
171 sfc_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
173 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
175 sfc_warn(sa, "MC %s",
176 (eme == EFX_MCDI_EXCEPTION_MC_REBOOT) ? "REBOOT" :
177 (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT) ? "BADASSERT" : "UNKNOWN");
179 sfc_panic(sa, "MCDI exceptions handling is not implemented\n");
182 #define SFC_MCDI_LOG_BUF_SIZE 128
185 sfc_mcdi_do_log(const struct sfc_adapter *sa,
186 char *buffer, void *data, size_t data_size,
187 size_t pfxsize, size_t position)
189 uint32_t *words = data;
190 /* Space separator plus 2 characters per byte */
191 const size_t word_str_space = 1 + 2 * sizeof(*words);
194 for (i = 0; i < data_size; i += sizeof(*words)) {
195 if (position + word_str_space >=
196 SFC_MCDI_LOG_BUF_SIZE) {
197 /* Flush at SFC_MCDI_LOG_BUF_SIZE with backslash
198 * at the end which is required by netlogdecode.
200 buffer[position] = '\0';
201 sfc_info(sa, "%s \\", buffer);
202 /* Preserve prefix for the next log message */
205 position += snprintf(buffer + position,
206 SFC_MCDI_LOG_BUF_SIZE - position,
214 sfc_mcdi_logger(void *arg, efx_log_msg_t type,
215 void *header, size_t header_size,
216 void *data, size_t data_size)
218 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
219 char buffer[SFC_MCDI_LOG_BUF_SIZE];
223 if (!sa->mcdi.logging)
226 /* The format including prefix added by sfc_info() is the format
227 * consumed by the Solarflare netlogdecode tool.
229 pfxsize = snprintf(buffer, sizeof(buffer), "MCDI RPC %s:",
230 type == EFX_LOG_MCDI_REQUEST ? "REQ" :
231 type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???");
232 start = sfc_mcdi_do_log(sa, buffer, header, header_size,
234 start = sfc_mcdi_do_log(sa, buffer, data, data_size, pfxsize, start);
235 if (start != pfxsize) {
236 buffer[start] = '\0';
237 sfc_info(sa, "%s", buffer);
242 sfc_mcdi_ev_proxy_response(void *arg, uint32_t handle, efx_rc_t result)
244 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
245 struct sfc_mcdi *mcdi = &sa->mcdi;
247 mcdi->proxy_handle = handle;
248 mcdi->proxy_result = result;
252 sfc_mcdi_init(struct sfc_adapter *sa)
254 struct sfc_mcdi *mcdi;
256 efx_mcdi_transport_t *emtp;
259 sfc_log_init(sa, "entry");
263 SFC_ASSERT(mcdi->state == SFC_MCDI_UNINITIALIZED);
265 rte_spinlock_init(&mcdi->lock);
267 mcdi->state = SFC_MCDI_INITIALIZED;
269 max_msg_size = sizeof(uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
270 rc = sfc_dma_alloc(sa, "mcdi", 0, max_msg_size, sa->socket_id,
275 /* Convert negative error to positive used in the driver */
276 rc = sfc_kvargs_process(sa, SFC_KVARG_MCDI_LOGGING,
277 sfc_kvarg_bool_handler, &mcdi->logging);
279 goto fail_kvargs_process;
281 emtp = &mcdi->transport;
282 emtp->emt_context = sa;
283 emtp->emt_dma_mem = &mcdi->mem;
284 emtp->emt_execute = sfc_mcdi_execute;
285 emtp->emt_ev_cpl = sfc_mcdi_ev_cpl;
286 emtp->emt_exception = sfc_mcdi_exception;
287 emtp->emt_logger = sfc_mcdi_logger;
288 emtp->emt_ev_proxy_response = sfc_mcdi_ev_proxy_response;
290 sfc_log_init(sa, "init MCDI");
291 rc = efx_mcdi_init(sa->nic, emtp);
298 memset(emtp, 0, sizeof(*emtp));
301 sfc_dma_free(sa, &mcdi->mem);
304 mcdi->state = SFC_MCDI_UNINITIALIZED;
309 sfc_mcdi_fini(struct sfc_adapter *sa)
311 struct sfc_mcdi *mcdi;
312 efx_mcdi_transport_t *emtp;
314 sfc_log_init(sa, "entry");
317 emtp = &mcdi->transport;
319 rte_spinlock_lock(&mcdi->lock);
321 SFC_ASSERT(mcdi->state == SFC_MCDI_INITIALIZED);
322 mcdi->state = SFC_MCDI_UNINITIALIZED;
324 sfc_log_init(sa, "fini MCDI");
325 efx_mcdi_fini(sa->nic);
326 memset(emtp, 0, sizeof(*emtp));
328 rte_spinlock_unlock(&mcdi->lock);
330 sfc_dma_free(sa, &mcdi->mem);