#define AR_BUFFER_SIZE (32*1024) #define AR_BUFFERS_MIN DIV_ROUND_UP(AR_BUFFER_SIZE, PAGE_SIZE) /* we need at least two pages for proper list management */ #define AR_BUFFERS (AR_BUFFERS_MIN >= 2 ? AR_BUFFERS_MIN : 2)
/* * A buffer that contains a block of DMA-able coherent memory used for * storing a portion of a DMA descriptor program.
*/ struct descriptor_buffer { struct list_head list;
dma_addr_t buffer_bus;
size_t buffer_size;
size_t used; struct descriptor buffer[];
};
/* * List of page-sized buffers for storing DMA descriptors. * Head of list contains buffers in use and tail of list contains * free buffers.
*/ struct list_head buffer_list;
/* * Pointer to a buffer inside buffer_list that contains the tail * end of the current DMA program.
*/ struct descriptor_buffer *buffer_tail;
/* * The descriptor containing the branch address of the first * descriptor that has not yet been filled by the device.
*/ struct descriptor *last;
/* * The last descriptor block in the DMA program. It contains the branch * address that must be updated upon appending a new descriptor.
*/ struct descriptor *prev; int prev_z;
__iomem char *registers; int node_id; int generation; int request_generation; /* for timestamping incoming requests */ unsigned quirks; unsignedint pri_req_max;
u32 bus_time; bool bus_time_running; bool is_root; bool csr_state_setclear_abdicate; int n_ir; int n_it; /* * Spinlock for accessing fw_ohci data. Never call out of * this driver with this lock held.
*/
spinlock_t lock;
// On PCI Express Root Complex in any type of AMD Ryzen machine, VIA VT6306/6307/6308 with Asmedia // ASM1083/1085 brings an inconvenience that the read accesses to 'Isochronous Cycle Timer' register // (at offset 0xf0 in PCI I/O space) often causes unexpected system reboot. The mechanism is not // clear, since the read access to the other registers is enough safe; e.g. 'Node ID' register, // while it is probable due to detection of any type of PCIe error. #define QUIRK_REBOOT_BY_CYCLE_TIMER_READ 0x80000000
/* In case of multiple matches in ohci_quirks[], only the first one is used. */ staticconststruct { unsignedshort vendor, device, revision, flags;
} ohci_quirks[] = {
{PCI_VENDOR_ID_AL, PCI_ANY_ID, PCI_ANY_ID,
QUIRK_CYCLE_TIMER},
switch (tcode) { case TCODE_WRITE_QUADLET_REQUEST: case TCODE_READ_QUADLET_RESPONSE: case TCODE_CYCLE_START:
snprintf(specific, sizeof(specific), " = %08x",
be32_to_cpu((__force __be32)header[3])); break; case TCODE_WRITE_BLOCK_REQUEST: case TCODE_READ_BLOCK_REQUEST: case TCODE_READ_BLOCK_RESPONSE: case TCODE_LOCK_REQUEST: case TCODE_LOCK_RESPONSE:
snprintf(specific, sizeof(specific), " %x,%x",
async_header_get_data_length(header),
async_header_get_extended_tcode(header)); break; default:
specific[0] = '\0';
}
switch (tcode) { case TCODE_STREAM_DATA:
ohci_notice(ohci, "A%c %s, %s\n",
dir, evts[evt], tcodes[tcode]); break; case TCODE_LINK_INTERNAL:
ohci_notice(ohci, "A%c %s, PHY %08x %08x\n",
dir, evts[evt], header[1], header[2]); break; case TCODE_WRITE_QUADLET_REQUEST: case TCODE_WRITE_BLOCK_REQUEST: case TCODE_READ_QUADLET_REQUEST: case TCODE_READ_BLOCK_REQUEST: case TCODE_LOCK_REQUEST:
ohci_notice(ohci, "A%c spd %x tl %02x, %04x -> %04x, %s, %s, %012llx%s\n",
dir, speed, async_header_get_tlabel(header),
async_header_get_source(header), async_header_get_destination(header),
evts[evt], tcodes[tcode], async_header_get_offset(header), specific); break; default:
ohci_notice(ohci, "A%c spd %x tl %02x, %04x -> %04x, %s, %s%s\n",
dir, speed, async_header_get_tlabel(header),
async_header_get_source(header), async_header_get_destination(header),
evts[evt], tcodes[tcode], specific);
}
}
staticinlinevoid flush_writes(conststruct fw_ohci *ohci)
{ /* Do a dummy read to flush writes. */
reg_read(ohci, OHCI1394_Version);
}
/* * Beware! read_phy_reg(), write_phy_reg(), update_phy_reg(), and * read_paged_phy_reg() require the caller to hold ohci->phy_reg_mutex. * In other words, only use ohci_read_phy_reg() and ohci_update_phy_reg() * directly. Exceptions are intrinsically serialized contexts like pci_probe.
*/ staticint read_phy_reg(struct fw_ohci *ohci, int addr)
{
u32 val; int i;
reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr)); for (i = 0; i < 3 + 100; i++) {
val = reg_read(ohci, OHCI1394_PhyControl); if (!~val) return -ENODEV; /* Card was ejected. */
if (val & OHCI1394_PhyControl_ReadDone) return OHCI1394_PhyControl_ReadData(val);
/* * Try a few times without waiting. Sleeping is necessary * only when the link/PHY interface is busy.
*/ if (i >= 3)
msleep(1);
}
ohci_err(ohci, "failed to read phy reg %d\n", addr);
dump_stack();
return -EBUSY;
}
staticint write_phy_reg(conststruct fw_ohci *ohci, int addr, u32 val)
{ int i;
reg_write(ohci, OHCI1394_PhyControl,
OHCI1394_PhyControl_Write(addr, val)); for (i = 0; i < 3 + 100; i++) {
val = reg_read(ohci, OHCI1394_PhyControl); if (!~val) return -ENODEV; /* Card was ejected. */
if (!(val & OHCI1394_PhyControl_WritePending)) return 0;
if (i >= 3)
msleep(1);
}
ohci_err(ohci, "failed to write phy reg %d, val %u\n", addr, val);
dump_stack();
return -EBUSY;
}
staticint update_phy_reg(struct fw_ohci *ohci, int addr, int clear_bits, int set_bits)
{ int ret = read_phy_reg(ohci, addr); if (ret < 0) return ret;
/* * The interrupt status bits are cleared by writing a one bit. * Avoid clearing them unless explicitly requested in set_bits.
*/ if (addr == 5)
clear_bits |= PHY_INT_STATUS_BITS;
wmb(); /* finish init of new descriptors before branch_address update */
d = &ctx->descriptors[ctx->last_buffer_index];
d->branch_address |= cpu_to_le32(1);
/* * We search for the buffer that contains the last AR packet DMA data written * by the controller.
*/ staticunsignedint ar_search_last_active_buffer(struct ar_context *ctx, unsignedint *buffer_offset)
{ unsignedint i, next_i, last = ctx->last_buffer_index;
__le16 res_count, next_res_count;
i = ar_first_buffer_index(ctx);
res_count = READ_ONCE(ctx->descriptors[i].res_count);
/* A buffer that is not yet completely filled must be the last one. */ while (i != last && res_count == 0) {
/* Peek at the next descriptor. */
next_i = ar_next_buffer_index(i);
rmb(); /* read descriptors in order */
next_res_count = READ_ONCE(ctx->descriptors[next_i].res_count); /* * If the next descriptor is still empty, we must stop at this * descriptor.
*/ if (next_res_count == cpu_to_le16(PAGE_SIZE)) { /* * The exception is when the DMA data for one packet is * split over three buffers; in this case, the middle * buffer's descriptor might be never updated by the * controller and look still empty, and we have to peek * at the third one.
*/ if (MAX_AR_PACKET_SIZE > PAGE_SIZE && i != last) {
next_i = ar_next_buffer_index(next_i);
rmb();
next_res_count = READ_ONCE(ctx->descriptors[next_i].res_count); if (next_res_count != cpu_to_le16(PAGE_SIZE)) goto next_buffer_is_active;
}
break;
}
next_buffer_is_active:
i = next_i;
res_count = next_res_count;
}
/* * Several controllers, notably from NEC and VIA, forget to * write ack_complete status at PHY packet reception.
*/ if (evt == OHCI1394_evt_no_status && tcode == TCODE_LINK_INTERNAL)
p.ack = ACK_COMPLETE;
/* * The OHCI bus reset handler synthesizes a PHY packet with * the new generation number when a bus reset happens (see * section 8.4.2.3). This helps us determine when a request * was received and make sure we send the response in the same * generation. We only need this for requests; for responses * we use the unique tlabel for finding the matching * request. * * Alas some chips sometimes emit bus reset packets with a * wrong generation. We set the correct generation for these * at a slightly incorrect time (in bus_reset_work).
*/ if (evt == OHCI1394_evt_bus_reset) { if (!(ohci->quirks & QUIRK_RESET_PACKET))
ohci->request_generation = (p.header[2] >> 16) & 0xff;
} elseif (ctx == &ohci->ar_request_ctx) {
fw_core_handle_request(&ohci->card, &p);
} else {
fw_core_handle_response(&ohci->card, &p);
}
i = ar_first_buffer_index(ctx); while (i != end_buffer) {
dma_sync_single_for_device(ctx->ohci->card.device,
ar_buffer_bus(ctx, i),
PAGE_SIZE, DMA_FROM_DEVICE);
ar_context_link_page(ctx, i);
i = ar_next_buffer_index(i);
}
}
if (end_buffer_index < ar_first_buffer_index(ctx)) { // The filled part of the overall buffer wraps around; handle all packets up to the // buffer end here. If the last packet wraps around, its tail will be visible after // the buffer end because the buffer start pages are mapped there again. void *buffer_end = ctx->buffer + AR_BUFFERS * PAGE_SIZE;
p = handle_ar_packets(ctx, p, buffer_end); if (p < buffer_end) goto error; // adjust p to point back into the actual buffer
p -= AR_BUFFERS * PAGE_SIZE;
}
p = handle_ar_packets(ctx, p, end); if (p != end) { if (p > end)
ar_context_abort(ctx, "inconsistent descriptor"); goto error;
}
for (i = 0; i < AR_BUFFERS; i++) {
ctx->pages[i] = dma_alloc_pages(dev, PAGE_SIZE, &dma_addr,
DMA_FROM_DEVICE, GFP_KERNEL); if (!ctx->pages[i]) goto out_of_memory;
set_page_private(ctx->pages[i], dma_addr);
dma_sync_single_for_device(dev, dma_addr, PAGE_SIZE,
DMA_FROM_DEVICE);
}
for (i = 0; i < AR_BUFFERS; i++)
pages[i] = ctx->pages[i]; for (i = 0; i < AR_WRAPAROUND_PAGES; i++)
pages[AR_BUFFERS + i] = ctx->pages[i];
ctx->buffer = vmap(pages, ARRAY_SIZE(pages), VM_MAP, PAGE_KERNEL); if (!ctx->buffer) goto out_of_memory;
/* figure out which descriptor the branch address goes in */ if (z == 2 && branch == cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS)) return d; else return d + z - 1;
}
/* If the branch address points to a buffer outside of the
* current buffer, advance to the next buffer. */ if (address < desc->buffer_bus ||
address >= desc->buffer_bus + desc->used)
desc = list_entry(desc->list.next, struct descriptor_buffer, list);
d = desc->buffer + (address - desc->buffer_bus) / sizeof(*d);
last = find_branch_descriptor(d, z);
if (!ctx->callback(ctx, d, last)) break;
if (old_desc != desc) { // If we've advanced to the next buffer, move the previous buffer to the // free list.
old_desc->used = 0;
guard(spinlock_irqsave)(&ctx->ohci->lock);
list_move_tail(&old_desc->list, &ctx->buffer_list);
}
ctx->last = last;
}
}
/* * Allocate a new buffer and add it to the list of free buffers for this * context. Must be called with ohci->lock held.
*/ staticint context_add_buffer(struct context *ctx)
{ struct descriptor_buffer *desc;
dma_addr_t bus_addr; int offset;
/* * 16MB of descriptors should be far more than enough for any DMA * program. This will catch run-away userspace or DoS attacks.
*/ if (ctx->total_allocation >= 16*1024*1024) return -ENOMEM;
desc = dmam_alloc_coherent(ctx->ohci->card.device, PAGE_SIZE, &bus_addr, GFP_ATOMIC); if (!desc) return -ENOMEM;
offset = (void *)&desc->buffer - (void *)desc; /* * Some controllers, like JMicron ones, always issue 0x20-byte DMA reads * for descriptors, even 0x10-byte ones. This can cause page faults when * an IOMMU is in use and the oversized read crosses a page boundary. * Work around this by always leaving at least 0x10 bytes of padding.
*/
desc->buffer_size = PAGE_SIZE - offset - 0x10;
desc->buffer_bus = bus_addr + offset;
desc->used = 0;
/* * We put a dummy descriptor in the buffer that has a NULL * branch address and looks like it's been sent. That way we * have a descriptor to append DMA programs to.
*/
memset(ctx->buffer_tail->buffer, 0, sizeof(*ctx->buffer_tail->buffer));
ctx->buffer_tail->buffer->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
ctx->buffer_tail->buffer->transfer_status = cpu_to_le16(0x8011);
ctx->buffer_tail->used += sizeof(*ctx->buffer_tail->buffer);
ctx->last = ctx->buffer_tail->buffer;
ctx->prev = ctx->buffer_tail->buffer;
ctx->prev_z = 1;
/* Must be called with ohci->lock held */ staticstruct descriptor *context_get_descriptors(struct context *ctx, int z, dma_addr_t *d_bus)
{ struct descriptor *d = NULL; struct descriptor_buffer *desc = ctx->buffer_tail;
if (z * sizeof(*d) > desc->buffer_size) return NULL;
if (z * sizeof(*d) > desc->buffer_size - desc->used) { /* No room for the descriptor in this buffer, so advance to the
* next one. */
if (desc->list.next == &ctx->buffer_list) { /* If there is no free buffer next in the list,
* allocate one. */ if (context_add_buffer(ctx) < 0) return NULL;
}
desc = list_entry(desc->list.next, struct descriptor_buffer, list);
ctx->buffer_tail = desc;
}
d = desc->buffer + desc->used / sizeof(*d);
memset(d, 0, z * sizeof(*d));
*d_bus = desc->buffer_bus + desc->used;
/* * VT6306 incorrectly checks only the single descriptor at the * CommandPtr when the wake bit is written, so if it's a * multi-descriptor block starting with an INPUT_MORE, put a copy of * the branch address in the first descriptor. * * Not doing this for transmit contexts since not sure how it interacts * with skip addresses.
*/ if (unlikely(ctx->ohci->quirks & QUIRK_IR_WAKE) &&
d_branch != ctx->prev &&
(ctx->prev->control & cpu_to_le16(DESCRIPTOR_CMD)) ==
cpu_to_le16(DESCRIPTOR_INPUT_MORE)) {
ctx->prev->branch_address = cpu_to_le32(d_bus | z);
}
/* * This function appends a packet to the DMA queue for transmission. * Must always be called with the ochi->lock held to ensure proper * generation handling and locking around packet queue manipulation.
*/ staticint at_context_queue_packet(struct at_context *ctx, struct fw_packet *packet)
{ struct context *context = &ctx->context; struct fw_ohci *ohci = context->ohci;
dma_addr_t d_bus, payload_bus; struct driver_data *driver_data; struct descriptor *d, *last;
__le32 *header; int z, tcode;
d = context_get_descriptors(context, 4, &d_bus); if (d == NULL) {
packet->ack = RCODE_SEND_ERROR; return -1;
}
tcode = async_header_get_tcode(packet->header);
header = (__le32 *) &d[1]; switch (tcode) { case TCODE_WRITE_QUADLET_REQUEST: case TCODE_WRITE_BLOCK_REQUEST: case TCODE_WRITE_RESPONSE: case TCODE_READ_QUADLET_REQUEST: case TCODE_READ_BLOCK_REQUEST: case TCODE_READ_QUADLET_RESPONSE: case TCODE_READ_BLOCK_RESPONSE: case TCODE_LOCK_REQUEST: case TCODE_LOCK_RESPONSE:
ohci1394_at_data_set_src_bus_id(header, false);
ohci1394_at_data_set_speed(header, packet->speed);
ohci1394_at_data_set_tlabel(header, async_header_get_tlabel(packet->header));
ohci1394_at_data_set_retry(header, async_header_get_retry(packet->header));
ohci1394_at_data_set_tcode(header, tcode);
/* FIXME: Document how the locking works. */ if (ohci->generation != packet->generation) { if (packet->payload_mapped)
dma_unmap_single(ohci->card.device, payload_bus,
packet->payload_length, DMA_TO_DEVICE);
packet->ack = RCODE_GENERATION; return -1;
}
context_append(context, d, z, 4 - z);
if (context->running)
reg_write(ohci, CONTROL_SET(context->regs), CONTEXT_WAKE); else
context_run(context, 0);
return 0;
}
staticvoid at_context_flush(struct at_context *ctx)
{ // Avoid dead lock due to programming mistake. if (WARN_ON_ONCE(current_work() == &ctx->work)) return;
case OHCI1394_evt_flushed: /* * The packet was flushed should give same error as * when we try to use a stale generation count.
*/
packet->ack = RCODE_GENERATION; break;
case OHCI1394_evt_missing_ack: if (READ_ONCE(ctx->flushing))
packet->ack = RCODE_GENERATION; else { /* * Using a valid (current) generation count, but the * node is not on the bus or not sending acks.
*/
packet->ack = RCODE_NO_ACK;
} break;
case ACK_COMPLETE + 0x10: case ACK_PENDING + 0x10: case ACK_BUSY_X + 0x10: case ACK_BUSY_A + 0x10: case ACK_BUSY_B + 0x10: case ACK_DATA_ERROR + 0x10: case ACK_TYPE_ERROR + 0x10:
packet->ack = evt - 0x10; break;
case OHCI1394_evt_no_status: if (READ_ONCE(ctx->flushing)) {
packet->ack = RCODE_GENERATION; break;
}
fallthrough;
i = csr - CSR_CONFIG_ROM; if (i + length > CONFIG_ROM_SIZE) {
fw_fill_response(&response, packet->header,
RCODE_ADDRESS_ERROR, NULL, 0);
} elseif (!tcode_is_read_request(tcode)) {
fw_fill_response(&response, packet->header,
RCODE_TYPE_ERROR, NULL, 0);
} else {
fw_fill_response(&response, packet->header, RCODE_COMPLETE,
(void *) ohci->config_rom + i, length);
}
// Timestamping on behalf of the hardware.
response.timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ohci));
fw_core_handle_response(&ohci->card, &response);
}
out: // Timestamping on behalf of the hardware.
response.timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ohci));
fw_core_handle_response(&ohci->card, &response);
}
/* * Some controllers exhibit one or more of the following bugs when updating the * iso cycle timer register: * - When the lowest six bits are wrapping around to zero, a read that happens * at the same time will return garbage in the lowest ten bits. * - When the cycleOffset field wraps around to zero, the cycleCount field is * not incremented for about 60 ns. * - Occasionally, the entire register reads zero. * * To catch these, we read the register three times and ensure that the * difference between each two consecutive reads is approximately the same, i.e. * less than twice the other. Furthermore, any negative difference indicates an * error. (A PCI read should take at least 20 ticks of the 24.576 MHz timer to * execute, so we have enough precision to compute the ratio of the differences.)
*/ static u32 get_cycle_time(struct fw_ohci *ohci)
{
u32 c0, c1, c2;
u32 t0, t1, t2;
s32 diff01, diff12; int i;
if (has_reboot_by_cycle_timer_read_quirk(ohci)) return 0;
/* * This function has to be called at least every 64 seconds. The bus_time * field stores not only the upper 25 bits of the BUS_TIME register but also * the most significant bit of the cycle timer in bit 6 so that we can detect * changes in this bit.
*/ static u32 update_bus_time(struct fw_ohci *ohci)
{
u32 cycle_time_seconds = get_cycle_time(ohci) >> 25;
/* * TI TSB82AA2B and TSB12LV26 do not receive the selfID of a locally * attached TSB41BA3D phy; see http://www.ti.com/litv/pdf/sllz059. * Construct the selfID from phy register contents.
*/ staticint find_and_insert_self_id(struct fw_ohci *ohci, int self_id_count)
{ int reg, i, pos, err; bool is_initiated_reset;
u32 self_id = 0;
// link active 1, speed 3, bridge 0, contender 1, more packets 0.
phy_packet_set_packet_identifier(&self_id, PHY_PACKET_PACKET_IDENTIFIER_SELF_ID);
phy_packet_self_id_zero_set_link_active(&self_id, true);
phy_packet_self_id_zero_set_scode(&self_id, SCODE_800);
phy_packet_self_id_zero_set_contender(&self_id, true);
reg = reg_read(ohci, OHCI1394_NodeID); if (!(reg & OHCI1394_NodeID_idValid)) {
ohci_notice(ohci, "node ID not valid, new bus reset in progress\n"); return -EBUSY;
}
phy_packet_self_id_set_phy_id(&self_id, reg & 0x3f);
/* * The count in the SelfIDCount register is the number of * bytes in the self ID receive buffer. Since we also receive * the inverted quadlets and a header quadlet, we shift one * bit extra to get the actual number of self IDs.
*/
self_id_count = ohci1394_self_id_count_get_size(reg) >> 1;
for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
u32 id = cond_le32_to_cpu(ohci->self_id[i], has_be_header_quirk(ohci));
u32 id2 = cond_le32_to_cpu(ohci->self_id[i + 1], has_be_header_quirk(ohci));
if (id != ~id2) { /* * If the invalid data looks like a cycle start packet, * it's likely to be the result of the cycle master * having a wrong gap count. In this case, the self IDs * so far are valid and should be processed so that the * bus manager can then correct the gap count.
*/ if (id == 0xffff008f) {
ohci_notice(ohci, "ignoring spurious self IDs\n");
self_id_count = j; break;
}
if (ohci->quirks & QUIRK_TI_SLLZ059) {
self_id_count = find_and_insert_self_id(ohci, self_id_count); if (self_id_count < 0) {
ohci_notice(ohci, "could not construct local self ID\n"); return;
}
}
if (self_id_count == 0) {
ohci_notice(ohci, "no self IDs\n"); return;
}
rmb();
/* * Check the consistency of the self IDs we just read. The * problem we face is that a new bus reset can start while we * read out the self IDs from the DMA buffer. If this happens, * the DMA buffer will be overwritten with new self IDs and we * will read out inconsistent data. The OHCI specification * (section 11.2) recommends a technique similar to * linux/seqlock.h, where we remember the generation of the * self IDs in the buffer before reading them out and compare * it to the current generation after reading them out. If * the two generations match we know we have a consistent set * of self IDs.
*/
reg = reg_read(ohci, OHCI1394_SelfIDCount);
new_generation = ohci1394_self_id_count_get_generation(reg); if (new_generation != generation) {
ohci_notice(ohci, "new bus reset, discarding self ids\n"); return;
}
// FIXME: Document how the locking works.
scoped_guard(spinlock_irq, &ohci->lock) {
ohci->generation = -1; // prevent AT packet queueing
context_stop(&ohci->at_request_ctx.context);
context_stop(&ohci->at_response_ctx.context);
}
/* * Per OHCI 1.2 draft, clause 7.2.3.3, hardware may leave unsent * packets in the AT queues and software needs to drain them. * Some OHCI 1.1 controllers (JMicron) apparently require this too.
*/
at_context_flush(&ohci->at_request_ctx);
at_context_flush(&ohci->at_response_ctx);
if (ohci->quirks & QUIRK_RESET_PACKET)
ohci->request_generation = generation;
// This next bit is unrelated to the AT context stuff but we have to do it under the // spinlock also. If a new config rom was set up before this reset, the old one is // now no longer in use and we can free it. Update the config rom pointers to point // to the current config rom and clear the next_config_rom pointer so a new update // can take place. if (ohci->next_config_rom != NULL) { if (ohci->next_config_rom != ohci->config_rom) {
free_rom = ohci->config_rom;
free_rom_bus = ohci->config_rom_bus;
}
ohci->config_rom = ohci->next_config_rom;
ohci->config_rom_bus = ohci->next_config_rom_bus;
ohci->next_config_rom = NULL;
// Restore config_rom image and manually update config_rom registers. // Writing the header quadlet will indicate that the config rom is ready, // so we do that last.
reg_write(ohci, OHCI1394_BusOptions, be32_to_cpu(ohci->config_rom[2]));
ohci->config_rom[0] = ohci->next_header;
reg_write(ohci, OHCI1394_ConfigROMhdr, be32_to_cpu(ohci->next_header));
}
if (unlikely(param_debug > 0)) {
dev_notice_ratelimited(ohci->card.device, "The debug parameter is superseded by tracepoints events, and deprecated.");
}
/* * busReset and postedWriteErr events must not be cleared yet * (OHCI 1.1 clauses 7.2.3.2 and 13.2.8.1)
*/
reg_write(ohci, OHCI1394_IntEventClear,
event & ~(OHCI1394_busReset | OHCI1394_postedWriteErr));
trace_irqs(ohci->card.index, event);
log_irqs(ohci, event); // The flag is masked again at bus_reset_work() scheduled by selfID event. if (event & OHCI1394_busReset)
reg_write(ohci, OHCI1394_IntMaskClear, OHCI1394_busReset);
if (event & OHCI1394_selfIDComplete)
queue_work(selfid_workqueue, &ohci->bus_reset_work);
if (event & OHCI1394_RQPkt)
queue_work(ohci->card.async_wq, &ohci->ar_request_ctx.work);
if (event & OHCI1394_RSPkt)
queue_work(ohci->card.async_wq, &ohci->ar_response_ctx.work);
if (event & OHCI1394_reqTxComplete)
queue_work(ohci->card.async_wq, &ohci->at_request_ctx.work);
if (event & OHCI1394_respTxComplete)
queue_work(ohci->card.async_wq, &ohci->at_response_ctx.work);
if (unlikely(event & OHCI1394_cycleTooLong)) {
dev_notice_ratelimited(ohci->card.device, "isochronous cycle too long\n");
reg_write(ohci, OHCI1394_LinkControlSet,
OHCI1394_LinkControl_cycleMaster);
}
if (unlikely(event & OHCI1394_cycleInconsistent)) { /* * We need to clear this event bit in order to make * cycleMatch isochronous I/O work. In theory we should * stop active cycleMatch iso contexts now and restart * them at least two cycles later. (FIXME?)
*/
dev_notice_ratelimited(ohci->card.device, "isochronous cycle inconsistent\n");
}
if (unlikely(event & OHCI1394_unrecoverableError))
handle_dead_contexts(ohci);
if (event & OHCI1394_cycle64Seconds) {
guard(spinlock)(&ohci->lock);
update_bus_time(ohci);
} else
flush_writes(ohci);
return IRQ_HANDLED;
}
staticint software_reset(struct fw_ohci *ohci)
{
u32 val; int i;
reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset); for (i = 0; i < 500; i++) {
val = reg_read(ohci, OHCI1394_HCControlSet); if (!~val) return -ENODEV; /* Card was ejected. */
if (!(val & OHCI1394_HCControl_softReset)) return 0;
ret = software_reset(ohci); if (ret < 0) {
ohci_err(ohci, "failed to reset ohci card\n"); return ret;
}
/* * Now enable LPS, which we need in order to start accessing * most of the registers. In fact, on some cards (ALI M5251), * accessing registers in the SClk domain without LPS enabled * will lock up the machine. Wait 50msec to make sure we have * full link enabled. However, with some cards (well, at least * a JMicron PCIe card), we have to try again sometimes. * * TI TSB82AA2 + TSB81BA3(A) cards signal LPS enabled early but * cannot actually use the phy at that time. These need tens of * millisecods pause between LPS write and first phy access too.
*/
for (i = 0; i < 32; i++) if (ohci->ir_context_support & (1 << i))
reg_write(ohci, OHCI1394_IsoRcvContextControlClear(i),
IR_CONTEXT_MULTI_CHANNEL_MODE);
version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff; if (version >= OHCI_VERSION_1_1) {
reg_write(ohci, OHCI1394_InitialChannelsAvailableHi,
0xfffffffe);
card->broadcast_channel_auto_allocated = true;
}
/* Get implemented bits of the priority arbitration request counter. */
reg_write(ohci, OHCI1394_FairnessControl, 0x3f);
ohci->pri_req_max = reg_read(ohci, OHCI1394_FairnessControl) & 0x3f;
reg_write(ohci, OHCI1394_FairnessControl, 0);
card->priority_budget_implemented = ohci->pri_req_max != 0;
ret = configure_1394a_enhancements(ohci); if (ret < 0) return ret;
/* Activate link_on bit and contender bit in our self ID packets.*/
ret = ohci_update_phy_reg(card, 4, 0, PHY_LINK_ACTIVE | PHY_CONTENDER); if (ret < 0) return ret;
/* * When the link is not yet enabled, the atomic config rom * update mechanism described below in ohci_set_config_rom() * is not active. We have to update ConfigRomHeader and * BusOptions manually, and the write to ConfigROMmap takes * effect immediately. We tie this to the enabling of the * link, so we have a valid config rom before enabling - the * OHCI requires that ConfigROMhdr and BusOptions have valid * values before enabling. * * However, when the ConfigROMmap is written, some controllers * always read back quadlets 0 and 2 from the config rom to * the ConfigRomHeader and BusOptions registers on bus reset. * They shouldn't do that in this initial case where the link * isn't enabled. This means we have to use the same * workaround here, setting the bus header to 0 and then write * the right values in the bus reset work item.
*/
if (config_rom) {
ohci->next_config_rom = dmam_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
&ohci->next_config_rom_bus, GFP_KERNEL); if (ohci->next_config_rom == NULL) return -ENOMEM;
copy_config_rom(ohci->next_config_rom, config_rom, length);
} else { /* * In the suspend case, config_rom is NULL, which * means that we just reuse the old config rom.
*/
ohci->next_config_rom = ohci->config_rom;
ohci->next_config_rom_bus = ohci->config_rom_bus;
}
/* * When the OHCI controller is enabled, the config rom update * mechanism is a bit tricky, but easy enough to use. See * section 5.5.6 in the OHCI specification. * * The OHCI controller caches the new config rom address in a * shadow register (ConfigROMmapNext) and needs a bus reset * for the changes to take place. When the bus reset is * detected, the controller loads the new values for the * ConfigRomHeader and BusOptions registers from the specified * config rom and loads ConfigROMmap from the ConfigROMmapNext * shadow register. All automatically and atomically. * * Now, there's a twist to this story. The automatic load of * ConfigRomHeader and BusOptions doesn't honor the * noByteSwapData bit, so with a be32 config rom, the * controller will load be32 values in to these registers * during the atomic update, even on little endian * architectures. The workaround we use is to put a 0 in the * header quadlet; 0 is endian agnostic and means that the * config rom isn't ready yet. In the bus reset work item we * then set up the real values for the two registers. * * We use ohci->lock to avoid racing with the code that sets * ohci->next_config_rom to NULL (see bus_reset_work).
*/
scoped_guard(spinlock_irq, &ohci->lock) { // If there is not an already pending config_rom update, push our new allocation // into the ohci->next_config_rom and then mark the local variable as null so that // we won't deallocate the new buffer. // // OTOH, if there is a pending config_rom update, just use that buffer with the new // config_rom data, and let this routine free the unused DMA allocation. if (ohci->next_config_rom == NULL) {
ohci->next_config_rom = next_config_rom;
ohci->next_config_rom_bus = next_config_rom_bus;
next_config_rom = NULL;
}
/* If we didn't use the DMA allocation, delete it. */ if (next_config_rom != NULL) {
dmam_free_coherent(ohci->card.device, CONFIG_ROM_SIZE, next_config_rom,
next_config_rom_bus);
}
/* * Now initiate a bus reset to have the changes take * effect. We clean up the old config rom memory and DMA * mappings in the bus reset work item, since the OHCI * controller could need to access it before the bus reset * takes effect.
*/
switch (csr_offset) { case CSR_STATE_CLEAR: case CSR_STATE_SET: if (ohci->is_root &&
(reg_read(ohci, OHCI1394_LinkControlSet) &
OHCI1394_LinkControl_cycleMaster))
value = CSR_STATE_BIT_CMSTR; else
value = 0; if (ohci->csr_state_setclear_abdicate)
value |= CSR_STATE_BIT_ABDICATE;
return value;
case CSR_NODE_IDS: return reg_read(ohci, OHCI1394_NodeID) << 16;
case CSR_CYCLE_TIME: return get_cycle_time(ohci);
case CSR_BUS_TIME:
{ // We might be called just after the cycle timer has wrapped around but just before // the cycle64Seconds handler, so we better check here, too, if the bus time needs // to be updated.
guard(spinlock_irqsave)(&ohci->lock); return update_bus_time(ohci);
} case CSR_BUSY_TIMEOUT:
value = reg_read(ohci, OHCI1394_ATRetries); return (value >> 4) & 0x0ffff00f;
/* * The two iso header quadlets are byteswapped to little * endian by the controller, but we want to present them * as big endian for consistency with the bus endianness.
*/ if (ctx->base.header_size > 0)
ctx_hdr[0] = swab32(dma_hdr[1]); /* iso packet header */ if (ctx->base.header_size > 4)
ctx_hdr[1] = swab32(dma_hdr[0]); /* timestamp */ if (ctx->base.header_size > 8)
memcpy(&ctx_hdr[2], &dma_hdr[2], ctx->base.header_size - 8);
ctx->header_length += ctx->base.header_size;
}
if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS))
flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_INTERRUPT);
return 1;
}
/* d == last because each descriptor block is only a single descriptor. */ staticint handle_ir_buffer_fill(struct context *context, struct descriptor *d, struct descriptor *last)
{ struct iso_context *ctx =
container_of(context, struct iso_context, context); unsignedint req_count, res_count, completed;
u32 buffer_dma;
/* only packets beginning with OUTPUT_MORE* have data buffers */ if (pd->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS)) return;
/* skip over the OUTPUT_MORE_IMMEDIATE descriptor */
pd += 2;
/* * If the packet has a header, the first OUTPUT_MORE/LAST descriptor's * data buffer is in the context program's coherent page and must not * be synced.
*/ if ((le32_to_cpu(pd->data_address) & PAGE_MASK) ==
(context->current_bus & PAGE_MASK)) { if (pd->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS)) return;
pd++;
}
do {
buffer_dma = le32_to_cpu(pd->data_address);
dma_sync_single_range_for_cpu(context->ohci->card.device,
buffer_dma & PAGE_MASK,
buffer_dma & ~PAGE_MASK,
le16_to_cpu(pd->req_count),
DMA_TO_DEVICE);
control = pd->control;
pd++;
} while (!(control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS)));
}
for (pd = d; pd <= last; pd++) if (pd->transfer_status) break; if (pd > last) /* Descriptor(s) not done yet, stop iteration */ return 0;
sync_it_packet_for_cpu(context, d);
if (ctx->header_length + 4 > PAGE_SIZE) { if (ctx->base.drop_overflow_headers) return 1;
flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_HEADER_OVERFLOW);
}
ctx_hdr = ctx->header + ctx->header_length;
ctx->last_timestamp = le16_to_cpu(last->res_count); /* Present this value as big-endian to match the receive code */
*ctx_hdr = cpu_to_be32((le16_to_cpu(pd->transfer_status) << 16) |
le16_to_cpu(pd->res_count));
ctx->header_length += 4;
if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS))
flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_INTERRUPT);
return 1;
}
staticvoid set_multichannel_mask(struct fw_ohci *ohci, u64 channels)
{
u32 hi = channels >> 32, lo = channels;
case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
control |= IR_CONTEXT_BUFFER_FILL|IR_CONTEXT_MULTI_CHANNEL_MODE;
fallthrough; case FW_ISO_CONTEXT_RECEIVE:
index = ctx - ohci->ir_context_list;
match = (tags << 28) | (sync << 8) | ctx->base.channel; if (cycle >= 0) {
match |= (cycle & 0x07fff) << 12;
control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
}
if (p->skip)
z = 1; else
z = 2; if (p->header_length > 0)
z++;
/* Determine the first page the payload isn't contained in. */
end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT; if (p->payload_length > 0)
payload_z = end_page - (payload_index >> PAGE_SHIFT); else
payload_z = 0;
z += payload_z;
/* Get header size in number of descriptors. */
header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
d = context_get_descriptors(&ctx->context, z + header_z, &d_bus); if (d == NULL) return -ENOMEM;
if (!p->skip) {
d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
d[0].req_count = cpu_to_le16(8); /* * Link the skip address to this descriptor itself. This causes * a context to skip a cycle whenever lost cycles or FIFO * overruns occur, without dropping the data. The application * should then decide whether this is an error condition or not. * FIXME: Make the context's cycle-lost behaviour configurable?
*/
d[0].branch_address = cpu_to_le32(d_bus | z);
/* * The OHCI controller puts the isochronous header and trailer in the * buffer, so we need at least 8 bytes.
*/
packet_count = packet->header_length / ctx->base.header_size;
header_size = max(ctx->base.header_size, (size_t)8);
/* Get header size in number of descriptors. */
header_z = DIV_ROUND_UP(header_size, sizeof(*d));
page = payload >> PAGE_SHIFT;
offset = payload & ~PAGE_MASK;
payload_per_buffer = packet->payload_length / packet_count;
for (i = 0; i < packet_count; i++) { /* d points to the header descriptor */
z = DIV_ROUND_UP(payload_per_buffer + offset, PAGE_SIZE) + 1;
d = context_get_descriptors(&ctx->context,
z + header_z, &d_bus); if (d == NULL) return -ENOMEM;
d->control = cpu_to_le16(DESCRIPTOR_STATUS |
DESCRIPTOR_INPUT_MORE); if (packet->skip && i == 0)
d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
d->req_count = cpu_to_le16(header_size);
d->res_count = d->req_count;
d->transfer_status = 0;
d->data_address = cpu_to_le32(d_bus + (z * sizeof(*d)));
/* We need one descriptor for each page in the buffer. */
z = DIV_ROUND_UP(offset + rest, PAGE_SIZE);
if (WARN_ON(offset & 3 || rest & 3 || page + z > buffer->page_count)) return -EFAULT;
for (i = 0; i < z; i++) {
d = context_get_descriptors(&ctx->context, 1, &d_bus); if (d == NULL) return -ENOMEM;
d->control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
DESCRIPTOR_BRANCH_ALWAYS); if (packet->skip && i == 0)
d->control |= cpu_to_le16(DESCRIPTOR_WAIT); if (packet->interrupt && i == z - 1)
d->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
staticint ohci_flush_iso_completions(struct fw_iso_context *base)
{ struct iso_context *ctx = container_of(base, struct iso_context, base); int ret = 0;
if (!test_and_set_bit_lock(0, &ctx->flushing_completions)) {
ohci_isoc_context_work(&base->work);
switch (base->type) { case FW_ISO_CONTEXT_TRANSMIT: case FW_ISO_CONTEXT_RECEIVE: if (ctx->header_length != 0)
flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_FLUSH); break; case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: if (ctx->mc_completed != 0)
flush_ir_buffer_fill(ctx); break; default:
ret = -ENOSYS;
}
ohci->registers = pcim_iomap_region(dev, 0, ohci_driver_name); if (IS_ERR(ohci->registers)) {
ohci_err(ohci, "request and map MMIO resource unavailable\n"); return -ENXIO;
}
for (i = 0; i < ARRAY_SIZE(ohci_quirks); i++) if ((ohci_quirks[i].vendor == dev->vendor) &&
(ohci_quirks[i].device == (unsignedshort)PCI_ANY_ID ||
ohci_quirks[i].device == dev->device) &&
(ohci_quirks[i].revision == (unsignedshort)PCI_ANY_ID ||
ohci_quirks[i].revision >= dev->revision)) {
ohci->quirks = ohci_quirks[i].flags; break;
} if (param_quirks)
ohci->quirks = param_quirks;
if (detect_vt630x_with_asm1083_on_amd_ryzen_machine(dev))
ohci->quirks |= QUIRK_REBOOT_BY_CYCLE_TIMER_READ;
/* * Because dma_alloc_coherent() allocates at least one page, * we save space by using a common buffer for the AR request/ * response descriptors and the self IDs buffer.
*/
BUILD_BUG_ON(AR_BUFFERS * sizeof(struct descriptor) > PAGE_SIZE/4);
BUILD_BUG_ON(SELF_ID_BUF_SIZE > PAGE_SIZE/2);
ohci->misc_buffer = dmam_alloc_coherent(&dev->dev, PAGE_SIZE, &ohci->misc_buffer_bus,
GFP_KERNEL); if (!ohci->misc_buffer) return -ENOMEM;
/* * If the removal is happening from the suspend state, LPS won't be * enabled and host registers (eg., IntMaskClear) won't be accessible.
*/ if (reg_read(ohci, OHCI1394_HCControlSet) & OHCI1394_HCControl_LPS) {
reg_write(ohci, OHCI1394_IntMaskClear, ~0);
flush_writes(ohci);
}
cancel_work_sync(&ohci->bus_reset_work);
fw_core_remove_card(&ohci->card);
/* * FIXME: Fail all pending packets here, now that the upper * layers can't queue any more.
*/
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.