1 // SPDX-License-Identifier: GPL-2.0
3 * Management-Controller-to-Driver Interface
5 * Copyright 2008-2013 Solarflare Communications Inc.
6 * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
8 #include <linux/delay.h>
9 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/ethtool.h>
15 #include <linux/if_vlan.h>
16 #include <linux/timer.h>
17 #include <linux/list.h>
18 #include <linux/pci.h>
19 #include <linux/device.h>
20 #include <linux/rwsem.h>
21 #include <linux/vmalloc.h>
22 #include <net/netevent.h>
23 #include <linux/log2.h>
24 #include <linux/net_tstamp.h>
25 #include <linux/wait.h>
30 struct cdx_mcdi_copy_buffer {
31 struct cdx_dword buffer[DIV_ROUND_UP(MCDI_CTL_SDU_LEN_MAX, 4)];
34 #ifdef CONFIG_MCDI_LOGGING
35 #define LOG_LINE_MAX (1024 - 32)
38 static void cdx_mcdi_cancel_cmd(struct cdx_mcdi *cdx, struct cdx_mcdi_cmd *cmd);
39 static void cdx_mcdi_wait_for_cleanup(struct cdx_mcdi *cdx);
40 static int cdx_mcdi_rpc_async_internal(struct cdx_mcdi *cdx,
41 struct cdx_mcdi_cmd *cmd,
42 unsigned int *handle);
43 static void cdx_mcdi_start_or_queue(struct cdx_mcdi_iface *mcdi,
45 static void cdx_mcdi_cmd_start_or_queue(struct cdx_mcdi_iface *mcdi,
46 struct cdx_mcdi_cmd *cmd);
47 static bool cdx_mcdi_complete_cmd(struct cdx_mcdi_iface *mcdi,
48 struct cdx_mcdi_cmd *cmd,
49 struct cdx_dword *outbuf,
51 struct list_head *cleanup_list);
52 static void cdx_mcdi_timeout_cmd(struct cdx_mcdi_iface *mcdi,
53 struct cdx_mcdi_cmd *cmd,
54 struct list_head *cleanup_list);
55 static void cdx_mcdi_cmd_work(struct work_struct *context);
56 static void cdx_mcdi_mode_fail(struct cdx_mcdi *cdx, struct list_head *cleanup_list);
57 static void _cdx_mcdi_display_error(struct cdx_mcdi *cdx, unsigned int cmd,
58 size_t inlen, int raw, int arg, int err_no);
60 static bool cdx_cmd_cancelled(struct cdx_mcdi_cmd *cmd)
62 return cmd->state == MCDI_STATE_RUNNING_CANCELLED;
65 static void cdx_mcdi_cmd_release(struct kref *ref)
67 kfree(container_of(ref, struct cdx_mcdi_cmd, ref));
70 static unsigned int cdx_mcdi_cmd_handle(struct cdx_mcdi_cmd *cmd)
75 static void _cdx_mcdi_remove_cmd(struct cdx_mcdi_iface *mcdi,
76 struct cdx_mcdi_cmd *cmd,
77 struct list_head *cleanup_list)
79 /* if cancelled, the completers have already been called */
80 if (cdx_cmd_cancelled(cmd))
84 list_add_tail(&cmd->cleanup_list, cleanup_list);
85 ++mcdi->outstanding_cleanups;
90 static void cdx_mcdi_remove_cmd(struct cdx_mcdi_iface *mcdi,
91 struct cdx_mcdi_cmd *cmd,
92 struct list_head *cleanup_list)
95 _cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
96 cmd->state = MCDI_STATE_FINISHED;
97 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
98 if (list_empty(&mcdi->cmd_list))
99 wake_up(&mcdi->cmd_complete_wq);
102 static unsigned long cdx_mcdi_rpc_timeout(struct cdx_mcdi *cdx, unsigned int cmd)
104 if (!cdx->mcdi_ops->mcdi_rpc_timeout)
105 return MCDI_RPC_TIMEOUT;
107 return cdx->mcdi_ops->mcdi_rpc_timeout(cdx, cmd);
110 int cdx_mcdi_init(struct cdx_mcdi *cdx)
112 struct cdx_mcdi_iface *mcdi;
115 cdx->mcdi = kzalloc(sizeof(*cdx->mcdi), GFP_KERNEL);
119 mcdi = cdx_mcdi_if(cdx);
122 #ifdef CONFIG_MCDI_LOGGING
123 mcdi->logging_buffer = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
124 if (!mcdi->logging_buffer)
127 mcdi->workqueue = alloc_ordered_workqueue("mcdi_wq", 0);
128 if (!mcdi->workqueue)
130 mutex_init(&mcdi->iface_lock);
131 mcdi->mode = MCDI_MODE_EVENTS;
132 INIT_LIST_HEAD(&mcdi->cmd_list);
133 init_waitqueue_head(&mcdi->cmd_complete_wq);
135 mcdi->new_epoch = true;
139 #ifdef CONFIG_MCDI_LOGGING
140 kfree(mcdi->logging_buffer);
149 void cdx_mcdi_finish(struct cdx_mcdi *cdx)
151 struct cdx_mcdi_iface *mcdi;
153 mcdi = cdx_mcdi_if(cdx);
157 cdx_mcdi_wait_for_cleanup(cdx);
159 #ifdef CONFIG_MCDI_LOGGING
160 kfree(mcdi->logging_buffer);
163 destroy_workqueue(mcdi->workqueue);
168 static bool cdx_mcdi_flushed(struct cdx_mcdi_iface *mcdi, bool ignore_cleanups)
172 mutex_lock(&mcdi->iface_lock);
173 flushed = list_empty(&mcdi->cmd_list) &&
174 (ignore_cleanups || !mcdi->outstanding_cleanups);
175 mutex_unlock(&mcdi->iface_lock);
179 /* Wait for outstanding MCDI commands to complete. */
180 static void cdx_mcdi_wait_for_cleanup(struct cdx_mcdi *cdx)
182 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
187 wait_event(mcdi->cmd_complete_wq,
188 cdx_mcdi_flushed(mcdi, false));
191 int cdx_mcdi_wait_for_quiescence(struct cdx_mcdi *cdx,
192 unsigned int timeout_jiffies)
194 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
195 DEFINE_WAIT_FUNC(wait, woken_wake_function);
201 flush_workqueue(mcdi->workqueue);
203 add_wait_queue(&mcdi->cmd_complete_wq, &wait);
205 while (!cdx_mcdi_flushed(mcdi, true)) {
206 rc = wait_woken(&wait, TASK_IDLE, timeout_jiffies);
212 remove_wait_queue(&mcdi->cmd_complete_wq, &wait);
222 static u8 cdx_mcdi_payload_csum(const struct cdx_dword *hdr, size_t hdr_len,
223 const struct cdx_dword *sdu, size_t sdu_len)
229 for (i = 0; i < hdr_len; i++)
233 for (i = 0; i < sdu_len; i++)
239 static void cdx_mcdi_send_request(struct cdx_mcdi *cdx,
240 struct cdx_mcdi_cmd *cmd)
242 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
243 const struct cdx_dword *inbuf = cmd->inbuf;
244 size_t inlen = cmd->inlen;
245 struct cdx_dword hdr[2];
249 #ifdef CONFIG_MCDI_LOGGING
255 #ifdef CONFIG_MCDI_LOGGING
256 buf = mcdi->logging_buffer; /* page-sized */
259 mcdi->prev_seq = cmd->seq;
260 mcdi->seq_held_by[cmd->seq] = cmd;
261 mcdi->db_held_by = cmd;
262 cmd->started = jiffies;
264 not_epoch = !mcdi->new_epoch;
268 WARN_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2);
269 CDX_POPULATE_DWORD_7(hdr[0],
270 MCDI_HEADER_RESPONSE, 0,
271 MCDI_HEADER_RESYNC, 1,
272 MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
273 MCDI_HEADER_DATALEN, 0,
274 MCDI_HEADER_SEQ, cmd->seq,
275 MCDI_HEADER_XFLAGS, xflags,
276 MCDI_HEADER_NOT_EPOCH, not_epoch);
277 CDX_POPULATE_DWORD_3(hdr[1],
278 MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd->cmd,
279 MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen,
280 MC_CMD_V2_EXTN_IN_MESSAGE_TYPE,
281 MC_CMD_V2_EXTN_IN_MCDI_MESSAGE_TYPE_PLATFORM);
284 #ifdef CONFIG_MCDI_LOGGING
285 if (!WARN_ON_ONCE(!buf)) {
286 const struct cdx_dword *frags[] = { hdr, inbuf };
287 const size_t frag_len[] = { hdr_len, round_up(inlen, 4) };
291 for (j = 0; j < ARRAY_SIZE(frags); j++) {
292 const struct cdx_dword *frag;
299 * Do not exceed the internal printk limit.
300 * The string before that is just over 70 bytes.
302 if ((bytes + 75) > LOG_LINE_MAX) {
303 pr_info("MCDI RPC REQ:%s \\\n", buf);
306 bytes += snprintf(buf + bytes,
307 LOG_LINE_MAX - bytes, " %08x",
308 le32_to_cpu(frag[i].cdx_u32));
312 pr_info("MCDI RPC REQ:%s\n", buf);
315 hdr[0].cdx_u32 |= (__force __le32)(cdx_mcdi_payload_csum(hdr, hdr_len, inbuf, inlen) <<
316 MCDI_HEADER_XFLAGS_LBN);
317 cdx->mcdi_ops->mcdi_request(cdx, hdr, hdr_len, inbuf, inlen);
319 mcdi->new_epoch = false;
322 static int cdx_mcdi_errno(struct cdx_mcdi *cdx, unsigned int mcdi_err)
326 case MC_CMD_ERR_QUEUE_FULL:
328 case MC_CMD_ERR_EPERM:
330 case MC_CMD_ERR_ENOENT:
332 case MC_CMD_ERR_EINTR:
334 case MC_CMD_ERR_EAGAIN:
336 case MC_CMD_ERR_EACCES:
338 case MC_CMD_ERR_EBUSY:
340 case MC_CMD_ERR_EINVAL:
342 case MC_CMD_ERR_ERANGE:
344 case MC_CMD_ERR_EDEADLK:
346 case MC_CMD_ERR_ENOSYS:
348 case MC_CMD_ERR_ETIME:
350 case MC_CMD_ERR_EALREADY:
352 case MC_CMD_ERR_ENOSPC:
354 case MC_CMD_ERR_ENOMEM:
356 case MC_CMD_ERR_ENOTSUP:
358 case MC_CMD_ERR_ALLOC_FAIL:
360 case MC_CMD_ERR_MAC_EXIST:
362 case MC_CMD_ERR_NO_EVB_PORT:
369 static void cdx_mcdi_process_cleanup_list(struct cdx_mcdi *cdx,
370 struct list_head *cleanup_list)
372 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
373 unsigned int cleanups = 0;
378 while (!list_empty(cleanup_list)) {
379 struct cdx_mcdi_cmd *cmd =
380 list_first_entry(cleanup_list,
381 struct cdx_mcdi_cmd, cleanup_list);
382 cmd->completer(cdx, cmd->cookie, cmd->rc,
383 cmd->outbuf, cmd->outlen);
384 list_del(&cmd->cleanup_list);
385 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
392 mutex_lock(&mcdi->iface_lock);
393 CDX_WARN_ON_PARANOID(cleanups > mcdi->outstanding_cleanups);
394 all_done = (mcdi->outstanding_cleanups -= cleanups) == 0;
395 mutex_unlock(&mcdi->iface_lock);
397 wake_up(&mcdi->cmd_complete_wq);
401 static void _cdx_mcdi_cancel_cmd(struct cdx_mcdi_iface *mcdi,
403 struct list_head *cleanup_list)
405 struct cdx_mcdi_cmd *cmd;
407 list_for_each_entry(cmd, &mcdi->cmd_list, list)
408 if (cdx_mcdi_cmd_handle(cmd) == handle) {
409 switch (cmd->state) {
410 case MCDI_STATE_QUEUED:
411 case MCDI_STATE_RETRY:
412 pr_debug("command %#x inlen %zu cancelled in queue\n",
413 cmd->cmd, cmd->inlen);
414 /* if not yet running, properly cancel it */
416 cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
418 case MCDI_STATE_RUNNING:
419 case MCDI_STATE_RUNNING_CANCELLED:
420 case MCDI_STATE_FINISHED:
429 static void cdx_mcdi_cancel_cmd(struct cdx_mcdi *cdx, struct cdx_mcdi_cmd *cmd)
431 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
432 LIST_HEAD(cleanup_list);
437 mutex_lock(&mcdi->iface_lock);
438 cdx_mcdi_timeout_cmd(mcdi, cmd, &cleanup_list);
439 mutex_unlock(&mcdi->iface_lock);
440 cdx_mcdi_process_cleanup_list(cdx, &cleanup_list);
443 struct cdx_mcdi_blocking_data {
446 wait_queue_head_t wq;
448 struct cdx_dword *outbuf;
450 size_t outlen_actual;
453 static void cdx_mcdi_blocking_data_release(struct kref *ref)
455 kfree(container_of(ref, struct cdx_mcdi_blocking_data, ref));
458 static void cdx_mcdi_rpc_completer(struct cdx_mcdi *cdx, unsigned long cookie,
459 int rc, struct cdx_dword *outbuf,
460 size_t outlen_actual)
462 struct cdx_mcdi_blocking_data *wait_data =
463 (struct cdx_mcdi_blocking_data *)cookie;
466 memcpy(wait_data->outbuf, outbuf,
467 min(outlen_actual, wait_data->outlen));
468 wait_data->outlen_actual = outlen_actual;
471 wait_data->done = true;
472 wake_up(&wait_data->wq);
473 kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release);
476 static int cdx_mcdi_rpc_sync(struct cdx_mcdi *cdx, unsigned int cmd,
477 const struct cdx_dword *inbuf, size_t inlen,
478 struct cdx_dword *outbuf, size_t outlen,
479 size_t *outlen_actual, bool quiet)
481 struct cdx_mcdi_blocking_data *wait_data;
482 struct cdx_mcdi_cmd *cmd_item;
489 wait_data = kmalloc(sizeof(*wait_data), GFP_KERNEL);
493 cmd_item = kmalloc(sizeof(*cmd_item), GFP_KERNEL);
499 kref_init(&wait_data->ref);
500 wait_data->done = false;
501 init_waitqueue_head(&wait_data->wq);
502 wait_data->outbuf = outbuf;
503 wait_data->outlen = outlen;
505 kref_init(&cmd_item->ref);
506 cmd_item->quiet = quiet;
507 cmd_item->cookie = (unsigned long)wait_data;
508 cmd_item->completer = &cdx_mcdi_rpc_completer;
510 cmd_item->inlen = inlen;
511 cmd_item->inbuf = inbuf;
513 /* Claim an extra reference for the completer to put. */
514 kref_get(&wait_data->ref);
515 rc = cdx_mcdi_rpc_async_internal(cdx, cmd_item, &handle);
517 kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release);
521 if (!wait_event_timeout(wait_data->wq, wait_data->done,
522 cdx_mcdi_rpc_timeout(cdx, cmd)) &&
524 pr_err("MC command 0x%x inlen %zu timed out (sync)\n",
527 cdx_mcdi_cancel_cmd(cdx, cmd_item);
529 wait_data->rc = -ETIMEDOUT;
530 wait_data->outlen_actual = 0;
534 *outlen_actual = wait_data->outlen_actual;
538 kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release);
543 static bool cdx_mcdi_get_seq(struct cdx_mcdi_iface *mcdi, unsigned char *seq)
545 *seq = mcdi->prev_seq;
547 *seq = (*seq + 1) % ARRAY_SIZE(mcdi->seq_held_by);
548 } while (mcdi->seq_held_by[*seq] && *seq != mcdi->prev_seq);
549 return !mcdi->seq_held_by[*seq];
552 static int cdx_mcdi_rpc_async_internal(struct cdx_mcdi *cdx,
553 struct cdx_mcdi_cmd *cmd,
554 unsigned int *handle)
556 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
557 LIST_HEAD(cleanup_list);
560 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
564 if (mcdi->mode == MCDI_MODE_FAIL) {
565 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
570 INIT_WORK(&cmd->work, cdx_mcdi_cmd_work);
571 INIT_LIST_HEAD(&cmd->list);
572 INIT_LIST_HEAD(&cmd->cleanup_list);
577 queue_work(mcdi->workqueue, &cmd->work);
581 static void cdx_mcdi_cmd_start_or_queue(struct cdx_mcdi_iface *mcdi,
582 struct cdx_mcdi_cmd *cmd)
584 struct cdx_mcdi *cdx = mcdi->cdx;
587 if (!mcdi->db_held_by &&
588 cdx_mcdi_get_seq(mcdi, &seq)) {
590 cmd->reboot_seen = false;
591 cdx_mcdi_send_request(cdx, cmd);
592 cmd->state = MCDI_STATE_RUNNING;
594 cmd->state = MCDI_STATE_QUEUED;
598 /* try to advance other commands */
599 static void cdx_mcdi_start_or_queue(struct cdx_mcdi_iface *mcdi,
602 struct cdx_mcdi_cmd *cmd, *tmp;
604 list_for_each_entry_safe(cmd, tmp, &mcdi->cmd_list, list)
605 if (cmd->state == MCDI_STATE_QUEUED ||
606 (cmd->state == MCDI_STATE_RETRY && allow_retry))
607 cdx_mcdi_cmd_start_or_queue(mcdi, cmd);
610 void cdx_mcdi_process_cmd(struct cdx_mcdi *cdx, struct cdx_dword *outbuf, int len)
612 struct cdx_mcdi_iface *mcdi;
613 struct cdx_mcdi_cmd *cmd;
614 LIST_HEAD(cleanup_list);
615 unsigned int respseq;
617 if (!len || !outbuf) {
618 pr_err("Got empty MC response\n");
622 mcdi = cdx_mcdi_if(cdx);
626 respseq = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_SEQ);
628 mutex_lock(&mcdi->iface_lock);
629 cmd = mcdi->seq_held_by[respseq];
632 if (cmd->state == MCDI_STATE_FINISHED) {
633 mutex_unlock(&mcdi->iface_lock);
634 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
638 cdx_mcdi_complete_cmd(mcdi, cmd, outbuf, len, &cleanup_list);
640 pr_err("MC response unexpected for seq : %0X\n", respseq);
643 mutex_unlock(&mcdi->iface_lock);
645 cdx_mcdi_process_cleanup_list(mcdi->cdx, &cleanup_list);
648 static void cdx_mcdi_cmd_work(struct work_struct *context)
650 struct cdx_mcdi_cmd *cmd =
651 container_of(context, struct cdx_mcdi_cmd, work);
652 struct cdx_mcdi_iface *mcdi = cmd->mcdi;
654 mutex_lock(&mcdi->iface_lock);
656 cmd->handle = mcdi->prev_handle++;
657 list_add_tail(&cmd->list, &mcdi->cmd_list);
658 cdx_mcdi_cmd_start_or_queue(mcdi, cmd);
660 mutex_unlock(&mcdi->iface_lock);
664 * Returns true if the MCDI module is finished with the command.
665 * (examples of false would be if the command was proxied, or it was
666 * rejected by the MC due to lack of resources and requeued).
668 static bool cdx_mcdi_complete_cmd(struct cdx_mcdi_iface *mcdi,
669 struct cdx_mcdi_cmd *cmd,
670 struct cdx_dword *outbuf,
672 struct list_head *cleanup_list)
674 size_t resp_hdr_len, resp_data_len;
675 struct cdx_mcdi *cdx = mcdi->cdx;
676 unsigned int respcmd, error;
677 bool completed = false;
680 /* ensure the command can't go away before this function returns */
683 respcmd = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_CODE);
684 error = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_ERROR);
686 if (respcmd != MC_CMD_V2_EXTN) {
688 resp_data_len = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_DATALEN);
694 CDX_DWORD_FIELD(outbuf[1], MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
697 if ((resp_hdr_len + resp_data_len) > len) {
698 pr_warn("Incomplete MCDI response received %d. Expected %zu\n",
699 len, (resp_hdr_len + resp_data_len));
703 #ifdef CONFIG_MCDI_LOGGING
704 if (!WARN_ON_ONCE(!mcdi->logging_buffer)) {
705 char *log = mcdi->logging_buffer;
709 WARN_ON_ONCE(resp_hdr_len % 4);
711 rlen = resp_hdr_len / 4 + DIV_ROUND_UP(resp_data_len, 4);
713 for (i = 0; i < rlen; i++) {
714 if ((bytes + 75) > LOG_LINE_MAX) {
715 pr_info("MCDI RPC RESP:%s \\\n", log);
718 bytes += snprintf(log + bytes, LOG_LINE_MAX - bytes,
719 " %08x", le32_to_cpu(outbuf[i].cdx_u32));
722 pr_info("MCDI RPC RESP:%s\n", log);
726 if (error && resp_data_len == 0) {
727 /* MC rebooted during command */
730 if (WARN_ON_ONCE(error && resp_data_len < 4))
733 rc = CDX_DWORD_FIELD(outbuf[resp_hdr_len / 4], CDX_DWORD);
737 if (resp_data_len >= MC_CMD_ERR_ARG_OFST + 4) {
738 int offset = (resp_hdr_len + MC_CMD_ERR_ARG_OFST) / 4;
740 err_arg = CDX_DWORD_VAL(outbuf[offset]);
743 _cdx_mcdi_display_error(cdx, cmd->cmd,
744 cmd->inlen, rc, err_arg,
745 cdx_mcdi_errno(cdx, rc));
747 rc = cdx_mcdi_errno(cdx, rc);
754 if (mcdi->db_held_by == cmd)
755 mcdi->db_held_by = NULL;
757 if (cdx_cmd_cancelled(cmd)) {
758 list_del(&cmd->list);
759 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
761 } else if (rc == MC_CMD_ERR_QUEUE_FULL) {
762 cmd->state = MCDI_STATE_RETRY;
765 cmd->outbuf = outbuf + DIV_ROUND_UP(resp_hdr_len, 4);
766 cmd->outlen = resp_data_len;
767 cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
771 /* free sequence number and buffer */
772 mcdi->seq_held_by[cmd->seq] = NULL;
774 cdx_mcdi_start_or_queue(mcdi, rc != MC_CMD_ERR_QUEUE_FULL);
776 /* wake up anyone waiting for flush */
777 wake_up(&mcdi->cmd_complete_wq);
779 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
784 static void cdx_mcdi_timeout_cmd(struct cdx_mcdi_iface *mcdi,
785 struct cdx_mcdi_cmd *cmd,
786 struct list_head *cleanup_list)
788 struct cdx_mcdi *cdx = mcdi->cdx;
790 pr_err("MC command 0x%x inlen %zu state %d timed out after %u ms\n",
791 cmd->cmd, cmd->inlen, cmd->state,
792 jiffies_to_msecs(jiffies - cmd->started));
794 cmd->rc = -ETIMEDOUT;
795 cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
797 cdx_mcdi_mode_fail(cdx, cleanup_list);
801 * cdx_mcdi_rpc - Issue an MCDI command and wait for completion
802 * @cdx: NIC through which to issue the command
803 * @cmd: Command type number
804 * @inbuf: Command parameters
805 * @inlen: Length of command parameters, in bytes. Must be a multiple
806 * of 4 and no greater than %MCDI_CTL_SDU_LEN_MAX_V1.
807 * @outbuf: Response buffer. May be %NULL if @outlen is 0.
808 * @outlen: Length of response buffer, in bytes. If the actual
809 * response is longer than @outlen & ~3, it will be truncated
811 * @outlen_actual: Pointer through which to return the actual response
812 * length. May be %NULL if this is not needed.
814 * This function may sleep and therefore must be called in process
817 * Return: A negative error code, or zero if successful. The error
818 * code may come from the MCDI response or may indicate a failure
819 * to communicate with the MC. In the former case, the response
820 * will still be copied to @outbuf and *@outlen_actual will be
821 * set accordingly. In the latter case, *@outlen_actual will be
824 int cdx_mcdi_rpc(struct cdx_mcdi *cdx, unsigned int cmd,
825 const struct cdx_dword *inbuf, size_t inlen,
826 struct cdx_dword *outbuf, size_t outlen,
827 size_t *outlen_actual)
829 return cdx_mcdi_rpc_sync(cdx, cmd, inbuf, inlen, outbuf, outlen,
830 outlen_actual, false);
834 * cdx_mcdi_rpc_async - Schedule an MCDI command to run asynchronously
835 * @cdx: NIC through which to issue the command
836 * @cmd: Command type number
837 * @inbuf: Command parameters
838 * @inlen: Length of command parameters, in bytes
839 * @complete: Function to be called on completion or cancellation.
840 * @cookie: Arbitrary value to be passed to @complete.
842 * This function does not sleep and therefore may be called in atomic
843 * context. It will fail if event queues are disabled or if MCDI
844 * event completions have been disabled due to an error.
846 * If it succeeds, the @complete function will be called exactly once
847 * in process context, when one of the following occurs:
848 * (a) the completion event is received (in process context)
849 * (b) event queues are disabled (in the process that disables them)
852 cdx_mcdi_rpc_async(struct cdx_mcdi *cdx, unsigned int cmd,
853 const struct cdx_dword *inbuf, size_t inlen,
854 cdx_mcdi_async_completer *complete, unsigned long cookie)
856 struct cdx_mcdi_cmd *cmd_item =
857 kmalloc(sizeof(struct cdx_mcdi_cmd) + inlen, GFP_ATOMIC);
862 kref_init(&cmd_item->ref);
863 cmd_item->quiet = true;
864 cmd_item->cookie = cookie;
865 cmd_item->completer = complete;
867 cmd_item->inlen = inlen;
868 /* inbuf is probably not valid after return, so take a copy */
869 cmd_item->inbuf = (struct cdx_dword *)(cmd_item + 1);
870 memcpy(cmd_item + 1, inbuf, inlen);
872 return cdx_mcdi_rpc_async_internal(cdx, cmd_item, NULL);
875 static void _cdx_mcdi_display_error(struct cdx_mcdi *cdx, unsigned int cmd,
876 size_t inlen, int raw, int arg, int err_no)
878 pr_err("MC command 0x%x inlen %d failed err_no=%d (raw=%d) arg=%d\n",
879 cmd, (int)inlen, err_no, raw, arg);
883 * Set MCDI mode to fail to prevent any new commands, then cancel any
884 * outstanding commands.
885 * Caller must hold the mcdi iface_lock.
887 static void cdx_mcdi_mode_fail(struct cdx_mcdi *cdx, struct list_head *cleanup_list)
889 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
894 mcdi->mode = MCDI_MODE_FAIL;
896 while (!list_empty(&mcdi->cmd_list)) {
897 struct cdx_mcdi_cmd *cmd;
899 cmd = list_first_entry(&mcdi->cmd_list, struct cdx_mcdi_cmd,
901 _cdx_mcdi_cancel_cmd(mcdi, cdx_mcdi_cmd_handle(cmd), cleanup_list);