1 /*
2 * Copyright (c) 2016 Wind River Systems, Inc.
3 * Copyright (c) 2016,2022 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/kernel.h>
9 #include <zephyr/init.h>
10 #include <zephyr/drivers/i3c.h>
11
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_DECLARE(i3c, CONFIG_I3C_LOG_LEVEL);
14
15 /* Statically allocated array of IBI work item nodes */
16 static struct i3c_ibi_work i3c_ibi_work_nodes[CONFIG_I3C_IBI_WORKQUEUE_LENGTH];
17
18 static K_KERNEL_STACK_DEFINE(i3c_ibi_work_q_stack,
19 CONFIG_I3C_IBI_WORKQUEUE_STACK_SIZE);
20
21 /* IBI workqueue */
22 static struct k_work_q i3c_ibi_work_q;
23
24 static sys_slist_t i3c_ibi_work_nodes_free;
25
ibi_work_submit(struct i3c_ibi_work * ibi_node)26 static inline int ibi_work_submit(struct i3c_ibi_work *ibi_node)
27 {
28 return k_work_submit_to_queue(&i3c_ibi_work_q, &ibi_node->work);
29 }
30
i3c_ibi_work_enqueue(struct i3c_ibi_work * ibi_work)31 int i3c_ibi_work_enqueue(struct i3c_ibi_work *ibi_work)
32 {
33 sys_snode_t *node;
34 struct i3c_ibi_work *ibi_node;
35 int ret;
36
37 node = sys_slist_get(&i3c_ibi_work_nodes_free);
38 if (node == NULL) {
39 ret = -ENOMEM;
40 goto out;
41 }
42
43 ibi_node = (struct i3c_ibi_work *)node;
44
45 (void)memcpy(ibi_node, ibi_work, sizeof(*ibi_node));
46
47 ret = ibi_work_submit(ibi_node);
48 if (ret >= 0) {
49 ret = 0;
50 }
51
52 out:
53 return ret;
54 }
55
i3c_ibi_work_enqueue_target_irq(struct i3c_device_desc * target,uint8_t * payload,size_t payload_len)56 int i3c_ibi_work_enqueue_target_irq(struct i3c_device_desc *target,
57 uint8_t *payload, size_t payload_len)
58 {
59 sys_snode_t *node;
60 struct i3c_ibi_work *ibi_node;
61 int ret;
62
63 node = sys_slist_get(&i3c_ibi_work_nodes_free);
64 if (node == NULL) {
65 ret = -ENOMEM;
66 goto out;
67 }
68
69 ibi_node = (struct i3c_ibi_work *)node;
70
71 ibi_node->type = I3C_IBI_TARGET_INTR;
72 ibi_node->target = target;
73 ibi_node->payload.payload_len = payload_len;
74
75 if ((payload != NULL) && (payload_len > 0U)) {
76 (void)memcpy(&ibi_node->payload.payload[0],
77 payload, payload_len);
78 }
79
80 ret = ibi_work_submit(ibi_node);
81 if (ret >= 0) {
82 ret = 0;
83 }
84
85 out:
86 return ret;
87 }
88
i3c_ibi_work_enqueue_controller_request(struct i3c_device_desc * target)89 int i3c_ibi_work_enqueue_controller_request(struct i3c_device_desc *target)
90 {
91 sys_snode_t *node;
92 struct i3c_ibi_work *ibi_node;
93 int ret;
94
95 node = sys_slist_get(&i3c_ibi_work_nodes_free);
96 if (node == NULL) {
97 ret = -ENOMEM;
98 goto out;
99 }
100
101 ibi_node = (struct i3c_ibi_work *)node;
102
103 ibi_node->type = I3C_IBI_CONTROLLER_ROLE_REQUEST;
104 ibi_node->target = target;
105
106 ret = ibi_work_submit(ibi_node);
107 if (ret >= 0) {
108 ret = 0;
109 }
110
111 out:
112 return ret;
113 }
114
i3c_ibi_work_enqueue_hotjoin(const struct device * dev)115 int i3c_ibi_work_enqueue_hotjoin(const struct device *dev)
116 {
117 sys_snode_t *node;
118 struct i3c_ibi_work *ibi_node;
119 int ret;
120
121 node = sys_slist_get(&i3c_ibi_work_nodes_free);
122 if (node == NULL) {
123 ret = -ENOMEM;
124 goto out;
125 }
126
127 ibi_node = (struct i3c_ibi_work *)node;
128
129 ibi_node->type = I3C_IBI_HOTJOIN;
130 ibi_node->controller = dev;
131 ibi_node->payload.payload_len = 0;
132
133 ret = ibi_work_submit(ibi_node);
134 if (ret >= 0) {
135 ret = 0;
136 }
137
138 out:
139 return ret;
140 }
141
i3c_ibi_work_enqueue_cb(const struct device * dev,k_work_handler_t work_cb)142 int i3c_ibi_work_enqueue_cb(const struct device *dev,
143 k_work_handler_t work_cb)
144 {
145 sys_snode_t *node;
146 struct i3c_ibi_work *ibi_node;
147 int ret;
148
149 node = sys_slist_get(&i3c_ibi_work_nodes_free);
150 if (node == NULL) {
151 ret = -ENOMEM;
152 goto out;
153 }
154
155 ibi_node = (struct i3c_ibi_work *)node;
156
157 ibi_node->type = I3C_IBI_WORKQUEUE_CB;
158 ibi_node->controller = dev;
159 ibi_node->work_cb = work_cb;
160
161 ret = ibi_work_submit(ibi_node);
162 if (ret >= 0) {
163 ret = 0;
164 }
165
166 out:
167 return ret;
168 }
169
i3c_ibi_work_handler(struct k_work * work)170 static void i3c_ibi_work_handler(struct k_work *work)
171 {
172 struct i3c_ibi_work *ibi_node = CONTAINER_OF(work, struct i3c_ibi_work, work);
173 struct i3c_ibi_payload *payload;
174 int ret = 0;
175
176 if (IS_ENABLED(CONFIG_I3C_IBI_WORKQUEUE_VERBOSE_DEBUG) &&
177 ((uint32_t)ibi_node->type <= I3C_IBI_TYPE_MAX)) {
178 LOG_DBG("Processing IBI work %p (type %d, len %u)",
179 ibi_node, (int)ibi_node->type,
180 ibi_node->payload.payload_len);
181
182 if (ibi_node->payload.payload_len > 0U) {
183 LOG_HEXDUMP_DBG(&ibi_node->payload.payload[0],
184 ibi_node->payload.payload_len, "IBI Payload");
185 }
186 }
187
188 switch (ibi_node->type) {
189 case I3C_IBI_TARGET_INTR:
190 if (ibi_node->payload.payload_len != 0U) {
191 payload = &ibi_node->payload;
192 } else {
193 payload = NULL;
194 }
195
196 if (ibi_node->target->ibi_cb) {
197 ret = ibi_node->target->ibi_cb(ibi_node->target, payload);
198 if ((ret != 0) && (ret != -EBUSY)) {
199 LOG_ERR("IBI work %p cb returns %d", ibi_node, ret);
200 }
201 } else {
202 LOG_ERR("No IBI callback for target %s", ibi_node->target->dev->name);
203 }
204 break;
205
206 case I3C_IBI_HOTJOIN:
207 ret = i3c_do_daa(ibi_node->controller);
208 if ((ret != 0) && (ret != -EBUSY)) {
209 LOG_ERR("i3c_do_daa returns %d", ret);
210 }
211
212 if (i3c_bus_has_sec_controller(ibi_node->controller)) {
213 ret = i3c_bus_deftgts(ibi_node->controller);
214 if (ret != 0) {
215 LOG_ERR("Error sending DEFTGTS");
216 }
217 }
218 break;
219
220 case I3C_IBI_WORKQUEUE_CB:
221 if (ibi_node->work_cb != NULL) {
222 ibi_node->work_cb(work);
223 }
224 break;
225
226 case I3C_IBI_CONTROLLER_ROLE_REQUEST:
227 ret = i3c_device_controller_handoff(ibi_node->target, true);
228 if (ret != 0) {
229 LOG_ERR("i3c_device_controller_handoff returns %d", ret);
230 }
231 break;
232
233 default:
234 /* Unknown IBI type: do nothing */
235 LOG_DBG("Cannot process IBI type %d", (int)ibi_node->type);
236 break;
237 }
238
239 if (ret == -EBUSY) {
240 /* Retry if bus is busy. */
241 if (ibi_work_submit(ibi_node) < 0) {
242 LOG_ERR("Error re-adding IBI work %p", ibi_node);
243 }
244 } else {
245 /* Add the now processed node back to the free list */
246 sys_slist_append(&i3c_ibi_work_nodes_free, (sys_snode_t *)ibi_node);
247 }
248 }
249
i3c_ibi_work_q_init(void)250 static int i3c_ibi_work_q_init(void)
251 {
252 struct k_work_queue_config cfg = {
253 .name = "i3c_ibi_workq",
254 .no_yield = true,
255 };
256
257 /* Init the linked list of work item nodes */
258 sys_slist_init(&i3c_ibi_work_nodes_free);
259
260 for (int i = 0; i < ARRAY_SIZE(i3c_ibi_work_nodes); i++) {
261 i3c_ibi_work_nodes[i].work.handler = i3c_ibi_work_handler;
262
263 sys_slist_append(&i3c_ibi_work_nodes_free,
264 (sys_snode_t *)&i3c_ibi_work_nodes[i]);
265 }
266
267 /* Start the workqueue */
268 k_work_queue_start(&i3c_ibi_work_q, i3c_ibi_work_q_stack,
269 K_KERNEL_STACK_SIZEOF(i3c_ibi_work_q_stack),
270 CONFIG_I3C_IBI_WORKQUEUE_PRIORITY, &cfg);
271
272 return 0;
273 }
274
275 SYS_INIT(i3c_ibi_work_q_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
276