xref: /openbmc/linux/drivers/rapidio/rio.c (revision 97ef6f74)
1394b701cSMatt Porter /*
2394b701cSMatt Porter  * RapidIO interconnect services
3394b701cSMatt Porter  * (RapidIO Interconnect Specification, http://www.rapidio.org)
4394b701cSMatt Porter  *
5394b701cSMatt Porter  * Copyright 2005 MontaVista Software, Inc.
6394b701cSMatt Porter  * Matt Porter <mporter@kernel.crashing.org>
7394b701cSMatt Porter  *
8e5cabeb3SAlexandre Bounine  * Copyright 2009 Integrated Device Technology, Inc.
9e5cabeb3SAlexandre Bounine  * Alex Bounine <alexandre.bounine@idt.com>
10e5cabeb3SAlexandre Bounine  * - Added Port-Write/Error Management initialization and handling
11e5cabeb3SAlexandre Bounine  *
12394b701cSMatt Porter  * This program is free software; you can redistribute  it and/or modify it
13394b701cSMatt Porter  * under  the terms of  the GNU General  Public License as published by the
14394b701cSMatt Porter  * Free Software Foundation;  either version 2 of the  License, or (at your
15394b701cSMatt Porter  * option) any later version.
16394b701cSMatt Porter  */
17394b701cSMatt Porter 
18394b701cSMatt Porter #include <linux/types.h>
19394b701cSMatt Porter #include <linux/kernel.h>
20394b701cSMatt Porter 
21394b701cSMatt Porter #include <linux/delay.h>
22394b701cSMatt Porter #include <linux/init.h>
23394b701cSMatt Porter #include <linux/rio.h>
24394b701cSMatt Porter #include <linux/rio_drv.h>
25394b701cSMatt Porter #include <linux/rio_ids.h>
26394b701cSMatt Porter #include <linux/rio_regs.h>
27394b701cSMatt Porter #include <linux/module.h>
28394b701cSMatt Porter #include <linux/spinlock.h>
29de25968cSTim Schmielau #include <linux/slab.h>
305febf1cdSKumar Gala #include <linux/interrupt.h>
31394b701cSMatt Porter 
32394b701cSMatt Porter #include "rio.h"
33394b701cSMatt Porter 
34394b701cSMatt Porter static LIST_HEAD(rio_mports);
35394b701cSMatt Porter 
36394b701cSMatt Porter /**
37394b701cSMatt Porter  * rio_local_get_device_id - Get the base/extended device id for a port
38394b701cSMatt Porter  * @port: RIO master port from which to get the deviceid
39394b701cSMatt Porter  *
40394b701cSMatt Porter  * Reads the base/extended device id from the local device
41394b701cSMatt Porter  * implementing the master port. Returns the 8/16-bit device
42394b701cSMatt Porter  * id.
43394b701cSMatt Porter  */
44394b701cSMatt Porter u16 rio_local_get_device_id(struct rio_mport *port)
45394b701cSMatt Porter {
46394b701cSMatt Porter 	u32 result;
47394b701cSMatt Porter 
48394b701cSMatt Porter 	rio_local_read_config_32(port, RIO_DID_CSR, &result);
49394b701cSMatt Porter 
50e0423236SZhang Wei 	return (RIO_GET_DID(port->sys_size, result));
51394b701cSMatt Porter }
52394b701cSMatt Porter 
53394b701cSMatt Porter /**
54394b701cSMatt Porter  * rio_request_inb_mbox - request inbound mailbox service
55394b701cSMatt Porter  * @mport: RIO master port from which to allocate the mailbox resource
566978bbc0SMatt Porter  * @dev_id: Device specific pointer to pass on event
57394b701cSMatt Porter  * @mbox: Mailbox number to claim
58394b701cSMatt Porter  * @entries: Number of entries in inbound mailbox queue
59394b701cSMatt Porter  * @minb: Callback to execute when inbound message is received
60394b701cSMatt Porter  *
61394b701cSMatt Porter  * Requests ownership of an inbound mailbox resource and binds
62394b701cSMatt Porter  * a callback function to the resource. Returns %0 on success.
63394b701cSMatt Porter  */
64394b701cSMatt Porter int rio_request_inb_mbox(struct rio_mport *mport,
656978bbc0SMatt Porter 			 void *dev_id,
66394b701cSMatt Porter 			 int mbox,
67394b701cSMatt Porter 			 int entries,
686978bbc0SMatt Porter 			 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
69394b701cSMatt Porter 				       int slot))
70394b701cSMatt Porter {
71394b701cSMatt Porter 	int rc = 0;
72394b701cSMatt Porter 
73394b701cSMatt Porter 	struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
74394b701cSMatt Porter 
75394b701cSMatt Porter 	if (res) {
76394b701cSMatt Porter 		rio_init_mbox_res(res, mbox, mbox);
77394b701cSMatt Porter 
78394b701cSMatt Porter 		/* Make sure this mailbox isn't in use */
79394b701cSMatt Porter 		if ((rc =
80394b701cSMatt Porter 		     request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
81394b701cSMatt Porter 				      res)) < 0) {
82394b701cSMatt Porter 			kfree(res);
83394b701cSMatt Porter 			goto out;
84394b701cSMatt Porter 		}
85394b701cSMatt Porter 
86394b701cSMatt Porter 		mport->inb_msg[mbox].res = res;
87394b701cSMatt Porter 
88394b701cSMatt Porter 		/* Hook the inbound message callback */
89394b701cSMatt Porter 		mport->inb_msg[mbox].mcback = minb;
90394b701cSMatt Porter 
916978bbc0SMatt Porter 		rc = rio_open_inb_mbox(mport, dev_id, mbox, entries);
92394b701cSMatt Porter 	} else
93394b701cSMatt Porter 		rc = -ENOMEM;
94394b701cSMatt Porter 
95394b701cSMatt Porter       out:
96394b701cSMatt Porter 	return rc;
97394b701cSMatt Porter }
98394b701cSMatt Porter 
99394b701cSMatt Porter /**
100394b701cSMatt Porter  * rio_release_inb_mbox - release inbound mailbox message service
101394b701cSMatt Porter  * @mport: RIO master port from which to release the mailbox resource
102394b701cSMatt Porter  * @mbox: Mailbox number to release
103394b701cSMatt Porter  *
104394b701cSMatt Porter  * Releases ownership of an inbound mailbox resource. Returns 0
105394b701cSMatt Porter  * if the request has been satisfied.
106394b701cSMatt Porter  */
107394b701cSMatt Porter int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
108394b701cSMatt Porter {
109394b701cSMatt Porter 	rio_close_inb_mbox(mport, mbox);
110394b701cSMatt Porter 
111394b701cSMatt Porter 	/* Release the mailbox resource */
112394b701cSMatt Porter 	return release_resource(mport->inb_msg[mbox].res);
113394b701cSMatt Porter }
114394b701cSMatt Porter 
115394b701cSMatt Porter /**
116394b701cSMatt Porter  * rio_request_outb_mbox - request outbound mailbox service
117394b701cSMatt Porter  * @mport: RIO master port from which to allocate the mailbox resource
1186978bbc0SMatt Porter  * @dev_id: Device specific pointer to pass on event
119394b701cSMatt Porter  * @mbox: Mailbox number to claim
120394b701cSMatt Porter  * @entries: Number of entries in outbound mailbox queue
121394b701cSMatt Porter  * @moutb: Callback to execute when outbound message is sent
122394b701cSMatt Porter  *
123394b701cSMatt Porter  * Requests ownership of an outbound mailbox resource and binds
124394b701cSMatt Porter  * a callback function to the resource. Returns 0 on success.
125394b701cSMatt Porter  */
126394b701cSMatt Porter int rio_request_outb_mbox(struct rio_mport *mport,
1276978bbc0SMatt Porter 			  void *dev_id,
128394b701cSMatt Porter 			  int mbox,
129394b701cSMatt Porter 			  int entries,
1306978bbc0SMatt Porter 			  void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
131394b701cSMatt Porter {
132394b701cSMatt Porter 	int rc = 0;
133394b701cSMatt Porter 
134394b701cSMatt Porter 	struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
135394b701cSMatt Porter 
136394b701cSMatt Porter 	if (res) {
137394b701cSMatt Porter 		rio_init_mbox_res(res, mbox, mbox);
138394b701cSMatt Porter 
139394b701cSMatt Porter 		/* Make sure this outbound mailbox isn't in use */
140394b701cSMatt Porter 		if ((rc =
141394b701cSMatt Porter 		     request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
142394b701cSMatt Porter 				      res)) < 0) {
143394b701cSMatt Porter 			kfree(res);
144394b701cSMatt Porter 			goto out;
145394b701cSMatt Porter 		}
146394b701cSMatt Porter 
147394b701cSMatt Porter 		mport->outb_msg[mbox].res = res;
148394b701cSMatt Porter 
149394b701cSMatt Porter 		/* Hook the inbound message callback */
150394b701cSMatt Porter 		mport->outb_msg[mbox].mcback = moutb;
151394b701cSMatt Porter 
1526978bbc0SMatt Porter 		rc = rio_open_outb_mbox(mport, dev_id, mbox, entries);
153394b701cSMatt Porter 	} else
154394b701cSMatt Porter 		rc = -ENOMEM;
155394b701cSMatt Porter 
156394b701cSMatt Porter       out:
157394b701cSMatt Porter 	return rc;
158394b701cSMatt Porter }
159394b701cSMatt Porter 
160394b701cSMatt Porter /**
161394b701cSMatt Porter  * rio_release_outb_mbox - release outbound mailbox message service
162394b701cSMatt Porter  * @mport: RIO master port from which to release the mailbox resource
163394b701cSMatt Porter  * @mbox: Mailbox number to release
164394b701cSMatt Porter  *
165394b701cSMatt Porter  * Releases ownership of an inbound mailbox resource. Returns 0
166394b701cSMatt Porter  * if the request has been satisfied.
167394b701cSMatt Porter  */
168394b701cSMatt Porter int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
169394b701cSMatt Porter {
170394b701cSMatt Porter 	rio_close_outb_mbox(mport, mbox);
171394b701cSMatt Porter 
172394b701cSMatt Porter 	/* Release the mailbox resource */
173394b701cSMatt Porter 	return release_resource(mport->outb_msg[mbox].res);
174394b701cSMatt Porter }
175394b701cSMatt Porter 
176394b701cSMatt Porter /**
177394b701cSMatt Porter  * rio_setup_inb_dbell - bind inbound doorbell callback
178394b701cSMatt Porter  * @mport: RIO master port to bind the doorbell callback
1796978bbc0SMatt Porter  * @dev_id: Device specific pointer to pass on event
180394b701cSMatt Porter  * @res: Doorbell message resource
181394b701cSMatt Porter  * @dinb: Callback to execute when doorbell is received
182394b701cSMatt Porter  *
183394b701cSMatt Porter  * Adds a doorbell resource/callback pair into a port's
184394b701cSMatt Porter  * doorbell event list. Returns 0 if the request has been
185394b701cSMatt Porter  * satisfied.
186394b701cSMatt Porter  */
187394b701cSMatt Porter static int
1886978bbc0SMatt Porter rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
1896978bbc0SMatt Porter 		    void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
190394b701cSMatt Porter 				  u16 info))
191394b701cSMatt Porter {
192394b701cSMatt Porter 	int rc = 0;
193394b701cSMatt Porter 	struct rio_dbell *dbell;
194394b701cSMatt Porter 
195394b701cSMatt Porter 	if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
196394b701cSMatt Porter 		rc = -ENOMEM;
197394b701cSMatt Porter 		goto out;
198394b701cSMatt Porter 	}
199394b701cSMatt Porter 
200394b701cSMatt Porter 	dbell->res = res;
201394b701cSMatt Porter 	dbell->dinb = dinb;
2026978bbc0SMatt Porter 	dbell->dev_id = dev_id;
203394b701cSMatt Porter 
204394b701cSMatt Porter 	list_add_tail(&dbell->node, &mport->dbells);
205394b701cSMatt Porter 
206394b701cSMatt Porter       out:
207394b701cSMatt Porter 	return rc;
208394b701cSMatt Porter }
209394b701cSMatt Porter 
210394b701cSMatt Porter /**
211394b701cSMatt Porter  * rio_request_inb_dbell - request inbound doorbell message service
212394b701cSMatt Porter  * @mport: RIO master port from which to allocate the doorbell resource
2136978bbc0SMatt Porter  * @dev_id: Device specific pointer to pass on event
214394b701cSMatt Porter  * @start: Doorbell info range start
215394b701cSMatt Porter  * @end: Doorbell info range end
216394b701cSMatt Porter  * @dinb: Callback to execute when doorbell is received
217394b701cSMatt Porter  *
218394b701cSMatt Porter  * Requests ownership of an inbound doorbell resource and binds
219394b701cSMatt Porter  * a callback function to the resource. Returns 0 if the request
220394b701cSMatt Porter  * has been satisfied.
221394b701cSMatt Porter  */
222394b701cSMatt Porter int rio_request_inb_dbell(struct rio_mport *mport,
2236978bbc0SMatt Porter 			  void *dev_id,
224394b701cSMatt Porter 			  u16 start,
225394b701cSMatt Porter 			  u16 end,
2266978bbc0SMatt Porter 			  void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
227394b701cSMatt Porter 					u16 dst, u16 info))
228394b701cSMatt Porter {
229394b701cSMatt Porter 	int rc = 0;
230394b701cSMatt Porter 
231394b701cSMatt Porter 	struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
232394b701cSMatt Porter 
233394b701cSMatt Porter 	if (res) {
234394b701cSMatt Porter 		rio_init_dbell_res(res, start, end);
235394b701cSMatt Porter 
236394b701cSMatt Porter 		/* Make sure these doorbells aren't in use */
237394b701cSMatt Porter 		if ((rc =
238394b701cSMatt Porter 		     request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
239394b701cSMatt Porter 				      res)) < 0) {
240394b701cSMatt Porter 			kfree(res);
241394b701cSMatt Porter 			goto out;
242394b701cSMatt Porter 		}
243394b701cSMatt Porter 
244394b701cSMatt Porter 		/* Hook the doorbell callback */
2456978bbc0SMatt Porter 		rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
246394b701cSMatt Porter 	} else
247394b701cSMatt Porter 		rc = -ENOMEM;
248394b701cSMatt Porter 
249394b701cSMatt Porter       out:
250394b701cSMatt Porter 	return rc;
251394b701cSMatt Porter }
252394b701cSMatt Porter 
253394b701cSMatt Porter /**
254394b701cSMatt Porter  * rio_release_inb_dbell - release inbound doorbell message service
255394b701cSMatt Porter  * @mport: RIO master port from which to release the doorbell resource
256394b701cSMatt Porter  * @start: Doorbell info range start
257394b701cSMatt Porter  * @end: Doorbell info range end
258394b701cSMatt Porter  *
259394b701cSMatt Porter  * Releases ownership of an inbound doorbell resource and removes
260394b701cSMatt Porter  * callback from the doorbell event list. Returns 0 if the request
261394b701cSMatt Porter  * has been satisfied.
262394b701cSMatt Porter  */
263394b701cSMatt Porter int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
264394b701cSMatt Porter {
265394b701cSMatt Porter 	int rc = 0, found = 0;
266394b701cSMatt Porter 	struct rio_dbell *dbell;
267394b701cSMatt Porter 
268394b701cSMatt Porter 	list_for_each_entry(dbell, &mport->dbells, node) {
269394b701cSMatt Porter 		if ((dbell->res->start == start) && (dbell->res->end == end)) {
270394b701cSMatt Porter 			found = 1;
271394b701cSMatt Porter 			break;
272394b701cSMatt Porter 		}
273394b701cSMatt Porter 	}
274394b701cSMatt Porter 
275394b701cSMatt Porter 	/* If we can't find an exact match, fail */
276394b701cSMatt Porter 	if (!found) {
277394b701cSMatt Porter 		rc = -EINVAL;
278394b701cSMatt Porter 		goto out;
279394b701cSMatt Porter 	}
280394b701cSMatt Porter 
281394b701cSMatt Porter 	/* Delete from list */
282394b701cSMatt Porter 	list_del(&dbell->node);
283394b701cSMatt Porter 
284394b701cSMatt Porter 	/* Release the doorbell resource */
285394b701cSMatt Porter 	rc = release_resource(dbell->res);
286394b701cSMatt Porter 
287394b701cSMatt Porter 	/* Free the doorbell event */
288394b701cSMatt Porter 	kfree(dbell);
289394b701cSMatt Porter 
290394b701cSMatt Porter       out:
291394b701cSMatt Porter 	return rc;
292394b701cSMatt Porter }
293394b701cSMatt Porter 
294394b701cSMatt Porter /**
295394b701cSMatt Porter  * rio_request_outb_dbell - request outbound doorbell message range
296394b701cSMatt Porter  * @rdev: RIO device from which to allocate the doorbell resource
297394b701cSMatt Porter  * @start: Doorbell message range start
298394b701cSMatt Porter  * @end: Doorbell message range end
299394b701cSMatt Porter  *
300394b701cSMatt Porter  * Requests ownership of a doorbell message range. Returns a resource
301394b701cSMatt Porter  * if the request has been satisfied or %NULL on failure.
302394b701cSMatt Porter  */
303394b701cSMatt Porter struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
304394b701cSMatt Porter 					u16 end)
305394b701cSMatt Porter {
306394b701cSMatt Porter 	struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
307394b701cSMatt Porter 
308394b701cSMatt Porter 	if (res) {
309394b701cSMatt Porter 		rio_init_dbell_res(res, start, end);
310394b701cSMatt Porter 
311394b701cSMatt Porter 		/* Make sure these doorbells aren't in use */
312394b701cSMatt Porter 		if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
313394b701cSMatt Porter 		    < 0) {
314394b701cSMatt Porter 			kfree(res);
315394b701cSMatt Porter 			res = NULL;
316394b701cSMatt Porter 		}
317394b701cSMatt Porter 	}
318394b701cSMatt Porter 
319394b701cSMatt Porter 	return res;
320394b701cSMatt Porter }
321394b701cSMatt Porter 
322394b701cSMatt Porter /**
323394b701cSMatt Porter  * rio_release_outb_dbell - release outbound doorbell message range
324394b701cSMatt Porter  * @rdev: RIO device from which to release the doorbell resource
325394b701cSMatt Porter  * @res: Doorbell resource to be freed
326394b701cSMatt Porter  *
327394b701cSMatt Porter  * Releases ownership of a doorbell message range. Returns 0 if the
328394b701cSMatt Porter  * request has been satisfied.
329394b701cSMatt Porter  */
330394b701cSMatt Porter int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
331394b701cSMatt Porter {
332394b701cSMatt Porter 	int rc = release_resource(res);
333394b701cSMatt Porter 
334394b701cSMatt Porter 	kfree(res);
335394b701cSMatt Porter 
336394b701cSMatt Porter 	return rc;
337394b701cSMatt Porter }
338394b701cSMatt Porter 
339394b701cSMatt Porter /**
340e5cabeb3SAlexandre Bounine  * rio_request_inb_pwrite - request inbound port-write message service
34197ef6f74SRandy Dunlap  * @rdev: RIO device to which register inbound port-write callback routine
342e5cabeb3SAlexandre Bounine  * @pwcback: Callback routine to execute when port-write is received
343e5cabeb3SAlexandre Bounine  *
344e5cabeb3SAlexandre Bounine  * Binds a port-write callback function to the RapidIO device.
345e5cabeb3SAlexandre Bounine  * Returns 0 if the request has been satisfied.
346e5cabeb3SAlexandre Bounine  */
347e5cabeb3SAlexandre Bounine int rio_request_inb_pwrite(struct rio_dev *rdev,
348e5cabeb3SAlexandre Bounine 	int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
349e5cabeb3SAlexandre Bounine {
350e5cabeb3SAlexandre Bounine 	int rc = 0;
351e5cabeb3SAlexandre Bounine 
352e5cabeb3SAlexandre Bounine 	spin_lock(&rio_global_list_lock);
353e5cabeb3SAlexandre Bounine 	if (rdev->pwcback != NULL)
354e5cabeb3SAlexandre Bounine 		rc = -ENOMEM;
355e5cabeb3SAlexandre Bounine 	else
356e5cabeb3SAlexandre Bounine 		rdev->pwcback = pwcback;
357e5cabeb3SAlexandre Bounine 
358e5cabeb3SAlexandre Bounine 	spin_unlock(&rio_global_list_lock);
359e5cabeb3SAlexandre Bounine 	return rc;
360e5cabeb3SAlexandre Bounine }
361e5cabeb3SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
362e5cabeb3SAlexandre Bounine 
363e5cabeb3SAlexandre Bounine /**
364e5cabeb3SAlexandre Bounine  * rio_release_inb_pwrite - release inbound port-write message service
365e5cabeb3SAlexandre Bounine  * @rdev: RIO device which registered for inbound port-write callback
366e5cabeb3SAlexandre Bounine  *
367e5cabeb3SAlexandre Bounine  * Removes callback from the rio_dev structure. Returns 0 if the request
368e5cabeb3SAlexandre Bounine  * has been satisfied.
369e5cabeb3SAlexandre Bounine  */
370e5cabeb3SAlexandre Bounine int rio_release_inb_pwrite(struct rio_dev *rdev)
371e5cabeb3SAlexandre Bounine {
372e5cabeb3SAlexandre Bounine 	int rc = -ENOMEM;
373e5cabeb3SAlexandre Bounine 
374e5cabeb3SAlexandre Bounine 	spin_lock(&rio_global_list_lock);
375e5cabeb3SAlexandre Bounine 	if (rdev->pwcback) {
376e5cabeb3SAlexandre Bounine 		rdev->pwcback = NULL;
377e5cabeb3SAlexandre Bounine 		rc = 0;
378e5cabeb3SAlexandre Bounine 	}
379e5cabeb3SAlexandre Bounine 
380e5cabeb3SAlexandre Bounine 	spin_unlock(&rio_global_list_lock);
381e5cabeb3SAlexandre Bounine 	return rc;
382e5cabeb3SAlexandre Bounine }
383e5cabeb3SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
384e5cabeb3SAlexandre Bounine 
385e5cabeb3SAlexandre Bounine /**
386e5cabeb3SAlexandre Bounine  * rio_mport_get_physefb - Helper function that returns register offset
387e5cabeb3SAlexandre Bounine  *                      for Physical Layer Extended Features Block.
38897ef6f74SRandy Dunlap  * @port: Master port to issue transaction
38997ef6f74SRandy Dunlap  * @local: Indicate a local master port or remote device access
39097ef6f74SRandy Dunlap  * @destid: Destination ID of the device
39197ef6f74SRandy Dunlap  * @hopcount: Number of switch hops to the device
392e5cabeb3SAlexandre Bounine  */
393e5cabeb3SAlexandre Bounine u32
394e5cabeb3SAlexandre Bounine rio_mport_get_physefb(struct rio_mport *port, int local,
395e5cabeb3SAlexandre Bounine 		      u16 destid, u8 hopcount)
396e5cabeb3SAlexandre Bounine {
397e5cabeb3SAlexandre Bounine 	u32 ext_ftr_ptr;
398e5cabeb3SAlexandre Bounine 	u32 ftr_header;
399e5cabeb3SAlexandre Bounine 
400e5cabeb3SAlexandre Bounine 	ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
401e5cabeb3SAlexandre Bounine 
402e5cabeb3SAlexandre Bounine 	while (ext_ftr_ptr)  {
403e5cabeb3SAlexandre Bounine 		if (local)
404e5cabeb3SAlexandre Bounine 			rio_local_read_config_32(port, ext_ftr_ptr,
405e5cabeb3SAlexandre Bounine 						 &ftr_header);
406e5cabeb3SAlexandre Bounine 		else
407e5cabeb3SAlexandre Bounine 			rio_mport_read_config_32(port, destid, hopcount,
408e5cabeb3SAlexandre Bounine 						 ext_ftr_ptr, &ftr_header);
409e5cabeb3SAlexandre Bounine 
410e5cabeb3SAlexandre Bounine 		ftr_header = RIO_GET_BLOCK_ID(ftr_header);
411e5cabeb3SAlexandre Bounine 		switch (ftr_header) {
412e5cabeb3SAlexandre Bounine 
413e5cabeb3SAlexandre Bounine 		case RIO_EFB_SER_EP_ID_V13P:
414e5cabeb3SAlexandre Bounine 		case RIO_EFB_SER_EP_REC_ID_V13P:
415e5cabeb3SAlexandre Bounine 		case RIO_EFB_SER_EP_FREE_ID_V13P:
416e5cabeb3SAlexandre Bounine 		case RIO_EFB_SER_EP_ID:
417e5cabeb3SAlexandre Bounine 		case RIO_EFB_SER_EP_REC_ID:
418e5cabeb3SAlexandre Bounine 		case RIO_EFB_SER_EP_FREE_ID:
419e5cabeb3SAlexandre Bounine 		case RIO_EFB_SER_EP_FREC_ID:
420e5cabeb3SAlexandre Bounine 
421e5cabeb3SAlexandre Bounine 			return ext_ftr_ptr;
422e5cabeb3SAlexandre Bounine 
423e5cabeb3SAlexandre Bounine 		default:
424e5cabeb3SAlexandre Bounine 			break;
425e5cabeb3SAlexandre Bounine 		}
426e5cabeb3SAlexandre Bounine 
427e5cabeb3SAlexandre Bounine 		ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
428e5cabeb3SAlexandre Bounine 						hopcount, ext_ftr_ptr);
429e5cabeb3SAlexandre Bounine 	}
430e5cabeb3SAlexandre Bounine 
431e5cabeb3SAlexandre Bounine 	return ext_ftr_ptr;
432e5cabeb3SAlexandre Bounine }
433e5cabeb3SAlexandre Bounine 
434e5cabeb3SAlexandre Bounine /**
435e5cabeb3SAlexandre Bounine  * rio_get_comptag - Begin or continue searching for a RIO device by component tag
43697ef6f74SRandy Dunlap  * @comp_tag: RIO component tag to match
437e5cabeb3SAlexandre Bounine  * @from: Previous RIO device found in search, or %NULL for new search
438e5cabeb3SAlexandre Bounine  *
439e5cabeb3SAlexandre Bounine  * Iterates through the list of known RIO devices. If a RIO device is
440e5cabeb3SAlexandre Bounine  * found with a matching @comp_tag, a pointer to its device
441e5cabeb3SAlexandre Bounine  * structure is returned. Otherwise, %NULL is returned. A new search
442e5cabeb3SAlexandre Bounine  * is initiated by passing %NULL to the @from argument. Otherwise, if
443e5cabeb3SAlexandre Bounine  * @from is not %NULL, searches continue from next device on the global
444e5cabeb3SAlexandre Bounine  * list.
445e5cabeb3SAlexandre Bounine  */
446e5cabeb3SAlexandre Bounine static struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
447e5cabeb3SAlexandre Bounine {
448e5cabeb3SAlexandre Bounine 	struct list_head *n;
449e5cabeb3SAlexandre Bounine 	struct rio_dev *rdev;
450e5cabeb3SAlexandre Bounine 
451e5cabeb3SAlexandre Bounine 	spin_lock(&rio_global_list_lock);
452e5cabeb3SAlexandre Bounine 	n = from ? from->global_list.next : rio_devices.next;
453e5cabeb3SAlexandre Bounine 
454e5cabeb3SAlexandre Bounine 	while (n && (n != &rio_devices)) {
455e5cabeb3SAlexandre Bounine 		rdev = rio_dev_g(n);
456e5cabeb3SAlexandre Bounine 		if (rdev->comp_tag == comp_tag)
457e5cabeb3SAlexandre Bounine 			goto exit;
458e5cabeb3SAlexandre Bounine 		n = n->next;
459e5cabeb3SAlexandre Bounine 	}
460e5cabeb3SAlexandre Bounine 	rdev = NULL;
461e5cabeb3SAlexandre Bounine exit:
462e5cabeb3SAlexandre Bounine 	spin_unlock(&rio_global_list_lock);
463e5cabeb3SAlexandre Bounine 	return rdev;
464e5cabeb3SAlexandre Bounine }
465e5cabeb3SAlexandre Bounine 
466e5cabeb3SAlexandre Bounine /**
467e5cabeb3SAlexandre Bounine  * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
468e5cabeb3SAlexandre Bounine  * @rdev: Pointer to RIO device control structure
469e5cabeb3SAlexandre Bounine  * @pnum: Switch port number to set LOCKOUT bit
470e5cabeb3SAlexandre Bounine  * @lock: Operation : set (=1) or clear (=0)
471e5cabeb3SAlexandre Bounine  */
472e5cabeb3SAlexandre Bounine int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
473e5cabeb3SAlexandre Bounine {
474e5cabeb3SAlexandre Bounine 	u8 hopcount = 0xff;
475e5cabeb3SAlexandre Bounine 	u16 destid = rdev->destid;
476e5cabeb3SAlexandre Bounine 	u32 regval;
477e5cabeb3SAlexandre Bounine 
478e5cabeb3SAlexandre Bounine 	if (rdev->rswitch) {
479e5cabeb3SAlexandre Bounine 		destid = rdev->rswitch->destid;
480e5cabeb3SAlexandre Bounine 		hopcount = rdev->rswitch->hopcount;
481e5cabeb3SAlexandre Bounine 	}
482e5cabeb3SAlexandre Bounine 
483e5cabeb3SAlexandre Bounine 	rio_mport_read_config_32(rdev->net->hport, destid, hopcount,
484e5cabeb3SAlexandre Bounine 				 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
485e5cabeb3SAlexandre Bounine 				 &regval);
486e5cabeb3SAlexandre Bounine 	if (lock)
487e5cabeb3SAlexandre Bounine 		regval |= RIO_PORT_N_CTL_LOCKOUT;
488e5cabeb3SAlexandre Bounine 	else
489e5cabeb3SAlexandre Bounine 		regval &= ~RIO_PORT_N_CTL_LOCKOUT;
490e5cabeb3SAlexandre Bounine 
491e5cabeb3SAlexandre Bounine 	rio_mport_write_config_32(rdev->net->hport, destid, hopcount,
492e5cabeb3SAlexandre Bounine 				  rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
493e5cabeb3SAlexandre Bounine 				  regval);
494e5cabeb3SAlexandre Bounine 	return 0;
495e5cabeb3SAlexandre Bounine }
496e5cabeb3SAlexandre Bounine 
497e5cabeb3SAlexandre Bounine /**
498e5cabeb3SAlexandre Bounine  * rio_inb_pwrite_handler - process inbound port-write message
499e5cabeb3SAlexandre Bounine  * @pw_msg: pointer to inbound port-write message
500e5cabeb3SAlexandre Bounine  *
501e5cabeb3SAlexandre Bounine  * Processes an inbound port-write message. Returns 0 if the request
502e5cabeb3SAlexandre Bounine  * has been satisfied.
503e5cabeb3SAlexandre Bounine  */
504e5cabeb3SAlexandre Bounine int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
505e5cabeb3SAlexandre Bounine {
506e5cabeb3SAlexandre Bounine 	struct rio_dev *rdev;
507e5cabeb3SAlexandre Bounine 	struct rio_mport *mport;
508e5cabeb3SAlexandre Bounine 	u8 hopcount;
509e5cabeb3SAlexandre Bounine 	u16 destid;
510e5cabeb3SAlexandre Bounine 	u32 err_status;
511e5cabeb3SAlexandre Bounine 	int rc, portnum;
512e5cabeb3SAlexandre Bounine 
513e5cabeb3SAlexandre Bounine 	rdev = rio_get_comptag(pw_msg->em.comptag, NULL);
514e5cabeb3SAlexandre Bounine 	if (rdev == NULL) {
515e5cabeb3SAlexandre Bounine 		/* Someting bad here (probably enumeration error) */
516e5cabeb3SAlexandre Bounine 		pr_err("RIO: %s No matching device for CTag 0x%08x\n",
517e5cabeb3SAlexandre Bounine 			__func__, pw_msg->em.comptag);
518e5cabeb3SAlexandre Bounine 		return -EIO;
519e5cabeb3SAlexandre Bounine 	}
520e5cabeb3SAlexandre Bounine 
521e5cabeb3SAlexandre Bounine 	pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
522e5cabeb3SAlexandre Bounine 
523e5cabeb3SAlexandre Bounine #ifdef DEBUG_PW
524e5cabeb3SAlexandre Bounine 	{
525e5cabeb3SAlexandre Bounine 	u32 i;
526e5cabeb3SAlexandre Bounine 	for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
527e5cabeb3SAlexandre Bounine 			pr_debug("0x%02x: %08x %08x %08x %08x",
528e5cabeb3SAlexandre Bounine 				 i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
529e5cabeb3SAlexandre Bounine 				 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
530e5cabeb3SAlexandre Bounine 			i += 4;
531e5cabeb3SAlexandre Bounine 	}
532e5cabeb3SAlexandre Bounine 	pr_debug("\n");
533e5cabeb3SAlexandre Bounine 	}
534e5cabeb3SAlexandre Bounine #endif
535e5cabeb3SAlexandre Bounine 
536e5cabeb3SAlexandre Bounine 	/* Call an external service function (if such is registered
537e5cabeb3SAlexandre Bounine 	 * for this device). This may be the service for endpoints that send
538e5cabeb3SAlexandre Bounine 	 * device-specific port-write messages. End-point messages expected
539e5cabeb3SAlexandre Bounine 	 * to be handled completely by EP specific device driver.
540e5cabeb3SAlexandre Bounine 	 * For switches rc==0 signals that no standard processing required.
541e5cabeb3SAlexandre Bounine 	 */
542e5cabeb3SAlexandre Bounine 	if (rdev->pwcback != NULL) {
543e5cabeb3SAlexandre Bounine 		rc = rdev->pwcback(rdev, pw_msg, 0);
544e5cabeb3SAlexandre Bounine 		if (rc == 0)
545e5cabeb3SAlexandre Bounine 			return 0;
546e5cabeb3SAlexandre Bounine 	}
547e5cabeb3SAlexandre Bounine 
548e5cabeb3SAlexandre Bounine 	/* For End-point devices processing stops here */
549e5cabeb3SAlexandre Bounine 	if (!(rdev->pef & RIO_PEF_SWITCH))
550e5cabeb3SAlexandre Bounine 		return 0;
551e5cabeb3SAlexandre Bounine 
552e5cabeb3SAlexandre Bounine 	if (rdev->phys_efptr == 0) {
553e5cabeb3SAlexandre Bounine 		pr_err("RIO_PW: Bad switch initialization for %s\n",
554e5cabeb3SAlexandre Bounine 			rio_name(rdev));
555e5cabeb3SAlexandre Bounine 		return 0;
556e5cabeb3SAlexandre Bounine 	}
557e5cabeb3SAlexandre Bounine 
558e5cabeb3SAlexandre Bounine 	mport = rdev->net->hport;
559e5cabeb3SAlexandre Bounine 	destid = rdev->rswitch->destid;
560e5cabeb3SAlexandre Bounine 	hopcount = rdev->rswitch->hopcount;
561e5cabeb3SAlexandre Bounine 
562e5cabeb3SAlexandre Bounine 	/*
563e5cabeb3SAlexandre Bounine 	 * Process the port-write notification from switch
564e5cabeb3SAlexandre Bounine 	 */
565e5cabeb3SAlexandre Bounine 
566e5cabeb3SAlexandre Bounine 	portnum = pw_msg->em.is_port & 0xFF;
567e5cabeb3SAlexandre Bounine 
568e5cabeb3SAlexandre Bounine 	if (rdev->rswitch->em_handle)
569e5cabeb3SAlexandre Bounine 		rdev->rswitch->em_handle(rdev, portnum);
570e5cabeb3SAlexandre Bounine 
571e5cabeb3SAlexandre Bounine 	rio_mport_read_config_32(mport, destid, hopcount,
572e5cabeb3SAlexandre Bounine 			rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
573e5cabeb3SAlexandre Bounine 			&err_status);
574e5cabeb3SAlexandre Bounine 	pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
575e5cabeb3SAlexandre Bounine 
576e5cabeb3SAlexandre Bounine 	if (pw_msg->em.errdetect) {
577e5cabeb3SAlexandre Bounine 		pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
578e5cabeb3SAlexandre Bounine 			 portnum, pw_msg->em.errdetect);
579e5cabeb3SAlexandre Bounine 		/* Clear EM Port N Error Detect CSR */
580e5cabeb3SAlexandre Bounine 		rio_mport_write_config_32(mport, destid, hopcount,
581e5cabeb3SAlexandre Bounine 			rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
582e5cabeb3SAlexandre Bounine 	}
583e5cabeb3SAlexandre Bounine 
584e5cabeb3SAlexandre Bounine 	if (pw_msg->em.ltlerrdet) {
585e5cabeb3SAlexandre Bounine 		pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
586e5cabeb3SAlexandre Bounine 			 pw_msg->em.ltlerrdet);
587e5cabeb3SAlexandre Bounine 		/* Clear EM L/T Layer Error Detect CSR */
588e5cabeb3SAlexandre Bounine 		rio_mport_write_config_32(mport, destid, hopcount,
589e5cabeb3SAlexandre Bounine 			rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
590e5cabeb3SAlexandre Bounine 	}
591e5cabeb3SAlexandre Bounine 
592e5cabeb3SAlexandre Bounine 	/* Clear Port Errors */
593e5cabeb3SAlexandre Bounine 	rio_mport_write_config_32(mport, destid, hopcount,
594e5cabeb3SAlexandre Bounine 			rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
595e5cabeb3SAlexandre Bounine 			err_status & RIO_PORT_N_ERR_STS_CLR_MASK);
596e5cabeb3SAlexandre Bounine 
597e5cabeb3SAlexandre Bounine 	if (rdev->rswitch->port_ok & (1 << portnum)) {
598e5cabeb3SAlexandre Bounine 		if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) {
599e5cabeb3SAlexandre Bounine 			rdev->rswitch->port_ok &= ~(1 << portnum);
600e5cabeb3SAlexandre Bounine 			rio_set_port_lockout(rdev, portnum, 1);
601e5cabeb3SAlexandre Bounine 
602e5cabeb3SAlexandre Bounine 			rio_mport_write_config_32(mport, destid, hopcount,
603e5cabeb3SAlexandre Bounine 				rdev->phys_efptr +
604e5cabeb3SAlexandre Bounine 					RIO_PORT_N_ACK_STS_CSR(portnum),
605e5cabeb3SAlexandre Bounine 				RIO_PORT_N_ACK_CLEAR);
606e5cabeb3SAlexandre Bounine 
607e5cabeb3SAlexandre Bounine 			/* Schedule Extraction Service */
608e5cabeb3SAlexandre Bounine 			pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
609e5cabeb3SAlexandre Bounine 			       rio_name(rdev), portnum);
610e5cabeb3SAlexandre Bounine 		}
611e5cabeb3SAlexandre Bounine 	} else {
612e5cabeb3SAlexandre Bounine 		if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
613e5cabeb3SAlexandre Bounine 			rdev->rswitch->port_ok |= (1 << portnum);
614e5cabeb3SAlexandre Bounine 			rio_set_port_lockout(rdev, portnum, 0);
615e5cabeb3SAlexandre Bounine 
616e5cabeb3SAlexandre Bounine 			/* Schedule Insertion Service */
617e5cabeb3SAlexandre Bounine 			pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
618e5cabeb3SAlexandre Bounine 			       rio_name(rdev), portnum);
619e5cabeb3SAlexandre Bounine 		}
620e5cabeb3SAlexandre Bounine 	}
621e5cabeb3SAlexandre Bounine 
622e5cabeb3SAlexandre Bounine 	/* Clear Port-Write Pending bit */
623e5cabeb3SAlexandre Bounine 	rio_mport_write_config_32(mport, destid, hopcount,
624e5cabeb3SAlexandre Bounine 			rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
625e5cabeb3SAlexandre Bounine 			RIO_PORT_N_ERR_STS_PW_PEND);
626e5cabeb3SAlexandre Bounine 
627e5cabeb3SAlexandre Bounine 	return 0;
628e5cabeb3SAlexandre Bounine }
629e5cabeb3SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
630e5cabeb3SAlexandre Bounine 
631e5cabeb3SAlexandre Bounine /**
632e5cabeb3SAlexandre Bounine  * rio_mport_get_efb - get pointer to next extended features block
633e5cabeb3SAlexandre Bounine  * @port: Master port to issue transaction
634e5cabeb3SAlexandre Bounine  * @local: Indicate a local master port or remote device access
635e5cabeb3SAlexandre Bounine  * @destid: Destination ID of the device
636e5cabeb3SAlexandre Bounine  * @hopcount: Number of switch hops to the device
637e5cabeb3SAlexandre Bounine  * @from: Offset of  current Extended Feature block header (if 0 starts
638e5cabeb3SAlexandre Bounine  * from	ExtFeaturePtr)
639e5cabeb3SAlexandre Bounine  */
640e5cabeb3SAlexandre Bounine u32
641e5cabeb3SAlexandre Bounine rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
642e5cabeb3SAlexandre Bounine 		      u8 hopcount, u32 from)
643e5cabeb3SAlexandre Bounine {
644e5cabeb3SAlexandre Bounine 	u32 reg_val;
645e5cabeb3SAlexandre Bounine 
646e5cabeb3SAlexandre Bounine 	if (from == 0) {
647e5cabeb3SAlexandre Bounine 		if (local)
648e5cabeb3SAlexandre Bounine 			rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
649e5cabeb3SAlexandre Bounine 						 &reg_val);
650e5cabeb3SAlexandre Bounine 		else
651e5cabeb3SAlexandre Bounine 			rio_mport_read_config_32(port, destid, hopcount,
652e5cabeb3SAlexandre Bounine 						 RIO_ASM_INFO_CAR, &reg_val);
653e5cabeb3SAlexandre Bounine 		return reg_val & RIO_EXT_FTR_PTR_MASK;
654e5cabeb3SAlexandre Bounine 	} else {
655e5cabeb3SAlexandre Bounine 		if (local)
656e5cabeb3SAlexandre Bounine 			rio_local_read_config_32(port, from, &reg_val);
657e5cabeb3SAlexandre Bounine 		else
658e5cabeb3SAlexandre Bounine 			rio_mport_read_config_32(port, destid, hopcount,
659e5cabeb3SAlexandre Bounine 						 from, &reg_val);
660e5cabeb3SAlexandre Bounine 		return RIO_GET_BLOCK_ID(reg_val);
661e5cabeb3SAlexandre Bounine 	}
662e5cabeb3SAlexandre Bounine }
663e5cabeb3SAlexandre Bounine 
664e5cabeb3SAlexandre Bounine /**
665394b701cSMatt Porter  * rio_mport_get_feature - query for devices' extended features
666394b701cSMatt Porter  * @port: Master port to issue transaction
667394b701cSMatt Porter  * @local: Indicate a local master port or remote device access
668394b701cSMatt Porter  * @destid: Destination ID of the device
669394b701cSMatt Porter  * @hopcount: Number of switch hops to the device
670394b701cSMatt Porter  * @ftr: Extended feature code
671394b701cSMatt Porter  *
672394b701cSMatt Porter  * Tell if a device supports a given RapidIO capability.
673394b701cSMatt Porter  * Returns the offset of the requested extended feature
674394b701cSMatt Porter  * block within the device's RIO configuration space or
675394b701cSMatt Porter  * 0 in case the device does not support it.  Possible
676394b701cSMatt Porter  * values for @ftr:
677394b701cSMatt Porter  *
678394b701cSMatt Porter  * %RIO_EFB_PAR_EP_ID		LP/LVDS EP Devices
679394b701cSMatt Porter  *
680394b701cSMatt Porter  * %RIO_EFB_PAR_EP_REC_ID	LP/LVDS EP Recovery Devices
681394b701cSMatt Porter  *
682394b701cSMatt Porter  * %RIO_EFB_PAR_EP_FREE_ID	LP/LVDS EP Free Devices
683394b701cSMatt Porter  *
684394b701cSMatt Porter  * %RIO_EFB_SER_EP_ID		LP/Serial EP Devices
685394b701cSMatt Porter  *
686394b701cSMatt Porter  * %RIO_EFB_SER_EP_REC_ID	LP/Serial EP Recovery Devices
687394b701cSMatt Porter  *
688394b701cSMatt Porter  * %RIO_EFB_SER_EP_FREE_ID	LP/Serial EP Free Devices
689394b701cSMatt Porter  */
690394b701cSMatt Porter u32
691394b701cSMatt Porter rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
692394b701cSMatt Porter 		      u8 hopcount, int ftr)
693394b701cSMatt Porter {
694394b701cSMatt Porter 	u32 asm_info, ext_ftr_ptr, ftr_header;
695394b701cSMatt Porter 
696394b701cSMatt Porter 	if (local)
697394b701cSMatt Porter 		rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
698394b701cSMatt Porter 	else
699394b701cSMatt Porter 		rio_mport_read_config_32(port, destid, hopcount,
700394b701cSMatt Porter 					 RIO_ASM_INFO_CAR, &asm_info);
701394b701cSMatt Porter 
702394b701cSMatt Porter 	ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
703394b701cSMatt Porter 
704394b701cSMatt Porter 	while (ext_ftr_ptr) {
705394b701cSMatt Porter 		if (local)
706394b701cSMatt Porter 			rio_local_read_config_32(port, ext_ftr_ptr,
707394b701cSMatt Porter 						 &ftr_header);
708394b701cSMatt Porter 		else
709394b701cSMatt Porter 			rio_mport_read_config_32(port, destid, hopcount,
710394b701cSMatt Porter 						 ext_ftr_ptr, &ftr_header);
711394b701cSMatt Porter 		if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
712394b701cSMatt Porter 			return ext_ftr_ptr;
713394b701cSMatt Porter 		if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
714394b701cSMatt Porter 			break;
715394b701cSMatt Porter 	}
716394b701cSMatt Porter 
717394b701cSMatt Porter 	return 0;
718394b701cSMatt Porter }
719394b701cSMatt Porter 
720394b701cSMatt Porter /**
721394b701cSMatt Porter  * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
722394b701cSMatt Porter  * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
723394b701cSMatt Porter  * @did: RIO did to match or %RIO_ANY_ID to match all dids
724394b701cSMatt Porter  * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
725394b701cSMatt Porter  * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
726394b701cSMatt Porter  * @from: Previous RIO device found in search, or %NULL for new search
727394b701cSMatt Porter  *
728394b701cSMatt Porter  * Iterates through the list of known RIO devices. If a RIO device is
729394b701cSMatt Porter  * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
730394b701cSMatt Porter  * count to the device is incrememted and a pointer to its device
731394b701cSMatt Porter  * structure is returned. Otherwise, %NULL is returned. A new search
732394b701cSMatt Porter  * is initiated by passing %NULL to the @from argument. Otherwise, if
733394b701cSMatt Porter  * @from is not %NULL, searches continue from next device on the global
734394b701cSMatt Porter  * list. The reference count for @from is always decremented if it is
735394b701cSMatt Porter  * not %NULL.
736394b701cSMatt Porter  */
737394b701cSMatt Porter struct rio_dev *rio_get_asm(u16 vid, u16 did,
738394b701cSMatt Porter 			    u16 asm_vid, u16 asm_did, struct rio_dev *from)
739394b701cSMatt Porter {
740394b701cSMatt Porter 	struct list_head *n;
741394b701cSMatt Porter 	struct rio_dev *rdev;
742394b701cSMatt Porter 
743394b701cSMatt Porter 	WARN_ON(in_interrupt());
744394b701cSMatt Porter 	spin_lock(&rio_global_list_lock);
745394b701cSMatt Porter 	n = from ? from->global_list.next : rio_devices.next;
746394b701cSMatt Porter 
747394b701cSMatt Porter 	while (n && (n != &rio_devices)) {
748394b701cSMatt Porter 		rdev = rio_dev_g(n);
749394b701cSMatt Porter 		if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
750394b701cSMatt Porter 		    (did == RIO_ANY_ID || rdev->did == did) &&
751394b701cSMatt Porter 		    (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
752394b701cSMatt Porter 		    (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
753394b701cSMatt Porter 			goto exit;
754394b701cSMatt Porter 		n = n->next;
755394b701cSMatt Porter 	}
756394b701cSMatt Porter 	rdev = NULL;
757394b701cSMatt Porter       exit:
758394b701cSMatt Porter 	rio_dev_put(from);
759394b701cSMatt Porter 	rdev = rio_dev_get(rdev);
760394b701cSMatt Porter 	spin_unlock(&rio_global_list_lock);
761394b701cSMatt Porter 	return rdev;
762394b701cSMatt Porter }
763394b701cSMatt Porter 
764394b701cSMatt Porter /**
765394b701cSMatt Porter  * rio_get_device - Begin or continue searching for a RIO device by vid/did
766394b701cSMatt Porter  * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
767394b701cSMatt Porter  * @did: RIO did to match or %RIO_ANY_ID to match all dids
768394b701cSMatt Porter  * @from: Previous RIO device found in search, or %NULL for new search
769394b701cSMatt Porter  *
770394b701cSMatt Porter  * Iterates through the list of known RIO devices. If a RIO device is
771394b701cSMatt Porter  * found with a matching @vid and @did, the reference count to the
772394b701cSMatt Porter  * device is incrememted and a pointer to its device structure is returned.
773394b701cSMatt Porter  * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
774394b701cSMatt Porter  * to the @from argument. Otherwise, if @from is not %NULL, searches
775394b701cSMatt Porter  * continue from next device on the global list. The reference count for
776394b701cSMatt Porter  * @from is always decremented if it is not %NULL.
777394b701cSMatt Porter  */
778394b701cSMatt Porter struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
779394b701cSMatt Porter {
780394b701cSMatt Porter 	return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
781394b701cSMatt Porter }
782394b701cSMatt Porter 
78307590ff0SAlexandre Bounine /**
78407590ff0SAlexandre Bounine  * rio_std_route_add_entry - Add switch route table entry using standard
78507590ff0SAlexandre Bounine  *   registers defined in RIO specification rev.1.3
78607590ff0SAlexandre Bounine  * @mport: Master port to issue transaction
78707590ff0SAlexandre Bounine  * @destid: Destination ID of the device
78807590ff0SAlexandre Bounine  * @hopcount: Number of switch hops to the device
78907590ff0SAlexandre Bounine  * @table: routing table ID (global or port-specific)
79007590ff0SAlexandre Bounine  * @route_destid: destID entry in the RT
79107590ff0SAlexandre Bounine  * @route_port: destination port for specified destID
79207590ff0SAlexandre Bounine  */
79307590ff0SAlexandre Bounine int rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
79407590ff0SAlexandre Bounine 		       u16 table, u16 route_destid, u8 route_port)
79507590ff0SAlexandre Bounine {
79607590ff0SAlexandre Bounine 	if (table == RIO_GLOBAL_TABLE) {
79707590ff0SAlexandre Bounine 		rio_mport_write_config_32(mport, destid, hopcount,
79807590ff0SAlexandre Bounine 				RIO_STD_RTE_CONF_DESTID_SEL_CSR,
79907590ff0SAlexandre Bounine 				(u32)route_destid);
80007590ff0SAlexandre Bounine 		rio_mport_write_config_32(mport, destid, hopcount,
80107590ff0SAlexandre Bounine 				RIO_STD_RTE_CONF_PORT_SEL_CSR,
80207590ff0SAlexandre Bounine 				(u32)route_port);
80307590ff0SAlexandre Bounine 	}
804e5cabeb3SAlexandre Bounine 
80507590ff0SAlexandre Bounine 	udelay(10);
80607590ff0SAlexandre Bounine 	return 0;
80707590ff0SAlexandre Bounine }
80807590ff0SAlexandre Bounine 
80907590ff0SAlexandre Bounine /**
81007590ff0SAlexandre Bounine  * rio_std_route_get_entry - Read switch route table entry (port number)
81107590ff0SAlexandre Bounine  *   assosiated with specified destID using standard registers defined in RIO
81207590ff0SAlexandre Bounine  *   specification rev.1.3
81307590ff0SAlexandre Bounine  * @mport: Master port to issue transaction
81407590ff0SAlexandre Bounine  * @destid: Destination ID of the device
81507590ff0SAlexandre Bounine  * @hopcount: Number of switch hops to the device
81607590ff0SAlexandre Bounine  * @table: routing table ID (global or port-specific)
81707590ff0SAlexandre Bounine  * @route_destid: destID entry in the RT
81807590ff0SAlexandre Bounine  * @route_port: returned destination port for specified destID
81907590ff0SAlexandre Bounine  */
82007590ff0SAlexandre Bounine int rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
82107590ff0SAlexandre Bounine 		       u16 table, u16 route_destid, u8 *route_port)
82207590ff0SAlexandre Bounine {
82307590ff0SAlexandre Bounine 	u32 result;
82407590ff0SAlexandre Bounine 
82507590ff0SAlexandre Bounine 	if (table == RIO_GLOBAL_TABLE) {
82607590ff0SAlexandre Bounine 		rio_mport_write_config_32(mport, destid, hopcount,
82707590ff0SAlexandre Bounine 				RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
82807590ff0SAlexandre Bounine 		rio_mport_read_config_32(mport, destid, hopcount,
82907590ff0SAlexandre Bounine 				RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
83007590ff0SAlexandre Bounine 
83107590ff0SAlexandre Bounine 		*route_port = (u8)result;
83207590ff0SAlexandre Bounine 	}
83307590ff0SAlexandre Bounine 
83407590ff0SAlexandre Bounine 	return 0;
83507590ff0SAlexandre Bounine }
83607590ff0SAlexandre Bounine 
83707590ff0SAlexandre Bounine /**
83807590ff0SAlexandre Bounine  * rio_std_route_clr_table - Clear swotch route table using standard registers
83907590ff0SAlexandre Bounine  *   defined in RIO specification rev.1.3.
84007590ff0SAlexandre Bounine  * @mport: Master port to issue transaction
84107590ff0SAlexandre Bounine  * @destid: Destination ID of the device
84207590ff0SAlexandre Bounine  * @hopcount: Number of switch hops to the device
84307590ff0SAlexandre Bounine  * @table: routing table ID (global or port-specific)
84407590ff0SAlexandre Bounine  */
84507590ff0SAlexandre Bounine int rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
84607590ff0SAlexandre Bounine 		       u16 table)
84707590ff0SAlexandre Bounine {
84807590ff0SAlexandre Bounine 	u32 max_destid = 0xff;
84907590ff0SAlexandre Bounine 	u32 i, pef, id_inc = 1, ext_cfg = 0;
85007590ff0SAlexandre Bounine 	u32 port_sel = RIO_INVALID_ROUTE;
85107590ff0SAlexandre Bounine 
85207590ff0SAlexandre Bounine 	if (table == RIO_GLOBAL_TABLE) {
85307590ff0SAlexandre Bounine 		rio_mport_read_config_32(mport, destid, hopcount,
85407590ff0SAlexandre Bounine 					 RIO_PEF_CAR, &pef);
85507590ff0SAlexandre Bounine 
85607590ff0SAlexandre Bounine 		if (mport->sys_size) {
85707590ff0SAlexandre Bounine 			rio_mport_read_config_32(mport, destid, hopcount,
85807590ff0SAlexandre Bounine 						 RIO_SWITCH_RT_LIMIT,
85907590ff0SAlexandre Bounine 						 &max_destid);
86007590ff0SAlexandre Bounine 			max_destid &= RIO_RT_MAX_DESTID;
86107590ff0SAlexandre Bounine 		}
86207590ff0SAlexandre Bounine 
86307590ff0SAlexandre Bounine 		if (pef & RIO_PEF_EXT_RT) {
86407590ff0SAlexandre Bounine 			ext_cfg = 0x80000000;
86507590ff0SAlexandre Bounine 			id_inc = 4;
86607590ff0SAlexandre Bounine 			port_sel = (RIO_INVALID_ROUTE << 24) |
86707590ff0SAlexandre Bounine 				   (RIO_INVALID_ROUTE << 16) |
86807590ff0SAlexandre Bounine 				   (RIO_INVALID_ROUTE << 8) |
86907590ff0SAlexandre Bounine 				   RIO_INVALID_ROUTE;
87007590ff0SAlexandre Bounine 		}
87107590ff0SAlexandre Bounine 
87207590ff0SAlexandre Bounine 		for (i = 0; i <= max_destid;) {
87307590ff0SAlexandre Bounine 			rio_mport_write_config_32(mport, destid, hopcount,
87407590ff0SAlexandre Bounine 					RIO_STD_RTE_CONF_DESTID_SEL_CSR,
87507590ff0SAlexandre Bounine 					ext_cfg | i);
87607590ff0SAlexandre Bounine 			rio_mport_write_config_32(mport, destid, hopcount,
87707590ff0SAlexandre Bounine 					RIO_STD_RTE_CONF_PORT_SEL_CSR,
87807590ff0SAlexandre Bounine 					port_sel);
87907590ff0SAlexandre Bounine 			i += id_inc;
88007590ff0SAlexandre Bounine 		}
88107590ff0SAlexandre Bounine 	}
88207590ff0SAlexandre Bounine 
88307590ff0SAlexandre Bounine 	udelay(10);
88407590ff0SAlexandre Bounine 	return 0;
88507590ff0SAlexandre Bounine }
88607590ff0SAlexandre Bounine 
887394b701cSMatt Porter static void rio_fixup_device(struct rio_dev *dev)
888394b701cSMatt Porter {
889394b701cSMatt Porter }
890394b701cSMatt Porter 
891394b701cSMatt Porter static int __devinit rio_init(void)
892394b701cSMatt Porter {
893394b701cSMatt Porter 	struct rio_dev *dev = NULL;
894394b701cSMatt Porter 
895394b701cSMatt Porter 	while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
896394b701cSMatt Porter 		rio_fixup_device(dev);
897394b701cSMatt Porter 	}
898394b701cSMatt Porter 	return 0;
899394b701cSMatt Porter }
900394b701cSMatt Porter 
901394b701cSMatt Porter device_initcall(rio_init);
902394b701cSMatt Porter 
90337d33d15SAl Viro int __devinit rio_init_mports(void)
904394b701cSMatt Porter {
905394b701cSMatt Porter 	int rc = 0;
906394b701cSMatt Porter 	struct rio_mport *port;
907394b701cSMatt Porter 
908394b701cSMatt Porter 	list_for_each_entry(port, &rio_mports, node) {
909394b701cSMatt Porter 		if (!request_mem_region(port->iores.start,
910394b701cSMatt Porter 					port->iores.end - port->iores.start,
911394b701cSMatt Porter 					port->name)) {
912394b701cSMatt Porter 			printk(KERN_ERR
9135febf1cdSKumar Gala 			       "RIO: Error requesting master port region 0x%016llx-0x%016llx\n",
9145febf1cdSKumar Gala 			       (u64)port->iores.start, (u64)port->iores.end - 1);
915394b701cSMatt Porter 			rc = -ENOMEM;
916394b701cSMatt Porter 			goto out;
917394b701cSMatt Porter 		}
918394b701cSMatt Porter 
919394b701cSMatt Porter 		if (port->host_deviceid >= 0)
920394b701cSMatt Porter 			rio_enum_mport(port);
921394b701cSMatt Porter 		else
922394b701cSMatt Porter 			rio_disc_mport(port);
923394b701cSMatt Porter 	}
924394b701cSMatt Porter 
925394b701cSMatt Porter       out:
926394b701cSMatt Porter 	return rc;
927394b701cSMatt Porter }
928394b701cSMatt Porter 
929394b701cSMatt Porter void rio_register_mport(struct rio_mport *port)
930394b701cSMatt Porter {
931394b701cSMatt Porter 	list_add_tail(&port->node, &rio_mports);
932394b701cSMatt Porter }
933394b701cSMatt Porter 
934394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_local_get_device_id);
935394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_get_device);
936394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_get_asm);
937394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
938394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
939394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
940394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
941394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
942394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
943394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
944394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
945