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