| 1 | |
| 2 | |
| 3 | * Copyright (C) 2005-2007 Kristian Hoegsberg <[email protected]>
|
| 4 |
|
| 5 |
|
| 6 | #include <linux/bug.h>
|
| 7 | #include <linux/completion.h>
|
| 8 | #include <linux/crc-itu-t.h>
|
| 9 | #include <linux/device.h>
|
| 10 | #include <linux/errno.h>
|
| 11 | #include <linux/firewire.h>
|
| 12 | #include <linux/firewire-constants.h>
|
| 13 | #include <linux/jiffies.h>
|
| 14 | #include <linux/kernel.h>
|
| 15 | #include <linux/kref.h>
|
| 16 | #include <linux/list.h>
|
| 17 | #include <linux/module.h>
|
| 18 | #include <linux/mutex.h>
|
| 19 | #include <linux/minmax.h>
|
| 20 | #include <linux/spinlock.h>
|
| 21 | #include <linux/workqueue.h>
|
| 22 |
|
| 23 | #include <linux/atomic.h>
|
| 24 | #include <asm/byteorder.h>
|
| 25 |
|
| 26 | #include "core.h"
|
| 27 | #include <trace/events/firewire.h>
|
| 28 |
|
| 29 | #define define_fw_printk_level(func, kern_level) \
|
| 30 | void func(const struct fw_card *card, const char *fmt, ...) \
|
| 31 | { \
|
| 32 | struct va_format vaf; \
|
| 33 | va_list args; \
|
| 34 | \
|
| 35 | va_start(args, fmt); \
|
| 36 | vaf.fmt = fmt; \
|
| 37 | vaf.va = &args; \
|
| 38 | printk(kern_level KBUILD_MODNAME " %s: %pV", \
|
| 39 | dev_name(card->device), &vaf); \
|
| 40 | va_end(args); \
|
| 41 | }
|
| 42 | define_fw_printk_level(fw_err, KERN_ERR);
|
| 43 | define_fw_printk_level(fw_notice, KERN_NOTICE);
|
| 44 |
|
| 45 | int fw_compute_block_crc(__be32 *block)
|
| 46 | {
|
| 47 | int length;
|
| 48 | u16 crc;
|
| 49 |
|
| 50 | length = (be32_to_cpu(block[0]) >> 16) & 0xff;
|
| 51 | crc = crc_itu_t(0, (u8 *)&block[1], length * 4);
|
| 52 | *block |= cpu_to_be32(crc);
|
| 53 |
|
| 54 | return length;
|
| 55 | }
|
| 56 |
|
| 57 | static DEFINE_MUTEX(card_mutex);
|
| 58 | static LIST_HEAD(card_list);
|
| 59 |
|
| 60 | static LIST_HEAD(descriptor_list);
|
| 61 | static int descriptor_count;
|
| 62 |
|
| 63 | static __be32 tmp_config_rom[256];
|
| 64 |
|
| 65 | static size_t config_rom_length = 1 + 4 + 1 + 1;
|
| 66 |
|
| 67 | #define BIB_CRC(v) ((v) << 0)
|
| 68 | #define BIB_CRC_LENGTH(v) ((v) << 16)
|
| 69 | #define BIB_INFO_LENGTH(v) ((v) << 24)
|
| 70 | #define BIB_BUS_NAME 0x31333934
|
| 71 | #define BIB_LINK_SPEED(v) ((v) << 0)
|
| 72 | #define BIB_GENERATION(v) ((v) << 4)
|
| 73 | #define BIB_MAX_ROM(v) ((v) << 8)
|
| 74 | #define BIB_MAX_RECEIVE(v) ((v) << 12)
|
| 75 | #define BIB_CYC_CLK_ACC(v) ((v) << 16)
|
| 76 | #define BIB_PMC ((1) << 27)
|
| 77 | #define BIB_BMC ((1) << 28)
|
| 78 | #define BIB_ISC ((1) << 29)
|
| 79 | #define BIB_CMC ((1) << 30)
|
| 80 | #define BIB_IRMC ((1) << 31)
|
| 81 | #define NODE_CAPABILITIES 0x0c0083c0
|
| 82 |
|
| 83 | |
| 84 | * IEEE-1394 specifies a default SPLIT_TIMEOUT value of 800 cycles (100 ms),
|
| 85 | * but we have to make it longer because there are many devices whose firmware
|
| 86 | * is just too slow for that.
|
| 87 |
|
| 88 | #define DEFAULT_SPLIT_TIMEOUT (2 * 8000)
|
| 89 |
|
| 90 | static void generate_config_rom(struct fw_card *card, __be32 *config_rom)
|
| 91 | {
|
| 92 | struct fw_descriptor *desc;
|
| 93 | int i, j, k, length;
|
| 94 |
|
| 95 | |
| 96 | * Initialize contents of config rom buffer. On the OHCI
|
| 97 | * controller, block reads to the config rom accesses the host
|
| 98 | * memory, but quadlet read access the hardware bus info block
|
| 99 | * registers. That's just crack, but it means we should make
|
| 100 | * sure the contents of bus info block in host memory matches
|
| 101 | * the version stored in the OHCI registers.
|
| 102 |
|
| 103 |
|
| 104 | config_rom[0] = cpu_to_be32(
|
| 105 | BIB_CRC_LENGTH(4) | BIB_INFO_LENGTH(4) | BIB_CRC(0));
|
| 106 | config_rom[1] = cpu_to_be32(BIB_BUS_NAME);
|
| 107 | config_rom[2] = cpu_to_be32(
|
| 108 | BIB_LINK_SPEED(card->link_speed) |
|
| 109 | BIB_GENERATION(card->config_rom_generation++ % 14 + 2) |
|
| 110 | BIB_MAX_ROM(2) |
|
| 111 | BIB_MAX_RECEIVE(card->max_receive) |
|
| 112 | BIB_BMC | BIB_ISC | BIB_CMC | BIB_IRMC);
|
| 113 | config_rom[3] = cpu_to_be32(card->guid >> 32);
|
| 114 | config_rom[4] = cpu_to_be32(card->guid);
|
| 115 |
|
| 116 |
|
| 117 | config_rom[6] = cpu_to_be32(NODE_CAPABILITIES);
|
| 118 | i = 7;
|
| 119 | j = 7 + descriptor_count;
|
| 120 |
|
| 121 |
|
| 122 | list_for_each_entry (desc, &descriptor_list, link) {
|
| 123 | if (desc->immediate > 0)
|
| 124 | config_rom[i++] = cpu_to_be32(desc->immediate);
|
| 125 | config_rom[i] = cpu_to_be32(desc->key | (j - i));
|
| 126 | i++;
|
| 127 | j += desc->length;
|
| 128 | }
|
| 129 |
|
| 130 |
|
| 131 | config_rom[5] = cpu_to_be32((i - 5 - 1) << 16);
|
| 132 |
|
| 133 |
|
| 134 | list_for_each_entry (desc, &descriptor_list, link) {
|
| 135 | for (k = 0; k < desc->length; k++)
|
| 136 | config_rom[i + k] = cpu_to_be32(desc->data[k]);
|
| 137 | i += desc->length;
|
| 138 | }
|
| 139 |
|
| 140 | |
| 141 | * assumes that CRC length and info length are identical for
|
| 142 | * the bus info block, which is always the case for this
|
| 143 | * implementation.
|
| 144 | for (i = 0; i < j; i += length + 1)
|
| 145 | length = fw_compute_block_crc(config_rom + i);
|
| 146 |
|
| 147 | WARN_ON(j != config_rom_length);
|
| 148 | }
|
| 149 |
|
| 150 | static void update_config_roms(void)
|
| 151 | {
|
| 152 | struct fw_card *card;
|
| 153 |
|
| 154 | list_for_each_entry (card, &card_list, link) {
|
| 155 | generate_config_rom(card, tmp_config_rom);
|
| 156 | card->driver->set_config_rom(card, tmp_config_rom,
|
| 157 | config_rom_length);
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | static size_t required_space(struct fw_descriptor *desc)
|
| 162 | {
|
| 163 |
|
| 164 | return desc->length + 1 + (desc->immediate > 0 ? 1 : 0);
|
| 165 | }
|
| 166 |
|
| 167 | int fw_core_add_descriptor(struct fw_descriptor *desc)
|
| 168 | {
|
| 169 | size_t i;
|
| 170 |
|
| 171 |
|
| 172 | if (!in_range(desc->length, 1, 256))
|
| 173 | return -EINVAL;
|
| 174 |
|
| 175 | i = 0;
|
| 176 | |
| 177 | * Validate internal block structures within the descriptor. Each sub-block
|
| 178 | * encodes its length in the top 16 bits of its header quadlet.
|
| 179 |
|
| 180 | while (i < desc->length) {
|
| 181 | u16 block_len = desc->data[i] >> 16;
|
| 182 |
|
| 183 | |
| 184 | * Guard against corrupted descriptors where an individual block length
|
| 185 | * claims to extend past the allocated end of desc->data, avoiding
|
| 186 | * out-of-bounds reads.
|
| 187 |
|
| 188 | if (block_len >= desc->length - i)
|
| 189 | return -EINVAL;
|
| 190 |
|
| 191 | i += block_len + 1;
|
| 192 | }
|
| 193 |
|
| 194 |
|
| 195 | if (i != desc->length)
|
| 196 | return -EINVAL;
|
| 197 |
|
| 198 | guard(mutex)(&card_mutex);
|
| 199 |
|
| 200 | if (config_rom_length + required_space(desc) > 256)
|
| 201 | return -EBUSY;
|
| 202 |
|
| 203 | list_add_tail(&desc->link, &descriptor_list);
|
| 204 | config_rom_length += required_space(desc);
|
| 205 | descriptor_count++;
|
| 206 | if (desc->immediate > 0)
|
| 207 | descriptor_count++;
|
| 208 | update_config_roms();
|
| 209 |
|
| 210 | return 0;
|
| 211 | }
|
| 212 | EXPORT_SYMBOL(fw_core_add_descriptor);
|
| 213 |
|
| 214 | void fw_core_remove_descriptor(struct fw_descriptor *desc)
|
| 215 | {
|
| 216 | guard(mutex)(&card_mutex);
|
| 217 |
|
| 218 | list_del(&desc->link);
|
| 219 | config_rom_length -= required_space(desc);
|
| 220 | descriptor_count--;
|
| 221 | if (desc->immediate > 0)
|
| 222 | descriptor_count--;
|
| 223 | update_config_roms();
|
| 224 | }
|
| 225 | EXPORT_SYMBOL(fw_core_remove_descriptor);
|
| 226 |
|
| 227 | static int reset_bus(struct fw_card *card, bool short_reset)
|
| 228 | {
|
| 229 | int reg = short_reset ? 5 : 1;
|
| 230 | int bit = short_reset ? PHY_BUS_SHORT_RESET : PHY_BUS_RESET;
|
| 231 |
|
| 232 | trace_bus_reset_initiate(card->index, card->generation, short_reset);
|
| 233 |
|
| 234 | return card->driver->update_phy_reg(card, reg, 0, bit);
|
| 235 | }
|
| 236 |
|
| 237 | void fw_schedule_bus_reset(struct fw_card *card, bool delayed, bool short_reset)
|
| 238 | {
|
| 239 | trace_bus_reset_schedule(card->index, card->generation, short_reset);
|
| 240 |
|
| 241 |
|
| 242 | card->br_short = short_reset;
|
| 243 |
|
| 244 |
|
| 245 | fw_card_get(card);
|
| 246 | if (!queue_delayed_work(fw_workqueue, &card->br_work, delayed ? msecs_to_jiffies(10) : 0))
|
| 247 | fw_card_put(card);
|
| 248 | }
|
| 249 | EXPORT_SYMBOL(fw_schedule_bus_reset);
|
| 250 |
|
| 251 | static void br_work(struct work_struct *work)
|
| 252 | {
|
| 253 | struct fw_card *card = from_work(card, work, br_work.work);
|
| 254 |
|
| 255 |
|
| 256 | if (card->reset_jiffies != 0 &&
|
| 257 | time_is_after_jiffies64(card->reset_jiffies + secs_to_jiffies(2))) {
|
| 258 | trace_bus_reset_postpone(card->index, card->generation, card->br_short);
|
| 259 |
|
| 260 | if (!queue_delayed_work(fw_workqueue, &card->br_work, secs_to_jiffies(2)))
|
| 261 | fw_card_put(card);
|
| 262 | return;
|
| 263 | }
|
| 264 |
|
| 265 | fw_send_phy_config(card, FW_PHY_CONFIG_NO_NODE_ID, card->generation,
|
| 266 | FW_PHY_CONFIG_CURRENT_GAP_COUNT);
|
| 267 | reset_bus(card, card->br_short);
|
| 268 | fw_card_put(card);
|
| 269 | }
|
| 270 |
|
| 271 | static void allocate_broadcast_channel(struct fw_card *card, int generation)
|
| 272 | {
|
| 273 | int channel, bandwidth = 0;
|
| 274 |
|
| 275 | if (!card->broadcast_channel_allocated) {
|
| 276 | fw_iso_resource_manage(card, generation, 1ULL << 31,
|
| 277 | &channel, &bandwidth, true);
|
| 278 | if (channel != 31) {
|
| 279 | fw_notice(card, "failed to allocate broadcast channel\n");
|
| 280 | return;
|
| 281 | }
|
| 282 | card->broadcast_channel_allocated = true;
|
| 283 | }
|
| 284 |
|
| 285 | device_for_each_child(card->device, (void *)(long)generation,
|
| 286 | fw_device_set_broadcast_channel);
|
| 287 | }
|
| 288 |
|
| 289 | void fw_schedule_bm_work(struct fw_card *card, unsigned long delay)
|
| 290 | {
|
| 291 | fw_card_get(card);
|
| 292 | if (!schedule_delayed_work(&card->bm_work, delay))
|
| 293 | fw_card_put(card);
|
| 294 | }
|
| 295 |
|
| 296 | enum bm_contention_outcome {
|
| 297 | |
| 298 | BM_CONTENTION_OUTCOME_WITHIN_WINDOW = 0,
|
| 299 | |
| 300 | BM_CONTENTION_OUTCOME_IRM_HAS_LINK_OFF,
|
| 301 | |
| 302 | BM_CONTENTION_OUTCOME_IRM_COMPLIES_1394_1995_ONLY,
|
| 303 | |
| 304 | BM_CONTENTION_OUTCOME_AT_NEW_GENERATION,
|
| 305 | |
| 306 | BM_CONTENTION_OUTCOME_LOCAL_PROBLEM_AT_TRANSACTION,
|
| 307 | |
| 308 | BM_CONTENTION_OUTCOME_IRM_IS_NOT_CAPABLE_FOR_IRM,
|
| 309 | |
| 310 | BM_CONTENTION_OUTCOME_IRM_HOLDS_ANOTHER_NODE_AS_BM,
|
| 311 | |
| 312 | BM_CONTENTION_OUTCOME_IRM_HOLDS_LOCAL_NODE_AS_BM,
|
| 313 | };
|
| 314 |
|
| 315 | static enum bm_contention_outcome contend_for_bm(struct fw_card *card)
|
| 316 | __must_hold(&card->lock)
|
| 317 | {
|
| 318 | int generation = card->generation;
|
| 319 | int local_id = card->local_node->node_id;
|
| 320 | __be32 data[2] = {
|
| 321 | cpu_to_be32(BUS_MANAGER_ID_NOT_REGISTERED),
|
| 322 | cpu_to_be32(local_id),
|
| 323 | };
|
| 324 | bool grace = time_is_before_jiffies64(card->reset_jiffies + msecs_to_jiffies(125));
|
| 325 | struct fw_node *irm_node;
|
| 326 | struct fw_device *irm_device;
|
| 327 | int irm_node_id, irm_device_quirks = 0;
|
| 328 | int rcode;
|
| 329 |
|
| 330 | lockdep_assert_held(&card->lock);
|
| 331 |
|
| 332 | if (!grace) {
|
| 333 | if (!is_next_generation(generation, card->bm_generation) || card->bm_abdicate)
|
| 334 | return BM_CONTENTION_OUTCOME_WITHIN_WINDOW;
|
| 335 | }
|
| 336 |
|
| 337 | irm_node = card->irm_node;
|
| 338 | if (!irm_node->link_on) {
|
| 339 | fw_notice(card, "IRM has link off, making local node (%02x) root\n", local_id);
|
| 340 | return BM_CONTENTION_OUTCOME_IRM_HAS_LINK_OFF;
|
| 341 | }
|
| 342 |
|
| 343 | |
| 344 | irm_device = fw_node_get_device(irm_node);
|
| 345 | if (irm_device)
|
| 346 | irm_device_quirks = READ_ONCE(irm_device->quirks);
|
| 347 | if ((irm_device_quirks & FW_DEVICE_QUIRK_IRM_IS_1394_1995_ONLY) &&
|
| 348 | !(irm_device_quirks & FW_DEVICE_QUIRK_IRM_IGNORES_BUS_MANAGER)) {
|
| 349 | fw_notice(card, "IRM is not 1394a compliant, making local node (%02x) root\n",
|
| 350 | local_id);
|
| 351 | return BM_CONTENTION_OUTCOME_IRM_COMPLIES_1394_1995_ONLY;
|
| 352 | }
|
| 353 |
|
| 354 | irm_node_id = irm_node->node_id;
|
| 355 |
|
| 356 | spin_unlock_irq(&card->lock);
|
| 357 |
|
| 358 | rcode = fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP, irm_node_id, generation,
|
| 359 | SCODE_100, CSR_REGISTER_BASE + CSR_BUS_MANAGER_ID, data,
|
| 360 | sizeof(data));
|
| 361 |
|
| 362 | spin_lock_irq(&card->lock);
|
| 363 |
|
| 364 | switch (rcode) {
|
| 365 | case RCODE_GENERATION:
|
| 366 | return BM_CONTENTION_OUTCOME_AT_NEW_GENERATION;
|
| 367 | case RCODE_SEND_ERROR:
|
| 368 | return BM_CONTENTION_OUTCOME_LOCAL_PROBLEM_AT_TRANSACTION;
|
| 369 | case RCODE_COMPLETE:
|
| 370 | {
|
| 371 | int bm_id = be32_to_cpu(data[0]);
|
| 372 |
|
| 373 | |
| 374 | if (bm_id != BUS_MANAGER_ID_NOT_REGISTERED)
|
| 375 | card->bm_node_id = 0xffc0 & bm_id;
|
| 376 | else
|
| 377 | card->bm_node_id = local_id;
|
| 378 |
|
| 379 | if (bm_id != BUS_MANAGER_ID_NOT_REGISTERED)
|
| 380 | return BM_CONTENTION_OUTCOME_IRM_HOLDS_ANOTHER_NODE_AS_BM;
|
| 381 | else
|
| 382 | return BM_CONTENTION_OUTCOME_IRM_HOLDS_LOCAL_NODE_AS_BM;
|
| 383 | }
|
| 384 | default:
|
| 385 | if (!(irm_device_quirks & FW_DEVICE_QUIRK_IRM_IGNORES_BUS_MANAGER)) {
|
| 386 | fw_notice(card, "BM lock failed (%s), making local node (%02x) root\n",
|
| 387 | fw_rcode_string(rcode), local_id);
|
| 388 | return BM_CONTENTION_OUTCOME_IRM_COMPLIES_1394_1995_ONLY;
|
| 389 | } else {
|
| 390 | return BM_CONTENTION_OUTCOME_IRM_IS_NOT_CAPABLE_FOR_IRM;
|
| 391 | }
|
| 392 | }
|
| 393 | }
|
| 394 |
|
| 395 | DEFINE_FREE(node_unref, struct fw_node *, if (_T) fw_node_put(_T))
|
| 396 | DEFINE_FREE(card_unref, struct fw_card *, if (_T) fw_card_put(_T))
|
| 397 |
|
| 398 | static void bm_work(struct work_struct *work)
|
| 399 | {
|
| 400 | static const char gap_count_table[] = {
|
| 401 | 63, 5, 7, 8, 10, 13, 16, 18, 21, 24, 26, 29, 32, 35, 37, 40
|
| 402 | };
|
| 403 | struct fw_card *card __free(card_unref) = from_work(card, work, bm_work.work);
|
| 404 | struct fw_node *root_node __free(node_unref) = NULL;
|
| 405 | int root_id, new_root_id, irm_id, local_id;
|
| 406 | int expected_gap_count, generation;
|
| 407 | bool stand_for_root = false;
|
| 408 |
|
| 409 | spin_lock_irq(&card->lock);
|
| 410 |
|
| 411 | if (card->local_node == NULL) {
|
| 412 | spin_unlock_irq(&card->lock);
|
| 413 | return;
|
| 414 | }
|
| 415 |
|
| 416 | generation = card->generation;
|
| 417 |
|
| 418 | root_node = fw_node_get(card->root_node);
|
| 419 |
|
| 420 | root_id = root_node->node_id;
|
| 421 | irm_id = card->irm_node->node_id;
|
| 422 | local_id = card->local_node->node_id;
|
| 423 |
|
| 424 | if (card->bm_generation != generation) {
|
| 425 | enum bm_contention_outcome result = contend_for_bm(card);
|
| 426 |
|
| 427 | switch (result) {
|
| 428 | case BM_CONTENTION_OUTCOME_WITHIN_WINDOW:
|
| 429 | spin_unlock_irq(&card->lock);
|
| 430 | fw_schedule_bm_work(card, msecs_to_jiffies(125));
|
| 431 | return;
|
| 432 | case BM_CONTENTION_OUTCOME_IRM_HAS_LINK_OFF:
|
| 433 | stand_for_root = true;
|
| 434 | break;
|
| 435 | case BM_CONTENTION_OUTCOME_IRM_COMPLIES_1394_1995_ONLY:
|
| 436 | stand_for_root = true;
|
| 437 | break;
|
| 438 | case BM_CONTENTION_OUTCOME_AT_NEW_GENERATION:
|
| 439 | |
| 440 | spin_unlock_irq(&card->lock);
|
| 441 | return;
|
| 442 | case BM_CONTENTION_OUTCOME_LOCAL_PROBLEM_AT_TRANSACTION:
|
| 443 | |
| 444 | |
| 445 | spin_unlock_irq(&card->lock);
|
| 446 | fw_schedule_bm_work(card, msecs_to_jiffies(125));
|
| 447 | return;
|
| 448 | case BM_CONTENTION_OUTCOME_IRM_IS_NOT_CAPABLE_FOR_IRM:
|
| 449 | |
| 450 | stand_for_root = true;
|
| 451 | break;
|
| 452 | case BM_CONTENTION_OUTCOME_IRM_HOLDS_ANOTHER_NODE_AS_BM:
|
| 453 | if (local_id == irm_id) {
|
| 454 | |
| 455 | spin_unlock_irq(&card->lock);
|
| 456 | allocate_broadcast_channel(card, generation);
|
| 457 | spin_lock_irq(&card->lock);
|
| 458 | }
|
| 459 | fallthrough;
|
| 460 | case BM_CONTENTION_OUTCOME_IRM_HOLDS_LOCAL_NODE_AS_BM:
|
| 461 | default:
|
| 462 | card->bm_generation = generation;
|
| 463 | break;
|
| 464 | }
|
| 465 | }
|
| 466 |
|
| 467 | |
| 468 | |
| 469 | if (!stand_for_root) {
|
| 470 | if (card->gap_count == GAP_COUNT_MISMATCHED) {
|
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | stand_for_root = true;
|
| 479 |
|
| 480 | |
| 481 | |
| 482 | card->bm_retries = 0;
|
| 483 | } else {
|
| 484 | |
| 485 | struct fw_device *root_device = fw_node_get_device(root_node);
|
| 486 |
|
| 487 | if (root_device == NULL) {
|
| 488 | |
| 489 | |
| 490 | stand_for_root = true;
|
| 491 | } else {
|
| 492 | bool root_device_is_running =
|
| 493 | atomic_read(&root_device->state) == FW_DEVICE_RUNNING;
|
| 494 |
|
| 495 | if (!root_device_is_running) {
|
| 496 | |
| 497 | |
| 498 | spin_unlock_irq(&card->lock);
|
| 499 | return;
|
| 500 | } else if (!root_device->cmc) {
|
| 501 | |
| 502 | |
| 503 | |
| 504 | stand_for_root = true;
|
| 505 | }
|
| 506 | }
|
| 507 | }
|
| 508 | }
|
| 509 |
|
| 510 | if (stand_for_root) {
|
| 511 | new_root_id = local_id;
|
| 512 | } else {
|
| 513 | |
| 514 | |
| 515 | new_root_id = root_id;
|
| 516 | }
|
| 517 |
|
| 518 | |
| 519 | * Pick a gap count from 1394a table E-1. The table doesn't cover
|
| 520 | * the typically much larger 1394b beta repeater delays though.
|
| 521 |
|
| 522 | if (!card->beta_repeaters_present &&
|
| 523 | root_node->max_hops < ARRAY_SIZE(gap_count_table))
|
| 524 | expected_gap_count = gap_count_table[root_node->max_hops];
|
| 525 | else
|
| 526 | expected_gap_count = 63;
|
| 527 |
|
| 528 | |
| 529 | |
| 530 | |
| 531 | if (card->bm_retries++ < 5 && (card->gap_count != expected_gap_count || new_root_id != root_id)) {
|
| 532 | int card_gap_count = card->gap_count;
|
| 533 |
|
| 534 | spin_unlock_irq(&card->lock);
|
| 535 |
|
| 536 | fw_notice(card, "phy config: new root=%x, gap_count=%d\n",
|
| 537 | new_root_id, expected_gap_count);
|
| 538 | fw_send_phy_config(card, new_root_id, generation, expected_gap_count);
|
| 539 | |
| 540 | * Where possible, use a short bus reset to minimize
|
| 541 | * disruption to isochronous transfers. But in the event
|
| 542 | * of a gap count inconsistency, use a long bus reset.
|
| 543 | *
|
| 544 | * As noted in 1394a 8.4.6.2, nodes on a mixed 1394/1394a bus
|
| 545 | * may set different gap counts after a bus reset. On a mixed
|
| 546 | * 1394/1394a bus, a short bus reset can get doubled. Some
|
| 547 | * nodes may treat the double reset as one bus reset and others
|
| 548 | * may treat it as two, causing a gap count inconsistency
|
| 549 | * again. Using a long bus reset prevents this.
|
| 550 |
|
| 551 | reset_bus(card, card_gap_count != 0);
|
| 552 |
|
| 553 | } else {
|
| 554 | struct fw_device *root_device = fw_node_get_device(root_node);
|
| 555 |
|
| 556 | spin_unlock_irq(&card->lock);
|
| 557 |
|
| 558 | if (root_device && root_device->cmc) {
|
| 559 | |
| 560 | __be32 data = cpu_to_be32(CSR_STATE_BIT_CMSTR);
|
| 561 | int rcode = fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
|
| 562 | root_id, generation, SCODE_100,
|
| 563 | CSR_REGISTER_BASE + CSR_STATE_SET,
|
| 564 | &data, sizeof(data));
|
| 565 | if (rcode == RCODE_GENERATION)
|
| 566 | return;
|
| 567 | }
|
| 568 |
|
| 569 | if (local_id == irm_id)
|
| 570 | allocate_broadcast_channel(card, generation);
|
| 571 | }
|
| 572 | }
|
| 573 |
|
| 574 | void fw_card_initialize(struct fw_card *card,
|
| 575 | const struct fw_card_driver *driver,
|
| 576 | struct device *device)
|
| 577 | {
|
| 578 | static atomic_t index = ATOMIC_INIT(-1);
|
| 579 |
|
| 580 | card->index = atomic_inc_return(&index);
|
| 581 | card->driver = driver;
|
| 582 | card->device = device;
|
| 583 |
|
| 584 | card->transactions.current_tlabel = 0;
|
| 585 | card->transactions.tlabel_mask = 0;
|
| 586 | INIT_LIST_HEAD(&card->transactions.list);
|
| 587 | spin_lock_init(&card->transactions.lock);
|
| 588 |
|
| 589 | spin_lock_init(&card->topology_map.lock);
|
| 590 |
|
| 591 | card->split_timeout.hi = DEFAULT_SPLIT_TIMEOUT / 8000;
|
| 592 | card->split_timeout.lo = (DEFAULT_SPLIT_TIMEOUT % 8000) << 19;
|
| 593 | card->split_timeout.cycles = DEFAULT_SPLIT_TIMEOUT;
|
| 594 | card->split_timeout.jiffies = isoc_cycles_to_jiffies(DEFAULT_SPLIT_TIMEOUT);
|
| 595 | spin_lock_init(&card->split_timeout.lock);
|
| 596 |
|
| 597 | card->color = 0;
|
| 598 | card->broadcast_channel = BROADCAST_CHANNEL_INITIAL;
|
| 599 |
|
| 600 | kref_init(&card->kref);
|
| 601 | init_completion(&card->done);
|
| 602 |
|
| 603 | spin_lock_init(&card->lock);
|
| 604 |
|
| 605 | card->local_node = NULL;
|
| 606 |
|
| 607 | INIT_DELAYED_WORK(&card->br_work, br_work);
|
| 608 | INIT_DELAYED_WORK(&card->bm_work, bm_work);
|
| 609 | }
|
| 610 | EXPORT_SYMBOL(fw_card_initialize);
|
| 611 |
|
| 612 | DEFINE_FREE(workqueue_destroy, struct workqueue_struct *, if (_T) destroy_workqueue(_T))
|
| 613 |
|
| 614 | int fw_card_add(struct fw_card *card, u32 max_receive, u32 link_speed, u64 guid,
|
| 615 | unsigned int supported_isoc_contexts)
|
| 616 | {
|
| 617 | struct workqueue_struct *isoc_wq __free(workqueue_destroy) = NULL;
|
| 618 | struct workqueue_struct *async_wq __free(workqueue_destroy) = NULL;
|
| 619 | int ret;
|
| 620 |
|
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | isoc_wq = alloc_workqueue("firewire-isoc-card%u",
|
| 634 | WQ_UNBOUND | WQ_FREEZABLE | WQ_HIGHPRI | WQ_SYSFS,
|
| 635 | supported_isoc_contexts, card->index);
|
| 636 | if (!isoc_wq)
|
| 637 | return -ENOMEM;
|
| 638 |
|
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | async_wq = alloc_workqueue("firewire-async-card%u",
|
| 649 | WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_HIGHPRI | WQ_SYSFS,
|
| 650 | 4, card->index);
|
| 651 | if (!async_wq)
|
| 652 | return -ENOMEM;
|
| 653 |
|
| 654 | card->isoc_wq = isoc_wq;
|
| 655 | card->async_wq = async_wq;
|
| 656 | card->max_receive = max_receive;
|
| 657 | card->link_speed = link_speed;
|
| 658 | card->guid = guid;
|
| 659 |
|
| 660 | scoped_guard(mutex, &card_mutex) {
|
| 661 | generate_config_rom(card, tmp_config_rom);
|
| 662 | ret = card->driver->enable(card, tmp_config_rom, config_rom_length);
|
| 663 | if (ret < 0) {
|
| 664 | card->isoc_wq = NULL;
|
| 665 | card->async_wq = NULL;
|
| 666 | return ret;
|
| 667 | }
|
| 668 | retain_and_null_ptr(isoc_wq);
|
| 669 | retain_and_null_ptr(async_wq);
|
| 670 |
|
| 671 | list_add_tail(&card->link, &card_list);
|
| 672 | }
|
| 673 |
|
| 674 | return 0;
|
| 675 | }
|
| 676 | EXPORT_SYMBOL(fw_card_add);
|
| 677 |
|
| 678 | |
| 679 | * The next few functions implement a dummy driver that is used once a card
|
| 680 | * driver shuts down an fw_card. This allows the driver to cleanly unload,
|
| 681 | * as all IO to the card will be handled (and failed) by the dummy driver
|
| 682 | * instead of calling into the module. Only functions for iso context
|
| 683 | * shutdown still need to be provided by the card driver.
|
| 684 | *
|
| 685 | * .read/write_csr() should never be called anymore after the dummy driver
|
| 686 | * was bound since they are only used within request handler context.
|
| 687 | * .set_config_rom() is never called since the card is taken out of card_list
|
| 688 | * before switching to the dummy driver.
|
| 689 |
|
| 690 |
|
| 691 | static int dummy_read_phy_reg(struct fw_card *card, int address)
|
| 692 | {
|
| 693 | return -ENODEV;
|
| 694 | }
|
| 695 |
|
| 696 | static int dummy_update_phy_reg(struct fw_card *card, int address,
|
| 697 | int clear_bits, int set_bits)
|
| 698 | {
|
| 699 | return -ENODEV;
|
| 700 | }
|
| 701 |
|
| 702 | static void dummy_send_request(struct fw_card *card, struct fw_packet *packet)
|
| 703 | {
|
| 704 | packet->callback(packet, card, RCODE_CANCELLED);
|
| 705 | }
|
| 706 |
|
| 707 | static void dummy_send_response(struct fw_card *card, struct fw_packet *packet)
|
| 708 | {
|
| 709 | packet->callback(packet, card, RCODE_CANCELLED);
|
| 710 | }
|
| 711 |
|
| 712 | static int dummy_cancel_packet(struct fw_card *card, struct fw_packet *packet)
|
| 713 | {
|
| 714 | return -ENOENT;
|
| 715 | }
|
| 716 |
|
| 717 | static int dummy_enable_phys_dma(struct fw_card *card,
|
| 718 | int node_id, int generation)
|
| 719 | {
|
| 720 | return -ENODEV;
|
| 721 | }
|
| 722 |
|
| 723 | static struct fw_iso_context *dummy_allocate_iso_context(struct fw_card *card, int type,
|
| 724 | int channel, size_t header_size, size_t header_storage_size)
|
| 725 | {
|
| 726 | return ERR_PTR(-ENODEV);
|
| 727 | }
|
| 728 |
|
| 729 | static u32 dummy_read_csr(struct fw_card *card, int csr_offset)
|
| 730 | {
|
| 731 | return 0;
|
| 732 | }
|
| 733 |
|
| 734 | static void dummy_write_csr(struct fw_card *card, int csr_offset, u32 value)
|
| 735 | {
|
| 736 | }
|
| 737 |
|
| 738 | static int dummy_start_iso(struct fw_iso_context *ctx,
|
| 739 | s32 cycle, u32 sync, u32 tags)
|
| 740 | {
|
| 741 | return -ENODEV;
|
| 742 | }
|
| 743 |
|
| 744 | static int dummy_set_iso_channels(struct fw_iso_context *ctx, u64 *channels)
|
| 745 | {
|
| 746 | return -ENODEV;
|
| 747 | }
|
| 748 |
|
| 749 | static int dummy_queue_iso(struct fw_iso_context *ctx, struct fw_iso_packet *p,
|
| 750 | struct fw_iso_buffer *buffer, unsigned long payload)
|
| 751 | {
|
| 752 | return -ENODEV;
|
| 753 | }
|
| 754 |
|
| 755 | static void dummy_flush_queue_iso(struct fw_iso_context *ctx)
|
| 756 | {
|
| 757 | }
|
| 758 |
|
| 759 | static int dummy_flush_iso_completions(struct fw_iso_context *ctx)
|
| 760 | {
|
| 761 | return -ENODEV;
|
| 762 | }
|
| 763 |
|
| 764 | static const struct fw_card_driver dummy_driver_template = {
|
| 765 | .read_phy_reg = dummy_read_phy_reg,
|
| 766 | .update_phy_reg = dummy_update_phy_reg,
|
| 767 | .send_request = dummy_send_request,
|
| 768 | .send_response = dummy_send_response,
|
| 769 | .cancel_packet = dummy_cancel_packet,
|
| 770 | .enable_phys_dma = dummy_enable_phys_dma,
|
| 771 | .read_csr = dummy_read_csr,
|
| 772 | .write_csr = dummy_write_csr,
|
| 773 | .allocate_iso_context = dummy_allocate_iso_context,
|
| 774 | .start_iso = dummy_start_iso,
|
| 775 | .set_iso_channels = dummy_set_iso_channels,
|
| 776 | .queue_iso = dummy_queue_iso,
|
| 777 | .flush_queue_iso = dummy_flush_queue_iso,
|
| 778 | .flush_iso_completions = dummy_flush_iso_completions,
|
| 779 | };
|
| 780 |
|
| 781 | void fw_card_release(struct kref *kref)
|
| 782 | {
|
| 783 | struct fw_card *card = container_of(kref, struct fw_card, kref);
|
| 784 |
|
| 785 | complete(&card->done);
|
| 786 | }
|
| 787 | EXPORT_SYMBOL_GPL(fw_card_release);
|
| 788 |
|
| 789 | void fw_core_remove_card(struct fw_card *card)
|
| 790 | {
|
| 791 | struct fw_card_driver dummy_driver = dummy_driver_template;
|
| 792 |
|
| 793 | might_sleep();
|
| 794 |
|
| 795 | card->driver->update_phy_reg(card, 4,
|
| 796 | PHY_LINK_ACTIVE | PHY_CONTENDER, 0);
|
| 797 | fw_schedule_bus_reset(card, false, true);
|
| 798 |
|
| 799 | scoped_guard(mutex, &card_mutex)
|
| 800 | list_del_init(&card->link);
|
| 801 |
|
| 802 |
|
| 803 | dummy_driver.free_iso_context = card->driver->free_iso_context;
|
| 804 | dummy_driver.stop_iso = card->driver->stop_iso;
|
| 805 | dummy_driver.disable = card->driver->disable;
|
| 806 | card->driver = &dummy_driver;
|
| 807 |
|
| 808 | drain_workqueue(card->isoc_wq);
|
| 809 | drain_workqueue(card->async_wq);
|
| 810 | card->driver->disable(card);
|
| 811 | fw_cancel_pending_transactions(card);
|
| 812 |
|
| 813 | scoped_guard(spinlock_irqsave, &card->lock)
|
| 814 | fw_destroy_nodes(card);
|
| 815 |
|
| 816 |
|
| 817 | fw_card_put(card);
|
| 818 | wait_for_completion(&card->done);
|
| 819 |
|
| 820 | destroy_workqueue(card->isoc_wq);
|
| 821 | destroy_workqueue(card->async_wq);
|
| 822 |
|
| 823 | WARN_ON(!list_empty(&card->transactions.list));
|
| 824 | }
|
| 825 | EXPORT_SYMBOL(fw_core_remove_card);
|
| 826 |
|
| 827 | |
| 828 | * fw_card_read_cycle_time: read from Isochronous Cycle Timer Register of 1394 OHCI in MMIO region
|
| 829 | * for controller card.
|
| 830 | * @card: The instance of card for 1394 OHCI controller.
|
| 831 | * @cycle_time: The mutual reference to value of cycle time for the read operation.
|
| 832 | *
|
| 833 | * Read value from Isochronous Cycle Timer Register of 1394 OHCI in MMIO region for the given
|
| 834 | * controller card. This function accesses the region without any lock primitives or IRQ mask.
|
| 835 | * When returning successfully, the content of @value argument has value aligned to host endianness,
|
| 836 | * formetted by CYCLE_TIME CSR Register of IEEE 1394 std.
|
| 837 | *
|
| 838 | * Context: Any context.
|
| 839 | * Return:
|
| 840 | * * 0 - Read successfully.
|
| 841 | * * -ENODEV - The controller is unavailable due to being removed or unbound.
|
| 842 |
|
| 843 | int fw_card_read_cycle_time(struct fw_card *card, u32 *cycle_time)
|
| 844 | {
|
| 845 | if (card->driver->read_csr == dummy_read_csr)
|
| 846 | return -ENODEV;
|
| 847 |
|
| 848 | |
| 849 | |
| 850 | *cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
|
| 851 | return 0;
|
| 852 | }
|
| 853 | EXPORT_SYMBOL_GPL(fw_card_read_cycle_time);
|