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_hotjoin(const struct device * dev)89 int i3c_ibi_work_enqueue_hotjoin(const struct device *dev)
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_HOTJOIN;
104 ibi_node->controller = dev;
105 ibi_node->payload.payload_len = 0;
106
107 ret = ibi_work_submit(ibi_node);
108 if (ret >= 0) {
109 ret = 0;
110 }
111
112 out:
113 return ret;
114 }
115
i3c_ibi_work_enqueue_cb(const struct device * dev,k_work_handler_t work_cb)116 int i3c_ibi_work_enqueue_cb(const struct device *dev,
117 k_work_handler_t work_cb)
118 {
119 sys_snode_t *node;
120 struct i3c_ibi_work *ibi_node;
121 int ret;
122
123 node = sys_slist_get(&i3c_ibi_work_nodes_free);
124 if (node == NULL) {
125 ret = -ENOMEM;
126 goto out;
127 }
128
129 ibi_node = (struct i3c_ibi_work *)node;
130
131 ibi_node->type = I3C_IBI_WORKQUEUE_CB;
132 ibi_node->controller = dev;
133 ibi_node->work_cb = work_cb;
134
135 ret = ibi_work_submit(ibi_node);
136 if (ret >= 0) {
137 ret = 0;
138 }
139
140 out:
141 return ret;
142 }
143
i3c_ibi_work_handler(struct k_work * work)144 static void i3c_ibi_work_handler(struct k_work *work)
145 {
146 struct i3c_ibi_work *ibi_node = CONTAINER_OF(work, struct i3c_ibi_work, work);
147 struct i3c_ibi_payload *payload;
148 int ret = 0;
149
150 if (IS_ENABLED(CONFIG_I3C_IBI_WORKQUEUE_VERBOSE_DEBUG) &&
151 ((uint32_t)ibi_node->type <= I3C_IBI_TYPE_MAX)) {
152 LOG_DBG("Processing IBI work %p (type %d, len %u)",
153 ibi_node, (int)ibi_node->type,
154 ibi_node->payload.payload_len);
155
156 if (ibi_node->payload.payload_len > 0U) {
157 LOG_HEXDUMP_DBG(&ibi_node->payload.payload[0],
158 ibi_node->payload.payload_len, "IBI Payload");
159 }
160 }
161
162 switch (ibi_node->type) {
163 case I3C_IBI_TARGET_INTR:
164 if (ibi_node->payload.payload_len != 0U) {
165 payload = &ibi_node->payload;
166 } else {
167 payload = NULL;
168 }
169
170 ret = ibi_node->target->ibi_cb(ibi_node->target, payload);
171 if ((ret != 0) && (ret != -EBUSY)) {
172 LOG_ERR("IBI work %p cb returns %d", ibi_node, ret);
173 }
174 break;
175
176 case I3C_IBI_HOTJOIN:
177 ret = i3c_do_daa(ibi_node->controller);
178 if ((ret != 0) && (ret != -EBUSY)) {
179 LOG_ERR("i3c_do_daa returns %d", ret);
180 }
181
182 if (i3c_bus_has_sec_controller(ibi_node->controller)) {
183 ret = i3c_bus_deftgts(ibi_node->controller);
184 if (ret != 0) {
185 LOG_ERR("Error sending DEFTGTS");
186 }
187 }
188 break;
189
190 case I3C_IBI_WORKQUEUE_CB:
191 if (ibi_node->work_cb != NULL) {
192 ibi_node->work_cb(work);
193 }
194 break;
195
196 case I3C_IBI_CONTROLLER_ROLE_REQUEST:
197 /* TODO: Add support for controller role request */
198 __fallthrough;
199
200 default:
201 /* Unknown IBI type: do nothing */
202 LOG_DBG("Cannot process IBI type %d", (int)ibi_node->type);
203 break;
204 }
205
206 if (ret == -EBUSY) {
207 /* Retry if bus is busy. */
208 if (ibi_work_submit(ibi_node) < 0) {
209 LOG_ERR("Error re-adding IBI work %p", ibi_node);
210 }
211 } else {
212 /* Add the now processed node back to the free list */
213 sys_slist_append(&i3c_ibi_work_nodes_free, (sys_snode_t *)ibi_node);
214 }
215 }
216
i3c_ibi_work_q_init(void)217 static int i3c_ibi_work_q_init(void)
218 {
219 struct k_work_queue_config cfg = {
220 .name = "i3c_ibi_workq",
221 .no_yield = true,
222 };
223
224 /* Init the linked list of work item nodes */
225 sys_slist_init(&i3c_ibi_work_nodes_free);
226
227 for (int i = 0; i < ARRAY_SIZE(i3c_ibi_work_nodes); i++) {
228 i3c_ibi_work_nodes[i].work.handler = i3c_ibi_work_handler;
229
230 sys_slist_append(&i3c_ibi_work_nodes_free,
231 (sys_snode_t *)&i3c_ibi_work_nodes[i]);
232 }
233
234 /* Start the workqueue */
235 k_work_queue_start(&i3c_ibi_work_q, i3c_ibi_work_q_stack,
236 K_KERNEL_STACK_SIZEOF(i3c_ibi_work_q_stack),
237 CONFIG_I3C_IBI_WORKQUEUE_PRIORITY, &cfg);
238
239 return 0;
240 }
241
242 SYS_INIT(i3c_ibi_work_q_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
243