1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/types.h>
18#include <linux/kernel.h>
19
20#include <linux/delay.h>
21#include <linux/init.h>
22#include <linux/rio.h>
23#include <linux/rio_drv.h>
24#include <linux/rio_ids.h>
25#include <linux/rio_regs.h>
26#include <linux/module.h>
27#include <linux/spinlock.h>
28#include <linux/slab.h>
29#include <linux/interrupt.h>
30
31#include "rio.h"
32
33MODULE_DESCRIPTION("RapidIO Subsystem Core");
34MODULE_AUTHOR("Matt Porter <mporter@kernel.crashing.org>");
35MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
36MODULE_LICENSE("GPL");
37
38static int hdid[RIO_MAX_MPORTS];
39static int ids_num;
40module_param_array(hdid, int, &ids_num, 0);
41MODULE_PARM_DESC(hdid,
42 "Destination ID assignment to local RapidIO controllers");
43
44static LIST_HEAD(rio_devices);
45static DEFINE_SPINLOCK(rio_global_list_lock);
46
47static LIST_HEAD(rio_mports);
48static LIST_HEAD(rio_scans);
49static DEFINE_MUTEX(rio_mport_list_lock);
50static unsigned char next_portid;
51static DEFINE_SPINLOCK(rio_mmap_lock);
52
53
54
55
56
57
58
59
60
61u16 rio_local_get_device_id(struct rio_mport *port)
62{
63 u32 result;
64
65 rio_local_read_config_32(port, RIO_DID_CSR, &result);
66
67 return (RIO_GET_DID(port->sys_size, result));
68}
69
70
71
72
73
74
75
76
77
78int rio_add_device(struct rio_dev *rdev)
79{
80 int err;
81
82 err = device_add(&rdev->dev);
83 if (err)
84 return err;
85
86 spin_lock(&rio_global_list_lock);
87 list_add_tail(&rdev->global_list, &rio_devices);
88 spin_unlock(&rio_global_list_lock);
89
90 rio_create_sysfs_dev_files(rdev);
91
92 return 0;
93}
94EXPORT_SYMBOL_GPL(rio_add_device);
95
96
97
98
99
100
101
102
103
104
105
106
107int rio_request_inb_mbox(struct rio_mport *mport,
108 void *dev_id,
109 int mbox,
110 int entries,
111 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
112 int slot))
113{
114 int rc = -ENOSYS;
115 struct resource *res;
116
117 if (mport->ops->open_inb_mbox == NULL)
118 goto out;
119
120 res = kmalloc(sizeof(struct resource), GFP_KERNEL);
121
122 if (res) {
123 rio_init_mbox_res(res, mbox, mbox);
124
125
126 if ((rc =
127 request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
128 res)) < 0) {
129 kfree(res);
130 goto out;
131 }
132
133 mport->inb_msg[mbox].res = res;
134
135
136 mport->inb_msg[mbox].mcback = minb;
137
138 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
139 } else
140 rc = -ENOMEM;
141
142 out:
143 return rc;
144}
145
146
147
148
149
150
151
152
153
154int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
155{
156 if (mport->ops->close_inb_mbox) {
157 mport->ops->close_inb_mbox(mport, mbox);
158
159
160 return release_resource(mport->inb_msg[mbox].res);
161 } else
162 return -ENOSYS;
163}
164
165
166
167
168
169
170
171
172
173
174
175
176int rio_request_outb_mbox(struct rio_mport *mport,
177 void *dev_id,
178 int mbox,
179 int entries,
180 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
181{
182 int rc = -ENOSYS;
183 struct resource *res;
184
185 if (mport->ops->open_outb_mbox == NULL)
186 goto out;
187
188 res = kmalloc(sizeof(struct resource), GFP_KERNEL);
189
190 if (res) {
191 rio_init_mbox_res(res, mbox, mbox);
192
193
194 if ((rc =
195 request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
196 res)) < 0) {
197 kfree(res);
198 goto out;
199 }
200
201 mport->outb_msg[mbox].res = res;
202
203
204 mport->outb_msg[mbox].mcback = moutb;
205
206 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
207 } else
208 rc = -ENOMEM;
209
210 out:
211 return rc;
212}
213
214
215
216
217
218
219
220
221
222int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
223{
224 if (mport->ops->close_outb_mbox) {
225 mport->ops->close_outb_mbox(mport, mbox);
226
227
228 return release_resource(mport->outb_msg[mbox].res);
229 } else
230 return -ENOSYS;
231}
232
233
234
235
236
237
238
239
240
241
242
243
244static int
245rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
246 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
247 u16 info))
248{
249 int rc = 0;
250 struct rio_dbell *dbell;
251
252 if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
253 rc = -ENOMEM;
254 goto out;
255 }
256
257 dbell->res = res;
258 dbell->dinb = dinb;
259 dbell->dev_id = dev_id;
260
261 list_add_tail(&dbell->node, &mport->dbells);
262
263 out:
264 return rc;
265}
266
267
268
269
270
271
272
273
274
275
276
277
278
279int rio_request_inb_dbell(struct rio_mport *mport,
280 void *dev_id,
281 u16 start,
282 u16 end,
283 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
284 u16 dst, u16 info))
285{
286 int rc = 0;
287
288 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
289
290 if (res) {
291 rio_init_dbell_res(res, start, end);
292
293
294 if ((rc =
295 request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
296 res)) < 0) {
297 kfree(res);
298 goto out;
299 }
300
301
302 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
303 } else
304 rc = -ENOMEM;
305
306 out:
307 return rc;
308}
309
310
311
312
313
314
315
316
317
318
319
320int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
321{
322 int rc = 0, found = 0;
323 struct rio_dbell *dbell;
324
325 list_for_each_entry(dbell, &mport->dbells, node) {
326 if ((dbell->res->start == start) && (dbell->res->end == end)) {
327 found = 1;
328 break;
329 }
330 }
331
332
333 if (!found) {
334 rc = -EINVAL;
335 goto out;
336 }
337
338
339 list_del(&dbell->node);
340
341
342 rc = release_resource(dbell->res);
343
344
345 kfree(dbell);
346
347 out:
348 return rc;
349}
350
351
352
353
354
355
356
357
358
359
360struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
361 u16 end)
362{
363 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
364
365 if (res) {
366 rio_init_dbell_res(res, start, end);
367
368
369 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
370 < 0) {
371 kfree(res);
372 res = NULL;
373 }
374 }
375
376 return res;
377}
378
379
380
381
382
383
384
385
386
387int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
388{
389 int rc = release_resource(res);
390
391 kfree(res);
392
393 return rc;
394}
395
396
397
398
399
400
401
402
403
404int rio_request_inb_pwrite(struct rio_dev *rdev,
405 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
406{
407 int rc = 0;
408
409 spin_lock(&rio_global_list_lock);
410 if (rdev->pwcback != NULL)
411 rc = -ENOMEM;
412 else
413 rdev->pwcback = pwcback;
414
415 spin_unlock(&rio_global_list_lock);
416 return rc;
417}
418EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
419
420
421
422
423
424
425
426
427int rio_release_inb_pwrite(struct rio_dev *rdev)
428{
429 int rc = -ENOMEM;
430
431 spin_lock(&rio_global_list_lock);
432 if (rdev->pwcback) {
433 rdev->pwcback = NULL;
434 rc = 0;
435 }
436
437 spin_unlock(&rio_global_list_lock);
438 return rc;
439}
440EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
441
442
443
444
445
446
447
448
449
450
451
452
453
454int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
455 u64 rbase, u32 size, u32 rflags)
456{
457 int rc = 0;
458 unsigned long flags;
459
460 if (!mport->ops->map_inb)
461 return -1;
462 spin_lock_irqsave(&rio_mmap_lock, flags);
463 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
464 spin_unlock_irqrestore(&rio_mmap_lock, flags);
465 return rc;
466}
467EXPORT_SYMBOL_GPL(rio_map_inb_region);
468
469
470
471
472
473
474void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
475{
476 unsigned long flags;
477 if (!mport->ops->unmap_inb)
478 return;
479 spin_lock_irqsave(&rio_mmap_lock, flags);
480 mport->ops->unmap_inb(mport, lstart);
481 spin_unlock_irqrestore(&rio_mmap_lock, flags);
482}
483EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
484
485
486
487
488
489
490
491
492
493u32
494rio_mport_get_physefb(struct rio_mport *port, int local,
495 u16 destid, u8 hopcount)
496{
497 u32 ext_ftr_ptr;
498 u32 ftr_header;
499
500 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
501
502 while (ext_ftr_ptr) {
503 if (local)
504 rio_local_read_config_32(port, ext_ftr_ptr,
505 &ftr_header);
506 else
507 rio_mport_read_config_32(port, destid, hopcount,
508 ext_ftr_ptr, &ftr_header);
509
510 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
511 switch (ftr_header) {
512
513 case RIO_EFB_SER_EP_ID_V13P:
514 case RIO_EFB_SER_EP_REC_ID_V13P:
515 case RIO_EFB_SER_EP_FREE_ID_V13P:
516 case RIO_EFB_SER_EP_ID:
517 case RIO_EFB_SER_EP_REC_ID:
518 case RIO_EFB_SER_EP_FREE_ID:
519 case RIO_EFB_SER_EP_FREC_ID:
520
521 return ext_ftr_ptr;
522
523 default:
524 break;
525 }
526
527 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
528 hopcount, ext_ftr_ptr);
529 }
530
531 return ext_ftr_ptr;
532}
533EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
534
535
536
537
538
539
540
541
542
543
544
545
546
547struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
548{
549 struct list_head *n;
550 struct rio_dev *rdev;
551
552 spin_lock(&rio_global_list_lock);
553 n = from ? from->global_list.next : rio_devices.next;
554
555 while (n && (n != &rio_devices)) {
556 rdev = rio_dev_g(n);
557 if (rdev->comp_tag == comp_tag)
558 goto exit;
559 n = n->next;
560 }
561 rdev = NULL;
562exit:
563 spin_unlock(&rio_global_list_lock);
564 return rdev;
565}
566EXPORT_SYMBOL_GPL(rio_get_comptag);
567
568
569
570
571
572
573
574int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
575{
576 u32 regval;
577
578 rio_read_config_32(rdev,
579 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
580 ®val);
581 if (lock)
582 regval |= RIO_PORT_N_CTL_LOCKOUT;
583 else
584 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
585
586 rio_write_config_32(rdev,
587 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
588 regval);
589 return 0;
590}
591EXPORT_SYMBOL_GPL(rio_set_port_lockout);
592
593
594
595
596
597
598
599
600
601
602
603
604
605int rio_enable_rx_tx_port(struct rio_mport *port,
606 int local, u16 destid,
607 u8 hopcount, u8 port_num)
608{
609#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
610 u32 regval;
611 u32 ext_ftr_ptr;
612
613
614
615
616 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
617 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
618
619 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
620
621 if (local) {
622 rio_local_read_config_32(port, ext_ftr_ptr +
623 RIO_PORT_N_CTL_CSR(0),
624 ®val);
625 } else {
626 if (rio_mport_read_config_32(port, destid, hopcount,
627 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), ®val) < 0)
628 return -EIO;
629 }
630
631 if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
632
633 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
634 | RIO_PORT_N_CTL_EN_TX_SER;
635 } else {
636
637 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
638 | RIO_PORT_N_CTL_EN_TX_PAR;
639 }
640
641 if (local) {
642 rio_local_write_config_32(port, ext_ftr_ptr +
643 RIO_PORT_N_CTL_CSR(0), regval);
644 } else {
645 if (rio_mport_write_config_32(port, destid, hopcount,
646 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
647 return -EIO;
648 }
649#endif
650 return 0;
651}
652EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
653
654
655
656
657
658
659
660
661
662
663
664static int
665rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
666{
667 u32 result;
668 int p_port, rc = -EIO;
669 struct rio_dev *prev = NULL;
670
671
672 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
673 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
674 prev = rdev->prev;
675 break;
676 }
677 rdev = rdev->prev;
678 }
679
680 if (prev == NULL)
681 goto err_out;
682
683 p_port = prev->rswitch->route_table[rdev->destid];
684
685 if (p_port != RIO_INVALID_ROUTE) {
686 pr_debug("RIO: link failed on [%s]-P%d\n",
687 rio_name(prev), p_port);
688 *nrdev = prev;
689 *npnum = p_port;
690 rc = 0;
691 } else
692 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
693err_out:
694 return rc;
695}
696
697
698
699
700
701
702
703int
704rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
705{
706 int i = 0;
707 u32 tmp;
708
709 while (rio_mport_read_config_32(mport, destid, hopcount,
710 RIO_DEV_ID_CAR, &tmp)) {
711 i++;
712 if (i == RIO_MAX_CHK_RETRY)
713 return -EIO;
714 mdelay(1);
715 }
716
717 return 0;
718}
719EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
720
721
722
723
724
725static int rio_chk_dev_access(struct rio_dev *rdev)
726{
727 return rio_mport_chk_dev_access(rdev->net->hport,
728 rdev->destid, rdev->hopcount);
729}
730
731
732
733
734
735
736
737
738static int
739rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
740{
741 u32 regval;
742 int checkcount;
743
744 if (lnkresp) {
745
746
747 rio_read_config_32(rdev,
748 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
749 ®val);
750 udelay(50);
751 }
752
753
754 rio_write_config_32(rdev,
755 rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
756 RIO_MNT_REQ_CMD_IS);
757
758
759 if (lnkresp == NULL)
760 return 0;
761
762 checkcount = 3;
763 while (checkcount--) {
764 udelay(50);
765 rio_read_config_32(rdev,
766 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
767 ®val);
768 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
769 *lnkresp = regval;
770 return 0;
771 }
772 }
773
774 return -EIO;
775}
776
777
778
779
780
781
782
783static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
784{
785 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
786 u32 regval;
787 u32 far_ackid, far_linkstat, near_ackid;
788
789 if (err_status == 0)
790 rio_read_config_32(rdev,
791 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
792 &err_status);
793
794 if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
795 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
796
797
798
799 if (rio_get_input_status(rdev, pnum, ®val)) {
800 pr_debug("RIO_EM: Input-status response timeout\n");
801 goto rd_err;
802 }
803
804 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
805 pnum, regval);
806 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
807 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
808 rio_read_config_32(rdev,
809 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
810 ®val);
811 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
812 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
813 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
814 " near_ackID=0x%02x\n",
815 pnum, far_ackid, far_linkstat, near_ackid);
816
817
818
819
820
821 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
822 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
823
824
825
826 rio_write_config_32(rdev,
827 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
828 (near_ackid << 24) |
829 (far_ackid << 8) | far_ackid);
830
831
832
833 far_ackid++;
834 if (nextdev)
835 rio_write_config_32(nextdev,
836 nextdev->phys_efptr +
837 RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
838 (far_ackid << 24) |
839 (near_ackid << 8) | near_ackid);
840 else
841 pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
842 }
843rd_err:
844 rio_read_config_32(rdev,
845 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
846 &err_status);
847 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
848 }
849
850 if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
851 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
852 rio_get_input_status(nextdev,
853 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
854 udelay(50);
855
856 rio_read_config_32(rdev,
857 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
858 &err_status);
859 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
860 }
861
862 return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
863 RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
864}
865
866
867
868
869
870
871
872
873int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
874{
875 struct rio_dev *rdev;
876 u32 err_status, em_perrdet, em_ltlerrdet;
877 int rc, portnum;
878
879 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
880 if (rdev == NULL) {
881
882 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
883 __func__, pw_msg->em.comptag);
884 return -EIO;
885 }
886
887 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
888
889#ifdef DEBUG_PW
890 {
891 u32 i;
892 for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
893 pr_debug("0x%02x: %08x %08x %08x %08x\n",
894 i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
895 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
896 i += 4;
897 }
898 }
899#endif
900
901
902
903
904
905
906
907 if (rdev->pwcback != NULL) {
908 rc = rdev->pwcback(rdev, pw_msg, 0);
909 if (rc == 0)
910 return 0;
911 }
912
913 portnum = pw_msg->em.is_port & 0xFF;
914
915
916
917
918
919 if (rio_chk_dev_access(rdev)) {
920 pr_debug("RIO: device access failed - get link partner\n");
921
922
923
924
925 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
926 pr_err("RIO: Route trace for %s failed\n",
927 rio_name(rdev));
928 return -EIO;
929 }
930 pw_msg = NULL;
931 }
932
933
934 if (!(rdev->pef & RIO_PEF_SWITCH))
935 return 0;
936
937 if (rdev->phys_efptr == 0) {
938 pr_err("RIO_PW: Bad switch initialization for %s\n",
939 rio_name(rdev));
940 return 0;
941 }
942
943
944
945
946 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
947 rdev->rswitch->ops->em_handle(rdev, portnum);
948
949 rio_read_config_32(rdev,
950 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
951 &err_status);
952 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
953
954 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
955
956 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
957 rdev->rswitch->port_ok |= (1 << portnum);
958 rio_set_port_lockout(rdev, portnum, 0);
959
960 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
961 rio_name(rdev), portnum);
962 }
963
964
965
966
967
968 if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
969 RIO_PORT_N_ERR_STS_PW_INP_ES)) {
970 if (rio_clr_err_stopped(rdev, portnum, err_status))
971 rio_clr_err_stopped(rdev, portnum, 0);
972 }
973 } else {
974
975 if (rdev->rswitch->port_ok & (1 << portnum)) {
976 rdev->rswitch->port_ok &= ~(1 << portnum);
977 rio_set_port_lockout(rdev, portnum, 1);
978
979 rio_write_config_32(rdev,
980 rdev->phys_efptr +
981 RIO_PORT_N_ACK_STS_CSR(portnum),
982 RIO_PORT_N_ACK_CLEAR);
983
984
985 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
986 rio_name(rdev), portnum);
987 }
988 }
989
990 rio_read_config_32(rdev,
991 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
992 if (em_perrdet) {
993 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
994 portnum, em_perrdet);
995
996 rio_write_config_32(rdev,
997 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
998 }
999
1000 rio_read_config_32(rdev,
1001 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1002 if (em_ltlerrdet) {
1003 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1004 em_ltlerrdet);
1005
1006 rio_write_config_32(rdev,
1007 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1008 }
1009
1010
1011 rio_write_config_32(rdev,
1012 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
1013 err_status);
1014
1015 return 0;
1016}
1017EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028u32
1029rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1030 u8 hopcount, u32 from)
1031{
1032 u32 reg_val;
1033
1034 if (from == 0) {
1035 if (local)
1036 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1037 ®_val);
1038 else
1039 rio_mport_read_config_32(port, destid, hopcount,
1040 RIO_ASM_INFO_CAR, ®_val);
1041 return reg_val & RIO_EXT_FTR_PTR_MASK;
1042 } else {
1043 if (local)
1044 rio_local_read_config_32(port, from, ®_val);
1045 else
1046 rio_mport_read_config_32(port, destid, hopcount,
1047 from, ®_val);
1048 return RIO_GET_BLOCK_ID(reg_val);
1049 }
1050}
1051EXPORT_SYMBOL_GPL(rio_mport_get_efb);
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079u32
1080rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1081 u8 hopcount, int ftr)
1082{
1083 u32 asm_info, ext_ftr_ptr, ftr_header;
1084
1085 if (local)
1086 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1087 else
1088 rio_mport_read_config_32(port, destid, hopcount,
1089 RIO_ASM_INFO_CAR, &asm_info);
1090
1091 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1092
1093 while (ext_ftr_ptr) {
1094 if (local)
1095 rio_local_read_config_32(port, ext_ftr_ptr,
1096 &ftr_header);
1097 else
1098 rio_mport_read_config_32(port, destid, hopcount,
1099 ext_ftr_ptr, &ftr_header);
1100 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1101 return ext_ftr_ptr;
1102 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
1103 break;
1104 }
1105
1106 return 0;
1107}
1108EXPORT_SYMBOL_GPL(rio_mport_get_feature);
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127struct rio_dev *rio_get_asm(u16 vid, u16 did,
1128 u16 asm_vid, u16 asm_did, struct rio_dev *from)
1129{
1130 struct list_head *n;
1131 struct rio_dev *rdev;
1132
1133 WARN_ON(in_interrupt());
1134 spin_lock(&rio_global_list_lock);
1135 n = from ? from->global_list.next : rio_devices.next;
1136
1137 while (n && (n != &rio_devices)) {
1138 rdev = rio_dev_g(n);
1139 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1140 (did == RIO_ANY_ID || rdev->did == did) &&
1141 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1142 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1143 goto exit;
1144 n = n->next;
1145 }
1146 rdev = NULL;
1147 exit:
1148 rio_dev_put(from);
1149 rdev = rio_dev_get(rdev);
1150 spin_unlock(&rio_global_list_lock);
1151 return rdev;
1152}
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1169{
1170 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1171}
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183static int
1184rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1185 u16 table, u16 route_destid, u8 route_port)
1186{
1187 if (table == RIO_GLOBAL_TABLE) {
1188 rio_mport_write_config_32(mport, destid, hopcount,
1189 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1190 (u32)route_destid);
1191 rio_mport_write_config_32(mport, destid, hopcount,
1192 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1193 (u32)route_port);
1194 }
1195
1196 udelay(10);
1197 return 0;
1198}
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211static int
1212rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1213 u16 table, u16 route_destid, u8 *route_port)
1214{
1215 u32 result;
1216
1217 if (table == RIO_GLOBAL_TABLE) {
1218 rio_mport_write_config_32(mport, destid, hopcount,
1219 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1220 rio_mport_read_config_32(mport, destid, hopcount,
1221 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1222
1223 *route_port = (u8)result;
1224 }
1225
1226 return 0;
1227}
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237static int
1238rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1239 u16 table)
1240{
1241 u32 max_destid = 0xff;
1242 u32 i, pef, id_inc = 1, ext_cfg = 0;
1243 u32 port_sel = RIO_INVALID_ROUTE;
1244
1245 if (table == RIO_GLOBAL_TABLE) {
1246 rio_mport_read_config_32(mport, destid, hopcount,
1247 RIO_PEF_CAR, &pef);
1248
1249 if (mport->sys_size) {
1250 rio_mport_read_config_32(mport, destid, hopcount,
1251 RIO_SWITCH_RT_LIMIT,
1252 &max_destid);
1253 max_destid &= RIO_RT_MAX_DESTID;
1254 }
1255
1256 if (pef & RIO_PEF_EXT_RT) {
1257 ext_cfg = 0x80000000;
1258 id_inc = 4;
1259 port_sel = (RIO_INVALID_ROUTE << 24) |
1260 (RIO_INVALID_ROUTE << 16) |
1261 (RIO_INVALID_ROUTE << 8) |
1262 RIO_INVALID_ROUTE;
1263 }
1264
1265 for (i = 0; i <= max_destid;) {
1266 rio_mport_write_config_32(mport, destid, hopcount,
1267 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1268 ext_cfg | i);
1269 rio_mport_write_config_32(mport, destid, hopcount,
1270 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1271 port_sel);
1272 i += id_inc;
1273 }
1274 }
1275
1276 udelay(10);
1277 return 0;
1278}
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290int rio_lock_device(struct rio_mport *port, u16 destid,
1291 u8 hopcount, int wait_ms)
1292{
1293 u32 result;
1294 int tcnt = 0;
1295
1296
1297 rio_mport_write_config_32(port, destid, hopcount,
1298 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1299 rio_mport_read_config_32(port, destid, hopcount,
1300 RIO_HOST_DID_LOCK_CSR, &result);
1301
1302 while (result != port->host_deviceid) {
1303 if (wait_ms != 0 && tcnt == wait_ms) {
1304 pr_debug("RIO: timeout when locking device %x:%x\n",
1305 destid, hopcount);
1306 return -EINVAL;
1307 }
1308
1309
1310 mdelay(1);
1311 tcnt++;
1312
1313 rio_mport_write_config_32(port, destid,
1314 hopcount,
1315 RIO_HOST_DID_LOCK_CSR,
1316 port->host_deviceid);
1317 rio_mport_read_config_32(port, destid,
1318 hopcount,
1319 RIO_HOST_DID_LOCK_CSR, &result);
1320 }
1321
1322 return 0;
1323}
1324EXPORT_SYMBOL_GPL(rio_lock_device);
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1335{
1336 u32 result;
1337
1338
1339 rio_mport_write_config_32(port, destid,
1340 hopcount,
1341 RIO_HOST_DID_LOCK_CSR,
1342 port->host_deviceid);
1343 rio_mport_read_config_32(port, destid, hopcount,
1344 RIO_HOST_DID_LOCK_CSR, &result);
1345 if ((result & 0xffff) != 0xffff) {
1346 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1347 destid, hopcount);
1348 return -EINVAL;
1349 }
1350
1351 return 0;
1352}
1353EXPORT_SYMBOL_GPL(rio_unlock_device);
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372int rio_route_add_entry(struct rio_dev *rdev,
1373 u16 table, u16 route_destid, u8 route_port, int lock)
1374{
1375 int rc = -EINVAL;
1376 struct rio_switch_ops *ops = rdev->rswitch->ops;
1377
1378 if (lock) {
1379 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1380 rdev->hopcount, 1000);
1381 if (rc)
1382 return rc;
1383 }
1384
1385 spin_lock(&rdev->rswitch->lock);
1386
1387 if (ops == NULL || ops->add_entry == NULL) {
1388 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1389 rdev->hopcount, table,
1390 route_destid, route_port);
1391 } else if (try_module_get(ops->owner)) {
1392 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1393 rdev->hopcount, table, route_destid,
1394 route_port);
1395 module_put(ops->owner);
1396 }
1397
1398 spin_unlock(&rdev->rswitch->lock);
1399
1400 if (lock)
1401 rio_unlock_device(rdev->net->hport, rdev->destid,
1402 rdev->hopcount);
1403
1404 return rc;
1405}
1406EXPORT_SYMBOL_GPL(rio_route_add_entry);
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1426 u16 route_destid, u8 *route_port, int lock)
1427{
1428 int rc = -EINVAL;
1429 struct rio_switch_ops *ops = rdev->rswitch->ops;
1430
1431 if (lock) {
1432 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1433 rdev->hopcount, 1000);
1434 if (rc)
1435 return rc;
1436 }
1437
1438 spin_lock(&rdev->rswitch->lock);
1439
1440 if (ops == NULL || ops->get_entry == NULL) {
1441 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1442 rdev->hopcount, table,
1443 route_destid, route_port);
1444 } else if (try_module_get(ops->owner)) {
1445 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1446 rdev->hopcount, table, route_destid,
1447 route_port);
1448 module_put(ops->owner);
1449 }
1450
1451 spin_unlock(&rdev->rswitch->lock);
1452
1453 if (lock)
1454 rio_unlock_device(rdev->net->hport, rdev->destid,
1455 rdev->hopcount);
1456 return rc;
1457}
1458EXPORT_SYMBOL_GPL(rio_route_get_entry);
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1475{
1476 int rc = -EINVAL;
1477 struct rio_switch_ops *ops = rdev->rswitch->ops;
1478
1479 if (lock) {
1480 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1481 rdev->hopcount, 1000);
1482 if (rc)
1483 return rc;
1484 }
1485
1486 spin_lock(&rdev->rswitch->lock);
1487
1488 if (ops == NULL || ops->clr_table == NULL) {
1489 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1490 rdev->hopcount, table);
1491 } else if (try_module_get(ops->owner)) {
1492 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1493 rdev->hopcount, table);
1494
1495 module_put(ops->owner);
1496 }
1497
1498 spin_unlock(&rdev->rswitch->lock);
1499
1500 if (lock)
1501 rio_unlock_device(rdev->net->hport, rdev->destid,
1502 rdev->hopcount);
1503
1504 return rc;
1505}
1506EXPORT_SYMBOL_GPL(rio_route_clr_table);
1507
1508#ifdef CONFIG_RAPIDIO_DMA_ENGINE
1509
1510static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1511{
1512 struct rio_mport *mport = arg;
1513
1514
1515 return mport == container_of(chan->device, struct rio_mport, dma);
1516}
1517
1518
1519
1520
1521
1522
1523
1524
1525struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
1526{
1527 dma_cap_mask_t mask;
1528
1529 dma_cap_zero(mask);
1530 dma_cap_set(DMA_SLAVE, mask);
1531 return dma_request_channel(mask, rio_chan_filter, mport);
1532}
1533EXPORT_SYMBOL_GPL(rio_request_mport_dma);
1534
1535
1536
1537
1538
1539
1540
1541
1542struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1543{
1544 return rio_request_mport_dma(rdev->net->hport);
1545}
1546EXPORT_SYMBOL_GPL(rio_request_dma);
1547
1548
1549
1550
1551
1552void rio_release_dma(struct dma_chan *dchan)
1553{
1554 dma_release_channel(dchan);
1555}
1556EXPORT_SYMBOL_GPL(rio_release_dma);
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
1573 u16 destid, struct rio_dma_data *data,
1574 enum dma_transfer_direction direction, unsigned long flags)
1575{
1576 struct rio_dma_ext rio_ext;
1577
1578 if (dchan->device->device_prep_slave_sg == NULL) {
1579 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1580 return NULL;
1581 }
1582
1583 rio_ext.destid = destid;
1584 rio_ext.rio_addr_u = data->rio_addr_u;
1585 rio_ext.rio_addr = data->rio_addr;
1586 rio_ext.wr_type = data->wr_type;
1587
1588 return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1589 direction, flags, &rio_ext);
1590}
1591EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1608 struct dma_chan *dchan, struct rio_dma_data *data,
1609 enum dma_transfer_direction direction, unsigned long flags)
1610{
1611 return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags);
1612}
1613EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1614
1615#endif
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625struct rio_mport *rio_find_mport(int mport_id)
1626{
1627 struct rio_mport *port;
1628
1629 mutex_lock(&rio_mport_list_lock);
1630 list_for_each_entry(port, &rio_mports, node) {
1631 if (port->id == mport_id)
1632 goto found;
1633 }
1634 port = NULL;
1635found:
1636 mutex_unlock(&rio_mport_list_lock);
1637
1638 return port;
1639}
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1655{
1656 struct rio_mport *port;
1657 struct rio_scan_node *scan;
1658 int rc = 0;
1659
1660 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1661
1662 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1663 !scan_ops)
1664 return -EINVAL;
1665
1666 mutex_lock(&rio_mport_list_lock);
1667
1668
1669
1670
1671
1672
1673 list_for_each_entry(scan, &rio_scans, node) {
1674 if (scan->mport_id == mport_id) {
1675 rc = -EBUSY;
1676 goto err_out;
1677 }
1678 }
1679
1680
1681
1682
1683 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
1684 if (!scan) {
1685 rc = -ENOMEM;
1686 goto err_out;
1687 }
1688
1689 scan->mport_id = mport_id;
1690 scan->ops = scan_ops;
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701 list_for_each_entry(port, &rio_mports, node) {
1702 if (port->id == mport_id) {
1703 port->nscan = scan_ops;
1704 break;
1705 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
1706 port->nscan = scan_ops;
1707 }
1708
1709 list_add_tail(&scan->node, &rio_scans);
1710
1711err_out:
1712 mutex_unlock(&rio_mport_list_lock);
1713
1714 return rc;
1715}
1716EXPORT_SYMBOL_GPL(rio_register_scan);
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
1730{
1731 struct rio_mport *port;
1732 struct rio_scan_node *scan;
1733
1734 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1735
1736 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
1737 return -EINVAL;
1738
1739 mutex_lock(&rio_mport_list_lock);
1740
1741 list_for_each_entry(port, &rio_mports, node)
1742 if (port->id == mport_id ||
1743 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
1744 port->nscan = NULL;
1745
1746 list_for_each_entry(scan, &rio_scans, node) {
1747 if (scan->mport_id == mport_id) {
1748 list_del(&scan->node);
1749 kfree(scan);
1750 break;
1751 }
1752 }
1753
1754 mutex_unlock(&rio_mport_list_lock);
1755
1756 return 0;
1757}
1758EXPORT_SYMBOL_GPL(rio_unregister_scan);
1759
1760
1761
1762
1763
1764int rio_mport_scan(int mport_id)
1765{
1766 struct rio_mport *port = NULL;
1767 int rc;
1768
1769 mutex_lock(&rio_mport_list_lock);
1770 list_for_each_entry(port, &rio_mports, node) {
1771 if (port->id == mport_id)
1772 goto found;
1773 }
1774 mutex_unlock(&rio_mport_list_lock);
1775 return -ENODEV;
1776found:
1777 if (!port->nscan) {
1778 mutex_unlock(&rio_mport_list_lock);
1779 return -EINVAL;
1780 }
1781
1782 if (!try_module_get(port->nscan->owner)) {
1783 mutex_unlock(&rio_mport_list_lock);
1784 return -ENODEV;
1785 }
1786
1787 mutex_unlock(&rio_mport_list_lock);
1788
1789 if (port->host_deviceid >= 0)
1790 rc = port->nscan->enumerate(port, 0);
1791 else
1792 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
1793
1794 module_put(port->nscan->owner);
1795 return rc;
1796}
1797
1798static void rio_fixup_device(struct rio_dev *dev)
1799{
1800}
1801
1802static int rio_init(void)
1803{
1804 struct rio_dev *dev = NULL;
1805
1806 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
1807 rio_fixup_device(dev);
1808 }
1809 return 0;
1810}
1811
1812static struct workqueue_struct *rio_wq;
1813
1814struct rio_disc_work {
1815 struct work_struct work;
1816 struct rio_mport *mport;
1817};
1818
1819static void disc_work_handler(struct work_struct *_work)
1820{
1821 struct rio_disc_work *work;
1822
1823 work = container_of(_work, struct rio_disc_work, work);
1824 pr_debug("RIO: discovery work for mport %d %s\n",
1825 work->mport->id, work->mport->name);
1826 if (try_module_get(work->mport->nscan->owner)) {
1827 work->mport->nscan->discover(work->mport, 0);
1828 module_put(work->mport->nscan->owner);
1829 }
1830}
1831
1832int rio_init_mports(void)
1833{
1834 struct rio_mport *port;
1835 struct rio_disc_work *work;
1836 int n = 0;
1837
1838 if (!next_portid)
1839 return -ENODEV;
1840
1841
1842
1843
1844
1845 mutex_lock(&rio_mport_list_lock);
1846 list_for_each_entry(port, &rio_mports, node) {
1847 if (port->host_deviceid >= 0) {
1848 if (port->nscan && try_module_get(port->nscan->owner)) {
1849 port->nscan->enumerate(port, 0);
1850 module_put(port->nscan->owner);
1851 }
1852 } else
1853 n++;
1854 }
1855 mutex_unlock(&rio_mport_list_lock);
1856
1857 if (!n)
1858 goto no_disc;
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868 rio_wq = alloc_workqueue("riodisc", 0, 0);
1869 if (!rio_wq) {
1870 pr_err("RIO: unable allocate rio_wq\n");
1871 goto no_disc;
1872 }
1873
1874 work = kcalloc(n, sizeof *work, GFP_KERNEL);
1875 if (!work) {
1876 pr_err("RIO: no memory for work struct\n");
1877 destroy_workqueue(rio_wq);
1878 goto no_disc;
1879 }
1880
1881 n = 0;
1882 mutex_lock(&rio_mport_list_lock);
1883 list_for_each_entry(port, &rio_mports, node) {
1884 if (port->host_deviceid < 0 && port->nscan) {
1885 work[n].mport = port;
1886 INIT_WORK(&work[n].work, disc_work_handler);
1887 queue_work(rio_wq, &work[n].work);
1888 n++;
1889 }
1890 }
1891
1892 flush_workqueue(rio_wq);
1893 mutex_unlock(&rio_mport_list_lock);
1894 pr_debug("RIO: destroy discovery workqueue\n");
1895 destroy_workqueue(rio_wq);
1896 kfree(work);
1897
1898no_disc:
1899 rio_init();
1900
1901 return 0;
1902}
1903
1904static int rio_get_hdid(int index)
1905{
1906 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
1907 return -1;
1908
1909 return hdid[index];
1910}
1911
1912int rio_register_mport(struct rio_mport *port)
1913{
1914 struct rio_scan_node *scan = NULL;
1915 int res = 0;
1916
1917 if (next_portid >= RIO_MAX_MPORTS) {
1918 pr_err("RIO: reached specified max number of mports\n");
1919 return 1;
1920 }
1921
1922 port->id = next_portid++;
1923 port->host_deviceid = rio_get_hdid(port->id);
1924 port->nscan = NULL;
1925
1926 dev_set_name(&port->dev, "rapidio%d", port->id);
1927 port->dev.class = &rio_mport_class;
1928
1929 res = device_register(&port->dev);
1930 if (res)
1931 dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
1932 port->id, res);
1933 else
1934 dev_dbg(&port->dev, "RIO: mport%d registered\n", port->id);
1935
1936 mutex_lock(&rio_mport_list_lock);
1937 list_add_tail(&port->node, &rio_mports);
1938
1939
1940
1941
1942
1943 list_for_each_entry(scan, &rio_scans, node) {
1944 if (port->id == scan->mport_id ||
1945 scan->mport_id == RIO_MPORT_ANY) {
1946 port->nscan = scan->ops;
1947 if (port->id == scan->mport_id)
1948 break;
1949 }
1950 }
1951 mutex_unlock(&rio_mport_list_lock);
1952
1953 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
1954 return 0;
1955}
1956EXPORT_SYMBOL_GPL(rio_register_mport);
1957
1958EXPORT_SYMBOL_GPL(rio_local_get_device_id);
1959EXPORT_SYMBOL_GPL(rio_get_device);
1960EXPORT_SYMBOL_GPL(rio_get_asm);
1961EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
1962EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
1963EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
1964EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
1965EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
1966EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
1967EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
1968EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
1969EXPORT_SYMBOL_GPL(rio_init_mports);
1970