4 * Copyright 2015 Google Inc.
5 * Copyright 2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/workqueue.h>
15 #define CPORT_FLAGS_E2EFC BIT(0)
16 #define CPORT_FLAGS_CSD_N BIT(1)
17 #define CPORT_FLAGS_CSV_N BIT(2)
21 GB_SVC_STATE_PROTOCOL_VERSION,
22 GB_SVC_STATE_SVC_HELLO,
26 struct gb_connection *connection;
27 enum gb_svc_state state;
28 struct ida device_id_map;
32 struct work_struct work;
33 struct gb_connection *connection;
34 struct gb_svc_intf_hotplug_request data;
39 * AP's SVC cport is required early to get messages from the SVC. This happens
40 * even before the Endo is created and hence any modules or interfaces.
42 * This is a temporary connection, used only at initial bootup.
44 struct gb_connection *
45 gb_ap_svc_connection_create(struct gb_host_device *hd)
47 struct gb_connection *connection;
49 connection = gb_connection_create_range(hd, NULL, &hd->dev,
59 * We know endo-type and AP's interface id now, lets create a proper svc
60 * connection (and its interface/bundle) now and get rid of the initial
61 * 'partially' initialized one svc connection.
63 static struct gb_interface *
64 gb_ap_interface_create(struct gb_host_device *hd,
65 struct gb_connection *connection, u8 interface_id)
67 struct gb_interface *intf;
68 struct device *dev = &hd->endo->dev;
70 intf = gb_interface_create(hd, interface_id);
72 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
73 __func__, interface_id);
77 intf->device_id = GB_DEVICE_ID_AP;
78 svc_update_connection(intf, connection);
80 /* Its no longer a partially initialized connection */
81 hd->initial_svc_connection = NULL;
86 static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
88 struct gb_svc_intf_device_id_request request;
90 request.intf_id = intf_id;
91 request.device_id = device_id;
93 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
94 &request, sizeof(request), NULL, 0);
97 int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
99 struct gb_svc_intf_reset_request request;
101 request.intf_id = intf_id;
103 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
104 &request, sizeof(request), NULL, 0);
106 EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
108 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
111 struct gb_svc_dme_peer_get_request request;
112 struct gb_svc_dme_peer_get_response response;
116 request.intf_id = intf_id;
117 request.attr = cpu_to_le16(attr);
118 request.selector = cpu_to_le16(selector);
120 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
121 &request, sizeof(request),
122 &response, sizeof(response));
124 pr_err("failed to get DME attribute (%hhu %hx %hu) %d\n",
125 intf_id, attr, selector, ret);
129 result = le16_to_cpu(response.result_code);
131 pr_err("Unipro error %hu while getting DME attribute (%hhu %hx %hu)\n",
132 result, intf_id, attr, selector);
137 *value = le32_to_cpu(response.attr_value);
141 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
143 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
146 struct gb_svc_dme_peer_set_request request;
147 struct gb_svc_dme_peer_set_response response;
151 request.intf_id = intf_id;
152 request.attr = cpu_to_le16(attr);
153 request.selector = cpu_to_le16(selector);
154 request.value = cpu_to_le32(value);
156 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
157 &request, sizeof(request),
158 &response, sizeof(response));
160 pr_err("failed to set DME attribute (%hhu %hx %hu %u) %d\n",
161 intf_id, attr, selector, value, ret);
165 result = le16_to_cpu(response.result_code);
167 pr_err("Unipro error %hu while setting DME attribute (%hhu %hx %hu %u)\n",
168 result, intf_id, attr, selector, value);
174 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
177 * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot
178 * status attribute. AP needs to read and clear it, after reading a non-zero
181 * FIXME: This is module-hardware dependent and needs to be extended for every
182 * type of module we want to support.
184 static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
186 struct gb_host_device *hd = intf->hd;
190 /* Read and clear boot status in T_TstSrcIncrement */
191 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id,
192 DME_ATTR_T_TST_SRC_INCREMENT,
193 DME_ATTR_SELECTOR_INDEX, &value);
199 * A nonzero boot status indicates the module has finished
203 dev_err(&intf->dev, "Module not ready yet\n");
208 * Check if the module needs to boot from unipro.
209 * For ES2: We need to check lowest 8 bits of 'value'.
210 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
212 * FIXME: Add code to find if we are on ES2 or ES3 to have separate
215 if (value == DME_TSI_UNIPRO_BOOT_STARTED ||
216 value == DME_TSI_FALLBACK_UNIPRO_BOOT_STARTED)
217 intf->boot_over_unipro = true;
219 return gb_svc_dme_peer_set(hd->svc, intf->interface_id,
220 DME_ATTR_T_TST_SRC_INCREMENT,
221 DME_ATTR_SELECTOR_INDEX, 0);
224 int gb_svc_connection_create(struct gb_svc *svc,
225 u8 intf1_id, u16 cport1_id,
226 u8 intf2_id, u16 cport2_id,
227 bool boot_over_unipro)
229 struct gb_svc_conn_create_request request;
231 request.intf1_id = intf1_id;
232 request.cport1_id = cpu_to_le16(cport1_id);
233 request.intf2_id = intf2_id;
234 request.cport2_id = cpu_to_le16(cport2_id);
236 * XXX: fix connections paramaters to TC0 and all CPort flags
242 * We need to skip setting E2EFC and other flags to the connection
243 * create request, for all cports, on an interface that need to boot
244 * over unipro, i.e. interfaces required to download firmware.
246 if (boot_over_unipro)
247 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_CSD_N;
249 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
251 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
252 &request, sizeof(request), NULL, 0);
254 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
256 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
257 u8 intf2_id, u16 cport2_id)
259 struct gb_svc_conn_destroy_request request;
260 struct gb_connection *connection = svc->connection;
263 request.intf1_id = intf1_id;
264 request.cport1_id = cpu_to_le16(cport1_id);
265 request.intf2_id = intf2_id;
266 request.cport2_id = cpu_to_le16(cport2_id);
268 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
269 &request, sizeof(request), NULL, 0);
271 pr_err("failed to destroy connection (%hhu:%hu %hhu:%hu) %d\n",
272 intf1_id, cport1_id, intf2_id, cport2_id, ret);
274 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
276 /* Creates bi-directional routes between the devices */
277 static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
278 u8 intf2_id, u8 dev2_id)
280 struct gb_svc_route_create_request request;
282 request.intf1_id = intf1_id;
283 request.dev1_id = dev1_id;
284 request.intf2_id = intf2_id;
285 request.dev2_id = dev2_id;
287 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
288 &request, sizeof(request), NULL, 0);
291 /* Destroys bi-directional routes between the devices */
292 static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
294 struct gb_svc_route_destroy_request request;
297 request.intf1_id = intf1_id;
298 request.intf2_id = intf2_id;
300 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
301 &request, sizeof(request), NULL, 0);
303 pr_err("failed to destroy route (%hhu %hhu) %d\n",
304 intf1_id, intf2_id, ret);
307 static int gb_svc_version_request(struct gb_operation *op)
309 struct gb_connection *connection = op->connection;
310 struct gb_protocol_version_request *request;
311 struct gb_protocol_version_response *response;
313 if (op->request->payload_size < sizeof(*request)) {
314 pr_err("%d: short version request (%zu < %zu)\n",
315 connection->intf_cport_id,
316 op->request->payload_size,
321 request = op->request->payload;
323 if (request->major > GB_SVC_VERSION_MAJOR) {
324 pr_err("%d: unsupported major version (%hhu > %hhu)\n",
325 connection->intf_cport_id, request->major,
326 GB_SVC_VERSION_MAJOR);
330 connection->module_major = request->major;
331 connection->module_minor = request->minor;
333 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL)) {
334 pr_err("%d: error allocating response\n",
335 connection->intf_cport_id);
339 response = op->response->payload;
340 response->major = connection->module_major;
341 response->minor = connection->module_minor;
346 static int gb_svc_hello(struct gb_operation *op)
348 struct gb_connection *connection = op->connection;
349 struct gb_host_device *hd = connection->hd;
350 struct gb_svc_hello_request *hello_request;
351 struct gb_interface *intf;
357 * SVC sends information about the endo and interface-id on the hello
358 * request, use that to create an endo.
360 if (op->request->payload_size < sizeof(*hello_request)) {
361 pr_err("%d: Illegal size of hello request (%zu < %zu)\n",
362 connection->intf_cport_id, op->request->payload_size,
363 sizeof(*hello_request));
367 hello_request = op->request->payload;
368 endo_id = le16_to_cpu(hello_request->endo_id);
369 interface_id = hello_request->interface_id;
372 ret = greybus_endo_setup(hd, endo_id, interface_id);
377 * Endo and its modules are ready now, fix AP's partially initialized
378 * svc protocol and its connection.
380 intf = gb_ap_interface_create(hd, connection, interface_id);
382 gb_endo_remove(hd->endo);
389 static void svc_intf_remove(struct gb_connection *connection,
390 struct gb_interface *intf)
392 struct gb_host_device *hd = connection->hd;
393 struct gb_svc *svc = connection->private;
394 u8 intf_id = intf->interface_id;
397 device_id = intf->device_id;
398 gb_interface_remove(intf);
401 * Destroy the two-way route between the AP and the interface.
403 gb_svc_route_destroy(svc, hd->endo->ap_intf_id, intf_id);
405 ida_simple_remove(&svc->device_id_map, device_id);
409 * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it
410 * returns, irrespective of success or Failure in bringing up the module.
412 static void svc_process_hotplug(struct work_struct *work)
414 struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug,
416 struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data;
417 struct gb_connection *connection = svc_hotplug->connection;
418 struct gb_svc *svc = connection->private;
419 struct gb_host_device *hd = connection->hd;
420 struct gb_interface *intf;
421 u8 intf_id, device_id;
425 * Grab the information we need.
427 intf_id = hotplug->intf_id;
429 intf = gb_interface_find(hd, intf_id);
432 * We have received a hotplug request for an interface that
435 * This can happen in cases like:
436 * - bootrom loading the firmware image and booting into that,
437 * which only generates a hotplug event. i.e. no hot-unplug
439 * - Or the firmware on the module crashed and sent hotplug
440 * request again to the SVC, which got propagated to AP.
442 * Remove the interface and add it again, and let user know
443 * about this with a print message.
445 pr_info("%d: Removed interface (%hhu) to add it again\n",
446 connection->intf_cport_id, intf_id);
447 svc_intf_remove(connection, intf);
450 intf = gb_interface_create(hd, intf_id);
452 pr_err("%d: Failed to create interface with id %hhu\n",
453 connection->intf_cport_id, intf_id);
454 goto free_svc_hotplug;
457 ret = gb_svc_read_and_clear_module_boot_status(intf);
459 goto destroy_interface;
461 intf->unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
462 intf->unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
463 intf->vendor_id = le32_to_cpu(hotplug->data.ara_vend_id);
464 intf->product_id = le32_to_cpu(hotplug->data.ara_prod_id);
467 * Create a device id for the interface:
468 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
469 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
471 * XXX Do we need to allocate device ID for SVC or the AP here? And what
472 * XXX about an AP with multiple interface blocks?
474 device_id = ida_simple_get(&svc->device_id_map,
475 GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
478 pr_err("%d: Failed to allocate device id for interface with id %hhu (%d)\n",
479 connection->intf_cport_id, intf_id, ret);
480 goto destroy_interface;
483 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
485 pr_err("%d: Device id operation failed, interface %hhu device_id %hhu (%d)\n",
486 connection->intf_cport_id, intf_id, device_id, ret);
491 * Create a two-way route between the AP and the new interface
493 ret = gb_svc_route_create(svc, hd->endo->ap_intf_id, GB_DEVICE_ID_AP,
496 pr_err("%d: Route create operation failed, interface %hhu device_id %hhu (%d)\n",
497 connection->intf_cport_id, intf_id, device_id, ret);
501 ret = gb_interface_init(intf, device_id);
503 pr_err("%d: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n",
504 connection->intf_cport_id, intf_id, device_id, ret);
508 goto free_svc_hotplug;
511 gb_svc_route_destroy(svc, hd->endo->ap_intf_id, intf_id);
514 * XXX Should we tell SVC that this id doesn't belong to interface
518 ida_simple_remove(&svc->device_id_map, device_id);
520 gb_interface_remove(intf);
526 * Bringing up a module can be time consuming, as that may require lots of
527 * initialization on the module side. Over that, we may also need to download
528 * the firmware first and flash that on the module.
530 * In order to make other hotplug events to not wait for all this to finish,
531 * handle most of module hotplug stuff outside of the hotplug callback, with
532 * help of a workqueue.
534 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
536 struct gb_message *request = op->request;
537 struct svc_hotplug *svc_hotplug;
539 if (request->payload_size < sizeof(svc_hotplug->data)) {
540 pr_err("%d: short hotplug request received (%zu < %zu)\n",
541 op->connection->intf_cport_id, request->payload_size,
542 sizeof(svc_hotplug->data));
546 svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_KERNEL);
550 svc_hotplug->connection = op->connection;
551 memcpy(&svc_hotplug->data, op->request->payload, sizeof(svc_hotplug->data));
553 INIT_WORK(&svc_hotplug->work, svc_process_hotplug);
554 queue_work(system_unbound_wq, &svc_hotplug->work);
559 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
561 struct gb_message *request = op->request;
562 struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload;
563 struct gb_host_device *hd = op->connection->hd;
564 struct gb_interface *intf;
567 if (request->payload_size < sizeof(*hot_unplug)) {
568 pr_err("connection %d: short hot unplug request received (%zu < %zu)\n",
569 op->connection->intf_cport_id, request->payload_size,
570 sizeof(*hot_unplug));
574 intf_id = hot_unplug->intf_id;
576 intf = gb_interface_find(hd, intf_id);
578 pr_err("connection %d: Couldn't find interface for id %hhu\n",
579 op->connection->intf_cport_id, intf_id);
583 svc_intf_remove(op->connection, intf);
588 static int gb_svc_intf_reset_recv(struct gb_operation *op)
590 struct gb_message *request = op->request;
591 struct gb_svc_intf_reset_request *reset;
594 if (request->payload_size < sizeof(*reset)) {
595 pr_err("connection %d: short reset request received (%zu < %zu)\n",
596 op->connection->intf_cport_id, request->payload_size,
600 reset = request->payload;
602 intf_id = reset->intf_id;
604 /* FIXME Reset the interface here */
609 static int gb_svc_request_recv(u8 type, struct gb_operation *op)
611 struct gb_connection *connection = op->connection;
612 struct gb_svc *svc = connection->private;
616 * SVC requests need to follow a specific order (at least initially) and
617 * below code takes care of enforcing that. The expected order is:
620 * - Any other request, but the earlier two.
622 * Incoming requests are guaranteed to be serialized and so we don't
623 * need to protect 'state' for any races.
626 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
627 if (svc->state != GB_SVC_STATE_RESET)
630 case GB_SVC_TYPE_SVC_HELLO:
631 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
635 if (svc->state != GB_SVC_STATE_SVC_HELLO)
641 pr_warn("connection %d: unexpected SVC request 0x%02x received (state %u)\n",
642 connection->intf_cport_id, type, svc->state);
647 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
648 ret = gb_svc_version_request(op);
650 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
652 case GB_SVC_TYPE_SVC_HELLO:
653 ret = gb_svc_hello(op);
655 svc->state = GB_SVC_STATE_SVC_HELLO;
657 case GB_SVC_TYPE_INTF_HOTPLUG:
658 return gb_svc_intf_hotplug_recv(op);
659 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
660 return gb_svc_intf_hot_unplug_recv(op);
661 case GB_SVC_TYPE_INTF_RESET:
662 return gb_svc_intf_reset_recv(op);
664 pr_err("connection %d: unsupported request: %hhu\n",
665 connection->intf_cport_id, type);
670 static int gb_svc_connection_init(struct gb_connection *connection)
674 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
678 connection->hd->svc = svc;
679 svc->state = GB_SVC_STATE_RESET;
680 svc->connection = connection;
681 connection->private = svc;
683 WARN_ON(connection->hd->initial_svc_connection);
684 connection->hd->initial_svc_connection = connection;
686 ida_init(&svc->device_id_map);
691 static void gb_svc_connection_exit(struct gb_connection *connection)
693 struct gb_svc *svc = connection->private;
695 ida_destroy(&svc->device_id_map);
696 connection->hd->svc = NULL;
697 connection->private = NULL;
701 static struct gb_protocol svc_protocol = {
703 .id = GREYBUS_PROTOCOL_SVC,
704 .major = GB_SVC_VERSION_MAJOR,
705 .minor = GB_SVC_VERSION_MINOR,
706 .connection_init = gb_svc_connection_init,
707 .connection_exit = gb_svc_connection_exit,
708 .request_recv = gb_svc_request_recv,
709 .flags = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
710 GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
711 GB_PROTOCOL_NO_BUNDLE |
712 GB_PROTOCOL_SKIP_VERSION |
713 GB_PROTOCOL_SKIP_SVC_CONNECTION,
715 gb_builtin_protocol_driver(svc_protocol);