xref: /linux/drivers/firewire/core-iso.c (revision 1b8b67503725fa6deb3a4f00ea61f982bd00c152)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Isochronous I/O functionality:
4  *   - Isochronous DMA context management
5  *   - Isochronous bus resource management (channels, bandwidth), client side
6  *
7  * Copyright (C) 2006 Kristian Hoegsberg <krh@bitplanet.net>
8  */
9 
10 #include <linux/dma-mapping.h>
11 #include <linux/errno.h>
12 #include <linux/firewire.h>
13 #include <linux/firewire-constants.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/vmalloc.h>
19 #include <linux/export.h>
20 
21 #include <asm/byteorder.h>
22 
23 #include "core.h"
24 
25 #include <trace/events/firewire.h>
26 
27 /*
28  * Isochronous DMA context management
29  */
30 
31 int fw_iso_buffer_alloc(struct fw_iso_buffer *buffer, int page_count)
32 {
33 	struct page **page_array __free(kfree) = kzalloc_objs(page_array[0],
34 							      page_count);
35 
36 	if (!page_array)
37 		return -ENOMEM;
38 
39 	// Retrieve noncontiguous pages. The descriptors for 1394 OHCI isochronous DMA contexts
40 	// have a set of address and length per each, while the reason to use pages is the
41 	// convenience to map them into virtual address space of user process.
42 	unsigned long nr_populated = alloc_pages_bulk(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO,
43 						      page_count, page_array);
44 	if (nr_populated != page_count) {
45 		// Assuming the above call fills page_array sequentially from the beginning.
46 		release_pages(page_array, nr_populated);
47 		return -ENOMEM;
48 	}
49 
50 	buffer->page_count = page_count;
51 	buffer->pages = no_free_ptr(page_array);
52 
53 	return 0;
54 }
55 
56 int fw_iso_buffer_map_dma(struct fw_iso_buffer *buffer, struct fw_card *card,
57 			  enum dma_data_direction direction)
58 {
59 	dma_addr_t *dma_addrs __free(kfree) = kzalloc_objs(dma_addrs[0],
60 							   buffer->page_count);
61 	int i;
62 
63 	if (!dma_addrs)
64 		return -ENOMEM;
65 
66 	// Retrieve DMA mapping addresses for the pages. They are not contiguous. Maintain the cache
67 	// coherency for the pages by hand.
68 	for (i = 0; i < buffer->page_count; i++) {
69 		// The dma_map_phys() with a physical address per page is available here, instead.
70 		dma_addr_t dma_addr = dma_map_page(card->device, buffer->pages[i], 0, PAGE_SIZE,
71 						   direction);
72 		if (dma_mapping_error(card->device, dma_addr))
73 			break;
74 
75 		dma_addrs[i] = dma_addr;
76 	}
77 	if (i < buffer->page_count) {
78 		while (i-- > 0)
79 			dma_unmap_page(card->device, dma_addrs[i], PAGE_SIZE, buffer->direction);
80 		return -ENOMEM;
81 	}
82 
83 	buffer->direction = direction;
84 	buffer->dma_addrs = no_free_ptr(dma_addrs);
85 
86 	return 0;
87 }
88 
89 int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
90 		       int page_count, enum dma_data_direction direction)
91 {
92 	int ret;
93 
94 	ret = fw_iso_buffer_alloc(buffer, page_count);
95 	if (ret < 0)
96 		return ret;
97 
98 	ret = fw_iso_buffer_map_dma(buffer, card, direction);
99 	if (ret < 0)
100 		fw_iso_buffer_destroy(buffer, card);
101 
102 	return ret;
103 }
104 EXPORT_SYMBOL(fw_iso_buffer_init);
105 
106 void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer,
107 			   struct fw_card *card)
108 {
109 	if (buffer->dma_addrs) {
110 		for (int i = 0; i < buffer->page_count; ++i) {
111 			dma_addr_t dma_addr = buffer->dma_addrs[i];
112 			dma_unmap_page(card->device, dma_addr, PAGE_SIZE, buffer->direction);
113 		}
114 		kfree(buffer->dma_addrs);
115 		buffer->dma_addrs = NULL;
116 	}
117 
118 	if (buffer->pages) {
119 		release_pages(buffer->pages, buffer->page_count);
120 		kfree(buffer->pages);
121 		buffer->pages = NULL;
122 	}
123 
124 	buffer->page_count = 0;
125 }
126 EXPORT_SYMBOL(fw_iso_buffer_destroy);
127 
128 /* Convert DMA address to offset into virtually contiguous buffer. */
129 size_t fw_iso_buffer_lookup(struct fw_iso_buffer *buffer, dma_addr_t completed)
130 {
131 	for (int i = 0; i < buffer->page_count; i++) {
132 		dma_addr_t dma_addr = buffer->dma_addrs[i];
133 		ssize_t offset = (ssize_t)completed - (ssize_t)dma_addr;
134 		if (offset > 0 && offset <= PAGE_SIZE)
135 			return (i << PAGE_SHIFT) + offset;
136 	}
137 
138 	return 0;
139 }
140 
141 struct fw_iso_context *__fw_iso_context_create(struct fw_card *card, int type, int channel,
142 		int speed, size_t header_size, size_t header_storage_size,
143 		union fw_iso_callback callback, void *callback_data)
144 {
145 	struct fw_iso_context *ctx;
146 
147 	ctx = smp_load_acquire(&card->driver)->allocate_iso_context(card, type, channel,
148 							header_size, header_storage_size);
149 	if (IS_ERR(ctx))
150 		return ctx;
151 
152 	ctx->card = card;
153 	ctx->type = type;
154 	ctx->channel = channel;
155 	ctx->speed = speed;
156 	ctx->flags = 0;
157 	ctx->header_size = header_size;
158 	ctx->header_storage_size = header_storage_size;
159 	ctx->callback = callback;
160 	ctx->callback_data = callback_data;
161 
162 	trace_isoc_outbound_allocate(ctx, channel, speed);
163 	trace_isoc_inbound_single_allocate(ctx, channel, header_size);
164 	trace_isoc_inbound_multiple_allocate(ctx);
165 
166 	return ctx;
167 }
168 EXPORT_SYMBOL(__fw_iso_context_create);
169 
170 void fw_iso_context_destroy(struct fw_iso_context *ctx)
171 {
172 	trace_isoc_outbound_destroy(ctx);
173 	trace_isoc_inbound_single_destroy(ctx);
174 	trace_isoc_inbound_multiple_destroy(ctx);
175 
176 	struct fw_card *card = ctx->card;
177 
178 	smp_load_acquire(&card->driver)->free_iso_context(ctx);
179 }
180 EXPORT_SYMBOL(fw_iso_context_destroy);
181 
182 int fw_iso_context_start(struct fw_iso_context *ctx,
183 			 int cycle, int sync, int tags)
184 {
185 	trace_isoc_outbound_start(ctx, cycle);
186 	trace_isoc_inbound_single_start(ctx, cycle, sync, tags);
187 	trace_isoc_inbound_multiple_start(ctx, cycle, sync, tags);
188 
189 	struct fw_card *card = ctx->card;
190 
191 	return smp_load_acquire(&card->driver)->start_iso(ctx, cycle, sync, tags);
192 }
193 EXPORT_SYMBOL(fw_iso_context_start);
194 
195 int fw_iso_context_set_channels(struct fw_iso_context *ctx, u64 *channels)
196 {
197 	trace_isoc_inbound_multiple_channels(ctx, *channels);
198 
199 	struct fw_card *card = ctx->card;
200 
201 	return smp_load_acquire(&card->driver)->set_iso_channels(ctx, channels);
202 }
203 
204 int fw_iso_context_queue(struct fw_iso_context *ctx,
205 			 struct fw_iso_packet *packet,
206 			 struct fw_iso_buffer *buffer,
207 			 unsigned long payload)
208 {
209 	trace_isoc_outbound_queue(ctx, payload, packet);
210 	trace_isoc_inbound_single_queue(ctx, payload, packet);
211 	trace_isoc_inbound_multiple_queue(ctx, payload, packet);
212 
213 	struct fw_card *card = ctx->card;
214 
215 	return smp_load_acquire(&card->driver)->queue_iso(ctx, packet, buffer, payload);
216 }
217 EXPORT_SYMBOL(fw_iso_context_queue);
218 
219 void fw_iso_context_queue_flush(struct fw_iso_context *ctx)
220 {
221 	trace_isoc_outbound_flush(ctx);
222 	trace_isoc_inbound_single_flush(ctx);
223 	trace_isoc_inbound_multiple_flush(ctx);
224 
225 	struct fw_card *card = ctx->card;
226 
227 	smp_load_acquire(&card->driver)->flush_queue_iso(ctx);
228 }
229 EXPORT_SYMBOL(fw_iso_context_queue_flush);
230 
231 /**
232  * fw_iso_context_flush_completions() - process isochronous context in current process context.
233  * @ctx: the isochronous context
234  *
235  * Process the isochronous context in the current process context. The registered callback function
236  * is called when a queued packet buffer with the interrupt flag is completed, either after
237  * transmission in the IT context or after being filled in the IR context. Additionally, the
238  * callback function is also called for the packet buffer completed at last. Furthermore, the
239  * callback function is called as well when the header buffer in the context becomes full. If it is
240  * required to process the context asynchronously, fw_iso_context_schedule_flush_completions() is
241  * available instead.
242  *
243  * Context: Process context. May sleep due to disable_work_sync().
244  */
245 int fw_iso_context_flush_completions(struct fw_iso_context *ctx)
246 {
247 	int err;
248 
249 	trace_isoc_outbound_flush_completions(ctx);
250 	trace_isoc_inbound_single_flush_completions(ctx);
251 	trace_isoc_inbound_multiple_flush_completions(ctx);
252 
253 	might_sleep();
254 
255 	// Avoid dead lock due to programming mistake.
256 	if (WARN_ON_ONCE(current_work() == &ctx->work))
257 		return 0;
258 
259 	disable_work_sync(&ctx->work);
260 
261 	struct fw_card *card = ctx->card;
262 
263 	err = smp_load_acquire(&card->driver)->flush_iso_completions(ctx);
264 
265 	enable_work(&ctx->work);
266 
267 	return err;
268 }
269 EXPORT_SYMBOL(fw_iso_context_flush_completions);
270 
271 int fw_iso_context_stop(struct fw_iso_context *ctx)
272 {
273 	int err;
274 
275 	trace_isoc_outbound_stop(ctx);
276 	trace_isoc_inbound_single_stop(ctx);
277 	trace_isoc_inbound_multiple_stop(ctx);
278 
279 	might_sleep();
280 
281 	// Avoid dead lock due to programming mistake.
282 	if (WARN_ON_ONCE(current_work() == &ctx->work))
283 		return 0;
284 
285 	struct fw_card *card = ctx->card;
286 
287 	err = smp_load_acquire(&card->driver)->stop_iso(ctx);
288 
289 	cancel_work_sync(&ctx->work);
290 
291 	return err;
292 }
293 EXPORT_SYMBOL(fw_iso_context_stop);
294 
295 /*
296  * Isochronous bus resource management (channels, bandwidth), client side
297  */
298 
299 static int manage_bandwidth(struct fw_card *card, int irm_id, int generation,
300 			    int bandwidth, bool allocate)
301 {
302 	int try, new, old = allocate ? BANDWIDTH_AVAILABLE_INITIAL : 0;
303 	__be32 data[2];
304 
305 	/*
306 	 * On a 1394a IRM with low contention, try < 1 is enough.
307 	 * On a 1394-1995 IRM, we need at least try < 2.
308 	 * Let's just do try < 5.
309 	 */
310 	for (try = 0; try < 5; try++) {
311 		new = allocate ? old - bandwidth : old + bandwidth;
312 		if (new < 0 || new > BANDWIDTH_AVAILABLE_INITIAL)
313 			return -EBUSY;
314 
315 		data[0] = cpu_to_be32(old);
316 		data[1] = cpu_to_be32(new);
317 		switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
318 				irm_id, generation, SCODE_100,
319 				CSR_REGISTER_BASE + CSR_BANDWIDTH_AVAILABLE,
320 				data, 8)) {
321 		case RCODE_GENERATION:
322 			/* A generation change frees all bandwidth. */
323 			return allocate ? -EAGAIN : bandwidth;
324 
325 		case RCODE_COMPLETE:
326 			if (be32_to_cpup(data) == old)
327 				return bandwidth;
328 
329 			old = be32_to_cpup(data);
330 			/* Fall through. */
331 		}
332 	}
333 
334 	return -EIO;
335 }
336 
337 static int manage_channel(struct fw_card *card, int irm_id, int generation,
338 		u32 channels_mask, u64 offset, bool allocate)
339 {
340 	__be32 bit, all, old;
341 	__be32 data[2];
342 	int channel, ret = -EIO, retry = 5;
343 
344 	old = all = allocate ? cpu_to_be32(~0) : 0;
345 
346 	for (channel = 0; channel < 32; channel++) {
347 		if (!(channels_mask & 1 << channel))
348 			continue;
349 
350 		ret = -EBUSY;
351 
352 		bit = cpu_to_be32(1 << (31 - channel));
353 		if ((old & bit) != (all & bit))
354 			continue;
355 
356 		data[0] = old;
357 		data[1] = old ^ bit;
358 		switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
359 					   irm_id, generation, SCODE_100,
360 					   offset, data, 8)) {
361 		case RCODE_GENERATION:
362 			/* A generation change frees all channels. */
363 			return allocate ? -EAGAIN : channel;
364 
365 		case RCODE_COMPLETE:
366 			if (data[0] == old)
367 				return channel;
368 
369 			old = data[0];
370 
371 			/* Is the IRM 1394a-2000 compliant? */
372 			if ((data[0] & bit) == (data[1] & bit))
373 				continue;
374 
375 			fallthrough;	/* It's a 1394-1995 IRM, retry */
376 		default:
377 			if (retry) {
378 				retry--;
379 				channel--;
380 			} else {
381 				ret = -EIO;
382 			}
383 		}
384 	}
385 
386 	return ret;
387 }
388 
389 static void deallocate_channel(struct fw_card *card, int irm_id,
390 			       int generation, int channel)
391 {
392 	u32 mask;
393 	u64 offset;
394 
395 	mask = channel < 32 ? 1 << channel : 1 << (channel - 32);
396 	offset = channel < 32 ? CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI :
397 				CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO;
398 
399 	manage_channel(card, irm_id, generation, mask, offset, false);
400 }
401 
402 /**
403  * fw_iso_resource_manage() - Allocate or deallocate a channel and/or bandwidth
404  * @card: card interface for this action
405  * @generation: bus generation
406  * @channels_mask: bitmask for channel allocation
407  * @channel: pointer for returning channel allocation result
408  * @bandwidth: pointer for returning bandwidth allocation result
409  * @allocate: whether to allocate (true) or deallocate (false)
410  *
411  * In parameters: card, generation, channels_mask, bandwidth, allocate
412  * Out parameters: channel, bandwidth
413  *
414  * This function blocks (sleeps) during communication with the IRM.
415  *
416  * Allocates or deallocates at most one channel out of channels_mask.
417  * channels_mask is a bitfield with MSB for channel 63 and LSB for channel 0.
418  * (Note, the IRM's CHANNELS_AVAILABLE is a big-endian bitfield with MSB for
419  * channel 0 and LSB for channel 63.)
420  * Allocates or deallocates as many bandwidth allocation units as specified.
421  *
422  * Returns channel < 0 if no channel was allocated or deallocated.
423  * Returns bandwidth = 0 if no bandwidth was allocated or deallocated.
424  *
425  * If generation is stale, deallocations succeed but allocations fail with
426  * channel = -EAGAIN.
427  *
428  * If channel allocation fails, no bandwidth will be allocated either.
429  * If bandwidth allocation fails, no channel will be allocated either.
430  * But deallocations of channel and bandwidth are tried independently
431  * of each other's success.
432  */
433 void fw_iso_resource_manage(struct fw_card *card, int generation,
434 			    u64 channels_mask, int *channel, int *bandwidth,
435 			    bool allocate)
436 {
437 	u32 channels_hi = channels_mask;	/* channels 31...0 */
438 	u32 channels_lo = channels_mask >> 32;	/* channels 63...32 */
439 	int irm_id, ret, c = -EINVAL;
440 
441 	scoped_guard(spinlock_irq, &card->lock)
442 		irm_id = card->irm_node->node_id;
443 
444 	if (channels_hi)
445 		c = manage_channel(card, irm_id, generation, channels_hi,
446 				CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI,
447 				allocate);
448 	if (channels_lo && c < 0) {
449 		c = manage_channel(card, irm_id, generation, channels_lo,
450 				CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO,
451 				allocate);
452 		if (c >= 0)
453 			c += 32;
454 	}
455 	*channel = c;
456 
457 	if (allocate && channels_mask != 0 && c < 0)
458 		*bandwidth = 0;
459 
460 	if (*bandwidth == 0)
461 		return;
462 
463 	ret = manage_bandwidth(card, irm_id, generation, *bandwidth, allocate);
464 	if (ret < 0)
465 		*bandwidth = 0;
466 
467 	if (allocate && ret < 0) {
468 		if (c >= 0)
469 			deallocate_channel(card, irm_id, generation, c);
470 		*channel = ret;
471 	}
472 }
473 EXPORT_SYMBOL(fw_iso_resource_manage);
474