blob: a99ca84d6ce2dafae73c38b027756b0b824e65c8 [file] [log] [blame]
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +01001/*
2 * Copyright (c) 2022 ARM Limited.
3 *
4 * This program is free software and is provided to you under the terms of the
5 * GNU General Public License version 2 as published by the Free Software
6 * Foundation, and any use by you of this program is subject to the terms
7 * of such GNU licence.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * SPDX-License-Identifier: GPL-2.0-only
19 */
20
21/****************************************************************************
22 * Includes
23 ****************************************************************************/
24
25#include "ethosu_network_info.h"
26
27#include "ethosu_device.h"
28#include "ethosu_network.h"
29#include "ethosu_mailbox.h"
30#include "uapi/ethosu.h"
31
Davide Grohmann32660f92022-04-27 16:49:07 +020032#define NETWORK_INFO_RESP_TIMEOUT_MS 3000
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010033
Davide Grohmann32660f92022-04-27 16:49:07 +020034static inline int ethosu_network_info_send(struct ethosu_network_info *info)
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010035{
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010036 /* Send network info request to firmware */
Davide Grohmann32660f92022-04-27 16:49:07 +020037 return ethosu_mailbox_network_info_request(&info->edev->mailbox,
38 &info->msg,
39 info->net->buf,
40 info->net->index);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010041}
42
43static void ethosu_network_info_fail(struct ethosu_mailbox_msg *msg)
44{
45 struct ethosu_network_info *info =
46 container_of(msg, typeof(*info), msg);
47
48 if (completion_done(&info->done))
49 return;
50
51 info->errno = -EFAULT;
52 complete(&info->done);
53}
54
55static int ethosu_network_info_resend(struct ethosu_mailbox_msg *msg)
56{
57 struct ethosu_network_info *info =
58 container_of(msg, typeof(*info), msg);
59 int ret;
60
61 /* Don't resend request if response has already been received */
62 if (completion_done(&info->done))
63 return 0;
64
65 /* Resend request */
66 ret = ethosu_network_info_send(info);
67 if (ret)
68 return ret;
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010069
70 return 0;
71}
72
Davide Grohmann32660f92022-04-27 16:49:07 +020073int ethosu_network_info_request(struct ethosu_network *net,
74 struct ethosu_uapi_network_info *uapi)
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010075{
76 struct ethosu_network_info *info;
77 int ret;
Davide Grohmann32660f92022-04-27 16:49:07 +020078 int timeout;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010079
Davide Grohmann32660f92022-04-27 16:49:07 +020080 info = devm_kzalloc(net->edev->dev, sizeof(*info), GFP_KERNEL);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010081 if (!info)
Davide Grohmann32660f92022-04-27 16:49:07 +020082 return -ENOMEM;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010083
Davide Grohmann32660f92022-04-27 16:49:07 +020084 info->edev = net->edev;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010085 info->net = net;
86 info->uapi = uapi;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010087 init_completion(&info->done);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010088 info->msg.fail = ethosu_network_info_fail;
89 info->msg.resend = ethosu_network_info_resend;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010090
Davide Grohmann32660f92022-04-27 16:49:07 +020091 ret = ethosu_mailbox_register(&info->edev->mailbox, &info->msg);
92 if (ret < 0)
93 goto kfree;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010094
95 /* Get reference to network */
Davide Grohmann32660f92022-04-27 16:49:07 +020096 ethosu_network_get(info->net);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010097
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010098 ret = ethosu_network_info_send(info);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010099 if (ret)
Davide Grohmann32660f92022-04-27 16:49:07 +0200100 goto deregister;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100101
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200102 dev_info(info->edev->dev,
103 "Network info create. info=0x%pK, net=0x%pK, msg.id=0x%x\n",
104 info, info->net, info->msg.id);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100105
Davide Grohmann32660f92022-04-27 16:49:07 +0200106 /* Unlock the device mutex and wait for completion */
107 mutex_unlock(&info->edev->mutex);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100108 timeout = wait_for_completion_timeout(&info->done,
Davide Grohmann32660f92022-04-27 16:49:07 +0200109 msecs_to_jiffies(
110 NETWORK_INFO_RESP_TIMEOUT_MS));
111 mutex_lock(&info->edev->mutex);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100112
Davide Grohmann32660f92022-04-27 16:49:07 +0200113 if (0 == timeout) {
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200114 dev_warn(info->edev->dev, "Network info timed out. info=0x%pK",
115 info);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100116
Davide Grohmann32660f92022-04-27 16:49:07 +0200117 ret = -ETIME;
118 goto deregister;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100119 }
120
Kristofer Jonssone3e26432022-06-28 11:29:42 +0200121 ret = info->errno;
122
Davide Grohmann32660f92022-04-27 16:49:07 +0200123deregister:
124 ethosu_mailbox_deregister(&info->edev->mailbox, &info->msg);
125 ethosu_network_put(info->net);
126
127kfree:
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200128 dev_info(info->edev->dev,
129 "Network info destroy. info=0x%pK, msg.id=0x%x\n",
130 info, info->msg.id);
Davide Grohmann32660f92022-04-27 16:49:07 +0200131 devm_kfree(info->edev->dev, info);
132
133 return ret;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100134}
135
136void ethosu_network_info_rsp(struct ethosu_device *edev,
137 struct ethosu_core_network_info_rsp *rsp)
138{
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100139 int ret;
Davide Grohmann32660f92022-04-27 16:49:07 +0200140 int id = (int)rsp->user_arg;
141 struct ethosu_mailbox_msg *msg;
142 struct ethosu_network_info *info;
143 uint32_t i;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100144
Davide Grohmann32660f92022-04-27 16:49:07 +0200145 msg = ethosu_mailbox_find(&edev->mailbox, id);
146 if (IS_ERR(msg)) {
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100147 dev_warn(edev->dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200148 "Id for network info msg not found. msg.id=0x%x\n",
Davide Grohmann32660f92022-04-27 16:49:07 +0200149 id);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100150
151 return;
152 }
153
Davide Grohmann32660f92022-04-27 16:49:07 +0200154 info = container_of(msg, typeof(*info), msg);
155
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100156 if (completion_done(&info->done))
157 return;
158
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100159 info->errno = 0;
160
161 if (rsp->status != ETHOSU_CORE_STATUS_OK) {
162 info->errno = -EBADF;
163 goto signal_complete;
164 }
165
166 if (rsp->ifm_count > ETHOSU_FD_MAX || rsp->ofm_count > ETHOSU_FD_MAX) {
167 info->errno = -ENFILE;
168 goto signal_complete;
169 }
170
Davide Grohmann6ab0b6b2022-05-17 16:11:14 +0200171 ret = strscpy(info->uapi->desc, rsp->desc, sizeof(info->uapi->desc));
172 if (ret < 0) {
173 info->errno = ret;
174 goto signal_complete;
175 }
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100176
177 info->uapi->ifm_count = rsp->ifm_count;
178 for (i = 0; i < rsp->ifm_count; i++)
179 info->uapi->ifm_size[i] = rsp->ifm_size[i];
180
181 info->uapi->ofm_count = rsp->ofm_count;
182 for (i = 0; i < rsp->ofm_count; i++)
183 info->uapi->ofm_size[i] = rsp->ofm_size[i];
184
185signal_complete:
186 complete(&info->done);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100187}