blob: c9bbdc04e2d768dca5241d629ca02fd3a1846755 [file] [log] [blame]
Alex Elder30c6d9d2015-05-22 13:02:08 -05001/*
2 * SVC Greybus driver.
3 *
4 * Copyright 2015 Google Inc.
5 * Copyright 2015 Linaro Ltd.
6 *
7 * Released under the GPLv2 only.
8 */
9
Alex Elder30c6d9d2015-05-22 13:02:08 -050010#include "greybus.h"
Alex Elder30c6d9d2015-05-22 13:02:08 -050011
Viresh Kumaread35462015-07-21 17:44:19 +053012static struct ida greybus_svc_device_id_map;
13
Viresh Kumard3d44842015-07-21 17:44:18 +053014/*
15 * AP's SVC cport is required early to get messages from the SVC. This happens
16 * even before the Endo is created and hence any modules or interfaces.
17 *
18 * This is a temporary connection, used only at initial bootup.
19 */
20struct gb_connection *
21gb_ap_svc_connection_create(struct greybus_host_device *hd)
22{
23 struct gb_connection *connection;
24
25 connection = gb_connection_create_range(hd, NULL, hd->parent,
26 GB_SVC_CPORT_ID,
27 GREYBUS_PROTOCOL_SVC,
28 GB_SVC_CPORT_ID,
29 GB_SVC_CPORT_ID + 1);
30
31 return connection;
32}
33EXPORT_SYMBOL_GPL(gb_ap_svc_connection_create);
34
35/*
36 * We know endo-type and AP's interface id now, lets create a proper svc
37 * connection (and its interface/bundle) now and get rid of the initial
38 * 'partially' initialized one svc connection.
39 */
40static struct gb_interface *
41gb_ap_interface_create(struct greybus_host_device *hd,
42 struct gb_connection *connection, u8 interface_id)
43{
44 struct gb_interface *intf;
45 struct device *dev = &hd->endo->dev;
46 int ret;
47
48 intf = gb_interface_create(hd, interface_id);
49 if (!intf) {
50 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
51 __func__, interface_id);
52 return NULL;
53 }
54
55 intf->device_id = GB_DEVICE_ID_AP;
56
57 /*
58 * XXX: Disable the initial svc connection here, but don't destroy it
59 * yet. We do need to send a response of 'svc-hello message' on that.
60 */
61
62 /* Establish new control CPort connection */
63 ret = gb_create_bundle_connection(intf, GREYBUS_CLASS_SVC);
64 if (ret) {
65 dev_err(&intf->dev, "%s: Failed to create svc connection (%d %d)\n",
66 __func__, interface_id, ret);
67 gb_interface_destroy(intf);
68 intf = NULL;
69 }
70
71 return intf;
72}
73
Alex Elder30c6d9d2015-05-22 13:02:08 -050074static int intf_device_id_operation(struct gb_svc *svc,
75 u8 intf_id, u8 device_id)
76{
77 struct gb_svc_intf_device_id_request request;
78
79 request.intf_id = intf_id;
80 request.device_id = device_id;
81
82 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
83 &request, sizeof(request), NULL, 0);
84}
85
86static int intf_reset_operation(struct gb_svc *svc, u8 intf_id)
87{
88 struct gb_svc_intf_reset_request request;
89
90 request.intf_id = intf_id;
91
92 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
93 &request, sizeof(request), NULL, 0);
94}
95
96static int connection_create_operation(struct gb_svc *svc,
97 u8 intf1_id, u16 cport1_id,
98 u8 intf2_id, u16 cport2_id)
99{
100 struct gb_svc_conn_create_request request;
101
102 request.intf1_id = intf1_id;
103 request.cport1_id = cport1_id;
104 request.intf2_id = intf2_id;
105 request.cport2_id = cport2_id;
106
107 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
108 &request, sizeof(request), NULL, 0);
109}
110
111static int connection_destroy_operation(struct gb_svc *svc,
112 u8 intf1_id, u16 cport1_id,
113 u8 intf2_id, u16 cport2_id)
114{
115 struct gb_svc_conn_destroy_request request;
116
117 request.intf1_id = intf1_id;
118 request.cport1_id = cport1_id;
119 request.intf2_id = intf2_id;
120 request.cport2_id = cport2_id;
121
122 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_DESTROY,
123 &request, sizeof(request), NULL, 0);
124}
125
126int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
127{
128 return intf_device_id_operation(svc, intf_id, device_id);
129}
130EXPORT_SYMBOL_GPL(gb_svc_intf_device_id);
131
132int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
133{
134 return intf_reset_operation(svc, intf_id);
135}
136EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
137
138int gb_svc_connection_create(struct gb_svc *svc,
139 u8 intf1_id, u16 cport1_id,
140 u8 intf2_id, u16 cport2_id)
141{
142 return connection_create_operation(svc, intf1_id, cport1_id,
143 intf2_id, cport2_id);
144}
145EXPORT_SYMBOL_GPL(gb_svc_connection_create);
146
147int gb_svc_connection_destroy(struct gb_svc *svc,
148 u8 intf1_id, u16 cport1_id,
149 u8 intf2_id, u16 cport2_id)
150{
151 return connection_destroy_operation(svc, intf1_id, cport1_id,
152 intf2_id, cport2_id);
153}
154EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
155
Viresh Kumaread35462015-07-21 17:44:19 +0530156static int gb_svc_version_request(struct gb_operation *op)
157{
158 struct gb_connection *connection = op->connection;
159 struct gb_protocol_version_response *version;
160 struct device *dev = &connection->dev;
161
162 version = op->request->payload;
163
164 if (version->major > GB_SVC_VERSION_MAJOR) {
165 dev_err(&connection->dev,
166 "unsupported major version (%hhu > %hhu)\n",
167 version->major, GB_SVC_VERSION_MAJOR);
168 return -ENOTSUPP;
169 }
170
171 if (!gb_operation_response_alloc(op, sizeof(*version), GFP_KERNEL)) {
172 dev_err(dev, "%s: error allocating response\n",
173 __func__);
174 return -ENOMEM;
175 }
176
177 version = op->response->payload;
178 version->major = GB_SVC_VERSION_MAJOR;
179 version->minor = GB_SVC_VERSION_MINOR;
180 return 0;
181}
182
183static int gb_svc_hello(struct gb_operation *op)
184{
185 struct gb_connection *connection = op->connection;
186 struct greybus_host_device *hd = connection->hd;
187 struct gb_svc_hello_request *hello_request;
188 struct device *dev = &connection->dev;
189 struct gb_interface *intf;
190 u16 endo_id;
191 u8 interface_id;
192 int ret;
193
194 /* Hello message should be received only during early bootup */
195 WARN_ON(hd->initial_svc_connection != connection);
196
197 /*
198 * SVC sends information about the endo and interface-id on the hello
199 * request, use that to create an endo.
200 */
201 if (op->request->payload_size != sizeof(*hello_request)) {
202 dev_err(dev, "%s: Illegal size of hello request (%d %d)\n",
203 __func__, op->request->payload_size,
204 sizeof(*hello_request));
205 return -EINVAL;
206 }
207
208 hello_request = op->request->payload;
209 endo_id = le16_to_cpu(hello_request->endo_id);
210 interface_id = hello_request->interface_id;
211
212 /* Setup Endo */
213 ret = greybus_endo_setup(hd, endo_id, interface_id);
214 if (ret)
215 return ret;
216
217 /*
218 * Endo and its modules are ready now, fix AP's partially initialized
219 * svc protocol and its connection.
220 */
221 intf = gb_ap_interface_create(hd, connection, interface_id);
222 if (!intf) {
223 gb_endo_remove(hd->endo);
224 return ret;
225 }
226
227 return 0;
228}
229
Alex Elder30c6d9d2015-05-22 13:02:08 -0500230static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
231{
232 struct gb_message *request = op->request;
Viresh Kumaread35462015-07-21 17:44:19 +0530233 struct gb_svc_intf_hotplug_request *hotplug = request->payload;
234 struct gb_svc *svc = op->connection->private;
235 struct greybus_host_device *hd = op->connection->bundle->intf->hd;
236 struct device *dev = &op->connection->dev;
237 struct gb_interface *intf;
238 u8 intf_id, device_id;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500239 u32 unipro_mfg_id;
240 u32 unipro_prod_id;
241 u32 ara_vend_id;
242 u32 ara_prod_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530243 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500244
245 if (request->payload_size < sizeof(*hotplug)) {
Viresh Kumaread35462015-07-21 17:44:19 +0530246 dev_err(dev, "%s: short hotplug request received\n", __func__);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500247 return -EINVAL;
248 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500249
250 /*
251 * Grab the information we need.
252 *
253 * XXX I'd really like to acknowledge receipt, and then
254 * XXX continue processing the request. There's no need
255 * XXX for the SVC to wait. In fact, it might be best to
256 * XXX have the SVC get acknowledgement before we proceed.
Viresh Kumar7eb89192015-07-01 12:13:50 +0530257 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500258 intf_id = hotplug->intf_id;
Phong Tranea15a402015-05-27 21:31:02 +0700259 unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
260 unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
261 ara_vend_id = le32_to_cpu(hotplug->data.ara_vend_id);
262 ara_prod_id = le32_to_cpu(hotplug->data.ara_prod_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500263
Viresh Kumaread35462015-07-21 17:44:19 +0530264 // FIXME May require firmware download
265 intf = gb_interface_create(hd, intf_id);
266 if (!intf) {
267 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
268 __func__, intf_id);
269 return -EINVAL;
270 }
271
272 /*
273 * Create a device id for the interface:
274 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
275 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
276 *
277 * XXX Do we need to allocate device ID for SVC or the AP here? And what
278 * XXX about an AP with multiple interface blocks?
279 */
280 device_id = ida_simple_get(&greybus_svc_device_id_map,
281 GB_DEVICE_ID_MODULES_START, 0, GFP_ATOMIC);
282 if (device_id < 0) {
283 ret = device_id;
284 dev_err(dev, "%s: Failed to allocate device id for interface with id %hhu (%d)\n",
285 __func__, intf_id, ret);
286 goto destroy_interface;
287 }
288
289 ret = intf_device_id_operation(svc, intf_id, device_id);
290 if (ret) {
291 dev_err(dev, "%s: Device id operation failed, interface %hhu device_id %hhu (%d)\n",
292 __func__, intf_id, device_id, ret);
293 goto ida_put;
294 }
295
296 ret = gb_interface_init(intf, device_id);
297 if (ret) {
298 dev_err(dev, "%s: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n",
299 __func__, intf_id, device_id, ret);
300 goto svc_id_free;
301 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500302
303 return 0;
Viresh Kumaread35462015-07-21 17:44:19 +0530304
305svc_id_free:
306 /*
307 * XXX Should we tell SVC that this id doesn't belong to interface
308 * XXX anymore.
309 */
310ida_put:
311 ida_simple_remove(&greybus_svc_device_id_map, device_id);
312destroy_interface:
313 gb_interface_remove(hd, intf_id);
314
315 return ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500316}
317
318static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
319{
320 struct gb_message *request = op->request;
Viresh Kumaread35462015-07-21 17:44:19 +0530321 struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload;
322 struct greybus_host_device *hd = op->connection->bundle->intf->hd;
323 struct device *dev = &op->connection->dev;
324 u8 device_id;
325 struct gb_interface *intf;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500326 u8 intf_id;
327
328 if (request->payload_size < sizeof(*hot_unplug)) {
329 dev_err(&op->connection->dev,
330 "short hot unplug request received\n");
331 return -EINVAL;
332 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500333
334 intf_id = hot_unplug->intf_id;
335
Viresh Kumaread35462015-07-21 17:44:19 +0530336 intf = gb_interface_find(hd, intf_id);
337 if (!intf) {
338 dev_err(dev, "%s: Couldn't find interface for id %hhu\n",
339 __func__, intf_id);
340 return -EINVAL;
341 }
342
343 device_id = intf->device_id;
344 gb_interface_remove(hd, intf_id);
345 ida_simple_remove(&greybus_svc_device_id_map, device_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500346
347 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500348}
349
350static int gb_svc_intf_reset_recv(struct gb_operation *op)
351{
352 struct gb_message *request = op->request;
353 struct gb_svc_intf_reset_request *reset;
354 u8 intf_id;
355
356 if (request->payload_size < sizeof(*reset)) {
357 dev_err(&op->connection->dev,
358 "short reset request received\n");
359 return -EINVAL;
360 }
361 reset = request->payload;
362
363 intf_id = reset->intf_id;
364
365 /* FIXME Reset the interface here */
366
367 return 0;
368}
369
370static int gb_svc_request_recv(u8 type, struct gb_operation *op)
371{
372 switch (type) {
Viresh Kumaread35462015-07-21 17:44:19 +0530373 case GB_SVC_TYPE_PROTOCOL_VERSION:
374 return gb_svc_version_request(op);
375 case GB_SVC_TYPE_SVC_HELLO:
376 return gb_svc_hello(op);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500377 case GB_SVC_TYPE_INTF_HOTPLUG:
378 return gb_svc_intf_hotplug_recv(op);
379 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
380 return gb_svc_intf_hot_unplug_recv(op);
381 case GB_SVC_TYPE_INTF_RESET:
382 return gb_svc_intf_reset_recv(op);
383 default:
384 dev_err(&op->connection->dev,
385 "unsupported request: %hhu\n", type);
386 return -EINVAL;
387 }
388}
389
Alex Elder30c6d9d2015-05-22 13:02:08 -0500390static int gb_svc_connection_init(struct gb_connection *connection)
391{
392 struct gb_svc *svc;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500393
394 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
395 if (!svc)
396 return -ENOMEM;
397
398 svc->connection = connection;
399 connection->private = svc;
Viresh Kumard3d44842015-07-21 17:44:18 +0530400
401 /*
402 * SVC connection is created twice:
403 * - before the interface-id of the AP and the endo type is known.
404 * - after receiving endo type and interface-id of the AP from the SVC.
405 *
406 * We should do light-weight initialization for the first case.
407 */
408 if (!connection->bundle) {
409 WARN_ON(connection->hd->initial_svc_connection);
410 connection->hd->initial_svc_connection = connection;
411 return 0;
412 }
413
414 ida_init(&greybus_svc_device_id_map);
415
Viresh Kumar6ceb8fd2015-07-03 17:00:30 +0530416 /* Set interface's svc connection */
417 connection->bundle->intf->svc = svc;
418
Viresh Kumar18d777c2015-07-21 17:44:20 +0530419 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500420}
421
422static void gb_svc_connection_exit(struct gb_connection *connection)
423{
424 struct gb_svc *svc = connection->private;
425
Viresh Kumard3d44842015-07-21 17:44:18 +0530426 if (connection->hd->initial_svc_connection == connection) {
427 connection->hd->initial_svc_connection = NULL;
428 } else {
429 if (WARN_ON(connection->bundle->intf->svc != svc))
430 return;
431 connection->bundle->intf->svc = NULL;
432 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500433
Viresh Kumard3d44842015-07-21 17:44:18 +0530434 connection->private = NULL;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500435 kfree(svc);
436}
437
438static struct gb_protocol svc_protocol = {
439 .name = "svc",
440 .id = GREYBUS_PROTOCOL_SVC,
Viresh Kumar06e305f2015-07-01 12:13:51 +0530441 .major = GB_SVC_VERSION_MAJOR,
442 .minor = GB_SVC_VERSION_MINOR,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500443 .connection_init = gb_svc_connection_init,
444 .connection_exit = gb_svc_connection_exit,
445 .request_recv = gb_svc_request_recv,
446};
Viresh Kumarab69c4c2015-07-03 17:00:29 +0530447gb_builtin_protocol_driver(svc_protocol);