1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for OHCI 1394 controllers 4 * 5 * Copyright (C) 2003-2006 Kristian Hoegsberg <[email protected]> 6 */ 7 8 #include <linux/bitops.h> 9 #include <linux/bug.h> 10 #include <linux/compiler.h> 11 #include <linux/delay.h> 12 #include <linux/device.h> 13 #include <linux/dma-mapping.h> 14 #include <linux/firewire.h> 15 #include <linux/firewire-constants.h> 16 #include <linux/init.h> 17 #include <linux/interrupt.h> 18 #include <linux/io.h> 19 #include <linux/kernel.h> 20 #include <linux/list.h> 21 #include <linux/mm.h> 22 #include <linux/module.h> 23 #include <linux/moduleparam.h> 24 #include <linux/mutex.h> 25 #include <linux/pci.h> 26 #include <linux/pci_ids.h> 27 #include <linux/slab.h> 28 #include <linux/spinlock.h> 29 #include <linux/string.h> 30 #include <linux/time.h> 31 #include <linux/vmalloc.h> 32 #include <linux/workqueue.h> 33 34 #include <asm/byteorder.h> 35 #include <asm/page.h> 36 37 #ifdef CONFIG_PPC_PMAC 38 #include <asm/pmac_feature.h> 39 #endif 40 41 #include "core.h" 42 #include "ohci.h" 43 #include "packet-header-definitions.h" 44 #include "phy-packet-definitions.h" 45 46 #include <trace/events/firewire.h> 47 48 static u32 cond_le32_to_cpu(__le32 value, bool has_be_header_quirk); 49 50 #define CREATE_TRACE_POINTS 51 #include <trace/events/firewire_ohci.h> 52 53 #define ohci_info(ohci, f, args...) dev_info(ohci->card.device, f, ##args) 54 #define ohci_notice(ohci, f, args...) dev_notice(ohci->card.device, f, ##args) 55 #define ohci_err(ohci, f, args...) dev_err(ohci->card.device, f, ##args) 56 57 #define DESCRIPTOR_OUTPUT_MORE 0 58 #define DESCRIPTOR_OUTPUT_LAST (1 << 12) 59 #define DESCRIPTOR_INPUT_MORE (2 << 12) 60 #define DESCRIPTOR_INPUT_LAST (3 << 12) 61 #define DESCRIPTOR_STATUS (1 << 11) 62 #define DESCRIPTOR_KEY_IMMEDIATE (2 << 8) 63 #define DESCRIPTOR_PING (1 << 7) 64 #define DESCRIPTOR_YY (1 << 6) 65 #define DESCRIPTOR_NO_IRQ (0 << 4) 66 #define DESCRIPTOR_IRQ_ERROR (1 << 4) 67 #define DESCRIPTOR_IRQ_ALWAYS (3 << 4) 68 #define DESCRIPTOR_BRANCH_ALWAYS (3 << 2) 69 #define DESCRIPTOR_WAIT (3 << 0) 70 71 #define DESCRIPTOR_CMD (0xf << 12) 72 73 struct descriptor { 74 __le16 req_count; 75 __le16 control; 76 __le32 data_address; 77 __le32 branch_address; 78 __le16 res_count; 79 __le16 transfer_status; 80 } __attribute__((aligned(16))); 81 82 #define CONTROL_SET(regs) (regs) 83 #define CONTROL_CLEAR(regs) ((regs) + 4) 84 #define COMMAND_PTR(regs) ((regs) + 12) 85 #define CONTEXT_MATCH(regs) ((regs) + 16) 86 87 #define AR_BUFFER_SIZE (32*1024) 88 #define AR_BUFFERS_MIN DIV_ROUND_UP(AR_BUFFER_SIZE, PAGE_SIZE) 89 /* we need at least two pages for proper list management */ 90 #define AR_BUFFERS (AR_BUFFERS_MIN >= 2 ? AR_BUFFERS_MIN : 2) 91 92 #define MAX_ASYNC_PAYLOAD 4096 93 #define MAX_AR_PACKET_SIZE (16 + MAX_ASYNC_PAYLOAD + 4) 94 #define AR_WRAPAROUND_PAGES DIV_ROUND_UP(MAX_AR_PACKET_SIZE, PAGE_SIZE) 95 96 struct ar_context { 97 struct fw_ohci *ohci; 98 struct page *pages[AR_BUFFERS]; 99 void *buffer; 100 struct descriptor *descriptors; 101 dma_addr_t descriptors_bus; 102 void *pointer; 103 unsigned int last_buffer_index; 104 u32 regs; 105 struct tasklet_struct tasklet; 106 }; 107 108 struct context; 109 110 typedef int (*descriptor_callback_t)(struct context *ctx, 111 struct descriptor *d, 112 struct descriptor *last); 113 114 /* 115 * A buffer that contains a block of DMA-able coherent memory used for 116 * storing a portion of a DMA descriptor program. 117 */ 118 struct descriptor_buffer { 119 struct list_head list; 120 dma_addr_t buffer_bus; 121 size_t buffer_size; 122 size_t used; 123 struct descriptor buffer[]; 124 }; 125 126 struct context { 127 struct fw_ohci *ohci; 128 u32 regs; 129 int total_allocation; 130 u32 current_bus; 131 bool running; 132 bool flushing; 133 134 /* 135 * List of page-sized buffers for storing DMA descriptors. 136 * Head of list contains buffers in use and tail of list contains 137 * free buffers. 138 */ 139 struct list_head buffer_list; 140 141 /* 142 * Pointer to a buffer inside buffer_list that contains the tail 143 * end of the current DMA program. 144 */ 145 struct descriptor_buffer *buffer_tail; 146 147 /* 148 * The descriptor containing the branch address of the first 149 * descriptor that has not yet been filled by the device. 150 */ 151 struct descriptor *last; 152 153 /* 154 * The last descriptor block in the DMA program. It contains the branch 155 * address that must be updated upon appending a new descriptor. 156 */ 157 struct descriptor *prev; 158 int prev_z; 159 160 descriptor_callback_t callback; 161 162 struct tasklet_struct tasklet; 163 }; 164 165 struct iso_context { 166 struct fw_iso_context base; 167 struct context context; 168 void *header; 169 size_t header_length; 170 unsigned long flushing_completions; 171 u32 mc_buffer_bus; 172 u16 mc_completed; 173 u16 last_timestamp; 174 u8 sync; 175 u8 tags; 176 }; 177 178 #define CONFIG_ROM_SIZE 1024 179 180 struct fw_ohci { 181 struct fw_card card; 182 183 __iomem char *registers; 184 int node_id; 185 int generation; 186 int request_generation; /* for timestamping incoming requests */ 187 unsigned quirks; 188 unsigned int pri_req_max; 189 u32 bus_time; 190 bool bus_time_running; 191 bool is_root; 192 bool csr_state_setclear_abdicate; 193 int n_ir; 194 int n_it; 195 /* 196 * Spinlock for accessing fw_ohci data. Never call out of 197 * this driver with this lock held. 198 */ 199 spinlock_t lock; 200 201 struct mutex phy_reg_mutex; 202 203 void *misc_buffer; 204 dma_addr_t misc_buffer_bus; 205 206 struct ar_context ar_request_ctx; 207 struct ar_context ar_response_ctx; 208 struct context at_request_ctx; 209 struct context at_response_ctx; 210 211 u32 it_context_support; 212 u32 it_context_mask; /* unoccupied IT contexts */ 213 struct iso_context *it_context_list; 214 u64 ir_context_channels; /* unoccupied channels */ 215 u32 ir_context_support; 216 u32 ir_context_mask; /* unoccupied IR contexts */ 217 struct iso_context *ir_context_list; 218 u64 mc_channels; /* channels in use by the multichannel IR context */ 219 bool mc_allocated; 220 221 __be32 *config_rom; 222 dma_addr_t config_rom_bus; 223 __be32 *next_config_rom; 224 dma_addr_t next_config_rom_bus; 225 __be32 next_header; 226 227 __le32 *self_id; 228 dma_addr_t self_id_bus; 229 struct work_struct bus_reset_work; 230 231 u32 self_id_buffer[512]; 232 }; 233 234 static struct workqueue_struct *selfid_workqueue; 235 236 static inline struct fw_ohci *fw_ohci(struct fw_card *card) 237 { 238 return container_of(card, struct fw_ohci, card); 239 } 240 241 #define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000 242 #define IR_CONTEXT_BUFFER_FILL 0x80000000 243 #define IR_CONTEXT_ISOCH_HEADER 0x40000000 244 #define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000 245 #define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000 246 #define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000 247 248 #define CONTEXT_RUN 0x8000 249 #define CONTEXT_WAKE 0x1000 250 #define CONTEXT_DEAD 0x0800 251 #define CONTEXT_ACTIVE 0x0400 252 253 #define OHCI1394_MAX_AT_REQ_RETRIES 0xf 254 #define OHCI1394_MAX_AT_RESP_RETRIES 0x2 255 #define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8 256 257 #define OHCI1394_REGISTER_SIZE 0x800 258 #define OHCI1394_PCI_HCI_Control 0x40 259 #define SELF_ID_BUF_SIZE 0x800 260 #define OHCI_VERSION_1_1 0x010010 261 262 static char ohci_driver_name[] = KBUILD_MODNAME; 263 264 #define PCI_VENDOR_ID_PINNACLE_SYSTEMS 0x11bd 265 #define PCI_DEVICE_ID_AGERE_FW643 0x5901 266 #define PCI_DEVICE_ID_CREATIVE_SB1394 0x4001 267 #define PCI_DEVICE_ID_JMICRON_JMB38X_FW 0x2380 268 #define PCI_DEVICE_ID_TI_TSB12LV22 0x8009 269 #define PCI_DEVICE_ID_TI_TSB12LV26 0x8020 270 #define PCI_DEVICE_ID_TI_TSB82AA2 0x8025 271 #define PCI_DEVICE_ID_VIA_VT630X 0x3044 272 #define PCI_REV_ID_VIA_VT6306 0x46 273 #define PCI_DEVICE_ID_VIA_VT6315 0x3403 274 275 #define QUIRK_CYCLE_TIMER 0x1 276 #define QUIRK_RESET_PACKET 0x2 277 #define QUIRK_BE_HEADERS 0x4 278 #define QUIRK_NO_1394A 0x8 279 #define QUIRK_NO_MSI 0x10 280 #define QUIRK_TI_SLLZ059 0x20 281 #define QUIRK_IR_WAKE 0x40 282 283 // On PCI Express Root Complex in any type of AMD Ryzen machine, VIA VT6306/6307/6308 with Asmedia 284 // ASM1083/1085 brings an inconvenience that the read accesses to 'Isochronous Cycle Timer' register 285 // (at offset 0xf0 in PCI I/O space) often causes unexpected system reboot. The mechanism is not 286 // clear, since the read access to the other registers is enough safe; e.g. 'Node ID' register, 287 // while it is probable due to detection of any type of PCIe error. 288 #define QUIRK_REBOOT_BY_CYCLE_TIMER_READ 0x80000000 289 290 #if IS_ENABLED(CONFIG_X86) 291 292 static bool has_reboot_by_cycle_timer_read_quirk(const struct fw_ohci *ohci) 293 { 294 return !!(ohci->quirks & QUIRK_REBOOT_BY_CYCLE_TIMER_READ); 295 } 296 297 #define PCI_DEVICE_ID_ASMEDIA_ASM108X 0x1080 298 299 static bool detect_vt630x_with_asm1083_on_amd_ryzen_machine(const struct pci_dev *pdev) 300 { 301 const struct pci_dev *pcie_to_pci_bridge; 302 303 // Detect any type of AMD Ryzen machine. 304 if (!static_cpu_has(X86_FEATURE_ZEN)) 305 return false; 306 307 // Detect VIA VT6306/6307/6308. 308 if (pdev->vendor != PCI_VENDOR_ID_VIA) 309 return false; 310 if (pdev->device != PCI_DEVICE_ID_VIA_VT630X) 311 return false; 312 313 // Detect Asmedia ASM1083/1085. 314 pcie_to_pci_bridge = pdev->bus->self; 315 if (pcie_to_pci_bridge->vendor != PCI_VENDOR_ID_ASMEDIA) 316 return false; 317 if (pcie_to_pci_bridge->device != PCI_DEVICE_ID_ASMEDIA_ASM108X) 318 return false; 319 320 return true; 321 } 322 323 #else 324 #define has_reboot_by_cycle_timer_read_quirk(ohci) false 325 #define detect_vt630x_with_asm1083_on_amd_ryzen_machine(pdev) false 326 #endif 327 328 /* In case of multiple matches in ohci_quirks[], only the first one is used. */ 329 static const struct { 330 unsigned short vendor, device, revision, flags; 331 } ohci_quirks[] = { 332 {PCI_VENDOR_ID_AL, PCI_ANY_ID, PCI_ANY_ID, 333 QUIRK_CYCLE_TIMER}, 334 335 {PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_FW, PCI_ANY_ID, 336 QUIRK_BE_HEADERS}, 337 338 {PCI_VENDOR_ID_ATT, PCI_DEVICE_ID_AGERE_FW643, 6, 339 QUIRK_NO_MSI}, 340 341 {PCI_VENDOR_ID_CREATIVE, PCI_DEVICE_ID_CREATIVE_SB1394, PCI_ANY_ID, 342 QUIRK_RESET_PACKET}, 343 344 {PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB38X_FW, PCI_ANY_ID, 345 QUIRK_NO_MSI}, 346 347 {PCI_VENDOR_ID_NEC, PCI_ANY_ID, PCI_ANY_ID, 348 QUIRK_CYCLE_TIMER}, 349 350 {PCI_VENDOR_ID_O2, PCI_ANY_ID, PCI_ANY_ID, 351 QUIRK_NO_MSI}, 352 353 {PCI_VENDOR_ID_RICOH, PCI_ANY_ID, PCI_ANY_ID, 354 QUIRK_CYCLE_TIMER | QUIRK_NO_MSI}, 355 356 {PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB12LV22, PCI_ANY_ID, 357 QUIRK_CYCLE_TIMER | QUIRK_RESET_PACKET | QUIRK_NO_1394A}, 358 359 {PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB12LV26, PCI_ANY_ID, 360 QUIRK_RESET_PACKET | QUIRK_TI_SLLZ059}, 361 362 {PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB82AA2, PCI_ANY_ID, 363 QUIRK_RESET_PACKET | QUIRK_TI_SLLZ059}, 364 365 {PCI_VENDOR_ID_TI, PCI_ANY_ID, PCI_ANY_ID, 366 QUIRK_RESET_PACKET}, 367 368 {PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT630X, PCI_REV_ID_VIA_VT6306, 369 QUIRK_CYCLE_TIMER | QUIRK_IR_WAKE}, 370 371 {PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT6315, 0, 372 QUIRK_CYCLE_TIMER /* FIXME: necessary? */ | QUIRK_NO_MSI}, 373 374 {PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT6315, PCI_ANY_ID, 375 QUIRK_NO_MSI}, 376 377 {PCI_VENDOR_ID_VIA, PCI_ANY_ID, PCI_ANY_ID, 378 QUIRK_CYCLE_TIMER | QUIRK_NO_MSI}, 379 }; 380 381 /* This overrides anything that was found in ohci_quirks[]. */ 382 static int param_quirks; 383 module_param_named(quirks, param_quirks, int, 0644); 384 MODULE_PARM_DESC(quirks, "Chip quirks (default = 0" 385 ", nonatomic cycle timer = " __stringify(QUIRK_CYCLE_TIMER) 386 ", reset packet generation = " __stringify(QUIRK_RESET_PACKET) 387 ", AR/selfID endianness = " __stringify(QUIRK_BE_HEADERS) 388 ", no 1394a enhancements = " __stringify(QUIRK_NO_1394A) 389 ", disable MSI = " __stringify(QUIRK_NO_MSI) 390 ", TI SLLZ059 erratum = " __stringify(QUIRK_TI_SLLZ059) 391 ", IR wake unreliable = " __stringify(QUIRK_IR_WAKE) 392 ")"); 393 394 #define OHCI_PARAM_DEBUG_AT_AR 1 395 #define OHCI_PARAM_DEBUG_SELFIDS 2 396 #define OHCI_PARAM_DEBUG_IRQS 4 397 398 static int param_debug; 399 module_param_named(debug, param_debug, int, 0644); 400 MODULE_PARM_DESC(debug, "Verbose logging (default = 0" 401 ", AT/AR events = " __stringify(OHCI_PARAM_DEBUG_AT_AR) 402 ", self-IDs = " __stringify(OHCI_PARAM_DEBUG_SELFIDS) 403 ", IRQs = " __stringify(OHCI_PARAM_DEBUG_IRQS) 404 ", or a combination, or all = -1)"); 405 406 static bool param_remote_dma; 407 module_param_named(remote_dma, param_remote_dma, bool, 0444); 408 MODULE_PARM_DESC(remote_dma, "Enable unfiltered remote DMA (default = N)"); 409 410 static void log_irqs(struct fw_ohci *ohci, u32 evt) 411 { 412 if (likely(!(param_debug & OHCI_PARAM_DEBUG_IRQS))) 413 return; 414 415 ohci_notice(ohci, "IRQ %08x%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", evt, 416 evt & OHCI1394_selfIDComplete ? " selfID" : "", 417 evt & OHCI1394_RQPkt ? " AR_req" : "", 418 evt & OHCI1394_RSPkt ? " AR_resp" : "", 419 evt & OHCI1394_reqTxComplete ? " AT_req" : "", 420 evt & OHCI1394_respTxComplete ? " AT_resp" : "", 421 evt & OHCI1394_isochRx ? " IR" : "", 422 evt & OHCI1394_isochTx ? " IT" : "", 423 evt & OHCI1394_postedWriteErr ? " postedWriteErr" : "", 424 evt & OHCI1394_cycleTooLong ? " cycleTooLong" : "", 425 evt & OHCI1394_cycle64Seconds ? " cycle64Seconds" : "", 426 evt & OHCI1394_cycleInconsistent ? " cycleInconsistent" : "", 427 evt & OHCI1394_regAccessFail ? " regAccessFail" : "", 428 evt & OHCI1394_unrecoverableError ? " unrecoverableError" : "", 429 evt & OHCI1394_busReset ? " busReset" : "", 430 evt & ~(OHCI1394_selfIDComplete | OHCI1394_RQPkt | 431 OHCI1394_RSPkt | OHCI1394_reqTxComplete | 432 OHCI1394_respTxComplete | OHCI1394_isochRx | 433 OHCI1394_isochTx | OHCI1394_postedWriteErr | 434 OHCI1394_cycleTooLong | OHCI1394_cycle64Seconds | 435 OHCI1394_cycleInconsistent | 436 OHCI1394_regAccessFail | OHCI1394_busReset) 437 ? " ?" : ""); 438 } 439 440 static void log_selfids(struct fw_ohci *ohci, int generation, int self_id_count) 441 { 442 static const char *const speed[] = { 443 [0] = "S100", [1] = "S200", [2] = "S400", [3] = "beta", 444 }; 445 static const char *const power[] = { 446 [0] = "+0W", [1] = "+15W", [2] = "+30W", [3] = "+45W", 447 [4] = "-3W", [5] = " ?W", [6] = "-3..-6W", [7] = "-3..-10W", 448 }; 449 static const char port[] = { 450 [PHY_PACKET_SELF_ID_PORT_STATUS_NONE] = '.', 451 [PHY_PACKET_SELF_ID_PORT_STATUS_NCONN] = '-', 452 [PHY_PACKET_SELF_ID_PORT_STATUS_PARENT] = 'p', 453 [PHY_PACKET_SELF_ID_PORT_STATUS_CHILD] = 'c', 454 }; 455 struct self_id_sequence_enumerator enumerator = { 456 .cursor = ohci->self_id_buffer, 457 .quadlet_count = self_id_count, 458 }; 459 460 if (likely(!(param_debug & OHCI_PARAM_DEBUG_SELFIDS))) 461 return; 462 463 ohci_notice(ohci, "%d selfIDs, generation %d, local node ID %04x\n", 464 self_id_count, generation, ohci->node_id); 465 466 while (enumerator.quadlet_count > 0) { 467 unsigned int quadlet_count; 468 unsigned int port_index; 469 const u32 *s; 470 int i; 471 472 s = self_id_sequence_enumerator_next(&enumerator, &quadlet_count); 473 if (IS_ERR(s)) 474 break; 475 476 ohci_notice(ohci, 477 "selfID 0: %08x, phy %d [%c%c%c] %s gc=%d %s %s%s%s\n", 478 *s, 479 phy_packet_self_id_get_phy_id(*s), 480 port[self_id_sequence_get_port_status(s, quadlet_count, 0)], 481 port[self_id_sequence_get_port_status(s, quadlet_count, 1)], 482 port[self_id_sequence_get_port_status(s, quadlet_count, 2)], 483 speed[*s >> 14 & 3], *s >> 16 & 63, 484 power[*s >> 8 & 7], *s >> 22 & 1 ? "L" : "", 485 *s >> 11 & 1 ? "c" : "", *s & 2 ? "i" : ""); 486 487 port_index = 3; 488 for (i = 1; i < quadlet_count; ++i) { 489 ohci_notice(ohci, 490 "selfID n: %08x, phy %d [%c%c%c%c%c%c%c%c]\n", 491 s[i], 492 phy_packet_self_id_get_phy_id(s[i]), 493 port[self_id_sequence_get_port_status(s, quadlet_count, port_index)], 494 port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 1)], 495 port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 2)], 496 port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 3)], 497 port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 4)], 498 port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 5)], 499 port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 6)], 500 port[self_id_sequence_get_port_status(s, quadlet_count, port_index + 7)] 501 ); 502 503 port_index += 8; 504 } 505 } 506 } 507 508 static const char *evts[] = { 509 [0x00] = "evt_no_status", [0x01] = "-reserved-", 510 [0x02] = "evt_long_packet", [0x03] = "evt_missing_ack", 511 [0x04] = "evt_underrun", [0x05] = "evt_overrun", 512 [0x06] = "evt_descriptor_read", [0x07] = "evt_data_read", 513 [0x08] = "evt_data_write", [0x09] = "evt_bus_reset", 514 [0x0a] = "evt_timeout", [0x0b] = "evt_tcode_err", 515 [0x0c] = "-reserved-", [0x0d] = "-reserved-", 516 [0x0e] = "evt_unknown", [0x0f] = "evt_flushed", 517 [0x10] = "-reserved-", [0x11] = "ack_complete", 518 [0x12] = "ack_pending ", [0x13] = "-reserved-", 519 [0x14] = "ack_busy_X", [0x15] = "ack_busy_A", 520 [0x16] = "ack_busy_B", [0x17] = "-reserved-", 521 [0x18] = "-reserved-", [0x19] = "-reserved-", 522 [0x1a] = "-reserved-", [0x1b] = "ack_tardy", 523 [0x1c] = "-reserved-", [0x1d] = "ack_data_error", 524 [0x1e] = "ack_type_error", [0x1f] = "-reserved-", 525 [0x20] = "pending/cancelled", 526 }; 527 528 static void log_ar_at_event(struct fw_ohci *ohci, 529 char dir, int speed, u32 *header, int evt) 530 { 531 static const char *const tcodes[] = { 532 [TCODE_WRITE_QUADLET_REQUEST] = "QW req", 533 [TCODE_WRITE_BLOCK_REQUEST] = "BW req", 534 [TCODE_WRITE_RESPONSE] = "W resp", 535 [0x3] = "-reserved-", 536 [TCODE_READ_QUADLET_REQUEST] = "QR req", 537 [TCODE_READ_BLOCK_REQUEST] = "BR req", 538 [TCODE_READ_QUADLET_RESPONSE] = "QR resp", 539 [TCODE_READ_BLOCK_RESPONSE] = "BR resp", 540 [TCODE_CYCLE_START] = "cycle start", 541 [TCODE_LOCK_REQUEST] = "Lk req", 542 [TCODE_STREAM_DATA] = "async stream packet", 543 [TCODE_LOCK_RESPONSE] = "Lk resp", 544 [0xc] = "-reserved-", 545 [0xd] = "-reserved-", 546 [TCODE_LINK_INTERNAL] = "link internal", 547 [0xf] = "-reserved-", 548 }; 549 int tcode = async_header_get_tcode(header); 550 char specific[12]; 551 552 if (likely(!(param_debug & OHCI_PARAM_DEBUG_AT_AR))) 553 return; 554 555 if (unlikely(evt >= ARRAY_SIZE(evts))) 556 evt = 0x1f; 557 558 if (evt == OHCI1394_evt_bus_reset) { 559 ohci_notice(ohci, "A%c evt_bus_reset, generation %d\n", 560 dir, (header[2] >> 16) & 0xff); 561 return; 562 } 563 564 switch (tcode) { 565 case TCODE_WRITE_QUADLET_REQUEST: 566 case TCODE_READ_QUADLET_RESPONSE: 567 case TCODE_CYCLE_START: 568 snprintf(specific, sizeof(specific), " = %08x", 569 be32_to_cpu((__force __be32)header[3])); 570 break; 571 case TCODE_WRITE_BLOCK_REQUEST: 572 case TCODE_READ_BLOCK_REQUEST: 573 case TCODE_READ_BLOCK_RESPONSE: 574 case TCODE_LOCK_REQUEST: 575 case TCODE_LOCK_RESPONSE: 576 snprintf(specific, sizeof(specific), " %x,%x", 577 async_header_get_data_length(header), 578 async_header_get_extended_tcode(header)); 579 break; 580 default: 581 specific[0] = '\0'; 582 } 583 584 switch (tcode) { 585 case TCODE_STREAM_DATA: 586 ohci_notice(ohci, "A%c %s, %s\n", 587 dir, evts[evt], tcodes[tcode]); 588 break; 589 case TCODE_LINK_INTERNAL: 590 ohci_notice(ohci, "A%c %s, PHY %08x %08x\n", 591 dir, evts[evt], header[1], header[2]); 592 break; 593 case TCODE_WRITE_QUADLET_REQUEST: 594 case TCODE_WRITE_BLOCK_REQUEST: 595 case TCODE_READ_QUADLET_REQUEST: 596 case TCODE_READ_BLOCK_REQUEST: 597 case TCODE_LOCK_REQUEST: 598 ohci_notice(ohci, 599 "A%c spd %x tl %02x, %04x -> %04x, %s, %s, %012llx%s\n", 600 dir, speed, async_header_get_tlabel(header), 601 async_header_get_source(header), async_header_get_destination(header), 602 evts[evt], tcodes[tcode], async_header_get_offset(header), specific); 603 break; 604 default: 605 ohci_notice(ohci, 606 "A%c spd %x tl %02x, %04x -> %04x, %s, %s%s\n", 607 dir, speed, async_header_get_tlabel(header), 608 async_header_get_source(header), async_header_get_destination(header), 609 evts[evt], tcodes[tcode], specific); 610 } 611 } 612 613 static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data) 614 { 615 writel(data, ohci->registers + offset); 616 } 617 618 static inline u32 reg_read(const struct fw_ohci *ohci, int offset) 619 { 620 return readl(ohci->registers + offset); 621 } 622 623 static inline void flush_writes(const struct fw_ohci *ohci) 624 { 625 /* Do a dummy read to flush writes. */ 626 reg_read(ohci, OHCI1394_Version); 627 } 628 629 /* 630 * Beware! read_phy_reg(), write_phy_reg(), update_phy_reg(), and 631 * read_paged_phy_reg() require the caller to hold ohci->phy_reg_mutex. 632 * In other words, only use ohci_read_phy_reg() and ohci_update_phy_reg() 633 * directly. Exceptions are intrinsically serialized contexts like pci_probe. 634 */ 635 static int read_phy_reg(struct fw_ohci *ohci, int addr) 636 { 637 u32 val; 638 int i; 639 640 reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr)); 641 for (i = 0; i < 3 + 100; i++) { 642 val = reg_read(ohci, OHCI1394_PhyControl); 643 if (!~val) 644 return -ENODEV; /* Card was ejected. */ 645 646 if (val & OHCI1394_PhyControl_ReadDone) 647 return OHCI1394_PhyControl_ReadData(val); 648 649 /* 650 * Try a few times without waiting. Sleeping is necessary 651 * only when the link/PHY interface is busy. 652 */ 653 if (i >= 3) 654 msleep(1); 655 } 656 ohci_err(ohci, "failed to read phy reg %d\n", addr); 657 dump_stack(); 658 659 return -EBUSY; 660 } 661 662 static int write_phy_reg(const struct fw_ohci *ohci, int addr, u32 val) 663 { 664 int i; 665 666 reg_write(ohci, OHCI1394_PhyControl, 667 OHCI1394_PhyControl_Write(addr, val)); 668 for (i = 0; i < 3 + 100; i++) { 669 val = reg_read(ohci, OHCI1394_PhyControl); 670 if (!~val) 671 return -ENODEV; /* Card was ejected. */ 672 673 if (!(val & OHCI1394_PhyControl_WritePending)) 674 return 0; 675 676 if (i >= 3) 677 msleep(1); 678 } 679 ohci_err(ohci, "failed to write phy reg %d, val %u\n", addr, val); 680 dump_stack(); 681 682 return -EBUSY; 683 } 684 685 static int update_phy_reg(struct fw_ohci *ohci, int addr, 686 int clear_bits, int set_bits) 687 { 688 int ret = read_phy_reg(ohci, addr); 689 if (ret < 0) 690 return ret; 691 692 /* 693 * The interrupt status bits are cleared by writing a one bit. 694 * Avoid clearing them unless explicitly requested in set_bits. 695 */ 696 if (addr == 5) 697 clear_bits |= PHY_INT_STATUS_BITS; 698 699 return write_phy_reg(ohci, addr, (ret & ~clear_bits) | set_bits); 700 } 701 702 static int read_paged_phy_reg(struct fw_ohci *ohci, int page, int addr) 703 { 704 int ret; 705 706 ret = update_phy_reg(ohci, 7, PHY_PAGE_SELECT, page << 5); 707 if (ret < 0) 708 return ret; 709 710 return read_phy_reg(ohci, addr); 711 } 712 713 static int ohci_read_phy_reg(struct fw_card *card, int addr) 714 { 715 struct fw_ohci *ohci = fw_ohci(card); 716 717 guard(mutex)(&ohci->phy_reg_mutex); 718 719 return read_phy_reg(ohci, addr); 720 } 721 722 static int ohci_update_phy_reg(struct fw_card *card, int addr, 723 int clear_bits, int set_bits) 724 { 725 struct fw_ohci *ohci = fw_ohci(card); 726 727 guard(mutex)(&ohci->phy_reg_mutex); 728 729 return update_phy_reg(ohci, addr, clear_bits, set_bits); 730 } 731 732 static inline dma_addr_t ar_buffer_bus(struct ar_context *ctx, unsigned int i) 733 { 734 return page_private(ctx->pages[i]); 735 } 736 737 static void ar_context_link_page(struct ar_context *ctx, unsigned int index) 738 { 739 struct descriptor *d; 740 741 d = &ctx->descriptors[index]; 742 d->branch_address &= cpu_to_le32(~0xf); 743 d->res_count = cpu_to_le16(PAGE_SIZE); 744 d->transfer_status = 0; 745 746 wmb(); /* finish init of new descriptors before branch_address update */ 747 d = &ctx->descriptors[ctx->last_buffer_index]; 748 d->branch_address |= cpu_to_le32(1); 749 750 ctx->last_buffer_index = index; 751 752 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE); 753 } 754 755 static void ar_context_release(struct ar_context *ctx) 756 { 757 struct device *dev = ctx->ohci->card.device; 758 unsigned int i; 759 760 if (!ctx->buffer) 761 return; 762 763 vunmap(ctx->buffer); 764 765 for (i = 0; i < AR_BUFFERS; i++) { 766 if (ctx->pages[i]) 767 dma_free_pages(dev, PAGE_SIZE, ctx->pages[i], 768 ar_buffer_bus(ctx, i), DMA_FROM_DEVICE); 769 } 770 } 771 772 static void ar_context_abort(struct ar_context *ctx, const char *error_msg) 773 { 774 struct fw_ohci *ohci = ctx->ohci; 775 776 if (reg_read(ohci, CONTROL_CLEAR(ctx->regs)) & CONTEXT_RUN) { 777 reg_write(ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN); 778 flush_writes(ohci); 779 780 ohci_err(ohci, "AR error: %s; DMA stopped\n", error_msg); 781 } 782 /* FIXME: restart? */ 783 } 784 785 static inline unsigned int ar_next_buffer_index(unsigned int index) 786 { 787 return (index + 1) % AR_BUFFERS; 788 } 789 790 static inline unsigned int ar_first_buffer_index(struct ar_context *ctx) 791 { 792 return ar_next_buffer_index(ctx->last_buffer_index); 793 } 794 795 /* 796 * We search for the buffer that contains the last AR packet DMA data written 797 * by the controller. 798 */ 799 static unsigned int ar_search_last_active_buffer(struct ar_context *ctx, 800 unsigned int *buffer_offset) 801 { 802 unsigned int i, next_i, last = ctx->last_buffer_index; 803 __le16 res_count, next_res_count; 804 805 i = ar_first_buffer_index(ctx); 806 res_count = READ_ONCE(ctx->descriptors[i].res_count); 807 808 /* A buffer that is not yet completely filled must be the last one. */ 809 while (i != last && res_count == 0) { 810 811 /* Peek at the next descriptor. */ 812 next_i = ar_next_buffer_index(i); 813 rmb(); /* read descriptors in order */ 814 next_res_count = READ_ONCE(ctx->descriptors[next_i].res_count); 815 /* 816 * If the next descriptor is still empty, we must stop at this 817 * descriptor. 818 */ 819 if (next_res_count == cpu_to_le16(PAGE_SIZE)) { 820 /* 821 * The exception is when the DMA data for one packet is 822 * split over three buffers; in this case, the middle 823 * buffer's descriptor might be never updated by the 824 * controller and look still empty, and we have to peek 825 * at the third one. 826 */ 827 if (MAX_AR_PACKET_SIZE > PAGE_SIZE && i != last) { 828 next_i = ar_next_buffer_index(next_i); 829 rmb(); 830 next_res_count = READ_ONCE(ctx->descriptors[next_i].res_count); 831 if (next_res_count != cpu_to_le16(PAGE_SIZE)) 832 goto next_buffer_is_active; 833 } 834 835 break; 836 } 837 838 next_buffer_is_active: 839 i = next_i; 840 res_count = next_res_count; 841 } 842 843 rmb(); /* read res_count before the DMA data */ 844 845 *buffer_offset = PAGE_SIZE - le16_to_cpu(res_count); 846 if (*buffer_offset > PAGE_SIZE) { 847 *buffer_offset = 0; 848 ar_context_abort(ctx, "corrupted descriptor"); 849 } 850 851 return i; 852 } 853 854 static void ar_sync_buffers_for_cpu(struct ar_context *ctx, 855 unsigned int end_buffer_index, 856 unsigned int end_buffer_offset) 857 { 858 unsigned int i; 859 860 i = ar_first_buffer_index(ctx); 861 while (i != end_buffer_index) { 862 dma_sync_single_for_cpu(ctx->ohci->card.device, 863 ar_buffer_bus(ctx, i), 864 PAGE_SIZE, DMA_FROM_DEVICE); 865 i = ar_next_buffer_index(i); 866 } 867 if (end_buffer_offset > 0) 868 dma_sync_single_for_cpu(ctx->ohci->card.device, 869 ar_buffer_bus(ctx, i), 870 end_buffer_offset, DMA_FROM_DEVICE); 871 } 872 873 #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32) 874 static u32 cond_le32_to_cpu(__le32 value, bool has_be_header_quirk) 875 { 876 return has_be_header_quirk ? (__force __u32)value : le32_to_cpu(value); 877 } 878 879 static bool has_be_header_quirk(const struct fw_ohci *ohci) 880 { 881 return !!(ohci->quirks & QUIRK_BE_HEADERS); 882 } 883 #else 884 static u32 cond_le32_to_cpu(__le32 value, bool has_be_header_quirk __maybe_unused) 885 { 886 return le32_to_cpu(value); 887 } 888 889 static bool has_be_header_quirk(const struct fw_ohci *ohci) 890 { 891 return false; 892 } 893 #endif 894 895 static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer) 896 { 897 struct fw_ohci *ohci = ctx->ohci; 898 struct fw_packet p; 899 u32 status, length, tcode; 900 int evt; 901 902 p.header[0] = cond_le32_to_cpu(buffer[0], has_be_header_quirk(ohci)); 903 p.header[1] = cond_le32_to_cpu(buffer[1], has_be_header_quirk(ohci)); 904 p.header[2] = cond_le32_to_cpu(buffer[2], has_be_header_quirk(ohci)); 905 906 tcode = async_header_get_tcode(p.header); 907 switch (tcode) { 908 case TCODE_WRITE_QUADLET_REQUEST: 909 case TCODE_READ_QUADLET_RESPONSE: 910 p.header[3] = (__force __u32) buffer[3]; 911 p.header_length = 16; 912 p.payload_length = 0; 913 break; 914 915 case TCODE_READ_BLOCK_REQUEST : 916 p.header[3] = cond_le32_to_cpu(buffer[3], has_be_header_quirk(ohci)); 917 p.header_length = 16; 918 p.payload_length = 0; 919 break; 920 921 case TCODE_WRITE_BLOCK_REQUEST: 922 case TCODE_READ_BLOCK_RESPONSE: 923 case TCODE_LOCK_REQUEST: 924 case TCODE_LOCK_RESPONSE: 925 p.header[3] = cond_le32_to_cpu(buffer[3], has_be_header_quirk(ohci)); 926 p.header_length = 16; 927 p.payload_length = async_header_get_data_length(p.header); 928 if (p.payload_length > MAX_ASYNC_PAYLOAD) { 929 ar_context_abort(ctx, "invalid packet length"); 930 return NULL; 931 } 932 break; 933 934 case TCODE_WRITE_RESPONSE: 935 case TCODE_READ_QUADLET_REQUEST: 936 case TCODE_LINK_INTERNAL: 937 p.header_length = 12; 938 p.payload_length = 0; 939 break; 940 941 default: 942 ar_context_abort(ctx, "invalid tcode"); 943 return NULL; 944 } 945 946 p.payload = (void *) buffer + p.header_length; 947 948 /* FIXME: What to do about evt_* errors? */ 949 length = (p.header_length + p.payload_length + 3) / 4; 950 status = cond_le32_to_cpu(buffer[length], has_be_header_quirk(ohci)); 951 evt = (status >> 16) & 0x1f; 952 953 p.ack = evt - 16; 954 p.speed = (status >> 21) & 0x7; 955 p.timestamp = status & 0xffff; 956 p.generation = ohci->request_generation; 957 958 log_ar_at_event(ohci, 'R', p.speed, p.header, evt); 959 960 /* 961 * Several controllers, notably from NEC and VIA, forget to 962 * write ack_complete status at PHY packet reception. 963 */ 964 if (evt == OHCI1394_evt_no_status && tcode == TCODE_LINK_INTERNAL) 965 p.ack = ACK_COMPLETE; 966 967 /* 968 * The OHCI bus reset handler synthesizes a PHY packet with 969 * the new generation number when a bus reset happens (see 970 * section 8.4.2.3). This helps us determine when a request 971 * was received and make sure we send the response in the same 972 * generation. We only need this for requests; for responses 973 * we use the unique tlabel for finding the matching 974 * request. 975 * 976 * Alas some chips sometimes emit bus reset packets with a 977 * wrong generation. We set the correct generation for these 978 * at a slightly incorrect time (in bus_reset_work). 979 */ 980 if (evt == OHCI1394_evt_bus_reset) { 981 if (!(ohci->quirks & QUIRK_RESET_PACKET)) 982 ohci->request_generation = (p.header[2] >> 16) & 0xff; 983 } else if (ctx == &ohci->ar_request_ctx) { 984 fw_core_handle_request(&ohci->card, &p); 985 } else { 986 fw_core_handle_response(&ohci->card, &p); 987 } 988 989 return buffer + length + 1; 990 } 991 992 static void *handle_ar_packets(struct ar_context *ctx, void *p, void *end) 993 { 994 void *next; 995 996 while (p < end) { 997 next = handle_ar_packet(ctx, p); 998 if (!next) 999 return p; 1000 p = next; 1001 } 1002 1003 return p; 1004 } 1005 1006 static void ar_recycle_buffers(struct ar_context *ctx, unsigned int end_buffer) 1007 { 1008 unsigned int i; 1009 1010 i = ar_first_buffer_index(ctx); 1011 while (i != end_buffer) { 1012 dma_sync_single_for_device(ctx->ohci->card.device, 1013 ar_buffer_bus(ctx, i), 1014 PAGE_SIZE, DMA_FROM_DEVICE); 1015 ar_context_link_page(ctx, i); 1016 i = ar_next_buffer_index(i); 1017 } 1018 } 1019 1020 static void ar_context_tasklet(unsigned long data) 1021 { 1022 struct ar_context *ctx = (struct ar_context *)data; 1023 unsigned int end_buffer_index, end_buffer_offset; 1024 void *p, *end; 1025 1026 p = ctx->pointer; 1027 if (!p) 1028 return; 1029 1030 end_buffer_index = ar_search_last_active_buffer(ctx, 1031 &end_buffer_offset); 1032 ar_sync_buffers_for_cpu(ctx, end_buffer_index, end_buffer_offset); 1033 end = ctx->buffer + end_buffer_index * PAGE_SIZE + end_buffer_offset; 1034 1035 if (end_buffer_index < ar_first_buffer_index(ctx)) { 1036 /* 1037 * The filled part of the overall buffer wraps around; handle 1038 * all packets up to the buffer end here. If the last packet 1039 * wraps around, its tail will be visible after the buffer end 1040 * because the buffer start pages are mapped there again. 1041 */ 1042 void *buffer_end = ctx->buffer + AR_BUFFERS * PAGE_SIZE; 1043 p = handle_ar_packets(ctx, p, buffer_end); 1044 if (p < buffer_end) 1045 goto error; 1046 /* adjust p to point back into the actual buffer */ 1047 p -= AR_BUFFERS * PAGE_SIZE; 1048 } 1049 1050 p = handle_ar_packets(ctx, p, end); 1051 if (p != end) { 1052 if (p > end) 1053 ar_context_abort(ctx, "inconsistent descriptor"); 1054 goto error; 1055 } 1056 1057 ctx->pointer = p; 1058 ar_recycle_buffers(ctx, end_buffer_index); 1059 1060 return; 1061 1062 error: 1063 ctx->pointer = NULL; 1064 } 1065 1066 static int ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, 1067 unsigned int descriptors_offset, u32 regs) 1068 { 1069 struct device *dev = ohci->card.device; 1070 unsigned int i; 1071 dma_addr_t dma_addr; 1072 struct page *pages[AR_BUFFERS + AR_WRAPAROUND_PAGES]; 1073 struct descriptor *d; 1074 1075 ctx->regs = regs; 1076 ctx->ohci = ohci; 1077 tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx); 1078 1079 for (i = 0; i < AR_BUFFERS; i++) { 1080 ctx->pages[i] = dma_alloc_pages(dev, PAGE_SIZE, &dma_addr, 1081 DMA_FROM_DEVICE, GFP_KERNEL); 1082 if (!ctx->pages[i]) 1083 goto out_of_memory; 1084 set_page_private(ctx->pages[i], dma_addr); 1085 dma_sync_single_for_device(dev, dma_addr, PAGE_SIZE, 1086 DMA_FROM_DEVICE); 1087 } 1088 1089 for (i = 0; i < AR_BUFFERS; i++) 1090 pages[i] = ctx->pages[i]; 1091 for (i = 0; i < AR_WRAPAROUND_PAGES; i++) 1092 pages[AR_BUFFERS + i] = ctx->pages[i]; 1093 ctx->buffer = vmap(pages, ARRAY_SIZE(pages), VM_MAP, PAGE_KERNEL); 1094 if (!ctx->buffer) 1095 goto out_of_memory; 1096 1097 ctx->descriptors = ohci->misc_buffer + descriptors_offset; 1098 ctx->descriptors_bus = ohci->misc_buffer_bus + descriptors_offset; 1099 1100 for (i = 0; i < AR_BUFFERS; i++) { 1101 d = &ctx->descriptors[i]; 1102 d->req_count = cpu_to_le16(PAGE_SIZE); 1103 d->control = cpu_to_le16(DESCRIPTOR_INPUT_MORE | 1104 DESCRIPTOR_STATUS | 1105 DESCRIPTOR_BRANCH_ALWAYS); 1106 d->data_address = cpu_to_le32(ar_buffer_bus(ctx, i)); 1107 d->branch_address = cpu_to_le32(ctx->descriptors_bus + 1108 ar_next_buffer_index(i) * sizeof(struct descriptor)); 1109 } 1110 1111 return 0; 1112 1113 out_of_memory: 1114 ar_context_release(ctx); 1115 1116 return -ENOMEM; 1117 } 1118 1119 static void ar_context_run(struct ar_context *ctx) 1120 { 1121 unsigned int i; 1122 1123 for (i = 0; i < AR_BUFFERS; i++) 1124 ar_context_link_page(ctx, i); 1125 1126 ctx->pointer = ctx->buffer; 1127 1128 reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ctx->descriptors_bus | 1); 1129 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN); 1130 } 1131 1132 static struct descriptor *find_branch_descriptor(struct descriptor *d, int z) 1133 { 1134 __le16 branch; 1135 1136 branch = d->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS); 1137 1138 /* figure out which descriptor the branch address goes in */ 1139 if (z == 2 && branch == cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS)) 1140 return d; 1141 else 1142 return d + z - 1; 1143 } 1144 1145 static void context_tasklet(unsigned long data) 1146 { 1147 struct context *ctx = (struct context *) data; 1148 struct descriptor *d, *last; 1149 u32 address; 1150 int z; 1151 struct descriptor_buffer *desc; 1152 1153 desc = list_entry(ctx->buffer_list.next, 1154 struct descriptor_buffer, list); 1155 last = ctx->last; 1156 while (last->branch_address != 0) { 1157 struct descriptor_buffer *old_desc = desc; 1158 address = le32_to_cpu(last->branch_address); 1159 z = address & 0xf; 1160 address &= ~0xf; 1161 ctx->current_bus = address; 1162 1163 /* If the branch address points to a buffer outside of the 1164 * current buffer, advance to the next buffer. */ 1165 if (address < desc->buffer_bus || 1166 address >= desc->buffer_bus + desc->used) 1167 desc = list_entry(desc->list.next, 1168 struct descriptor_buffer, list); 1169 d = desc->buffer + (address - desc->buffer_bus) / sizeof(*d); 1170 last = find_branch_descriptor(d, z); 1171 1172 if (!ctx->callback(ctx, d, last)) 1173 break; 1174 1175 if (old_desc != desc) { 1176 /* If we've advanced to the next buffer, move the 1177 * previous buffer to the free list. */ 1178 unsigned long flags; 1179 old_desc->used = 0; 1180 spin_lock_irqsave(&ctx->ohci->lock, flags); 1181 list_move_tail(&old_desc->list, &ctx->buffer_list); 1182 spin_unlock_irqrestore(&ctx->ohci->lock, flags); 1183 } 1184 ctx->last = last; 1185 } 1186 } 1187 1188 /* 1189 * Allocate a new buffer and add it to the list of free buffers for this 1190 * context. Must be called with ohci->lock held. 1191 */ 1192 static int context_add_buffer(struct context *ctx) 1193 { 1194 struct descriptor_buffer *desc; 1195 dma_addr_t bus_addr; 1196 int offset; 1197 1198 /* 1199 * 16MB of descriptors should be far more than enough for any DMA 1200 * program. This will catch run-away userspace or DoS attacks. 1201 */ 1202 if (ctx->total_allocation >= 16*1024*1024) 1203 return -ENOMEM; 1204 1205 desc = dmam_alloc_coherent(ctx->ohci->card.device, PAGE_SIZE, &bus_addr, GFP_ATOMIC); 1206 if (!desc) 1207 return -ENOMEM; 1208 1209 offset = (void *)&desc->buffer - (void *)desc; 1210 /* 1211 * Some controllers, like JMicron ones, always issue 0x20-byte DMA reads 1212 * for descriptors, even 0x10-byte ones. This can cause page faults when 1213 * an IOMMU is in use and the oversized read crosses a page boundary. 1214 * Work around this by always leaving at least 0x10 bytes of padding. 1215 */ 1216 desc->buffer_size = PAGE_SIZE - offset - 0x10; 1217 desc->buffer_bus = bus_addr + offset; 1218 desc->used = 0; 1219 1220 list_add_tail(&desc->list, &ctx->buffer_list); 1221 ctx->total_allocation += PAGE_SIZE; 1222 1223 return 0; 1224 } 1225 1226 static int context_init(struct context *ctx, struct fw_ohci *ohci, 1227 u32 regs, descriptor_callback_t callback) 1228 { 1229 ctx->ohci = ohci; 1230 ctx->regs = regs; 1231 ctx->total_allocation = 0; 1232 1233 INIT_LIST_HEAD(&ctx->buffer_list); 1234 if (context_add_buffer(ctx) < 0) 1235 return -ENOMEM; 1236 1237 ctx->buffer_tail = list_entry(ctx->buffer_list.next, 1238 struct descriptor_buffer, list); 1239 1240 tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx); 1241 ctx->callback = callback; 1242 1243 /* 1244 * We put a dummy descriptor in the buffer that has a NULL 1245 * branch address and looks like it's been sent. That way we 1246 * have a descriptor to append DMA programs to. 1247 */ 1248 memset(ctx->buffer_tail->buffer, 0, sizeof(*ctx->buffer_tail->buffer)); 1249 ctx->buffer_tail->buffer->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST); 1250 ctx->buffer_tail->buffer->transfer_status = cpu_to_le16(0x8011); 1251 ctx->buffer_tail->used += sizeof(*ctx->buffer_tail->buffer); 1252 ctx->last = ctx->buffer_tail->buffer; 1253 ctx->prev = ctx->buffer_tail->buffer; 1254 ctx->prev_z = 1; 1255 1256 return 0; 1257 } 1258 1259 static void context_release(struct context *ctx) 1260 { 1261 struct fw_card *card = &ctx->ohci->card; 1262 struct descriptor_buffer *desc, *tmp; 1263 1264 list_for_each_entry_safe(desc, tmp, &ctx->buffer_list, list) { 1265 dmam_free_coherent(card->device, PAGE_SIZE, desc, 1266 desc->buffer_bus - ((void *)&desc->buffer - (void *)desc)); 1267 } 1268 } 1269 1270 /* Must be called with ohci->lock held */ 1271 static struct descriptor *context_get_descriptors(struct context *ctx, 1272 int z, dma_addr_t *d_bus) 1273 { 1274 struct descriptor *d = NULL; 1275 struct descriptor_buffer *desc = ctx->buffer_tail; 1276 1277 if (z * sizeof(*d) > desc->buffer_size) 1278 return NULL; 1279 1280 if (z * sizeof(*d) > desc->buffer_size - desc->used) { 1281 /* No room for the descriptor in this buffer, so advance to the 1282 * next one. */ 1283 1284 if (desc->list.next == &ctx->buffer_list) { 1285 /* If there is no free buffer next in the list, 1286 * allocate one. */ 1287 if (context_add_buffer(ctx) < 0) 1288 return NULL; 1289 } 1290 desc = list_entry(desc->list.next, 1291 struct descriptor_buffer, list); 1292 ctx->buffer_tail = desc; 1293 } 1294 1295 d = desc->buffer + desc->used / sizeof(*d); 1296 memset(d, 0, z * sizeof(*d)); 1297 *d_bus = desc->buffer_bus + desc->used; 1298 1299 return d; 1300 } 1301 1302 static void context_run(struct context *ctx, u32 extra) 1303 { 1304 struct fw_ohci *ohci = ctx->ohci; 1305 1306 reg_write(ohci, COMMAND_PTR(ctx->regs), 1307 le32_to_cpu(ctx->last->branch_address)); 1308 reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0); 1309 reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra); 1310 ctx->running = true; 1311 flush_writes(ohci); 1312 } 1313 1314 static void context_append(struct context *ctx, 1315 struct descriptor *d, int z, int extra) 1316 { 1317 dma_addr_t d_bus; 1318 struct descriptor_buffer *desc = ctx->buffer_tail; 1319 struct descriptor *d_branch; 1320 1321 d_bus = desc->buffer_bus + (d - desc->buffer) * sizeof(*d); 1322 1323 desc->used += (z + extra) * sizeof(*d); 1324 1325 wmb(); /* finish init of new descriptors before branch_address update */ 1326 1327 d_branch = find_branch_descriptor(ctx->prev, ctx->prev_z); 1328 d_branch->branch_address = cpu_to_le32(d_bus | z); 1329 1330 /* 1331 * VT6306 incorrectly checks only the single descriptor at the 1332 * CommandPtr when the wake bit is written, so if it's a 1333 * multi-descriptor block starting with an INPUT_MORE, put a copy of 1334 * the branch address in the first descriptor. 1335 * 1336 * Not doing this for transmit contexts since not sure how it interacts 1337 * with skip addresses. 1338 */ 1339 if (unlikely(ctx->ohci->quirks & QUIRK_IR_WAKE) && 1340 d_branch != ctx->prev && 1341 (ctx->prev->control & cpu_to_le16(DESCRIPTOR_CMD)) == 1342 cpu_to_le16(DESCRIPTOR_INPUT_MORE)) { 1343 ctx->prev->branch_address = cpu_to_le32(d_bus | z); 1344 } 1345 1346 ctx->prev = d; 1347 ctx->prev_z = z; 1348 } 1349 1350 static void context_stop(struct context *ctx) 1351 { 1352 struct fw_ohci *ohci = ctx->ohci; 1353 u32 reg; 1354 int i; 1355 1356 reg_write(ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN); 1357 ctx->running = false; 1358 1359 for (i = 0; i < 1000; i++) { 1360 reg = reg_read(ohci, CONTROL_SET(ctx->regs)); 1361 if ((reg & CONTEXT_ACTIVE) == 0) 1362 return; 1363 1364 if (i) 1365 udelay(10); 1366 } 1367 ohci_err(ohci, "DMA context still active (0x%08x)\n", reg); 1368 } 1369 1370 struct driver_data { 1371 u8 inline_data[8]; 1372 struct fw_packet *packet; 1373 }; 1374 1375 /* 1376 * This function apppends a packet to the DMA queue for transmission. 1377 * Must always be called with the ochi->lock held to ensure proper 1378 * generation handling and locking around packet queue manipulation. 1379 */ 1380 static int at_context_queue_packet(struct context *ctx, 1381 struct fw_packet *packet) 1382 { 1383 struct fw_ohci *ohci = ctx->ohci; 1384 dma_addr_t d_bus, payload_bus; 1385 struct driver_data *driver_data; 1386 struct descriptor *d, *last; 1387 __le32 *header; 1388 int z, tcode; 1389 1390 d = context_get_descriptors(ctx, 4, &d_bus); 1391 if (d == NULL) { 1392 packet->ack = RCODE_SEND_ERROR; 1393 return -1; 1394 } 1395 1396 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE); 1397 d[0].res_count = cpu_to_le16(packet->timestamp); 1398 1399 tcode = async_header_get_tcode(packet->header); 1400 header = (__le32 *) &d[1]; 1401 switch (tcode) { 1402 case TCODE_WRITE_QUADLET_REQUEST: 1403 case TCODE_WRITE_BLOCK_REQUEST: 1404 case TCODE_WRITE_RESPONSE: 1405 case TCODE_READ_QUADLET_REQUEST: 1406 case TCODE_READ_BLOCK_REQUEST: 1407 case TCODE_READ_QUADLET_RESPONSE: 1408 case TCODE_READ_BLOCK_RESPONSE: 1409 case TCODE_LOCK_REQUEST: 1410 case TCODE_LOCK_RESPONSE: 1411 ohci1394_at_data_set_src_bus_id(header, false); 1412 ohci1394_at_data_set_speed(header, packet->speed); 1413 ohci1394_at_data_set_tlabel(header, async_header_get_tlabel(packet->header)); 1414 ohci1394_at_data_set_retry(header, async_header_get_retry(packet->header)); 1415 ohci1394_at_data_set_tcode(header, tcode); 1416 1417 ohci1394_at_data_set_destination_id(header, 1418 async_header_get_destination(packet->header)); 1419 1420 if (ctx == &ctx->ohci->at_response_ctx) { 1421 ohci1394_at_data_set_rcode(header, async_header_get_rcode(packet->header)); 1422 } else { 1423 ohci1394_at_data_set_destination_offset(header, 1424 async_header_get_offset(packet->header)); 1425 } 1426 1427 if (tcode_is_block_packet(tcode)) 1428 header[3] = cpu_to_le32(packet->header[3]); 1429 else 1430 header[3] = (__force __le32) packet->header[3]; 1431 1432 d[0].req_count = cpu_to_le16(packet->header_length); 1433 break; 1434 case TCODE_LINK_INTERNAL: 1435 ohci1394_at_data_set_speed(header, packet->speed); 1436 ohci1394_at_data_set_tcode(header, TCODE_LINK_INTERNAL); 1437 1438 header[1] = cpu_to_le32(packet->header[1]); 1439 header[2] = cpu_to_le32(packet->header[2]); 1440 d[0].req_count = cpu_to_le16(12); 1441 1442 if (is_ping_packet(&packet->header[1])) 1443 d[0].control |= cpu_to_le16(DESCRIPTOR_PING); 1444 break; 1445 1446 case TCODE_STREAM_DATA: 1447 ohci1394_it_data_set_speed(header, packet->speed); 1448 ohci1394_it_data_set_tag(header, isoc_header_get_tag(packet->header[0])); 1449 ohci1394_it_data_set_channel(header, isoc_header_get_channel(packet->header[0])); 1450 ohci1394_it_data_set_tcode(header, TCODE_STREAM_DATA); 1451 ohci1394_it_data_set_sync(header, isoc_header_get_sy(packet->header[0])); 1452 1453 ohci1394_it_data_set_data_length(header, isoc_header_get_data_length(packet->header[0])); 1454 1455 d[0].req_count = cpu_to_le16(8); 1456 break; 1457 1458 default: 1459 /* BUG(); */ 1460 packet->ack = RCODE_SEND_ERROR; 1461 return -1; 1462 } 1463 1464 BUILD_BUG_ON(sizeof(struct driver_data) > sizeof(struct descriptor)); 1465 driver_data = (struct driver_data *) &d[3]; 1466 driver_data->packet = packet; 1467 packet->driver_data = driver_data; 1468 1469 if (packet->payload_length > 0) { 1470 if (packet->payload_length > sizeof(driver_data->inline_data)) { 1471 payload_bus = dma_map_single(ohci->card.device, 1472 packet->payload, 1473 packet->payload_length, 1474 DMA_TO_DEVICE); 1475 if (dma_mapping_error(ohci->card.device, payload_bus)) { 1476 packet->ack = RCODE_SEND_ERROR; 1477 return -1; 1478 } 1479 packet->payload_bus = payload_bus; 1480 packet->payload_mapped = true; 1481 } else { 1482 memcpy(driver_data->inline_data, packet->payload, 1483 packet->payload_length); 1484 payload_bus = d_bus + 3 * sizeof(*d); 1485 } 1486 1487 d[2].req_count = cpu_to_le16(packet->payload_length); 1488 d[2].data_address = cpu_to_le32(payload_bus); 1489 last = &d[2]; 1490 z = 3; 1491 } else { 1492 last = &d[0]; 1493 z = 2; 1494 } 1495 1496 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST | 1497 DESCRIPTOR_IRQ_ALWAYS | 1498 DESCRIPTOR_BRANCH_ALWAYS); 1499 1500 /* FIXME: Document how the locking works. */ 1501 if (ohci->generation != packet->generation) { 1502 if (packet->payload_mapped) 1503 dma_unmap_single(ohci->card.device, payload_bus, 1504 packet->payload_length, DMA_TO_DEVICE); 1505 packet->ack = RCODE_GENERATION; 1506 return -1; 1507 } 1508 1509 context_append(ctx, d, z, 4 - z); 1510 1511 if (ctx->running) 1512 reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE); 1513 else 1514 context_run(ctx, 0); 1515 1516 return 0; 1517 } 1518 1519 static void at_context_flush(struct context *ctx) 1520 { 1521 tasklet_disable(&ctx->tasklet); 1522 1523 ctx->flushing = true; 1524 context_tasklet((unsigned long)ctx); 1525 ctx->flushing = false; 1526 1527 tasklet_enable(&ctx->tasklet); 1528 } 1529 1530 static int handle_at_packet(struct context *context, 1531 struct descriptor *d, 1532 struct descriptor *last) 1533 { 1534 struct driver_data *driver_data; 1535 struct fw_packet *packet; 1536 struct fw_ohci *ohci = context->ohci; 1537 int evt; 1538 1539 if (last->transfer_status == 0 && !context->flushing) 1540 /* This descriptor isn't done yet, stop iteration. */ 1541 return 0; 1542 1543 driver_data = (struct driver_data *) &d[3]; 1544 packet = driver_data->packet; 1545 if (packet == NULL) 1546 /* This packet was cancelled, just continue. */ 1547 return 1; 1548 1549 if (packet->payload_mapped) 1550 dma_unmap_single(ohci->card.device, packet->payload_bus, 1551 packet->payload_length, DMA_TO_DEVICE); 1552 1553 evt = le16_to_cpu(last->transfer_status) & 0x1f; 1554 packet->timestamp = le16_to_cpu(last->res_count); 1555 1556 log_ar_at_event(ohci, 'T', packet->speed, packet->header, evt); 1557 1558 switch (evt) { 1559 case OHCI1394_evt_timeout: 1560 /* Async response transmit timed out. */ 1561 packet->ack = RCODE_CANCELLED; 1562 break; 1563 1564 case OHCI1394_evt_flushed: 1565 /* 1566 * The packet was flushed should give same error as 1567 * when we try to use a stale generation count. 1568 */ 1569 packet->ack = RCODE_GENERATION; 1570 break; 1571 1572 case OHCI1394_evt_missing_ack: 1573 if (context->flushing) 1574 packet->ack = RCODE_GENERATION; 1575 else { 1576 /* 1577 * Using a valid (current) generation count, but the 1578 * node is not on the bus or not sending acks. 1579 */ 1580 packet->ack = RCODE_NO_ACK; 1581 } 1582 break; 1583 1584 case ACK_COMPLETE + 0x10: 1585 case ACK_PENDING + 0x10: 1586 case ACK_BUSY_X + 0x10: 1587 case ACK_BUSY_A + 0x10: 1588 case ACK_BUSY_B + 0x10: 1589 case ACK_DATA_ERROR + 0x10: 1590 case ACK_TYPE_ERROR + 0x10: 1591 packet->ack = evt - 0x10; 1592 break; 1593 1594 case OHCI1394_evt_no_status: 1595 if (context->flushing) { 1596 packet->ack = RCODE_GENERATION; 1597 break; 1598 } 1599 fallthrough; 1600 1601 default: 1602 packet->ack = RCODE_SEND_ERROR; 1603 break; 1604 } 1605 1606 packet->callback(packet, &ohci->card, packet->ack); 1607 1608 return 1; 1609 } 1610 1611 static u32 get_cycle_time(struct fw_ohci *ohci); 1612 1613 static void handle_local_rom(struct fw_ohci *ohci, 1614 struct fw_packet *packet, u32 csr) 1615 { 1616 struct fw_packet response; 1617 int tcode, length, i; 1618 1619 tcode = async_header_get_tcode(packet->header); 1620 if (tcode_is_block_packet(tcode)) 1621 length = async_header_get_data_length(packet->header); 1622 else 1623 length = 4; 1624 1625 i = csr - CSR_CONFIG_ROM; 1626 if (i + length > CONFIG_ROM_SIZE) { 1627 fw_fill_response(&response, packet->header, 1628 RCODE_ADDRESS_ERROR, NULL, 0); 1629 } else if (!tcode_is_read_request(tcode)) { 1630 fw_fill_response(&response, packet->header, 1631 RCODE_TYPE_ERROR, NULL, 0); 1632 } else { 1633 fw_fill_response(&response, packet->header, RCODE_COMPLETE, 1634 (void *) ohci->config_rom + i, length); 1635 } 1636 1637 // Timestamping on behalf of the hardware. 1638 response.timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ohci)); 1639 fw_core_handle_response(&ohci->card, &response); 1640 } 1641 1642 static void handle_local_lock(struct fw_ohci *ohci, 1643 struct fw_packet *packet, u32 csr) 1644 { 1645 struct fw_packet response; 1646 int tcode, length, ext_tcode, sel, try; 1647 __be32 *payload, lock_old; 1648 u32 lock_arg, lock_data; 1649 1650 tcode = async_header_get_tcode(packet->header); 1651 length = async_header_get_data_length(packet->header); 1652 payload = packet->payload; 1653 ext_tcode = async_header_get_extended_tcode(packet->header); 1654 1655 if (tcode == TCODE_LOCK_REQUEST && 1656 ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) { 1657 lock_arg = be32_to_cpu(payload[0]); 1658 lock_data = be32_to_cpu(payload[1]); 1659 } else if (tcode == TCODE_READ_QUADLET_REQUEST) { 1660 lock_arg = 0; 1661 lock_data = 0; 1662 } else { 1663 fw_fill_response(&response, packet->header, 1664 RCODE_TYPE_ERROR, NULL, 0); 1665 goto out; 1666 } 1667 1668 sel = (csr - CSR_BUS_MANAGER_ID) / 4; 1669 reg_write(ohci, OHCI1394_CSRData, lock_data); 1670 reg_write(ohci, OHCI1394_CSRCompareData, lock_arg); 1671 reg_write(ohci, OHCI1394_CSRControl, sel); 1672 1673 for (try = 0; try < 20; try++) 1674 if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000) { 1675 lock_old = cpu_to_be32(reg_read(ohci, 1676 OHCI1394_CSRData)); 1677 fw_fill_response(&response, packet->header, 1678 RCODE_COMPLETE, 1679 &lock_old, sizeof(lock_old)); 1680 goto out; 1681 } 1682 1683 ohci_err(ohci, "swap not done (CSR lock timeout)\n"); 1684 fw_fill_response(&response, packet->header, RCODE_BUSY, NULL, 0); 1685 1686 out: 1687 // Timestamping on behalf of the hardware. 1688 response.timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ohci)); 1689 fw_core_handle_response(&ohci->card, &response); 1690 } 1691 1692 static void handle_local_request(struct context *ctx, struct fw_packet *packet) 1693 { 1694 u64 offset, csr; 1695 1696 if (ctx == &ctx->ohci->at_request_ctx) { 1697 packet->ack = ACK_PENDING; 1698 packet->callback(packet, &ctx->ohci->card, packet->ack); 1699 } 1700 1701 offset = async_header_get_offset(packet->header); 1702 csr = offset - CSR_REGISTER_BASE; 1703 1704 /* Handle config rom reads. */ 1705 if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END) 1706 handle_local_rom(ctx->ohci, packet, csr); 1707 else switch (csr) { 1708 case CSR_BUS_MANAGER_ID: 1709 case CSR_BANDWIDTH_AVAILABLE: 1710 case CSR_CHANNELS_AVAILABLE_HI: 1711 case CSR_CHANNELS_AVAILABLE_LO: 1712 handle_local_lock(ctx->ohci, packet, csr); 1713 break; 1714 default: 1715 if (ctx == &ctx->ohci->at_request_ctx) 1716 fw_core_handle_request(&ctx->ohci->card, packet); 1717 else 1718 fw_core_handle_response(&ctx->ohci->card, packet); 1719 break; 1720 } 1721 1722 if (ctx == &ctx->ohci->at_response_ctx) { 1723 packet->ack = ACK_COMPLETE; 1724 packet->callback(packet, &ctx->ohci->card, packet->ack); 1725 } 1726 } 1727 1728 static void at_context_transmit(struct context *ctx, struct fw_packet *packet) 1729 { 1730 unsigned long flags; 1731 int ret; 1732 1733 spin_lock_irqsave(&ctx->ohci->lock, flags); 1734 1735 if (async_header_get_destination(packet->header) == ctx->ohci->node_id && 1736 ctx->ohci->generation == packet->generation) { 1737 spin_unlock_irqrestore(&ctx->ohci->lock, flags); 1738 1739 // Timestamping on behalf of the hardware. 1740 packet->timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ctx->ohci)); 1741 1742 handle_local_request(ctx, packet); 1743 return; 1744 } 1745 1746 ret = at_context_queue_packet(ctx, packet); 1747 spin_unlock_irqrestore(&ctx->ohci->lock, flags); 1748 1749 if (ret < 0) { 1750 // Timestamping on behalf of the hardware. 1751 packet->timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ctx->ohci)); 1752 1753 packet->callback(packet, &ctx->ohci->card, packet->ack); 1754 } 1755 } 1756 1757 static void detect_dead_context(struct fw_ohci *ohci, 1758 const char *name, unsigned int regs) 1759 { 1760 u32 ctl; 1761 1762 ctl = reg_read(ohci, CONTROL_SET(regs)); 1763 if (ctl & CONTEXT_DEAD) 1764 ohci_err(ohci, "DMA context %s has stopped, error code: %s\n", 1765 name, evts[ctl & 0x1f]); 1766 } 1767 1768 static void handle_dead_contexts(struct fw_ohci *ohci) 1769 { 1770 unsigned int i; 1771 char name[8]; 1772 1773 detect_dead_context(ohci, "ATReq", OHCI1394_AsReqTrContextBase); 1774 detect_dead_context(ohci, "ATRsp", OHCI1394_AsRspTrContextBase); 1775 detect_dead_context(ohci, "ARReq", OHCI1394_AsReqRcvContextBase); 1776 detect_dead_context(ohci, "ARRsp", OHCI1394_AsRspRcvContextBase); 1777 for (i = 0; i < 32; ++i) { 1778 if (!(ohci->it_context_support & (1 << i))) 1779 continue; 1780 sprintf(name, "IT%u", i); 1781 detect_dead_context(ohci, name, OHCI1394_IsoXmitContextBase(i)); 1782 } 1783 for (i = 0; i < 32; ++i) { 1784 if (!(ohci->ir_context_support & (1 << i))) 1785 continue; 1786 sprintf(name, "IR%u", i); 1787 detect_dead_context(ohci, name, OHCI1394_IsoRcvContextBase(i)); 1788 } 1789 /* TODO: maybe try to flush and restart the dead contexts */ 1790 } 1791 1792 static u32 cycle_timer_ticks(u32 cycle_timer) 1793 { 1794 u32 ticks; 1795 1796 ticks = cycle_timer & 0xfff; 1797 ticks += 3072 * ((cycle_timer >> 12) & 0x1fff); 1798 ticks += (3072 * 8000) * (cycle_timer >> 25); 1799 1800 return ticks; 1801 } 1802 1803 /* 1804 * Some controllers exhibit one or more of the following bugs when updating the 1805 * iso cycle timer register: 1806 * - When the lowest six bits are wrapping around to zero, a read that happens 1807 * at the same time will return garbage in the lowest ten bits. 1808 * - When the cycleOffset field wraps around to zero, the cycleCount field is 1809 * not incremented for about 60 ns. 1810 * - Occasionally, the entire register reads zero. 1811 * 1812 * To catch these, we read the register three times and ensure that the 1813 * difference between each two consecutive reads is approximately the same, i.e. 1814 * less than twice the other. Furthermore, any negative difference indicates an 1815 * error. (A PCI read should take at least 20 ticks of the 24.576 MHz timer to 1816 * execute, so we have enough precision to compute the ratio of the differences.) 1817 */ 1818 static u32 get_cycle_time(struct fw_ohci *ohci) 1819 { 1820 u32 c0, c1, c2; 1821 u32 t0, t1, t2; 1822 s32 diff01, diff12; 1823 int i; 1824 1825 if (has_reboot_by_cycle_timer_read_quirk(ohci)) 1826 return 0; 1827 1828 c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer); 1829 1830 if (ohci->quirks & QUIRK_CYCLE_TIMER) { 1831 i = 0; 1832 c1 = c2; 1833 c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer); 1834 do { 1835 c0 = c1; 1836 c1 = c2; 1837 c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer); 1838 t0 = cycle_timer_ticks(c0); 1839 t1 = cycle_timer_ticks(c1); 1840 t2 = cycle_timer_ticks(c2); 1841 diff01 = t1 - t0; 1842 diff12 = t2 - t1; 1843 } while ((diff01 <= 0 || diff12 <= 0 || 1844 diff01 / diff12 >= 2 || diff12 / diff01 >= 2) 1845 && i++ < 20); 1846 } 1847 1848 return c2; 1849 } 1850 1851 /* 1852 * This function has to be called at least every 64 seconds. The bus_time 1853 * field stores not only the upper 25 bits of the BUS_TIME register but also 1854 * the most significant bit of the cycle timer in bit 6 so that we can detect 1855 * changes in this bit. 1856 */ 1857 static u32 update_bus_time(struct fw_ohci *ohci) 1858 { 1859 u32 cycle_time_seconds = get_cycle_time(ohci) >> 25; 1860 1861 if (unlikely(!ohci->bus_time_running)) { 1862 reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_cycle64Seconds); 1863 ohci->bus_time = (lower_32_bits(ktime_get_seconds()) & ~0x7f) | 1864 (cycle_time_seconds & 0x40); 1865 ohci->bus_time_running = true; 1866 } 1867 1868 if ((ohci->bus_time & 0x40) != (cycle_time_seconds & 0x40)) 1869 ohci->bus_time += 0x40; 1870 1871 return ohci->bus_time | cycle_time_seconds; 1872 } 1873 1874 static int get_status_for_port(struct fw_ohci *ohci, int port_index, 1875 enum phy_packet_self_id_port_status *status) 1876 { 1877 int reg; 1878 1879 scoped_guard(mutex, &ohci->phy_reg_mutex) { 1880 reg = write_phy_reg(ohci, 7, port_index); 1881 if (reg < 0) 1882 return reg; 1883 1884 reg = read_phy_reg(ohci, 8); 1885 if (reg < 0) 1886 return reg; 1887 } 1888 1889 switch (reg & 0x0f) { 1890 case 0x06: 1891 // is child node (connected to parent node) 1892 *status = PHY_PACKET_SELF_ID_PORT_STATUS_PARENT; 1893 break; 1894 case 0x0e: 1895 // is parent node (connected to child node) 1896 *status = PHY_PACKET_SELF_ID_PORT_STATUS_CHILD; 1897 break; 1898 default: 1899 // not connected 1900 *status = PHY_PACKET_SELF_ID_PORT_STATUS_NCONN; 1901 break; 1902 } 1903 1904 return 0; 1905 } 1906 1907 static int get_self_id_pos(struct fw_ohci *ohci, u32 self_id, 1908 int self_id_count) 1909 { 1910 unsigned int left_phy_id = phy_packet_self_id_get_phy_id(self_id); 1911 int i; 1912 1913 for (i = 0; i < self_id_count; i++) { 1914 u32 entry = ohci->self_id_buffer[i]; 1915 unsigned int right_phy_id = phy_packet_self_id_get_phy_id(entry); 1916 1917 if (left_phy_id == right_phy_id) 1918 return -1; 1919 if (left_phy_id < right_phy_id) 1920 return i; 1921 } 1922 return i; 1923 } 1924 1925 static bool initiated_reset(struct fw_ohci *ohci) 1926 { 1927 int reg; 1928 1929 guard(mutex)(&ohci->phy_reg_mutex); 1930 1931 // Select page 7 1932 reg = write_phy_reg(ohci, 7, 0xe0); 1933 if (reg < 0) 1934 return reg; 1935 1936 reg = read_phy_reg(ohci, 8); 1937 if (reg < 0) 1938 return reg; 1939 1940 // set PMODE bit 1941 reg |= 0x40; 1942 reg = write_phy_reg(ohci, 8, reg); 1943 if (reg < 0) 1944 return reg; 1945 1946 // read register 12 1947 reg = read_phy_reg(ohci, 12); 1948 if (reg < 0) 1949 return reg; 1950 1951 // bit 3 indicates "initiated reset" 1952 return !!((reg & 0x08) == 0x08); 1953 } 1954 1955 /* 1956 * TI TSB82AA2B and TSB12LV26 do not receive the selfID of a locally 1957 * attached TSB41BA3D phy; see http://www.ti.com/litv/pdf/sllz059. 1958 * Construct the selfID from phy register contents. 1959 */ 1960 static int find_and_insert_self_id(struct fw_ohci *ohci, int self_id_count) 1961 { 1962 int reg, i, pos; 1963 u32 self_id = 0; 1964 1965 // link active 1, speed 3, bridge 0, contender 1, more packets 0. 1966 phy_packet_set_packet_identifier(&self_id, PHY_PACKET_PACKET_IDENTIFIER_SELF_ID); 1967 phy_packet_self_id_zero_set_link_active(&self_id, true); 1968 phy_packet_self_id_zero_set_scode(&self_id, SCODE_800); 1969 phy_packet_self_id_zero_set_contender(&self_id, true); 1970 1971 reg = reg_read(ohci, OHCI1394_NodeID); 1972 if (!(reg & OHCI1394_NodeID_idValid)) { 1973 ohci_notice(ohci, 1974 "node ID not valid, new bus reset in progress\n"); 1975 return -EBUSY; 1976 } 1977 phy_packet_self_id_set_phy_id(&self_id, reg & 0x3f); 1978 1979 reg = ohci_read_phy_reg(&ohci->card, 4); 1980 if (reg < 0) 1981 return reg; 1982 phy_packet_self_id_zero_set_power_class(&self_id, reg & 0x07); 1983 1984 reg = ohci_read_phy_reg(&ohci->card, 1); 1985 if (reg < 0) 1986 return reg; 1987 phy_packet_self_id_zero_set_gap_count(&self_id, reg & 0x3f); 1988 1989 for (i = 0; i < 3; i++) { 1990 enum phy_packet_self_id_port_status status; 1991 int err; 1992 1993 err = get_status_for_port(ohci, i, &status); 1994 if (err < 0) 1995 return err; 1996 1997 self_id_sequence_set_port_status(&self_id, 1, i, status); 1998 } 1999 2000 phy_packet_self_id_zero_set_initiated_reset(&self_id, initiated_reset(ohci)); 2001 2002 pos = get_self_id_pos(ohci, self_id, self_id_count); 2003 if (pos >= 0) { 2004 memmove(&(ohci->self_id_buffer[pos+1]), 2005 &(ohci->self_id_buffer[pos]), 2006 (self_id_count - pos) * sizeof(*ohci->self_id_buffer)); 2007 ohci->self_id_buffer[pos] = self_id; 2008 self_id_count++; 2009 } 2010 return self_id_count; 2011 } 2012 2013 static void bus_reset_work(struct work_struct *work) 2014 { 2015 struct fw_ohci *ohci = 2016 container_of(work, struct fw_ohci, bus_reset_work); 2017 int self_id_count, generation, new_generation, i, j; 2018 u32 reg, quadlet; 2019 void *free_rom = NULL; 2020 dma_addr_t free_rom_bus = 0; 2021 bool is_new_root; 2022 2023 reg = reg_read(ohci, OHCI1394_NodeID); 2024 if (!(reg & OHCI1394_NodeID_idValid)) { 2025 ohci_notice(ohci, 2026 "node ID not valid, new bus reset in progress\n"); 2027 return; 2028 } 2029 if ((reg & OHCI1394_NodeID_nodeNumber) == 63) { 2030 ohci_notice(ohci, "malconfigured bus\n"); 2031 return; 2032 } 2033 ohci->node_id = reg & (OHCI1394_NodeID_busNumber | 2034 OHCI1394_NodeID_nodeNumber); 2035 2036 is_new_root = (reg & OHCI1394_NodeID_root) != 0; 2037 if (!(ohci->is_root && is_new_root)) 2038 reg_write(ohci, OHCI1394_LinkControlSet, 2039 OHCI1394_LinkControl_cycleMaster); 2040 ohci->is_root = is_new_root; 2041 2042 reg = reg_read(ohci, OHCI1394_SelfIDCount); 2043 if (ohci1394_self_id_count_is_error(reg)) { 2044 ohci_notice(ohci, "self ID receive error\n"); 2045 return; 2046 } 2047 /* 2048 * The count in the SelfIDCount register is the number of 2049 * bytes in the self ID receive buffer. Since we also receive 2050 * the inverted quadlets and a header quadlet, we shift one 2051 * bit extra to get the actual number of self IDs. 2052 */ 2053 self_id_count = ohci1394_self_id_count_get_size(reg) >> 1; 2054 2055 if (self_id_count > 252) { 2056 ohci_notice(ohci, "bad selfIDSize (%08x)\n", reg); 2057 return; 2058 } 2059 2060 quadlet = cond_le32_to_cpu(ohci->self_id[0], has_be_header_quirk(ohci)); 2061 generation = ohci1394_self_id_receive_q0_get_generation(quadlet); 2062 rmb(); 2063 2064 for (i = 1, j = 0; j < self_id_count; i += 2, j++) { 2065 u32 id = cond_le32_to_cpu(ohci->self_id[i], has_be_header_quirk(ohci)); 2066 u32 id2 = cond_le32_to_cpu(ohci->self_id[i + 1], has_be_header_quirk(ohci)); 2067 2068 if (id != ~id2) { 2069 /* 2070 * If the invalid data looks like a cycle start packet, 2071 * it's likely to be the result of the cycle master 2072 * having a wrong gap count. In this case, the self IDs 2073 * so far are valid and should be processed so that the 2074 * bus manager can then correct the gap count. 2075 */ 2076 if (id == 0xffff008f) { 2077 ohci_notice(ohci, "ignoring spurious self IDs\n"); 2078 self_id_count = j; 2079 break; 2080 } 2081 2082 ohci_notice(ohci, "bad self ID %d/%d (%08x != ~%08x)\n", 2083 j, self_id_count, id, id2); 2084 return; 2085 } 2086 ohci->self_id_buffer[j] = id; 2087 } 2088 2089 if (ohci->quirks & QUIRK_TI_SLLZ059) { 2090 self_id_count = find_and_insert_self_id(ohci, self_id_count); 2091 if (self_id_count < 0) { 2092 ohci_notice(ohci, 2093 "could not construct local self ID\n"); 2094 return; 2095 } 2096 } 2097 2098 if (self_id_count == 0) { 2099 ohci_notice(ohci, "no self IDs\n"); 2100 return; 2101 } 2102 rmb(); 2103 2104 /* 2105 * Check the consistency of the self IDs we just read. The 2106 * problem we face is that a new bus reset can start while we 2107 * read out the self IDs from the DMA buffer. If this happens, 2108 * the DMA buffer will be overwritten with new self IDs and we 2109 * will read out inconsistent data. The OHCI specification 2110 * (section 11.2) recommends a technique similar to 2111 * linux/seqlock.h, where we remember the generation of the 2112 * self IDs in the buffer before reading them out and compare 2113 * it to the current generation after reading them out. If 2114 * the two generations match we know we have a consistent set 2115 * of self IDs. 2116 */ 2117 2118 reg = reg_read(ohci, OHCI1394_SelfIDCount); 2119 new_generation = ohci1394_self_id_count_get_generation(reg); 2120 if (new_generation != generation) { 2121 ohci_notice(ohci, "new bus reset, discarding self ids\n"); 2122 return; 2123 } 2124 2125 /* FIXME: Document how the locking works. */ 2126 spin_lock_irq(&ohci->lock); 2127 2128 ohci->generation = -1; /* prevent AT packet queueing */ 2129 context_stop(&ohci->at_request_ctx); 2130 context_stop(&ohci->at_response_ctx); 2131 2132 spin_unlock_irq(&ohci->lock); 2133 2134 /* 2135 * Per OHCI 1.2 draft, clause 7.2.3.3, hardware may leave unsent 2136 * packets in the AT queues and software needs to drain them. 2137 * Some OHCI 1.1 controllers (JMicron) apparently require this too. 2138 */ 2139 at_context_flush(&ohci->at_request_ctx); 2140 at_context_flush(&ohci->at_response_ctx); 2141 2142 spin_lock_irq(&ohci->lock); 2143 2144 ohci->generation = generation; 2145 reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset); 2146 reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_busReset); 2147 2148 if (ohci->quirks & QUIRK_RESET_PACKET) 2149 ohci->request_generation = generation; 2150 2151 /* 2152 * This next bit is unrelated to the AT context stuff but we 2153 * have to do it under the spinlock also. If a new config rom 2154 * was set up before this reset, the old one is now no longer 2155 * in use and we can free it. Update the config rom pointers 2156 * to point to the current config rom and clear the 2157 * next_config_rom pointer so a new update can take place. 2158 */ 2159 2160 if (ohci->next_config_rom != NULL) { 2161 if (ohci->next_config_rom != ohci->config_rom) { 2162 free_rom = ohci->config_rom; 2163 free_rom_bus = ohci->config_rom_bus; 2164 } 2165 ohci->config_rom = ohci->next_config_rom; 2166 ohci->config_rom_bus = ohci->next_config_rom_bus; 2167 ohci->next_config_rom = NULL; 2168 2169 /* 2170 * Restore config_rom image and manually update 2171 * config_rom registers. Writing the header quadlet 2172 * will indicate that the config rom is ready, so we 2173 * do that last. 2174 */ 2175 reg_write(ohci, OHCI1394_BusOptions, 2176 be32_to_cpu(ohci->config_rom[2])); 2177 ohci->config_rom[0] = ohci->next_header; 2178 reg_write(ohci, OHCI1394_ConfigROMhdr, 2179 be32_to_cpu(ohci->next_header)); 2180 } 2181 2182 if (param_remote_dma) { 2183 reg_write(ohci, OHCI1394_PhyReqFilterHiSet, ~0); 2184 reg_write(ohci, OHCI1394_PhyReqFilterLoSet, ~0); 2185 } 2186 2187 spin_unlock_irq(&ohci->lock); 2188 2189 if (free_rom) 2190 dmam_free_coherent(ohci->card.device, CONFIG_ROM_SIZE, free_rom, free_rom_bus); 2191 2192 log_selfids(ohci, generation, self_id_count); 2193 2194 fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation, 2195 self_id_count, ohci->self_id_buffer, 2196 ohci->csr_state_setclear_abdicate); 2197 ohci->csr_state_setclear_abdicate = false; 2198 } 2199 2200 static irqreturn_t irq_handler(int irq, void *data) 2201 { 2202 struct fw_ohci *ohci = data; 2203 u32 event, iso_event; 2204 int i; 2205 2206 event = reg_read(ohci, OHCI1394_IntEventClear); 2207 2208 if (!event || !~event) 2209 return IRQ_NONE; 2210 2211 /* 2212 * busReset and postedWriteErr events must not be cleared yet 2213 * (OHCI 1.1 clauses 7.2.3.2 and 13.2.8.1) 2214 */ 2215 reg_write(ohci, OHCI1394_IntEventClear, 2216 event & ~(OHCI1394_busReset | OHCI1394_postedWriteErr)); 2217 trace_irqs(ohci->card.index, event); 2218 log_irqs(ohci, event); 2219 // The flag is masked again at bus_reset_work() scheduled by selfID event. 2220 if (event & OHCI1394_busReset) 2221 reg_write(ohci, OHCI1394_IntMaskClear, OHCI1394_busReset); 2222 2223 if (event & OHCI1394_selfIDComplete) { 2224 if (trace_self_id_complete_enabled()) { 2225 u32 reg = reg_read(ohci, OHCI1394_SelfIDCount); 2226 2227 trace_self_id_complete(ohci->card.index, reg, ohci->self_id, 2228 has_be_header_quirk(ohci)); 2229 } 2230 queue_work(selfid_workqueue, &ohci->bus_reset_work); 2231 } 2232 2233 if (event & OHCI1394_RQPkt) 2234 tasklet_schedule(&ohci->ar_request_ctx.tasklet); 2235 2236 if (event & OHCI1394_RSPkt) 2237 tasklet_schedule(&ohci->ar_response_ctx.tasklet); 2238 2239 if (event & OHCI1394_reqTxComplete) 2240 tasklet_schedule(&ohci->at_request_ctx.tasklet); 2241 2242 if (event & OHCI1394_respTxComplete) 2243 tasklet_schedule(&ohci->at_response_ctx.tasklet); 2244 2245 if (event & OHCI1394_isochRx) { 2246 iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear); 2247 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event); 2248 2249 while (iso_event) { 2250 i = ffs(iso_event) - 1; 2251 tasklet_schedule( 2252 &ohci->ir_context_list[i].context.tasklet); 2253 iso_event &= ~(1 << i); 2254 } 2255 } 2256 2257 if (event & OHCI1394_isochTx) { 2258 iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear); 2259 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event); 2260 2261 while (iso_event) { 2262 i = ffs(iso_event) - 1; 2263 tasklet_schedule( 2264 &ohci->it_context_list[i].context.tasklet); 2265 iso_event &= ~(1 << i); 2266 } 2267 } 2268 2269 if (unlikely(event & OHCI1394_regAccessFail)) 2270 ohci_err(ohci, "register access failure\n"); 2271 2272 if (unlikely(event & OHCI1394_postedWriteErr)) { 2273 reg_read(ohci, OHCI1394_PostedWriteAddressHi); 2274 reg_read(ohci, OHCI1394_PostedWriteAddressLo); 2275 reg_write(ohci, OHCI1394_IntEventClear, 2276 OHCI1394_postedWriteErr); 2277 if (printk_ratelimit()) 2278 ohci_err(ohci, "PCI posted write error\n"); 2279 } 2280 2281 if (unlikely(event & OHCI1394_cycleTooLong)) { 2282 if (printk_ratelimit()) 2283 ohci_notice(ohci, "isochronous cycle too long\n"); 2284 reg_write(ohci, OHCI1394_LinkControlSet, 2285 OHCI1394_LinkControl_cycleMaster); 2286 } 2287 2288 if (unlikely(event & OHCI1394_cycleInconsistent)) { 2289 /* 2290 * We need to clear this event bit in order to make 2291 * cycleMatch isochronous I/O work. In theory we should 2292 * stop active cycleMatch iso contexts now and restart 2293 * them at least two cycles later. (FIXME?) 2294 */ 2295 if (printk_ratelimit()) 2296 ohci_notice(ohci, "isochronous cycle inconsistent\n"); 2297 } 2298 2299 if (unlikely(event & OHCI1394_unrecoverableError)) 2300 handle_dead_contexts(ohci); 2301 2302 if (event & OHCI1394_cycle64Seconds) { 2303 spin_lock(&ohci->lock); 2304 update_bus_time(ohci); 2305 spin_unlock(&ohci->lock); 2306 } else 2307 flush_writes(ohci); 2308 2309 return IRQ_HANDLED; 2310 } 2311 2312 static int software_reset(struct fw_ohci *ohci) 2313 { 2314 u32 val; 2315 int i; 2316 2317 reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset); 2318 for (i = 0; i < 500; i++) { 2319 val = reg_read(ohci, OHCI1394_HCControlSet); 2320 if (!~val) 2321 return -ENODEV; /* Card was ejected. */ 2322 2323 if (!(val & OHCI1394_HCControl_softReset)) 2324 return 0; 2325 2326 msleep(1); 2327 } 2328 2329 return -EBUSY; 2330 } 2331 2332 static void copy_config_rom(__be32 *dest, const __be32 *src, size_t length) 2333 { 2334 size_t size = length * 4; 2335 2336 memcpy(dest, src, size); 2337 if (size < CONFIG_ROM_SIZE) 2338 memset(&dest[length], 0, CONFIG_ROM_SIZE - size); 2339 } 2340 2341 static int configure_1394a_enhancements(struct fw_ohci *ohci) 2342 { 2343 bool enable_1394a; 2344 int ret, clear, set, offset; 2345 2346 /* Check if the driver should configure link and PHY. */ 2347 if (!(reg_read(ohci, OHCI1394_HCControlSet) & 2348 OHCI1394_HCControl_programPhyEnable)) 2349 return 0; 2350 2351 /* Paranoia: check whether the PHY supports 1394a, too. */ 2352 enable_1394a = false; 2353 ret = read_phy_reg(ohci, 2); 2354 if (ret < 0) 2355 return ret; 2356 if ((ret & PHY_EXTENDED_REGISTERS) == PHY_EXTENDED_REGISTERS) { 2357 ret = read_paged_phy_reg(ohci, 1, 8); 2358 if (ret < 0) 2359 return ret; 2360 if (ret >= 1) 2361 enable_1394a = true; 2362 } 2363 2364 if (ohci->quirks & QUIRK_NO_1394A) 2365 enable_1394a = false; 2366 2367 /* Configure PHY and link consistently. */ 2368 if (enable_1394a) { 2369 clear = 0; 2370 set = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI; 2371 } else { 2372 clear = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI; 2373 set = 0; 2374 } 2375 ret = update_phy_reg(ohci, 5, clear, set); 2376 if (ret < 0) 2377 return ret; 2378 2379 if (enable_1394a) 2380 offset = OHCI1394_HCControlSet; 2381 else 2382 offset = OHCI1394_HCControlClear; 2383 reg_write(ohci, offset, OHCI1394_HCControl_aPhyEnhanceEnable); 2384 2385 /* Clean up: configuration has been taken care of. */ 2386 reg_write(ohci, OHCI1394_HCControlClear, 2387 OHCI1394_HCControl_programPhyEnable); 2388 2389 return 0; 2390 } 2391 2392 static int probe_tsb41ba3d(struct fw_ohci *ohci) 2393 { 2394 /* TI vendor ID = 0x080028, TSB41BA3D product ID = 0x833005 (sic) */ 2395 static const u8 id[] = { 0x08, 0x00, 0x28, 0x83, 0x30, 0x05, }; 2396 int reg, i; 2397 2398 reg = read_phy_reg(ohci, 2); 2399 if (reg < 0) 2400 return reg; 2401 if ((reg & PHY_EXTENDED_REGISTERS) != PHY_EXTENDED_REGISTERS) 2402 return 0; 2403 2404 for (i = ARRAY_SIZE(id) - 1; i >= 0; i--) { 2405 reg = read_paged_phy_reg(ohci, 1, i + 10); 2406 if (reg < 0) 2407 return reg; 2408 if (reg != id[i]) 2409 return 0; 2410 } 2411 return 1; 2412 } 2413 2414 static int ohci_enable(struct fw_card *card, 2415 const __be32 *config_rom, size_t length) 2416 { 2417 struct fw_ohci *ohci = fw_ohci(card); 2418 u32 lps, version, irqs; 2419 int i, ret; 2420 2421 ret = software_reset(ohci); 2422 if (ret < 0) { 2423 ohci_err(ohci, "failed to reset ohci card\n"); 2424 return ret; 2425 } 2426 2427 /* 2428 * Now enable LPS, which we need in order to start accessing 2429 * most of the registers. In fact, on some cards (ALI M5251), 2430 * accessing registers in the SClk domain without LPS enabled 2431 * will lock up the machine. Wait 50msec to make sure we have 2432 * full link enabled. However, with some cards (well, at least 2433 * a JMicron PCIe card), we have to try again sometimes. 2434 * 2435 * TI TSB82AA2 + TSB81BA3(A) cards signal LPS enabled early but 2436 * cannot actually use the phy at that time. These need tens of 2437 * millisecods pause between LPS write and first phy access too. 2438 */ 2439 2440 reg_write(ohci, OHCI1394_HCControlSet, 2441 OHCI1394_HCControl_LPS | 2442 OHCI1394_HCControl_postedWriteEnable); 2443 flush_writes(ohci); 2444 2445 for (lps = 0, i = 0; !lps && i < 3; i++) { 2446 msleep(50); 2447 lps = reg_read(ohci, OHCI1394_HCControlSet) & 2448 OHCI1394_HCControl_LPS; 2449 } 2450 2451 if (!lps) { 2452 ohci_err(ohci, "failed to set Link Power Status\n"); 2453 return -EIO; 2454 } 2455 2456 if (ohci->quirks & QUIRK_TI_SLLZ059) { 2457 ret = probe_tsb41ba3d(ohci); 2458 if (ret < 0) 2459 return ret; 2460 if (ret) 2461 ohci_notice(ohci, "local TSB41BA3D phy\n"); 2462 else 2463 ohci->quirks &= ~QUIRK_TI_SLLZ059; 2464 } 2465 2466 reg_write(ohci, OHCI1394_HCControlClear, 2467 OHCI1394_HCControl_noByteSwapData); 2468 2469 reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus); 2470 reg_write(ohci, OHCI1394_LinkControlSet, 2471 OHCI1394_LinkControl_cycleTimerEnable | 2472 OHCI1394_LinkControl_cycleMaster); 2473 2474 reg_write(ohci, OHCI1394_ATRetries, 2475 OHCI1394_MAX_AT_REQ_RETRIES | 2476 (OHCI1394_MAX_AT_RESP_RETRIES << 4) | 2477 (OHCI1394_MAX_PHYS_RESP_RETRIES << 8) | 2478 (200 << 16)); 2479 2480 ohci->bus_time_running = false; 2481 2482 for (i = 0; i < 32; i++) 2483 if (ohci->ir_context_support & (1 << i)) 2484 reg_write(ohci, OHCI1394_IsoRcvContextControlClear(i), 2485 IR_CONTEXT_MULTI_CHANNEL_MODE); 2486 2487 version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff; 2488 if (version >= OHCI_VERSION_1_1) { 2489 reg_write(ohci, OHCI1394_InitialChannelsAvailableHi, 2490 0xfffffffe); 2491 card->broadcast_channel_auto_allocated = true; 2492 } 2493 2494 /* Get implemented bits of the priority arbitration request counter. */ 2495 reg_write(ohci, OHCI1394_FairnessControl, 0x3f); 2496 ohci->pri_req_max = reg_read(ohci, OHCI1394_FairnessControl) & 0x3f; 2497 reg_write(ohci, OHCI1394_FairnessControl, 0); 2498 card->priority_budget_implemented = ohci->pri_req_max != 0; 2499 2500 reg_write(ohci, OHCI1394_PhyUpperBound, FW_MAX_PHYSICAL_RANGE >> 16); 2501 reg_write(ohci, OHCI1394_IntEventClear, ~0); 2502 reg_write(ohci, OHCI1394_IntMaskClear, ~0); 2503 2504 ret = configure_1394a_enhancements(ohci); 2505 if (ret < 0) 2506 return ret; 2507 2508 /* Activate link_on bit and contender bit in our self ID packets.*/ 2509 ret = ohci_update_phy_reg(card, 4, 0, PHY_LINK_ACTIVE | PHY_CONTENDER); 2510 if (ret < 0) 2511 return ret; 2512 2513 /* 2514 * When the link is not yet enabled, the atomic config rom 2515 * update mechanism described below in ohci_set_config_rom() 2516 * is not active. We have to update ConfigRomHeader and 2517 * BusOptions manually, and the write to ConfigROMmap takes 2518 * effect immediately. We tie this to the enabling of the 2519 * link, so we have a valid config rom before enabling - the 2520 * OHCI requires that ConfigROMhdr and BusOptions have valid 2521 * values before enabling. 2522 * 2523 * However, when the ConfigROMmap is written, some controllers 2524 * always read back quadlets 0 and 2 from the config rom to 2525 * the ConfigRomHeader and BusOptions registers on bus reset. 2526 * They shouldn't do that in this initial case where the link 2527 * isn't enabled. This means we have to use the same 2528 * workaround here, setting the bus header to 0 and then write 2529 * the right values in the bus reset tasklet. 2530 */ 2531 2532 if (config_rom) { 2533 ohci->next_config_rom = dmam_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE, 2534 &ohci->next_config_rom_bus, GFP_KERNEL); 2535 if (ohci->next_config_rom == NULL) 2536 return -ENOMEM; 2537 2538 copy_config_rom(ohci->next_config_rom, config_rom, length); 2539 } else { 2540 /* 2541 * In the suspend case, config_rom is NULL, which 2542 * means that we just reuse the old config rom. 2543 */ 2544 ohci->next_config_rom = ohci->config_rom; 2545 ohci->next_config_rom_bus = ohci->config_rom_bus; 2546 } 2547 2548 ohci->next_header = ohci->next_config_rom[0]; 2549 ohci->next_config_rom[0] = 0; 2550 reg_write(ohci, OHCI1394_ConfigROMhdr, 0); 2551 reg_write(ohci, OHCI1394_BusOptions, 2552 be32_to_cpu(ohci->next_config_rom[2])); 2553 reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus); 2554 2555 reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000); 2556 2557 irqs = OHCI1394_reqTxComplete | OHCI1394_respTxComplete | 2558 OHCI1394_RQPkt | OHCI1394_RSPkt | 2559 OHCI1394_isochTx | OHCI1394_isochRx | 2560 OHCI1394_postedWriteErr | 2561 OHCI1394_selfIDComplete | 2562 OHCI1394_regAccessFail | 2563 OHCI1394_cycleInconsistent | 2564 OHCI1394_unrecoverableError | 2565 OHCI1394_cycleTooLong | 2566 OHCI1394_masterIntEnable | 2567 OHCI1394_busReset; 2568 reg_write(ohci, OHCI1394_IntMaskSet, irqs); 2569 2570 reg_write(ohci, OHCI1394_HCControlSet, 2571 OHCI1394_HCControl_linkEnable | 2572 OHCI1394_HCControl_BIBimageValid); 2573 2574 reg_write(ohci, OHCI1394_LinkControlSet, 2575 OHCI1394_LinkControl_rcvSelfID | 2576 OHCI1394_LinkControl_rcvPhyPkt); 2577 2578 ar_context_run(&ohci->ar_request_ctx); 2579 ar_context_run(&ohci->ar_response_ctx); 2580 2581 flush_writes(ohci); 2582 2583 /* We are ready to go, reset bus to finish initialization. */ 2584 fw_schedule_bus_reset(&ohci->card, false, true); 2585 2586 return 0; 2587 } 2588 2589 static int ohci_set_config_rom(struct fw_card *card, 2590 const __be32 *config_rom, size_t length) 2591 { 2592 struct fw_ohci *ohci; 2593 __be32 *next_config_rom; 2594 dma_addr_t next_config_rom_bus; 2595 2596 ohci = fw_ohci(card); 2597 2598 /* 2599 * When the OHCI controller is enabled, the config rom update 2600 * mechanism is a bit tricky, but easy enough to use. See 2601 * section 5.5.6 in the OHCI specification. 2602 * 2603 * The OHCI controller caches the new config rom address in a 2604 * shadow register (ConfigROMmapNext) and needs a bus reset 2605 * for the changes to take place. When the bus reset is 2606 * detected, the controller loads the new values for the 2607 * ConfigRomHeader and BusOptions registers from the specified 2608 * config rom and loads ConfigROMmap from the ConfigROMmapNext 2609 * shadow register. All automatically and atomically. 2610 * 2611 * Now, there's a twist to this story. The automatic load of 2612 * ConfigRomHeader and BusOptions doesn't honor the 2613 * noByteSwapData bit, so with a be32 config rom, the 2614 * controller will load be32 values in to these registers 2615 * during the atomic update, even on litte endian 2616 * architectures. The workaround we use is to put a 0 in the 2617 * header quadlet; 0 is endian agnostic and means that the 2618 * config rom isn't ready yet. In the bus reset tasklet we 2619 * then set up the real values for the two registers. 2620 * 2621 * We use ohci->lock to avoid racing with the code that sets 2622 * ohci->next_config_rom to NULL (see bus_reset_work). 2623 */ 2624 2625 next_config_rom = dmam_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE, 2626 &next_config_rom_bus, GFP_KERNEL); 2627 if (next_config_rom == NULL) 2628 return -ENOMEM; 2629 2630 spin_lock_irq(&ohci->lock); 2631 2632 /* 2633 * If there is not an already pending config_rom update, 2634 * push our new allocation into the ohci->next_config_rom 2635 * and then mark the local variable as null so that we 2636 * won't deallocate the new buffer. 2637 * 2638 * OTOH, if there is a pending config_rom update, just 2639 * use that buffer with the new config_rom data, and 2640 * let this routine free the unused DMA allocation. 2641 */ 2642 2643 if (ohci->next_config_rom == NULL) { 2644 ohci->next_config_rom = next_config_rom; 2645 ohci->next_config_rom_bus = next_config_rom_bus; 2646 next_config_rom = NULL; 2647 } 2648 2649 copy_config_rom(ohci->next_config_rom, config_rom, length); 2650 2651 ohci->next_header = config_rom[0]; 2652 ohci->next_config_rom[0] = 0; 2653 2654 reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus); 2655 2656 spin_unlock_irq(&ohci->lock); 2657 2658 /* If we didn't use the DMA allocation, delete it. */ 2659 if (next_config_rom != NULL) { 2660 dmam_free_coherent(ohci->card.device, CONFIG_ROM_SIZE, next_config_rom, 2661 next_config_rom_bus); 2662 } 2663 2664 /* 2665 * Now initiate a bus reset to have the changes take 2666 * effect. We clean up the old config rom memory and DMA 2667 * mappings in the bus reset tasklet, since the OHCI 2668 * controller could need to access it before the bus reset 2669 * takes effect. 2670 */ 2671 2672 fw_schedule_bus_reset(&ohci->card, true, true); 2673 2674 return 0; 2675 } 2676 2677 static void ohci_send_request(struct fw_card *card, struct fw_packet *packet) 2678 { 2679 struct fw_ohci *ohci = fw_ohci(card); 2680 2681 at_context_transmit(&ohci->at_request_ctx, packet); 2682 } 2683 2684 static void ohci_send_response(struct fw_card *card, struct fw_packet *packet) 2685 { 2686 struct fw_ohci *ohci = fw_ohci(card); 2687 2688 at_context_transmit(&ohci->at_response_ctx, packet); 2689 } 2690 2691 static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet) 2692 { 2693 struct fw_ohci *ohci = fw_ohci(card); 2694 struct context *ctx = &ohci->at_request_ctx; 2695 struct driver_data *driver_data = packet->driver_data; 2696 int ret = -ENOENT; 2697 2698 tasklet_disable_in_atomic(&ctx->tasklet); 2699 2700 if (packet->ack != 0) 2701 goto out; 2702 2703 if (packet->payload_mapped) 2704 dma_unmap_single(ohci->card.device, packet->payload_bus, 2705 packet->payload_length, DMA_TO_DEVICE); 2706 2707 log_ar_at_event(ohci, 'T', packet->speed, packet->header, 0x20); 2708 driver_data->packet = NULL; 2709 packet->ack = RCODE_CANCELLED; 2710 2711 // Timestamping on behalf of the hardware. 2712 packet->timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ohci)); 2713 2714 packet->callback(packet, &ohci->card, packet->ack); 2715 ret = 0; 2716 out: 2717 tasklet_enable(&ctx->tasklet); 2718 2719 return ret; 2720 } 2721 2722 static int ohci_enable_phys_dma(struct fw_card *card, 2723 int node_id, int generation) 2724 { 2725 struct fw_ohci *ohci = fw_ohci(card); 2726 unsigned long flags; 2727 int n, ret = 0; 2728 2729 if (param_remote_dma) 2730 return 0; 2731 2732 /* 2733 * FIXME: Make sure this bitmask is cleared when we clear the busReset 2734 * interrupt bit. Clear physReqResourceAllBuses on bus reset. 2735 */ 2736 2737 spin_lock_irqsave(&ohci->lock, flags); 2738 2739 if (ohci->generation != generation) { 2740 ret = -ESTALE; 2741 goto out; 2742 } 2743 2744 /* 2745 * Note, if the node ID contains a non-local bus ID, physical DMA is 2746 * enabled for _all_ nodes on remote buses. 2747 */ 2748 2749 n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63; 2750 if (n < 32) 2751 reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n); 2752 else 2753 reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32)); 2754 2755 flush_writes(ohci); 2756 out: 2757 spin_unlock_irqrestore(&ohci->lock, flags); 2758 2759 return ret; 2760 } 2761 2762 static u32 ohci_read_csr(struct fw_card *card, int csr_offset) 2763 { 2764 struct fw_ohci *ohci = fw_ohci(card); 2765 unsigned long flags; 2766 u32 value; 2767 2768 switch (csr_offset) { 2769 case CSR_STATE_CLEAR: 2770 case CSR_STATE_SET: 2771 if (ohci->is_root && 2772 (reg_read(ohci, OHCI1394_LinkControlSet) & 2773 OHCI1394_LinkControl_cycleMaster)) 2774 value = CSR_STATE_BIT_CMSTR; 2775 else 2776 value = 0; 2777 if (ohci->csr_state_setclear_abdicate) 2778 value |= CSR_STATE_BIT_ABDICATE; 2779 2780 return value; 2781 2782 case CSR_NODE_IDS: 2783 return reg_read(ohci, OHCI1394_NodeID) << 16; 2784 2785 case CSR_CYCLE_TIME: 2786 return get_cycle_time(ohci); 2787 2788 case CSR_BUS_TIME: 2789 /* 2790 * We might be called just after the cycle timer has wrapped 2791 * around but just before the cycle64Seconds handler, so we 2792 * better check here, too, if the bus time needs to be updated. 2793 */ 2794 spin_lock_irqsave(&ohci->lock, flags); 2795 value = update_bus_time(ohci); 2796 spin_unlock_irqrestore(&ohci->lock, flags); 2797 return value; 2798 2799 case CSR_BUSY_TIMEOUT: 2800 value = reg_read(ohci, OHCI1394_ATRetries); 2801 return (value >> 4) & 0x0ffff00f; 2802 2803 case CSR_PRIORITY_BUDGET: 2804 return (reg_read(ohci, OHCI1394_FairnessControl) & 0x3f) | 2805 (ohci->pri_req_max << 8); 2806 2807 default: 2808 WARN_ON(1); 2809 return 0; 2810 } 2811 } 2812 2813 static void ohci_write_csr(struct fw_card *card, int csr_offset, u32 value) 2814 { 2815 struct fw_ohci *ohci = fw_ohci(card); 2816 unsigned long flags; 2817 2818 switch (csr_offset) { 2819 case CSR_STATE_CLEAR: 2820 if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) { 2821 reg_write(ohci, OHCI1394_LinkControlClear, 2822 OHCI1394_LinkControl_cycleMaster); 2823 flush_writes(ohci); 2824 } 2825 if (value & CSR_STATE_BIT_ABDICATE) 2826 ohci->csr_state_setclear_abdicate = false; 2827 break; 2828 2829 case CSR_STATE_SET: 2830 if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) { 2831 reg_write(ohci, OHCI1394_LinkControlSet, 2832 OHCI1394_LinkControl_cycleMaster); 2833 flush_writes(ohci); 2834 } 2835 if (value & CSR_STATE_BIT_ABDICATE) 2836 ohci->csr_state_setclear_abdicate = true; 2837 break; 2838 2839 case CSR_NODE_IDS: 2840 reg_write(ohci, OHCI1394_NodeID, value >> 16); 2841 flush_writes(ohci); 2842 break; 2843 2844 case CSR_CYCLE_TIME: 2845 reg_write(ohci, OHCI1394_IsochronousCycleTimer, value); 2846 reg_write(ohci, OHCI1394_IntEventSet, 2847 OHCI1394_cycleInconsistent); 2848 flush_writes(ohci); 2849 break; 2850 2851 case CSR_BUS_TIME: 2852 spin_lock_irqsave(&ohci->lock, flags); 2853 ohci->bus_time = (update_bus_time(ohci) & 0x40) | 2854 (value & ~0x7f); 2855 spin_unlock_irqrestore(&ohci->lock, flags); 2856 break; 2857 2858 case CSR_BUSY_TIMEOUT: 2859 value = (value & 0xf) | ((value & 0xf) << 4) | 2860 ((value & 0xf) << 8) | ((value & 0x0ffff000) << 4); 2861 reg_write(ohci, OHCI1394_ATRetries, value); 2862 flush_writes(ohci); 2863 break; 2864 2865 case CSR_PRIORITY_BUDGET: 2866 reg_write(ohci, OHCI1394_FairnessControl, value & 0x3f); 2867 flush_writes(ohci); 2868 break; 2869 2870 default: 2871 WARN_ON(1); 2872 break; 2873 } 2874 } 2875 2876 static void flush_iso_completions(struct iso_context *ctx, enum fw_iso_context_completions_cause cause) 2877 { 2878 trace_isoc_inbound_single_completions(&ctx->base, ctx->last_timestamp, cause, ctx->header, 2879 ctx->header_length); 2880 trace_isoc_outbound_completions(&ctx->base, ctx->last_timestamp, cause, ctx->header, 2881 ctx->header_length); 2882 2883 ctx->base.callback.sc(&ctx->base, ctx->last_timestamp, 2884 ctx->header_length, ctx->header, 2885 ctx->base.callback_data); 2886 ctx->header_length = 0; 2887 } 2888 2889 static void copy_iso_headers(struct iso_context *ctx, const u32 *dma_hdr) 2890 { 2891 u32 *ctx_hdr; 2892 2893 if (ctx->header_length + ctx->base.header_size > PAGE_SIZE) { 2894 if (ctx->base.drop_overflow_headers) 2895 return; 2896 flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_HEADER_OVERFLOW); 2897 } 2898 2899 ctx_hdr = ctx->header + ctx->header_length; 2900 ctx->last_timestamp = (u16)le32_to_cpu((__force __le32)dma_hdr[0]); 2901 2902 /* 2903 * The two iso header quadlets are byteswapped to little 2904 * endian by the controller, but we want to present them 2905 * as big endian for consistency with the bus endianness. 2906 */ 2907 if (ctx->base.header_size > 0) 2908 ctx_hdr[0] = swab32(dma_hdr[1]); /* iso packet header */ 2909 if (ctx->base.header_size > 4) 2910 ctx_hdr[1] = swab32(dma_hdr[0]); /* timestamp */ 2911 if (ctx->base.header_size > 8) 2912 memcpy(&ctx_hdr[2], &dma_hdr[2], ctx->base.header_size - 8); 2913 ctx->header_length += ctx->base.header_size; 2914 } 2915 2916 static int handle_ir_packet_per_buffer(struct context *context, 2917 struct descriptor *d, 2918 struct descriptor *last) 2919 { 2920 struct iso_context *ctx = 2921 container_of(context, struct iso_context, context); 2922 struct descriptor *pd; 2923 u32 buffer_dma; 2924 2925 for (pd = d; pd <= last; pd++) 2926 if (pd->transfer_status) 2927 break; 2928 if (pd > last) 2929 /* Descriptor(s) not done yet, stop iteration */ 2930 return 0; 2931 2932 while (!(d->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))) { 2933 d++; 2934 buffer_dma = le32_to_cpu(d->data_address); 2935 dma_sync_single_range_for_cpu(context->ohci->card.device, 2936 buffer_dma & PAGE_MASK, 2937 buffer_dma & ~PAGE_MASK, 2938 le16_to_cpu(d->req_count), 2939 DMA_FROM_DEVICE); 2940 } 2941 2942 copy_iso_headers(ctx, (u32 *) (last + 1)); 2943 2944 if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) 2945 flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_IRQ); 2946 2947 return 1; 2948 } 2949 2950 /* d == last because each descriptor block is only a single descriptor. */ 2951 static int handle_ir_buffer_fill(struct context *context, 2952 struct descriptor *d, 2953 struct descriptor *last) 2954 { 2955 struct iso_context *ctx = 2956 container_of(context, struct iso_context, context); 2957 unsigned int req_count, res_count, completed; 2958 u32 buffer_dma; 2959 2960 req_count = le16_to_cpu(last->req_count); 2961 res_count = le16_to_cpu(READ_ONCE(last->res_count)); 2962 completed = req_count - res_count; 2963 buffer_dma = le32_to_cpu(last->data_address); 2964 2965 if (completed > 0) { 2966 ctx->mc_buffer_bus = buffer_dma; 2967 ctx->mc_completed = completed; 2968 } 2969 2970 if (res_count != 0) 2971 /* Descriptor(s) not done yet, stop iteration */ 2972 return 0; 2973 2974 dma_sync_single_range_for_cpu(context->ohci->card.device, 2975 buffer_dma & PAGE_MASK, 2976 buffer_dma & ~PAGE_MASK, 2977 completed, DMA_FROM_DEVICE); 2978 2979 if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) { 2980 trace_isoc_inbound_multiple_completions(&ctx->base, completed, 2981 FW_ISO_CONTEXT_COMPLETIONS_CAUSE_IRQ); 2982 2983 ctx->base.callback.mc(&ctx->base, 2984 buffer_dma + completed, 2985 ctx->base.callback_data); 2986 ctx->mc_completed = 0; 2987 } 2988 2989 return 1; 2990 } 2991 2992 static void flush_ir_buffer_fill(struct iso_context *ctx) 2993 { 2994 dma_sync_single_range_for_cpu(ctx->context.ohci->card.device, 2995 ctx->mc_buffer_bus & PAGE_MASK, 2996 ctx->mc_buffer_bus & ~PAGE_MASK, 2997 ctx->mc_completed, DMA_FROM_DEVICE); 2998 2999 trace_isoc_inbound_multiple_completions(&ctx->base, ctx->mc_completed, 3000 FW_ISO_CONTEXT_COMPLETIONS_CAUSE_FLUSH); 3001 3002 ctx->base.callback.mc(&ctx->base, 3003 ctx->mc_buffer_bus + ctx->mc_completed, 3004 ctx->base.callback_data); 3005 ctx->mc_completed = 0; 3006 } 3007 3008 static inline void sync_it_packet_for_cpu(struct context *context, 3009 struct descriptor *pd) 3010 { 3011 __le16 control; 3012 u32 buffer_dma; 3013 3014 /* only packets beginning with OUTPUT_MORE* have data buffers */ 3015 if (pd->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS)) 3016 return; 3017 3018 /* skip over the OUTPUT_MORE_IMMEDIATE descriptor */ 3019 pd += 2; 3020 3021 /* 3022 * If the packet has a header, the first OUTPUT_MORE/LAST descriptor's 3023 * data buffer is in the context program's coherent page and must not 3024 * be synced. 3025 */ 3026 if ((le32_to_cpu(pd->data_address) & PAGE_MASK) == 3027 (context->current_bus & PAGE_MASK)) { 3028 if (pd->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS)) 3029 return; 3030 pd++; 3031 } 3032 3033 do { 3034 buffer_dma = le32_to_cpu(pd->data_address); 3035 dma_sync_single_range_for_cpu(context->ohci->card.device, 3036 buffer_dma & PAGE_MASK, 3037 buffer_dma & ~PAGE_MASK, 3038 le16_to_cpu(pd->req_count), 3039 DMA_TO_DEVICE); 3040 control = pd->control; 3041 pd++; 3042 } while (!(control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))); 3043 } 3044 3045 static int handle_it_packet(struct context *context, 3046 struct descriptor *d, 3047 struct descriptor *last) 3048 { 3049 struct iso_context *ctx = 3050 container_of(context, struct iso_context, context); 3051 struct descriptor *pd; 3052 __be32 *ctx_hdr; 3053 3054 for (pd = d; pd <= last; pd++) 3055 if (pd->transfer_status) 3056 break; 3057 if (pd > last) 3058 /* Descriptor(s) not done yet, stop iteration */ 3059 return 0; 3060 3061 sync_it_packet_for_cpu(context, d); 3062 3063 if (ctx->header_length + 4 > PAGE_SIZE) { 3064 if (ctx->base.drop_overflow_headers) 3065 return 1; 3066 flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_HEADER_OVERFLOW); 3067 } 3068 3069 ctx_hdr = ctx->header + ctx->header_length; 3070 ctx->last_timestamp = le16_to_cpu(last->res_count); 3071 /* Present this value as big-endian to match the receive code */ 3072 *ctx_hdr = cpu_to_be32((le16_to_cpu(pd->transfer_status) << 16) | 3073 le16_to_cpu(pd->res_count)); 3074 ctx->header_length += 4; 3075 3076 if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) 3077 flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_IRQ); 3078 3079 return 1; 3080 } 3081 3082 static void set_multichannel_mask(struct fw_ohci *ohci, u64 channels) 3083 { 3084 u32 hi = channels >> 32, lo = channels; 3085 3086 reg_write(ohci, OHCI1394_IRMultiChanMaskHiClear, ~hi); 3087 reg_write(ohci, OHCI1394_IRMultiChanMaskLoClear, ~lo); 3088 reg_write(ohci, OHCI1394_IRMultiChanMaskHiSet, hi); 3089 reg_write(ohci, OHCI1394_IRMultiChanMaskLoSet, lo); 3090 ohci->mc_channels = channels; 3091 } 3092 3093 static struct fw_iso_context *ohci_allocate_iso_context(struct fw_card *card, 3094 int type, int channel, size_t header_size) 3095 { 3096 struct fw_ohci *ohci = fw_ohci(card); 3097 struct iso_context *ctx; 3098 descriptor_callback_t callback; 3099 u64 *channels; 3100 u32 *mask, regs; 3101 int index, ret = -EBUSY; 3102 3103 spin_lock_irq(&ohci->lock); 3104 3105 switch (type) { 3106 case FW_ISO_CONTEXT_TRANSMIT: 3107 mask = &ohci->it_context_mask; 3108 callback = handle_it_packet; 3109 index = ffs(*mask) - 1; 3110 if (index >= 0) { 3111 *mask &= ~(1 << index); 3112 regs = OHCI1394_IsoXmitContextBase(index); 3113 ctx = &ohci->it_context_list[index]; 3114 } 3115 break; 3116 3117 case FW_ISO_CONTEXT_RECEIVE: 3118 channels = &ohci->ir_context_channels; 3119 mask = &ohci->ir_context_mask; 3120 callback = handle_ir_packet_per_buffer; 3121 index = *channels & 1ULL << channel ? ffs(*mask) - 1 : -1; 3122 if (index >= 0) { 3123 *channels &= ~(1ULL << channel); 3124 *mask &= ~(1 << index); 3125 regs = OHCI1394_IsoRcvContextBase(index); 3126 ctx = &ohci->ir_context_list[index]; 3127 } 3128 break; 3129 3130 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3131 mask = &ohci->ir_context_mask; 3132 callback = handle_ir_buffer_fill; 3133 index = !ohci->mc_allocated ? ffs(*mask) - 1 : -1; 3134 if (index >= 0) { 3135 ohci->mc_allocated = true; 3136 *mask &= ~(1 << index); 3137 regs = OHCI1394_IsoRcvContextBase(index); 3138 ctx = &ohci->ir_context_list[index]; 3139 } 3140 break; 3141 3142 default: 3143 index = -1; 3144 ret = -ENOSYS; 3145 } 3146 3147 spin_unlock_irq(&ohci->lock); 3148 3149 if (index < 0) 3150 return ERR_PTR(ret); 3151 3152 memset(ctx, 0, sizeof(*ctx)); 3153 ctx->header_length = 0; 3154 ctx->header = (void *) __get_free_page(GFP_KERNEL); 3155 if (ctx->header == NULL) { 3156 ret = -ENOMEM; 3157 goto out; 3158 } 3159 ret = context_init(&ctx->context, ohci, regs, callback); 3160 if (ret < 0) 3161 goto out_with_header; 3162 3163 if (type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL) { 3164 set_multichannel_mask(ohci, 0); 3165 ctx->mc_completed = 0; 3166 } 3167 3168 return &ctx->base; 3169 3170 out_with_header: 3171 free_page((unsigned long)ctx->header); 3172 out: 3173 spin_lock_irq(&ohci->lock); 3174 3175 switch (type) { 3176 case FW_ISO_CONTEXT_RECEIVE: 3177 *channels |= 1ULL << channel; 3178 break; 3179 3180 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3181 ohci->mc_allocated = false; 3182 break; 3183 } 3184 *mask |= 1 << index; 3185 3186 spin_unlock_irq(&ohci->lock); 3187 3188 return ERR_PTR(ret); 3189 } 3190 3191 static int ohci_start_iso(struct fw_iso_context *base, 3192 s32 cycle, u32 sync, u32 tags) 3193 { 3194 struct iso_context *ctx = container_of(base, struct iso_context, base); 3195 struct fw_ohci *ohci = ctx->context.ohci; 3196 u32 control = IR_CONTEXT_ISOCH_HEADER, match; 3197 int index; 3198 3199 /* the controller cannot start without any queued packets */ 3200 if (ctx->context.last->branch_address == 0) 3201 return -ENODATA; 3202 3203 switch (ctx->base.type) { 3204 case FW_ISO_CONTEXT_TRANSMIT: 3205 index = ctx - ohci->it_context_list; 3206 match = 0; 3207 if (cycle >= 0) 3208 match = IT_CONTEXT_CYCLE_MATCH_ENABLE | 3209 (cycle & 0x7fff) << 16; 3210 3211 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index); 3212 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index); 3213 context_run(&ctx->context, match); 3214 break; 3215 3216 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3217 control |= IR_CONTEXT_BUFFER_FILL|IR_CONTEXT_MULTI_CHANNEL_MODE; 3218 fallthrough; 3219 case FW_ISO_CONTEXT_RECEIVE: 3220 index = ctx - ohci->ir_context_list; 3221 match = (tags << 28) | (sync << 8) | ctx->base.channel; 3222 if (cycle >= 0) { 3223 match |= (cycle & 0x07fff) << 12; 3224 control |= IR_CONTEXT_CYCLE_MATCH_ENABLE; 3225 } 3226 3227 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index); 3228 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index); 3229 reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match); 3230 context_run(&ctx->context, control); 3231 3232 ctx->sync = sync; 3233 ctx->tags = tags; 3234 3235 break; 3236 } 3237 3238 return 0; 3239 } 3240 3241 static int ohci_stop_iso(struct fw_iso_context *base) 3242 { 3243 struct fw_ohci *ohci = fw_ohci(base->card); 3244 struct iso_context *ctx = container_of(base, struct iso_context, base); 3245 int index; 3246 3247 switch (ctx->base.type) { 3248 case FW_ISO_CONTEXT_TRANSMIT: 3249 index = ctx - ohci->it_context_list; 3250 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index); 3251 break; 3252 3253 case FW_ISO_CONTEXT_RECEIVE: 3254 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3255 index = ctx - ohci->ir_context_list; 3256 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index); 3257 break; 3258 } 3259 flush_writes(ohci); 3260 context_stop(&ctx->context); 3261 tasklet_kill(&ctx->context.tasklet); 3262 3263 return 0; 3264 } 3265 3266 static void ohci_free_iso_context(struct fw_iso_context *base) 3267 { 3268 struct fw_ohci *ohci = fw_ohci(base->card); 3269 struct iso_context *ctx = container_of(base, struct iso_context, base); 3270 unsigned long flags; 3271 int index; 3272 3273 ohci_stop_iso(base); 3274 context_release(&ctx->context); 3275 free_page((unsigned long)ctx->header); 3276 3277 spin_lock_irqsave(&ohci->lock, flags); 3278 3279 switch (base->type) { 3280 case FW_ISO_CONTEXT_TRANSMIT: 3281 index = ctx - ohci->it_context_list; 3282 ohci->it_context_mask |= 1 << index; 3283 break; 3284 3285 case FW_ISO_CONTEXT_RECEIVE: 3286 index = ctx - ohci->ir_context_list; 3287 ohci->ir_context_mask |= 1 << index; 3288 ohci->ir_context_channels |= 1ULL << base->channel; 3289 break; 3290 3291 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3292 index = ctx - ohci->ir_context_list; 3293 ohci->ir_context_mask |= 1 << index; 3294 ohci->ir_context_channels |= ohci->mc_channels; 3295 ohci->mc_channels = 0; 3296 ohci->mc_allocated = false; 3297 break; 3298 } 3299 3300 spin_unlock_irqrestore(&ohci->lock, flags); 3301 } 3302 3303 static int ohci_set_iso_channels(struct fw_iso_context *base, u64 *channels) 3304 { 3305 struct fw_ohci *ohci = fw_ohci(base->card); 3306 unsigned long flags; 3307 int ret; 3308 3309 switch (base->type) { 3310 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3311 3312 spin_lock_irqsave(&ohci->lock, flags); 3313 3314 /* Don't allow multichannel to grab other contexts' channels. */ 3315 if (~ohci->ir_context_channels & ~ohci->mc_channels & *channels) { 3316 *channels = ohci->ir_context_channels; 3317 ret = -EBUSY; 3318 } else { 3319 set_multichannel_mask(ohci, *channels); 3320 ret = 0; 3321 } 3322 3323 spin_unlock_irqrestore(&ohci->lock, flags); 3324 3325 break; 3326 default: 3327 ret = -EINVAL; 3328 } 3329 3330 return ret; 3331 } 3332 3333 #ifdef CONFIG_PM 3334 static void ohci_resume_iso_dma(struct fw_ohci *ohci) 3335 { 3336 int i; 3337 struct iso_context *ctx; 3338 3339 for (i = 0 ; i < ohci->n_ir ; i++) { 3340 ctx = &ohci->ir_context_list[i]; 3341 if (ctx->context.running) 3342 ohci_start_iso(&ctx->base, 0, ctx->sync, ctx->tags); 3343 } 3344 3345 for (i = 0 ; i < ohci->n_it ; i++) { 3346 ctx = &ohci->it_context_list[i]; 3347 if (ctx->context.running) 3348 ohci_start_iso(&ctx->base, 0, ctx->sync, ctx->tags); 3349 } 3350 } 3351 #endif 3352 3353 static int queue_iso_transmit(struct iso_context *ctx, 3354 struct fw_iso_packet *packet, 3355 struct fw_iso_buffer *buffer, 3356 unsigned long payload) 3357 { 3358 struct descriptor *d, *last, *pd; 3359 struct fw_iso_packet *p; 3360 __le32 *header; 3361 dma_addr_t d_bus, page_bus; 3362 u32 z, header_z, payload_z, irq; 3363 u32 payload_index, payload_end_index, next_page_index; 3364 int page, end_page, i, length, offset; 3365 3366 p = packet; 3367 payload_index = payload; 3368 3369 if (p->skip) 3370 z = 1; 3371 else 3372 z = 2; 3373 if (p->header_length > 0) 3374 z++; 3375 3376 /* Determine the first page the payload isn't contained in. */ 3377 end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT; 3378 if (p->payload_length > 0) 3379 payload_z = end_page - (payload_index >> PAGE_SHIFT); 3380 else 3381 payload_z = 0; 3382 3383 z += payload_z; 3384 3385 /* Get header size in number of descriptors. */ 3386 header_z = DIV_ROUND_UP(p->header_length, sizeof(*d)); 3387 3388 d = context_get_descriptors(&ctx->context, z + header_z, &d_bus); 3389 if (d == NULL) 3390 return -ENOMEM; 3391 3392 if (!p->skip) { 3393 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE); 3394 d[0].req_count = cpu_to_le16(8); 3395 /* 3396 * Link the skip address to this descriptor itself. This causes 3397 * a context to skip a cycle whenever lost cycles or FIFO 3398 * overruns occur, without dropping the data. The application 3399 * should then decide whether this is an error condition or not. 3400 * FIXME: Make the context's cycle-lost behaviour configurable? 3401 */ 3402 d[0].branch_address = cpu_to_le32(d_bus | z); 3403 3404 header = (__le32 *) &d[1]; 3405 3406 ohci1394_it_data_set_speed(header, ctx->base.speed); 3407 ohci1394_it_data_set_tag(header, p->tag); 3408 ohci1394_it_data_set_channel(header, ctx->base.channel); 3409 ohci1394_it_data_set_tcode(header, TCODE_STREAM_DATA); 3410 ohci1394_it_data_set_sync(header, p->sy); 3411 3412 ohci1394_it_data_set_data_length(header, p->header_length + p->payload_length); 3413 } 3414 3415 if (p->header_length > 0) { 3416 d[2].req_count = cpu_to_le16(p->header_length); 3417 d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d)); 3418 memcpy(&d[z], p->header, p->header_length); 3419 } 3420 3421 pd = d + z - payload_z; 3422 payload_end_index = payload_index + p->payload_length; 3423 for (i = 0; i < payload_z; i++) { 3424 page = payload_index >> PAGE_SHIFT; 3425 offset = payload_index & ~PAGE_MASK; 3426 next_page_index = (page + 1) << PAGE_SHIFT; 3427 length = 3428 min(next_page_index, payload_end_index) - payload_index; 3429 pd[i].req_count = cpu_to_le16(length); 3430 3431 page_bus = page_private(buffer->pages[page]); 3432 pd[i].data_address = cpu_to_le32(page_bus + offset); 3433 3434 dma_sync_single_range_for_device(ctx->context.ohci->card.device, 3435 page_bus, offset, length, 3436 DMA_TO_DEVICE); 3437 3438 payload_index += length; 3439 } 3440 3441 if (p->interrupt) 3442 irq = DESCRIPTOR_IRQ_ALWAYS; 3443 else 3444 irq = DESCRIPTOR_NO_IRQ; 3445 3446 last = z == 2 ? d : d + z - 1; 3447 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST | 3448 DESCRIPTOR_STATUS | 3449 DESCRIPTOR_BRANCH_ALWAYS | 3450 irq); 3451 3452 context_append(&ctx->context, d, z, header_z); 3453 3454 return 0; 3455 } 3456 3457 static int queue_iso_packet_per_buffer(struct iso_context *ctx, 3458 struct fw_iso_packet *packet, 3459 struct fw_iso_buffer *buffer, 3460 unsigned long payload) 3461 { 3462 struct device *device = ctx->context.ohci->card.device; 3463 struct descriptor *d, *pd; 3464 dma_addr_t d_bus, page_bus; 3465 u32 z, header_z, rest; 3466 int i, j, length; 3467 int page, offset, packet_count, header_size, payload_per_buffer; 3468 3469 /* 3470 * The OHCI controller puts the isochronous header and trailer in the 3471 * buffer, so we need at least 8 bytes. 3472 */ 3473 packet_count = packet->header_length / ctx->base.header_size; 3474 header_size = max(ctx->base.header_size, (size_t)8); 3475 3476 /* Get header size in number of descriptors. */ 3477 header_z = DIV_ROUND_UP(header_size, sizeof(*d)); 3478 page = payload >> PAGE_SHIFT; 3479 offset = payload & ~PAGE_MASK; 3480 payload_per_buffer = packet->payload_length / packet_count; 3481 3482 for (i = 0; i < packet_count; i++) { 3483 /* d points to the header descriptor */ 3484 z = DIV_ROUND_UP(payload_per_buffer + offset, PAGE_SIZE) + 1; 3485 d = context_get_descriptors(&ctx->context, 3486 z + header_z, &d_bus); 3487 if (d == NULL) 3488 return -ENOMEM; 3489 3490 d->control = cpu_to_le16(DESCRIPTOR_STATUS | 3491 DESCRIPTOR_INPUT_MORE); 3492 if (packet->skip && i == 0) 3493 d->control |= cpu_to_le16(DESCRIPTOR_WAIT); 3494 d->req_count = cpu_to_le16(header_size); 3495 d->res_count = d->req_count; 3496 d->transfer_status = 0; 3497 d->data_address = cpu_to_le32(d_bus + (z * sizeof(*d))); 3498 3499 rest = payload_per_buffer; 3500 pd = d; 3501 for (j = 1; j < z; j++) { 3502 pd++; 3503 pd->control = cpu_to_le16(DESCRIPTOR_STATUS | 3504 DESCRIPTOR_INPUT_MORE); 3505 3506 if (offset + rest < PAGE_SIZE) 3507 length = rest; 3508 else 3509 length = PAGE_SIZE - offset; 3510 pd->req_count = cpu_to_le16(length); 3511 pd->res_count = pd->req_count; 3512 pd->transfer_status = 0; 3513 3514 page_bus = page_private(buffer->pages[page]); 3515 pd->data_address = cpu_to_le32(page_bus + offset); 3516 3517 dma_sync_single_range_for_device(device, page_bus, 3518 offset, length, 3519 DMA_FROM_DEVICE); 3520 3521 offset = (offset + length) & ~PAGE_MASK; 3522 rest -= length; 3523 if (offset == 0) 3524 page++; 3525 } 3526 pd->control = cpu_to_le16(DESCRIPTOR_STATUS | 3527 DESCRIPTOR_INPUT_LAST | 3528 DESCRIPTOR_BRANCH_ALWAYS); 3529 if (packet->interrupt && i == packet_count - 1) 3530 pd->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS); 3531 3532 context_append(&ctx->context, d, z, header_z); 3533 } 3534 3535 return 0; 3536 } 3537 3538 static int queue_iso_buffer_fill(struct iso_context *ctx, 3539 struct fw_iso_packet *packet, 3540 struct fw_iso_buffer *buffer, 3541 unsigned long payload) 3542 { 3543 struct descriptor *d; 3544 dma_addr_t d_bus, page_bus; 3545 int page, offset, rest, z, i, length; 3546 3547 page = payload >> PAGE_SHIFT; 3548 offset = payload & ~PAGE_MASK; 3549 rest = packet->payload_length; 3550 3551 /* We need one descriptor for each page in the buffer. */ 3552 z = DIV_ROUND_UP(offset + rest, PAGE_SIZE); 3553 3554 if (WARN_ON(offset & 3 || rest & 3 || page + z > buffer->page_count)) 3555 return -EFAULT; 3556 3557 for (i = 0; i < z; i++) { 3558 d = context_get_descriptors(&ctx->context, 1, &d_bus); 3559 if (d == NULL) 3560 return -ENOMEM; 3561 3562 d->control = cpu_to_le16(DESCRIPTOR_INPUT_MORE | 3563 DESCRIPTOR_BRANCH_ALWAYS); 3564 if (packet->skip && i == 0) 3565 d->control |= cpu_to_le16(DESCRIPTOR_WAIT); 3566 if (packet->interrupt && i == z - 1) 3567 d->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS); 3568 3569 if (offset + rest < PAGE_SIZE) 3570 length = rest; 3571 else 3572 length = PAGE_SIZE - offset; 3573 d->req_count = cpu_to_le16(length); 3574 d->res_count = d->req_count; 3575 d->transfer_status = 0; 3576 3577 page_bus = page_private(buffer->pages[page]); 3578 d->data_address = cpu_to_le32(page_bus + offset); 3579 3580 dma_sync_single_range_for_device(ctx->context.ohci->card.device, 3581 page_bus, offset, length, 3582 DMA_FROM_DEVICE); 3583 3584 rest -= length; 3585 offset = 0; 3586 page++; 3587 3588 context_append(&ctx->context, d, 1, 0); 3589 } 3590 3591 return 0; 3592 } 3593 3594 static int ohci_queue_iso(struct fw_iso_context *base, 3595 struct fw_iso_packet *packet, 3596 struct fw_iso_buffer *buffer, 3597 unsigned long payload) 3598 { 3599 struct iso_context *ctx = container_of(base, struct iso_context, base); 3600 unsigned long flags; 3601 int ret = -ENOSYS; 3602 3603 spin_lock_irqsave(&ctx->context.ohci->lock, flags); 3604 switch (base->type) { 3605 case FW_ISO_CONTEXT_TRANSMIT: 3606 ret = queue_iso_transmit(ctx, packet, buffer, payload); 3607 break; 3608 case FW_ISO_CONTEXT_RECEIVE: 3609 ret = queue_iso_packet_per_buffer(ctx, packet, buffer, payload); 3610 break; 3611 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3612 ret = queue_iso_buffer_fill(ctx, packet, buffer, payload); 3613 break; 3614 } 3615 spin_unlock_irqrestore(&ctx->context.ohci->lock, flags); 3616 3617 return ret; 3618 } 3619 3620 static void ohci_flush_queue_iso(struct fw_iso_context *base) 3621 { 3622 struct context *ctx = 3623 &container_of(base, struct iso_context, base)->context; 3624 3625 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE); 3626 } 3627 3628 static int ohci_flush_iso_completions(struct fw_iso_context *base) 3629 { 3630 struct iso_context *ctx = container_of(base, struct iso_context, base); 3631 int ret = 0; 3632 3633 tasklet_disable_in_atomic(&ctx->context.tasklet); 3634 3635 if (!test_and_set_bit_lock(0, &ctx->flushing_completions)) { 3636 context_tasklet((unsigned long)&ctx->context); 3637 3638 switch (base->type) { 3639 case FW_ISO_CONTEXT_TRANSMIT: 3640 case FW_ISO_CONTEXT_RECEIVE: 3641 if (ctx->header_length != 0) 3642 flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_FLUSH); 3643 break; 3644 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3645 if (ctx->mc_completed != 0) 3646 flush_ir_buffer_fill(ctx); 3647 break; 3648 default: 3649 ret = -ENOSYS; 3650 } 3651 3652 clear_bit_unlock(0, &ctx->flushing_completions); 3653 smp_mb__after_atomic(); 3654 } 3655 3656 tasklet_enable(&ctx->context.tasklet); 3657 3658 return ret; 3659 } 3660 3661 static const struct fw_card_driver ohci_driver = { 3662 .enable = ohci_enable, 3663 .read_phy_reg = ohci_read_phy_reg, 3664 .update_phy_reg = ohci_update_phy_reg, 3665 .set_config_rom = ohci_set_config_rom, 3666 .send_request = ohci_send_request, 3667 .send_response = ohci_send_response, 3668 .cancel_packet = ohci_cancel_packet, 3669 .enable_phys_dma = ohci_enable_phys_dma, 3670 .read_csr = ohci_read_csr, 3671 .write_csr = ohci_write_csr, 3672 3673 .allocate_iso_context = ohci_allocate_iso_context, 3674 .free_iso_context = ohci_free_iso_context, 3675 .set_iso_channels = ohci_set_iso_channels, 3676 .queue_iso = ohci_queue_iso, 3677 .flush_queue_iso = ohci_flush_queue_iso, 3678 .flush_iso_completions = ohci_flush_iso_completions, 3679 .start_iso = ohci_start_iso, 3680 .stop_iso = ohci_stop_iso, 3681 }; 3682 3683 #ifdef CONFIG_PPC_PMAC 3684 static void pmac_ohci_on(struct pci_dev *dev) 3685 { 3686 if (machine_is(powermac)) { 3687 struct device_node *ofn = pci_device_to_OF_node(dev); 3688 3689 if (ofn) { 3690 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 1); 3691 pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 1); 3692 } 3693 } 3694 } 3695 3696 static void pmac_ohci_off(struct pci_dev *dev) 3697 { 3698 if (machine_is(powermac)) { 3699 struct device_node *ofn = pci_device_to_OF_node(dev); 3700 3701 if (ofn) { 3702 pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 0); 3703 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 0); 3704 } 3705 } 3706 } 3707 #else 3708 static inline void pmac_ohci_on(struct pci_dev *dev) {} 3709 static inline void pmac_ohci_off(struct pci_dev *dev) {} 3710 #endif /* CONFIG_PPC_PMAC */ 3711 3712 static void release_ohci(struct device *dev, void *data) 3713 { 3714 struct pci_dev *pdev = to_pci_dev(dev); 3715 struct fw_ohci *ohci = pci_get_drvdata(pdev); 3716 3717 pmac_ohci_off(pdev); 3718 3719 ar_context_release(&ohci->ar_response_ctx); 3720 ar_context_release(&ohci->ar_request_ctx); 3721 3722 dev_notice(dev, "removed fw-ohci device\n"); 3723 } 3724 3725 static int pci_probe(struct pci_dev *dev, 3726 const struct pci_device_id *ent) 3727 { 3728 struct fw_ohci *ohci; 3729 u32 bus_options, max_receive, link_speed, version; 3730 u64 guid; 3731 int i, flags, irq, err; 3732 size_t size; 3733 3734 if (dev->vendor == PCI_VENDOR_ID_PINNACLE_SYSTEMS) { 3735 dev_err(&dev->dev, "Pinnacle MovieBoard is not yet supported\n"); 3736 return -ENOSYS; 3737 } 3738 3739 ohci = devres_alloc(release_ohci, sizeof(*ohci), GFP_KERNEL); 3740 if (ohci == NULL) 3741 return -ENOMEM; 3742 fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev); 3743 pci_set_drvdata(dev, ohci); 3744 pmac_ohci_on(dev); 3745 devres_add(&dev->dev, ohci); 3746 3747 err = pcim_enable_device(dev); 3748 if (err) { 3749 dev_err(&dev->dev, "failed to enable OHCI hardware\n"); 3750 return err; 3751 } 3752 3753 pci_set_master(dev); 3754 pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0); 3755 3756 spin_lock_init(&ohci->lock); 3757 mutex_init(&ohci->phy_reg_mutex); 3758 3759 INIT_WORK(&ohci->bus_reset_work, bus_reset_work); 3760 3761 if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM) || 3762 pci_resource_len(dev, 0) < OHCI1394_REGISTER_SIZE) { 3763 ohci_err(ohci, "invalid MMIO resource\n"); 3764 return -ENXIO; 3765 } 3766 3767 err = pcim_iomap_regions(dev, 1 << 0, ohci_driver_name); 3768 if (err) { 3769 ohci_err(ohci, "request and map MMIO resource unavailable\n"); 3770 return -ENXIO; 3771 } 3772 ohci->registers = pcim_iomap_table(dev)[0]; 3773 3774 for (i = 0; i < ARRAY_SIZE(ohci_quirks); i++) 3775 if ((ohci_quirks[i].vendor == dev->vendor) && 3776 (ohci_quirks[i].device == (unsigned short)PCI_ANY_ID || 3777 ohci_quirks[i].device == dev->device) && 3778 (ohci_quirks[i].revision == (unsigned short)PCI_ANY_ID || 3779 ohci_quirks[i].revision >= dev->revision)) { 3780 ohci->quirks = ohci_quirks[i].flags; 3781 break; 3782 } 3783 if (param_quirks) 3784 ohci->quirks = param_quirks; 3785 3786 if (detect_vt630x_with_asm1083_on_amd_ryzen_machine(dev)) 3787 ohci->quirks |= QUIRK_REBOOT_BY_CYCLE_TIMER_READ; 3788 3789 /* 3790 * Because dma_alloc_coherent() allocates at least one page, 3791 * we save space by using a common buffer for the AR request/ 3792 * response descriptors and the self IDs buffer. 3793 */ 3794 BUILD_BUG_ON(AR_BUFFERS * sizeof(struct descriptor) > PAGE_SIZE/4); 3795 BUILD_BUG_ON(SELF_ID_BUF_SIZE > PAGE_SIZE/2); 3796 ohci->misc_buffer = dmam_alloc_coherent(&dev->dev, PAGE_SIZE, &ohci->misc_buffer_bus, 3797 GFP_KERNEL); 3798 if (!ohci->misc_buffer) 3799 return -ENOMEM; 3800 3801 err = ar_context_init(&ohci->ar_request_ctx, ohci, 0, 3802 OHCI1394_AsReqRcvContextControlSet); 3803 if (err < 0) 3804 return err; 3805 3806 err = ar_context_init(&ohci->ar_response_ctx, ohci, PAGE_SIZE/4, 3807 OHCI1394_AsRspRcvContextControlSet); 3808 if (err < 0) 3809 return err; 3810 3811 err = context_init(&ohci->at_request_ctx, ohci, 3812 OHCI1394_AsReqTrContextControlSet, handle_at_packet); 3813 if (err < 0) 3814 return err; 3815 3816 err = context_init(&ohci->at_response_ctx, ohci, 3817 OHCI1394_AsRspTrContextControlSet, handle_at_packet); 3818 if (err < 0) 3819 return err; 3820 3821 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0); 3822 ohci->ir_context_channels = ~0ULL; 3823 ohci->ir_context_support = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet); 3824 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0); 3825 ohci->ir_context_mask = ohci->ir_context_support; 3826 ohci->n_ir = hweight32(ohci->ir_context_mask); 3827 size = sizeof(struct iso_context) * ohci->n_ir; 3828 ohci->ir_context_list = devm_kzalloc(&dev->dev, size, GFP_KERNEL); 3829 if (!ohci->ir_context_list) 3830 return -ENOMEM; 3831 3832 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0); 3833 ohci->it_context_support = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet); 3834 /* JMicron JMB38x often shows 0 at first read, just ignore it */ 3835 if (!ohci->it_context_support) { 3836 ohci_notice(ohci, "overriding IsoXmitIntMask\n"); 3837 ohci->it_context_support = 0xf; 3838 } 3839 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0); 3840 ohci->it_context_mask = ohci->it_context_support; 3841 ohci->n_it = hweight32(ohci->it_context_mask); 3842 size = sizeof(struct iso_context) * ohci->n_it; 3843 ohci->it_context_list = devm_kzalloc(&dev->dev, size, GFP_KERNEL); 3844 if (!ohci->it_context_list) 3845 return -ENOMEM; 3846 3847 ohci->self_id = ohci->misc_buffer + PAGE_SIZE/2; 3848 ohci->self_id_bus = ohci->misc_buffer_bus + PAGE_SIZE/2; 3849 3850 bus_options = reg_read(ohci, OHCI1394_BusOptions); 3851 max_receive = (bus_options >> 12) & 0xf; 3852 link_speed = bus_options & 0x7; 3853 guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) | 3854 reg_read(ohci, OHCI1394_GUIDLo); 3855 3856 flags = PCI_IRQ_INTX; 3857 if (!(ohci->quirks & QUIRK_NO_MSI)) 3858 flags |= PCI_IRQ_MSI; 3859 err = pci_alloc_irq_vectors(dev, 1, 1, flags); 3860 if (err < 0) 3861 return err; 3862 irq = pci_irq_vector(dev, 0); 3863 if (irq < 0) { 3864 err = irq; 3865 goto fail_msi; 3866 } 3867 3868 err = request_threaded_irq(irq, irq_handler, NULL, 3869 pci_dev_msi_enabled(dev) ? 0 : IRQF_SHARED, ohci_driver_name, 3870 ohci); 3871 if (err < 0) { 3872 ohci_err(ohci, "failed to allocate interrupt %d\n", irq); 3873 goto fail_msi; 3874 } 3875 3876 err = fw_card_add(&ohci->card, max_receive, link_speed, guid); 3877 if (err) 3878 goto fail_irq; 3879 3880 version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff; 3881 ohci_notice(ohci, 3882 "added OHCI v%x.%x device as card %d, " 3883 "%d IR + %d IT contexts, quirks 0x%x%s\n", 3884 version >> 16, version & 0xff, ohci->card.index, 3885 ohci->n_ir, ohci->n_it, ohci->quirks, 3886 reg_read(ohci, OHCI1394_PhyUpperBound) ? 3887 ", physUB" : ""); 3888 3889 return 0; 3890 3891 fail_irq: 3892 free_irq(irq, ohci); 3893 fail_msi: 3894 pci_free_irq_vectors(dev); 3895 3896 return err; 3897 } 3898 3899 static void pci_remove(struct pci_dev *dev) 3900 { 3901 struct fw_ohci *ohci = pci_get_drvdata(dev); 3902 int irq; 3903 3904 /* 3905 * If the removal is happening from the suspend state, LPS won't be 3906 * enabled and host registers (eg., IntMaskClear) won't be accessible. 3907 */ 3908 if (reg_read(ohci, OHCI1394_HCControlSet) & OHCI1394_HCControl_LPS) { 3909 reg_write(ohci, OHCI1394_IntMaskClear, ~0); 3910 flush_writes(ohci); 3911 } 3912 cancel_work_sync(&ohci->bus_reset_work); 3913 fw_core_remove_card(&ohci->card); 3914 3915 /* 3916 * FIXME: Fail all pending packets here, now that the upper 3917 * layers can't queue any more. 3918 */ 3919 3920 software_reset(ohci); 3921 3922 irq = pci_irq_vector(dev, 0); 3923 if (irq >= 0) 3924 free_irq(irq, ohci); 3925 pci_free_irq_vectors(dev); 3926 3927 dev_notice(&dev->dev, "removing fw-ohci device\n"); 3928 } 3929 3930 #ifdef CONFIG_PM 3931 static int pci_suspend(struct pci_dev *dev, pm_message_t state) 3932 { 3933 struct fw_ohci *ohci = pci_get_drvdata(dev); 3934 int err; 3935 3936 software_reset(ohci); 3937 err = pci_save_state(dev); 3938 if (err) { 3939 ohci_err(ohci, "pci_save_state failed\n"); 3940 return err; 3941 } 3942 err = pci_set_power_state(dev, pci_choose_state(dev, state)); 3943 if (err) 3944 ohci_err(ohci, "pci_set_power_state failed with %d\n", err); 3945 pmac_ohci_off(dev); 3946 3947 return 0; 3948 } 3949 3950 static int pci_resume(struct pci_dev *dev) 3951 { 3952 struct fw_ohci *ohci = pci_get_drvdata(dev); 3953 int err; 3954 3955 pmac_ohci_on(dev); 3956 pci_set_power_state(dev, PCI_D0); 3957 pci_restore_state(dev); 3958 err = pci_enable_device(dev); 3959 if (err) { 3960 ohci_err(ohci, "pci_enable_device failed\n"); 3961 return err; 3962 } 3963 3964 /* Some systems don't setup GUID register on resume from ram */ 3965 if (!reg_read(ohci, OHCI1394_GUIDLo) && 3966 !reg_read(ohci, OHCI1394_GUIDHi)) { 3967 reg_write(ohci, OHCI1394_GUIDLo, (u32)ohci->card.guid); 3968 reg_write(ohci, OHCI1394_GUIDHi, (u32)(ohci->card.guid >> 32)); 3969 } 3970 3971 err = ohci_enable(&ohci->card, NULL, 0); 3972 if (err) 3973 return err; 3974 3975 ohci_resume_iso_dma(ohci); 3976 3977 return 0; 3978 } 3979 #endif 3980 3981 static const struct pci_device_id pci_table[] = { 3982 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) }, 3983 { } 3984 }; 3985 3986 MODULE_DEVICE_TABLE(pci, pci_table); 3987 3988 static struct pci_driver fw_ohci_pci_driver = { 3989 .name = ohci_driver_name, 3990 .id_table = pci_table, 3991 .probe = pci_probe, 3992 .remove = pci_remove, 3993 #ifdef CONFIG_PM 3994 .resume = pci_resume, 3995 .suspend = pci_suspend, 3996 #endif 3997 }; 3998 3999 static int __init fw_ohci_init(void) 4000 { 4001 selfid_workqueue = alloc_workqueue(KBUILD_MODNAME, WQ_MEM_RECLAIM, 0); 4002 if (!selfid_workqueue) 4003 return -ENOMEM; 4004 4005 return pci_register_driver(&fw_ohci_pci_driver); 4006 } 4007 4008 static void __exit fw_ohci_cleanup(void) 4009 { 4010 pci_unregister_driver(&fw_ohci_pci_driver); 4011 destroy_workqueue(selfid_workqueue); 4012 } 4013 4014 module_init(fw_ohci_init); 4015 module_exit(fw_ohci_cleanup); 4016 4017 MODULE_AUTHOR("Kristian Hoegsberg <[email protected]>"); 4018 MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers"); 4019 MODULE_LICENSE("GPL"); 4020 4021 /* Provide a module alias so root-on-sbp2 initrds don't break. */ 4022 MODULE_ALIAS("ohci1394"); 4023