Remove broken ptrace() special-case code from file mapping
[deliverable/linux.git] / drivers / firewire / fw-ohci.c
CommitLineData
c781c06d
KH
1/*
2 * Driver for OHCI 1394 controllers
ed568912 3 *
ed568912
KH
4 * Copyright (C) 2003-2006 Kristian Hoegsberg <krh@bitplanet.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
e524f616 21#include <linux/compiler.h>
ed568912 22#include <linux/delay.h>
cf3e72fd 23#include <linux/dma-mapping.h>
c26f0234 24#include <linux/gfp.h>
a7fb60db
SR
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/kernel.h>
faa2fb4e 28#include <linux/mm.h>
a7fb60db
SR
29#include <linux/module.h>
30#include <linux/pci.h>
c26f0234 31#include <linux/spinlock.h>
cf3e72fd 32
c26f0234 33#include <asm/page.h>
ee71c2f9 34#include <asm/system.h>
ed568912 35
ed568912 36#include "fw-ohci.h"
a7fb60db 37#include "fw-transaction.h"
ed568912 38
a77754a7
KH
39#define DESCRIPTOR_OUTPUT_MORE 0
40#define DESCRIPTOR_OUTPUT_LAST (1 << 12)
41#define DESCRIPTOR_INPUT_MORE (2 << 12)
42#define DESCRIPTOR_INPUT_LAST (3 << 12)
43#define DESCRIPTOR_STATUS (1 << 11)
44#define DESCRIPTOR_KEY_IMMEDIATE (2 << 8)
45#define DESCRIPTOR_PING (1 << 7)
46#define DESCRIPTOR_YY (1 << 6)
47#define DESCRIPTOR_NO_IRQ (0 << 4)
48#define DESCRIPTOR_IRQ_ERROR (1 << 4)
49#define DESCRIPTOR_IRQ_ALWAYS (3 << 4)
50#define DESCRIPTOR_BRANCH_ALWAYS (3 << 2)
51#define DESCRIPTOR_WAIT (3 << 0)
ed568912
KH
52
53struct descriptor {
54 __le16 req_count;
55 __le16 control;
56 __le32 data_address;
57 __le32 branch_address;
58 __le16 res_count;
59 __le16 transfer_status;
60} __attribute__((aligned(16)));
61
295e3feb
KH
62struct db_descriptor {
63 __le16 first_size;
64 __le16 control;
65 __le16 second_req_count;
66 __le16 first_req_count;
67 __le32 branch_address;
68 __le16 second_res_count;
69 __le16 first_res_count;
70 __le32 reserved0;
71 __le32 first_buffer;
72 __le32 second_buffer;
73 __le32 reserved1;
74} __attribute__((aligned(16)));
75
a77754a7
KH
76#define CONTROL_SET(regs) (regs)
77#define CONTROL_CLEAR(regs) ((regs) + 4)
78#define COMMAND_PTR(regs) ((regs) + 12)
79#define CONTEXT_MATCH(regs) ((regs) + 16)
72e318e0 80
32b46093 81struct ar_buffer {
ed568912 82 struct descriptor descriptor;
32b46093
KH
83 struct ar_buffer *next;
84 __le32 data[0];
85};
ed568912 86
32b46093
KH
87struct ar_context {
88 struct fw_ohci *ohci;
89 struct ar_buffer *current_buffer;
90 struct ar_buffer *last_buffer;
91 void *pointer;
72e318e0 92 u32 regs;
ed568912
KH
93 struct tasklet_struct tasklet;
94};
95
30200739
KH
96struct context;
97
98typedef int (*descriptor_callback_t)(struct context *ctx,
99 struct descriptor *d,
100 struct descriptor *last);
101struct context {
373b2edd 102 struct fw_ohci *ohci;
30200739 103 u32 regs;
373b2edd 104
30200739
KH
105 struct descriptor *buffer;
106 dma_addr_t buffer_bus;
107 size_t buffer_size;
108 struct descriptor *head_descriptor;
109 struct descriptor *tail_descriptor;
110 struct descriptor *tail_descriptor_last;
111 struct descriptor *prev_descriptor;
112
113 descriptor_callback_t callback;
114
373b2edd 115 struct tasklet_struct tasklet;
30200739 116};
30200739 117
a77754a7
KH
118#define IT_HEADER_SY(v) ((v) << 0)
119#define IT_HEADER_TCODE(v) ((v) << 4)
120#define IT_HEADER_CHANNEL(v) ((v) << 8)
121#define IT_HEADER_TAG(v) ((v) << 14)
122#define IT_HEADER_SPEED(v) ((v) << 16)
123#define IT_HEADER_DATA_LENGTH(v) ((v) << 16)
ed568912
KH
124
125struct iso_context {
126 struct fw_iso_context base;
30200739 127 struct context context;
9b32d5f3
KH
128 void *header;
129 size_t header_length;
ed568912
KH
130};
131
132#define CONFIG_ROM_SIZE 1024
133
134struct fw_ohci {
135 struct fw_card card;
136
e364cf4e 137 u32 version;
ed568912
KH
138 __iomem char *registers;
139 dma_addr_t self_id_bus;
140 __le32 *self_id_cpu;
141 struct tasklet_struct bus_reset_tasklet;
e636fe25 142 int node_id;
ed568912
KH
143 int generation;
144 int request_generation;
d60d7f1d 145 u32 bus_seconds;
ed568912 146
c781c06d
KH
147 /*
148 * Spinlock for accessing fw_ohci data. Never call out of
149 * this driver with this lock held.
150 */
ed568912
KH
151 spinlock_t lock;
152 u32 self_id_buffer[512];
153
154 /* Config rom buffers */
155 __be32 *config_rom;
156 dma_addr_t config_rom_bus;
157 __be32 *next_config_rom;
158 dma_addr_t next_config_rom_bus;
159 u32 next_header;
160
161 struct ar_context ar_request_ctx;
162 struct ar_context ar_response_ctx;
f319b6a0
KH
163 struct context at_request_ctx;
164 struct context at_response_ctx;
ed568912
KH
165
166 u32 it_context_mask;
167 struct iso_context *it_context_list;
168 u32 ir_context_mask;
169 struct iso_context *ir_context_list;
170};
171
95688e97 172static inline struct fw_ohci *fw_ohci(struct fw_card *card)
ed568912
KH
173{
174 return container_of(card, struct fw_ohci, card);
175}
176
295e3feb
KH
177#define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
178#define IR_CONTEXT_BUFFER_FILL 0x80000000
179#define IR_CONTEXT_ISOCH_HEADER 0x40000000
180#define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
181#define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
182#define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
ed568912
KH
183
184#define CONTEXT_RUN 0x8000
185#define CONTEXT_WAKE 0x1000
186#define CONTEXT_DEAD 0x0800
187#define CONTEXT_ACTIVE 0x0400
188
189#define OHCI1394_MAX_AT_REQ_RETRIES 0x2
190#define OHCI1394_MAX_AT_RESP_RETRIES 0x2
191#define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
192
193#define FW_OHCI_MAJOR 240
194#define OHCI1394_REGISTER_SIZE 0x800
195#define OHCI_LOOP_COUNT 500
196#define OHCI1394_PCI_HCI_Control 0x40
197#define SELF_ID_BUF_SIZE 0x800
32b46093 198#define OHCI_TCODE_PHY_PACKET 0x0e
e364cf4e 199#define OHCI_VERSION_1_1 0x010010
f319b6a0
KH
200#define ISO_BUFFER_SIZE (64 * 1024)
201#define AT_BUFFER_SIZE 4096
0edeefd9 202
ed568912
KH
203static char ohci_driver_name[] = KBUILD_MODNAME;
204
95688e97 205static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
ed568912
KH
206{
207 writel(data, ohci->registers + offset);
208}
209
95688e97 210static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
ed568912
KH
211{
212 return readl(ohci->registers + offset);
213}
214
95688e97 215static inline void flush_writes(const struct fw_ohci *ohci)
ed568912
KH
216{
217 /* Do a dummy read to flush writes. */
218 reg_read(ohci, OHCI1394_Version);
219}
220
221static int
222ohci_update_phy_reg(struct fw_card *card, int addr,
223 int clear_bits, int set_bits)
224{
225 struct fw_ohci *ohci = fw_ohci(card);
226 u32 val, old;
227
228 reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
362e901c 229 flush_writes(ohci);
ed568912
KH
230 msleep(2);
231 val = reg_read(ohci, OHCI1394_PhyControl);
232 if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
233 fw_error("failed to set phy reg bits.\n");
234 return -EBUSY;
235 }
236
237 old = OHCI1394_PhyControl_ReadData(val);
238 old = (old & ~clear_bits) | set_bits;
239 reg_write(ohci, OHCI1394_PhyControl,
240 OHCI1394_PhyControl_Write(addr, old));
241
242 return 0;
243}
244
32b46093 245static int ar_context_add_page(struct ar_context *ctx)
ed568912 246{
32b46093
KH
247 struct device *dev = ctx->ohci->card.device;
248 struct ar_buffer *ab;
249 dma_addr_t ab_bus;
250 size_t offset;
251
252 ab = (struct ar_buffer *) __get_free_page(GFP_ATOMIC);
253 if (ab == NULL)
254 return -ENOMEM;
255
256 ab_bus = dma_map_single(dev, ab, PAGE_SIZE, DMA_BIDIRECTIONAL);
257 if (dma_mapping_error(ab_bus)) {
258 free_page((unsigned long) ab);
259 return -ENOMEM;
260 }
261
2d826cc5 262 memset(&ab->descriptor, 0, sizeof(ab->descriptor));
a77754a7
KH
263 ab->descriptor.control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
264 DESCRIPTOR_STATUS |
265 DESCRIPTOR_BRANCH_ALWAYS);
32b46093
KH
266 offset = offsetof(struct ar_buffer, data);
267 ab->descriptor.req_count = cpu_to_le16(PAGE_SIZE - offset);
268 ab->descriptor.data_address = cpu_to_le32(ab_bus + offset);
269 ab->descriptor.res_count = cpu_to_le16(PAGE_SIZE - offset);
270 ab->descriptor.branch_address = 0;
271
272 dma_sync_single_for_device(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
273
ec839e43 274 ctx->last_buffer->descriptor.branch_address = cpu_to_le32(ab_bus | 1);
32b46093
KH
275 ctx->last_buffer->next = ab;
276 ctx->last_buffer = ab;
277
a77754a7 278 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
ed568912 279 flush_writes(ctx->ohci);
32b46093
KH
280
281 return 0;
ed568912
KH
282}
283
32b46093 284static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
ed568912 285{
ed568912 286 struct fw_ohci *ohci = ctx->ohci;
2639a6fb
KH
287 struct fw_packet p;
288 u32 status, length, tcode;
2639a6fb 289
32b46093
KH
290 p.header[0] = le32_to_cpu(buffer[0]);
291 p.header[1] = le32_to_cpu(buffer[1]);
292 p.header[2] = le32_to_cpu(buffer[2]);
2639a6fb
KH
293
294 tcode = (p.header[0] >> 4) & 0x0f;
295 switch (tcode) {
296 case TCODE_WRITE_QUADLET_REQUEST:
297 case TCODE_READ_QUADLET_RESPONSE:
32b46093 298 p.header[3] = (__force __u32) buffer[3];
2639a6fb 299 p.header_length = 16;
32b46093 300 p.payload_length = 0;
2639a6fb
KH
301 break;
302
2639a6fb 303 case TCODE_READ_BLOCK_REQUEST :
32b46093
KH
304 p.header[3] = le32_to_cpu(buffer[3]);
305 p.header_length = 16;
306 p.payload_length = 0;
307 break;
308
309 case TCODE_WRITE_BLOCK_REQUEST:
2639a6fb
KH
310 case TCODE_READ_BLOCK_RESPONSE:
311 case TCODE_LOCK_REQUEST:
312 case TCODE_LOCK_RESPONSE:
32b46093 313 p.header[3] = le32_to_cpu(buffer[3]);
2639a6fb 314 p.header_length = 16;
32b46093 315 p.payload_length = p.header[3] >> 16;
2639a6fb
KH
316 break;
317
318 case TCODE_WRITE_RESPONSE:
319 case TCODE_READ_QUADLET_REQUEST:
32b46093 320 case OHCI_TCODE_PHY_PACKET:
2639a6fb 321 p.header_length = 12;
32b46093 322 p.payload_length = 0;
2639a6fb
KH
323 break;
324 }
ed568912 325
32b46093
KH
326 p.payload = (void *) buffer + p.header_length;
327
328 /* FIXME: What to do about evt_* errors? */
329 length = (p.header_length + p.payload_length + 3) / 4;
330 status = le32_to_cpu(buffer[length]);
331
332 p.ack = ((status >> 16) & 0x1f) - 16;
333 p.speed = (status >> 21) & 0x7;
334 p.timestamp = status & 0xffff;
335 p.generation = ohci->request_generation;
ed568912 336
c781c06d
KH
337 /*
338 * The OHCI bus reset handler synthesizes a phy packet with
ed568912
KH
339 * the new generation number when a bus reset happens (see
340 * section 8.4.2.3). This helps us determine when a request
341 * was received and make sure we send the response in the same
342 * generation. We only need this for requests; for responses
343 * we use the unique tlabel for finding the matching
c781c06d
KH
344 * request.
345 */
ed568912 346
2639a6fb 347 if (p.ack + 16 == 0x09)
32b46093 348 ohci->request_generation = (buffer[2] >> 16) & 0xff;
ed568912 349 else if (ctx == &ohci->ar_request_ctx)
2639a6fb 350 fw_core_handle_request(&ohci->card, &p);
ed568912 351 else
2639a6fb 352 fw_core_handle_response(&ohci->card, &p);
ed568912 353
32b46093
KH
354 return buffer + length + 1;
355}
ed568912 356
32b46093
KH
357static void ar_context_tasklet(unsigned long data)
358{
359 struct ar_context *ctx = (struct ar_context *)data;
360 struct fw_ohci *ohci = ctx->ohci;
361 struct ar_buffer *ab;
362 struct descriptor *d;
363 void *buffer, *end;
364
365 ab = ctx->current_buffer;
366 d = &ab->descriptor;
367
368 if (d->res_count == 0) {
369 size_t size, rest, offset;
370
c781c06d
KH
371 /*
372 * This descriptor is finished and we may have a
32b46093 373 * packet split across this and the next buffer. We
c781c06d
KH
374 * reuse the page for reassembling the split packet.
375 */
32b46093
KH
376
377 offset = offsetof(struct ar_buffer, data);
378 dma_unmap_single(ohci->card.device,
0a9972ba
SR
379 le32_to_cpu(ab->descriptor.data_address) - offset,
380 PAGE_SIZE, DMA_BIDIRECTIONAL);
32b46093
KH
381
382 buffer = ab;
383 ab = ab->next;
384 d = &ab->descriptor;
385 size = buffer + PAGE_SIZE - ctx->pointer;
386 rest = le16_to_cpu(d->req_count) - le16_to_cpu(d->res_count);
387 memmove(buffer, ctx->pointer, size);
388 memcpy(buffer + size, ab->data, rest);
389 ctx->current_buffer = ab;
390 ctx->pointer = (void *) ab->data + rest;
391 end = buffer + size + rest;
392
393 while (buffer < end)
394 buffer = handle_ar_packet(ctx, buffer);
395
396 free_page((unsigned long)buffer);
397 ar_context_add_page(ctx);
398 } else {
399 buffer = ctx->pointer;
400 ctx->pointer = end =
401 (void *) ab + PAGE_SIZE - le16_to_cpu(d->res_count);
402
403 while (buffer < end)
404 buffer = handle_ar_packet(ctx, buffer);
405 }
ed568912
KH
406}
407
408static int
72e318e0 409ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 regs)
ed568912 410{
32b46093 411 struct ar_buffer ab;
ed568912 412
72e318e0
KH
413 ctx->regs = regs;
414 ctx->ohci = ohci;
415 ctx->last_buffer = &ab;
ed568912
KH
416 tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
417
32b46093
KH
418 ar_context_add_page(ctx);
419 ar_context_add_page(ctx);
420 ctx->current_buffer = ab.next;
421 ctx->pointer = ctx->current_buffer->data;
422
2aef469a
KH
423 return 0;
424}
425
426static void ar_context_run(struct ar_context *ctx)
427{
428 struct ar_buffer *ab = ctx->current_buffer;
429 dma_addr_t ab_bus;
430 size_t offset;
431
432 offset = offsetof(struct ar_buffer, data);
0a9972ba 433 ab_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
2aef469a
KH
434
435 reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ab_bus | 1);
a77754a7 436 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
32b46093 437 flush_writes(ctx->ohci);
ed568912 438}
373b2edd 439
30200739
KH
440static void context_tasklet(unsigned long data)
441{
442 struct context *ctx = (struct context *) data;
443 struct fw_ohci *ohci = ctx->ohci;
444 struct descriptor *d, *last;
445 u32 address;
446 int z;
447
448 dma_sync_single_for_cpu(ohci->card.device, ctx->buffer_bus,
449 ctx->buffer_size, DMA_TO_DEVICE);
450
451 d = ctx->tail_descriptor;
452 last = ctx->tail_descriptor_last;
453
454 while (last->branch_address != 0) {
455 address = le32_to_cpu(last->branch_address);
456 z = address & 0xf;
2d826cc5 457 d = ctx->buffer + (address - ctx->buffer_bus) / sizeof(*d);
30200739
KH
458 last = (z == 2) ? d : d + z - 1;
459
460 if (!ctx->callback(ctx, d, last))
461 break;
462
463 ctx->tail_descriptor = d;
464 ctx->tail_descriptor_last = last;
465 }
466}
467
468static int
469context_init(struct context *ctx, struct fw_ohci *ohci,
470 size_t buffer_size, u32 regs,
471 descriptor_callback_t callback)
472{
473 ctx->ohci = ohci;
474 ctx->regs = regs;
475 ctx->buffer_size = buffer_size;
476 ctx->buffer = kmalloc(buffer_size, GFP_KERNEL);
477 if (ctx->buffer == NULL)
478 return -ENOMEM;
479
480 tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
481 ctx->callback = callback;
482
483 ctx->buffer_bus =
484 dma_map_single(ohci->card.device, ctx->buffer,
485 buffer_size, DMA_TO_DEVICE);
486 if (dma_mapping_error(ctx->buffer_bus)) {
487 kfree(ctx->buffer);
488 return -ENOMEM;
489 }
490
491 ctx->head_descriptor = ctx->buffer;
492 ctx->prev_descriptor = ctx->buffer;
493 ctx->tail_descriptor = ctx->buffer;
494 ctx->tail_descriptor_last = ctx->buffer;
495
c781c06d
KH
496 /*
497 * We put a dummy descriptor in the buffer that has a NULL
30200739
KH
498 * branch address and looks like it's been sent. That way we
499 * have a descriptor to append DMA programs to. Also, the
500 * ring buffer invariant is that it always has at least one
c781c06d
KH
501 * element so that head == tail means buffer full.
502 */
30200739 503
2d826cc5 504 memset(ctx->head_descriptor, 0, sizeof(*ctx->head_descriptor));
a77754a7 505 ctx->head_descriptor->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
30200739
KH
506 ctx->head_descriptor->transfer_status = cpu_to_le16(0x8011);
507 ctx->head_descriptor++;
508
509 return 0;
510}
511
9b32d5f3 512static void
30200739
KH
513context_release(struct context *ctx)
514{
515 struct fw_card *card = &ctx->ohci->card;
516
517 dma_unmap_single(card->device, ctx->buffer_bus,
518 ctx->buffer_size, DMA_TO_DEVICE);
519 kfree(ctx->buffer);
520}
521
522static struct descriptor *
523context_get_descriptors(struct context *ctx, int z, dma_addr_t *d_bus)
524{
525 struct descriptor *d, *tail, *end;
526
527 d = ctx->head_descriptor;
528 tail = ctx->tail_descriptor;
2d826cc5 529 end = ctx->buffer + ctx->buffer_size / sizeof(*d);
30200739
KH
530
531 if (d + z <= tail) {
532 goto has_space;
533 } else if (d > tail && d + z <= end) {
534 goto has_space;
535 } else if (d > tail && ctx->buffer + z <= tail) {
536 d = ctx->buffer;
537 goto has_space;
538 }
539
540 return NULL;
541
542 has_space:
2d826cc5
KH
543 memset(d, 0, z * sizeof(*d));
544 *d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof(*d);
30200739
KH
545
546 return d;
547}
548
295e3feb 549static void context_run(struct context *ctx, u32 extra)
30200739
KH
550{
551 struct fw_ohci *ohci = ctx->ohci;
552
a77754a7 553 reg_write(ohci, COMMAND_PTR(ctx->regs),
30200739 554 le32_to_cpu(ctx->tail_descriptor_last->branch_address));
a77754a7
KH
555 reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
556 reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
30200739
KH
557 flush_writes(ohci);
558}
559
560static void context_append(struct context *ctx,
561 struct descriptor *d, int z, int extra)
562{
563 dma_addr_t d_bus;
564
2d826cc5 565 d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof(*d);
30200739
KH
566
567 ctx->head_descriptor = d + z + extra;
568 ctx->prev_descriptor->branch_address = cpu_to_le32(d_bus | z);
569 ctx->prev_descriptor = z == 2 ? d : d + z - 1;
570
571 dma_sync_single_for_device(ctx->ohci->card.device, ctx->buffer_bus,
572 ctx->buffer_size, DMA_TO_DEVICE);
573
a77754a7 574 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
30200739
KH
575 flush_writes(ctx->ohci);
576}
577
578static void context_stop(struct context *ctx)
579{
580 u32 reg;
b8295668 581 int i;
30200739 582
a77754a7 583 reg_write(ctx->ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
b8295668 584 flush_writes(ctx->ohci);
30200739 585
b8295668 586 for (i = 0; i < 10; i++) {
a77754a7 587 reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
b8295668
KH
588 if ((reg & CONTEXT_ACTIVE) == 0)
589 break;
590
591 fw_notify("context_stop: still active (0x%08x)\n", reg);
b980f5a2 592 mdelay(1);
b8295668 593 }
30200739 594}
ed568912 595
f319b6a0
KH
596struct driver_data {
597 struct fw_packet *packet;
598};
ed568912 599
c781c06d
KH
600/*
601 * This function apppends a packet to the DMA queue for transmission.
f319b6a0 602 * Must always be called with the ochi->lock held to ensure proper
c781c06d
KH
603 * generation handling and locking around packet queue manipulation.
604 */
f319b6a0
KH
605static int
606at_context_queue_packet(struct context *ctx, struct fw_packet *packet)
ed568912 607{
ed568912 608 struct fw_ohci *ohci = ctx->ohci;
4b6d51ec 609 dma_addr_t d_bus, uninitialized_var(payload_bus);
f319b6a0
KH
610 struct driver_data *driver_data;
611 struct descriptor *d, *last;
612 __le32 *header;
ed568912 613 int z, tcode;
f319b6a0 614 u32 reg;
ed568912 615
f319b6a0
KH
616 d = context_get_descriptors(ctx, 4, &d_bus);
617 if (d == NULL) {
618 packet->ack = RCODE_SEND_ERROR;
619 return -1;
ed568912
KH
620 }
621
a77754a7 622 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
f319b6a0
KH
623 d[0].res_count = cpu_to_le16(packet->timestamp);
624
c781c06d
KH
625 /*
626 * The DMA format for asyncronous link packets is different
ed568912
KH
627 * from the IEEE1394 layout, so shift the fields around
628 * accordingly. If header_length is 8, it's a PHY packet, to
c781c06d
KH
629 * which we need to prepend an extra quadlet.
630 */
f319b6a0
KH
631
632 header = (__le32 *) &d[1];
ed568912 633 if (packet->header_length > 8) {
f319b6a0
KH
634 header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
635 (packet->speed << 16));
636 header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
637 (packet->header[0] & 0xffff0000));
638 header[2] = cpu_to_le32(packet->header[2]);
ed568912
KH
639
640 tcode = (packet->header[0] >> 4) & 0x0f;
641 if (TCODE_IS_BLOCK_PACKET(tcode))
f319b6a0 642 header[3] = cpu_to_le32(packet->header[3]);
ed568912 643 else
f319b6a0
KH
644 header[3] = (__force __le32) packet->header[3];
645
646 d[0].req_count = cpu_to_le16(packet->header_length);
ed568912 647 } else {
f319b6a0
KH
648 header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
649 (packet->speed << 16));
650 header[1] = cpu_to_le32(packet->header[0]);
651 header[2] = cpu_to_le32(packet->header[1]);
652 d[0].req_count = cpu_to_le16(12);
ed568912
KH
653 }
654
f319b6a0
KH
655 driver_data = (struct driver_data *) &d[3];
656 driver_data->packet = packet;
20d11673 657 packet->driver_data = driver_data;
f319b6a0
KH
658
659 if (packet->payload_length > 0) {
660 payload_bus =
661 dma_map_single(ohci->card.device, packet->payload,
662 packet->payload_length, DMA_TO_DEVICE);
663 if (dma_mapping_error(payload_bus)) {
664 packet->ack = RCODE_SEND_ERROR;
665 return -1;
666 }
667
668 d[2].req_count = cpu_to_le16(packet->payload_length);
669 d[2].data_address = cpu_to_le32(payload_bus);
670 last = &d[2];
671 z = 3;
ed568912 672 } else {
f319b6a0
KH
673 last = &d[0];
674 z = 2;
ed568912 675 }
ed568912 676
a77754a7
KH
677 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
678 DESCRIPTOR_IRQ_ALWAYS |
679 DESCRIPTOR_BRANCH_ALWAYS);
ed568912 680
f319b6a0
KH
681 /* FIXME: Document how the locking works. */
682 if (ohci->generation != packet->generation) {
ab88ca48
SR
683 if (packet->payload_length > 0)
684 dma_unmap_single(ohci->card.device, payload_bus,
685 packet->payload_length, DMA_TO_DEVICE);
f319b6a0
KH
686 packet->ack = RCODE_GENERATION;
687 return -1;
688 }
689
690 context_append(ctx, d, z, 4 - z);
ed568912 691
f319b6a0 692 /* If the context isn't already running, start it up. */
a77754a7 693 reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
053b3080 694 if ((reg & CONTEXT_RUN) == 0)
f319b6a0
KH
695 context_run(ctx, 0);
696
697 return 0;
ed568912
KH
698}
699
f319b6a0
KH
700static int handle_at_packet(struct context *context,
701 struct descriptor *d,
702 struct descriptor *last)
ed568912 703{
f319b6a0 704 struct driver_data *driver_data;
ed568912 705 struct fw_packet *packet;
f319b6a0
KH
706 struct fw_ohci *ohci = context->ohci;
707 dma_addr_t payload_bus;
ed568912
KH
708 int evt;
709
f319b6a0
KH
710 if (last->transfer_status == 0)
711 /* This descriptor isn't done yet, stop iteration. */
712 return 0;
ed568912 713
f319b6a0
KH
714 driver_data = (struct driver_data *) &d[3];
715 packet = driver_data->packet;
716 if (packet == NULL)
717 /* This packet was cancelled, just continue. */
718 return 1;
730c32f5 719
f319b6a0
KH
720 payload_bus = le32_to_cpu(last->data_address);
721 if (payload_bus != 0)
722 dma_unmap_single(ohci->card.device, payload_bus,
ed568912 723 packet->payload_length, DMA_TO_DEVICE);
ed568912 724
f319b6a0
KH
725 evt = le16_to_cpu(last->transfer_status) & 0x1f;
726 packet->timestamp = le16_to_cpu(last->res_count);
ed568912 727
f319b6a0
KH
728 switch (evt) {
729 case OHCI1394_evt_timeout:
730 /* Async response transmit timed out. */
731 packet->ack = RCODE_CANCELLED;
732 break;
ed568912 733
f319b6a0 734 case OHCI1394_evt_flushed:
c781c06d
KH
735 /*
736 * The packet was flushed should give same error as
737 * when we try to use a stale generation count.
738 */
f319b6a0
KH
739 packet->ack = RCODE_GENERATION;
740 break;
ed568912 741
f319b6a0 742 case OHCI1394_evt_missing_ack:
c781c06d
KH
743 /*
744 * Using a valid (current) generation count, but the
745 * node is not on the bus or not sending acks.
746 */
f319b6a0
KH
747 packet->ack = RCODE_NO_ACK;
748 break;
ed568912 749
f319b6a0
KH
750 case ACK_COMPLETE + 0x10:
751 case ACK_PENDING + 0x10:
752 case ACK_BUSY_X + 0x10:
753 case ACK_BUSY_A + 0x10:
754 case ACK_BUSY_B + 0x10:
755 case ACK_DATA_ERROR + 0x10:
756 case ACK_TYPE_ERROR + 0x10:
757 packet->ack = evt - 0x10;
758 break;
ed568912 759
f319b6a0
KH
760 default:
761 packet->ack = RCODE_SEND_ERROR;
762 break;
763 }
ed568912 764
f319b6a0 765 packet->callback(packet, &ohci->card, packet->ack);
ed568912 766
f319b6a0 767 return 1;
ed568912
KH
768}
769
a77754a7
KH
770#define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
771#define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
772#define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
773#define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
774#define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
93c4cceb
KH
775
776static void
777handle_local_rom(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
778{
779 struct fw_packet response;
780 int tcode, length, i;
781
a77754a7 782 tcode = HEADER_GET_TCODE(packet->header[0]);
93c4cceb 783 if (TCODE_IS_BLOCK_PACKET(tcode))
a77754a7 784 length = HEADER_GET_DATA_LENGTH(packet->header[3]);
93c4cceb
KH
785 else
786 length = 4;
787
788 i = csr - CSR_CONFIG_ROM;
789 if (i + length > CONFIG_ROM_SIZE) {
790 fw_fill_response(&response, packet->header,
791 RCODE_ADDRESS_ERROR, NULL, 0);
792 } else if (!TCODE_IS_READ_REQUEST(tcode)) {
793 fw_fill_response(&response, packet->header,
794 RCODE_TYPE_ERROR, NULL, 0);
795 } else {
796 fw_fill_response(&response, packet->header, RCODE_COMPLETE,
797 (void *) ohci->config_rom + i, length);
798 }
799
800 fw_core_handle_response(&ohci->card, &response);
801}
802
803static void
804handle_local_lock(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
805{
806 struct fw_packet response;
807 int tcode, length, ext_tcode, sel;
808 __be32 *payload, lock_old;
809 u32 lock_arg, lock_data;
810
a77754a7
KH
811 tcode = HEADER_GET_TCODE(packet->header[0]);
812 length = HEADER_GET_DATA_LENGTH(packet->header[3]);
93c4cceb 813 payload = packet->payload;
a77754a7 814 ext_tcode = HEADER_GET_EXTENDED_TCODE(packet->header[3]);
93c4cceb
KH
815
816 if (tcode == TCODE_LOCK_REQUEST &&
817 ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
818 lock_arg = be32_to_cpu(payload[0]);
819 lock_data = be32_to_cpu(payload[1]);
820 } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
821 lock_arg = 0;
822 lock_data = 0;
823 } else {
824 fw_fill_response(&response, packet->header,
825 RCODE_TYPE_ERROR, NULL, 0);
826 goto out;
827 }
828
829 sel = (csr - CSR_BUS_MANAGER_ID) / 4;
830 reg_write(ohci, OHCI1394_CSRData, lock_data);
831 reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
832 reg_write(ohci, OHCI1394_CSRControl, sel);
833
834 if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
835 lock_old = cpu_to_be32(reg_read(ohci, OHCI1394_CSRData));
836 else
837 fw_notify("swap not done yet\n");
838
839 fw_fill_response(&response, packet->header,
2d826cc5 840 RCODE_COMPLETE, &lock_old, sizeof(lock_old));
93c4cceb
KH
841 out:
842 fw_core_handle_response(&ohci->card, &response);
843}
844
845static void
f319b6a0 846handle_local_request(struct context *ctx, struct fw_packet *packet)
93c4cceb
KH
847{
848 u64 offset;
849 u32 csr;
850
473d28c7
KH
851 if (ctx == &ctx->ohci->at_request_ctx) {
852 packet->ack = ACK_PENDING;
853 packet->callback(packet, &ctx->ohci->card, packet->ack);
854 }
93c4cceb
KH
855
856 offset =
857 ((unsigned long long)
a77754a7 858 HEADER_GET_OFFSET_HIGH(packet->header[1]) << 32) |
93c4cceb
KH
859 packet->header[2];
860 csr = offset - CSR_REGISTER_BASE;
861
862 /* Handle config rom reads. */
863 if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
864 handle_local_rom(ctx->ohci, packet, csr);
865 else switch (csr) {
866 case CSR_BUS_MANAGER_ID:
867 case CSR_BANDWIDTH_AVAILABLE:
868 case CSR_CHANNELS_AVAILABLE_HI:
869 case CSR_CHANNELS_AVAILABLE_LO:
870 handle_local_lock(ctx->ohci, packet, csr);
871 break;
872 default:
873 if (ctx == &ctx->ohci->at_request_ctx)
874 fw_core_handle_request(&ctx->ohci->card, packet);
875 else
876 fw_core_handle_response(&ctx->ohci->card, packet);
877 break;
878 }
473d28c7
KH
879
880 if (ctx == &ctx->ohci->at_response_ctx) {
881 packet->ack = ACK_COMPLETE;
882 packet->callback(packet, &ctx->ohci->card, packet->ack);
883 }
93c4cceb 884}
e636fe25 885
ed568912 886static void
f319b6a0 887at_context_transmit(struct context *ctx, struct fw_packet *packet)
ed568912 888{
ed568912 889 unsigned long flags;
f319b6a0 890 int retval;
ed568912
KH
891
892 spin_lock_irqsave(&ctx->ohci->lock, flags);
893
a77754a7 894 if (HEADER_GET_DESTINATION(packet->header[0]) == ctx->ohci->node_id &&
e636fe25 895 ctx->ohci->generation == packet->generation) {
93c4cceb
KH
896 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
897 handle_local_request(ctx, packet);
898 return;
e636fe25 899 }
ed568912 900
f319b6a0 901 retval = at_context_queue_packet(ctx, packet);
ed568912
KH
902 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
903
f319b6a0
KH
904 if (retval < 0)
905 packet->callback(packet, &ctx->ohci->card, packet->ack);
906
ed568912
KH
907}
908
909static void bus_reset_tasklet(unsigned long data)
910{
911 struct fw_ohci *ohci = (struct fw_ohci *)data;
e636fe25 912 int self_id_count, i, j, reg;
ed568912
KH
913 int generation, new_generation;
914 unsigned long flags;
4eaff7d6
SR
915 void *free_rom = NULL;
916 dma_addr_t free_rom_bus = 0;
ed568912
KH
917
918 reg = reg_read(ohci, OHCI1394_NodeID);
919 if (!(reg & OHCI1394_NodeID_idValid)) {
02ff8f8e 920 fw_notify("node ID not valid, new bus reset in progress\n");
ed568912
KH
921 return;
922 }
02ff8f8e
SR
923 if ((reg & OHCI1394_NodeID_nodeNumber) == 63) {
924 fw_notify("malconfigured bus\n");
925 return;
926 }
927 ohci->node_id = reg & (OHCI1394_NodeID_busNumber |
928 OHCI1394_NodeID_nodeNumber);
ed568912 929
c781c06d
KH
930 /*
931 * The count in the SelfIDCount register is the number of
ed568912
KH
932 * bytes in the self ID receive buffer. Since we also receive
933 * the inverted quadlets and a header quadlet, we shift one
c781c06d
KH
934 * bit extra to get the actual number of self IDs.
935 */
ed568912
KH
936
937 self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
938 generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
ee71c2f9 939 rmb();
ed568912
KH
940
941 for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
942 if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
943 fw_error("inconsistent self IDs\n");
944 ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]);
945 }
ee71c2f9 946 rmb();
ed568912 947
c781c06d
KH
948 /*
949 * Check the consistency of the self IDs we just read. The
ed568912
KH
950 * problem we face is that a new bus reset can start while we
951 * read out the self IDs from the DMA buffer. If this happens,
952 * the DMA buffer will be overwritten with new self IDs and we
953 * will read out inconsistent data. The OHCI specification
954 * (section 11.2) recommends a technique similar to
955 * linux/seqlock.h, where we remember the generation of the
956 * self IDs in the buffer before reading them out and compare
957 * it to the current generation after reading them out. If
958 * the two generations match we know we have a consistent set
c781c06d
KH
959 * of self IDs.
960 */
ed568912
KH
961
962 new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
963 if (new_generation != generation) {
964 fw_notify("recursive bus reset detected, "
965 "discarding self ids\n");
966 return;
967 }
968
969 /* FIXME: Document how the locking works. */
970 spin_lock_irqsave(&ohci->lock, flags);
971
972 ohci->generation = generation;
f319b6a0
KH
973 context_stop(&ohci->at_request_ctx);
974 context_stop(&ohci->at_response_ctx);
ed568912
KH
975 reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
976
c781c06d
KH
977 /*
978 * This next bit is unrelated to the AT context stuff but we
ed568912
KH
979 * have to do it under the spinlock also. If a new config rom
980 * was set up before this reset, the old one is now no longer
981 * in use and we can free it. Update the config rom pointers
982 * to point to the current config rom and clear the
c781c06d
KH
983 * next_config_rom pointer so a new udpate can take place.
984 */
ed568912
KH
985
986 if (ohci->next_config_rom != NULL) {
4eaff7d6
SR
987 free_rom = ohci->config_rom;
988 free_rom_bus = ohci->config_rom_bus;
ed568912
KH
989 ohci->config_rom = ohci->next_config_rom;
990 ohci->config_rom_bus = ohci->next_config_rom_bus;
991 ohci->next_config_rom = NULL;
992
c781c06d
KH
993 /*
994 * Restore config_rom image and manually update
ed568912
KH
995 * config_rom registers. Writing the header quadlet
996 * will indicate that the config rom is ready, so we
c781c06d
KH
997 * do that last.
998 */
ed568912
KH
999 reg_write(ohci, OHCI1394_BusOptions,
1000 be32_to_cpu(ohci->config_rom[2]));
1001 ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
1002 reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
1003 }
1004
1005 spin_unlock_irqrestore(&ohci->lock, flags);
1006
4eaff7d6
SR
1007 if (free_rom)
1008 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1009 free_rom, free_rom_bus);
1010
e636fe25 1011 fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
ed568912
KH
1012 self_id_count, ohci->self_id_buffer);
1013}
1014
1015static irqreturn_t irq_handler(int irq, void *data)
1016{
1017 struct fw_ohci *ohci = data;
d60d7f1d 1018 u32 event, iso_event, cycle_time;
ed568912
KH
1019 int i;
1020
1021 event = reg_read(ohci, OHCI1394_IntEventClear);
1022
a515958d 1023 if (!event || !~event)
ed568912
KH
1024 return IRQ_NONE;
1025
1026 reg_write(ohci, OHCI1394_IntEventClear, event);
1027
1028 if (event & OHCI1394_selfIDComplete)
1029 tasklet_schedule(&ohci->bus_reset_tasklet);
1030
1031 if (event & OHCI1394_RQPkt)
1032 tasklet_schedule(&ohci->ar_request_ctx.tasklet);
1033
1034 if (event & OHCI1394_RSPkt)
1035 tasklet_schedule(&ohci->ar_response_ctx.tasklet);
1036
1037 if (event & OHCI1394_reqTxComplete)
1038 tasklet_schedule(&ohci->at_request_ctx.tasklet);
1039
1040 if (event & OHCI1394_respTxComplete)
1041 tasklet_schedule(&ohci->at_response_ctx.tasklet);
1042
c889475f 1043 iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
ed568912
KH
1044 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
1045
1046 while (iso_event) {
1047 i = ffs(iso_event) - 1;
30200739 1048 tasklet_schedule(&ohci->ir_context_list[i].context.tasklet);
ed568912
KH
1049 iso_event &= ~(1 << i);
1050 }
1051
c889475f 1052 iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
ed568912
KH
1053 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
1054
1055 while (iso_event) {
1056 i = ffs(iso_event) - 1;
30200739 1057 tasklet_schedule(&ohci->it_context_list[i].context.tasklet);
ed568912
KH
1058 iso_event &= ~(1 << i);
1059 }
1060
e524f616
SR
1061 if (unlikely(event & OHCI1394_postedWriteErr))
1062 fw_error("PCI posted write error\n");
1063
d60d7f1d
KH
1064 if (event & OHCI1394_cycle64Seconds) {
1065 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1066 if ((cycle_time & 0x80000000) == 0)
1067 ohci->bus_seconds++;
1068 }
1069
ed568912
KH
1070 return IRQ_HANDLED;
1071}
1072
2aef469a
KH
1073static int software_reset(struct fw_ohci *ohci)
1074{
1075 int i;
1076
1077 reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
1078
1079 for (i = 0; i < OHCI_LOOP_COUNT; i++) {
1080 if ((reg_read(ohci, OHCI1394_HCControlSet) &
1081 OHCI1394_HCControl_softReset) == 0)
1082 return 0;
1083 msleep(1);
1084 }
1085
1086 return -EBUSY;
1087}
1088
ed568912
KH
1089static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
1090{
1091 struct fw_ohci *ohci = fw_ohci(card);
1092 struct pci_dev *dev = to_pci_dev(card->device);
1093
2aef469a
KH
1094 if (software_reset(ohci)) {
1095 fw_error("Failed to reset ohci card.\n");
1096 return -EBUSY;
1097 }
1098
1099 /*
1100 * Now enable LPS, which we need in order to start accessing
1101 * most of the registers. In fact, on some cards (ALI M5251),
1102 * accessing registers in the SClk domain without LPS enabled
1103 * will lock up the machine. Wait 50msec to make sure we have
1104 * full link enabled.
1105 */
1106 reg_write(ohci, OHCI1394_HCControlSet,
1107 OHCI1394_HCControl_LPS |
1108 OHCI1394_HCControl_postedWriteEnable);
1109 flush_writes(ohci);
1110 msleep(50);
1111
1112 reg_write(ohci, OHCI1394_HCControlClear,
1113 OHCI1394_HCControl_noByteSwapData);
1114
1115 reg_write(ohci, OHCI1394_LinkControlSet,
1116 OHCI1394_LinkControl_rcvSelfID |
1117 OHCI1394_LinkControl_cycleTimerEnable |
1118 OHCI1394_LinkControl_cycleMaster);
1119
1120 reg_write(ohci, OHCI1394_ATRetries,
1121 OHCI1394_MAX_AT_REQ_RETRIES |
1122 (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
1123 (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
1124
1125 ar_context_run(&ohci->ar_request_ctx);
1126 ar_context_run(&ohci->ar_response_ctx);
1127
1128 reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
1129 reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
1130 reg_write(ohci, OHCI1394_IntEventClear, ~0);
1131 reg_write(ohci, OHCI1394_IntMaskClear, ~0);
1132 reg_write(ohci, OHCI1394_IntMaskSet,
1133 OHCI1394_selfIDComplete |
1134 OHCI1394_RQPkt | OHCI1394_RSPkt |
1135 OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
1136 OHCI1394_isochRx | OHCI1394_isochTx |
e524f616
SR
1137 OHCI1394_postedWriteErr | OHCI1394_cycle64Seconds |
1138 OHCI1394_masterIntEnable);
2aef469a
KH
1139
1140 /* Activate link_on bit and contender bit in our self ID packets.*/
1141 if (ohci_update_phy_reg(card, 4, 0,
1142 PHY_LINK_ACTIVE | PHY_CONTENDER) < 0)
1143 return -EIO;
1144
c781c06d
KH
1145 /*
1146 * When the link is not yet enabled, the atomic config rom
ed568912
KH
1147 * update mechanism described below in ohci_set_config_rom()
1148 * is not active. We have to update ConfigRomHeader and
1149 * BusOptions manually, and the write to ConfigROMmap takes
1150 * effect immediately. We tie this to the enabling of the
1151 * link, so we have a valid config rom before enabling - the
1152 * OHCI requires that ConfigROMhdr and BusOptions have valid
1153 * values before enabling.
1154 *
1155 * However, when the ConfigROMmap is written, some controllers
1156 * always read back quadlets 0 and 2 from the config rom to
1157 * the ConfigRomHeader and BusOptions registers on bus reset.
1158 * They shouldn't do that in this initial case where the link
1159 * isn't enabled. This means we have to use the same
1160 * workaround here, setting the bus header to 0 and then write
1161 * the right values in the bus reset tasklet.
1162 */
1163
1164 ohci->next_config_rom =
1165 dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1166 &ohci->next_config_rom_bus, GFP_KERNEL);
1167 if (ohci->next_config_rom == NULL)
1168 return -ENOMEM;
1169
1170 memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
1171 fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
1172
1173 ohci->next_header = config_rom[0];
1174 ohci->next_config_rom[0] = 0;
1175 reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
1176 reg_write(ohci, OHCI1394_BusOptions, config_rom[2]);
1177 reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
1178
1179 reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
1180
1181 if (request_irq(dev->irq, irq_handler,
65efffa8 1182 IRQF_SHARED, ohci_driver_name, ohci)) {
ed568912
KH
1183 fw_error("Failed to allocate shared interrupt %d.\n",
1184 dev->irq);
1185 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1186 ohci->config_rom, ohci->config_rom_bus);
1187 return -EIO;
1188 }
1189
1190 reg_write(ohci, OHCI1394_HCControlSet,
1191 OHCI1394_HCControl_linkEnable |
1192 OHCI1394_HCControl_BIBimageValid);
1193 flush_writes(ohci);
1194
c781c06d
KH
1195 /*
1196 * We are ready to go, initiate bus reset to finish the
1197 * initialization.
1198 */
ed568912
KH
1199
1200 fw_core_initiate_bus_reset(&ohci->card, 1);
1201
1202 return 0;
1203}
1204
1205static int
1206ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
1207{
1208 struct fw_ohci *ohci;
1209 unsigned long flags;
4eaff7d6 1210 int retval = -EBUSY;
ed568912
KH
1211 __be32 *next_config_rom;
1212 dma_addr_t next_config_rom_bus;
1213
1214 ohci = fw_ohci(card);
1215
c781c06d
KH
1216 /*
1217 * When the OHCI controller is enabled, the config rom update
ed568912
KH
1218 * mechanism is a bit tricky, but easy enough to use. See
1219 * section 5.5.6 in the OHCI specification.
1220 *
1221 * The OHCI controller caches the new config rom address in a
1222 * shadow register (ConfigROMmapNext) and needs a bus reset
1223 * for the changes to take place. When the bus reset is
1224 * detected, the controller loads the new values for the
1225 * ConfigRomHeader and BusOptions registers from the specified
1226 * config rom and loads ConfigROMmap from the ConfigROMmapNext
1227 * shadow register. All automatically and atomically.
1228 *
1229 * Now, there's a twist to this story. The automatic load of
1230 * ConfigRomHeader and BusOptions doesn't honor the
1231 * noByteSwapData bit, so with a be32 config rom, the
1232 * controller will load be32 values in to these registers
1233 * during the atomic update, even on litte endian
1234 * architectures. The workaround we use is to put a 0 in the
1235 * header quadlet; 0 is endian agnostic and means that the
1236 * config rom isn't ready yet. In the bus reset tasklet we
1237 * then set up the real values for the two registers.
1238 *
1239 * We use ohci->lock to avoid racing with the code that sets
1240 * ohci->next_config_rom to NULL (see bus_reset_tasklet).
1241 */
1242
1243 next_config_rom =
1244 dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1245 &next_config_rom_bus, GFP_KERNEL);
1246 if (next_config_rom == NULL)
1247 return -ENOMEM;
1248
1249 spin_lock_irqsave(&ohci->lock, flags);
1250
1251 if (ohci->next_config_rom == NULL) {
1252 ohci->next_config_rom = next_config_rom;
1253 ohci->next_config_rom_bus = next_config_rom_bus;
1254
1255 memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
1256 fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
1257 length * 4);
1258
1259 ohci->next_header = config_rom[0];
1260 ohci->next_config_rom[0] = 0;
1261
1262 reg_write(ohci, OHCI1394_ConfigROMmap,
1263 ohci->next_config_rom_bus);
4eaff7d6 1264 retval = 0;
ed568912
KH
1265 }
1266
1267 spin_unlock_irqrestore(&ohci->lock, flags);
1268
c781c06d
KH
1269 /*
1270 * Now initiate a bus reset to have the changes take
ed568912
KH
1271 * effect. We clean up the old config rom memory and DMA
1272 * mappings in the bus reset tasklet, since the OHCI
1273 * controller could need to access it before the bus reset
c781c06d
KH
1274 * takes effect.
1275 */
ed568912
KH
1276 if (retval == 0)
1277 fw_core_initiate_bus_reset(&ohci->card, 1);
4eaff7d6
SR
1278 else
1279 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1280 next_config_rom, next_config_rom_bus);
ed568912
KH
1281
1282 return retval;
1283}
1284
1285static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
1286{
1287 struct fw_ohci *ohci = fw_ohci(card);
1288
1289 at_context_transmit(&ohci->at_request_ctx, packet);
1290}
1291
1292static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
1293{
1294 struct fw_ohci *ohci = fw_ohci(card);
1295
1296 at_context_transmit(&ohci->at_response_ctx, packet);
1297}
1298
730c32f5
KH
1299static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
1300{
1301 struct fw_ohci *ohci = fw_ohci(card);
f319b6a0
KH
1302 struct context *ctx = &ohci->at_request_ctx;
1303 struct driver_data *driver_data = packet->driver_data;
1304 int retval = -ENOENT;
730c32f5 1305
f319b6a0 1306 tasklet_disable(&ctx->tasklet);
730c32f5 1307
f319b6a0
KH
1308 if (packet->ack != 0)
1309 goto out;
730c32f5 1310
f319b6a0
KH
1311 driver_data->packet = NULL;
1312 packet->ack = RCODE_CANCELLED;
1313 packet->callback(packet, &ohci->card, packet->ack);
1314 retval = 0;
730c32f5 1315
f319b6a0
KH
1316 out:
1317 tasklet_enable(&ctx->tasklet);
730c32f5 1318
f319b6a0 1319 return retval;
730c32f5
KH
1320}
1321
ed568912
KH
1322static int
1323ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
1324{
1325 struct fw_ohci *ohci = fw_ohci(card);
1326 unsigned long flags;
907293d7 1327 int n, retval = 0;
ed568912 1328
c781c06d
KH
1329 /*
1330 * FIXME: Make sure this bitmask is cleared when we clear the busReset
1331 * interrupt bit. Clear physReqResourceAllBuses on bus reset.
1332 */
ed568912
KH
1333
1334 spin_lock_irqsave(&ohci->lock, flags);
1335
1336 if (ohci->generation != generation) {
1337 retval = -ESTALE;
1338 goto out;
1339 }
1340
c781c06d
KH
1341 /*
1342 * Note, if the node ID contains a non-local bus ID, physical DMA is
1343 * enabled for _all_ nodes on remote buses.
1344 */
907293d7
SR
1345
1346 n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
1347 if (n < 32)
1348 reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
1349 else
1350 reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
1351
ed568912 1352 flush_writes(ohci);
ed568912 1353 out:
6cad95fe 1354 spin_unlock_irqrestore(&ohci->lock, flags);
ed568912
KH
1355 return retval;
1356}
373b2edd 1357
d60d7f1d
KH
1358static u64
1359ohci_get_bus_time(struct fw_card *card)
1360{
1361 struct fw_ohci *ohci = fw_ohci(card);
1362 u32 cycle_time;
1363 u64 bus_time;
1364
1365 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1366 bus_time = ((u64) ohci->bus_seconds << 32) | cycle_time;
1367
1368 return bus_time;
1369}
1370
d2746dc1
KH
1371static int handle_ir_dualbuffer_packet(struct context *context,
1372 struct descriptor *d,
1373 struct descriptor *last)
ed568912 1374{
295e3feb
KH
1375 struct iso_context *ctx =
1376 container_of(context, struct iso_context, context);
1377 struct db_descriptor *db = (struct db_descriptor *) d;
c70dc788 1378 __le32 *ir_header;
9b32d5f3 1379 size_t header_length;
c70dc788
KH
1380 void *p, *end;
1381 int i;
d2746dc1 1382
295e3feb
KH
1383 if (db->first_res_count > 0 && db->second_res_count > 0)
1384 /* This descriptor isn't done yet, stop iteration. */
1385 return 0;
1386
c70dc788
KH
1387 header_length = le16_to_cpu(db->first_req_count) -
1388 le16_to_cpu(db->first_res_count);
1389
1390 i = ctx->header_length;
1391 p = db + 1;
1392 end = p + header_length;
1393 while (p < end && i + ctx->base.header_size <= PAGE_SIZE) {
c781c06d
KH
1394 /*
1395 * The iso header is byteswapped to little endian by
15536221
KH
1396 * the controller, but the remaining header quadlets
1397 * are big endian. We want to present all the headers
1398 * as big endian, so we have to swap the first
c781c06d
KH
1399 * quadlet.
1400 */
15536221
KH
1401 *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
1402 memcpy(ctx->header + i + 4, p + 8, ctx->base.header_size - 4);
c70dc788
KH
1403 i += ctx->base.header_size;
1404 p += ctx->base.header_size + 4;
1405 }
1406
1407 ctx->header_length = i;
9b32d5f3 1408
a77754a7 1409 if (le16_to_cpu(db->control) & DESCRIPTOR_IRQ_ALWAYS) {
c70dc788
KH
1410 ir_header = (__le32 *) (db + 1);
1411 ctx->base.callback(&ctx->base,
1412 le32_to_cpu(ir_header[0]) & 0xffff,
9b32d5f3 1413 ctx->header_length, ctx->header,
295e3feb 1414 ctx->base.callback_data);
9b32d5f3
KH
1415 ctx->header_length = 0;
1416 }
ed568912 1417
295e3feb 1418 return 1;
ed568912
KH
1419}
1420
30200739
KH
1421static int handle_it_packet(struct context *context,
1422 struct descriptor *d,
1423 struct descriptor *last)
ed568912 1424{
30200739
KH
1425 struct iso_context *ctx =
1426 container_of(context, struct iso_context, context);
373b2edd 1427
30200739
KH
1428 if (last->transfer_status == 0)
1429 /* This descriptor isn't done yet, stop iteration. */
1430 return 0;
1431
a77754a7 1432 if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS)
9b32d5f3
KH
1433 ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count),
1434 0, NULL, ctx->base.callback_data);
30200739
KH
1435
1436 return 1;
ed568912
KH
1437}
1438
30200739 1439static struct fw_iso_context *
eb0306ea 1440ohci_allocate_iso_context(struct fw_card *card, int type, size_t header_size)
ed568912
KH
1441{
1442 struct fw_ohci *ohci = fw_ohci(card);
1443 struct iso_context *ctx, *list;
30200739 1444 descriptor_callback_t callback;
295e3feb 1445 u32 *mask, regs;
ed568912 1446 unsigned long flags;
9b32d5f3 1447 int index, retval = -ENOMEM;
ed568912
KH
1448
1449 if (type == FW_ISO_CONTEXT_TRANSMIT) {
1450 mask = &ohci->it_context_mask;
1451 list = ohci->it_context_list;
30200739 1452 callback = handle_it_packet;
ed568912 1453 } else {
373b2edd
SR
1454 mask = &ohci->ir_context_mask;
1455 list = ohci->ir_context_list;
c70dc788 1456 callback = handle_ir_dualbuffer_packet;
ed568912
KH
1457 }
1458
c70dc788 1459 /* FIXME: We need a fallback for pre 1.1 OHCI. */
e364cf4e
KH
1460 if (callback == handle_ir_dualbuffer_packet &&
1461 ohci->version < OHCI_VERSION_1_1)
c74e92c2 1462 return ERR_PTR(-ENOSYS);
e364cf4e 1463
ed568912
KH
1464 spin_lock_irqsave(&ohci->lock, flags);
1465 index = ffs(*mask) - 1;
1466 if (index >= 0)
1467 *mask &= ~(1 << index);
1468 spin_unlock_irqrestore(&ohci->lock, flags);
1469
1470 if (index < 0)
1471 return ERR_PTR(-EBUSY);
1472
373b2edd
SR
1473 if (type == FW_ISO_CONTEXT_TRANSMIT)
1474 regs = OHCI1394_IsoXmitContextBase(index);
1475 else
1476 regs = OHCI1394_IsoRcvContextBase(index);
1477
ed568912 1478 ctx = &list[index];
2d826cc5 1479 memset(ctx, 0, sizeof(*ctx));
9b32d5f3
KH
1480 ctx->header_length = 0;
1481 ctx->header = (void *) __get_free_page(GFP_KERNEL);
1482 if (ctx->header == NULL)
1483 goto out;
1484
30200739 1485 retval = context_init(&ctx->context, ohci, ISO_BUFFER_SIZE,
295e3feb 1486 regs, callback);
9b32d5f3
KH
1487 if (retval < 0)
1488 goto out_with_header;
ed568912
KH
1489
1490 return &ctx->base;
9b32d5f3
KH
1491
1492 out_with_header:
1493 free_page((unsigned long)ctx->header);
1494 out:
1495 spin_lock_irqsave(&ohci->lock, flags);
1496 *mask |= 1 << index;
1497 spin_unlock_irqrestore(&ohci->lock, flags);
1498
1499 return ERR_PTR(retval);
ed568912
KH
1500}
1501
eb0306ea
KH
1502static int ohci_start_iso(struct fw_iso_context *base,
1503 s32 cycle, u32 sync, u32 tags)
ed568912 1504{
373b2edd 1505 struct iso_context *ctx = container_of(base, struct iso_context, base);
30200739 1506 struct fw_ohci *ohci = ctx->context.ohci;
8a2f7d93 1507 u32 control, match;
ed568912
KH
1508 int index;
1509
295e3feb
KH
1510 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1511 index = ctx - ohci->it_context_list;
8a2f7d93
KH
1512 match = 0;
1513 if (cycle >= 0)
1514 match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
295e3feb 1515 (cycle & 0x7fff) << 16;
21efb3cf 1516
295e3feb
KH
1517 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
1518 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
8a2f7d93 1519 context_run(&ctx->context, match);
295e3feb
KH
1520 } else {
1521 index = ctx - ohci->ir_context_list;
8a2f7d93
KH
1522 control = IR_CONTEXT_DUAL_BUFFER_MODE | IR_CONTEXT_ISOCH_HEADER;
1523 match = (tags << 28) | (sync << 8) | ctx->base.channel;
1524 if (cycle >= 0) {
1525 match |= (cycle & 0x07fff) << 12;
1526 control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
1527 }
ed568912 1528
295e3feb
KH
1529 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
1530 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
a77754a7 1531 reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
8a2f7d93 1532 context_run(&ctx->context, control);
295e3feb 1533 }
ed568912
KH
1534
1535 return 0;
1536}
1537
b8295668
KH
1538static int ohci_stop_iso(struct fw_iso_context *base)
1539{
1540 struct fw_ohci *ohci = fw_ohci(base->card);
373b2edd 1541 struct iso_context *ctx = container_of(base, struct iso_context, base);
b8295668
KH
1542 int index;
1543
1544 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1545 index = ctx - ohci->it_context_list;
1546 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
1547 } else {
1548 index = ctx - ohci->ir_context_list;
1549 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
1550 }
1551 flush_writes(ohci);
1552 context_stop(&ctx->context);
1553
1554 return 0;
1555}
1556
ed568912
KH
1557static void ohci_free_iso_context(struct fw_iso_context *base)
1558{
1559 struct fw_ohci *ohci = fw_ohci(base->card);
373b2edd 1560 struct iso_context *ctx = container_of(base, struct iso_context, base);
ed568912
KH
1561 unsigned long flags;
1562 int index;
1563
b8295668
KH
1564 ohci_stop_iso(base);
1565 context_release(&ctx->context);
9b32d5f3 1566 free_page((unsigned long)ctx->header);
b8295668 1567
ed568912
KH
1568 spin_lock_irqsave(&ohci->lock, flags);
1569
1570 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1571 index = ctx - ohci->it_context_list;
ed568912
KH
1572 ohci->it_context_mask |= 1 << index;
1573 } else {
1574 index = ctx - ohci->ir_context_list;
ed568912
KH
1575 ohci->ir_context_mask |= 1 << index;
1576 }
ed568912
KH
1577
1578 spin_unlock_irqrestore(&ohci->lock, flags);
1579}
1580
1581static int
295e3feb
KH
1582ohci_queue_iso_transmit(struct fw_iso_context *base,
1583 struct fw_iso_packet *packet,
1584 struct fw_iso_buffer *buffer,
1585 unsigned long payload)
ed568912 1586{
373b2edd 1587 struct iso_context *ctx = container_of(base, struct iso_context, base);
30200739 1588 struct descriptor *d, *last, *pd;
ed568912
KH
1589 struct fw_iso_packet *p;
1590 __le32 *header;
9aad8125 1591 dma_addr_t d_bus, page_bus;
ed568912
KH
1592 u32 z, header_z, payload_z, irq;
1593 u32 payload_index, payload_end_index, next_page_index;
30200739 1594 int page, end_page, i, length, offset;
ed568912 1595
c781c06d
KH
1596 /*
1597 * FIXME: Cycle lost behavior should be configurable: lose
1598 * packet, retransmit or terminate..
1599 */
ed568912
KH
1600
1601 p = packet;
9aad8125 1602 payload_index = payload;
ed568912
KH
1603
1604 if (p->skip)
1605 z = 1;
1606 else
1607 z = 2;
1608 if (p->header_length > 0)
1609 z++;
1610
1611 /* Determine the first page the payload isn't contained in. */
1612 end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
1613 if (p->payload_length > 0)
1614 payload_z = end_page - (payload_index >> PAGE_SHIFT);
1615 else
1616 payload_z = 0;
1617
1618 z += payload_z;
1619
1620 /* Get header size in number of descriptors. */
2d826cc5 1621 header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
ed568912 1622
30200739
KH
1623 d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
1624 if (d == NULL)
1625 return -ENOMEM;
ed568912
KH
1626
1627 if (!p->skip) {
a77754a7 1628 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
ed568912
KH
1629 d[0].req_count = cpu_to_le16(8);
1630
1631 header = (__le32 *) &d[1];
a77754a7
KH
1632 header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) |
1633 IT_HEADER_TAG(p->tag) |
1634 IT_HEADER_TCODE(TCODE_STREAM_DATA) |
1635 IT_HEADER_CHANNEL(ctx->base.channel) |
1636 IT_HEADER_SPEED(ctx->base.speed));
ed568912 1637 header[1] =
a77754a7 1638 cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length +
ed568912
KH
1639 p->payload_length));
1640 }
1641
1642 if (p->header_length > 0) {
1643 d[2].req_count = cpu_to_le16(p->header_length);
2d826cc5 1644 d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
ed568912
KH
1645 memcpy(&d[z], p->header, p->header_length);
1646 }
1647
1648 pd = d + z - payload_z;
1649 payload_end_index = payload_index + p->payload_length;
1650 for (i = 0; i < payload_z; i++) {
1651 page = payload_index >> PAGE_SHIFT;
1652 offset = payload_index & ~PAGE_MASK;
1653 next_page_index = (page + 1) << PAGE_SHIFT;
1654 length =
1655 min(next_page_index, payload_end_index) - payload_index;
1656 pd[i].req_count = cpu_to_le16(length);
9aad8125
KH
1657
1658 page_bus = page_private(buffer->pages[page]);
1659 pd[i].data_address = cpu_to_le32(page_bus + offset);
ed568912
KH
1660
1661 payload_index += length;
1662 }
1663
ed568912 1664 if (p->interrupt)
a77754a7 1665 irq = DESCRIPTOR_IRQ_ALWAYS;
ed568912 1666 else
a77754a7 1667 irq = DESCRIPTOR_NO_IRQ;
ed568912 1668
30200739 1669 last = z == 2 ? d : d + z - 1;
a77754a7
KH
1670 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
1671 DESCRIPTOR_STATUS |
1672 DESCRIPTOR_BRANCH_ALWAYS |
cbb59da7 1673 irq);
ed568912 1674
30200739 1675 context_append(&ctx->context, d, z, header_z);
ed568912
KH
1676
1677 return 0;
1678}
373b2edd 1679
295e3feb 1680static int
d2746dc1
KH
1681ohci_queue_iso_receive_dualbuffer(struct fw_iso_context *base,
1682 struct fw_iso_packet *packet,
1683 struct fw_iso_buffer *buffer,
1684 unsigned long payload)
295e3feb
KH
1685{
1686 struct iso_context *ctx = container_of(base, struct iso_context, base);
1687 struct db_descriptor *db = NULL;
1688 struct descriptor *d;
1689 struct fw_iso_packet *p;
1690 dma_addr_t d_bus, page_bus;
1691 u32 z, header_z, length, rest;
c70dc788 1692 int page, offset, packet_count, header_size;
373b2edd 1693
c781c06d
KH
1694 /*
1695 * FIXME: Cycle lost behavior should be configurable: lose
1696 * packet, retransmit or terminate..
1697 */
295e3feb 1698
c70dc788
KH
1699 if (packet->skip) {
1700 d = context_get_descriptors(&ctx->context, 2, &d_bus);
1701 if (d == NULL)
1702 return -ENOMEM;
1703
1704 db = (struct db_descriptor *) d;
a77754a7
KH
1705 db->control = cpu_to_le16(DESCRIPTOR_STATUS |
1706 DESCRIPTOR_BRANCH_ALWAYS |
1707 DESCRIPTOR_WAIT);
c70dc788
KH
1708 db->first_size = cpu_to_le16(ctx->base.header_size + 4);
1709 context_append(&ctx->context, d, 2, 0);
1710 }
98b6cbe8 1711
295e3feb
KH
1712 p = packet;
1713 z = 2;
1714
c781c06d
KH
1715 /*
1716 * The OHCI controller puts the status word in the header
1717 * buffer too, so we need 4 extra bytes per packet.
1718 */
c70dc788
KH
1719 packet_count = p->header_length / ctx->base.header_size;
1720 header_size = packet_count * (ctx->base.header_size + 4);
1721
295e3feb 1722 /* Get header size in number of descriptors. */
2d826cc5 1723 header_z = DIV_ROUND_UP(header_size, sizeof(*d));
295e3feb
KH
1724 page = payload >> PAGE_SHIFT;
1725 offset = payload & ~PAGE_MASK;
1726 rest = p->payload_length;
1727
1728 /* FIXME: OHCI 1.0 doesn't support dual buffer receive */
295e3feb
KH
1729 /* FIXME: make packet-per-buffer/dual-buffer a context option */
1730 while (rest > 0) {
1731 d = context_get_descriptors(&ctx->context,
1732 z + header_z, &d_bus);
1733 if (d == NULL)
1734 return -ENOMEM;
1735
1736 db = (struct db_descriptor *) d;
a77754a7
KH
1737 db->control = cpu_to_le16(DESCRIPTOR_STATUS |
1738 DESCRIPTOR_BRANCH_ALWAYS);
c70dc788
KH
1739 db->first_size = cpu_to_le16(ctx->base.header_size + 4);
1740 db->first_req_count = cpu_to_le16(header_size);
1e1d196b 1741 db->first_res_count = db->first_req_count;
2d826cc5 1742 db->first_buffer = cpu_to_le32(d_bus + sizeof(*db));
373b2edd 1743
295e3feb
KH
1744 if (offset + rest < PAGE_SIZE)
1745 length = rest;
1746 else
1747 length = PAGE_SIZE - offset;
1748
1e1d196b
KH
1749 db->second_req_count = cpu_to_le16(length);
1750 db->second_res_count = db->second_req_count;
295e3feb
KH
1751 page_bus = page_private(buffer->pages[page]);
1752 db->second_buffer = cpu_to_le32(page_bus + offset);
1753
cb2d2cdb 1754 if (p->interrupt && length == rest)
a77754a7 1755 db->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
cb2d2cdb 1756
295e3feb
KH
1757 context_append(&ctx->context, d, z, header_z);
1758 offset = (offset + length) & ~PAGE_MASK;
1759 rest -= length;
1760 page++;
1761 }
1762
d2746dc1
KH
1763 return 0;
1764}
21efb3cf 1765
295e3feb
KH
1766static int
1767ohci_queue_iso(struct fw_iso_context *base,
1768 struct fw_iso_packet *packet,
1769 struct fw_iso_buffer *buffer,
1770 unsigned long payload)
1771{
e364cf4e
KH
1772 struct iso_context *ctx = container_of(base, struct iso_context, base);
1773
295e3feb
KH
1774 if (base->type == FW_ISO_CONTEXT_TRANSMIT)
1775 return ohci_queue_iso_transmit(base, packet, buffer, payload);
e364cf4e 1776 else if (ctx->context.ohci->version >= OHCI_VERSION_1_1)
d2746dc1
KH
1777 return ohci_queue_iso_receive_dualbuffer(base, packet,
1778 buffer, payload);
e364cf4e
KH
1779 else
1780 /* FIXME: Implement fallback for OHCI 1.0 controllers. */
c74e92c2 1781 return -ENOSYS;
295e3feb
KH
1782}
1783
21ebcd12 1784static const struct fw_card_driver ohci_driver = {
ed568912
KH
1785 .name = ohci_driver_name,
1786 .enable = ohci_enable,
1787 .update_phy_reg = ohci_update_phy_reg,
1788 .set_config_rom = ohci_set_config_rom,
1789 .send_request = ohci_send_request,
1790 .send_response = ohci_send_response,
730c32f5 1791 .cancel_packet = ohci_cancel_packet,
ed568912 1792 .enable_phys_dma = ohci_enable_phys_dma,
d60d7f1d 1793 .get_bus_time = ohci_get_bus_time,
ed568912
KH
1794
1795 .allocate_iso_context = ohci_allocate_iso_context,
1796 .free_iso_context = ohci_free_iso_context,
1797 .queue_iso = ohci_queue_iso,
69cdb726 1798 .start_iso = ohci_start_iso,
b8295668 1799 .stop_iso = ohci_stop_iso,
ed568912
KH
1800};
1801
ed568912
KH
1802static int __devinit
1803pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
1804{
1805 struct fw_ohci *ohci;
e364cf4e 1806 u32 bus_options, max_receive, link_speed;
ed568912 1807 u64 guid;
d79406dd 1808 int err;
ed568912
KH
1809 size_t size;
1810
2d826cc5 1811 ohci = kzalloc(sizeof(*ohci), GFP_KERNEL);
ed568912
KH
1812 if (ohci == NULL) {
1813 fw_error("Could not malloc fw_ohci data.\n");
1814 return -ENOMEM;
1815 }
1816
1817 fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
1818
d79406dd
KH
1819 err = pci_enable_device(dev);
1820 if (err) {
ed568912 1821 fw_error("Failed to enable OHCI hardware.\n");
d79406dd 1822 goto fail_put_card;
ed568912
KH
1823 }
1824
1825 pci_set_master(dev);
1826 pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
1827 pci_set_drvdata(dev, ohci);
1828
1829 spin_lock_init(&ohci->lock);
1830
1831 tasklet_init(&ohci->bus_reset_tasklet,
1832 bus_reset_tasklet, (unsigned long)ohci);
1833
d79406dd
KH
1834 err = pci_request_region(dev, 0, ohci_driver_name);
1835 if (err) {
ed568912 1836 fw_error("MMIO resource unavailable\n");
d79406dd 1837 goto fail_disable;
ed568912
KH
1838 }
1839
1840 ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
1841 if (ohci->registers == NULL) {
1842 fw_error("Failed to remap registers\n");
d79406dd
KH
1843 err = -ENXIO;
1844 goto fail_iomem;
ed568912
KH
1845 }
1846
ed568912
KH
1847 ar_context_init(&ohci->ar_request_ctx, ohci,
1848 OHCI1394_AsReqRcvContextControlSet);
1849
1850 ar_context_init(&ohci->ar_response_ctx, ohci,
1851 OHCI1394_AsRspRcvContextControlSet);
1852
f319b6a0
KH
1853 context_init(&ohci->at_request_ctx, ohci, AT_BUFFER_SIZE,
1854 OHCI1394_AsReqTrContextControlSet, handle_at_packet);
ed568912 1855
f319b6a0
KH
1856 context_init(&ohci->at_response_ctx, ohci, AT_BUFFER_SIZE,
1857 OHCI1394_AsRspTrContextControlSet, handle_at_packet);
ed568912 1858
ed568912
KH
1859 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
1860 ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
1861 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
1862 size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
1863 ohci->it_context_list = kzalloc(size, GFP_KERNEL);
1864
1865 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
1866 ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
1867 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
1868 size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
1869 ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
1870
1871 if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
1872 fw_error("Out of memory for it/ir contexts.\n");
d79406dd
KH
1873 err = -ENOMEM;
1874 goto fail_registers;
ed568912
KH
1875 }
1876
1877 /* self-id dma buffer allocation */
1878 ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
1879 SELF_ID_BUF_SIZE,
1880 &ohci->self_id_bus,
1881 GFP_KERNEL);
1882 if (ohci->self_id_cpu == NULL) {
1883 fw_error("Out of memory for self ID buffer.\n");
d79406dd
KH
1884 err = -ENOMEM;
1885 goto fail_registers;
ed568912
KH
1886 }
1887
ed568912
KH
1888 bus_options = reg_read(ohci, OHCI1394_BusOptions);
1889 max_receive = (bus_options >> 12) & 0xf;
1890 link_speed = bus_options & 0x7;
1891 guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
1892 reg_read(ohci, OHCI1394_GUIDLo);
1893
d79406dd
KH
1894 err = fw_card_add(&ohci->card, max_receive, link_speed, guid);
1895 if (err < 0)
1896 goto fail_self_id;
ed568912 1897
e364cf4e 1898 ohci->version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
500be725 1899 fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n",
e364cf4e 1900 dev->dev.bus_id, ohci->version >> 16, ohci->version & 0xff);
c74e92c2
SR
1901 if (ohci->version < OHCI_VERSION_1_1) {
1902 fw_notify(" Isochronous I/O is not yet implemented for "
1903 "OHCI 1.0 chips.\n");
1904 fw_notify(" Cameras, audio devices etc. won't work on "
1905 "this controller with this driver version.\n");
1906 }
ed568912 1907 return 0;
d79406dd
KH
1908
1909 fail_self_id:
1910 dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
1911 ohci->self_id_cpu, ohci->self_id_bus);
1912 fail_registers:
1913 kfree(ohci->it_context_list);
1914 kfree(ohci->ir_context_list);
1915 pci_iounmap(dev, ohci->registers);
1916 fail_iomem:
1917 pci_release_region(dev, 0);
1918 fail_disable:
1919 pci_disable_device(dev);
1920 fail_put_card:
1921 fw_card_put(&ohci->card);
1922
1923 return err;
ed568912
KH
1924}
1925
1926static void pci_remove(struct pci_dev *dev)
1927{
1928 struct fw_ohci *ohci;
1929
1930 ohci = pci_get_drvdata(dev);
e254a4b4
KH
1931 reg_write(ohci, OHCI1394_IntMaskClear, ~0);
1932 flush_writes(ohci);
ed568912
KH
1933 fw_core_remove_card(&ohci->card);
1934
c781c06d
KH
1935 /*
1936 * FIXME: Fail all pending packets here, now that the upper
1937 * layers can't queue any more.
1938 */
ed568912
KH
1939
1940 software_reset(ohci);
1941 free_irq(dev->irq, ohci);
d79406dd
KH
1942 dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
1943 ohci->self_id_cpu, ohci->self_id_bus);
1944 kfree(ohci->it_context_list);
1945 kfree(ohci->ir_context_list);
1946 pci_iounmap(dev, ohci->registers);
1947 pci_release_region(dev, 0);
1948 pci_disable_device(dev);
1949 fw_card_put(&ohci->card);
ed568912
KH
1950
1951 fw_notify("Removed fw-ohci device.\n");
1952}
1953
2aef469a
KH
1954#ifdef CONFIG_PM
1955static int pci_suspend(struct pci_dev *pdev, pm_message_t state)
1956{
1957 struct fw_ohci *ohci = pci_get_drvdata(pdev);
1958 int err;
1959
1960 software_reset(ohci);
1961 free_irq(pdev->irq, ohci);
1962 err = pci_save_state(pdev);
1963 if (err) {
8a8cea27 1964 fw_error("pci_save_state failed\n");
2aef469a
KH
1965 return err;
1966 }
1967 err = pci_set_power_state(pdev, pci_choose_state(pdev, state));
55111428
SR
1968 if (err)
1969 fw_error("pci_set_power_state failed with %d\n", err);
2aef469a
KH
1970
1971 return 0;
1972}
1973
1974static int pci_resume(struct pci_dev *pdev)
1975{
1976 struct fw_ohci *ohci = pci_get_drvdata(pdev);
1977 int err;
1978
1979 pci_set_power_state(pdev, PCI_D0);
1980 pci_restore_state(pdev);
1981 err = pci_enable_device(pdev);
1982 if (err) {
8a8cea27 1983 fw_error("pci_enable_device failed\n");
2aef469a
KH
1984 return err;
1985 }
1986
1987 return ohci_enable(&ohci->card, ohci->config_rom, CONFIG_ROM_SIZE);
1988}
1989#endif
1990
ed568912
KH
1991static struct pci_device_id pci_table[] = {
1992 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
1993 { }
1994};
1995
1996MODULE_DEVICE_TABLE(pci, pci_table);
1997
1998static struct pci_driver fw_ohci_pci_driver = {
1999 .name = ohci_driver_name,
2000 .id_table = pci_table,
2001 .probe = pci_probe,
2002 .remove = pci_remove,
2aef469a
KH
2003#ifdef CONFIG_PM
2004 .resume = pci_resume,
2005 .suspend = pci_suspend,
2006#endif
ed568912
KH
2007};
2008
2009MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
2010MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
2011MODULE_LICENSE("GPL");
2012
1e4c7b0d
OH
2013/* Provide a module alias so root-on-sbp2 initrds don't break. */
2014#ifndef CONFIG_IEEE1394_OHCI1394_MODULE
2015MODULE_ALIAS("ohci1394");
2016#endif
2017
ed568912
KH
2018static int __init fw_ohci_init(void)
2019{
2020 return pci_register_driver(&fw_ohci_pci_driver);
2021}
2022
2023static void __exit fw_ohci_cleanup(void)
2024{
2025 pci_unregister_driver(&fw_ohci_pci_driver);
2026}
2027
2028module_init(fw_ohci_init);
2029module_exit(fw_ohci_cleanup);
This page took 0.294786 seconds and 5 git commands to generate.