2007-05-07 20:33:32 -04:00
|
|
|
/*
|
2009-01-04 10:23:29 -05:00
|
|
|
* Isochronous I/O functionality:
|
|
|
|
* - Isochronous DMA context management
|
|
|
|
* - Isochronous bus resource management (channels, bandwidth), client side
|
2006-12-19 19:58:27 -05:00
|
|
|
*
|
|
|
|
* 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>
|
2009-01-04 10:23:29 -05:00
|
|
|
#include <linux/errno.h>
|
firewire: reorganize header files
The three header files of firewire-core, i.e.
"drivers/firewire/fw-device.h",
"drivers/firewire/fw-topology.h",
"drivers/firewire/fw-transaction.h",
are replaced by
"drivers/firewire/core.h",
"include/linux/firewire.h".
The latter includes everything which a firewire high-level driver (like
firewire-sbp2) needs besides linux/firewire-constants.h, while core.h
contains the rest which is needed by firewire-core itself and by low-
level drivers (card drivers) like firewire-ohci.
High-level drivers can now also reside outside of drivers/firewire
without having to add drivers/firewire to the header file search path in
makefiles. At least the firedtv driver will be such a driver.
I also considered to spread the contents of core.h over several files,
one for each .c file where the respective implementation resides. But
it turned out that most core .c files will end up including most of the
core .h files. Also, the combined core.h isn't unreasonably big, and it
will lose more of its contents to linux/firewire.h anyway soon when more
firewire drivers are added. (IP-over-1394, firedtv, and there are plans
for one or two more.)
Furthermore, fw-ohci.h is renamed to ohci.h. The name of core.h and
ohci.h is chosen with regard to name changes of the .c files in a
follow-up change.
Signed-off-by: Stefan Richter <stefanr@s5r6.in-berlin.de>
2009-06-05 10:26:18 -04:00
|
|
|
#include <linux/firewire.h>
|
2009-01-04 10:23:29 -05:00
|
|
|
#include <linux/firewire-constants.h>
|
|
|
|
#include <linux/kernel.h>
|
2006-12-19 19:58:27 -05:00
|
|
|
#include <linux/mm.h>
|
2009-01-04 10:23:29 -05:00
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/vmalloc.h>
|
2006-12-19 19:58:27 -05:00
|
|
|
|
2009-06-04 15:09:38 -04:00
|
|
|
#include <asm/byteorder.h>
|
|
|
|
|
firewire: reorganize header files
The three header files of firewire-core, i.e.
"drivers/firewire/fw-device.h",
"drivers/firewire/fw-topology.h",
"drivers/firewire/fw-transaction.h",
are replaced by
"drivers/firewire/core.h",
"include/linux/firewire.h".
The latter includes everything which a firewire high-level driver (like
firewire-sbp2) needs besides linux/firewire-constants.h, while core.h
contains the rest which is needed by firewire-core itself and by low-
level drivers (card drivers) like firewire-ohci.
High-level drivers can now also reside outside of drivers/firewire
without having to add drivers/firewire to the header file search path in
makefiles. At least the firedtv driver will be such a driver.
I also considered to spread the contents of core.h over several files,
one for each .c file where the respective implementation resides. But
it turned out that most core .c files will end up including most of the
core .h files. Also, the combined core.h isn't unreasonably big, and it
will lose more of its contents to linux/firewire.h anyway soon when more
firewire drivers are added. (IP-over-1394, firedtv, and there are plans
for one or two more.)
Furthermore, fw-ohci.h is renamed to ohci.h. The name of core.h and
ohci.h is chosen with regard to name changes of the .c files in a
follow-up change.
Signed-off-by: Stefan Richter <stefanr@s5r6.in-berlin.de>
2009-06-05 10:26:18 -04:00
|
|
|
#include "core.h"
|
2009-01-04 10:23:29 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Isochronous DMA context management
|
|
|
|
*/
|
2006-12-19 19:58:27 -05:00
|
|
|
|
2008-12-14 15:47:04 -05:00
|
|
|
int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
|
|
|
|
int page_count, enum dma_data_direction direction)
|
2006-12-19 19:58:27 -05:00
|
|
|
{
|
2008-12-14 15:45:45 -05:00
|
|
|
int i, j;
|
2007-02-16 17:34:38 -05:00
|
|
|
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++) {
|
2007-02-16 17:34:48 -05:00
|
|
|
buffer->pages[i] = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
|
2007-02-16 17:34:38 -05:00
|
|
|
if (buffer->pages[i] == NULL)
|
|
|
|
goto out_pages;
|
2007-03-04 08:45:18 -05:00
|
|
|
|
2007-02-16 17:34:38 -05:00
|
|
|
address = dma_map_page(card->device, buffer->pages[i],
|
|
|
|
0, PAGE_SIZE, direction);
|
2008-07-25 22:44:49 -04:00
|
|
|
if (dma_mapping_error(card->device, address)) {
|
2007-02-16 17:34:38 -05:00
|
|
|
__free_page(buffer->pages[i]);
|
|
|
|
goto out_pages;
|
|
|
|
}
|
|
|
|
set_page_private(buffer->pages[i], address);
|
2006-12-19 19:58:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-02-06 14:49:40 -05:00
|
|
|
|
2007-02-16 17:34:38 -05:00
|
|
|
out_pages:
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
address = page_private(buffer->pages[j]);
|
|
|
|
dma_unmap_page(card->device, address,
|
2009-06-14 18:38:50 -04:00
|
|
|
PAGE_SIZE, direction);
|
2007-02-16 17:34:38 -05:00
|
|
|
__free_page(buffer->pages[j]);
|
|
|
|
}
|
|
|
|
kfree(buffer->pages);
|
|
|
|
out:
|
|
|
|
buffer->pages = NULL;
|
2009-02-03 11:55:19 -05:00
|
|
|
|
2008-12-14 15:45:45 -05:00
|
|
|
return -ENOMEM;
|
2007-02-16 17:34:38 -05:00
|
|
|
}
|
2009-05-18 13:08:06 -04:00
|
|
|
EXPORT_SYMBOL(fw_iso_buffer_init);
|
2007-02-16 17:34:38 -05:00
|
|
|
|
|
|
|
int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
unsigned long uaddr;
|
2009-02-03 11:55:19 -05:00
|
|
|
int i, err;
|
2007-02-16 17:34:38 -05:00
|
|
|
|
|
|
|
uaddr = vma->vm_start;
|
|
|
|
for (i = 0; i < buffer->page_count; i++) {
|
2009-02-03 11:55:19 -05:00
|
|
|
err = vm_insert_page(vma, uaddr, buffer->pages[i]);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2007-02-16 17:34:38 -05:00
|
|
|
uaddr += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2006-12-19 19:58:27 -05:00
|
|
|
}
|
|
|
|
|
2007-02-16 17:34:38 -05:00
|
|
|
void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer,
|
|
|
|
struct fw_card *card)
|
2006-12-19 19:58:27 -05:00
|
|
|
{
|
|
|
|
int i;
|
2007-02-16 17:34:38 -05:00
|
|
|
dma_addr_t address;
|
2006-12-19 19:58:27 -05:00
|
|
|
|
2007-02-16 17:34:38 -05:00
|
|
|
for (i = 0; i < buffer->page_count; i++) {
|
|
|
|
address = page_private(buffer->pages[i]);
|
|
|
|
dma_unmap_page(card->device, address,
|
2009-06-14 18:38:50 -04:00
|
|
|
PAGE_SIZE, buffer->direction);
|
2007-02-16 17:34:38 -05:00
|
|
|
__free_page(buffer->pages[i]);
|
|
|
|
}
|
2006-12-19 19:58:27 -05:00
|
|
|
|
2007-02-16 17:34:38 -05:00
|
|
|
kfree(buffer->pages);
|
|
|
|
buffer->pages = NULL;
|
2006-12-19 19:58:27 -05:00
|
|
|
}
|
2009-05-18 13:08:06 -04:00
|
|
|
EXPORT_SYMBOL(fw_iso_buffer_destroy);
|
2006-12-19 19:58:27 -05:00
|
|
|
|
2008-12-14 15:47:04 -05:00
|
|
|
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)
|
2006-12-19 19:58:27 -05:00
|
|
|
{
|
|
|
|
struct fw_iso_context *ctx;
|
|
|
|
|
2008-12-21 10:39:46 -05:00
|
|
|
ctx = card->driver->allocate_iso_context(card,
|
|
|
|
type, channel, header_size);
|
2006-12-19 19:58:27 -05:00
|
|
|
if (IS_ERR(ctx))
|
|
|
|
return ctx;
|
|
|
|
|
|
|
|
ctx->card = card;
|
|
|
|
ctx->type = type;
|
2007-02-16 17:34:50 -05:00
|
|
|
ctx->channel = channel;
|
|
|
|
ctx->speed = speed;
|
2007-02-16 17:34:40 -05:00
|
|
|
ctx->header_size = header_size;
|
2006-12-19 19:58:27 -05:00
|
|
|
ctx->callback = callback;
|
|
|
|
ctx->callback_data = callback_data;
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
2009-05-18 13:08:06 -04:00
|
|
|
EXPORT_SYMBOL(fw_iso_context_create);
|
2006-12-19 19:58:27 -05:00
|
|
|
|
|
|
|
void fw_iso_context_destroy(struct fw_iso_context *ctx)
|
|
|
|
{
|
|
|
|
struct fw_card *card = ctx->card;
|
|
|
|
|
|
|
|
card->driver->free_iso_context(ctx);
|
|
|
|
}
|
2009-05-18 13:08:06 -04:00
|
|
|
EXPORT_SYMBOL(fw_iso_context_destroy);
|
2006-12-19 19:58:27 -05:00
|
|
|
|
2008-12-14 15:47:04 -05:00
|
|
|
int fw_iso_context_start(struct fw_iso_context *ctx,
|
|
|
|
int cycle, int sync, int tags)
|
2006-12-19 19:58:27 -05:00
|
|
|
{
|
2007-03-14 17:34:54 -04:00
|
|
|
return ctx->card->driver->start_iso(ctx, cycle, sync, tags);
|
2006-12-19 19:58:27 -05:00
|
|
|
}
|
2009-05-18 13:08:06 -04:00
|
|
|
EXPORT_SYMBOL(fw_iso_context_start);
|
2006-12-19 19:58:27 -05:00
|
|
|
|
2008-12-14 15:47:04 -05:00
|
|
|
int fw_iso_context_queue(struct fw_iso_context *ctx,
|
|
|
|
struct fw_iso_packet *packet,
|
|
|
|
struct fw_iso_buffer *buffer,
|
|
|
|
unsigned long payload)
|
2006-12-19 19:58:27 -05:00
|
|
|
{
|
|
|
|
struct fw_card *card = ctx->card;
|
|
|
|
|
2007-02-16 17:34:38 -05:00
|
|
|
return card->driver->queue_iso(ctx, packet, buffer, payload);
|
2006-12-19 19:58:27 -05:00
|
|
|
}
|
2009-05-18 13:08:06 -04:00
|
|
|
EXPORT_SYMBOL(fw_iso_context_queue);
|
2007-02-16 17:34:42 -05:00
|
|
|
|
2008-12-14 15:47:04 -05:00
|
|
|
int fw_iso_context_stop(struct fw_iso_context *ctx)
|
2007-02-16 17:34:42 -05:00
|
|
|
{
|
|
|
|
return ctx->card->driver->stop_iso(ctx);
|
|
|
|
}
|
2009-05-18 13:08:06 -04:00
|
|
|
EXPORT_SYMBOL(fw_iso_context_stop);
|
2009-01-04 10:23:29 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Isochronous bus resource management (channels, bandwidth), client side
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int manage_bandwidth(struct fw_card *card, int irm_id, int generation,
|
2009-06-20 07:23:59 -04:00
|
|
|
int bandwidth, bool allocate, __be32 data[2])
|
2009-01-04 10:23:29 -05:00
|
|
|
{
|
|
|
|
int try, new, old = allocate ? BANDWIDTH_AVAILABLE_INITIAL : 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
break;
|
|
|
|
|
|
|
|
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,
|
2009-09-05 07:23:49 -04:00
|
|
|
data, 8)) {
|
2009-01-04 10:23:29 -05:00
|
|
|
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,
|
2009-06-20 07:23:59 -04:00
|
|
|
u32 channels_mask, u64 offset, bool allocate, __be32 data[2])
|
2009-01-04 10:23:29 -05:00
|
|
|
{
|
2009-06-20 07:23:59 -04:00
|
|
|
__be32 c, all, old;
|
2009-01-04 10:23:29 -05:00
|
|
|
int i, retry = 5;
|
|
|
|
|
2009-01-08 17:07:40 -05:00
|
|
|
old = all = allocate ? cpu_to_be32(~0) : 0;
|
|
|
|
|
2009-01-04 10:23:29 -05:00
|
|
|
for (i = 0; i < 32; i++) {
|
2009-01-08 17:07:40 -05:00
|
|
|
if (!(channels_mask & 1 << i))
|
2009-01-04 10:23:29 -05:00
|
|
|
continue;
|
|
|
|
|
2009-01-08 17:07:40 -05:00
|
|
|
c = cpu_to_be32(1 << (31 - i));
|
|
|
|
if ((old & c) != (all & c))
|
2009-01-04 10:23:29 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
data[0] = old;
|
|
|
|
data[1] = old ^ c;
|
|
|
|
switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
|
|
|
|
irm_id, generation, SCODE_100,
|
2009-09-05 07:23:49 -04:00
|
|
|
offset, data, 8)) {
|
2009-01-04 10:23:29 -05:00
|
|
|
case RCODE_GENERATION:
|
|
|
|
/* A generation change frees all channels. */
|
|
|
|
return allocate ? -EAGAIN : i;
|
|
|
|
|
|
|
|
case RCODE_COMPLETE:
|
|
|
|
if (data[0] == old)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
old = data[0];
|
|
|
|
|
|
|
|
/* Is the IRM 1394a-2000 compliant? */
|
2009-01-08 17:07:40 -05:00
|
|
|
if ((data[0] & c) == (data[1] & c))
|
2009-01-04 10:23:29 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* 1394-1995 IRM, fall through to retry. */
|
|
|
|
default:
|
|
|
|
if (retry--)
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void deallocate_channel(struct fw_card *card, int irm_id,
|
2009-06-20 07:23:59 -04:00
|
|
|
int generation, int channel, __be32 buffer[2])
|
2009-01-04 10:23:29 -05:00
|
|
|
{
|
2009-01-08 17:07:40 -05:00
|
|
|
u32 mask;
|
2009-01-04 10:23:29 -05:00
|
|
|
u64 offset;
|
|
|
|
|
2009-01-08 17:07:40 -05:00
|
|
|
mask = channel < 32 ? 1 << channel : 1 << (channel - 32);
|
2009-01-04 10:23:29 -05:00
|
|
|
offset = channel < 32 ? CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI :
|
|
|
|
CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO;
|
|
|
|
|
2009-06-20 07:23:59 -04:00
|
|
|
manage_channel(card, irm_id, generation, mask, offset, false, buffer);
|
2009-01-04 10:23:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fw_iso_resource_manage - Allocate or deallocate a channel and/or bandwidth
|
|
|
|
*
|
|
|
|
* In parameters: card, generation, channels_mask, bandwidth, allocate
|
|
|
|
* Out parameters: channel, bandwidth
|
|
|
|
* This function blocks (sleeps) during communication with the IRM.
|
2009-01-08 17:07:40 -05:00
|
|
|
*
|
2009-01-04 10:23:29 -05:00
|
|
|
* Allocates or deallocates at most one channel out of channels_mask.
|
2009-01-08 17:07:40 -05:00
|
|
|
* 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.
|
2009-01-04 10:23:29 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2009-01-08 17:07:40 -05:00
|
|
|
* If channel allocation fails, no bandwidth will be allocated either.
|
2009-01-04 10:23:29 -05:00
|
|
|
* If bandwidth allocation fails, no channel will be allocated either.
|
2009-01-08 17:07:40 -05:00
|
|
|
* But deallocations of channel and bandwidth are tried independently
|
|
|
|
* of each other's success.
|
2009-01-04 10:23:29 -05:00
|
|
|
*/
|
|
|
|
void fw_iso_resource_manage(struct fw_card *card, int generation,
|
|
|
|
u64 channels_mask, int *channel, int *bandwidth,
|
2009-06-20 07:23:59 -04:00
|
|
|
bool allocate, __be32 buffer[2])
|
2009-01-04 10:23:29 -05:00
|
|
|
{
|
2009-01-08 17:07:40 -05:00
|
|
|
u32 channels_hi = channels_mask; /* channels 31...0 */
|
|
|
|
u32 channels_lo = channels_mask >> 32; /* channels 63...32 */
|
2009-01-04 10:23:29 -05:00
|
|
|
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,
|
2009-06-20 07:23:59 -04:00
|
|
|
CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI,
|
|
|
|
allocate, buffer);
|
2009-01-04 10:23:29 -05:00
|
|
|
if (channels_lo && c < 0) {
|
|
|
|
c = manage_channel(card, irm_id, generation, channels_lo,
|
2009-06-20 07:23:59 -04:00
|
|
|
CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO,
|
|
|
|
allocate, buffer);
|
2009-01-04 10:23:29 -05:00
|
|
|
if (c >= 0)
|
|
|
|
c += 32;
|
|
|
|
}
|
|
|
|
*channel = c;
|
|
|
|
|
2009-01-08 17:07:40 -05:00
|
|
|
if (allocate && channels_mask != 0 && c < 0)
|
2009-01-04 10:23:29 -05:00
|
|
|
*bandwidth = 0;
|
|
|
|
|
|
|
|
if (*bandwidth == 0)
|
|
|
|
return;
|
|
|
|
|
2009-06-20 07:23:59 -04:00
|
|
|
ret = manage_bandwidth(card, irm_id, generation, *bandwidth,
|
|
|
|
allocate, buffer);
|
2009-01-04 10:23:29 -05:00
|
|
|
if (ret < 0)
|
|
|
|
*bandwidth = 0;
|
|
|
|
|
2009-01-08 17:07:40 -05:00
|
|
|
if (allocate && ret < 0 && c >= 0) {
|
2009-06-20 07:23:59 -04:00
|
|
|
deallocate_channel(card, irm_id, generation, c, buffer);
|
2009-01-04 10:23:29 -05:00
|
|
|
*channel = ret;
|
|
|
|
}
|
|
|
|
}
|