0001
0002
0003
0004
0005
0006 #include <linux/bug.h>
0007 #include <linux/completion.h>
0008 #include <linux/crc-itu-t.h>
0009 #include <linux/device.h>
0010 #include <linux/errno.h>
0011 #include <linux/firewire.h>
0012 #include <linux/firewire-constants.h>
0013 #include <linux/jiffies.h>
0014 #include <linux/kernel.h>
0015 #include <linux/kref.h>
0016 #include <linux/list.h>
0017 #include <linux/module.h>
0018 #include <linux/mutex.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/workqueue.h>
0021
0022 #include <linux/atomic.h>
0023 #include <asm/byteorder.h>
0024
0025 #include "core.h"
0026
0027 #define define_fw_printk_level(func, kern_level) \
0028 void func(const struct fw_card *card, const char *fmt, ...) \
0029 { \
0030 struct va_format vaf; \
0031 va_list args; \
0032 \
0033 va_start(args, fmt); \
0034 vaf.fmt = fmt; \
0035 vaf.va = &args; \
0036 printk(kern_level KBUILD_MODNAME " %s: %pV", \
0037 dev_name(card->device), &vaf); \
0038 va_end(args); \
0039 }
0040 define_fw_printk_level(fw_err, KERN_ERR);
0041 define_fw_printk_level(fw_notice, KERN_NOTICE);
0042
0043 int fw_compute_block_crc(__be32 *block)
0044 {
0045 int length;
0046 u16 crc;
0047
0048 length = (be32_to_cpu(block[0]) >> 16) & 0xff;
0049 crc = crc_itu_t(0, (u8 *)&block[1], length * 4);
0050 *block |= cpu_to_be32(crc);
0051
0052 return length;
0053 }
0054
0055 static DEFINE_MUTEX(card_mutex);
0056 static LIST_HEAD(card_list);
0057
0058 static LIST_HEAD(descriptor_list);
0059 static int descriptor_count;
0060
0061 static __be32 tmp_config_rom[256];
0062
0063 static size_t config_rom_length = 1 + 4 + 1 + 1;
0064
0065 #define BIB_CRC(v) ((v) << 0)
0066 #define BIB_CRC_LENGTH(v) ((v) << 16)
0067 #define BIB_INFO_LENGTH(v) ((v) << 24)
0068 #define BIB_BUS_NAME 0x31333934
0069 #define BIB_LINK_SPEED(v) ((v) << 0)
0070 #define BIB_GENERATION(v) ((v) << 4)
0071 #define BIB_MAX_ROM(v) ((v) << 8)
0072 #define BIB_MAX_RECEIVE(v) ((v) << 12)
0073 #define BIB_CYC_CLK_ACC(v) ((v) << 16)
0074 #define BIB_PMC ((1) << 27)
0075 #define BIB_BMC ((1) << 28)
0076 #define BIB_ISC ((1) << 29)
0077 #define BIB_CMC ((1) << 30)
0078 #define BIB_IRMC ((1) << 31)
0079 #define NODE_CAPABILITIES 0x0c0083c0
0080
0081
0082
0083
0084
0085
0086 #define DEFAULT_SPLIT_TIMEOUT (2 * 8000)
0087
0088 #define CANON_OUI 0x000085
0089
0090 static void generate_config_rom(struct fw_card *card, __be32 *config_rom)
0091 {
0092 struct fw_descriptor *desc;
0093 int i, j, k, length;
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 config_rom[0] = cpu_to_be32(
0105 BIB_CRC_LENGTH(4) | BIB_INFO_LENGTH(4) | BIB_CRC(0));
0106 config_rom[1] = cpu_to_be32(BIB_BUS_NAME);
0107 config_rom[2] = cpu_to_be32(
0108 BIB_LINK_SPEED(card->link_speed) |
0109 BIB_GENERATION(card->config_rom_generation++ % 14 + 2) |
0110 BIB_MAX_ROM(2) |
0111 BIB_MAX_RECEIVE(card->max_receive) |
0112 BIB_BMC | BIB_ISC | BIB_CMC | BIB_IRMC);
0113 config_rom[3] = cpu_to_be32(card->guid >> 32);
0114 config_rom[4] = cpu_to_be32(card->guid);
0115
0116
0117 config_rom[6] = cpu_to_be32(NODE_CAPABILITIES);
0118 i = 7;
0119 j = 7 + descriptor_count;
0120
0121
0122 list_for_each_entry (desc, &descriptor_list, link) {
0123 if (desc->immediate > 0)
0124 config_rom[i++] = cpu_to_be32(desc->immediate);
0125 config_rom[i] = cpu_to_be32(desc->key | (j - i));
0126 i++;
0127 j += desc->length;
0128 }
0129
0130
0131 config_rom[5] = cpu_to_be32((i - 5 - 1) << 16);
0132
0133
0134 list_for_each_entry (desc, &descriptor_list, link) {
0135 for (k = 0; k < desc->length; k++)
0136 config_rom[i + k] = cpu_to_be32(desc->data[k]);
0137 i += desc->length;
0138 }
0139
0140
0141
0142
0143
0144 for (i = 0; i < j; i += length + 1)
0145 length = fw_compute_block_crc(config_rom + i);
0146
0147 WARN_ON(j != config_rom_length);
0148 }
0149
0150 static void update_config_roms(void)
0151 {
0152 struct fw_card *card;
0153
0154 list_for_each_entry (card, &card_list, link) {
0155 generate_config_rom(card, tmp_config_rom);
0156 card->driver->set_config_rom(card, tmp_config_rom,
0157 config_rom_length);
0158 }
0159 }
0160
0161 static size_t required_space(struct fw_descriptor *desc)
0162 {
0163
0164 return desc->length + 1 + (desc->immediate > 0 ? 1 : 0);
0165 }
0166
0167 int fw_core_add_descriptor(struct fw_descriptor *desc)
0168 {
0169 size_t i;
0170 int ret;
0171
0172
0173
0174
0175
0176
0177 i = 0;
0178 while (i < desc->length)
0179 i += (desc->data[i] >> 16) + 1;
0180
0181 if (i != desc->length)
0182 return -EINVAL;
0183
0184 mutex_lock(&card_mutex);
0185
0186 if (config_rom_length + required_space(desc) > 256) {
0187 ret = -EBUSY;
0188 } else {
0189 list_add_tail(&desc->link, &descriptor_list);
0190 config_rom_length += required_space(desc);
0191 descriptor_count++;
0192 if (desc->immediate > 0)
0193 descriptor_count++;
0194 update_config_roms();
0195 ret = 0;
0196 }
0197
0198 mutex_unlock(&card_mutex);
0199
0200 return ret;
0201 }
0202 EXPORT_SYMBOL(fw_core_add_descriptor);
0203
0204 void fw_core_remove_descriptor(struct fw_descriptor *desc)
0205 {
0206 mutex_lock(&card_mutex);
0207
0208 list_del(&desc->link);
0209 config_rom_length -= required_space(desc);
0210 descriptor_count--;
0211 if (desc->immediate > 0)
0212 descriptor_count--;
0213 update_config_roms();
0214
0215 mutex_unlock(&card_mutex);
0216 }
0217 EXPORT_SYMBOL(fw_core_remove_descriptor);
0218
0219 static int reset_bus(struct fw_card *card, bool short_reset)
0220 {
0221 int reg = short_reset ? 5 : 1;
0222 int bit = short_reset ? PHY_BUS_SHORT_RESET : PHY_BUS_RESET;
0223
0224 return card->driver->update_phy_reg(card, reg, 0, bit);
0225 }
0226
0227 void fw_schedule_bus_reset(struct fw_card *card, bool delayed, bool short_reset)
0228 {
0229
0230 card->br_short = short_reset;
0231
0232
0233 fw_card_get(card);
0234 if (!queue_delayed_work(fw_workqueue, &card->br_work,
0235 delayed ? DIV_ROUND_UP(HZ, 100) : 0))
0236 fw_card_put(card);
0237 }
0238 EXPORT_SYMBOL(fw_schedule_bus_reset);
0239
0240 static void br_work(struct work_struct *work)
0241 {
0242 struct fw_card *card = container_of(work, struct fw_card, br_work.work);
0243
0244
0245 if (card->reset_jiffies != 0 &&
0246 time_before64(get_jiffies_64(), card->reset_jiffies + 2 * HZ)) {
0247 if (!queue_delayed_work(fw_workqueue, &card->br_work, 2 * HZ))
0248 fw_card_put(card);
0249 return;
0250 }
0251
0252 fw_send_phy_config(card, FW_PHY_CONFIG_NO_NODE_ID, card->generation,
0253 FW_PHY_CONFIG_CURRENT_GAP_COUNT);
0254 reset_bus(card, card->br_short);
0255 fw_card_put(card);
0256 }
0257
0258 static void allocate_broadcast_channel(struct fw_card *card, int generation)
0259 {
0260 int channel, bandwidth = 0;
0261
0262 if (!card->broadcast_channel_allocated) {
0263 fw_iso_resource_manage(card, generation, 1ULL << 31,
0264 &channel, &bandwidth, true);
0265 if (channel != 31) {
0266 fw_notice(card, "failed to allocate broadcast channel\n");
0267 return;
0268 }
0269 card->broadcast_channel_allocated = true;
0270 }
0271
0272 device_for_each_child(card->device, (void *)(long)generation,
0273 fw_device_set_broadcast_channel);
0274 }
0275
0276 static const char gap_count_table[] = {
0277 63, 5, 7, 8, 10, 13, 16, 18, 21, 24, 26, 29, 32, 35, 37, 40
0278 };
0279
0280 void fw_schedule_bm_work(struct fw_card *card, unsigned long delay)
0281 {
0282 fw_card_get(card);
0283 if (!schedule_delayed_work(&card->bm_work, delay))
0284 fw_card_put(card);
0285 }
0286
0287 static void bm_work(struct work_struct *work)
0288 {
0289 struct fw_card *card = container_of(work, struct fw_card, bm_work.work);
0290 struct fw_device *root_device, *irm_device;
0291 struct fw_node *root_node;
0292 int root_id, new_root_id, irm_id, bm_id, local_id;
0293 int gap_count, generation, grace, rcode;
0294 bool do_reset = false;
0295 bool root_device_is_running;
0296 bool root_device_is_cmc;
0297 bool irm_is_1394_1995_only;
0298 bool keep_this_irm;
0299 __be32 transaction_data[2];
0300
0301 spin_lock_irq(&card->lock);
0302
0303 if (card->local_node == NULL) {
0304 spin_unlock_irq(&card->lock);
0305 goto out_put_card;
0306 }
0307
0308 generation = card->generation;
0309
0310 root_node = card->root_node;
0311 fw_node_get(root_node);
0312 root_device = root_node->data;
0313 root_device_is_running = root_device &&
0314 atomic_read(&root_device->state) == FW_DEVICE_RUNNING;
0315 root_device_is_cmc = root_device && root_device->cmc;
0316
0317 irm_device = card->irm_node->data;
0318 irm_is_1394_1995_only = irm_device && irm_device->config_rom &&
0319 (irm_device->config_rom[2] & 0x000000f0) == 0;
0320
0321
0322 keep_this_irm = irm_device && irm_device->config_rom &&
0323 irm_device->config_rom[3] >> 8 == CANON_OUI;
0324
0325 root_id = root_node->node_id;
0326 irm_id = card->irm_node->node_id;
0327 local_id = card->local_node->node_id;
0328
0329 grace = time_after64(get_jiffies_64(),
0330 card->reset_jiffies + DIV_ROUND_UP(HZ, 8));
0331
0332 if ((is_next_generation(generation, card->bm_generation) &&
0333 !card->bm_abdicate) ||
0334 (card->bm_generation != generation && grace)) {
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347 if (!card->irm_node->link_on) {
0348 new_root_id = local_id;
0349 fw_notice(card, "%s, making local node (%02x) root\n",
0350 "IRM has link off", new_root_id);
0351 goto pick_me;
0352 }
0353
0354 if (irm_is_1394_1995_only && !keep_this_irm) {
0355 new_root_id = local_id;
0356 fw_notice(card, "%s, making local node (%02x) root\n",
0357 "IRM is not 1394a compliant", new_root_id);
0358 goto pick_me;
0359 }
0360
0361 transaction_data[0] = cpu_to_be32(0x3f);
0362 transaction_data[1] = cpu_to_be32(local_id);
0363
0364 spin_unlock_irq(&card->lock);
0365
0366 rcode = fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
0367 irm_id, generation, SCODE_100,
0368 CSR_REGISTER_BASE + CSR_BUS_MANAGER_ID,
0369 transaction_data, 8);
0370
0371 if (rcode == RCODE_GENERATION)
0372
0373 goto out;
0374
0375 bm_id = be32_to_cpu(transaction_data[0]);
0376
0377 spin_lock_irq(&card->lock);
0378 if (rcode == RCODE_COMPLETE && generation == card->generation)
0379 card->bm_node_id =
0380 bm_id == 0x3f ? local_id : 0xffc0 | bm_id;
0381 spin_unlock_irq(&card->lock);
0382
0383 if (rcode == RCODE_COMPLETE && bm_id != 0x3f) {
0384
0385 if (local_id == irm_id)
0386 allocate_broadcast_channel(card, generation);
0387
0388 goto out;
0389 }
0390
0391 if (rcode == RCODE_SEND_ERROR) {
0392
0393
0394
0395
0396
0397 fw_schedule_bm_work(card, DIV_ROUND_UP(HZ, 8));
0398 goto out;
0399 }
0400
0401 spin_lock_irq(&card->lock);
0402
0403 if (rcode != RCODE_COMPLETE && !keep_this_irm) {
0404
0405
0406
0407
0408
0409
0410 new_root_id = local_id;
0411 fw_notice(card, "BM lock failed (%s), making local node (%02x) root\n",
0412 fw_rcode_string(rcode), new_root_id);
0413 goto pick_me;
0414 }
0415 } else if (card->bm_generation != generation) {
0416
0417
0418
0419
0420 spin_unlock_irq(&card->lock);
0421 fw_schedule_bm_work(card, DIV_ROUND_UP(HZ, 8));
0422 goto out;
0423 }
0424
0425
0426
0427
0428
0429
0430 card->bm_generation = generation;
0431
0432 if (root_device == NULL) {
0433
0434
0435
0436
0437 new_root_id = local_id;
0438 } else if (!root_device_is_running) {
0439
0440
0441
0442
0443 spin_unlock_irq(&card->lock);
0444 goto out;
0445 } else if (root_device_is_cmc) {
0446
0447
0448
0449
0450 new_root_id = root_id;
0451 } else {
0452
0453
0454
0455
0456
0457 new_root_id = local_id;
0458 }
0459
0460 pick_me:
0461
0462
0463
0464
0465 if (!card->beta_repeaters_present &&
0466 root_node->max_hops < ARRAY_SIZE(gap_count_table))
0467 gap_count = gap_count_table[root_node->max_hops];
0468 else
0469 gap_count = 63;
0470
0471
0472
0473
0474
0475
0476
0477 if (card->bm_retries++ < 5 &&
0478 (card->gap_count != gap_count || new_root_id != root_id))
0479 do_reset = true;
0480
0481 spin_unlock_irq(&card->lock);
0482
0483 if (do_reset) {
0484 fw_notice(card, "phy config: new root=%x, gap_count=%d\n",
0485 new_root_id, gap_count);
0486 fw_send_phy_config(card, new_root_id, generation, gap_count);
0487 reset_bus(card, true);
0488
0489 goto out;
0490 }
0491
0492 if (root_device_is_cmc) {
0493
0494
0495
0496 transaction_data[0] = cpu_to_be32(CSR_STATE_BIT_CMSTR);
0497 rcode = fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
0498 root_id, generation, SCODE_100,
0499 CSR_REGISTER_BASE + CSR_STATE_SET,
0500 transaction_data, 4);
0501 if (rcode == RCODE_GENERATION)
0502 goto out;
0503 }
0504
0505 if (local_id == irm_id)
0506 allocate_broadcast_channel(card, generation);
0507
0508 out:
0509 fw_node_put(root_node);
0510 out_put_card:
0511 fw_card_put(card);
0512 }
0513
0514 void fw_card_initialize(struct fw_card *card,
0515 const struct fw_card_driver *driver,
0516 struct device *device)
0517 {
0518 static atomic_t index = ATOMIC_INIT(-1);
0519
0520 card->index = atomic_inc_return(&index);
0521 card->driver = driver;
0522 card->device = device;
0523 card->current_tlabel = 0;
0524 card->tlabel_mask = 0;
0525 card->split_timeout_hi = DEFAULT_SPLIT_TIMEOUT / 8000;
0526 card->split_timeout_lo = (DEFAULT_SPLIT_TIMEOUT % 8000) << 19;
0527 card->split_timeout_cycles = DEFAULT_SPLIT_TIMEOUT;
0528 card->split_timeout_jiffies =
0529 DIV_ROUND_UP(DEFAULT_SPLIT_TIMEOUT * HZ, 8000);
0530 card->color = 0;
0531 card->broadcast_channel = BROADCAST_CHANNEL_INITIAL;
0532
0533 kref_init(&card->kref);
0534 init_completion(&card->done);
0535 INIT_LIST_HEAD(&card->transaction_list);
0536 INIT_LIST_HEAD(&card->phy_receiver_list);
0537 spin_lock_init(&card->lock);
0538
0539 card->local_node = NULL;
0540
0541 INIT_DELAYED_WORK(&card->br_work, br_work);
0542 INIT_DELAYED_WORK(&card->bm_work, bm_work);
0543 }
0544 EXPORT_SYMBOL(fw_card_initialize);
0545
0546 int fw_card_add(struct fw_card *card,
0547 u32 max_receive, u32 link_speed, u64 guid)
0548 {
0549 int ret;
0550
0551 card->max_receive = max_receive;
0552 card->link_speed = link_speed;
0553 card->guid = guid;
0554
0555 mutex_lock(&card_mutex);
0556
0557 generate_config_rom(card, tmp_config_rom);
0558 ret = card->driver->enable(card, tmp_config_rom, config_rom_length);
0559 if (ret == 0)
0560 list_add_tail(&card->link, &card_list);
0561
0562 mutex_unlock(&card_mutex);
0563
0564 return ret;
0565 }
0566 EXPORT_SYMBOL(fw_card_add);
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581 static int dummy_read_phy_reg(struct fw_card *card, int address)
0582 {
0583 return -ENODEV;
0584 }
0585
0586 static int dummy_update_phy_reg(struct fw_card *card, int address,
0587 int clear_bits, int set_bits)
0588 {
0589 return -ENODEV;
0590 }
0591
0592 static void dummy_send_request(struct fw_card *card, struct fw_packet *packet)
0593 {
0594 packet->callback(packet, card, RCODE_CANCELLED);
0595 }
0596
0597 static void dummy_send_response(struct fw_card *card, struct fw_packet *packet)
0598 {
0599 packet->callback(packet, card, RCODE_CANCELLED);
0600 }
0601
0602 static int dummy_cancel_packet(struct fw_card *card, struct fw_packet *packet)
0603 {
0604 return -ENOENT;
0605 }
0606
0607 static int dummy_enable_phys_dma(struct fw_card *card,
0608 int node_id, int generation)
0609 {
0610 return -ENODEV;
0611 }
0612
0613 static struct fw_iso_context *dummy_allocate_iso_context(struct fw_card *card,
0614 int type, int channel, size_t header_size)
0615 {
0616 return ERR_PTR(-ENODEV);
0617 }
0618
0619 static u32 dummy_read_csr(struct fw_card *card, int csr_offset)
0620 {
0621 return 0;
0622 }
0623
0624 static void dummy_write_csr(struct fw_card *card, int csr_offset, u32 value)
0625 {
0626 }
0627
0628 static int dummy_start_iso(struct fw_iso_context *ctx,
0629 s32 cycle, u32 sync, u32 tags)
0630 {
0631 return -ENODEV;
0632 }
0633
0634 static int dummy_set_iso_channels(struct fw_iso_context *ctx, u64 *channels)
0635 {
0636 return -ENODEV;
0637 }
0638
0639 static int dummy_queue_iso(struct fw_iso_context *ctx, struct fw_iso_packet *p,
0640 struct fw_iso_buffer *buffer, unsigned long payload)
0641 {
0642 return -ENODEV;
0643 }
0644
0645 static void dummy_flush_queue_iso(struct fw_iso_context *ctx)
0646 {
0647 }
0648
0649 static int dummy_flush_iso_completions(struct fw_iso_context *ctx)
0650 {
0651 return -ENODEV;
0652 }
0653
0654 static const struct fw_card_driver dummy_driver_template = {
0655 .read_phy_reg = dummy_read_phy_reg,
0656 .update_phy_reg = dummy_update_phy_reg,
0657 .send_request = dummy_send_request,
0658 .send_response = dummy_send_response,
0659 .cancel_packet = dummy_cancel_packet,
0660 .enable_phys_dma = dummy_enable_phys_dma,
0661 .read_csr = dummy_read_csr,
0662 .write_csr = dummy_write_csr,
0663 .allocate_iso_context = dummy_allocate_iso_context,
0664 .start_iso = dummy_start_iso,
0665 .set_iso_channels = dummy_set_iso_channels,
0666 .queue_iso = dummy_queue_iso,
0667 .flush_queue_iso = dummy_flush_queue_iso,
0668 .flush_iso_completions = dummy_flush_iso_completions,
0669 };
0670
0671 void fw_card_release(struct kref *kref)
0672 {
0673 struct fw_card *card = container_of(kref, struct fw_card, kref);
0674
0675 complete(&card->done);
0676 }
0677 EXPORT_SYMBOL_GPL(fw_card_release);
0678
0679 void fw_core_remove_card(struct fw_card *card)
0680 {
0681 struct fw_card_driver dummy_driver = dummy_driver_template;
0682 unsigned long flags;
0683
0684 card->driver->update_phy_reg(card, 4,
0685 PHY_LINK_ACTIVE | PHY_CONTENDER, 0);
0686 fw_schedule_bus_reset(card, false, true);
0687
0688 mutex_lock(&card_mutex);
0689 list_del_init(&card->link);
0690 mutex_unlock(&card_mutex);
0691
0692
0693 dummy_driver.free_iso_context = card->driver->free_iso_context;
0694 dummy_driver.stop_iso = card->driver->stop_iso;
0695 card->driver = &dummy_driver;
0696
0697 spin_lock_irqsave(&card->lock, flags);
0698 fw_destroy_nodes(card);
0699 spin_unlock_irqrestore(&card->lock, flags);
0700
0701
0702 fw_card_put(card);
0703 wait_for_completion(&card->done);
0704
0705 WARN_ON(!list_empty(&card->transaction_list));
0706 }
0707 EXPORT_SYMBOL(fw_core_remove_card);
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725 int fw_card_read_cycle_time(struct fw_card *card, u32 *cycle_time)
0726 {
0727 if (card->driver->read_csr == dummy_read_csr)
0728 return -ENODEV;
0729
0730
0731
0732 *cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
0733 return 0;
0734 }
0735 EXPORT_SYMBOL_GPL(fw_card_read_cycle_time);