2 * isochronous resources helper functions
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
8 #include <linux/device.h>
9 #include <linux/firewire.h>
10 #include <linux/firewire-constants.h>
11 #include <linux/jiffies.h>
12 #include <linux/mutex.h>
13 #include <linux/sched.h>
14 #include <linux/spinlock.h>
15 #include "iso-resources.h"
18 * fw_iso_resources_init - initializes a &struct fw_iso_resources
19 * @r: the resource manager to initialize
20 * @unit: the device unit for which the resources will be needed
22 * If the device does not support all channel numbers, change @r->channels_mask
23 * after calling this function.
25 int fw_iso_resources_init(struct fw_iso_resources *r, struct fw_unit *unit)
27 r->channels_mask = ~0uLL;
28 r->unit = fw_unit_get(unit);
29 mutex_init(&r->mutex);
36 * fw_iso_resources_destroy - destroy a resource manager
37 * @r: the resource manager that is no longer needed
39 void fw_iso_resources_destroy(struct fw_iso_resources *r)
41 WARN_ON(r->allocated);
42 mutex_destroy(&r->mutex);
46 static unsigned int packet_bandwidth(unsigned int max_payload_bytes, int speed)
48 unsigned int bytes, s400_bytes;
50 /* iso packets have three header quadlets and quadlet-aligned payload */
51 bytes = 3 * 4 + ALIGN(max_payload_bytes, 4);
53 /* convert to bandwidth units (quadlets at S1600 = bytes at S400) */
54 if (speed <= SCODE_400)
55 s400_bytes = bytes * (1 << (SCODE_400 - speed));
57 s400_bytes = DIV_ROUND_UP(bytes, 1 << (speed - SCODE_400));
62 static int current_bandwidth_overhead(struct fw_card *card)
65 * Under the usual pessimistic assumption (cable length 4.5 m), the
66 * isochronous overhead for N cables is 1.797 µs + N * 0.494 µs, or
67 * 88.3 + N * 24.3 in bandwidth units.
69 * The calculation below tries to deduce N from the current gap count.
70 * If the gap count has been optimized by measuring the actual packet
71 * transmission time, this derived overhead should be near the actual
74 return card->gap_count < 63 ? card->gap_count * 97 / 10 + 89 : 512;
77 static int wait_isoch_resource_delay_after_bus_reset(struct fw_card *card)
80 s64 delay = (card->reset_jiffies + HZ) - get_jiffies_64();
83 if (schedule_timeout_interruptible(delay) > 0)
89 * fw_iso_resources_allocate - allocate isochronous channel and bandwidth
90 * @r: the resource manager
91 * @max_payload_bytes: the amount of data (including CIP headers) per packet
92 * @speed: the speed (e.g., SCODE_400) at which the packets will be sent
94 * This function allocates one isochronous channel and enough bandwidth for the
95 * specified packet size.
97 * Returns the channel number that the caller must use for streaming, or
98 * a negative error code. Due to potentionally long delays, this function is
99 * interruptible and can return -ERESTARTSYS. On success, the caller is
100 * responsible for calling fw_iso_resources_update() on bus resets, and
101 * fw_iso_resources_free() when the resources are not longer needed.
103 int fw_iso_resources_allocate(struct fw_iso_resources *r,
104 unsigned int max_payload_bytes, int speed)
106 struct fw_card *card = fw_parent_device(r->unit)->card;
107 int bandwidth, channel, err;
109 if (WARN_ON(r->allocated))
112 r->bandwidth = packet_bandwidth(max_payload_bytes, speed);
114 retry_after_bus_reset:
115 spin_lock_irq(&card->lock);
116 r->generation = card->generation;
117 r->bandwidth_overhead = current_bandwidth_overhead(card);
118 spin_unlock_irq(&card->lock);
120 err = wait_isoch_resource_delay_after_bus_reset(card);
124 mutex_lock(&r->mutex);
126 bandwidth = r->bandwidth + r->bandwidth_overhead;
127 fw_iso_resource_manage(card, r->generation, r->channels_mask,
128 &channel, &bandwidth, true);
129 if (channel == -EAGAIN) {
130 mutex_unlock(&r->mutex);
131 goto retry_after_bus_reset;
134 r->channel = channel;
137 if (channel == -EBUSY)
138 dev_err(&r->unit->device,
139 "isochronous resources exhausted\n");
141 dev_err(&r->unit->device,
142 "isochronous resource allocation failed\n");
145 mutex_unlock(&r->mutex);
151 * fw_iso_resources_update - update resource allocations after a bus reset
152 * @r: the resource manager
154 * This function must be called from the driver's .update handler to reallocate
155 * any resources that were allocated before the bus reset. It is safe to call
156 * this function if no resources are currently allocated.
158 * Returns a negative error code on failure. If this happens, the caller must
161 int fw_iso_resources_update(struct fw_iso_resources *r)
163 struct fw_card *card = fw_parent_device(r->unit)->card;
164 int bandwidth, channel;
166 mutex_lock(&r->mutex);
169 mutex_unlock(&r->mutex);
173 spin_lock_irq(&card->lock);
174 r->generation = card->generation;
175 r->bandwidth_overhead = current_bandwidth_overhead(card);
176 spin_unlock_irq(&card->lock);
178 bandwidth = r->bandwidth + r->bandwidth_overhead;
180 fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
181 &channel, &bandwidth, true);
183 * When another bus reset happens, pretend that the allocation
184 * succeeded; we will try again for the new generation later.
186 if (channel < 0 && channel != -EAGAIN) {
187 r->allocated = false;
188 if (channel == -EBUSY)
189 dev_err(&r->unit->device,
190 "isochronous resources exhausted\n");
192 dev_err(&r->unit->device,
193 "isochronous resource allocation failed\n");
196 mutex_unlock(&r->mutex);
202 * fw_iso_resources_free - frees allocated resources
203 * @r: the resource manager
205 * This function deallocates the channel and bandwidth, if allocated.
207 void fw_iso_resources_free(struct fw_iso_resources *r)
209 struct fw_card *card = fw_parent_device(r->unit)->card;
210 int bandwidth, channel;
212 mutex_lock(&r->mutex);
215 bandwidth = r->bandwidth + r->bandwidth_overhead;
216 fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
217 &channel, &bandwidth, false);
219 dev_err(&r->unit->device,
220 "isochronous resource deallocation failed\n");
222 r->allocated = false;
225 mutex_unlock(&r->mutex);