1
2
3
4
5
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
20
21
22
23
24
25
26int fw_iso_resources_init(struct fw_iso_resources *r, struct fw_unit *unit)
27{
28 r->channels_mask = ~0uLL;
29 r->unit = fw_unit_get(unit);
30 mutex_init(&r->mutex);
31 r->allocated = false;
32
33 return 0;
34}
35EXPORT_SYMBOL(fw_iso_resources_init);
36
37
38
39
40
41void fw_iso_resources_destroy(struct fw_iso_resources *r)
42{
43 WARN_ON(r->allocated);
44 mutex_destroy(&r->mutex);
45 fw_unit_put(r->unit);
46}
47EXPORT_SYMBOL(fw_iso_resources_destroy);
48
49static unsigned int packet_bandwidth(unsigned int max_payload_bytes, int speed)
50{
51 unsigned int bytes, s400_bytes;
52
53
54 bytes = 3 * 4 + ALIGN(max_payload_bytes, 4);
55
56
57 if (speed <= SCODE_400)
58 s400_bytes = bytes * (1 << (SCODE_400 - speed));
59 else
60 s400_bytes = DIV_ROUND_UP(bytes, 1 << (speed - SCODE_400));
61
62 return s400_bytes;
63}
64
65static int current_bandwidth_overhead(struct fw_card *card)
66{
67
68
69
70
71
72
73
74
75
76
77 return card->gap_count < 63 ? card->gap_count * 97 / 10 + 89 : 512;
78}
79
80static int wait_isoch_resource_delay_after_bus_reset(struct fw_card *card)
81{
82 for (;;) {
83 s64 delay = (card->reset_jiffies + HZ) - get_jiffies_64();
84 if (delay <= 0)
85 return 0;
86 if (schedule_timeout_interruptible(delay) > 0)
87 return -ERESTARTSYS;
88 }
89}
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106int fw_iso_resources_allocate(struct fw_iso_resources *r,
107 unsigned int max_payload_bytes, int speed)
108{
109 struct fw_card *card = fw_parent_device(r->unit)->card;
110 int bandwidth, channel, err;
111
112 if (WARN_ON(r->allocated))
113 return -EBADFD;
114
115 r->bandwidth = packet_bandwidth(max_payload_bytes, speed);
116
117retry_after_bus_reset:
118 spin_lock_irq(&card->lock);
119 r->generation = card->generation;
120 r->bandwidth_overhead = current_bandwidth_overhead(card);
121 spin_unlock_irq(&card->lock);
122
123 err = wait_isoch_resource_delay_after_bus_reset(card);
124 if (err < 0)
125 return err;
126
127 mutex_lock(&r->mutex);
128
129 bandwidth = r->bandwidth + r->bandwidth_overhead;
130 fw_iso_resource_manage(card, r->generation, r->channels_mask,
131 &channel, &bandwidth, true);
132 if (channel == -EAGAIN) {
133 mutex_unlock(&r->mutex);
134 goto retry_after_bus_reset;
135 }
136 if (channel >= 0) {
137 r->channel = channel;
138 r->allocated = true;
139 } else {
140 if (channel == -EBUSY)
141 dev_err(&r->unit->device,
142 "isochronous resources exhausted\n");
143 else
144 dev_err(&r->unit->device,
145 "isochronous resource allocation failed\n");
146 }
147
148 mutex_unlock(&r->mutex);
149
150 return channel;
151}
152EXPORT_SYMBOL(fw_iso_resources_allocate);
153
154
155
156
157
158
159
160
161
162
163
164
165int fw_iso_resources_update(struct fw_iso_resources *r)
166{
167 struct fw_card *card = fw_parent_device(r->unit)->card;
168 int bandwidth, channel;
169
170 mutex_lock(&r->mutex);
171
172 if (!r->allocated) {
173 mutex_unlock(&r->mutex);
174 return 0;
175 }
176
177 spin_lock_irq(&card->lock);
178 r->generation = card->generation;
179 r->bandwidth_overhead = current_bandwidth_overhead(card);
180 spin_unlock_irq(&card->lock);
181
182 bandwidth = r->bandwidth + r->bandwidth_overhead;
183
184 fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
185 &channel, &bandwidth, true);
186
187
188
189
190 if (channel < 0 && channel != -EAGAIN) {
191 r->allocated = false;
192 if (channel == -EBUSY)
193 dev_err(&r->unit->device,
194 "isochronous resources exhausted\n");
195 else
196 dev_err(&r->unit->device,
197 "isochronous resource allocation failed\n");
198 }
199
200 mutex_unlock(&r->mutex);
201
202 return channel;
203}
204EXPORT_SYMBOL(fw_iso_resources_update);
205
206
207
208
209
210
211
212void fw_iso_resources_free(struct fw_iso_resources *r)
213{
214 struct fw_card *card = fw_parent_device(r->unit)->card;
215 int bandwidth, channel;
216
217 mutex_lock(&r->mutex);
218
219 if (r->allocated) {
220 bandwidth = r->bandwidth + r->bandwidth_overhead;
221 fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
222 &channel, &bandwidth, false);
223 if (channel < 0)
224 dev_err(&r->unit->device,
225 "isochronous resource deallocation failed\n");
226
227 r->allocated = false;
228 }
229
230 mutex_unlock(&r->mutex);
231}
232EXPORT_SYMBOL(fw_iso_resources_free);
233