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 previous buffer to the 1177 // free list. 1178 old_desc->used = 0; 1179 guard(spinlock_irqsave)(&ctx->ohci->lock); 1180 list_move_tail(&old_desc->list, &ctx->buffer_list); 1181 } 1182 ctx->last = last; 1183 } 1184 } 1185 1186 /* 1187 * Allocate a new buffer and add it to the list of free buffers for this 1188 * context. Must be called with ohci->lock held. 1189 */ 1190 static int context_add_buffer(struct context *ctx) 1191 { 1192 struct descriptor_buffer *desc; 1193 dma_addr_t bus_addr; 1194 int offset; 1195 1196 /* 1197 * 16MB of descriptors should be far more than enough for any DMA 1198 * program. This will catch run-away userspace or DoS attacks. 1199 */ 1200 if (ctx->total_allocation >= 16*1024*1024) 1201 return -ENOMEM; 1202 1203 desc = dmam_alloc_coherent(ctx->ohci->card.device, PAGE_SIZE, &bus_addr, GFP_ATOMIC); 1204 if (!desc) 1205 return -ENOMEM; 1206 1207 offset = (void *)&desc->buffer - (void *)desc; 1208 /* 1209 * Some controllers, like JMicron ones, always issue 0x20-byte DMA reads 1210 * for descriptors, even 0x10-byte ones. This can cause page faults when 1211 * an IOMMU is in use and the oversized read crosses a page boundary. 1212 * Work around this by always leaving at least 0x10 bytes of padding. 1213 */ 1214 desc->buffer_size = PAGE_SIZE - offset - 0x10; 1215 desc->buffer_bus = bus_addr + offset; 1216 desc->used = 0; 1217 1218 list_add_tail(&desc->list, &ctx->buffer_list); 1219 ctx->total_allocation += PAGE_SIZE; 1220 1221 return 0; 1222 } 1223 1224 static int context_init(struct context *ctx, struct fw_ohci *ohci, 1225 u32 regs, descriptor_callback_t callback) 1226 { 1227 ctx->ohci = ohci; 1228 ctx->regs = regs; 1229 ctx->total_allocation = 0; 1230 1231 INIT_LIST_HEAD(&ctx->buffer_list); 1232 if (context_add_buffer(ctx) < 0) 1233 return -ENOMEM; 1234 1235 ctx->buffer_tail = list_entry(ctx->buffer_list.next, 1236 struct descriptor_buffer, list); 1237 1238 tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx); 1239 ctx->callback = callback; 1240 1241 /* 1242 * We put a dummy descriptor in the buffer that has a NULL 1243 * branch address and looks like it's been sent. That way we 1244 * have a descriptor to append DMA programs to. 1245 */ 1246 memset(ctx->buffer_tail->buffer, 0, sizeof(*ctx->buffer_tail->buffer)); 1247 ctx->buffer_tail->buffer->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST); 1248 ctx->buffer_tail->buffer->transfer_status = cpu_to_le16(0x8011); 1249 ctx->buffer_tail->used += sizeof(*ctx->buffer_tail->buffer); 1250 ctx->last = ctx->buffer_tail->buffer; 1251 ctx->prev = ctx->buffer_tail->buffer; 1252 ctx->prev_z = 1; 1253 1254 return 0; 1255 } 1256 1257 static void context_release(struct context *ctx) 1258 { 1259 struct fw_card *card = &ctx->ohci->card; 1260 struct descriptor_buffer *desc, *tmp; 1261 1262 list_for_each_entry_safe(desc, tmp, &ctx->buffer_list, list) { 1263 dmam_free_coherent(card->device, PAGE_SIZE, desc, 1264 desc->buffer_bus - ((void *)&desc->buffer - (void *)desc)); 1265 } 1266 } 1267 1268 /* Must be called with ohci->lock held */ 1269 static struct descriptor *context_get_descriptors(struct context *ctx, 1270 int z, dma_addr_t *d_bus) 1271 { 1272 struct descriptor *d = NULL; 1273 struct descriptor_buffer *desc = ctx->buffer_tail; 1274 1275 if (z * sizeof(*d) > desc->buffer_size) 1276 return NULL; 1277 1278 if (z * sizeof(*d) > desc->buffer_size - desc->used) { 1279 /* No room for the descriptor in this buffer, so advance to the 1280 * next one. */ 1281 1282 if (desc->list.next == &ctx->buffer_list) { 1283 /* If there is no free buffer next in the list, 1284 * allocate one. */ 1285 if (context_add_buffer(ctx) < 0) 1286 return NULL; 1287 } 1288 desc = list_entry(desc->list.next, 1289 struct descriptor_buffer, list); 1290 ctx->buffer_tail = desc; 1291 } 1292 1293 d = desc->buffer + desc->used / sizeof(*d); 1294 memset(d, 0, z * sizeof(*d)); 1295 *d_bus = desc->buffer_bus + desc->used; 1296 1297 return d; 1298 } 1299 1300 static void context_run(struct context *ctx, u32 extra) 1301 { 1302 struct fw_ohci *ohci = ctx->ohci; 1303 1304 reg_write(ohci, COMMAND_PTR(ctx->regs), 1305 le32_to_cpu(ctx->last->branch_address)); 1306 reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0); 1307 reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra); 1308 ctx->running = true; 1309 flush_writes(ohci); 1310 } 1311 1312 static void context_append(struct context *ctx, 1313 struct descriptor *d, int z, int extra) 1314 { 1315 dma_addr_t d_bus; 1316 struct descriptor_buffer *desc = ctx->buffer_tail; 1317 struct descriptor *d_branch; 1318 1319 d_bus = desc->buffer_bus + (d - desc->buffer) * sizeof(*d); 1320 1321 desc->used += (z + extra) * sizeof(*d); 1322 1323 wmb(); /* finish init of new descriptors before branch_address update */ 1324 1325 d_branch = find_branch_descriptor(ctx->prev, ctx->prev_z); 1326 d_branch->branch_address = cpu_to_le32(d_bus | z); 1327 1328 /* 1329 * VT6306 incorrectly checks only the single descriptor at the 1330 * CommandPtr when the wake bit is written, so if it's a 1331 * multi-descriptor block starting with an INPUT_MORE, put a copy of 1332 * the branch address in the first descriptor. 1333 * 1334 * Not doing this for transmit contexts since not sure how it interacts 1335 * with skip addresses. 1336 */ 1337 if (unlikely(ctx->ohci->quirks & QUIRK_IR_WAKE) && 1338 d_branch != ctx->prev && 1339 (ctx->prev->control & cpu_to_le16(DESCRIPTOR_CMD)) == 1340 cpu_to_le16(DESCRIPTOR_INPUT_MORE)) { 1341 ctx->prev->branch_address = cpu_to_le32(d_bus | z); 1342 } 1343 1344 ctx->prev = d; 1345 ctx->prev_z = z; 1346 } 1347 1348 static void context_stop(struct context *ctx) 1349 { 1350 struct fw_ohci *ohci = ctx->ohci; 1351 u32 reg; 1352 int i; 1353 1354 reg_write(ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN); 1355 ctx->running = false; 1356 1357 for (i = 0; i < 1000; i++) { 1358 reg = reg_read(ohci, CONTROL_SET(ctx->regs)); 1359 if ((reg & CONTEXT_ACTIVE) == 0) 1360 return; 1361 1362 if (i) 1363 udelay(10); 1364 } 1365 ohci_err(ohci, "DMA context still active (0x%08x)\n", reg); 1366 } 1367 1368 struct driver_data { 1369 u8 inline_data[8]; 1370 struct fw_packet *packet; 1371 }; 1372 1373 /* 1374 * This function apppends a packet to the DMA queue for transmission. 1375 * Must always be called with the ochi->lock held to ensure proper 1376 * generation handling and locking around packet queue manipulation. 1377 */ 1378 static int at_context_queue_packet(struct context *ctx, 1379 struct fw_packet *packet) 1380 { 1381 struct fw_ohci *ohci = ctx->ohci; 1382 dma_addr_t d_bus, payload_bus; 1383 struct driver_data *driver_data; 1384 struct descriptor *d, *last; 1385 __le32 *header; 1386 int z, tcode; 1387 1388 d = context_get_descriptors(ctx, 4, &d_bus); 1389 if (d == NULL) { 1390 packet->ack = RCODE_SEND_ERROR; 1391 return -1; 1392 } 1393 1394 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE); 1395 d[0].res_count = cpu_to_le16(packet->timestamp); 1396 1397 tcode = async_header_get_tcode(packet->header); 1398 header = (__le32 *) &d[1]; 1399 switch (tcode) { 1400 case TCODE_WRITE_QUADLET_REQUEST: 1401 case TCODE_WRITE_BLOCK_REQUEST: 1402 case TCODE_WRITE_RESPONSE: 1403 case TCODE_READ_QUADLET_REQUEST: 1404 case TCODE_READ_BLOCK_REQUEST: 1405 case TCODE_READ_QUADLET_RESPONSE: 1406 case TCODE_READ_BLOCK_RESPONSE: 1407 case TCODE_LOCK_REQUEST: 1408 case TCODE_LOCK_RESPONSE: 1409 ohci1394_at_data_set_src_bus_id(header, false); 1410 ohci1394_at_data_set_speed(header, packet->speed); 1411 ohci1394_at_data_set_tlabel(header, async_header_get_tlabel(packet->header)); 1412 ohci1394_at_data_set_retry(header, async_header_get_retry(packet->header)); 1413 ohci1394_at_data_set_tcode(header, tcode); 1414 1415 ohci1394_at_data_set_destination_id(header, 1416 async_header_get_destination(packet->header)); 1417 1418 if (ctx == &ctx->ohci->at_response_ctx) { 1419 ohci1394_at_data_set_rcode(header, async_header_get_rcode(packet->header)); 1420 } else { 1421 ohci1394_at_data_set_destination_offset(header, 1422 async_header_get_offset(packet->header)); 1423 } 1424 1425 if (tcode_is_block_packet(tcode)) 1426 header[3] = cpu_to_le32(packet->header[3]); 1427 else 1428 header[3] = (__force __le32) packet->header[3]; 1429 1430 d[0].req_count = cpu_to_le16(packet->header_length); 1431 break; 1432 case TCODE_LINK_INTERNAL: 1433 ohci1394_at_data_set_speed(header, packet->speed); 1434 ohci1394_at_data_set_tcode(header, TCODE_LINK_INTERNAL); 1435 1436 header[1] = cpu_to_le32(packet->header[1]); 1437 header[2] = cpu_to_le32(packet->header[2]); 1438 d[0].req_count = cpu_to_le16(12); 1439 1440 if (is_ping_packet(&packet->header[1])) 1441 d[0].control |= cpu_to_le16(DESCRIPTOR_PING); 1442 break; 1443 1444 case TCODE_STREAM_DATA: 1445 ohci1394_it_data_set_speed(header, packet->speed); 1446 ohci1394_it_data_set_tag(header, isoc_header_get_tag(packet->header[0])); 1447 ohci1394_it_data_set_channel(header, isoc_header_get_channel(packet->header[0])); 1448 ohci1394_it_data_set_tcode(header, TCODE_STREAM_DATA); 1449 ohci1394_it_data_set_sync(header, isoc_header_get_sy(packet->header[0])); 1450 1451 ohci1394_it_data_set_data_length(header, isoc_header_get_data_length(packet->header[0])); 1452 1453 d[0].req_count = cpu_to_le16(8); 1454 break; 1455 1456 default: 1457 /* BUG(); */ 1458 packet->ack = RCODE_SEND_ERROR; 1459 return -1; 1460 } 1461 1462 BUILD_BUG_ON(sizeof(struct driver_data) > sizeof(struct descriptor)); 1463 driver_data = (struct driver_data *) &d[3]; 1464 driver_data->packet = packet; 1465 packet->driver_data = driver_data; 1466 1467 if (packet->payload_length > 0) { 1468 if (packet->payload_length > sizeof(driver_data->inline_data)) { 1469 payload_bus = dma_map_single(ohci->card.device, 1470 packet->payload, 1471 packet->payload_length, 1472 DMA_TO_DEVICE); 1473 if (dma_mapping_error(ohci->card.device, payload_bus)) { 1474 packet->ack = RCODE_SEND_ERROR; 1475 return -1; 1476 } 1477 packet->payload_bus = payload_bus; 1478 packet->payload_mapped = true; 1479 } else { 1480 memcpy(driver_data->inline_data, packet->payload, 1481 packet->payload_length); 1482 payload_bus = d_bus + 3 * sizeof(*d); 1483 } 1484 1485 d[2].req_count = cpu_to_le16(packet->payload_length); 1486 d[2].data_address = cpu_to_le32(payload_bus); 1487 last = &d[2]; 1488 z = 3; 1489 } else { 1490 last = &d[0]; 1491 z = 2; 1492 } 1493 1494 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST | 1495 DESCRIPTOR_IRQ_ALWAYS | 1496 DESCRIPTOR_BRANCH_ALWAYS); 1497 1498 /* FIXME: Document how the locking works. */ 1499 if (ohci->generation != packet->generation) { 1500 if (packet->payload_mapped) 1501 dma_unmap_single(ohci->card.device, payload_bus, 1502 packet->payload_length, DMA_TO_DEVICE); 1503 packet->ack = RCODE_GENERATION; 1504 return -1; 1505 } 1506 1507 context_append(ctx, d, z, 4 - z); 1508 1509 if (ctx->running) 1510 reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE); 1511 else 1512 context_run(ctx, 0); 1513 1514 return 0; 1515 } 1516 1517 static void at_context_flush(struct context *ctx) 1518 { 1519 tasklet_disable(&ctx->tasklet); 1520 1521 ctx->flushing = true; 1522 context_tasklet((unsigned long)ctx); 1523 ctx->flushing = false; 1524 1525 tasklet_enable(&ctx->tasklet); 1526 } 1527 1528 static int handle_at_packet(struct context *context, 1529 struct descriptor *d, 1530 struct descriptor *last) 1531 { 1532 struct driver_data *driver_data; 1533 struct fw_packet *packet; 1534 struct fw_ohci *ohci = context->ohci; 1535 int evt; 1536 1537 if (last->transfer_status == 0 && !context->flushing) 1538 /* This descriptor isn't done yet, stop iteration. */ 1539 return 0; 1540 1541 driver_data = (struct driver_data *) &d[3]; 1542 packet = driver_data->packet; 1543 if (packet == NULL) 1544 /* This packet was cancelled, just continue. */ 1545 return 1; 1546 1547 if (packet->payload_mapped) 1548 dma_unmap_single(ohci->card.device, packet->payload_bus, 1549 packet->payload_length, DMA_TO_DEVICE); 1550 1551 evt = le16_to_cpu(last->transfer_status) & 0x1f; 1552 packet->timestamp = le16_to_cpu(last->res_count); 1553 1554 log_ar_at_event(ohci, 'T', packet->speed, packet->header, evt); 1555 1556 switch (evt) { 1557 case OHCI1394_evt_timeout: 1558 /* Async response transmit timed out. */ 1559 packet->ack = RCODE_CANCELLED; 1560 break; 1561 1562 case OHCI1394_evt_flushed: 1563 /* 1564 * The packet was flushed should give same error as 1565 * when we try to use a stale generation count. 1566 */ 1567 packet->ack = RCODE_GENERATION; 1568 break; 1569 1570 case OHCI1394_evt_missing_ack: 1571 if (context->flushing) 1572 packet->ack = RCODE_GENERATION; 1573 else { 1574 /* 1575 * Using a valid (current) generation count, but the 1576 * node is not on the bus or not sending acks. 1577 */ 1578 packet->ack = RCODE_NO_ACK; 1579 } 1580 break; 1581 1582 case ACK_COMPLETE + 0x10: 1583 case ACK_PENDING + 0x10: 1584 case ACK_BUSY_X + 0x10: 1585 case ACK_BUSY_A + 0x10: 1586 case ACK_BUSY_B + 0x10: 1587 case ACK_DATA_ERROR + 0x10: 1588 case ACK_TYPE_ERROR + 0x10: 1589 packet->ack = evt - 0x10; 1590 break; 1591 1592 case OHCI1394_evt_no_status: 1593 if (context->flushing) { 1594 packet->ack = RCODE_GENERATION; 1595 break; 1596 } 1597 fallthrough; 1598 1599 default: 1600 packet->ack = RCODE_SEND_ERROR; 1601 break; 1602 } 1603 1604 packet->callback(packet, &ohci->card, packet->ack); 1605 1606 return 1; 1607 } 1608 1609 static u32 get_cycle_time(struct fw_ohci *ohci); 1610 1611 static void handle_local_rom(struct fw_ohci *ohci, 1612 struct fw_packet *packet, u32 csr) 1613 { 1614 struct fw_packet response; 1615 int tcode, length, i; 1616 1617 tcode = async_header_get_tcode(packet->header); 1618 if (tcode_is_block_packet(tcode)) 1619 length = async_header_get_data_length(packet->header); 1620 else 1621 length = 4; 1622 1623 i = csr - CSR_CONFIG_ROM; 1624 if (i + length > CONFIG_ROM_SIZE) { 1625 fw_fill_response(&response, packet->header, 1626 RCODE_ADDRESS_ERROR, NULL, 0); 1627 } else if (!tcode_is_read_request(tcode)) { 1628 fw_fill_response(&response, packet->header, 1629 RCODE_TYPE_ERROR, NULL, 0); 1630 } else { 1631 fw_fill_response(&response, packet->header, RCODE_COMPLETE, 1632 (void *) ohci->config_rom + i, length); 1633 } 1634 1635 // Timestamping on behalf of the hardware. 1636 response.timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ohci)); 1637 fw_core_handle_response(&ohci->card, &response); 1638 } 1639 1640 static void handle_local_lock(struct fw_ohci *ohci, 1641 struct fw_packet *packet, u32 csr) 1642 { 1643 struct fw_packet response; 1644 int tcode, length, ext_tcode, sel, try; 1645 __be32 *payload, lock_old; 1646 u32 lock_arg, lock_data; 1647 1648 tcode = async_header_get_tcode(packet->header); 1649 length = async_header_get_data_length(packet->header); 1650 payload = packet->payload; 1651 ext_tcode = async_header_get_extended_tcode(packet->header); 1652 1653 if (tcode == TCODE_LOCK_REQUEST && 1654 ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) { 1655 lock_arg = be32_to_cpu(payload[0]); 1656 lock_data = be32_to_cpu(payload[1]); 1657 } else if (tcode == TCODE_READ_QUADLET_REQUEST) { 1658 lock_arg = 0; 1659 lock_data = 0; 1660 } else { 1661 fw_fill_response(&response, packet->header, 1662 RCODE_TYPE_ERROR, NULL, 0); 1663 goto out; 1664 } 1665 1666 sel = (csr - CSR_BUS_MANAGER_ID) / 4; 1667 reg_write(ohci, OHCI1394_CSRData, lock_data); 1668 reg_write(ohci, OHCI1394_CSRCompareData, lock_arg); 1669 reg_write(ohci, OHCI1394_CSRControl, sel); 1670 1671 for (try = 0; try < 20; try++) 1672 if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000) { 1673 lock_old = cpu_to_be32(reg_read(ohci, 1674 OHCI1394_CSRData)); 1675 fw_fill_response(&response, packet->header, 1676 RCODE_COMPLETE, 1677 &lock_old, sizeof(lock_old)); 1678 goto out; 1679 } 1680 1681 ohci_err(ohci, "swap not done (CSR lock timeout)\n"); 1682 fw_fill_response(&response, packet->header, RCODE_BUSY, NULL, 0); 1683 1684 out: 1685 // Timestamping on behalf of the hardware. 1686 response.timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ohci)); 1687 fw_core_handle_response(&ohci->card, &response); 1688 } 1689 1690 static void handle_local_request(struct context *ctx, struct fw_packet *packet) 1691 { 1692 u64 offset, csr; 1693 1694 if (ctx == &ctx->ohci->at_request_ctx) { 1695 packet->ack = ACK_PENDING; 1696 packet->callback(packet, &ctx->ohci->card, packet->ack); 1697 } 1698 1699 offset = async_header_get_offset(packet->header); 1700 csr = offset - CSR_REGISTER_BASE; 1701 1702 /* Handle config rom reads. */ 1703 if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END) 1704 handle_local_rom(ctx->ohci, packet, csr); 1705 else switch (csr) { 1706 case CSR_BUS_MANAGER_ID: 1707 case CSR_BANDWIDTH_AVAILABLE: 1708 case CSR_CHANNELS_AVAILABLE_HI: 1709 case CSR_CHANNELS_AVAILABLE_LO: 1710 handle_local_lock(ctx->ohci, packet, csr); 1711 break; 1712 default: 1713 if (ctx == &ctx->ohci->at_request_ctx) 1714 fw_core_handle_request(&ctx->ohci->card, packet); 1715 else 1716 fw_core_handle_response(&ctx->ohci->card, packet); 1717 break; 1718 } 1719 1720 if (ctx == &ctx->ohci->at_response_ctx) { 1721 packet->ack = ACK_COMPLETE; 1722 packet->callback(packet, &ctx->ohci->card, packet->ack); 1723 } 1724 } 1725 1726 static void at_context_transmit(struct context *ctx, struct fw_packet *packet) 1727 { 1728 unsigned long flags; 1729 int ret; 1730 1731 spin_lock_irqsave(&ctx->ohci->lock, flags); 1732 1733 if (async_header_get_destination(packet->header) == ctx->ohci->node_id && 1734 ctx->ohci->generation == packet->generation) { 1735 spin_unlock_irqrestore(&ctx->ohci->lock, flags); 1736 1737 // Timestamping on behalf of the hardware. 1738 packet->timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ctx->ohci)); 1739 1740 handle_local_request(ctx, packet); 1741 return; 1742 } 1743 1744 ret = at_context_queue_packet(ctx, packet); 1745 spin_unlock_irqrestore(&ctx->ohci->lock, flags); 1746 1747 if (ret < 0) { 1748 // Timestamping on behalf of the hardware. 1749 packet->timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ctx->ohci)); 1750 1751 packet->callback(packet, &ctx->ohci->card, packet->ack); 1752 } 1753 } 1754 1755 static void detect_dead_context(struct fw_ohci *ohci, 1756 const char *name, unsigned int regs) 1757 { 1758 u32 ctl; 1759 1760 ctl = reg_read(ohci, CONTROL_SET(regs)); 1761 if (ctl & CONTEXT_DEAD) 1762 ohci_err(ohci, "DMA context %s has stopped, error code: %s\n", 1763 name, evts[ctl & 0x1f]); 1764 } 1765 1766 static void handle_dead_contexts(struct fw_ohci *ohci) 1767 { 1768 unsigned int i; 1769 char name[8]; 1770 1771 detect_dead_context(ohci, "ATReq", OHCI1394_AsReqTrContextBase); 1772 detect_dead_context(ohci, "ATRsp", OHCI1394_AsRspTrContextBase); 1773 detect_dead_context(ohci, "ARReq", OHCI1394_AsReqRcvContextBase); 1774 detect_dead_context(ohci, "ARRsp", OHCI1394_AsRspRcvContextBase); 1775 for (i = 0; i < 32; ++i) { 1776 if (!(ohci->it_context_support & (1 << i))) 1777 continue; 1778 sprintf(name, "IT%u", i); 1779 detect_dead_context(ohci, name, OHCI1394_IsoXmitContextBase(i)); 1780 } 1781 for (i = 0; i < 32; ++i) { 1782 if (!(ohci->ir_context_support & (1 << i))) 1783 continue; 1784 sprintf(name, "IR%u", i); 1785 detect_dead_context(ohci, name, OHCI1394_IsoRcvContextBase(i)); 1786 } 1787 /* TODO: maybe try to flush and restart the dead contexts */ 1788 } 1789 1790 static u32 cycle_timer_ticks(u32 cycle_timer) 1791 { 1792 u32 ticks; 1793 1794 ticks = cycle_timer & 0xfff; 1795 ticks += 3072 * ((cycle_timer >> 12) & 0x1fff); 1796 ticks += (3072 * 8000) * (cycle_timer >> 25); 1797 1798 return ticks; 1799 } 1800 1801 /* 1802 * Some controllers exhibit one or more of the following bugs when updating the 1803 * iso cycle timer register: 1804 * - When the lowest six bits are wrapping around to zero, a read that happens 1805 * at the same time will return garbage in the lowest ten bits. 1806 * - When the cycleOffset field wraps around to zero, the cycleCount field is 1807 * not incremented for about 60 ns. 1808 * - Occasionally, the entire register reads zero. 1809 * 1810 * To catch these, we read the register three times and ensure that the 1811 * difference between each two consecutive reads is approximately the same, i.e. 1812 * less than twice the other. Furthermore, any negative difference indicates an 1813 * error. (A PCI read should take at least 20 ticks of the 24.576 MHz timer to 1814 * execute, so we have enough precision to compute the ratio of the differences.) 1815 */ 1816 static u32 get_cycle_time(struct fw_ohci *ohci) 1817 { 1818 u32 c0, c1, c2; 1819 u32 t0, t1, t2; 1820 s32 diff01, diff12; 1821 int i; 1822 1823 if (has_reboot_by_cycle_timer_read_quirk(ohci)) 1824 return 0; 1825 1826 c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer); 1827 1828 if (ohci->quirks & QUIRK_CYCLE_TIMER) { 1829 i = 0; 1830 c1 = c2; 1831 c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer); 1832 do { 1833 c0 = c1; 1834 c1 = c2; 1835 c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer); 1836 t0 = cycle_timer_ticks(c0); 1837 t1 = cycle_timer_ticks(c1); 1838 t2 = cycle_timer_ticks(c2); 1839 diff01 = t1 - t0; 1840 diff12 = t2 - t1; 1841 } while ((diff01 <= 0 || diff12 <= 0 || 1842 diff01 / diff12 >= 2 || diff12 / diff01 >= 2) 1843 && i++ < 20); 1844 } 1845 1846 return c2; 1847 } 1848 1849 /* 1850 * This function has to be called at least every 64 seconds. The bus_time 1851 * field stores not only the upper 25 bits of the BUS_TIME register but also 1852 * the most significant bit of the cycle timer in bit 6 so that we can detect 1853 * changes in this bit. 1854 */ 1855 static u32 update_bus_time(struct fw_ohci *ohci) 1856 { 1857 u32 cycle_time_seconds = get_cycle_time(ohci) >> 25; 1858 1859 if (unlikely(!ohci->bus_time_running)) { 1860 reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_cycle64Seconds); 1861 ohci->bus_time = (lower_32_bits(ktime_get_seconds()) & ~0x7f) | 1862 (cycle_time_seconds & 0x40); 1863 ohci->bus_time_running = true; 1864 } 1865 1866 if ((ohci->bus_time & 0x40) != (cycle_time_seconds & 0x40)) 1867 ohci->bus_time += 0x40; 1868 1869 return ohci->bus_time | cycle_time_seconds; 1870 } 1871 1872 static int get_status_for_port(struct fw_ohci *ohci, int port_index, 1873 enum phy_packet_self_id_port_status *status) 1874 { 1875 int reg; 1876 1877 scoped_guard(mutex, &ohci->phy_reg_mutex) { 1878 reg = write_phy_reg(ohci, 7, port_index); 1879 if (reg < 0) 1880 return reg; 1881 1882 reg = read_phy_reg(ohci, 8); 1883 if (reg < 0) 1884 return reg; 1885 } 1886 1887 switch (reg & 0x0f) { 1888 case 0x06: 1889 // is child node (connected to parent node) 1890 *status = PHY_PACKET_SELF_ID_PORT_STATUS_PARENT; 1891 break; 1892 case 0x0e: 1893 // is parent node (connected to child node) 1894 *status = PHY_PACKET_SELF_ID_PORT_STATUS_CHILD; 1895 break; 1896 default: 1897 // not connected 1898 *status = PHY_PACKET_SELF_ID_PORT_STATUS_NCONN; 1899 break; 1900 } 1901 1902 return 0; 1903 } 1904 1905 static int get_self_id_pos(struct fw_ohci *ohci, u32 self_id, 1906 int self_id_count) 1907 { 1908 unsigned int left_phy_id = phy_packet_self_id_get_phy_id(self_id); 1909 int i; 1910 1911 for (i = 0; i < self_id_count; i++) { 1912 u32 entry = ohci->self_id_buffer[i]; 1913 unsigned int right_phy_id = phy_packet_self_id_get_phy_id(entry); 1914 1915 if (left_phy_id == right_phy_id) 1916 return -1; 1917 if (left_phy_id < right_phy_id) 1918 return i; 1919 } 1920 return i; 1921 } 1922 1923 static bool initiated_reset(struct fw_ohci *ohci) 1924 { 1925 int reg; 1926 1927 guard(mutex)(&ohci->phy_reg_mutex); 1928 1929 // Select page 7 1930 reg = write_phy_reg(ohci, 7, 0xe0); 1931 if (reg < 0) 1932 return reg; 1933 1934 reg = read_phy_reg(ohci, 8); 1935 if (reg < 0) 1936 return reg; 1937 1938 // set PMODE bit 1939 reg |= 0x40; 1940 reg = write_phy_reg(ohci, 8, reg); 1941 if (reg < 0) 1942 return reg; 1943 1944 // read register 12 1945 reg = read_phy_reg(ohci, 12); 1946 if (reg < 0) 1947 return reg; 1948 1949 // bit 3 indicates "initiated reset" 1950 return !!((reg & 0x08) == 0x08); 1951 } 1952 1953 /* 1954 * TI TSB82AA2B and TSB12LV26 do not receive the selfID of a locally 1955 * attached TSB41BA3D phy; see http://www.ti.com/litv/pdf/sllz059. 1956 * Construct the selfID from phy register contents. 1957 */ 1958 static int find_and_insert_self_id(struct fw_ohci *ohci, int self_id_count) 1959 { 1960 int reg, i, pos; 1961 u32 self_id = 0; 1962 1963 // link active 1, speed 3, bridge 0, contender 1, more packets 0. 1964 phy_packet_set_packet_identifier(&self_id, PHY_PACKET_PACKET_IDENTIFIER_SELF_ID); 1965 phy_packet_self_id_zero_set_link_active(&self_id, true); 1966 phy_packet_self_id_zero_set_scode(&self_id, SCODE_800); 1967 phy_packet_self_id_zero_set_contender(&self_id, true); 1968 1969 reg = reg_read(ohci, OHCI1394_NodeID); 1970 if (!(reg & OHCI1394_NodeID_idValid)) { 1971 ohci_notice(ohci, 1972 "node ID not valid, new bus reset in progress\n"); 1973 return -EBUSY; 1974 } 1975 phy_packet_self_id_set_phy_id(&self_id, reg & 0x3f); 1976 1977 reg = ohci_read_phy_reg(&ohci->card, 4); 1978 if (reg < 0) 1979 return reg; 1980 phy_packet_self_id_zero_set_power_class(&self_id, reg & 0x07); 1981 1982 reg = ohci_read_phy_reg(&ohci->card, 1); 1983 if (reg < 0) 1984 return reg; 1985 phy_packet_self_id_zero_set_gap_count(&self_id, reg & 0x3f); 1986 1987 for (i = 0; i < 3; i++) { 1988 enum phy_packet_self_id_port_status status; 1989 int err; 1990 1991 err = get_status_for_port(ohci, i, &status); 1992 if (err < 0) 1993 return err; 1994 1995 self_id_sequence_set_port_status(&self_id, 1, i, status); 1996 } 1997 1998 phy_packet_self_id_zero_set_initiated_reset(&self_id, initiated_reset(ohci)); 1999 2000 pos = get_self_id_pos(ohci, self_id, self_id_count); 2001 if (pos >= 0) { 2002 memmove(&(ohci->self_id_buffer[pos+1]), 2003 &(ohci->self_id_buffer[pos]), 2004 (self_id_count - pos) * sizeof(*ohci->self_id_buffer)); 2005 ohci->self_id_buffer[pos] = self_id; 2006 self_id_count++; 2007 } 2008 return self_id_count; 2009 } 2010 2011 static void bus_reset_work(struct work_struct *work) 2012 { 2013 struct fw_ohci *ohci = 2014 container_of(work, struct fw_ohci, bus_reset_work); 2015 int self_id_count, generation, new_generation, i, j; 2016 u32 reg, quadlet; 2017 void *free_rom = NULL; 2018 dma_addr_t free_rom_bus = 0; 2019 bool is_new_root; 2020 2021 reg = reg_read(ohci, OHCI1394_NodeID); 2022 if (!(reg & OHCI1394_NodeID_idValid)) { 2023 ohci_notice(ohci, 2024 "node ID not valid, new bus reset in progress\n"); 2025 return; 2026 } 2027 if ((reg & OHCI1394_NodeID_nodeNumber) == 63) { 2028 ohci_notice(ohci, "malconfigured bus\n"); 2029 return; 2030 } 2031 ohci->node_id = reg & (OHCI1394_NodeID_busNumber | 2032 OHCI1394_NodeID_nodeNumber); 2033 2034 is_new_root = (reg & OHCI1394_NodeID_root) != 0; 2035 if (!(ohci->is_root && is_new_root)) 2036 reg_write(ohci, OHCI1394_LinkControlSet, 2037 OHCI1394_LinkControl_cycleMaster); 2038 ohci->is_root = is_new_root; 2039 2040 reg = reg_read(ohci, OHCI1394_SelfIDCount); 2041 if (ohci1394_self_id_count_is_error(reg)) { 2042 ohci_notice(ohci, "self ID receive error\n"); 2043 return; 2044 } 2045 /* 2046 * The count in the SelfIDCount register is the number of 2047 * bytes in the self ID receive buffer. Since we also receive 2048 * the inverted quadlets and a header quadlet, we shift one 2049 * bit extra to get the actual number of self IDs. 2050 */ 2051 self_id_count = ohci1394_self_id_count_get_size(reg) >> 1; 2052 2053 if (self_id_count > 252) { 2054 ohci_notice(ohci, "bad selfIDSize (%08x)\n", reg); 2055 return; 2056 } 2057 2058 quadlet = cond_le32_to_cpu(ohci->self_id[0], has_be_header_quirk(ohci)); 2059 generation = ohci1394_self_id_receive_q0_get_generation(quadlet); 2060 rmb(); 2061 2062 for (i = 1, j = 0; j < self_id_count; i += 2, j++) { 2063 u32 id = cond_le32_to_cpu(ohci->self_id[i], has_be_header_quirk(ohci)); 2064 u32 id2 = cond_le32_to_cpu(ohci->self_id[i + 1], has_be_header_quirk(ohci)); 2065 2066 if (id != ~id2) { 2067 /* 2068 * If the invalid data looks like a cycle start packet, 2069 * it's likely to be the result of the cycle master 2070 * having a wrong gap count. In this case, the self IDs 2071 * so far are valid and should be processed so that the 2072 * bus manager can then correct the gap count. 2073 */ 2074 if (id == 0xffff008f) { 2075 ohci_notice(ohci, "ignoring spurious self IDs\n"); 2076 self_id_count = j; 2077 break; 2078 } 2079 2080 ohci_notice(ohci, "bad self ID %d/%d (%08x != ~%08x)\n", 2081 j, self_id_count, id, id2); 2082 return; 2083 } 2084 ohci->self_id_buffer[j] = id; 2085 } 2086 2087 if (ohci->quirks & QUIRK_TI_SLLZ059) { 2088 self_id_count = find_and_insert_self_id(ohci, self_id_count); 2089 if (self_id_count < 0) { 2090 ohci_notice(ohci, 2091 "could not construct local self ID\n"); 2092 return; 2093 } 2094 } 2095 2096 if (self_id_count == 0) { 2097 ohci_notice(ohci, "no self IDs\n"); 2098 return; 2099 } 2100 rmb(); 2101 2102 /* 2103 * Check the consistency of the self IDs we just read. The 2104 * problem we face is that a new bus reset can start while we 2105 * read out the self IDs from the DMA buffer. If this happens, 2106 * the DMA buffer will be overwritten with new self IDs and we 2107 * will read out inconsistent data. The OHCI specification 2108 * (section 11.2) recommends a technique similar to 2109 * linux/seqlock.h, where we remember the generation of the 2110 * self IDs in the buffer before reading them out and compare 2111 * it to the current generation after reading them out. If 2112 * the two generations match we know we have a consistent set 2113 * of self IDs. 2114 */ 2115 2116 reg = reg_read(ohci, OHCI1394_SelfIDCount); 2117 new_generation = ohci1394_self_id_count_get_generation(reg); 2118 if (new_generation != generation) { 2119 ohci_notice(ohci, "new bus reset, discarding self ids\n"); 2120 return; 2121 } 2122 2123 // FIXME: Document how the locking works. 2124 scoped_guard(spinlock_irq, &ohci->lock) { 2125 ohci->generation = -1; // prevent AT packet queueing 2126 context_stop(&ohci->at_request_ctx); 2127 context_stop(&ohci->at_response_ctx); 2128 } 2129 2130 /* 2131 * Per OHCI 1.2 draft, clause 7.2.3.3, hardware may leave unsent 2132 * packets in the AT queues and software needs to drain them. 2133 * Some OHCI 1.1 controllers (JMicron) apparently require this too. 2134 */ 2135 at_context_flush(&ohci->at_request_ctx); 2136 at_context_flush(&ohci->at_response_ctx); 2137 2138 scoped_guard(spinlock_irq, &ohci->lock) { 2139 ohci->generation = generation; 2140 reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset); 2141 reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_busReset); 2142 2143 if (ohci->quirks & QUIRK_RESET_PACKET) 2144 ohci->request_generation = generation; 2145 2146 // This next bit is unrelated to the AT context stuff but we have to do it under the 2147 // spinlock also. If a new config rom was set up before this reset, the old one is 2148 // now no longer in use and we can free it. Update the config rom pointers to point 2149 // to the current config rom and clear the next_config_rom pointer so a new update 2150 // can take place. 2151 if (ohci->next_config_rom != NULL) { 2152 if (ohci->next_config_rom != ohci->config_rom) { 2153 free_rom = ohci->config_rom; 2154 free_rom_bus = ohci->config_rom_bus; 2155 } 2156 ohci->config_rom = ohci->next_config_rom; 2157 ohci->config_rom_bus = ohci->next_config_rom_bus; 2158 ohci->next_config_rom = NULL; 2159 2160 // Restore config_rom image and manually update config_rom registers. 2161 // Writing the header quadlet will indicate that the config rom is ready, 2162 // so we do that last. 2163 reg_write(ohci, OHCI1394_BusOptions, be32_to_cpu(ohci->config_rom[2])); 2164 ohci->config_rom[0] = ohci->next_header; 2165 reg_write(ohci, OHCI1394_ConfigROMhdr, be32_to_cpu(ohci->next_header)); 2166 } 2167 2168 if (param_remote_dma) { 2169 reg_write(ohci, OHCI1394_PhyReqFilterHiSet, ~0); 2170 reg_write(ohci, OHCI1394_PhyReqFilterLoSet, ~0); 2171 } 2172 } 2173 2174 if (free_rom) 2175 dmam_free_coherent(ohci->card.device, CONFIG_ROM_SIZE, free_rom, free_rom_bus); 2176 2177 log_selfids(ohci, generation, self_id_count); 2178 2179 fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation, 2180 self_id_count, ohci->self_id_buffer, 2181 ohci->csr_state_setclear_abdicate); 2182 ohci->csr_state_setclear_abdicate = false; 2183 } 2184 2185 static irqreturn_t irq_handler(int irq, void *data) 2186 { 2187 struct fw_ohci *ohci = data; 2188 u32 event, iso_event; 2189 int i; 2190 2191 event = reg_read(ohci, OHCI1394_IntEventClear); 2192 2193 if (!event || !~event) 2194 return IRQ_NONE; 2195 2196 /* 2197 * busReset and postedWriteErr events must not be cleared yet 2198 * (OHCI 1.1 clauses 7.2.3.2 and 13.2.8.1) 2199 */ 2200 reg_write(ohci, OHCI1394_IntEventClear, 2201 event & ~(OHCI1394_busReset | OHCI1394_postedWriteErr)); 2202 trace_irqs(ohci->card.index, event); 2203 log_irqs(ohci, event); 2204 // The flag is masked again at bus_reset_work() scheduled by selfID event. 2205 if (event & OHCI1394_busReset) 2206 reg_write(ohci, OHCI1394_IntMaskClear, OHCI1394_busReset); 2207 2208 if (event & OHCI1394_selfIDComplete) { 2209 if (trace_self_id_complete_enabled()) { 2210 u32 reg = reg_read(ohci, OHCI1394_SelfIDCount); 2211 2212 trace_self_id_complete(ohci->card.index, reg, ohci->self_id, 2213 has_be_header_quirk(ohci)); 2214 } 2215 queue_work(selfid_workqueue, &ohci->bus_reset_work); 2216 } 2217 2218 if (event & OHCI1394_RQPkt) 2219 tasklet_schedule(&ohci->ar_request_ctx.tasklet); 2220 2221 if (event & OHCI1394_RSPkt) 2222 tasklet_schedule(&ohci->ar_response_ctx.tasklet); 2223 2224 if (event & OHCI1394_reqTxComplete) 2225 tasklet_schedule(&ohci->at_request_ctx.tasklet); 2226 2227 if (event & OHCI1394_respTxComplete) 2228 tasklet_schedule(&ohci->at_response_ctx.tasklet); 2229 2230 if (event & OHCI1394_isochRx) { 2231 iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear); 2232 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event); 2233 2234 while (iso_event) { 2235 i = ffs(iso_event) - 1; 2236 tasklet_schedule( 2237 &ohci->ir_context_list[i].context.tasklet); 2238 iso_event &= ~(1 << i); 2239 } 2240 } 2241 2242 if (event & OHCI1394_isochTx) { 2243 iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear); 2244 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event); 2245 2246 while (iso_event) { 2247 i = ffs(iso_event) - 1; 2248 tasklet_schedule( 2249 &ohci->it_context_list[i].context.tasklet); 2250 iso_event &= ~(1 << i); 2251 } 2252 } 2253 2254 if (unlikely(event & OHCI1394_regAccessFail)) 2255 ohci_err(ohci, "register access failure\n"); 2256 2257 if (unlikely(event & OHCI1394_postedWriteErr)) { 2258 reg_read(ohci, OHCI1394_PostedWriteAddressHi); 2259 reg_read(ohci, OHCI1394_PostedWriteAddressLo); 2260 reg_write(ohci, OHCI1394_IntEventClear, 2261 OHCI1394_postedWriteErr); 2262 if (printk_ratelimit()) 2263 ohci_err(ohci, "PCI posted write error\n"); 2264 } 2265 2266 if (unlikely(event & OHCI1394_cycleTooLong)) { 2267 if (printk_ratelimit()) 2268 ohci_notice(ohci, "isochronous cycle too long\n"); 2269 reg_write(ohci, OHCI1394_LinkControlSet, 2270 OHCI1394_LinkControl_cycleMaster); 2271 } 2272 2273 if (unlikely(event & OHCI1394_cycleInconsistent)) { 2274 /* 2275 * We need to clear this event bit in order to make 2276 * cycleMatch isochronous I/O work. In theory we should 2277 * stop active cycleMatch iso contexts now and restart 2278 * them at least two cycles later. (FIXME?) 2279 */ 2280 if (printk_ratelimit()) 2281 ohci_notice(ohci, "isochronous cycle inconsistent\n"); 2282 } 2283 2284 if (unlikely(event & OHCI1394_unrecoverableError)) 2285 handle_dead_contexts(ohci); 2286 2287 if (event & OHCI1394_cycle64Seconds) { 2288 guard(spinlock)(&ohci->lock); 2289 update_bus_time(ohci); 2290 } else 2291 flush_writes(ohci); 2292 2293 return IRQ_HANDLED; 2294 } 2295 2296 static int software_reset(struct fw_ohci *ohci) 2297 { 2298 u32 val; 2299 int i; 2300 2301 reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset); 2302 for (i = 0; i < 500; i++) { 2303 val = reg_read(ohci, OHCI1394_HCControlSet); 2304 if (!~val) 2305 return -ENODEV; /* Card was ejected. */ 2306 2307 if (!(val & OHCI1394_HCControl_softReset)) 2308 return 0; 2309 2310 msleep(1); 2311 } 2312 2313 return -EBUSY; 2314 } 2315 2316 static void copy_config_rom(__be32 *dest, const __be32 *src, size_t length) 2317 { 2318 size_t size = length * 4; 2319 2320 memcpy(dest, src, size); 2321 if (size < CONFIG_ROM_SIZE) 2322 memset(&dest[length], 0, CONFIG_ROM_SIZE - size); 2323 } 2324 2325 static int configure_1394a_enhancements(struct fw_ohci *ohci) 2326 { 2327 bool enable_1394a; 2328 int ret, clear, set, offset; 2329 2330 /* Check if the driver should configure link and PHY. */ 2331 if (!(reg_read(ohci, OHCI1394_HCControlSet) & 2332 OHCI1394_HCControl_programPhyEnable)) 2333 return 0; 2334 2335 /* Paranoia: check whether the PHY supports 1394a, too. */ 2336 enable_1394a = false; 2337 ret = read_phy_reg(ohci, 2); 2338 if (ret < 0) 2339 return ret; 2340 if ((ret & PHY_EXTENDED_REGISTERS) == PHY_EXTENDED_REGISTERS) { 2341 ret = read_paged_phy_reg(ohci, 1, 8); 2342 if (ret < 0) 2343 return ret; 2344 if (ret >= 1) 2345 enable_1394a = true; 2346 } 2347 2348 if (ohci->quirks & QUIRK_NO_1394A) 2349 enable_1394a = false; 2350 2351 /* Configure PHY and link consistently. */ 2352 if (enable_1394a) { 2353 clear = 0; 2354 set = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI; 2355 } else { 2356 clear = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI; 2357 set = 0; 2358 } 2359 ret = update_phy_reg(ohci, 5, clear, set); 2360 if (ret < 0) 2361 return ret; 2362 2363 if (enable_1394a) 2364 offset = OHCI1394_HCControlSet; 2365 else 2366 offset = OHCI1394_HCControlClear; 2367 reg_write(ohci, offset, OHCI1394_HCControl_aPhyEnhanceEnable); 2368 2369 /* Clean up: configuration has been taken care of. */ 2370 reg_write(ohci, OHCI1394_HCControlClear, 2371 OHCI1394_HCControl_programPhyEnable); 2372 2373 return 0; 2374 } 2375 2376 static int probe_tsb41ba3d(struct fw_ohci *ohci) 2377 { 2378 /* TI vendor ID = 0x080028, TSB41BA3D product ID = 0x833005 (sic) */ 2379 static const u8 id[] = { 0x08, 0x00, 0x28, 0x83, 0x30, 0x05, }; 2380 int reg, i; 2381 2382 reg = read_phy_reg(ohci, 2); 2383 if (reg < 0) 2384 return reg; 2385 if ((reg & PHY_EXTENDED_REGISTERS) != PHY_EXTENDED_REGISTERS) 2386 return 0; 2387 2388 for (i = ARRAY_SIZE(id) - 1; i >= 0; i--) { 2389 reg = read_paged_phy_reg(ohci, 1, i + 10); 2390 if (reg < 0) 2391 return reg; 2392 if (reg != id[i]) 2393 return 0; 2394 } 2395 return 1; 2396 } 2397 2398 static int ohci_enable(struct fw_card *card, 2399 const __be32 *config_rom, size_t length) 2400 { 2401 struct fw_ohci *ohci = fw_ohci(card); 2402 u32 lps, version, irqs; 2403 int i, ret; 2404 2405 ret = software_reset(ohci); 2406 if (ret < 0) { 2407 ohci_err(ohci, "failed to reset ohci card\n"); 2408 return ret; 2409 } 2410 2411 /* 2412 * Now enable LPS, which we need in order to start accessing 2413 * most of the registers. In fact, on some cards (ALI M5251), 2414 * accessing registers in the SClk domain without LPS enabled 2415 * will lock up the machine. Wait 50msec to make sure we have 2416 * full link enabled. However, with some cards (well, at least 2417 * a JMicron PCIe card), we have to try again sometimes. 2418 * 2419 * TI TSB82AA2 + TSB81BA3(A) cards signal LPS enabled early but 2420 * cannot actually use the phy at that time. These need tens of 2421 * millisecods pause between LPS write and first phy access too. 2422 */ 2423 2424 reg_write(ohci, OHCI1394_HCControlSet, 2425 OHCI1394_HCControl_LPS | 2426 OHCI1394_HCControl_postedWriteEnable); 2427 flush_writes(ohci); 2428 2429 for (lps = 0, i = 0; !lps && i < 3; i++) { 2430 msleep(50); 2431 lps = reg_read(ohci, OHCI1394_HCControlSet) & 2432 OHCI1394_HCControl_LPS; 2433 } 2434 2435 if (!lps) { 2436 ohci_err(ohci, "failed to set Link Power Status\n"); 2437 return -EIO; 2438 } 2439 2440 if (ohci->quirks & QUIRK_TI_SLLZ059) { 2441 ret = probe_tsb41ba3d(ohci); 2442 if (ret < 0) 2443 return ret; 2444 if (ret) 2445 ohci_notice(ohci, "local TSB41BA3D phy\n"); 2446 else 2447 ohci->quirks &= ~QUIRK_TI_SLLZ059; 2448 } 2449 2450 reg_write(ohci, OHCI1394_HCControlClear, 2451 OHCI1394_HCControl_noByteSwapData); 2452 2453 reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus); 2454 reg_write(ohci, OHCI1394_LinkControlSet, 2455 OHCI1394_LinkControl_cycleTimerEnable | 2456 OHCI1394_LinkControl_cycleMaster); 2457 2458 reg_write(ohci, OHCI1394_ATRetries, 2459 OHCI1394_MAX_AT_REQ_RETRIES | 2460 (OHCI1394_MAX_AT_RESP_RETRIES << 4) | 2461 (OHCI1394_MAX_PHYS_RESP_RETRIES << 8) | 2462 (200 << 16)); 2463 2464 ohci->bus_time_running = false; 2465 2466 for (i = 0; i < 32; i++) 2467 if (ohci->ir_context_support & (1 << i)) 2468 reg_write(ohci, OHCI1394_IsoRcvContextControlClear(i), 2469 IR_CONTEXT_MULTI_CHANNEL_MODE); 2470 2471 version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff; 2472 if (version >= OHCI_VERSION_1_1) { 2473 reg_write(ohci, OHCI1394_InitialChannelsAvailableHi, 2474 0xfffffffe); 2475 card->broadcast_channel_auto_allocated = true; 2476 } 2477 2478 /* Get implemented bits of the priority arbitration request counter. */ 2479 reg_write(ohci, OHCI1394_FairnessControl, 0x3f); 2480 ohci->pri_req_max = reg_read(ohci, OHCI1394_FairnessControl) & 0x3f; 2481 reg_write(ohci, OHCI1394_FairnessControl, 0); 2482 card->priority_budget_implemented = ohci->pri_req_max != 0; 2483 2484 reg_write(ohci, OHCI1394_PhyUpperBound, FW_MAX_PHYSICAL_RANGE >> 16); 2485 reg_write(ohci, OHCI1394_IntEventClear, ~0); 2486 reg_write(ohci, OHCI1394_IntMaskClear, ~0); 2487 2488 ret = configure_1394a_enhancements(ohci); 2489 if (ret < 0) 2490 return ret; 2491 2492 /* Activate link_on bit and contender bit in our self ID packets.*/ 2493 ret = ohci_update_phy_reg(card, 4, 0, PHY_LINK_ACTIVE | PHY_CONTENDER); 2494 if (ret < 0) 2495 return ret; 2496 2497 /* 2498 * When the link is not yet enabled, the atomic config rom 2499 * update mechanism described below in ohci_set_config_rom() 2500 * is not active. We have to update ConfigRomHeader and 2501 * BusOptions manually, and the write to ConfigROMmap takes 2502 * effect immediately. We tie this to the enabling of the 2503 * link, so we have a valid config rom before enabling - the 2504 * OHCI requires that ConfigROMhdr and BusOptions have valid 2505 * values before enabling. 2506 * 2507 * However, when the ConfigROMmap is written, some controllers 2508 * always read back quadlets 0 and 2 from the config rom to 2509 * the ConfigRomHeader and BusOptions registers on bus reset. 2510 * They shouldn't do that in this initial case where the link 2511 * isn't enabled. This means we have to use the same 2512 * workaround here, setting the bus header to 0 and then write 2513 * the right values in the bus reset tasklet. 2514 */ 2515 2516 if (config_rom) { 2517 ohci->next_config_rom = dmam_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE, 2518 &ohci->next_config_rom_bus, GFP_KERNEL); 2519 if (ohci->next_config_rom == NULL) 2520 return -ENOMEM; 2521 2522 copy_config_rom(ohci->next_config_rom, config_rom, length); 2523 } else { 2524 /* 2525 * In the suspend case, config_rom is NULL, which 2526 * means that we just reuse the old config rom. 2527 */ 2528 ohci->next_config_rom = ohci->config_rom; 2529 ohci->next_config_rom_bus = ohci->config_rom_bus; 2530 } 2531 2532 ohci->next_header = ohci->next_config_rom[0]; 2533 ohci->next_config_rom[0] = 0; 2534 reg_write(ohci, OHCI1394_ConfigROMhdr, 0); 2535 reg_write(ohci, OHCI1394_BusOptions, 2536 be32_to_cpu(ohci->next_config_rom[2])); 2537 reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus); 2538 2539 reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000); 2540 2541 irqs = OHCI1394_reqTxComplete | OHCI1394_respTxComplete | 2542 OHCI1394_RQPkt | OHCI1394_RSPkt | 2543 OHCI1394_isochTx | OHCI1394_isochRx | 2544 OHCI1394_postedWriteErr | 2545 OHCI1394_selfIDComplete | 2546 OHCI1394_regAccessFail | 2547 OHCI1394_cycleInconsistent | 2548 OHCI1394_unrecoverableError | 2549 OHCI1394_cycleTooLong | 2550 OHCI1394_masterIntEnable | 2551 OHCI1394_busReset; 2552 reg_write(ohci, OHCI1394_IntMaskSet, irqs); 2553 2554 reg_write(ohci, OHCI1394_HCControlSet, 2555 OHCI1394_HCControl_linkEnable | 2556 OHCI1394_HCControl_BIBimageValid); 2557 2558 reg_write(ohci, OHCI1394_LinkControlSet, 2559 OHCI1394_LinkControl_rcvSelfID | 2560 OHCI1394_LinkControl_rcvPhyPkt); 2561 2562 ar_context_run(&ohci->ar_request_ctx); 2563 ar_context_run(&ohci->ar_response_ctx); 2564 2565 flush_writes(ohci); 2566 2567 /* We are ready to go, reset bus to finish initialization. */ 2568 fw_schedule_bus_reset(&ohci->card, false, true); 2569 2570 return 0; 2571 } 2572 2573 static int ohci_set_config_rom(struct fw_card *card, 2574 const __be32 *config_rom, size_t length) 2575 { 2576 struct fw_ohci *ohci; 2577 __be32 *next_config_rom; 2578 dma_addr_t next_config_rom_bus; 2579 2580 ohci = fw_ohci(card); 2581 2582 /* 2583 * When the OHCI controller is enabled, the config rom update 2584 * mechanism is a bit tricky, but easy enough to use. See 2585 * section 5.5.6 in the OHCI specification. 2586 * 2587 * The OHCI controller caches the new config rom address in a 2588 * shadow register (ConfigROMmapNext) and needs a bus reset 2589 * for the changes to take place. When the bus reset is 2590 * detected, the controller loads the new values for the 2591 * ConfigRomHeader and BusOptions registers from the specified 2592 * config rom and loads ConfigROMmap from the ConfigROMmapNext 2593 * shadow register. All automatically and atomically. 2594 * 2595 * Now, there's a twist to this story. The automatic load of 2596 * ConfigRomHeader and BusOptions doesn't honor the 2597 * noByteSwapData bit, so with a be32 config rom, the 2598 * controller will load be32 values in to these registers 2599 * during the atomic update, even on litte endian 2600 * architectures. The workaround we use is to put a 0 in the 2601 * header quadlet; 0 is endian agnostic and means that the 2602 * config rom isn't ready yet. In the bus reset tasklet we 2603 * then set up the real values for the two registers. 2604 * 2605 * We use ohci->lock to avoid racing with the code that sets 2606 * ohci->next_config_rom to NULL (see bus_reset_work). 2607 */ 2608 2609 next_config_rom = dmam_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE, 2610 &next_config_rom_bus, GFP_KERNEL); 2611 if (next_config_rom == NULL) 2612 return -ENOMEM; 2613 2614 scoped_guard(spinlock_irq, &ohci->lock) { 2615 // If there is not an already pending config_rom update, push our new allocation 2616 // into the ohci->next_config_rom and then mark the local variable as null so that 2617 // we won't deallocate the new buffer. 2618 // 2619 // OTOH, if there is a pending config_rom update, just use that buffer with the new 2620 // config_rom data, and let this routine free the unused DMA allocation. 2621 if (ohci->next_config_rom == NULL) { 2622 ohci->next_config_rom = next_config_rom; 2623 ohci->next_config_rom_bus = next_config_rom_bus; 2624 next_config_rom = NULL; 2625 } 2626 2627 copy_config_rom(ohci->next_config_rom, config_rom, length); 2628 2629 ohci->next_header = config_rom[0]; 2630 ohci->next_config_rom[0] = 0; 2631 2632 reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus); 2633 } 2634 2635 /* If we didn't use the DMA allocation, delete it. */ 2636 if (next_config_rom != NULL) { 2637 dmam_free_coherent(ohci->card.device, CONFIG_ROM_SIZE, next_config_rom, 2638 next_config_rom_bus); 2639 } 2640 2641 /* 2642 * Now initiate a bus reset to have the changes take 2643 * effect. We clean up the old config rom memory and DMA 2644 * mappings in the bus reset tasklet, since the OHCI 2645 * controller could need to access it before the bus reset 2646 * takes effect. 2647 */ 2648 2649 fw_schedule_bus_reset(&ohci->card, true, true); 2650 2651 return 0; 2652 } 2653 2654 static void ohci_send_request(struct fw_card *card, struct fw_packet *packet) 2655 { 2656 struct fw_ohci *ohci = fw_ohci(card); 2657 2658 at_context_transmit(&ohci->at_request_ctx, packet); 2659 } 2660 2661 static void ohci_send_response(struct fw_card *card, struct fw_packet *packet) 2662 { 2663 struct fw_ohci *ohci = fw_ohci(card); 2664 2665 at_context_transmit(&ohci->at_response_ctx, packet); 2666 } 2667 2668 static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet) 2669 { 2670 struct fw_ohci *ohci = fw_ohci(card); 2671 struct context *ctx = &ohci->at_request_ctx; 2672 struct driver_data *driver_data = packet->driver_data; 2673 int ret = -ENOENT; 2674 2675 tasklet_disable_in_atomic(&ctx->tasklet); 2676 2677 if (packet->ack != 0) 2678 goto out; 2679 2680 if (packet->payload_mapped) 2681 dma_unmap_single(ohci->card.device, packet->payload_bus, 2682 packet->payload_length, DMA_TO_DEVICE); 2683 2684 log_ar_at_event(ohci, 'T', packet->speed, packet->header, 0x20); 2685 driver_data->packet = NULL; 2686 packet->ack = RCODE_CANCELLED; 2687 2688 // Timestamping on behalf of the hardware. 2689 packet->timestamp = cycle_time_to_ohci_tstamp(get_cycle_time(ohci)); 2690 2691 packet->callback(packet, &ohci->card, packet->ack); 2692 ret = 0; 2693 out: 2694 tasklet_enable(&ctx->tasklet); 2695 2696 return ret; 2697 } 2698 2699 static int ohci_enable_phys_dma(struct fw_card *card, 2700 int node_id, int generation) 2701 { 2702 struct fw_ohci *ohci = fw_ohci(card); 2703 int n, ret = 0; 2704 2705 if (param_remote_dma) 2706 return 0; 2707 2708 /* 2709 * FIXME: Make sure this bitmask is cleared when we clear the busReset 2710 * interrupt bit. Clear physReqResourceAllBuses on bus reset. 2711 */ 2712 2713 guard(spinlock_irqsave)(&ohci->lock); 2714 2715 if (ohci->generation != generation) 2716 return -ESTALE; 2717 2718 /* 2719 * Note, if the node ID contains a non-local bus ID, physical DMA is 2720 * enabled for _all_ nodes on remote buses. 2721 */ 2722 2723 n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63; 2724 if (n < 32) 2725 reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n); 2726 else 2727 reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32)); 2728 2729 flush_writes(ohci); 2730 2731 return ret; 2732 } 2733 2734 static u32 ohci_read_csr(struct fw_card *card, int csr_offset) 2735 { 2736 struct fw_ohci *ohci = fw_ohci(card); 2737 u32 value; 2738 2739 switch (csr_offset) { 2740 case CSR_STATE_CLEAR: 2741 case CSR_STATE_SET: 2742 if (ohci->is_root && 2743 (reg_read(ohci, OHCI1394_LinkControlSet) & 2744 OHCI1394_LinkControl_cycleMaster)) 2745 value = CSR_STATE_BIT_CMSTR; 2746 else 2747 value = 0; 2748 if (ohci->csr_state_setclear_abdicate) 2749 value |= CSR_STATE_BIT_ABDICATE; 2750 2751 return value; 2752 2753 case CSR_NODE_IDS: 2754 return reg_read(ohci, OHCI1394_NodeID) << 16; 2755 2756 case CSR_CYCLE_TIME: 2757 return get_cycle_time(ohci); 2758 2759 case CSR_BUS_TIME: 2760 { 2761 // We might be called just after the cycle timer has wrapped around but just before 2762 // the cycle64Seconds handler, so we better check here, too, if the bus time needs 2763 // to be updated. 2764 2765 guard(spinlock_irqsave)(&ohci->lock); 2766 return update_bus_time(ohci); 2767 } 2768 case CSR_BUSY_TIMEOUT: 2769 value = reg_read(ohci, OHCI1394_ATRetries); 2770 return (value >> 4) & 0x0ffff00f; 2771 2772 case CSR_PRIORITY_BUDGET: 2773 return (reg_read(ohci, OHCI1394_FairnessControl) & 0x3f) | 2774 (ohci->pri_req_max << 8); 2775 2776 default: 2777 WARN_ON(1); 2778 return 0; 2779 } 2780 } 2781 2782 static void ohci_write_csr(struct fw_card *card, int csr_offset, u32 value) 2783 { 2784 struct fw_ohci *ohci = fw_ohci(card); 2785 2786 switch (csr_offset) { 2787 case CSR_STATE_CLEAR: 2788 if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) { 2789 reg_write(ohci, OHCI1394_LinkControlClear, 2790 OHCI1394_LinkControl_cycleMaster); 2791 flush_writes(ohci); 2792 } 2793 if (value & CSR_STATE_BIT_ABDICATE) 2794 ohci->csr_state_setclear_abdicate = false; 2795 break; 2796 2797 case CSR_STATE_SET: 2798 if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) { 2799 reg_write(ohci, OHCI1394_LinkControlSet, 2800 OHCI1394_LinkControl_cycleMaster); 2801 flush_writes(ohci); 2802 } 2803 if (value & CSR_STATE_BIT_ABDICATE) 2804 ohci->csr_state_setclear_abdicate = true; 2805 break; 2806 2807 case CSR_NODE_IDS: 2808 reg_write(ohci, OHCI1394_NodeID, value >> 16); 2809 flush_writes(ohci); 2810 break; 2811 2812 case CSR_CYCLE_TIME: 2813 reg_write(ohci, OHCI1394_IsochronousCycleTimer, value); 2814 reg_write(ohci, OHCI1394_IntEventSet, 2815 OHCI1394_cycleInconsistent); 2816 flush_writes(ohci); 2817 break; 2818 2819 case CSR_BUS_TIME: 2820 { 2821 guard(spinlock_irqsave)(&ohci->lock); 2822 ohci->bus_time = (update_bus_time(ohci) & 0x40) | (value & ~0x7f); 2823 break; 2824 } 2825 case CSR_BUSY_TIMEOUT: 2826 value = (value & 0xf) | ((value & 0xf) << 4) | 2827 ((value & 0xf) << 8) | ((value & 0x0ffff000) << 4); 2828 reg_write(ohci, OHCI1394_ATRetries, value); 2829 flush_writes(ohci); 2830 break; 2831 2832 case CSR_PRIORITY_BUDGET: 2833 reg_write(ohci, OHCI1394_FairnessControl, value & 0x3f); 2834 flush_writes(ohci); 2835 break; 2836 2837 default: 2838 WARN_ON(1); 2839 break; 2840 } 2841 } 2842 2843 static void flush_iso_completions(struct iso_context *ctx, enum fw_iso_context_completions_cause cause) 2844 { 2845 trace_isoc_inbound_single_completions(&ctx->base, ctx->last_timestamp, cause, ctx->header, 2846 ctx->header_length); 2847 trace_isoc_outbound_completions(&ctx->base, ctx->last_timestamp, cause, ctx->header, 2848 ctx->header_length); 2849 2850 ctx->base.callback.sc(&ctx->base, ctx->last_timestamp, 2851 ctx->header_length, ctx->header, 2852 ctx->base.callback_data); 2853 ctx->header_length = 0; 2854 } 2855 2856 static void copy_iso_headers(struct iso_context *ctx, const u32 *dma_hdr) 2857 { 2858 u32 *ctx_hdr; 2859 2860 if (ctx->header_length + ctx->base.header_size > PAGE_SIZE) { 2861 if (ctx->base.drop_overflow_headers) 2862 return; 2863 flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_HEADER_OVERFLOW); 2864 } 2865 2866 ctx_hdr = ctx->header + ctx->header_length; 2867 ctx->last_timestamp = (u16)le32_to_cpu((__force __le32)dma_hdr[0]); 2868 2869 /* 2870 * The two iso header quadlets are byteswapped to little 2871 * endian by the controller, but we want to present them 2872 * as big endian for consistency with the bus endianness. 2873 */ 2874 if (ctx->base.header_size > 0) 2875 ctx_hdr[0] = swab32(dma_hdr[1]); /* iso packet header */ 2876 if (ctx->base.header_size > 4) 2877 ctx_hdr[1] = swab32(dma_hdr[0]); /* timestamp */ 2878 if (ctx->base.header_size > 8) 2879 memcpy(&ctx_hdr[2], &dma_hdr[2], ctx->base.header_size - 8); 2880 ctx->header_length += ctx->base.header_size; 2881 } 2882 2883 static int handle_ir_packet_per_buffer(struct context *context, 2884 struct descriptor *d, 2885 struct descriptor *last) 2886 { 2887 struct iso_context *ctx = 2888 container_of(context, struct iso_context, context); 2889 struct descriptor *pd; 2890 u32 buffer_dma; 2891 2892 for (pd = d; pd <= last; pd++) 2893 if (pd->transfer_status) 2894 break; 2895 if (pd > last) 2896 /* Descriptor(s) not done yet, stop iteration */ 2897 return 0; 2898 2899 while (!(d->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))) { 2900 d++; 2901 buffer_dma = le32_to_cpu(d->data_address); 2902 dma_sync_single_range_for_cpu(context->ohci->card.device, 2903 buffer_dma & PAGE_MASK, 2904 buffer_dma & ~PAGE_MASK, 2905 le16_to_cpu(d->req_count), 2906 DMA_FROM_DEVICE); 2907 } 2908 2909 copy_iso_headers(ctx, (u32 *) (last + 1)); 2910 2911 if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) 2912 flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_IRQ); 2913 2914 return 1; 2915 } 2916 2917 /* d == last because each descriptor block is only a single descriptor. */ 2918 static int handle_ir_buffer_fill(struct context *context, 2919 struct descriptor *d, 2920 struct descriptor *last) 2921 { 2922 struct iso_context *ctx = 2923 container_of(context, struct iso_context, context); 2924 unsigned int req_count, res_count, completed; 2925 u32 buffer_dma; 2926 2927 req_count = le16_to_cpu(last->req_count); 2928 res_count = le16_to_cpu(READ_ONCE(last->res_count)); 2929 completed = req_count - res_count; 2930 buffer_dma = le32_to_cpu(last->data_address); 2931 2932 if (completed > 0) { 2933 ctx->mc_buffer_bus = buffer_dma; 2934 ctx->mc_completed = completed; 2935 } 2936 2937 if (res_count != 0) 2938 /* Descriptor(s) not done yet, stop iteration */ 2939 return 0; 2940 2941 dma_sync_single_range_for_cpu(context->ohci->card.device, 2942 buffer_dma & PAGE_MASK, 2943 buffer_dma & ~PAGE_MASK, 2944 completed, DMA_FROM_DEVICE); 2945 2946 if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) { 2947 trace_isoc_inbound_multiple_completions(&ctx->base, completed, 2948 FW_ISO_CONTEXT_COMPLETIONS_CAUSE_IRQ); 2949 2950 ctx->base.callback.mc(&ctx->base, 2951 buffer_dma + completed, 2952 ctx->base.callback_data); 2953 ctx->mc_completed = 0; 2954 } 2955 2956 return 1; 2957 } 2958 2959 static void flush_ir_buffer_fill(struct iso_context *ctx) 2960 { 2961 dma_sync_single_range_for_cpu(ctx->context.ohci->card.device, 2962 ctx->mc_buffer_bus & PAGE_MASK, 2963 ctx->mc_buffer_bus & ~PAGE_MASK, 2964 ctx->mc_completed, DMA_FROM_DEVICE); 2965 2966 trace_isoc_inbound_multiple_completions(&ctx->base, ctx->mc_completed, 2967 FW_ISO_CONTEXT_COMPLETIONS_CAUSE_FLUSH); 2968 2969 ctx->base.callback.mc(&ctx->base, 2970 ctx->mc_buffer_bus + ctx->mc_completed, 2971 ctx->base.callback_data); 2972 ctx->mc_completed = 0; 2973 } 2974 2975 static inline void sync_it_packet_for_cpu(struct context *context, 2976 struct descriptor *pd) 2977 { 2978 __le16 control; 2979 u32 buffer_dma; 2980 2981 /* only packets beginning with OUTPUT_MORE* have data buffers */ 2982 if (pd->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS)) 2983 return; 2984 2985 /* skip over the OUTPUT_MORE_IMMEDIATE descriptor */ 2986 pd += 2; 2987 2988 /* 2989 * If the packet has a header, the first OUTPUT_MORE/LAST descriptor's 2990 * data buffer is in the context program's coherent page and must not 2991 * be synced. 2992 */ 2993 if ((le32_to_cpu(pd->data_address) & PAGE_MASK) == 2994 (context->current_bus & PAGE_MASK)) { 2995 if (pd->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS)) 2996 return; 2997 pd++; 2998 } 2999 3000 do { 3001 buffer_dma = le32_to_cpu(pd->data_address); 3002 dma_sync_single_range_for_cpu(context->ohci->card.device, 3003 buffer_dma & PAGE_MASK, 3004 buffer_dma & ~PAGE_MASK, 3005 le16_to_cpu(pd->req_count), 3006 DMA_TO_DEVICE); 3007 control = pd->control; 3008 pd++; 3009 } while (!(control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))); 3010 } 3011 3012 static int handle_it_packet(struct context *context, 3013 struct descriptor *d, 3014 struct descriptor *last) 3015 { 3016 struct iso_context *ctx = 3017 container_of(context, struct iso_context, context); 3018 struct descriptor *pd; 3019 __be32 *ctx_hdr; 3020 3021 for (pd = d; pd <= last; pd++) 3022 if (pd->transfer_status) 3023 break; 3024 if (pd > last) 3025 /* Descriptor(s) not done yet, stop iteration */ 3026 return 0; 3027 3028 sync_it_packet_for_cpu(context, d); 3029 3030 if (ctx->header_length + 4 > PAGE_SIZE) { 3031 if (ctx->base.drop_overflow_headers) 3032 return 1; 3033 flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_HEADER_OVERFLOW); 3034 } 3035 3036 ctx_hdr = ctx->header + ctx->header_length; 3037 ctx->last_timestamp = le16_to_cpu(last->res_count); 3038 /* Present this value as big-endian to match the receive code */ 3039 *ctx_hdr = cpu_to_be32((le16_to_cpu(pd->transfer_status) << 16) | 3040 le16_to_cpu(pd->res_count)); 3041 ctx->header_length += 4; 3042 3043 if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) 3044 flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_IRQ); 3045 3046 return 1; 3047 } 3048 3049 static void set_multichannel_mask(struct fw_ohci *ohci, u64 channels) 3050 { 3051 u32 hi = channels >> 32, lo = channels; 3052 3053 reg_write(ohci, OHCI1394_IRMultiChanMaskHiClear, ~hi); 3054 reg_write(ohci, OHCI1394_IRMultiChanMaskLoClear, ~lo); 3055 reg_write(ohci, OHCI1394_IRMultiChanMaskHiSet, hi); 3056 reg_write(ohci, OHCI1394_IRMultiChanMaskLoSet, lo); 3057 ohci->mc_channels = channels; 3058 } 3059 3060 static struct fw_iso_context *ohci_allocate_iso_context(struct fw_card *card, 3061 int type, int channel, size_t header_size) 3062 { 3063 struct fw_ohci *ohci = fw_ohci(card); 3064 struct iso_context *ctx; 3065 descriptor_callback_t callback; 3066 u64 *channels; 3067 u32 *mask, regs; 3068 int index, ret = -EBUSY; 3069 3070 scoped_guard(spinlock_irq, &ohci->lock) { 3071 switch (type) { 3072 case FW_ISO_CONTEXT_TRANSMIT: 3073 mask = &ohci->it_context_mask; 3074 callback = handle_it_packet; 3075 index = ffs(*mask) - 1; 3076 if (index >= 0) { 3077 *mask &= ~(1 << index); 3078 regs = OHCI1394_IsoXmitContextBase(index); 3079 ctx = &ohci->it_context_list[index]; 3080 } 3081 break; 3082 3083 case FW_ISO_CONTEXT_RECEIVE: 3084 channels = &ohci->ir_context_channels; 3085 mask = &ohci->ir_context_mask; 3086 callback = handle_ir_packet_per_buffer; 3087 index = *channels & 1ULL << channel ? ffs(*mask) - 1 : -1; 3088 if (index >= 0) { 3089 *channels &= ~(1ULL << channel); 3090 *mask &= ~(1 << index); 3091 regs = OHCI1394_IsoRcvContextBase(index); 3092 ctx = &ohci->ir_context_list[index]; 3093 } 3094 break; 3095 3096 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3097 mask = &ohci->ir_context_mask; 3098 callback = handle_ir_buffer_fill; 3099 index = !ohci->mc_allocated ? ffs(*mask) - 1 : -1; 3100 if (index >= 0) { 3101 ohci->mc_allocated = true; 3102 *mask &= ~(1 << index); 3103 regs = OHCI1394_IsoRcvContextBase(index); 3104 ctx = &ohci->ir_context_list[index]; 3105 } 3106 break; 3107 3108 default: 3109 index = -1; 3110 ret = -ENOSYS; 3111 } 3112 3113 if (index < 0) 3114 return ERR_PTR(ret); 3115 } 3116 3117 memset(ctx, 0, sizeof(*ctx)); 3118 ctx->header_length = 0; 3119 ctx->header = (void *) __get_free_page(GFP_KERNEL); 3120 if (ctx->header == NULL) { 3121 ret = -ENOMEM; 3122 goto out; 3123 } 3124 ret = context_init(&ctx->context, ohci, regs, callback); 3125 if (ret < 0) 3126 goto out_with_header; 3127 3128 if (type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL) { 3129 set_multichannel_mask(ohci, 0); 3130 ctx->mc_completed = 0; 3131 } 3132 3133 return &ctx->base; 3134 3135 out_with_header: 3136 free_page((unsigned long)ctx->header); 3137 out: 3138 scoped_guard(spinlock_irq, &ohci->lock) { 3139 switch (type) { 3140 case FW_ISO_CONTEXT_RECEIVE: 3141 *channels |= 1ULL << channel; 3142 break; 3143 3144 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3145 ohci->mc_allocated = false; 3146 break; 3147 } 3148 *mask |= 1 << index; 3149 } 3150 3151 return ERR_PTR(ret); 3152 } 3153 3154 static int ohci_start_iso(struct fw_iso_context *base, 3155 s32 cycle, u32 sync, u32 tags) 3156 { 3157 struct iso_context *ctx = container_of(base, struct iso_context, base); 3158 struct fw_ohci *ohci = ctx->context.ohci; 3159 u32 control = IR_CONTEXT_ISOCH_HEADER, match; 3160 int index; 3161 3162 /* the controller cannot start without any queued packets */ 3163 if (ctx->context.last->branch_address == 0) 3164 return -ENODATA; 3165 3166 switch (ctx->base.type) { 3167 case FW_ISO_CONTEXT_TRANSMIT: 3168 index = ctx - ohci->it_context_list; 3169 match = 0; 3170 if (cycle >= 0) 3171 match = IT_CONTEXT_CYCLE_MATCH_ENABLE | 3172 (cycle & 0x7fff) << 16; 3173 3174 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index); 3175 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index); 3176 context_run(&ctx->context, match); 3177 break; 3178 3179 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3180 control |= IR_CONTEXT_BUFFER_FILL|IR_CONTEXT_MULTI_CHANNEL_MODE; 3181 fallthrough; 3182 case FW_ISO_CONTEXT_RECEIVE: 3183 index = ctx - ohci->ir_context_list; 3184 match = (tags << 28) | (sync << 8) | ctx->base.channel; 3185 if (cycle >= 0) { 3186 match |= (cycle & 0x07fff) << 12; 3187 control |= IR_CONTEXT_CYCLE_MATCH_ENABLE; 3188 } 3189 3190 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index); 3191 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index); 3192 reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match); 3193 context_run(&ctx->context, control); 3194 3195 ctx->sync = sync; 3196 ctx->tags = tags; 3197 3198 break; 3199 } 3200 3201 return 0; 3202 } 3203 3204 static int ohci_stop_iso(struct fw_iso_context *base) 3205 { 3206 struct fw_ohci *ohci = fw_ohci(base->card); 3207 struct iso_context *ctx = container_of(base, struct iso_context, base); 3208 int index; 3209 3210 switch (ctx->base.type) { 3211 case FW_ISO_CONTEXT_TRANSMIT: 3212 index = ctx - ohci->it_context_list; 3213 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index); 3214 break; 3215 3216 case FW_ISO_CONTEXT_RECEIVE: 3217 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3218 index = ctx - ohci->ir_context_list; 3219 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index); 3220 break; 3221 } 3222 flush_writes(ohci); 3223 context_stop(&ctx->context); 3224 tasklet_kill(&ctx->context.tasklet); 3225 3226 return 0; 3227 } 3228 3229 static void ohci_free_iso_context(struct fw_iso_context *base) 3230 { 3231 struct fw_ohci *ohci = fw_ohci(base->card); 3232 struct iso_context *ctx = container_of(base, struct iso_context, base); 3233 int index; 3234 3235 ohci_stop_iso(base); 3236 context_release(&ctx->context); 3237 free_page((unsigned long)ctx->header); 3238 3239 guard(spinlock_irqsave)(&ohci->lock); 3240 3241 switch (base->type) { 3242 case FW_ISO_CONTEXT_TRANSMIT: 3243 index = ctx - ohci->it_context_list; 3244 ohci->it_context_mask |= 1 << index; 3245 break; 3246 3247 case FW_ISO_CONTEXT_RECEIVE: 3248 index = ctx - ohci->ir_context_list; 3249 ohci->ir_context_mask |= 1 << index; 3250 ohci->ir_context_channels |= 1ULL << base->channel; 3251 break; 3252 3253 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3254 index = ctx - ohci->ir_context_list; 3255 ohci->ir_context_mask |= 1 << index; 3256 ohci->ir_context_channels |= ohci->mc_channels; 3257 ohci->mc_channels = 0; 3258 ohci->mc_allocated = false; 3259 break; 3260 } 3261 } 3262 3263 static int ohci_set_iso_channels(struct fw_iso_context *base, u64 *channels) 3264 { 3265 struct fw_ohci *ohci = fw_ohci(base->card); 3266 3267 switch (base->type) { 3268 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3269 { 3270 guard(spinlock_irqsave)(&ohci->lock); 3271 3272 // Don't allow multichannel to grab other contexts' channels. 3273 if (~ohci->ir_context_channels & ~ohci->mc_channels & *channels) { 3274 *channels = ohci->ir_context_channels; 3275 return -EBUSY; 3276 } else { 3277 set_multichannel_mask(ohci, *channels); 3278 return 0; 3279 } 3280 } 3281 default: 3282 return -EINVAL; 3283 } 3284 } 3285 3286 #ifdef CONFIG_PM 3287 static void ohci_resume_iso_dma(struct fw_ohci *ohci) 3288 { 3289 int i; 3290 struct iso_context *ctx; 3291 3292 for (i = 0 ; i < ohci->n_ir ; i++) { 3293 ctx = &ohci->ir_context_list[i]; 3294 if (ctx->context.running) 3295 ohci_start_iso(&ctx->base, 0, ctx->sync, ctx->tags); 3296 } 3297 3298 for (i = 0 ; i < ohci->n_it ; i++) { 3299 ctx = &ohci->it_context_list[i]; 3300 if (ctx->context.running) 3301 ohci_start_iso(&ctx->base, 0, ctx->sync, ctx->tags); 3302 } 3303 } 3304 #endif 3305 3306 static int queue_iso_transmit(struct iso_context *ctx, 3307 struct fw_iso_packet *packet, 3308 struct fw_iso_buffer *buffer, 3309 unsigned long payload) 3310 { 3311 struct descriptor *d, *last, *pd; 3312 struct fw_iso_packet *p; 3313 __le32 *header; 3314 dma_addr_t d_bus, page_bus; 3315 u32 z, header_z, payload_z, irq; 3316 u32 payload_index, payload_end_index, next_page_index; 3317 int page, end_page, i, length, offset; 3318 3319 p = packet; 3320 payload_index = payload; 3321 3322 if (p->skip) 3323 z = 1; 3324 else 3325 z = 2; 3326 if (p->header_length > 0) 3327 z++; 3328 3329 /* Determine the first page the payload isn't contained in. */ 3330 end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT; 3331 if (p->payload_length > 0) 3332 payload_z = end_page - (payload_index >> PAGE_SHIFT); 3333 else 3334 payload_z = 0; 3335 3336 z += payload_z; 3337 3338 /* Get header size in number of descriptors. */ 3339 header_z = DIV_ROUND_UP(p->header_length, sizeof(*d)); 3340 3341 d = context_get_descriptors(&ctx->context, z + header_z, &d_bus); 3342 if (d == NULL) 3343 return -ENOMEM; 3344 3345 if (!p->skip) { 3346 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE); 3347 d[0].req_count = cpu_to_le16(8); 3348 /* 3349 * Link the skip address to this descriptor itself. This causes 3350 * a context to skip a cycle whenever lost cycles or FIFO 3351 * overruns occur, without dropping the data. The application 3352 * should then decide whether this is an error condition or not. 3353 * FIXME: Make the context's cycle-lost behaviour configurable? 3354 */ 3355 d[0].branch_address = cpu_to_le32(d_bus | z); 3356 3357 header = (__le32 *) &d[1]; 3358 3359 ohci1394_it_data_set_speed(header, ctx->base.speed); 3360 ohci1394_it_data_set_tag(header, p->tag); 3361 ohci1394_it_data_set_channel(header, ctx->base.channel); 3362 ohci1394_it_data_set_tcode(header, TCODE_STREAM_DATA); 3363 ohci1394_it_data_set_sync(header, p->sy); 3364 3365 ohci1394_it_data_set_data_length(header, p->header_length + p->payload_length); 3366 } 3367 3368 if (p->header_length > 0) { 3369 d[2].req_count = cpu_to_le16(p->header_length); 3370 d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d)); 3371 memcpy(&d[z], p->header, p->header_length); 3372 } 3373 3374 pd = d + z - payload_z; 3375 payload_end_index = payload_index + p->payload_length; 3376 for (i = 0; i < payload_z; i++) { 3377 page = payload_index >> PAGE_SHIFT; 3378 offset = payload_index & ~PAGE_MASK; 3379 next_page_index = (page + 1) << PAGE_SHIFT; 3380 length = 3381 min(next_page_index, payload_end_index) - payload_index; 3382 pd[i].req_count = cpu_to_le16(length); 3383 3384 page_bus = page_private(buffer->pages[page]); 3385 pd[i].data_address = cpu_to_le32(page_bus + offset); 3386 3387 dma_sync_single_range_for_device(ctx->context.ohci->card.device, 3388 page_bus, offset, length, 3389 DMA_TO_DEVICE); 3390 3391 payload_index += length; 3392 } 3393 3394 if (p->interrupt) 3395 irq = DESCRIPTOR_IRQ_ALWAYS; 3396 else 3397 irq = DESCRIPTOR_NO_IRQ; 3398 3399 last = z == 2 ? d : d + z - 1; 3400 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST | 3401 DESCRIPTOR_STATUS | 3402 DESCRIPTOR_BRANCH_ALWAYS | 3403 irq); 3404 3405 context_append(&ctx->context, d, z, header_z); 3406 3407 return 0; 3408 } 3409 3410 static int queue_iso_packet_per_buffer(struct iso_context *ctx, 3411 struct fw_iso_packet *packet, 3412 struct fw_iso_buffer *buffer, 3413 unsigned long payload) 3414 { 3415 struct device *device = ctx->context.ohci->card.device; 3416 struct descriptor *d, *pd; 3417 dma_addr_t d_bus, page_bus; 3418 u32 z, header_z, rest; 3419 int i, j, length; 3420 int page, offset, packet_count, header_size, payload_per_buffer; 3421 3422 /* 3423 * The OHCI controller puts the isochronous header and trailer in the 3424 * buffer, so we need at least 8 bytes. 3425 */ 3426 packet_count = packet->header_length / ctx->base.header_size; 3427 header_size = max(ctx->base.header_size, (size_t)8); 3428 3429 /* Get header size in number of descriptors. */ 3430 header_z = DIV_ROUND_UP(header_size, sizeof(*d)); 3431 page = payload >> PAGE_SHIFT; 3432 offset = payload & ~PAGE_MASK; 3433 payload_per_buffer = packet->payload_length / packet_count; 3434 3435 for (i = 0; i < packet_count; i++) { 3436 /* d points to the header descriptor */ 3437 z = DIV_ROUND_UP(payload_per_buffer + offset, PAGE_SIZE) + 1; 3438 d = context_get_descriptors(&ctx->context, 3439 z + header_z, &d_bus); 3440 if (d == NULL) 3441 return -ENOMEM; 3442 3443 d->control = cpu_to_le16(DESCRIPTOR_STATUS | 3444 DESCRIPTOR_INPUT_MORE); 3445 if (packet->skip && i == 0) 3446 d->control |= cpu_to_le16(DESCRIPTOR_WAIT); 3447 d->req_count = cpu_to_le16(header_size); 3448 d->res_count = d->req_count; 3449 d->transfer_status = 0; 3450 d->data_address = cpu_to_le32(d_bus + (z * sizeof(*d))); 3451 3452 rest = payload_per_buffer; 3453 pd = d; 3454 for (j = 1; j < z; j++) { 3455 pd++; 3456 pd->control = cpu_to_le16(DESCRIPTOR_STATUS | 3457 DESCRIPTOR_INPUT_MORE); 3458 3459 if (offset + rest < PAGE_SIZE) 3460 length = rest; 3461 else 3462 length = PAGE_SIZE - offset; 3463 pd->req_count = cpu_to_le16(length); 3464 pd->res_count = pd->req_count; 3465 pd->transfer_status = 0; 3466 3467 page_bus = page_private(buffer->pages[page]); 3468 pd->data_address = cpu_to_le32(page_bus + offset); 3469 3470 dma_sync_single_range_for_device(device, page_bus, 3471 offset, length, 3472 DMA_FROM_DEVICE); 3473 3474 offset = (offset + length) & ~PAGE_MASK; 3475 rest -= length; 3476 if (offset == 0) 3477 page++; 3478 } 3479 pd->control = cpu_to_le16(DESCRIPTOR_STATUS | 3480 DESCRIPTOR_INPUT_LAST | 3481 DESCRIPTOR_BRANCH_ALWAYS); 3482 if (packet->interrupt && i == packet_count - 1) 3483 pd->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS); 3484 3485 context_append(&ctx->context, d, z, header_z); 3486 } 3487 3488 return 0; 3489 } 3490 3491 static int queue_iso_buffer_fill(struct iso_context *ctx, 3492 struct fw_iso_packet *packet, 3493 struct fw_iso_buffer *buffer, 3494 unsigned long payload) 3495 { 3496 struct descriptor *d; 3497 dma_addr_t d_bus, page_bus; 3498 int page, offset, rest, z, i, length; 3499 3500 page = payload >> PAGE_SHIFT; 3501 offset = payload & ~PAGE_MASK; 3502 rest = packet->payload_length; 3503 3504 /* We need one descriptor for each page in the buffer. */ 3505 z = DIV_ROUND_UP(offset + rest, PAGE_SIZE); 3506 3507 if (WARN_ON(offset & 3 || rest & 3 || page + z > buffer->page_count)) 3508 return -EFAULT; 3509 3510 for (i = 0; i < z; i++) { 3511 d = context_get_descriptors(&ctx->context, 1, &d_bus); 3512 if (d == NULL) 3513 return -ENOMEM; 3514 3515 d->control = cpu_to_le16(DESCRIPTOR_INPUT_MORE | 3516 DESCRIPTOR_BRANCH_ALWAYS); 3517 if (packet->skip && i == 0) 3518 d->control |= cpu_to_le16(DESCRIPTOR_WAIT); 3519 if (packet->interrupt && i == z - 1) 3520 d->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS); 3521 3522 if (offset + rest < PAGE_SIZE) 3523 length = rest; 3524 else 3525 length = PAGE_SIZE - offset; 3526 d->req_count = cpu_to_le16(length); 3527 d->res_count = d->req_count; 3528 d->transfer_status = 0; 3529 3530 page_bus = page_private(buffer->pages[page]); 3531 d->data_address = cpu_to_le32(page_bus + offset); 3532 3533 dma_sync_single_range_for_device(ctx->context.ohci->card.device, 3534 page_bus, offset, length, 3535 DMA_FROM_DEVICE); 3536 3537 rest -= length; 3538 offset = 0; 3539 page++; 3540 3541 context_append(&ctx->context, d, 1, 0); 3542 } 3543 3544 return 0; 3545 } 3546 3547 static int ohci_queue_iso(struct fw_iso_context *base, 3548 struct fw_iso_packet *packet, 3549 struct fw_iso_buffer *buffer, 3550 unsigned long payload) 3551 { 3552 struct iso_context *ctx = container_of(base, struct iso_context, base); 3553 3554 guard(spinlock_irqsave)(&ctx->context.ohci->lock); 3555 3556 switch (base->type) { 3557 case FW_ISO_CONTEXT_TRANSMIT: 3558 return queue_iso_transmit(ctx, packet, buffer, payload); 3559 case FW_ISO_CONTEXT_RECEIVE: 3560 return queue_iso_packet_per_buffer(ctx, packet, buffer, payload); 3561 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3562 return queue_iso_buffer_fill(ctx, packet, buffer, payload); 3563 default: 3564 return -ENOSYS; 3565 } 3566 } 3567 3568 static void ohci_flush_queue_iso(struct fw_iso_context *base) 3569 { 3570 struct context *ctx = 3571 &container_of(base, struct iso_context, base)->context; 3572 3573 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE); 3574 } 3575 3576 static int ohci_flush_iso_completions(struct fw_iso_context *base) 3577 { 3578 struct iso_context *ctx = container_of(base, struct iso_context, base); 3579 int ret = 0; 3580 3581 tasklet_disable_in_atomic(&ctx->context.tasklet); 3582 3583 if (!test_and_set_bit_lock(0, &ctx->flushing_completions)) { 3584 context_tasklet((unsigned long)&ctx->context); 3585 3586 switch (base->type) { 3587 case FW_ISO_CONTEXT_TRANSMIT: 3588 case FW_ISO_CONTEXT_RECEIVE: 3589 if (ctx->header_length != 0) 3590 flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_FLUSH); 3591 break; 3592 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3593 if (ctx->mc_completed != 0) 3594 flush_ir_buffer_fill(ctx); 3595 break; 3596 default: 3597 ret = -ENOSYS; 3598 } 3599 3600 clear_bit_unlock(0, &ctx->flushing_completions); 3601 smp_mb__after_atomic(); 3602 } 3603 3604 tasklet_enable(&ctx->context.tasklet); 3605 3606 return ret; 3607 } 3608 3609 static const struct fw_card_driver ohci_driver = { 3610 .enable = ohci_enable, 3611 .read_phy_reg = ohci_read_phy_reg, 3612 .update_phy_reg = ohci_update_phy_reg, 3613 .set_config_rom = ohci_set_config_rom, 3614 .send_request = ohci_send_request, 3615 .send_response = ohci_send_response, 3616 .cancel_packet = ohci_cancel_packet, 3617 .enable_phys_dma = ohci_enable_phys_dma, 3618 .read_csr = ohci_read_csr, 3619 .write_csr = ohci_write_csr, 3620 3621 .allocate_iso_context = ohci_allocate_iso_context, 3622 .free_iso_context = ohci_free_iso_context, 3623 .set_iso_channels = ohci_set_iso_channels, 3624 .queue_iso = ohci_queue_iso, 3625 .flush_queue_iso = ohci_flush_queue_iso, 3626 .flush_iso_completions = ohci_flush_iso_completions, 3627 .start_iso = ohci_start_iso, 3628 .stop_iso = ohci_stop_iso, 3629 }; 3630 3631 #ifdef CONFIG_PPC_PMAC 3632 static void pmac_ohci_on(struct pci_dev *dev) 3633 { 3634 if (machine_is(powermac)) { 3635 struct device_node *ofn = pci_device_to_OF_node(dev); 3636 3637 if (ofn) { 3638 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 1); 3639 pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 1); 3640 } 3641 } 3642 } 3643 3644 static void pmac_ohci_off(struct pci_dev *dev) 3645 { 3646 if (machine_is(powermac)) { 3647 struct device_node *ofn = pci_device_to_OF_node(dev); 3648 3649 if (ofn) { 3650 pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 0); 3651 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 0); 3652 } 3653 } 3654 } 3655 #else 3656 static inline void pmac_ohci_on(struct pci_dev *dev) {} 3657 static inline void pmac_ohci_off(struct pci_dev *dev) {} 3658 #endif /* CONFIG_PPC_PMAC */ 3659 3660 static void release_ohci(struct device *dev, void *data) 3661 { 3662 struct pci_dev *pdev = to_pci_dev(dev); 3663 struct fw_ohci *ohci = pci_get_drvdata(pdev); 3664 3665 pmac_ohci_off(pdev); 3666 3667 ar_context_release(&ohci->ar_response_ctx); 3668 ar_context_release(&ohci->ar_request_ctx); 3669 3670 dev_notice(dev, "removed fw-ohci device\n"); 3671 } 3672 3673 static int pci_probe(struct pci_dev *dev, 3674 const struct pci_device_id *ent) 3675 { 3676 struct fw_ohci *ohci; 3677 u32 bus_options, max_receive, link_speed, version; 3678 u64 guid; 3679 int i, flags, irq, err; 3680 size_t size; 3681 3682 if (dev->vendor == PCI_VENDOR_ID_PINNACLE_SYSTEMS) { 3683 dev_err(&dev->dev, "Pinnacle MovieBoard is not yet supported\n"); 3684 return -ENOSYS; 3685 } 3686 3687 ohci = devres_alloc(release_ohci, sizeof(*ohci), GFP_KERNEL); 3688 if (ohci == NULL) 3689 return -ENOMEM; 3690 fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev); 3691 pci_set_drvdata(dev, ohci); 3692 pmac_ohci_on(dev); 3693 devres_add(&dev->dev, ohci); 3694 3695 err = pcim_enable_device(dev); 3696 if (err) { 3697 dev_err(&dev->dev, "failed to enable OHCI hardware\n"); 3698 return err; 3699 } 3700 3701 pci_set_master(dev); 3702 pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0); 3703 3704 spin_lock_init(&ohci->lock); 3705 mutex_init(&ohci->phy_reg_mutex); 3706 3707 INIT_WORK(&ohci->bus_reset_work, bus_reset_work); 3708 3709 if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM) || 3710 pci_resource_len(dev, 0) < OHCI1394_REGISTER_SIZE) { 3711 ohci_err(ohci, "invalid MMIO resource\n"); 3712 return -ENXIO; 3713 } 3714 3715 err = pcim_iomap_regions(dev, 1 << 0, ohci_driver_name); 3716 if (err) { 3717 ohci_err(ohci, "request and map MMIO resource unavailable\n"); 3718 return -ENXIO; 3719 } 3720 ohci->registers = pcim_iomap_table(dev)[0]; 3721 3722 for (i = 0; i < ARRAY_SIZE(ohci_quirks); i++) 3723 if ((ohci_quirks[i].vendor == dev->vendor) && 3724 (ohci_quirks[i].device == (unsigned short)PCI_ANY_ID || 3725 ohci_quirks[i].device == dev->device) && 3726 (ohci_quirks[i].revision == (unsigned short)PCI_ANY_ID || 3727 ohci_quirks[i].revision >= dev->revision)) { 3728 ohci->quirks = ohci_quirks[i].flags; 3729 break; 3730 } 3731 if (param_quirks) 3732 ohci->quirks = param_quirks; 3733 3734 if (detect_vt630x_with_asm1083_on_amd_ryzen_machine(dev)) 3735 ohci->quirks |= QUIRK_REBOOT_BY_CYCLE_TIMER_READ; 3736 3737 /* 3738 * Because dma_alloc_coherent() allocates at least one page, 3739 * we save space by using a common buffer for the AR request/ 3740 * response descriptors and the self IDs buffer. 3741 */ 3742 BUILD_BUG_ON(AR_BUFFERS * sizeof(struct descriptor) > PAGE_SIZE/4); 3743 BUILD_BUG_ON(SELF_ID_BUF_SIZE > PAGE_SIZE/2); 3744 ohci->misc_buffer = dmam_alloc_coherent(&dev->dev, PAGE_SIZE, &ohci->misc_buffer_bus, 3745 GFP_KERNEL); 3746 if (!ohci->misc_buffer) 3747 return -ENOMEM; 3748 3749 err = ar_context_init(&ohci->ar_request_ctx, ohci, 0, 3750 OHCI1394_AsReqRcvContextControlSet); 3751 if (err < 0) 3752 return err; 3753 3754 err = ar_context_init(&ohci->ar_response_ctx, ohci, PAGE_SIZE/4, 3755 OHCI1394_AsRspRcvContextControlSet); 3756 if (err < 0) 3757 return err; 3758 3759 err = context_init(&ohci->at_request_ctx, ohci, 3760 OHCI1394_AsReqTrContextControlSet, handle_at_packet); 3761 if (err < 0) 3762 return err; 3763 3764 err = context_init(&ohci->at_response_ctx, ohci, 3765 OHCI1394_AsRspTrContextControlSet, handle_at_packet); 3766 if (err < 0) 3767 return err; 3768 3769 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0); 3770 ohci->ir_context_channels = ~0ULL; 3771 ohci->ir_context_support = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet); 3772 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0); 3773 ohci->ir_context_mask = ohci->ir_context_support; 3774 ohci->n_ir = hweight32(ohci->ir_context_mask); 3775 size = sizeof(struct iso_context) * ohci->n_ir; 3776 ohci->ir_context_list = devm_kzalloc(&dev->dev, size, GFP_KERNEL); 3777 if (!ohci->ir_context_list) 3778 return -ENOMEM; 3779 3780 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0); 3781 ohci->it_context_support = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet); 3782 /* JMicron JMB38x often shows 0 at first read, just ignore it */ 3783 if (!ohci->it_context_support) { 3784 ohci_notice(ohci, "overriding IsoXmitIntMask\n"); 3785 ohci->it_context_support = 0xf; 3786 } 3787 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0); 3788 ohci->it_context_mask = ohci->it_context_support; 3789 ohci->n_it = hweight32(ohci->it_context_mask); 3790 size = sizeof(struct iso_context) * ohci->n_it; 3791 ohci->it_context_list = devm_kzalloc(&dev->dev, size, GFP_KERNEL); 3792 if (!ohci->it_context_list) 3793 return -ENOMEM; 3794 3795 ohci->self_id = ohci->misc_buffer + PAGE_SIZE/2; 3796 ohci->self_id_bus = ohci->misc_buffer_bus + PAGE_SIZE/2; 3797 3798 bus_options = reg_read(ohci, OHCI1394_BusOptions); 3799 max_receive = (bus_options >> 12) & 0xf; 3800 link_speed = bus_options & 0x7; 3801 guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) | 3802 reg_read(ohci, OHCI1394_GUIDLo); 3803 3804 flags = PCI_IRQ_INTX; 3805 if (!(ohci->quirks & QUIRK_NO_MSI)) 3806 flags |= PCI_IRQ_MSI; 3807 err = pci_alloc_irq_vectors(dev, 1, 1, flags); 3808 if (err < 0) 3809 return err; 3810 irq = pci_irq_vector(dev, 0); 3811 if (irq < 0) { 3812 err = irq; 3813 goto fail_msi; 3814 } 3815 3816 err = request_threaded_irq(irq, irq_handler, NULL, 3817 pci_dev_msi_enabled(dev) ? 0 : IRQF_SHARED, ohci_driver_name, 3818 ohci); 3819 if (err < 0) { 3820 ohci_err(ohci, "failed to allocate interrupt %d\n", irq); 3821 goto fail_msi; 3822 } 3823 3824 err = fw_card_add(&ohci->card, max_receive, link_speed, guid); 3825 if (err) 3826 goto fail_irq; 3827 3828 version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff; 3829 ohci_notice(ohci, 3830 "added OHCI v%x.%x device as card %d, " 3831 "%d IR + %d IT contexts, quirks 0x%x%s\n", 3832 version >> 16, version & 0xff, ohci->card.index, 3833 ohci->n_ir, ohci->n_it, ohci->quirks, 3834 reg_read(ohci, OHCI1394_PhyUpperBound) ? 3835 ", physUB" : ""); 3836 3837 return 0; 3838 3839 fail_irq: 3840 free_irq(irq, ohci); 3841 fail_msi: 3842 pci_free_irq_vectors(dev); 3843 3844 return err; 3845 } 3846 3847 static void pci_remove(struct pci_dev *dev) 3848 { 3849 struct fw_ohci *ohci = pci_get_drvdata(dev); 3850 int irq; 3851 3852 /* 3853 * If the removal is happening from the suspend state, LPS won't be 3854 * enabled and host registers (eg., IntMaskClear) won't be accessible. 3855 */ 3856 if (reg_read(ohci, OHCI1394_HCControlSet) & OHCI1394_HCControl_LPS) { 3857 reg_write(ohci, OHCI1394_IntMaskClear, ~0); 3858 flush_writes(ohci); 3859 } 3860 cancel_work_sync(&ohci->bus_reset_work); 3861 fw_core_remove_card(&ohci->card); 3862 3863 /* 3864 * FIXME: Fail all pending packets here, now that the upper 3865 * layers can't queue any more. 3866 */ 3867 3868 software_reset(ohci); 3869 3870 irq = pci_irq_vector(dev, 0); 3871 if (irq >= 0) 3872 free_irq(irq, ohci); 3873 pci_free_irq_vectors(dev); 3874 3875 dev_notice(&dev->dev, "removing fw-ohci device\n"); 3876 } 3877 3878 #ifdef CONFIG_PM 3879 static int pci_suspend(struct pci_dev *dev, pm_message_t state) 3880 { 3881 struct fw_ohci *ohci = pci_get_drvdata(dev); 3882 int err; 3883 3884 software_reset(ohci); 3885 err = pci_save_state(dev); 3886 if (err) { 3887 ohci_err(ohci, "pci_save_state failed\n"); 3888 return err; 3889 } 3890 err = pci_set_power_state(dev, pci_choose_state(dev, state)); 3891 if (err) 3892 ohci_err(ohci, "pci_set_power_state failed with %d\n", err); 3893 pmac_ohci_off(dev); 3894 3895 return 0; 3896 } 3897 3898 static int pci_resume(struct pci_dev *dev) 3899 { 3900 struct fw_ohci *ohci = pci_get_drvdata(dev); 3901 int err; 3902 3903 pmac_ohci_on(dev); 3904 pci_set_power_state(dev, PCI_D0); 3905 pci_restore_state(dev); 3906 err = pci_enable_device(dev); 3907 if (err) { 3908 ohci_err(ohci, "pci_enable_device failed\n"); 3909 return err; 3910 } 3911 3912 /* Some systems don't setup GUID register on resume from ram */ 3913 if (!reg_read(ohci, OHCI1394_GUIDLo) && 3914 !reg_read(ohci, OHCI1394_GUIDHi)) { 3915 reg_write(ohci, OHCI1394_GUIDLo, (u32)ohci->card.guid); 3916 reg_write(ohci, OHCI1394_GUIDHi, (u32)(ohci->card.guid >> 32)); 3917 } 3918 3919 err = ohci_enable(&ohci->card, NULL, 0); 3920 if (err) 3921 return err; 3922 3923 ohci_resume_iso_dma(ohci); 3924 3925 return 0; 3926 } 3927 #endif 3928 3929 static const struct pci_device_id pci_table[] = { 3930 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) }, 3931 { } 3932 }; 3933 3934 MODULE_DEVICE_TABLE(pci, pci_table); 3935 3936 static struct pci_driver fw_ohci_pci_driver = { 3937 .name = ohci_driver_name, 3938 .id_table = pci_table, 3939 .probe = pci_probe, 3940 .remove = pci_remove, 3941 #ifdef CONFIG_PM 3942 .resume = pci_resume, 3943 .suspend = pci_suspend, 3944 #endif 3945 }; 3946 3947 static int __init fw_ohci_init(void) 3948 { 3949 selfid_workqueue = alloc_workqueue(KBUILD_MODNAME, WQ_MEM_RECLAIM, 0); 3950 if (!selfid_workqueue) 3951 return -ENOMEM; 3952 3953 return pci_register_driver(&fw_ohci_pci_driver); 3954 } 3955 3956 static void __exit fw_ohci_cleanup(void) 3957 { 3958 pci_unregister_driver(&fw_ohci_pci_driver); 3959 destroy_workqueue(selfid_workqueue); 3960 } 3961 3962 module_init(fw_ohci_init); 3963 module_exit(fw_ohci_cleanup); 3964 3965 MODULE_AUTHOR("Kristian Hoegsberg <[email protected]>"); 3966 MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers"); 3967 MODULE_LICENSE("GPL"); 3968 3969 /* Provide a module alias so root-on-sbp2 initrds don't break. */ 3970 MODULE_ALIAS("ohci1394"); 3971