blob: f511d16efaeecac791773d8533d35d347eef7f38 [file] [log] [blame]
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001/*
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01002 * Isochronous I/O functionality:
3 * - Isochronous DMA context management
4 * - Isochronous bus resource management (channels, bandwidth), client side
Kristian Høgsberg3038e352006-12-19 19:58:27 -05005 *
Kristian Høgsberg3038e352006-12-19 19:58:27 -05006 * Copyright (C) 2006 Kristian Hoegsberg <krh@bitplanet.net>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
Kristian Høgsberg3038e352006-12-19 19:58:27 -050023#include <linux/dma-mapping.h>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010024#include <linux/errno.h>
25#include <linux/firewire-constants.h>
26#include <linux/kernel.h>
Kristian Høgsberg3038e352006-12-19 19:58:27 -050027#include <linux/mm.h>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010028#include <linux/spinlock.h>
29#include <linux/vmalloc.h>
Kristian Høgsberg3038e352006-12-19 19:58:27 -050030
Kristian Høgsberg3038e352006-12-19 19:58:27 -050031#include "fw-topology.h"
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010032#include "fw-transaction.h"
33
34/*
35 * Isochronous DMA context management
36 */
Kristian Høgsberg3038e352006-12-19 19:58:27 -050037
Stefan Richter53dca512008-12-14 21:47:04 +010038int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
39 int page_count, enum dma_data_direction direction)
Kristian Høgsberg3038e352006-12-19 19:58:27 -050040{
Stefan Richter2dbd7d72008-12-14 21:45:45 +010041 int i, j;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050042 dma_addr_t address;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050043
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050044 buffer->page_count = page_count;
45 buffer->direction = direction;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050046
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050047 buffer->pages = kmalloc(page_count * sizeof(buffer->pages[0]),
48 GFP_KERNEL);
49 if (buffer->pages == NULL)
50 goto out;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050051
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050052 for (i = 0; i < buffer->page_count; i++) {
Kristian Høgsberg68be3fa2007-02-16 17:34:48 -050053 buffer->pages[i] = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050054 if (buffer->pages[i] == NULL)
55 goto out_pages;
Stefan Richter373b2ed2007-03-04 14:45:18 +010056
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050057 address = dma_map_page(card->device, buffer->pages[i],
58 0, PAGE_SIZE, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -070059 if (dma_mapping_error(card->device, address)) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050060 __free_page(buffer->pages[i]);
61 goto out_pages;
62 }
63 set_page_private(buffer->pages[i], address);
Kristian Høgsberg3038e352006-12-19 19:58:27 -050064 }
65
66 return 0;
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -050067
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050068 out_pages:
69 for (j = 0; j < i; j++) {
70 address = page_private(buffer->pages[j]);
71 dma_unmap_page(card->device, address,
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -050072 PAGE_SIZE, DMA_TO_DEVICE);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050073 __free_page(buffer->pages[j]);
74 }
75 kfree(buffer->pages);
76 out:
77 buffer->pages = NULL;
Stefan Richter2dbd7d72008-12-14 21:45:45 +010078 return -ENOMEM;
Kristian Høgsberg3038e352006-12-19 19:58:27 -050079}
80
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050081int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma)
82{
83 unsigned long uaddr;
Stefan Richter2dbd7d72008-12-14 21:45:45 +010084 int i, ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050085
86 uaddr = vma->vm_start;
87 for (i = 0; i < buffer->page_count; i++) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +010088 ret = vm_insert_page(vma, uaddr, buffer->pages[i]);
89 if (ret)
90 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050091 uaddr += PAGE_SIZE;
92 }
93
94 return 0;
95}
96
97void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer,
98 struct fw_card *card)
Kristian Høgsberg3038e352006-12-19 19:58:27 -050099{
100 int i;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500101 dma_addr_t address;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500102
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500103 for (i = 0; i < buffer->page_count; i++) {
104 address = page_private(buffer->pages[i]);
105 dma_unmap_page(card->device, address,
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500106 PAGE_SIZE, DMA_TO_DEVICE);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500107 __free_page(buffer->pages[i]);
108 }
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500109
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500110 kfree(buffer->pages);
111 buffer->pages = NULL;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500112}
113
Stefan Richter53dca512008-12-14 21:47:04 +0100114struct fw_iso_context *fw_iso_context_create(struct fw_card *card,
115 int type, int channel, int speed, size_t header_size,
116 fw_iso_callback_t callback, void *callback_data)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500117{
118 struct fw_iso_context *ctx;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500119
Stefan Richter4817ed22008-12-21 16:39:46 +0100120 ctx = card->driver->allocate_iso_context(card,
121 type, channel, header_size);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500122 if (IS_ERR(ctx))
123 return ctx;
124
125 ctx->card = card;
126 ctx->type = type;
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500127 ctx->channel = channel;
128 ctx->speed = speed;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500129 ctx->header_size = header_size;
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500130 ctx->callback = callback;
131 ctx->callback_data = callback_data;
132
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500133 return ctx;
134}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500135
136void fw_iso_context_destroy(struct fw_iso_context *ctx)
137{
138 struct fw_card *card = ctx->card;
139
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500140 card->driver->free_iso_context(ctx);
141}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500142
Stefan Richter53dca512008-12-14 21:47:04 +0100143int fw_iso_context_start(struct fw_iso_context *ctx,
144 int cycle, int sync, int tags)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500145{
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400146 return ctx->card->driver->start_iso(ctx, cycle, sync, tags);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500147}
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500148
Stefan Richter53dca512008-12-14 21:47:04 +0100149int fw_iso_context_queue(struct fw_iso_context *ctx,
150 struct fw_iso_packet *packet,
151 struct fw_iso_buffer *buffer,
152 unsigned long payload)
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500153{
154 struct fw_card *card = ctx->card;
155
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500156 return card->driver->queue_iso(ctx, packet, buffer, payload);
Kristian Høgsberg3038e352006-12-19 19:58:27 -0500157}
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500158
Stefan Richter53dca512008-12-14 21:47:04 +0100159int fw_iso_context_stop(struct fw_iso_context *ctx)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500160{
161 return ctx->card->driver->stop_iso(ctx);
162}
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100163
164/*
165 * Isochronous bus resource management (channels, bandwidth), client side
166 */
167
168static int manage_bandwidth(struct fw_card *card, int irm_id, int generation,
169 int bandwidth, bool allocate)
170{
171 __be32 data[2];
172 int try, new, old = allocate ? BANDWIDTH_AVAILABLE_INITIAL : 0;
173
174 /*
175 * On a 1394a IRM with low contention, try < 1 is enough.
176 * On a 1394-1995 IRM, we need at least try < 2.
177 * Let's just do try < 5.
178 */
179 for (try = 0; try < 5; try++) {
180 new = allocate ? old - bandwidth : old + bandwidth;
181 if (new < 0 || new > BANDWIDTH_AVAILABLE_INITIAL)
182 break;
183
184 data[0] = cpu_to_be32(old);
185 data[1] = cpu_to_be32(new);
186 switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
187 irm_id, generation, SCODE_100,
188 CSR_REGISTER_BASE + CSR_BANDWIDTH_AVAILABLE,
189 data, sizeof(data))) {
190 case RCODE_GENERATION:
191 /* A generation change frees all bandwidth. */
192 return allocate ? -EAGAIN : bandwidth;
193
194 case RCODE_COMPLETE:
195 if (be32_to_cpup(data) == old)
196 return bandwidth;
197
198 old = be32_to_cpup(data);
199 /* Fall through. */
200 }
201 }
202
203 return -EIO;
204}
205
206static int manage_channel(struct fw_card *card, int irm_id, int generation,
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100207 u32 channels_mask, u64 offset, bool allocate)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100208{
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100209 __be32 data[2], c, all, old;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100210 int i, retry = 5;
211
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100212 old = all = allocate ? cpu_to_be32(~0) : 0;
213
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100214 for (i = 0; i < 32; i++) {
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100215 if (!(channels_mask & 1 << i))
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100216 continue;
217
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100218 c = cpu_to_be32(1 << (31 - i));
219 if ((old & c) != (all & c))
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100220 continue;
221
222 data[0] = old;
223 data[1] = old ^ c;
224 switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
225 irm_id, generation, SCODE_100,
226 offset, data, sizeof(data))) {
227 case RCODE_GENERATION:
228 /* A generation change frees all channels. */
229 return allocate ? -EAGAIN : i;
230
231 case RCODE_COMPLETE:
232 if (data[0] == old)
233 return i;
234
235 old = data[0];
236
237 /* Is the IRM 1394a-2000 compliant? */
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100238 if ((data[0] & c) == (data[1] & c))
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100239 continue;
240
241 /* 1394-1995 IRM, fall through to retry. */
242 default:
243 if (retry--)
244 i--;
245 }
246 }
247
248 return -EIO;
249}
250
251static void deallocate_channel(struct fw_card *card, int irm_id,
252 int generation, int channel)
253{
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100254 u32 mask;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100255 u64 offset;
256
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100257 mask = channel < 32 ? 1 << channel : 1 << (channel - 32);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100258 offset = channel < 32 ? CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI :
259 CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO;
260
261 manage_channel(card, irm_id, generation, mask, offset, false);
262}
263
264/**
265 * fw_iso_resource_manage - Allocate or deallocate a channel and/or bandwidth
266 *
267 * In parameters: card, generation, channels_mask, bandwidth, allocate
268 * Out parameters: channel, bandwidth
269 * This function blocks (sleeps) during communication with the IRM.
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100270 *
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100271 * Allocates or deallocates at most one channel out of channels_mask.
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100272 * channels_mask is a bitfield with MSB for channel 63 and LSB for channel 0.
273 * (Note, the IRM's CHANNELS_AVAILABLE is a big-endian bitfield with MSB for
274 * channel 0 and LSB for channel 63.)
275 * Allocates or deallocates as many bandwidth allocation units as specified.
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100276 *
277 * Returns channel < 0 if no channel was allocated or deallocated.
278 * Returns bandwidth = 0 if no bandwidth was allocated or deallocated.
279 *
280 * If generation is stale, deallocations succeed but allocations fail with
281 * channel = -EAGAIN.
282 *
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100283 * If channel allocation fails, no bandwidth will be allocated either.
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100284 * If bandwidth allocation fails, no channel will be allocated either.
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100285 * But deallocations of channel and bandwidth are tried independently
286 * of each other's success.
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100287 */
288void fw_iso_resource_manage(struct fw_card *card, int generation,
289 u64 channels_mask, int *channel, int *bandwidth,
290 bool allocate)
291{
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100292 u32 channels_hi = channels_mask; /* channels 31...0 */
293 u32 channels_lo = channels_mask >> 32; /* channels 63...32 */
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100294 int irm_id, ret, c = -EINVAL;
295
296 spin_lock_irq(&card->lock);
297 irm_id = card->irm_node->node_id;
298 spin_unlock_irq(&card->lock);
299
300 if (channels_hi)
301 c = manage_channel(card, irm_id, generation, channels_hi,
302 CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI, allocate);
303 if (channels_lo && c < 0) {
304 c = manage_channel(card, irm_id, generation, channels_lo,
305 CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO, allocate);
306 if (c >= 0)
307 c += 32;
308 }
309 *channel = c;
310
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100311 if (allocate && channels_mask != 0 && c < 0)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100312 *bandwidth = 0;
313
314 if (*bandwidth == 0)
315 return;
316
317 ret = manage_bandwidth(card, irm_id, generation, *bandwidth, allocate);
318 if (ret < 0)
319 *bandwidth = 0;
320
Stefan Richter5d9cb7d272009-01-08 23:07:40 +0100321 if (allocate && ret < 0 && c >= 0) {
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100322 deallocate_channel(card, irm_id, generation, c);
323 *channel = ret;
324 }
325}