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 = 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}
46EXPORT_SYMBOL(fw_iso_resources_destroy);
47
48static unsigned int packet_bandwidth(unsigned int max_payload_bytes, int speed)
49{
50 unsigned int bytes, s400_bytes;
51
52
53 bytes = 3 * 4 + ALIGN(max_payload_bytes, 4);
54
55
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
64static int current_bandwidth_overhead(struct fw_card *card)
65{
66
67
68
69
70
71
72
73
74
75
76 return card->gap_count < 63 ? card->gap_count * 97 / 10 + 89 : 512;
77}
78
79static 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105int 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
116retry_after_bus_reset:
117 spin_lock_irq(&card->lock);
118 r->generation = card->generation;
119 r->bandwidth_overhead = current_bandwidth_overhead(card);
120 spin_unlock_irq(&card->lock);
121
122 err = wait_isoch_resource_delay_after_bus_reset(card);
123 if (err < 0)
124 return err;
125
126 mutex_lock(&r->mutex);
127
128 bandwidth = r->bandwidth + r->bandwidth_overhead;
129 fw_iso_resource_manage(card, r->generation, r->channels_mask,
130 &channel, &bandwidth, true);
131 if (channel == -EAGAIN) {
132 mutex_unlock(&r->mutex);
133 goto retry_after_bus_reset;
134 }
135 if (channel >= 0) {
136 r->channel = channel;
137 r->allocated = true;
138 } else {
139 if (channel == -EBUSY)
140 dev_err(&r->unit->device,
141 "isochronous resources exhausted\n");
142 else
143 dev_err(&r->unit->device,
144 "isochronous resource allocation failed\n");
145 }
146
147 mutex_unlock(&r->mutex);
148
149 return channel;
150}
151EXPORT_SYMBOL(fw_iso_resources_allocate);
152
153
154
155
156
157
158
159
160
161
162
163
164int fw_iso_resources_update(struct fw_iso_resources *r)
165{
166 struct fw_card *card = fw_parent_device(r->unit)->card;
167 int bandwidth, channel;
168
169 mutex_lock(&r->mutex);
170
171 if (!r->allocated) {
172 mutex_unlock(&r->mutex);
173 return 0;
174 }
175
176 spin_lock_irq(&card->lock);
177 r->generation = card->generation;
178 r->bandwidth_overhead = current_bandwidth_overhead(card);
179 spin_unlock_irq(&card->lock);
180
181 bandwidth = r->bandwidth + r->bandwidth_overhead;
182
183 fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
184 &channel, &bandwidth, true);
185
186
187
188
189 if (channel < 0 && channel != -EAGAIN) {
190 r->allocated = false;
191 if (channel == -EBUSY)
192 dev_err(&r->unit->device,
193 "isochronous resources exhausted\n");
194 else
195 dev_err(&r->unit->device,
196 "isochronous resource allocation failed\n");
197 }
198
199 mutex_unlock(&r->mutex);
200
201 return channel;
202}
203EXPORT_SYMBOL(fw_iso_resources_update);
204
205
206
207
208
209
210
211void fw_iso_resources_free(struct fw_iso_resources *r)
212{
213 struct fw_card *card;
214 int bandwidth, channel;
215
216
217 if (r->unit == NULL)
218 return;
219 card = fw_parent_device(r->unit)->card;
220
221 mutex_lock(&r->mutex);
222
223 if (r->allocated) {
224 bandwidth = r->bandwidth + r->bandwidth_overhead;
225 fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
226 &channel, &bandwidth, false);
227 if (channel < 0)
228 dev_err(&r->unit->device,
229 "isochronous resource deallocation failed\n");
230
231 r->allocated = false;
232 }
233
234 mutex_unlock(&r->mutex);
235}
236EXPORT_SYMBOL(fw_iso_resources_free);
237