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 static void cdx_mcdi_cancel_cmd(struct cdx_mcdi *cdx, struct cdx_mcdi_cmd *cmd);
35 static void cdx_mcdi_wait_for_cleanup(struct cdx_mcdi *cdx);
36 static int cdx_mcdi_rpc_async_internal(struct cdx_mcdi *cdx,
37 struct cdx_mcdi_cmd *cmd,
38 unsigned int *handle);
39 static void cdx_mcdi_start_or_queue(struct cdx_mcdi_iface *mcdi,
41 static void cdx_mcdi_cmd_start_or_queue(struct cdx_mcdi_iface *mcdi,
42 struct cdx_mcdi_cmd *cmd);
43 static bool cdx_mcdi_complete_cmd(struct cdx_mcdi_iface *mcdi,
44 struct cdx_mcdi_cmd *cmd,
45 struct cdx_dword *outbuf,
47 struct list_head *cleanup_list);
48 static void cdx_mcdi_timeout_cmd(struct cdx_mcdi_iface *mcdi,
49 struct cdx_mcdi_cmd *cmd,
50 struct list_head *cleanup_list);
51 static void cdx_mcdi_cmd_work(struct work_struct *context);
52 static void cdx_mcdi_mode_fail(struct cdx_mcdi *cdx, struct list_head *cleanup_list);
53 static void _cdx_mcdi_display_error(struct cdx_mcdi *cdx, unsigned int cmd,
54 size_t inlen, int raw, int arg, int err_no);
56 static bool cdx_cmd_cancelled(struct cdx_mcdi_cmd *cmd)
58 return cmd->state == MCDI_STATE_RUNNING_CANCELLED;
61 static void cdx_mcdi_cmd_release(struct kref *ref)
63 kfree(container_of(ref, struct cdx_mcdi_cmd, ref));
66 static unsigned int cdx_mcdi_cmd_handle(struct cdx_mcdi_cmd *cmd)
71 static void _cdx_mcdi_remove_cmd(struct cdx_mcdi_iface *mcdi,
72 struct cdx_mcdi_cmd *cmd,
73 struct list_head *cleanup_list)
75 /* if cancelled, the completers have already been called */
76 if (cdx_cmd_cancelled(cmd))
80 list_add_tail(&cmd->cleanup_list, cleanup_list);
81 ++mcdi->outstanding_cleanups;
86 static void cdx_mcdi_remove_cmd(struct cdx_mcdi_iface *mcdi,
87 struct cdx_mcdi_cmd *cmd,
88 struct list_head *cleanup_list)
91 _cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
92 cmd->state = MCDI_STATE_FINISHED;
93 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
94 if (list_empty(&mcdi->cmd_list))
95 wake_up(&mcdi->cmd_complete_wq);
98 static unsigned long cdx_mcdi_rpc_timeout(struct cdx_mcdi *cdx, unsigned int cmd)
100 if (!cdx->mcdi_ops->mcdi_rpc_timeout)
101 return MCDI_RPC_TIMEOUT;
103 return cdx->mcdi_ops->mcdi_rpc_timeout(cdx, cmd);
106 int cdx_mcdi_init(struct cdx_mcdi *cdx)
108 struct cdx_mcdi_iface *mcdi;
111 cdx->mcdi = kzalloc(sizeof(*cdx->mcdi), GFP_KERNEL);
115 mcdi = cdx_mcdi_if(cdx);
118 mcdi->workqueue = alloc_ordered_workqueue("mcdi_wq", 0);
119 if (!mcdi->workqueue)
121 mutex_init(&mcdi->iface_lock);
122 mcdi->mode = MCDI_MODE_EVENTS;
123 INIT_LIST_HEAD(&mcdi->cmd_list);
124 init_waitqueue_head(&mcdi->cmd_complete_wq);
126 mcdi->new_epoch = true;
136 void cdx_mcdi_finish(struct cdx_mcdi *cdx)
138 struct cdx_mcdi_iface *mcdi;
140 mcdi = cdx_mcdi_if(cdx);
144 cdx_mcdi_wait_for_cleanup(cdx);
146 destroy_workqueue(mcdi->workqueue);
151 static bool cdx_mcdi_flushed(struct cdx_mcdi_iface *mcdi, bool ignore_cleanups)
155 mutex_lock(&mcdi->iface_lock);
156 flushed = list_empty(&mcdi->cmd_list) &&
157 (ignore_cleanups || !mcdi->outstanding_cleanups);
158 mutex_unlock(&mcdi->iface_lock);
162 /* Wait for outstanding MCDI commands to complete. */
163 static void cdx_mcdi_wait_for_cleanup(struct cdx_mcdi *cdx)
165 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
170 wait_event(mcdi->cmd_complete_wq,
171 cdx_mcdi_flushed(mcdi, false));
174 int cdx_mcdi_wait_for_quiescence(struct cdx_mcdi *cdx,
175 unsigned int timeout_jiffies)
177 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
178 DEFINE_WAIT_FUNC(wait, woken_wake_function);
184 flush_workqueue(mcdi->workqueue);
186 add_wait_queue(&mcdi->cmd_complete_wq, &wait);
188 while (!cdx_mcdi_flushed(mcdi, true)) {
189 rc = wait_woken(&wait, TASK_IDLE, timeout_jiffies);
195 remove_wait_queue(&mcdi->cmd_complete_wq, &wait);
205 static u8 cdx_mcdi_payload_csum(const struct cdx_dword *hdr, size_t hdr_len,
206 const struct cdx_dword *sdu, size_t sdu_len)
212 for (i = 0; i < hdr_len; i++)
216 for (i = 0; i < sdu_len; i++)
222 static void cdx_mcdi_send_request(struct cdx_mcdi *cdx,
223 struct cdx_mcdi_cmd *cmd)
225 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
226 const struct cdx_dword *inbuf = cmd->inbuf;
227 size_t inlen = cmd->inlen;
228 struct cdx_dword hdr[2];
236 mcdi->prev_seq = cmd->seq;
237 mcdi->seq_held_by[cmd->seq] = cmd;
238 mcdi->db_held_by = cmd;
239 cmd->started = jiffies;
241 not_epoch = !mcdi->new_epoch;
245 WARN_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2);
246 CDX_POPULATE_DWORD_7(hdr[0],
247 MCDI_HEADER_RESPONSE, 0,
248 MCDI_HEADER_RESYNC, 1,
249 MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
250 MCDI_HEADER_DATALEN, 0,
251 MCDI_HEADER_SEQ, cmd->seq,
252 MCDI_HEADER_XFLAGS, xflags,
253 MCDI_HEADER_NOT_EPOCH, not_epoch);
254 CDX_POPULATE_DWORD_3(hdr[1],
255 MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd->cmd,
256 MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen,
257 MC_CMD_V2_EXTN_IN_MESSAGE_TYPE,
258 MC_CMD_V2_EXTN_IN_MCDI_MESSAGE_TYPE_PLATFORM);
261 hdr[0].cdx_u32 |= (__force __le32)(cdx_mcdi_payload_csum(hdr, hdr_len, inbuf, inlen) <<
262 MCDI_HEADER_XFLAGS_LBN);
264 print_hex_dump_debug("MCDI REQ HEADER: ", DUMP_PREFIX_NONE, 32, 4, hdr, hdr_len, false);
265 print_hex_dump_debug("MCDI REQ PAYLOAD: ", DUMP_PREFIX_NONE, 32, 4, inbuf, inlen, false);
267 cdx->mcdi_ops->mcdi_request(cdx, hdr, hdr_len, inbuf, inlen);
269 mcdi->new_epoch = false;
272 static int cdx_mcdi_errno(struct cdx_mcdi *cdx, unsigned int mcdi_err)
276 case MC_CMD_ERR_QUEUE_FULL:
278 case MC_CMD_ERR_EPERM:
280 case MC_CMD_ERR_ENOENT:
282 case MC_CMD_ERR_EINTR:
284 case MC_CMD_ERR_EAGAIN:
286 case MC_CMD_ERR_EACCES:
288 case MC_CMD_ERR_EBUSY:
290 case MC_CMD_ERR_EINVAL:
292 case MC_CMD_ERR_ERANGE:
294 case MC_CMD_ERR_EDEADLK:
296 case MC_CMD_ERR_ENOSYS:
298 case MC_CMD_ERR_ETIME:
300 case MC_CMD_ERR_EALREADY:
302 case MC_CMD_ERR_ENOSPC:
304 case MC_CMD_ERR_ENOMEM:
306 case MC_CMD_ERR_ENOTSUP:
308 case MC_CMD_ERR_ALLOC_FAIL:
310 case MC_CMD_ERR_MAC_EXIST:
312 case MC_CMD_ERR_NO_EVB_PORT:
319 static void cdx_mcdi_process_cleanup_list(struct cdx_mcdi *cdx,
320 struct list_head *cleanup_list)
322 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
323 unsigned int cleanups = 0;
328 while (!list_empty(cleanup_list)) {
329 struct cdx_mcdi_cmd *cmd =
330 list_first_entry(cleanup_list,
331 struct cdx_mcdi_cmd, cleanup_list);
332 cmd->completer(cdx, cmd->cookie, cmd->rc,
333 cmd->outbuf, cmd->outlen);
334 list_del(&cmd->cleanup_list);
335 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
342 mutex_lock(&mcdi->iface_lock);
343 CDX_WARN_ON_PARANOID(cleanups > mcdi->outstanding_cleanups);
344 all_done = (mcdi->outstanding_cleanups -= cleanups) == 0;
345 mutex_unlock(&mcdi->iface_lock);
347 wake_up(&mcdi->cmd_complete_wq);
351 static void _cdx_mcdi_cancel_cmd(struct cdx_mcdi_iface *mcdi,
353 struct list_head *cleanup_list)
355 struct cdx_mcdi_cmd *cmd;
357 list_for_each_entry(cmd, &mcdi->cmd_list, list)
358 if (cdx_mcdi_cmd_handle(cmd) == handle) {
359 switch (cmd->state) {
360 case MCDI_STATE_QUEUED:
361 case MCDI_STATE_RETRY:
362 pr_debug("command %#x inlen %zu cancelled in queue\n",
363 cmd->cmd, cmd->inlen);
364 /* if not yet running, properly cancel it */
366 cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
368 case MCDI_STATE_RUNNING:
369 case MCDI_STATE_RUNNING_CANCELLED:
370 case MCDI_STATE_FINISHED:
379 static void cdx_mcdi_cancel_cmd(struct cdx_mcdi *cdx, struct cdx_mcdi_cmd *cmd)
381 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
382 LIST_HEAD(cleanup_list);
387 mutex_lock(&mcdi->iface_lock);
388 cdx_mcdi_timeout_cmd(mcdi, cmd, &cleanup_list);
389 mutex_unlock(&mcdi->iface_lock);
390 cdx_mcdi_process_cleanup_list(cdx, &cleanup_list);
393 struct cdx_mcdi_blocking_data {
396 wait_queue_head_t wq;
398 struct cdx_dword *outbuf;
400 size_t outlen_actual;
403 static void cdx_mcdi_blocking_data_release(struct kref *ref)
405 kfree(container_of(ref, struct cdx_mcdi_blocking_data, ref));
408 static void cdx_mcdi_rpc_completer(struct cdx_mcdi *cdx, unsigned long cookie,
409 int rc, struct cdx_dword *outbuf,
410 size_t outlen_actual)
412 struct cdx_mcdi_blocking_data *wait_data =
413 (struct cdx_mcdi_blocking_data *)cookie;
416 memcpy(wait_data->outbuf, outbuf,
417 min(outlen_actual, wait_data->outlen));
418 wait_data->outlen_actual = outlen_actual;
421 wait_data->done = true;
422 wake_up(&wait_data->wq);
423 kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release);
426 static int cdx_mcdi_rpc_sync(struct cdx_mcdi *cdx, unsigned int cmd,
427 const struct cdx_dword *inbuf, size_t inlen,
428 struct cdx_dword *outbuf, size_t outlen,
429 size_t *outlen_actual, bool quiet)
431 struct cdx_mcdi_blocking_data *wait_data;
432 struct cdx_mcdi_cmd *cmd_item;
439 wait_data = kmalloc(sizeof(*wait_data), GFP_KERNEL);
443 cmd_item = kmalloc(sizeof(*cmd_item), GFP_KERNEL);
449 kref_init(&wait_data->ref);
450 wait_data->done = false;
451 init_waitqueue_head(&wait_data->wq);
452 wait_data->outbuf = outbuf;
453 wait_data->outlen = outlen;
455 kref_init(&cmd_item->ref);
456 cmd_item->quiet = quiet;
457 cmd_item->cookie = (unsigned long)wait_data;
458 cmd_item->completer = &cdx_mcdi_rpc_completer;
460 cmd_item->inlen = inlen;
461 cmd_item->inbuf = inbuf;
463 /* Claim an extra reference for the completer to put. */
464 kref_get(&wait_data->ref);
465 rc = cdx_mcdi_rpc_async_internal(cdx, cmd_item, &handle);
467 kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release);
471 if (!wait_event_timeout(wait_data->wq, wait_data->done,
472 cdx_mcdi_rpc_timeout(cdx, cmd)) &&
474 pr_err("MC command 0x%x inlen %zu timed out (sync)\n",
477 cdx_mcdi_cancel_cmd(cdx, cmd_item);
479 wait_data->rc = -ETIMEDOUT;
480 wait_data->outlen_actual = 0;
484 *outlen_actual = wait_data->outlen_actual;
488 kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release);
493 static bool cdx_mcdi_get_seq(struct cdx_mcdi_iface *mcdi, unsigned char *seq)
495 *seq = mcdi->prev_seq;
497 *seq = (*seq + 1) % ARRAY_SIZE(mcdi->seq_held_by);
498 } while (mcdi->seq_held_by[*seq] && *seq != mcdi->prev_seq);
499 return !mcdi->seq_held_by[*seq];
502 static int cdx_mcdi_rpc_async_internal(struct cdx_mcdi *cdx,
503 struct cdx_mcdi_cmd *cmd,
504 unsigned int *handle)
506 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
507 LIST_HEAD(cleanup_list);
510 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
514 if (mcdi->mode == MCDI_MODE_FAIL) {
515 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
520 INIT_WORK(&cmd->work, cdx_mcdi_cmd_work);
521 INIT_LIST_HEAD(&cmd->list);
522 INIT_LIST_HEAD(&cmd->cleanup_list);
527 queue_work(mcdi->workqueue, &cmd->work);
531 static void cdx_mcdi_cmd_start_or_queue(struct cdx_mcdi_iface *mcdi,
532 struct cdx_mcdi_cmd *cmd)
534 struct cdx_mcdi *cdx = mcdi->cdx;
537 if (!mcdi->db_held_by &&
538 cdx_mcdi_get_seq(mcdi, &seq)) {
540 cmd->reboot_seen = false;
541 cdx_mcdi_send_request(cdx, cmd);
542 cmd->state = MCDI_STATE_RUNNING;
544 cmd->state = MCDI_STATE_QUEUED;
548 /* try to advance other commands */
549 static void cdx_mcdi_start_or_queue(struct cdx_mcdi_iface *mcdi,
552 struct cdx_mcdi_cmd *cmd, *tmp;
554 list_for_each_entry_safe(cmd, tmp, &mcdi->cmd_list, list)
555 if (cmd->state == MCDI_STATE_QUEUED ||
556 (cmd->state == MCDI_STATE_RETRY && allow_retry))
557 cdx_mcdi_cmd_start_or_queue(mcdi, cmd);
560 void cdx_mcdi_process_cmd(struct cdx_mcdi *cdx, struct cdx_dword *outbuf, int len)
562 struct cdx_mcdi_iface *mcdi;
563 struct cdx_mcdi_cmd *cmd;
564 LIST_HEAD(cleanup_list);
565 unsigned int respseq;
567 if (!len || !outbuf) {
568 pr_err("Got empty MC response\n");
572 mcdi = cdx_mcdi_if(cdx);
576 respseq = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_SEQ);
578 mutex_lock(&mcdi->iface_lock);
579 cmd = mcdi->seq_held_by[respseq];
582 if (cmd->state == MCDI_STATE_FINISHED) {
583 mutex_unlock(&mcdi->iface_lock);
584 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
588 cdx_mcdi_complete_cmd(mcdi, cmd, outbuf, len, &cleanup_list);
590 pr_err("MC response unexpected for seq : %0X\n", respseq);
593 mutex_unlock(&mcdi->iface_lock);
595 cdx_mcdi_process_cleanup_list(mcdi->cdx, &cleanup_list);
598 static void cdx_mcdi_cmd_work(struct work_struct *context)
600 struct cdx_mcdi_cmd *cmd =
601 container_of(context, struct cdx_mcdi_cmd, work);
602 struct cdx_mcdi_iface *mcdi = cmd->mcdi;
604 mutex_lock(&mcdi->iface_lock);
606 cmd->handle = mcdi->prev_handle++;
607 list_add_tail(&cmd->list, &mcdi->cmd_list);
608 cdx_mcdi_cmd_start_or_queue(mcdi, cmd);
610 mutex_unlock(&mcdi->iface_lock);
614 * Returns true if the MCDI module is finished with the command.
615 * (examples of false would be if the command was proxied, or it was
616 * rejected by the MC due to lack of resources and requeued).
618 static bool cdx_mcdi_complete_cmd(struct cdx_mcdi_iface *mcdi,
619 struct cdx_mcdi_cmd *cmd,
620 struct cdx_dword *outbuf,
622 struct list_head *cleanup_list)
624 size_t resp_hdr_len, resp_data_len;
625 struct cdx_mcdi *cdx = mcdi->cdx;
626 unsigned int respcmd, error;
627 bool completed = false;
630 /* ensure the command can't go away before this function returns */
633 respcmd = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_CODE);
634 error = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_ERROR);
636 if (respcmd != MC_CMD_V2_EXTN) {
638 resp_data_len = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_DATALEN);
644 CDX_DWORD_FIELD(outbuf[1], MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
647 if ((resp_hdr_len + resp_data_len) > len) {
648 pr_warn("Incomplete MCDI response received %d. Expected %zu\n",
649 len, (resp_hdr_len + resp_data_len));
653 print_hex_dump_debug("MCDI RESP HEADER: ", DUMP_PREFIX_NONE, 32, 4,
654 outbuf, resp_hdr_len, false);
655 print_hex_dump_debug("MCDI RESP PAYLOAD: ", DUMP_PREFIX_NONE, 32, 4,
656 outbuf + (resp_hdr_len / 4), resp_data_len, false);
658 if (error && resp_data_len == 0) {
659 /* MC rebooted during command */
662 if (WARN_ON_ONCE(error && resp_data_len < 4))
665 rc = CDX_DWORD_FIELD(outbuf[resp_hdr_len / 4], CDX_DWORD);
669 if (resp_data_len >= MC_CMD_ERR_ARG_OFST + 4) {
670 int offset = (resp_hdr_len + MC_CMD_ERR_ARG_OFST) / 4;
672 err_arg = CDX_DWORD_VAL(outbuf[offset]);
675 _cdx_mcdi_display_error(cdx, cmd->cmd,
676 cmd->inlen, rc, err_arg,
677 cdx_mcdi_errno(cdx, rc));
679 rc = cdx_mcdi_errno(cdx, rc);
686 if (mcdi->db_held_by == cmd)
687 mcdi->db_held_by = NULL;
689 if (cdx_cmd_cancelled(cmd)) {
690 list_del(&cmd->list);
691 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
693 } else if (rc == MC_CMD_ERR_QUEUE_FULL) {
694 cmd->state = MCDI_STATE_RETRY;
697 cmd->outbuf = outbuf + DIV_ROUND_UP(resp_hdr_len, 4);
698 cmd->outlen = resp_data_len;
699 cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
703 /* free sequence number and buffer */
704 mcdi->seq_held_by[cmd->seq] = NULL;
706 cdx_mcdi_start_or_queue(mcdi, rc != MC_CMD_ERR_QUEUE_FULL);
708 /* wake up anyone waiting for flush */
709 wake_up(&mcdi->cmd_complete_wq);
711 kref_put(&cmd->ref, cdx_mcdi_cmd_release);
716 static void cdx_mcdi_timeout_cmd(struct cdx_mcdi_iface *mcdi,
717 struct cdx_mcdi_cmd *cmd,
718 struct list_head *cleanup_list)
720 struct cdx_mcdi *cdx = mcdi->cdx;
722 pr_err("MC command 0x%x inlen %zu state %d timed out after %u ms\n",
723 cmd->cmd, cmd->inlen, cmd->state,
724 jiffies_to_msecs(jiffies - cmd->started));
726 cmd->rc = -ETIMEDOUT;
727 cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
729 cdx_mcdi_mode_fail(cdx, cleanup_list);
733 * cdx_mcdi_rpc - Issue an MCDI command and wait for completion
734 * @cdx: NIC through which to issue the command
735 * @cmd: Command type number
736 * @inbuf: Command parameters
737 * @inlen: Length of command parameters, in bytes. Must be a multiple
738 * of 4 and no greater than %MCDI_CTL_SDU_LEN_MAX_V1.
739 * @outbuf: Response buffer. May be %NULL if @outlen is 0.
740 * @outlen: Length of response buffer, in bytes. If the actual
741 * response is longer than @outlen & ~3, it will be truncated
743 * @outlen_actual: Pointer through which to return the actual response
744 * length. May be %NULL if this is not needed.
746 * This function may sleep and therefore must be called in process
749 * Return: A negative error code, or zero if successful. The error
750 * code may come from the MCDI response or may indicate a failure
751 * to communicate with the MC. In the former case, the response
752 * will still be copied to @outbuf and *@outlen_actual will be
753 * set accordingly. In the latter case, *@outlen_actual will be
756 int cdx_mcdi_rpc(struct cdx_mcdi *cdx, unsigned int cmd,
757 const struct cdx_dword *inbuf, size_t inlen,
758 struct cdx_dword *outbuf, size_t outlen,
759 size_t *outlen_actual)
761 return cdx_mcdi_rpc_sync(cdx, cmd, inbuf, inlen, outbuf, outlen,
762 outlen_actual, false);
766 * cdx_mcdi_rpc_async - Schedule an MCDI command to run asynchronously
767 * @cdx: NIC through which to issue the command
768 * @cmd: Command type number
769 * @inbuf: Command parameters
770 * @inlen: Length of command parameters, in bytes
771 * @complete: Function to be called on completion or cancellation.
772 * @cookie: Arbitrary value to be passed to @complete.
774 * This function does not sleep and therefore may be called in atomic
775 * context. It will fail if event queues are disabled or if MCDI
776 * event completions have been disabled due to an error.
778 * If it succeeds, the @complete function will be called exactly once
779 * in process context, when one of the following occurs:
780 * (a) the completion event is received (in process context)
781 * (b) event queues are disabled (in the process that disables them)
784 cdx_mcdi_rpc_async(struct cdx_mcdi *cdx, unsigned int cmd,
785 const struct cdx_dword *inbuf, size_t inlen,
786 cdx_mcdi_async_completer *complete, unsigned long cookie)
788 struct cdx_mcdi_cmd *cmd_item =
789 kmalloc(sizeof(struct cdx_mcdi_cmd) + inlen, GFP_ATOMIC);
794 kref_init(&cmd_item->ref);
795 cmd_item->quiet = true;
796 cmd_item->cookie = cookie;
797 cmd_item->completer = complete;
799 cmd_item->inlen = inlen;
800 /* inbuf is probably not valid after return, so take a copy */
801 cmd_item->inbuf = (struct cdx_dword *)(cmd_item + 1);
802 memcpy(cmd_item + 1, inbuf, inlen);
804 return cdx_mcdi_rpc_async_internal(cdx, cmd_item, NULL);
807 static void _cdx_mcdi_display_error(struct cdx_mcdi *cdx, unsigned int cmd,
808 size_t inlen, int raw, int arg, int err_no)
810 pr_err("MC command 0x%x inlen %d failed err_no=%d (raw=%d) arg=%d\n",
811 cmd, (int)inlen, err_no, raw, arg);
815 * Set MCDI mode to fail to prevent any new commands, then cancel any
816 * outstanding commands.
817 * Caller must hold the mcdi iface_lock.
819 static void cdx_mcdi_mode_fail(struct cdx_mcdi *cdx, struct list_head *cleanup_list)
821 struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
826 mcdi->mode = MCDI_MODE_FAIL;
828 while (!list_empty(&mcdi->cmd_list)) {
829 struct cdx_mcdi_cmd *cmd;
831 cmd = list_first_entry(&mcdi->cmd_list, struct cdx_mcdi_cmd,
833 _cdx_mcdi_cancel_cmd(mcdi, cdx_mcdi_cmd_handle(cmd), cleanup_list);