18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * helpers for managing a buffer for many packets
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/firewire.h>
98c2ecf20Sopenharmony_ci#include <linux/export.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include "packets-buffer.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/**
148c2ecf20Sopenharmony_ci * iso_packets_buffer_init - allocates the memory for packets
158c2ecf20Sopenharmony_ci * @b: the buffer structure to initialize
168c2ecf20Sopenharmony_ci * @unit: the device at the other end of the stream
178c2ecf20Sopenharmony_ci * @count: the number of packets
188c2ecf20Sopenharmony_ci * @packet_size: the (maximum) size of a packet, in bytes
198c2ecf20Sopenharmony_ci * @direction: %DMA_TO_DEVICE or %DMA_FROM_DEVICE
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ciint iso_packets_buffer_init(struct iso_packets_buffer *b, struct fw_unit *unit,
228c2ecf20Sopenharmony_ci			    unsigned int count, unsigned int packet_size,
238c2ecf20Sopenharmony_ci			    enum dma_data_direction direction)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	unsigned int packets_per_page, pages;
268c2ecf20Sopenharmony_ci	unsigned int i, page_index, offset_in_page;
278c2ecf20Sopenharmony_ci	void *p;
288c2ecf20Sopenharmony_ci	int err;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	b->packets = kmalloc_array(count, sizeof(*b->packets), GFP_KERNEL);
318c2ecf20Sopenharmony_ci	if (!b->packets) {
328c2ecf20Sopenharmony_ci		err = -ENOMEM;
338c2ecf20Sopenharmony_ci		goto error;
348c2ecf20Sopenharmony_ci	}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	packet_size = L1_CACHE_ALIGN(packet_size);
378c2ecf20Sopenharmony_ci	packets_per_page = PAGE_SIZE / packet_size;
388c2ecf20Sopenharmony_ci	if (WARN_ON(!packets_per_page)) {
398c2ecf20Sopenharmony_ci		err = -EINVAL;
408c2ecf20Sopenharmony_ci		goto err_packets;
418c2ecf20Sopenharmony_ci	}
428c2ecf20Sopenharmony_ci	pages = DIV_ROUND_UP(count, packets_per_page);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	err = fw_iso_buffer_init(&b->iso_buffer, fw_parent_device(unit)->card,
458c2ecf20Sopenharmony_ci				 pages, direction);
468c2ecf20Sopenharmony_ci	if (err < 0)
478c2ecf20Sopenharmony_ci		goto err_packets;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	for (i = 0; i < count; ++i) {
508c2ecf20Sopenharmony_ci		page_index = i / packets_per_page;
518c2ecf20Sopenharmony_ci		p = page_address(b->iso_buffer.pages[page_index]);
528c2ecf20Sopenharmony_ci		offset_in_page = (i % packets_per_page) * packet_size;
538c2ecf20Sopenharmony_ci		b->packets[i].buffer = p + offset_in_page;
548c2ecf20Sopenharmony_ci		b->packets[i].offset = page_index * PAGE_SIZE + offset_in_page;
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return 0;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cierr_packets:
608c2ecf20Sopenharmony_ci	kfree(b->packets);
618c2ecf20Sopenharmony_cierror:
628c2ecf20Sopenharmony_ci	return err;
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ciEXPORT_SYMBOL(iso_packets_buffer_init);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/**
678c2ecf20Sopenharmony_ci * iso_packets_buffer_destroy - frees packet buffer resources
688c2ecf20Sopenharmony_ci * @b: the buffer structure to free
698c2ecf20Sopenharmony_ci * @unit: the device at the other end of the stream
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_civoid iso_packets_buffer_destroy(struct iso_packets_buffer *b,
728c2ecf20Sopenharmony_ci				struct fw_unit *unit)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	fw_iso_buffer_destroy(&b->iso_buffer, fw_parent_device(unit)->card);
758c2ecf20Sopenharmony_ci	kfree(b->packets);
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ciEXPORT_SYMBOL(iso_packets_buffer_destroy);
78