xref: /linux/sound/firewire/iso-resources.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * isochronous resources helper functions
4  *
5  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6  */
7 
8 #include <linux/device.h>
9 #include <linux/firewire.h>
10 #include <linux/firewire-constants.h>
11 #include <linux/export.h>
12 #include <linux/jiffies.h>
13 #include <linux/mutex.h>
14 #include <linux/sched.h>
15 #include <linux/spinlock.h>
16 #include "iso-resources.h"
17 
18 /**
19  * fw_iso_resources_init - initializes a &struct fw_iso_resources
20  * @r: the resource manager to initialize
21  * @unit: the device unit for which the resources will be needed
22  *
23  * If the device does not support all channel numbers, change @r->channels_mask
24  * after calling this function.
25  */
26 int fw_iso_resources_init(struct fw_iso_resources *r, struct fw_unit *unit)
27 {
28 	r->channels_mask = ~0uLL;
29 	r->unit = unit;
30 	mutex_init(&r->mutex);
31 	r->allocated = false;
32 
33 	return 0;
34 }
35 EXPORT_SYMBOL(fw_iso_resources_init);
36 
37 /**
38  * fw_iso_resources_destroy - destroy a resource manager
39  * @r: the resource manager that is no longer needed
40  */
41 void fw_iso_resources_destroy(struct fw_iso_resources *r)
42 {
43 	WARN_ON(r->allocated);
44 	mutex_destroy(&r->mutex);
45 }
46 EXPORT_SYMBOL(fw_iso_resources_destroy);
47 
48 static unsigned int packet_bandwidth(unsigned int max_payload_bytes, int speed)
49 {
50 	unsigned int bytes, s400_bytes;
51 
52 	/* iso packets have three header quadlets and quadlet-aligned payload */
53 	bytes = 3 * 4 + ALIGN(max_payload_bytes, 4);
54 
55 	/* convert to bandwidth units (quadlets at S1600 = bytes at S400) */
56 	if (speed <= SCODE_400)
57 		s400_bytes = bytes * (1 << (SCODE_400 - speed));
58 	else
59 		s400_bytes = DIV_ROUND_UP(bytes, 1 << (speed - SCODE_400));
60 
61 	return s400_bytes;
62 }
63 
64 static int current_bandwidth_overhead(struct fw_card *card)
65 {
66 	/*
67 	 * Under the usual pessimistic assumption (cable length 4.5 m), the
68 	 * isochronous overhead for N cables is 1.797 µs + N * 0.494 µs, or
69 	 * 88.3 + N * 24.3 in bandwidth units.
70 	 *
71 	 * The calculation below tries to deduce N from the current gap count.
72 	 * If the gap count has been optimized by measuring the actual packet
73 	 * transmission time, this derived overhead should be near the actual
74 	 * overhead as well.
75 	 */
76 	return card->gap_count < 63 ? card->gap_count * 97 / 10 + 89 : 512;
77 }
78 
79 static int wait_isoch_resource_delay_after_bus_reset(struct fw_card *card)
80 {
81 	for (;;) {
82 		s64 delay = (card->reset_jiffies + HZ) - get_jiffies_64();
83 		if (delay <= 0)
84 			return 0;
85 		if (schedule_timeout_interruptible(delay) > 0)
86 			return -ERESTARTSYS;
87 	}
88 }
89 
90 /**
91  * fw_iso_resources_allocate - allocate isochronous channel and bandwidth
92  * @r: the resource manager
93  * @max_payload_bytes: the amount of data (including CIP headers) per packet
94  * @speed: the speed (e.g., SCODE_400) at which the packets will be sent
95  *
96  * This function allocates one isochronous channel and enough bandwidth for the
97  * specified packet size.
98  *
99  * Returns the channel number that the caller must use for streaming, or
100  * a negative error code.  Due to potentionally long delays, this function is
101  * interruptible and can return -ERESTARTSYS.  On success, the caller is
102  * responsible for calling fw_iso_resources_update() on bus resets, and
103  * fw_iso_resources_free() when the resources are not longer needed.
104  */
105 int fw_iso_resources_allocate(struct fw_iso_resources *r,
106 			      unsigned int max_payload_bytes, int speed)
107 {
108 	struct fw_card *card = fw_parent_device(r->unit)->card;
109 	int bandwidth, channel, err;
110 
111 	if (WARN_ON(r->allocated))
112 		return -EBADFD;
113 
114 	r->bandwidth = packet_bandwidth(max_payload_bytes, speed);
115 
116 retry_after_bus_reset:
117 	scoped_guard(spinlock_irq, &card->lock) {
118 		r->generation = card->generation;
119 		r->bandwidth_overhead = current_bandwidth_overhead(card);
120 	}
121 
122 	err = wait_isoch_resource_delay_after_bus_reset(card);
123 	if (err < 0)
124 		return err;
125 
126 	scoped_guard(mutex, &r->mutex) {
127 		bandwidth = r->bandwidth + r->bandwidth_overhead;
128 		fw_iso_resource_manage(card, r->generation, r->channels_mask,
129 				       &channel, &bandwidth, true);
130 		if (channel == -EAGAIN)
131 			goto retry_after_bus_reset;
132 		if (channel >= 0) {
133 			r->channel = channel;
134 			r->allocated = true;
135 		} else {
136 			if (channel == -EBUSY)
137 				dev_err(&r->unit->device,
138 					"isochronous resources exhausted\n");
139 			else
140 				dev_err(&r->unit->device,
141 					"isochronous resource allocation failed\n");
142 		}
143 	}
144 
145 	return channel;
146 }
147 EXPORT_SYMBOL(fw_iso_resources_allocate);
148 
149 /**
150  * fw_iso_resources_update - update resource allocations after a bus reset
151  * @r: the resource manager
152  *
153  * This function must be called from the driver's .update handler to reallocate
154  * any resources that were allocated before the bus reset.  It is safe to call
155  * this function if no resources are currently allocated.
156  *
157  * Returns a negative error code on failure.  If this happens, the caller must
158  * stop streaming.
159  */
160 int fw_iso_resources_update(struct fw_iso_resources *r)
161 {
162 	struct fw_card *card = fw_parent_device(r->unit)->card;
163 	int bandwidth, channel;
164 
165 	guard(mutex)(&r->mutex);
166 
167 	if (!r->allocated)
168 		return 0;
169 
170 	scoped_guard(spinlock_irq, &card->lock) {
171 		r->generation = card->generation;
172 		r->bandwidth_overhead = current_bandwidth_overhead(card);
173 	}
174 
175 	bandwidth = r->bandwidth + r->bandwidth_overhead;
176 
177 	fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
178 			       &channel, &bandwidth, true);
179 	/*
180 	 * When another bus reset happens, pretend that the allocation
181 	 * succeeded; we will try again for the new generation later.
182 	 */
183 	if (channel < 0 && channel != -EAGAIN) {
184 		r->allocated = false;
185 		if (channel == -EBUSY)
186 			dev_err(&r->unit->device,
187 				"isochronous resources exhausted\n");
188 		else
189 			dev_err(&r->unit->device,
190 				"isochronous resource allocation failed\n");
191 	}
192 
193 	return channel;
194 }
195 EXPORT_SYMBOL(fw_iso_resources_update);
196 
197 /**
198  * fw_iso_resources_free - frees allocated resources
199  * @r: the resource manager
200  *
201  * This function deallocates the channel and bandwidth, if allocated.
202  */
203 void fw_iso_resources_free(struct fw_iso_resources *r)
204 {
205 	struct fw_card *card;
206 	int bandwidth, channel;
207 
208 	/* Not initialized. */
209 	if (r->unit == NULL)
210 		return;
211 	card = fw_parent_device(r->unit)->card;
212 
213 	guard(mutex)(&r->mutex);
214 
215 	if (r->allocated) {
216 		bandwidth = r->bandwidth + r->bandwidth_overhead;
217 		fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
218 				       &channel, &bandwidth, false);
219 		if (channel < 0)
220 			dev_err(&r->unit->device,
221 				"isochronous resource deallocation failed\n");
222 
223 		r->allocated = false;
224 	}
225 }
226 EXPORT_SYMBOL(fw_iso_resources_free);
227