^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * helpers for managing a buffer for many packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/firewire.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "packets-buffer.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * iso_packets_buffer_init - allocates the memory for packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * @b: the buffer structure to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * @unit: the device at the other end of the stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * @count: the number of packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * @packet_size: the (maximum) size of a packet, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @direction: %DMA_TO_DEVICE or %DMA_FROM_DEVICE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int iso_packets_buffer_init(struct iso_packets_buffer *b, struct fw_unit *unit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned int count, unsigned int packet_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) enum dma_data_direction direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) unsigned int packets_per_page, pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned int i, page_index, offset_in_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) b->packets = kmalloc_array(count, sizeof(*b->packets), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (!b->packets) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) packet_size = L1_CACHE_ALIGN(packet_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) packets_per_page = PAGE_SIZE / packet_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (WARN_ON(!packets_per_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) goto err_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) pages = DIV_ROUND_UP(count, packets_per_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) err = fw_iso_buffer_init(&b->iso_buffer, fw_parent_device(unit)->card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) pages, direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) goto err_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) for (i = 0; i < count; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) page_index = i / packets_per_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) p = page_address(b->iso_buffer.pages[page_index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) offset_in_page = (i % packets_per_page) * packet_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) b->packets[i].buffer = p + offset_in_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) b->packets[i].offset = page_index * PAGE_SIZE + offset_in_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) err_packets:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) kfree(b->packets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) EXPORT_SYMBOL(iso_packets_buffer_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * iso_packets_buffer_destroy - frees packet buffer resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * @b: the buffer structure to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * @unit: the device at the other end of the stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) void iso_packets_buffer_destroy(struct iso_packets_buffer *b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct fw_unit *unit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) fw_iso_buffer_destroy(&b->iso_buffer, fw_parent_device(unit)->card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) kfree(b->packets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) EXPORT_SYMBOL(iso_packets_buffer_destroy);