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)
19 struct gb_svc_deferred_request {
20 struct work_struct work;
21 struct gb_operation *operation;
25 static ssize_t endo_id_show(struct device *dev,
26 struct device_attribute *attr, char *buf)
28 struct gb_svc *svc = to_gb_svc(dev);
30 return sprintf(buf, "0x%04x\n", svc->endo_id);
32 static DEVICE_ATTR_RO(endo_id);
34 static ssize_t ap_intf_id_show(struct device *dev,
35 struct device_attribute *attr, char *buf)
37 struct gb_svc *svc = to_gb_svc(dev);
39 return sprintf(buf, "%u\n", svc->ap_intf_id);
41 static DEVICE_ATTR_RO(ap_intf_id);
43 static struct attribute *svc_attrs[] = {
44 &dev_attr_endo_id.attr,
45 &dev_attr_ap_intf_id.attr,
48 ATTRIBUTE_GROUPS(svc);
50 static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
52 struct gb_svc_intf_device_id_request request;
54 request.intf_id = intf_id;
55 request.device_id = device_id;
57 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
58 &request, sizeof(request), NULL, 0);
61 int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
63 struct gb_svc_intf_reset_request request;
65 request.intf_id = intf_id;
67 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
68 &request, sizeof(request), NULL, 0);
70 EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
72 int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id)
74 struct gb_svc_intf_eject_request request;
76 request.intf_id = intf_id;
79 * The pulse width for module release in svc is long so we need to
80 * increase the timeout so the operation will not return to soon.
82 return gb_operation_sync_timeout(svc->connection,
83 GB_SVC_TYPE_INTF_EJECT, &request,
84 sizeof(request), NULL, 0,
87 EXPORT_SYMBOL_GPL(gb_svc_intf_eject);
89 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
92 struct gb_svc_dme_peer_get_request request;
93 struct gb_svc_dme_peer_get_response response;
97 request.intf_id = intf_id;
98 request.attr = cpu_to_le16(attr);
99 request.selector = cpu_to_le16(selector);
101 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
102 &request, sizeof(request),
103 &response, sizeof(response));
105 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
106 intf_id, attr, selector, ret);
110 result = le16_to_cpu(response.result_code);
112 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
113 intf_id, attr, selector, result);
118 *value = le32_to_cpu(response.attr_value);
122 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
124 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
127 struct gb_svc_dme_peer_set_request request;
128 struct gb_svc_dme_peer_set_response response;
132 request.intf_id = intf_id;
133 request.attr = cpu_to_le16(attr);
134 request.selector = cpu_to_le16(selector);
135 request.value = cpu_to_le32(value);
137 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
138 &request, sizeof(request),
139 &response, sizeof(response));
141 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
142 intf_id, attr, selector, value, ret);
146 result = le16_to_cpu(response.result_code);
148 dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
149 intf_id, attr, selector, value, result);
155 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
158 * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot
159 * status attribute ES3_INIT_STATUS. AP needs to read and clear it, after
160 * reading a non-zero value from it.
162 * FIXME: This is module-hardware dependent and needs to be extended for every
163 * type of module we want to support.
165 static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
167 struct gb_host_device *hd = intf->hd;
174 * Check if the module is ES2 or ES3, and choose attr number
176 * FIXME: Remove ES2 support from the kernel entirely.
178 if (intf->ddbl1_manufacturer_id == ES2_DDBL1_MFR_ID &&
179 intf->ddbl1_product_id == ES2_DDBL1_PROD_ID)
180 attr = DME_ATTR_T_TST_SRC_INCREMENT;
182 attr = DME_ATTR_ES3_INIT_STATUS;
184 /* Read and clear boot status in ES3_INIT_STATUS */
185 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr,
186 DME_ATTR_SELECTOR_INDEX, &value);
192 * A nonzero boot status indicates the module has finished
196 dev_err(&intf->dev, "Module not ready yet\n");
201 * Check if the module needs to boot from UniPro.
202 * For ES2: We need to check lowest 8 bits of 'value'.
203 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
204 * FIXME: Remove ES2 support from the kernel entirely.
206 if (intf->ddbl1_manufacturer_id == ES2_DDBL1_MFR_ID &&
207 intf->ddbl1_product_id == ES2_DDBL1_PROD_ID)
210 init_status = value >> 24;
212 if (init_status == DME_DIS_UNIPRO_BOOT_STARTED ||
213 init_status == DME_DIS_FALLBACK_UNIPRO_BOOT_STARTED)
214 intf->boot_over_unipro = true;
216 return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
217 DME_ATTR_SELECTOR_INDEX, 0);
220 int gb_svc_connection_create(struct gb_svc *svc,
221 u8 intf1_id, u16 cport1_id,
222 u8 intf2_id, u16 cport2_id,
223 bool boot_over_unipro)
225 struct gb_svc_conn_create_request request;
227 request.intf1_id = intf1_id;
228 request.cport1_id = cpu_to_le16(cport1_id);
229 request.intf2_id = intf2_id;
230 request.cport2_id = cpu_to_le16(cport2_id);
232 * XXX: fix connections paramaters to TC0 and all CPort flags
238 * We need to skip setting E2EFC and other flags to the connection
239 * create request, for all cports, on an interface that need to boot
240 * over unipro, i.e. interfaces required to download firmware.
242 if (boot_over_unipro)
243 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_CSD_N;
245 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
247 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
248 &request, sizeof(request), NULL, 0);
250 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
252 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
253 u8 intf2_id, u16 cport2_id)
255 struct gb_svc_conn_destroy_request request;
256 struct gb_connection *connection = svc->connection;
259 request.intf1_id = intf1_id;
260 request.cport1_id = cpu_to_le16(cport1_id);
261 request.intf2_id = intf2_id;
262 request.cport2_id = cpu_to_le16(cport2_id);
264 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
265 &request, sizeof(request), NULL, 0);
267 dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
268 intf1_id, cport1_id, intf2_id, cport2_id, ret);
271 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
273 /* Creates bi-directional routes between the devices */
274 static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
275 u8 intf2_id, u8 dev2_id)
277 struct gb_svc_route_create_request request;
279 request.intf1_id = intf1_id;
280 request.dev1_id = dev1_id;
281 request.intf2_id = intf2_id;
282 request.dev2_id = dev2_id;
284 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
285 &request, sizeof(request), NULL, 0);
288 /* Destroys bi-directional routes between the devices */
289 static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
291 struct gb_svc_route_destroy_request request;
294 request.intf1_id = intf1_id;
295 request.intf2_id = intf2_id;
297 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
298 &request, sizeof(request), NULL, 0);
300 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
301 intf1_id, intf2_id, ret);
305 int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series,
306 u8 tx_mode, u8 tx_gear, u8 tx_nlanes,
307 u8 rx_mode, u8 rx_gear, u8 rx_nlanes,
308 u8 flags, u32 quirks)
310 struct gb_svc_intf_set_pwrm_request request;
311 struct gb_svc_intf_set_pwrm_response response;
314 request.intf_id = intf_id;
315 request.hs_series = hs_series;
316 request.tx_mode = tx_mode;
317 request.tx_gear = tx_gear;
318 request.tx_nlanes = tx_nlanes;
319 request.rx_mode = rx_mode;
320 request.rx_gear = rx_gear;
321 request.rx_nlanes = rx_nlanes;
322 request.flags = flags;
323 request.quirks = cpu_to_le32(quirks);
325 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
326 &request, sizeof(request),
327 &response, sizeof(response));
331 return le16_to_cpu(response.result_code);
333 EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
335 static int gb_svc_version_request(struct gb_operation *op)
337 struct gb_connection *connection = op->connection;
338 struct gb_svc *svc = connection->private;
339 struct gb_protocol_version_request *request;
340 struct gb_protocol_version_response *response;
342 if (op->request->payload_size < sizeof(*request)) {
343 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
344 op->request->payload_size,
349 request = op->request->payload;
351 if (request->major > GB_SVC_VERSION_MAJOR) {
352 dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
353 request->major, GB_SVC_VERSION_MAJOR);
357 connection->module_major = request->major;
358 connection->module_minor = request->minor;
360 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
363 response = op->response->payload;
364 response->major = connection->module_major;
365 response->minor = connection->module_minor;
370 static int gb_svc_hello(struct gb_operation *op)
372 struct gb_connection *connection = op->connection;
373 struct gb_svc *svc = connection->private;
374 struct gb_svc_hello_request *hello_request;
377 if (op->request->payload_size < sizeof(*hello_request)) {
378 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
379 op->request->payload_size,
380 sizeof(*hello_request));
384 hello_request = op->request->payload;
385 svc->endo_id = le16_to_cpu(hello_request->endo_id);
386 svc->ap_intf_id = hello_request->interface_id;
388 ret = device_add(&svc->dev);
390 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
397 static void gb_svc_intf_remove(struct gb_svc *svc, struct gb_interface *intf)
399 u8 intf_id = intf->interface_id;
400 u8 device_id = intf->device_id;
402 intf->disconnected = true;
404 gb_interface_remove(intf);
407 * Destroy the two-way route between the AP and the interface.
409 gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
411 ida_simple_remove(&svc->device_id_map, device_id);
414 static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
416 struct gb_svc_intf_hotplug_request *request;
417 struct gb_connection *connection = operation->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;
424 /* The request message size has already been verified. */
425 request = operation->request->payload;
426 intf_id = request->intf_id;
428 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
430 intf = gb_interface_find(hd, intf_id);
433 * We have received a hotplug request for an interface that
436 * This can happen in cases like:
437 * - bootrom loading the firmware image and booting into that,
438 * which only generates a hotplug event. i.e. no hot-unplug
440 * - Or the firmware on the module crashed and sent hotplug
441 * request again to the SVC, which got propagated to AP.
443 * Remove the interface and add it again, and let user know
444 * about this with a print message.
446 dev_info(&svc->dev, "removing interface %u to add it again\n",
448 gb_svc_intf_remove(svc, intf);
451 intf = gb_interface_create(hd, intf_id);
453 dev_err(&svc->dev, "failed to create interface %u\n",
458 intf->ddbl1_manufacturer_id = le32_to_cpu(request->data.ddbl1_mfr_id);
459 intf->ddbl1_product_id = le32_to_cpu(request->data.ddbl1_prod_id);
460 intf->vendor_id = le32_to_cpu(request->data.ara_vend_id);
461 intf->product_id = le32_to_cpu(request->data.ara_prod_id);
462 intf->serial_number = le64_to_cpu(request->data.serial_number);
464 ret = gb_svc_read_and_clear_module_boot_status(intf);
466 dev_err(&svc->dev, "failed to clear boot status of interface %u: %d\n",
468 goto destroy_interface;
472 * Create a device id for the interface:
473 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
474 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
476 * XXX Do we need to allocate device ID for SVC or the AP here? And what
477 * XXX about an AP with multiple interface blocks?
479 device_id = ida_simple_get(&svc->device_id_map,
480 GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
483 dev_err(&svc->dev, "failed to allocate device id for interface %u: %d\n",
485 goto destroy_interface;
488 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
490 dev_err(&svc->dev, "failed to set device id %u for interface %u: %d\n",
491 device_id, intf_id, ret);
496 * Create a two-way route between the AP and the new interface
498 ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_DEVICE_ID_AP,
501 dev_err(&svc->dev, "failed to create route to interface %u (device id %u): %d\n",
502 intf_id, device_id, ret);
506 ret = gb_interface_init(intf, device_id);
508 dev_err(&svc->dev, "failed to initialize interface %u (device id %u): %d\n",
509 intf_id, device_id, ret);
516 gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
519 * XXX Should we tell SVC that this id doesn't belong to interface
523 ida_simple_remove(&svc->device_id_map, device_id);
525 gb_interface_remove(intf);
528 static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
530 struct gb_svc *svc = operation->connection->private;
531 struct gb_svc_intf_hot_unplug_request *request;
532 struct gb_host_device *hd = operation->connection->hd;
533 struct gb_interface *intf;
536 /* The request message size has already been verified. */
537 request = operation->request->payload;
538 intf_id = request->intf_id;
540 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
542 intf = gb_interface_find(hd, intf_id);
544 dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
549 gb_svc_intf_remove(svc, intf);
552 static void gb_svc_process_deferred_request(struct work_struct *work)
554 struct gb_svc_deferred_request *dr;
555 struct gb_operation *operation;
559 dr = container_of(work, struct gb_svc_deferred_request, work);
560 operation = dr->operation;
561 svc = operation->connection->private;
562 type = operation->request->header->type;
565 case GB_SVC_TYPE_INTF_HOTPLUG:
566 gb_svc_process_intf_hotplug(operation);
568 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
569 gb_svc_process_intf_hot_unplug(operation);
572 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
575 gb_operation_put(operation);
579 static int gb_svc_queue_deferred_request(struct gb_operation *operation)
581 struct gb_svc *svc = operation->connection->private;
582 struct gb_svc_deferred_request *dr;
584 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
588 gb_operation_get(operation);
590 dr->operation = operation;
591 INIT_WORK(&dr->work, gb_svc_process_deferred_request);
593 queue_work(svc->wq, &dr->work);
599 * Bringing up a module can be time consuming, as that may require lots of
600 * initialization on the module side. Over that, we may also need to download
601 * the firmware first and flash that on the module.
603 * In order not to make other svc events wait for all this to finish,
604 * handle most of module hotplug stuff outside of the hotplug callback, with
605 * help of a workqueue.
607 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
609 struct gb_svc *svc = op->connection->private;
610 struct gb_svc_intf_hotplug_request *request;
612 if (op->request->payload_size < sizeof(*request)) {
613 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
614 op->request->payload_size, sizeof(*request));
618 request = op->request->payload;
620 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
622 return gb_svc_queue_deferred_request(op);
625 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
627 struct gb_svc *svc = op->connection->private;
628 struct gb_svc_intf_hot_unplug_request *request;
630 if (op->request->payload_size < sizeof(*request)) {
631 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
632 op->request->payload_size, sizeof(*request));
636 request = op->request->payload;
638 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
640 return gb_svc_queue_deferred_request(op);
643 static int gb_svc_intf_reset_recv(struct gb_operation *op)
645 struct gb_svc *svc = op->connection->private;
646 struct gb_message *request = op->request;
647 struct gb_svc_intf_reset_request *reset;
650 if (request->payload_size < sizeof(*reset)) {
651 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
652 request->payload_size, sizeof(*reset));
655 reset = request->payload;
657 intf_id = reset->intf_id;
659 /* FIXME Reset the interface here */
664 static int gb_svc_request_recv(u8 type, struct gb_operation *op)
666 struct gb_connection *connection = op->connection;
667 struct gb_svc *svc = connection->private;
671 * SVC requests need to follow a specific order (at least initially) and
672 * below code takes care of enforcing that. The expected order is:
675 * - Any other request, but the earlier two.
677 * Incoming requests are guaranteed to be serialized and so we don't
678 * need to protect 'state' for any races.
681 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
682 if (svc->state != GB_SVC_STATE_RESET)
685 case GB_SVC_TYPE_SVC_HELLO:
686 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
690 if (svc->state != GB_SVC_STATE_SVC_HELLO)
696 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
702 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
703 ret = gb_svc_version_request(op);
705 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
707 case GB_SVC_TYPE_SVC_HELLO:
708 ret = gb_svc_hello(op);
710 svc->state = GB_SVC_STATE_SVC_HELLO;
712 case GB_SVC_TYPE_INTF_HOTPLUG:
713 return gb_svc_intf_hotplug_recv(op);
714 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
715 return gb_svc_intf_hot_unplug_recv(op);
716 case GB_SVC_TYPE_INTF_RESET:
717 return gb_svc_intf_reset_recv(op);
719 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
724 static void gb_svc_release(struct device *dev)
726 struct gb_svc *svc = to_gb_svc(dev);
729 gb_connection_destroy(svc->connection);
730 ida_destroy(&svc->device_id_map);
731 destroy_workqueue(svc->wq);
735 struct device_type greybus_svc_type = {
736 .name = "greybus_svc",
737 .release = gb_svc_release,
740 struct gb_svc *gb_svc_create(struct gb_host_device *hd)
744 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
748 svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
754 svc->dev.parent = &hd->dev;
755 svc->dev.bus = &greybus_bus_type;
756 svc->dev.type = &greybus_svc_type;
757 svc->dev.groups = svc_groups;
758 svc->dev.dma_mask = svc->dev.parent->dma_mask;
759 device_initialize(&svc->dev);
761 dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
763 ida_init(&svc->device_id_map);
764 svc->state = GB_SVC_STATE_RESET;
767 svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
768 GREYBUS_PROTOCOL_SVC);
769 if (!svc->connection) {
770 dev_err(&svc->dev, "failed to create connection\n");
771 put_device(&svc->dev);
775 svc->connection->private = svc;
780 int gb_svc_add(struct gb_svc *svc)
785 * The SVC protocol is currently driven by the SVC, so the SVC device
786 * is added from the connection request handler when enough
787 * information has been received.
789 ret = gb_connection_init(svc->connection);
796 void gb_svc_del(struct gb_svc *svc)
799 * The SVC device may have been registered from the request handler.
801 if (device_is_registered(&svc->dev))
802 device_del(&svc->dev);
804 gb_connection_exit(svc->connection);
806 flush_workqueue(svc->wq);
809 void gb_svc_put(struct gb_svc *svc)
811 put_device(&svc->dev);
814 static int gb_svc_connection_init(struct gb_connection *connection)
816 struct gb_svc *svc = connection->private;
818 dev_dbg(&svc->dev, "%s\n", __func__);
823 static void gb_svc_connection_exit(struct gb_connection *connection)
825 struct gb_svc *svc = connection->private;
827 dev_dbg(&svc->dev, "%s\n", __func__);
830 static struct gb_protocol svc_protocol = {
832 .id = GREYBUS_PROTOCOL_SVC,
833 .major = GB_SVC_VERSION_MAJOR,
834 .minor = GB_SVC_VERSION_MINOR,
835 .connection_init = gb_svc_connection_init,
836 .connection_exit = gb_svc_connection_exit,
837 .request_recv = gb_svc_request_recv,
838 .flags = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
839 GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
840 GB_PROTOCOL_SKIP_VERSION,
842 gb_builtin_protocol_driver(svc_protocol);