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