0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/types.h>
0014 #include <linux/kernel.h>
0015
0016 #include <linux/delay.h>
0017 #include <linux/init.h>
0018 #include <linux/rio.h>
0019 #include <linux/rio_drv.h>
0020 #include <linux/rio_ids.h>
0021 #include <linux/rio_regs.h>
0022 #include <linux/module.h>
0023 #include <linux/spinlock.h>
0024 #include <linux/slab.h>
0025 #include <linux/interrupt.h>
0026
0027 #include "rio.h"
0028
0029
0030
0031
0032
0033
0034
0035 struct rio_pwrite {
0036 struct list_head node;
0037
0038 int (*pwcback)(struct rio_mport *mport, void *context,
0039 union rio_pw_msg *msg, int step);
0040 void *context;
0041 };
0042
0043 MODULE_DESCRIPTION("RapidIO Subsystem Core");
0044 MODULE_AUTHOR("Matt Porter <mporter@kernel.crashing.org>");
0045 MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
0046 MODULE_LICENSE("GPL");
0047
0048 static int hdid[RIO_MAX_MPORTS];
0049 static int ids_num;
0050 module_param_array(hdid, int, &ids_num, 0);
0051 MODULE_PARM_DESC(hdid,
0052 "Destination ID assignment to local RapidIO controllers");
0053
0054 static LIST_HEAD(rio_devices);
0055 static LIST_HEAD(rio_nets);
0056 static DEFINE_SPINLOCK(rio_global_list_lock);
0057
0058 static LIST_HEAD(rio_mports);
0059 static LIST_HEAD(rio_scans);
0060 static DEFINE_MUTEX(rio_mport_list_lock);
0061 static unsigned char next_portid;
0062 static DEFINE_SPINLOCK(rio_mmap_lock);
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072 u16 rio_local_get_device_id(struct rio_mport *port)
0073 {
0074 u32 result;
0075
0076 rio_local_read_config_32(port, RIO_DID_CSR, &result);
0077
0078 return (RIO_GET_DID(port->sys_size, result));
0079 }
0080 EXPORT_SYMBOL_GPL(rio_local_get_device_id);
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090 int rio_query_mport(struct rio_mport *port,
0091 struct rio_mport_attr *mport_attr)
0092 {
0093 if (!port->ops->query_mport)
0094 return -ENODATA;
0095 return port->ops->query_mport(port, mport_attr);
0096 }
0097 EXPORT_SYMBOL(rio_query_mport);
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108 struct rio_net *rio_alloc_net(struct rio_mport *mport)
0109 {
0110 struct rio_net *net = kzalloc(sizeof(*net), GFP_KERNEL);
0111
0112 if (net) {
0113 INIT_LIST_HEAD(&net->node);
0114 INIT_LIST_HEAD(&net->devices);
0115 INIT_LIST_HEAD(&net->switches);
0116 INIT_LIST_HEAD(&net->mports);
0117 mport->net = net;
0118 }
0119 return net;
0120 }
0121 EXPORT_SYMBOL_GPL(rio_alloc_net);
0122
0123 int rio_add_net(struct rio_net *net)
0124 {
0125 int err;
0126
0127 err = device_register(&net->dev);
0128 if (err)
0129 return err;
0130 spin_lock(&rio_global_list_lock);
0131 list_add_tail(&net->node, &rio_nets);
0132 spin_unlock(&rio_global_list_lock);
0133
0134 return 0;
0135 }
0136 EXPORT_SYMBOL_GPL(rio_add_net);
0137
0138 void rio_free_net(struct rio_net *net)
0139 {
0140 spin_lock(&rio_global_list_lock);
0141 if (!list_empty(&net->node))
0142 list_del(&net->node);
0143 spin_unlock(&rio_global_list_lock);
0144 if (net->release)
0145 net->release(net);
0146 device_unregister(&net->dev);
0147 }
0148 EXPORT_SYMBOL_GPL(rio_free_net);
0149
0150
0151
0152
0153
0154
0155
0156
0157 void rio_local_set_device_id(struct rio_mport *port, u16 did)
0158 {
0159 rio_local_write_config_32(port, RIO_DID_CSR,
0160 RIO_SET_DID(port->sys_size, did));
0161 }
0162 EXPORT_SYMBOL_GPL(rio_local_set_device_id);
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172 int rio_add_device(struct rio_dev *rdev)
0173 {
0174 int err;
0175
0176 atomic_set(&rdev->state, RIO_DEVICE_RUNNING);
0177 err = device_register(&rdev->dev);
0178 if (err)
0179 return err;
0180
0181 spin_lock(&rio_global_list_lock);
0182 list_add_tail(&rdev->global_list, &rio_devices);
0183 if (rdev->net) {
0184 list_add_tail(&rdev->net_list, &rdev->net->devices);
0185 if (rdev->pef & RIO_PEF_SWITCH)
0186 list_add_tail(&rdev->rswitch->node,
0187 &rdev->net->switches);
0188 }
0189 spin_unlock(&rio_global_list_lock);
0190
0191 return 0;
0192 }
0193 EXPORT_SYMBOL_GPL(rio_add_device);
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203 void rio_del_device(struct rio_dev *rdev, enum rio_device_state state)
0204 {
0205 pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev));
0206 atomic_set(&rdev->state, state);
0207 spin_lock(&rio_global_list_lock);
0208 list_del(&rdev->global_list);
0209 if (rdev->net) {
0210 list_del(&rdev->net_list);
0211 if (rdev->pef & RIO_PEF_SWITCH) {
0212 list_del(&rdev->rswitch->node);
0213 kfree(rdev->rswitch->route_table);
0214 }
0215 }
0216 spin_unlock(&rio_global_list_lock);
0217 device_unregister(&rdev->dev);
0218 }
0219 EXPORT_SYMBOL_GPL(rio_del_device);
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232 int rio_request_inb_mbox(struct rio_mport *mport,
0233 void *dev_id,
0234 int mbox,
0235 int entries,
0236 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
0237 int slot))
0238 {
0239 int rc = -ENOSYS;
0240 struct resource *res;
0241
0242 if (!mport->ops->open_inb_mbox)
0243 goto out;
0244
0245 res = kzalloc(sizeof(*res), GFP_KERNEL);
0246 if (res) {
0247 rio_init_mbox_res(res, mbox, mbox);
0248
0249
0250 rc = request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
0251 res);
0252 if (rc < 0) {
0253 kfree(res);
0254 goto out;
0255 }
0256
0257 mport->inb_msg[mbox].res = res;
0258
0259
0260 mport->inb_msg[mbox].mcback = minb;
0261
0262 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
0263 if (rc) {
0264 mport->inb_msg[mbox].mcback = NULL;
0265 mport->inb_msg[mbox].res = NULL;
0266 release_resource(res);
0267 kfree(res);
0268 }
0269 } else
0270 rc = -ENOMEM;
0271
0272 out:
0273 return rc;
0274 }
0275 EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285 int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
0286 {
0287 int rc;
0288
0289 if (!mport->ops->close_inb_mbox || !mport->inb_msg[mbox].res)
0290 return -EINVAL;
0291
0292 mport->ops->close_inb_mbox(mport, mbox);
0293 mport->inb_msg[mbox].mcback = NULL;
0294
0295 rc = release_resource(mport->inb_msg[mbox].res);
0296 if (rc)
0297 return rc;
0298
0299 kfree(mport->inb_msg[mbox].res);
0300 mport->inb_msg[mbox].res = NULL;
0301
0302 return 0;
0303 }
0304 EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317 int rio_request_outb_mbox(struct rio_mport *mport,
0318 void *dev_id,
0319 int mbox,
0320 int entries,
0321 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
0322 {
0323 int rc = -ENOSYS;
0324 struct resource *res;
0325
0326 if (!mport->ops->open_outb_mbox)
0327 goto out;
0328
0329 res = kzalloc(sizeof(*res), GFP_KERNEL);
0330 if (res) {
0331 rio_init_mbox_res(res, mbox, mbox);
0332
0333
0334 rc = request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
0335 res);
0336 if (rc < 0) {
0337 kfree(res);
0338 goto out;
0339 }
0340
0341 mport->outb_msg[mbox].res = res;
0342
0343
0344 mport->outb_msg[mbox].mcback = moutb;
0345
0346 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
0347 if (rc) {
0348 mport->outb_msg[mbox].mcback = NULL;
0349 mport->outb_msg[mbox].res = NULL;
0350 release_resource(res);
0351 kfree(res);
0352 }
0353 } else
0354 rc = -ENOMEM;
0355
0356 out:
0357 return rc;
0358 }
0359 EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369 int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
0370 {
0371 int rc;
0372
0373 if (!mport->ops->close_outb_mbox || !mport->outb_msg[mbox].res)
0374 return -EINVAL;
0375
0376 mport->ops->close_outb_mbox(mport, mbox);
0377 mport->outb_msg[mbox].mcback = NULL;
0378
0379 rc = release_resource(mport->outb_msg[mbox].res);
0380 if (rc)
0381 return rc;
0382
0383 kfree(mport->outb_msg[mbox].res);
0384 mport->outb_msg[mbox].res = NULL;
0385
0386 return 0;
0387 }
0388 EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401 static int
0402 rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
0403 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
0404 u16 info))
0405 {
0406 struct rio_dbell *dbell = kmalloc(sizeof(*dbell), GFP_KERNEL);
0407
0408 if (!dbell)
0409 return -ENOMEM;
0410
0411 dbell->res = res;
0412 dbell->dinb = dinb;
0413 dbell->dev_id = dev_id;
0414
0415 mutex_lock(&mport->lock);
0416 list_add_tail(&dbell->node, &mport->dbells);
0417 mutex_unlock(&mport->lock);
0418 return 0;
0419 }
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433 int rio_request_inb_dbell(struct rio_mport *mport,
0434 void *dev_id,
0435 u16 start,
0436 u16 end,
0437 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
0438 u16 dst, u16 info))
0439 {
0440 int rc;
0441 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
0442
0443 if (res) {
0444 rio_init_dbell_res(res, start, end);
0445
0446
0447 rc = request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
0448 res);
0449 if (rc < 0) {
0450 kfree(res);
0451 goto out;
0452 }
0453
0454
0455 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
0456 } else
0457 rc = -ENOMEM;
0458
0459 out:
0460 return rc;
0461 }
0462 EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474 int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
0475 {
0476 int rc = 0, found = 0;
0477 struct rio_dbell *dbell;
0478
0479 mutex_lock(&mport->lock);
0480 list_for_each_entry(dbell, &mport->dbells, node) {
0481 if ((dbell->res->start == start) && (dbell->res->end == end)) {
0482 list_del(&dbell->node);
0483 found = 1;
0484 break;
0485 }
0486 }
0487 mutex_unlock(&mport->lock);
0488
0489
0490 if (!found) {
0491 rc = -EINVAL;
0492 goto out;
0493 }
0494
0495
0496 rc = release_resource(dbell->res);
0497
0498
0499 kfree(dbell);
0500
0501 out:
0502 return rc;
0503 }
0504 EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515 struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
0516 u16 end)
0517 {
0518 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
0519
0520 if (res) {
0521 rio_init_dbell_res(res, start, end);
0522
0523
0524 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
0525 < 0) {
0526 kfree(res);
0527 res = NULL;
0528 }
0529 }
0530
0531 return res;
0532 }
0533 EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543 int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
0544 {
0545 int rc = release_resource(res);
0546
0547 kfree(res);
0548
0549 return rc;
0550 }
0551 EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562 int rio_add_mport_pw_handler(struct rio_mport *mport, void *context,
0563 int (*pwcback)(struct rio_mport *mport,
0564 void *context, union rio_pw_msg *msg, int step))
0565 {
0566 struct rio_pwrite *pwrite = kzalloc(sizeof(*pwrite), GFP_KERNEL);
0567
0568 if (!pwrite)
0569 return -ENOMEM;
0570
0571 pwrite->pwcback = pwcback;
0572 pwrite->context = context;
0573 mutex_lock(&mport->lock);
0574 list_add_tail(&pwrite->node, &mport->pwrites);
0575 mutex_unlock(&mport->lock);
0576 return 0;
0577 }
0578 EXPORT_SYMBOL_GPL(rio_add_mport_pw_handler);
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589 int rio_del_mport_pw_handler(struct rio_mport *mport, void *context,
0590 int (*pwcback)(struct rio_mport *mport,
0591 void *context, union rio_pw_msg *msg, int step))
0592 {
0593 int rc = -EINVAL;
0594 struct rio_pwrite *pwrite;
0595
0596 mutex_lock(&mport->lock);
0597 list_for_each_entry(pwrite, &mport->pwrites, node) {
0598 if (pwrite->pwcback == pwcback && pwrite->context == context) {
0599 list_del(&pwrite->node);
0600 kfree(pwrite);
0601 rc = 0;
0602 break;
0603 }
0604 }
0605 mutex_unlock(&mport->lock);
0606
0607 return rc;
0608 }
0609 EXPORT_SYMBOL_GPL(rio_del_mport_pw_handler);
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620 int rio_request_inb_pwrite(struct rio_dev *rdev,
0621 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
0622 {
0623 int rc = 0;
0624
0625 spin_lock(&rio_global_list_lock);
0626 if (rdev->pwcback)
0627 rc = -ENOMEM;
0628 else
0629 rdev->pwcback = pwcback;
0630
0631 spin_unlock(&rio_global_list_lock);
0632 return rc;
0633 }
0634 EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644 int rio_release_inb_pwrite(struct rio_dev *rdev)
0645 {
0646 int rc = -ENOMEM;
0647
0648 spin_lock(&rio_global_list_lock);
0649 if (rdev->pwcback) {
0650 rdev->pwcback = NULL;
0651 rc = 0;
0652 }
0653
0654 spin_unlock(&rio_global_list_lock);
0655 return rc;
0656 }
0657 EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
0658
0659
0660
0661
0662
0663
0664 void rio_pw_enable(struct rio_mport *mport, int enable)
0665 {
0666 if (mport->ops->pwenable) {
0667 mutex_lock(&mport->lock);
0668
0669 if ((enable && ++mport->pwe_refcnt == 1) ||
0670 (!enable && mport->pwe_refcnt && --mport->pwe_refcnt == 0))
0671 mport->ops->pwenable(mport, enable);
0672 mutex_unlock(&mport->lock);
0673 }
0674 }
0675 EXPORT_SYMBOL_GPL(rio_pw_enable);
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689 int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
0690 u64 rbase, u32 size, u32 rflags)
0691 {
0692 int rc;
0693 unsigned long flags;
0694
0695 if (!mport->ops->map_inb)
0696 return -1;
0697 spin_lock_irqsave(&rio_mmap_lock, flags);
0698 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
0699 spin_unlock_irqrestore(&rio_mmap_lock, flags);
0700 return rc;
0701 }
0702 EXPORT_SYMBOL_GPL(rio_map_inb_region);
0703
0704
0705
0706
0707
0708
0709 void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
0710 {
0711 unsigned long flags;
0712 if (!mport->ops->unmap_inb)
0713 return;
0714 spin_lock_irqsave(&rio_mmap_lock, flags);
0715 mport->ops->unmap_inb(mport, lstart);
0716 spin_unlock_irqrestore(&rio_mmap_lock, flags);
0717 }
0718 EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733 int rio_map_outb_region(struct rio_mport *mport, u16 destid, u64 rbase,
0734 u32 size, u32 rflags, dma_addr_t *local)
0735 {
0736 int rc;
0737 unsigned long flags;
0738
0739 if (!mport->ops->map_outb)
0740 return -ENODEV;
0741
0742 spin_lock_irqsave(&rio_mmap_lock, flags);
0743 rc = mport->ops->map_outb(mport, destid, rbase, size,
0744 rflags, local);
0745 spin_unlock_irqrestore(&rio_mmap_lock, flags);
0746
0747 return rc;
0748 }
0749 EXPORT_SYMBOL_GPL(rio_map_outb_region);
0750
0751
0752
0753
0754
0755
0756
0757 void rio_unmap_outb_region(struct rio_mport *mport, u16 destid, u64 rstart)
0758 {
0759 unsigned long flags;
0760
0761 if (!mport->ops->unmap_outb)
0762 return;
0763
0764 spin_lock_irqsave(&rio_mmap_lock, flags);
0765 mport->ops->unmap_outb(mport, destid, rstart);
0766 spin_unlock_irqrestore(&rio_mmap_lock, flags);
0767 }
0768 EXPORT_SYMBOL_GPL(rio_unmap_outb_region);
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779 u32
0780 rio_mport_get_physefb(struct rio_mport *port, int local,
0781 u16 destid, u8 hopcount, u32 *rmap)
0782 {
0783 u32 ext_ftr_ptr;
0784 u32 ftr_header;
0785
0786 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
0787
0788 while (ext_ftr_ptr) {
0789 if (local)
0790 rio_local_read_config_32(port, ext_ftr_ptr,
0791 &ftr_header);
0792 else
0793 rio_mport_read_config_32(port, destid, hopcount,
0794 ext_ftr_ptr, &ftr_header);
0795
0796 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
0797 switch (ftr_header) {
0798
0799 case RIO_EFB_SER_EP_ID:
0800 case RIO_EFB_SER_EP_REC_ID:
0801 case RIO_EFB_SER_EP_FREE_ID:
0802 case RIO_EFB_SER_EP_M1_ID:
0803 case RIO_EFB_SER_EP_SW_M1_ID:
0804 case RIO_EFB_SER_EPF_M1_ID:
0805 case RIO_EFB_SER_EPF_SW_M1_ID:
0806 *rmap = 1;
0807 return ext_ftr_ptr;
0808
0809 case RIO_EFB_SER_EP_M2_ID:
0810 case RIO_EFB_SER_EP_SW_M2_ID:
0811 case RIO_EFB_SER_EPF_M2_ID:
0812 case RIO_EFB_SER_EPF_SW_M2_ID:
0813 *rmap = 2;
0814 return ext_ftr_ptr;
0815
0816 default:
0817 break;
0818 }
0819
0820 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
0821 hopcount, ext_ftr_ptr);
0822 }
0823
0824 return ext_ftr_ptr;
0825 }
0826 EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
0827
0828
0829
0830
0831
0832
0833
0834
0835
0836
0837
0838
0839
0840 struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
0841 {
0842 struct list_head *n;
0843 struct rio_dev *rdev;
0844
0845 spin_lock(&rio_global_list_lock);
0846 n = from ? from->global_list.next : rio_devices.next;
0847
0848 while (n && (n != &rio_devices)) {
0849 rdev = rio_dev_g(n);
0850 if (rdev->comp_tag == comp_tag)
0851 goto exit;
0852 n = n->next;
0853 }
0854 rdev = NULL;
0855 exit:
0856 spin_unlock(&rio_global_list_lock);
0857 return rdev;
0858 }
0859 EXPORT_SYMBOL_GPL(rio_get_comptag);
0860
0861
0862
0863
0864
0865
0866
0867 int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
0868 {
0869 u32 regval;
0870
0871 rio_read_config_32(rdev,
0872 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
0873 ®val);
0874 if (lock)
0875 regval |= RIO_PORT_N_CTL_LOCKOUT;
0876 else
0877 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
0878
0879 rio_write_config_32(rdev,
0880 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
0881 regval);
0882 return 0;
0883 }
0884 EXPORT_SYMBOL_GPL(rio_set_port_lockout);
0885
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897
0898 int rio_enable_rx_tx_port(struct rio_mport *port,
0899 int local, u16 destid,
0900 u8 hopcount, u8 port_num)
0901 {
0902 #ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
0903 u32 regval;
0904 u32 ext_ftr_ptr;
0905 u32 rmap;
0906
0907
0908
0909
0910 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
0911 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
0912
0913 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid,
0914 hopcount, &rmap);
0915
0916 if (local) {
0917 rio_local_read_config_32(port,
0918 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap),
0919 ®val);
0920 } else {
0921 if (rio_mport_read_config_32(port, destid, hopcount,
0922 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
0923 ®val) < 0)
0924 return -EIO;
0925 }
0926
0927 regval = regval | RIO_PORT_N_CTL_EN_RX | RIO_PORT_N_CTL_EN_TX;
0928
0929 if (local) {
0930 rio_local_write_config_32(port,
0931 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap), regval);
0932 } else {
0933 if (rio_mport_write_config_32(port, destid, hopcount,
0934 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
0935 regval) < 0)
0936 return -EIO;
0937 }
0938 #endif
0939 return 0;
0940 }
0941 EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
0942
0943
0944
0945
0946
0947
0948
0949
0950
0951
0952
0953 static int
0954 rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
0955 {
0956 u32 result;
0957 int p_port, rc = -EIO;
0958 struct rio_dev *prev = NULL;
0959
0960
0961 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
0962 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
0963 prev = rdev->prev;
0964 break;
0965 }
0966 rdev = rdev->prev;
0967 }
0968
0969 if (!prev)
0970 goto err_out;
0971
0972 p_port = prev->rswitch->route_table[rdev->destid];
0973
0974 if (p_port != RIO_INVALID_ROUTE) {
0975 pr_debug("RIO: link failed on [%s]-P%d\n",
0976 rio_name(prev), p_port);
0977 *nrdev = prev;
0978 *npnum = p_port;
0979 rc = 0;
0980 } else
0981 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
0982 err_out:
0983 return rc;
0984 }
0985
0986
0987
0988
0989
0990
0991
0992 int
0993 rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
0994 {
0995 int i = 0;
0996 u32 tmp;
0997
0998 while (rio_mport_read_config_32(mport, destid, hopcount,
0999 RIO_DEV_ID_CAR, &tmp)) {
1000 i++;
1001 if (i == RIO_MAX_CHK_RETRY)
1002 return -EIO;
1003 mdelay(1);
1004 }
1005
1006 return 0;
1007 }
1008 EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
1009
1010
1011
1012
1013
1014 static int rio_chk_dev_access(struct rio_dev *rdev)
1015 {
1016 return rio_mport_chk_dev_access(rdev->net->hport,
1017 rdev->destid, rdev->hopcount);
1018 }
1019
1020
1021
1022
1023
1024
1025
1026
1027 static int
1028 rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
1029 {
1030 u32 regval;
1031 int checkcount;
1032
1033 if (lnkresp) {
1034
1035
1036 rio_read_config_32(rdev,
1037 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
1038 ®val);
1039 udelay(50);
1040 }
1041
1042
1043 rio_write_config_32(rdev,
1044 RIO_DEV_PORT_N_MNT_REQ_CSR(rdev, pnum),
1045 RIO_MNT_REQ_CMD_IS);
1046
1047
1048 if (!lnkresp)
1049 return 0;
1050
1051 checkcount = 3;
1052 while (checkcount--) {
1053 udelay(50);
1054 rio_read_config_32(rdev,
1055 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
1056 ®val);
1057 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
1058 *lnkresp = regval;
1059 return 0;
1060 }
1061 }
1062
1063 return -EIO;
1064 }
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079 static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
1080 {
1081 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
1082 u32 regval;
1083 u32 far_ackid, far_linkstat, near_ackid;
1084
1085 if (err_status == 0)
1086 rio_read_config_32(rdev,
1087 RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1088 &err_status);
1089
1090 if (err_status & RIO_PORT_N_ERR_STS_OUT_ES) {
1091 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
1092
1093
1094
1095 if (rio_get_input_status(rdev, pnum, ®val)) {
1096 pr_debug("RIO_EM: Input-status response timeout\n");
1097 goto rd_err;
1098 }
1099
1100 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
1101 pnum, regval);
1102 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
1103 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
1104 rio_read_config_32(rdev,
1105 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
1106 ®val);
1107 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
1108 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
1109 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
1110 " near_ackID=0x%02x\n",
1111 pnum, far_ackid, far_linkstat, near_ackid);
1112
1113
1114
1115
1116
1117 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
1118 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
1119
1120
1121
1122 rio_write_config_32(rdev,
1123 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
1124 (near_ackid << 24) |
1125 (far_ackid << 8) | far_ackid);
1126
1127
1128
1129 far_ackid++;
1130 if (!nextdev) {
1131 pr_debug("RIO_EM: nextdev pointer == NULL\n");
1132 goto rd_err;
1133 }
1134
1135 rio_write_config_32(nextdev,
1136 RIO_DEV_PORT_N_ACK_STS_CSR(nextdev,
1137 RIO_GET_PORT_NUM(nextdev->swpinfo)),
1138 (far_ackid << 24) |
1139 (near_ackid << 8) | near_ackid);
1140 }
1141 rd_err:
1142 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1143 &err_status);
1144 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1145 }
1146
1147 if ((err_status & RIO_PORT_N_ERR_STS_INP_ES) && nextdev) {
1148 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
1149 rio_get_input_status(nextdev,
1150 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
1151 udelay(50);
1152
1153 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1154 &err_status);
1155 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1156 }
1157
1158 return (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
1159 RIO_PORT_N_ERR_STS_INP_ES)) ? 1 : 0;
1160 }
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170 int rio_inb_pwrite_handler(struct rio_mport *mport, union rio_pw_msg *pw_msg)
1171 {
1172 struct rio_dev *rdev;
1173 u32 err_status, em_perrdet, em_ltlerrdet;
1174 int rc, portnum;
1175 struct rio_pwrite *pwrite;
1176
1177 #ifdef DEBUG_PW
1178 {
1179 u32 i;
1180
1181 pr_debug("%s: PW to mport_%d:\n", __func__, mport->id);
1182 for (i = 0; i < RIO_PW_MSG_SIZE / sizeof(u32); i = i + 4) {
1183 pr_debug("0x%02x: %08x %08x %08x %08x\n",
1184 i * 4, pw_msg->raw[i], pw_msg->raw[i + 1],
1185 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
1186 }
1187 }
1188 #endif
1189
1190 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
1191 if (rdev) {
1192 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
1193 } else {
1194 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
1195 __func__, pw_msg->em.comptag);
1196 }
1197
1198
1199
1200
1201
1202
1203
1204 if (rdev && rdev->pwcback) {
1205 rc = rdev->pwcback(rdev, pw_msg, 0);
1206 if (rc == 0)
1207 return 0;
1208 }
1209
1210 mutex_lock(&mport->lock);
1211 list_for_each_entry(pwrite, &mport->pwrites, node)
1212 pwrite->pwcback(mport, pwrite->context, pw_msg, 0);
1213 mutex_unlock(&mport->lock);
1214
1215 if (!rdev)
1216 return 0;
1217
1218
1219
1220
1221
1222
1223 portnum = pw_msg->em.is_port & 0xFF;
1224
1225
1226
1227
1228
1229 if (rio_chk_dev_access(rdev)) {
1230 pr_debug("RIO: device access failed - get link partner\n");
1231
1232
1233
1234
1235 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
1236 pr_err("RIO: Route trace for %s failed\n",
1237 rio_name(rdev));
1238 return -EIO;
1239 }
1240 pw_msg = NULL;
1241 }
1242
1243
1244 if (!(rdev->pef & RIO_PEF_SWITCH))
1245 return 0;
1246
1247 if (rdev->phys_efptr == 0) {
1248 pr_err("RIO_PW: Bad switch initialization for %s\n",
1249 rio_name(rdev));
1250 return 0;
1251 }
1252
1253
1254
1255
1256 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
1257 rdev->rswitch->ops->em_handle(rdev, portnum);
1258
1259 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1260 &err_status);
1261 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
1262
1263 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
1264
1265 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
1266 rdev->rswitch->port_ok |= (1 << portnum);
1267 rio_set_port_lockout(rdev, portnum, 0);
1268
1269 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
1270 rio_name(rdev), portnum);
1271 }
1272
1273
1274
1275
1276
1277 if (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
1278 RIO_PORT_N_ERR_STS_INP_ES)) {
1279 if (rio_clr_err_stopped(rdev, portnum, err_status))
1280 rio_clr_err_stopped(rdev, portnum, 0);
1281 }
1282 } else {
1283
1284 if (rdev->rswitch->port_ok & (1 << portnum)) {
1285 rdev->rswitch->port_ok &= ~(1 << portnum);
1286 rio_set_port_lockout(rdev, portnum, 1);
1287
1288 if (rdev->phys_rmap == 1) {
1289 rio_write_config_32(rdev,
1290 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, portnum),
1291 RIO_PORT_N_ACK_CLEAR);
1292 } else {
1293 rio_write_config_32(rdev,
1294 RIO_DEV_PORT_N_OB_ACK_CSR(rdev, portnum),
1295 RIO_PORT_N_OB_ACK_CLEAR);
1296 rio_write_config_32(rdev,
1297 RIO_DEV_PORT_N_IB_ACK_CSR(rdev, portnum),
1298 0);
1299 }
1300
1301
1302 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
1303 rio_name(rdev), portnum);
1304 }
1305 }
1306
1307 rio_read_config_32(rdev,
1308 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
1309 if (em_perrdet) {
1310 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
1311 portnum, em_perrdet);
1312
1313 rio_write_config_32(rdev,
1314 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
1315 }
1316
1317 rio_read_config_32(rdev,
1318 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1319 if (em_ltlerrdet) {
1320 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1321 em_ltlerrdet);
1322
1323 rio_write_config_32(rdev,
1324 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1325 }
1326
1327
1328 rio_write_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1329 err_status);
1330
1331 return 0;
1332 }
1333 EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344 u32
1345 rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1346 u8 hopcount, u32 from)
1347 {
1348 u32 reg_val;
1349
1350 if (from == 0) {
1351 if (local)
1352 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1353 ®_val);
1354 else
1355 rio_mport_read_config_32(port, destid, hopcount,
1356 RIO_ASM_INFO_CAR, ®_val);
1357 return reg_val & RIO_EXT_FTR_PTR_MASK;
1358 } else {
1359 if (local)
1360 rio_local_read_config_32(port, from, ®_val);
1361 else
1362 rio_mport_read_config_32(port, destid, hopcount,
1363 from, ®_val);
1364 return RIO_GET_BLOCK_ID(reg_val);
1365 }
1366 }
1367 EXPORT_SYMBOL_GPL(rio_mport_get_efb);
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382 u32
1383 rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1384 u8 hopcount, int ftr)
1385 {
1386 u32 asm_info, ext_ftr_ptr, ftr_header;
1387
1388 if (local)
1389 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1390 else
1391 rio_mport_read_config_32(port, destid, hopcount,
1392 RIO_ASM_INFO_CAR, &asm_info);
1393
1394 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1395
1396 while (ext_ftr_ptr) {
1397 if (local)
1398 rio_local_read_config_32(port, ext_ftr_ptr,
1399 &ftr_header);
1400 else
1401 rio_mport_read_config_32(port, destid, hopcount,
1402 ext_ftr_ptr, &ftr_header);
1403 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1404 return ext_ftr_ptr;
1405
1406 ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header);
1407 if (!ext_ftr_ptr)
1408 break;
1409 }
1410
1411 return 0;
1412 }
1413 EXPORT_SYMBOL_GPL(rio_mport_get_feature);
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425 static int
1426 rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1427 u16 table, u16 route_destid, u8 route_port)
1428 {
1429 if (table == RIO_GLOBAL_TABLE) {
1430 rio_mport_write_config_32(mport, destid, hopcount,
1431 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1432 (u32)route_destid);
1433 rio_mport_write_config_32(mport, destid, hopcount,
1434 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1435 (u32)route_port);
1436 }
1437
1438 udelay(10);
1439 return 0;
1440 }
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453 static int
1454 rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1455 u16 table, u16 route_destid, u8 *route_port)
1456 {
1457 u32 result;
1458
1459 if (table == RIO_GLOBAL_TABLE) {
1460 rio_mport_write_config_32(mport, destid, hopcount,
1461 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1462 rio_mport_read_config_32(mport, destid, hopcount,
1463 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1464
1465 *route_port = (u8)result;
1466 }
1467
1468 return 0;
1469 }
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479 static int
1480 rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1481 u16 table)
1482 {
1483 u32 max_destid = 0xff;
1484 u32 i, pef, id_inc = 1, ext_cfg = 0;
1485 u32 port_sel = RIO_INVALID_ROUTE;
1486
1487 if (table == RIO_GLOBAL_TABLE) {
1488 rio_mport_read_config_32(mport, destid, hopcount,
1489 RIO_PEF_CAR, &pef);
1490
1491 if (mport->sys_size) {
1492 rio_mport_read_config_32(mport, destid, hopcount,
1493 RIO_SWITCH_RT_LIMIT,
1494 &max_destid);
1495 max_destid &= RIO_RT_MAX_DESTID;
1496 }
1497
1498 if (pef & RIO_PEF_EXT_RT) {
1499 ext_cfg = 0x80000000;
1500 id_inc = 4;
1501 port_sel = (RIO_INVALID_ROUTE << 24) |
1502 (RIO_INVALID_ROUTE << 16) |
1503 (RIO_INVALID_ROUTE << 8) |
1504 RIO_INVALID_ROUTE;
1505 }
1506
1507 for (i = 0; i <= max_destid;) {
1508 rio_mport_write_config_32(mport, destid, hopcount,
1509 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1510 ext_cfg | i);
1511 rio_mport_write_config_32(mport, destid, hopcount,
1512 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1513 port_sel);
1514 i += id_inc;
1515 }
1516 }
1517
1518 udelay(10);
1519 return 0;
1520 }
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532 int rio_lock_device(struct rio_mport *port, u16 destid,
1533 u8 hopcount, int wait_ms)
1534 {
1535 u32 result;
1536 int tcnt = 0;
1537
1538
1539 rio_mport_write_config_32(port, destid, hopcount,
1540 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1541 rio_mport_read_config_32(port, destid, hopcount,
1542 RIO_HOST_DID_LOCK_CSR, &result);
1543
1544 while (result != port->host_deviceid) {
1545 if (wait_ms != 0 && tcnt == wait_ms) {
1546 pr_debug("RIO: timeout when locking device %x:%x\n",
1547 destid, hopcount);
1548 return -EINVAL;
1549 }
1550
1551
1552 mdelay(1);
1553 tcnt++;
1554
1555 rio_mport_write_config_32(port, destid,
1556 hopcount,
1557 RIO_HOST_DID_LOCK_CSR,
1558 port->host_deviceid);
1559 rio_mport_read_config_32(port, destid,
1560 hopcount,
1561 RIO_HOST_DID_LOCK_CSR, &result);
1562 }
1563
1564 return 0;
1565 }
1566 EXPORT_SYMBOL_GPL(rio_lock_device);
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576 int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1577 {
1578 u32 result;
1579
1580
1581 rio_mport_write_config_32(port, destid,
1582 hopcount,
1583 RIO_HOST_DID_LOCK_CSR,
1584 port->host_deviceid);
1585 rio_mport_read_config_32(port, destid, hopcount,
1586 RIO_HOST_DID_LOCK_CSR, &result);
1587 if ((result & 0xffff) != 0xffff) {
1588 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1589 destid, hopcount);
1590 return -EINVAL;
1591 }
1592
1593 return 0;
1594 }
1595 EXPORT_SYMBOL_GPL(rio_unlock_device);
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614 int rio_route_add_entry(struct rio_dev *rdev,
1615 u16 table, u16 route_destid, u8 route_port, int lock)
1616 {
1617 int rc = -EINVAL;
1618 struct rio_switch_ops *ops = rdev->rswitch->ops;
1619
1620 if (lock) {
1621 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1622 rdev->hopcount, 1000);
1623 if (rc)
1624 return rc;
1625 }
1626
1627 spin_lock(&rdev->rswitch->lock);
1628
1629 if (!ops || !ops->add_entry) {
1630 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1631 rdev->hopcount, table,
1632 route_destid, route_port);
1633 } else if (try_module_get(ops->owner)) {
1634 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1635 rdev->hopcount, table, route_destid,
1636 route_port);
1637 module_put(ops->owner);
1638 }
1639
1640 spin_unlock(&rdev->rswitch->lock);
1641
1642 if (lock)
1643 rio_unlock_device(rdev->net->hport, rdev->destid,
1644 rdev->hopcount);
1645
1646 return rc;
1647 }
1648 EXPORT_SYMBOL_GPL(rio_route_add_entry);
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667 int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1668 u16 route_destid, u8 *route_port, int lock)
1669 {
1670 int rc = -EINVAL;
1671 struct rio_switch_ops *ops = rdev->rswitch->ops;
1672
1673 if (lock) {
1674 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1675 rdev->hopcount, 1000);
1676 if (rc)
1677 return rc;
1678 }
1679
1680 spin_lock(&rdev->rswitch->lock);
1681
1682 if (!ops || !ops->get_entry) {
1683 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1684 rdev->hopcount, table,
1685 route_destid, route_port);
1686 } else if (try_module_get(ops->owner)) {
1687 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1688 rdev->hopcount, table, route_destid,
1689 route_port);
1690 module_put(ops->owner);
1691 }
1692
1693 spin_unlock(&rdev->rswitch->lock);
1694
1695 if (lock)
1696 rio_unlock_device(rdev->net->hport, rdev->destid,
1697 rdev->hopcount);
1698 return rc;
1699 }
1700 EXPORT_SYMBOL_GPL(rio_route_get_entry);
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716 int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1717 {
1718 int rc = -EINVAL;
1719 struct rio_switch_ops *ops = rdev->rswitch->ops;
1720
1721 if (lock) {
1722 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1723 rdev->hopcount, 1000);
1724 if (rc)
1725 return rc;
1726 }
1727
1728 spin_lock(&rdev->rswitch->lock);
1729
1730 if (!ops || !ops->clr_table) {
1731 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1732 rdev->hopcount, table);
1733 } else if (try_module_get(ops->owner)) {
1734 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1735 rdev->hopcount, table);
1736
1737 module_put(ops->owner);
1738 }
1739
1740 spin_unlock(&rdev->rswitch->lock);
1741
1742 if (lock)
1743 rio_unlock_device(rdev->net->hport, rdev->destid,
1744 rdev->hopcount);
1745
1746 return rc;
1747 }
1748 EXPORT_SYMBOL_GPL(rio_route_clr_table);
1749
1750 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
1751
1752 static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1753 {
1754 struct rio_mport *mport = arg;
1755
1756
1757 return mport == container_of(chan->device, struct rio_mport, dma);
1758 }
1759
1760
1761
1762
1763
1764
1765
1766
1767 struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
1768 {
1769 dma_cap_mask_t mask;
1770
1771 dma_cap_zero(mask);
1772 dma_cap_set(DMA_SLAVE, mask);
1773 return dma_request_channel(mask, rio_chan_filter, mport);
1774 }
1775 EXPORT_SYMBOL_GPL(rio_request_mport_dma);
1776
1777
1778
1779
1780
1781
1782
1783
1784 struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1785 {
1786 return rio_request_mport_dma(rdev->net->hport);
1787 }
1788 EXPORT_SYMBOL_GPL(rio_request_dma);
1789
1790
1791
1792
1793
1794 void rio_release_dma(struct dma_chan *dchan)
1795 {
1796 dma_release_channel(dchan);
1797 }
1798 EXPORT_SYMBOL_GPL(rio_release_dma);
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816 struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
1817 u16 destid, struct rio_dma_data *data,
1818 enum dma_transfer_direction direction, unsigned long flags)
1819 {
1820 struct rio_dma_ext rio_ext;
1821
1822 if (!dchan->device->device_prep_slave_sg) {
1823 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1824 return NULL;
1825 }
1826
1827 rio_ext.destid = destid;
1828 rio_ext.rio_addr_u = data->rio_addr_u;
1829 rio_ext.rio_addr = data->rio_addr;
1830 rio_ext.wr_type = data->wr_type;
1831
1832 return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1833 direction, flags, &rio_ext);
1834 }
1835 EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853 struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1854 struct dma_chan *dchan, struct rio_dma_data *data,
1855 enum dma_transfer_direction direction, unsigned long flags)
1856 {
1857 return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags);
1858 }
1859 EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1860
1861 #endif
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871 struct rio_mport *rio_find_mport(int mport_id)
1872 {
1873 struct rio_mport *port;
1874
1875 mutex_lock(&rio_mport_list_lock);
1876 list_for_each_entry(port, &rio_mports, node) {
1877 if (port->id == mport_id)
1878 goto found;
1879 }
1880 port = NULL;
1881 found:
1882 mutex_unlock(&rio_mport_list_lock);
1883
1884 return port;
1885 }
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900 int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1901 {
1902 struct rio_mport *port;
1903 struct rio_scan_node *scan;
1904 int rc = 0;
1905
1906 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1907
1908 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1909 !scan_ops)
1910 return -EINVAL;
1911
1912 mutex_lock(&rio_mport_list_lock);
1913
1914
1915
1916
1917
1918
1919 list_for_each_entry(scan, &rio_scans, node) {
1920 if (scan->mport_id == mport_id) {
1921 rc = -EBUSY;
1922 goto err_out;
1923 }
1924 }
1925
1926
1927
1928
1929 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
1930 if (!scan) {
1931 rc = -ENOMEM;
1932 goto err_out;
1933 }
1934
1935 scan->mport_id = mport_id;
1936 scan->ops = scan_ops;
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947 list_for_each_entry(port, &rio_mports, node) {
1948 if (port->id == mport_id) {
1949 port->nscan = scan_ops;
1950 break;
1951 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
1952 port->nscan = scan_ops;
1953 }
1954
1955 list_add_tail(&scan->node, &rio_scans);
1956
1957 err_out:
1958 mutex_unlock(&rio_mport_list_lock);
1959
1960 return rc;
1961 }
1962 EXPORT_SYMBOL_GPL(rio_register_scan);
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975 int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
1976 {
1977 struct rio_mport *port;
1978 struct rio_scan_node *scan;
1979
1980 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1981
1982 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
1983 return -EINVAL;
1984
1985 mutex_lock(&rio_mport_list_lock);
1986
1987 list_for_each_entry(port, &rio_mports, node)
1988 if (port->id == mport_id ||
1989 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
1990 port->nscan = NULL;
1991
1992 list_for_each_entry(scan, &rio_scans, node) {
1993 if (scan->mport_id == mport_id) {
1994 list_del(&scan->node);
1995 kfree(scan);
1996 break;
1997 }
1998 }
1999
2000 mutex_unlock(&rio_mport_list_lock);
2001
2002 return 0;
2003 }
2004 EXPORT_SYMBOL_GPL(rio_unregister_scan);
2005
2006
2007
2008
2009
2010 int rio_mport_scan(int mport_id)
2011 {
2012 struct rio_mport *port = NULL;
2013 int rc;
2014
2015 mutex_lock(&rio_mport_list_lock);
2016 list_for_each_entry(port, &rio_mports, node) {
2017 if (port->id == mport_id)
2018 goto found;
2019 }
2020 mutex_unlock(&rio_mport_list_lock);
2021 return -ENODEV;
2022 found:
2023 if (!port->nscan) {
2024 mutex_unlock(&rio_mport_list_lock);
2025 return -EINVAL;
2026 }
2027
2028 if (!try_module_get(port->nscan->owner)) {
2029 mutex_unlock(&rio_mport_list_lock);
2030 return -ENODEV;
2031 }
2032
2033 mutex_unlock(&rio_mport_list_lock);
2034
2035 if (port->host_deviceid >= 0)
2036 rc = port->nscan->enumerate(port, 0);
2037 else
2038 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
2039
2040 module_put(port->nscan->owner);
2041 return rc;
2042 }
2043
2044 static struct workqueue_struct *rio_wq;
2045
2046 struct rio_disc_work {
2047 struct work_struct work;
2048 struct rio_mport *mport;
2049 };
2050
2051 static void disc_work_handler(struct work_struct *_work)
2052 {
2053 struct rio_disc_work *work;
2054
2055 work = container_of(_work, struct rio_disc_work, work);
2056 pr_debug("RIO: discovery work for mport %d %s\n",
2057 work->mport->id, work->mport->name);
2058 if (try_module_get(work->mport->nscan->owner)) {
2059 work->mport->nscan->discover(work->mport, 0);
2060 module_put(work->mport->nscan->owner);
2061 }
2062 }
2063
2064 int rio_init_mports(void)
2065 {
2066 struct rio_mport *port;
2067 struct rio_disc_work *work;
2068 int n = 0;
2069
2070 if (!next_portid)
2071 return -ENODEV;
2072
2073
2074
2075
2076
2077 mutex_lock(&rio_mport_list_lock);
2078 list_for_each_entry(port, &rio_mports, node) {
2079 if (port->host_deviceid >= 0) {
2080 if (port->nscan && try_module_get(port->nscan->owner)) {
2081 port->nscan->enumerate(port, 0);
2082 module_put(port->nscan->owner);
2083 }
2084 } else
2085 n++;
2086 }
2087 mutex_unlock(&rio_mport_list_lock);
2088
2089 if (!n)
2090 goto no_disc;
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100 rio_wq = alloc_workqueue("riodisc", 0, 0);
2101 if (!rio_wq) {
2102 pr_err("RIO: unable allocate rio_wq\n");
2103 goto no_disc;
2104 }
2105
2106 work = kcalloc(n, sizeof *work, GFP_KERNEL);
2107 if (!work) {
2108 destroy_workqueue(rio_wq);
2109 goto no_disc;
2110 }
2111
2112 n = 0;
2113 mutex_lock(&rio_mport_list_lock);
2114 list_for_each_entry(port, &rio_mports, node) {
2115 if (port->host_deviceid < 0 && port->nscan) {
2116 work[n].mport = port;
2117 INIT_WORK(&work[n].work, disc_work_handler);
2118 queue_work(rio_wq, &work[n].work);
2119 n++;
2120 }
2121 }
2122
2123 flush_workqueue(rio_wq);
2124 mutex_unlock(&rio_mport_list_lock);
2125 pr_debug("RIO: destroy discovery workqueue\n");
2126 destroy_workqueue(rio_wq);
2127 kfree(work);
2128
2129 no_disc:
2130 return 0;
2131 }
2132 EXPORT_SYMBOL_GPL(rio_init_mports);
2133
2134 static int rio_get_hdid(int index)
2135 {
2136 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
2137 return -1;
2138
2139 return hdid[index];
2140 }
2141
2142 int rio_mport_initialize(struct rio_mport *mport)
2143 {
2144 if (next_portid >= RIO_MAX_MPORTS) {
2145 pr_err("RIO: reached specified max number of mports\n");
2146 return -ENODEV;
2147 }
2148
2149 atomic_set(&mport->state, RIO_DEVICE_INITIALIZING);
2150 mport->id = next_portid++;
2151 mport->host_deviceid = rio_get_hdid(mport->id);
2152 mport->nscan = NULL;
2153 mutex_init(&mport->lock);
2154 mport->pwe_refcnt = 0;
2155 INIT_LIST_HEAD(&mport->pwrites);
2156
2157 return 0;
2158 }
2159 EXPORT_SYMBOL_GPL(rio_mport_initialize);
2160
2161 int rio_register_mport(struct rio_mport *port)
2162 {
2163 struct rio_scan_node *scan = NULL;
2164 int res = 0;
2165
2166 mutex_lock(&rio_mport_list_lock);
2167
2168
2169
2170
2171
2172 list_for_each_entry(scan, &rio_scans, node) {
2173 if (port->id == scan->mport_id ||
2174 scan->mport_id == RIO_MPORT_ANY) {
2175 port->nscan = scan->ops;
2176 if (port->id == scan->mport_id)
2177 break;
2178 }
2179 }
2180
2181 list_add_tail(&port->node, &rio_mports);
2182 mutex_unlock(&rio_mport_list_lock);
2183
2184 dev_set_name(&port->dev, "rapidio%d", port->id);
2185 port->dev.class = &rio_mport_class;
2186 atomic_set(&port->state, RIO_DEVICE_RUNNING);
2187
2188 res = device_register(&port->dev);
2189 if (res)
2190 dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
2191 port->id, res);
2192 else
2193 dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id);
2194
2195 return res;
2196 }
2197 EXPORT_SYMBOL_GPL(rio_register_mport);
2198
2199 static int rio_mport_cleanup_callback(struct device *dev, void *data)
2200 {
2201 struct rio_dev *rdev = to_rio_dev(dev);
2202
2203 if (dev->bus == &rio_bus_type)
2204 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
2205 return 0;
2206 }
2207
2208 static int rio_net_remove_children(struct rio_net *net)
2209 {
2210
2211
2212
2213
2214 device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback);
2215 return 0;
2216 }
2217
2218 int rio_unregister_mport(struct rio_mport *port)
2219 {
2220 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
2221
2222
2223 if (atomic_cmpxchg(&port->state,
2224 RIO_DEVICE_RUNNING,
2225 RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) {
2226 pr_err("RIO: %s unexpected state transition for mport %s\n",
2227 __func__, port->name);
2228 }
2229
2230 if (port->net && port->net->hport == port) {
2231 rio_net_remove_children(port->net);
2232 rio_free_net(port->net);
2233 }
2234
2235
2236
2237
2238
2239 mutex_lock(&rio_mport_list_lock);
2240 list_del(&port->node);
2241 mutex_unlock(&rio_mport_list_lock);
2242 device_unregister(&port->dev);
2243
2244 return 0;
2245 }
2246 EXPORT_SYMBOL_GPL(rio_unregister_mport);