4 * Copyright 2015 Google Inc.
5 * Copyright 2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/workqueue.h>
14 #define CPORT_FLAGS_E2EFC BIT(0)
15 #define CPORT_FLAGS_CSD_N BIT(1)
16 #define CPORT_FLAGS_CSV_N BIT(2)
20 struct work_struct work;
21 struct gb_connection *connection;
22 struct gb_svc_intf_hotplug_request data;
26 static ssize_t endo_id_show(struct device *dev,
27 struct device_attribute *attr, char *buf)
29 struct gb_svc *svc = to_gb_svc(dev);
31 return sprintf(buf, "0x%04x\n", svc->endo_id);
33 static DEVICE_ATTR_RO(endo_id);
35 static ssize_t ap_intf_id_show(struct device *dev,
36 struct device_attribute *attr, char *buf)
38 struct gb_svc *svc = to_gb_svc(dev);
40 return sprintf(buf, "%u\n", svc->ap_intf_id);
42 static DEVICE_ATTR_RO(ap_intf_id);
44 static struct attribute *svc_attrs[] = {
45 &dev_attr_endo_id.attr,
46 &dev_attr_ap_intf_id.attr,
49 ATTRIBUTE_GROUPS(svc);
51 static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
53 struct gb_svc_intf_device_id_request request;
55 request.intf_id = intf_id;
56 request.device_id = device_id;
58 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
59 &request, sizeof(request), NULL, 0);
62 int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
64 struct gb_svc_intf_reset_request request;
66 request.intf_id = intf_id;
68 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
69 &request, sizeof(request), NULL, 0);
71 EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
73 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
76 struct gb_svc_dme_peer_get_request request;
77 struct gb_svc_dme_peer_get_response response;
81 request.intf_id = intf_id;
82 request.attr = cpu_to_le16(attr);
83 request.selector = cpu_to_le16(selector);
85 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
86 &request, sizeof(request),
87 &response, sizeof(response));
89 dev_err(&svc->dev, "failed to get DME attribute (%hhu %hx %hu): %d\n",
90 intf_id, attr, selector, ret);
94 result = le16_to_cpu(response.result_code);
96 dev_err(&svc->dev, "UniPro error while getting DME attribute (%hhu %hx %hu): %hu\n",
97 intf_id, attr, selector, result);
102 *value = le32_to_cpu(response.attr_value);
106 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
108 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
111 struct gb_svc_dme_peer_set_request request;
112 struct gb_svc_dme_peer_set_response response;
116 request.intf_id = intf_id;
117 request.attr = cpu_to_le16(attr);
118 request.selector = cpu_to_le16(selector);
119 request.value = cpu_to_le32(value);
121 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
122 &request, sizeof(request),
123 &response, sizeof(response));
125 dev_err(&svc->dev, "failed to set DME attribute (%hhu %hx %hu %u): %d\n",
126 intf_id, attr, selector, value, ret);
130 result = le16_to_cpu(response.result_code);
132 dev_err(&svc->dev, "UniPro error while setting DME attribute (%hhu %hx %hu %u): %hu\n",
133 intf_id, attr, selector, value, result);
139 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
142 * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot
143 * status attribute. AP needs to read and clear it, after reading a non-zero
146 * FIXME: This is module-hardware dependent and needs to be extended for every
147 * type of module we want to support.
149 static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
151 struct gb_host_device *hd = intf->hd;
155 /* Read and clear boot status in T_TstSrcIncrement */
156 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id,
157 DME_ATTR_T_TST_SRC_INCREMENT,
158 DME_ATTR_SELECTOR_INDEX, &value);
164 * A nonzero boot status indicates the module has finished
168 dev_err(&intf->dev, "Module not ready yet\n");
173 * Check if the module needs to boot from unipro.
174 * For ES2: We need to check lowest 8 bits of 'value'.
175 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
177 * FIXME: Add code to find if we are on ES2 or ES3 to have separate
180 if (value == DME_TSI_UNIPRO_BOOT_STARTED ||
181 value == DME_TSI_FALLBACK_UNIPRO_BOOT_STARTED)
182 intf->boot_over_unipro = true;
184 return gb_svc_dme_peer_set(hd->svc, intf->interface_id,
185 DME_ATTR_T_TST_SRC_INCREMENT,
186 DME_ATTR_SELECTOR_INDEX, 0);
189 int gb_svc_connection_create(struct gb_svc *svc,
190 u8 intf1_id, u16 cport1_id,
191 u8 intf2_id, u16 cport2_id,
192 bool boot_over_unipro)
194 struct gb_svc_conn_create_request request;
196 request.intf1_id = intf1_id;
197 request.cport1_id = cpu_to_le16(cport1_id);
198 request.intf2_id = intf2_id;
199 request.cport2_id = cpu_to_le16(cport2_id);
201 * XXX: fix connections paramaters to TC0 and all CPort flags
207 * We need to skip setting E2EFC and other flags to the connection
208 * create request, for all cports, on an interface that need to boot
209 * over unipro, i.e. interfaces required to download firmware.
211 if (boot_over_unipro)
212 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_CSD_N;
214 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
216 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
217 &request, sizeof(request), NULL, 0);
219 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
221 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
222 u8 intf2_id, u16 cport2_id)
224 struct gb_svc_conn_destroy_request request;
225 struct gb_connection *connection = svc->connection;
228 request.intf1_id = intf1_id;
229 request.cport1_id = cpu_to_le16(cport1_id);
230 request.intf2_id = intf2_id;
231 request.cport2_id = cpu_to_le16(cport2_id);
233 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
234 &request, sizeof(request), NULL, 0);
236 dev_err(&svc->dev, "failed to destroy connection (%hhu:%hu %hhu:%hu): %d\n",
237 intf1_id, cport1_id, intf2_id, cport2_id, ret);
240 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
242 /* Creates bi-directional routes between the devices */
243 static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
244 u8 intf2_id, u8 dev2_id)
246 struct gb_svc_route_create_request request;
248 request.intf1_id = intf1_id;
249 request.dev1_id = dev1_id;
250 request.intf2_id = intf2_id;
251 request.dev2_id = dev2_id;
253 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
254 &request, sizeof(request), NULL, 0);
257 /* Destroys bi-directional routes between the devices */
258 static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
260 struct gb_svc_route_destroy_request request;
263 request.intf1_id = intf1_id;
264 request.intf2_id = intf2_id;
266 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
267 &request, sizeof(request), NULL, 0);
269 dev_err(&svc->dev, "failed to destroy route (%hhu %hhu): %d\n",
270 intf1_id, intf2_id, ret);
274 static int gb_svc_version_request(struct gb_operation *op)
276 struct gb_connection *connection = op->connection;
277 struct gb_svc *svc = connection->private;
278 struct gb_protocol_version_request *request;
279 struct gb_protocol_version_response *response;
281 if (op->request->payload_size < sizeof(*request)) {
282 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
283 op->request->payload_size,
288 request = op->request->payload;
290 if (request->major > GB_SVC_VERSION_MAJOR) {
291 dev_warn(&svc->dev, "unsupported major version (%hhu > %hhu)\n",
292 request->major, GB_SVC_VERSION_MAJOR);
296 connection->module_major = request->major;
297 connection->module_minor = request->minor;
299 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
302 response = op->response->payload;
303 response->major = connection->module_major;
304 response->minor = connection->module_minor;
309 static int gb_svc_hello(struct gb_operation *op)
311 struct gb_connection *connection = op->connection;
312 struct gb_svc *svc = connection->private;
313 struct gb_svc_hello_request *hello_request;
316 if (op->request->payload_size < sizeof(*hello_request)) {
317 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
318 op->request->payload_size,
319 sizeof(*hello_request));
323 hello_request = op->request->payload;
324 svc->endo_id = le16_to_cpu(hello_request->endo_id);
325 svc->ap_intf_id = hello_request->interface_id;
327 ret = device_add(&svc->dev);
329 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
336 static void svc_intf_remove(struct gb_connection *connection,
337 struct gb_interface *intf)
339 struct gb_svc *svc = connection->private;
340 u8 intf_id = intf->interface_id;
343 device_id = intf->device_id;
344 gb_interface_remove(intf);
347 * Destroy the two-way route between the AP and the interface.
349 gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
351 ida_simple_remove(&svc->device_id_map, device_id);
355 * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it
356 * returns, irrespective of success or Failure in bringing up the module.
358 static void svc_process_hotplug(struct work_struct *work)
360 struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug,
362 struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data;
363 struct gb_connection *connection = svc_hotplug->connection;
364 struct gb_svc *svc = connection->private;
365 struct gb_host_device *hd = connection->hd;
366 struct gb_interface *intf;
367 u8 intf_id, device_id;
371 * Grab the information we need.
373 intf_id = hotplug->intf_id;
375 intf = gb_interface_find(hd, intf_id);
378 * We have received a hotplug request for an interface that
381 * This can happen in cases like:
382 * - bootrom loading the firmware image and booting into that,
383 * which only generates a hotplug event. i.e. no hot-unplug
385 * - Or the firmware on the module crashed and sent hotplug
386 * request again to the SVC, which got propagated to AP.
388 * Remove the interface and add it again, and let user know
389 * about this with a print message.
391 dev_info(&svc->dev, "removing interface %hhu to add it again\n",
393 svc_intf_remove(connection, intf);
396 intf = gb_interface_create(hd, intf_id);
398 dev_err(&svc->dev, "failed to create interface %hhu\n",
400 goto free_svc_hotplug;
403 ret = gb_svc_read_and_clear_module_boot_status(intf);
405 goto destroy_interface;
407 intf->unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
408 intf->unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
409 intf->vendor_id = le32_to_cpu(hotplug->data.ara_vend_id);
410 intf->product_id = le32_to_cpu(hotplug->data.ara_prod_id);
413 * Create a device id for the interface:
414 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
415 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
417 * XXX Do we need to allocate device ID for SVC or the AP here? And what
418 * XXX about an AP with multiple interface blocks?
420 device_id = ida_simple_get(&svc->device_id_map,
421 GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
424 dev_err(&svc->dev, "failed to allocate device id for interface %hhu: %d\n",
426 goto destroy_interface;
429 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
431 dev_err(&svc->dev, "failed to set device id %hhu for interface %hhu: %d\n",
432 device_id, intf_id, ret);
437 * Create a two-way route between the AP and the new interface
439 ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_DEVICE_ID_AP,
442 dev_err(&svc->dev, "failed to create route to interface %hhu (device id %hhu): %d\n",
443 intf_id, device_id, ret);
447 ret = gb_interface_init(intf, device_id);
449 dev_err(&svc->dev, "failed to initialize interface %hhu (device id %hhu): %d\n",
450 intf_id, device_id, ret);
454 goto free_svc_hotplug;
457 gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
460 * XXX Should we tell SVC that this id doesn't belong to interface
464 ida_simple_remove(&svc->device_id_map, device_id);
466 gb_interface_remove(intf);
472 * Bringing up a module can be time consuming, as that may require lots of
473 * initialization on the module side. Over that, we may also need to download
474 * the firmware first and flash that on the module.
476 * In order to make other hotplug events to not wait for all this to finish,
477 * handle most of module hotplug stuff outside of the hotplug callback, with
478 * help of a workqueue.
480 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
482 struct gb_svc *svc = op->connection->private;
483 struct gb_message *request = op->request;
484 struct svc_hotplug *svc_hotplug;
486 if (request->payload_size < sizeof(svc_hotplug->data)) {
487 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
488 request->payload_size,
489 sizeof(svc_hotplug->data));
493 svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_KERNEL);
497 svc_hotplug->connection = op->connection;
498 memcpy(&svc_hotplug->data, op->request->payload, sizeof(svc_hotplug->data));
500 INIT_WORK(&svc_hotplug->work, svc_process_hotplug);
501 queue_work(system_unbound_wq, &svc_hotplug->work);
506 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
508 struct gb_svc *svc = op->connection->private;
509 struct gb_message *request = op->request;
510 struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload;
511 struct gb_host_device *hd = op->connection->hd;
512 struct gb_interface *intf;
515 if (request->payload_size < sizeof(*hot_unplug)) {
516 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
517 request->payload_size,
518 sizeof(*hot_unplug));
522 intf_id = hot_unplug->intf_id;
524 intf = gb_interface_find(hd, intf_id);
526 dev_warn(&svc->dev, "could not find hot-unplug interface %hhu\n",
531 svc_intf_remove(op->connection, intf);
536 static int gb_svc_intf_reset_recv(struct gb_operation *op)
538 struct gb_svc *svc = op->connection->private;
539 struct gb_message *request = op->request;
540 struct gb_svc_intf_reset_request *reset;
543 if (request->payload_size < sizeof(*reset)) {
544 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
545 request->payload_size, sizeof(*reset));
548 reset = request->payload;
550 intf_id = reset->intf_id;
552 /* FIXME Reset the interface here */
557 static int gb_svc_request_recv(u8 type, struct gb_operation *op)
559 struct gb_connection *connection = op->connection;
560 struct gb_svc *svc = connection->private;
564 * SVC requests need to follow a specific order (at least initially) and
565 * below code takes care of enforcing that. The expected order is:
568 * - Any other request, but the earlier two.
570 * Incoming requests are guaranteed to be serialized and so we don't
571 * need to protect 'state' for any races.
574 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
575 if (svc->state != GB_SVC_STATE_RESET)
578 case GB_SVC_TYPE_SVC_HELLO:
579 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
583 if (svc->state != GB_SVC_STATE_SVC_HELLO)
589 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
595 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
596 ret = gb_svc_version_request(op);
598 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
600 case GB_SVC_TYPE_SVC_HELLO:
601 ret = gb_svc_hello(op);
603 svc->state = GB_SVC_STATE_SVC_HELLO;
605 case GB_SVC_TYPE_INTF_HOTPLUG:
606 return gb_svc_intf_hotplug_recv(op);
607 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
608 return gb_svc_intf_hot_unplug_recv(op);
609 case GB_SVC_TYPE_INTF_RESET:
610 return gb_svc_intf_reset_recv(op);
612 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
617 static void gb_svc_release(struct device *dev)
619 struct gb_svc *svc = to_gb_svc(dev);
621 ida_destroy(&svc->device_id_map);
625 struct device_type greybus_svc_type = {
626 .name = "greybus_svc",
627 .release = gb_svc_release,
630 static int gb_svc_connection_init(struct gb_connection *connection)
632 struct gb_host_device *hd = connection->hd;
635 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
639 svc->dev.parent = &hd->dev;
640 svc->dev.bus = &greybus_bus_type;
641 svc->dev.type = &greybus_svc_type;
642 svc->dev.groups = svc_groups;
643 svc->dev.dma_mask = svc->dev.parent->dma_mask;
644 device_initialize(&svc->dev);
646 dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
648 ida_init(&svc->device_id_map);
649 svc->state = GB_SVC_STATE_RESET;
650 svc->connection = connection;
651 connection->private = svc;
658 static void gb_svc_connection_exit(struct gb_connection *connection)
660 struct gb_svc *svc = connection->private;
662 if (device_is_registered(&svc->dev))
663 device_del(&svc->dev);
665 connection->hd->svc = NULL;
666 connection->private = NULL;
668 put_device(&svc->dev);
671 static struct gb_protocol svc_protocol = {
673 .id = GREYBUS_PROTOCOL_SVC,
674 .major = GB_SVC_VERSION_MAJOR,
675 .minor = GB_SVC_VERSION_MINOR,
676 .connection_init = gb_svc_connection_init,
677 .connection_exit = gb_svc_connection_exit,
678 .request_recv = gb_svc_request_recv,
679 .flags = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
680 GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
681 GB_PROTOCOL_NO_BUNDLE |
682 GB_PROTOCOL_SKIP_VERSION,
684 gb_builtin_protocol_driver(svc_protocol);