ALSA: firewire-lib: extend tracepoints event including CYCLE_TIME of 1394 OHCI
[linux-2.6-block.git] / sound / firewire / amdtp-stream.c
CommitLineData
da607e19 1// SPDX-License-Identifier: GPL-2.0-only
31ef9134
CL
2/*
3 * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
4 * with Common Isochronous Packet (IEC 61883-1) headers
5 *
6 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
31ef9134
CL
7 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/firewire.h>
acfedcbe 12#include <linux/firewire-constants.h>
31ef9134
CL
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <sound/pcm.h>
7b2d99fa 16#include <sound/pcm_params.h>
d67c46b9 17#include "amdtp-stream.h"
31ef9134
CL
18
19#define TICKS_PER_CYCLE 3072
20#define CYCLES_PER_SECOND 8000
21#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
22
3e106f4f 23#define OHCI_SECOND_MODULUS 8
10aa8e4a 24
0c95c1d6
TS
25/* Always support Linux tracing subsystem. */
26#define CREATE_TRACE_POINTS
27#include "amdtp-stream-trace.h"
28
ca5b5050 29#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
31ef9134 30
b445db44
TS
31/* isochronous header parameters */
32#define ISO_DATA_LENGTH_SHIFT 16
3b196c39 33#define TAG_NO_CIP_HEADER 0
31ef9134
CL
34#define TAG_CIP 1
35
67d92ee7
TS
36// Common Isochronous Packet (CIP) header parameters. Use two quadlets CIP header when supported.
37#define CIP_HEADER_QUADLETS 2
9a2820c1
TS
38#define CIP_EOH_SHIFT 31
39#define CIP_EOH (1u << CIP_EOH_SHIFT)
b445db44 40#define CIP_EOH_MASK 0x80000000
9a2820c1
TS
41#define CIP_SID_SHIFT 24
42#define CIP_SID_MASK 0x3f000000
43#define CIP_DBS_MASK 0x00ff0000
44#define CIP_DBS_SHIFT 16
9863874f
TS
45#define CIP_SPH_MASK 0x00000400
46#define CIP_SPH_SHIFT 10
9a2820c1
TS
47#define CIP_DBC_MASK 0x000000ff
48#define CIP_FMT_SHIFT 24
b445db44 49#define CIP_FMT_MASK 0x3f000000
9a2820c1
TS
50#define CIP_FDF_MASK 0x00ff0000
51#define CIP_FDF_SHIFT 16
fb25dcc8 52#define CIP_FDF_NO_DATA 0xff
b445db44
TS
53#define CIP_SYT_MASK 0x0000ffff
54#define CIP_SYT_NO_INFO 0xffff
f9e5ecdf 55#define CIP_SYT_CYCLE_MODULUS 16
fb25dcc8 56#define CIP_NO_DATA ((CIP_FDF_NO_DATA << CIP_FDF_SHIFT) | CIP_SYT_NO_INFO)
b445db44 57
67d92ee7
TS
58#define CIP_HEADER_SIZE (sizeof(__be32) * CIP_HEADER_QUADLETS)
59
51c29fd2 60/* Audio and Music transfer protocol specific parameters */
414ba022 61#define CIP_FMT_AM 0x10
2b3fc456 62#define AMDTP_FDF_NO_DATA 0xff
31ef9134 63
f11453c7 64// For iso header and tstamp.
67d92ee7
TS
65#define IR_CTX_HEADER_DEFAULT_QUADLETS 2
66// Add nothing.
67#define IR_CTX_HEADER_SIZE_NO_CIP (sizeof(__be32) * IR_CTX_HEADER_DEFAULT_QUADLETS)
68// Add two quadlets CIP header.
69#define IR_CTX_HEADER_SIZE_CIP (IR_CTX_HEADER_SIZE_NO_CIP + CIP_HEADER_SIZE)
cc4f8e91 70#define HEADER_TSTAMP_MASK 0x0000ffff
4b7da117 71
67d92ee7 72#define IT_PKT_HEADER_SIZE_CIP CIP_HEADER_SIZE
b18f0cfa
TS
73#define IT_PKT_HEADER_SIZE_NO_CIP 0 // Nothing.
74
6a3ce97d
TS
75// The initial firmware of OXFW970 can postpone transmission of packet during finishing
76// asynchronous transaction. This module accepts 5 cycles to skip as maximum to avoid buffer
77// overrun. Actual device can skip more, then this module stops the packet streaming.
78#define IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES 5
79
31ef9134 80/**
be4a2894
TS
81 * amdtp_stream_init - initialize an AMDTP stream structure
82 * @s: the AMDTP stream to initialize
31ef9134 83 * @unit: the target of the stream
3ff7e8f0 84 * @dir: the direction of stream
ffe66bbe 85 * @flags: the details of the streaming protocol consist of cip_flags enumeration-constants.
5955815e 86 * @fmt: the value of fmt field in CIP header
9a738ad1 87 * @process_ctx_payloads: callback handler to process payloads of isoc context
df075fee 88 * @protocol_size: the size to allocate newly for protocol
31ef9134 89 */
be4a2894 90int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
ffe66bbe 91 enum amdtp_stream_direction dir, unsigned int flags,
df075fee 92 unsigned int fmt,
9a738ad1 93 amdtp_stream_process_ctx_payloads_t process_ctx_payloads,
df075fee 94 unsigned int protocol_size)
31ef9134 95{
9a738ad1 96 if (process_ctx_payloads == NULL)
df075fee
TS
97 return -EINVAL;
98
99 s->protocol = kzalloc(protocol_size, GFP_KERNEL);
100 if (!s->protocol)
101 return -ENOMEM;
102
c6f224dc 103 s->unit = unit;
3ff7e8f0 104 s->direction = dir;
31ef9134
CL
105 s->flags = flags;
106 s->context = ERR_PTR(-1);
107 mutex_init(&s->mutex);
ec00f5e4 108 s->packet_index = 0;
31ef9134 109
bdaedca7 110 init_waitqueue_head(&s->ready_wait);
7b3b0d85 111
5955815e 112 s->fmt = fmt;
9a738ad1 113 s->process_ctx_payloads = process_ctx_payloads;
414ba022 114
31ef9134
CL
115 return 0;
116}
be4a2894 117EXPORT_SYMBOL(amdtp_stream_init);
31ef9134
CL
118
119/**
be4a2894
TS
120 * amdtp_stream_destroy - free stream resources
121 * @s: the AMDTP stream to destroy
31ef9134 122 */
be4a2894 123void amdtp_stream_destroy(struct amdtp_stream *s)
31ef9134 124{
44c376b9
TS
125 /* Not initialized. */
126 if (s->protocol == NULL)
127 return;
128
be4a2894 129 WARN_ON(amdtp_stream_running(s));
df075fee 130 kfree(s->protocol);
31ef9134 131 mutex_destroy(&s->mutex);
31ef9134 132}
be4a2894 133EXPORT_SYMBOL(amdtp_stream_destroy);
31ef9134 134
c5280e99 135const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
a7304e3b
CL
136 [CIP_SFC_32000] = 8,
137 [CIP_SFC_44100] = 8,
138 [CIP_SFC_48000] = 8,
139 [CIP_SFC_88200] = 16,
140 [CIP_SFC_96000] = 16,
141 [CIP_SFC_176400] = 32,
142 [CIP_SFC_192000] = 32,
143};
144EXPORT_SYMBOL(amdtp_syt_intervals);
145
f9503a68 146const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
1017abed
TS
147 [CIP_SFC_32000] = 32000,
148 [CIP_SFC_44100] = 44100,
149 [CIP_SFC_48000] = 48000,
150 [CIP_SFC_88200] = 88200,
151 [CIP_SFC_96000] = 96000,
152 [CIP_SFC_176400] = 176400,
153 [CIP_SFC_192000] = 192000,
154};
155EXPORT_SYMBOL(amdtp_rate_table);
156
59502295
TS
157static int apply_constraint_to_size(struct snd_pcm_hw_params *params,
158 struct snd_pcm_hw_rule *rule)
159{
160 struct snd_interval *s = hw_param_interval(params, rule->var);
161 const struct snd_interval *r =
162 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
826b5de9
TS
163 struct snd_interval t = {0};
164 unsigned int step = 0;
59502295
TS
165 int i;
166
167 for (i = 0; i < CIP_SFC_COUNT; ++i) {
826b5de9
TS
168 if (snd_interval_test(r, amdtp_rate_table[i]))
169 step = max(step, amdtp_syt_intervals[i]);
59502295
TS
170 }
171
826b5de9
TS
172 t.min = roundup(s->min, step);
173 t.max = rounddown(s->max, step);
174 t.integer = 1;
59502295
TS
175
176 return snd_interval_refine(s, &t);
177}
178
7b2d99fa
TS
179/**
180 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
181 * @s: the AMDTP stream, which must be initialized.
182 * @runtime: the PCM substream runtime
183 */
184int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
185 struct snd_pcm_runtime *runtime)
186{
55799c5a 187 struct snd_pcm_hardware *hw = &runtime->hw;
99921ec6
TS
188 unsigned int ctx_header_size;
189 unsigned int maximum_usec_per_period;
7b2d99fa
TS
190 int err;
191
d360870a 192 hw->info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
55799c5a
TS
193 SNDRV_PCM_INFO_INTERLEAVED |
194 SNDRV_PCM_INFO_JOINT_DUPLEX |
195 SNDRV_PCM_INFO_MMAP |
d360870a
TS
196 SNDRV_PCM_INFO_MMAP_VALID |
197 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP;
55799c5a 198
55799c5a
TS
199 hw->periods_min = 2;
200 hw->periods_max = UINT_MAX;
201
202 /* bytes for a frame */
203 hw->period_bytes_min = 4 * hw->channels_max;
204
205 /* Just to prevent from allocating much pages. */
206 hw->period_bytes_max = hw->period_bytes_min * 2048;
207 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
208
99921ec6
TS
209 // Linux driver for 1394 OHCI controller voluntarily flushes isoc
210 // context when total size of accumulated context header reaches
2b3d2987 211 // PAGE_SIZE. This kicks work for the isoc context and brings
99921ec6
TS
212 // callback in the middle of scheduled interrupts.
213 // Although AMDTP streams in the same domain use the same events per
214 // IRQ, use the largest size of context header between IT/IR contexts.
215 // Here, use the value of context header in IR context is for both
216 // contexts.
217 if (!(s->flags & CIP_NO_HEADER))
218 ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
219 else
220 ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
221 maximum_usec_per_period = USEC_PER_SEC * PAGE_SIZE /
222 CYCLES_PER_SECOND / ctx_header_size;
223
f706df4f
TS
224 // In IEC 61883-6, one isoc packet can transfer events up to the value
225 // of syt interval. This comes from the interval of isoc cycle. As 1394
226 // OHCI controller can generate hardware IRQ per isoc packet, the
227 // interval is 125 usec.
228 // However, there are two ways of transmission in IEC 61883-6; blocking
229 // and non-blocking modes. In blocking mode, the sequence of isoc packet
230 // includes 'empty' or 'NODATA' packets which include no event. In
231 // non-blocking mode, the number of events per packet is variable up to
232 // the syt interval.
233 // Due to the above protocol design, the minimum PCM frames per
234 // interrupt should be double of the value of syt interval, thus it is
235 // 250 usec.
7b2d99fa
TS
236 err = snd_pcm_hw_constraint_minmax(runtime,
237 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
f706df4f 238 250, maximum_usec_per_period);
7b2d99fa
TS
239 if (err < 0)
240 goto end;
241
242 /* Non-Blocking stream has no more constraints */
243 if (!(s->flags & CIP_BLOCKING))
244 goto end;
245
246 /*
247 * One AMDTP packet can include some frames. In blocking mode, the
248 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
249 * depending on its sampling rate. For accurate period interrupt, it's
ce991981 250 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
7b2d99fa 251 */
59502295
TS
252 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
253 apply_constraint_to_size, NULL,
826b5de9 254 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
59502295
TS
255 SNDRV_PCM_HW_PARAM_RATE, -1);
256 if (err < 0)
257 goto end;
59502295
TS
258 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
259 apply_constraint_to_size, NULL,
826b5de9 260 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
59502295
TS
261 SNDRV_PCM_HW_PARAM_RATE, -1);
262 if (err < 0)
263 goto end;
7b2d99fa
TS
264end:
265 return err;
266}
267EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
268
31ef9134 269/**
be4a2894
TS
270 * amdtp_stream_set_parameters - set stream parameters
271 * @s: the AMDTP stream to configure
31ef9134 272 * @rate: the sample rate
df075fee 273 * @data_block_quadlets: the size of a data block in quadlet unit
31ef9134 274 *
a7304e3b 275 * The parameters must be set before the stream is started, and must not be
31ef9134
CL
276 * changed while the stream is running.
277 */
df075fee
TS
278int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
279 unsigned int data_block_quadlets)
31ef9134 280{
df075fee 281 unsigned int sfc;
31ef9134 282
547e631c 283 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
1017abed 284 if (amdtp_rate_table[sfc] == rate)
547e631c
TS
285 break;
286 }
287 if (sfc == ARRAY_SIZE(amdtp_rate_table))
288 return -EINVAL;
e84d15f6 289
e84d15f6 290 s->sfc = sfc;
df075fee 291 s->data_block_quadlets = data_block_quadlets;
a7304e3b 292 s->syt_interval = amdtp_syt_intervals[sfc];
e84d15f6 293
d3d10a4a 294 // default buffering in the device.
13d11f14
TS
295 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
296
297 // additional buffering needed to adjust for no-data packets.
298 if (s->flags & CIP_BLOCKING)
299 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
77d2a8a4 300
547e631c 301 return 0;
31ef9134 302}
be4a2894 303EXPORT_SYMBOL(amdtp_stream_set_parameters);
31ef9134 304
c75f3678
TS
305// The CIP header is processed in context header apart from context payload.
306static int amdtp_stream_get_max_ctx_payload_size(struct amdtp_stream *s)
307{
308 unsigned int multiplier;
309
310 if (s->flags & CIP_JUMBO_PAYLOAD)
311 multiplier = IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES;
312 else
313 multiplier = 1;
314
315 return s->syt_interval * s->data_block_quadlets * sizeof(__be32) * multiplier;
316}
317
31ef9134 318/**
be4a2894
TS
319 * amdtp_stream_get_max_payload - get the stream's packet size
320 * @s: the AMDTP stream
31ef9134
CL
321 *
322 * This function must not be called before the stream has been configured
be4a2894 323 * with amdtp_stream_set_parameters().
31ef9134 324 */
be4a2894 325unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
31ef9134 326{
c75f3678 327 unsigned int cip_header_size;
a2064710 328
3b196c39 329 if (!(s->flags & CIP_NO_HEADER))
67d92ee7 330 cip_header_size = CIP_HEADER_SIZE;
c75f3678
TS
331 else
332 cip_header_size = 0;
a2064710 333
c75f3678 334 return cip_header_size + amdtp_stream_get_max_ctx_payload_size(s);
31ef9134 335}
be4a2894 336EXPORT_SYMBOL(amdtp_stream_get_max_payload);
31ef9134 337
76fb8789 338/**
be4a2894
TS
339 * amdtp_stream_pcm_prepare - prepare PCM device for running
340 * @s: the AMDTP stream
76fb8789
CL
341 *
342 * This function should be called from the PCM device's .prepare callback.
343 */
be4a2894 344void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
76fb8789 345{
76fb8789
CL
346 s->pcm_buffer_pointer = 0;
347 s->pcm_period_pointer = 0;
348}
be4a2894 349EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
76fb8789 350
c9f3ac2a 351static void pool_blocking_data_blocks(struct amdtp_stream *s, struct seq_desc *descs,
119c446a 352 unsigned int size, unsigned int pos, unsigned int count)
31ef9134 353{
c9f3ac2a
TS
354 const unsigned int syt_interval = s->syt_interval;
355 int i;
31ef9134 356
c9f3ac2a 357 for (i = 0; i < count; ++i) {
119c446a 358 struct seq_desc *desc = descs + pos;
c9f3ac2a
TS
359
360 if (desc->syt_offset != CIP_SYT_NO_INFO)
361 desc->data_blocks = syt_interval;
875be091 362 else
c9f3ac2a
TS
363 desc->data_blocks = 0;
364
119c446a 365 pos = (pos + 1) % size;
c9f3ac2a
TS
366 }
367}
368
369static void pool_ideal_nonblocking_data_blocks(struct amdtp_stream *s, struct seq_desc *descs,
119c446a 370 unsigned int size, unsigned int pos,
c9f3ac2a
TS
371 unsigned int count)
372{
373 const enum cip_sfc sfc = s->sfc;
374 unsigned int state = s->ctx_data.rx.data_block_state;
375 int i;
376
377 for (i = 0; i < count; ++i) {
119c446a 378 struct seq_desc *desc = descs + pos;
c9f3ac2a 379
274fc355 380 if (!cip_sfc_is_base_44100(sfc)) {
d3d10a4a 381 // Sample_rate / 8000 is an integer, and precomputed.
c9f3ac2a 382 desc->data_blocks = state;
875be091 383 } else {
c9f3ac2a 384 unsigned int phase = state;
31ef9134
CL
385
386 /*
387 * This calculates the number of data blocks per packet so that
388 * 1) the overall rate is correct and exactly synchronized to
389 * the bus clock, and
390 * 2) packets with a rounded-up number of blocks occur as early
391 * as possible in the sequence (to prevent underruns of the
392 * device's buffer).
393 */
274fc355 394 if (sfc == CIP_SFC_44100)
875be091 395 /* 6 6 5 6 5 6 5 ... */
c9f3ac2a 396 desc->data_blocks = 5 + ((phase & 1) ^ (phase == 0 || phase >= 40));
875be091
TS
397 else
398 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
c9f3ac2a 399 desc->data_blocks = 11 * (sfc >> 1) + (phase == 0);
274fc355 400 if (++phase >= (80 >> (sfc >> 1)))
875be091 401 phase = 0;
c9f3ac2a 402 state = phase;
875be091 403 }
c9f3ac2a 404
119c446a 405 pos = (pos + 1) % size;
31ef9134
CL
406 }
407
c9f3ac2a 408 s->ctx_data.rx.data_block_state = state;
31ef9134
CL
409}
410
816d8482
TS
411static unsigned int calculate_syt_offset(unsigned int *last_syt_offset,
412 unsigned int *syt_offset_state, enum cip_sfc sfc)
31ef9134 413{
816d8482 414 unsigned int syt_offset;
31ef9134 415
816d8482
TS
416 if (*last_syt_offset < TICKS_PER_CYCLE) {
417 if (!cip_sfc_is_base_44100(sfc))
418 syt_offset = *last_syt_offset + *syt_offset_state;
31ef9134
CL
419 else {
420 /*
421 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
422 * n * SYT_INTERVAL * 24576000 / sample_rate
423 * Modulo TICKS_PER_CYCLE, the difference between successive
424 * elements is about 1386.23. Rounding the results of this
425 * formula to the SYT precision results in a sequence of
426 * differences that begins with:
427 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
428 * This code generates _exactly_ the same sequence.
429 */
816d8482
TS
430 unsigned int phase = *syt_offset_state;
431 unsigned int index = phase % 13;
432
433 syt_offset = *last_syt_offset;
31ef9134
CL
434 syt_offset += 1386 + ((index && !(index & 3)) ||
435 phase == 146);
436 if (++phase >= 147)
437 phase = 0;
816d8482 438 *syt_offset_state = phase;
31ef9134
CL
439 }
440 } else
816d8482
TS
441 syt_offset = *last_syt_offset - TICKS_PER_CYCLE;
442 *last_syt_offset = syt_offset;
31ef9134 443
83cfb5c5
TS
444 if (syt_offset >= TICKS_PER_CYCLE)
445 syt_offset = CIP_SYT_NO_INFO;
31ef9134 446
83cfb5c5 447 return syt_offset;
31ef9134
CL
448}
449
c79b7158 450static void pool_ideal_syt_offsets(struct amdtp_stream *s, struct seq_desc *descs,
119c446a 451 unsigned int size, unsigned int pos, unsigned int count)
c79b7158
TS
452{
453 const enum cip_sfc sfc = s->sfc;
454 unsigned int last = s->ctx_data.rx.last_syt_offset;
455 unsigned int state = s->ctx_data.rx.syt_offset_state;
456 int i;
457
458 for (i = 0; i < count; ++i) {
119c446a 459 struct seq_desc *desc = descs + pos;
c79b7158
TS
460
461 desc->syt_offset = calculate_syt_offset(&last, &state, sfc);
462
119c446a 463 pos = (pos + 1) % size;
c79b7158
TS
464 }
465
466 s->ctx_data.rx.last_syt_offset = last;
467 s->ctx_data.rx.syt_offset_state = state;
468}
469
f9e5ecdf
TS
470static unsigned int compute_syt_offset(unsigned int syt, unsigned int cycle,
471 unsigned int transfer_delay)
472{
473 unsigned int cycle_lo = (cycle % CYCLES_PER_SECOND) & 0x0f;
474 unsigned int syt_cycle_lo = (syt & 0xf000) >> 12;
475 unsigned int syt_offset;
476
477 // Round up.
478 if (syt_cycle_lo < cycle_lo)
479 syt_cycle_lo += CIP_SYT_CYCLE_MODULUS;
480 syt_cycle_lo -= cycle_lo;
481
482 // Subtract transfer delay so that the synchronization offset is not so large
483 // at transmission.
484 syt_offset = syt_cycle_lo * TICKS_PER_CYCLE + (syt & 0x0fff);
485 if (syt_offset < transfer_delay)
486 syt_offset += CIP_SYT_CYCLE_MODULUS * TICKS_PER_CYCLE;
487
488 return syt_offset - transfer_delay;
489}
490
39c2649c
TS
491// Both of the producer and consumer of the queue runs in the same clock of IEEE 1394 bus.
492// Additionally, the sequence of tx packets is severely checked against any discontinuity
493// before filling entries in the queue. The calculation is safe even if it looks fragile by
494// overrun.
495static unsigned int calculate_cached_cycle_count(struct amdtp_stream *s, unsigned int head)
496{
497 const unsigned int cache_size = s->ctx_data.tx.cache.size;
cccddec4 498 unsigned int cycles = s->ctx_data.tx.cache.pos;
39c2649c
TS
499
500 if (cycles < head)
501 cycles += cache_size;
502 cycles -= head;
503
504 return cycles;
505}
506
cec371ff 507static void cache_seq(struct amdtp_stream *s, const struct pkt_desc *src, unsigned int desc_count)
f9e5ecdf
TS
508{
509 const unsigned int transfer_delay = s->transfer_delay;
510 const unsigned int cache_size = s->ctx_data.tx.cache.size;
511 struct seq_desc *cache = s->ctx_data.tx.cache.descs;
cccddec4 512 unsigned int cache_pos = s->ctx_data.tx.cache.pos;
f9e5ecdf
TS
513 bool aware_syt = !(s->flags & CIP_UNAWARE_SYT);
514 int i;
515
516 for (i = 0; i < desc_count; ++i) {
cccddec4 517 struct seq_desc *dst = cache + cache_pos;
f9e5ecdf
TS
518
519 if (aware_syt && src->syt != CIP_SYT_NO_INFO)
520 dst->syt_offset = compute_syt_offset(src->syt, src->cycle, transfer_delay);
521 else
522 dst->syt_offset = CIP_SYT_NO_INFO;
523 dst->data_blocks = src->data_blocks;
524
cccddec4 525 cache_pos = (cache_pos + 1) % cache_size;
cec371ff 526 src = amdtp_stream_next_packet_desc(s, src);
f9e5ecdf
TS
527 }
528
cccddec4 529 s->ctx_data.tx.cache.pos = cache_pos;
f9e5ecdf
TS
530}
531
119c446a
TS
532static void pool_ideal_seq_descs(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
533 unsigned int pos, unsigned int count)
6f24bb8a 534{
119c446a 535 pool_ideal_syt_offsets(s, descs, size, pos, count);
c79b7158 536
c9f3ac2a 537 if (s->flags & CIP_BLOCKING)
119c446a 538 pool_blocking_data_blocks(s, descs, size, pos, count);
c9f3ac2a 539 else
119c446a 540 pool_ideal_nonblocking_data_blocks(s, descs, size, pos, count);
6f24bb8a
TS
541}
542
119c446a
TS
543static void pool_replayed_seq(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
544 unsigned int pos, unsigned int count)
39c2649c
TS
545{
546 struct amdtp_stream *target = s->ctx_data.rx.replay_target;
547 const struct seq_desc *cache = target->ctx_data.tx.cache.descs;
548 const unsigned int cache_size = target->ctx_data.tx.cache.size;
c38d8cff 549 unsigned int cache_pos = s->ctx_data.rx.cache_pos;
39c2649c
TS
550 int i;
551
552 for (i = 0; i < count; ++i) {
c38d8cff
TS
553 descs[pos] = cache[cache_pos];
554 cache_pos = (cache_pos + 1) % cache_size;
119c446a 555 pos = (pos + 1) % size;
39c2649c
TS
556 }
557
c38d8cff 558 s->ctx_data.rx.cache_pos = cache_pos;
39c2649c
TS
559}
560
f2bdee85
TS
561static void pool_seq_descs(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
562 unsigned int pos, unsigned int count)
39c2649c
TS
563{
564 struct amdtp_domain *d = s->domain;
119c446a
TS
565 void (*pool_seq_descs)(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
566 unsigned int pos, unsigned int count);
39c2649c 567
2f21a177 568 if (!d->replay.enable || !s->ctx_data.rx.replay_target) {
119c446a 569 pool_seq_descs = pool_ideal_seq_descs;
2f21a177
TS
570 } else {
571 if (!d->replay.on_the_fly) {
119c446a 572 pool_seq_descs = pool_replayed_seq;
2f21a177
TS
573 } else {
574 struct amdtp_stream *tx = s->ctx_data.rx.replay_target;
575 const unsigned int cache_size = tx->ctx_data.tx.cache.size;
c38d8cff
TS
576 const unsigned int cache_pos = s->ctx_data.rx.cache_pos;
577 unsigned int cached_cycles = calculate_cached_cycle_count(tx, cache_pos);
2f21a177
TS
578
579 if (cached_cycles > count && cached_cycles > cache_size / 2)
119c446a 580 pool_seq_descs = pool_replayed_seq;
2f21a177 581 else
119c446a 582 pool_seq_descs = pool_ideal_seq_descs;
2f21a177
TS
583 }
584 }
119c446a
TS
585
586 pool_seq_descs(s, descs, size, pos, count);
39c2649c
TS
587}
588
4b7da117
TS
589static void update_pcm_pointers(struct amdtp_stream *s,
590 struct snd_pcm_substream *pcm,
591 unsigned int frames)
65845f29
TS
592{
593 unsigned int ptr;
594
4b7da117
TS
595 ptr = s->pcm_buffer_pointer + frames;
596 if (ptr >= pcm->runtime->buffer_size)
597 ptr -= pcm->runtime->buffer_size;
6aa7de05 598 WRITE_ONCE(s->pcm_buffer_pointer, ptr);
4b7da117
TS
599
600 s->pcm_period_pointer += frames;
601 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
602 s->pcm_period_pointer -= pcm->runtime->period_size;
d360870a
TS
603
604 // The program in user process should periodically check the status of intermediate
605 // buffer associated to PCM substream to process PCM frames in the buffer, instead
606 // of receiving notification of period elapsed by poll wait.
7ba5ca32 607 if (!pcm->runtime->no_period_wakeup) {
3b86ec63 608 if (in_softirq()) {
7ba5ca32
TS
609 // In software IRQ context for 1394 OHCI.
610 snd_pcm_period_elapsed(pcm);
611 } else {
612 // In process context of ALSA PCM application under acquired lock of
613 // PCM substream.
614 snd_pcm_period_elapsed_under_stream_lock(pcm);
615 }
616 }
4b7da117
TS
617 }
618}
619
e229853d
TS
620static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params,
621 bool sched_irq)
4b7da117 622{
6007bf54 623 int err;
df9160b9 624
e229853d 625 params->interrupt = sched_irq;
6007bf54
TS
626 params->tag = s->tag;
627 params->sy = 0;
df9160b9 628
6007bf54 629 err = fw_iso_context_queue(s->context, params, &s->buffer.iso_buffer,
4b7da117
TS
630 s->buffer.packets[s->packet_index].offset);
631 if (err < 0) {
632 dev_err(&s->unit->device, "queueing error: %d\n", err);
633 goto end;
634 }
635
a0e02331 636 if (++s->packet_index >= s->queue_size)
4b7da117
TS
637 s->packet_index = 0;
638end:
639 return err;
640}
641
642static inline int queue_out_packet(struct amdtp_stream *s,
e229853d 643 struct fw_iso_packet *params, bool sched_irq)
4b7da117 644{
b18f0cfa
TS
645 params->skip =
646 !!(params->header_length == 0 && params->payload_length == 0);
e229853d 647 return queue_packet(s, params, sched_irq);
4b7da117
TS
648}
649
6007bf54 650static inline int queue_in_packet(struct amdtp_stream *s,
60dd4929 651 struct fw_iso_packet *params)
2b3fc456 652{
6007bf54
TS
653 // Queue one packet for IR context.
654 params->header_length = s->ctx_data.tx.ctx_header_size;
655 params->payload_length = s->ctx_data.tx.max_ctx_payload_length;
656 params->skip = false;
60dd4929 657 return queue_packet(s, params, false);
2b3fc456
TS
658}
659
252219c7 660static void generate_cip_header(struct amdtp_stream *s, __be32 cip_header[2],
860d798c 661 unsigned int data_block_counter, unsigned int syt)
252219c7
TS
662{
663 cip_header[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) |
664 (s->data_block_quadlets << CIP_DBS_SHIFT) |
665 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
860d798c 666 data_block_counter);
252219c7
TS
667 cip_header[1] = cpu_to_be32(CIP_EOH |
668 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
669 ((s->ctx_data.rx.fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
670 (syt & CIP_SYT_MASK));
671}
672
6bc1a269 673static void build_it_pkt_header(struct amdtp_stream *s, unsigned int cycle,
233dbbc7 674 struct fw_iso_packet *params, unsigned int header_length,
860d798c
TS
675 unsigned int data_blocks,
676 unsigned int data_block_counter,
fef4e61b 677 unsigned int syt, unsigned int index, u32 curr_cycle_time)
31ef9134 678{
0ebf3ceb 679 unsigned int payload_length;
16be4589 680 __be32 *cip_header;
20e44577 681
0ebf3ceb
TS
682 payload_length = data_blocks * sizeof(__be32) * s->data_block_quadlets;
683 params->payload_length = payload_length;
684
233dbbc7 685 if (header_length > 0) {
6bc1a269 686 cip_header = (__be32 *)params->header;
860d798c 687 generate_cip_header(s, cip_header, data_block_counter, syt);
233dbbc7 688 params->header_length = header_length;
b18f0cfa
TS
689 } else {
690 cip_header = NULL;
691 }
31ef9134 692
233dbbc7 693 trace_amdtp_packet(s, cycle, cip_header, payload_length + header_length, data_blocks,
fef4e61b 694 data_block_counter, s->packet_index, index, curr_cycle_time);
3b196c39
TS
695}
696
e335425b
TS
697static int check_cip_header(struct amdtp_stream *s, const __be32 *buf,
698 unsigned int payload_length,
a35463d1
TS
699 unsigned int *data_blocks,
700 unsigned int *data_block_counter, unsigned int *syt)
2b3fc456
TS
701{
702 u32 cip_header[2];
e335425b
TS
703 unsigned int sph;
704 unsigned int fmt;
705 unsigned int fdf;
a35463d1 706 unsigned int dbc;
c8bdf49b 707 bool lost;
2b3fc456 708
e335425b
TS
709 cip_header[0] = be32_to_cpu(buf[0]);
710 cip_header[1] = be32_to_cpu(buf[1]);
2b3fc456
TS
711
712 /*
713 * This module supports 'Two-quadlet CIP header with SYT field'.
77d2a8a4 714 * For convenience, also check FMT field is AM824 or not.
2b3fc456 715 */
2128f78f
TS
716 if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
717 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
718 (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
2b3fc456
TS
719 dev_info_ratelimited(&s->unit->device,
720 "Invalid CIP header for AMDTP: %08X:%08X\n",
721 cip_header[0], cip_header[1]);
e335425b 722 return -EAGAIN;
2b3fc456
TS
723 }
724
414ba022 725 /* Check valid protocol or not. */
9863874f 726 sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
414ba022 727 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
9863874f 728 if (sph != s->sph || fmt != s->fmt) {
2a7e1713
TS
729 dev_info_ratelimited(&s->unit->device,
730 "Detect unexpected protocol: %08x %08x\n",
731 cip_header[0], cip_header[1]);
e335425b 732 return -EAGAIN;
414ba022
TS
733 }
734
2b3fc456 735 /* Calculate data blocks */
414ba022 736 fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
4fd18787 737 if (payload_length == 0 || (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
e335425b 738 *data_blocks = 0;
2b3fc456 739 } else {
e335425b
TS
740 unsigned int data_block_quadlets =
741 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
2b3fc456
TS
742 /* avoid division by zero */
743 if (data_block_quadlets == 0) {
12e0f438 744 dev_err(&s->unit->device,
2b3fc456
TS
745 "Detect invalid value in dbs field: %08X\n",
746 cip_header[0]);
a9007054 747 return -EPROTO;
2b3fc456 748 }
69702239
TS
749 if (s->flags & CIP_WRONG_DBS)
750 data_block_quadlets = s->data_block_quadlets;
2b3fc456 751
4fd18787 752 *data_blocks = payload_length / sizeof(__be32) / data_block_quadlets;
2b3fc456
TS
753 }
754
755 /* Check data block counter continuity */
a35463d1 756 dbc = cip_header[0] & CIP_DBC_MASK;
e335425b 757 if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
a35463d1
TS
758 *data_block_counter != UINT_MAX)
759 dbc = *data_block_counter;
9d59124c 760
a35463d1
TS
761 if ((dbc == 0x00 && (s->flags & CIP_SKIP_DBC_ZERO_CHECK)) ||
762 *data_block_counter == UINT_MAX) {
b84b1a27
TS
763 lost = false;
764 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
a35463d1 765 lost = dbc != *data_block_counter;
d9cd0065 766 } else {
e335425b
TS
767 unsigned int dbc_interval;
768
769 if (*data_blocks > 0 && s->ctx_data.tx.dbc_interval > 0)
d3d10a4a 770 dbc_interval = s->ctx_data.tx.dbc_interval;
d9cd0065 771 else
e335425b 772 dbc_interval = *data_blocks;
d9cd0065 773
a35463d1 774 lost = dbc != ((*data_block_counter + dbc_interval) & 0xff);
d9cd0065 775 }
c8bdf49b
TS
776
777 if (lost) {
12e0f438
TS
778 dev_err(&s->unit->device,
779 "Detect discontinuity of CIP: %02X %02X\n",
a35463d1 780 *data_block_counter, dbc);
6fc6b9ce 781 return -EIO;
2b3fc456
TS
782 }
783
753e7179
TS
784 *data_block_counter = dbc;
785
8070d265
TS
786 if (!(s->flags & CIP_UNAWARE_SYT))
787 *syt = cip_header[1] & CIP_SYT_MASK;
2b3fc456 788
e335425b
TS
789 return 0;
790}
791
98e3e43b
TS
792static int parse_ir_ctx_header(struct amdtp_stream *s, unsigned int cycle,
793 const __be32 *ctx_header,
a35463d1
TS
794 unsigned int *data_blocks,
795 unsigned int *data_block_counter,
fef4e61b
TS
796 unsigned int *syt, unsigned int packet_index, unsigned int index,
797 u32 curr_cycle_time)
e335425b 798{
ebd2a647 799 unsigned int payload_length;
f11453c7 800 const __be32 *cip_header;
395f41e2 801 unsigned int cip_header_size;
e335425b 802
ebd2a647 803 payload_length = be32_to_cpu(ctx_header[0]) >> ISO_DATA_LENGTH_SHIFT;
395f41e2
TS
804
805 if (!(s->flags & CIP_NO_HEADER))
67d92ee7 806 cip_header_size = CIP_HEADER_SIZE;
395f41e2
TS
807 else
808 cip_header_size = 0;
809
ebd2a647 810 if (payload_length > cip_header_size + s->ctx_data.tx.max_ctx_payload_length) {
e335425b
TS
811 dev_err(&s->unit->device,
812 "Detect jumbo payload: %04x %04x\n",
ebd2a647 813 payload_length, cip_header_size + s->ctx_data.tx.max_ctx_payload_length);
e335425b
TS
814 return -EIO;
815 }
816
395f41e2 817 if (cip_header_size > 0) {
ebd2a647 818 if (payload_length >= cip_header_size) {
344f0f82
TS
819 int err;
820
67d92ee7 821 cip_header = ctx_header + IR_CTX_HEADER_DEFAULT_QUADLETS;
4fd18787
TS
822 err = check_cip_header(s, cip_header, payload_length - cip_header_size,
823 data_blocks, data_block_counter, syt);
c09010ee
TS
824 if (err < 0)
825 return err;
826 } else {
827 // Handle the cycle so that empty packet arrives.
828 cip_header = NULL;
829 *data_blocks = 0;
830 *syt = 0;
831 }
947b437e
TS
832 } else {
833 cip_header = NULL;
ebd2a647 834 *data_blocks = payload_length / sizeof(__be32) / s->data_block_quadlets;
98e3e43b 835 *syt = 0;
7fbf9096 836
a35463d1
TS
837 if (*data_block_counter == UINT_MAX)
838 *data_block_counter = 0;
e335425b
TS
839 }
840
ebd2a647 841 trace_amdtp_packet(s, cycle, cip_header, payload_length, *data_blocks,
fef4e61b 842 *data_block_counter, packet_index, index, curr_cycle_time);
e335425b 843
344f0f82 844 return 0;
2b3fc456
TS
845}
846
26cd1e58
TS
847// In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
848// the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
849// it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
3e106f4f 850static inline u32 compute_ohci_cycle_count(__be32 ctx_header_tstamp)
73fc7f08 851{
26cd1e58 852 u32 tstamp = be32_to_cpu(ctx_header_tstamp) & HEADER_TSTAMP_MASK;
73fc7f08
TS
853 return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
854}
855
3e106f4f 856static inline u32 increment_ohci_cycle_count(u32 cycle, unsigned int addend)
73fc7f08
TS
857{
858 cycle += addend;
3e106f4f
TS
859 if (cycle >= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND)
860 cycle -= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND;
73fc7f08
TS
861 return cycle;
862}
863
705794c5
TS
864static int compare_ohci_cycle_count(u32 lval, u32 rval)
865{
866 if (lval == rval)
867 return 0;
868 else if (lval < rval && rval - lval < OHCI_SECOND_MODULUS * CYCLES_PER_SECOND / 2)
869 return -1;
870 else
871 return 1;
872}
873
26cd1e58 874// Align to actual cycle count for the packet which is going to be scheduled.
a0e02331
TS
875// This module queued the same number of isochronous cycle as the size of queue
876// to kip isochronous cycle, therefore it's OK to just increment the cycle by
877// the size of queue for scheduled cycle.
3e106f4f
TS
878static inline u32 compute_ohci_it_cycle(const __be32 ctx_header_tstamp,
879 unsigned int queue_size)
26cd1e58 880{
3e106f4f
TS
881 u32 cycle = compute_ohci_cycle_count(ctx_header_tstamp);
882 return increment_ohci_cycle_count(cycle, queue_size);
26cd1e58
TS
883}
884
cec371ff 885static int generate_tx_packet_descs(struct amdtp_stream *s, struct pkt_desc *desc,
cccddec4
TS
886 const __be32 *ctx_header, unsigned int packet_count,
887 unsigned int *desc_count)
753e7179 888{
705794c5 889 unsigned int next_cycle = s->next_cycle;
753e7179 890 unsigned int dbc = s->data_block_counter;
814b4312
TS
891 unsigned int packet_index = s->packet_index;
892 unsigned int queue_size = s->queue_size;
fef4e61b 893 u32 curr_cycle_time;
753e7179
TS
894 int i;
895 int err;
896
fef4e61b
TS
897 if (trace_amdtp_packet_enabled())
898 (void)fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &curr_cycle_time);
899
73246fc4 900 *desc_count = 0;
cccddec4 901 for (i = 0; i < packet_count; ++i) {
753e7179 902 unsigned int cycle;
705794c5 903 bool lost;
753e7179
TS
904 unsigned int data_blocks;
905 unsigned int syt;
906
3e106f4f 907 cycle = compute_ohci_cycle_count(ctx_header[1]);
705794c5
TS
908 lost = (next_cycle != cycle);
909 if (lost) {
910 if (s->flags & CIP_NO_HEADER) {
911 // Fireface skips transmission just for an isoc cycle corresponding
912 // to empty packet.
73246fc4
TS
913 unsigned int prev_cycle = next_cycle;
914
705794c5
TS
915 next_cycle = increment_ohci_cycle_count(next_cycle, 1);
916 lost = (next_cycle != cycle);
73246fc4
TS
917 if (!lost) {
918 // Prepare a description for the skipped cycle for
919 // sequence replay.
920 desc->cycle = prev_cycle;
921 desc->syt = 0;
922 desc->data_blocks = 0;
923 desc->data_block_counter = dbc;
924 desc->ctx_payload = NULL;
cec371ff 925 desc = amdtp_stream_next_packet_desc(s, desc);
73246fc4
TS
926 ++(*desc_count);
927 }
705794c5
TS
928 } else if (s->flags & CIP_JUMBO_PAYLOAD) {
929 // OXFW970 skips transmission for several isoc cycles during
73246fc4
TS
930 // asynchronous transaction. The sequence replay is impossible due
931 // to the reason.
705794c5
TS
932 unsigned int safe_cycle = increment_ohci_cycle_count(next_cycle,
933 IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES);
934 lost = (compare_ohci_cycle_count(safe_cycle, cycle) > 0);
935 }
936 if (lost) {
937 dev_err(&s->unit->device, "Detect discontinuity of cycle: %d %d\n",
938 next_cycle, cycle);
939 return -EIO;
940 }
941 }
753e7179 942
ebd2a647 943 err = parse_ir_ctx_header(s, cycle, ctx_header, &data_blocks, &dbc, &syt,
fef4e61b 944 packet_index, i, curr_cycle_time);
753e7179
TS
945 if (err < 0)
946 return err;
947
948 desc->cycle = cycle;
949 desc->syt = syt;
950 desc->data_blocks = data_blocks;
951 desc->data_block_counter = dbc;
814b4312 952 desc->ctx_payload = s->buffer.packets[packet_index].buffer;
753e7179
TS
953
954 if (!(s->flags & CIP_DBC_IS_END_EVENT))
955 dbc = (dbc + desc->data_blocks) & 0xff;
956
705794c5 957 next_cycle = increment_ohci_cycle_count(next_cycle, 1);
cec371ff 958 desc = amdtp_stream_next_packet_desc(s, desc);
73246fc4 959 ++(*desc_count);
705794c5 960 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header);
814b4312 961 packet_index = (packet_index + 1) % queue_size;
753e7179
TS
962 }
963
705794c5 964 s->next_cycle = next_cycle;
753e7179
TS
965 s->data_block_counter = dbc;
966
967 return 0;
968}
969
83cfb5c5
TS
970static unsigned int compute_syt(unsigned int syt_offset, unsigned int cycle,
971 unsigned int transfer_delay)
972{
973 unsigned int syt;
974
975 syt_offset += transfer_delay;
976 syt = ((cycle + syt_offset / TICKS_PER_CYCLE) << 12) |
977 (syt_offset % TICKS_PER_CYCLE);
978 return syt & CIP_SYT_MASK;
979}
980
cec371ff 981static void generate_rx_packet_descs(struct amdtp_stream *s, struct pkt_desc *desc,
f2bdee85 982 const __be32 *ctx_header, unsigned int packet_count)
f4f6ae7b 983{
f2bdee85
TS
984 struct seq_desc *seq_descs = s->ctx_data.rx.seq.descs;
985 unsigned int seq_size = s->ctx_data.rx.seq.size;
986 unsigned int seq_pos = s->ctx_data.rx.seq.pos;
f4f6ae7b 987 unsigned int dbc = s->data_block_counter;
8070d265 988 bool aware_syt = !(s->flags & CIP_UNAWARE_SYT);
f4f6ae7b
TS
989 int i;
990
f2bdee85
TS
991 pool_seq_descs(s, seq_descs, seq_size, seq_pos, packet_count);
992
993 for (i = 0; i < packet_count; ++i) {
a0e02331 994 unsigned int index = (s->packet_index + i) % s->queue_size;
f2bdee85 995 const struct seq_desc *seq = seq_descs + seq_pos;
f4f6ae7b 996
3e106f4f 997 desc->cycle = compute_ohci_it_cycle(*ctx_header, s->queue_size);
69efd5c4 998
13d11f14
TS
999 if (aware_syt && seq->syt_offset != CIP_SYT_NO_INFO)
1000 desc->syt = compute_syt(seq->syt_offset, desc->cycle, s->transfer_delay);
1001 else
8070d265 1002 desc->syt = CIP_SYT_NO_INFO;
8070d265 1003
69efd5c4 1004 desc->data_blocks = seq->data_blocks;
f4f6ae7b
TS
1005
1006 if (s->flags & CIP_DBC_IS_END_EVENT)
1007 dbc = (dbc + desc->data_blocks) & 0xff;
1008
1009 desc->data_block_counter = dbc;
1010
1011 if (!(s->flags & CIP_DBC_IS_END_EVENT))
1012 dbc = (dbc + desc->data_blocks) & 0xff;
1013
1014 desc->ctx_payload = s->buffer.packets[index].buffer;
1015
f2bdee85 1016 seq_pos = (seq_pos + 1) % seq_size;
cec371ff 1017 desc = amdtp_stream_next_packet_desc(s, desc);
69efd5c4 1018
f4f6ae7b
TS
1019 ++ctx_header;
1020 }
1021
1022 s->data_block_counter = dbc;
f2bdee85 1023 s->ctx_data.rx.seq.pos = seq_pos;
f4f6ae7b
TS
1024}
1025
fce9b013
TS
1026static inline void cancel_stream(struct amdtp_stream *s)
1027{
1028 s->packet_index = -1;
3b86ec63 1029 if (in_softirq())
fce9b013
TS
1030 amdtp_stream_pcm_abort(s);
1031 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
1032}
1033
0f5cfcb2
TS
1034static void process_ctx_payloads(struct amdtp_stream *s,
1035 const struct pkt_desc *descs,
0cac60c7 1036 unsigned int count)
31ef9134 1037{
9a738ad1
TS
1038 struct snd_pcm_substream *pcm;
1039 unsigned int pcm_frames;
5e2ece0f 1040
9a738ad1 1041 pcm = READ_ONCE(s->pcm);
0cac60c7 1042 pcm_frames = s->process_ctx_payloads(s, descs, count, pcm);
9a738ad1
TS
1043 if (pcm)
1044 update_pcm_pointers(s, pcm, pcm_frames);
0f5cfcb2
TS
1045}
1046
9b1fcd9b
TS
1047static void process_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1048 void *header, void *private_data)
0f5cfcb2
TS
1049{
1050 struct amdtp_stream *s = private_data;
69efd5c4 1051 const struct amdtp_domain *d = s->domain;
0f5cfcb2 1052 const __be32 *ctx_header = header;
9b1fcd9b 1053 const unsigned int events_per_period = d->events_per_period;
60dd4929 1054 unsigned int event_count = s->ctx_data.rx.event_count;
f0117128 1055 struct pkt_desc *desc = s->packet_descs_cursor;
233dbbc7 1056 unsigned int pkt_header_length;
a0e02331 1057 unsigned int packets;
fef4e61b 1058 u32 curr_cycle_time;
d360870a 1059 bool need_hw_irq;
0f5cfcb2
TS
1060 int i;
1061
1062 if (s->packet_index < 0)
1063 return;
1064
a0e02331
TS
1065 // Calculate the number of packets in buffer and check XRUN.
1066 packets = header_length / sizeof(*ctx_header);
1067
cec371ff 1068 generate_rx_packet_descs(s, desc, ctx_header, packets);
0f5cfcb2 1069
cec371ff 1070 process_ctx_payloads(s, desc, packets);
5e2ece0f 1071
233dbbc7
TS
1072 if (!(s->flags & CIP_NO_HEADER))
1073 pkt_header_length = IT_PKT_HEADER_SIZE_CIP;
1074 else
1075 pkt_header_length = 0;
1076
d360870a
TS
1077 if (s == d->irq_target) {
1078 // At NO_PERIOD_WAKEUP mode, the packets for all IT/IR contexts are processed by
1079 // the tasks of user process operating ALSA PCM character device by calling ioctl(2)
1080 // with some requests, instead of scheduled hardware IRQ of an IT context.
1081 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
1082 need_hw_irq = !pcm || !pcm->runtime->no_period_wakeup;
1083 } else {
1084 need_hw_irq = false;
1085 }
1086
fef4e61b
TS
1087 if (trace_amdtp_packet_enabled())
1088 (void)fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &curr_cycle_time);
1089
5e2ece0f 1090 for (i = 0; i < packets; ++i) {
6bc1a269
TS
1091 struct {
1092 struct fw_iso_packet params;
67d92ee7 1093 __be32 header[CIP_HEADER_QUADLETS];
6bc1a269 1094 } template = { {0}, {0} };
e229853d 1095 bool sched_irq = false;
31ef9134 1096
233dbbc7 1097 build_it_pkt_header(s, desc->cycle, &template.params, pkt_header_length,
f4f6ae7b 1098 desc->data_blocks, desc->data_block_counter,
fef4e61b 1099 desc->syt, i, curr_cycle_time);
6bc1a269 1100
2472cfb3 1101 if (s == s->domain->irq_target) {
60dd4929
TS
1102 event_count += desc->data_blocks;
1103 if (event_count >= events_per_period) {
1104 event_count -= events_per_period;
d360870a 1105 sched_irq = need_hw_irq;
60dd4929 1106 }
e229853d
TS
1107 }
1108
1109 if (queue_out_packet(s, &template.params, sched_irq) < 0) {
fce9b013 1110 cancel_stream(s);
a4103bd7
TS
1111 return;
1112 }
cec371ff
TS
1113
1114 desc = amdtp_stream_next_packet_desc(s, desc);
ccccad86 1115 }
a4103bd7 1116
60dd4929 1117 s->ctx_data.rx.event_count = event_count;
f0117128 1118 s->packet_descs_cursor = desc;
31ef9134
CL
1119}
1120
9b1fcd9b
TS
1121static void skip_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1122 void *header, void *private_data)
1123{
1124 struct amdtp_stream *s = private_data;
1125 struct amdtp_domain *d = s->domain;
1126 const __be32 *ctx_header = header;
1127 unsigned int packets;
1128 unsigned int cycle;
1129 int i;
1130
1131 if (s->packet_index < 0)
1132 return;
1133
1134 packets = header_length / sizeof(*ctx_header);
1135
1136 cycle = compute_ohci_it_cycle(ctx_header[packets - 1], s->queue_size);
1137 s->next_cycle = increment_ohci_cycle_count(cycle, 1);
1138
1139 for (i = 0; i < packets; ++i) {
1140 struct fw_iso_packet params = {
1141 .header_length = 0,
1142 .payload_length = 0,
1143 };
1144 bool sched_irq = (s == d->irq_target && i == packets - 1);
1145
1146 if (queue_out_packet(s, &params, sched_irq) < 0) {
1147 cancel_stream(s);
1148 return;
1149 }
1150 }
1151}
1152
1153static void irq_target_callback(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1154 void *header, void *private_data);
1155
1156static void process_rx_packets_intermediately(struct fw_iso_context *context, u32 tstamp,
1157 size_t header_length, void *header, void *private_data)
1158{
1159 struct amdtp_stream *s = private_data;
1160 struct amdtp_domain *d = s->domain;
1161 __be32 *ctx_header = header;
1162 const unsigned int queue_size = s->queue_size;
1163 unsigned int packets;
1164 unsigned int offset;
1165
1166 if (s->packet_index < 0)
1167 return;
1168
1169 packets = header_length / sizeof(*ctx_header);
1170
1171 offset = 0;
1172 while (offset < packets) {
1173 unsigned int cycle = compute_ohci_it_cycle(ctx_header[offset], queue_size);
1174
1175 if (compare_ohci_cycle_count(cycle, d->processing_cycle.rx_start) >= 0)
1176 break;
1177
1178 ++offset;
1179 }
1180
1181 if (offset > 0) {
1182 unsigned int length = sizeof(*ctx_header) * offset;
1183
1184 skip_rx_packets(context, tstamp, length, ctx_header, private_data);
1185 if (amdtp_streaming_error(s))
1186 return;
1187
1188 ctx_header += offset;
1189 header_length -= length;
1190 }
1191
1192 if (offset < packets) {
bdaedca7
TS
1193 s->ready_processing = true;
1194 wake_up(&s->ready_wait);
1195
c38d8cff
TS
1196 if (d->replay.enable)
1197 s->ctx_data.rx.cache_pos = 0;
1198
9b1fcd9b
TS
1199 process_rx_packets(context, tstamp, header_length, ctx_header, private_data);
1200 if (amdtp_streaming_error(s))
1201 return;
1202
1203 if (s == d->irq_target)
1204 s->context->callback.sc = irq_target_callback;
1205 else
1206 s->context->callback.sc = process_rx_packets;
1207 }
1208}
1209
da3623ab
TS
1210static void process_tx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1211 void *header, void *private_data)
2b3fc456
TS
1212{
1213 struct amdtp_stream *s = private_data;
cc4f8e91 1214 __be32 *ctx_header = header;
f0117128 1215 struct pkt_desc *desc = s->packet_descs_cursor;
cccddec4 1216 unsigned int packet_count;
73246fc4 1217 unsigned int desc_count;
753e7179
TS
1218 int i;
1219 int err;
2b3fc456 1220
a4103bd7
TS
1221 if (s->packet_index < 0)
1222 return;
1223
a0e02331 1224 // Calculate the number of packets in buffer and check XRUN.
cccddec4 1225 packet_count = header_length / s->ctx_data.tx.ctx_header_size;
f90e2ded 1226
73246fc4 1227 desc_count = 0;
cec371ff 1228 err = generate_tx_packet_descs(s, desc, ctx_header, packet_count, &desc_count);
753e7179
TS
1229 if (err < 0) {
1230 if (err != -EAGAIN) {
1231 cancel_stream(s);
1232 return;
1233 }
5e2ece0f 1234 } else {
f9e5ecdf
TS
1235 struct amdtp_domain *d = s->domain;
1236
cec371ff 1237 process_ctx_payloads(s, desc, desc_count);
f9e5ecdf
TS
1238
1239 if (d->replay.enable)
cec371ff 1240 cache_seq(s, desc, desc_count);
f0117128
TS
1241
1242 for (i = 0; i < desc_count; ++i)
1243 desc = amdtp_stream_next_packet_desc(s, desc);
1244 s->packet_descs_cursor = desc;
5e2ece0f
TS
1245 }
1246
cccddec4 1247 for (i = 0; i < packet_count; ++i) {
5e2ece0f 1248 struct fw_iso_packet params = {0};
2b3fc456 1249
60dd4929 1250 if (queue_in_packet(s, &params) < 0) {
753e7179
TS
1251 cancel_stream(s);
1252 return;
1253 }
7b3b0d85 1254 }
60dd4929
TS
1255}
1256
da3623ab
TS
1257static void drop_tx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1258 void *header, void *private_data)
1259{
1260 struct amdtp_stream *s = private_data;
1261 const __be32 *ctx_header = header;
1262 unsigned int packets;
1263 unsigned int cycle;
1264 int i;
1265
1266 if (s->packet_index < 0)
1267 return;
1268
1269 packets = header_length / s->ctx_data.tx.ctx_header_size;
1270
1271 ctx_header += (packets - 1) * s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header);
1272 cycle = compute_ohci_cycle_count(ctx_header[1]);
1273 s->next_cycle = increment_ohci_cycle_count(cycle, 1);
1274
1275 for (i = 0; i < packets; ++i) {
1276 struct fw_iso_packet params = {0};
1277
1278 if (queue_in_packet(s, &params) < 0) {
1279 cancel_stream(s);
1280 return;
1281 }
1282 }
1283}
1284
1285static void process_tx_packets_intermediately(struct fw_iso_context *context, u32 tstamp,
1286 size_t header_length, void *header, void *private_data)
1287{
1288 struct amdtp_stream *s = private_data;
1289 struct amdtp_domain *d = s->domain;
1290 __be32 *ctx_header;
1291 unsigned int packets;
1292 unsigned int offset;
1293
1294 if (s->packet_index < 0)
1295 return;
1296
1297 packets = header_length / s->ctx_data.tx.ctx_header_size;
1298
1299 offset = 0;
1300 ctx_header = header;
1301 while (offset < packets) {
1302 unsigned int cycle = compute_ohci_cycle_count(ctx_header[1]);
1303
1304 if (compare_ohci_cycle_count(cycle, d->processing_cycle.tx_start) >= 0)
1305 break;
1306
1307 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32);
1308 ++offset;
1309 }
1310
1311 ctx_header = header;
1312
1313 if (offset > 0) {
1314 size_t length = s->ctx_data.tx.ctx_header_size * offset;
1315
1316 drop_tx_packets(context, tstamp, length, ctx_header, s);
1317 if (amdtp_streaming_error(s))
1318 return;
1319
1320 ctx_header += length / sizeof(*ctx_header);
1321 header_length -= length;
1322 }
1323
1324 if (offset < packets) {
bdaedca7
TS
1325 s->ready_processing = true;
1326 wake_up(&s->ready_wait);
1327
da3623ab
TS
1328 process_tx_packets(context, tstamp, header_length, ctx_header, s);
1329 if (amdtp_streaming_error(s))
1330 return;
1331
1332 context->callback.sc = process_tx_packets;
1333 }
1334}
1335
fb25dcc8
TS
1336static void drop_tx_packets_initially(struct fw_iso_context *context, u32 tstamp,
1337 size_t header_length, void *header, void *private_data)
1338{
1339 struct amdtp_stream *s = private_data;
1340 struct amdtp_domain *d = s->domain;
1341 __be32 *ctx_header;
1342 unsigned int count;
1343 unsigned int events;
1344 int i;
1345
1346 if (s->packet_index < 0)
1347 return;
1348
1349 count = header_length / s->ctx_data.tx.ctx_header_size;
1350
1351 // Attempt to detect any event in the batch of packets.
1352 events = 0;
1353 ctx_header = header;
1354 for (i = 0; i < count; ++i) {
1355 unsigned int payload_quads =
1356 (be32_to_cpu(*ctx_header) >> ISO_DATA_LENGTH_SHIFT) / sizeof(__be32);
1357 unsigned int data_blocks;
1358
1359 if (s->flags & CIP_NO_HEADER) {
1360 data_blocks = payload_quads / s->data_block_quadlets;
1361 } else {
1362 __be32 *cip_headers = ctx_header + IR_CTX_HEADER_DEFAULT_QUADLETS;
1363
1364 if (payload_quads < CIP_HEADER_QUADLETS) {
1365 data_blocks = 0;
1366 } else {
1367 payload_quads -= CIP_HEADER_QUADLETS;
1368
1369 if (s->flags & CIP_UNAWARE_SYT) {
1370 data_blocks = payload_quads / s->data_block_quadlets;
1371 } else {
1372 u32 cip1 = be32_to_cpu(cip_headers[1]);
1373
1374 // NODATA packet can includes any data blocks but they are
1375 // not available as event.
1376 if ((cip1 & CIP_NO_DATA) == CIP_NO_DATA)
1377 data_blocks = 0;
1378 else
1379 data_blocks = payload_quads / s->data_block_quadlets;
1380 }
1381 }
1382 }
1383
1384 events += data_blocks;
1385
1386 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32);
1387 }
1388
1389 drop_tx_packets(context, tstamp, header_length, header, s);
1390
1391 if (events > 0)
1392 s->ctx_data.tx.event_starts = true;
1393
1394 // Decide the cycle count to begin processing content of packet in IR contexts.
1395 {
1396 unsigned int stream_count = 0;
1397 unsigned int event_starts_count = 0;
1398 unsigned int cycle = UINT_MAX;
1399
1400 list_for_each_entry(s, &d->streams, list) {
1401 if (s->direction == AMDTP_IN_STREAM) {
1402 ++stream_count;
1403 if (s->ctx_data.tx.event_starts)
1404 ++event_starts_count;
1405 }
1406 }
1407
1408 if (stream_count == event_starts_count) {
1409 unsigned int next_cycle;
1410
1411 list_for_each_entry(s, &d->streams, list) {
1412 if (s->direction != AMDTP_IN_STREAM)
1413 continue;
1414
1415 next_cycle = increment_ohci_cycle_count(s->next_cycle,
1416 d->processing_cycle.tx_init_skip);
1417 if (cycle == UINT_MAX ||
1418 compare_ohci_cycle_count(next_cycle, cycle) > 0)
1419 cycle = next_cycle;
1420
1421 s->context->callback.sc = process_tx_packets_intermediately;
1422 }
1423
1424 d->processing_cycle.tx_start = cycle;
1425 }
1426 }
1427}
1428
9b1fcd9b 1429static void process_ctxs_in_domain(struct amdtp_domain *d)
60dd4929 1430{
60dd4929
TS
1431 struct amdtp_stream *s;
1432
60dd4929 1433 list_for_each_entry(s, &d->streams, list) {
9b1fcd9b 1434 if (s != d->irq_target && amdtp_stream_running(s))
60dd4929 1435 fw_iso_context_flush_completions(s->context);
9b1fcd9b
TS
1436
1437 if (amdtp_streaming_error(s))
1438 goto error;
60dd4929
TS
1439 }
1440
1441 return;
1442error:
9b1fcd9b
TS
1443 if (amdtp_stream_running(d->irq_target))
1444 cancel_stream(d->irq_target);
60dd4929
TS
1445
1446 list_for_each_entry(s, &d->streams, list) {
1447 if (amdtp_stream_running(s))
1448 cancel_stream(s);
1449 }
2b3fc456
TS
1450}
1451
9b1fcd9b
TS
1452static void irq_target_callback(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1453 void *header, void *private_data)
1454{
1455 struct amdtp_stream *s = private_data;
1456 struct amdtp_domain *d = s->domain;
9b1fcd9b
TS
1457
1458 process_rx_packets(context, tstamp, header_length, header, private_data);
1459 process_ctxs_in_domain(d);
1460}
1461
1462static void irq_target_callback_intermediately(struct fw_iso_context *context, u32 tstamp,
1463 size_t header_length, void *header, void *private_data)
1464{
1465 struct amdtp_stream *s = private_data;
1466 struct amdtp_domain *d = s->domain;
9b1fcd9b
TS
1467
1468 process_rx_packets_intermediately(context, tstamp, header_length, header, private_data);
1469 process_ctxs_in_domain(d);
1470}
1471
1472static void irq_target_callback_skip(struct fw_iso_context *context, u32 tstamp,
1473 size_t header_length, void *header, void *private_data)
1474{
1475 struct amdtp_stream *s = private_data;
1476 struct amdtp_domain *d = s->domain;
39c2649c 1477 bool ready_to_start;
9b1fcd9b
TS
1478
1479 skip_rx_packets(context, tstamp, header_length, header, private_data);
1480 process_ctxs_in_domain(d);
1481
2f21a177 1482 if (d->replay.enable && !d->replay.on_the_fly) {
39c2649c
TS
1483 unsigned int rx_count = 0;
1484 unsigned int rx_ready_count = 0;
1485 struct amdtp_stream *rx;
1486
1487 list_for_each_entry(rx, &d->streams, list) {
1488 struct amdtp_stream *tx;
1489 unsigned int cached_cycles;
1490
1491 if (rx->direction != AMDTP_OUT_STREAM)
1492 continue;
1493 ++rx_count;
1494
1495 tx = rx->ctx_data.rx.replay_target;
1496 cached_cycles = calculate_cached_cycle_count(tx, 0);
1497 if (cached_cycles > tx->ctx_data.tx.cache.size / 2)
1498 ++rx_ready_count;
1499 }
1500
1501 ready_to_start = (rx_count == rx_ready_count);
1502 } else {
1503 ready_to_start = true;
1504 }
1505
9b1fcd9b
TS
1506 // Decide the cycle count to begin processing content of packet in IT contexts. All of IT
1507 // contexts are expected to start and get callback when reaching here.
39c2649c
TS
1508 if (ready_to_start) {
1509 unsigned int cycle = s->next_cycle;
1510 list_for_each_entry(s, &d->streams, list) {
1511 if (s->direction != AMDTP_OUT_STREAM)
1512 continue;
9b1fcd9b 1513
39c2649c
TS
1514 if (compare_ohci_cycle_count(s->next_cycle, cycle) > 0)
1515 cycle = s->next_cycle;
9b1fcd9b 1516
39c2649c
TS
1517 if (s == d->irq_target)
1518 s->context->callback.sc = irq_target_callback_intermediately;
1519 else
1520 s->context->callback.sc = process_rx_packets_intermediately;
1521 }
9b1fcd9b 1522
39c2649c
TS
1523 d->processing_cycle.rx_start = cycle;
1524 }
9b1fcd9b
TS
1525}
1526
b7c7699b
TS
1527// This is executed one time. For in-stream, first packet has come. For out-stream, prepared to
1528// transmit first packet.
7b3b0d85 1529static void amdtp_stream_first_callback(struct fw_iso_context *context,
73fc7f08 1530 u32 tstamp, size_t header_length,
7b3b0d85
TS
1531 void *header, void *private_data)
1532{
1533 struct amdtp_stream *s = private_data;
da3623ab 1534 struct amdtp_domain *d = s->domain;
7b3b0d85 1535
a04513f8 1536 if (s->direction == AMDTP_IN_STREAM) {
fb25dcc8 1537 context->callback.sc = drop_tx_packets_initially;
a04513f8 1538 } else {
da3623ab 1539 if (s == d->irq_target)
9b1fcd9b 1540 context->callback.sc = irq_target_callback_skip;
2472cfb3 1541 else
9b1fcd9b 1542 context->callback.sc = skip_rx_packets;
a04513f8
TS
1543 }
1544
73fc7f08 1545 context->callback.sc(context, tstamp, header_length, header, s);
7b3b0d85
TS
1546}
1547
31ef9134 1548/**
be4a2894
TS
1549 * amdtp_stream_start - start transferring packets
1550 * @s: the AMDTP stream to start
31ef9134
CL
1551 * @channel: the isochronous channel on the bus
1552 * @speed: firewire speed code
af86b0b1
TS
1553 * @queue_size: The number of packets in the queue.
1554 * @idle_irq_interval: the interval to queue packet during initial state.
31ef9134
CL
1555 *
1556 * The stream cannot be started until it has been configured with
be4a2894
TS
1557 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
1558 * device can be started.
31ef9134 1559 */
a0e02331 1560static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed,
bd165079 1561 unsigned int queue_size, unsigned int idle_irq_interval)
31ef9134 1562{
2472cfb3 1563 bool is_irq_target = (s == s->domain->irq_target);
d3d10a4a 1564 unsigned int ctx_header_size;
f11453c7 1565 unsigned int max_ctx_payload_size;
2b3fc456 1566 enum dma_data_direction dir;
cec371ff
TS
1567 struct pkt_desc *descs;
1568 int i, type, tag, err;
31ef9134
CL
1569
1570 mutex_lock(&s->mutex);
1571
be4a2894 1572 if (WARN_ON(amdtp_stream_running(s) ||
4b7da117 1573 (s->data_block_quadlets < 1))) {
31ef9134
CL
1574 err = -EBADFD;
1575 goto err_unlock;
1576 }
1577
d3d10a4a 1578 if (s->direction == AMDTP_IN_STREAM) {
60dd4929
TS
1579 // NOTE: IT context should be used for constant IRQ.
1580 if (is_irq_target) {
1581 err = -EINVAL;
1582 goto err_unlock;
1583 }
1584
b6bc8123 1585 s->data_block_counter = UINT_MAX;
d3d10a4a 1586 } else {
b6bc8123 1587 s->data_block_counter = 0;
d3d10a4a 1588 }
31ef9134 1589
1be4f21d 1590 // initialize packet buffer.
2b3fc456
TS
1591 if (s->direction == AMDTP_IN_STREAM) {
1592 dir = DMA_FROM_DEVICE;
1593 type = FW_ISO_CONTEXT_RECEIVE;
c75f3678 1594 if (!(s->flags & CIP_NO_HEADER))
f11453c7 1595 ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
c75f3678 1596 else
f11453c7 1597 ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
2b3fc456
TS
1598 } else {
1599 dir = DMA_TO_DEVICE;
1600 type = FW_ISO_CONTEXT_TRANSMIT;
df9160b9 1601 ctx_header_size = 0; // No effect for IT context.
b18f0cfa 1602 }
c75f3678 1603 max_ctx_payload_size = amdtp_stream_get_max_ctx_payload_size(s);
f11453c7 1604
c75f3678 1605 err = iso_packets_buffer_init(&s->buffer, s->unit, queue_size, max_ctx_payload_size, dir);
31ef9134
CL
1606 if (err < 0)
1607 goto err_unlock;
af86b0b1 1608 s->queue_size = queue_size;
60dd4929 1609
31ef9134 1610 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
d3d10a4a 1611 type, channel, speed, ctx_header_size,
2472cfb3 1612 amdtp_stream_first_callback, s);
31ef9134
CL
1613 if (IS_ERR(s->context)) {
1614 err = PTR_ERR(s->context);
1615 if (err == -EBUSY)
1616 dev_err(&s->unit->device,
be4a2894 1617 "no free stream on this controller\n");
31ef9134
CL
1618 goto err_buffer;
1619 }
1620
be4a2894 1621 amdtp_stream_update(s);
31ef9134 1622
d3d10a4a 1623 if (s->direction == AMDTP_IN_STREAM) {
f11453c7 1624 s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size;
d3d10a4a 1625 s->ctx_data.tx.ctx_header_size = ctx_header_size;
fb25dcc8 1626 s->ctx_data.tx.event_starts = false;
f9e5ecdf
TS
1627
1628 if (s->domain->replay.enable) {
1629 // struct fw_iso_context.drop_overflow_headers is false therefore it's
1630 // possible to cache much unexpectedly.
1631 s->ctx_data.tx.cache.size = max_t(unsigned int, s->syt_interval * 2,
1632 queue_size * 3 / 2);
cccddec4 1633 s->ctx_data.tx.cache.pos = 0;
f9e5ecdf
TS
1634 s->ctx_data.tx.cache.descs = kcalloc(s->ctx_data.tx.cache.size,
1635 sizeof(*s->ctx_data.tx.cache.descs), GFP_KERNEL);
8b6e2193
DC
1636 if (!s->ctx_data.tx.cache.descs) {
1637 err = -ENOMEM;
f9e5ecdf 1638 goto err_context;
8b6e2193 1639 }
f9e5ecdf 1640 }
bd165079 1641 } else {
6f24bb8a
TS
1642 static const struct {
1643 unsigned int data_block;
1644 unsigned int syt_offset;
1645 } *entry, initial_state[] = {
1646 [CIP_SFC_32000] = { 4, 3072 },
1647 [CIP_SFC_48000] = { 6, 1024 },
1648 [CIP_SFC_96000] = { 12, 1024 },
1649 [CIP_SFC_192000] = { 24, 1024 },
1650 [CIP_SFC_44100] = { 0, 67 },
1651 [CIP_SFC_88200] = { 0, 67 },
1652 [CIP_SFC_176400] = { 0, 67 },
1653 };
1654
1655 s->ctx_data.rx.seq.descs = kcalloc(queue_size, sizeof(*s->ctx_data.rx.seq.descs), GFP_KERNEL);
8b6e2193
DC
1656 if (!s->ctx_data.rx.seq.descs) {
1657 err = -ENOMEM;
6f24bb8a 1658 goto err_context;
8b6e2193 1659 }
6f24bb8a 1660 s->ctx_data.rx.seq.size = queue_size;
119c446a 1661 s->ctx_data.rx.seq.pos = 0;
6f24bb8a
TS
1662
1663 entry = &initial_state[s->sfc];
1664 s->ctx_data.rx.data_block_state = entry->data_block;
1665 s->ctx_data.rx.syt_offset_state = entry->syt_offset;
1666 s->ctx_data.rx.last_syt_offset = TICKS_PER_CYCLE;
1667
bd165079 1668 s->ctx_data.rx.event_count = 0;
d3d10a4a 1669 }
52759c09 1670
3b196c39
TS
1671 if (s->flags & CIP_NO_HEADER)
1672 s->tag = TAG_NO_CIP_HEADER;
1673 else
1674 s->tag = TAG_CIP;
1675
cec371ff
TS
1676 descs = kcalloc(s->queue_size, sizeof(*descs), GFP_KERNEL);
1677 if (!descs) {
04130cf8
TS
1678 err = -ENOMEM;
1679 goto err_context;
1680 }
f0117128 1681 s->packet_descs = descs;
cec371ff
TS
1682
1683 INIT_LIST_HEAD(&s->packet_descs_list);
1684 for (i = 0; i < s->queue_size; ++i) {
1685 INIT_LIST_HEAD(&descs->link);
1686 list_add_tail(&descs->link, &s->packet_descs_list);
1687 ++descs;
1688 }
f0117128 1689 s->packet_descs_cursor = list_first_entry(&s->packet_descs_list, struct pkt_desc, link);
04130cf8 1690
ec00f5e4 1691 s->packet_index = 0;
4b7da117 1692 do {
6007bf54 1693 struct fw_iso_packet params;
e229853d 1694
b18f0cfa 1695 if (s->direction == AMDTP_IN_STREAM) {
60dd4929 1696 err = queue_in_packet(s, &params);
b18f0cfa 1697 } else {
60dd4929
TS
1698 bool sched_irq = false;
1699
b18f0cfa
TS
1700 params.header_length = 0;
1701 params.payload_length = 0;
60dd4929
TS
1702
1703 if (is_irq_target) {
1704 sched_irq = !((s->packet_index + 1) %
1705 idle_irq_interval);
1706 }
1707
e229853d 1708 err = queue_out_packet(s, &params, sched_irq);
b18f0cfa 1709 }
4b7da117 1710 if (err < 0)
04130cf8 1711 goto err_pkt_descs;
4b7da117 1712 } while (s->packet_index > 0);
31ef9134 1713
2b3fc456 1714 /* NOTE: TAG1 matches CIP. This just affects in stream. */
7ab56645 1715 tag = FW_ISO_CONTEXT_MATCH_TAG1;
3b196c39 1716 if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
7ab56645
TS
1717 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
1718
bdaedca7 1719 s->ready_processing = false;
bd165079 1720 err = fw_iso_context_start(s->context, -1, 0, tag);
31ef9134 1721 if (err < 0)
04130cf8 1722 goto err_pkt_descs;
31ef9134
CL
1723
1724 mutex_unlock(&s->mutex);
1725
1726 return 0;
04130cf8 1727err_pkt_descs:
f0117128
TS
1728 kfree(s->packet_descs);
1729 s->packet_descs = NULL;
31ef9134 1730err_context:
f9e5ecdf 1731 if (s->direction == AMDTP_OUT_STREAM) {
6f24bb8a 1732 kfree(s->ctx_data.rx.seq.descs);
f9e5ecdf
TS
1733 } else {
1734 if (s->domain->replay.enable)
1735 kfree(s->ctx_data.tx.cache.descs);
1736 }
31ef9134
CL
1737 fw_iso_context_destroy(s->context);
1738 s->context = ERR_PTR(-1);
1739err_buffer:
1740 iso_packets_buffer_destroy(&s->buffer, s->unit);
1741err_unlock:
1742 mutex_unlock(&s->mutex);
1743
1744 return err;
1745}
31ef9134 1746
e9148ddd 1747/**
f890f9a0
TS
1748 * amdtp_domain_stream_pcm_pointer - get the PCM buffer position
1749 * @d: the AMDTP domain.
be4a2894 1750 * @s: the AMDTP stream that transports the PCM data
e9148ddd
CL
1751 *
1752 * Returns the current buffer position, in frames.
1753 */
f890f9a0
TS
1754unsigned long amdtp_domain_stream_pcm_pointer(struct amdtp_domain *d,
1755 struct amdtp_stream *s)
e9148ddd 1756{
f890f9a0
TS
1757 struct amdtp_stream *irq_target = d->irq_target;
1758
7ba5ca32 1759 // Process isochronous packets queued till recent isochronous cycle to handle PCM frames.
f890f9a0 1760 if (irq_target && amdtp_stream_running(irq_target)) {
7ba5ca32
TS
1761 // In software IRQ context, the call causes dead-lock to disable the tasklet
1762 // synchronously.
3b86ec63 1763 if (!in_softirq())
f890f9a0 1764 fw_iso_context_flush_completions(irq_target->context);
f890f9a0 1765 }
e9148ddd 1766
6aa7de05 1767 return READ_ONCE(s->pcm_buffer_pointer);
e9148ddd 1768}
f890f9a0 1769EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_pointer);
e9148ddd 1770
875becf8 1771/**
e6dcc92f
TS
1772 * amdtp_domain_stream_pcm_ack - acknowledge queued PCM frames
1773 * @d: the AMDTP domain.
875becf8
TS
1774 * @s: the AMDTP stream that transfers the PCM frames
1775 *
1776 * Returns zero always.
1777 */
e6dcc92f 1778int amdtp_domain_stream_pcm_ack(struct amdtp_domain *d, struct amdtp_stream *s)
875becf8 1779{
e6dcc92f
TS
1780 struct amdtp_stream *irq_target = d->irq_target;
1781
1782 // Process isochronous packets for recent isochronous cycle to handle
1783 // queued PCM frames.
987b705b 1784 if (irq_target && amdtp_stream_running(irq_target))
e6dcc92f 1785 fw_iso_context_flush_completions(irq_target->context);
875becf8
TS
1786
1787 return 0;
1788}
e6dcc92f 1789EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_ack);
875becf8 1790
31ef9134 1791/**
be4a2894
TS
1792 * amdtp_stream_update - update the stream after a bus reset
1793 * @s: the AMDTP stream
31ef9134 1794 */
be4a2894 1795void amdtp_stream_update(struct amdtp_stream *s)
31ef9134 1796{
9a2820c1 1797 /* Precomputing. */
6aa7de05
MR
1798 WRITE_ONCE(s->source_node_id_field,
1799 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK);
31ef9134 1800}
be4a2894 1801EXPORT_SYMBOL(amdtp_stream_update);
31ef9134
CL
1802
1803/**
be4a2894
TS
1804 * amdtp_stream_stop - stop sending packets
1805 * @s: the AMDTP stream to stop
31ef9134
CL
1806 *
1807 * All PCM and MIDI devices of the stream must be stopped before the stream
1808 * itself can be stopped.
1809 */
74f94e41 1810static void amdtp_stream_stop(struct amdtp_stream *s)
31ef9134
CL
1811{
1812 mutex_lock(&s->mutex);
1813
be4a2894 1814 if (!amdtp_stream_running(s)) {
31ef9134
CL
1815 mutex_unlock(&s->mutex);
1816 return;
1817 }
1818
1819 fw_iso_context_stop(s->context);
1820 fw_iso_context_destroy(s->context);
1821 s->context = ERR_PTR(-1);
1822 iso_packets_buffer_destroy(&s->buffer, s->unit);
f0117128
TS
1823 kfree(s->packet_descs);
1824 s->packet_descs = NULL;
31ef9134 1825
f9e5ecdf 1826 if (s->direction == AMDTP_OUT_STREAM) {
6f24bb8a 1827 kfree(s->ctx_data.rx.seq.descs);
f9e5ecdf
TS
1828 } else {
1829 if (s->domain->replay.enable)
1830 kfree(s->ctx_data.tx.cache.descs);
1831 }
7b3b0d85 1832
31ef9134
CL
1833 mutex_unlock(&s->mutex);
1834}
31ef9134
CL
1835
1836/**
be4a2894 1837 * amdtp_stream_pcm_abort - abort the running PCM device
31ef9134
CL
1838 * @s: the AMDTP stream about to be stopped
1839 *
1840 * If the isochronous stream needs to be stopped asynchronously, call this
1841 * function first to stop the PCM device.
1842 */
be4a2894 1843void amdtp_stream_pcm_abort(struct amdtp_stream *s)
31ef9134
CL
1844{
1845 struct snd_pcm_substream *pcm;
1846
6aa7de05 1847 pcm = READ_ONCE(s->pcm);
1fb8510c
TI
1848 if (pcm)
1849 snd_pcm_stop_xrun(pcm);
31ef9134 1850}
be4a2894 1851EXPORT_SYMBOL(amdtp_stream_pcm_abort);
3ec3d7a3
TS
1852
1853/**
1854 * amdtp_domain_init - initialize an AMDTP domain structure
1855 * @d: the AMDTP domain to initialize.
1856 */
1857int amdtp_domain_init(struct amdtp_domain *d)
1858{
1859 INIT_LIST_HEAD(&d->streams);
1860
d68c3123
TS
1861 d->events_per_period = 0;
1862
3ec3d7a3
TS
1863 return 0;
1864}
1865EXPORT_SYMBOL_GPL(amdtp_domain_init);
1866
1867/**
1868 * amdtp_domain_destroy - destroy an AMDTP domain structure
1869 * @d: the AMDTP domain to destroy.
1870 */
1871void amdtp_domain_destroy(struct amdtp_domain *d)
1872{
8d0d5c3f
TS
1873 // At present nothing to do.
1874 return;
3ec3d7a3
TS
1875}
1876EXPORT_SYMBOL_GPL(amdtp_domain_destroy);
6261f90b 1877
157a53ee
TS
1878/**
1879 * amdtp_domain_add_stream - register isoc context into the domain.
1880 * @d: the AMDTP domain.
1881 * @s: the AMDTP stream.
1882 * @channel: the isochronous channel on the bus.
1883 * @speed: firewire speed code.
1884 */
1885int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s,
1886 int channel, int speed)
1887{
1888 struct amdtp_stream *tmp;
1889
1890 list_for_each_entry(tmp, &d->streams, list) {
1891 if (s == tmp)
1892 return -EBUSY;
1893 }
1894
1895 list_add(&s->list, &d->streams);
1896
1897 s->channel = channel;
1898 s->speed = speed;
2472cfb3 1899 s->domain = d;
157a53ee
TS
1900
1901 return 0;
1902}
1903EXPORT_SYMBOL_GPL(amdtp_domain_add_stream);
1904
39c2649c
TS
1905// Make the reference from rx stream to tx stream for sequence replay. When the number of tx streams
1906// is less than the number of rx streams, the first tx stream is selected.
1907static int make_association(struct amdtp_domain *d)
1908{
1909 unsigned int dst_index = 0;
1910 struct amdtp_stream *rx;
1911
1912 // Make association to replay target.
1913 list_for_each_entry(rx, &d->streams, list) {
1914 if (rx->direction == AMDTP_OUT_STREAM) {
1915 unsigned int src_index = 0;
1916 struct amdtp_stream *tx = NULL;
1917 struct amdtp_stream *s;
1918
1919 list_for_each_entry(s, &d->streams, list) {
1920 if (s->direction == AMDTP_IN_STREAM) {
1921 if (dst_index == src_index) {
1922 tx = s;
1923 break;
1924 }
1925
1926 ++src_index;
1927 }
1928 }
1929 if (!tx) {
1930 // Select the first entry.
1931 list_for_each_entry(s, &d->streams, list) {
1932 if (s->direction == AMDTP_IN_STREAM) {
1933 tx = s;
1934 break;
1935 }
1936 }
1937 // No target is available to replay sequence.
1938 if (!tx)
1939 return -EINVAL;
1940 }
1941
1942 rx->ctx_data.rx.replay_target = tx;
39c2649c
TS
1943
1944 ++dst_index;
1945 }
1946 }
1947
1948 return 0;
1949}
1950
9b4702b0
TS
1951/**
1952 * amdtp_domain_start - start sending packets for isoc context in the domain.
1953 * @d: the AMDTP domain.
26541cb1
TS
1954 * @tx_init_skip_cycles: the number of cycles to skip processing packets at initial stage of IR
1955 * contexts.
f9e5ecdf
TS
1956 * @replay_seq: whether to replay the sequence of packet in IR context for the sequence of packet in
1957 * IT context.
2f21a177
TS
1958 * @replay_on_the_fly: transfer rx packets according to nominal frequency, then begin to replay
1959 * according to arrival of events in tx packets.
9b4702b0 1960 */
2f21a177
TS
1961int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles, bool replay_seq,
1962 bool replay_on_the_fly)
9b4702b0 1963{
af86b0b1
TS
1964 unsigned int events_per_buffer = d->events_per_buffer;
1965 unsigned int events_per_period = d->events_per_period;
af86b0b1 1966 unsigned int queue_size;
9b4702b0 1967 struct amdtp_stream *s;
0cbbeaf3 1968 bool found = false;
acfedcbe 1969 int err;
9b4702b0 1970
39c2649c
TS
1971 if (replay_seq) {
1972 err = make_association(d);
1973 if (err < 0)
1974 return err;
1975 }
f9e5ecdf 1976 d->replay.enable = replay_seq;
2f21a177 1977 d->replay.on_the_fly = replay_on_the_fly;
f9e5ecdf 1978
60dd4929 1979 // Select an IT context as IRQ target.
9b4702b0 1980 list_for_each_entry(s, &d->streams, list) {
0cbbeaf3
CJ
1981 if (s->direction == AMDTP_OUT_STREAM) {
1982 found = true;
9b4702b0 1983 break;
0cbbeaf3 1984 }
9b4702b0 1985 }
0cbbeaf3 1986 if (!found)
60dd4929
TS
1987 return -ENXIO;
1988 d->irq_target = s;
9b4702b0 1989
26541cb1
TS
1990 d->processing_cycle.tx_init_skip = tx_init_skip_cycles;
1991
af86b0b1
TS
1992 // This is a case that AMDTP streams in domain run just for MIDI
1993 // substream. Use the number of events equivalent to 10 msec as
1994 // interval of hardware IRQ.
1995 if (events_per_period == 0)
1996 events_per_period = amdtp_rate_table[d->irq_target->sfc] / 100;
1997 if (events_per_buffer == 0)
1998 events_per_buffer = events_per_period * 3;
1999
2000 queue_size = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_buffer,
2001 amdtp_rate_table[d->irq_target->sfc]);
2002
60dd4929 2003 list_for_each_entry(s, &d->streams, list) {
bd165079 2004 unsigned int idle_irq_interval = 0;
acfedcbe 2005
bd165079
TS
2006 if (s->direction == AMDTP_OUT_STREAM && s == d->irq_target) {
2007 idle_irq_interval = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_period,
2008 amdtp_rate_table[d->irq_target->sfc]);
60dd4929 2009 }
9b4702b0 2010
bd165079
TS
2011 // Starts immediately but actually DMA context starts several hundred cycles later.
2012 err = amdtp_stream_start(s, s->channel, s->speed, queue_size, idle_irq_interval);
2013 if (err < 0)
2014 goto error;
2015 }
60dd4929
TS
2016
2017 return 0;
2018error:
2019 list_for_each_entry(s, &d->streams, list)
2020 amdtp_stream_stop(s);
9b4702b0
TS
2021 return err;
2022}
2023EXPORT_SYMBOL_GPL(amdtp_domain_start);
2024
6261f90b
TS
2025/**
2026 * amdtp_domain_stop - stop sending packets for isoc context in the same domain.
2027 * @d: the AMDTP domain to which the isoc contexts belong.
2028 */
2029void amdtp_domain_stop(struct amdtp_domain *d)
2030{
2031 struct amdtp_stream *s, *next;
2032
60dd4929
TS
2033 if (d->irq_target)
2034 amdtp_stream_stop(d->irq_target);
2035
6261f90b
TS
2036 list_for_each_entry_safe(s, next, &d->streams, list) {
2037 list_del(&s->list);
2038
60dd4929
TS
2039 if (s != d->irq_target)
2040 amdtp_stream_stop(s);
6261f90b 2041 }
d68c3123
TS
2042
2043 d->events_per_period = 0;
60dd4929 2044 d->irq_target = NULL;
6261f90b
TS
2045}
2046EXPORT_SYMBOL_GPL(amdtp_domain_stop);