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