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