1eb96b740SNipun Gupta // SPDX-License-Identifier: GPL-2.0
2eb96b740SNipun Gupta /*
3eb96b740SNipun Gupta * Management-Controller-to-Driver Interface
4eb96b740SNipun Gupta *
5eb96b740SNipun Gupta * Copyright 2008-2013 Solarflare Communications Inc.
6eb96b740SNipun Gupta * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
7eb96b740SNipun Gupta */
8eb96b740SNipun Gupta #include <linux/delay.h>
9eb96b740SNipun Gupta #include <linux/slab.h>
10eb96b740SNipun Gupta #include <linux/io.h>
11eb96b740SNipun Gupta #include <linux/spinlock.h>
12eb96b740SNipun Gupta #include <linux/netdevice.h>
13eb96b740SNipun Gupta #include <linux/etherdevice.h>
14eb96b740SNipun Gupta #include <linux/ethtool.h>
15eb96b740SNipun Gupta #include <linux/if_vlan.h>
16eb96b740SNipun Gupta #include <linux/timer.h>
17eb96b740SNipun Gupta #include <linux/list.h>
18eb96b740SNipun Gupta #include <linux/pci.h>
19eb96b740SNipun Gupta #include <linux/device.h>
20eb96b740SNipun Gupta #include <linux/rwsem.h>
21eb96b740SNipun Gupta #include <linux/vmalloc.h>
22eb96b740SNipun Gupta #include <net/netevent.h>
23eb96b740SNipun Gupta #include <linux/log2.h>
24eb96b740SNipun Gupta #include <linux/net_tstamp.h>
25eb96b740SNipun Gupta #include <linux/wait.h>
26eb96b740SNipun Gupta
27eb96b740SNipun Gupta #include "bitfield.h"
28eb96b740SNipun Gupta #include "mcdi.h"
29eb96b740SNipun Gupta
30eb96b740SNipun Gupta static void cdx_mcdi_cancel_cmd(struct cdx_mcdi *cdx, struct cdx_mcdi_cmd *cmd);
31eb96b740SNipun Gupta static void cdx_mcdi_wait_for_cleanup(struct cdx_mcdi *cdx);
32eb96b740SNipun Gupta static int cdx_mcdi_rpc_async_internal(struct cdx_mcdi *cdx,
33eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd,
34eb96b740SNipun Gupta unsigned int *handle);
35eb96b740SNipun Gupta static void cdx_mcdi_start_or_queue(struct cdx_mcdi_iface *mcdi,
36eb96b740SNipun Gupta bool allow_retry);
37eb96b740SNipun Gupta static void cdx_mcdi_cmd_start_or_queue(struct cdx_mcdi_iface *mcdi,
38eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd);
39eb96b740SNipun Gupta static bool cdx_mcdi_complete_cmd(struct cdx_mcdi_iface *mcdi,
40eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd,
41eb96b740SNipun Gupta struct cdx_dword *outbuf,
42eb96b740SNipun Gupta int len,
43eb96b740SNipun Gupta struct list_head *cleanup_list);
44eb96b740SNipun Gupta static void cdx_mcdi_timeout_cmd(struct cdx_mcdi_iface *mcdi,
45eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd,
46eb96b740SNipun Gupta struct list_head *cleanup_list);
47eb96b740SNipun Gupta static void cdx_mcdi_cmd_work(struct work_struct *context);
48eb96b740SNipun Gupta static void cdx_mcdi_mode_fail(struct cdx_mcdi *cdx, struct list_head *cleanup_list);
49eb96b740SNipun Gupta static void _cdx_mcdi_display_error(struct cdx_mcdi *cdx, unsigned int cmd,
50eb96b740SNipun Gupta size_t inlen, int raw, int arg, int err_no);
51eb96b740SNipun Gupta
cdx_cmd_cancelled(struct cdx_mcdi_cmd * cmd)52eb96b740SNipun Gupta static bool cdx_cmd_cancelled(struct cdx_mcdi_cmd *cmd)
53eb96b740SNipun Gupta {
54eb96b740SNipun Gupta return cmd->state == MCDI_STATE_RUNNING_CANCELLED;
55eb96b740SNipun Gupta }
56eb96b740SNipun Gupta
cdx_mcdi_cmd_release(struct kref * ref)57eb96b740SNipun Gupta static void cdx_mcdi_cmd_release(struct kref *ref)
58eb96b740SNipun Gupta {
59eb96b740SNipun Gupta kfree(container_of(ref, struct cdx_mcdi_cmd, ref));
60eb96b740SNipun Gupta }
61eb96b740SNipun Gupta
cdx_mcdi_cmd_handle(struct cdx_mcdi_cmd * cmd)62eb96b740SNipun Gupta static unsigned int cdx_mcdi_cmd_handle(struct cdx_mcdi_cmd *cmd)
63eb96b740SNipun Gupta {
64eb96b740SNipun Gupta return cmd->handle;
65eb96b740SNipun Gupta }
66eb96b740SNipun Gupta
_cdx_mcdi_remove_cmd(struct cdx_mcdi_iface * mcdi,struct cdx_mcdi_cmd * cmd,struct list_head * cleanup_list)67eb96b740SNipun Gupta static void _cdx_mcdi_remove_cmd(struct cdx_mcdi_iface *mcdi,
68eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd,
69eb96b740SNipun Gupta struct list_head *cleanup_list)
70eb96b740SNipun Gupta {
71eb96b740SNipun Gupta /* if cancelled, the completers have already been called */
72eb96b740SNipun Gupta if (cdx_cmd_cancelled(cmd))
73eb96b740SNipun Gupta return;
74eb96b740SNipun Gupta
75eb96b740SNipun Gupta if (cmd->completer) {
76eb96b740SNipun Gupta list_add_tail(&cmd->cleanup_list, cleanup_list);
77eb96b740SNipun Gupta ++mcdi->outstanding_cleanups;
78eb96b740SNipun Gupta kref_get(&cmd->ref);
79eb96b740SNipun Gupta }
80eb96b740SNipun Gupta }
81eb96b740SNipun Gupta
cdx_mcdi_remove_cmd(struct cdx_mcdi_iface * mcdi,struct cdx_mcdi_cmd * cmd,struct list_head * cleanup_list)82eb96b740SNipun Gupta static void cdx_mcdi_remove_cmd(struct cdx_mcdi_iface *mcdi,
83eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd,
84eb96b740SNipun Gupta struct list_head *cleanup_list)
85eb96b740SNipun Gupta {
86eb96b740SNipun Gupta list_del(&cmd->list);
87eb96b740SNipun Gupta _cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
88eb96b740SNipun Gupta cmd->state = MCDI_STATE_FINISHED;
89eb96b740SNipun Gupta kref_put(&cmd->ref, cdx_mcdi_cmd_release);
90eb96b740SNipun Gupta if (list_empty(&mcdi->cmd_list))
91eb96b740SNipun Gupta wake_up(&mcdi->cmd_complete_wq);
92eb96b740SNipun Gupta }
93eb96b740SNipun Gupta
cdx_mcdi_rpc_timeout(struct cdx_mcdi * cdx,unsigned int cmd)94eb96b740SNipun Gupta static unsigned long cdx_mcdi_rpc_timeout(struct cdx_mcdi *cdx, unsigned int cmd)
95eb96b740SNipun Gupta {
96eb96b740SNipun Gupta if (!cdx->mcdi_ops->mcdi_rpc_timeout)
97eb96b740SNipun Gupta return MCDI_RPC_TIMEOUT;
98eb96b740SNipun Gupta else
99eb96b740SNipun Gupta return cdx->mcdi_ops->mcdi_rpc_timeout(cdx, cmd);
100eb96b740SNipun Gupta }
101eb96b740SNipun Gupta
cdx_mcdi_init(struct cdx_mcdi * cdx)102eb96b740SNipun Gupta int cdx_mcdi_init(struct cdx_mcdi *cdx)
103eb96b740SNipun Gupta {
104eb96b740SNipun Gupta struct cdx_mcdi_iface *mcdi;
105eb96b740SNipun Gupta int rc = -ENOMEM;
106eb96b740SNipun Gupta
107eb96b740SNipun Gupta cdx->mcdi = kzalloc(sizeof(*cdx->mcdi), GFP_KERNEL);
108eb96b740SNipun Gupta if (!cdx->mcdi)
109eb96b740SNipun Gupta goto fail;
110eb96b740SNipun Gupta
111eb96b740SNipun Gupta mcdi = cdx_mcdi_if(cdx);
112eb96b740SNipun Gupta mcdi->cdx = cdx;
113eb96b740SNipun Gupta
114eb96b740SNipun Gupta mcdi->workqueue = alloc_ordered_workqueue("mcdi_wq", 0);
115eb96b740SNipun Gupta if (!mcdi->workqueue)
116*b1c8ea3cSAbhijit Gangurde goto fail2;
117eb96b740SNipun Gupta mutex_init(&mcdi->iface_lock);
118eb96b740SNipun Gupta mcdi->mode = MCDI_MODE_EVENTS;
119eb96b740SNipun Gupta INIT_LIST_HEAD(&mcdi->cmd_list);
120eb96b740SNipun Gupta init_waitqueue_head(&mcdi->cmd_complete_wq);
121eb96b740SNipun Gupta
122eb96b740SNipun Gupta mcdi->new_epoch = true;
123eb96b740SNipun Gupta
124eb96b740SNipun Gupta return 0;
125eb96b740SNipun Gupta fail2:
126eb96b740SNipun Gupta kfree(cdx->mcdi);
127eb96b740SNipun Gupta cdx->mcdi = NULL;
128eb96b740SNipun Gupta fail:
129eb96b740SNipun Gupta return rc;
130eb96b740SNipun Gupta }
131eb96b740SNipun Gupta
cdx_mcdi_finish(struct cdx_mcdi * cdx)132eb96b740SNipun Gupta void cdx_mcdi_finish(struct cdx_mcdi *cdx)
133eb96b740SNipun Gupta {
134eb96b740SNipun Gupta struct cdx_mcdi_iface *mcdi;
135eb96b740SNipun Gupta
136eb96b740SNipun Gupta mcdi = cdx_mcdi_if(cdx);
137eb96b740SNipun Gupta if (!mcdi)
138eb96b740SNipun Gupta return;
139eb96b740SNipun Gupta
140eb96b740SNipun Gupta cdx_mcdi_wait_for_cleanup(cdx);
141eb96b740SNipun Gupta
142eb96b740SNipun Gupta destroy_workqueue(mcdi->workqueue);
143eb96b740SNipun Gupta kfree(cdx->mcdi);
144eb96b740SNipun Gupta cdx->mcdi = NULL;
145eb96b740SNipun Gupta }
146eb96b740SNipun Gupta
cdx_mcdi_flushed(struct cdx_mcdi_iface * mcdi,bool ignore_cleanups)147eb96b740SNipun Gupta static bool cdx_mcdi_flushed(struct cdx_mcdi_iface *mcdi, bool ignore_cleanups)
148eb96b740SNipun Gupta {
149eb96b740SNipun Gupta bool flushed;
150eb96b740SNipun Gupta
151eb96b740SNipun Gupta mutex_lock(&mcdi->iface_lock);
152eb96b740SNipun Gupta flushed = list_empty(&mcdi->cmd_list) &&
153eb96b740SNipun Gupta (ignore_cleanups || !mcdi->outstanding_cleanups);
154eb96b740SNipun Gupta mutex_unlock(&mcdi->iface_lock);
155eb96b740SNipun Gupta return flushed;
156eb96b740SNipun Gupta }
157eb96b740SNipun Gupta
158eb96b740SNipun Gupta /* Wait for outstanding MCDI commands to complete. */
cdx_mcdi_wait_for_cleanup(struct cdx_mcdi * cdx)159eb96b740SNipun Gupta static void cdx_mcdi_wait_for_cleanup(struct cdx_mcdi *cdx)
160eb96b740SNipun Gupta {
161eb96b740SNipun Gupta struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
162eb96b740SNipun Gupta
163eb96b740SNipun Gupta if (!mcdi)
164eb96b740SNipun Gupta return;
165eb96b740SNipun Gupta
166eb96b740SNipun Gupta wait_event(mcdi->cmd_complete_wq,
167eb96b740SNipun Gupta cdx_mcdi_flushed(mcdi, false));
168eb96b740SNipun Gupta }
169eb96b740SNipun Gupta
cdx_mcdi_wait_for_quiescence(struct cdx_mcdi * cdx,unsigned int timeout_jiffies)170eb96b740SNipun Gupta int cdx_mcdi_wait_for_quiescence(struct cdx_mcdi *cdx,
171eb96b740SNipun Gupta unsigned int timeout_jiffies)
172eb96b740SNipun Gupta {
173eb96b740SNipun Gupta struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
174eb96b740SNipun Gupta DEFINE_WAIT_FUNC(wait, woken_wake_function);
175eb96b740SNipun Gupta int rc = 0;
176eb96b740SNipun Gupta
177eb96b740SNipun Gupta if (!mcdi)
178eb96b740SNipun Gupta return -EINVAL;
179eb96b740SNipun Gupta
180eb96b740SNipun Gupta flush_workqueue(mcdi->workqueue);
181eb96b740SNipun Gupta
182eb96b740SNipun Gupta add_wait_queue(&mcdi->cmd_complete_wq, &wait);
183eb96b740SNipun Gupta
184eb96b740SNipun Gupta while (!cdx_mcdi_flushed(mcdi, true)) {
185eb96b740SNipun Gupta rc = wait_woken(&wait, TASK_IDLE, timeout_jiffies);
186eb96b740SNipun Gupta if (rc)
187eb96b740SNipun Gupta continue;
188eb96b740SNipun Gupta break;
189eb96b740SNipun Gupta }
190eb96b740SNipun Gupta
191eb96b740SNipun Gupta remove_wait_queue(&mcdi->cmd_complete_wq, &wait);
192eb96b740SNipun Gupta
193eb96b740SNipun Gupta if (rc > 0)
194eb96b740SNipun Gupta rc = 0;
195eb96b740SNipun Gupta else if (rc == 0)
196eb96b740SNipun Gupta rc = -ETIMEDOUT;
197eb96b740SNipun Gupta
198eb96b740SNipun Gupta return rc;
199eb96b740SNipun Gupta }
200eb96b740SNipun Gupta
cdx_mcdi_payload_csum(const struct cdx_dword * hdr,size_t hdr_len,const struct cdx_dword * sdu,size_t sdu_len)201eb96b740SNipun Gupta static u8 cdx_mcdi_payload_csum(const struct cdx_dword *hdr, size_t hdr_len,
202eb96b740SNipun Gupta const struct cdx_dword *sdu, size_t sdu_len)
203eb96b740SNipun Gupta {
204eb96b740SNipun Gupta u8 *p = (u8 *)hdr;
205eb96b740SNipun Gupta u8 csum = 0;
206eb96b740SNipun Gupta int i;
207eb96b740SNipun Gupta
208eb96b740SNipun Gupta for (i = 0; i < hdr_len; i++)
209eb96b740SNipun Gupta csum += p[i];
210eb96b740SNipun Gupta
211eb96b740SNipun Gupta p = (u8 *)sdu;
212eb96b740SNipun Gupta for (i = 0; i < sdu_len; i++)
213eb96b740SNipun Gupta csum += p[i];
214eb96b740SNipun Gupta
215eb96b740SNipun Gupta return ~csum & 0xff;
216eb96b740SNipun Gupta }
217eb96b740SNipun Gupta
cdx_mcdi_send_request(struct cdx_mcdi * cdx,struct cdx_mcdi_cmd * cmd)218eb96b740SNipun Gupta static void cdx_mcdi_send_request(struct cdx_mcdi *cdx,
219eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd)
220eb96b740SNipun Gupta {
221eb96b740SNipun Gupta struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
222eb96b740SNipun Gupta const struct cdx_dword *inbuf = cmd->inbuf;
223eb96b740SNipun Gupta size_t inlen = cmd->inlen;
224eb96b740SNipun Gupta struct cdx_dword hdr[2];
225eb96b740SNipun Gupta size_t hdr_len;
226eb96b740SNipun Gupta bool not_epoch;
227eb96b740SNipun Gupta u32 xflags;
228eb96b740SNipun Gupta
229eb96b740SNipun Gupta if (!mcdi)
230eb96b740SNipun Gupta return;
231eb96b740SNipun Gupta
232eb96b740SNipun Gupta mcdi->prev_seq = cmd->seq;
233eb96b740SNipun Gupta mcdi->seq_held_by[cmd->seq] = cmd;
234eb96b740SNipun Gupta mcdi->db_held_by = cmd;
235eb96b740SNipun Gupta cmd->started = jiffies;
236eb96b740SNipun Gupta
237eb96b740SNipun Gupta not_epoch = !mcdi->new_epoch;
238eb96b740SNipun Gupta xflags = 0;
239eb96b740SNipun Gupta
240eb96b740SNipun Gupta /* MCDI v2 */
241eb96b740SNipun Gupta WARN_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2);
242eb96b740SNipun Gupta CDX_POPULATE_DWORD_7(hdr[0],
243eb96b740SNipun Gupta MCDI_HEADER_RESPONSE, 0,
244eb96b740SNipun Gupta MCDI_HEADER_RESYNC, 1,
245eb96b740SNipun Gupta MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
246eb96b740SNipun Gupta MCDI_HEADER_DATALEN, 0,
247eb96b740SNipun Gupta MCDI_HEADER_SEQ, cmd->seq,
248eb96b740SNipun Gupta MCDI_HEADER_XFLAGS, xflags,
249eb96b740SNipun Gupta MCDI_HEADER_NOT_EPOCH, not_epoch);
250eb96b740SNipun Gupta CDX_POPULATE_DWORD_3(hdr[1],
251eb96b740SNipun Gupta MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd->cmd,
252eb96b740SNipun Gupta MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen,
253eb96b740SNipun Gupta MC_CMD_V2_EXTN_IN_MESSAGE_TYPE,
254eb96b740SNipun Gupta MC_CMD_V2_EXTN_IN_MCDI_MESSAGE_TYPE_PLATFORM);
255eb96b740SNipun Gupta hdr_len = 8;
256eb96b740SNipun Gupta
257eb96b740SNipun Gupta hdr[0].cdx_u32 |= (__force __le32)(cdx_mcdi_payload_csum(hdr, hdr_len, inbuf, inlen) <<
258eb96b740SNipun Gupta MCDI_HEADER_XFLAGS_LBN);
259*b1c8ea3cSAbhijit Gangurde
260*b1c8ea3cSAbhijit Gangurde print_hex_dump_debug("MCDI REQ HEADER: ", DUMP_PREFIX_NONE, 32, 4, hdr, hdr_len, false);
261*b1c8ea3cSAbhijit Gangurde print_hex_dump_debug("MCDI REQ PAYLOAD: ", DUMP_PREFIX_NONE, 32, 4, inbuf, inlen, false);
262*b1c8ea3cSAbhijit Gangurde
263eb96b740SNipun Gupta cdx->mcdi_ops->mcdi_request(cdx, hdr, hdr_len, inbuf, inlen);
264eb96b740SNipun Gupta
265eb96b740SNipun Gupta mcdi->new_epoch = false;
266eb96b740SNipun Gupta }
267eb96b740SNipun Gupta
cdx_mcdi_errno(struct cdx_mcdi * cdx,unsigned int mcdi_err)268eb96b740SNipun Gupta static int cdx_mcdi_errno(struct cdx_mcdi *cdx, unsigned int mcdi_err)
269eb96b740SNipun Gupta {
270eb96b740SNipun Gupta switch (mcdi_err) {
271eb96b740SNipun Gupta case 0:
272eb96b740SNipun Gupta case MC_CMD_ERR_QUEUE_FULL:
273eb96b740SNipun Gupta return mcdi_err;
274eb96b740SNipun Gupta case MC_CMD_ERR_EPERM:
275eb96b740SNipun Gupta return -EPERM;
276eb96b740SNipun Gupta case MC_CMD_ERR_ENOENT:
277eb96b740SNipun Gupta return -ENOENT;
278eb96b740SNipun Gupta case MC_CMD_ERR_EINTR:
279eb96b740SNipun Gupta return -EINTR;
280eb96b740SNipun Gupta case MC_CMD_ERR_EAGAIN:
281eb96b740SNipun Gupta return -EAGAIN;
282eb96b740SNipun Gupta case MC_CMD_ERR_EACCES:
283eb96b740SNipun Gupta return -EACCES;
284eb96b740SNipun Gupta case MC_CMD_ERR_EBUSY:
285eb96b740SNipun Gupta return -EBUSY;
286eb96b740SNipun Gupta case MC_CMD_ERR_EINVAL:
287eb96b740SNipun Gupta return -EINVAL;
288eb96b740SNipun Gupta case MC_CMD_ERR_ERANGE:
289eb96b740SNipun Gupta return -ERANGE;
290eb96b740SNipun Gupta case MC_CMD_ERR_EDEADLK:
291eb96b740SNipun Gupta return -EDEADLK;
292eb96b740SNipun Gupta case MC_CMD_ERR_ENOSYS:
293eb96b740SNipun Gupta return -EOPNOTSUPP;
294eb96b740SNipun Gupta case MC_CMD_ERR_ETIME:
295eb96b740SNipun Gupta return -ETIME;
296eb96b740SNipun Gupta case MC_CMD_ERR_EALREADY:
297eb96b740SNipun Gupta return -EALREADY;
298eb96b740SNipun Gupta case MC_CMD_ERR_ENOSPC:
299eb96b740SNipun Gupta return -ENOSPC;
300eb96b740SNipun Gupta case MC_CMD_ERR_ENOMEM:
301eb96b740SNipun Gupta return -ENOMEM;
302eb96b740SNipun Gupta case MC_CMD_ERR_ENOTSUP:
303eb96b740SNipun Gupta return -EOPNOTSUPP;
304eb96b740SNipun Gupta case MC_CMD_ERR_ALLOC_FAIL:
305eb96b740SNipun Gupta return -ENOBUFS;
306eb96b740SNipun Gupta case MC_CMD_ERR_MAC_EXIST:
307eb96b740SNipun Gupta return -EADDRINUSE;
308eb96b740SNipun Gupta case MC_CMD_ERR_NO_EVB_PORT:
309eb96b740SNipun Gupta return -EAGAIN;
310eb96b740SNipun Gupta default:
311eb96b740SNipun Gupta return -EPROTO;
312eb96b740SNipun Gupta }
313eb96b740SNipun Gupta }
314eb96b740SNipun Gupta
cdx_mcdi_process_cleanup_list(struct cdx_mcdi * cdx,struct list_head * cleanup_list)315eb96b740SNipun Gupta static void cdx_mcdi_process_cleanup_list(struct cdx_mcdi *cdx,
316eb96b740SNipun Gupta struct list_head *cleanup_list)
317eb96b740SNipun Gupta {
318eb96b740SNipun Gupta struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
319eb96b740SNipun Gupta unsigned int cleanups = 0;
320eb96b740SNipun Gupta
321eb96b740SNipun Gupta if (!mcdi)
322eb96b740SNipun Gupta return;
323eb96b740SNipun Gupta
324eb96b740SNipun Gupta while (!list_empty(cleanup_list)) {
325eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd =
326eb96b740SNipun Gupta list_first_entry(cleanup_list,
327eb96b740SNipun Gupta struct cdx_mcdi_cmd, cleanup_list);
328eb96b740SNipun Gupta cmd->completer(cdx, cmd->cookie, cmd->rc,
329eb96b740SNipun Gupta cmd->outbuf, cmd->outlen);
330eb96b740SNipun Gupta list_del(&cmd->cleanup_list);
331eb96b740SNipun Gupta kref_put(&cmd->ref, cdx_mcdi_cmd_release);
332eb96b740SNipun Gupta ++cleanups;
333eb96b740SNipun Gupta }
334eb96b740SNipun Gupta
335eb96b740SNipun Gupta if (cleanups) {
336eb96b740SNipun Gupta bool all_done;
337eb96b740SNipun Gupta
338eb96b740SNipun Gupta mutex_lock(&mcdi->iface_lock);
339eb96b740SNipun Gupta CDX_WARN_ON_PARANOID(cleanups > mcdi->outstanding_cleanups);
340eb96b740SNipun Gupta all_done = (mcdi->outstanding_cleanups -= cleanups) == 0;
341eb96b740SNipun Gupta mutex_unlock(&mcdi->iface_lock);
342eb96b740SNipun Gupta if (all_done)
343eb96b740SNipun Gupta wake_up(&mcdi->cmd_complete_wq);
344eb96b740SNipun Gupta }
345eb96b740SNipun Gupta }
346eb96b740SNipun Gupta
_cdx_mcdi_cancel_cmd(struct cdx_mcdi_iface * mcdi,unsigned int handle,struct list_head * cleanup_list)347eb96b740SNipun Gupta static void _cdx_mcdi_cancel_cmd(struct cdx_mcdi_iface *mcdi,
348eb96b740SNipun Gupta unsigned int handle,
349eb96b740SNipun Gupta struct list_head *cleanup_list)
350eb96b740SNipun Gupta {
351eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd;
352eb96b740SNipun Gupta
353eb96b740SNipun Gupta list_for_each_entry(cmd, &mcdi->cmd_list, list)
354eb96b740SNipun Gupta if (cdx_mcdi_cmd_handle(cmd) == handle) {
355eb96b740SNipun Gupta switch (cmd->state) {
356eb96b740SNipun Gupta case MCDI_STATE_QUEUED:
357eb96b740SNipun Gupta case MCDI_STATE_RETRY:
358eb96b740SNipun Gupta pr_debug("command %#x inlen %zu cancelled in queue\n",
359eb96b740SNipun Gupta cmd->cmd, cmd->inlen);
360eb96b740SNipun Gupta /* if not yet running, properly cancel it */
361eb96b740SNipun Gupta cmd->rc = -EPIPE;
362eb96b740SNipun Gupta cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
363eb96b740SNipun Gupta break;
364eb96b740SNipun Gupta case MCDI_STATE_RUNNING:
365eb96b740SNipun Gupta case MCDI_STATE_RUNNING_CANCELLED:
366eb96b740SNipun Gupta case MCDI_STATE_FINISHED:
367eb96b740SNipun Gupta default:
368eb96b740SNipun Gupta /* invalid state? */
369eb96b740SNipun Gupta WARN_ON(1);
370eb96b740SNipun Gupta }
371eb96b740SNipun Gupta break;
372eb96b740SNipun Gupta }
373eb96b740SNipun Gupta }
374eb96b740SNipun Gupta
cdx_mcdi_cancel_cmd(struct cdx_mcdi * cdx,struct cdx_mcdi_cmd * cmd)375eb96b740SNipun Gupta static void cdx_mcdi_cancel_cmd(struct cdx_mcdi *cdx, struct cdx_mcdi_cmd *cmd)
376eb96b740SNipun Gupta {
377eb96b740SNipun Gupta struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
378eb96b740SNipun Gupta LIST_HEAD(cleanup_list);
379eb96b740SNipun Gupta
380eb96b740SNipun Gupta if (!mcdi)
381eb96b740SNipun Gupta return;
382eb96b740SNipun Gupta
383eb96b740SNipun Gupta mutex_lock(&mcdi->iface_lock);
384eb96b740SNipun Gupta cdx_mcdi_timeout_cmd(mcdi, cmd, &cleanup_list);
385eb96b740SNipun Gupta mutex_unlock(&mcdi->iface_lock);
386eb96b740SNipun Gupta cdx_mcdi_process_cleanup_list(cdx, &cleanup_list);
387eb96b740SNipun Gupta }
388eb96b740SNipun Gupta
389eb96b740SNipun Gupta struct cdx_mcdi_blocking_data {
390eb96b740SNipun Gupta struct kref ref;
391eb96b740SNipun Gupta bool done;
392eb96b740SNipun Gupta wait_queue_head_t wq;
393eb96b740SNipun Gupta int rc;
394eb96b740SNipun Gupta struct cdx_dword *outbuf;
395eb96b740SNipun Gupta size_t outlen;
396eb96b740SNipun Gupta size_t outlen_actual;
397eb96b740SNipun Gupta };
398eb96b740SNipun Gupta
cdx_mcdi_blocking_data_release(struct kref * ref)399eb96b740SNipun Gupta static void cdx_mcdi_blocking_data_release(struct kref *ref)
400eb96b740SNipun Gupta {
401eb96b740SNipun Gupta kfree(container_of(ref, struct cdx_mcdi_blocking_data, ref));
402eb96b740SNipun Gupta }
403eb96b740SNipun Gupta
cdx_mcdi_rpc_completer(struct cdx_mcdi * cdx,unsigned long cookie,int rc,struct cdx_dword * outbuf,size_t outlen_actual)404eb96b740SNipun Gupta static void cdx_mcdi_rpc_completer(struct cdx_mcdi *cdx, unsigned long cookie,
405eb96b740SNipun Gupta int rc, struct cdx_dword *outbuf,
406eb96b740SNipun Gupta size_t outlen_actual)
407eb96b740SNipun Gupta {
408eb96b740SNipun Gupta struct cdx_mcdi_blocking_data *wait_data =
409eb96b740SNipun Gupta (struct cdx_mcdi_blocking_data *)cookie;
410eb96b740SNipun Gupta
411eb96b740SNipun Gupta wait_data->rc = rc;
412eb96b740SNipun Gupta memcpy(wait_data->outbuf, outbuf,
413eb96b740SNipun Gupta min(outlen_actual, wait_data->outlen));
414eb96b740SNipun Gupta wait_data->outlen_actual = outlen_actual;
415eb96b740SNipun Gupta /* memory barrier */
416eb96b740SNipun Gupta smp_wmb();
417eb96b740SNipun Gupta wait_data->done = true;
418eb96b740SNipun Gupta wake_up(&wait_data->wq);
419eb96b740SNipun Gupta kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release);
420eb96b740SNipun Gupta }
421eb96b740SNipun Gupta
cdx_mcdi_rpc_sync(struct cdx_mcdi * cdx,unsigned int cmd,const struct cdx_dword * inbuf,size_t inlen,struct cdx_dword * outbuf,size_t outlen,size_t * outlen_actual,bool quiet)422eb96b740SNipun Gupta static int cdx_mcdi_rpc_sync(struct cdx_mcdi *cdx, unsigned int cmd,
423eb96b740SNipun Gupta const struct cdx_dword *inbuf, size_t inlen,
424eb96b740SNipun Gupta struct cdx_dword *outbuf, size_t outlen,
425eb96b740SNipun Gupta size_t *outlen_actual, bool quiet)
426eb96b740SNipun Gupta {
427eb96b740SNipun Gupta struct cdx_mcdi_blocking_data *wait_data;
428eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd_item;
429eb96b740SNipun Gupta unsigned int handle;
430eb96b740SNipun Gupta int rc;
431eb96b740SNipun Gupta
432eb96b740SNipun Gupta if (outlen_actual)
433eb96b740SNipun Gupta *outlen_actual = 0;
434eb96b740SNipun Gupta
435eb96b740SNipun Gupta wait_data = kmalloc(sizeof(*wait_data), GFP_KERNEL);
436eb96b740SNipun Gupta if (!wait_data)
437eb96b740SNipun Gupta return -ENOMEM;
438eb96b740SNipun Gupta
439eb96b740SNipun Gupta cmd_item = kmalloc(sizeof(*cmd_item), GFP_KERNEL);
440eb96b740SNipun Gupta if (!cmd_item) {
441eb96b740SNipun Gupta kfree(wait_data);
442eb96b740SNipun Gupta return -ENOMEM;
443eb96b740SNipun Gupta }
444eb96b740SNipun Gupta
445eb96b740SNipun Gupta kref_init(&wait_data->ref);
446eb96b740SNipun Gupta wait_data->done = false;
447eb96b740SNipun Gupta init_waitqueue_head(&wait_data->wq);
448eb96b740SNipun Gupta wait_data->outbuf = outbuf;
449eb96b740SNipun Gupta wait_data->outlen = outlen;
450eb96b740SNipun Gupta
451eb96b740SNipun Gupta kref_init(&cmd_item->ref);
452eb96b740SNipun Gupta cmd_item->quiet = quiet;
453eb96b740SNipun Gupta cmd_item->cookie = (unsigned long)wait_data;
454eb96b740SNipun Gupta cmd_item->completer = &cdx_mcdi_rpc_completer;
455eb96b740SNipun Gupta cmd_item->cmd = cmd;
456eb96b740SNipun Gupta cmd_item->inlen = inlen;
457eb96b740SNipun Gupta cmd_item->inbuf = inbuf;
458eb96b740SNipun Gupta
459eb96b740SNipun Gupta /* Claim an extra reference for the completer to put. */
460eb96b740SNipun Gupta kref_get(&wait_data->ref);
461eb96b740SNipun Gupta rc = cdx_mcdi_rpc_async_internal(cdx, cmd_item, &handle);
462eb96b740SNipun Gupta if (rc) {
463eb96b740SNipun Gupta kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release);
464eb96b740SNipun Gupta goto out;
465eb96b740SNipun Gupta }
466eb96b740SNipun Gupta
467eb96b740SNipun Gupta if (!wait_event_timeout(wait_data->wq, wait_data->done,
468eb96b740SNipun Gupta cdx_mcdi_rpc_timeout(cdx, cmd)) &&
469eb96b740SNipun Gupta !wait_data->done) {
470eb96b740SNipun Gupta pr_err("MC command 0x%x inlen %zu timed out (sync)\n",
471eb96b740SNipun Gupta cmd, inlen);
472eb96b740SNipun Gupta
473eb96b740SNipun Gupta cdx_mcdi_cancel_cmd(cdx, cmd_item);
474eb96b740SNipun Gupta
475eb96b740SNipun Gupta wait_data->rc = -ETIMEDOUT;
476eb96b740SNipun Gupta wait_data->outlen_actual = 0;
477eb96b740SNipun Gupta }
478eb96b740SNipun Gupta
479eb96b740SNipun Gupta if (outlen_actual)
480eb96b740SNipun Gupta *outlen_actual = wait_data->outlen_actual;
481eb96b740SNipun Gupta rc = wait_data->rc;
482eb96b740SNipun Gupta
483eb96b740SNipun Gupta out:
484eb96b740SNipun Gupta kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release);
485eb96b740SNipun Gupta
486eb96b740SNipun Gupta return rc;
487eb96b740SNipun Gupta }
488eb96b740SNipun Gupta
cdx_mcdi_get_seq(struct cdx_mcdi_iface * mcdi,unsigned char * seq)489eb96b740SNipun Gupta static bool cdx_mcdi_get_seq(struct cdx_mcdi_iface *mcdi, unsigned char *seq)
490eb96b740SNipun Gupta {
491eb96b740SNipun Gupta *seq = mcdi->prev_seq;
492eb96b740SNipun Gupta do {
493eb96b740SNipun Gupta *seq = (*seq + 1) % ARRAY_SIZE(mcdi->seq_held_by);
494eb96b740SNipun Gupta } while (mcdi->seq_held_by[*seq] && *seq != mcdi->prev_seq);
495eb96b740SNipun Gupta return !mcdi->seq_held_by[*seq];
496eb96b740SNipun Gupta }
497eb96b740SNipun Gupta
cdx_mcdi_rpc_async_internal(struct cdx_mcdi * cdx,struct cdx_mcdi_cmd * cmd,unsigned int * handle)498eb96b740SNipun Gupta static int cdx_mcdi_rpc_async_internal(struct cdx_mcdi *cdx,
499eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd,
500eb96b740SNipun Gupta unsigned int *handle)
501eb96b740SNipun Gupta {
502eb96b740SNipun Gupta struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
503eb96b740SNipun Gupta LIST_HEAD(cleanup_list);
504eb96b740SNipun Gupta
505eb96b740SNipun Gupta if (!mcdi) {
506eb96b740SNipun Gupta kref_put(&cmd->ref, cdx_mcdi_cmd_release);
507eb96b740SNipun Gupta return -ENETDOWN;
508eb96b740SNipun Gupta }
509eb96b740SNipun Gupta
510eb96b740SNipun Gupta if (mcdi->mode == MCDI_MODE_FAIL) {
511eb96b740SNipun Gupta kref_put(&cmd->ref, cdx_mcdi_cmd_release);
512eb96b740SNipun Gupta return -ENETDOWN;
513eb96b740SNipun Gupta }
514eb96b740SNipun Gupta
515eb96b740SNipun Gupta cmd->mcdi = mcdi;
516eb96b740SNipun Gupta INIT_WORK(&cmd->work, cdx_mcdi_cmd_work);
517eb96b740SNipun Gupta INIT_LIST_HEAD(&cmd->list);
518eb96b740SNipun Gupta INIT_LIST_HEAD(&cmd->cleanup_list);
519eb96b740SNipun Gupta cmd->rc = 0;
520eb96b740SNipun Gupta cmd->outbuf = NULL;
521eb96b740SNipun Gupta cmd->outlen = 0;
522eb96b740SNipun Gupta
523eb96b740SNipun Gupta queue_work(mcdi->workqueue, &cmd->work);
524eb96b740SNipun Gupta return 0;
525eb96b740SNipun Gupta }
526eb96b740SNipun Gupta
cdx_mcdi_cmd_start_or_queue(struct cdx_mcdi_iface * mcdi,struct cdx_mcdi_cmd * cmd)527eb96b740SNipun Gupta static void cdx_mcdi_cmd_start_or_queue(struct cdx_mcdi_iface *mcdi,
528eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd)
529eb96b740SNipun Gupta {
530eb96b740SNipun Gupta struct cdx_mcdi *cdx = mcdi->cdx;
531eb96b740SNipun Gupta u8 seq;
532eb96b740SNipun Gupta
533eb96b740SNipun Gupta if (!mcdi->db_held_by &&
534eb96b740SNipun Gupta cdx_mcdi_get_seq(mcdi, &seq)) {
535eb96b740SNipun Gupta cmd->seq = seq;
536eb96b740SNipun Gupta cmd->reboot_seen = false;
537eb96b740SNipun Gupta cdx_mcdi_send_request(cdx, cmd);
538eb96b740SNipun Gupta cmd->state = MCDI_STATE_RUNNING;
539eb96b740SNipun Gupta } else {
540eb96b740SNipun Gupta cmd->state = MCDI_STATE_QUEUED;
541eb96b740SNipun Gupta }
542eb96b740SNipun Gupta }
543eb96b740SNipun Gupta
544eb96b740SNipun Gupta /* try to advance other commands */
cdx_mcdi_start_or_queue(struct cdx_mcdi_iface * mcdi,bool allow_retry)545eb96b740SNipun Gupta static void cdx_mcdi_start_or_queue(struct cdx_mcdi_iface *mcdi,
546eb96b740SNipun Gupta bool allow_retry)
547eb96b740SNipun Gupta {
548eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd, *tmp;
549eb96b740SNipun Gupta
550eb96b740SNipun Gupta list_for_each_entry_safe(cmd, tmp, &mcdi->cmd_list, list)
551eb96b740SNipun Gupta if (cmd->state == MCDI_STATE_QUEUED ||
552eb96b740SNipun Gupta (cmd->state == MCDI_STATE_RETRY && allow_retry))
553eb96b740SNipun Gupta cdx_mcdi_cmd_start_or_queue(mcdi, cmd);
554eb96b740SNipun Gupta }
555eb96b740SNipun Gupta
cdx_mcdi_process_cmd(struct cdx_mcdi * cdx,struct cdx_dword * outbuf,int len)556eb96b740SNipun Gupta void cdx_mcdi_process_cmd(struct cdx_mcdi *cdx, struct cdx_dword *outbuf, int len)
557eb96b740SNipun Gupta {
558eb96b740SNipun Gupta struct cdx_mcdi_iface *mcdi;
559eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd;
560eb96b740SNipun Gupta LIST_HEAD(cleanup_list);
561eb96b740SNipun Gupta unsigned int respseq;
562eb96b740SNipun Gupta
563eb96b740SNipun Gupta if (!len || !outbuf) {
564eb96b740SNipun Gupta pr_err("Got empty MC response\n");
565eb96b740SNipun Gupta return;
566eb96b740SNipun Gupta }
567eb96b740SNipun Gupta
568eb96b740SNipun Gupta mcdi = cdx_mcdi_if(cdx);
569eb96b740SNipun Gupta if (!mcdi)
570eb96b740SNipun Gupta return;
571eb96b740SNipun Gupta
572eb96b740SNipun Gupta respseq = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_SEQ);
573eb96b740SNipun Gupta
574eb96b740SNipun Gupta mutex_lock(&mcdi->iface_lock);
575eb96b740SNipun Gupta cmd = mcdi->seq_held_by[respseq];
576eb96b740SNipun Gupta
577eb96b740SNipun Gupta if (cmd) {
578eb96b740SNipun Gupta if (cmd->state == MCDI_STATE_FINISHED) {
579eb96b740SNipun Gupta mutex_unlock(&mcdi->iface_lock);
580eb96b740SNipun Gupta kref_put(&cmd->ref, cdx_mcdi_cmd_release);
581eb96b740SNipun Gupta return;
582eb96b740SNipun Gupta }
583eb96b740SNipun Gupta
584eb96b740SNipun Gupta cdx_mcdi_complete_cmd(mcdi, cmd, outbuf, len, &cleanup_list);
585eb96b740SNipun Gupta } else {
586eb96b740SNipun Gupta pr_err("MC response unexpected for seq : %0X\n", respseq);
587eb96b740SNipun Gupta }
588eb96b740SNipun Gupta
589eb96b740SNipun Gupta mutex_unlock(&mcdi->iface_lock);
590eb96b740SNipun Gupta
591eb96b740SNipun Gupta cdx_mcdi_process_cleanup_list(mcdi->cdx, &cleanup_list);
592eb96b740SNipun Gupta }
593eb96b740SNipun Gupta
cdx_mcdi_cmd_work(struct work_struct * context)594eb96b740SNipun Gupta static void cdx_mcdi_cmd_work(struct work_struct *context)
595eb96b740SNipun Gupta {
596eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd =
597eb96b740SNipun Gupta container_of(context, struct cdx_mcdi_cmd, work);
598eb96b740SNipun Gupta struct cdx_mcdi_iface *mcdi = cmd->mcdi;
599eb96b740SNipun Gupta
600eb96b740SNipun Gupta mutex_lock(&mcdi->iface_lock);
601eb96b740SNipun Gupta
602eb96b740SNipun Gupta cmd->handle = mcdi->prev_handle++;
603eb96b740SNipun Gupta list_add_tail(&cmd->list, &mcdi->cmd_list);
604eb96b740SNipun Gupta cdx_mcdi_cmd_start_or_queue(mcdi, cmd);
605eb96b740SNipun Gupta
606eb96b740SNipun Gupta mutex_unlock(&mcdi->iface_lock);
607eb96b740SNipun Gupta }
608eb96b740SNipun Gupta
609eb96b740SNipun Gupta /*
610eb96b740SNipun Gupta * Returns true if the MCDI module is finished with the command.
611eb96b740SNipun Gupta * (examples of false would be if the command was proxied, or it was
612eb96b740SNipun Gupta * rejected by the MC due to lack of resources and requeued).
613eb96b740SNipun Gupta */
cdx_mcdi_complete_cmd(struct cdx_mcdi_iface * mcdi,struct cdx_mcdi_cmd * cmd,struct cdx_dword * outbuf,int len,struct list_head * cleanup_list)614eb96b740SNipun Gupta static bool cdx_mcdi_complete_cmd(struct cdx_mcdi_iface *mcdi,
615eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd,
616eb96b740SNipun Gupta struct cdx_dword *outbuf,
617eb96b740SNipun Gupta int len,
618eb96b740SNipun Gupta struct list_head *cleanup_list)
619eb96b740SNipun Gupta {
620eb96b740SNipun Gupta size_t resp_hdr_len, resp_data_len;
621eb96b740SNipun Gupta struct cdx_mcdi *cdx = mcdi->cdx;
622eb96b740SNipun Gupta unsigned int respcmd, error;
623eb96b740SNipun Gupta bool completed = false;
624eb96b740SNipun Gupta int rc;
625eb96b740SNipun Gupta
626eb96b740SNipun Gupta /* ensure the command can't go away before this function returns */
627eb96b740SNipun Gupta kref_get(&cmd->ref);
628eb96b740SNipun Gupta
629eb96b740SNipun Gupta respcmd = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_CODE);
630eb96b740SNipun Gupta error = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_ERROR);
631eb96b740SNipun Gupta
632eb96b740SNipun Gupta if (respcmd != MC_CMD_V2_EXTN) {
633eb96b740SNipun Gupta resp_hdr_len = 4;
634eb96b740SNipun Gupta resp_data_len = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_DATALEN);
635eb96b740SNipun Gupta } else {
636eb96b740SNipun Gupta resp_data_len = 0;
637eb96b740SNipun Gupta resp_hdr_len = 8;
638eb96b740SNipun Gupta if (len >= 8)
639eb96b740SNipun Gupta resp_data_len =
640eb96b740SNipun Gupta CDX_DWORD_FIELD(outbuf[1], MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
641eb96b740SNipun Gupta }
642eb96b740SNipun Gupta
643eb96b740SNipun Gupta if ((resp_hdr_len + resp_data_len) > len) {
644eb96b740SNipun Gupta pr_warn("Incomplete MCDI response received %d. Expected %zu\n",
645eb96b740SNipun Gupta len, (resp_hdr_len + resp_data_len));
646eb96b740SNipun Gupta resp_data_len = 0;
647eb96b740SNipun Gupta }
648eb96b740SNipun Gupta
649*b1c8ea3cSAbhijit Gangurde print_hex_dump_debug("MCDI RESP HEADER: ", DUMP_PREFIX_NONE, 32, 4,
650*b1c8ea3cSAbhijit Gangurde outbuf, resp_hdr_len, false);
651*b1c8ea3cSAbhijit Gangurde print_hex_dump_debug("MCDI RESP PAYLOAD: ", DUMP_PREFIX_NONE, 32, 4,
652*b1c8ea3cSAbhijit Gangurde outbuf + (resp_hdr_len / 4), resp_data_len, false);
653eb96b740SNipun Gupta
654eb96b740SNipun Gupta if (error && resp_data_len == 0) {
655eb96b740SNipun Gupta /* MC rebooted during command */
656eb96b740SNipun Gupta rc = -EIO;
657eb96b740SNipun Gupta } else {
658eb96b740SNipun Gupta if (WARN_ON_ONCE(error && resp_data_len < 4))
659eb96b740SNipun Gupta resp_data_len = 4;
660eb96b740SNipun Gupta if (error) {
661eb96b740SNipun Gupta rc = CDX_DWORD_FIELD(outbuf[resp_hdr_len / 4], CDX_DWORD);
662eb96b740SNipun Gupta if (!cmd->quiet) {
663eb96b740SNipun Gupta int err_arg = 0;
664eb96b740SNipun Gupta
665eb96b740SNipun Gupta if (resp_data_len >= MC_CMD_ERR_ARG_OFST + 4) {
666eb96b740SNipun Gupta int offset = (resp_hdr_len + MC_CMD_ERR_ARG_OFST) / 4;
667eb96b740SNipun Gupta
668eb96b740SNipun Gupta err_arg = CDX_DWORD_VAL(outbuf[offset]);
669eb96b740SNipun Gupta }
670eb96b740SNipun Gupta
671eb96b740SNipun Gupta _cdx_mcdi_display_error(cdx, cmd->cmd,
672eb96b740SNipun Gupta cmd->inlen, rc, err_arg,
673eb96b740SNipun Gupta cdx_mcdi_errno(cdx, rc));
674eb96b740SNipun Gupta }
675eb96b740SNipun Gupta rc = cdx_mcdi_errno(cdx, rc);
676eb96b740SNipun Gupta } else {
677eb96b740SNipun Gupta rc = 0;
678eb96b740SNipun Gupta }
679eb96b740SNipun Gupta }
680eb96b740SNipun Gupta
681eb96b740SNipun Gupta /* free doorbell */
682eb96b740SNipun Gupta if (mcdi->db_held_by == cmd)
683eb96b740SNipun Gupta mcdi->db_held_by = NULL;
684eb96b740SNipun Gupta
685eb96b740SNipun Gupta if (cdx_cmd_cancelled(cmd)) {
686eb96b740SNipun Gupta list_del(&cmd->list);
687eb96b740SNipun Gupta kref_put(&cmd->ref, cdx_mcdi_cmd_release);
688eb96b740SNipun Gupta completed = true;
689eb96b740SNipun Gupta } else if (rc == MC_CMD_ERR_QUEUE_FULL) {
690eb96b740SNipun Gupta cmd->state = MCDI_STATE_RETRY;
691eb96b740SNipun Gupta } else {
692eb96b740SNipun Gupta cmd->rc = rc;
693eb96b740SNipun Gupta cmd->outbuf = outbuf + DIV_ROUND_UP(resp_hdr_len, 4);
694eb96b740SNipun Gupta cmd->outlen = resp_data_len;
695eb96b740SNipun Gupta cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
696eb96b740SNipun Gupta completed = true;
697eb96b740SNipun Gupta }
698eb96b740SNipun Gupta
699eb96b740SNipun Gupta /* free sequence number and buffer */
700eb96b740SNipun Gupta mcdi->seq_held_by[cmd->seq] = NULL;
701eb96b740SNipun Gupta
702eb96b740SNipun Gupta cdx_mcdi_start_or_queue(mcdi, rc != MC_CMD_ERR_QUEUE_FULL);
703eb96b740SNipun Gupta
704eb96b740SNipun Gupta /* wake up anyone waiting for flush */
705eb96b740SNipun Gupta wake_up(&mcdi->cmd_complete_wq);
706eb96b740SNipun Gupta
707eb96b740SNipun Gupta kref_put(&cmd->ref, cdx_mcdi_cmd_release);
708eb96b740SNipun Gupta
709eb96b740SNipun Gupta return completed;
710eb96b740SNipun Gupta }
711eb96b740SNipun Gupta
cdx_mcdi_timeout_cmd(struct cdx_mcdi_iface * mcdi,struct cdx_mcdi_cmd * cmd,struct list_head * cleanup_list)712eb96b740SNipun Gupta static void cdx_mcdi_timeout_cmd(struct cdx_mcdi_iface *mcdi,
713eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd,
714eb96b740SNipun Gupta struct list_head *cleanup_list)
715eb96b740SNipun Gupta {
716eb96b740SNipun Gupta struct cdx_mcdi *cdx = mcdi->cdx;
717eb96b740SNipun Gupta
718eb96b740SNipun Gupta pr_err("MC command 0x%x inlen %zu state %d timed out after %u ms\n",
719eb96b740SNipun Gupta cmd->cmd, cmd->inlen, cmd->state,
720eb96b740SNipun Gupta jiffies_to_msecs(jiffies - cmd->started));
721eb96b740SNipun Gupta
722eb96b740SNipun Gupta cmd->rc = -ETIMEDOUT;
723eb96b740SNipun Gupta cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list);
724eb96b740SNipun Gupta
725eb96b740SNipun Gupta cdx_mcdi_mode_fail(cdx, cleanup_list);
726eb96b740SNipun Gupta }
727eb96b740SNipun Gupta
728eb96b740SNipun Gupta /**
729eb96b740SNipun Gupta * cdx_mcdi_rpc - Issue an MCDI command and wait for completion
730eb96b740SNipun Gupta * @cdx: NIC through which to issue the command
731eb96b740SNipun Gupta * @cmd: Command type number
732eb96b740SNipun Gupta * @inbuf: Command parameters
733eb96b740SNipun Gupta * @inlen: Length of command parameters, in bytes. Must be a multiple
734eb96b740SNipun Gupta * of 4 and no greater than %MCDI_CTL_SDU_LEN_MAX_V1.
735eb96b740SNipun Gupta * @outbuf: Response buffer. May be %NULL if @outlen is 0.
736eb96b740SNipun Gupta * @outlen: Length of response buffer, in bytes. If the actual
737eb96b740SNipun Gupta * response is longer than @outlen & ~3, it will be truncated
738eb96b740SNipun Gupta * to that length.
739eb96b740SNipun Gupta * @outlen_actual: Pointer through which to return the actual response
740eb96b740SNipun Gupta * length. May be %NULL if this is not needed.
741eb96b740SNipun Gupta *
742eb96b740SNipun Gupta * This function may sleep and therefore must be called in process
743eb96b740SNipun Gupta * context.
744eb96b740SNipun Gupta *
745eb96b740SNipun Gupta * Return: A negative error code, or zero if successful. The error
746eb96b740SNipun Gupta * code may come from the MCDI response or may indicate a failure
747eb96b740SNipun Gupta * to communicate with the MC. In the former case, the response
748eb96b740SNipun Gupta * will still be copied to @outbuf and *@outlen_actual will be
749eb96b740SNipun Gupta * set accordingly. In the latter case, *@outlen_actual will be
750eb96b740SNipun Gupta * set to zero.
751eb96b740SNipun Gupta */
cdx_mcdi_rpc(struct cdx_mcdi * cdx,unsigned int cmd,const struct cdx_dword * inbuf,size_t inlen,struct cdx_dword * outbuf,size_t outlen,size_t * outlen_actual)752eb96b740SNipun Gupta int cdx_mcdi_rpc(struct cdx_mcdi *cdx, unsigned int cmd,
753eb96b740SNipun Gupta const struct cdx_dword *inbuf, size_t inlen,
754eb96b740SNipun Gupta struct cdx_dword *outbuf, size_t outlen,
755eb96b740SNipun Gupta size_t *outlen_actual)
756eb96b740SNipun Gupta {
757eb96b740SNipun Gupta return cdx_mcdi_rpc_sync(cdx, cmd, inbuf, inlen, outbuf, outlen,
758eb96b740SNipun Gupta outlen_actual, false);
759eb96b740SNipun Gupta }
760eb96b740SNipun Gupta
761eb96b740SNipun Gupta /**
762eb96b740SNipun Gupta * cdx_mcdi_rpc_async - Schedule an MCDI command to run asynchronously
763eb96b740SNipun Gupta * @cdx: NIC through which to issue the command
764eb96b740SNipun Gupta * @cmd: Command type number
765eb96b740SNipun Gupta * @inbuf: Command parameters
766eb96b740SNipun Gupta * @inlen: Length of command parameters, in bytes
767eb96b740SNipun Gupta * @complete: Function to be called on completion or cancellation.
768eb96b740SNipun Gupta * @cookie: Arbitrary value to be passed to @complete.
769eb96b740SNipun Gupta *
770eb96b740SNipun Gupta * This function does not sleep and therefore may be called in atomic
771eb96b740SNipun Gupta * context. It will fail if event queues are disabled or if MCDI
772eb96b740SNipun Gupta * event completions have been disabled due to an error.
773eb96b740SNipun Gupta *
774eb96b740SNipun Gupta * If it succeeds, the @complete function will be called exactly once
775eb96b740SNipun Gupta * in process context, when one of the following occurs:
776eb96b740SNipun Gupta * (a) the completion event is received (in process context)
777eb96b740SNipun Gupta * (b) event queues are disabled (in the process that disables them)
778eb96b740SNipun Gupta */
779eb96b740SNipun Gupta int
cdx_mcdi_rpc_async(struct cdx_mcdi * cdx,unsigned int cmd,const struct cdx_dword * inbuf,size_t inlen,cdx_mcdi_async_completer * complete,unsigned long cookie)780eb96b740SNipun Gupta cdx_mcdi_rpc_async(struct cdx_mcdi *cdx, unsigned int cmd,
781eb96b740SNipun Gupta const struct cdx_dword *inbuf, size_t inlen,
782eb96b740SNipun Gupta cdx_mcdi_async_completer *complete, unsigned long cookie)
783eb96b740SNipun Gupta {
784eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd_item =
785eb96b740SNipun Gupta kmalloc(sizeof(struct cdx_mcdi_cmd) + inlen, GFP_ATOMIC);
786eb96b740SNipun Gupta
787eb96b740SNipun Gupta if (!cmd_item)
788eb96b740SNipun Gupta return -ENOMEM;
789eb96b740SNipun Gupta
790eb96b740SNipun Gupta kref_init(&cmd_item->ref);
791eb96b740SNipun Gupta cmd_item->quiet = true;
792eb96b740SNipun Gupta cmd_item->cookie = cookie;
793eb96b740SNipun Gupta cmd_item->completer = complete;
794eb96b740SNipun Gupta cmd_item->cmd = cmd;
795eb96b740SNipun Gupta cmd_item->inlen = inlen;
796eb96b740SNipun Gupta /* inbuf is probably not valid after return, so take a copy */
797eb96b740SNipun Gupta cmd_item->inbuf = (struct cdx_dword *)(cmd_item + 1);
798eb96b740SNipun Gupta memcpy(cmd_item + 1, inbuf, inlen);
799eb96b740SNipun Gupta
800eb96b740SNipun Gupta return cdx_mcdi_rpc_async_internal(cdx, cmd_item, NULL);
801eb96b740SNipun Gupta }
802eb96b740SNipun Gupta
_cdx_mcdi_display_error(struct cdx_mcdi * cdx,unsigned int cmd,size_t inlen,int raw,int arg,int err_no)803eb96b740SNipun Gupta static void _cdx_mcdi_display_error(struct cdx_mcdi *cdx, unsigned int cmd,
804eb96b740SNipun Gupta size_t inlen, int raw, int arg, int err_no)
805eb96b740SNipun Gupta {
806eb96b740SNipun Gupta pr_err("MC command 0x%x inlen %d failed err_no=%d (raw=%d) arg=%d\n",
807eb96b740SNipun Gupta cmd, (int)inlen, err_no, raw, arg);
808eb96b740SNipun Gupta }
809eb96b740SNipun Gupta
810eb96b740SNipun Gupta /*
811eb96b740SNipun Gupta * Set MCDI mode to fail to prevent any new commands, then cancel any
812eb96b740SNipun Gupta * outstanding commands.
813eb96b740SNipun Gupta * Caller must hold the mcdi iface_lock.
814eb96b740SNipun Gupta */
cdx_mcdi_mode_fail(struct cdx_mcdi * cdx,struct list_head * cleanup_list)815eb96b740SNipun Gupta static void cdx_mcdi_mode_fail(struct cdx_mcdi *cdx, struct list_head *cleanup_list)
816eb96b740SNipun Gupta {
817eb96b740SNipun Gupta struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx);
818eb96b740SNipun Gupta
819eb96b740SNipun Gupta if (!mcdi)
820eb96b740SNipun Gupta return;
821eb96b740SNipun Gupta
822eb96b740SNipun Gupta mcdi->mode = MCDI_MODE_FAIL;
823eb96b740SNipun Gupta
824eb96b740SNipun Gupta while (!list_empty(&mcdi->cmd_list)) {
825eb96b740SNipun Gupta struct cdx_mcdi_cmd *cmd;
826eb96b740SNipun Gupta
827eb96b740SNipun Gupta cmd = list_first_entry(&mcdi->cmd_list, struct cdx_mcdi_cmd,
828eb96b740SNipun Gupta list);
829eb96b740SNipun Gupta _cdx_mcdi_cancel_cmd(mcdi, cdx_mcdi_cmd_handle(cmd), cleanup_list);
830eb96b740SNipun Gupta }
831eb96b740SNipun Gupta }
832