blob: 9d4a6714f9ecf724f0f213249e8631fee30af517 [file] [log] [blame]
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001/*
2 * isochronous resources helper functions
3 *
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
6 */
7
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>
Clemens Ladisch5b2599a2011-03-15 07:55:50 +010014#include <linux/slab.h>
Clemens Ladisch31ef9132011-03-15 07:53:21 +010015#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 */
Clemens Ladisch5b2599a2011-03-15 07:55:50 +010026int fw_iso_resources_init(struct fw_iso_resources *r, struct fw_unit *unit)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010027{
Clemens Ladisch5b2599a2011-03-15 07:55:50 +010028 r->buffer = kmalloc(2 * 4, GFP_KERNEL);
29 if (!r->buffer)
30 return -ENOMEM;
31
Clemens Ladisch31ef9132011-03-15 07:53:21 +010032 r->channels_mask = ~0uLL;
33 r->unit = fw_unit_get(unit);
34 mutex_init(&r->mutex);
35 r->allocated = false;
Clemens Ladisch5b2599a2011-03-15 07:55:50 +010036
37 return 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010038}
Clemens Ladisch3a691b22011-05-11 10:44:51 +020039EXPORT_SYMBOL(fw_iso_resources_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010040
41/**
42 * fw_iso_resources_destroy - destroy a resource manager
43 * @r: the resource manager that is no longer needed
44 */
45void fw_iso_resources_destroy(struct fw_iso_resources *r)
46{
47 WARN_ON(r->allocated);
Clemens Ladisch5b2599a2011-03-15 07:55:50 +010048 kfree(r->buffer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010049 mutex_destroy(&r->mutex);
50 fw_unit_put(r->unit);
51}
Clemens Ladisch3a691b22011-05-11 10:44:51 +020052EXPORT_SYMBOL(fw_iso_resources_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010053
54static unsigned int packet_bandwidth(unsigned int max_payload_bytes, int speed)
55{
56 unsigned int bytes, s400_bytes;
57
58 /* iso packets have three header quadlets and quadlet-aligned payload */
59 bytes = 3 * 4 + ALIGN(max_payload_bytes, 4);
60
61 /* convert to bandwidth units (quadlets at S1600 = bytes at S400) */
62 if (speed <= SCODE_400)
63 s400_bytes = bytes * (1 << (SCODE_400 - speed));
64 else
65 s400_bytes = DIV_ROUND_UP(bytes, 1 << (speed - SCODE_400));
66
67 return s400_bytes;
68}
69
70static int current_bandwidth_overhead(struct fw_card *card)
71{
72 /*
73 * Under the usual pessimistic assumption (cable length 4.5 m), the
74 * isochronous overhead for N cables is 1.797 µs + N * 0.494 µs, or
75 * 88.3 + N * 24.3 in bandwidth units.
76 *
77 * The calculation below tries to deduce N from the current gap count.
78 * If the gap count has been optimized by measuring the actual packet
79 * transmission time, this derived overhead should be near the actual
80 * overhead as well.
81 */
82 return card->gap_count < 63 ? card->gap_count * 97 / 10 + 89 : 512;
83}
84
85static int wait_isoch_resource_delay_after_bus_reset(struct fw_card *card)
86{
87 for (;;) {
88 s64 delay = (card->reset_jiffies + HZ) - get_jiffies_64();
89 if (delay <= 0)
90 return 0;
91 if (schedule_timeout_interruptible(delay) > 0)
92 return -ERESTARTSYS;
93 }
94}
95
96/**
97 * fw_iso_resources_allocate - allocate isochronous channel and bandwidth
98 * @r: the resource manager
99 * @max_payload_bytes: the amount of data (including CIP headers) per packet
100 * @speed: the speed (e.g., SCODE_400) at which the packets will be sent
101 *
102 * This function allocates one isochronous channel and enough bandwidth for the
103 * specified packet size.
104 *
105 * Returns the channel number that the caller must use for streaming, or
106 * a negative error code. Due to potentionally long delays, this function is
107 * interruptible and can return -ERESTARTSYS. On success, the caller is
108 * responsible for calling fw_iso_resources_update() on bus resets, and
109 * fw_iso_resources_free() when the resources are not longer needed.
110 */
111int fw_iso_resources_allocate(struct fw_iso_resources *r,
112 unsigned int max_payload_bytes, int speed)
113{
114 struct fw_card *card = fw_parent_device(r->unit)->card;
115 int bandwidth, channel, err;
116
117 if (WARN_ON(r->allocated))
118 return -EBADFD;
119
120 r->bandwidth = packet_bandwidth(max_payload_bytes, speed);
121
122retry_after_bus_reset:
123 spin_lock_irq(&card->lock);
124 r->generation = card->generation;
125 r->bandwidth_overhead = current_bandwidth_overhead(card);
126 spin_unlock_irq(&card->lock);
127
128 err = wait_isoch_resource_delay_after_bus_reset(card);
129 if (err < 0)
130 return err;
131
132 mutex_lock(&r->mutex);
133
134 bandwidth = r->bandwidth + r->bandwidth_overhead;
135 fw_iso_resource_manage(card, r->generation, r->channels_mask,
136 &channel, &bandwidth, true, r->buffer);
137 if (channel == -EAGAIN) {
138 mutex_unlock(&r->mutex);
139 goto retry_after_bus_reset;
140 }
141 if (channel >= 0) {
142 r->channel = channel;
143 r->allocated = true;
144 } else {
145 if (channel == -EBUSY)
146 dev_err(&r->unit->device,
147 "isochronous resources exhausted\n");
148 else
149 dev_err(&r->unit->device,
150 "isochronous resource allocation failed\n");
151 }
152
153 mutex_unlock(&r->mutex);
154
155 return channel;
156}
Clemens Ladisch3a691b22011-05-11 10:44:51 +0200157EXPORT_SYMBOL(fw_iso_resources_allocate);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100158
159/**
160 * fw_iso_resources_update - update resource allocations after a bus reset
161 * @r: the resource manager
162 *
163 * This function must be called from the driver's .update handler to reallocate
164 * any resources that were allocated before the bus reset. It is safe to call
165 * this function if no resources are currently allocated.
166 *
167 * Returns a negative error code on failure. If this happens, the caller must
168 * stop streaming.
169 */
170int fw_iso_resources_update(struct fw_iso_resources *r)
171{
172 struct fw_card *card = fw_parent_device(r->unit)->card;
173 int bandwidth, channel;
174
175 mutex_lock(&r->mutex);
176
177 if (!r->allocated) {
178 mutex_unlock(&r->mutex);
179 return 0;
180 }
181
182 spin_lock_irq(&card->lock);
183 r->generation = card->generation;
184 r->bandwidth_overhead = current_bandwidth_overhead(card);
185 spin_unlock_irq(&card->lock);
186
187 bandwidth = r->bandwidth + r->bandwidth_overhead;
188
189 fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
190 &channel, &bandwidth, true, r->buffer);
191 /*
192 * When another bus reset happens, pretend that the allocation
193 * succeeded; we will try again for the new generation later.
194 */
195 if (channel < 0 && channel != -EAGAIN) {
196 r->allocated = false;
197 if (channel == -EBUSY)
198 dev_err(&r->unit->device,
199 "isochronous resources exhausted\n");
200 else
201 dev_err(&r->unit->device,
202 "isochronous resource allocation failed\n");
203 }
204
205 mutex_unlock(&r->mutex);
206
207 return channel;
208}
Clemens Ladisch3a691b22011-05-11 10:44:51 +0200209EXPORT_SYMBOL(fw_iso_resources_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100210
211/**
212 * fw_iso_resources_free - frees allocated resources
213 * @r: the resource manager
214 *
215 * This function deallocates the channel and bandwidth, if allocated.
216 */
217void fw_iso_resources_free(struct fw_iso_resources *r)
218{
219 struct fw_card *card = fw_parent_device(r->unit)->card;
220 int bandwidth, channel;
221
222 mutex_lock(&r->mutex);
223
224 if (r->allocated) {
225 bandwidth = r->bandwidth + r->bandwidth_overhead;
226 fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
227 &channel, &bandwidth, false, r->buffer);
228 if (channel < 0)
229 dev_err(&r->unit->device,
230 "isochronous resource deallocation failed\n");
231
232 r->allocated = false;
233 }
234
235 mutex_unlock(&r->mutex);
236}
Clemens Ladisch3a691b22011-05-11 10:44:51 +0200237EXPORT_SYMBOL(fw_iso_resources_free);