xref: /linux/drivers/greybus/interface.c (revision 306ec721d043bbe5e818d59fbb37c28d999b5d8b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Greybus interface code
4  *
5  * Copyright 2014 Google Inc.
6  * Copyright 2014 Linaro Ltd.
7  */
8 
9 #include <linux/delay.h>
10 #include <linux/greybus.h>
11 
12 #include "greybus_trace.h"
13 
14 #define GB_INTERFACE_MODE_SWITCH_TIMEOUT	2000
15 
16 #define GB_INTERFACE_DEVICE_ID_BAD	0xff
17 
18 #define GB_INTERFACE_AUTOSUSPEND_MS			3000
19 
20 /* Time required for interface to enter standby before disabling REFCLK */
21 #define GB_INTERFACE_SUSPEND_HIBERNATE_DELAY_MS			20
22 
23 /* Don't-care selector index */
24 #define DME_SELECTOR_INDEX_NULL		0
25 
26 /* DME attributes */
27 /* FIXME: remove ES2 support and DME_T_TST_SRC_INCREMENT */
28 #define DME_T_TST_SRC_INCREMENT		0x4083
29 
30 #define DME_DDBL1_MANUFACTURERID	0x5003
31 #define DME_DDBL1_PRODUCTID		0x5004
32 
33 #define DME_TOSHIBA_GMP_VID		0x6000
34 #define DME_TOSHIBA_GMP_PID		0x6001
35 #define DME_TOSHIBA_GMP_SN0		0x6002
36 #define DME_TOSHIBA_GMP_SN1		0x6003
37 #define DME_TOSHIBA_GMP_INIT_STATUS	0x6101
38 
39 /* DDBL1 Manufacturer and Product ids */
40 #define TOSHIBA_DMID			0x0126
41 #define TOSHIBA_ES2_BRIDGE_DPID		0x1000
42 #define TOSHIBA_ES3_APBRIDGE_DPID	0x1001
43 #define TOSHIBA_ES3_GBPHY_DPID	0x1002
44 
45 static int gb_interface_hibernate_link(struct gb_interface *intf);
46 static int gb_interface_refclk_set(struct gb_interface *intf, bool enable);
47 
48 static int gb_interface_dme_attr_get(struct gb_interface *intf,
49 				     u16 attr, u32 *val)
50 {
51 	return gb_svc_dme_peer_get(intf->hd->svc, intf->interface_id,
52 					attr, DME_SELECTOR_INDEX_NULL, val);
53 }
54 
55 static int gb_interface_read_ara_dme(struct gb_interface *intf)
56 {
57 	u32 sn0, sn1;
58 	int ret;
59 
60 	/*
61 	 * Unless this is a Toshiba bridge, bail out until we have defined
62 	 * standard GMP attributes.
63 	 */
64 	if (intf->ddbl1_manufacturer_id != TOSHIBA_DMID) {
65 		dev_err(&intf->dev, "unknown manufacturer %08x\n",
66 			intf->ddbl1_manufacturer_id);
67 		return -ENODEV;
68 	}
69 
70 	ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_VID,
71 					&intf->vendor_id);
72 	if (ret)
73 		return ret;
74 
75 	ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_PID,
76 					&intf->product_id);
77 	if (ret)
78 		return ret;
79 
80 	ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_SN0, &sn0);
81 	if (ret)
82 		return ret;
83 
84 	ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_SN1, &sn1);
85 	if (ret)
86 		return ret;
87 
88 	intf->serial_number = (u64)sn1 << 32 | sn0;
89 
90 	return 0;
91 }
92 
93 static int gb_interface_read_dme(struct gb_interface *intf)
94 {
95 	int ret;
96 
97 	/* DME attributes have already been read */
98 	if (intf->dme_read)
99 		return 0;
100 
101 	ret = gb_interface_dme_attr_get(intf, DME_DDBL1_MANUFACTURERID,
102 					&intf->ddbl1_manufacturer_id);
103 	if (ret)
104 		return ret;
105 
106 	ret = gb_interface_dme_attr_get(intf, DME_DDBL1_PRODUCTID,
107 					&intf->ddbl1_product_id);
108 	if (ret)
109 		return ret;
110 
111 	if (intf->ddbl1_manufacturer_id == TOSHIBA_DMID &&
112 	    intf->ddbl1_product_id == TOSHIBA_ES2_BRIDGE_DPID) {
113 		intf->quirks |= GB_INTERFACE_QUIRK_NO_GMP_IDS;
114 		intf->quirks |= GB_INTERFACE_QUIRK_NO_INIT_STATUS;
115 	}
116 
117 	ret = gb_interface_read_ara_dme(intf);
118 	if (ret)
119 		return ret;
120 
121 	intf->dme_read = true;
122 
123 	return 0;
124 }
125 
126 static int gb_interface_route_create(struct gb_interface *intf)
127 {
128 	struct gb_svc *svc = intf->hd->svc;
129 	u8 intf_id = intf->interface_id;
130 	u8 device_id;
131 	int ret;
132 
133 	/* Allocate an interface device id. */
134 	ret = ida_alloc_range(&svc->device_id_map, GB_SVC_DEVICE_ID_MIN,
135 			      GB_SVC_DEVICE_ID_MAX, GFP_KERNEL);
136 	if (ret < 0) {
137 		dev_err(&intf->dev, "failed to allocate device id: %d\n", ret);
138 		return ret;
139 	}
140 	device_id = ret;
141 
142 	ret = gb_svc_intf_device_id(svc, intf_id, device_id);
143 	if (ret) {
144 		dev_err(&intf->dev, "failed to set device id %u: %d\n",
145 			device_id, ret);
146 		goto err_ida_remove;
147 	}
148 
149 	/* FIXME: Hard-coded AP device id. */
150 	ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_SVC_DEVICE_ID_AP,
151 				  intf_id, device_id);
152 	if (ret) {
153 		dev_err(&intf->dev, "failed to create route: %d\n", ret);
154 		goto err_svc_id_free;
155 	}
156 
157 	intf->device_id = device_id;
158 
159 	return 0;
160 
161 err_svc_id_free:
162 	/*
163 	 * XXX Should we tell SVC that this id doesn't belong to interface
164 	 * XXX anymore.
165 	 */
166 err_ida_remove:
167 	ida_free(&svc->device_id_map, device_id);
168 
169 	return ret;
170 }
171 
172 static void gb_interface_route_destroy(struct gb_interface *intf)
173 {
174 	struct gb_svc *svc = intf->hd->svc;
175 
176 	if (intf->device_id == GB_INTERFACE_DEVICE_ID_BAD)
177 		return;
178 
179 	gb_svc_route_destroy(svc, svc->ap_intf_id, intf->interface_id);
180 	ida_free(&svc->device_id_map, intf->device_id);
181 	intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
182 }
183 
184 /* Locking: Caller holds the interface mutex. */
185 static int gb_interface_legacy_mode_switch(struct gb_interface *intf)
186 {
187 	int ret;
188 
189 	dev_info(&intf->dev, "legacy mode switch detected\n");
190 
191 	/* Mark as disconnected to prevent I/O during disable. */
192 	intf->disconnected = true;
193 	gb_interface_disable(intf);
194 	intf->disconnected = false;
195 
196 	ret = gb_interface_enable(intf);
197 	if (ret) {
198 		dev_err(&intf->dev, "failed to re-enable interface: %d\n", ret);
199 		gb_interface_deactivate(intf);
200 	}
201 
202 	return ret;
203 }
204 
205 void gb_interface_mailbox_event(struct gb_interface *intf, u16 result,
206 				u32 mailbox)
207 {
208 	mutex_lock(&intf->mutex);
209 
210 	if (result) {
211 		dev_warn(&intf->dev,
212 			 "mailbox event with UniPro error: 0x%04x\n",
213 			 result);
214 		goto err_disable;
215 	}
216 
217 	if (mailbox != GB_SVC_INTF_MAILBOX_GREYBUS) {
218 		dev_warn(&intf->dev,
219 			 "mailbox event with unexpected value: 0x%08x\n",
220 			 mailbox);
221 		goto err_disable;
222 	}
223 
224 	if (intf->quirks & GB_INTERFACE_QUIRK_LEGACY_MODE_SWITCH) {
225 		gb_interface_legacy_mode_switch(intf);
226 		goto out_unlock;
227 	}
228 
229 	if (!intf->mode_switch) {
230 		dev_warn(&intf->dev, "unexpected mailbox event: 0x%08x\n",
231 			 mailbox);
232 		goto err_disable;
233 	}
234 
235 	dev_info(&intf->dev, "mode switch detected\n");
236 
237 	complete(&intf->mode_switch_completion);
238 
239 out_unlock:
240 	mutex_unlock(&intf->mutex);
241 
242 	return;
243 
244 err_disable:
245 	gb_interface_disable(intf);
246 	gb_interface_deactivate(intf);
247 	mutex_unlock(&intf->mutex);
248 }
249 
250 static void gb_interface_mode_switch_work(struct work_struct *work)
251 {
252 	struct gb_interface *intf;
253 	struct gb_control *control;
254 	unsigned long timeout;
255 	int ret;
256 
257 	intf = container_of(work, struct gb_interface, mode_switch_work);
258 
259 	mutex_lock(&intf->mutex);
260 	/* Make sure interface is still enabled. */
261 	if (!intf->enabled) {
262 		dev_dbg(&intf->dev, "mode switch aborted\n");
263 		intf->mode_switch = false;
264 		mutex_unlock(&intf->mutex);
265 		goto out_interface_put;
266 	}
267 
268 	/*
269 	 * Prepare the control device for mode switch and make sure to get an
270 	 * extra reference before it goes away during interface disable.
271 	 */
272 	control = gb_control_get(intf->control);
273 	gb_control_mode_switch_prepare(control);
274 	gb_interface_disable(intf);
275 	mutex_unlock(&intf->mutex);
276 
277 	timeout = msecs_to_jiffies(GB_INTERFACE_MODE_SWITCH_TIMEOUT);
278 	ret = wait_for_completion_interruptible_timeout(
279 			&intf->mode_switch_completion, timeout);
280 
281 	/* Finalise control-connection mode switch. */
282 	gb_control_mode_switch_complete(control);
283 	gb_control_put(control);
284 
285 	if (ret < 0) {
286 		dev_err(&intf->dev, "mode switch interrupted\n");
287 		goto err_deactivate;
288 	} else if (ret == 0) {
289 		dev_err(&intf->dev, "mode switch timed out\n");
290 		goto err_deactivate;
291 	}
292 
293 	/* Re-enable (re-enumerate) interface if still active. */
294 	mutex_lock(&intf->mutex);
295 	intf->mode_switch = false;
296 	if (intf->active) {
297 		ret = gb_interface_enable(intf);
298 		if (ret) {
299 			dev_err(&intf->dev, "failed to re-enable interface: %d\n",
300 				ret);
301 			gb_interface_deactivate(intf);
302 		}
303 	}
304 	mutex_unlock(&intf->mutex);
305 
306 out_interface_put:
307 	gb_interface_put(intf);
308 
309 	return;
310 
311 err_deactivate:
312 	mutex_lock(&intf->mutex);
313 	intf->mode_switch = false;
314 	gb_interface_deactivate(intf);
315 	mutex_unlock(&intf->mutex);
316 
317 	gb_interface_put(intf);
318 }
319 
320 int gb_interface_request_mode_switch(struct gb_interface *intf)
321 {
322 	int ret = 0;
323 
324 	mutex_lock(&intf->mutex);
325 	if (intf->mode_switch) {
326 		ret = -EBUSY;
327 		goto out_unlock;
328 	}
329 
330 	intf->mode_switch = true;
331 	reinit_completion(&intf->mode_switch_completion);
332 
333 	/*
334 	 * Get a reference to the interface device, which will be put once the
335 	 * mode switch is complete.
336 	 */
337 	get_device(&intf->dev);
338 
339 	if (!queue_work(system_long_wq, &intf->mode_switch_work)) {
340 		put_device(&intf->dev);
341 		ret = -EBUSY;
342 		goto out_unlock;
343 	}
344 
345 out_unlock:
346 	mutex_unlock(&intf->mutex);
347 
348 	return ret;
349 }
350 EXPORT_SYMBOL_GPL(gb_interface_request_mode_switch);
351 
352 /*
353  * T_TstSrcIncrement is written by the module on ES2 as a stand-in for the
354  * init-status attribute DME_TOSHIBA_INIT_STATUS. The AP needs to read and
355  * clear it after reading a non-zero value from it.
356  *
357  * FIXME: This is module-hardware dependent and needs to be extended for every
358  * type of module we want to support.
359  */
360 static int gb_interface_read_and_clear_init_status(struct gb_interface *intf)
361 {
362 	struct gb_host_device *hd = intf->hd;
363 	unsigned long bootrom_quirks;
364 	unsigned long s2l_quirks;
365 	int ret;
366 	u32 value;
367 	u16 attr;
368 	u8 init_status;
369 
370 	/*
371 	 * ES2 bridges use T_TstSrcIncrement for the init status.
372 	 *
373 	 * FIXME: Remove ES2 support
374 	 */
375 	if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
376 		attr = DME_T_TST_SRC_INCREMENT;
377 	else
378 		attr = DME_TOSHIBA_GMP_INIT_STATUS;
379 
380 	ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr,
381 				  DME_SELECTOR_INDEX_NULL, &value);
382 	if (ret)
383 		return ret;
384 
385 	/*
386 	 * A nonzero init status indicates the module has finished
387 	 * initializing.
388 	 */
389 	if (!value) {
390 		dev_err(&intf->dev, "invalid init status\n");
391 		return -ENODEV;
392 	}
393 
394 	/*
395 	 * Extract the init status.
396 	 *
397 	 * For ES2: We need to check lowest 8 bits of 'value'.
398 	 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
399 	 *
400 	 * FIXME: Remove ES2 support
401 	 */
402 	if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
403 		init_status = value & 0xff;
404 	else
405 		init_status = value >> 24;
406 
407 	/*
408 	 * Check if the interface is executing the quirky ES3 bootrom that,
409 	 * for example, requires E2EFC, CSD and CSV to be disabled.
410 	 */
411 	bootrom_quirks = GB_INTERFACE_QUIRK_NO_CPORT_FEATURES |
412 				GB_INTERFACE_QUIRK_FORCED_DISABLE |
413 				GB_INTERFACE_QUIRK_LEGACY_MODE_SWITCH |
414 				GB_INTERFACE_QUIRK_NO_BUNDLE_ACTIVATE;
415 
416 	s2l_quirks = GB_INTERFACE_QUIRK_NO_PM;
417 
418 	switch (init_status) {
419 	case GB_INIT_BOOTROM_UNIPRO_BOOT_STARTED:
420 	case GB_INIT_BOOTROM_FALLBACK_UNIPRO_BOOT_STARTED:
421 		intf->quirks |= bootrom_quirks;
422 		break;
423 	case GB_INIT_S2_LOADER_BOOT_STARTED:
424 		/* S2 Loader doesn't support runtime PM */
425 		intf->quirks &= ~bootrom_quirks;
426 		intf->quirks |= s2l_quirks;
427 		break;
428 	default:
429 		intf->quirks &= ~bootrom_quirks;
430 		intf->quirks &= ~s2l_quirks;
431 	}
432 
433 	/* Clear the init status. */
434 	return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
435 				   DME_SELECTOR_INDEX_NULL, 0);
436 }
437 
438 /* interface sysfs attributes */
439 #define gb_interface_attr(field, type)					\
440 static ssize_t field##_show(struct device *dev,				\
441 			    struct device_attribute *attr,		\
442 			    char *buf)					\
443 {									\
444 	struct gb_interface *intf = to_gb_interface(dev);		\
445 	return scnprintf(buf, PAGE_SIZE, type"\n", intf->field);	\
446 }									\
447 static DEVICE_ATTR_RO(field)
448 
449 gb_interface_attr(ddbl1_manufacturer_id, "0x%08x");
450 gb_interface_attr(ddbl1_product_id, "0x%08x");
451 gb_interface_attr(interface_id, "%u");
452 gb_interface_attr(vendor_id, "0x%08x");
453 gb_interface_attr(product_id, "0x%08x");
454 gb_interface_attr(serial_number, "0x%016llx");
455 
456 static ssize_t voltage_now_show(struct device *dev,
457 				struct device_attribute *attr, char *buf)
458 {
459 	struct gb_interface *intf = to_gb_interface(dev);
460 	int ret;
461 	u32 measurement;
462 
463 	ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
464 					    GB_SVC_PWRMON_TYPE_VOL,
465 					    &measurement);
466 	if (ret) {
467 		dev_err(&intf->dev, "failed to get voltage sample (%d)\n", ret);
468 		return ret;
469 	}
470 
471 	return sprintf(buf, "%u\n", measurement);
472 }
473 static DEVICE_ATTR_RO(voltage_now);
474 
475 static ssize_t current_now_show(struct device *dev,
476 				struct device_attribute *attr, char *buf)
477 {
478 	struct gb_interface *intf = to_gb_interface(dev);
479 	int ret;
480 	u32 measurement;
481 
482 	ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
483 					    GB_SVC_PWRMON_TYPE_CURR,
484 					    &measurement);
485 	if (ret) {
486 		dev_err(&intf->dev, "failed to get current sample (%d)\n", ret);
487 		return ret;
488 	}
489 
490 	return sprintf(buf, "%u\n", measurement);
491 }
492 static DEVICE_ATTR_RO(current_now);
493 
494 static ssize_t power_now_show(struct device *dev,
495 			      struct device_attribute *attr, char *buf)
496 {
497 	struct gb_interface *intf = to_gb_interface(dev);
498 	int ret;
499 	u32 measurement;
500 
501 	ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
502 					    GB_SVC_PWRMON_TYPE_PWR,
503 					    &measurement);
504 	if (ret) {
505 		dev_err(&intf->dev, "failed to get power sample (%d)\n", ret);
506 		return ret;
507 	}
508 
509 	return sprintf(buf, "%u\n", measurement);
510 }
511 static DEVICE_ATTR_RO(power_now);
512 
513 static ssize_t power_state_show(struct device *dev,
514 				struct device_attribute *attr, char *buf)
515 {
516 	struct gb_interface *intf = to_gb_interface(dev);
517 
518 	if (intf->active)
519 		return scnprintf(buf, PAGE_SIZE, "on\n");
520 	else
521 		return scnprintf(buf, PAGE_SIZE, "off\n");
522 }
523 
524 static ssize_t power_state_store(struct device *dev,
525 				 struct device_attribute *attr, const char *buf,
526 				 size_t len)
527 {
528 	struct gb_interface *intf = to_gb_interface(dev);
529 	bool activate;
530 	int ret = 0;
531 
532 	if (kstrtobool(buf, &activate))
533 		return -EINVAL;
534 
535 	mutex_lock(&intf->mutex);
536 
537 	if (activate == intf->active)
538 		goto unlock;
539 
540 	if (activate) {
541 		ret = gb_interface_activate(intf);
542 		if (ret) {
543 			dev_err(&intf->dev,
544 				"failed to activate interface: %d\n", ret);
545 			goto unlock;
546 		}
547 
548 		ret = gb_interface_enable(intf);
549 		if (ret) {
550 			dev_err(&intf->dev,
551 				"failed to enable interface: %d\n", ret);
552 			gb_interface_deactivate(intf);
553 			goto unlock;
554 		}
555 	} else {
556 		gb_interface_disable(intf);
557 		gb_interface_deactivate(intf);
558 	}
559 
560 unlock:
561 	mutex_unlock(&intf->mutex);
562 
563 	if (ret)
564 		return ret;
565 
566 	return len;
567 }
568 static DEVICE_ATTR_RW(power_state);
569 
570 static const char *gb_interface_type_string(struct gb_interface *intf)
571 {
572 	static const char * const types[] = {
573 		[GB_INTERFACE_TYPE_INVALID] = "invalid",
574 		[GB_INTERFACE_TYPE_UNKNOWN] = "unknown",
575 		[GB_INTERFACE_TYPE_DUMMY] = "dummy",
576 		[GB_INTERFACE_TYPE_UNIPRO] = "unipro",
577 		[GB_INTERFACE_TYPE_GREYBUS] = "greybus",
578 	};
579 
580 	return types[intf->type];
581 }
582 
583 static ssize_t interface_type_show(struct device *dev,
584 				   struct device_attribute *attr, char *buf)
585 {
586 	struct gb_interface *intf = to_gb_interface(dev);
587 
588 	return sprintf(buf, "%s\n", gb_interface_type_string(intf));
589 }
590 static DEVICE_ATTR_RO(interface_type);
591 
592 static struct attribute *interface_unipro_attrs[] = {
593 	&dev_attr_ddbl1_manufacturer_id.attr,
594 	&dev_attr_ddbl1_product_id.attr,
595 	NULL
596 };
597 
598 static struct attribute *interface_greybus_attrs[] = {
599 	&dev_attr_vendor_id.attr,
600 	&dev_attr_product_id.attr,
601 	&dev_attr_serial_number.attr,
602 	NULL
603 };
604 
605 static struct attribute *interface_power_attrs[] = {
606 	&dev_attr_voltage_now.attr,
607 	&dev_attr_current_now.attr,
608 	&dev_attr_power_now.attr,
609 	&dev_attr_power_state.attr,
610 	NULL
611 };
612 
613 static struct attribute *interface_common_attrs[] = {
614 	&dev_attr_interface_id.attr,
615 	&dev_attr_interface_type.attr,
616 	NULL
617 };
618 
619 static umode_t interface_unipro_is_visible(struct kobject *kobj,
620 					   struct attribute *attr, int n)
621 {
622 	struct device *dev = kobj_to_dev(kobj);
623 	struct gb_interface *intf = to_gb_interface(dev);
624 
625 	switch (intf->type) {
626 	case GB_INTERFACE_TYPE_UNIPRO:
627 	case GB_INTERFACE_TYPE_GREYBUS:
628 		return attr->mode;
629 	default:
630 		return 0;
631 	}
632 }
633 
634 static umode_t interface_greybus_is_visible(struct kobject *kobj,
635 					    struct attribute *attr, int n)
636 {
637 	struct device *dev = kobj_to_dev(kobj);
638 	struct gb_interface *intf = to_gb_interface(dev);
639 
640 	switch (intf->type) {
641 	case GB_INTERFACE_TYPE_GREYBUS:
642 		return attr->mode;
643 	default:
644 		return 0;
645 	}
646 }
647 
648 static umode_t interface_power_is_visible(struct kobject *kobj,
649 					  struct attribute *attr, int n)
650 {
651 	struct device *dev = kobj_to_dev(kobj);
652 	struct gb_interface *intf = to_gb_interface(dev);
653 
654 	switch (intf->type) {
655 	case GB_INTERFACE_TYPE_UNIPRO:
656 	case GB_INTERFACE_TYPE_GREYBUS:
657 		return attr->mode;
658 	default:
659 		return 0;
660 	}
661 }
662 
663 static const struct attribute_group interface_unipro_group = {
664 	.is_visible	= interface_unipro_is_visible,
665 	.attrs		= interface_unipro_attrs,
666 };
667 
668 static const struct attribute_group interface_greybus_group = {
669 	.is_visible	= interface_greybus_is_visible,
670 	.attrs		= interface_greybus_attrs,
671 };
672 
673 static const struct attribute_group interface_power_group = {
674 	.is_visible	= interface_power_is_visible,
675 	.attrs		= interface_power_attrs,
676 };
677 
678 static const struct attribute_group interface_common_group = {
679 	.attrs		= interface_common_attrs,
680 };
681 
682 static const struct attribute_group *interface_groups[] = {
683 	&interface_unipro_group,
684 	&interface_greybus_group,
685 	&interface_power_group,
686 	&interface_common_group,
687 	NULL
688 };
689 
690 static void gb_interface_release(struct device *dev)
691 {
692 	struct gb_interface *intf = to_gb_interface(dev);
693 
694 	trace_gb_interface_release(intf);
695 
696 	kfree(intf);
697 }
698 
699 #ifdef CONFIG_PM
700 static int gb_interface_suspend(struct device *dev)
701 {
702 	struct gb_interface *intf = to_gb_interface(dev);
703 	int ret;
704 
705 	ret = gb_control_interface_suspend_prepare(intf->control);
706 	if (ret)
707 		return ret;
708 
709 	ret = gb_control_suspend(intf->control);
710 	if (ret)
711 		goto err_hibernate_abort;
712 
713 	ret = gb_interface_hibernate_link(intf);
714 	if (ret)
715 		return ret;
716 
717 	/* Delay to allow interface to enter standby before disabling refclk */
718 	msleep(GB_INTERFACE_SUSPEND_HIBERNATE_DELAY_MS);
719 
720 	ret = gb_interface_refclk_set(intf, false);
721 	if (ret)
722 		return ret;
723 
724 	return 0;
725 
726 err_hibernate_abort:
727 	gb_control_interface_hibernate_abort(intf->control);
728 
729 	return ret;
730 }
731 
732 static int gb_interface_resume(struct device *dev)
733 {
734 	struct gb_interface *intf = to_gb_interface(dev);
735 	struct gb_svc *svc = intf->hd->svc;
736 	int ret;
737 
738 	ret = gb_interface_refclk_set(intf, true);
739 	if (ret)
740 		return ret;
741 
742 	ret = gb_svc_intf_resume(svc, intf->interface_id);
743 	if (ret)
744 		return ret;
745 
746 	ret = gb_control_resume(intf->control);
747 	if (ret)
748 		return ret;
749 
750 	return 0;
751 }
752 
753 static int gb_interface_runtime_idle(struct device *dev)
754 {
755 	pm_runtime_mark_last_busy(dev);
756 	pm_request_autosuspend(dev);
757 
758 	return 0;
759 }
760 #endif
761 
762 static const struct dev_pm_ops gb_interface_pm_ops = {
763 	SET_RUNTIME_PM_OPS(gb_interface_suspend, gb_interface_resume,
764 			   gb_interface_runtime_idle)
765 };
766 
767 const struct device_type greybus_interface_type = {
768 	.name =		"greybus_interface",
769 	.release =	gb_interface_release,
770 	.pm =		&gb_interface_pm_ops,
771 };
772 
773 /*
774  * A Greybus module represents a user-replaceable component on a GMP
775  * phone.  An interface is the physical connection on that module.  A
776  * module may have more than one interface.
777  *
778  * Create a gb_interface structure to represent a discovered interface.
779  * The position of interface within the Endo is encoded in "interface_id"
780  * argument.
781  *
782  * Returns a pointer to the new interfce or a null pointer if a
783  * failure occurs due to memory exhaustion.
784  */
785 struct gb_interface *gb_interface_create(struct gb_module *module,
786 					 u8 interface_id)
787 {
788 	struct gb_host_device *hd = module->hd;
789 	struct gb_interface *intf;
790 
791 	intf = kzalloc(sizeof(*intf), GFP_KERNEL);
792 	if (!intf)
793 		return NULL;
794 
795 	intf->hd = hd;		/* XXX refcount? */
796 	intf->module = module;
797 	intf->interface_id = interface_id;
798 	INIT_LIST_HEAD(&intf->bundles);
799 	INIT_LIST_HEAD(&intf->manifest_descs);
800 	mutex_init(&intf->mutex);
801 	INIT_WORK(&intf->mode_switch_work, gb_interface_mode_switch_work);
802 	init_completion(&intf->mode_switch_completion);
803 
804 	/* Invalid device id to start with */
805 	intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
806 
807 	intf->dev.parent = &module->dev;
808 	intf->dev.bus = &greybus_bus_type;
809 	intf->dev.type = &greybus_interface_type;
810 	intf->dev.groups = interface_groups;
811 	intf->dev.dma_mask = module->dev.dma_mask;
812 	device_initialize(&intf->dev);
813 	dev_set_name(&intf->dev, "%s.%u", dev_name(&module->dev),
814 		     interface_id);
815 
816 	pm_runtime_set_autosuspend_delay(&intf->dev,
817 					 GB_INTERFACE_AUTOSUSPEND_MS);
818 
819 	trace_gb_interface_create(intf);
820 
821 	return intf;
822 }
823 
824 static int gb_interface_vsys_set(struct gb_interface *intf, bool enable)
825 {
826 	struct gb_svc *svc = intf->hd->svc;
827 	int ret;
828 
829 	dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
830 
831 	ret = gb_svc_intf_vsys_set(svc, intf->interface_id, enable);
832 	if (ret) {
833 		dev_err(&intf->dev, "failed to set v_sys: %d\n", ret);
834 		return ret;
835 	}
836 
837 	return 0;
838 }
839 
840 static int gb_interface_refclk_set(struct gb_interface *intf, bool enable)
841 {
842 	struct gb_svc *svc = intf->hd->svc;
843 	int ret;
844 
845 	dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
846 
847 	ret = gb_svc_intf_refclk_set(svc, intf->interface_id, enable);
848 	if (ret) {
849 		dev_err(&intf->dev, "failed to set refclk: %d\n", ret);
850 		return ret;
851 	}
852 
853 	return 0;
854 }
855 
856 static int gb_interface_unipro_set(struct gb_interface *intf, bool enable)
857 {
858 	struct gb_svc *svc = intf->hd->svc;
859 	int ret;
860 
861 	dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
862 
863 	ret = gb_svc_intf_unipro_set(svc, intf->interface_id, enable);
864 	if (ret) {
865 		dev_err(&intf->dev, "failed to set UniPro: %d\n", ret);
866 		return ret;
867 	}
868 
869 	return 0;
870 }
871 
872 static int gb_interface_activate_operation(struct gb_interface *intf,
873 					   enum gb_interface_type *intf_type)
874 {
875 	struct gb_svc *svc = intf->hd->svc;
876 	u8 type;
877 	int ret;
878 
879 	dev_dbg(&intf->dev, "%s\n", __func__);
880 
881 	ret = gb_svc_intf_activate(svc, intf->interface_id, &type);
882 	if (ret) {
883 		dev_err(&intf->dev, "failed to activate: %d\n", ret);
884 		return ret;
885 	}
886 
887 	switch (type) {
888 	case GB_SVC_INTF_TYPE_DUMMY:
889 		*intf_type = GB_INTERFACE_TYPE_DUMMY;
890 		/* FIXME: handle as an error for now */
891 		return -ENODEV;
892 	case GB_SVC_INTF_TYPE_UNIPRO:
893 		*intf_type = GB_INTERFACE_TYPE_UNIPRO;
894 		dev_err(&intf->dev, "interface type UniPro not supported\n");
895 		/* FIXME: handle as an error for now */
896 		return -ENODEV;
897 	case GB_SVC_INTF_TYPE_GREYBUS:
898 		*intf_type = GB_INTERFACE_TYPE_GREYBUS;
899 		break;
900 	default:
901 		dev_err(&intf->dev, "unknown interface type: %u\n", type);
902 		*intf_type = GB_INTERFACE_TYPE_UNKNOWN;
903 		return -ENODEV;
904 	}
905 
906 	return 0;
907 }
908 
909 static int gb_interface_hibernate_link(struct gb_interface *intf)
910 {
911 	struct gb_svc *svc = intf->hd->svc;
912 
913 	return gb_svc_intf_set_power_mode_hibernate(svc, intf->interface_id);
914 }
915 
916 static int _gb_interface_activate(struct gb_interface *intf,
917 				  enum gb_interface_type *type)
918 {
919 	int ret;
920 
921 	*type = GB_INTERFACE_TYPE_UNKNOWN;
922 
923 	if (intf->ejected || intf->removed)
924 		return -ENODEV;
925 
926 	ret = gb_interface_vsys_set(intf, true);
927 	if (ret)
928 		return ret;
929 
930 	ret = gb_interface_refclk_set(intf, true);
931 	if (ret)
932 		goto err_vsys_disable;
933 
934 	ret = gb_interface_unipro_set(intf, true);
935 	if (ret)
936 		goto err_refclk_disable;
937 
938 	ret = gb_interface_activate_operation(intf, type);
939 	if (ret) {
940 		switch (*type) {
941 		case GB_INTERFACE_TYPE_UNIPRO:
942 		case GB_INTERFACE_TYPE_GREYBUS:
943 			goto err_hibernate_link;
944 		default:
945 			goto err_unipro_disable;
946 		}
947 	}
948 
949 	ret = gb_interface_read_dme(intf);
950 	if (ret)
951 		goto err_hibernate_link;
952 
953 	ret = gb_interface_route_create(intf);
954 	if (ret)
955 		goto err_hibernate_link;
956 
957 	intf->active = true;
958 
959 	trace_gb_interface_activate(intf);
960 
961 	return 0;
962 
963 err_hibernate_link:
964 	gb_interface_hibernate_link(intf);
965 err_unipro_disable:
966 	gb_interface_unipro_set(intf, false);
967 err_refclk_disable:
968 	gb_interface_refclk_set(intf, false);
969 err_vsys_disable:
970 	gb_interface_vsys_set(intf, false);
971 
972 	return ret;
973 }
974 
975 /*
976  * At present, we assume a UniPro-only module to be a Greybus module that
977  * failed to send its mailbox poke. There is some reason to believe that this
978  * is because of a bug in the ES3 bootrom.
979  *
980  * FIXME: Check if this is a Toshiba bridge before retrying?
981  */
982 static int _gb_interface_activate_es3_hack(struct gb_interface *intf,
983 					   enum gb_interface_type *type)
984 {
985 	int retries = 3;
986 	int ret;
987 
988 	while (retries--) {
989 		ret = _gb_interface_activate(intf, type);
990 		if (ret == -ENODEV && *type == GB_INTERFACE_TYPE_UNIPRO)
991 			continue;
992 
993 		break;
994 	}
995 
996 	return ret;
997 }
998 
999 /*
1000  * Activate an interface.
1001  *
1002  * Locking: Caller holds the interface mutex.
1003  */
1004 int gb_interface_activate(struct gb_interface *intf)
1005 {
1006 	enum gb_interface_type type;
1007 	int ret;
1008 
1009 	switch (intf->type) {
1010 	case GB_INTERFACE_TYPE_INVALID:
1011 	case GB_INTERFACE_TYPE_GREYBUS:
1012 		ret = _gb_interface_activate_es3_hack(intf, &type);
1013 		break;
1014 	default:
1015 		ret = _gb_interface_activate(intf, &type);
1016 	}
1017 
1018 	/* Make sure type is detected correctly during reactivation. */
1019 	if (intf->type != GB_INTERFACE_TYPE_INVALID) {
1020 		if (type != intf->type) {
1021 			dev_err(&intf->dev, "failed to detect interface type\n");
1022 
1023 			if (!ret)
1024 				gb_interface_deactivate(intf);
1025 
1026 			return -EIO;
1027 		}
1028 	} else {
1029 		intf->type = type;
1030 	}
1031 
1032 	return ret;
1033 }
1034 
1035 /*
1036  * Deactivate an interface.
1037  *
1038  * Locking: Caller holds the interface mutex.
1039  */
1040 void gb_interface_deactivate(struct gb_interface *intf)
1041 {
1042 	if (!intf->active)
1043 		return;
1044 
1045 	trace_gb_interface_deactivate(intf);
1046 
1047 	/* Abort any ongoing mode switch. */
1048 	if (intf->mode_switch)
1049 		complete(&intf->mode_switch_completion);
1050 
1051 	gb_interface_route_destroy(intf);
1052 	gb_interface_hibernate_link(intf);
1053 	gb_interface_unipro_set(intf, false);
1054 	gb_interface_refclk_set(intf, false);
1055 	gb_interface_vsys_set(intf, false);
1056 
1057 	intf->active = false;
1058 }
1059 
1060 /*
1061  * Enable an interface by enabling its control connection, fetching the
1062  * manifest and other information over it, and finally registering its child
1063  * devices.
1064  *
1065  * Locking: Caller holds the interface mutex.
1066  */
1067 int gb_interface_enable(struct gb_interface *intf)
1068 {
1069 	struct gb_control *control;
1070 	struct gb_bundle *bundle, *tmp;
1071 	int ret, size;
1072 	void *manifest;
1073 
1074 	ret = gb_interface_read_and_clear_init_status(intf);
1075 	if (ret) {
1076 		dev_err(&intf->dev, "failed to clear init status: %d\n", ret);
1077 		return ret;
1078 	}
1079 
1080 	/* Establish control connection */
1081 	control = gb_control_create(intf);
1082 	if (IS_ERR(control)) {
1083 		dev_err(&intf->dev, "failed to create control device: %ld\n",
1084 			PTR_ERR(control));
1085 		return PTR_ERR(control);
1086 	}
1087 	intf->control = control;
1088 
1089 	ret = gb_control_enable(intf->control);
1090 	if (ret)
1091 		goto err_put_control;
1092 
1093 	/* Get manifest size using control protocol on CPort */
1094 	size = gb_control_get_manifest_size_operation(intf);
1095 	if (size <= 0) {
1096 		dev_err(&intf->dev, "failed to get manifest size: %d\n", size);
1097 
1098 		if (size)
1099 			ret = size;
1100 		else
1101 			ret =  -EINVAL;
1102 
1103 		goto err_disable_control;
1104 	}
1105 
1106 	manifest = kmalloc(size, GFP_KERNEL);
1107 	if (!manifest) {
1108 		ret = -ENOMEM;
1109 		goto err_disable_control;
1110 	}
1111 
1112 	/* Get manifest using control protocol on CPort */
1113 	ret = gb_control_get_manifest_operation(intf, manifest, size);
1114 	if (ret) {
1115 		dev_err(&intf->dev, "failed to get manifest: %d\n", ret);
1116 		goto err_free_manifest;
1117 	}
1118 
1119 	/*
1120 	 * Parse the manifest and build up our data structures representing
1121 	 * what's in it.
1122 	 */
1123 	if (!gb_manifest_parse(intf, manifest, size)) {
1124 		dev_err(&intf->dev, "failed to parse manifest\n");
1125 		ret = -EINVAL;
1126 		goto err_destroy_bundles;
1127 	}
1128 
1129 	ret = gb_control_get_bundle_versions(intf->control);
1130 	if (ret)
1131 		goto err_destroy_bundles;
1132 
1133 	/* Register the control device and any bundles */
1134 	ret = gb_control_add(intf->control);
1135 	if (ret)
1136 		goto err_destroy_bundles;
1137 
1138 	pm_runtime_use_autosuspend(&intf->dev);
1139 	pm_runtime_get_noresume(&intf->dev);
1140 	pm_runtime_set_active(&intf->dev);
1141 	pm_runtime_enable(&intf->dev);
1142 
1143 	list_for_each_entry_safe_reverse(bundle, tmp, &intf->bundles, links) {
1144 		ret = gb_bundle_add(bundle);
1145 		if (ret) {
1146 			gb_bundle_destroy(bundle);
1147 			continue;
1148 		}
1149 	}
1150 
1151 	kfree(manifest);
1152 
1153 	intf->enabled = true;
1154 
1155 	pm_runtime_put(&intf->dev);
1156 
1157 	trace_gb_interface_enable(intf);
1158 
1159 	return 0;
1160 
1161 err_destroy_bundles:
1162 	list_for_each_entry_safe(bundle, tmp, &intf->bundles, links)
1163 		gb_bundle_destroy(bundle);
1164 err_free_manifest:
1165 	kfree(manifest);
1166 err_disable_control:
1167 	gb_control_disable(intf->control);
1168 err_put_control:
1169 	gb_control_put(intf->control);
1170 	intf->control = NULL;
1171 
1172 	return ret;
1173 }
1174 
1175 /*
1176  * Disable an interface and destroy its bundles.
1177  *
1178  * Locking: Caller holds the interface mutex.
1179  */
1180 void gb_interface_disable(struct gb_interface *intf)
1181 {
1182 	struct gb_bundle *bundle;
1183 	struct gb_bundle *next;
1184 
1185 	if (!intf->enabled)
1186 		return;
1187 
1188 	trace_gb_interface_disable(intf);
1189 
1190 	pm_runtime_get_sync(&intf->dev);
1191 
1192 	/* Set disconnected flag to avoid I/O during connection tear down. */
1193 	if (intf->quirks & GB_INTERFACE_QUIRK_FORCED_DISABLE)
1194 		intf->disconnected = true;
1195 
1196 	list_for_each_entry_safe(bundle, next, &intf->bundles, links)
1197 		gb_bundle_destroy(bundle);
1198 
1199 	if (!intf->mode_switch && !intf->disconnected)
1200 		gb_control_interface_deactivate_prepare(intf->control);
1201 
1202 	gb_control_del(intf->control);
1203 	gb_control_disable(intf->control);
1204 	gb_control_put(intf->control);
1205 	intf->control = NULL;
1206 
1207 	intf->enabled = false;
1208 
1209 	pm_runtime_disable(&intf->dev);
1210 	pm_runtime_set_suspended(&intf->dev);
1211 	pm_runtime_dont_use_autosuspend(&intf->dev);
1212 	pm_runtime_put_noidle(&intf->dev);
1213 }
1214 
1215 /* Register an interface. */
1216 int gb_interface_add(struct gb_interface *intf)
1217 {
1218 	int ret;
1219 
1220 	ret = device_add(&intf->dev);
1221 	if (ret) {
1222 		dev_err(&intf->dev, "failed to register interface: %d\n", ret);
1223 		return ret;
1224 	}
1225 
1226 	trace_gb_interface_add(intf);
1227 
1228 	dev_info(&intf->dev, "Interface added (%s)\n",
1229 		 gb_interface_type_string(intf));
1230 
1231 	switch (intf->type) {
1232 	case GB_INTERFACE_TYPE_GREYBUS:
1233 		dev_info(&intf->dev, "GMP VID=0x%08x, PID=0x%08x\n",
1234 			 intf->vendor_id, intf->product_id);
1235 		fallthrough;
1236 	case GB_INTERFACE_TYPE_UNIPRO:
1237 		dev_info(&intf->dev, "DDBL1 Manufacturer=0x%08x, Product=0x%08x\n",
1238 			 intf->ddbl1_manufacturer_id,
1239 			 intf->ddbl1_product_id);
1240 		break;
1241 	default:
1242 		break;
1243 	}
1244 
1245 	return 0;
1246 }
1247 
1248 /* Deregister an interface. */
1249 void gb_interface_del(struct gb_interface *intf)
1250 {
1251 	if (device_is_registered(&intf->dev)) {
1252 		trace_gb_interface_del(intf);
1253 
1254 		device_del(&intf->dev);
1255 		dev_info(&intf->dev, "Interface removed\n");
1256 	}
1257 }
1258 
1259 void gb_interface_put(struct gb_interface *intf)
1260 {
1261 	put_device(&intf->dev);
1262 }
1263