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