blob: 4bf55e0e4f3440ade912da86e6ad9806d8116574 [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"
Viresh Kumar067906f2015-08-06 12:44:55 +053011#include <linux/workqueue.h>
Alex Elder30c6d9d2015-05-22 13:02:08 -050012
Perry Hung0b226492015-07-24 19:02:34 -040013#define CPORT_FLAGS_E2EFC (1)
14#define CPORT_FLAGS_CSD_N (2)
15#define CPORT_FLAGS_CSV_N (4)
16
Viresh Kumarb45864d2015-07-24 15:32:21 +053017struct gb_svc {
18 struct gb_connection *connection;
19 u8 version_major;
20 u8 version_minor;
21};
22
Viresh Kumar067906f2015-08-06 12:44:55 +053023struct svc_hotplug {
24 struct work_struct work;
25 struct gb_connection *connection;
26 struct gb_svc_intf_hotplug_request data;
27};
28
Viresh Kumaread35462015-07-21 17:44:19 +053029static struct ida greybus_svc_device_id_map;
30
Viresh Kumard3d44842015-07-21 17:44:18 +053031/*
32 * AP's SVC cport is required early to get messages from the SVC. This happens
33 * even before the Endo is created and hence any modules or interfaces.
34 *
35 * This is a temporary connection, used only at initial bootup.
36 */
37struct gb_connection *
38gb_ap_svc_connection_create(struct greybus_host_device *hd)
39{
40 struct gb_connection *connection;
41
42 connection = gb_connection_create_range(hd, NULL, hd->parent,
43 GB_SVC_CPORT_ID,
44 GREYBUS_PROTOCOL_SVC,
45 GB_SVC_CPORT_ID,
46 GB_SVC_CPORT_ID + 1);
47
48 return connection;
49}
Viresh Kumard3d44842015-07-21 17:44:18 +053050
51/*
52 * We know endo-type and AP's interface id now, lets create a proper svc
53 * connection (and its interface/bundle) now and get rid of the initial
54 * 'partially' initialized one svc connection.
55 */
56static struct gb_interface *
57gb_ap_interface_create(struct greybus_host_device *hd,
58 struct gb_connection *connection, u8 interface_id)
59{
60 struct gb_interface *intf;
61 struct device *dev = &hd->endo->dev;
Viresh Kumard3d44842015-07-21 17:44:18 +053062
63 intf = gb_interface_create(hd, interface_id);
64 if (!intf) {
65 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
66 __func__, interface_id);
67 return NULL;
68 }
69
70 intf->device_id = GB_DEVICE_ID_AP;
Viresh Kumar67c93ae2015-07-24 15:32:19 +053071 svc_update_connection(intf, connection);
Viresh Kumard3d44842015-07-21 17:44:18 +053072
Viresh Kumardcd05002015-07-24 15:32:20 +053073 /* Its no longer a partially initialized connection */
74 hd->initial_svc_connection = NULL;
75
Viresh Kumard3d44842015-07-21 17:44:18 +053076 return intf;
77}
78
Alex Elder30c6d9d2015-05-22 13:02:08 -050079static int intf_device_id_operation(struct gb_svc *svc,
80 u8 intf_id, u8 device_id)
81{
82 struct gb_svc_intf_device_id_request request;
83
84 request.intf_id = intf_id;
85 request.device_id = device_id;
86
87 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
88 &request, sizeof(request), NULL, 0);
89}
90
91static int intf_reset_operation(struct gb_svc *svc, u8 intf_id)
92{
93 struct gb_svc_intf_reset_request request;
94
95 request.intf_id = intf_id;
96
97 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
98 &request, sizeof(request), NULL, 0);
99}
100
101static int connection_create_operation(struct gb_svc *svc,
102 u8 intf1_id, u16 cport1_id,
103 u8 intf2_id, u16 cport2_id)
104{
105 struct gb_svc_conn_create_request request;
106
107 request.intf1_id = intf1_id;
108 request.cport1_id = cport1_id;
109 request.intf2_id = intf2_id;
110 request.cport2_id = cport2_id;
Perry Hung0b226492015-07-24 19:02:34 -0400111 /*
112 * XXX: fix connections paramaters to TC0 and all CPort flags
113 * for now.
114 */
115 request.tc = 0;
116 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500117
118 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
119 &request, sizeof(request), NULL, 0);
120}
121
122static int connection_destroy_operation(struct gb_svc *svc,
123 u8 intf1_id, u16 cport1_id,
124 u8 intf2_id, u16 cport2_id)
125{
126 struct gb_svc_conn_destroy_request request;
127
128 request.intf1_id = intf1_id;
129 request.cport1_id = cport1_id;
130 request.intf2_id = intf2_id;
131 request.cport2_id = cport2_id;
132
133 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_DESTROY,
134 &request, sizeof(request), NULL, 0);
135}
136
Perry Hunge08aaa42015-07-24 19:02:31 -0400137static int route_create_operation(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
138 u8 intf2_id, u8 dev2_id)
139{
140 struct gb_svc_route_create_request request;
141
142 request.intf1_id = intf1_id;
143 request.dev1_id = dev1_id;
144 request.intf2_id = intf2_id;
145 request.dev2_id = dev2_id;
146
147 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
148 &request, sizeof(request), NULL, 0);
149}
150
Alex Elder30c6d9d2015-05-22 13:02:08 -0500151int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
152{
153 return intf_device_id_operation(svc, intf_id, device_id);
154}
155EXPORT_SYMBOL_GPL(gb_svc_intf_device_id);
156
157int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
158{
159 return intf_reset_operation(svc, intf_id);
160}
161EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
162
163int gb_svc_connection_create(struct gb_svc *svc,
164 u8 intf1_id, u16 cport1_id,
165 u8 intf2_id, u16 cport2_id)
166{
167 return connection_create_operation(svc, intf1_id, cport1_id,
168 intf2_id, cport2_id);
169}
170EXPORT_SYMBOL_GPL(gb_svc_connection_create);
171
172int gb_svc_connection_destroy(struct gb_svc *svc,
173 u8 intf1_id, u16 cport1_id,
174 u8 intf2_id, u16 cport2_id)
175{
176 return connection_destroy_operation(svc, intf1_id, cport1_id,
177 intf2_id, cport2_id);
178}
179EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
180
Perry Hunge08aaa42015-07-24 19:02:31 -0400181int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
Viresh Kumar03cb4fa2015-07-29 11:44:08 +0530182 u8 intf2_id, u8 dev2_id)
183{
Perry Hunge08aaa42015-07-24 19:02:31 -0400184 return route_create_operation(svc, intf1_id, dev1_id,
185 intf2_id, dev2_id);
186}
187EXPORT_SYMBOL_GPL(gb_svc_route_create);
188
Viresh Kumaread35462015-07-21 17:44:19 +0530189static int gb_svc_version_request(struct gb_operation *op)
190{
191 struct gb_connection *connection = op->connection;
192 struct gb_protocol_version_response *version;
193 struct device *dev = &connection->dev;
194
195 version = op->request->payload;
196
197 if (version->major > GB_SVC_VERSION_MAJOR) {
198 dev_err(&connection->dev,
199 "unsupported major version (%hhu > %hhu)\n",
200 version->major, GB_SVC_VERSION_MAJOR);
201 return -ENOTSUPP;
202 }
203
204 if (!gb_operation_response_alloc(op, sizeof(*version), GFP_KERNEL)) {
205 dev_err(dev, "%s: error allocating response\n",
206 __func__);
207 return -ENOMEM;
208 }
209
210 version = op->response->payload;
211 version->major = GB_SVC_VERSION_MAJOR;
212 version->minor = GB_SVC_VERSION_MINOR;
213 return 0;
214}
215
216static int gb_svc_hello(struct gb_operation *op)
217{
218 struct gb_connection *connection = op->connection;
219 struct greybus_host_device *hd = connection->hd;
220 struct gb_svc_hello_request *hello_request;
221 struct device *dev = &connection->dev;
222 struct gb_interface *intf;
223 u16 endo_id;
224 u8 interface_id;
225 int ret;
226
227 /* Hello message should be received only during early bootup */
228 WARN_ON(hd->initial_svc_connection != connection);
229
230 /*
231 * SVC sends information about the endo and interface-id on the hello
232 * request, use that to create an endo.
233 */
234 if (op->request->payload_size != sizeof(*hello_request)) {
Viresh Kumar155b9f12015-07-23 10:30:47 +0530235 dev_err(dev, "%s: Illegal size of hello request (%zu %zu)\n",
Viresh Kumaread35462015-07-21 17:44:19 +0530236 __func__, op->request->payload_size,
237 sizeof(*hello_request));
238 return -EINVAL;
239 }
240
241 hello_request = op->request->payload;
242 endo_id = le16_to_cpu(hello_request->endo_id);
243 interface_id = hello_request->interface_id;
244
245 /* Setup Endo */
246 ret = greybus_endo_setup(hd, endo_id, interface_id);
247 if (ret)
248 return ret;
249
250 /*
251 * Endo and its modules are ready now, fix AP's partially initialized
252 * svc protocol and its connection.
253 */
254 intf = gb_ap_interface_create(hd, connection, interface_id);
255 if (!intf) {
256 gb_endo_remove(hd->endo);
257 return ret;
258 }
259
260 return 0;
261}
262
Viresh Kumar067906f2015-08-06 12:44:55 +0530263/*
264 * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it
265 * returns, irrespective of success or Failure in bringing up the module.
266 */
267static void svc_process_hotplug(struct work_struct *work)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500268{
Viresh Kumar067906f2015-08-06 12:44:55 +0530269 struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug,
270 work);
271 struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data;
272 struct gb_connection *connection = svc_hotplug->connection;
273 struct gb_svc *svc = connection->private;
274 struct greybus_host_device *hd = connection->bundle->intf->hd;
275 struct device *dev = &connection->dev;
Viresh Kumaread35462015-07-21 17:44:19 +0530276 struct gb_interface *intf;
277 u8 intf_id, device_id;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500278 u32 unipro_mfg_id;
279 u32 unipro_prod_id;
280 u32 ara_vend_id;
281 u32 ara_prod_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530282 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500283
Alex Elder30c6d9d2015-05-22 13:02:08 -0500284 /*
285 * Grab the information we need.
Viresh Kumar7eb89192015-07-01 12:13:50 +0530286 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500287 intf_id = hotplug->intf_id;
Phong Tranea15a402015-05-27 21:31:02 +0700288 unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
289 unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
290 ara_vend_id = le32_to_cpu(hotplug->data.ara_vend_id);
291 ara_prod_id = le32_to_cpu(hotplug->data.ara_prod_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500292
Viresh Kumaread35462015-07-21 17:44:19 +0530293 // FIXME May require firmware download
294 intf = gb_interface_create(hd, intf_id);
295 if (!intf) {
296 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
297 __func__, intf_id);
Viresh Kumar067906f2015-08-06 12:44:55 +0530298 goto free_svc_hotplug;
Viresh Kumaread35462015-07-21 17:44:19 +0530299 }
300
301 /*
302 * Create a device id for the interface:
303 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
304 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
305 *
306 * XXX Do we need to allocate device ID for SVC or the AP here? And what
307 * XXX about an AP with multiple interface blocks?
308 */
309 device_id = ida_simple_get(&greybus_svc_device_id_map,
310 GB_DEVICE_ID_MODULES_START, 0, GFP_ATOMIC);
311 if (device_id < 0) {
312 ret = device_id;
313 dev_err(dev, "%s: Failed to allocate device id for interface with id %hhu (%d)\n",
314 __func__, intf_id, ret);
315 goto destroy_interface;
316 }
317
318 ret = intf_device_id_operation(svc, intf_id, device_id);
319 if (ret) {
320 dev_err(dev, "%s: Device id operation failed, interface %hhu device_id %hhu (%d)\n",
321 __func__, intf_id, device_id, ret);
322 goto ida_put;
323 }
324
Perry Hung7e275462015-07-24 19:02:32 -0400325 /*
326 * Create a two-way route between the AP and the new interface
327 */
328 ret = route_create_operation(svc, hd->endo->ap_intf_id,
329 GB_DEVICE_ID_AP, intf_id, device_id);
330 if (ret) {
331 dev_err(dev, "%s: Route create operation failed, interface %hhu device_id %hhu (%d)\n",
332 __func__, intf_id, device_id, ret);
333 goto ida_put;
334 }
335
336 ret = route_create_operation(svc, intf_id, device_id,
337 hd->endo->ap_intf_id, GB_DEVICE_ID_AP);
338 if (ret) {
339 dev_err(dev, "%s: Route create operation failed, interface %hhu device_id %hhu (%d)\n",
340 __func__, intf_id, device_id, ret);
341 goto ida_put;
342 }
343
Viresh Kumaread35462015-07-21 17:44:19 +0530344 ret = gb_interface_init(intf, device_id);
345 if (ret) {
346 dev_err(dev, "%s: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n",
347 __func__, intf_id, device_id, ret);
348 goto svc_id_free;
349 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500350
Viresh Kumar067906f2015-08-06 12:44:55 +0530351 goto free_svc_hotplug;
Viresh Kumaread35462015-07-21 17:44:19 +0530352
353svc_id_free:
354 /*
355 * XXX Should we tell SVC that this id doesn't belong to interface
356 * XXX anymore.
357 */
358ida_put:
359 ida_simple_remove(&greybus_svc_device_id_map, device_id);
360destroy_interface:
361 gb_interface_remove(hd, intf_id);
Viresh Kumar067906f2015-08-06 12:44:55 +0530362free_svc_hotplug:
363 kfree(svc_hotplug);
364}
Viresh Kumaread35462015-07-21 17:44:19 +0530365
Viresh Kumar067906f2015-08-06 12:44:55 +0530366/*
367 * Bringing up a module can be time consuming, as that may require lots of
368 * initialization on the module side. Over that, we may also need to download
369 * the firmware first and flash that on the module.
370 *
371 * In order to make other hotplug events to not wait for all this to finish,
372 * handle most of module hotplug stuff outside of the hotplug callback, with
373 * help of a workqueue.
374 */
375static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
376{
377 struct gb_message *request = op->request;
378 struct svc_hotplug *svc_hotplug;
379
380 if (request->payload_size < sizeof(svc_hotplug->data)) {
381 dev_err(&op->connection->dev,
382 "%s: short hotplug request received (%zu < %zu)\n",
383 __func__, request->payload_size,
384 sizeof(svc_hotplug->data));
385 return -EINVAL;
386 }
387
388 svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_ATOMIC);
389 if (!svc_hotplug)
390 return -ENOMEM;
391
392 svc_hotplug->connection = op->connection;
393 memcpy(&svc_hotplug->data, op->request->payload, sizeof(svc_hotplug->data));
394
395 INIT_WORK(&svc_hotplug->work, svc_process_hotplug);
396 queue_work(system_unbound_wq, &svc_hotplug->work);
397
398 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500399}
400
401static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
402{
403 struct gb_message *request = op->request;
Viresh Kumaread35462015-07-21 17:44:19 +0530404 struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload;
405 struct greybus_host_device *hd = op->connection->bundle->intf->hd;
406 struct device *dev = &op->connection->dev;
407 u8 device_id;
408 struct gb_interface *intf;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500409 u8 intf_id;
410
411 if (request->payload_size < sizeof(*hot_unplug)) {
412 dev_err(&op->connection->dev,
Viresh Kumar6d05ad32015-08-06 12:44:54 +0530413 "short hot unplug request received (%zu < %zu)\n",
414 request->payload_size, sizeof(*hot_unplug));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500415 return -EINVAL;
416 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500417
418 intf_id = hot_unplug->intf_id;
419
Viresh Kumaread35462015-07-21 17:44:19 +0530420 intf = gb_interface_find(hd, intf_id);
421 if (!intf) {
422 dev_err(dev, "%s: Couldn't find interface for id %hhu\n",
423 __func__, intf_id);
424 return -EINVAL;
425 }
426
427 device_id = intf->device_id;
428 gb_interface_remove(hd, intf_id);
429 ida_simple_remove(&greybus_svc_device_id_map, device_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500430
431 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500432}
433
434static int gb_svc_intf_reset_recv(struct gb_operation *op)
435{
436 struct gb_message *request = op->request;
437 struct gb_svc_intf_reset_request *reset;
438 u8 intf_id;
439
440 if (request->payload_size < sizeof(*reset)) {
441 dev_err(&op->connection->dev,
Viresh Kumar6d05ad32015-08-06 12:44:54 +0530442 "short reset request received (%zu < %zu)\n",
443 request->payload_size, sizeof(*reset));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500444 return -EINVAL;
445 }
446 reset = request->payload;
447
448 intf_id = reset->intf_id;
449
450 /* FIXME Reset the interface here */
451
452 return 0;
453}
454
455static int gb_svc_request_recv(u8 type, struct gb_operation *op)
456{
457 switch (type) {
Viresh Kumaread35462015-07-21 17:44:19 +0530458 case GB_SVC_TYPE_PROTOCOL_VERSION:
459 return gb_svc_version_request(op);
460 case GB_SVC_TYPE_SVC_HELLO:
461 return gb_svc_hello(op);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500462 case GB_SVC_TYPE_INTF_HOTPLUG:
463 return gb_svc_intf_hotplug_recv(op);
464 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
465 return gb_svc_intf_hot_unplug_recv(op);
466 case GB_SVC_TYPE_INTF_RESET:
467 return gb_svc_intf_reset_recv(op);
468 default:
469 dev_err(&op->connection->dev,
470 "unsupported request: %hhu\n", type);
471 return -EINVAL;
472 }
473}
474
Alex Elder30c6d9d2015-05-22 13:02:08 -0500475static int gb_svc_connection_init(struct gb_connection *connection)
476{
477 struct gb_svc *svc;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500478
479 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
480 if (!svc)
481 return -ENOMEM;
482
Perry Hung75a60ed2015-07-24 19:02:33 -0400483 connection->hd->svc = svc;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500484 svc->connection = connection;
485 connection->private = svc;
Viresh Kumard3d44842015-07-21 17:44:18 +0530486
Viresh Kumardcd05002015-07-24 15:32:20 +0530487 WARN_ON(connection->hd->initial_svc_connection);
488 connection->hd->initial_svc_connection = connection;
Viresh Kumard3d44842015-07-21 17:44:18 +0530489
490 ida_init(&greybus_svc_device_id_map);
491
Viresh Kumar18d777c2015-07-21 17:44:20 +0530492 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500493}
494
495static void gb_svc_connection_exit(struct gb_connection *connection)
496{
497 struct gb_svc *svc = connection->private;
498
Perry Hung75a60ed2015-07-24 19:02:33 -0400499 connection->hd->svc = NULL;
Viresh Kumard3d44842015-07-21 17:44:18 +0530500 connection->private = NULL;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500501 kfree(svc);
502}
503
504static struct gb_protocol svc_protocol = {
505 .name = "svc",
506 .id = GREYBUS_PROTOCOL_SVC,
Viresh Kumar06e305f2015-07-01 12:13:51 +0530507 .major = GB_SVC_VERSION_MAJOR,
508 .minor = GB_SVC_VERSION_MINOR,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500509 .connection_init = gb_svc_connection_init,
510 .connection_exit = gb_svc_connection_exit,
511 .request_recv = gb_svc_request_recv,
512};
Viresh Kumarab69c4c2015-07-03 17:00:29 +0530513gb_builtin_protocol_driver(svc_protocol);