core-iso.c 9.6 KB
Newer Older
1
/*
2 3 4
 * Isochronous I/O functionality:
 *   - Isochronous DMA context management
 *   - Isochronous bus resource management (channels, bandwidth), client side
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Copyright (C) 2006 Kristian Hoegsberg <krh@bitplanet.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <linux/dma-mapping.h>
24
#include <linux/errno.h>
25
#include <linux/firewire.h>
26 27
#include <linux/firewire-constants.h>
#include <linux/kernel.h>
28
#include <linux/mm.h>
29
#include <linux/slab.h>
30 31
#include <linux/spinlock.h>
#include <linux/vmalloc.h>
32

S
Stefan Richter 已提交
33 34
#include <asm/byteorder.h>

35
#include "core.h"
36 37 38 39

/*
 * Isochronous DMA context management
 */
40

41 42
int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
		       int page_count, enum dma_data_direction direction)
43
{
44
	int i, j;
45 46 47 48 49 50 51 52 53 54 55
	dma_addr_t address;

	buffer->page_count = page_count;
	buffer->direction = direction;

	buffer->pages = kmalloc(page_count * sizeof(buffer->pages[0]),
				GFP_KERNEL);
	if (buffer->pages == NULL)
		goto out;

	for (i = 0; i < buffer->page_count; i++) {
56
		buffer->pages[i] = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
57 58
		if (buffer->pages[i] == NULL)
			goto out_pages;
S
Stefan Richter 已提交
59

60 61
		address = dma_map_page(card->device, buffer->pages[i],
				       0, PAGE_SIZE, direction);
62
		if (dma_mapping_error(card->device, address)) {
63 64 65 66
			__free_page(buffer->pages[i]);
			goto out_pages;
		}
		set_page_private(buffer->pages[i], address);
67 68 69
	}

	return 0;
70

71 72 73 74
 out_pages:
	for (j = 0; j < i; j++) {
		address = page_private(buffer->pages[j]);
		dma_unmap_page(card->device, address,
75
			       PAGE_SIZE, direction);
76 77 78 79 80
		__free_page(buffer->pages[j]);
	}
	kfree(buffer->pages);
 out:
	buffer->pages = NULL;
81

82
	return -ENOMEM;
83
}
J
Jay Fenlason 已提交
84
EXPORT_SYMBOL(fw_iso_buffer_init);
85 86 87 88

int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma)
{
	unsigned long uaddr;
89
	int i, err;
90 91 92

	uaddr = vma->vm_start;
	for (i = 0; i < buffer->page_count; i++) {
93 94 95 96
		err = vm_insert_page(vma, uaddr, buffer->pages[i]);
		if (err)
			return err;

97 98 99 100
		uaddr += PAGE_SIZE;
	}

	return 0;
101 102
}

103 104
void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer,
			   struct fw_card *card)
105 106
{
	int i;
107
	dma_addr_t address;
108

109 110 111
	for (i = 0; i < buffer->page_count; i++) {
		address = page_private(buffer->pages[i]);
		dma_unmap_page(card->device, address,
112
			       PAGE_SIZE, buffer->direction);
113 114
		__free_page(buffer->pages[i]);
	}
115

116 117
	kfree(buffer->pages);
	buffer->pages = NULL;
118
}
J
Jay Fenlason 已提交
119
EXPORT_SYMBOL(fw_iso_buffer_destroy);
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
/* Convert DMA address to offset into virtually contiguous buffer. */
size_t fw_iso_buffer_lookup(struct fw_iso_buffer *buffer, dma_addr_t completed)
{
	int i;
	dma_addr_t address;
	ssize_t offset;

	for (i = 0; i < buffer->page_count; i++) {
		address = page_private(buffer->pages[i]);
		offset = (ssize_t)completed - (ssize_t)address;
		if (offset > 0 && offset <= PAGE_SIZE)
			return (i << PAGE_SHIFT) + offset;
	}

	return 0;
}

138 139 140
struct fw_iso_context *fw_iso_context_create(struct fw_card *card,
		int type, int channel, int speed, size_t header_size,
		fw_iso_callback_t callback, void *callback_data)
141 142 143
{
	struct fw_iso_context *ctx;

144 145
	ctx = card->driver->allocate_iso_context(card,
						 type, channel, header_size);
146 147 148 149 150
	if (IS_ERR(ctx))
		return ctx;

	ctx->card = card;
	ctx->type = type;
151 152
	ctx->channel = channel;
	ctx->speed = speed;
153
	ctx->header_size = header_size;
154
	ctx->callback.sc = callback;
155 156 157 158
	ctx->callback_data = callback_data;

	return ctx;
}
J
Jay Fenlason 已提交
159
EXPORT_SYMBOL(fw_iso_context_create);
160 161 162

void fw_iso_context_destroy(struct fw_iso_context *ctx)
{
163
	ctx->card->driver->free_iso_context(ctx);
164
}
J
Jay Fenlason 已提交
165
EXPORT_SYMBOL(fw_iso_context_destroy);
166

167 168
int fw_iso_context_start(struct fw_iso_context *ctx,
			 int cycle, int sync, int tags)
169
{
170
	return ctx->card->driver->start_iso(ctx, cycle, sync, tags);
171
}
J
Jay Fenlason 已提交
172
EXPORT_SYMBOL(fw_iso_context_start);
173

174 175 176 177 178
int fw_iso_context_set_channels(struct fw_iso_context *ctx, u64 *channels)
{
	return ctx->card->driver->set_iso_channels(ctx, channels);
}

179 180 181 182
int fw_iso_context_queue(struct fw_iso_context *ctx,
			 struct fw_iso_packet *packet,
			 struct fw_iso_buffer *buffer,
			 unsigned long payload)
183
{
184
	return ctx->card->driver->queue_iso(ctx, packet, buffer, payload);
185
}
J
Jay Fenlason 已提交
186
EXPORT_SYMBOL(fw_iso_context_queue);
187

188 189 190 191 192 193
void fw_iso_context_queue_flush(struct fw_iso_context *ctx)
{
	ctx->card->driver->flush_queue_iso(ctx);
}
EXPORT_SYMBOL(fw_iso_context_queue_flush);

194
int fw_iso_context_stop(struct fw_iso_context *ctx)
195 196 197
{
	return ctx->card->driver->stop_iso(ctx);
}
J
Jay Fenlason 已提交
198
EXPORT_SYMBOL(fw_iso_context_stop);
199 200 201 202 203 204

/*
 * Isochronous bus resource management (channels, bandwidth), client side
 */

static int manage_bandwidth(struct fw_card *card, int irm_id, int generation,
205
			    int bandwidth, bool allocate)
206 207
{
	int try, new, old = allocate ? BANDWIDTH_AVAILABLE_INITIAL : 0;
208
	__be32 data[2];
209 210 211 212 213 214 215 216 217

	/*
	 * On a 1394a IRM with low contention, try < 1 is enough.
	 * On a 1394-1995 IRM, we need at least try < 2.
	 * Let's just do try < 5.
	 */
	for (try = 0; try < 5; try++) {
		new = allocate ? old - bandwidth : old + bandwidth;
		if (new < 0 || new > BANDWIDTH_AVAILABLE_INITIAL)
218
			return -EBUSY;
219 220 221 222 223 224

		data[0] = cpu_to_be32(old);
		data[1] = cpu_to_be32(new);
		switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
				irm_id, generation, SCODE_100,
				CSR_REGISTER_BASE + CSR_BANDWIDTH_AVAILABLE,
225
				data, 8)) {
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
		case RCODE_GENERATION:
			/* A generation change frees all bandwidth. */
			return allocate ? -EAGAIN : bandwidth;

		case RCODE_COMPLETE:
			if (be32_to_cpup(data) == old)
				return bandwidth;

			old = be32_to_cpup(data);
			/* Fall through. */
		}
	}

	return -EIO;
}

static int manage_channel(struct fw_card *card, int irm_id, int generation,
243
		u32 channels_mask, u64 offset, bool allocate)
244
{
245
	__be32 bit, all, old;
246
	__be32 data[2];
247
	int channel, ret = -EIO, retry = 5;
248

249 250
	old = all = allocate ? cpu_to_be32(~0) : 0;

251 252
	for (channel = 0; channel < 32; channel++) {
		if (!(channels_mask & 1 << channel))
253 254
			continue;

255 256
		ret = -EBUSY;

257 258
		bit = cpu_to_be32(1 << (31 - channel));
		if ((old & bit) != (all & bit))
259 260 261
			continue;

		data[0] = old;
262
		data[1] = old ^ bit;
263 264
		switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
					   irm_id, generation, SCODE_100,
265
					   offset, data, 8)) {
266 267
		case RCODE_GENERATION:
			/* A generation change frees all channels. */
268
			return allocate ? -EAGAIN : channel;
269 270 271

		case RCODE_COMPLETE:
			if (data[0] == old)
272
				return channel;
273 274 275 276

			old = data[0];

			/* Is the IRM 1394a-2000 compliant? */
277
			if ((data[0] & bit) == (data[1] & bit))
278 279 280 281
				continue;

			/* 1394-1995 IRM, fall through to retry. */
		default:
282 283
			if (retry) {
				retry--;
284
				channel--;
285 286
			} else {
				ret = -EIO;
287
			}
288 289 290
		}
	}

291
	return ret;
292 293 294
}

static void deallocate_channel(struct fw_card *card, int irm_id,
295
			       int generation, int channel)
296
{
297
	u32 mask;
298 299
	u64 offset;

300
	mask = channel < 32 ? 1 << channel : 1 << (channel - 32);
301 302 303
	offset = channel < 32 ? CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI :
				CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO;

304
	manage_channel(card, irm_id, generation, mask, offset, false);
305 306 307
}

/**
308
 * fw_iso_resource_manage() - Allocate or deallocate a channel and/or bandwidth
309 310 311 312
 *
 * In parameters: card, generation, channels_mask, bandwidth, allocate
 * Out parameters: channel, bandwidth
 * This function blocks (sleeps) during communication with the IRM.
313
 *
314
 * Allocates or deallocates at most one channel out of channels_mask.
315 316 317 318
 * channels_mask is a bitfield with MSB for channel 63 and LSB for channel 0.
 * (Note, the IRM's CHANNELS_AVAILABLE is a big-endian bitfield with MSB for
 * channel 0 and LSB for channel 63.)
 * Allocates or deallocates as many bandwidth allocation units as specified.
319 320 321 322 323 324 325
 *
 * Returns channel < 0 if no channel was allocated or deallocated.
 * Returns bandwidth = 0 if no bandwidth was allocated or deallocated.
 *
 * If generation is stale, deallocations succeed but allocations fail with
 * channel = -EAGAIN.
 *
326
 * If channel allocation fails, no bandwidth will be allocated either.
327
 * If bandwidth allocation fails, no channel will be allocated either.
328 329
 * But deallocations of channel and bandwidth are tried independently
 * of each other's success.
330 331 332
 */
void fw_iso_resource_manage(struct fw_card *card, int generation,
			    u64 channels_mask, int *channel, int *bandwidth,
333
			    bool allocate)
334
{
335 336
	u32 channels_hi = channels_mask;	/* channels 31...0 */
	u32 channels_lo = channels_mask >> 32;	/* channels 63...32 */
337 338 339 340 341 342 343 344
	int irm_id, ret, c = -EINVAL;

	spin_lock_irq(&card->lock);
	irm_id = card->irm_node->node_id;
	spin_unlock_irq(&card->lock);

	if (channels_hi)
		c = manage_channel(card, irm_id, generation, channels_hi,
345
				CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI,
346
				allocate);
347 348
	if (channels_lo && c < 0) {
		c = manage_channel(card, irm_id, generation, channels_lo,
349
				CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO,
350
				allocate);
351 352 353 354 355
		if (c >= 0)
			c += 32;
	}
	*channel = c;

356
	if (allocate && channels_mask != 0 && c < 0)
357 358 359 360 361
		*bandwidth = 0;

	if (*bandwidth == 0)
		return;

362
	ret = manage_bandwidth(card, irm_id, generation, *bandwidth, allocate);
363 364 365
	if (ret < 0)
		*bandwidth = 0;

366 367
	if (allocate && ret < 0) {
		if (c >= 0)
368
			deallocate_channel(card, irm_id, generation, c);
369 370 371
		*channel = ret;
	}
}
372
EXPORT_SYMBOL(fw_iso_resource_manage);