1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright 2015 Google Inc.
6 * Copyright 2015 Linaro Ltd.
9 #include <linux/debugfs.h>
10 #include <linux/kstrtox.h>
11 #include <linux/workqueue.h>
12 #include <linux/greybus.h>
14 #define SVC_INTF_EJECT_TIMEOUT 9000
15 #define SVC_INTF_ACTIVATE_TIMEOUT 6000
16 #define SVC_INTF_RESUME_TIMEOUT 3000
18 struct gb_svc_deferred_request {
19 struct work_struct work;
20 struct gb_operation *operation;
23 static int gb_svc_queue_deferred_request(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);
44 // This is a hack, we need to do this "right" and clean the interface up
45 // properly, not just forcibly yank the thing out of the system and hope for the
46 // best. But for now, people want their modules to come out without having to
47 // throw the thing to the ground or get out a screwdriver.
48 static ssize_t intf_eject_store(struct device *dev,
49 struct device_attribute *attr, const char *buf,
52 struct gb_svc *svc = to_gb_svc(dev);
53 unsigned short intf_id;
56 ret = kstrtou16(buf, 10, &intf_id);
60 dev_warn(dev, "Forcibly trying to eject interface %d\n", intf_id);
62 ret = gb_svc_intf_eject(svc, intf_id);
68 static DEVICE_ATTR_WO(intf_eject);
70 static ssize_t watchdog_show(struct device *dev, struct device_attribute *attr,
73 struct gb_svc *svc = to_gb_svc(dev);
75 return sprintf(buf, "%s\n",
76 gb_svc_watchdog_enabled(svc) ? "enabled" : "disabled");
79 static ssize_t watchdog_store(struct device *dev,
80 struct device_attribute *attr, const char *buf,
83 struct gb_svc *svc = to_gb_svc(dev);
87 retval = kstrtobool(buf, &user_request);
92 retval = gb_svc_watchdog_enable(svc);
94 retval = gb_svc_watchdog_disable(svc);
99 static DEVICE_ATTR_RW(watchdog);
101 static ssize_t watchdog_action_show(struct device *dev,
102 struct device_attribute *attr, char *buf)
104 struct gb_svc *svc = to_gb_svc(dev);
106 if (svc->action == GB_SVC_WATCHDOG_BITE_PANIC_KERNEL)
107 return sprintf(buf, "panic\n");
108 else if (svc->action == GB_SVC_WATCHDOG_BITE_RESET_UNIPRO)
109 return sprintf(buf, "reset\n");
114 static ssize_t watchdog_action_store(struct device *dev,
115 struct device_attribute *attr,
116 const char *buf, size_t len)
118 struct gb_svc *svc = to_gb_svc(dev);
120 if (sysfs_streq(buf, "panic"))
121 svc->action = GB_SVC_WATCHDOG_BITE_PANIC_KERNEL;
122 else if (sysfs_streq(buf, "reset"))
123 svc->action = GB_SVC_WATCHDOG_BITE_RESET_UNIPRO;
129 static DEVICE_ATTR_RW(watchdog_action);
131 static int gb_svc_pwrmon_rail_count_get(struct gb_svc *svc, u8 *value)
133 struct gb_svc_pwrmon_rail_count_get_response response;
136 ret = gb_operation_sync(svc->connection,
137 GB_SVC_TYPE_PWRMON_RAIL_COUNT_GET, NULL, 0,
138 &response, sizeof(response));
140 dev_err(&svc->dev, "failed to get rail count: %d\n", ret);
144 *value = response.rail_count;
149 static int gb_svc_pwrmon_rail_names_get(struct gb_svc *svc,
150 struct gb_svc_pwrmon_rail_names_get_response *response,
155 ret = gb_operation_sync(svc->connection,
156 GB_SVC_TYPE_PWRMON_RAIL_NAMES_GET, NULL, 0,
159 dev_err(&svc->dev, "failed to get rail names: %d\n", ret);
163 if (response->status != GB_SVC_OP_SUCCESS) {
165 "SVC error while getting rail names: %u\n",
173 static int gb_svc_pwrmon_sample_get(struct gb_svc *svc, u8 rail_id,
174 u8 measurement_type, u32 *value)
176 struct gb_svc_pwrmon_sample_get_request request;
177 struct gb_svc_pwrmon_sample_get_response response;
180 request.rail_id = rail_id;
181 request.measurement_type = measurement_type;
183 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_PWRMON_SAMPLE_GET,
184 &request, sizeof(request),
185 &response, sizeof(response));
187 dev_err(&svc->dev, "failed to get rail sample: %d\n", ret);
191 if (response.result) {
193 "UniPro error while getting rail power sample (%d %d): %d\n",
194 rail_id, measurement_type, response.result);
195 switch (response.result) {
196 case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
198 case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
205 *value = le32_to_cpu(response.measurement);
210 int gb_svc_pwrmon_intf_sample_get(struct gb_svc *svc, u8 intf_id,
211 u8 measurement_type, u32 *value)
213 struct gb_svc_pwrmon_intf_sample_get_request request;
214 struct gb_svc_pwrmon_intf_sample_get_response response;
217 request.intf_id = intf_id;
218 request.measurement_type = measurement_type;
220 ret = gb_operation_sync(svc->connection,
221 GB_SVC_TYPE_PWRMON_INTF_SAMPLE_GET,
222 &request, sizeof(request),
223 &response, sizeof(response));
225 dev_err(&svc->dev, "failed to get intf sample: %d\n", ret);
229 if (response.result) {
231 "UniPro error while getting intf power sample (%d %d): %d\n",
232 intf_id, measurement_type, response.result);
233 switch (response.result) {
234 case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
236 case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
243 *value = le32_to_cpu(response.measurement);
248 static struct attribute *svc_attrs[] = {
249 &dev_attr_endo_id.attr,
250 &dev_attr_ap_intf_id.attr,
251 &dev_attr_intf_eject.attr,
252 &dev_attr_watchdog.attr,
253 &dev_attr_watchdog_action.attr,
256 ATTRIBUTE_GROUPS(svc);
258 int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
260 struct gb_svc_intf_device_id_request request;
262 request.intf_id = intf_id;
263 request.device_id = device_id;
265 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
266 &request, sizeof(request), NULL, 0);
269 int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id)
271 struct gb_svc_intf_eject_request request;
274 request.intf_id = intf_id;
277 * The pulse width for module release in svc is long so we need to
278 * increase the timeout so the operation will not return to soon.
280 ret = gb_operation_sync_timeout(svc->connection,
281 GB_SVC_TYPE_INTF_EJECT, &request,
282 sizeof(request), NULL, 0,
283 SVC_INTF_EJECT_TIMEOUT);
285 dev_err(&svc->dev, "failed to eject interface %u\n", intf_id);
292 int gb_svc_intf_vsys_set(struct gb_svc *svc, u8 intf_id, bool enable)
294 struct gb_svc_intf_vsys_request request;
295 struct gb_svc_intf_vsys_response response;
298 request.intf_id = intf_id;
301 type = GB_SVC_TYPE_INTF_VSYS_ENABLE;
303 type = GB_SVC_TYPE_INTF_VSYS_DISABLE;
305 ret = gb_operation_sync(svc->connection, type,
306 &request, sizeof(request),
307 &response, sizeof(response));
310 if (response.result_code != GB_SVC_INTF_VSYS_OK)
315 int gb_svc_intf_refclk_set(struct gb_svc *svc, u8 intf_id, bool enable)
317 struct gb_svc_intf_refclk_request request;
318 struct gb_svc_intf_refclk_response response;
321 request.intf_id = intf_id;
324 type = GB_SVC_TYPE_INTF_REFCLK_ENABLE;
326 type = GB_SVC_TYPE_INTF_REFCLK_DISABLE;
328 ret = gb_operation_sync(svc->connection, type,
329 &request, sizeof(request),
330 &response, sizeof(response));
333 if (response.result_code != GB_SVC_INTF_REFCLK_OK)
338 int gb_svc_intf_unipro_set(struct gb_svc *svc, u8 intf_id, bool enable)
340 struct gb_svc_intf_unipro_request request;
341 struct gb_svc_intf_unipro_response response;
344 request.intf_id = intf_id;
347 type = GB_SVC_TYPE_INTF_UNIPRO_ENABLE;
349 type = GB_SVC_TYPE_INTF_UNIPRO_DISABLE;
351 ret = gb_operation_sync(svc->connection, type,
352 &request, sizeof(request),
353 &response, sizeof(response));
356 if (response.result_code != GB_SVC_INTF_UNIPRO_OK)
361 int gb_svc_intf_activate(struct gb_svc *svc, u8 intf_id, u8 *intf_type)
363 struct gb_svc_intf_activate_request request;
364 struct gb_svc_intf_activate_response response;
367 request.intf_id = intf_id;
369 ret = gb_operation_sync_timeout(svc->connection,
370 GB_SVC_TYPE_INTF_ACTIVATE,
371 &request, sizeof(request),
372 &response, sizeof(response),
373 SVC_INTF_ACTIVATE_TIMEOUT);
376 if (response.status != GB_SVC_OP_SUCCESS) {
377 dev_err(&svc->dev, "failed to activate interface %u: %u\n",
378 intf_id, response.status);
382 *intf_type = response.intf_type;
387 int gb_svc_intf_resume(struct gb_svc *svc, u8 intf_id)
389 struct gb_svc_intf_resume_request request;
390 struct gb_svc_intf_resume_response response;
393 request.intf_id = intf_id;
395 ret = gb_operation_sync_timeout(svc->connection,
396 GB_SVC_TYPE_INTF_RESUME,
397 &request, sizeof(request),
398 &response, sizeof(response),
399 SVC_INTF_RESUME_TIMEOUT);
401 dev_err(&svc->dev, "failed to send interface resume %u: %d\n",
406 if (response.status != GB_SVC_OP_SUCCESS) {
407 dev_err(&svc->dev, "failed to resume interface %u: %u\n",
408 intf_id, response.status);
415 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
418 struct gb_svc_dme_peer_get_request request;
419 struct gb_svc_dme_peer_get_response response;
423 request.intf_id = intf_id;
424 request.attr = cpu_to_le16(attr);
425 request.selector = cpu_to_le16(selector);
427 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
428 &request, sizeof(request),
429 &response, sizeof(response));
431 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
432 intf_id, attr, selector, ret);
436 result = le16_to_cpu(response.result_code);
438 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
439 intf_id, attr, selector, result);
444 *value = le32_to_cpu(response.attr_value);
449 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
452 struct gb_svc_dme_peer_set_request request;
453 struct gb_svc_dme_peer_set_response response;
457 request.intf_id = intf_id;
458 request.attr = cpu_to_le16(attr);
459 request.selector = cpu_to_le16(selector);
460 request.value = cpu_to_le32(value);
462 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
463 &request, sizeof(request),
464 &response, sizeof(response));
466 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
467 intf_id, attr, selector, value, ret);
471 result = le16_to_cpu(response.result_code);
473 dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
474 intf_id, attr, selector, value, result);
481 int gb_svc_connection_create(struct gb_svc *svc,
482 u8 intf1_id, u16 cport1_id,
483 u8 intf2_id, u16 cport2_id,
486 struct gb_svc_conn_create_request request;
488 request.intf1_id = intf1_id;
489 request.cport1_id = cpu_to_le16(cport1_id);
490 request.intf2_id = intf2_id;
491 request.cport2_id = cpu_to_le16(cport2_id);
492 request.tc = 0; /* TC0 */
493 request.flags = cport_flags;
495 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
496 &request, sizeof(request), NULL, 0);
499 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
500 u8 intf2_id, u16 cport2_id)
502 struct gb_svc_conn_destroy_request request;
503 struct gb_connection *connection = svc->connection;
506 request.intf1_id = intf1_id;
507 request.cport1_id = cpu_to_le16(cport1_id);
508 request.intf2_id = intf2_id;
509 request.cport2_id = cpu_to_le16(cport2_id);
511 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
512 &request, sizeof(request), NULL, 0);
514 dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
515 intf1_id, cport1_id, intf2_id, cport2_id, ret);
519 /* Creates bi-directional routes between the devices */
520 int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
521 u8 intf2_id, u8 dev2_id)
523 struct gb_svc_route_create_request request;
525 request.intf1_id = intf1_id;
526 request.dev1_id = dev1_id;
527 request.intf2_id = intf2_id;
528 request.dev2_id = dev2_id;
530 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
531 &request, sizeof(request), NULL, 0);
534 /* Destroys bi-directional routes between the devices */
535 void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
537 struct gb_svc_route_destroy_request request;
540 request.intf1_id = intf1_id;
541 request.intf2_id = intf2_id;
543 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
544 &request, sizeof(request), NULL, 0);
546 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
547 intf1_id, intf2_id, ret);
551 int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series,
552 u8 tx_mode, u8 tx_gear, u8 tx_nlanes,
553 u8 tx_amplitude, u8 tx_hs_equalizer,
554 u8 rx_mode, u8 rx_gear, u8 rx_nlanes,
555 u8 flags, u32 quirks,
556 struct gb_svc_l2_timer_cfg *local,
557 struct gb_svc_l2_timer_cfg *remote)
559 struct gb_svc_intf_set_pwrm_request request;
560 struct gb_svc_intf_set_pwrm_response response;
564 memset(&request, 0, sizeof(request));
566 request.intf_id = intf_id;
567 request.hs_series = hs_series;
568 request.tx_mode = tx_mode;
569 request.tx_gear = tx_gear;
570 request.tx_nlanes = tx_nlanes;
571 request.tx_amplitude = tx_amplitude;
572 request.tx_hs_equalizer = tx_hs_equalizer;
573 request.rx_mode = rx_mode;
574 request.rx_gear = rx_gear;
575 request.rx_nlanes = rx_nlanes;
576 request.flags = flags;
577 request.quirks = cpu_to_le32(quirks);
579 request.local_l2timerdata = *local;
581 request.remote_l2timerdata = *remote;
583 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
584 &request, sizeof(request),
585 &response, sizeof(response));
589 result_code = response.result_code;
590 if (result_code != GB_SVC_SETPWRM_PWR_LOCAL) {
591 dev_err(&svc->dev, "set power mode = %d\n", result_code);
597 EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
599 int gb_svc_intf_set_power_mode_hibernate(struct gb_svc *svc, u8 intf_id)
601 struct gb_svc_intf_set_pwrm_request request;
602 struct gb_svc_intf_set_pwrm_response response;
606 memset(&request, 0, sizeof(request));
608 request.intf_id = intf_id;
609 request.hs_series = GB_SVC_UNIPRO_HS_SERIES_A;
610 request.tx_mode = GB_SVC_UNIPRO_HIBERNATE_MODE;
611 request.rx_mode = GB_SVC_UNIPRO_HIBERNATE_MODE;
613 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
614 &request, sizeof(request),
615 &response, sizeof(response));
618 "failed to send set power mode operation to interface %u: %d\n",
623 result_code = response.result_code;
624 if (result_code != GB_SVC_SETPWRM_PWR_OK) {
626 "failed to hibernate the link for interface %u: %u\n",
627 intf_id, result_code);
634 int gb_svc_ping(struct gb_svc *svc)
636 return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
638 GB_OPERATION_TIMEOUT_DEFAULT * 2);
641 static int gb_svc_version_request(struct gb_operation *op)
643 struct gb_connection *connection = op->connection;
644 struct gb_svc *svc = gb_connection_get_data(connection);
645 struct gb_svc_version_request *request;
646 struct gb_svc_version_response *response;
648 if (op->request->payload_size < sizeof(*request)) {
649 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
650 op->request->payload_size,
655 request = op->request->payload;
657 if (request->major > GB_SVC_VERSION_MAJOR) {
658 dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
659 request->major, GB_SVC_VERSION_MAJOR);
663 svc->protocol_major = request->major;
664 svc->protocol_minor = request->minor;
666 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
669 response = op->response->payload;
670 response->major = svc->protocol_major;
671 response->minor = svc->protocol_minor;
676 static ssize_t pwr_debugfs_voltage_read(struct file *file, char __user *buf,
677 size_t len, loff_t *offset)
679 struct svc_debugfs_pwrmon_rail *pwrmon_rails =
680 file_inode(file)->i_private;
681 struct gb_svc *svc = pwrmon_rails->svc;
686 ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
687 GB_SVC_PWRMON_TYPE_VOL, &value);
690 "failed to get voltage sample %u: %d\n",
691 pwrmon_rails->id, ret);
695 desc = scnprintf(buff, sizeof(buff), "%u\n", value);
697 return simple_read_from_buffer(buf, len, offset, buff, desc);
700 static ssize_t pwr_debugfs_current_read(struct file *file, char __user *buf,
701 size_t len, loff_t *offset)
703 struct svc_debugfs_pwrmon_rail *pwrmon_rails =
704 file_inode(file)->i_private;
705 struct gb_svc *svc = pwrmon_rails->svc;
710 ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
711 GB_SVC_PWRMON_TYPE_CURR, &value);
714 "failed to get current sample %u: %d\n",
715 pwrmon_rails->id, ret);
719 desc = scnprintf(buff, sizeof(buff), "%u\n", value);
721 return simple_read_from_buffer(buf, len, offset, buff, desc);
724 static ssize_t pwr_debugfs_power_read(struct file *file, char __user *buf,
725 size_t len, loff_t *offset)
727 struct svc_debugfs_pwrmon_rail *pwrmon_rails =
728 file_inode(file)->i_private;
729 struct gb_svc *svc = pwrmon_rails->svc;
734 ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
735 GB_SVC_PWRMON_TYPE_PWR, &value);
737 dev_err(&svc->dev, "failed to get power sample %u: %d\n",
738 pwrmon_rails->id, ret);
742 desc = scnprintf(buff, sizeof(buff), "%u\n", value);
744 return simple_read_from_buffer(buf, len, offset, buff, desc);
747 static const struct file_operations pwrmon_debugfs_voltage_fops = {
748 .read = pwr_debugfs_voltage_read,
751 static const struct file_operations pwrmon_debugfs_current_fops = {
752 .read = pwr_debugfs_current_read,
755 static const struct file_operations pwrmon_debugfs_power_fops = {
756 .read = pwr_debugfs_power_read,
759 static void gb_svc_pwrmon_debugfs_init(struct gb_svc *svc)
764 struct gb_svc_pwrmon_rail_names_get_response *rail_names;
767 dent = debugfs_create_dir("pwrmon", svc->debugfs_dentry);
768 if (IS_ERR_OR_NULL(dent))
771 if (gb_svc_pwrmon_rail_count_get(svc, &rail_count))
772 goto err_pwrmon_debugfs;
774 if (!rail_count || rail_count > GB_SVC_PWRMON_MAX_RAIL_COUNT)
775 goto err_pwrmon_debugfs;
777 bufsize = sizeof(*rail_names) +
778 GB_SVC_PWRMON_RAIL_NAME_BUFSIZE * rail_count;
780 rail_names = kzalloc(bufsize, GFP_KERNEL);
782 goto err_pwrmon_debugfs;
784 svc->pwrmon_rails = kcalloc(rail_count, sizeof(*svc->pwrmon_rails),
786 if (!svc->pwrmon_rails)
787 goto err_pwrmon_debugfs_free;
789 if (gb_svc_pwrmon_rail_names_get(svc, rail_names, bufsize))
790 goto err_pwrmon_debugfs_free;
792 for (i = 0; i < rail_count; i++) {
794 struct svc_debugfs_pwrmon_rail *rail = &svc->pwrmon_rails[i];
795 char fname[GB_SVC_PWRMON_RAIL_NAME_BUFSIZE];
797 snprintf(fname, sizeof(fname), "%s",
798 (char *)&rail_names->name[i]);
803 dir = debugfs_create_dir(fname, dent);
804 debugfs_create_file("voltage_now", 0444, dir, rail,
805 &pwrmon_debugfs_voltage_fops);
806 debugfs_create_file("current_now", 0444, dir, rail,
807 &pwrmon_debugfs_current_fops);
808 debugfs_create_file("power_now", 0444, dir, rail,
809 &pwrmon_debugfs_power_fops);
815 err_pwrmon_debugfs_free:
817 kfree(svc->pwrmon_rails);
818 svc->pwrmon_rails = NULL;
821 debugfs_remove(dent);
824 static void gb_svc_debugfs_init(struct gb_svc *svc)
826 svc->debugfs_dentry = debugfs_create_dir(dev_name(&svc->dev),
828 gb_svc_pwrmon_debugfs_init(svc);
831 static void gb_svc_debugfs_exit(struct gb_svc *svc)
833 debugfs_remove_recursive(svc->debugfs_dentry);
834 kfree(svc->pwrmon_rails);
835 svc->pwrmon_rails = NULL;
838 static int gb_svc_hello(struct gb_operation *op)
840 struct gb_connection *connection = op->connection;
841 struct gb_svc *svc = gb_connection_get_data(connection);
842 struct gb_svc_hello_request *hello_request;
845 if (op->request->payload_size < sizeof(*hello_request)) {
846 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
847 op->request->payload_size,
848 sizeof(*hello_request));
852 hello_request = op->request->payload;
853 svc->endo_id = le16_to_cpu(hello_request->endo_id);
854 svc->ap_intf_id = hello_request->interface_id;
856 ret = device_add(&svc->dev);
858 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
862 ret = gb_svc_watchdog_create(svc);
864 dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
865 goto err_deregister_svc;
869 * FIXME: This is a temporary hack to reconfigure the link at HELLO
870 * (which abuses the deferred request processing mechanism).
872 ret = gb_svc_queue_deferred_request(op);
874 goto err_destroy_watchdog;
876 gb_svc_debugfs_init(svc);
880 err_destroy_watchdog:
881 gb_svc_watchdog_destroy(svc);
883 device_del(&svc->dev);
888 static struct gb_interface *gb_svc_interface_lookup(struct gb_svc *svc,
891 struct gb_host_device *hd = svc->hd;
892 struct gb_module *module;
893 size_t num_interfaces;
896 list_for_each_entry(module, &hd->modules, hd_node) {
897 module_id = module->module_id;
898 num_interfaces = module->num_interfaces;
900 if (intf_id >= module_id &&
901 intf_id < module_id + num_interfaces) {
902 return module->interfaces[intf_id - module_id];
909 static struct gb_module *gb_svc_module_lookup(struct gb_svc *svc, u8 module_id)
911 struct gb_host_device *hd = svc->hd;
912 struct gb_module *module;
914 list_for_each_entry(module, &hd->modules, hd_node) {
915 if (module->module_id == module_id)
922 static void gb_svc_process_hello_deferred(struct gb_operation *operation)
924 struct gb_connection *connection = operation->connection;
925 struct gb_svc *svc = gb_connection_get_data(connection);
929 * XXX This is a hack/work-around to reconfigure the APBridgeA-Switch
930 * link to PWM G2, 1 Lane, Slow Auto, so that it has sufficient
931 * bandwidth for 3 audio streams plus boot-over-UniPro of a hot-plugged
934 * The code should be removed once SW-2217, Heuristic for UniPro
935 * Power Mode Changes is resolved.
937 ret = gb_svc_intf_set_power_mode(svc, svc->ap_intf_id,
938 GB_SVC_UNIPRO_HS_SERIES_A,
939 GB_SVC_UNIPRO_SLOW_AUTO_MODE,
941 GB_SVC_SMALL_AMPLITUDE,
942 GB_SVC_NO_DE_EMPHASIS,
943 GB_SVC_UNIPRO_SLOW_AUTO_MODE,
950 "power mode change failed on AP to switch link: %d\n",
954 static void gb_svc_process_module_inserted(struct gb_operation *operation)
956 struct gb_svc_module_inserted_request *request;
957 struct gb_connection *connection = operation->connection;
958 struct gb_svc *svc = gb_connection_get_data(connection);
959 struct gb_host_device *hd = svc->hd;
960 struct gb_module *module;
961 size_t num_interfaces;
966 /* The request message size has already been verified. */
967 request = operation->request->payload;
968 module_id = request->primary_intf_id;
969 num_interfaces = request->intf_count;
970 flags = le16_to_cpu(request->flags);
972 dev_dbg(&svc->dev, "%s - id = %u, num_interfaces = %zu, flags = 0x%04x\n",
973 __func__, module_id, num_interfaces, flags);
975 if (flags & GB_SVC_MODULE_INSERTED_FLAG_NO_PRIMARY) {
976 dev_warn(&svc->dev, "no primary interface detected on module %u\n",
980 module = gb_svc_module_lookup(svc, module_id);
982 dev_warn(&svc->dev, "unexpected module-inserted event %u\n",
987 module = gb_module_create(hd, module_id, num_interfaces);
989 dev_err(&svc->dev, "failed to create module\n");
993 ret = gb_module_add(module);
995 gb_module_put(module);
999 list_add(&module->hd_node, &hd->modules);
1002 static void gb_svc_process_module_removed(struct gb_operation *operation)
1004 struct gb_svc_module_removed_request *request;
1005 struct gb_connection *connection = operation->connection;
1006 struct gb_svc *svc = gb_connection_get_data(connection);
1007 struct gb_module *module;
1010 /* The request message size has already been verified. */
1011 request = operation->request->payload;
1012 module_id = request->primary_intf_id;
1014 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, module_id);
1016 module = gb_svc_module_lookup(svc, module_id);
1018 dev_warn(&svc->dev, "unexpected module-removed event %u\n",
1023 module->disconnected = true;
1025 gb_module_del(module);
1026 list_del(&module->hd_node);
1027 gb_module_put(module);
1030 static void gb_svc_process_intf_oops(struct gb_operation *operation)
1032 struct gb_svc_intf_oops_request *request;
1033 struct gb_connection *connection = operation->connection;
1034 struct gb_svc *svc = gb_connection_get_data(connection);
1035 struct gb_interface *intf;
1039 /* The request message size has already been verified. */
1040 request = operation->request->payload;
1041 intf_id = request->intf_id;
1042 reason = request->reason;
1044 intf = gb_svc_interface_lookup(svc, intf_id);
1046 dev_warn(&svc->dev, "unexpected interface-oops event %u\n",
1051 dev_info(&svc->dev, "Deactivating interface %u, interface oops reason = %u\n",
1054 mutex_lock(&intf->mutex);
1055 intf->disconnected = true;
1056 gb_interface_disable(intf);
1057 gb_interface_deactivate(intf);
1058 mutex_unlock(&intf->mutex);
1061 static void gb_svc_process_intf_mailbox_event(struct gb_operation *operation)
1063 struct gb_svc_intf_mailbox_event_request *request;
1064 struct gb_connection *connection = operation->connection;
1065 struct gb_svc *svc = gb_connection_get_data(connection);
1066 struct gb_interface *intf;
1071 /* The request message size has already been verified. */
1072 request = operation->request->payload;
1073 intf_id = request->intf_id;
1074 result_code = le16_to_cpu(request->result_code);
1075 mailbox = le32_to_cpu(request->mailbox);
1077 dev_dbg(&svc->dev, "%s - id = %u, result = 0x%04x, mailbox = 0x%08x\n",
1078 __func__, intf_id, result_code, mailbox);
1080 intf = gb_svc_interface_lookup(svc, intf_id);
1082 dev_warn(&svc->dev, "unexpected mailbox event %u\n", intf_id);
1086 gb_interface_mailbox_event(intf, result_code, mailbox);
1089 static void gb_svc_process_deferred_request(struct work_struct *work)
1091 struct gb_svc_deferred_request *dr;
1092 struct gb_operation *operation;
1096 dr = container_of(work, struct gb_svc_deferred_request, work);
1097 operation = dr->operation;
1098 svc = gb_connection_get_data(operation->connection);
1099 type = operation->request->header->type;
1102 case GB_SVC_TYPE_SVC_HELLO:
1103 gb_svc_process_hello_deferred(operation);
1105 case GB_SVC_TYPE_MODULE_INSERTED:
1106 gb_svc_process_module_inserted(operation);
1108 case GB_SVC_TYPE_MODULE_REMOVED:
1109 gb_svc_process_module_removed(operation);
1111 case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
1112 gb_svc_process_intf_mailbox_event(operation);
1114 case GB_SVC_TYPE_INTF_OOPS:
1115 gb_svc_process_intf_oops(operation);
1118 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
1121 gb_operation_put(operation);
1125 static int gb_svc_queue_deferred_request(struct gb_operation *operation)
1127 struct gb_svc *svc = gb_connection_get_data(operation->connection);
1128 struct gb_svc_deferred_request *dr;
1130 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1134 gb_operation_get(operation);
1136 dr->operation = operation;
1137 INIT_WORK(&dr->work, gb_svc_process_deferred_request);
1139 queue_work(svc->wq, &dr->work);
1144 static int gb_svc_intf_reset_recv(struct gb_operation *op)
1146 struct gb_svc *svc = gb_connection_get_data(op->connection);
1147 struct gb_message *request = op->request;
1148 struct gb_svc_intf_reset_request *reset;
1150 if (request->payload_size < sizeof(*reset)) {
1151 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
1152 request->payload_size, sizeof(*reset));
1155 reset = request->payload;
1157 /* FIXME Reset the interface here */
1162 static int gb_svc_module_inserted_recv(struct gb_operation *op)
1164 struct gb_svc *svc = gb_connection_get_data(op->connection);
1165 struct gb_svc_module_inserted_request *request;
1167 if (op->request->payload_size < sizeof(*request)) {
1168 dev_warn(&svc->dev, "short module-inserted request received (%zu < %zu)\n",
1169 op->request->payload_size, sizeof(*request));
1173 request = op->request->payload;
1175 dev_dbg(&svc->dev, "%s - id = %u\n", __func__,
1176 request->primary_intf_id);
1178 return gb_svc_queue_deferred_request(op);
1181 static int gb_svc_module_removed_recv(struct gb_operation *op)
1183 struct gb_svc *svc = gb_connection_get_data(op->connection);
1184 struct gb_svc_module_removed_request *request;
1186 if (op->request->payload_size < sizeof(*request)) {
1187 dev_warn(&svc->dev, "short module-removed request received (%zu < %zu)\n",
1188 op->request->payload_size, sizeof(*request));
1192 request = op->request->payload;
1194 dev_dbg(&svc->dev, "%s - id = %u\n", __func__,
1195 request->primary_intf_id);
1197 return gb_svc_queue_deferred_request(op);
1200 static int gb_svc_intf_oops_recv(struct gb_operation *op)
1202 struct gb_svc *svc = gb_connection_get_data(op->connection);
1203 struct gb_svc_intf_oops_request *request;
1205 if (op->request->payload_size < sizeof(*request)) {
1206 dev_warn(&svc->dev, "short intf-oops request received (%zu < %zu)\n",
1207 op->request->payload_size, sizeof(*request));
1211 return gb_svc_queue_deferred_request(op);
1214 static int gb_svc_intf_mailbox_event_recv(struct gb_operation *op)
1216 struct gb_svc *svc = gb_connection_get_data(op->connection);
1217 struct gb_svc_intf_mailbox_event_request *request;
1219 if (op->request->payload_size < sizeof(*request)) {
1220 dev_warn(&svc->dev, "short mailbox request received (%zu < %zu)\n",
1221 op->request->payload_size, sizeof(*request));
1225 request = op->request->payload;
1227 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
1229 return gb_svc_queue_deferred_request(op);
1232 static int gb_svc_request_handler(struct gb_operation *op)
1234 struct gb_connection *connection = op->connection;
1235 struct gb_svc *svc = gb_connection_get_data(connection);
1240 * SVC requests need to follow a specific order (at least initially) and
1241 * below code takes care of enforcing that. The expected order is:
1242 * - PROTOCOL_VERSION
1244 * - Any other request, but the earlier two.
1246 * Incoming requests are guaranteed to be serialized and so we don't
1247 * need to protect 'state' for any races.
1250 case GB_SVC_TYPE_PROTOCOL_VERSION:
1251 if (svc->state != GB_SVC_STATE_RESET)
1254 case GB_SVC_TYPE_SVC_HELLO:
1255 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
1259 if (svc->state != GB_SVC_STATE_SVC_HELLO)
1265 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
1271 case GB_SVC_TYPE_PROTOCOL_VERSION:
1272 ret = gb_svc_version_request(op);
1274 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
1276 case GB_SVC_TYPE_SVC_HELLO:
1277 ret = gb_svc_hello(op);
1279 svc->state = GB_SVC_STATE_SVC_HELLO;
1281 case GB_SVC_TYPE_INTF_RESET:
1282 return gb_svc_intf_reset_recv(op);
1283 case GB_SVC_TYPE_MODULE_INSERTED:
1284 return gb_svc_module_inserted_recv(op);
1285 case GB_SVC_TYPE_MODULE_REMOVED:
1286 return gb_svc_module_removed_recv(op);
1287 case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
1288 return gb_svc_intf_mailbox_event_recv(op);
1289 case GB_SVC_TYPE_INTF_OOPS:
1290 return gb_svc_intf_oops_recv(op);
1292 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
1297 static void gb_svc_release(struct device *dev)
1299 struct gb_svc *svc = to_gb_svc(dev);
1301 if (svc->connection)
1302 gb_connection_destroy(svc->connection);
1303 ida_destroy(&svc->device_id_map);
1304 destroy_workqueue(svc->wq);
1308 struct device_type greybus_svc_type = {
1309 .name = "greybus_svc",
1310 .release = gb_svc_release,
1313 struct gb_svc *gb_svc_create(struct gb_host_device *hd)
1317 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
1321 svc->wq = alloc_ordered_workqueue("%s:svc", 0, dev_name(&hd->dev));
1327 svc->dev.parent = &hd->dev;
1328 svc->dev.bus = &greybus_bus_type;
1329 svc->dev.type = &greybus_svc_type;
1330 svc->dev.groups = svc_groups;
1331 svc->dev.dma_mask = svc->dev.parent->dma_mask;
1332 device_initialize(&svc->dev);
1334 dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
1336 ida_init(&svc->device_id_map);
1337 svc->state = GB_SVC_STATE_RESET;
1340 svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
1341 gb_svc_request_handler);
1342 if (IS_ERR(svc->connection)) {
1343 dev_err(&svc->dev, "failed to create connection: %ld\n",
1344 PTR_ERR(svc->connection));
1345 goto err_put_device;
1348 gb_connection_set_data(svc->connection, svc);
1353 put_device(&svc->dev);
1357 int gb_svc_add(struct gb_svc *svc)
1362 * The SVC protocol is currently driven by the SVC, so the SVC device
1363 * is added from the connection request handler when enough
1364 * information has been received.
1366 ret = gb_connection_enable(svc->connection);
1373 static void gb_svc_remove_modules(struct gb_svc *svc)
1375 struct gb_host_device *hd = svc->hd;
1376 struct gb_module *module, *tmp;
1378 list_for_each_entry_safe(module, tmp, &hd->modules, hd_node) {
1379 gb_module_del(module);
1380 list_del(&module->hd_node);
1381 gb_module_put(module);
1385 void gb_svc_del(struct gb_svc *svc)
1387 gb_connection_disable_rx(svc->connection);
1390 * The SVC device may have been registered from the request handler.
1392 if (device_is_registered(&svc->dev)) {
1393 gb_svc_debugfs_exit(svc);
1394 gb_svc_watchdog_destroy(svc);
1395 device_del(&svc->dev);
1398 flush_workqueue(svc->wq);
1400 gb_svc_remove_modules(svc);
1402 gb_connection_disable(svc->connection);
1405 void gb_svc_put(struct gb_svc *svc)
1407 put_device(&svc->dev);