Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Connection Management Procedures (IEC 61883-1) helper functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/firewire.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/firewire-constants.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "lib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "iso-resources.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "cmp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) /* MPR common fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define MPR_SPEED_MASK		0xc0000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define MPR_SPEED_SHIFT		30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define MPR_XSPEED_MASK		0x00000060
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define MPR_XSPEED_SHIFT	5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define MPR_PLUGS_MASK		0x0000001f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) /* PCR common fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define PCR_ONLINE		0x80000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define PCR_BCAST_CONN		0x40000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define PCR_P2P_CONN_MASK	0x3f000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define PCR_P2P_CONN_SHIFT	24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define PCR_CHANNEL_MASK	0x003f0000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define PCR_CHANNEL_SHIFT	16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /* oPCR specific fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define OPCR_XSPEED_MASK	0x00C00000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define OPCR_XSPEED_SHIFT	22
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define OPCR_SPEED_MASK		0x0000C000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define OPCR_SPEED_SHIFT	14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define OPCR_OVERHEAD_ID_MASK	0x00003C00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define OPCR_OVERHEAD_ID_SHIFT	10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) enum bus_reset_handling {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	ABORT_ON_BUS_RESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	SUCCEED_ON_BUS_RESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static __printf(2, 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) void cmp_error(struct cmp_connection *c, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	va_list va;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	va_start(va, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	dev_err(&c->resources.unit->device, "%cPCR%u: %pV",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		(c->direction == CMP_INPUT) ? 'i' : 'o',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		c->pcr_index, &(struct va_format){ fmt, &va });
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	va_end(va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static u64 mpr_address(struct cmp_connection *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (c->direction == CMP_INPUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return CSR_REGISTER_BASE + CSR_IMPR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return CSR_REGISTER_BASE + CSR_OMPR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static u64 pcr_address(struct cmp_connection *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (c->direction == CMP_INPUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		return CSR_REGISTER_BASE + CSR_IPCR(c->pcr_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		return CSR_REGISTER_BASE + CSR_OPCR(c->pcr_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static int pcr_modify(struct cmp_connection *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		      __be32 (*modify)(struct cmp_connection *c, __be32 old),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		      int (*check)(struct cmp_connection *c, __be32 pcr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		      enum bus_reset_handling bus_reset_handling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	__be32 old_arg, buffer[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	buffer[0] = c->last_pcr_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		old_arg = buffer[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		buffer[1] = modify(c, buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		err = snd_fw_transaction(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 				c->resources.unit, TCODE_LOCK_COMPARE_SWAP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				pcr_address(c), buffer, 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 				FW_FIXED_GENERATION | c->resources.generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			if (err == -EAGAIN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			    bus_reset_handling == SUCCEED_ON_BUS_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 				err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		if (buffer[0] == old_arg) /* success? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		if (check) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			err = check(c, buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	c->last_pcr_value = buffer[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  * cmp_connection_init - initializes a connection manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  * @c: the connection manager to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  * @unit: a unit of the target device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  * @direction: input or output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  * @pcr_index: the index of the iPCR/oPCR on the target device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int cmp_connection_init(struct cmp_connection *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			struct fw_unit *unit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			enum cmp_direction direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			unsigned int pcr_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	__be32 mpr_be;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	u32 mpr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	c->direction = direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				 mpr_address(c), &mpr_be, 4, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	mpr = be32_to_cpu(mpr_be);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (pcr_index >= (mpr & MPR_PLUGS_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	err = fw_iso_resources_init(&c->resources, unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	c->connected = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	mutex_init(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	c->last_pcr_value = cpu_to_be32(0x80000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	c->pcr_index = pcr_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	c->max_speed = (mpr & MPR_SPEED_MASK) >> MPR_SPEED_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (c->max_speed == SCODE_BETA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		c->max_speed += (mpr & MPR_XSPEED_MASK) >> MPR_XSPEED_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) EXPORT_SYMBOL(cmp_connection_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * cmp_connection_check_used - check connection is already esablished or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  * @c: the connection manager to be checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * @used: the pointer to store the result of checking the connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int cmp_connection_check_used(struct cmp_connection *c, bool *used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	__be32 pcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	err = snd_fw_transaction(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			c->resources.unit, TCODE_READ_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			pcr_address(c), &pcr, 4, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (err >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		*used = !!(pcr & cpu_to_be32(PCR_BCAST_CONN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 					     PCR_P2P_CONN_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) EXPORT_SYMBOL(cmp_connection_check_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * cmp_connection_destroy - free connection manager resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  * @c: the connection manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) void cmp_connection_destroy(struct cmp_connection *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	WARN_ON(c->connected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	mutex_destroy(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	fw_iso_resources_destroy(&c->resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) EXPORT_SYMBOL(cmp_connection_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int cmp_connection_reserve(struct cmp_connection *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			   unsigned int max_payload_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	mutex_lock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (WARN_ON(c->resources.allocated)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	c->speed = min(c->max_speed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		       fw_parent_device(c->resources.unit)->max_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	err = fw_iso_resources_allocate(&c->resources, max_payload_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 					c->speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	mutex_unlock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) EXPORT_SYMBOL(cmp_connection_reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) void cmp_connection_release(struct cmp_connection *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	mutex_lock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	fw_iso_resources_free(&c->resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	mutex_unlock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) EXPORT_SYMBOL(cmp_connection_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static __be32 ipcr_set_modify(struct cmp_connection *c, __be32 ipcr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	ipcr &= ~cpu_to_be32(PCR_BCAST_CONN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			     PCR_P2P_CONN_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			     PCR_CHANNEL_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	ipcr |= cpu_to_be32(1 << PCR_P2P_CONN_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	ipcr |= cpu_to_be32(c->resources.channel << PCR_CHANNEL_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	return ipcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static int get_overhead_id(struct cmp_connection *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	 * apply "oPCR overhead ID encoding"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	 * the encoding table can convert up to 512.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	 * here the value over 512 is converted as the same way as 512.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	for (id = 1; id < 16; id++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (c->resources.bandwidth_overhead < (id << 5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (id == 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static __be32 opcr_set_modify(struct cmp_connection *c, __be32 opcr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	unsigned int spd, xspd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	/* generate speed and extended speed field value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (c->speed > SCODE_400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		spd  = SCODE_800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		xspd = c->speed - SCODE_800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		spd = c->speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		xspd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	opcr &= ~cpu_to_be32(PCR_BCAST_CONN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			     PCR_P2P_CONN_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			     OPCR_XSPEED_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			     PCR_CHANNEL_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			     OPCR_SPEED_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			     OPCR_OVERHEAD_ID_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	opcr |= cpu_to_be32(1 << PCR_P2P_CONN_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	opcr |= cpu_to_be32(xspd << OPCR_XSPEED_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	opcr |= cpu_to_be32(c->resources.channel << PCR_CHANNEL_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	opcr |= cpu_to_be32(spd << OPCR_SPEED_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	opcr |= cpu_to_be32(get_overhead_id(c) << OPCR_OVERHEAD_ID_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	return opcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static int pcr_set_check(struct cmp_connection *c, __be32 pcr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (pcr & cpu_to_be32(PCR_BCAST_CONN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			      PCR_P2P_CONN_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		cmp_error(c, "plug is already in use\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (!(pcr & cpu_to_be32(PCR_ONLINE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		cmp_error(c, "plug is not on-line\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		return -ECONNREFUSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  * cmp_connection_establish - establish a connection to the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  * @c: the connection manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)  * This function establishes a point-to-point connection from the local
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)  * computer to the target by allocating isochronous resources (channel and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)  * bandwidth) and setting the target's input/output plug control register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  * When this function succeeds, the caller is responsible for starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  * transmitting packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int cmp_connection_establish(struct cmp_connection *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	mutex_lock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (WARN_ON(c->connected)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		mutex_unlock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		return -EISCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) retry_after_bus_reset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (c->direction == CMP_OUTPUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		err = pcr_modify(c, opcr_set_modify, pcr_set_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 				 ABORT_ON_BUS_RESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		err = pcr_modify(c, ipcr_set_modify, pcr_set_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 				 ABORT_ON_BUS_RESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (err == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		err = fw_iso_resources_update(&c->resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		if (err >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			goto retry_after_bus_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (err >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		c->connected = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	mutex_unlock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) EXPORT_SYMBOL(cmp_connection_establish);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)  * cmp_connection_update - update the connection after a bus reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  * @c: the connection manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  * This function must be called from the driver's .update handler to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  * reestablish any connection that might have been active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  * Returns zero on success, or a negative error code.  On an error, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * connection is broken and the caller must stop transmitting iso packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) int cmp_connection_update(struct cmp_connection *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	mutex_lock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (!c->connected) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		mutex_unlock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	err = fw_iso_resources_update(&c->resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		goto err_unconnect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (c->direction == CMP_OUTPUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		err = pcr_modify(c, opcr_set_modify, pcr_set_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 				 SUCCEED_ON_BUS_RESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		err = pcr_modify(c, ipcr_set_modify, pcr_set_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 				 SUCCEED_ON_BUS_RESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		goto err_unconnect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	mutex_unlock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) err_unconnect:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	c->connected = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	mutex_unlock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) EXPORT_SYMBOL(cmp_connection_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static __be32 pcr_break_modify(struct cmp_connection *c, __be32 pcr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	return pcr & ~cpu_to_be32(PCR_BCAST_CONN | PCR_P2P_CONN_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)  * cmp_connection_break - break the connection to the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)  * @c: the connection manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)  * This function deactives the connection in the target's input/output plug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)  * control register, and frees the isochronous resources of the connection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)  * Before calling this function, the caller should cease transmitting packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) void cmp_connection_break(struct cmp_connection *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	mutex_lock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (!c->connected) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		mutex_unlock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	err = pcr_modify(c, pcr_break_modify, NULL, SUCCEED_ON_BUS_RESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		cmp_error(c, "plug is still connected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	c->connected = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	mutex_unlock(&c->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) EXPORT_SYMBOL(cmp_connection_break);