xref: /linux/sound/firewire/amdtp-stream.c (revision 6b779f8a8648848d74c24b07d0e2436c00211788)
1da607e19SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d67c46b9STakashi Sakamoto /*
3d67c46b9STakashi Sakamoto  * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
4d67c46b9STakashi Sakamoto  * with Common Isochronous Packet (IEC 61883-1) headers
5d67c46b9STakashi Sakamoto  *
6d67c46b9STakashi Sakamoto  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
7d67c46b9STakashi Sakamoto  */
8d67c46b9STakashi Sakamoto 
9d67c46b9STakashi Sakamoto #include <linux/device.h>
10d67c46b9STakashi Sakamoto #include <linux/err.h>
11d67c46b9STakashi Sakamoto #include <linux/firewire.h>
12acfedcbeSTakashi Sakamoto #include <linux/firewire-constants.h>
13d67c46b9STakashi Sakamoto #include <linux/module.h>
14d67c46b9STakashi Sakamoto #include <linux/slab.h>
15d67c46b9STakashi Sakamoto #include <sound/pcm.h>
16d67c46b9STakashi Sakamoto #include <sound/pcm_params.h>
17d67c46b9STakashi Sakamoto #include "amdtp-stream.h"
18d67c46b9STakashi Sakamoto 
19d67c46b9STakashi Sakamoto #define TICKS_PER_CYCLE		3072
20d67c46b9STakashi Sakamoto #define CYCLES_PER_SECOND	8000
21d67c46b9STakashi Sakamoto #define TICKS_PER_SECOND	(TICKS_PER_CYCLE * CYCLES_PER_SECOND)
22d67c46b9STakashi Sakamoto 
233e106f4fSTakashi Sakamoto #define OHCI_SECOND_MODULUS		8
2410aa8e4aSTakashi Sakamoto 
250c95c1d6STakashi Sakamoto /* Always support Linux tracing subsystem. */
260c95c1d6STakashi Sakamoto #define CREATE_TRACE_POINTS
270c95c1d6STakashi Sakamoto #include "amdtp-stream-trace.h"
280c95c1d6STakashi Sakamoto 
29d67c46b9STakashi Sakamoto #define TRANSFER_DELAY_TICKS	0x2e00 /* 479.17 microseconds */
30d67c46b9STakashi Sakamoto 
31d67c46b9STakashi Sakamoto /* isochronous header parameters */
32d67c46b9STakashi Sakamoto #define ISO_DATA_LENGTH_SHIFT	16
333b196c39STakashi Sakamoto #define TAG_NO_CIP_HEADER	0
34d67c46b9STakashi Sakamoto #define TAG_CIP			1
35d67c46b9STakashi Sakamoto 
3667d92ee7STakashi Sakamoto // Common Isochronous Packet (CIP) header parameters. Use two quadlets CIP header when supported.
3767d92ee7STakashi Sakamoto #define CIP_HEADER_QUADLETS	2
38d67c46b9STakashi Sakamoto #define CIP_EOH_SHIFT		31
39d67c46b9STakashi Sakamoto #define CIP_EOH			(1u << CIP_EOH_SHIFT)
40d67c46b9STakashi Sakamoto #define CIP_EOH_MASK		0x80000000
41d67c46b9STakashi Sakamoto #define CIP_SID_SHIFT		24
42d67c46b9STakashi Sakamoto #define CIP_SID_MASK		0x3f000000
43d67c46b9STakashi Sakamoto #define CIP_DBS_MASK		0x00ff0000
44d67c46b9STakashi Sakamoto #define CIP_DBS_SHIFT		16
459863874fSTakashi Sakamoto #define CIP_SPH_MASK		0x00000400
469863874fSTakashi Sakamoto #define CIP_SPH_SHIFT		10
47d67c46b9STakashi Sakamoto #define CIP_DBC_MASK		0x000000ff
48d67c46b9STakashi Sakamoto #define CIP_FMT_SHIFT		24
49d67c46b9STakashi Sakamoto #define CIP_FMT_MASK		0x3f000000
50d67c46b9STakashi Sakamoto #define CIP_FDF_MASK		0x00ff0000
51d67c46b9STakashi Sakamoto #define CIP_FDF_SHIFT		16
52fb25dcc8STakashi Sakamoto #define CIP_FDF_NO_DATA		0xff
53d67c46b9STakashi Sakamoto #define CIP_SYT_MASK		0x0000ffff
54d67c46b9STakashi Sakamoto #define CIP_SYT_NO_INFO		0xffff
55f9e5ecdfSTakashi Sakamoto #define CIP_SYT_CYCLE_MODULUS	16
56fb25dcc8STakashi Sakamoto #define CIP_NO_DATA		((CIP_FDF_NO_DATA << CIP_FDF_SHIFT) | CIP_SYT_NO_INFO)
57d67c46b9STakashi Sakamoto 
5867d92ee7STakashi Sakamoto #define CIP_HEADER_SIZE		(sizeof(__be32) * CIP_HEADER_QUADLETS)
5967d92ee7STakashi Sakamoto 
6051c29fd2STakashi Sakamoto /* Audio and Music transfer protocol specific parameters */
61d67c46b9STakashi Sakamoto #define CIP_FMT_AM		0x10
62d67c46b9STakashi Sakamoto #define AMDTP_FDF_NO_DATA	0xff
63d67c46b9STakashi Sakamoto 
64f11453c7STakashi Sakamoto // For iso header and tstamp.
6567d92ee7STakashi Sakamoto #define IR_CTX_HEADER_DEFAULT_QUADLETS	2
6667d92ee7STakashi Sakamoto // Add nothing.
6767d92ee7STakashi Sakamoto #define IR_CTX_HEADER_SIZE_NO_CIP	(sizeof(__be32) * IR_CTX_HEADER_DEFAULT_QUADLETS)
6867d92ee7STakashi Sakamoto // Add two quadlets CIP header.
6967d92ee7STakashi Sakamoto #define IR_CTX_HEADER_SIZE_CIP		(IR_CTX_HEADER_SIZE_NO_CIP + CIP_HEADER_SIZE)
70cc4f8e91STakashi Sakamoto #define HEADER_TSTAMP_MASK	0x0000ffff
71d67c46b9STakashi Sakamoto 
7267d92ee7STakashi Sakamoto #define IT_PKT_HEADER_SIZE_CIP		CIP_HEADER_SIZE
73b18f0cfaSTakashi Sakamoto #define IT_PKT_HEADER_SIZE_NO_CIP	0 // Nothing.
74b18f0cfaSTakashi Sakamoto 
756a3ce97dSTakashi Sakamoto // The initial firmware of OXFW970 can postpone transmission of packet during finishing
766a3ce97dSTakashi Sakamoto // asynchronous transaction. This module accepts 5 cycles to skip as maximum to avoid buffer
776a3ce97dSTakashi Sakamoto // overrun. Actual device can skip more, then this module stops the packet streaming.
786a3ce97dSTakashi Sakamoto #define IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES	5
796a3ce97dSTakashi Sakamoto 
806ccf9984SEdmund Raile static void pcm_period_work(struct work_struct *work);
816ccf9984SEdmund Raile 
82d67c46b9STakashi Sakamoto /**
83d67c46b9STakashi Sakamoto  * amdtp_stream_init - initialize an AMDTP stream structure
84d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to initialize
85d67c46b9STakashi Sakamoto  * @unit: the target of the stream
86d67c46b9STakashi Sakamoto  * @dir: the direction of stream
87ffe66bbeSTakashi Sakamoto  * @flags: the details of the streaming protocol consist of cip_flags enumeration-constants.
885955815eSTakashi Sakamoto  * @fmt: the value of fmt field in CIP header
899a738ad1STakashi Sakamoto  * @process_ctx_payloads: callback handler to process payloads of isoc context
90df075feeSTakashi Sakamoto  * @protocol_size: the size to allocate newly for protocol
91d67c46b9STakashi Sakamoto  */
amdtp_stream_init(struct amdtp_stream * s,struct fw_unit * unit,enum amdtp_stream_direction dir,unsigned int flags,unsigned int fmt,amdtp_stream_process_ctx_payloads_t process_ctx_payloads,unsigned int protocol_size)92d67c46b9STakashi Sakamoto int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
93ffe66bbeSTakashi Sakamoto 		      enum amdtp_stream_direction dir, unsigned int flags,
94df075feeSTakashi Sakamoto 		      unsigned int fmt,
959a738ad1STakashi Sakamoto 		      amdtp_stream_process_ctx_payloads_t process_ctx_payloads,
96df075feeSTakashi Sakamoto 		      unsigned int protocol_size)
97d67c46b9STakashi Sakamoto {
989a738ad1STakashi Sakamoto 	if (process_ctx_payloads == NULL)
99df075feeSTakashi Sakamoto 		return -EINVAL;
100df075feeSTakashi Sakamoto 
101df075feeSTakashi Sakamoto 	s->protocol = kzalloc(protocol_size, GFP_KERNEL);
102df075feeSTakashi Sakamoto 	if (!s->protocol)
103df075feeSTakashi Sakamoto 		return -ENOMEM;
104df075feeSTakashi Sakamoto 
105d67c46b9STakashi Sakamoto 	s->unit = unit;
106d67c46b9STakashi Sakamoto 	s->direction = dir;
107d67c46b9STakashi Sakamoto 	s->flags = flags;
108d67c46b9STakashi Sakamoto 	s->context = ERR_PTR(-1);
109d67c46b9STakashi Sakamoto 	mutex_init(&s->mutex);
1106ccf9984SEdmund Raile 	INIT_WORK(&s->period_work, pcm_period_work);
111d67c46b9STakashi Sakamoto 	s->packet_index = 0;
112d67c46b9STakashi Sakamoto 
113bdaedca7STakashi Sakamoto 	init_waitqueue_head(&s->ready_wait);
114d67c46b9STakashi Sakamoto 
1155955815eSTakashi Sakamoto 	s->fmt = fmt;
1169a738ad1STakashi Sakamoto 	s->process_ctx_payloads = process_ctx_payloads;
117d67c46b9STakashi Sakamoto 
118d67c46b9STakashi Sakamoto 	return 0;
119d67c46b9STakashi Sakamoto }
120d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_init);
121d67c46b9STakashi Sakamoto 
122d67c46b9STakashi Sakamoto /**
123d67c46b9STakashi Sakamoto  * amdtp_stream_destroy - free stream resources
124d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to destroy
125d67c46b9STakashi Sakamoto  */
amdtp_stream_destroy(struct amdtp_stream * s)126d67c46b9STakashi Sakamoto void amdtp_stream_destroy(struct amdtp_stream *s)
127d67c46b9STakashi Sakamoto {
12844c376b9STakashi Sakamoto 	/* Not initialized. */
12944c376b9STakashi Sakamoto 	if (s->protocol == NULL)
13044c376b9STakashi Sakamoto 		return;
13144c376b9STakashi Sakamoto 
132d67c46b9STakashi Sakamoto 	WARN_ON(amdtp_stream_running(s));
133df075feeSTakashi Sakamoto 	kfree(s->protocol);
134d67c46b9STakashi Sakamoto 	mutex_destroy(&s->mutex);
135d67c46b9STakashi Sakamoto }
136d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_destroy);
137d67c46b9STakashi Sakamoto 
138d67c46b9STakashi Sakamoto const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
139d67c46b9STakashi Sakamoto 	[CIP_SFC_32000]  =  8,
140d67c46b9STakashi Sakamoto 	[CIP_SFC_44100]  =  8,
141d67c46b9STakashi Sakamoto 	[CIP_SFC_48000]  =  8,
142d67c46b9STakashi Sakamoto 	[CIP_SFC_88200]  = 16,
143d67c46b9STakashi Sakamoto 	[CIP_SFC_96000]  = 16,
144d67c46b9STakashi Sakamoto 	[CIP_SFC_176400] = 32,
145d67c46b9STakashi Sakamoto 	[CIP_SFC_192000] = 32,
146d67c46b9STakashi Sakamoto };
147d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_syt_intervals);
148d67c46b9STakashi Sakamoto 
149d67c46b9STakashi Sakamoto const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
150d67c46b9STakashi Sakamoto 	[CIP_SFC_32000]  =  32000,
151d67c46b9STakashi Sakamoto 	[CIP_SFC_44100]  =  44100,
152d67c46b9STakashi Sakamoto 	[CIP_SFC_48000]  =  48000,
153d67c46b9STakashi Sakamoto 	[CIP_SFC_88200]  =  88200,
154d67c46b9STakashi Sakamoto 	[CIP_SFC_96000]  =  96000,
155d67c46b9STakashi Sakamoto 	[CIP_SFC_176400] = 176400,
156d67c46b9STakashi Sakamoto 	[CIP_SFC_192000] = 192000,
157d67c46b9STakashi Sakamoto };
158d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_rate_table);
159d67c46b9STakashi Sakamoto 
apply_constraint_to_size(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)16059502295STakashi Sakamoto static int apply_constraint_to_size(struct snd_pcm_hw_params *params,
16159502295STakashi Sakamoto 				    struct snd_pcm_hw_rule *rule)
16259502295STakashi Sakamoto {
16359502295STakashi Sakamoto 	struct snd_interval *s = hw_param_interval(params, rule->var);
16459502295STakashi Sakamoto 	const struct snd_interval *r =
16559502295STakashi Sakamoto 		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
166826b5de9STakashi Sakamoto 	struct snd_interval t = {0};
167826b5de9STakashi Sakamoto 	unsigned int step = 0;
16859502295STakashi Sakamoto 	int i;
16959502295STakashi Sakamoto 
17059502295STakashi Sakamoto 	for (i = 0; i < CIP_SFC_COUNT; ++i) {
171826b5de9STakashi Sakamoto 		if (snd_interval_test(r, amdtp_rate_table[i]))
172826b5de9STakashi Sakamoto 			step = max(step, amdtp_syt_intervals[i]);
17359502295STakashi Sakamoto 	}
17459502295STakashi Sakamoto 
175826b5de9STakashi Sakamoto 	t.min = roundup(s->min, step);
176826b5de9STakashi Sakamoto 	t.max = rounddown(s->max, step);
177826b5de9STakashi Sakamoto 	t.integer = 1;
17859502295STakashi Sakamoto 
17959502295STakashi Sakamoto 	return snd_interval_refine(s, &t);
18059502295STakashi Sakamoto }
18159502295STakashi Sakamoto 
182d67c46b9STakashi Sakamoto /**
183d67c46b9STakashi Sakamoto  * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
184d67c46b9STakashi Sakamoto  * @s:		the AMDTP stream, which must be initialized.
185d67c46b9STakashi Sakamoto  * @runtime:	the PCM substream runtime
186d67c46b9STakashi Sakamoto  */
amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream * s,struct snd_pcm_runtime * runtime)187d67c46b9STakashi Sakamoto int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
188d67c46b9STakashi Sakamoto 					struct snd_pcm_runtime *runtime)
189d67c46b9STakashi Sakamoto {
19055799c5aSTakashi Sakamoto 	struct snd_pcm_hardware *hw = &runtime->hw;
19199921ec6STakashi Sakamoto 	unsigned int ctx_header_size;
19299921ec6STakashi Sakamoto 	unsigned int maximum_usec_per_period;
193d67c46b9STakashi Sakamoto 	int err;
194d67c46b9STakashi Sakamoto 
195d360870aSTakashi Sakamoto 	hw->info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
19655799c5aSTakashi Sakamoto 		   SNDRV_PCM_INFO_INTERLEAVED |
19755799c5aSTakashi Sakamoto 		   SNDRV_PCM_INFO_JOINT_DUPLEX |
19855799c5aSTakashi Sakamoto 		   SNDRV_PCM_INFO_MMAP |
199d360870aSTakashi Sakamoto 		   SNDRV_PCM_INFO_MMAP_VALID |
200d360870aSTakashi Sakamoto 		   SNDRV_PCM_INFO_NO_PERIOD_WAKEUP;
20155799c5aSTakashi Sakamoto 
20255799c5aSTakashi Sakamoto 	hw->periods_min = 2;
20355799c5aSTakashi Sakamoto 	hw->periods_max = UINT_MAX;
20455799c5aSTakashi Sakamoto 
20555799c5aSTakashi Sakamoto 	/* bytes for a frame */
20655799c5aSTakashi Sakamoto 	hw->period_bytes_min = 4 * hw->channels_max;
20755799c5aSTakashi Sakamoto 
20855799c5aSTakashi Sakamoto 	/* Just to prevent from allocating much pages. */
20955799c5aSTakashi Sakamoto 	hw->period_bytes_max = hw->period_bytes_min * 2048;
21055799c5aSTakashi Sakamoto 	hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
21155799c5aSTakashi Sakamoto 
21299921ec6STakashi Sakamoto 	// Linux driver for 1394 OHCI controller voluntarily flushes isoc
21399921ec6STakashi Sakamoto 	// context when total size of accumulated context header reaches
2142b3d2987STakashi Iwai 	// PAGE_SIZE. This kicks work for the isoc context and brings
21599921ec6STakashi Sakamoto 	// callback in the middle of scheduled interrupts.
21699921ec6STakashi Sakamoto 	// Although AMDTP streams in the same domain use the same events per
21799921ec6STakashi Sakamoto 	// IRQ, use the largest size of context header between IT/IR contexts.
21899921ec6STakashi Sakamoto 	// Here, use the value of context header in IR context is for both
21999921ec6STakashi Sakamoto 	// contexts.
22099921ec6STakashi Sakamoto 	if (!(s->flags & CIP_NO_HEADER))
22199921ec6STakashi Sakamoto 		ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
22299921ec6STakashi Sakamoto 	else
22399921ec6STakashi Sakamoto 		ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
22499921ec6STakashi Sakamoto 	maximum_usec_per_period = USEC_PER_SEC * PAGE_SIZE /
22599921ec6STakashi Sakamoto 				  CYCLES_PER_SECOND / ctx_header_size;
22699921ec6STakashi Sakamoto 
227f706df4fSTakashi Sakamoto 	// In IEC 61883-6, one isoc packet can transfer events up to the value
228f706df4fSTakashi Sakamoto 	// of syt interval. This comes from the interval of isoc cycle. As 1394
229f706df4fSTakashi Sakamoto 	// OHCI controller can generate hardware IRQ per isoc packet, the
230f706df4fSTakashi Sakamoto 	// interval is 125 usec.
231f706df4fSTakashi Sakamoto 	// However, there are two ways of transmission in IEC 61883-6; blocking
232f706df4fSTakashi Sakamoto 	// and non-blocking modes. In blocking mode, the sequence of isoc packet
233f706df4fSTakashi Sakamoto 	// includes 'empty' or 'NODATA' packets which include no event. In
234f706df4fSTakashi Sakamoto 	// non-blocking mode, the number of events per packet is variable up to
235f706df4fSTakashi Sakamoto 	// the syt interval.
236f706df4fSTakashi Sakamoto 	// Due to the above protocol design, the minimum PCM frames per
237f706df4fSTakashi Sakamoto 	// interrupt should be double of the value of syt interval, thus it is
238f706df4fSTakashi Sakamoto 	// 250 usec.
239d67c46b9STakashi Sakamoto 	err = snd_pcm_hw_constraint_minmax(runtime,
240d67c46b9STakashi Sakamoto 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
241f706df4fSTakashi Sakamoto 					   250, maximum_usec_per_period);
242d67c46b9STakashi Sakamoto 	if (err < 0)
243d67c46b9STakashi Sakamoto 		goto end;
244d67c46b9STakashi Sakamoto 
245d67c46b9STakashi Sakamoto 	/* Non-Blocking stream has no more constraints */
246d67c46b9STakashi Sakamoto 	if (!(s->flags & CIP_BLOCKING))
247d67c46b9STakashi Sakamoto 		goto end;
248d67c46b9STakashi Sakamoto 
249d67c46b9STakashi Sakamoto 	/*
250d67c46b9STakashi Sakamoto 	 * One AMDTP packet can include some frames. In blocking mode, the
251d67c46b9STakashi Sakamoto 	 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
252d67c46b9STakashi Sakamoto 	 * depending on its sampling rate. For accurate period interrupt, it's
253d67c46b9STakashi Sakamoto 	 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
254d67c46b9STakashi Sakamoto 	 */
25559502295STakashi Sakamoto 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
25659502295STakashi Sakamoto 				  apply_constraint_to_size, NULL,
257826b5de9STakashi Sakamoto 				  SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
25859502295STakashi Sakamoto 				  SNDRV_PCM_HW_PARAM_RATE, -1);
259d67c46b9STakashi Sakamoto 	if (err < 0)
260d67c46b9STakashi Sakamoto 		goto end;
26159502295STakashi Sakamoto 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
26259502295STakashi Sakamoto 				  apply_constraint_to_size, NULL,
263826b5de9STakashi Sakamoto 				  SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
26459502295STakashi Sakamoto 				  SNDRV_PCM_HW_PARAM_RATE, -1);
26559502295STakashi Sakamoto 	if (err < 0)
26659502295STakashi Sakamoto 		goto end;
267d67c46b9STakashi Sakamoto end:
268d67c46b9STakashi Sakamoto 	return err;
269d67c46b9STakashi Sakamoto }
270d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
271d67c46b9STakashi Sakamoto 
272d67c46b9STakashi Sakamoto /**
273d67c46b9STakashi Sakamoto  * amdtp_stream_set_parameters - set stream parameters
274d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to configure
275d67c46b9STakashi Sakamoto  * @rate: the sample rate
276df075feeSTakashi Sakamoto  * @data_block_quadlets: the size of a data block in quadlet unit
277a36183f6STakashi Sakamoto  * @pcm_frame_multiplier: the multiplier to compute the number of PCM frames by the number of AMDTP
278a36183f6STakashi Sakamoto  *			  events.
279d67c46b9STakashi Sakamoto  *
280d67c46b9STakashi Sakamoto  * The parameters must be set before the stream is started, and must not be
281d67c46b9STakashi Sakamoto  * changed while the stream is running.
282d67c46b9STakashi Sakamoto  */
amdtp_stream_set_parameters(struct amdtp_stream * s,unsigned int rate,unsigned int data_block_quadlets,unsigned int pcm_frame_multiplier)283df075feeSTakashi Sakamoto int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
284a36183f6STakashi Sakamoto 				unsigned int data_block_quadlets, unsigned int pcm_frame_multiplier)
285d67c46b9STakashi Sakamoto {
286df075feeSTakashi Sakamoto 	unsigned int sfc;
287d67c46b9STakashi Sakamoto 
288d67c46b9STakashi Sakamoto 	for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
289d67c46b9STakashi Sakamoto 		if (amdtp_rate_table[sfc] == rate)
290d67c46b9STakashi Sakamoto 			break;
291d67c46b9STakashi Sakamoto 	}
292d67c46b9STakashi Sakamoto 	if (sfc == ARRAY_SIZE(amdtp_rate_table))
293d67c46b9STakashi Sakamoto 		return -EINVAL;
294d67c46b9STakashi Sakamoto 
295d67c46b9STakashi Sakamoto 	s->sfc = sfc;
296df075feeSTakashi Sakamoto 	s->data_block_quadlets = data_block_quadlets;
297d67c46b9STakashi Sakamoto 	s->syt_interval = amdtp_syt_intervals[sfc];
298d67c46b9STakashi Sakamoto 
299d3d10a4aSTakashi Sakamoto 	// default buffering in the device.
30013d11f14STakashi Sakamoto 	s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
301d3d10a4aSTakashi Sakamoto 
30213d11f14STakashi Sakamoto 	// additional buffering needed to adjust for no-data packets.
30313d11f14STakashi Sakamoto 	if (s->flags & CIP_BLOCKING)
30413d11f14STakashi Sakamoto 		s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
305d67c46b9STakashi Sakamoto 
306a36183f6STakashi Sakamoto 	s->pcm_frame_multiplier = pcm_frame_multiplier;
307a36183f6STakashi Sakamoto 
308d67c46b9STakashi Sakamoto 	return 0;
309d67c46b9STakashi Sakamoto }
310d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_set_parameters);
311d67c46b9STakashi Sakamoto 
312c75f3678STakashi Sakamoto // The CIP header is processed in context header apart from context payload.
amdtp_stream_get_max_ctx_payload_size(struct amdtp_stream * s)313c75f3678STakashi Sakamoto static int amdtp_stream_get_max_ctx_payload_size(struct amdtp_stream *s)
314c75f3678STakashi Sakamoto {
315c75f3678STakashi Sakamoto 	unsigned int multiplier;
316c75f3678STakashi Sakamoto 
317c75f3678STakashi Sakamoto 	if (s->flags & CIP_JUMBO_PAYLOAD)
318c75f3678STakashi Sakamoto 		multiplier = IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES;
319c75f3678STakashi Sakamoto 	else
320c75f3678STakashi Sakamoto 		multiplier = 1;
321c75f3678STakashi Sakamoto 
322c75f3678STakashi Sakamoto 	return s->syt_interval * s->data_block_quadlets * sizeof(__be32) * multiplier;
323c75f3678STakashi Sakamoto }
324c75f3678STakashi Sakamoto 
325d67c46b9STakashi Sakamoto /**
326d67c46b9STakashi Sakamoto  * amdtp_stream_get_max_payload - get the stream's packet size
327d67c46b9STakashi Sakamoto  * @s: the AMDTP stream
328d67c46b9STakashi Sakamoto  *
329d67c46b9STakashi Sakamoto  * This function must not be called before the stream has been configured
330d67c46b9STakashi Sakamoto  * with amdtp_stream_set_parameters().
331d67c46b9STakashi Sakamoto  */
amdtp_stream_get_max_payload(struct amdtp_stream * s)332d67c46b9STakashi Sakamoto unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
333d67c46b9STakashi Sakamoto {
334c75f3678STakashi Sakamoto 	unsigned int cip_header_size;
335d67c46b9STakashi Sakamoto 
3363b196c39STakashi Sakamoto 	if (!(s->flags & CIP_NO_HEADER))
33767d92ee7STakashi Sakamoto 		cip_header_size = CIP_HEADER_SIZE;
338c75f3678STakashi Sakamoto 	else
339c75f3678STakashi Sakamoto 		cip_header_size = 0;
340d67c46b9STakashi Sakamoto 
341c75f3678STakashi Sakamoto 	return cip_header_size + amdtp_stream_get_max_ctx_payload_size(s);
342d67c46b9STakashi Sakamoto }
343d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_get_max_payload);
344d67c46b9STakashi Sakamoto 
345d67c46b9STakashi Sakamoto /**
346d67c46b9STakashi Sakamoto  * amdtp_stream_pcm_prepare - prepare PCM device for running
347d67c46b9STakashi Sakamoto  * @s: the AMDTP stream
348d67c46b9STakashi Sakamoto  *
349d67c46b9STakashi Sakamoto  * This function should be called from the PCM device's .prepare callback.
350d67c46b9STakashi Sakamoto  */
amdtp_stream_pcm_prepare(struct amdtp_stream * s)351d67c46b9STakashi Sakamoto void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
352d67c46b9STakashi Sakamoto {
3536ccf9984SEdmund Raile 	cancel_work_sync(&s->period_work);
354d67c46b9STakashi Sakamoto 	s->pcm_buffer_pointer = 0;
355d67c46b9STakashi Sakamoto 	s->pcm_period_pointer = 0;
356d67c46b9STakashi Sakamoto }
357d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
358d67c46b9STakashi Sakamoto 
359af13842cSTakashi Sakamoto #define prev_packet_desc(s, desc) \
360af13842cSTakashi Sakamoto 	list_prev_entry_circular(desc, &s->packet_descs_list, link)
361af13842cSTakashi Sakamoto 
pool_blocking_data_blocks(struct amdtp_stream * s,struct seq_desc * descs,unsigned int size,unsigned int pos,unsigned int count)362c9f3ac2aSTakashi Sakamoto static void pool_blocking_data_blocks(struct amdtp_stream *s, struct seq_desc *descs,
363119c446aSTakashi Sakamoto 				      unsigned int size, unsigned int pos, unsigned int count)
364d67c46b9STakashi Sakamoto {
365c9f3ac2aSTakashi Sakamoto 	const unsigned int syt_interval = s->syt_interval;
366c9f3ac2aSTakashi Sakamoto 	int i;
367d67c46b9STakashi Sakamoto 
368c9f3ac2aSTakashi Sakamoto 	for (i = 0; i < count; ++i) {
369119c446aSTakashi Sakamoto 		struct seq_desc *desc = descs + pos;
370c9f3ac2aSTakashi Sakamoto 
371c9f3ac2aSTakashi Sakamoto 		if (desc->syt_offset != CIP_SYT_NO_INFO)
372c9f3ac2aSTakashi Sakamoto 			desc->data_blocks = syt_interval;
373d67c46b9STakashi Sakamoto 		else
374c9f3ac2aSTakashi Sakamoto 			desc->data_blocks = 0;
375c9f3ac2aSTakashi Sakamoto 
376119c446aSTakashi Sakamoto 		pos = (pos + 1) % size;
377c9f3ac2aSTakashi Sakamoto 	}
378c9f3ac2aSTakashi Sakamoto }
379c9f3ac2aSTakashi Sakamoto 
pool_ideal_nonblocking_data_blocks(struct amdtp_stream * s,struct seq_desc * descs,unsigned int size,unsigned int pos,unsigned int count)380c9f3ac2aSTakashi Sakamoto static void pool_ideal_nonblocking_data_blocks(struct amdtp_stream *s, struct seq_desc *descs,
381119c446aSTakashi Sakamoto 					       unsigned int size, unsigned int pos,
382c9f3ac2aSTakashi Sakamoto 					       unsigned int count)
383c9f3ac2aSTakashi Sakamoto {
384c9f3ac2aSTakashi Sakamoto 	const enum cip_sfc sfc = s->sfc;
385c9f3ac2aSTakashi Sakamoto 	unsigned int state = s->ctx_data.rx.data_block_state;
386c9f3ac2aSTakashi Sakamoto 	int i;
387c9f3ac2aSTakashi Sakamoto 
388c9f3ac2aSTakashi Sakamoto 	for (i = 0; i < count; ++i) {
389119c446aSTakashi Sakamoto 		struct seq_desc *desc = descs + pos;
390c9f3ac2aSTakashi Sakamoto 
391274fc355STakashi Sakamoto 		if (!cip_sfc_is_base_44100(sfc)) {
392d3d10a4aSTakashi Sakamoto 			// Sample_rate / 8000 is an integer, and precomputed.
393c9f3ac2aSTakashi Sakamoto 			desc->data_blocks = state;
394d67c46b9STakashi Sakamoto 		} else {
395c9f3ac2aSTakashi Sakamoto 			unsigned int phase = state;
396d67c46b9STakashi Sakamoto 
397d67c46b9STakashi Sakamoto 		/*
398d67c46b9STakashi Sakamoto 		 * This calculates the number of data blocks per packet so that
399d67c46b9STakashi Sakamoto 		 * 1) the overall rate is correct and exactly synchronized to
400d67c46b9STakashi Sakamoto 		 *    the bus clock, and
401d67c46b9STakashi Sakamoto 		 * 2) packets with a rounded-up number of blocks occur as early
402d67c46b9STakashi Sakamoto 		 *    as possible in the sequence (to prevent underruns of the
403d67c46b9STakashi Sakamoto 		 *    device's buffer).
404d67c46b9STakashi Sakamoto 		 */
405274fc355STakashi Sakamoto 			if (sfc == CIP_SFC_44100)
406d67c46b9STakashi Sakamoto 				/* 6 6 5 6 5 6 5 ... */
407c9f3ac2aSTakashi Sakamoto 				desc->data_blocks = 5 + ((phase & 1) ^ (phase == 0 || phase >= 40));
408d67c46b9STakashi Sakamoto 			else
409d67c46b9STakashi Sakamoto 				/* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
410c9f3ac2aSTakashi Sakamoto 				desc->data_blocks = 11 * (sfc >> 1) + (phase == 0);
411274fc355STakashi Sakamoto 			if (++phase >= (80 >> (sfc >> 1)))
412d67c46b9STakashi Sakamoto 				phase = 0;
413c9f3ac2aSTakashi Sakamoto 			state = phase;
414d67c46b9STakashi Sakamoto 		}
415d67c46b9STakashi Sakamoto 
416119c446aSTakashi Sakamoto 		pos = (pos + 1) % size;
417c9f3ac2aSTakashi Sakamoto 	}
418c9f3ac2aSTakashi Sakamoto 
419c9f3ac2aSTakashi Sakamoto 	s->ctx_data.rx.data_block_state = state;
420d67c46b9STakashi Sakamoto }
421d67c46b9STakashi Sakamoto 
calculate_syt_offset(unsigned int * last_syt_offset,unsigned int * syt_offset_state,enum cip_sfc sfc)422816d8482STakashi Sakamoto static unsigned int calculate_syt_offset(unsigned int *last_syt_offset,
423816d8482STakashi Sakamoto 			unsigned int *syt_offset_state, enum cip_sfc sfc)
424d67c46b9STakashi Sakamoto {
425816d8482STakashi Sakamoto 	unsigned int syt_offset;
426d67c46b9STakashi Sakamoto 
427816d8482STakashi Sakamoto 	if (*last_syt_offset < TICKS_PER_CYCLE) {
428816d8482STakashi Sakamoto 		if (!cip_sfc_is_base_44100(sfc))
429816d8482STakashi Sakamoto 			syt_offset = *last_syt_offset + *syt_offset_state;
430d67c46b9STakashi Sakamoto 		else {
431d67c46b9STakashi Sakamoto 		/*
432d67c46b9STakashi Sakamoto 		 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
433d67c46b9STakashi Sakamoto 		 *   n * SYT_INTERVAL * 24576000 / sample_rate
434d67c46b9STakashi Sakamoto 		 * Modulo TICKS_PER_CYCLE, the difference between successive
435d67c46b9STakashi Sakamoto 		 * elements is about 1386.23.  Rounding the results of this
436d67c46b9STakashi Sakamoto 		 * formula to the SYT precision results in a sequence of
437d67c46b9STakashi Sakamoto 		 * differences that begins with:
438d67c46b9STakashi Sakamoto 		 *   1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
439d67c46b9STakashi Sakamoto 		 * This code generates _exactly_ the same sequence.
440d67c46b9STakashi Sakamoto 		 */
441816d8482STakashi Sakamoto 			unsigned int phase = *syt_offset_state;
442816d8482STakashi Sakamoto 			unsigned int index = phase % 13;
443816d8482STakashi Sakamoto 
444816d8482STakashi Sakamoto 			syt_offset = *last_syt_offset;
445d67c46b9STakashi Sakamoto 			syt_offset += 1386 + ((index && !(index & 3)) ||
446d67c46b9STakashi Sakamoto 					      phase == 146);
447d67c46b9STakashi Sakamoto 			if (++phase >= 147)
448d67c46b9STakashi Sakamoto 				phase = 0;
449816d8482STakashi Sakamoto 			*syt_offset_state = phase;
450d67c46b9STakashi Sakamoto 		}
451d67c46b9STakashi Sakamoto 	} else
452816d8482STakashi Sakamoto 		syt_offset = *last_syt_offset - TICKS_PER_CYCLE;
453816d8482STakashi Sakamoto 	*last_syt_offset = syt_offset;
454d67c46b9STakashi Sakamoto 
45583cfb5c5STakashi Sakamoto 	if (syt_offset >= TICKS_PER_CYCLE)
45683cfb5c5STakashi Sakamoto 		syt_offset = CIP_SYT_NO_INFO;
457d67c46b9STakashi Sakamoto 
45883cfb5c5STakashi Sakamoto 	return syt_offset;
459d67c46b9STakashi Sakamoto }
460d67c46b9STakashi Sakamoto 
pool_ideal_syt_offsets(struct amdtp_stream * s,struct seq_desc * descs,unsigned int size,unsigned int pos,unsigned int count)461c79b7158STakashi Sakamoto static void pool_ideal_syt_offsets(struct amdtp_stream *s, struct seq_desc *descs,
462119c446aSTakashi Sakamoto 				   unsigned int size, unsigned int pos, unsigned int count)
463c79b7158STakashi Sakamoto {
464c79b7158STakashi Sakamoto 	const enum cip_sfc sfc = s->sfc;
465c79b7158STakashi Sakamoto 	unsigned int last = s->ctx_data.rx.last_syt_offset;
466c79b7158STakashi Sakamoto 	unsigned int state = s->ctx_data.rx.syt_offset_state;
467c79b7158STakashi Sakamoto 	int i;
468c79b7158STakashi Sakamoto 
469c79b7158STakashi Sakamoto 	for (i = 0; i < count; ++i) {
470119c446aSTakashi Sakamoto 		struct seq_desc *desc = descs + pos;
471c79b7158STakashi Sakamoto 
472c79b7158STakashi Sakamoto 		desc->syt_offset = calculate_syt_offset(&last, &state, sfc);
473c79b7158STakashi Sakamoto 
474119c446aSTakashi Sakamoto 		pos = (pos + 1) % size;
475c79b7158STakashi Sakamoto 	}
476c79b7158STakashi Sakamoto 
477c79b7158STakashi Sakamoto 	s->ctx_data.rx.last_syt_offset = last;
478c79b7158STakashi Sakamoto 	s->ctx_data.rx.syt_offset_state = state;
479c79b7158STakashi Sakamoto }
480c79b7158STakashi Sakamoto 
compute_syt_offset(unsigned int syt,unsigned int cycle,unsigned int transfer_delay)481f9e5ecdfSTakashi Sakamoto static unsigned int compute_syt_offset(unsigned int syt, unsigned int cycle,
482f9e5ecdfSTakashi Sakamoto 				       unsigned int transfer_delay)
483f9e5ecdfSTakashi Sakamoto {
484f9e5ecdfSTakashi Sakamoto 	unsigned int cycle_lo = (cycle % CYCLES_PER_SECOND) & 0x0f;
485f9e5ecdfSTakashi Sakamoto 	unsigned int syt_cycle_lo = (syt & 0xf000) >> 12;
486f9e5ecdfSTakashi Sakamoto 	unsigned int syt_offset;
487f9e5ecdfSTakashi Sakamoto 
488f9e5ecdfSTakashi Sakamoto 	// Round up.
489f9e5ecdfSTakashi Sakamoto 	if (syt_cycle_lo < cycle_lo)
490f9e5ecdfSTakashi Sakamoto 		syt_cycle_lo += CIP_SYT_CYCLE_MODULUS;
491f9e5ecdfSTakashi Sakamoto 	syt_cycle_lo -= cycle_lo;
492f9e5ecdfSTakashi Sakamoto 
493f9e5ecdfSTakashi Sakamoto 	// Subtract transfer delay so that the synchronization offset is not so large
494f9e5ecdfSTakashi Sakamoto 	// at transmission.
495f9e5ecdfSTakashi Sakamoto 	syt_offset = syt_cycle_lo * TICKS_PER_CYCLE + (syt & 0x0fff);
496f9e5ecdfSTakashi Sakamoto 	if (syt_offset < transfer_delay)
497f9e5ecdfSTakashi Sakamoto 		syt_offset += CIP_SYT_CYCLE_MODULUS * TICKS_PER_CYCLE;
498f9e5ecdfSTakashi Sakamoto 
499f9e5ecdfSTakashi Sakamoto 	return syt_offset - transfer_delay;
500f9e5ecdfSTakashi Sakamoto }
501f9e5ecdfSTakashi Sakamoto 
50239c2649cSTakashi Sakamoto // Both of the producer and consumer of the queue runs in the same clock of IEEE 1394 bus.
50339c2649cSTakashi Sakamoto // Additionally, the sequence of tx packets is severely checked against any discontinuity
50439c2649cSTakashi Sakamoto // before filling entries in the queue. The calculation is safe even if it looks fragile by
50539c2649cSTakashi Sakamoto // overrun.
calculate_cached_cycle_count(struct amdtp_stream * s,unsigned int head)50639c2649cSTakashi Sakamoto static unsigned int calculate_cached_cycle_count(struct amdtp_stream *s, unsigned int head)
50739c2649cSTakashi Sakamoto {
50839c2649cSTakashi Sakamoto 	const unsigned int cache_size = s->ctx_data.tx.cache.size;
509cccddec4STakashi Sakamoto 	unsigned int cycles = s->ctx_data.tx.cache.pos;
51039c2649cSTakashi Sakamoto 
51139c2649cSTakashi Sakamoto 	if (cycles < head)
51239c2649cSTakashi Sakamoto 		cycles += cache_size;
51339c2649cSTakashi Sakamoto 	cycles -= head;
51439c2649cSTakashi Sakamoto 
51539c2649cSTakashi Sakamoto 	return cycles;
51639c2649cSTakashi Sakamoto }
51739c2649cSTakashi Sakamoto 
cache_seq(struct amdtp_stream * s,const struct pkt_desc * src,unsigned int desc_count)518cec371ffSTakashi Sakamoto static void cache_seq(struct amdtp_stream *s, const struct pkt_desc *src, unsigned int desc_count)
519f9e5ecdfSTakashi Sakamoto {
520f9e5ecdfSTakashi Sakamoto 	const unsigned int transfer_delay = s->transfer_delay;
521f9e5ecdfSTakashi Sakamoto 	const unsigned int cache_size = s->ctx_data.tx.cache.size;
522f9e5ecdfSTakashi Sakamoto 	struct seq_desc *cache = s->ctx_data.tx.cache.descs;
523cccddec4STakashi Sakamoto 	unsigned int cache_pos = s->ctx_data.tx.cache.pos;
524f9e5ecdfSTakashi Sakamoto 	bool aware_syt = !(s->flags & CIP_UNAWARE_SYT);
525f9e5ecdfSTakashi Sakamoto 	int i;
526f9e5ecdfSTakashi Sakamoto 
527f9e5ecdfSTakashi Sakamoto 	for (i = 0; i < desc_count; ++i) {
528cccddec4STakashi Sakamoto 		struct seq_desc *dst = cache + cache_pos;
529f9e5ecdfSTakashi Sakamoto 
530f9e5ecdfSTakashi Sakamoto 		if (aware_syt && src->syt != CIP_SYT_NO_INFO)
531f9e5ecdfSTakashi Sakamoto 			dst->syt_offset = compute_syt_offset(src->syt, src->cycle, transfer_delay);
532f9e5ecdfSTakashi Sakamoto 		else
533f9e5ecdfSTakashi Sakamoto 			dst->syt_offset = CIP_SYT_NO_INFO;
534f9e5ecdfSTakashi Sakamoto 		dst->data_blocks = src->data_blocks;
535f9e5ecdfSTakashi Sakamoto 
536cccddec4STakashi Sakamoto 		cache_pos = (cache_pos + 1) % cache_size;
537cec371ffSTakashi Sakamoto 		src = amdtp_stream_next_packet_desc(s, src);
538f9e5ecdfSTakashi Sakamoto 	}
539f9e5ecdfSTakashi Sakamoto 
540cccddec4STakashi Sakamoto 	s->ctx_data.tx.cache.pos = cache_pos;
541f9e5ecdfSTakashi Sakamoto }
542f9e5ecdfSTakashi Sakamoto 
pool_ideal_seq_descs(struct amdtp_stream * s,struct seq_desc * descs,unsigned int size,unsigned int pos,unsigned int count)543119c446aSTakashi Sakamoto static void pool_ideal_seq_descs(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
544119c446aSTakashi Sakamoto 				 unsigned int pos, unsigned int count)
5456f24bb8aSTakashi Sakamoto {
546119c446aSTakashi Sakamoto 	pool_ideal_syt_offsets(s, descs, size, pos, count);
547c79b7158STakashi Sakamoto 
548c9f3ac2aSTakashi Sakamoto 	if (s->flags & CIP_BLOCKING)
549119c446aSTakashi Sakamoto 		pool_blocking_data_blocks(s, descs, size, pos, count);
550c9f3ac2aSTakashi Sakamoto 	else
551119c446aSTakashi Sakamoto 		pool_ideal_nonblocking_data_blocks(s, descs, size, pos, count);
5526f24bb8aSTakashi Sakamoto }
5536f24bb8aSTakashi Sakamoto 
pool_replayed_seq(struct amdtp_stream * s,struct seq_desc * descs,unsigned int size,unsigned int pos,unsigned int count)554119c446aSTakashi Sakamoto static void pool_replayed_seq(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
555119c446aSTakashi Sakamoto 			      unsigned int pos, unsigned int count)
55639c2649cSTakashi Sakamoto {
55739c2649cSTakashi Sakamoto 	struct amdtp_stream *target = s->ctx_data.rx.replay_target;
55839c2649cSTakashi Sakamoto 	const struct seq_desc *cache = target->ctx_data.tx.cache.descs;
55939c2649cSTakashi Sakamoto 	const unsigned int cache_size = target->ctx_data.tx.cache.size;
560c38d8cffSTakashi Sakamoto 	unsigned int cache_pos = s->ctx_data.rx.cache_pos;
56139c2649cSTakashi Sakamoto 	int i;
56239c2649cSTakashi Sakamoto 
56339c2649cSTakashi Sakamoto 	for (i = 0; i < count; ++i) {
564c38d8cffSTakashi Sakamoto 		descs[pos] = cache[cache_pos];
565c38d8cffSTakashi Sakamoto 		cache_pos = (cache_pos + 1) % cache_size;
566119c446aSTakashi Sakamoto 		pos = (pos + 1) % size;
56739c2649cSTakashi Sakamoto 	}
56839c2649cSTakashi Sakamoto 
569c38d8cffSTakashi Sakamoto 	s->ctx_data.rx.cache_pos = cache_pos;
57039c2649cSTakashi Sakamoto }
57139c2649cSTakashi Sakamoto 
pool_seq_descs(struct amdtp_stream * s,struct seq_desc * descs,unsigned int size,unsigned int pos,unsigned int count)572f2bdee85STakashi Sakamoto static void pool_seq_descs(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
573f2bdee85STakashi Sakamoto 			   unsigned int pos, unsigned int count)
57439c2649cSTakashi Sakamoto {
57539c2649cSTakashi Sakamoto 	struct amdtp_domain *d = s->domain;
576119c446aSTakashi Sakamoto 	void (*pool_seq_descs)(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
577119c446aSTakashi Sakamoto 			       unsigned int pos, unsigned int count);
57839c2649cSTakashi Sakamoto 
5792f21a177STakashi Sakamoto 	if (!d->replay.enable || !s->ctx_data.rx.replay_target) {
580119c446aSTakashi Sakamoto 		pool_seq_descs = pool_ideal_seq_descs;
5812f21a177STakashi Sakamoto 	} else {
5822f21a177STakashi Sakamoto 		if (!d->replay.on_the_fly) {
583119c446aSTakashi Sakamoto 			pool_seq_descs = pool_replayed_seq;
5842f21a177STakashi Sakamoto 		} else {
5852f21a177STakashi Sakamoto 			struct amdtp_stream *tx = s->ctx_data.rx.replay_target;
5862f21a177STakashi Sakamoto 			const unsigned int cache_size = tx->ctx_data.tx.cache.size;
587c38d8cffSTakashi Sakamoto 			const unsigned int cache_pos = s->ctx_data.rx.cache_pos;
588c38d8cffSTakashi Sakamoto 			unsigned int cached_cycles = calculate_cached_cycle_count(tx, cache_pos);
5892f21a177STakashi Sakamoto 
5902f21a177STakashi Sakamoto 			if (cached_cycles > count && cached_cycles > cache_size / 2)
591119c446aSTakashi Sakamoto 				pool_seq_descs = pool_replayed_seq;
5922f21a177STakashi Sakamoto 			else
593119c446aSTakashi Sakamoto 				pool_seq_descs = pool_ideal_seq_descs;
5942f21a177STakashi Sakamoto 		}
5952f21a177STakashi Sakamoto 	}
596119c446aSTakashi Sakamoto 
597119c446aSTakashi Sakamoto 	pool_seq_descs(s, descs, size, pos, count);
59839c2649cSTakashi Sakamoto }
59939c2649cSTakashi Sakamoto 
update_pcm_pointers(struct amdtp_stream * s,struct snd_pcm_substream * pcm,unsigned int frames)600d67c46b9STakashi Sakamoto static void update_pcm_pointers(struct amdtp_stream *s,
601d67c46b9STakashi Sakamoto 				struct snd_pcm_substream *pcm,
602d67c46b9STakashi Sakamoto 				unsigned int frames)
603d67c46b9STakashi Sakamoto {
604d67c46b9STakashi Sakamoto 	unsigned int ptr;
605d67c46b9STakashi Sakamoto 
606d67c46b9STakashi Sakamoto 	ptr = s->pcm_buffer_pointer + frames;
607d67c46b9STakashi Sakamoto 	if (ptr >= pcm->runtime->buffer_size)
608d67c46b9STakashi Sakamoto 		ptr -= pcm->runtime->buffer_size;
6096aa7de05SMark Rutland 	WRITE_ONCE(s->pcm_buffer_pointer, ptr);
610d67c46b9STakashi Sakamoto 
611d67c46b9STakashi Sakamoto 	s->pcm_period_pointer += frames;
612d67c46b9STakashi Sakamoto 	if (s->pcm_period_pointer >= pcm->runtime->period_size) {
613d67c46b9STakashi Sakamoto 		s->pcm_period_pointer -= pcm->runtime->period_size;
614d360870aSTakashi Sakamoto 
615d360870aSTakashi Sakamoto 		// The program in user process should periodically check the status of intermediate
616d360870aSTakashi Sakamoto 		// buffer associated to PCM substream to process PCM frames in the buffer, instead
617d360870aSTakashi Sakamoto 		// of receiving notification of period elapsed by poll wait.
618*3dab73abSEdmund Raile 		if (!pcm->runtime->no_period_wakeup)
619*3dab73abSEdmund Raile 			queue_work(system_highpri_wq, &s->period_work);
620d67c46b9STakashi Sakamoto 	}
621d67c46b9STakashi Sakamoto }
622d67c46b9STakashi Sakamoto 
pcm_period_work(struct work_struct * work)6236ccf9984SEdmund Raile static void pcm_period_work(struct work_struct *work)
6246ccf9984SEdmund Raile {
6256ccf9984SEdmund Raile 	struct amdtp_stream *s = container_of(work, struct amdtp_stream,
6266ccf9984SEdmund Raile 					      period_work);
6276ccf9984SEdmund Raile 	struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
6286ccf9984SEdmund Raile 
6296ccf9984SEdmund Raile 	if (pcm)
630d67c46b9STakashi Sakamoto 		snd_pcm_period_elapsed(pcm);
631d67c46b9STakashi Sakamoto }
632d67c46b9STakashi Sakamoto 
queue_packet(struct amdtp_stream * s,struct fw_iso_packet * params,bool sched_irq)633e229853dSTakashi Sakamoto static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params,
634e229853dSTakashi Sakamoto 			bool sched_irq)
635d67c46b9STakashi Sakamoto {
6366007bf54STakashi Sakamoto 	int err;
637d67c46b9STakashi Sakamoto 
638e229853dSTakashi Sakamoto 	params->interrupt = sched_irq;
6396007bf54STakashi Sakamoto 	params->tag = s->tag;
6406007bf54STakashi Sakamoto 	params->sy = 0;
641d67c46b9STakashi Sakamoto 
6426007bf54STakashi Sakamoto 	err = fw_iso_context_queue(s->context, params, &s->buffer.iso_buffer,
643d67c46b9STakashi Sakamoto 				   s->buffer.packets[s->packet_index].offset);
644d67c46b9STakashi Sakamoto 	if (err < 0) {
645d67c46b9STakashi Sakamoto 		dev_err(&s->unit->device, "queueing error: %d\n", err);
646d67c46b9STakashi Sakamoto 		goto end;
647d67c46b9STakashi Sakamoto 	}
648d67c46b9STakashi Sakamoto 
649a0e02331STakashi Sakamoto 	if (++s->packet_index >= s->queue_size)
650d67c46b9STakashi Sakamoto 		s->packet_index = 0;
651d67c46b9STakashi Sakamoto end:
652d67c46b9STakashi Sakamoto 	return err;
653d67c46b9STakashi Sakamoto }
654d67c46b9STakashi Sakamoto 
queue_out_packet(struct amdtp_stream * s,struct fw_iso_packet * params,bool sched_irq)655d67c46b9STakashi Sakamoto static inline int queue_out_packet(struct amdtp_stream *s,
656e229853dSTakashi Sakamoto 				   struct fw_iso_packet *params, bool sched_irq)
657d67c46b9STakashi Sakamoto {
658b18f0cfaSTakashi Sakamoto 	params->skip =
659b18f0cfaSTakashi Sakamoto 		!!(params->header_length == 0 && params->payload_length == 0);
660e229853dSTakashi Sakamoto 	return queue_packet(s, params, sched_irq);
661d67c46b9STakashi Sakamoto }
662d67c46b9STakashi Sakamoto 
queue_in_packet(struct amdtp_stream * s,struct fw_iso_packet * params)6636007bf54STakashi Sakamoto static inline int queue_in_packet(struct amdtp_stream *s,
66460dd4929STakashi Sakamoto 				  struct fw_iso_packet *params)
665d67c46b9STakashi Sakamoto {
6666007bf54STakashi Sakamoto 	// Queue one packet for IR context.
6676007bf54STakashi Sakamoto 	params->header_length = s->ctx_data.tx.ctx_header_size;
6686007bf54STakashi Sakamoto 	params->payload_length = s->ctx_data.tx.max_ctx_payload_length;
6696007bf54STakashi Sakamoto 	params->skip = false;
67060dd4929STakashi Sakamoto 	return queue_packet(s, params, false);
671d67c46b9STakashi Sakamoto }
672d67c46b9STakashi Sakamoto 
generate_cip_header(struct amdtp_stream * s,__be32 cip_header[2],unsigned int data_block_counter,unsigned int syt)673252219c7STakashi Sakamoto static void generate_cip_header(struct amdtp_stream *s, __be32 cip_header[2],
674860d798cSTakashi Sakamoto 			unsigned int data_block_counter, unsigned int syt)
675252219c7STakashi Sakamoto {
676252219c7STakashi Sakamoto 	cip_header[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) |
677252219c7STakashi Sakamoto 				(s->data_block_quadlets << CIP_DBS_SHIFT) |
678252219c7STakashi Sakamoto 				((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
679860d798cSTakashi Sakamoto 				data_block_counter);
680252219c7STakashi Sakamoto 	cip_header[1] = cpu_to_be32(CIP_EOH |
681252219c7STakashi Sakamoto 			((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
682252219c7STakashi Sakamoto 			((s->ctx_data.rx.fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
683252219c7STakashi Sakamoto 			(syt & CIP_SYT_MASK));
684252219c7STakashi Sakamoto }
685252219c7STakashi Sakamoto 
build_it_pkt_header(struct amdtp_stream * s,unsigned int cycle,struct fw_iso_packet * params,unsigned int header_length,unsigned int data_blocks,unsigned int data_block_counter,unsigned int syt,unsigned int index,u32 curr_cycle_time)6866bc1a269STakashi Sakamoto static void build_it_pkt_header(struct amdtp_stream *s, unsigned int cycle,
687233dbbc7STakashi Sakamoto 				struct fw_iso_packet *params, unsigned int header_length,
688860d798cSTakashi Sakamoto 				unsigned int data_blocks,
689860d798cSTakashi Sakamoto 				unsigned int data_block_counter,
690fef4e61bSTakashi Sakamoto 				unsigned int syt, unsigned int index, u32 curr_cycle_time)
691d67c46b9STakashi Sakamoto {
6920ebf3cebSTakashi Sakamoto 	unsigned int payload_length;
69316be4589STakashi Sakamoto 	__be32 *cip_header;
694d67c46b9STakashi Sakamoto 
6950ebf3cebSTakashi Sakamoto 	payload_length = data_blocks * sizeof(__be32) * s->data_block_quadlets;
6960ebf3cebSTakashi Sakamoto 	params->payload_length = payload_length;
6970ebf3cebSTakashi Sakamoto 
698233dbbc7STakashi Sakamoto 	if (header_length > 0) {
6996bc1a269STakashi Sakamoto 		cip_header = (__be32 *)params->header;
700860d798cSTakashi Sakamoto 		generate_cip_header(s, cip_header, data_block_counter, syt);
701233dbbc7STakashi Sakamoto 		params->header_length = header_length;
702b18f0cfaSTakashi Sakamoto 	} else {
703b18f0cfaSTakashi Sakamoto 		cip_header = NULL;
704b18f0cfaSTakashi Sakamoto 	}
705d67c46b9STakashi Sakamoto 
706233dbbc7STakashi Sakamoto 	trace_amdtp_packet(s, cycle, cip_header, payload_length + header_length, data_blocks,
707fef4e61bSTakashi Sakamoto 			   data_block_counter, s->packet_index, index, curr_cycle_time);
7083b196c39STakashi Sakamoto }
7093b196c39STakashi Sakamoto 
check_cip_header(struct amdtp_stream * s,const __be32 * buf,unsigned int payload_length,unsigned int * data_blocks,unsigned int * data_block_counter,unsigned int * syt)710e335425bSTakashi Sakamoto static int check_cip_header(struct amdtp_stream *s, const __be32 *buf,
711e335425bSTakashi Sakamoto 			    unsigned int payload_length,
712a35463d1STakashi Sakamoto 			    unsigned int *data_blocks,
713a35463d1STakashi Sakamoto 			    unsigned int *data_block_counter, unsigned int *syt)
714d67c46b9STakashi Sakamoto {
715d67c46b9STakashi Sakamoto 	u32 cip_header[2];
716e335425bSTakashi Sakamoto 	unsigned int sph;
717e335425bSTakashi Sakamoto 	unsigned int fmt;
718e335425bSTakashi Sakamoto 	unsigned int fdf;
719a35463d1STakashi Sakamoto 	unsigned int dbc;
720d67c46b9STakashi Sakamoto 	bool lost;
721d67c46b9STakashi Sakamoto 
722e335425bSTakashi Sakamoto 	cip_header[0] = be32_to_cpu(buf[0]);
723e335425bSTakashi Sakamoto 	cip_header[1] = be32_to_cpu(buf[1]);
724d67c46b9STakashi Sakamoto 
725d67c46b9STakashi Sakamoto 	/*
726d67c46b9STakashi Sakamoto 	 * This module supports 'Two-quadlet CIP header with SYT field'.
727d67c46b9STakashi Sakamoto 	 * For convenience, also check FMT field is AM824 or not.
728d67c46b9STakashi Sakamoto 	 */
7292128f78fSTakashi Sakamoto 	if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
7302128f78fSTakashi Sakamoto 	     ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
7312128f78fSTakashi Sakamoto 	    (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
732d67c46b9STakashi Sakamoto 		dev_info_ratelimited(&s->unit->device,
733d67c46b9STakashi Sakamoto 				"Invalid CIP header for AMDTP: %08X:%08X\n",
734d67c46b9STakashi Sakamoto 				cip_header[0], cip_header[1]);
735e335425bSTakashi Sakamoto 		return -EAGAIN;
736d67c46b9STakashi Sakamoto 	}
737d67c46b9STakashi Sakamoto 
738d67c46b9STakashi Sakamoto 	/* Check valid protocol or not. */
7399863874fSTakashi Sakamoto 	sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
740d67c46b9STakashi Sakamoto 	fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
7419863874fSTakashi Sakamoto 	if (sph != s->sph || fmt != s->fmt) {
7422a7e1713STakashi Sakamoto 		dev_info_ratelimited(&s->unit->device,
743d67c46b9STakashi Sakamoto 				     "Detect unexpected protocol: %08x %08x\n",
744d67c46b9STakashi Sakamoto 				     cip_header[0], cip_header[1]);
745e335425bSTakashi Sakamoto 		return -EAGAIN;
746d67c46b9STakashi Sakamoto 	}
747d67c46b9STakashi Sakamoto 
748d67c46b9STakashi Sakamoto 	/* Calculate data blocks */
749d67c46b9STakashi Sakamoto 	fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
7504fd18787STakashi Sakamoto 	if (payload_length == 0 || (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
751e335425bSTakashi Sakamoto 		*data_blocks = 0;
752d67c46b9STakashi Sakamoto 	} else {
753e335425bSTakashi Sakamoto 		unsigned int data_block_quadlets =
754d67c46b9STakashi Sakamoto 				(cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
755d67c46b9STakashi Sakamoto 		/* avoid division by zero */
756d67c46b9STakashi Sakamoto 		if (data_block_quadlets == 0) {
757d67c46b9STakashi Sakamoto 			dev_err(&s->unit->device,
758d67c46b9STakashi Sakamoto 				"Detect invalid value in dbs field: %08X\n",
759d67c46b9STakashi Sakamoto 				cip_header[0]);
760d67c46b9STakashi Sakamoto 			return -EPROTO;
761d67c46b9STakashi Sakamoto 		}
762d67c46b9STakashi Sakamoto 		if (s->flags & CIP_WRONG_DBS)
763d67c46b9STakashi Sakamoto 			data_block_quadlets = s->data_block_quadlets;
764d67c46b9STakashi Sakamoto 
7654fd18787STakashi Sakamoto 		*data_blocks = payload_length / sizeof(__be32) / data_block_quadlets;
766d67c46b9STakashi Sakamoto 	}
767d67c46b9STakashi Sakamoto 
768d67c46b9STakashi Sakamoto 	/* Check data block counter continuity */
769a35463d1STakashi Sakamoto 	dbc = cip_header[0] & CIP_DBC_MASK;
770e335425bSTakashi Sakamoto 	if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
771a35463d1STakashi Sakamoto 	    *data_block_counter != UINT_MAX)
772a35463d1STakashi Sakamoto 		dbc = *data_block_counter;
773d67c46b9STakashi Sakamoto 
774a35463d1STakashi Sakamoto 	if ((dbc == 0x00 && (s->flags & CIP_SKIP_DBC_ZERO_CHECK)) ||
775a35463d1STakashi Sakamoto 	    *data_block_counter == UINT_MAX) {
776d67c46b9STakashi Sakamoto 		lost = false;
777d67c46b9STakashi Sakamoto 	} else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
778a35463d1STakashi Sakamoto 		lost = dbc != *data_block_counter;
779d67c46b9STakashi Sakamoto 	} else {
780e335425bSTakashi Sakamoto 		unsigned int dbc_interval;
781e335425bSTakashi Sakamoto 
7824a486439STakashi Sakamoto 		if (!(s->flags & CIP_DBC_IS_PAYLOAD_QUADLETS)) {
783e335425bSTakashi Sakamoto 			if (*data_blocks > 0 && s->ctx_data.tx.dbc_interval > 0)
784d3d10a4aSTakashi Sakamoto 				dbc_interval = s->ctx_data.tx.dbc_interval;
785d67c46b9STakashi Sakamoto 			else
786e335425bSTakashi Sakamoto 				dbc_interval = *data_blocks;
7874a486439STakashi Sakamoto 		} else {
7884a486439STakashi Sakamoto 			dbc_interval = payload_length / sizeof(__be32);
7894a486439STakashi Sakamoto 		}
790d67c46b9STakashi Sakamoto 
791a35463d1STakashi Sakamoto 		lost = dbc != ((*data_block_counter + dbc_interval) & 0xff);
792d67c46b9STakashi Sakamoto 	}
793d67c46b9STakashi Sakamoto 
794d67c46b9STakashi Sakamoto 	if (lost) {
795d67c46b9STakashi Sakamoto 		dev_err(&s->unit->device,
796d67c46b9STakashi Sakamoto 			"Detect discontinuity of CIP: %02X %02X\n",
797a35463d1STakashi Sakamoto 			*data_block_counter, dbc);
798d67c46b9STakashi Sakamoto 		return -EIO;
799d67c46b9STakashi Sakamoto 	}
800d67c46b9STakashi Sakamoto 
801753e7179STakashi Sakamoto 	*data_block_counter = dbc;
802753e7179STakashi Sakamoto 
8038070d265STakashi Sakamoto 	if (!(s->flags & CIP_UNAWARE_SYT))
804e335425bSTakashi Sakamoto 		*syt = cip_header[1] & CIP_SYT_MASK;
805e335425bSTakashi Sakamoto 
806e335425bSTakashi Sakamoto 	return 0;
807e335425bSTakashi Sakamoto }
808e335425bSTakashi Sakamoto 
parse_ir_ctx_header(struct amdtp_stream * s,unsigned int cycle,const __be32 * ctx_header,unsigned int * data_blocks,unsigned int * data_block_counter,unsigned int * syt,unsigned int packet_index,unsigned int index,u32 curr_cycle_time)80998e3e43bSTakashi Sakamoto static int parse_ir_ctx_header(struct amdtp_stream *s, unsigned int cycle,
81098e3e43bSTakashi Sakamoto 			       const __be32 *ctx_header,
811a35463d1STakashi Sakamoto 			       unsigned int *data_blocks,
812a35463d1STakashi Sakamoto 			       unsigned int *data_block_counter,
813fef4e61bSTakashi Sakamoto 			       unsigned int *syt, unsigned int packet_index, unsigned int index,
814fef4e61bSTakashi Sakamoto 			       u32 curr_cycle_time)
815e335425bSTakashi Sakamoto {
816ebd2a647STakashi Sakamoto 	unsigned int payload_length;
817f11453c7STakashi Sakamoto 	const __be32 *cip_header;
818395f41e2STakashi Sakamoto 	unsigned int cip_header_size;
819e335425bSTakashi Sakamoto 
820ebd2a647STakashi Sakamoto 	payload_length = be32_to_cpu(ctx_header[0]) >> ISO_DATA_LENGTH_SHIFT;
821395f41e2STakashi Sakamoto 
822395f41e2STakashi Sakamoto 	if (!(s->flags & CIP_NO_HEADER))
82367d92ee7STakashi Sakamoto 		cip_header_size = CIP_HEADER_SIZE;
824395f41e2STakashi Sakamoto 	else
825395f41e2STakashi Sakamoto 		cip_header_size = 0;
826395f41e2STakashi Sakamoto 
827ebd2a647STakashi Sakamoto 	if (payload_length > cip_header_size + s->ctx_data.tx.max_ctx_payload_length) {
828e335425bSTakashi Sakamoto 		dev_err(&s->unit->device,
829e335425bSTakashi Sakamoto 			"Detect jumbo payload: %04x %04x\n",
830ebd2a647STakashi Sakamoto 			payload_length, cip_header_size + s->ctx_data.tx.max_ctx_payload_length);
831e335425bSTakashi Sakamoto 		return -EIO;
832e335425bSTakashi Sakamoto 	}
833e335425bSTakashi Sakamoto 
834395f41e2STakashi Sakamoto 	if (cip_header_size > 0) {
835ebd2a647STakashi Sakamoto 		if (payload_length >= cip_header_size) {
836344f0f82STakashi Sakamoto 			int err;
837344f0f82STakashi Sakamoto 
83867d92ee7STakashi Sakamoto 			cip_header = ctx_header + IR_CTX_HEADER_DEFAULT_QUADLETS;
8394fd18787STakashi Sakamoto 			err = check_cip_header(s, cip_header, payload_length - cip_header_size,
8404fd18787STakashi Sakamoto 					       data_blocks, data_block_counter, syt);
841b8b0e24cSTakashi Sakamoto 			if (err < 0)
842e335425bSTakashi Sakamoto 				return err;
843947b437eSTakashi Sakamoto 		} else {
844c09010eeSTakashi Sakamoto 			// Handle the cycle so that empty packet arrives.
845c09010eeSTakashi Sakamoto 			cip_header = NULL;
846c09010eeSTakashi Sakamoto 			*data_blocks = 0;
847c09010eeSTakashi Sakamoto 			*syt = 0;
848c09010eeSTakashi Sakamoto 		}
849c09010eeSTakashi Sakamoto 	} else {
850947b437eSTakashi Sakamoto 		cip_header = NULL;
851ebd2a647STakashi Sakamoto 		*data_blocks = payload_length / sizeof(__be32) / s->data_block_quadlets;
85298e3e43bSTakashi Sakamoto 		*syt = 0;
8537fbf9096STakashi Sakamoto 
854a35463d1STakashi Sakamoto 		if (*data_block_counter == UINT_MAX)
855a35463d1STakashi Sakamoto 			*data_block_counter = 0;
856947b437eSTakashi Sakamoto 	}
857e335425bSTakashi Sakamoto 
858ebd2a647STakashi Sakamoto 	trace_amdtp_packet(s, cycle, cip_header, payload_length, *data_blocks,
859fef4e61bSTakashi Sakamoto 			   *data_block_counter, packet_index, index, curr_cycle_time);
86064d0bf4dSTakashi Sakamoto 
861344f0f82STakashi Sakamoto 	return 0;
862d67c46b9STakashi Sakamoto }
863d67c46b9STakashi Sakamoto 
86426cd1e58STakashi Sakamoto // In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
86526cd1e58STakashi Sakamoto // the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
86626cd1e58STakashi Sakamoto // it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
compute_ohci_iso_ctx_cycle_count(u32 tstamp)867af13842cSTakashi Sakamoto static inline u32 compute_ohci_iso_ctx_cycle_count(u32 tstamp)
868af13842cSTakashi Sakamoto {
869af13842cSTakashi Sakamoto 	return (((tstamp >> 13) & 0x07) * CYCLES_PER_SECOND) + (tstamp & 0x1fff);
870af13842cSTakashi Sakamoto }
871af13842cSTakashi Sakamoto 
compute_ohci_cycle_count(__be32 ctx_header_tstamp)8723e106f4fSTakashi Sakamoto static inline u32 compute_ohci_cycle_count(__be32 ctx_header_tstamp)
87373fc7f08STakashi Sakamoto {
87426cd1e58STakashi Sakamoto 	u32 tstamp = be32_to_cpu(ctx_header_tstamp) & HEADER_TSTAMP_MASK;
875af13842cSTakashi Sakamoto 	return compute_ohci_iso_ctx_cycle_count(tstamp);
87673fc7f08STakashi Sakamoto }
87773fc7f08STakashi Sakamoto 
increment_ohci_cycle_count(u32 cycle,unsigned int addend)8783e106f4fSTakashi Sakamoto static inline u32 increment_ohci_cycle_count(u32 cycle, unsigned int addend)
87973fc7f08STakashi Sakamoto {
88073fc7f08STakashi Sakamoto 	cycle += addend;
8813e106f4fSTakashi Sakamoto 	if (cycle >= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND)
8823e106f4fSTakashi Sakamoto 		cycle -= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND;
88373fc7f08STakashi Sakamoto 	return cycle;
88473fc7f08STakashi Sakamoto }
88573fc7f08STakashi Sakamoto 
decrement_ohci_cycle_count(u32 minuend,u32 subtrahend)886af13842cSTakashi Sakamoto static inline u32 decrement_ohci_cycle_count(u32 minuend, u32 subtrahend)
887af13842cSTakashi Sakamoto {
888af13842cSTakashi Sakamoto 	if (minuend < subtrahend)
889af13842cSTakashi Sakamoto 		minuend += OHCI_SECOND_MODULUS * CYCLES_PER_SECOND;
890af13842cSTakashi Sakamoto 
891af13842cSTakashi Sakamoto 	return minuend - subtrahend;
892af13842cSTakashi Sakamoto }
893af13842cSTakashi Sakamoto 
compare_ohci_cycle_count(u32 lval,u32 rval)894705794c5STakashi Sakamoto static int compare_ohci_cycle_count(u32 lval, u32 rval)
895705794c5STakashi Sakamoto {
896705794c5STakashi Sakamoto 	if (lval == rval)
897705794c5STakashi Sakamoto 		return 0;
898705794c5STakashi Sakamoto 	else if (lval < rval && rval - lval < OHCI_SECOND_MODULUS * CYCLES_PER_SECOND / 2)
899705794c5STakashi Sakamoto 		return -1;
900705794c5STakashi Sakamoto 	else
901705794c5STakashi Sakamoto 		return 1;
902705794c5STakashi Sakamoto }
903705794c5STakashi Sakamoto 
90426cd1e58STakashi Sakamoto // Align to actual cycle count for the packet which is going to be scheduled.
905a0e02331STakashi Sakamoto // This module queued the same number of isochronous cycle as the size of queue
906a0e02331STakashi Sakamoto // to kip isochronous cycle, therefore it's OK to just increment the cycle by
907a0e02331STakashi Sakamoto // the size of queue for scheduled cycle.
compute_ohci_it_cycle(const __be32 ctx_header_tstamp,unsigned int queue_size)9083e106f4fSTakashi Sakamoto static inline u32 compute_ohci_it_cycle(const __be32 ctx_header_tstamp,
909a0e02331STakashi Sakamoto 					unsigned int queue_size)
91026cd1e58STakashi Sakamoto {
9113e106f4fSTakashi Sakamoto 	u32 cycle = compute_ohci_cycle_count(ctx_header_tstamp);
9123e106f4fSTakashi Sakamoto 	return increment_ohci_cycle_count(cycle, queue_size);
91326cd1e58STakashi Sakamoto }
91426cd1e58STakashi Sakamoto 
generate_tx_packet_descs(struct amdtp_stream * s,struct pkt_desc * desc,const __be32 * ctx_header,unsigned int packet_count,unsigned int * desc_count)915cec371ffSTakashi Sakamoto static int generate_tx_packet_descs(struct amdtp_stream *s, struct pkt_desc *desc,
916cccddec4STakashi Sakamoto 				    const __be32 *ctx_header, unsigned int packet_count,
91773246fc4STakashi Sakamoto 				    unsigned int *desc_count)
918753e7179STakashi Sakamoto {
919705794c5STakashi Sakamoto 	unsigned int next_cycle = s->next_cycle;
920753e7179STakashi Sakamoto 	unsigned int dbc = s->data_block_counter;
921814b4312STakashi Sakamoto 	unsigned int packet_index = s->packet_index;
922814b4312STakashi Sakamoto 	unsigned int queue_size = s->queue_size;
923d8dc8720STakashi Sakamoto 	u32 curr_cycle_time = 0;
924753e7179STakashi Sakamoto 	int i;
925753e7179STakashi Sakamoto 	int err;
926753e7179STakashi Sakamoto 
927fef4e61bSTakashi Sakamoto 	if (trace_amdtp_packet_enabled())
928fef4e61bSTakashi Sakamoto 		(void)fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &curr_cycle_time);
929fef4e61bSTakashi Sakamoto 
93073246fc4STakashi Sakamoto 	*desc_count = 0;
931cccddec4STakashi Sakamoto 	for (i = 0; i < packet_count; ++i) {
932753e7179STakashi Sakamoto 		unsigned int cycle;
933705794c5STakashi Sakamoto 		bool lost;
934753e7179STakashi Sakamoto 		unsigned int data_blocks;
935753e7179STakashi Sakamoto 		unsigned int syt;
936753e7179STakashi Sakamoto 
9373e106f4fSTakashi Sakamoto 		cycle = compute_ohci_cycle_count(ctx_header[1]);
938705794c5STakashi Sakamoto 		lost = (next_cycle != cycle);
939705794c5STakashi Sakamoto 		if (lost) {
940705794c5STakashi Sakamoto 			if (s->flags & CIP_NO_HEADER) {
941705794c5STakashi Sakamoto 				// Fireface skips transmission just for an isoc cycle corresponding
942705794c5STakashi Sakamoto 				// to empty packet.
94373246fc4STakashi Sakamoto 				unsigned int prev_cycle = next_cycle;
94473246fc4STakashi Sakamoto 
945705794c5STakashi Sakamoto 				next_cycle = increment_ohci_cycle_count(next_cycle, 1);
946705794c5STakashi Sakamoto 				lost = (next_cycle != cycle);
94773246fc4STakashi Sakamoto 				if (!lost) {
94873246fc4STakashi Sakamoto 					// Prepare a description for the skipped cycle for
94973246fc4STakashi Sakamoto 					// sequence replay.
95073246fc4STakashi Sakamoto 					desc->cycle = prev_cycle;
95173246fc4STakashi Sakamoto 					desc->syt = 0;
95273246fc4STakashi Sakamoto 					desc->data_blocks = 0;
95373246fc4STakashi Sakamoto 					desc->data_block_counter = dbc;
95473246fc4STakashi Sakamoto 					desc->ctx_payload = NULL;
955cec371ffSTakashi Sakamoto 					desc = amdtp_stream_next_packet_desc(s, desc);
95673246fc4STakashi Sakamoto 					++(*desc_count);
95773246fc4STakashi Sakamoto 				}
958705794c5STakashi Sakamoto 			} else if (s->flags & CIP_JUMBO_PAYLOAD) {
959705794c5STakashi Sakamoto 				// OXFW970 skips transmission for several isoc cycles during
96073246fc4STakashi Sakamoto 				// asynchronous transaction. The sequence replay is impossible due
96173246fc4STakashi Sakamoto 				// to the reason.
962705794c5STakashi Sakamoto 				unsigned int safe_cycle = increment_ohci_cycle_count(next_cycle,
963705794c5STakashi Sakamoto 								IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES);
96477ce9654STakashi Sakamoto 				lost = (compare_ohci_cycle_count(safe_cycle, cycle) < 0);
965705794c5STakashi Sakamoto 			}
966705794c5STakashi Sakamoto 			if (lost) {
967705794c5STakashi Sakamoto 				dev_err(&s->unit->device, "Detect discontinuity of cycle: %d %d\n",
968705794c5STakashi Sakamoto 					next_cycle, cycle);
969705794c5STakashi Sakamoto 				return -EIO;
970705794c5STakashi Sakamoto 			}
971705794c5STakashi Sakamoto 		}
972753e7179STakashi Sakamoto 
973ebd2a647STakashi Sakamoto 		err = parse_ir_ctx_header(s, cycle, ctx_header, &data_blocks, &dbc, &syt,
974fef4e61bSTakashi Sakamoto 					  packet_index, i, curr_cycle_time);
975753e7179STakashi Sakamoto 		if (err < 0)
976753e7179STakashi Sakamoto 			return err;
977753e7179STakashi Sakamoto 
978753e7179STakashi Sakamoto 		desc->cycle = cycle;
979753e7179STakashi Sakamoto 		desc->syt = syt;
980753e7179STakashi Sakamoto 		desc->data_blocks = data_blocks;
981753e7179STakashi Sakamoto 		desc->data_block_counter = dbc;
982814b4312STakashi Sakamoto 		desc->ctx_payload = s->buffer.packets[packet_index].buffer;
983753e7179STakashi Sakamoto 
984753e7179STakashi Sakamoto 		if (!(s->flags & CIP_DBC_IS_END_EVENT))
985753e7179STakashi Sakamoto 			dbc = (dbc + desc->data_blocks) & 0xff;
986753e7179STakashi Sakamoto 
987705794c5STakashi Sakamoto 		next_cycle = increment_ohci_cycle_count(next_cycle, 1);
988cec371ffSTakashi Sakamoto 		desc = amdtp_stream_next_packet_desc(s, desc);
98973246fc4STakashi Sakamoto 		++(*desc_count);
990705794c5STakashi Sakamoto 		ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header);
991814b4312STakashi Sakamoto 		packet_index = (packet_index + 1) % queue_size;
992753e7179STakashi Sakamoto 	}
993753e7179STakashi Sakamoto 
994705794c5STakashi Sakamoto 	s->next_cycle = next_cycle;
995753e7179STakashi Sakamoto 	s->data_block_counter = dbc;
996753e7179STakashi Sakamoto 
997753e7179STakashi Sakamoto 	return 0;
998753e7179STakashi Sakamoto }
999753e7179STakashi Sakamoto 
compute_syt(unsigned int syt_offset,unsigned int cycle,unsigned int transfer_delay)100083cfb5c5STakashi Sakamoto static unsigned int compute_syt(unsigned int syt_offset, unsigned int cycle,
100183cfb5c5STakashi Sakamoto 				unsigned int transfer_delay)
100283cfb5c5STakashi Sakamoto {
100383cfb5c5STakashi Sakamoto 	unsigned int syt;
100483cfb5c5STakashi Sakamoto 
100583cfb5c5STakashi Sakamoto 	syt_offset += transfer_delay;
100683cfb5c5STakashi Sakamoto 	syt = ((cycle + syt_offset / TICKS_PER_CYCLE) << 12) |
100783cfb5c5STakashi Sakamoto 	      (syt_offset % TICKS_PER_CYCLE);
100883cfb5c5STakashi Sakamoto 	return syt & CIP_SYT_MASK;
100983cfb5c5STakashi Sakamoto }
101083cfb5c5STakashi Sakamoto 
generate_rx_packet_descs(struct amdtp_stream * s,struct pkt_desc * desc,const __be32 * ctx_header,unsigned int packet_count)1011cec371ffSTakashi Sakamoto static void generate_rx_packet_descs(struct amdtp_stream *s, struct pkt_desc *desc,
1012f2bdee85STakashi Sakamoto 				     const __be32 *ctx_header, unsigned int packet_count)
1013f4f6ae7bSTakashi Sakamoto {
1014f2bdee85STakashi Sakamoto 	struct seq_desc *seq_descs = s->ctx_data.rx.seq.descs;
1015f2bdee85STakashi Sakamoto 	unsigned int seq_size = s->ctx_data.rx.seq.size;
1016f2bdee85STakashi Sakamoto 	unsigned int seq_pos = s->ctx_data.rx.seq.pos;
1017f4f6ae7bSTakashi Sakamoto 	unsigned int dbc = s->data_block_counter;
10188070d265STakashi Sakamoto 	bool aware_syt = !(s->flags & CIP_UNAWARE_SYT);
1019f4f6ae7bSTakashi Sakamoto 	int i;
1020f4f6ae7bSTakashi Sakamoto 
1021f2bdee85STakashi Sakamoto 	pool_seq_descs(s, seq_descs, seq_size, seq_pos, packet_count);
1022f2bdee85STakashi Sakamoto 
1023f2bdee85STakashi Sakamoto 	for (i = 0; i < packet_count; ++i) {
1024a0e02331STakashi Sakamoto 		unsigned int index = (s->packet_index + i) % s->queue_size;
1025f2bdee85STakashi Sakamoto 		const struct seq_desc *seq = seq_descs + seq_pos;
1026f4f6ae7bSTakashi Sakamoto 
10273e106f4fSTakashi Sakamoto 		desc->cycle = compute_ohci_it_cycle(*ctx_header, s->queue_size);
102869efd5c4STakashi Sakamoto 
102913d11f14STakashi Sakamoto 		if (aware_syt && seq->syt_offset != CIP_SYT_NO_INFO)
103013d11f14STakashi Sakamoto 			desc->syt = compute_syt(seq->syt_offset, desc->cycle, s->transfer_delay);
103113d11f14STakashi Sakamoto 		else
10328070d265STakashi Sakamoto 			desc->syt = CIP_SYT_NO_INFO;
10338070d265STakashi Sakamoto 
103469efd5c4STakashi Sakamoto 		desc->data_blocks = seq->data_blocks;
1035f4f6ae7bSTakashi Sakamoto 
1036f4f6ae7bSTakashi Sakamoto 		if (s->flags & CIP_DBC_IS_END_EVENT)
1037f4f6ae7bSTakashi Sakamoto 			dbc = (dbc + desc->data_blocks) & 0xff;
1038f4f6ae7bSTakashi Sakamoto 
1039f4f6ae7bSTakashi Sakamoto 		desc->data_block_counter = dbc;
1040f4f6ae7bSTakashi Sakamoto 
1041f4f6ae7bSTakashi Sakamoto 		if (!(s->flags & CIP_DBC_IS_END_EVENT))
1042f4f6ae7bSTakashi Sakamoto 			dbc = (dbc + desc->data_blocks) & 0xff;
1043f4f6ae7bSTakashi Sakamoto 
1044f4f6ae7bSTakashi Sakamoto 		desc->ctx_payload = s->buffer.packets[index].buffer;
1045f4f6ae7bSTakashi Sakamoto 
1046f2bdee85STakashi Sakamoto 		seq_pos = (seq_pos + 1) % seq_size;
1047cec371ffSTakashi Sakamoto 		desc = amdtp_stream_next_packet_desc(s, desc);
104869efd5c4STakashi Sakamoto 
1049f4f6ae7bSTakashi Sakamoto 		++ctx_header;
1050f4f6ae7bSTakashi Sakamoto 	}
1051f4f6ae7bSTakashi Sakamoto 
1052f4f6ae7bSTakashi Sakamoto 	s->data_block_counter = dbc;
1053f2bdee85STakashi Sakamoto 	s->ctx_data.rx.seq.pos = seq_pos;
1054f4f6ae7bSTakashi Sakamoto }
1055f4f6ae7bSTakashi Sakamoto 
cancel_stream(struct amdtp_stream * s)1056fce9b013STakashi Sakamoto static inline void cancel_stream(struct amdtp_stream *s)
1057fce9b013STakashi Sakamoto {
1058fce9b013STakashi Sakamoto 	s->packet_index = -1;
10593b86ec63STakashi Sakamoto 	if (in_softirq())
1060fce9b013STakashi Sakamoto 		amdtp_stream_pcm_abort(s);
1061fce9b013STakashi Sakamoto 	WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
1062fce9b013STakashi Sakamoto }
1063fce9b013STakashi Sakamoto 
compute_pcm_extra_delay(struct amdtp_stream * s,const struct pkt_desc * desc,unsigned int count)1064af13842cSTakashi Sakamoto static snd_pcm_sframes_t compute_pcm_extra_delay(struct amdtp_stream *s,
1065af13842cSTakashi Sakamoto 						 const struct pkt_desc *desc, unsigned int count)
1066af13842cSTakashi Sakamoto {
1067af13842cSTakashi Sakamoto 	unsigned int data_block_count = 0;
1068af13842cSTakashi Sakamoto 	u32 latest_cycle;
1069af13842cSTakashi Sakamoto 	u32 cycle_time;
1070af13842cSTakashi Sakamoto 	u32 curr_cycle;
1071af13842cSTakashi Sakamoto 	u32 cycle_gap;
1072af13842cSTakashi Sakamoto 	int i, err;
1073af13842cSTakashi Sakamoto 
1074af13842cSTakashi Sakamoto 	if (count == 0)
1075af13842cSTakashi Sakamoto 		goto end;
1076af13842cSTakashi Sakamoto 
1077af13842cSTakashi Sakamoto 	// Forward to the latest record.
1078af13842cSTakashi Sakamoto 	for (i = 0; i < count - 1; ++i)
1079af13842cSTakashi Sakamoto 		desc = amdtp_stream_next_packet_desc(s, desc);
1080af13842cSTakashi Sakamoto 	latest_cycle = desc->cycle;
1081af13842cSTakashi Sakamoto 
1082af13842cSTakashi Sakamoto 	err = fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &cycle_time);
1083af13842cSTakashi Sakamoto 	if (err < 0)
1084af13842cSTakashi Sakamoto 		goto end;
1085af13842cSTakashi Sakamoto 
1086af13842cSTakashi Sakamoto 	// Compute cycle count with lower 3 bits of second field and cycle field like timestamp
1087af13842cSTakashi Sakamoto 	// format of 1394 OHCI isochronous context.
1088af13842cSTakashi Sakamoto 	curr_cycle = compute_ohci_iso_ctx_cycle_count((cycle_time >> 12) & 0x0000ffff);
1089af13842cSTakashi Sakamoto 
1090af13842cSTakashi Sakamoto 	if (s->direction == AMDTP_IN_STREAM) {
1091af13842cSTakashi Sakamoto 		// NOTE: The AMDTP packet descriptor should be for the past isochronous cycle since
1092af13842cSTakashi Sakamoto 		// it corresponds to arrived isochronous packet.
1093af13842cSTakashi Sakamoto 		if (compare_ohci_cycle_count(latest_cycle, curr_cycle) > 0)
1094af13842cSTakashi Sakamoto 			goto end;
1095af13842cSTakashi Sakamoto 		cycle_gap = decrement_ohci_cycle_count(curr_cycle, latest_cycle);
1096af13842cSTakashi Sakamoto 
1097af13842cSTakashi Sakamoto 		// NOTE: estimate delay by recent history of arrived AMDTP packets. The estimated
1098af13842cSTakashi Sakamoto 		// value expectedly corresponds to a few packets (0-2) since the packet arrived at
1099af13842cSTakashi Sakamoto 		// the most recent isochronous cycle has been already processed.
1100af13842cSTakashi Sakamoto 		for (i = 0; i < cycle_gap; ++i) {
1101af13842cSTakashi Sakamoto 			desc = amdtp_stream_next_packet_desc(s, desc);
1102af13842cSTakashi Sakamoto 			data_block_count += desc->data_blocks;
1103af13842cSTakashi Sakamoto 		}
1104af13842cSTakashi Sakamoto 	} else {
1105af13842cSTakashi Sakamoto 		// NOTE: The AMDTP packet descriptor should be for the future isochronous cycle
1106af13842cSTakashi Sakamoto 		// since it was already scheduled.
1107af13842cSTakashi Sakamoto 		if (compare_ohci_cycle_count(latest_cycle, curr_cycle) < 0)
1108af13842cSTakashi Sakamoto 			goto end;
1109af13842cSTakashi Sakamoto 		cycle_gap = decrement_ohci_cycle_count(latest_cycle, curr_cycle);
1110af13842cSTakashi Sakamoto 
1111af13842cSTakashi Sakamoto 		// NOTE: use history of scheduled packets.
1112af13842cSTakashi Sakamoto 		for (i = 0; i < cycle_gap; ++i) {
1113af13842cSTakashi Sakamoto 			data_block_count += desc->data_blocks;
1114af13842cSTakashi Sakamoto 			desc = prev_packet_desc(s, desc);
1115af13842cSTakashi Sakamoto 		}
1116af13842cSTakashi Sakamoto 	}
1117af13842cSTakashi Sakamoto end:
1118af13842cSTakashi Sakamoto 	return data_block_count * s->pcm_frame_multiplier;
1119af13842cSTakashi Sakamoto }
1120af13842cSTakashi Sakamoto 
process_ctx_payloads(struct amdtp_stream * s,const struct pkt_desc * desc,unsigned int count)11210f5cfcb2STakashi Sakamoto static void process_ctx_payloads(struct amdtp_stream *s,
1122a36183f6STakashi Sakamoto 				 const struct pkt_desc *desc,
11230cac60c7STakashi Sakamoto 				 unsigned int count)
11240f5cfcb2STakashi Sakamoto {
11259a738ad1STakashi Sakamoto 	struct snd_pcm_substream *pcm;
1126a36183f6STakashi Sakamoto 	int i;
11270f5cfcb2STakashi Sakamoto 
11289a738ad1STakashi Sakamoto 	pcm = READ_ONCE(s->pcm);
11297fc693e4STakashi Sakamoto 	s->process_ctx_payloads(s, desc, count, pcm);
1130a36183f6STakashi Sakamoto 
1131a36183f6STakashi Sakamoto 	if (pcm) {
1132a36183f6STakashi Sakamoto 		unsigned int data_block_count = 0;
1133a36183f6STakashi Sakamoto 
1134af13842cSTakashi Sakamoto 		pcm->runtime->delay = compute_pcm_extra_delay(s, desc, count);
1135af13842cSTakashi Sakamoto 
1136a36183f6STakashi Sakamoto 		for (i = 0; i < count; ++i) {
1137a36183f6STakashi Sakamoto 			data_block_count += desc->data_blocks;
1138a36183f6STakashi Sakamoto 			desc = amdtp_stream_next_packet_desc(s, desc);
1139a36183f6STakashi Sakamoto 		}
1140a36183f6STakashi Sakamoto 
1141a36183f6STakashi Sakamoto 		update_pcm_pointers(s, pcm, data_block_count * s->pcm_frame_multiplier);
1142a36183f6STakashi Sakamoto 	}
11430f5cfcb2STakashi Sakamoto }
11440f5cfcb2STakashi Sakamoto 
process_rx_packets(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)11459b1fcd9bSTakashi Sakamoto static void process_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
11469b1fcd9bSTakashi Sakamoto 			       void *header, void *private_data)
1147d67c46b9STakashi Sakamoto {
1148d67c46b9STakashi Sakamoto 	struct amdtp_stream *s = private_data;
114969efd5c4STakashi Sakamoto 	const struct amdtp_domain *d = s->domain;
115026cd1e58STakashi Sakamoto 	const __be32 *ctx_header = header;
11519b1fcd9bSTakashi Sakamoto 	const unsigned int events_per_period = d->events_per_period;
115260dd4929STakashi Sakamoto 	unsigned int event_count = s->ctx_data.rx.event_count;
1153f0117128STakashi Sakamoto 	struct pkt_desc *desc = s->packet_descs_cursor;
1154233dbbc7STakashi Sakamoto 	unsigned int pkt_header_length;
1155a0e02331STakashi Sakamoto 	unsigned int packets;
1156fef4e61bSTakashi Sakamoto 	u32 curr_cycle_time;
1157d360870aSTakashi Sakamoto 	bool need_hw_irq;
11580dcb4efbSTakashi Sakamoto 	int i;
1159d67c46b9STakashi Sakamoto 
1160d67c46b9STakashi Sakamoto 	if (s->packet_index < 0)
1161d67c46b9STakashi Sakamoto 		return;
1162d67c46b9STakashi Sakamoto 
1163a0e02331STakashi Sakamoto 	// Calculate the number of packets in buffer and check XRUN.
1164a0e02331STakashi Sakamoto 	packets = header_length / sizeof(*ctx_header);
1165a0e02331STakashi Sakamoto 
1166cec371ffSTakashi Sakamoto 	generate_rx_packet_descs(s, desc, ctx_header, packets);
1167f4f6ae7bSTakashi Sakamoto 
1168cec371ffSTakashi Sakamoto 	process_ctx_payloads(s, desc, packets);
11695e2ece0fSTakashi Sakamoto 
1170233dbbc7STakashi Sakamoto 	if (!(s->flags & CIP_NO_HEADER))
1171233dbbc7STakashi Sakamoto 		pkt_header_length = IT_PKT_HEADER_SIZE_CIP;
1172233dbbc7STakashi Sakamoto 	else
1173233dbbc7STakashi Sakamoto 		pkt_header_length = 0;
1174233dbbc7STakashi Sakamoto 
1175d360870aSTakashi Sakamoto 	if (s == d->irq_target) {
1176d360870aSTakashi Sakamoto 		// At NO_PERIOD_WAKEUP mode, the packets for all IT/IR contexts are processed by
1177d360870aSTakashi Sakamoto 		// the tasks of user process operating ALSA PCM character device by calling ioctl(2)
1178d360870aSTakashi Sakamoto 		// with some requests, instead of scheduled hardware IRQ of an IT context.
1179d360870aSTakashi Sakamoto 		struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
1180d360870aSTakashi Sakamoto 		need_hw_irq = !pcm || !pcm->runtime->no_period_wakeup;
1181d360870aSTakashi Sakamoto 	} else {
1182d360870aSTakashi Sakamoto 		need_hw_irq = false;
1183d360870aSTakashi Sakamoto 	}
1184d360870aSTakashi Sakamoto 
1185fef4e61bSTakashi Sakamoto 	if (trace_amdtp_packet_enabled())
1186fef4e61bSTakashi Sakamoto 		(void)fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &curr_cycle_time);
1187fef4e61bSTakashi Sakamoto 
11885e2ece0fSTakashi Sakamoto 	for (i = 0; i < packets; ++i) {
1189c1839501STakashi Sakamoto 		DEFINE_RAW_FLEX(struct fw_iso_packet, template, header, CIP_HEADER_QUADLETS);
1190e229853dSTakashi Sakamoto 		bool sched_irq = false;
1191860d798cSTakashi Sakamoto 
11921d717123SGustavo A. R. Silva 		build_it_pkt_header(s, desc->cycle, template, pkt_header_length,
1193f4f6ae7bSTakashi Sakamoto 				    desc->data_blocks, desc->data_block_counter,
1194fef4e61bSTakashi Sakamoto 				    desc->syt, i, curr_cycle_time);
11956bc1a269STakashi Sakamoto 
11962472cfb3STakashi Sakamoto 		if (s == s->domain->irq_target) {
1197e229853dSTakashi Sakamoto 			event_count += desc->data_blocks;
1198e229853dSTakashi Sakamoto 			if (event_count >= events_per_period) {
1199e229853dSTakashi Sakamoto 				event_count -= events_per_period;
1200d360870aSTakashi Sakamoto 				sched_irq = need_hw_irq;
1201e229853dSTakashi Sakamoto 			}
120260dd4929STakashi Sakamoto 		}
1203e229853dSTakashi Sakamoto 
12041d717123SGustavo A. R. Silva 		if (queue_out_packet(s, template, sched_irq) < 0) {
1205fce9b013STakashi Sakamoto 			cancel_stream(s);
1206d67c46b9STakashi Sakamoto 			return;
1207d67c46b9STakashi Sakamoto 		}
1208cec371ffSTakashi Sakamoto 
1209cec371ffSTakashi Sakamoto 		desc = amdtp_stream_next_packet_desc(s, desc);
1210d67c46b9STakashi Sakamoto 	}
1211d67c46b9STakashi Sakamoto 
121260dd4929STakashi Sakamoto 	s->ctx_data.rx.event_count = event_count;
1213f0117128STakashi Sakamoto 	s->packet_descs_cursor = desc;
1214d67c46b9STakashi Sakamoto }
1215d67c46b9STakashi Sakamoto 
skip_rx_packets(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)12169b1fcd9bSTakashi Sakamoto static void skip_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
12179b1fcd9bSTakashi Sakamoto 			    void *header, void *private_data)
12189b1fcd9bSTakashi Sakamoto {
12199b1fcd9bSTakashi Sakamoto 	struct amdtp_stream *s = private_data;
12209b1fcd9bSTakashi Sakamoto 	struct amdtp_domain *d = s->domain;
12219b1fcd9bSTakashi Sakamoto 	const __be32 *ctx_header = header;
12229b1fcd9bSTakashi Sakamoto 	unsigned int packets;
12239b1fcd9bSTakashi Sakamoto 	unsigned int cycle;
12249b1fcd9bSTakashi Sakamoto 	int i;
12259b1fcd9bSTakashi Sakamoto 
12269b1fcd9bSTakashi Sakamoto 	if (s->packet_index < 0)
12279b1fcd9bSTakashi Sakamoto 		return;
12289b1fcd9bSTakashi Sakamoto 
12299b1fcd9bSTakashi Sakamoto 	packets = header_length / sizeof(*ctx_header);
12309b1fcd9bSTakashi Sakamoto 
12319b1fcd9bSTakashi Sakamoto 	cycle = compute_ohci_it_cycle(ctx_header[packets - 1], s->queue_size);
12329b1fcd9bSTakashi Sakamoto 	s->next_cycle = increment_ohci_cycle_count(cycle, 1);
12339b1fcd9bSTakashi Sakamoto 
12349b1fcd9bSTakashi Sakamoto 	for (i = 0; i < packets; ++i) {
12359b1fcd9bSTakashi Sakamoto 		struct fw_iso_packet params = {
12369b1fcd9bSTakashi Sakamoto 			.header_length = 0,
12379b1fcd9bSTakashi Sakamoto 			.payload_length = 0,
12389b1fcd9bSTakashi Sakamoto 		};
12399b1fcd9bSTakashi Sakamoto 		bool sched_irq = (s == d->irq_target && i == packets - 1);
12409b1fcd9bSTakashi Sakamoto 
12419b1fcd9bSTakashi Sakamoto 		if (queue_out_packet(s, &params, sched_irq) < 0) {
12429b1fcd9bSTakashi Sakamoto 			cancel_stream(s);
12439b1fcd9bSTakashi Sakamoto 			return;
12449b1fcd9bSTakashi Sakamoto 		}
12459b1fcd9bSTakashi Sakamoto 	}
12469b1fcd9bSTakashi Sakamoto }
12479b1fcd9bSTakashi Sakamoto 
12489b1fcd9bSTakashi Sakamoto static void irq_target_callback(struct fw_iso_context *context, u32 tstamp, size_t header_length,
12499b1fcd9bSTakashi Sakamoto 				void *header, void *private_data);
12509b1fcd9bSTakashi Sakamoto 
process_rx_packets_intermediately(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)12519b1fcd9bSTakashi Sakamoto static void process_rx_packets_intermediately(struct fw_iso_context *context, u32 tstamp,
12529b1fcd9bSTakashi Sakamoto 					size_t header_length, void *header, void *private_data)
12539b1fcd9bSTakashi Sakamoto {
12549b1fcd9bSTakashi Sakamoto 	struct amdtp_stream *s = private_data;
12559b1fcd9bSTakashi Sakamoto 	struct amdtp_domain *d = s->domain;
12569b1fcd9bSTakashi Sakamoto 	__be32 *ctx_header = header;
12579b1fcd9bSTakashi Sakamoto 	const unsigned int queue_size = s->queue_size;
12589b1fcd9bSTakashi Sakamoto 	unsigned int packets;
12599b1fcd9bSTakashi Sakamoto 	unsigned int offset;
12609b1fcd9bSTakashi Sakamoto 
12619b1fcd9bSTakashi Sakamoto 	if (s->packet_index < 0)
12629b1fcd9bSTakashi Sakamoto 		return;
12639b1fcd9bSTakashi Sakamoto 
12649b1fcd9bSTakashi Sakamoto 	packets = header_length / sizeof(*ctx_header);
12659b1fcd9bSTakashi Sakamoto 
12669b1fcd9bSTakashi Sakamoto 	offset = 0;
12679b1fcd9bSTakashi Sakamoto 	while (offset < packets) {
12689b1fcd9bSTakashi Sakamoto 		unsigned int cycle = compute_ohci_it_cycle(ctx_header[offset], queue_size);
12699b1fcd9bSTakashi Sakamoto 
12709b1fcd9bSTakashi Sakamoto 		if (compare_ohci_cycle_count(cycle, d->processing_cycle.rx_start) >= 0)
12719b1fcd9bSTakashi Sakamoto 			break;
12729b1fcd9bSTakashi Sakamoto 
12739b1fcd9bSTakashi Sakamoto 		++offset;
12749b1fcd9bSTakashi Sakamoto 	}
12759b1fcd9bSTakashi Sakamoto 
12769b1fcd9bSTakashi Sakamoto 	if (offset > 0) {
12779b1fcd9bSTakashi Sakamoto 		unsigned int length = sizeof(*ctx_header) * offset;
12789b1fcd9bSTakashi Sakamoto 
12799b1fcd9bSTakashi Sakamoto 		skip_rx_packets(context, tstamp, length, ctx_header, private_data);
12809b1fcd9bSTakashi Sakamoto 		if (amdtp_streaming_error(s))
12819b1fcd9bSTakashi Sakamoto 			return;
12829b1fcd9bSTakashi Sakamoto 
12839b1fcd9bSTakashi Sakamoto 		ctx_header += offset;
12849b1fcd9bSTakashi Sakamoto 		header_length -= length;
12859b1fcd9bSTakashi Sakamoto 	}
12869b1fcd9bSTakashi Sakamoto 
12879b1fcd9bSTakashi Sakamoto 	if (offset < packets) {
1288bdaedca7STakashi Sakamoto 		s->ready_processing = true;
1289bdaedca7STakashi Sakamoto 		wake_up(&s->ready_wait);
1290bdaedca7STakashi Sakamoto 
1291c38d8cffSTakashi Sakamoto 		if (d->replay.enable)
1292c38d8cffSTakashi Sakamoto 			s->ctx_data.rx.cache_pos = 0;
1293c38d8cffSTakashi Sakamoto 
12949b1fcd9bSTakashi Sakamoto 		process_rx_packets(context, tstamp, header_length, ctx_header, private_data);
12959b1fcd9bSTakashi Sakamoto 		if (amdtp_streaming_error(s))
12969b1fcd9bSTakashi Sakamoto 			return;
12979b1fcd9bSTakashi Sakamoto 
12989b1fcd9bSTakashi Sakamoto 		if (s == d->irq_target)
12999b1fcd9bSTakashi Sakamoto 			s->context->callback.sc = irq_target_callback;
13009b1fcd9bSTakashi Sakamoto 		else
13019b1fcd9bSTakashi Sakamoto 			s->context->callback.sc = process_rx_packets;
13029b1fcd9bSTakashi Sakamoto 	}
13039b1fcd9bSTakashi Sakamoto }
13049b1fcd9bSTakashi Sakamoto 
process_tx_packets(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)1305da3623abSTakashi Sakamoto static void process_tx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1306da3623abSTakashi Sakamoto 			       void *header, void *private_data)
1307d67c46b9STakashi Sakamoto {
1308d67c46b9STakashi Sakamoto 	struct amdtp_stream *s = private_data;
1309cc4f8e91STakashi Sakamoto 	__be32 *ctx_header = header;
1310f0117128STakashi Sakamoto 	struct pkt_desc *desc = s->packet_descs_cursor;
1311cccddec4STakashi Sakamoto 	unsigned int packet_count;
131273246fc4STakashi Sakamoto 	unsigned int desc_count;
1313753e7179STakashi Sakamoto 	int i;
1314753e7179STakashi Sakamoto 	int err;
1315d67c46b9STakashi Sakamoto 
1316d67c46b9STakashi Sakamoto 	if (s->packet_index < 0)
1317d67c46b9STakashi Sakamoto 		return;
1318d67c46b9STakashi Sakamoto 
1319a0e02331STakashi Sakamoto 	// Calculate the number of packets in buffer and check XRUN.
1320cccddec4STakashi Sakamoto 	packet_count = header_length / s->ctx_data.tx.ctx_header_size;
1321f90e2dedSTakashi Sakamoto 
132273246fc4STakashi Sakamoto 	desc_count = 0;
1323cec371ffSTakashi Sakamoto 	err = generate_tx_packet_descs(s, desc, ctx_header, packet_count, &desc_count);
1324753e7179STakashi Sakamoto 	if (err < 0) {
1325753e7179STakashi Sakamoto 		if (err != -EAGAIN) {
1326753e7179STakashi Sakamoto 			cancel_stream(s);
1327753e7179STakashi Sakamoto 			return;
1328753e7179STakashi Sakamoto 		}
13295e2ece0fSTakashi Sakamoto 	} else {
1330f9e5ecdfSTakashi Sakamoto 		struct amdtp_domain *d = s->domain;
1331f9e5ecdfSTakashi Sakamoto 
1332cec371ffSTakashi Sakamoto 		process_ctx_payloads(s, desc, desc_count);
1333f9e5ecdfSTakashi Sakamoto 
1334f9e5ecdfSTakashi Sakamoto 		if (d->replay.enable)
1335cec371ffSTakashi Sakamoto 			cache_seq(s, desc, desc_count);
1336f0117128STakashi Sakamoto 
1337f0117128STakashi Sakamoto 		for (i = 0; i < desc_count; ++i)
1338f0117128STakashi Sakamoto 			desc = amdtp_stream_next_packet_desc(s, desc);
1339f0117128STakashi Sakamoto 		s->packet_descs_cursor = desc;
13405e2ece0fSTakashi Sakamoto 	}
13415e2ece0fSTakashi Sakamoto 
1342cccddec4STakashi Sakamoto 	for (i = 0; i < packet_count; ++i) {
13435e2ece0fSTakashi Sakamoto 		struct fw_iso_packet params = {0};
1344a35463d1STakashi Sakamoto 
134560dd4929STakashi Sakamoto 		if (queue_in_packet(s, &params) < 0) {
1346753e7179STakashi Sakamoto 			cancel_stream(s);
1347753e7179STakashi Sakamoto 			return;
1348753e7179STakashi Sakamoto 		}
1349d67c46b9STakashi Sakamoto 	}
1350d67c46b9STakashi Sakamoto }
1351d67c46b9STakashi Sakamoto 
drop_tx_packets(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)1352da3623abSTakashi Sakamoto static void drop_tx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1353da3623abSTakashi Sakamoto 			    void *header, void *private_data)
1354da3623abSTakashi Sakamoto {
1355da3623abSTakashi Sakamoto 	struct amdtp_stream *s = private_data;
1356da3623abSTakashi Sakamoto 	const __be32 *ctx_header = header;
1357da3623abSTakashi Sakamoto 	unsigned int packets;
1358da3623abSTakashi Sakamoto 	unsigned int cycle;
1359da3623abSTakashi Sakamoto 	int i;
1360da3623abSTakashi Sakamoto 
1361da3623abSTakashi Sakamoto 	if (s->packet_index < 0)
1362da3623abSTakashi Sakamoto 		return;
1363da3623abSTakashi Sakamoto 
1364da3623abSTakashi Sakamoto 	packets = header_length / s->ctx_data.tx.ctx_header_size;
1365da3623abSTakashi Sakamoto 
1366da3623abSTakashi Sakamoto 	ctx_header += (packets - 1) * s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header);
1367da3623abSTakashi Sakamoto 	cycle = compute_ohci_cycle_count(ctx_header[1]);
1368da3623abSTakashi Sakamoto 	s->next_cycle = increment_ohci_cycle_count(cycle, 1);
1369da3623abSTakashi Sakamoto 
1370da3623abSTakashi Sakamoto 	for (i = 0; i < packets; ++i) {
1371da3623abSTakashi Sakamoto 		struct fw_iso_packet params = {0};
1372da3623abSTakashi Sakamoto 
1373da3623abSTakashi Sakamoto 		if (queue_in_packet(s, &params) < 0) {
1374da3623abSTakashi Sakamoto 			cancel_stream(s);
1375da3623abSTakashi Sakamoto 			return;
1376da3623abSTakashi Sakamoto 		}
1377da3623abSTakashi Sakamoto 	}
1378da3623abSTakashi Sakamoto }
1379da3623abSTakashi Sakamoto 
process_tx_packets_intermediately(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)1380da3623abSTakashi Sakamoto static void process_tx_packets_intermediately(struct fw_iso_context *context, u32 tstamp,
1381da3623abSTakashi Sakamoto 					size_t header_length, void *header, void *private_data)
1382da3623abSTakashi Sakamoto {
1383da3623abSTakashi Sakamoto 	struct amdtp_stream *s = private_data;
1384da3623abSTakashi Sakamoto 	struct amdtp_domain *d = s->domain;
1385da3623abSTakashi Sakamoto 	__be32 *ctx_header;
1386da3623abSTakashi Sakamoto 	unsigned int packets;
1387da3623abSTakashi Sakamoto 	unsigned int offset;
1388da3623abSTakashi Sakamoto 
1389da3623abSTakashi Sakamoto 	if (s->packet_index < 0)
1390da3623abSTakashi Sakamoto 		return;
1391da3623abSTakashi Sakamoto 
1392da3623abSTakashi Sakamoto 	packets = header_length / s->ctx_data.tx.ctx_header_size;
1393da3623abSTakashi Sakamoto 
1394da3623abSTakashi Sakamoto 	offset = 0;
1395da3623abSTakashi Sakamoto 	ctx_header = header;
1396da3623abSTakashi Sakamoto 	while (offset < packets) {
1397da3623abSTakashi Sakamoto 		unsigned int cycle = compute_ohci_cycle_count(ctx_header[1]);
1398da3623abSTakashi Sakamoto 
1399da3623abSTakashi Sakamoto 		if (compare_ohci_cycle_count(cycle, d->processing_cycle.tx_start) >= 0)
1400da3623abSTakashi Sakamoto 			break;
1401da3623abSTakashi Sakamoto 
1402da3623abSTakashi Sakamoto 		ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32);
1403da3623abSTakashi Sakamoto 		++offset;
1404da3623abSTakashi Sakamoto 	}
1405da3623abSTakashi Sakamoto 
1406da3623abSTakashi Sakamoto 	ctx_header = header;
1407da3623abSTakashi Sakamoto 
1408da3623abSTakashi Sakamoto 	if (offset > 0) {
1409da3623abSTakashi Sakamoto 		size_t length = s->ctx_data.tx.ctx_header_size * offset;
1410da3623abSTakashi Sakamoto 
1411da3623abSTakashi Sakamoto 		drop_tx_packets(context, tstamp, length, ctx_header, s);
1412da3623abSTakashi Sakamoto 		if (amdtp_streaming_error(s))
1413da3623abSTakashi Sakamoto 			return;
1414da3623abSTakashi Sakamoto 
1415da3623abSTakashi Sakamoto 		ctx_header += length / sizeof(*ctx_header);
1416da3623abSTakashi Sakamoto 		header_length -= length;
1417da3623abSTakashi Sakamoto 	}
1418da3623abSTakashi Sakamoto 
1419da3623abSTakashi Sakamoto 	if (offset < packets) {
1420bdaedca7STakashi Sakamoto 		s->ready_processing = true;
1421bdaedca7STakashi Sakamoto 		wake_up(&s->ready_wait);
1422bdaedca7STakashi Sakamoto 
1423da3623abSTakashi Sakamoto 		process_tx_packets(context, tstamp, header_length, ctx_header, s);
1424da3623abSTakashi Sakamoto 		if (amdtp_streaming_error(s))
1425da3623abSTakashi Sakamoto 			return;
1426da3623abSTakashi Sakamoto 
1427da3623abSTakashi Sakamoto 		context->callback.sc = process_tx_packets;
1428da3623abSTakashi Sakamoto 	}
1429da3623abSTakashi Sakamoto }
1430da3623abSTakashi Sakamoto 
drop_tx_packets_initially(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)1431fb25dcc8STakashi Sakamoto static void drop_tx_packets_initially(struct fw_iso_context *context, u32 tstamp,
1432fb25dcc8STakashi Sakamoto 				      size_t header_length, void *header, void *private_data)
1433fb25dcc8STakashi Sakamoto {
1434fb25dcc8STakashi Sakamoto 	struct amdtp_stream *s = private_data;
1435fb25dcc8STakashi Sakamoto 	struct amdtp_domain *d = s->domain;
1436fb25dcc8STakashi Sakamoto 	__be32 *ctx_header;
1437fb25dcc8STakashi Sakamoto 	unsigned int count;
1438fb25dcc8STakashi Sakamoto 	unsigned int events;
1439fb25dcc8STakashi Sakamoto 	int i;
1440fb25dcc8STakashi Sakamoto 
1441fb25dcc8STakashi Sakamoto 	if (s->packet_index < 0)
1442fb25dcc8STakashi Sakamoto 		return;
1443fb25dcc8STakashi Sakamoto 
1444fb25dcc8STakashi Sakamoto 	count = header_length / s->ctx_data.tx.ctx_header_size;
1445fb25dcc8STakashi Sakamoto 
1446fb25dcc8STakashi Sakamoto 	// Attempt to detect any event in the batch of packets.
1447fb25dcc8STakashi Sakamoto 	events = 0;
1448fb25dcc8STakashi Sakamoto 	ctx_header = header;
1449fb25dcc8STakashi Sakamoto 	for (i = 0; i < count; ++i) {
1450fb25dcc8STakashi Sakamoto 		unsigned int payload_quads =
1451fb25dcc8STakashi Sakamoto 			(be32_to_cpu(*ctx_header) >> ISO_DATA_LENGTH_SHIFT) / sizeof(__be32);
1452fb25dcc8STakashi Sakamoto 		unsigned int data_blocks;
1453fb25dcc8STakashi Sakamoto 
1454fb25dcc8STakashi Sakamoto 		if (s->flags & CIP_NO_HEADER) {
1455fb25dcc8STakashi Sakamoto 			data_blocks = payload_quads / s->data_block_quadlets;
1456fb25dcc8STakashi Sakamoto 		} else {
1457fb25dcc8STakashi Sakamoto 			__be32 *cip_headers = ctx_header + IR_CTX_HEADER_DEFAULT_QUADLETS;
1458fb25dcc8STakashi Sakamoto 
1459fb25dcc8STakashi Sakamoto 			if (payload_quads < CIP_HEADER_QUADLETS) {
1460fb25dcc8STakashi Sakamoto 				data_blocks = 0;
1461fb25dcc8STakashi Sakamoto 			} else {
1462fb25dcc8STakashi Sakamoto 				payload_quads -= CIP_HEADER_QUADLETS;
1463fb25dcc8STakashi Sakamoto 
1464fb25dcc8STakashi Sakamoto 				if (s->flags & CIP_UNAWARE_SYT) {
1465fb25dcc8STakashi Sakamoto 					data_blocks = payload_quads / s->data_block_quadlets;
1466fb25dcc8STakashi Sakamoto 				} else {
1467fb25dcc8STakashi Sakamoto 					u32 cip1 = be32_to_cpu(cip_headers[1]);
1468fb25dcc8STakashi Sakamoto 
1469fb25dcc8STakashi Sakamoto 					// NODATA packet can includes any data blocks but they are
1470fb25dcc8STakashi Sakamoto 					// not available as event.
1471fb25dcc8STakashi Sakamoto 					if ((cip1 & CIP_NO_DATA) == CIP_NO_DATA)
1472fb25dcc8STakashi Sakamoto 						data_blocks = 0;
1473fb25dcc8STakashi Sakamoto 					else
1474fb25dcc8STakashi Sakamoto 						data_blocks = payload_quads / s->data_block_quadlets;
1475fb25dcc8STakashi Sakamoto 				}
1476fb25dcc8STakashi Sakamoto 			}
1477fb25dcc8STakashi Sakamoto 		}
1478fb25dcc8STakashi Sakamoto 
1479fb25dcc8STakashi Sakamoto 		events += data_blocks;
1480fb25dcc8STakashi Sakamoto 
1481fb25dcc8STakashi Sakamoto 		ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32);
1482fb25dcc8STakashi Sakamoto 	}
1483fb25dcc8STakashi Sakamoto 
1484fb25dcc8STakashi Sakamoto 	drop_tx_packets(context, tstamp, header_length, header, s);
1485fb25dcc8STakashi Sakamoto 
1486fb25dcc8STakashi Sakamoto 	if (events > 0)
1487fb25dcc8STakashi Sakamoto 		s->ctx_data.tx.event_starts = true;
1488fb25dcc8STakashi Sakamoto 
1489fb25dcc8STakashi Sakamoto 	// Decide the cycle count to begin processing content of packet in IR contexts.
1490fb25dcc8STakashi Sakamoto 	{
1491fb25dcc8STakashi Sakamoto 		unsigned int stream_count = 0;
1492fb25dcc8STakashi Sakamoto 		unsigned int event_starts_count = 0;
1493fb25dcc8STakashi Sakamoto 		unsigned int cycle = UINT_MAX;
1494fb25dcc8STakashi Sakamoto 
1495fb25dcc8STakashi Sakamoto 		list_for_each_entry(s, &d->streams, list) {
1496fb25dcc8STakashi Sakamoto 			if (s->direction == AMDTP_IN_STREAM) {
1497fb25dcc8STakashi Sakamoto 				++stream_count;
1498fb25dcc8STakashi Sakamoto 				if (s->ctx_data.tx.event_starts)
1499fb25dcc8STakashi Sakamoto 					++event_starts_count;
1500fb25dcc8STakashi Sakamoto 			}
1501fb25dcc8STakashi Sakamoto 		}
1502fb25dcc8STakashi Sakamoto 
1503fb25dcc8STakashi Sakamoto 		if (stream_count == event_starts_count) {
1504fb25dcc8STakashi Sakamoto 			unsigned int next_cycle;
1505fb25dcc8STakashi Sakamoto 
1506fb25dcc8STakashi Sakamoto 			list_for_each_entry(s, &d->streams, list) {
1507fb25dcc8STakashi Sakamoto 				if (s->direction != AMDTP_IN_STREAM)
1508fb25dcc8STakashi Sakamoto 					continue;
1509fb25dcc8STakashi Sakamoto 
1510fb25dcc8STakashi Sakamoto 				next_cycle = increment_ohci_cycle_count(s->next_cycle,
1511fb25dcc8STakashi Sakamoto 								d->processing_cycle.tx_init_skip);
1512fb25dcc8STakashi Sakamoto 				if (cycle == UINT_MAX ||
1513fb25dcc8STakashi Sakamoto 				    compare_ohci_cycle_count(next_cycle, cycle) > 0)
1514fb25dcc8STakashi Sakamoto 					cycle = next_cycle;
1515fb25dcc8STakashi Sakamoto 
1516fb25dcc8STakashi Sakamoto 				s->context->callback.sc = process_tx_packets_intermediately;
1517fb25dcc8STakashi Sakamoto 			}
1518fb25dcc8STakashi Sakamoto 
1519fb25dcc8STakashi Sakamoto 			d->processing_cycle.tx_start = cycle;
1520fb25dcc8STakashi Sakamoto 		}
1521fb25dcc8STakashi Sakamoto 	}
1522fb25dcc8STakashi Sakamoto }
1523fb25dcc8STakashi Sakamoto 
process_ctxs_in_domain(struct amdtp_domain * d)15249b1fcd9bSTakashi Sakamoto static void process_ctxs_in_domain(struct amdtp_domain *d)
152560dd4929STakashi Sakamoto {
152660dd4929STakashi Sakamoto 	struct amdtp_stream *s;
152760dd4929STakashi Sakamoto 
152860dd4929STakashi Sakamoto 	list_for_each_entry(s, &d->streams, list) {
15299b1fcd9bSTakashi Sakamoto 		if (s != d->irq_target && amdtp_stream_running(s))
153060dd4929STakashi Sakamoto 			fw_iso_context_flush_completions(s->context);
15319b1fcd9bSTakashi Sakamoto 
153260dd4929STakashi Sakamoto 		if (amdtp_streaming_error(s))
153360dd4929STakashi Sakamoto 			goto error;
153460dd4929STakashi Sakamoto 	}
153560dd4929STakashi Sakamoto 
153660dd4929STakashi Sakamoto 	return;
153760dd4929STakashi Sakamoto error:
15389b1fcd9bSTakashi Sakamoto 	if (amdtp_stream_running(d->irq_target))
15399b1fcd9bSTakashi Sakamoto 		cancel_stream(d->irq_target);
154060dd4929STakashi Sakamoto 
154160dd4929STakashi Sakamoto 	list_for_each_entry(s, &d->streams, list) {
154260dd4929STakashi Sakamoto 		if (amdtp_stream_running(s))
154360dd4929STakashi Sakamoto 			cancel_stream(s);
154460dd4929STakashi Sakamoto 	}
154560dd4929STakashi Sakamoto }
154660dd4929STakashi Sakamoto 
irq_target_callback(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)15479b1fcd9bSTakashi Sakamoto static void irq_target_callback(struct fw_iso_context *context, u32 tstamp, size_t header_length,
15489b1fcd9bSTakashi Sakamoto 				void *header, void *private_data)
15499b1fcd9bSTakashi Sakamoto {
15509b1fcd9bSTakashi Sakamoto 	struct amdtp_stream *s = private_data;
15519b1fcd9bSTakashi Sakamoto 	struct amdtp_domain *d = s->domain;
15529b1fcd9bSTakashi Sakamoto 
15539b1fcd9bSTakashi Sakamoto 	process_rx_packets(context, tstamp, header_length, header, private_data);
15549b1fcd9bSTakashi Sakamoto 	process_ctxs_in_domain(d);
15559b1fcd9bSTakashi Sakamoto }
15569b1fcd9bSTakashi Sakamoto 
irq_target_callback_intermediately(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)15579b1fcd9bSTakashi Sakamoto static void irq_target_callback_intermediately(struct fw_iso_context *context, u32 tstamp,
15589b1fcd9bSTakashi Sakamoto 					size_t header_length, void *header, void *private_data)
15599b1fcd9bSTakashi Sakamoto {
15609b1fcd9bSTakashi Sakamoto 	struct amdtp_stream *s = private_data;
15619b1fcd9bSTakashi Sakamoto 	struct amdtp_domain *d = s->domain;
15629b1fcd9bSTakashi Sakamoto 
15639b1fcd9bSTakashi Sakamoto 	process_rx_packets_intermediately(context, tstamp, header_length, header, private_data);
15649b1fcd9bSTakashi Sakamoto 	process_ctxs_in_domain(d);
15659b1fcd9bSTakashi Sakamoto }
15669b1fcd9bSTakashi Sakamoto 
irq_target_callback_skip(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)15679b1fcd9bSTakashi Sakamoto static void irq_target_callback_skip(struct fw_iso_context *context, u32 tstamp,
15689b1fcd9bSTakashi Sakamoto 				     size_t header_length, void *header, void *private_data)
15699b1fcd9bSTakashi Sakamoto {
15709b1fcd9bSTakashi Sakamoto 	struct amdtp_stream *s = private_data;
15719b1fcd9bSTakashi Sakamoto 	struct amdtp_domain *d = s->domain;
157239c2649cSTakashi Sakamoto 	bool ready_to_start;
15739b1fcd9bSTakashi Sakamoto 
15749b1fcd9bSTakashi Sakamoto 	skip_rx_packets(context, tstamp, header_length, header, private_data);
15759b1fcd9bSTakashi Sakamoto 	process_ctxs_in_domain(d);
15769b1fcd9bSTakashi Sakamoto 
15772f21a177STakashi Sakamoto 	if (d->replay.enable && !d->replay.on_the_fly) {
157839c2649cSTakashi Sakamoto 		unsigned int rx_count = 0;
157939c2649cSTakashi Sakamoto 		unsigned int rx_ready_count = 0;
158039c2649cSTakashi Sakamoto 		struct amdtp_stream *rx;
158139c2649cSTakashi Sakamoto 
158239c2649cSTakashi Sakamoto 		list_for_each_entry(rx, &d->streams, list) {
158339c2649cSTakashi Sakamoto 			struct amdtp_stream *tx;
158439c2649cSTakashi Sakamoto 			unsigned int cached_cycles;
158539c2649cSTakashi Sakamoto 
158639c2649cSTakashi Sakamoto 			if (rx->direction != AMDTP_OUT_STREAM)
158739c2649cSTakashi Sakamoto 				continue;
158839c2649cSTakashi Sakamoto 			++rx_count;
158939c2649cSTakashi Sakamoto 
159039c2649cSTakashi Sakamoto 			tx = rx->ctx_data.rx.replay_target;
159139c2649cSTakashi Sakamoto 			cached_cycles = calculate_cached_cycle_count(tx, 0);
159239c2649cSTakashi Sakamoto 			if (cached_cycles > tx->ctx_data.tx.cache.size / 2)
159339c2649cSTakashi Sakamoto 				++rx_ready_count;
159439c2649cSTakashi Sakamoto 		}
159539c2649cSTakashi Sakamoto 
159639c2649cSTakashi Sakamoto 		ready_to_start = (rx_count == rx_ready_count);
159739c2649cSTakashi Sakamoto 	} else {
159839c2649cSTakashi Sakamoto 		ready_to_start = true;
159939c2649cSTakashi Sakamoto 	}
160039c2649cSTakashi Sakamoto 
16019b1fcd9bSTakashi Sakamoto 	// Decide the cycle count to begin processing content of packet in IT contexts. All of IT
16029b1fcd9bSTakashi Sakamoto 	// contexts are expected to start and get callback when reaching here.
160339c2649cSTakashi Sakamoto 	if (ready_to_start) {
160439c2649cSTakashi Sakamoto 		unsigned int cycle = s->next_cycle;
16059b1fcd9bSTakashi Sakamoto 		list_for_each_entry(s, &d->streams, list) {
16069b1fcd9bSTakashi Sakamoto 			if (s->direction != AMDTP_OUT_STREAM)
16079b1fcd9bSTakashi Sakamoto 				continue;
16089b1fcd9bSTakashi Sakamoto 
16099b1fcd9bSTakashi Sakamoto 			if (compare_ohci_cycle_count(s->next_cycle, cycle) > 0)
16109b1fcd9bSTakashi Sakamoto 				cycle = s->next_cycle;
16119b1fcd9bSTakashi Sakamoto 
16129b1fcd9bSTakashi Sakamoto 			if (s == d->irq_target)
16139b1fcd9bSTakashi Sakamoto 				s->context->callback.sc = irq_target_callback_intermediately;
16149b1fcd9bSTakashi Sakamoto 			else
16159b1fcd9bSTakashi Sakamoto 				s->context->callback.sc = process_rx_packets_intermediately;
16169b1fcd9bSTakashi Sakamoto 		}
16179b1fcd9bSTakashi Sakamoto 
16189b1fcd9bSTakashi Sakamoto 		d->processing_cycle.rx_start = cycle;
16199b1fcd9bSTakashi Sakamoto 	}
162039c2649cSTakashi Sakamoto }
16219b1fcd9bSTakashi Sakamoto 
1622b7c7699bSTakashi Sakamoto // This is executed one time. For in-stream, first packet has come. For out-stream, prepared to
1623b7c7699bSTakashi Sakamoto // transmit first packet.
amdtp_stream_first_callback(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)1624d67c46b9STakashi Sakamoto static void amdtp_stream_first_callback(struct fw_iso_context *context,
162573fc7f08STakashi Sakamoto 					u32 tstamp, size_t header_length,
1626d67c46b9STakashi Sakamoto 					void *header, void *private_data)
1627d67c46b9STakashi Sakamoto {
1628d67c46b9STakashi Sakamoto 	struct amdtp_stream *s = private_data;
1629da3623abSTakashi Sakamoto 	struct amdtp_domain *d = s->domain;
1630d67c46b9STakashi Sakamoto 
1631cc4f8e91STakashi Sakamoto 	if (s->direction == AMDTP_IN_STREAM) {
1632fb25dcc8STakashi Sakamoto 		context->callback.sc = drop_tx_packets_initially;
1633a04513f8STakashi Sakamoto 	} else {
1634da3623abSTakashi Sakamoto 		if (s == d->irq_target)
16359b1fcd9bSTakashi Sakamoto 			context->callback.sc = irq_target_callback_skip;
16362472cfb3STakashi Sakamoto 		else
16379b1fcd9bSTakashi Sakamoto 			context->callback.sc = skip_rx_packets;
1638a04513f8STakashi Sakamoto 	}
1639a04513f8STakashi Sakamoto 
164073fc7f08STakashi Sakamoto 	context->callback.sc(context, tstamp, header_length, header, s);
1641d67c46b9STakashi Sakamoto }
1642d67c46b9STakashi Sakamoto 
1643d67c46b9STakashi Sakamoto /**
1644d67c46b9STakashi Sakamoto  * amdtp_stream_start - start transferring packets
1645d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to start
1646d67c46b9STakashi Sakamoto  * @channel: the isochronous channel on the bus
1647d67c46b9STakashi Sakamoto  * @speed: firewire speed code
1648af86b0b1STakashi Sakamoto  * @queue_size: The number of packets in the queue.
1649af86b0b1STakashi Sakamoto  * @idle_irq_interval: the interval to queue packet during initial state.
1650d67c46b9STakashi Sakamoto  *
1651d67c46b9STakashi Sakamoto  * The stream cannot be started until it has been configured with
1652d67c46b9STakashi Sakamoto  * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
1653d67c46b9STakashi Sakamoto  * device can be started.
1654d67c46b9STakashi Sakamoto  */
amdtp_stream_start(struct amdtp_stream * s,int channel,int speed,unsigned int queue_size,unsigned int idle_irq_interval)1655a0e02331STakashi Sakamoto static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed,
1656bd165079STakashi Sakamoto 			      unsigned int queue_size, unsigned int idle_irq_interval)
1657d67c46b9STakashi Sakamoto {
16582472cfb3STakashi Sakamoto 	bool is_irq_target = (s == s->domain->irq_target);
1659d3d10a4aSTakashi Sakamoto 	unsigned int ctx_header_size;
1660f11453c7STakashi Sakamoto 	unsigned int max_ctx_payload_size;
1661d67c46b9STakashi Sakamoto 	enum dma_data_direction dir;
1662cec371ffSTakashi Sakamoto 	struct pkt_desc *descs;
1663cec371ffSTakashi Sakamoto 	int i, type, tag, err;
1664d67c46b9STakashi Sakamoto 
1665d67c46b9STakashi Sakamoto 	mutex_lock(&s->mutex);
1666d67c46b9STakashi Sakamoto 
1667d67c46b9STakashi Sakamoto 	if (WARN_ON(amdtp_stream_running(s) ||
1668d67c46b9STakashi Sakamoto 		    (s->data_block_quadlets < 1))) {
1669d67c46b9STakashi Sakamoto 		err = -EBADFD;
1670d67c46b9STakashi Sakamoto 		goto err_unlock;
1671d67c46b9STakashi Sakamoto 	}
1672d67c46b9STakashi Sakamoto 
1673d3d10a4aSTakashi Sakamoto 	if (s->direction == AMDTP_IN_STREAM) {
167460dd4929STakashi Sakamoto 		// NOTE: IT context should be used for constant IRQ.
167560dd4929STakashi Sakamoto 		if (is_irq_target) {
167660dd4929STakashi Sakamoto 			err = -EINVAL;
167760dd4929STakashi Sakamoto 			goto err_unlock;
167860dd4929STakashi Sakamoto 		}
167960dd4929STakashi Sakamoto 
1680d67c46b9STakashi Sakamoto 		s->data_block_counter = UINT_MAX;
1681d3d10a4aSTakashi Sakamoto 	} else {
1682d67c46b9STakashi Sakamoto 		s->data_block_counter = 0;
1683d3d10a4aSTakashi Sakamoto 	}
1684d67c46b9STakashi Sakamoto 
16851be4f21dSTakashi Sakamoto 	// initialize packet buffer.
1686d67c46b9STakashi Sakamoto 	if (s->direction == AMDTP_IN_STREAM) {
1687d67c46b9STakashi Sakamoto 		dir = DMA_FROM_DEVICE;
1688d67c46b9STakashi Sakamoto 		type = FW_ISO_CONTEXT_RECEIVE;
1689c75f3678STakashi Sakamoto 		if (!(s->flags & CIP_NO_HEADER))
1690f11453c7STakashi Sakamoto 			ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
1691c75f3678STakashi Sakamoto 		else
1692f11453c7STakashi Sakamoto 			ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
1693d67c46b9STakashi Sakamoto 	} else {
1694d67c46b9STakashi Sakamoto 		dir = DMA_TO_DEVICE;
1695d67c46b9STakashi Sakamoto 		type = FW_ISO_CONTEXT_TRANSMIT;
1696df9160b9STakashi Sakamoto 		ctx_header_size = 0;	// No effect for IT context.
1697b18f0cfaSTakashi Sakamoto 	}
1698c75f3678STakashi Sakamoto 	max_ctx_payload_size = amdtp_stream_get_max_ctx_payload_size(s);
1699f11453c7STakashi Sakamoto 
1700c75f3678STakashi Sakamoto 	err = iso_packets_buffer_init(&s->buffer, s->unit, queue_size, max_ctx_payload_size, dir);
1701d67c46b9STakashi Sakamoto 	if (err < 0)
1702d67c46b9STakashi Sakamoto 		goto err_unlock;
1703af86b0b1STakashi Sakamoto 	s->queue_size = queue_size;
170460dd4929STakashi Sakamoto 
1705d67c46b9STakashi Sakamoto 	s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
1706d3d10a4aSTakashi Sakamoto 					  type, channel, speed, ctx_header_size,
17072472cfb3STakashi Sakamoto 					  amdtp_stream_first_callback, s);
1708d67c46b9STakashi Sakamoto 	if (IS_ERR(s->context)) {
1709d67c46b9STakashi Sakamoto 		err = PTR_ERR(s->context);
1710d67c46b9STakashi Sakamoto 		if (err == -EBUSY)
1711d67c46b9STakashi Sakamoto 			dev_err(&s->unit->device,
1712d67c46b9STakashi Sakamoto 				"no free stream on this controller\n");
1713d67c46b9STakashi Sakamoto 		goto err_buffer;
1714d67c46b9STakashi Sakamoto 	}
1715d67c46b9STakashi Sakamoto 
1716d67c46b9STakashi Sakamoto 	amdtp_stream_update(s);
1717d67c46b9STakashi Sakamoto 
1718d3d10a4aSTakashi Sakamoto 	if (s->direction == AMDTP_IN_STREAM) {
1719f11453c7STakashi Sakamoto 		s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size;
1720d3d10a4aSTakashi Sakamoto 		s->ctx_data.tx.ctx_header_size = ctx_header_size;
1721fb25dcc8STakashi Sakamoto 		s->ctx_data.tx.event_starts = false;
1722f9e5ecdfSTakashi Sakamoto 
1723f9e5ecdfSTakashi Sakamoto 		if (s->domain->replay.enable) {
1724f9e5ecdfSTakashi Sakamoto 			// struct fw_iso_context.drop_overflow_headers is false therefore it's
1725f9e5ecdfSTakashi Sakamoto 			// possible to cache much unexpectedly.
1726f9e5ecdfSTakashi Sakamoto 			s->ctx_data.tx.cache.size = max_t(unsigned int, s->syt_interval * 2,
1727f9e5ecdfSTakashi Sakamoto 							  queue_size * 3 / 2);
1728cccddec4STakashi Sakamoto 			s->ctx_data.tx.cache.pos = 0;
1729f9e5ecdfSTakashi Sakamoto 			s->ctx_data.tx.cache.descs = kcalloc(s->ctx_data.tx.cache.size,
1730f9e5ecdfSTakashi Sakamoto 						sizeof(*s->ctx_data.tx.cache.descs), GFP_KERNEL);
17318b6e2193SDan Carpenter 			if (!s->ctx_data.tx.cache.descs) {
17328b6e2193SDan Carpenter 				err = -ENOMEM;
1733f9e5ecdfSTakashi Sakamoto 				goto err_context;
1734f9e5ecdfSTakashi Sakamoto 			}
17358b6e2193SDan Carpenter 		}
1736bd165079STakashi Sakamoto 	} else {
17376f24bb8aSTakashi Sakamoto 		static const struct {
17386f24bb8aSTakashi Sakamoto 			unsigned int data_block;
17396f24bb8aSTakashi Sakamoto 			unsigned int syt_offset;
17406f24bb8aSTakashi Sakamoto 		} *entry, initial_state[] = {
17416f24bb8aSTakashi Sakamoto 			[CIP_SFC_32000]  = {  4, 3072 },
17426f24bb8aSTakashi Sakamoto 			[CIP_SFC_48000]  = {  6, 1024 },
17436f24bb8aSTakashi Sakamoto 			[CIP_SFC_96000]  = { 12, 1024 },
17446f24bb8aSTakashi Sakamoto 			[CIP_SFC_192000] = { 24, 1024 },
17456f24bb8aSTakashi Sakamoto 			[CIP_SFC_44100]  = {  0,   67 },
17466f24bb8aSTakashi Sakamoto 			[CIP_SFC_88200]  = {  0,   67 },
17476f24bb8aSTakashi Sakamoto 			[CIP_SFC_176400] = {  0,   67 },
17486f24bb8aSTakashi Sakamoto 		};
17496f24bb8aSTakashi Sakamoto 
17506f24bb8aSTakashi Sakamoto 		s->ctx_data.rx.seq.descs = kcalloc(queue_size, sizeof(*s->ctx_data.rx.seq.descs), GFP_KERNEL);
17518b6e2193SDan Carpenter 		if (!s->ctx_data.rx.seq.descs) {
17528b6e2193SDan Carpenter 			err = -ENOMEM;
17536f24bb8aSTakashi Sakamoto 			goto err_context;
17548b6e2193SDan Carpenter 		}
17556f24bb8aSTakashi Sakamoto 		s->ctx_data.rx.seq.size = queue_size;
1756119c446aSTakashi Sakamoto 		s->ctx_data.rx.seq.pos = 0;
17576f24bb8aSTakashi Sakamoto 
17586f24bb8aSTakashi Sakamoto 		entry = &initial_state[s->sfc];
17596f24bb8aSTakashi Sakamoto 		s->ctx_data.rx.data_block_state = entry->data_block;
17606f24bb8aSTakashi Sakamoto 		s->ctx_data.rx.syt_offset_state = entry->syt_offset;
17616f24bb8aSTakashi Sakamoto 		s->ctx_data.rx.last_syt_offset = TICKS_PER_CYCLE;
17626f24bb8aSTakashi Sakamoto 
1763bd165079STakashi Sakamoto 		s->ctx_data.rx.event_count = 0;
1764d3d10a4aSTakashi Sakamoto 	}
176552759c09STakashi Sakamoto 
17663b196c39STakashi Sakamoto 	if (s->flags & CIP_NO_HEADER)
17673b196c39STakashi Sakamoto 		s->tag = TAG_NO_CIP_HEADER;
17683b196c39STakashi Sakamoto 	else
17693b196c39STakashi Sakamoto 		s->tag = TAG_CIP;
17703b196c39STakashi Sakamoto 
1771af13842cSTakashi Sakamoto 	// NOTE: When operating without hardIRQ/softIRQ, applications tends to call ioctl request
1772af13842cSTakashi Sakamoto 	// for runtime of PCM substream in the interval equivalent to the size of PCM buffer. It
1773af13842cSTakashi Sakamoto 	// could take a round over queue of AMDTP packet descriptors and small loss of history. For
1774af13842cSTakashi Sakamoto 	// safe, keep more 8 elements for the queue, equivalent to 1 ms.
1775af13842cSTakashi Sakamoto 	descs = kcalloc(s->queue_size + 8, sizeof(*descs), GFP_KERNEL);
1776cec371ffSTakashi Sakamoto 	if (!descs) {
177704130cf8STakashi Sakamoto 		err = -ENOMEM;
177804130cf8STakashi Sakamoto 		goto err_context;
177904130cf8STakashi Sakamoto 	}
1780f0117128STakashi Sakamoto 	s->packet_descs = descs;
1781cec371ffSTakashi Sakamoto 
1782cec371ffSTakashi Sakamoto 	INIT_LIST_HEAD(&s->packet_descs_list);
1783cec371ffSTakashi Sakamoto 	for (i = 0; i < s->queue_size; ++i) {
1784cec371ffSTakashi Sakamoto 		INIT_LIST_HEAD(&descs->link);
1785cec371ffSTakashi Sakamoto 		list_add_tail(&descs->link, &s->packet_descs_list);
1786cec371ffSTakashi Sakamoto 		++descs;
1787cec371ffSTakashi Sakamoto 	}
1788f0117128STakashi Sakamoto 	s->packet_descs_cursor = list_first_entry(&s->packet_descs_list, struct pkt_desc, link);
178904130cf8STakashi Sakamoto 
1790d67c46b9STakashi Sakamoto 	s->packet_index = 0;
1791d67c46b9STakashi Sakamoto 	do {
17926007bf54STakashi Sakamoto 		struct fw_iso_packet params;
1793e229853dSTakashi Sakamoto 
1794b18f0cfaSTakashi Sakamoto 		if (s->direction == AMDTP_IN_STREAM) {
179560dd4929STakashi Sakamoto 			err = queue_in_packet(s, &params);
1796b18f0cfaSTakashi Sakamoto 		} else {
179760dd4929STakashi Sakamoto 			bool sched_irq = false;
179860dd4929STakashi Sakamoto 
1799b18f0cfaSTakashi Sakamoto 			params.header_length = 0;
1800b18f0cfaSTakashi Sakamoto 			params.payload_length = 0;
180160dd4929STakashi Sakamoto 
180260dd4929STakashi Sakamoto 			if (is_irq_target) {
180360dd4929STakashi Sakamoto 				sched_irq = !((s->packet_index + 1) %
180460dd4929STakashi Sakamoto 					      idle_irq_interval);
180560dd4929STakashi Sakamoto 			}
180660dd4929STakashi Sakamoto 
1807e229853dSTakashi Sakamoto 			err = queue_out_packet(s, &params, sched_irq);
1808b18f0cfaSTakashi Sakamoto 		}
1809d67c46b9STakashi Sakamoto 		if (err < 0)
181004130cf8STakashi Sakamoto 			goto err_pkt_descs;
1811d67c46b9STakashi Sakamoto 	} while (s->packet_index > 0);
1812d67c46b9STakashi Sakamoto 
1813d67c46b9STakashi Sakamoto 	/* NOTE: TAG1 matches CIP. This just affects in stream. */
1814d67c46b9STakashi Sakamoto 	tag = FW_ISO_CONTEXT_MATCH_TAG1;
18153b196c39STakashi Sakamoto 	if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
1816d67c46b9STakashi Sakamoto 		tag |= FW_ISO_CONTEXT_MATCH_TAG0;
1817d67c46b9STakashi Sakamoto 
1818bdaedca7STakashi Sakamoto 	s->ready_processing = false;
1819bd165079STakashi Sakamoto 	err = fw_iso_context_start(s->context, -1, 0, tag);
1820d67c46b9STakashi Sakamoto 	if (err < 0)
182104130cf8STakashi Sakamoto 		goto err_pkt_descs;
1822d67c46b9STakashi Sakamoto 
1823d67c46b9STakashi Sakamoto 	mutex_unlock(&s->mutex);
1824d67c46b9STakashi Sakamoto 
1825d67c46b9STakashi Sakamoto 	return 0;
182604130cf8STakashi Sakamoto err_pkt_descs:
1827f0117128STakashi Sakamoto 	kfree(s->packet_descs);
1828f0117128STakashi Sakamoto 	s->packet_descs = NULL;
1829d67c46b9STakashi Sakamoto err_context:
1830f9e5ecdfSTakashi Sakamoto 	if (s->direction == AMDTP_OUT_STREAM) {
18316f24bb8aSTakashi Sakamoto 		kfree(s->ctx_data.rx.seq.descs);
1832f9e5ecdfSTakashi Sakamoto 	} else {
1833f9e5ecdfSTakashi Sakamoto 		if (s->domain->replay.enable)
1834f9e5ecdfSTakashi Sakamoto 			kfree(s->ctx_data.tx.cache.descs);
1835f9e5ecdfSTakashi Sakamoto 	}
1836d67c46b9STakashi Sakamoto 	fw_iso_context_destroy(s->context);
1837d67c46b9STakashi Sakamoto 	s->context = ERR_PTR(-1);
1838d67c46b9STakashi Sakamoto err_buffer:
1839d67c46b9STakashi Sakamoto 	iso_packets_buffer_destroy(&s->buffer, s->unit);
1840d67c46b9STakashi Sakamoto err_unlock:
1841d67c46b9STakashi Sakamoto 	mutex_unlock(&s->mutex);
1842d67c46b9STakashi Sakamoto 
1843d67c46b9STakashi Sakamoto 	return err;
1844d67c46b9STakashi Sakamoto }
1845d67c46b9STakashi Sakamoto 
1846d67c46b9STakashi Sakamoto /**
1847f890f9a0STakashi Sakamoto  * amdtp_domain_stream_pcm_pointer - get the PCM buffer position
1848f890f9a0STakashi Sakamoto  * @d: the AMDTP domain.
1849d67c46b9STakashi Sakamoto  * @s: the AMDTP stream that transports the PCM data
1850d67c46b9STakashi Sakamoto  *
1851d67c46b9STakashi Sakamoto  * Returns the current buffer position, in frames.
1852d67c46b9STakashi Sakamoto  */
amdtp_domain_stream_pcm_pointer(struct amdtp_domain * d,struct amdtp_stream * s)1853f890f9a0STakashi Sakamoto unsigned long amdtp_domain_stream_pcm_pointer(struct amdtp_domain *d,
1854f890f9a0STakashi Sakamoto 					      struct amdtp_stream *s)
1855d67c46b9STakashi Sakamoto {
1856f890f9a0STakashi Sakamoto 	struct amdtp_stream *irq_target = d->irq_target;
1857f890f9a0STakashi Sakamoto 
1858f890f9a0STakashi Sakamoto 	if (irq_target && amdtp_stream_running(irq_target)) {
1859*3dab73abSEdmund Raile 		// use wq to prevent AB/BA deadlock competition for
1860*3dab73abSEdmund Raile 		// substream lock:
1861*3dab73abSEdmund Raile 		// fw_iso_context_flush_completions() acquires
1862*3dab73abSEdmund Raile 		// lock by ohci_flush_iso_completions(),
1863*3dab73abSEdmund Raile 		// amdtp-stream process_rx_packets() attempts to
1864*3dab73abSEdmund Raile 		// acquire same lock by snd_pcm_elapsed()
1865*3dab73abSEdmund Raile 		if (current_work() != &s->period_work)
1866f890f9a0STakashi Sakamoto 			fw_iso_context_flush_completions(irq_target->context);
1867f890f9a0STakashi Sakamoto 	}
1868d67c46b9STakashi Sakamoto 
18696aa7de05SMark Rutland 	return READ_ONCE(s->pcm_buffer_pointer);
1870d67c46b9STakashi Sakamoto }
1871f890f9a0STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_pointer);
1872d67c46b9STakashi Sakamoto 
1873d67c46b9STakashi Sakamoto /**
1874e6dcc92fSTakashi Sakamoto  * amdtp_domain_stream_pcm_ack - acknowledge queued PCM frames
1875e6dcc92fSTakashi Sakamoto  * @d: the AMDTP domain.
1876875becf8STakashi Sakamoto  * @s: the AMDTP stream that transfers the PCM frames
1877875becf8STakashi Sakamoto  *
1878875becf8STakashi Sakamoto  * Returns zero always.
1879875becf8STakashi Sakamoto  */
amdtp_domain_stream_pcm_ack(struct amdtp_domain * d,struct amdtp_stream * s)1880e6dcc92fSTakashi Sakamoto int amdtp_domain_stream_pcm_ack(struct amdtp_domain *d, struct amdtp_stream *s)
1881875becf8STakashi Sakamoto {
1882e6dcc92fSTakashi Sakamoto 	struct amdtp_stream *irq_target = d->irq_target;
1883e6dcc92fSTakashi Sakamoto 
1884e6dcc92fSTakashi Sakamoto 	// Process isochronous packets for recent isochronous cycle to handle
1885e6dcc92fSTakashi Sakamoto 	// queued PCM frames.
1886987b705bSTakashi Sakamoto 	if (irq_target && amdtp_stream_running(irq_target))
1887e6dcc92fSTakashi Sakamoto 		fw_iso_context_flush_completions(irq_target->context);
1888875becf8STakashi Sakamoto 
1889875becf8STakashi Sakamoto 	return 0;
1890875becf8STakashi Sakamoto }
1891e6dcc92fSTakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_ack);
1892875becf8STakashi Sakamoto 
1893875becf8STakashi Sakamoto /**
1894d67c46b9STakashi Sakamoto  * amdtp_stream_update - update the stream after a bus reset
1895d67c46b9STakashi Sakamoto  * @s: the AMDTP stream
1896d67c46b9STakashi Sakamoto  */
amdtp_stream_update(struct amdtp_stream * s)1897d67c46b9STakashi Sakamoto void amdtp_stream_update(struct amdtp_stream *s)
1898d67c46b9STakashi Sakamoto {
1899d67c46b9STakashi Sakamoto 	/* Precomputing. */
19006aa7de05SMark Rutland 	WRITE_ONCE(s->source_node_id_field,
19016aa7de05SMark Rutland                    (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK);
1902d67c46b9STakashi Sakamoto }
1903d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_update);
1904d67c46b9STakashi Sakamoto 
1905d67c46b9STakashi Sakamoto /**
1906d67c46b9STakashi Sakamoto  * amdtp_stream_stop - stop sending packets
1907d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to stop
1908d67c46b9STakashi Sakamoto  *
1909d67c46b9STakashi Sakamoto  * All PCM and MIDI devices of the stream must be stopped before the stream
1910d67c46b9STakashi Sakamoto  * itself can be stopped.
1911d67c46b9STakashi Sakamoto  */
amdtp_stream_stop(struct amdtp_stream * s)191274f94e41STakashi Sakamoto static void amdtp_stream_stop(struct amdtp_stream *s)
1913d67c46b9STakashi Sakamoto {
1914d67c46b9STakashi Sakamoto 	mutex_lock(&s->mutex);
1915d67c46b9STakashi Sakamoto 
1916d67c46b9STakashi Sakamoto 	if (!amdtp_stream_running(s)) {
1917d67c46b9STakashi Sakamoto 		mutex_unlock(&s->mutex);
1918d67c46b9STakashi Sakamoto 		return;
1919d67c46b9STakashi Sakamoto 	}
1920d67c46b9STakashi Sakamoto 
19216ccf9984SEdmund Raile 	cancel_work_sync(&s->period_work);
1922d67c46b9STakashi Sakamoto 	fw_iso_context_stop(s->context);
1923d67c46b9STakashi Sakamoto 	fw_iso_context_destroy(s->context);
1924d67c46b9STakashi Sakamoto 	s->context = ERR_PTR(-1);
1925d67c46b9STakashi Sakamoto 	iso_packets_buffer_destroy(&s->buffer, s->unit);
1926f0117128STakashi Sakamoto 	kfree(s->packet_descs);
1927f0117128STakashi Sakamoto 	s->packet_descs = NULL;
1928d67c46b9STakashi Sakamoto 
1929f9e5ecdfSTakashi Sakamoto 	if (s->direction == AMDTP_OUT_STREAM) {
19306f24bb8aSTakashi Sakamoto 		kfree(s->ctx_data.rx.seq.descs);
1931f9e5ecdfSTakashi Sakamoto 	} else {
1932f9e5ecdfSTakashi Sakamoto 		if (s->domain->replay.enable)
1933f9e5ecdfSTakashi Sakamoto 			kfree(s->ctx_data.tx.cache.descs);
1934f9e5ecdfSTakashi Sakamoto 	}
1935d67c46b9STakashi Sakamoto 
1936d67c46b9STakashi Sakamoto 	mutex_unlock(&s->mutex);
1937d67c46b9STakashi Sakamoto }
1938d67c46b9STakashi Sakamoto 
1939d67c46b9STakashi Sakamoto /**
1940d67c46b9STakashi Sakamoto  * amdtp_stream_pcm_abort - abort the running PCM device
1941d67c46b9STakashi Sakamoto  * @s: the AMDTP stream about to be stopped
1942d67c46b9STakashi Sakamoto  *
1943d67c46b9STakashi Sakamoto  * If the isochronous stream needs to be stopped asynchronously, call this
1944d67c46b9STakashi Sakamoto  * function first to stop the PCM device.
1945d67c46b9STakashi Sakamoto  */
amdtp_stream_pcm_abort(struct amdtp_stream * s)1946d67c46b9STakashi Sakamoto void amdtp_stream_pcm_abort(struct amdtp_stream *s)
1947d67c46b9STakashi Sakamoto {
1948d67c46b9STakashi Sakamoto 	struct snd_pcm_substream *pcm;
1949d67c46b9STakashi Sakamoto 
19506aa7de05SMark Rutland 	pcm = READ_ONCE(s->pcm);
1951d67c46b9STakashi Sakamoto 	if (pcm)
1952d67c46b9STakashi Sakamoto 		snd_pcm_stop_xrun(pcm);
1953d67c46b9STakashi Sakamoto }
1954d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_abort);
19553ec3d7a3STakashi Sakamoto 
19563ec3d7a3STakashi Sakamoto /**
19573ec3d7a3STakashi Sakamoto  * amdtp_domain_init - initialize an AMDTP domain structure
19583ec3d7a3STakashi Sakamoto  * @d: the AMDTP domain to initialize.
19593ec3d7a3STakashi Sakamoto  */
amdtp_domain_init(struct amdtp_domain * d)19603ec3d7a3STakashi Sakamoto int amdtp_domain_init(struct amdtp_domain *d)
19613ec3d7a3STakashi Sakamoto {
19623ec3d7a3STakashi Sakamoto 	INIT_LIST_HEAD(&d->streams);
19633ec3d7a3STakashi Sakamoto 
1964d68c3123STakashi Sakamoto 	d->events_per_period = 0;
1965d68c3123STakashi Sakamoto 
19663ec3d7a3STakashi Sakamoto 	return 0;
19673ec3d7a3STakashi Sakamoto }
19683ec3d7a3STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_init);
19693ec3d7a3STakashi Sakamoto 
19703ec3d7a3STakashi Sakamoto /**
19713ec3d7a3STakashi Sakamoto  * amdtp_domain_destroy - destroy an AMDTP domain structure
19723ec3d7a3STakashi Sakamoto  * @d: the AMDTP domain to destroy.
19733ec3d7a3STakashi Sakamoto  */
amdtp_domain_destroy(struct amdtp_domain * d)19743ec3d7a3STakashi Sakamoto void amdtp_domain_destroy(struct amdtp_domain *d)
19753ec3d7a3STakashi Sakamoto {
19768d0d5c3fSTakashi Sakamoto 	// At present nothing to do.
19778d0d5c3fSTakashi Sakamoto 	return;
19783ec3d7a3STakashi Sakamoto }
19793ec3d7a3STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_destroy);
19806261f90bSTakashi Sakamoto 
19816261f90bSTakashi Sakamoto /**
1982157a53eeSTakashi Sakamoto  * amdtp_domain_add_stream - register isoc context into the domain.
1983157a53eeSTakashi Sakamoto  * @d: the AMDTP domain.
1984157a53eeSTakashi Sakamoto  * @s: the AMDTP stream.
1985157a53eeSTakashi Sakamoto  * @channel: the isochronous channel on the bus.
1986157a53eeSTakashi Sakamoto  * @speed: firewire speed code.
1987157a53eeSTakashi Sakamoto  */
amdtp_domain_add_stream(struct amdtp_domain * d,struct amdtp_stream * s,int channel,int speed)1988157a53eeSTakashi Sakamoto int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s,
1989157a53eeSTakashi Sakamoto 			    int channel, int speed)
1990157a53eeSTakashi Sakamoto {
1991157a53eeSTakashi Sakamoto 	struct amdtp_stream *tmp;
1992157a53eeSTakashi Sakamoto 
1993157a53eeSTakashi Sakamoto 	list_for_each_entry(tmp, &d->streams, list) {
1994157a53eeSTakashi Sakamoto 		if (s == tmp)
1995157a53eeSTakashi Sakamoto 			return -EBUSY;
1996157a53eeSTakashi Sakamoto 	}
1997157a53eeSTakashi Sakamoto 
1998157a53eeSTakashi Sakamoto 	list_add(&s->list, &d->streams);
1999157a53eeSTakashi Sakamoto 
2000157a53eeSTakashi Sakamoto 	s->channel = channel;
2001157a53eeSTakashi Sakamoto 	s->speed = speed;
20022472cfb3STakashi Sakamoto 	s->domain = d;
2003157a53eeSTakashi Sakamoto 
2004157a53eeSTakashi Sakamoto 	return 0;
2005157a53eeSTakashi Sakamoto }
2006157a53eeSTakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_add_stream);
2007157a53eeSTakashi Sakamoto 
200839c2649cSTakashi Sakamoto // Make the reference from rx stream to tx stream for sequence replay. When the number of tx streams
200939c2649cSTakashi Sakamoto // is less than the number of rx streams, the first tx stream is selected.
make_association(struct amdtp_domain * d)201039c2649cSTakashi Sakamoto static int make_association(struct amdtp_domain *d)
201139c2649cSTakashi Sakamoto {
201239c2649cSTakashi Sakamoto 	unsigned int dst_index = 0;
201339c2649cSTakashi Sakamoto 	struct amdtp_stream *rx;
201439c2649cSTakashi Sakamoto 
201539c2649cSTakashi Sakamoto 	// Make association to replay target.
201639c2649cSTakashi Sakamoto 	list_for_each_entry(rx, &d->streams, list) {
201739c2649cSTakashi Sakamoto 		if (rx->direction == AMDTP_OUT_STREAM) {
201839c2649cSTakashi Sakamoto 			unsigned int src_index = 0;
201939c2649cSTakashi Sakamoto 			struct amdtp_stream *tx = NULL;
202039c2649cSTakashi Sakamoto 			struct amdtp_stream *s;
202139c2649cSTakashi Sakamoto 
202239c2649cSTakashi Sakamoto 			list_for_each_entry(s, &d->streams, list) {
202339c2649cSTakashi Sakamoto 				if (s->direction == AMDTP_IN_STREAM) {
202439c2649cSTakashi Sakamoto 					if (dst_index == src_index) {
202539c2649cSTakashi Sakamoto 						tx = s;
202639c2649cSTakashi Sakamoto 						break;
202739c2649cSTakashi Sakamoto 					}
202839c2649cSTakashi Sakamoto 
202939c2649cSTakashi Sakamoto 					++src_index;
203039c2649cSTakashi Sakamoto 				}
203139c2649cSTakashi Sakamoto 			}
203239c2649cSTakashi Sakamoto 			if (!tx) {
203339c2649cSTakashi Sakamoto 				// Select the first entry.
203439c2649cSTakashi Sakamoto 				list_for_each_entry(s, &d->streams, list) {
203539c2649cSTakashi Sakamoto 					if (s->direction == AMDTP_IN_STREAM) {
203639c2649cSTakashi Sakamoto 						tx = s;
203739c2649cSTakashi Sakamoto 						break;
203839c2649cSTakashi Sakamoto 					}
203939c2649cSTakashi Sakamoto 				}
204039c2649cSTakashi Sakamoto 				// No target is available to replay sequence.
204139c2649cSTakashi Sakamoto 				if (!tx)
204239c2649cSTakashi Sakamoto 					return -EINVAL;
204339c2649cSTakashi Sakamoto 			}
204439c2649cSTakashi Sakamoto 
204539c2649cSTakashi Sakamoto 			rx->ctx_data.rx.replay_target = tx;
204639c2649cSTakashi Sakamoto 
204739c2649cSTakashi Sakamoto 			++dst_index;
204839c2649cSTakashi Sakamoto 		}
204939c2649cSTakashi Sakamoto 	}
205039c2649cSTakashi Sakamoto 
205139c2649cSTakashi Sakamoto 	return 0;
205239c2649cSTakashi Sakamoto }
205339c2649cSTakashi Sakamoto 
2054157a53eeSTakashi Sakamoto /**
20559b4702b0STakashi Sakamoto  * amdtp_domain_start - start sending packets for isoc context in the domain.
20569b4702b0STakashi Sakamoto  * @d: the AMDTP domain.
205726541cb1STakashi Sakamoto  * @tx_init_skip_cycles: the number of cycles to skip processing packets at initial stage of IR
205826541cb1STakashi Sakamoto  *			 contexts.
2059f9e5ecdfSTakashi Sakamoto  * @replay_seq: whether to replay the sequence of packet in IR context for the sequence of packet in
2060f9e5ecdfSTakashi Sakamoto  *		IT context.
20612f21a177STakashi Sakamoto  * @replay_on_the_fly: transfer rx packets according to nominal frequency, then begin to replay
20622f21a177STakashi Sakamoto  *		       according to arrival of events in tx packets.
20639b4702b0STakashi Sakamoto  */
amdtp_domain_start(struct amdtp_domain * d,unsigned int tx_init_skip_cycles,bool replay_seq,bool replay_on_the_fly)20642f21a177STakashi Sakamoto int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles, bool replay_seq,
20652f21a177STakashi Sakamoto 		       bool replay_on_the_fly)
20669b4702b0STakashi Sakamoto {
2067af86b0b1STakashi Sakamoto 	unsigned int events_per_buffer = d->events_per_buffer;
2068af86b0b1STakashi Sakamoto 	unsigned int events_per_period = d->events_per_period;
2069af86b0b1STakashi Sakamoto 	unsigned int queue_size;
20709b4702b0STakashi Sakamoto 	struct amdtp_stream *s;
20710cbbeaf3SChristophe JAILLET 	bool found = false;
2072acfedcbeSTakashi Sakamoto 	int err;
20739b4702b0STakashi Sakamoto 
207439c2649cSTakashi Sakamoto 	if (replay_seq) {
207539c2649cSTakashi Sakamoto 		err = make_association(d);
207639c2649cSTakashi Sakamoto 		if (err < 0)
207739c2649cSTakashi Sakamoto 			return err;
207839c2649cSTakashi Sakamoto 	}
2079f9e5ecdfSTakashi Sakamoto 	d->replay.enable = replay_seq;
20802f21a177STakashi Sakamoto 	d->replay.on_the_fly = replay_on_the_fly;
2081f9e5ecdfSTakashi Sakamoto 
208260dd4929STakashi Sakamoto 	// Select an IT context as IRQ target.
20839b4702b0STakashi Sakamoto 	list_for_each_entry(s, &d->streams, list) {
20840cbbeaf3SChristophe JAILLET 		if (s->direction == AMDTP_OUT_STREAM) {
20850cbbeaf3SChristophe JAILLET 			found = true;
20869b4702b0STakashi Sakamoto 			break;
20879b4702b0STakashi Sakamoto 		}
20880cbbeaf3SChristophe JAILLET 	}
20890cbbeaf3SChristophe JAILLET 	if (!found)
209060dd4929STakashi Sakamoto 		return -ENXIO;
209160dd4929STakashi Sakamoto 	d->irq_target = s;
20929b4702b0STakashi Sakamoto 
209326541cb1STakashi Sakamoto 	d->processing_cycle.tx_init_skip = tx_init_skip_cycles;
209426541cb1STakashi Sakamoto 
2095af86b0b1STakashi Sakamoto 	// This is a case that AMDTP streams in domain run just for MIDI
2096af86b0b1STakashi Sakamoto 	// substream. Use the number of events equivalent to 10 msec as
2097af86b0b1STakashi Sakamoto 	// interval of hardware IRQ.
2098af86b0b1STakashi Sakamoto 	if (events_per_period == 0)
2099af86b0b1STakashi Sakamoto 		events_per_period = amdtp_rate_table[d->irq_target->sfc] / 100;
2100af86b0b1STakashi Sakamoto 	if (events_per_buffer == 0)
2101af86b0b1STakashi Sakamoto 		events_per_buffer = events_per_period * 3;
2102af86b0b1STakashi Sakamoto 
2103af86b0b1STakashi Sakamoto 	queue_size = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_buffer,
2104af86b0b1STakashi Sakamoto 				  amdtp_rate_table[d->irq_target->sfc]);
2105af86b0b1STakashi Sakamoto 
210660dd4929STakashi Sakamoto 	list_for_each_entry(s, &d->streams, list) {
2107bd165079STakashi Sakamoto 		unsigned int idle_irq_interval = 0;
2108acfedcbeSTakashi Sakamoto 
2109bd165079STakashi Sakamoto 		if (s->direction == AMDTP_OUT_STREAM && s == d->irq_target) {
2110af86b0b1STakashi Sakamoto 			idle_irq_interval = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_period,
2111af86b0b1STakashi Sakamoto 							 amdtp_rate_table[d->irq_target->sfc]);
2112bd165079STakashi Sakamoto 		}
2113bd165079STakashi Sakamoto 
2114bd165079STakashi Sakamoto 		// Starts immediately but actually DMA context starts several hundred cycles later.
2115bd165079STakashi Sakamoto 		err = amdtp_stream_start(s, s->channel, s->speed, queue_size, idle_irq_interval);
211660dd4929STakashi Sakamoto 		if (err < 0)
211760dd4929STakashi Sakamoto 			goto error;
2118bd165079STakashi Sakamoto 	}
211960dd4929STakashi Sakamoto 
212060dd4929STakashi Sakamoto 	return 0;
212160dd4929STakashi Sakamoto error:
212260dd4929STakashi Sakamoto 	list_for_each_entry(s, &d->streams, list)
212360dd4929STakashi Sakamoto 		amdtp_stream_stop(s);
21249b4702b0STakashi Sakamoto 	return err;
21259b4702b0STakashi Sakamoto }
21269b4702b0STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_start);
21279b4702b0STakashi Sakamoto 
21289b4702b0STakashi Sakamoto /**
21296261f90bSTakashi Sakamoto  * amdtp_domain_stop - stop sending packets for isoc context in the same domain.
21306261f90bSTakashi Sakamoto  * @d: the AMDTP domain to which the isoc contexts belong.
21316261f90bSTakashi Sakamoto  */
amdtp_domain_stop(struct amdtp_domain * d)21326261f90bSTakashi Sakamoto void amdtp_domain_stop(struct amdtp_domain *d)
21336261f90bSTakashi Sakamoto {
21346261f90bSTakashi Sakamoto 	struct amdtp_stream *s, *next;
21356261f90bSTakashi Sakamoto 
213660dd4929STakashi Sakamoto 	if (d->irq_target)
213760dd4929STakashi Sakamoto 		amdtp_stream_stop(d->irq_target);
213860dd4929STakashi Sakamoto 
21396261f90bSTakashi Sakamoto 	list_for_each_entry_safe(s, next, &d->streams, list) {
21406261f90bSTakashi Sakamoto 		list_del(&s->list);
21416261f90bSTakashi Sakamoto 
214260dd4929STakashi Sakamoto 		if (s != d->irq_target)
21436261f90bSTakashi Sakamoto 			amdtp_stream_stop(s);
21446261f90bSTakashi Sakamoto 	}
2145d68c3123STakashi Sakamoto 
2146d68c3123STakashi Sakamoto 	d->events_per_period = 0;
214760dd4929STakashi Sakamoto 	d->irq_target = NULL;
21486261f90bSTakashi Sakamoto }
21496261f90bSTakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_stop);
2150