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