0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <linux/types.h>
0018 #include <linux/kernel.h>
0019
0020 #include <linux/delay.h>
0021 #include <linux/dma-mapping.h>
0022 #include <linux/init.h>
0023 #include <linux/rio.h>
0024 #include <linux/rio_drv.h>
0025 #include <linux/rio_ids.h>
0026 #include <linux/rio_regs.h>
0027 #include <linux/module.h>
0028 #include <linux/spinlock.h>
0029 #include <linux/timer.h>
0030 #include <linux/sched.h>
0031 #include <linux/jiffies.h>
0032 #include <linux/slab.h>
0033
0034 #include "rio.h"
0035
0036 static void rio_init_em(struct rio_dev *rdev);
0037
0038 struct rio_id_table {
0039 u16 start;
0040 u32 max;
0041 spinlock_t lock;
0042 unsigned long table[];
0043 };
0044
0045 static int next_destid = 0;
0046 static int next_comptag = 1;
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 static u16 rio_destid_alloc(struct rio_net *net)
0057 {
0058 int destid;
0059 struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
0060
0061 spin_lock(&idtab->lock);
0062 destid = find_first_zero_bit(idtab->table, idtab->max);
0063
0064 if (destid < idtab->max) {
0065 set_bit(destid, idtab->table);
0066 destid += idtab->start;
0067 } else
0068 destid = RIO_INVALID_DESTID;
0069
0070 spin_unlock(&idtab->lock);
0071 return (u16)destid;
0072 }
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 static int rio_destid_reserve(struct rio_net *net, u16 destid)
0083 {
0084 int oldbit;
0085 struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
0086
0087 destid -= idtab->start;
0088 spin_lock(&idtab->lock);
0089 oldbit = test_and_set_bit(destid, idtab->table);
0090 spin_unlock(&idtab->lock);
0091 return oldbit;
0092 }
0093
0094
0095
0096
0097
0098
0099
0100
0101 static void rio_destid_free(struct rio_net *net, u16 destid)
0102 {
0103 struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
0104
0105 destid -= idtab->start;
0106 spin_lock(&idtab->lock);
0107 clear_bit(destid, idtab->table);
0108 spin_unlock(&idtab->lock);
0109 }
0110
0111
0112
0113
0114
0115 static u16 rio_destid_first(struct rio_net *net)
0116 {
0117 int destid;
0118 struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
0119
0120 spin_lock(&idtab->lock);
0121 destid = find_first_bit(idtab->table, idtab->max);
0122 if (destid >= idtab->max)
0123 destid = RIO_INVALID_DESTID;
0124 else
0125 destid += idtab->start;
0126 spin_unlock(&idtab->lock);
0127 return (u16)destid;
0128 }
0129
0130
0131
0132
0133
0134
0135 static u16 rio_destid_next(struct rio_net *net, u16 from)
0136 {
0137 int destid;
0138 struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
0139
0140 spin_lock(&idtab->lock);
0141 destid = find_next_bit(idtab->table, idtab->max, from);
0142 if (destid >= idtab->max)
0143 destid = RIO_INVALID_DESTID;
0144 else
0145 destid += idtab->start;
0146 spin_unlock(&idtab->lock);
0147 return (u16)destid;
0148 }
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159 static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
0160 {
0161 u32 result;
0162
0163 rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
0164
0165 return RIO_GET_DID(port->sys_size, result);
0166 }
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
0178 {
0179 rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
0180 RIO_SET_DID(port->sys_size, did));
0181 }
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191 static int rio_clear_locks(struct rio_net *net)
0192 {
0193 struct rio_mport *port = net->hport;
0194 struct rio_dev *rdev;
0195 u32 result;
0196 int ret = 0;
0197
0198
0199 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
0200 port->host_deviceid);
0201 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
0202 if ((result & 0xffff) != 0xffff) {
0203 printk(KERN_INFO
0204 "RIO: badness when releasing host lock on master port, result %8.8x\n",
0205 result);
0206 ret = -EINVAL;
0207 }
0208 list_for_each_entry(rdev, &net->devices, net_list) {
0209 rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
0210 port->host_deviceid);
0211 rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
0212 if ((result & 0xffff) != 0xffff) {
0213 printk(KERN_INFO
0214 "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
0215 rdev->vid, rdev->did);
0216 ret = -EINVAL;
0217 }
0218
0219
0220 rio_read_config_32(rdev,
0221 rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
0222 &result);
0223 result |= RIO_PORT_GEN_DISCOVERED | RIO_PORT_GEN_MASTER;
0224 rio_write_config_32(rdev,
0225 rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
0226 result);
0227 }
0228
0229 return ret;
0230 }
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240 static int rio_enum_host(struct rio_mport *port)
0241 {
0242 u32 result;
0243
0244
0245 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
0246 port->host_deviceid);
0247
0248 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
0249 if ((result & 0xffff) != port->host_deviceid)
0250 return -1;
0251
0252
0253 rio_local_set_device_id(port, port->host_deviceid);
0254 return 0;
0255 }
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267 static int rio_device_has_destid(struct rio_mport *port, int src_ops,
0268 int dst_ops)
0269 {
0270 u32 mask = RIO_OPS_READ | RIO_OPS_WRITE | RIO_OPS_ATOMIC_TST_SWP | RIO_OPS_ATOMIC_INC | RIO_OPS_ATOMIC_DEC | RIO_OPS_ATOMIC_SET | RIO_OPS_ATOMIC_CLR;
0271
0272 return !!((src_ops | dst_ops) & mask);
0273 }
0274
0275
0276
0277
0278
0279
0280
0281
0282 static void rio_release_dev(struct device *dev)
0283 {
0284 struct rio_dev *rdev;
0285
0286 rdev = to_rio_dev(dev);
0287 kfree(rdev);
0288 }
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299 static int rio_is_switch(struct rio_dev *rdev)
0300 {
0301 if (rdev->pef & RIO_PEF_SWITCH)
0302 return 1;
0303 return 0;
0304 }
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322 static struct rio_dev *rio_setup_device(struct rio_net *net,
0323 struct rio_mport *port, u16 destid,
0324 u8 hopcount, int do_enum)
0325 {
0326 int ret = 0;
0327 struct rio_dev *rdev;
0328 struct rio_switch *rswitch = NULL;
0329 int result, rdid;
0330 size_t size;
0331 u32 swpinfo = 0;
0332
0333 size = sizeof(*rdev);
0334 if (rio_mport_read_config_32(port, destid, hopcount,
0335 RIO_PEF_CAR, &result))
0336 return NULL;
0337
0338 if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) {
0339 rio_mport_read_config_32(port, destid, hopcount,
0340 RIO_SWP_INFO_CAR, &swpinfo);
0341 if (result & RIO_PEF_SWITCH)
0342 size += struct_size(rswitch, nextdev, RIO_GET_TOTAL_PORTS(swpinfo));
0343 }
0344
0345 rdev = kzalloc(size, GFP_KERNEL);
0346 if (!rdev)
0347 return NULL;
0348
0349 rdev->net = net;
0350 rdev->pef = result;
0351 rdev->swpinfo = swpinfo;
0352 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
0353 &result);
0354 rdev->did = result >> 16;
0355 rdev->vid = result & 0xffff;
0356 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
0357 &rdev->device_rev);
0358 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
0359 &result);
0360 rdev->asm_did = result >> 16;
0361 rdev->asm_vid = result & 0xffff;
0362 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
0363 &result);
0364 rdev->asm_rev = result >> 16;
0365 if (rdev->pef & RIO_PEF_EXT_FEATURES) {
0366 rdev->efptr = result & 0xffff;
0367 rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid,
0368 hopcount, &rdev->phys_rmap);
0369 pr_debug("RIO: %s Register Map %d device\n",
0370 __func__, rdev->phys_rmap);
0371
0372 rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
0373 hopcount, RIO_EFB_ERR_MGMNT);
0374 if (!rdev->em_efptr)
0375 rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
0376 hopcount, RIO_EFB_ERR_MGMNT_HS);
0377 }
0378
0379 rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
0380 &rdev->src_ops);
0381 rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
0382 &rdev->dst_ops);
0383
0384 if (do_enum) {
0385
0386 if (next_comptag >= 0x10000) {
0387 pr_err("RIO: Component Tag Counter Overflow\n");
0388 goto cleanup;
0389 }
0390 rio_mport_write_config_32(port, destid, hopcount,
0391 RIO_COMPONENT_TAG_CSR, next_comptag);
0392 rdev->comp_tag = next_comptag++;
0393 rdev->do_enum = true;
0394 } else {
0395 rio_mport_read_config_32(port, destid, hopcount,
0396 RIO_COMPONENT_TAG_CSR,
0397 &rdev->comp_tag);
0398 }
0399
0400 if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
0401 if (do_enum) {
0402 rio_set_device_id(port, destid, hopcount, next_destid);
0403 rdev->destid = next_destid;
0404 next_destid = rio_destid_alloc(net);
0405 } else
0406 rdev->destid = rio_get_device_id(port, destid, hopcount);
0407
0408 rdev->hopcount = 0xff;
0409 } else {
0410
0411
0412
0413 rdev->destid = destid;
0414 rdev->hopcount = hopcount;
0415 }
0416
0417
0418 if (rio_is_switch(rdev)) {
0419 rswitch = rdev->rswitch;
0420 rswitch->port_ok = 0;
0421 spin_lock_init(&rswitch->lock);
0422 rswitch->route_table =
0423 kzalloc(RIO_MAX_ROUTE_ENTRIES(port->sys_size),
0424 GFP_KERNEL);
0425 if (!rswitch->route_table)
0426 goto cleanup;
0427
0428 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
0429 rdid++)
0430 rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
0431 dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
0432 rdev->comp_tag & RIO_CTAG_UDEVID);
0433
0434 if (do_enum)
0435 rio_route_clr_table(rdev, RIO_GLOBAL_TABLE, 0);
0436 } else {
0437 if (do_enum)
0438
0439 rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
0440
0441 dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
0442 rdev->comp_tag & RIO_CTAG_UDEVID);
0443 }
0444
0445 rdev->dev.parent = &net->dev;
0446 rio_attach_device(rdev);
0447 rdev->dev.release = rio_release_dev;
0448 rdev->dma_mask = DMA_BIT_MASK(32);
0449 rdev->dev.dma_mask = &rdev->dma_mask;
0450 rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
0451
0452 if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
0453 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
0454 0, 0xffff);
0455
0456 ret = rio_add_device(rdev);
0457 if (ret)
0458 goto cleanup;
0459
0460 rio_dev_get(rdev);
0461
0462 return rdev;
0463
0464 cleanup:
0465 if (rswitch)
0466 kfree(rswitch->route_table);
0467
0468 kfree(rdev);
0469 return NULL;
0470 }
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482 static int
0483 rio_sport_is_active(struct rio_dev *rdev, int sp)
0484 {
0485 u32 result = 0;
0486
0487 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, sp),
0488 &result);
0489
0490 return result & RIO_PORT_N_ERR_STS_PORT_OK;
0491 }
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501 static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
0502 {
0503 u32 result;
0504
0505 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
0506 RIO_HOST_DID_LOCK_CSR, &result);
0507
0508 return (u16) (result & 0xffff);
0509 }
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522 static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
0523 u8 hopcount, struct rio_dev *prev, int prev_port)
0524 {
0525 struct rio_dev *rdev;
0526 u32 regval;
0527 int tmp;
0528
0529 if (rio_mport_chk_dev_access(port,
0530 RIO_ANY_DESTID(port->sys_size), hopcount)) {
0531 pr_debug("RIO: device access check failed\n");
0532 return -1;
0533 }
0534
0535 if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
0536 pr_debug("RIO: PE already discovered by this host\n");
0537
0538
0539
0540
0541 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size),
0542 hopcount, RIO_COMPONENT_TAG_CSR, ®val);
0543
0544 if (regval) {
0545 rdev = rio_get_comptag((regval & 0xffff), NULL);
0546
0547 if (rdev && prev && rio_is_switch(prev)) {
0548 pr_debug("RIO: redundant path to %s\n",
0549 rio_name(rdev));
0550 prev->rswitch->nextdev[prev_port] = rdev;
0551 }
0552 }
0553
0554 return 0;
0555 }
0556
0557
0558 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
0559 hopcount,
0560 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
0561 while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
0562 < port->host_deviceid) {
0563
0564 mdelay(1);
0565
0566 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
0567 hopcount,
0568 RIO_HOST_DID_LOCK_CSR,
0569 port->host_deviceid);
0570 }
0571
0572 if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
0573 pr_debug(
0574 "RIO: PE locked by a higher priority host...retreating\n");
0575 return -1;
0576 }
0577
0578
0579 rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
0580 hopcount, 1);
0581 if (rdev) {
0582 rdev->prev = prev;
0583 if (prev && rio_is_switch(prev))
0584 prev->rswitch->nextdev[prev_port] = rdev;
0585 } else
0586 return -1;
0587
0588 if (rio_is_switch(rdev)) {
0589 int sw_destid;
0590 int cur_destid;
0591 int sw_inport;
0592 u16 destid;
0593 int port_num;
0594
0595 sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
0596 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
0597 port->host_deviceid, sw_inport, 0);
0598 rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
0599
0600 destid = rio_destid_first(net);
0601 while (destid != RIO_INVALID_DESTID && destid < next_destid) {
0602 if (destid != port->host_deviceid) {
0603 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
0604 destid, sw_inport, 0);
0605 rdev->rswitch->route_table[destid] = sw_inport;
0606 }
0607 destid = rio_destid_next(net, destid + 1);
0608 }
0609 pr_debug(
0610 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
0611 rio_name(rdev), rdev->vid, rdev->did,
0612 RIO_GET_TOTAL_PORTS(rdev->swpinfo));
0613 sw_destid = next_destid;
0614 for (port_num = 0;
0615 port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
0616 port_num++) {
0617 if (sw_inport == port_num) {
0618 rio_enable_rx_tx_port(port, 0,
0619 RIO_ANY_DESTID(port->sys_size),
0620 hopcount, port_num);
0621 rdev->rswitch->port_ok |= (1 << port_num);
0622 continue;
0623 }
0624
0625 cur_destid = next_destid;
0626
0627 if (rio_sport_is_active(rdev, port_num)) {
0628 pr_debug(
0629 "RIO: scanning device on port %d\n",
0630 port_num);
0631 rio_enable_rx_tx_port(port, 0,
0632 RIO_ANY_DESTID(port->sys_size),
0633 hopcount, port_num);
0634 rdev->rswitch->port_ok |= (1 << port_num);
0635 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
0636 RIO_ANY_DESTID(port->sys_size),
0637 port_num, 0);
0638
0639 if (rio_enum_peer(net, port, hopcount + 1,
0640 rdev, port_num) < 0)
0641 return -1;
0642
0643
0644 destid = rio_destid_next(net, cur_destid + 1);
0645 if (destid != RIO_INVALID_DESTID) {
0646 for (destid = cur_destid;
0647 destid < next_destid;) {
0648 if (destid != port->host_deviceid) {
0649 rio_route_add_entry(rdev,
0650 RIO_GLOBAL_TABLE,
0651 destid,
0652 port_num,
0653 0);
0654 rdev->rswitch->
0655 route_table[destid] =
0656 port_num;
0657 }
0658 destid = rio_destid_next(net,
0659 destid + 1);
0660 }
0661 }
0662 } else {
0663
0664
0665
0666 if (rdev->em_efptr)
0667 rio_set_port_lockout(rdev, port_num, 1);
0668
0669 rdev->rswitch->port_ok &= ~(1 << port_num);
0670 }
0671 }
0672
0673
0674 if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
0675 (rdev->em_efptr)) {
0676 rio_write_config_32(rdev,
0677 rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
0678 (port->host_deviceid << 16) |
0679 (port->sys_size << 15));
0680 }
0681
0682 rio_init_em(rdev);
0683
0684
0685 if (next_destid == sw_destid)
0686 next_destid = rio_destid_alloc(net);
0687
0688 rdev->destid = sw_destid;
0689 } else
0690 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
0691 rio_name(rdev), rdev->vid, rdev->did);
0692
0693 return 0;
0694 }
0695
0696
0697
0698
0699
0700
0701
0702
0703
0704 static int rio_enum_complete(struct rio_mport *port)
0705 {
0706 u32 regval;
0707
0708 rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR,
0709 ®val);
0710 return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0;
0711 }
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725 static int
0726 rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
0727 u8 hopcount, struct rio_dev *prev, int prev_port)
0728 {
0729 u8 port_num, route_port;
0730 struct rio_dev *rdev;
0731 u16 ndestid;
0732
0733
0734 if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
0735 rdev->prev = prev;
0736 if (prev && rio_is_switch(prev))
0737 prev->rswitch->nextdev[prev_port] = rdev;
0738 } else
0739 return -1;
0740
0741 if (rio_is_switch(rdev)) {
0742
0743 rdev->destid = destid;
0744
0745 pr_debug(
0746 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
0747 rio_name(rdev), rdev->vid, rdev->did,
0748 RIO_GET_TOTAL_PORTS(rdev->swpinfo));
0749 for (port_num = 0;
0750 port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
0751 port_num++) {
0752 if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
0753 continue;
0754
0755 if (rio_sport_is_active(rdev, port_num)) {
0756 pr_debug(
0757 "RIO: scanning device on port %d\n",
0758 port_num);
0759
0760 rio_lock_device(port, destid, hopcount, 1000);
0761
0762 for (ndestid = 0;
0763 ndestid < RIO_ANY_DESTID(port->sys_size);
0764 ndestid++) {
0765 rio_route_get_entry(rdev,
0766 RIO_GLOBAL_TABLE,
0767 ndestid,
0768 &route_port, 0);
0769 if (route_port == port_num)
0770 break;
0771 }
0772
0773 if (ndestid == RIO_ANY_DESTID(port->sys_size))
0774 continue;
0775 rio_unlock_device(port, destid, hopcount);
0776 if (rio_disc_peer(net, port, ndestid,
0777 hopcount + 1, rdev, port_num) < 0)
0778 return -1;
0779 }
0780 }
0781 } else
0782 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
0783 rio_name(rdev), rdev->vid, rdev->did);
0784
0785 return 0;
0786 }
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797 static int rio_mport_is_active(struct rio_mport *port)
0798 {
0799 u32 result = 0;
0800
0801 rio_local_read_config_32(port,
0802 port->phys_efptr +
0803 RIO_PORT_N_ERR_STS_CSR(port->index, port->phys_rmap),
0804 &result);
0805 return result & RIO_PORT_N_ERR_STS_PORT_OK;
0806 }
0807
0808 static void rio_scan_release_net(struct rio_net *net)
0809 {
0810 pr_debug("RIO-SCAN: %s: net_%d\n", __func__, net->id);
0811 kfree(net->enum_data);
0812 }
0813
0814 static void rio_scan_release_dev(struct device *dev)
0815 {
0816 struct rio_net *net;
0817
0818 net = to_rio_net(dev);
0819 pr_debug("RIO-SCAN: %s: net_%d\n", __func__, net->id);
0820 kfree(net);
0821 }
0822
0823
0824
0825
0826
0827
0828
0829
0830
0831
0832
0833 static struct rio_net *rio_scan_alloc_net(struct rio_mport *mport,
0834 int do_enum, u16 start)
0835 {
0836 struct rio_net *net;
0837
0838 net = rio_alloc_net(mport);
0839
0840 if (net && do_enum) {
0841 struct rio_id_table *idtab;
0842 size_t size;
0843
0844 size = sizeof(struct rio_id_table) +
0845 BITS_TO_LONGS(
0846 RIO_MAX_ROUTE_ENTRIES(mport->sys_size)
0847 ) * sizeof(long);
0848
0849 idtab = kzalloc(size, GFP_KERNEL);
0850
0851 if (idtab == NULL) {
0852 pr_err("RIO: failed to allocate destID table\n");
0853 rio_free_net(net);
0854 net = NULL;
0855 } else {
0856 net->enum_data = idtab;
0857 net->release = rio_scan_release_net;
0858 idtab->start = start;
0859 idtab->max = RIO_MAX_ROUTE_ENTRIES(mport->sys_size);
0860 spin_lock_init(&idtab->lock);
0861 }
0862 }
0863
0864 if (net) {
0865 net->id = mport->id;
0866 net->hport = mport;
0867 dev_set_name(&net->dev, "rnet_%d", net->id);
0868 net->dev.parent = &mport->dev;
0869 net->dev.release = rio_scan_release_dev;
0870 rio_add_net(net);
0871 }
0872
0873 return net;
0874 }
0875
0876
0877
0878
0879
0880
0881
0882
0883
0884 static void rio_update_route_tables(struct rio_net *net)
0885 {
0886 struct rio_dev *rdev, *swrdev;
0887 struct rio_switch *rswitch;
0888 u8 sport;
0889 u16 destid;
0890
0891 list_for_each_entry(rdev, &net->devices, net_list) {
0892
0893 destid = rdev->destid;
0894
0895 list_for_each_entry(rswitch, &net->switches, node) {
0896
0897 if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
0898 continue;
0899
0900 if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
0901 swrdev = sw_to_rio_dev(rswitch);
0902
0903
0904 if (swrdev->destid == destid)
0905 continue;
0906
0907 sport = RIO_GET_PORT_NUM(swrdev->swpinfo);
0908
0909 rio_route_add_entry(swrdev, RIO_GLOBAL_TABLE,
0910 destid, sport, 0);
0911 rswitch->route_table[destid] = sport;
0912 }
0913 }
0914 }
0915 }
0916
0917
0918
0919
0920
0921
0922
0923
0924 static void rio_init_em(struct rio_dev *rdev)
0925 {
0926 if (rio_is_switch(rdev) && (rdev->em_efptr) &&
0927 rdev->rswitch->ops && rdev->rswitch->ops->em_init) {
0928 rdev->rswitch->ops->em_init(rdev);
0929 }
0930 }
0931
0932
0933
0934
0935
0936
0937
0938
0939
0940
0941
0942 static int rio_enum_mport(struct rio_mport *mport, u32 flags)
0943 {
0944 struct rio_net *net = NULL;
0945 int rc = 0;
0946
0947 printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
0948 mport->name);
0949
0950
0951
0952
0953
0954
0955
0956 if (mport->nnode.next || mport->nnode.prev)
0957 return -EBUSY;
0958
0959
0960 if (rio_enum_host(mport) < 0) {
0961 printk(KERN_INFO
0962 "RIO: master port %d device has been enumerated by a remote host\n",
0963 mport->id);
0964 rc = -EBUSY;
0965 goto out;
0966 }
0967
0968
0969 if (rio_mport_is_active(mport)) {
0970 net = rio_scan_alloc_net(mport, 1, 0);
0971 if (!net) {
0972 printk(KERN_ERR "RIO: failed to allocate new net\n");
0973 rc = -ENOMEM;
0974 goto out;
0975 }
0976
0977
0978 rio_destid_reserve(net, mport->host_deviceid);
0979
0980
0981 rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
0982
0983
0984 rio_local_write_config_32(mport, RIO_COMPONENT_TAG_CSR,
0985 next_comptag++);
0986
0987 next_destid = rio_destid_alloc(net);
0988
0989 if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) {
0990
0991 printk(KERN_INFO
0992 "RIO: master port %d device has lost enumeration to a remote host\n",
0993 mport->id);
0994 rio_clear_locks(net);
0995 rc = -EBUSY;
0996 goto out;
0997 }
0998
0999 rio_destid_free(net, next_destid);
1000 rio_update_route_tables(net);
1001 rio_clear_locks(net);
1002 rio_pw_enable(mport, 1);
1003 } else {
1004 printk(KERN_INFO "RIO: master port %d link inactive\n",
1005 mport->id);
1006 rc = -EINVAL;
1007 }
1008
1009 out:
1010 return rc;
1011 }
1012
1013
1014
1015
1016
1017
1018
1019
1020 static void rio_build_route_tables(struct rio_net *net)
1021 {
1022 struct rio_switch *rswitch;
1023 struct rio_dev *rdev;
1024 int i;
1025 u8 sport;
1026
1027 list_for_each_entry(rswitch, &net->switches, node) {
1028 rdev = sw_to_rio_dev(rswitch);
1029
1030 rio_lock_device(net->hport, rdev->destid,
1031 rdev->hopcount, 1000);
1032 for (i = 0;
1033 i < RIO_MAX_ROUTE_ENTRIES(net->hport->sys_size);
1034 i++) {
1035 if (rio_route_get_entry(rdev, RIO_GLOBAL_TABLE,
1036 i, &sport, 0) < 0)
1037 continue;
1038 rswitch->route_table[i] = sport;
1039 }
1040
1041 rio_unlock_device(net->hport, rdev->destid, rdev->hopcount);
1042 }
1043 }
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057 static int rio_disc_mport(struct rio_mport *mport, u32 flags)
1058 {
1059 struct rio_net *net = NULL;
1060 unsigned long to_end;
1061
1062 printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
1063 mport->name);
1064
1065
1066 if (rio_mport_is_active(mport)) {
1067 if (rio_enum_complete(mport))
1068 goto enum_done;
1069 else if (flags & RIO_SCAN_ENUM_NO_WAIT)
1070 return -EAGAIN;
1071
1072 pr_debug("RIO: wait for enumeration to complete...\n");
1073
1074 to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
1075 while (time_before(jiffies, to_end)) {
1076 if (rio_enum_complete(mport))
1077 goto enum_done;
1078 msleep(10);
1079 }
1080
1081 pr_debug("RIO: discovery timeout on mport %d %s\n",
1082 mport->id, mport->name);
1083 goto bail;
1084 enum_done:
1085 pr_debug("RIO: ... enumeration done\n");
1086
1087 net = rio_scan_alloc_net(mport, 0, 0);
1088 if (!net) {
1089 printk(KERN_ERR "RIO: Failed to allocate new net\n");
1090 goto bail;
1091 }
1092
1093
1094 rio_local_read_config_32(mport, RIO_DID_CSR,
1095 &mport->host_deviceid);
1096 mport->host_deviceid = RIO_GET_DID(mport->sys_size,
1097 mport->host_deviceid);
1098
1099 if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
1100 0, NULL, 0) < 0) {
1101 printk(KERN_INFO
1102 "RIO: master port %d device has failed discovery\n",
1103 mport->id);
1104 goto bail;
1105 }
1106
1107 rio_build_route_tables(net);
1108 }
1109
1110 return 0;
1111 bail:
1112 return -EBUSY;
1113 }
1114
1115 static struct rio_scan rio_scan_ops = {
1116 .owner = THIS_MODULE,
1117 .enumerate = rio_enum_mport,
1118 .discover = rio_disc_mport,
1119 };
1120
1121 static bool scan;
1122 module_param(scan, bool, 0);
1123 MODULE_PARM_DESC(scan, "Start RapidIO network enumeration/discovery "
1124 "(default = 0)");
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140 static int __init rio_basic_attach(void)
1141 {
1142 if (rio_register_scan(RIO_MPORT_ANY, &rio_scan_ops))
1143 return -EIO;
1144 if (scan)
1145 rio_init_mports();
1146 return 0;
1147 }
1148
1149 late_initcall(rio_basic_attach);
1150
1151 MODULE_DESCRIPTION("Basic RapidIO enumeration/discovery");
1152 MODULE_LICENSE("GPL");