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