xref: /openbmc/linux/drivers/rapidio/rio.c (revision e92a216d)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2394b701cSMatt Porter /*
3394b701cSMatt Porter  * RapidIO interconnect services
4394b701cSMatt Porter  * (RapidIO Interconnect Specification, http://www.rapidio.org)
5394b701cSMatt Porter  *
6394b701cSMatt Porter  * Copyright 2005 MontaVista Software, Inc.
7394b701cSMatt Porter  * Matt Porter <mporter@kernel.crashing.org>
8394b701cSMatt Porter  *
9fdf90abcSAlexandre Bounine  * Copyright 2009 - 2013 Integrated Device Technology, Inc.
10e5cabeb3SAlexandre Bounine  * Alex Bounine <alexandre.bounine@idt.com>
11394b701cSMatt Porter  */
12394b701cSMatt Porter 
13394b701cSMatt Porter #include <linux/types.h>
14394b701cSMatt Porter #include <linux/kernel.h>
15394b701cSMatt Porter 
16394b701cSMatt Porter #include <linux/delay.h>
17394b701cSMatt Porter #include <linux/init.h>
18394b701cSMatt Porter #include <linux/rio.h>
19394b701cSMatt Porter #include <linux/rio_drv.h>
20394b701cSMatt Porter #include <linux/rio_ids.h>
21394b701cSMatt Porter #include <linux/rio_regs.h>
22394b701cSMatt Porter #include <linux/module.h>
23394b701cSMatt Porter #include <linux/spinlock.h>
24de25968cSTim Schmielau #include <linux/slab.h>
255febf1cdSKumar Gala #include <linux/interrupt.h>
26394b701cSMatt Porter 
27394b701cSMatt Porter #include "rio.h"
28394b701cSMatt Porter 
299a0b0627SAlexandre Bounine /*
309a0b0627SAlexandre Bounine  * struct rio_pwrite - RIO portwrite event
319a0b0627SAlexandre Bounine  * @node:    Node in list of doorbell events
329a0b0627SAlexandre Bounine  * @pwcback: Doorbell event callback
339a0b0627SAlexandre Bounine  * @context: Handler specific context to pass on event
349a0b0627SAlexandre Bounine  */
359a0b0627SAlexandre Bounine struct rio_pwrite {
369a0b0627SAlexandre Bounine 	struct list_head node;
379a0b0627SAlexandre Bounine 
389a0b0627SAlexandre Bounine 	int (*pwcback)(struct rio_mport *mport, void *context,
399a0b0627SAlexandre Bounine 		       union rio_pw_msg *msg, int step);
409a0b0627SAlexandre Bounine 	void *context;
419a0b0627SAlexandre Bounine };
429a0b0627SAlexandre Bounine 
43fdf90abcSAlexandre Bounine MODULE_DESCRIPTION("RapidIO Subsystem Core");
44fdf90abcSAlexandre Bounine MODULE_AUTHOR("Matt Porter <mporter@kernel.crashing.org>");
45fdf90abcSAlexandre Bounine MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
46fdf90abcSAlexandre Bounine MODULE_LICENSE("GPL");
47fdf90abcSAlexandre Bounine 
48fdf90abcSAlexandre Bounine static int hdid[RIO_MAX_MPORTS];
49fdf90abcSAlexandre Bounine static int ids_num;
50fdf90abcSAlexandre Bounine module_param_array(hdid, int, &ids_num, 0);
51fdf90abcSAlexandre Bounine MODULE_PARM_DESC(hdid,
52fdf90abcSAlexandre Bounine 	"Destination ID assignment to local RapidIO controllers");
53fdf90abcSAlexandre Bounine 
54a11650e1SAlexandre Bounine static LIST_HEAD(rio_devices);
55e6b585caSAlexandre Bounine static LIST_HEAD(rio_nets);
56a11650e1SAlexandre Bounine static DEFINE_SPINLOCK(rio_global_list_lock);
57a11650e1SAlexandre Bounine 
58394b701cSMatt Porter static LIST_HEAD(rio_mports);
599edbc30bSAlexandre Bounine static LIST_HEAD(rio_scans);
60a11650e1SAlexandre Bounine static DEFINE_MUTEX(rio_mport_list_lock);
61569fccb6SAlexandre Bounine static unsigned char next_portid;
62da1589f0SAlexandre Bounine static DEFINE_SPINLOCK(rio_mmap_lock);
63394b701cSMatt Porter 
64394b701cSMatt Porter /**
65394b701cSMatt Porter  * rio_local_get_device_id - Get the base/extended device id for a port
66394b701cSMatt Porter  * @port: RIO master port from which to get the deviceid
67394b701cSMatt Porter  *
68394b701cSMatt Porter  * Reads the base/extended device id from the local device
69394b701cSMatt Porter  * implementing the master port. Returns the 8/16-bit device
70394b701cSMatt Porter  * id.
71394b701cSMatt Porter  */
rio_local_get_device_id(struct rio_mport * port)72394b701cSMatt Porter u16 rio_local_get_device_id(struct rio_mport *port)
73394b701cSMatt Porter {
74394b701cSMatt Porter 	u32 result;
75394b701cSMatt Porter 
76394b701cSMatt Porter 	rio_local_read_config_32(port, RIO_DID_CSR, &result);
77394b701cSMatt Porter 
78e0423236SZhang Wei 	return (RIO_GET_DID(port->sys_size, result));
79394b701cSMatt Porter }
804ba61ecaSMarkus Elfring EXPORT_SYMBOL_GPL(rio_local_get_device_id);
81394b701cSMatt Porter 
82394b701cSMatt Porter /**
838b189fdbSAlexandre Bounine  * rio_query_mport - Query mport device attributes
848b189fdbSAlexandre Bounine  * @port: mport device to query
858b189fdbSAlexandre Bounine  * @mport_attr: mport attributes data structure
868b189fdbSAlexandre Bounine  *
878b189fdbSAlexandre Bounine  * Returns attributes of specified mport through the
888b189fdbSAlexandre Bounine  * pointer to attributes data structure.
898b189fdbSAlexandre Bounine  */
rio_query_mport(struct rio_mport * port,struct rio_mport_attr * mport_attr)908b189fdbSAlexandre Bounine int rio_query_mport(struct rio_mport *port,
918b189fdbSAlexandre Bounine 		    struct rio_mport_attr *mport_attr)
928b189fdbSAlexandre Bounine {
938b189fdbSAlexandre Bounine 	if (!port->ops->query_mport)
948b189fdbSAlexandre Bounine 		return -ENODATA;
958b189fdbSAlexandre Bounine 	return port->ops->query_mport(port, mport_attr);
968b189fdbSAlexandre Bounine }
978b189fdbSAlexandre Bounine EXPORT_SYMBOL(rio_query_mport);
988b189fdbSAlexandre Bounine 
998b189fdbSAlexandre Bounine /**
100e6b585caSAlexandre Bounine  * rio_alloc_net- Allocate and initialize a new RIO network data structure
101e6b585caSAlexandre Bounine  * @mport: Master port associated with the RIO network
102e6b585caSAlexandre Bounine  *
103e6b585caSAlexandre Bounine  * Allocates a RIO network structure, initializes per-network
104e6b585caSAlexandre Bounine  * list heads, and adds the associated master port to the
105e6b585caSAlexandre Bounine  * network list of associated master ports. Returns a
106e6b585caSAlexandre Bounine  * RIO network pointer on success or %NULL on failure.
107e6b585caSAlexandre Bounine  */
rio_alloc_net(struct rio_mport * mport)108e6b585caSAlexandre Bounine struct rio_net *rio_alloc_net(struct rio_mport *mport)
109e6b585caSAlexandre Bounine {
110d1509c09SMarkus Elfring 	struct rio_net *net = kzalloc(sizeof(*net), GFP_KERNEL);
111e6b585caSAlexandre Bounine 
112e6b585caSAlexandre Bounine 	if (net) {
113e6b585caSAlexandre Bounine 		INIT_LIST_HEAD(&net->node);
114e6b585caSAlexandre Bounine 		INIT_LIST_HEAD(&net->devices);
115e6b585caSAlexandre Bounine 		INIT_LIST_HEAD(&net->switches);
116e6b585caSAlexandre Bounine 		INIT_LIST_HEAD(&net->mports);
117e6b585caSAlexandre Bounine 		mport->net = net;
118e6b585caSAlexandre Bounine 	}
119e6b585caSAlexandre Bounine 	return net;
120e6b585caSAlexandre Bounine }
121e6b585caSAlexandre Bounine EXPORT_SYMBOL_GPL(rio_alloc_net);
122e6b585caSAlexandre Bounine 
rio_add_net(struct rio_net * net)123e6b585caSAlexandre Bounine int rio_add_net(struct rio_net *net)
124e6b585caSAlexandre Bounine {
125e6b585caSAlexandre Bounine 	int err;
126e6b585caSAlexandre Bounine 
127e6b585caSAlexandre Bounine 	err = device_register(&net->dev);
128e6b585caSAlexandre Bounine 	if (err)
129e6b585caSAlexandre Bounine 		return err;
130e6b585caSAlexandre Bounine 	spin_lock(&rio_global_list_lock);
131e6b585caSAlexandre Bounine 	list_add_tail(&net->node, &rio_nets);
132e6b585caSAlexandre Bounine 	spin_unlock(&rio_global_list_lock);
133e6b585caSAlexandre Bounine 
134e6b585caSAlexandre Bounine 	return 0;
135e6b585caSAlexandre Bounine }
136e6b585caSAlexandre Bounine EXPORT_SYMBOL_GPL(rio_add_net);
137e6b585caSAlexandre Bounine 
rio_free_net(struct rio_net * net)138e6b585caSAlexandre Bounine void rio_free_net(struct rio_net *net)
139e6b585caSAlexandre Bounine {
140e6b585caSAlexandre Bounine 	spin_lock(&rio_global_list_lock);
141e6b585caSAlexandre Bounine 	if (!list_empty(&net->node))
142e6b585caSAlexandre Bounine 		list_del(&net->node);
143e6b585caSAlexandre Bounine 	spin_unlock(&rio_global_list_lock);
144e6b585caSAlexandre Bounine 	if (net->release)
145e6b585caSAlexandre Bounine 		net->release(net);
146e6b585caSAlexandre Bounine 	device_unregister(&net->dev);
147e6b585caSAlexandre Bounine }
148e6b585caSAlexandre Bounine EXPORT_SYMBOL_GPL(rio_free_net);
149e6b585caSAlexandre Bounine 
150e6b585caSAlexandre Bounine /**
1515024622fSAlexandre Bounine  * rio_local_set_device_id - Set the base/extended device id for a port
1525024622fSAlexandre Bounine  * @port: RIO master port
1535024622fSAlexandre Bounine  * @did: Device ID value to be written
1545024622fSAlexandre Bounine  *
1555024622fSAlexandre Bounine  * Writes the base/extended device id from a device.
1565024622fSAlexandre Bounine  */
rio_local_set_device_id(struct rio_mport * port,u16 did)1575024622fSAlexandre Bounine void rio_local_set_device_id(struct rio_mport *port, u16 did)
1585024622fSAlexandre Bounine {
1595024622fSAlexandre Bounine 	rio_local_write_config_32(port, RIO_DID_CSR,
1605024622fSAlexandre Bounine 				  RIO_SET_DID(port->sys_size, did));
1615024622fSAlexandre Bounine }
1625024622fSAlexandre Bounine EXPORT_SYMBOL_GPL(rio_local_set_device_id);
1635024622fSAlexandre Bounine 
1645024622fSAlexandre Bounine /**
165a11650e1SAlexandre Bounine  * rio_add_device- Adds a RIO device to the device model
166a11650e1SAlexandre Bounine  * @rdev: RIO device
167a11650e1SAlexandre Bounine  *
168a11650e1SAlexandre Bounine  * Adds the RIO device to the global device list and adds the RIO
169a11650e1SAlexandre Bounine  * device to the RIO device list.  Creates the generic sysfs nodes
170a11650e1SAlexandre Bounine  * for an RIO device.
171a11650e1SAlexandre Bounine  */
rio_add_device(struct rio_dev * rdev)172a11650e1SAlexandre Bounine int rio_add_device(struct rio_dev *rdev)
173a11650e1SAlexandre Bounine {
174a11650e1SAlexandre Bounine 	int err;
175a11650e1SAlexandre Bounine 
176b77a2030SAlexandre Bounine 	atomic_set(&rdev->state, RIO_DEVICE_RUNNING);
177b74ec56eSAlexandre Bounine 	err = device_register(&rdev->dev);
178a11650e1SAlexandre Bounine 	if (err)
179a11650e1SAlexandre Bounine 		return err;
180a11650e1SAlexandre Bounine 
181a11650e1SAlexandre Bounine 	spin_lock(&rio_global_list_lock);
182a11650e1SAlexandre Bounine 	list_add_tail(&rdev->global_list, &rio_devices);
183b74ec56eSAlexandre Bounine 	if (rdev->net) {
184b74ec56eSAlexandre Bounine 		list_add_tail(&rdev->net_list, &rdev->net->devices);
185b74ec56eSAlexandre Bounine 		if (rdev->pef & RIO_PEF_SWITCH)
186b74ec56eSAlexandre Bounine 			list_add_tail(&rdev->rswitch->node,
187b74ec56eSAlexandre Bounine 				      &rdev->net->switches);
188b74ec56eSAlexandre Bounine 	}
189a11650e1SAlexandre Bounine 	spin_unlock(&rio_global_list_lock);
190a11650e1SAlexandre Bounine 
191a11650e1SAlexandre Bounine 	return 0;
192a11650e1SAlexandre Bounine }
193a11650e1SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_add_device);
194a11650e1SAlexandre Bounine 
195b74ec56eSAlexandre Bounine /*
196b74ec56eSAlexandre Bounine  * rio_del_device - removes a RIO device from the device model
197b74ec56eSAlexandre Bounine  * @rdev: RIO device
198b77a2030SAlexandre Bounine  * @state: device state to set during removal process
199b74ec56eSAlexandre Bounine  *
200b74ec56eSAlexandre Bounine  * Removes the RIO device to the kernel device list and subsystem's device list.
201b74ec56eSAlexandre Bounine  * Clears sysfs entries for the removed device.
202b74ec56eSAlexandre Bounine  */
rio_del_device(struct rio_dev * rdev,enum rio_device_state state)203b77a2030SAlexandre Bounine void rio_del_device(struct rio_dev *rdev, enum rio_device_state state)
204b74ec56eSAlexandre Bounine {
205b74ec56eSAlexandre Bounine 	pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev));
206b77a2030SAlexandre Bounine 	atomic_set(&rdev->state, state);
207b74ec56eSAlexandre Bounine 	spin_lock(&rio_global_list_lock);
208b74ec56eSAlexandre Bounine 	list_del(&rdev->global_list);
209b74ec56eSAlexandre Bounine 	if (rdev->net) {
210b74ec56eSAlexandre Bounine 		list_del(&rdev->net_list);
211b74ec56eSAlexandre Bounine 		if (rdev->pef & RIO_PEF_SWITCH) {
212b74ec56eSAlexandre Bounine 			list_del(&rdev->rswitch->node);
213b74ec56eSAlexandre Bounine 			kfree(rdev->rswitch->route_table);
214b74ec56eSAlexandre Bounine 		}
215b74ec56eSAlexandre Bounine 	}
216b74ec56eSAlexandre Bounine 	spin_unlock(&rio_global_list_lock);
217b74ec56eSAlexandre Bounine 	device_unregister(&rdev->dev);
218b74ec56eSAlexandre Bounine }
219b74ec56eSAlexandre Bounine EXPORT_SYMBOL_GPL(rio_del_device);
220b74ec56eSAlexandre Bounine 
221a11650e1SAlexandre Bounine /**
222394b701cSMatt Porter  * rio_request_inb_mbox - request inbound mailbox service
223394b701cSMatt Porter  * @mport: RIO master port from which to allocate the mailbox resource
2246978bbc0SMatt Porter  * @dev_id: Device specific pointer to pass on event
225394b701cSMatt Porter  * @mbox: Mailbox number to claim
226394b701cSMatt Porter  * @entries: Number of entries in inbound mailbox queue
227394b701cSMatt Porter  * @minb: Callback to execute when inbound message is received
228394b701cSMatt Porter  *
229394b701cSMatt Porter  * Requests ownership of an inbound mailbox resource and binds
230394b701cSMatt Porter  * a callback function to the resource. Returns %0 on success.
231394b701cSMatt Porter  */
rio_request_inb_mbox(struct rio_mport * mport,void * dev_id,int mbox,int entries,void (* minb)(struct rio_mport * mport,void * dev_id,int mbox,int slot))232394b701cSMatt Porter int rio_request_inb_mbox(struct rio_mport *mport,
2336978bbc0SMatt Porter 			 void *dev_id,
234394b701cSMatt Porter 			 int mbox,
235394b701cSMatt Porter 			 int entries,
2366978bbc0SMatt Porter 			 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
237394b701cSMatt Porter 				       int slot))
238394b701cSMatt Porter {
239f8f06269SAlexandre Bounine 	int rc = -ENOSYS;
240f8f06269SAlexandre Bounine 	struct resource *res;
241394b701cSMatt Porter 
24293dd49afSMarkus Elfring 	if (!mport->ops->open_inb_mbox)
243f8f06269SAlexandre Bounine 		goto out;
244f8f06269SAlexandre Bounine 
245d1509c09SMarkus Elfring 	res = kzalloc(sizeof(*res), GFP_KERNEL);
246394b701cSMatt Porter 	if (res) {
247394b701cSMatt Porter 		rio_init_mbox_res(res, mbox, mbox);
248394b701cSMatt Porter 
249394b701cSMatt Porter 		/* Make sure this mailbox isn't in use */
250e1d66d04SMarkus Elfring 		rc = request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
251e1d66d04SMarkus Elfring 				      res);
252e1d66d04SMarkus Elfring 		if (rc < 0) {
253394b701cSMatt Porter 			kfree(res);
254394b701cSMatt Porter 			goto out;
255394b701cSMatt Porter 		}
256394b701cSMatt Porter 
257394b701cSMatt Porter 		mport->inb_msg[mbox].res = res;
258394b701cSMatt Porter 
259394b701cSMatt Porter 		/* Hook the inbound message callback */
260394b701cSMatt Porter 		mport->inb_msg[mbox].mcback = minb;
261394b701cSMatt Porter 
262f8f06269SAlexandre Bounine 		rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
26306e1b249SAlexandre Bounine 		if (rc) {
26406e1b249SAlexandre Bounine 			mport->inb_msg[mbox].mcback = NULL;
26506e1b249SAlexandre Bounine 			mport->inb_msg[mbox].res = NULL;
26606e1b249SAlexandre Bounine 			release_resource(res);
26706e1b249SAlexandre Bounine 			kfree(res);
26806e1b249SAlexandre Bounine 		}
269394b701cSMatt Porter 	} else
270394b701cSMatt Porter 		rc = -ENOMEM;
271394b701cSMatt Porter 
272394b701cSMatt Porter       out:
273394b701cSMatt Porter 	return rc;
274394b701cSMatt Porter }
2754ba61ecaSMarkus Elfring EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
276394b701cSMatt Porter 
277394b701cSMatt Porter /**
278394b701cSMatt Porter  * rio_release_inb_mbox - release inbound mailbox message service
279394b701cSMatt Porter  * @mport: RIO master port from which to release the mailbox resource
280394b701cSMatt Porter  * @mbox: Mailbox number to release
281394b701cSMatt Porter  *
282394b701cSMatt Porter  * Releases ownership of an inbound mailbox resource. Returns 0
283394b701cSMatt Porter  * if the request has been satisfied.
284394b701cSMatt Porter  */
rio_release_inb_mbox(struct rio_mport * mport,int mbox)285394b701cSMatt Porter int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
286394b701cSMatt Porter {
28706e1b249SAlexandre Bounine 	int rc;
288394b701cSMatt Porter 
28906e1b249SAlexandre Bounine 	if (!mport->ops->close_inb_mbox || !mport->inb_msg[mbox].res)
29006e1b249SAlexandre Bounine 		return -EINVAL;
29106e1b249SAlexandre Bounine 
29206e1b249SAlexandre Bounine 	mport->ops->close_inb_mbox(mport, mbox);
29306e1b249SAlexandre Bounine 	mport->inb_msg[mbox].mcback = NULL;
29406e1b249SAlexandre Bounine 
29506e1b249SAlexandre Bounine 	rc = release_resource(mport->inb_msg[mbox].res);
29606e1b249SAlexandre Bounine 	if (rc)
29706e1b249SAlexandre Bounine 		return rc;
29806e1b249SAlexandre Bounine 
29906e1b249SAlexandre Bounine 	kfree(mport->inb_msg[mbox].res);
30006e1b249SAlexandre Bounine 	mport->inb_msg[mbox].res = NULL;
30106e1b249SAlexandre Bounine 
30206e1b249SAlexandre Bounine 	return 0;
303394b701cSMatt Porter }
3044ba61ecaSMarkus Elfring EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
305394b701cSMatt Porter 
306394b701cSMatt Porter /**
307394b701cSMatt Porter  * rio_request_outb_mbox - request outbound mailbox service
308394b701cSMatt Porter  * @mport: RIO master port from which to allocate the mailbox resource
3096978bbc0SMatt Porter  * @dev_id: Device specific pointer to pass on event
310394b701cSMatt Porter  * @mbox: Mailbox number to claim
311394b701cSMatt Porter  * @entries: Number of entries in outbound mailbox queue
312394b701cSMatt Porter  * @moutb: Callback to execute when outbound message is sent
313394b701cSMatt Porter  *
314394b701cSMatt Porter  * Requests ownership of an outbound mailbox resource and binds
315394b701cSMatt Porter  * a callback function to the resource. Returns 0 on success.
316394b701cSMatt Porter  */
rio_request_outb_mbox(struct rio_mport * mport,void * dev_id,int mbox,int entries,void (* moutb)(struct rio_mport * mport,void * dev_id,int mbox,int slot))317394b701cSMatt Porter int rio_request_outb_mbox(struct rio_mport *mport,
3186978bbc0SMatt Porter 			  void *dev_id,
319394b701cSMatt Porter 			  int mbox,
320394b701cSMatt Porter 			  int entries,
3216978bbc0SMatt Porter 			  void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
322394b701cSMatt Porter {
323f8f06269SAlexandre Bounine 	int rc = -ENOSYS;
324f8f06269SAlexandre Bounine 	struct resource *res;
325394b701cSMatt Porter 
32693dd49afSMarkus Elfring 	if (!mport->ops->open_outb_mbox)
327f8f06269SAlexandre Bounine 		goto out;
328f8f06269SAlexandre Bounine 
329d1509c09SMarkus Elfring 	res = kzalloc(sizeof(*res), GFP_KERNEL);
330394b701cSMatt Porter 	if (res) {
331394b701cSMatt Porter 		rio_init_mbox_res(res, mbox, mbox);
332394b701cSMatt Porter 
333394b701cSMatt Porter 		/* Make sure this outbound mailbox isn't in use */
334e1d66d04SMarkus Elfring 		rc = request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
335e1d66d04SMarkus Elfring 				      res);
336e1d66d04SMarkus Elfring 		if (rc < 0) {
337394b701cSMatt Porter 			kfree(res);
338394b701cSMatt Porter 			goto out;
339394b701cSMatt Porter 		}
340394b701cSMatt Porter 
341394b701cSMatt Porter 		mport->outb_msg[mbox].res = res;
342394b701cSMatt Porter 
343394b701cSMatt Porter 		/* Hook the inbound message callback */
344394b701cSMatt Porter 		mport->outb_msg[mbox].mcback = moutb;
345394b701cSMatt Porter 
346f8f06269SAlexandre Bounine 		rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
34706e1b249SAlexandre Bounine 		if (rc) {
34806e1b249SAlexandre Bounine 			mport->outb_msg[mbox].mcback = NULL;
34906e1b249SAlexandre Bounine 			mport->outb_msg[mbox].res = NULL;
35006e1b249SAlexandre Bounine 			release_resource(res);
35106e1b249SAlexandre Bounine 			kfree(res);
35206e1b249SAlexandre Bounine 		}
353394b701cSMatt Porter 	} else
354394b701cSMatt Porter 		rc = -ENOMEM;
355394b701cSMatt Porter 
356394b701cSMatt Porter       out:
357394b701cSMatt Porter 	return rc;
358394b701cSMatt Porter }
3594ba61ecaSMarkus Elfring EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
360394b701cSMatt Porter 
361394b701cSMatt Porter /**
362394b701cSMatt Porter  * rio_release_outb_mbox - release outbound mailbox message service
363394b701cSMatt Porter  * @mport: RIO master port from which to release the mailbox resource
364394b701cSMatt Porter  * @mbox: Mailbox number to release
365394b701cSMatt Porter  *
366394b701cSMatt Porter  * Releases ownership of an inbound mailbox resource. Returns 0
367394b701cSMatt Porter  * if the request has been satisfied.
368394b701cSMatt Porter  */
rio_release_outb_mbox(struct rio_mport * mport,int mbox)369394b701cSMatt Porter int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
370394b701cSMatt Porter {
37106e1b249SAlexandre Bounine 	int rc;
372394b701cSMatt Porter 
37306e1b249SAlexandre Bounine 	if (!mport->ops->close_outb_mbox || !mport->outb_msg[mbox].res)
37406e1b249SAlexandre Bounine 		return -EINVAL;
37506e1b249SAlexandre Bounine 
37606e1b249SAlexandre Bounine 	mport->ops->close_outb_mbox(mport, mbox);
37706e1b249SAlexandre Bounine 	mport->outb_msg[mbox].mcback = NULL;
37806e1b249SAlexandre Bounine 
37906e1b249SAlexandre Bounine 	rc = release_resource(mport->outb_msg[mbox].res);
38006e1b249SAlexandre Bounine 	if (rc)
38106e1b249SAlexandre Bounine 		return rc;
38206e1b249SAlexandre Bounine 
38306e1b249SAlexandre Bounine 	kfree(mport->outb_msg[mbox].res);
38406e1b249SAlexandre Bounine 	mport->outb_msg[mbox].res = NULL;
38506e1b249SAlexandre Bounine 
38606e1b249SAlexandre Bounine 	return 0;
387394b701cSMatt Porter }
3884ba61ecaSMarkus Elfring EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
389394b701cSMatt Porter 
390394b701cSMatt Porter /**
391394b701cSMatt Porter  * rio_setup_inb_dbell - bind inbound doorbell callback
392394b701cSMatt Porter  * @mport: RIO master port to bind the doorbell callback
3936978bbc0SMatt Porter  * @dev_id: Device specific pointer to pass on event
394394b701cSMatt Porter  * @res: Doorbell message resource
395394b701cSMatt Porter  * @dinb: Callback to execute when doorbell is received
396394b701cSMatt Porter  *
397394b701cSMatt Porter  * Adds a doorbell resource/callback pair into a port's
398394b701cSMatt Porter  * doorbell event list. Returns 0 if the request has been
399394b701cSMatt Porter  * satisfied.
400394b701cSMatt Porter  */
401394b701cSMatt Porter static int
rio_setup_inb_dbell(struct rio_mport * mport,void * dev_id,struct resource * res,void (* dinb)(struct rio_mport * mport,void * dev_id,u16 src,u16 dst,u16 info))4026978bbc0SMatt Porter rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
4036978bbc0SMatt Porter 		    void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
404394b701cSMatt Porter 				  u16 info))
405394b701cSMatt Porter {
406e1d66d04SMarkus Elfring 	struct rio_dbell *dbell = kmalloc(sizeof(*dbell), GFP_KERNEL);
407394b701cSMatt Porter 
4081acd14bfSMarkus Elfring 	if (!dbell)
4091acd14bfSMarkus Elfring 		return -ENOMEM;
410394b701cSMatt Porter 
411394b701cSMatt Porter 	dbell->res = res;
412394b701cSMatt Porter 	dbell->dinb = dinb;
4136978bbc0SMatt Porter 	dbell->dev_id = dev_id;
414394b701cSMatt Porter 
415a7b4c636SAlexandre Bounine 	mutex_lock(&mport->lock);
416394b701cSMatt Porter 	list_add_tail(&dbell->node, &mport->dbells);
417a7b4c636SAlexandre Bounine 	mutex_unlock(&mport->lock);
4181acd14bfSMarkus Elfring 	return 0;
419394b701cSMatt Porter }
420394b701cSMatt Porter 
421394b701cSMatt Porter /**
422394b701cSMatt Porter  * rio_request_inb_dbell - request inbound doorbell message service
423394b701cSMatt Porter  * @mport: RIO master port from which to allocate the doorbell resource
4246978bbc0SMatt Porter  * @dev_id: Device specific pointer to pass on event
425394b701cSMatt Porter  * @start: Doorbell info range start
426394b701cSMatt Porter  * @end: Doorbell info range end
427394b701cSMatt Porter  * @dinb: Callback to execute when doorbell is received
428394b701cSMatt Porter  *
429394b701cSMatt Porter  * Requests ownership of an inbound doorbell resource and binds
430394b701cSMatt Porter  * a callback function to the resource. Returns 0 if the request
431394b701cSMatt Porter  * has been satisfied.
432394b701cSMatt Porter  */
rio_request_inb_dbell(struct rio_mport * mport,void * dev_id,u16 start,u16 end,void (* dinb)(struct rio_mport * mport,void * dev_id,u16 src,u16 dst,u16 info))433394b701cSMatt Porter int rio_request_inb_dbell(struct rio_mport *mport,
4346978bbc0SMatt Porter 			  void *dev_id,
435394b701cSMatt Porter 			  u16 start,
436394b701cSMatt Porter 			  u16 end,
4376978bbc0SMatt Porter 			  void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
438394b701cSMatt Porter 					u16 dst, u16 info))
439394b701cSMatt Porter {
440002f6f40SMarkus Elfring 	int rc;
441d1509c09SMarkus Elfring 	struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
442394b701cSMatt Porter 
443394b701cSMatt Porter 	if (res) {
444394b701cSMatt Porter 		rio_init_dbell_res(res, start, end);
445394b701cSMatt Porter 
446394b701cSMatt Porter 		/* Make sure these doorbells aren't in use */
447e1d66d04SMarkus Elfring 		rc = request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
448e1d66d04SMarkus Elfring 				      res);
449e1d66d04SMarkus Elfring 		if (rc < 0) {
450394b701cSMatt Porter 			kfree(res);
451394b701cSMatt Porter 			goto out;
452394b701cSMatt Porter 		}
453394b701cSMatt Porter 
454394b701cSMatt Porter 		/* Hook the doorbell callback */
4556978bbc0SMatt Porter 		rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
456394b701cSMatt Porter 	} else
457394b701cSMatt Porter 		rc = -ENOMEM;
458394b701cSMatt Porter 
459394b701cSMatt Porter       out:
460394b701cSMatt Porter 	return rc;
461394b701cSMatt Porter }
4624ba61ecaSMarkus Elfring EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
463394b701cSMatt Porter 
464394b701cSMatt Porter /**
465394b701cSMatt Porter  * rio_release_inb_dbell - release inbound doorbell message service
466394b701cSMatt Porter  * @mport: RIO master port from which to release the doorbell resource
467394b701cSMatt Porter  * @start: Doorbell info range start
468394b701cSMatt Porter  * @end: Doorbell info range end
469394b701cSMatt Porter  *
470394b701cSMatt Porter  * Releases ownership of an inbound doorbell resource and removes
471394b701cSMatt Porter  * callback from the doorbell event list. Returns 0 if the request
472394b701cSMatt Porter  * has been satisfied.
473394b701cSMatt Porter  */
rio_release_inb_dbell(struct rio_mport * mport,u16 start,u16 end)474394b701cSMatt Porter int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
475394b701cSMatt Porter {
476394b701cSMatt Porter 	int rc = 0, found = 0;
477394b701cSMatt Porter 	struct rio_dbell *dbell;
478394b701cSMatt Porter 
479a7b4c636SAlexandre Bounine 	mutex_lock(&mport->lock);
480394b701cSMatt Porter 	list_for_each_entry(dbell, &mport->dbells, node) {
481394b701cSMatt Porter 		if ((dbell->res->start == start) && (dbell->res->end == end)) {
482a7b4c636SAlexandre Bounine 			list_del(&dbell->node);
483394b701cSMatt Porter 			found = 1;
484394b701cSMatt Porter 			break;
485394b701cSMatt Porter 		}
486394b701cSMatt Porter 	}
487a7b4c636SAlexandre Bounine 	mutex_unlock(&mport->lock);
488394b701cSMatt Porter 
489394b701cSMatt Porter 	/* If we can't find an exact match, fail */
490394b701cSMatt Porter 	if (!found) {
491394b701cSMatt Porter 		rc = -EINVAL;
492394b701cSMatt Porter 		goto out;
493394b701cSMatt Porter 	}
494394b701cSMatt Porter 
495394b701cSMatt Porter 	/* Release the doorbell resource */
496394b701cSMatt Porter 	rc = release_resource(dbell->res);
497394b701cSMatt Porter 
498394b701cSMatt Porter 	/* Free the doorbell event */
499394b701cSMatt Porter 	kfree(dbell);
500394b701cSMatt Porter 
501394b701cSMatt Porter       out:
502394b701cSMatt Porter 	return rc;
503394b701cSMatt Porter }
5044ba61ecaSMarkus Elfring EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
505394b701cSMatt Porter 
506394b701cSMatt Porter /**
507394b701cSMatt Porter  * rio_request_outb_dbell - request outbound doorbell message range
508394b701cSMatt Porter  * @rdev: RIO device from which to allocate the doorbell resource
509394b701cSMatt Porter  * @start: Doorbell message range start
510394b701cSMatt Porter  * @end: Doorbell message range end
511394b701cSMatt Porter  *
512394b701cSMatt Porter  * Requests ownership of a doorbell message range. Returns a resource
513394b701cSMatt Porter  * if the request has been satisfied or %NULL on failure.
514394b701cSMatt Porter  */
rio_request_outb_dbell(struct rio_dev * rdev,u16 start,u16 end)515394b701cSMatt Porter struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
516394b701cSMatt Porter 					u16 end)
517394b701cSMatt Porter {
5189a975beeSToshi Kani 	struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
519394b701cSMatt Porter 
520394b701cSMatt Porter 	if (res) {
521394b701cSMatt Porter 		rio_init_dbell_res(res, start, end);
522394b701cSMatt Porter 
523394b701cSMatt Porter 		/* Make sure these doorbells aren't in use */
524394b701cSMatt Porter 		if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
525394b701cSMatt Porter 		    < 0) {
526394b701cSMatt Porter 			kfree(res);
527394b701cSMatt Porter 			res = NULL;
528394b701cSMatt Porter 		}
529394b701cSMatt Porter 	}
530394b701cSMatt Porter 
531394b701cSMatt Porter 	return res;
532394b701cSMatt Porter }
5334ba61ecaSMarkus Elfring EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
534394b701cSMatt Porter 
535394b701cSMatt Porter /**
536394b701cSMatt Porter  * rio_release_outb_dbell - release outbound doorbell message range
537394b701cSMatt Porter  * @rdev: RIO device from which to release the doorbell resource
538394b701cSMatt Porter  * @res: Doorbell resource to be freed
539394b701cSMatt Porter  *
540394b701cSMatt Porter  * Releases ownership of a doorbell message range. Returns 0 if the
541394b701cSMatt Porter  * request has been satisfied.
542394b701cSMatt Porter  */
rio_release_outb_dbell(struct rio_dev * rdev,struct resource * res)543394b701cSMatt Porter int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
544394b701cSMatt Porter {
545394b701cSMatt Porter 	int rc = release_resource(res);
546394b701cSMatt Porter 
547394b701cSMatt Porter 	kfree(res);
548394b701cSMatt Porter 
549394b701cSMatt Porter 	return rc;
550394b701cSMatt Porter }
5514ba61ecaSMarkus Elfring EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
552394b701cSMatt Porter 
553394b701cSMatt Porter /**
5549a0b0627SAlexandre Bounine  * rio_add_mport_pw_handler - add port-write message handler into the list
5559a0b0627SAlexandre Bounine  *                            of mport specific pw handlers
5569a0b0627SAlexandre Bounine  * @mport:   RIO master port to bind the portwrite callback
5579a0b0627SAlexandre Bounine  * @context: Handler specific context to pass on event
5589a0b0627SAlexandre Bounine  * @pwcback: Callback to execute when portwrite is received
5599a0b0627SAlexandre Bounine  *
5609a0b0627SAlexandre Bounine  * Returns 0 if the request has been satisfied.
5619a0b0627SAlexandre Bounine  */
rio_add_mport_pw_handler(struct rio_mport * mport,void * context,int (* pwcback)(struct rio_mport * mport,void * context,union rio_pw_msg * msg,int step))5629a0b0627SAlexandre Bounine int rio_add_mport_pw_handler(struct rio_mport *mport, void *context,
5639a0b0627SAlexandre Bounine 			     int (*pwcback)(struct rio_mport *mport,
5649a0b0627SAlexandre Bounine 			     void *context, union rio_pw_msg *msg, int step))
5659a0b0627SAlexandre Bounine {
566d1509c09SMarkus Elfring 	struct rio_pwrite *pwrite = kzalloc(sizeof(*pwrite), GFP_KERNEL);
5679a0b0627SAlexandre Bounine 
5681acd14bfSMarkus Elfring 	if (!pwrite)
5691acd14bfSMarkus Elfring 		return -ENOMEM;
5709a0b0627SAlexandre Bounine 
5719a0b0627SAlexandre Bounine 	pwrite->pwcback = pwcback;
5729a0b0627SAlexandre Bounine 	pwrite->context = context;
5739a0b0627SAlexandre Bounine 	mutex_lock(&mport->lock);
5749a0b0627SAlexandre Bounine 	list_add_tail(&pwrite->node, &mport->pwrites);
5759a0b0627SAlexandre Bounine 	mutex_unlock(&mport->lock);
5761acd14bfSMarkus Elfring 	return 0;
5779a0b0627SAlexandre Bounine }
5789a0b0627SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_add_mport_pw_handler);
5799a0b0627SAlexandre Bounine 
5809a0b0627SAlexandre Bounine /**
5819a0b0627SAlexandre Bounine  * rio_del_mport_pw_handler - remove port-write message handler from the list
5829a0b0627SAlexandre Bounine  *                            of mport specific pw handlers
5839a0b0627SAlexandre Bounine  * @mport:   RIO master port to bind the portwrite callback
5849a0b0627SAlexandre Bounine  * @context: Registered handler specific context to pass on event
5859a0b0627SAlexandre Bounine  * @pwcback: Registered callback function
5869a0b0627SAlexandre Bounine  *
5879a0b0627SAlexandre Bounine  * Returns 0 if the request has been satisfied.
5889a0b0627SAlexandre Bounine  */
rio_del_mport_pw_handler(struct rio_mport * mport,void * context,int (* pwcback)(struct rio_mport * mport,void * context,union rio_pw_msg * msg,int step))5899a0b0627SAlexandre Bounine int rio_del_mport_pw_handler(struct rio_mport *mport, void *context,
5909a0b0627SAlexandre Bounine 			     int (*pwcback)(struct rio_mport *mport,
5919a0b0627SAlexandre Bounine 			     void *context, union rio_pw_msg *msg, int step))
5929a0b0627SAlexandre Bounine {
5939a0b0627SAlexandre Bounine 	int rc = -EINVAL;
5949a0b0627SAlexandre Bounine 	struct rio_pwrite *pwrite;
5959a0b0627SAlexandre Bounine 
5969a0b0627SAlexandre Bounine 	mutex_lock(&mport->lock);
5979a0b0627SAlexandre Bounine 	list_for_each_entry(pwrite, &mport->pwrites, node) {
5989a0b0627SAlexandre Bounine 		if (pwrite->pwcback == pwcback && pwrite->context == context) {
5999a0b0627SAlexandre Bounine 			list_del(&pwrite->node);
6009a0b0627SAlexandre Bounine 			kfree(pwrite);
6019a0b0627SAlexandre Bounine 			rc = 0;
6029a0b0627SAlexandre Bounine 			break;
6039a0b0627SAlexandre Bounine 		}
6049a0b0627SAlexandre Bounine 	}
6059a0b0627SAlexandre Bounine 	mutex_unlock(&mport->lock);
6069a0b0627SAlexandre Bounine 
6079a0b0627SAlexandre Bounine 	return rc;
6089a0b0627SAlexandre Bounine }
6099a0b0627SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_del_mport_pw_handler);
6109a0b0627SAlexandre Bounine 
6119a0b0627SAlexandre Bounine /**
6129a0b0627SAlexandre Bounine  * rio_request_inb_pwrite - request inbound port-write message service for
6139a0b0627SAlexandre Bounine  *                          specific RapidIO device
61497ef6f74SRandy Dunlap  * @rdev: RIO device to which register inbound port-write callback routine
615e5cabeb3SAlexandre Bounine  * @pwcback: Callback routine to execute when port-write is received
616e5cabeb3SAlexandre Bounine  *
617e5cabeb3SAlexandre Bounine  * Binds a port-write callback function to the RapidIO device.
618e5cabeb3SAlexandre Bounine  * Returns 0 if the request has been satisfied.
619e5cabeb3SAlexandre Bounine  */
rio_request_inb_pwrite(struct rio_dev * rdev,int (* pwcback)(struct rio_dev * rdev,union rio_pw_msg * msg,int step))620e5cabeb3SAlexandre Bounine int rio_request_inb_pwrite(struct rio_dev *rdev,
621e5cabeb3SAlexandre Bounine 	int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
622e5cabeb3SAlexandre Bounine {
623e5cabeb3SAlexandre Bounine 	int rc = 0;
624e5cabeb3SAlexandre Bounine 
625e5cabeb3SAlexandre Bounine 	spin_lock(&rio_global_list_lock);
62693dd49afSMarkus Elfring 	if (rdev->pwcback)
627e5cabeb3SAlexandre Bounine 		rc = -ENOMEM;
628e5cabeb3SAlexandre Bounine 	else
629e5cabeb3SAlexandre Bounine 		rdev->pwcback = pwcback;
630e5cabeb3SAlexandre Bounine 
631e5cabeb3SAlexandre Bounine 	spin_unlock(&rio_global_list_lock);
632e5cabeb3SAlexandre Bounine 	return rc;
633e5cabeb3SAlexandre Bounine }
634e5cabeb3SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
635e5cabeb3SAlexandre Bounine 
636e5cabeb3SAlexandre Bounine /**
637e5cabeb3SAlexandre Bounine  * rio_release_inb_pwrite - release inbound port-write message service
6389a0b0627SAlexandre Bounine  *                          associated with specific RapidIO device
639e5cabeb3SAlexandre Bounine  * @rdev: RIO device which registered for inbound port-write callback
640e5cabeb3SAlexandre Bounine  *
641e5cabeb3SAlexandre Bounine  * Removes callback from the rio_dev structure. Returns 0 if the request
642e5cabeb3SAlexandre Bounine  * has been satisfied.
643e5cabeb3SAlexandre Bounine  */
rio_release_inb_pwrite(struct rio_dev * rdev)644e5cabeb3SAlexandre Bounine int rio_release_inb_pwrite(struct rio_dev *rdev)
645e5cabeb3SAlexandre Bounine {
646e5cabeb3SAlexandre Bounine 	int rc = -ENOMEM;
647e5cabeb3SAlexandre Bounine 
648e5cabeb3SAlexandre Bounine 	spin_lock(&rio_global_list_lock);
649e5cabeb3SAlexandre Bounine 	if (rdev->pwcback) {
650e5cabeb3SAlexandre Bounine 		rdev->pwcback = NULL;
651e5cabeb3SAlexandre Bounine 		rc = 0;
652e5cabeb3SAlexandre Bounine 	}
653e5cabeb3SAlexandre Bounine 
654e5cabeb3SAlexandre Bounine 	spin_unlock(&rio_global_list_lock);
655e5cabeb3SAlexandre Bounine 	return rc;
656e5cabeb3SAlexandre Bounine }
657e5cabeb3SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
658e5cabeb3SAlexandre Bounine 
659e5cabeb3SAlexandre Bounine /**
660b6cb95e8SAlexandre Bounine  * rio_pw_enable - Enables/disables port-write handling by a master port
661b6cb95e8SAlexandre Bounine  * @mport: Master port associated with port-write handling
662b6cb95e8SAlexandre Bounine  * @enable:  1=enable,  0=disable
663b6cb95e8SAlexandre Bounine  */
rio_pw_enable(struct rio_mport * mport,int enable)664b6cb95e8SAlexandre Bounine void rio_pw_enable(struct rio_mport *mport, int enable)
665b6cb95e8SAlexandre Bounine {
666b6cb95e8SAlexandre Bounine 	if (mport->ops->pwenable) {
667b6cb95e8SAlexandre Bounine 		mutex_lock(&mport->lock);
668b6cb95e8SAlexandre Bounine 
669b6cb95e8SAlexandre Bounine 		if ((enable && ++mport->pwe_refcnt == 1) ||
670b6cb95e8SAlexandre Bounine 		    (!enable && mport->pwe_refcnt && --mport->pwe_refcnt == 0))
671b6cb95e8SAlexandre Bounine 			mport->ops->pwenable(mport, enable);
672b6cb95e8SAlexandre Bounine 		mutex_unlock(&mport->lock);
673b6cb95e8SAlexandre Bounine 	}
674b6cb95e8SAlexandre Bounine }
675b6cb95e8SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_pw_enable);
676b6cb95e8SAlexandre Bounine 
677b6cb95e8SAlexandre Bounine /**
678da1589f0SAlexandre Bounine  * rio_map_inb_region -- Map inbound memory region.
679da1589f0SAlexandre Bounine  * @mport: Master port.
6802ca3cb50SRandy Dunlap  * @local: physical address of memory region to be mapped
681da1589f0SAlexandre Bounine  * @rbase: RIO base address assigned to this window
682da1589f0SAlexandre Bounine  * @size: Size of the memory region
683da1589f0SAlexandre Bounine  * @rflags: Flags for mapping.
684da1589f0SAlexandre Bounine  *
685da1589f0SAlexandre Bounine  * Return: 0 -- Success.
686da1589f0SAlexandre Bounine  *
687da1589f0SAlexandre Bounine  * This function will create the mapping from RIO space to local memory.
688da1589f0SAlexandre Bounine  */
rio_map_inb_region(struct rio_mport * mport,dma_addr_t local,u64 rbase,u32 size,u32 rflags)689da1589f0SAlexandre Bounine int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
690da1589f0SAlexandre Bounine 			u64 rbase, u32 size, u32 rflags)
691da1589f0SAlexandre Bounine {
692002f6f40SMarkus Elfring 	int rc;
693da1589f0SAlexandre Bounine 	unsigned long flags;
694da1589f0SAlexandre Bounine 
695da1589f0SAlexandre Bounine 	if (!mport->ops->map_inb)
696da1589f0SAlexandre Bounine 		return -1;
697da1589f0SAlexandre Bounine 	spin_lock_irqsave(&rio_mmap_lock, flags);
698da1589f0SAlexandre Bounine 	rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
699da1589f0SAlexandre Bounine 	spin_unlock_irqrestore(&rio_mmap_lock, flags);
700da1589f0SAlexandre Bounine 	return rc;
701da1589f0SAlexandre Bounine }
702da1589f0SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_map_inb_region);
703da1589f0SAlexandre Bounine 
704da1589f0SAlexandre Bounine /**
705da1589f0SAlexandre Bounine  * rio_unmap_inb_region -- Unmap the inbound memory region
706da1589f0SAlexandre Bounine  * @mport: Master port
707da1589f0SAlexandre Bounine  * @lstart: physical address of memory region to be unmapped
708da1589f0SAlexandre Bounine  */
rio_unmap_inb_region(struct rio_mport * mport,dma_addr_t lstart)709da1589f0SAlexandre Bounine void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
710da1589f0SAlexandre Bounine {
711da1589f0SAlexandre Bounine 	unsigned long flags;
712da1589f0SAlexandre Bounine 	if (!mport->ops->unmap_inb)
713da1589f0SAlexandre Bounine 		return;
714da1589f0SAlexandre Bounine 	spin_lock_irqsave(&rio_mmap_lock, flags);
715da1589f0SAlexandre Bounine 	mport->ops->unmap_inb(mport, lstart);
716da1589f0SAlexandre Bounine 	spin_unlock_irqrestore(&rio_mmap_lock, flags);
717da1589f0SAlexandre Bounine }
718da1589f0SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
719da1589f0SAlexandre Bounine 
720da1589f0SAlexandre Bounine /**
72193bdaca5SAlexandre Bounine  * rio_map_outb_region -- Map outbound memory region.
72293bdaca5SAlexandre Bounine  * @mport: Master port.
72393bdaca5SAlexandre Bounine  * @destid: destination id window points to
72493bdaca5SAlexandre Bounine  * @rbase: RIO base address window translates to
72593bdaca5SAlexandre Bounine  * @size: Size of the memory region
72693bdaca5SAlexandre Bounine  * @rflags: Flags for mapping.
72793bdaca5SAlexandre Bounine  * @local: physical address of memory region mapped
72893bdaca5SAlexandre Bounine  *
72993bdaca5SAlexandre Bounine  * Return: 0 -- Success.
73093bdaca5SAlexandre Bounine  *
73193bdaca5SAlexandre Bounine  * This function will create the mapping from RIO space to local memory.
73293bdaca5SAlexandre Bounine  */
rio_map_outb_region(struct rio_mport * mport,u16 destid,u64 rbase,u32 size,u32 rflags,dma_addr_t * local)73393bdaca5SAlexandre Bounine int rio_map_outb_region(struct rio_mport *mport, u16 destid, u64 rbase,
73493bdaca5SAlexandre Bounine 			u32 size, u32 rflags, dma_addr_t *local)
73593bdaca5SAlexandre Bounine {
736002f6f40SMarkus Elfring 	int rc;
73793bdaca5SAlexandre Bounine 	unsigned long flags;
73893bdaca5SAlexandre Bounine 
73993bdaca5SAlexandre Bounine 	if (!mport->ops->map_outb)
74093bdaca5SAlexandre Bounine 		return -ENODEV;
74193bdaca5SAlexandre Bounine 
74293bdaca5SAlexandre Bounine 	spin_lock_irqsave(&rio_mmap_lock, flags);
74393bdaca5SAlexandre Bounine 	rc = mport->ops->map_outb(mport, destid, rbase, size,
74493bdaca5SAlexandre Bounine 		rflags, local);
74593bdaca5SAlexandre Bounine 	spin_unlock_irqrestore(&rio_mmap_lock, flags);
74693bdaca5SAlexandre Bounine 
74793bdaca5SAlexandre Bounine 	return rc;
74893bdaca5SAlexandre Bounine }
74993bdaca5SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_map_outb_region);
75093bdaca5SAlexandre Bounine 
75193bdaca5SAlexandre Bounine /**
7523de990b0SMauro Carvalho Chehab  * rio_unmap_outb_region -- Unmap the inbound memory region
75393bdaca5SAlexandre Bounine  * @mport: Master port
75493bdaca5SAlexandre Bounine  * @destid: destination id mapping points to
75593bdaca5SAlexandre Bounine  * @rstart: RIO base address window translates to
75693bdaca5SAlexandre Bounine  */
rio_unmap_outb_region(struct rio_mport * mport,u16 destid,u64 rstart)75793bdaca5SAlexandre Bounine void rio_unmap_outb_region(struct rio_mport *mport, u16 destid, u64 rstart)
75893bdaca5SAlexandre Bounine {
75993bdaca5SAlexandre Bounine 	unsigned long flags;
76093bdaca5SAlexandre Bounine 
76193bdaca5SAlexandre Bounine 	if (!mport->ops->unmap_outb)
76293bdaca5SAlexandre Bounine 		return;
76393bdaca5SAlexandre Bounine 
76493bdaca5SAlexandre Bounine 	spin_lock_irqsave(&rio_mmap_lock, flags);
76593bdaca5SAlexandre Bounine 	mport->ops->unmap_outb(mport, destid, rstart);
76693bdaca5SAlexandre Bounine 	spin_unlock_irqrestore(&rio_mmap_lock, flags);
76793bdaca5SAlexandre Bounine }
76893bdaca5SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_unmap_outb_region);
76993bdaca5SAlexandre Bounine 
77093bdaca5SAlexandre Bounine /**
771e5cabeb3SAlexandre Bounine  * rio_mport_get_physefb - Helper function that returns register offset
772e5cabeb3SAlexandre Bounine  *                      for Physical Layer Extended Features Block.
77397ef6f74SRandy Dunlap  * @port: Master port to issue transaction
77497ef6f74SRandy Dunlap  * @local: Indicate a local master port or remote device access
77597ef6f74SRandy Dunlap  * @destid: Destination ID of the device
77697ef6f74SRandy Dunlap  * @hopcount: Number of switch hops to the device
7771ae842deSAlexandre Bounine  * @rmap: pointer to location to store register map type info
778e5cabeb3SAlexandre Bounine  */
779e5cabeb3SAlexandre Bounine u32
rio_mport_get_physefb(struct rio_mport * port,int local,u16 destid,u8 hopcount,u32 * rmap)780e5cabeb3SAlexandre Bounine rio_mport_get_physefb(struct rio_mport *port, int local,
7811ae842deSAlexandre Bounine 		      u16 destid, u8 hopcount, u32 *rmap)
782e5cabeb3SAlexandre Bounine {
783e5cabeb3SAlexandre Bounine 	u32 ext_ftr_ptr;
784e5cabeb3SAlexandre Bounine 	u32 ftr_header;
785e5cabeb3SAlexandre Bounine 
786e5cabeb3SAlexandre Bounine 	ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
787e5cabeb3SAlexandre Bounine 
788e5cabeb3SAlexandre Bounine 	while (ext_ftr_ptr)  {
789e5cabeb3SAlexandre Bounine 		if (local)
790e5cabeb3SAlexandre Bounine 			rio_local_read_config_32(port, ext_ftr_ptr,
791e5cabeb3SAlexandre Bounine 						 &ftr_header);
792e5cabeb3SAlexandre Bounine 		else
793e5cabeb3SAlexandre Bounine 			rio_mport_read_config_32(port, destid, hopcount,
794e5cabeb3SAlexandre Bounine 						 ext_ftr_ptr, &ftr_header);
795e5cabeb3SAlexandre Bounine 
796e5cabeb3SAlexandre Bounine 		ftr_header = RIO_GET_BLOCK_ID(ftr_header);
797e5cabeb3SAlexandre Bounine 		switch (ftr_header) {
798e5cabeb3SAlexandre Bounine 
799e5cabeb3SAlexandre Bounine 		case RIO_EFB_SER_EP_ID:
800e5cabeb3SAlexandre Bounine 		case RIO_EFB_SER_EP_REC_ID:
801e5cabeb3SAlexandre Bounine 		case RIO_EFB_SER_EP_FREE_ID:
8021ae842deSAlexandre Bounine 		case RIO_EFB_SER_EP_M1_ID:
8031ae842deSAlexandre Bounine 		case RIO_EFB_SER_EP_SW_M1_ID:
8041ae842deSAlexandre Bounine 		case RIO_EFB_SER_EPF_M1_ID:
8051ae842deSAlexandre Bounine 		case RIO_EFB_SER_EPF_SW_M1_ID:
8061ae842deSAlexandre Bounine 			*rmap = 1;
8071ae842deSAlexandre Bounine 			return ext_ftr_ptr;
808e5cabeb3SAlexandre Bounine 
8091ae842deSAlexandre Bounine 		case RIO_EFB_SER_EP_M2_ID:
8101ae842deSAlexandre Bounine 		case RIO_EFB_SER_EP_SW_M2_ID:
8111ae842deSAlexandre Bounine 		case RIO_EFB_SER_EPF_M2_ID:
8121ae842deSAlexandre Bounine 		case RIO_EFB_SER_EPF_SW_M2_ID:
8131ae842deSAlexandre Bounine 			*rmap = 2;
814e5cabeb3SAlexandre Bounine 			return ext_ftr_ptr;
815e5cabeb3SAlexandre Bounine 
816e5cabeb3SAlexandre Bounine 		default:
817e5cabeb3SAlexandre Bounine 			break;
818e5cabeb3SAlexandre Bounine 		}
819e5cabeb3SAlexandre Bounine 
820e5cabeb3SAlexandre Bounine 		ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
821e5cabeb3SAlexandre Bounine 						hopcount, ext_ftr_ptr);
822e5cabeb3SAlexandre Bounine 	}
823e5cabeb3SAlexandre Bounine 
824e5cabeb3SAlexandre Bounine 	return ext_ftr_ptr;
825e5cabeb3SAlexandre Bounine }
826a11650e1SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
827e5cabeb3SAlexandre Bounine 
828e5cabeb3SAlexandre Bounine /**
829e5cabeb3SAlexandre Bounine  * rio_get_comptag - Begin or continue searching for a RIO device by component tag
83097ef6f74SRandy Dunlap  * @comp_tag: RIO component tag to match
831e5cabeb3SAlexandre Bounine  * @from: Previous RIO device found in search, or %NULL for new search
832e5cabeb3SAlexandre Bounine  *
833e5cabeb3SAlexandre Bounine  * Iterates through the list of known RIO devices. If a RIO device is
834e5cabeb3SAlexandre Bounine  * found with a matching @comp_tag, a pointer to its device
835e5cabeb3SAlexandre Bounine  * structure is returned. Otherwise, %NULL is returned. A new search
836e5cabeb3SAlexandre Bounine  * is initiated by passing %NULL to the @from argument. Otherwise, if
837e5cabeb3SAlexandre Bounine  * @from is not %NULL, searches continue from next device on the global
838e5cabeb3SAlexandre Bounine  * list.
839e5cabeb3SAlexandre Bounine  */
rio_get_comptag(u32 comp_tag,struct rio_dev * from)840af84ca38SAlexandre Bounine struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
841e5cabeb3SAlexandre Bounine {
842e5cabeb3SAlexandre Bounine 	struct list_head *n;
843e5cabeb3SAlexandre Bounine 	struct rio_dev *rdev;
844e5cabeb3SAlexandre Bounine 
845e5cabeb3SAlexandre Bounine 	spin_lock(&rio_global_list_lock);
846e5cabeb3SAlexandre Bounine 	n = from ? from->global_list.next : rio_devices.next;
847e5cabeb3SAlexandre Bounine 
848e5cabeb3SAlexandre Bounine 	while (n && (n != &rio_devices)) {
849e5cabeb3SAlexandre Bounine 		rdev = rio_dev_g(n);
850e5cabeb3SAlexandre Bounine 		if (rdev->comp_tag == comp_tag)
851e5cabeb3SAlexandre Bounine 			goto exit;
852e5cabeb3SAlexandre Bounine 		n = n->next;
853e5cabeb3SAlexandre Bounine 	}
854e5cabeb3SAlexandre Bounine 	rdev = NULL;
855e5cabeb3SAlexandre Bounine exit:
856e5cabeb3SAlexandre Bounine 	spin_unlock(&rio_global_list_lock);
857e5cabeb3SAlexandre Bounine 	return rdev;
858e5cabeb3SAlexandre Bounine }
859a11650e1SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_get_comptag);
860e5cabeb3SAlexandre Bounine 
861e5cabeb3SAlexandre Bounine /**
862e5cabeb3SAlexandre Bounine  * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
863e5cabeb3SAlexandre Bounine  * @rdev: Pointer to RIO device control structure
864e5cabeb3SAlexandre Bounine  * @pnum: Switch port number to set LOCKOUT bit
865e5cabeb3SAlexandre Bounine  * @lock: Operation : set (=1) or clear (=0)
866e5cabeb3SAlexandre Bounine  */
rio_set_port_lockout(struct rio_dev * rdev,u32 pnum,int lock)867e5cabeb3SAlexandre Bounine int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
868e5cabeb3SAlexandre Bounine {
869e5cabeb3SAlexandre Bounine 	u32 regval;
870e5cabeb3SAlexandre Bounine 
871a93192a5SAlexandre Bounine 	rio_read_config_32(rdev,
8721ae842deSAlexandre Bounine 		RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
873e5cabeb3SAlexandre Bounine 		&regval);
874e5cabeb3SAlexandre Bounine 	if (lock)
875e5cabeb3SAlexandre Bounine 		regval |= RIO_PORT_N_CTL_LOCKOUT;
876e5cabeb3SAlexandre Bounine 	else
877e5cabeb3SAlexandre Bounine 		regval &= ~RIO_PORT_N_CTL_LOCKOUT;
878e5cabeb3SAlexandre Bounine 
879a93192a5SAlexandre Bounine 	rio_write_config_32(rdev,
8801ae842deSAlexandre Bounine 		RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
881e5cabeb3SAlexandre Bounine 		regval);
882e5cabeb3SAlexandre Bounine 	return 0;
883e5cabeb3SAlexandre Bounine }
884a11650e1SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_set_port_lockout);
885a11650e1SAlexandre Bounine 
886a11650e1SAlexandre Bounine /**
887a11650e1SAlexandre Bounine  * rio_enable_rx_tx_port - enable input receiver and output transmitter of
888a11650e1SAlexandre Bounine  * given port
889a11650e1SAlexandre Bounine  * @port: Master port associated with the RIO network
890a11650e1SAlexandre Bounine  * @local: local=1 select local port otherwise a far device is reached
891a11650e1SAlexandre Bounine  * @destid: Destination ID of the device to check host bit
892a11650e1SAlexandre Bounine  * @hopcount: Number of hops to reach the target
893a11650e1SAlexandre Bounine  * @port_num: Port (-number on switch) to enable on a far end device
894a11650e1SAlexandre Bounine  *
895a11650e1SAlexandre Bounine  * Returns 0 or 1 from on General Control Command and Status Register
896a11650e1SAlexandre Bounine  * (EXT_PTR+0x3C)
897a11650e1SAlexandre Bounine  */
rio_enable_rx_tx_port(struct rio_mport * port,int local,u16 destid,u8 hopcount,u8 port_num)898a11650e1SAlexandre Bounine int rio_enable_rx_tx_port(struct rio_mport *port,
899a11650e1SAlexandre Bounine 			  int local, u16 destid,
900a11650e1SAlexandre Bounine 			  u8 hopcount, u8 port_num)
901a11650e1SAlexandre Bounine {
902a11650e1SAlexandre Bounine #ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
903a11650e1SAlexandre Bounine 	u32 regval;
904a11650e1SAlexandre Bounine 	u32 ext_ftr_ptr;
9051ae842deSAlexandre Bounine 	u32 rmap;
906a11650e1SAlexandre Bounine 
907a11650e1SAlexandre Bounine 	/*
908a11650e1SAlexandre Bounine 	* enable rx input tx output port
909a11650e1SAlexandre Bounine 	*/
910a11650e1SAlexandre Bounine 	pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
911a11650e1SAlexandre Bounine 		 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
912a11650e1SAlexandre Bounine 
9131ae842deSAlexandre Bounine 	ext_ftr_ptr = rio_mport_get_physefb(port, local, destid,
9141ae842deSAlexandre Bounine 					    hopcount, &rmap);
915a11650e1SAlexandre Bounine 
916a11650e1SAlexandre Bounine 	if (local) {
9171ae842deSAlexandre Bounine 		rio_local_read_config_32(port,
9181ae842deSAlexandre Bounine 				ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap),
919a11650e1SAlexandre Bounine 				&regval);
920a11650e1SAlexandre Bounine 	} else {
921a11650e1SAlexandre Bounine 		if (rio_mport_read_config_32(port, destid, hopcount,
9221ae842deSAlexandre Bounine 			ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
9231ae842deSAlexandre Bounine 				&regval) < 0)
924a11650e1SAlexandre Bounine 			return -EIO;
925a11650e1SAlexandre Bounine 	}
926a11650e1SAlexandre Bounine 
9271ae842deSAlexandre Bounine 	regval = regval | RIO_PORT_N_CTL_EN_RX | RIO_PORT_N_CTL_EN_TX;
928a11650e1SAlexandre Bounine 
929a11650e1SAlexandre Bounine 	if (local) {
9301ae842deSAlexandre Bounine 		rio_local_write_config_32(port,
9311ae842deSAlexandre Bounine 			ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap), regval);
932a11650e1SAlexandre Bounine 	} else {
933a11650e1SAlexandre Bounine 		if (rio_mport_write_config_32(port, destid, hopcount,
9341ae842deSAlexandre Bounine 			ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
9351ae842deSAlexandre Bounine 				regval) < 0)
936a11650e1SAlexandre Bounine 			return -EIO;
937a11650e1SAlexandre Bounine 	}
938a11650e1SAlexandre Bounine #endif
939a11650e1SAlexandre Bounine 	return 0;
940a11650e1SAlexandre Bounine }
941a11650e1SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
942a11650e1SAlexandre Bounine 
943e5cabeb3SAlexandre Bounine 
944e5cabeb3SAlexandre Bounine /**
9456429cd49SAlexandre Bounine  * rio_chk_dev_route - Validate route to the specified device.
9466429cd49SAlexandre Bounine  * @rdev:  RIO device failed to respond
9476429cd49SAlexandre Bounine  * @nrdev: Last active device on the route to rdev
9486429cd49SAlexandre Bounine  * @npnum: nrdev's port number on the route to rdev
9496429cd49SAlexandre Bounine  *
9506429cd49SAlexandre Bounine  * Follows a route to the specified RIO device to determine the last available
9516429cd49SAlexandre Bounine  * device (and corresponding RIO port) on the route.
9526429cd49SAlexandre Bounine  */
9536429cd49SAlexandre Bounine static int
rio_chk_dev_route(struct rio_dev * rdev,struct rio_dev ** nrdev,int * npnum)9546429cd49SAlexandre Bounine rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
9556429cd49SAlexandre Bounine {
9566429cd49SAlexandre Bounine 	u32 result;
957a93192a5SAlexandre Bounine 	int p_port, rc = -EIO;
9586429cd49SAlexandre Bounine 	struct rio_dev *prev = NULL;
9596429cd49SAlexandre Bounine 
9606429cd49SAlexandre Bounine 	/* Find switch with failed RIO link */
9616429cd49SAlexandre Bounine 	while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
9626429cd49SAlexandre Bounine 		if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
9636429cd49SAlexandre Bounine 			prev = rdev->prev;
9646429cd49SAlexandre Bounine 			break;
9656429cd49SAlexandre Bounine 		}
9666429cd49SAlexandre Bounine 		rdev = rdev->prev;
9676429cd49SAlexandre Bounine 	}
9686429cd49SAlexandre Bounine 
96993dd49afSMarkus Elfring 	if (!prev)
9706429cd49SAlexandre Bounine 		goto err_out;
9716429cd49SAlexandre Bounine 
972a93192a5SAlexandre Bounine 	p_port = prev->rswitch->route_table[rdev->destid];
9736429cd49SAlexandre Bounine 
974af84ca38SAlexandre Bounine 	if (p_port != RIO_INVALID_ROUTE) {
9756429cd49SAlexandre Bounine 		pr_debug("RIO: link failed on [%s]-P%d\n",
9766429cd49SAlexandre Bounine 			 rio_name(prev), p_port);
9776429cd49SAlexandre Bounine 		*nrdev = prev;
9786429cd49SAlexandre Bounine 		*npnum = p_port;
9796429cd49SAlexandre Bounine 		rc = 0;
9806429cd49SAlexandre Bounine 	} else
981af84ca38SAlexandre Bounine 		pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
9826429cd49SAlexandre Bounine err_out:
9836429cd49SAlexandre Bounine 	return rc;
9846429cd49SAlexandre Bounine }
9856429cd49SAlexandre Bounine 
9866429cd49SAlexandre Bounine /**
9876429cd49SAlexandre Bounine  * rio_mport_chk_dev_access - Validate access to the specified device.
9886429cd49SAlexandre Bounine  * @mport: Master port to send transactions
9896429cd49SAlexandre Bounine  * @destid: Device destination ID in network
9906429cd49SAlexandre Bounine  * @hopcount: Number of hops into the network
9916429cd49SAlexandre Bounine  */
992e274e0edSAlexandre Bounine int
rio_mport_chk_dev_access(struct rio_mport * mport,u16 destid,u8 hopcount)9936429cd49SAlexandre Bounine rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
9946429cd49SAlexandre Bounine {
9956429cd49SAlexandre Bounine 	int i = 0;
9966429cd49SAlexandre Bounine 	u32 tmp;
9976429cd49SAlexandre Bounine 
9986429cd49SAlexandre Bounine 	while (rio_mport_read_config_32(mport, destid, hopcount,
9996429cd49SAlexandre Bounine 					RIO_DEV_ID_CAR, &tmp)) {
10006429cd49SAlexandre Bounine 		i++;
10016429cd49SAlexandre Bounine 		if (i == RIO_MAX_CHK_RETRY)
10026429cd49SAlexandre Bounine 			return -EIO;
10036429cd49SAlexandre Bounine 		mdelay(1);
10046429cd49SAlexandre Bounine 	}
10056429cd49SAlexandre Bounine 
10066429cd49SAlexandre Bounine 	return 0;
10076429cd49SAlexandre Bounine }
1008a11650e1SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
10096429cd49SAlexandre Bounine 
10106429cd49SAlexandre Bounine /**
10116429cd49SAlexandre Bounine  * rio_chk_dev_access - Validate access to the specified device.
10126429cd49SAlexandre Bounine  * @rdev: Pointer to RIO device control structure
10136429cd49SAlexandre Bounine  */
rio_chk_dev_access(struct rio_dev * rdev)10146429cd49SAlexandre Bounine static int rio_chk_dev_access(struct rio_dev *rdev)
10156429cd49SAlexandre Bounine {
1016a93192a5SAlexandre Bounine 	return rio_mport_chk_dev_access(rdev->net->hport,
1017a93192a5SAlexandre Bounine 					rdev->destid, rdev->hopcount);
10186429cd49SAlexandre Bounine }
10196429cd49SAlexandre Bounine 
10206429cd49SAlexandre Bounine /**
1021dd5648c9SAlexandre Bounine  * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
1022dd5648c9SAlexandre Bounine  *                        returns link-response (if requested).
1023dd5648c9SAlexandre Bounine  * @rdev: RIO devive to issue Input-status command
1024dd5648c9SAlexandre Bounine  * @pnum: Device port number to issue the command
1025dd5648c9SAlexandre Bounine  * @lnkresp: Response from a link partner
1026dd5648c9SAlexandre Bounine  */
1027dd5648c9SAlexandre Bounine static int
rio_get_input_status(struct rio_dev * rdev,int pnum,u32 * lnkresp)1028dd5648c9SAlexandre Bounine rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
1029dd5648c9SAlexandre Bounine {
1030dd5648c9SAlexandre Bounine 	u32 regval;
1031dd5648c9SAlexandre Bounine 	int checkcount;
1032dd5648c9SAlexandre Bounine 
1033dd5648c9SAlexandre Bounine 	if (lnkresp) {
1034dd5648c9SAlexandre Bounine 		/* Read from link maintenance response register
1035dd5648c9SAlexandre Bounine 		 * to clear valid bit */
1036a93192a5SAlexandre Bounine 		rio_read_config_32(rdev,
10371ae842deSAlexandre Bounine 			RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
1038dd5648c9SAlexandre Bounine 			&regval);
1039dd5648c9SAlexandre Bounine 		udelay(50);
1040dd5648c9SAlexandre Bounine 	}
1041dd5648c9SAlexandre Bounine 
1042dd5648c9SAlexandre Bounine 	/* Issue Input-status command */
1043a93192a5SAlexandre Bounine 	rio_write_config_32(rdev,
10441ae842deSAlexandre Bounine 		RIO_DEV_PORT_N_MNT_REQ_CSR(rdev, pnum),
1045dd5648c9SAlexandre Bounine 		RIO_MNT_REQ_CMD_IS);
1046dd5648c9SAlexandre Bounine 
1047dd5648c9SAlexandre Bounine 	/* Exit if the response is not expected */
104893dd49afSMarkus Elfring 	if (!lnkresp)
1049dd5648c9SAlexandre Bounine 		return 0;
1050dd5648c9SAlexandre Bounine 
1051dd5648c9SAlexandre Bounine 	checkcount = 3;
1052dd5648c9SAlexandre Bounine 	while (checkcount--) {
1053dd5648c9SAlexandre Bounine 		udelay(50);
1054a93192a5SAlexandre Bounine 		rio_read_config_32(rdev,
10551ae842deSAlexandre Bounine 			RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
1056dd5648c9SAlexandre Bounine 			&regval);
1057dd5648c9SAlexandre Bounine 		if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
1058dd5648c9SAlexandre Bounine 			*lnkresp = regval;
1059dd5648c9SAlexandre Bounine 			return 0;
1060dd5648c9SAlexandre Bounine 		}
1061dd5648c9SAlexandre Bounine 	}
1062dd5648c9SAlexandre Bounine 
1063dd5648c9SAlexandre Bounine 	return -EIO;
1064dd5648c9SAlexandre Bounine }
1065dd5648c9SAlexandre Bounine 
1066dd5648c9SAlexandre Bounine /**
1067dd5648c9SAlexandre Bounine  * rio_clr_err_stopped - Clears port Error-stopped states.
1068dd5648c9SAlexandre Bounine  * @rdev: Pointer to RIO device control structure
1069dd5648c9SAlexandre Bounine  * @pnum: Switch port number to clear errors
1070dd5648c9SAlexandre Bounine  * @err_status: port error status (if 0 reads register from device)
10711ae842deSAlexandre Bounine  *
10721ae842deSAlexandre Bounine  * TODO: Currently this routine is not compatible with recovery process
10731ae842deSAlexandre Bounine  * specified for idt_gen3 RapidIO switch devices. It has to be reviewed
10741ae842deSAlexandre Bounine  * to implement universal recovery process that is compatible full range
10751ae842deSAlexandre Bounine  * off available devices.
10761ae842deSAlexandre Bounine  * IDT gen3 switch driver now implements HW-specific error handler that
10771ae842deSAlexandre Bounine  * issues soft port reset to the port to reset ERR_STOP bits and ackIDs.
1078dd5648c9SAlexandre Bounine  */
rio_clr_err_stopped(struct rio_dev * rdev,u32 pnum,u32 err_status)1079dd5648c9SAlexandre Bounine static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
1080dd5648c9SAlexandre Bounine {
1081dd5648c9SAlexandre Bounine 	struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
1082dd5648c9SAlexandre Bounine 	u32 regval;
1083dd5648c9SAlexandre Bounine 	u32 far_ackid, far_linkstat, near_ackid;
1084dd5648c9SAlexandre Bounine 
1085dd5648c9SAlexandre Bounine 	if (err_status == 0)
1086a93192a5SAlexandre Bounine 		rio_read_config_32(rdev,
10871ae842deSAlexandre Bounine 			RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1088dd5648c9SAlexandre Bounine 			&err_status);
1089dd5648c9SAlexandre Bounine 
10901ae842deSAlexandre Bounine 	if (err_status & RIO_PORT_N_ERR_STS_OUT_ES) {
1091dd5648c9SAlexandre Bounine 		pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
1092dd5648c9SAlexandre Bounine 		/*
1093dd5648c9SAlexandre Bounine 		 * Send a Link-Request/Input-Status control symbol
1094dd5648c9SAlexandre Bounine 		 */
1095dd5648c9SAlexandre Bounine 		if (rio_get_input_status(rdev, pnum, &regval)) {
1096dd5648c9SAlexandre Bounine 			pr_debug("RIO_EM: Input-status response timeout\n");
1097dd5648c9SAlexandre Bounine 			goto rd_err;
1098dd5648c9SAlexandre Bounine 		}
1099dd5648c9SAlexandre Bounine 
1100dd5648c9SAlexandre Bounine 		pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
1101dd5648c9SAlexandre Bounine 			 pnum, regval);
1102dd5648c9SAlexandre Bounine 		far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
1103dd5648c9SAlexandre Bounine 		far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
1104a93192a5SAlexandre Bounine 		rio_read_config_32(rdev,
11051ae842deSAlexandre Bounine 			RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
1106dd5648c9SAlexandre Bounine 			&regval);
1107dd5648c9SAlexandre Bounine 		pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
1108dd5648c9SAlexandre Bounine 		near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
1109dd5648c9SAlexandre Bounine 		pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
1110dd5648c9SAlexandre Bounine 			 " near_ackID=0x%02x\n",
1111dd5648c9SAlexandre Bounine 			pnum, far_ackid, far_linkstat, near_ackid);
1112dd5648c9SAlexandre Bounine 
1113dd5648c9SAlexandre Bounine 		/*
1114dd5648c9SAlexandre Bounine 		 * If required, synchronize ackIDs of near and
1115dd5648c9SAlexandre Bounine 		 * far sides.
1116dd5648c9SAlexandre Bounine 		 */
1117dd5648c9SAlexandre Bounine 		if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
1118dd5648c9SAlexandre Bounine 		    (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
1119dd5648c9SAlexandre Bounine 			/* Align near outstanding/outbound ackIDs with
1120dd5648c9SAlexandre Bounine 			 * far inbound.
1121dd5648c9SAlexandre Bounine 			 */
1122a93192a5SAlexandre Bounine 			rio_write_config_32(rdev,
11231ae842deSAlexandre Bounine 				RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
1124dd5648c9SAlexandre Bounine 				(near_ackid << 24) |
1125dd5648c9SAlexandre Bounine 					(far_ackid << 8) | far_ackid);
1126dd5648c9SAlexandre Bounine 			/* Align far outstanding/outbound ackIDs with
1127dd5648c9SAlexandre Bounine 			 * near inbound.
1128dd5648c9SAlexandre Bounine 			 */
1129dd5648c9SAlexandre Bounine 			far_ackid++;
11301ae842deSAlexandre Bounine 			if (!nextdev) {
11311ae842deSAlexandre Bounine 				pr_debug("RIO_EM: nextdev pointer == NULL\n");
11321ae842deSAlexandre Bounine 				goto rd_err;
11331ae842deSAlexandre Bounine 			}
11341ae842deSAlexandre Bounine 
1135dd5648c9SAlexandre Bounine 			rio_write_config_32(nextdev,
11361ae842deSAlexandre Bounine 				RIO_DEV_PORT_N_ACK_STS_CSR(nextdev,
11371ae842deSAlexandre Bounine 					RIO_GET_PORT_NUM(nextdev->swpinfo)),
1138dd5648c9SAlexandre Bounine 				(far_ackid << 24) |
1139dd5648c9SAlexandre Bounine 				(near_ackid << 8) | near_ackid);
1140dd5648c9SAlexandre Bounine 		}
1141dd5648c9SAlexandre Bounine rd_err:
11421ae842deSAlexandre Bounine 		rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1143dd5648c9SAlexandre Bounine 				   &err_status);
1144dd5648c9SAlexandre Bounine 		pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1145dd5648c9SAlexandre Bounine 	}
1146dd5648c9SAlexandre Bounine 
11471ae842deSAlexandre Bounine 	if ((err_status & RIO_PORT_N_ERR_STS_INP_ES) && nextdev) {
1148dd5648c9SAlexandre Bounine 		pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
1149dd5648c9SAlexandre Bounine 		rio_get_input_status(nextdev,
1150dd5648c9SAlexandre Bounine 				     RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
1151dd5648c9SAlexandre Bounine 		udelay(50);
1152dd5648c9SAlexandre Bounine 
11531ae842deSAlexandre Bounine 		rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1154dd5648c9SAlexandre Bounine 				   &err_status);
1155dd5648c9SAlexandre Bounine 		pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1156dd5648c9SAlexandre Bounine 	}
1157dd5648c9SAlexandre Bounine 
11581ae842deSAlexandre Bounine 	return (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
11591ae842deSAlexandre Bounine 			      RIO_PORT_N_ERR_STS_INP_ES)) ? 1 : 0;
1160dd5648c9SAlexandre Bounine }
1161dd5648c9SAlexandre Bounine 
1162dd5648c9SAlexandre Bounine /**
11639a0b0627SAlexandre Bounine  * rio_inb_pwrite_handler - inbound port-write message handler
11649a0b0627SAlexandre Bounine  * @mport:  mport device associated with port-write
1165e5cabeb3SAlexandre Bounine  * @pw_msg: pointer to inbound port-write message
1166e5cabeb3SAlexandre Bounine  *
1167e5cabeb3SAlexandre Bounine  * Processes an inbound port-write message. Returns 0 if the request
1168e5cabeb3SAlexandre Bounine  * has been satisfied.
1169e5cabeb3SAlexandre Bounine  */
rio_inb_pwrite_handler(struct rio_mport * mport,union rio_pw_msg * pw_msg)11709a0b0627SAlexandre Bounine int rio_inb_pwrite_handler(struct rio_mport *mport, union rio_pw_msg *pw_msg)
1171e5cabeb3SAlexandre Bounine {
1172e5cabeb3SAlexandre Bounine 	struct rio_dev *rdev;
1173dd5648c9SAlexandre Bounine 	u32 err_status, em_perrdet, em_ltlerrdet;
1174e5cabeb3SAlexandre Bounine 	int rc, portnum;
11759a0b0627SAlexandre Bounine 	struct rio_pwrite *pwrite;
1176e5cabeb3SAlexandre Bounine 
1177e5cabeb3SAlexandre Bounine #ifdef DEBUG_PW
1178e5cabeb3SAlexandre Bounine 	{
1179e5cabeb3SAlexandre Bounine 		u32 i;
11809a0b0627SAlexandre Bounine 
11819a0b0627SAlexandre Bounine 		pr_debug("%s: PW to mport_%d:\n", __func__, mport->id);
11829a0b0627SAlexandre Bounine 		for (i = 0; i < RIO_PW_MSG_SIZE / sizeof(u32); i = i + 4) {
1183dd5648c9SAlexandre Bounine 			pr_debug("0x%02x: %08x %08x %08x %08x\n",
1184e5cabeb3SAlexandre Bounine 				i * 4, pw_msg->raw[i], pw_msg->raw[i + 1],
1185e5cabeb3SAlexandre Bounine 				pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
1186e5cabeb3SAlexandre Bounine 		}
1187e5cabeb3SAlexandre Bounine 	}
1188e5cabeb3SAlexandre Bounine #endif
1189e5cabeb3SAlexandre Bounine 
11909a0b0627SAlexandre Bounine 	rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
11919a0b0627SAlexandre Bounine 	if (rdev) {
11929a0b0627SAlexandre Bounine 		pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
11939a0b0627SAlexandre Bounine 	} else {
11949a0b0627SAlexandre Bounine 		pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
11959a0b0627SAlexandre Bounine 			__func__, pw_msg->em.comptag);
11969a0b0627SAlexandre Bounine 	}
11979a0b0627SAlexandre Bounine 
11989a0b0627SAlexandre Bounine 	/* Call a device-specific handler (if it is registered for the device).
11999a0b0627SAlexandre Bounine 	 * This may be the service for endpoints that send device-specific
12009a0b0627SAlexandre Bounine 	 * port-write messages. End-point messages expected to be handled
12019a0b0627SAlexandre Bounine 	 * completely by EP specific device driver.
1202e5cabeb3SAlexandre Bounine 	 * For switches rc==0 signals that no standard processing required.
1203e5cabeb3SAlexandre Bounine 	 */
12049a0b0627SAlexandre Bounine 	if (rdev && rdev->pwcback) {
1205e5cabeb3SAlexandre Bounine 		rc = rdev->pwcback(rdev, pw_msg, 0);
1206e5cabeb3SAlexandre Bounine 		if (rc == 0)
1207e5cabeb3SAlexandre Bounine 			return 0;
1208e5cabeb3SAlexandre Bounine 	}
1209e5cabeb3SAlexandre Bounine 
12109a0b0627SAlexandre Bounine 	mutex_lock(&mport->lock);
12119a0b0627SAlexandre Bounine 	list_for_each_entry(pwrite, &mport->pwrites, node)
12129a0b0627SAlexandre Bounine 		pwrite->pwcback(mport, pwrite->context, pw_msg, 0);
12139a0b0627SAlexandre Bounine 	mutex_unlock(&mport->lock);
12149a0b0627SAlexandre Bounine 
12159a0b0627SAlexandre Bounine 	if (!rdev)
12169a0b0627SAlexandre Bounine 		return 0;
12179a0b0627SAlexandre Bounine 
12189a0b0627SAlexandre Bounine 	/*
12199a0b0627SAlexandre Bounine 	 * FIXME: The code below stays as it was before for now until we decide
12209a0b0627SAlexandre Bounine 	 * how to do default PW handling in combination with per-mport callbacks
12219a0b0627SAlexandre Bounine 	 */
12229a0b0627SAlexandre Bounine 
12236429cd49SAlexandre Bounine 	portnum = pw_msg->em.is_port & 0xFF;
12246429cd49SAlexandre Bounine 
12256429cd49SAlexandre Bounine 	/* Check if device and route to it are functional:
12266429cd49SAlexandre Bounine 	 * Sometimes devices may send PW message(s) just before being
12276429cd49SAlexandre Bounine 	 * powered down (or link being lost).
12286429cd49SAlexandre Bounine 	 */
12296429cd49SAlexandre Bounine 	if (rio_chk_dev_access(rdev)) {
12306429cd49SAlexandre Bounine 		pr_debug("RIO: device access failed - get link partner\n");
12316429cd49SAlexandre Bounine 		/* Scan route to the device and identify failed link.
12326429cd49SAlexandre Bounine 		 * This will replace device and port reported in PW message.
12336429cd49SAlexandre Bounine 		 * PW message should not be used after this point.
12346429cd49SAlexandre Bounine 		 */
12356429cd49SAlexandre Bounine 		if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
12366429cd49SAlexandre Bounine 			pr_err("RIO: Route trace for %s failed\n",
12376429cd49SAlexandre Bounine 				rio_name(rdev));
12386429cd49SAlexandre Bounine 			return -EIO;
12396429cd49SAlexandre Bounine 		}
12406429cd49SAlexandre Bounine 		pw_msg = NULL;
12416429cd49SAlexandre Bounine 	}
12426429cd49SAlexandre Bounine 
1243e5cabeb3SAlexandre Bounine 	/* For End-point devices processing stops here */
1244e5cabeb3SAlexandre Bounine 	if (!(rdev->pef & RIO_PEF_SWITCH))
1245e5cabeb3SAlexandre Bounine 		return 0;
1246e5cabeb3SAlexandre Bounine 
1247e5cabeb3SAlexandre Bounine 	if (rdev->phys_efptr == 0) {
1248e5cabeb3SAlexandre Bounine 		pr_err("RIO_PW: Bad switch initialization for %s\n",
1249e5cabeb3SAlexandre Bounine 			rio_name(rdev));
1250e5cabeb3SAlexandre Bounine 		return 0;
1251e5cabeb3SAlexandre Bounine 	}
1252e5cabeb3SAlexandre Bounine 
1253e5cabeb3SAlexandre Bounine 	/*
1254e5cabeb3SAlexandre Bounine 	 * Process the port-write notification from switch
1255e5cabeb3SAlexandre Bounine 	 */
12562ec3ba69SAlexandre Bounine 	if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
12572ec3ba69SAlexandre Bounine 		rdev->rswitch->ops->em_handle(rdev, portnum);
1258e5cabeb3SAlexandre Bounine 
12591ae842deSAlexandre Bounine 	rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1260e5cabeb3SAlexandre Bounine 			   &err_status);
1261e5cabeb3SAlexandre Bounine 	pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
1262e5cabeb3SAlexandre Bounine 
1263dd5648c9SAlexandre Bounine 	if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
1264dd5648c9SAlexandre Bounine 
1265dd5648c9SAlexandre Bounine 		if (!(rdev->rswitch->port_ok & (1 << portnum))) {
1266dd5648c9SAlexandre Bounine 			rdev->rswitch->port_ok |= (1 << portnum);
1267dd5648c9SAlexandre Bounine 			rio_set_port_lockout(rdev, portnum, 0);
1268dd5648c9SAlexandre Bounine 			/* Schedule Insertion Service */
1269dd5648c9SAlexandre Bounine 			pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
1270dd5648c9SAlexandre Bounine 			       rio_name(rdev), portnum);
1271e5cabeb3SAlexandre Bounine 		}
1272e5cabeb3SAlexandre Bounine 
1273dd5648c9SAlexandre Bounine 		/* Clear error-stopped states (if reported).
1274dd5648c9SAlexandre Bounine 		 * Depending on the link partner state, two attempts
1275dd5648c9SAlexandre Bounine 		 * may be needed for successful recovery.
1276dd5648c9SAlexandre Bounine 		 */
12771ae842deSAlexandre Bounine 		if (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
12781ae842deSAlexandre Bounine 				  RIO_PORT_N_ERR_STS_INP_ES)) {
1279dd5648c9SAlexandre Bounine 			if (rio_clr_err_stopped(rdev, portnum, err_status))
1280dd5648c9SAlexandre Bounine 				rio_clr_err_stopped(rdev, portnum, 0);
1281e5cabeb3SAlexandre Bounine 		}
1282dd5648c9SAlexandre Bounine 	}  else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
1283e5cabeb3SAlexandre Bounine 
1284e5cabeb3SAlexandre Bounine 		if (rdev->rswitch->port_ok & (1 << portnum)) {
1285e5cabeb3SAlexandre Bounine 			rdev->rswitch->port_ok &= ~(1 << portnum);
1286e5cabeb3SAlexandre Bounine 			rio_set_port_lockout(rdev, portnum, 1);
1287e5cabeb3SAlexandre Bounine 
12881ae842deSAlexandre Bounine 			if (rdev->phys_rmap == 1) {
1289a93192a5SAlexandre Bounine 			rio_write_config_32(rdev,
12901ae842deSAlexandre Bounine 				RIO_DEV_PORT_N_ACK_STS_CSR(rdev, portnum),
1291e5cabeb3SAlexandre Bounine 				RIO_PORT_N_ACK_CLEAR);
12921ae842deSAlexandre Bounine 			} else {
12931ae842deSAlexandre Bounine 				rio_write_config_32(rdev,
12941ae842deSAlexandre Bounine 					RIO_DEV_PORT_N_OB_ACK_CSR(rdev, portnum),
12951ae842deSAlexandre Bounine 					RIO_PORT_N_OB_ACK_CLEAR);
12961ae842deSAlexandre Bounine 				rio_write_config_32(rdev,
12971ae842deSAlexandre Bounine 					RIO_DEV_PORT_N_IB_ACK_CSR(rdev, portnum),
12981ae842deSAlexandre Bounine 					0);
12991ae842deSAlexandre Bounine 			}
1300e5cabeb3SAlexandre Bounine 
1301e5cabeb3SAlexandre Bounine 			/* Schedule Extraction Service */
1302e5cabeb3SAlexandre Bounine 			pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
1303e5cabeb3SAlexandre Bounine 			       rio_name(rdev), portnum);
1304e5cabeb3SAlexandre Bounine 		}
1305dd5648c9SAlexandre Bounine 	}
1306e5cabeb3SAlexandre Bounine 
1307a93192a5SAlexandre Bounine 	rio_read_config_32(rdev,
1308dd5648c9SAlexandre Bounine 		rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
1309dd5648c9SAlexandre Bounine 	if (em_perrdet) {
1310dd5648c9SAlexandre Bounine 		pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
1311dd5648c9SAlexandre Bounine 			 portnum, em_perrdet);
1312dd5648c9SAlexandre Bounine 		/* Clear EM Port N Error Detect CSR */
1313a93192a5SAlexandre Bounine 		rio_write_config_32(rdev,
1314dd5648c9SAlexandre Bounine 			rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
1315e5cabeb3SAlexandre Bounine 	}
1316dd5648c9SAlexandre Bounine 
1317a93192a5SAlexandre Bounine 	rio_read_config_32(rdev,
1318dd5648c9SAlexandre Bounine 		rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1319dd5648c9SAlexandre Bounine 	if (em_ltlerrdet) {
1320dd5648c9SAlexandre Bounine 		pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1321dd5648c9SAlexandre Bounine 			 em_ltlerrdet);
1322dd5648c9SAlexandre Bounine 		/* Clear EM L/T Layer Error Detect CSR */
1323a93192a5SAlexandre Bounine 		rio_write_config_32(rdev,
1324dd5648c9SAlexandre Bounine 			rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1325e5cabeb3SAlexandre Bounine 	}
1326e5cabeb3SAlexandre Bounine 
1327388c45ccSAlexandre Bounine 	/* Clear remaining error bits and Port-Write Pending bit */
13281ae842deSAlexandre Bounine 	rio_write_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1329388c45ccSAlexandre Bounine 			    err_status);
1330e5cabeb3SAlexandre Bounine 
1331e5cabeb3SAlexandre Bounine 	return 0;
1332e5cabeb3SAlexandre Bounine }
1333e5cabeb3SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1334e5cabeb3SAlexandre Bounine 
1335e5cabeb3SAlexandre Bounine /**
1336e5cabeb3SAlexandre Bounine  * rio_mport_get_efb - get pointer to next extended features block
1337e5cabeb3SAlexandre Bounine  * @port: Master port to issue transaction
1338e5cabeb3SAlexandre Bounine  * @local: Indicate a local master port or remote device access
1339e5cabeb3SAlexandre Bounine  * @destid: Destination ID of the device
1340e5cabeb3SAlexandre Bounine  * @hopcount: Number of switch hops to the device
1341e5cabeb3SAlexandre Bounine  * @from: Offset of  current Extended Feature block header (if 0 starts
1342e5cabeb3SAlexandre Bounine  * from	ExtFeaturePtr)
1343e5cabeb3SAlexandre Bounine  */
1344e5cabeb3SAlexandre Bounine u32
rio_mport_get_efb(struct rio_mport * port,int local,u16 destid,u8 hopcount,u32 from)1345e5cabeb3SAlexandre Bounine rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1346e5cabeb3SAlexandre Bounine 		      u8 hopcount, u32 from)
1347e5cabeb3SAlexandre Bounine {
1348e5cabeb3SAlexandre Bounine 	u32 reg_val;
1349e5cabeb3SAlexandre Bounine 
1350e5cabeb3SAlexandre Bounine 	if (from == 0) {
1351e5cabeb3SAlexandre Bounine 		if (local)
1352e5cabeb3SAlexandre Bounine 			rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1353e5cabeb3SAlexandre Bounine 						 &reg_val);
1354e5cabeb3SAlexandre Bounine 		else
1355e5cabeb3SAlexandre Bounine 			rio_mport_read_config_32(port, destid, hopcount,
1356e5cabeb3SAlexandre Bounine 						 RIO_ASM_INFO_CAR, &reg_val);
1357e5cabeb3SAlexandre Bounine 		return reg_val & RIO_EXT_FTR_PTR_MASK;
1358e5cabeb3SAlexandre Bounine 	} else {
1359e5cabeb3SAlexandre Bounine 		if (local)
1360e5cabeb3SAlexandre Bounine 			rio_local_read_config_32(port, from, &reg_val);
1361e5cabeb3SAlexandre Bounine 		else
1362e5cabeb3SAlexandre Bounine 			rio_mport_read_config_32(port, destid, hopcount,
1363e5cabeb3SAlexandre Bounine 						 from, &reg_val);
1364e5cabeb3SAlexandre Bounine 		return RIO_GET_BLOCK_ID(reg_val);
1365e5cabeb3SAlexandre Bounine 	}
1366e5cabeb3SAlexandre Bounine }
1367a11650e1SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_mport_get_efb);
1368e5cabeb3SAlexandre Bounine 
1369e5cabeb3SAlexandre Bounine /**
1370394b701cSMatt Porter  * rio_mport_get_feature - query for devices' extended features
1371394b701cSMatt Porter  * @port: Master port to issue transaction
1372394b701cSMatt Porter  * @local: Indicate a local master port or remote device access
1373394b701cSMatt Porter  * @destid: Destination ID of the device
1374394b701cSMatt Porter  * @hopcount: Number of switch hops to the device
1375394b701cSMatt Porter  * @ftr: Extended feature code
1376394b701cSMatt Porter  *
1377394b701cSMatt Porter  * Tell if a device supports a given RapidIO capability.
1378394b701cSMatt Porter  * Returns the offset of the requested extended feature
1379394b701cSMatt Porter  * block within the device's RIO configuration space or
13801ae842deSAlexandre Bounine  * 0 in case the device does not support it.
1381394b701cSMatt Porter  */
1382394b701cSMatt Porter u32
rio_mport_get_feature(struct rio_mport * port,int local,u16 destid,u8 hopcount,int ftr)1383394b701cSMatt Porter rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1384394b701cSMatt Porter 		      u8 hopcount, int ftr)
1385394b701cSMatt Porter {
1386394b701cSMatt Porter 	u32 asm_info, ext_ftr_ptr, ftr_header;
1387394b701cSMatt Porter 
1388394b701cSMatt Porter 	if (local)
1389394b701cSMatt Porter 		rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1390394b701cSMatt Porter 	else
1391394b701cSMatt Porter 		rio_mport_read_config_32(port, destid, hopcount,
1392394b701cSMatt Porter 					 RIO_ASM_INFO_CAR, &asm_info);
1393394b701cSMatt Porter 
1394394b701cSMatt Porter 	ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1395394b701cSMatt Porter 
1396394b701cSMatt Porter 	while (ext_ftr_ptr) {
1397394b701cSMatt Porter 		if (local)
1398394b701cSMatt Porter 			rio_local_read_config_32(port, ext_ftr_ptr,
1399394b701cSMatt Porter 						 &ftr_header);
1400394b701cSMatt Porter 		else
1401394b701cSMatt Porter 			rio_mport_read_config_32(port, destid, hopcount,
1402394b701cSMatt Porter 						 ext_ftr_ptr, &ftr_header);
1403394b701cSMatt Porter 		if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1404394b701cSMatt Porter 			return ext_ftr_ptr;
1405e1d66d04SMarkus Elfring 
1406e1d66d04SMarkus Elfring 		ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header);
1407e1d66d04SMarkus Elfring 		if (!ext_ftr_ptr)
1408394b701cSMatt Porter 			break;
1409394b701cSMatt Porter 	}
1410394b701cSMatt Porter 
1411394b701cSMatt Porter 	return 0;
1412394b701cSMatt Porter }
1413a11650e1SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_mport_get_feature);
1414394b701cSMatt Porter 
1415394b701cSMatt Porter /**
141607590ff0SAlexandre Bounine  * rio_std_route_add_entry - Add switch route table entry using standard
141707590ff0SAlexandre Bounine  *   registers defined in RIO specification rev.1.3
141807590ff0SAlexandre Bounine  * @mport: Master port to issue transaction
141907590ff0SAlexandre Bounine  * @destid: Destination ID of the device
142007590ff0SAlexandre Bounine  * @hopcount: Number of switch hops to the device
142107590ff0SAlexandre Bounine  * @table: routing table ID (global or port-specific)
142207590ff0SAlexandre Bounine  * @route_destid: destID entry in the RT
142307590ff0SAlexandre Bounine  * @route_port: destination port for specified destID
142407590ff0SAlexandre Bounine  */
14252ec3ba69SAlexandre Bounine static int
rio_std_route_add_entry(struct rio_mport * mport,u16 destid,u8 hopcount,u16 table,u16 route_destid,u8 route_port)14262ec3ba69SAlexandre Bounine rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
142707590ff0SAlexandre Bounine 			u16 table, u16 route_destid, u8 route_port)
142807590ff0SAlexandre Bounine {
142907590ff0SAlexandre Bounine 	if (table == RIO_GLOBAL_TABLE) {
143007590ff0SAlexandre Bounine 		rio_mport_write_config_32(mport, destid, hopcount,
143107590ff0SAlexandre Bounine 				RIO_STD_RTE_CONF_DESTID_SEL_CSR,
143207590ff0SAlexandre Bounine 				(u32)route_destid);
143307590ff0SAlexandre Bounine 		rio_mport_write_config_32(mport, destid, hopcount,
143407590ff0SAlexandre Bounine 				RIO_STD_RTE_CONF_PORT_SEL_CSR,
143507590ff0SAlexandre Bounine 				(u32)route_port);
143607590ff0SAlexandre Bounine 	}
1437e5cabeb3SAlexandre Bounine 
143807590ff0SAlexandre Bounine 	udelay(10);
143907590ff0SAlexandre Bounine 	return 0;
144007590ff0SAlexandre Bounine }
144107590ff0SAlexandre Bounine 
144207590ff0SAlexandre Bounine /**
144307590ff0SAlexandre Bounine  * rio_std_route_get_entry - Read switch route table entry (port number)
1444638c5945SUwe Kleine-König  *   associated with specified destID using standard registers defined in RIO
144507590ff0SAlexandre Bounine  *   specification rev.1.3
144607590ff0SAlexandre Bounine  * @mport: Master port to issue transaction
144707590ff0SAlexandre Bounine  * @destid: Destination ID of the device
144807590ff0SAlexandre Bounine  * @hopcount: Number of switch hops to the device
144907590ff0SAlexandre Bounine  * @table: routing table ID (global or port-specific)
145007590ff0SAlexandre Bounine  * @route_destid: destID entry in the RT
145107590ff0SAlexandre Bounine  * @route_port: returned destination port for specified destID
145207590ff0SAlexandre Bounine  */
14532ec3ba69SAlexandre Bounine static int
rio_std_route_get_entry(struct rio_mport * mport,u16 destid,u8 hopcount,u16 table,u16 route_destid,u8 * route_port)14542ec3ba69SAlexandre Bounine rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
145507590ff0SAlexandre Bounine 			u16 table, u16 route_destid, u8 *route_port)
145607590ff0SAlexandre Bounine {
145707590ff0SAlexandre Bounine 	u32 result;
145807590ff0SAlexandre Bounine 
145907590ff0SAlexandre Bounine 	if (table == RIO_GLOBAL_TABLE) {
146007590ff0SAlexandre Bounine 		rio_mport_write_config_32(mport, destid, hopcount,
146107590ff0SAlexandre Bounine 				RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
146207590ff0SAlexandre Bounine 		rio_mport_read_config_32(mport, destid, hopcount,
146307590ff0SAlexandre Bounine 				RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
146407590ff0SAlexandre Bounine 
146507590ff0SAlexandre Bounine 		*route_port = (u8)result;
146607590ff0SAlexandre Bounine 	}
146707590ff0SAlexandre Bounine 
146807590ff0SAlexandre Bounine 	return 0;
146907590ff0SAlexandre Bounine }
147007590ff0SAlexandre Bounine 
147107590ff0SAlexandre Bounine /**
147207590ff0SAlexandre Bounine  * rio_std_route_clr_table - Clear swotch route table using standard registers
147307590ff0SAlexandre Bounine  *   defined in RIO specification rev.1.3.
147407590ff0SAlexandre Bounine  * @mport: Master port to issue transaction
147507590ff0SAlexandre Bounine  * @destid: Destination ID of the device
147607590ff0SAlexandre Bounine  * @hopcount: Number of switch hops to the device
147707590ff0SAlexandre Bounine  * @table: routing table ID (global or port-specific)
147807590ff0SAlexandre Bounine  */
14792ec3ba69SAlexandre Bounine static int
rio_std_route_clr_table(struct rio_mport * mport,u16 destid,u8 hopcount,u16 table)14802ec3ba69SAlexandre Bounine rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
148107590ff0SAlexandre Bounine 			u16 table)
148207590ff0SAlexandre Bounine {
148307590ff0SAlexandre Bounine 	u32 max_destid = 0xff;
148407590ff0SAlexandre Bounine 	u32 i, pef, id_inc = 1, ext_cfg = 0;
148507590ff0SAlexandre Bounine 	u32 port_sel = RIO_INVALID_ROUTE;
148607590ff0SAlexandre Bounine 
148707590ff0SAlexandre Bounine 	if (table == RIO_GLOBAL_TABLE) {
148807590ff0SAlexandre Bounine 		rio_mport_read_config_32(mport, destid, hopcount,
148907590ff0SAlexandre Bounine 					 RIO_PEF_CAR, &pef);
149007590ff0SAlexandre Bounine 
149107590ff0SAlexandre Bounine 		if (mport->sys_size) {
149207590ff0SAlexandre Bounine 			rio_mport_read_config_32(mport, destid, hopcount,
149307590ff0SAlexandre Bounine 						 RIO_SWITCH_RT_LIMIT,
149407590ff0SAlexandre Bounine 						 &max_destid);
149507590ff0SAlexandre Bounine 			max_destid &= RIO_RT_MAX_DESTID;
149607590ff0SAlexandre Bounine 		}
149707590ff0SAlexandre Bounine 
149807590ff0SAlexandre Bounine 		if (pef & RIO_PEF_EXT_RT) {
149907590ff0SAlexandre Bounine 			ext_cfg = 0x80000000;
150007590ff0SAlexandre Bounine 			id_inc = 4;
150107590ff0SAlexandre Bounine 			port_sel = (RIO_INVALID_ROUTE << 24) |
150207590ff0SAlexandre Bounine 				   (RIO_INVALID_ROUTE << 16) |
150307590ff0SAlexandre Bounine 				   (RIO_INVALID_ROUTE << 8) |
150407590ff0SAlexandre Bounine 				   RIO_INVALID_ROUTE;
150507590ff0SAlexandre Bounine 		}
150607590ff0SAlexandre Bounine 
150707590ff0SAlexandre Bounine 		for (i = 0; i <= max_destid;) {
150807590ff0SAlexandre Bounine 			rio_mport_write_config_32(mport, destid, hopcount,
150907590ff0SAlexandre Bounine 					RIO_STD_RTE_CONF_DESTID_SEL_CSR,
151007590ff0SAlexandre Bounine 					ext_cfg | i);
151107590ff0SAlexandre Bounine 			rio_mport_write_config_32(mport, destid, hopcount,
151207590ff0SAlexandre Bounine 					RIO_STD_RTE_CONF_PORT_SEL_CSR,
151307590ff0SAlexandre Bounine 					port_sel);
151407590ff0SAlexandre Bounine 			i += id_inc;
151507590ff0SAlexandre Bounine 		}
151607590ff0SAlexandre Bounine 	}
151707590ff0SAlexandre Bounine 
151807590ff0SAlexandre Bounine 	udelay(10);
151907590ff0SAlexandre Bounine 	return 0;
152007590ff0SAlexandre Bounine }
152107590ff0SAlexandre Bounine 
15222ec3ba69SAlexandre Bounine /**
15232ec3ba69SAlexandre Bounine  * rio_lock_device - Acquires host device lock for specified device
15242ec3ba69SAlexandre Bounine  * @port: Master port to send transaction
15252ec3ba69SAlexandre Bounine  * @destid: Destination ID for device/switch
15262ec3ba69SAlexandre Bounine  * @hopcount: Hopcount to reach switch
15272ec3ba69SAlexandre Bounine  * @wait_ms: Max wait time in msec (0 = no timeout)
15282ec3ba69SAlexandre Bounine  *
15292ec3ba69SAlexandre Bounine  * Attepts to acquire host device lock for specified device
15302ec3ba69SAlexandre Bounine  * Returns 0 if device lock acquired or EINVAL if timeout expires.
15312ec3ba69SAlexandre Bounine  */
rio_lock_device(struct rio_mport * port,u16 destid,u8 hopcount,int wait_ms)15322ec3ba69SAlexandre Bounine int rio_lock_device(struct rio_mport *port, u16 destid,
15332ec3ba69SAlexandre Bounine 		    u8 hopcount, int wait_ms)
15342ec3ba69SAlexandre Bounine {
15352ec3ba69SAlexandre Bounine 	u32 result;
15362ec3ba69SAlexandre Bounine 	int tcnt = 0;
15372ec3ba69SAlexandre Bounine 
15382ec3ba69SAlexandre Bounine 	/* Attempt to acquire device lock */
15392ec3ba69SAlexandre Bounine 	rio_mport_write_config_32(port, destid, hopcount,
15402ec3ba69SAlexandre Bounine 				  RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
15412ec3ba69SAlexandre Bounine 	rio_mport_read_config_32(port, destid, hopcount,
15422ec3ba69SAlexandre Bounine 				 RIO_HOST_DID_LOCK_CSR, &result);
15432ec3ba69SAlexandre Bounine 
15442ec3ba69SAlexandre Bounine 	while (result != port->host_deviceid) {
15452ec3ba69SAlexandre Bounine 		if (wait_ms != 0 && tcnt == wait_ms) {
15462ec3ba69SAlexandre Bounine 			pr_debug("RIO: timeout when locking device %x:%x\n",
15472ec3ba69SAlexandre Bounine 				destid, hopcount);
15482ec3ba69SAlexandre Bounine 			return -EINVAL;
15492ec3ba69SAlexandre Bounine 		}
15502ec3ba69SAlexandre Bounine 
15512ec3ba69SAlexandre Bounine 		/* Delay a bit */
15522ec3ba69SAlexandre Bounine 		mdelay(1);
15532ec3ba69SAlexandre Bounine 		tcnt++;
15542ec3ba69SAlexandre Bounine 		/* Try to acquire device lock again */
15552ec3ba69SAlexandre Bounine 		rio_mport_write_config_32(port, destid,
15562ec3ba69SAlexandre Bounine 			hopcount,
15572ec3ba69SAlexandre Bounine 			RIO_HOST_DID_LOCK_CSR,
15582ec3ba69SAlexandre Bounine 			port->host_deviceid);
15592ec3ba69SAlexandre Bounine 		rio_mport_read_config_32(port, destid,
15602ec3ba69SAlexandre Bounine 			hopcount,
15612ec3ba69SAlexandre Bounine 			RIO_HOST_DID_LOCK_CSR, &result);
15622ec3ba69SAlexandre Bounine 	}
15632ec3ba69SAlexandre Bounine 
15642ec3ba69SAlexandre Bounine 	return 0;
15652ec3ba69SAlexandre Bounine }
15662ec3ba69SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_lock_device);
15672ec3ba69SAlexandre Bounine 
15682ec3ba69SAlexandre Bounine /**
15692ec3ba69SAlexandre Bounine  * rio_unlock_device - Releases host device lock for specified device
15702ec3ba69SAlexandre Bounine  * @port: Master port to send transaction
15712ec3ba69SAlexandre Bounine  * @destid: Destination ID for device/switch
15722ec3ba69SAlexandre Bounine  * @hopcount: Hopcount to reach switch
15732ec3ba69SAlexandre Bounine  *
15742ec3ba69SAlexandre Bounine  * Returns 0 if device lock released or EINVAL if fails.
15752ec3ba69SAlexandre Bounine  */
rio_unlock_device(struct rio_mport * port,u16 destid,u8 hopcount)15762ec3ba69SAlexandre Bounine int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
15772ec3ba69SAlexandre Bounine {
15782ec3ba69SAlexandre Bounine 	u32 result;
15792ec3ba69SAlexandre Bounine 
15802ec3ba69SAlexandre Bounine 	/* Release device lock */
15812ec3ba69SAlexandre Bounine 	rio_mport_write_config_32(port, destid,
15822ec3ba69SAlexandre Bounine 				  hopcount,
15832ec3ba69SAlexandre Bounine 				  RIO_HOST_DID_LOCK_CSR,
15842ec3ba69SAlexandre Bounine 				  port->host_deviceid);
15852ec3ba69SAlexandre Bounine 	rio_mport_read_config_32(port, destid, hopcount,
15862ec3ba69SAlexandre Bounine 		RIO_HOST_DID_LOCK_CSR, &result);
15872ec3ba69SAlexandre Bounine 	if ((result & 0xffff) != 0xffff) {
15882ec3ba69SAlexandre Bounine 		pr_debug("RIO: badness when releasing device lock %x:%x\n",
15892ec3ba69SAlexandre Bounine 			 destid, hopcount);
15902ec3ba69SAlexandre Bounine 		return -EINVAL;
15912ec3ba69SAlexandre Bounine 	}
15922ec3ba69SAlexandre Bounine 
15932ec3ba69SAlexandre Bounine 	return 0;
15942ec3ba69SAlexandre Bounine }
15952ec3ba69SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_unlock_device);
15962ec3ba69SAlexandre Bounine 
15972ec3ba69SAlexandre Bounine /**
15982ec3ba69SAlexandre Bounine  * rio_route_add_entry- Add a route entry to a switch routing table
15992ec3ba69SAlexandre Bounine  * @rdev: RIO device
16002ec3ba69SAlexandre Bounine  * @table: Routing table ID
16012ec3ba69SAlexandre Bounine  * @route_destid: Destination ID to be routed
16022ec3ba69SAlexandre Bounine  * @route_port: Port number to be routed
16032ec3ba69SAlexandre Bounine  * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
16042ec3ba69SAlexandre Bounine  *
16052ec3ba69SAlexandre Bounine  * If available calls the switch specific add_entry() method to add a route
16062ec3ba69SAlexandre Bounine  * entry into a switch routing table. Otherwise uses standard RT update method
16072ec3ba69SAlexandre Bounine  * as defined by RapidIO specification. A specific routing table can be selected
16082ec3ba69SAlexandre Bounine  * using the @table argument if a switch has per port routing tables or
16092ec3ba69SAlexandre Bounine  * the standard (or global) table may be used by passing
16102ec3ba69SAlexandre Bounine  * %RIO_GLOBAL_TABLE in @table.
16112ec3ba69SAlexandre Bounine  *
16122ec3ba69SAlexandre Bounine  * Returns %0 on success or %-EINVAL on failure.
16132ec3ba69SAlexandre Bounine  */
rio_route_add_entry(struct rio_dev * rdev,u16 table,u16 route_destid,u8 route_port,int lock)16142ec3ba69SAlexandre Bounine int rio_route_add_entry(struct rio_dev *rdev,
16152ec3ba69SAlexandre Bounine 			u16 table, u16 route_destid, u8 route_port, int lock)
16162ec3ba69SAlexandre Bounine {
16172ec3ba69SAlexandre Bounine 	int rc = -EINVAL;
16182ec3ba69SAlexandre Bounine 	struct rio_switch_ops *ops = rdev->rswitch->ops;
16192ec3ba69SAlexandre Bounine 
16202ec3ba69SAlexandre Bounine 	if (lock) {
16212ec3ba69SAlexandre Bounine 		rc = rio_lock_device(rdev->net->hport, rdev->destid,
16222ec3ba69SAlexandre Bounine 				     rdev->hopcount, 1000);
16232ec3ba69SAlexandre Bounine 		if (rc)
16242ec3ba69SAlexandre Bounine 			return rc;
16252ec3ba69SAlexandre Bounine 	}
16262ec3ba69SAlexandre Bounine 
16272ec3ba69SAlexandre Bounine 	spin_lock(&rdev->rswitch->lock);
16282ec3ba69SAlexandre Bounine 
162993dd49afSMarkus Elfring 	if (!ops || !ops->add_entry) {
16302ec3ba69SAlexandre Bounine 		rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
16312ec3ba69SAlexandre Bounine 					     rdev->hopcount, table,
16322ec3ba69SAlexandre Bounine 					     route_destid, route_port);
16332ec3ba69SAlexandre Bounine 	} else if (try_module_get(ops->owner)) {
16342ec3ba69SAlexandre Bounine 		rc = ops->add_entry(rdev->net->hport, rdev->destid,
16352ec3ba69SAlexandre Bounine 				    rdev->hopcount, table, route_destid,
16362ec3ba69SAlexandre Bounine 				    route_port);
16372ec3ba69SAlexandre Bounine 		module_put(ops->owner);
16382ec3ba69SAlexandre Bounine 	}
16392ec3ba69SAlexandre Bounine 
16402ec3ba69SAlexandre Bounine 	spin_unlock(&rdev->rswitch->lock);
16412ec3ba69SAlexandre Bounine 
16422ec3ba69SAlexandre Bounine 	if (lock)
16432ec3ba69SAlexandre Bounine 		rio_unlock_device(rdev->net->hport, rdev->destid,
16442ec3ba69SAlexandre Bounine 				  rdev->hopcount);
16452ec3ba69SAlexandre Bounine 
16462ec3ba69SAlexandre Bounine 	return rc;
16472ec3ba69SAlexandre Bounine }
16482ec3ba69SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_route_add_entry);
16492ec3ba69SAlexandre Bounine 
16502ec3ba69SAlexandre Bounine /**
16512ec3ba69SAlexandre Bounine  * rio_route_get_entry- Read an entry from a switch routing table
16522ec3ba69SAlexandre Bounine  * @rdev: RIO device
16532ec3ba69SAlexandre Bounine  * @table: Routing table ID
16542ec3ba69SAlexandre Bounine  * @route_destid: Destination ID to be routed
16552ec3ba69SAlexandre Bounine  * @route_port: Pointer to read port number into
16562ec3ba69SAlexandre Bounine  * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
16572ec3ba69SAlexandre Bounine  *
16582ec3ba69SAlexandre Bounine  * If available calls the switch specific get_entry() method to fetch a route
16592ec3ba69SAlexandre Bounine  * entry from a switch routing table. Otherwise uses standard RT read method
16602ec3ba69SAlexandre Bounine  * as defined by RapidIO specification. A specific routing table can be selected
16612ec3ba69SAlexandre Bounine  * using the @table argument if a switch has per port routing tables or
16622ec3ba69SAlexandre Bounine  * the standard (or global) table may be used by passing
16632ec3ba69SAlexandre Bounine  * %RIO_GLOBAL_TABLE in @table.
16642ec3ba69SAlexandre Bounine  *
16652ec3ba69SAlexandre Bounine  * Returns %0 on success or %-EINVAL on failure.
16662ec3ba69SAlexandre Bounine  */
rio_route_get_entry(struct rio_dev * rdev,u16 table,u16 route_destid,u8 * route_port,int lock)16672ec3ba69SAlexandre Bounine int rio_route_get_entry(struct rio_dev *rdev, u16 table,
16682ec3ba69SAlexandre Bounine 			u16 route_destid, u8 *route_port, int lock)
16692ec3ba69SAlexandre Bounine {
16702ec3ba69SAlexandre Bounine 	int rc = -EINVAL;
16712ec3ba69SAlexandre Bounine 	struct rio_switch_ops *ops = rdev->rswitch->ops;
16722ec3ba69SAlexandre Bounine 
16732ec3ba69SAlexandre Bounine 	if (lock) {
16742ec3ba69SAlexandre Bounine 		rc = rio_lock_device(rdev->net->hport, rdev->destid,
16752ec3ba69SAlexandre Bounine 				     rdev->hopcount, 1000);
16762ec3ba69SAlexandre Bounine 		if (rc)
16772ec3ba69SAlexandre Bounine 			return rc;
16782ec3ba69SAlexandre Bounine 	}
16792ec3ba69SAlexandre Bounine 
16802ec3ba69SAlexandre Bounine 	spin_lock(&rdev->rswitch->lock);
16812ec3ba69SAlexandre Bounine 
168293dd49afSMarkus Elfring 	if (!ops || !ops->get_entry) {
16832ec3ba69SAlexandre Bounine 		rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
16842ec3ba69SAlexandre Bounine 					     rdev->hopcount, table,
16852ec3ba69SAlexandre Bounine 					     route_destid, route_port);
16862ec3ba69SAlexandre Bounine 	} else if (try_module_get(ops->owner)) {
16872ec3ba69SAlexandre Bounine 		rc = ops->get_entry(rdev->net->hport, rdev->destid,
16882ec3ba69SAlexandre Bounine 				    rdev->hopcount, table, route_destid,
16892ec3ba69SAlexandre Bounine 				    route_port);
16902ec3ba69SAlexandre Bounine 		module_put(ops->owner);
16912ec3ba69SAlexandre Bounine 	}
16922ec3ba69SAlexandre Bounine 
16932ec3ba69SAlexandre Bounine 	spin_unlock(&rdev->rswitch->lock);
16942ec3ba69SAlexandre Bounine 
16952ec3ba69SAlexandre Bounine 	if (lock)
16962ec3ba69SAlexandre Bounine 		rio_unlock_device(rdev->net->hport, rdev->destid,
16972ec3ba69SAlexandre Bounine 				  rdev->hopcount);
16982ec3ba69SAlexandre Bounine 	return rc;
16992ec3ba69SAlexandre Bounine }
17002ec3ba69SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_route_get_entry);
17012ec3ba69SAlexandre Bounine 
17022ec3ba69SAlexandre Bounine /**
17032ec3ba69SAlexandre Bounine  * rio_route_clr_table - Clear a switch routing table
17042ec3ba69SAlexandre Bounine  * @rdev: RIO device
17052ec3ba69SAlexandre Bounine  * @table: Routing table ID
17062ec3ba69SAlexandre Bounine  * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
17072ec3ba69SAlexandre Bounine  *
17082ec3ba69SAlexandre Bounine  * If available calls the switch specific clr_table() method to clear a switch
17092ec3ba69SAlexandre Bounine  * routing table. Otherwise uses standard RT write method as defined by RapidIO
17102ec3ba69SAlexandre Bounine  * specification. A specific routing table can be selected using the @table
17112ec3ba69SAlexandre Bounine  * argument if a switch has per port routing tables or the standard (or global)
17122ec3ba69SAlexandre Bounine  * table may be used by passing %RIO_GLOBAL_TABLE in @table.
17132ec3ba69SAlexandre Bounine  *
17142ec3ba69SAlexandre Bounine  * Returns %0 on success or %-EINVAL on failure.
17152ec3ba69SAlexandre Bounine  */
rio_route_clr_table(struct rio_dev * rdev,u16 table,int lock)17162ec3ba69SAlexandre Bounine int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
17172ec3ba69SAlexandre Bounine {
17182ec3ba69SAlexandre Bounine 	int rc = -EINVAL;
17192ec3ba69SAlexandre Bounine 	struct rio_switch_ops *ops = rdev->rswitch->ops;
17202ec3ba69SAlexandre Bounine 
17212ec3ba69SAlexandre Bounine 	if (lock) {
17222ec3ba69SAlexandre Bounine 		rc = rio_lock_device(rdev->net->hport, rdev->destid,
17232ec3ba69SAlexandre Bounine 				     rdev->hopcount, 1000);
17242ec3ba69SAlexandre Bounine 		if (rc)
17252ec3ba69SAlexandre Bounine 			return rc;
17262ec3ba69SAlexandre Bounine 	}
17272ec3ba69SAlexandre Bounine 
17282ec3ba69SAlexandre Bounine 	spin_lock(&rdev->rswitch->lock);
17292ec3ba69SAlexandre Bounine 
173093dd49afSMarkus Elfring 	if (!ops || !ops->clr_table) {
17312ec3ba69SAlexandre Bounine 		rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
17322ec3ba69SAlexandre Bounine 					     rdev->hopcount, table);
17332ec3ba69SAlexandre Bounine 	} else if (try_module_get(ops->owner)) {
17342ec3ba69SAlexandre Bounine 		rc = ops->clr_table(rdev->net->hport, rdev->destid,
17352ec3ba69SAlexandre Bounine 				    rdev->hopcount, table);
17362ec3ba69SAlexandre Bounine 
17372ec3ba69SAlexandre Bounine 		module_put(ops->owner);
17382ec3ba69SAlexandre Bounine 	}
17392ec3ba69SAlexandre Bounine 
17402ec3ba69SAlexandre Bounine 	spin_unlock(&rdev->rswitch->lock);
17412ec3ba69SAlexandre Bounine 
17422ec3ba69SAlexandre Bounine 	if (lock)
17432ec3ba69SAlexandre Bounine 		rio_unlock_device(rdev->net->hport, rdev->destid,
17442ec3ba69SAlexandre Bounine 				  rdev->hopcount);
17452ec3ba69SAlexandre Bounine 
17462ec3ba69SAlexandre Bounine 	return rc;
17472ec3ba69SAlexandre Bounine }
17482ec3ba69SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_route_clr_table);
17492ec3ba69SAlexandre Bounine 
1750e42d98ebSAlexandre Bounine #ifdef CONFIG_RAPIDIO_DMA_ENGINE
1751e42d98ebSAlexandre Bounine 
rio_chan_filter(struct dma_chan * chan,void * arg)1752e42d98ebSAlexandre Bounine static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1753e42d98ebSAlexandre Bounine {
17544aff1ce7SAlexandre Bounine 	struct rio_mport *mport = arg;
1755e42d98ebSAlexandre Bounine 
1756e42d98ebSAlexandre Bounine 	/* Check that DMA device belongs to the right MPORT */
17574aff1ce7SAlexandre Bounine 	return mport == container_of(chan->device, struct rio_mport, dma);
1758e42d98ebSAlexandre Bounine }
1759e42d98ebSAlexandre Bounine 
1760e42d98ebSAlexandre Bounine /**
17614aff1ce7SAlexandre Bounine  * rio_request_mport_dma - request RapidIO capable DMA channel associated
17624aff1ce7SAlexandre Bounine  *   with specified local RapidIO mport device.
17634aff1ce7SAlexandre Bounine  * @mport: RIO mport to perform DMA data transfers
17644aff1ce7SAlexandre Bounine  *
17654aff1ce7SAlexandre Bounine  * Returns pointer to allocated DMA channel or NULL if failed.
17664aff1ce7SAlexandre Bounine  */
rio_request_mport_dma(struct rio_mport * mport)17674aff1ce7SAlexandre Bounine struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
17684aff1ce7SAlexandre Bounine {
17694aff1ce7SAlexandre Bounine 	dma_cap_mask_t mask;
17704aff1ce7SAlexandre Bounine 
17714aff1ce7SAlexandre Bounine 	dma_cap_zero(mask);
17724aff1ce7SAlexandre Bounine 	dma_cap_set(DMA_SLAVE, mask);
17734aff1ce7SAlexandre Bounine 	return dma_request_channel(mask, rio_chan_filter, mport);
17744aff1ce7SAlexandre Bounine }
17754aff1ce7SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_request_mport_dma);
17764aff1ce7SAlexandre Bounine 
17774aff1ce7SAlexandre Bounine /**
1778e42d98ebSAlexandre Bounine  * rio_request_dma - request RapidIO capable DMA channel that supports
1779e42d98ebSAlexandre Bounine  *   specified target RapidIO device.
17804aff1ce7SAlexandre Bounine  * @rdev: RIO device associated with DMA transfer
1781e42d98ebSAlexandre Bounine  *
1782e42d98ebSAlexandre Bounine  * Returns pointer to allocated DMA channel or NULL if failed.
1783e42d98ebSAlexandre Bounine  */
rio_request_dma(struct rio_dev * rdev)1784e42d98ebSAlexandre Bounine struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1785e42d98ebSAlexandre Bounine {
17864aff1ce7SAlexandre Bounine 	return rio_request_mport_dma(rdev->net->hport);
1787e42d98ebSAlexandre Bounine }
1788e42d98ebSAlexandre Bounine EXPORT_SYMBOL_GPL(rio_request_dma);
1789e42d98ebSAlexandre Bounine 
1790e42d98ebSAlexandre Bounine /**
1791e42d98ebSAlexandre Bounine  * rio_release_dma - release specified DMA channel
1792e42d98ebSAlexandre Bounine  * @dchan: DMA channel to release
1793e42d98ebSAlexandre Bounine  */
rio_release_dma(struct dma_chan * dchan)1794e42d98ebSAlexandre Bounine void rio_release_dma(struct dma_chan *dchan)
1795e42d98ebSAlexandre Bounine {
1796e42d98ebSAlexandre Bounine 	dma_release_channel(dchan);
1797e42d98ebSAlexandre Bounine }
1798e42d98ebSAlexandre Bounine EXPORT_SYMBOL_GPL(rio_release_dma);
1799e42d98ebSAlexandre Bounine 
1800e42d98ebSAlexandre Bounine /**
18014aff1ce7SAlexandre Bounine  * rio_dma_prep_xfer - RapidIO specific wrapper
18024aff1ce7SAlexandre Bounine  *   for device_prep_slave_sg callback defined by DMAENGINE.
18034aff1ce7SAlexandre Bounine  * @dchan: DMA channel to configure
18044aff1ce7SAlexandre Bounine  * @destid: target RapidIO device destination ID
18054aff1ce7SAlexandre Bounine  * @data: RIO specific data descriptor
18064aff1ce7SAlexandre Bounine  * @direction: DMA data transfer direction (TO or FROM the device)
18074aff1ce7SAlexandre Bounine  * @flags: dmaengine defined flags
18084aff1ce7SAlexandre Bounine  *
18094aff1ce7SAlexandre Bounine  * Initializes RapidIO capable DMA channel for the specified data transfer.
18104aff1ce7SAlexandre Bounine  * Uses DMA channel private extension to pass information related to remote
18114aff1ce7SAlexandre Bounine  * target RIO device.
1812f8e3a68cSAlexandre Bounine  *
1813f8e3a68cSAlexandre Bounine  * Returns: pointer to DMA transaction descriptor if successful,
1814f8e3a68cSAlexandre Bounine  *          error-valued pointer or NULL if failed.
18154aff1ce7SAlexandre Bounine  */
rio_dma_prep_xfer(struct dma_chan * dchan,u16 destid,struct rio_dma_data * data,enum dma_transfer_direction direction,unsigned long flags)18164aff1ce7SAlexandre Bounine struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
18174aff1ce7SAlexandre Bounine 	u16 destid, struct rio_dma_data *data,
18184aff1ce7SAlexandre Bounine 	enum dma_transfer_direction direction, unsigned long flags)
18194aff1ce7SAlexandre Bounine {
18204aff1ce7SAlexandre Bounine 	struct rio_dma_ext rio_ext;
18214aff1ce7SAlexandre Bounine 
182293dd49afSMarkus Elfring 	if (!dchan->device->device_prep_slave_sg) {
18234aff1ce7SAlexandre Bounine 		pr_err("%s: prep_rio_sg == NULL\n", __func__);
18244aff1ce7SAlexandre Bounine 		return NULL;
18254aff1ce7SAlexandre Bounine 	}
18264aff1ce7SAlexandre Bounine 
18274aff1ce7SAlexandre Bounine 	rio_ext.destid = destid;
18284aff1ce7SAlexandre Bounine 	rio_ext.rio_addr_u = data->rio_addr_u;
18294aff1ce7SAlexandre Bounine 	rio_ext.rio_addr = data->rio_addr;
18304aff1ce7SAlexandre Bounine 	rio_ext.wr_type = data->wr_type;
18314aff1ce7SAlexandre Bounine 
18324aff1ce7SAlexandre Bounine 	return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
18334aff1ce7SAlexandre Bounine 				     direction, flags, &rio_ext);
18344aff1ce7SAlexandre Bounine }
18354aff1ce7SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
18364aff1ce7SAlexandre Bounine 
18374aff1ce7SAlexandre Bounine /**
1838e42d98ebSAlexandre Bounine  * rio_dma_prep_slave_sg - RapidIO specific wrapper
1839e42d98ebSAlexandre Bounine  *   for device_prep_slave_sg callback defined by DMAENGINE.
1840e42d98ebSAlexandre Bounine  * @rdev: RIO device control structure
1841e42d98ebSAlexandre Bounine  * @dchan: DMA channel to configure
1842e42d98ebSAlexandre Bounine  * @data: RIO specific data descriptor
1843e42d98ebSAlexandre Bounine  * @direction: DMA data transfer direction (TO or FROM the device)
1844e42d98ebSAlexandre Bounine  * @flags: dmaengine defined flags
1845e42d98ebSAlexandre Bounine  *
1846e42d98ebSAlexandre Bounine  * Initializes RapidIO capable DMA channel for the specified data transfer.
1847e42d98ebSAlexandre Bounine  * Uses DMA channel private extension to pass information related to remote
1848e42d98ebSAlexandre Bounine  * target RIO device.
1849f8e3a68cSAlexandre Bounine  *
1850f8e3a68cSAlexandre Bounine  * Returns: pointer to DMA transaction descriptor if successful,
1851f8e3a68cSAlexandre Bounine  *          error-valued pointer or NULL if failed.
1852e42d98ebSAlexandre Bounine  */
rio_dma_prep_slave_sg(struct rio_dev * rdev,struct dma_chan * dchan,struct rio_dma_data * data,enum dma_transfer_direction direction,unsigned long flags)1853e42d98ebSAlexandre Bounine struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1854e42d98ebSAlexandre Bounine 	struct dma_chan *dchan, struct rio_dma_data *data,
1855e42d98ebSAlexandre Bounine 	enum dma_transfer_direction direction, unsigned long flags)
1856e42d98ebSAlexandre Bounine {
18574aff1ce7SAlexandre Bounine 	return rio_dma_prep_xfer(dchan,	rdev->destid, data, direction, flags);
1858e42d98ebSAlexandre Bounine }
1859e42d98ebSAlexandre Bounine EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1860e42d98ebSAlexandre Bounine 
1861e42d98ebSAlexandre Bounine #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1862e42d98ebSAlexandre Bounine 
1863a11650e1SAlexandre Bounine /**
1864bc8fcfeaSAlexandre Bounine  * rio_find_mport - find RIO mport by its ID
1865bc8fcfeaSAlexandre Bounine  * @mport_id: number (ID) of mport device
1866bc8fcfeaSAlexandre Bounine  *
1867bc8fcfeaSAlexandre Bounine  * Given a RIO mport number, the desired mport is located
1868bc8fcfeaSAlexandre Bounine  * in the global list of mports. If the mport is found, a pointer to its
1869bc8fcfeaSAlexandre Bounine  * data structure is returned.  If no mport is found, %NULL is returned.
1870bc8fcfeaSAlexandre Bounine  */
rio_find_mport(int mport_id)1871bc8fcfeaSAlexandre Bounine struct rio_mport *rio_find_mport(int mport_id)
1872bc8fcfeaSAlexandre Bounine {
1873bc8fcfeaSAlexandre Bounine 	struct rio_mport *port;
1874bc8fcfeaSAlexandre Bounine 
1875bc8fcfeaSAlexandre Bounine 	mutex_lock(&rio_mport_list_lock);
1876bc8fcfeaSAlexandre Bounine 	list_for_each_entry(port, &rio_mports, node) {
1877bc8fcfeaSAlexandre Bounine 		if (port->id == mport_id)
1878bc8fcfeaSAlexandre Bounine 			goto found;
1879bc8fcfeaSAlexandre Bounine 	}
1880bc8fcfeaSAlexandre Bounine 	port = NULL;
1881bc8fcfeaSAlexandre Bounine found:
1882bc8fcfeaSAlexandre Bounine 	mutex_unlock(&rio_mport_list_lock);
1883bc8fcfeaSAlexandre Bounine 
1884bc8fcfeaSAlexandre Bounine 	return port;
1885bc8fcfeaSAlexandre Bounine }
1886bc8fcfeaSAlexandre Bounine 
1887bc8fcfeaSAlexandre Bounine /**
1888a11650e1SAlexandre Bounine  * rio_register_scan - enumeration/discovery method registration interface
1889a11650e1SAlexandre Bounine  * @mport_id: mport device ID for which fabric scan routine has to be set
1890a11650e1SAlexandre Bounine  *            (RIO_MPORT_ANY = set for all available mports)
18919edbc30bSAlexandre Bounine  * @scan_ops: enumeration/discovery operations structure
1892a11650e1SAlexandre Bounine  *
18939edbc30bSAlexandre Bounine  * Registers enumeration/discovery operations with RapidIO subsystem and
18949edbc30bSAlexandre Bounine  * attaches it to the specified mport device (or all available mports
18959edbc30bSAlexandre Bounine  * if RIO_MPORT_ANY is specified).
18969edbc30bSAlexandre Bounine  *
1897a11650e1SAlexandre Bounine  * Returns error if the mport already has an enumerator attached to it.
18989edbc30bSAlexandre Bounine  * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
1899a11650e1SAlexandre Bounine  */
rio_register_scan(int mport_id,struct rio_scan * scan_ops)1900a11650e1SAlexandre Bounine int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1901a11650e1SAlexandre Bounine {
1902a11650e1SAlexandre Bounine 	struct rio_mport *port;
19039edbc30bSAlexandre Bounine 	struct rio_scan_node *scan;
19049edbc30bSAlexandre Bounine 	int rc = 0;
19059edbc30bSAlexandre Bounine 
19069edbc30bSAlexandre Bounine 	pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
19079edbc30bSAlexandre Bounine 
19089edbc30bSAlexandre Bounine 	if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
19099edbc30bSAlexandre Bounine 	    !scan_ops)
19109edbc30bSAlexandre Bounine 		return -EINVAL;
1911a11650e1SAlexandre Bounine 
1912a11650e1SAlexandre Bounine 	mutex_lock(&rio_mport_list_lock);
19139edbc30bSAlexandre Bounine 
19149edbc30bSAlexandre Bounine 	/*
19159edbc30bSAlexandre Bounine 	 * Check if there is another enumerator already registered for
19169edbc30bSAlexandre Bounine 	 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
19179edbc30bSAlexandre Bounine 	 * for the same mport ID are not supported.
19189edbc30bSAlexandre Bounine 	 */
19199edbc30bSAlexandre Bounine 	list_for_each_entry(scan, &rio_scans, node) {
19209edbc30bSAlexandre Bounine 		if (scan->mport_id == mport_id) {
19219edbc30bSAlexandre Bounine 			rc = -EBUSY;
19229edbc30bSAlexandre Bounine 			goto err_out;
19239edbc30bSAlexandre Bounine 		}
19249edbc30bSAlexandre Bounine 	}
19259edbc30bSAlexandre Bounine 
19269edbc30bSAlexandre Bounine 	/*
19279edbc30bSAlexandre Bounine 	 * Allocate and initialize new scan registration node.
19289edbc30bSAlexandre Bounine 	 */
19299edbc30bSAlexandre Bounine 	scan = kzalloc(sizeof(*scan), GFP_KERNEL);
19309edbc30bSAlexandre Bounine 	if (!scan) {
19319edbc30bSAlexandre Bounine 		rc = -ENOMEM;
19329edbc30bSAlexandre Bounine 		goto err_out;
19339edbc30bSAlexandre Bounine 	}
19349edbc30bSAlexandre Bounine 
19359edbc30bSAlexandre Bounine 	scan->mport_id = mport_id;
19369edbc30bSAlexandre Bounine 	scan->ops = scan_ops;
19379edbc30bSAlexandre Bounine 
19389edbc30bSAlexandre Bounine 	/*
19399edbc30bSAlexandre Bounine 	 * Traverse the list of registered mports to attach this new scan.
19409edbc30bSAlexandre Bounine 	 *
19419edbc30bSAlexandre Bounine 	 * The new scan with matching mport ID overrides any previously attached
19429edbc30bSAlexandre Bounine 	 * scan assuming that old scan (if any) is the default one (based on the
19439edbc30bSAlexandre Bounine 	 * enumerator registration check above).
19449edbc30bSAlexandre Bounine 	 * If the new scan is the global one, it will be attached only to mports
19459edbc30bSAlexandre Bounine 	 * that do not have their own individual operations already attached.
19469edbc30bSAlexandre Bounine 	 */
1947a11650e1SAlexandre Bounine 	list_for_each_entry(port, &rio_mports, node) {
19489edbc30bSAlexandre Bounine 		if (port->id == mport_id) {
1949a11650e1SAlexandre Bounine 			port->nscan = scan_ops;
1950a11650e1SAlexandre Bounine 			break;
19519edbc30bSAlexandre Bounine 		} else if (mport_id == RIO_MPORT_ANY && !port->nscan)
19529edbc30bSAlexandre Bounine 			port->nscan = scan_ops;
1953a11650e1SAlexandre Bounine 	}
19549edbc30bSAlexandre Bounine 
19559edbc30bSAlexandre Bounine 	list_add_tail(&scan->node, &rio_scans);
19569edbc30bSAlexandre Bounine 
19579edbc30bSAlexandre Bounine err_out:
1958a11650e1SAlexandre Bounine 	mutex_unlock(&rio_mport_list_lock);
1959a11650e1SAlexandre Bounine 
1960a11650e1SAlexandre Bounine 	return rc;
1961a11650e1SAlexandre Bounine }
1962a11650e1SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_register_scan);
1963a11650e1SAlexandre Bounine 
1964a11650e1SAlexandre Bounine /**
1965a11650e1SAlexandre Bounine  * rio_unregister_scan - removes enumeration/discovery method from mport
1966a11650e1SAlexandre Bounine  * @mport_id: mport device ID for which fabric scan routine has to be
19679edbc30bSAlexandre Bounine  *            unregistered (RIO_MPORT_ANY = apply to all mports that use
19689edbc30bSAlexandre Bounine  *            the specified scan_ops)
19699edbc30bSAlexandre Bounine  * @scan_ops: enumeration/discovery operations structure
1970a11650e1SAlexandre Bounine  *
1971a11650e1SAlexandre Bounine  * Removes enumeration or discovery method assigned to the specified mport
19729edbc30bSAlexandre Bounine  * device. If RIO_MPORT_ANY is specified, removes the specified operations from
19739edbc30bSAlexandre Bounine  * all mports that have them attached.
1974a11650e1SAlexandre Bounine  */
rio_unregister_scan(int mport_id,struct rio_scan * scan_ops)19759edbc30bSAlexandre Bounine int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
1976a11650e1SAlexandre Bounine {
1977a11650e1SAlexandre Bounine 	struct rio_mport *port;
19789edbc30bSAlexandre Bounine 	struct rio_scan_node *scan;
19799edbc30bSAlexandre Bounine 
19809edbc30bSAlexandre Bounine 	pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
19819edbc30bSAlexandre Bounine 
19829edbc30bSAlexandre Bounine 	if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
19839edbc30bSAlexandre Bounine 		return -EINVAL;
1984a11650e1SAlexandre Bounine 
1985a11650e1SAlexandre Bounine 	mutex_lock(&rio_mport_list_lock);
19869edbc30bSAlexandre Bounine 
19879edbc30bSAlexandre Bounine 	list_for_each_entry(port, &rio_mports, node)
19889edbc30bSAlexandre Bounine 		if (port->id == mport_id ||
19899edbc30bSAlexandre Bounine 		    (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
1990a11650e1SAlexandre Bounine 			port->nscan = NULL;
19919edbc30bSAlexandre Bounine 
1992f93f3c4eSDan Carpenter 	list_for_each_entry(scan, &rio_scans, node) {
19939edbc30bSAlexandre Bounine 		if (scan->mport_id == mport_id) {
19949edbc30bSAlexandre Bounine 			list_del(&scan->node);
19959edbc30bSAlexandre Bounine 			kfree(scan);
1996f93f3c4eSDan Carpenter 			break;
1997f93f3c4eSDan Carpenter 		}
1998a11650e1SAlexandre Bounine 	}
19999edbc30bSAlexandre Bounine 
2000a11650e1SAlexandre Bounine 	mutex_unlock(&rio_mport_list_lock);
2001a11650e1SAlexandre Bounine 
2002a11650e1SAlexandre Bounine 	return 0;
2003a11650e1SAlexandre Bounine }
2004a11650e1SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_unregister_scan);
2005a11650e1SAlexandre Bounine 
20069edbc30bSAlexandre Bounine /**
20079edbc30bSAlexandre Bounine  * rio_mport_scan - execute enumeration/discovery on the specified mport
20089edbc30bSAlexandre Bounine  * @mport_id: number (ID) of mport device
20099edbc30bSAlexandre Bounine  */
rio_mport_scan(int mport_id)20109edbc30bSAlexandre Bounine int rio_mport_scan(int mport_id)
20119edbc30bSAlexandre Bounine {
20129edbc30bSAlexandre Bounine 	struct rio_mport *port = NULL;
20139edbc30bSAlexandre Bounine 	int rc;
20149edbc30bSAlexandre Bounine 
20159edbc30bSAlexandre Bounine 	mutex_lock(&rio_mport_list_lock);
20169edbc30bSAlexandre Bounine 	list_for_each_entry(port, &rio_mports, node) {
20179edbc30bSAlexandre Bounine 		if (port->id == mport_id)
20189edbc30bSAlexandre Bounine 			goto found;
20199edbc30bSAlexandre Bounine 	}
20209edbc30bSAlexandre Bounine 	mutex_unlock(&rio_mport_list_lock);
20219edbc30bSAlexandre Bounine 	return -ENODEV;
20229edbc30bSAlexandre Bounine found:
20239edbc30bSAlexandre Bounine 	if (!port->nscan) {
20249edbc30bSAlexandre Bounine 		mutex_unlock(&rio_mport_list_lock);
20259edbc30bSAlexandre Bounine 		return -EINVAL;
20269edbc30bSAlexandre Bounine 	}
20279edbc30bSAlexandre Bounine 
20289edbc30bSAlexandre Bounine 	if (!try_module_get(port->nscan->owner)) {
20299edbc30bSAlexandre Bounine 		mutex_unlock(&rio_mport_list_lock);
20309edbc30bSAlexandre Bounine 		return -ENODEV;
20319edbc30bSAlexandre Bounine 	}
20329edbc30bSAlexandre Bounine 
20339edbc30bSAlexandre Bounine 	mutex_unlock(&rio_mport_list_lock);
20349edbc30bSAlexandre Bounine 
20359edbc30bSAlexandre Bounine 	if (port->host_deviceid >= 0)
20369edbc30bSAlexandre Bounine 		rc = port->nscan->enumerate(port, 0);
20379edbc30bSAlexandre Bounine 	else
20389edbc30bSAlexandre Bounine 		rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
20399edbc30bSAlexandre Bounine 
20409edbc30bSAlexandre Bounine 	module_put(port->nscan->owner);
20419edbc30bSAlexandre Bounine 	return rc;
20429edbc30bSAlexandre Bounine }
20439edbc30bSAlexandre Bounine 
2044005842efSAlexandre Bounine static struct workqueue_struct *rio_wq;
2045005842efSAlexandre Bounine 
2046005842efSAlexandre Bounine struct rio_disc_work {
2047005842efSAlexandre Bounine 	struct work_struct	work;
2048005842efSAlexandre Bounine 	struct rio_mport	*mport;
2049005842efSAlexandre Bounine };
2050005842efSAlexandre Bounine 
disc_work_handler(struct work_struct * _work)2051305c891eSBill Pemberton static void disc_work_handler(struct work_struct *_work)
2052005842efSAlexandre Bounine {
2053005842efSAlexandre Bounine 	struct rio_disc_work *work;
2054005842efSAlexandre Bounine 
2055005842efSAlexandre Bounine 	work = container_of(_work, struct rio_disc_work, work);
2056005842efSAlexandre Bounine 	pr_debug("RIO: discovery work for mport %d %s\n",
2057005842efSAlexandre Bounine 		 work->mport->id, work->mport->name);
20589edbc30bSAlexandre Bounine 	if (try_module_get(work->mport->nscan->owner)) {
2059bc8fcfeaSAlexandre Bounine 		work->mport->nscan->discover(work->mport, 0);
20609edbc30bSAlexandre Bounine 		module_put(work->mport->nscan->owner);
20619edbc30bSAlexandre Bounine 	}
2062005842efSAlexandre Bounine }
2063005842efSAlexandre Bounine 
rio_init_mports(void)2064305c891eSBill Pemberton int rio_init_mports(void)
2065394b701cSMatt Porter {
2066394b701cSMatt Porter 	struct rio_mport *port;
2067005842efSAlexandre Bounine 	struct rio_disc_work *work;
20682574740dSAlexandre Bounine 	int n = 0;
2069394b701cSMatt Porter 
20702574740dSAlexandre Bounine 	if (!next_portid)
20712574740dSAlexandre Bounine 		return -ENODEV;
20722574740dSAlexandre Bounine 
20732574740dSAlexandre Bounine 	/*
20742574740dSAlexandre Bounine 	 * First, run enumerations and check if we need to perform discovery
20752574740dSAlexandre Bounine 	 * on any of the registered mports.
20762574740dSAlexandre Bounine 	 */
2077a11650e1SAlexandre Bounine 	mutex_lock(&rio_mport_list_lock);
2078394b701cSMatt Porter 	list_for_each_entry(port, &rio_mports, node) {
2079a11650e1SAlexandre Bounine 		if (port->host_deviceid >= 0) {
20809edbc30bSAlexandre Bounine 			if (port->nscan && try_module_get(port->nscan->owner)) {
2081bc8fcfeaSAlexandre Bounine 				port->nscan->enumerate(port, 0);
20829edbc30bSAlexandre Bounine 				module_put(port->nscan->owner);
20839edbc30bSAlexandre Bounine 			}
2084a11650e1SAlexandre Bounine 		} else
20852574740dSAlexandre Bounine 			n++;
20862574740dSAlexandre Bounine 	}
2087a11650e1SAlexandre Bounine 	mutex_unlock(&rio_mport_list_lock);
20882574740dSAlexandre Bounine 
20892574740dSAlexandre Bounine 	if (!n)
20902574740dSAlexandre Bounine 		goto no_disc;
20912574740dSAlexandre Bounine 
20922574740dSAlexandre Bounine 	/*
20932574740dSAlexandre Bounine 	 * If we have mports that require discovery schedule a discovery work
20942574740dSAlexandre Bounine 	 * for each of them. If the code below fails to allocate needed
20952574740dSAlexandre Bounine 	 * resources, exit without error to keep results of enumeration
20962574740dSAlexandre Bounine 	 * process (if any).
20979edbc30bSAlexandre Bounine 	 * TODO: Implement restart of discovery process for all or
20982574740dSAlexandre Bounine 	 * individual discovering mports.
20992574740dSAlexandre Bounine 	 */
2100005842efSAlexandre Bounine 	rio_wq = alloc_workqueue("riodisc", 0, 0);
2101005842efSAlexandre Bounine 	if (!rio_wq) {
2102005842efSAlexandre Bounine 		pr_err("RIO: unable allocate rio_wq\n");
21032574740dSAlexandre Bounine 		goto no_disc;
2104005842efSAlexandre Bounine 	}
2105005842efSAlexandre Bounine 
21062574740dSAlexandre Bounine 	work = kcalloc(n, sizeof *work, GFP_KERNEL);
2107005842efSAlexandre Bounine 	if (!work) {
2108005842efSAlexandre Bounine 		destroy_workqueue(rio_wq);
21092574740dSAlexandre Bounine 		goto no_disc;
2110394b701cSMatt Porter 	}
2111394b701cSMatt Porter 
21122574740dSAlexandre Bounine 	n = 0;
2113a11650e1SAlexandre Bounine 	mutex_lock(&rio_mport_list_lock);
21142574740dSAlexandre Bounine 	list_for_each_entry(port, &rio_mports, node) {
2115a11650e1SAlexandre Bounine 		if (port->host_deviceid < 0 && port->nscan) {
21162574740dSAlexandre Bounine 			work[n].mport = port;
21172574740dSAlexandre Bounine 			INIT_WORK(&work[n].work, disc_work_handler);
21182574740dSAlexandre Bounine 			queue_work(rio_wq, &work[n].work);
21192574740dSAlexandre Bounine 			n++;
21202574740dSAlexandre Bounine 		}
21212574740dSAlexandre Bounine 	}
21222574740dSAlexandre Bounine 
21232574740dSAlexandre Bounine 	flush_workqueue(rio_wq);
21249edbc30bSAlexandre Bounine 	mutex_unlock(&rio_mport_list_lock);
21252574740dSAlexandre Bounine 	pr_debug("RIO: destroy discovery workqueue\n");
21262574740dSAlexandre Bounine 	destroy_workqueue(rio_wq);
21272574740dSAlexandre Bounine 	kfree(work);
21282574740dSAlexandre Bounine 
21292574740dSAlexandre Bounine no_disc:
2130c1256ebeSAlexandre Bounine 	return 0;
2131394b701cSMatt Porter }
21324ba61ecaSMarkus Elfring EXPORT_SYMBOL_GPL(rio_init_mports);
2133394b701cSMatt Porter 
rio_get_hdid(int index)2134569fccb6SAlexandre Bounine static int rio_get_hdid(int index)
2135569fccb6SAlexandre Bounine {
2136fdf90abcSAlexandre Bounine 	if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
2137569fccb6SAlexandre Bounine 		return -1;
2138569fccb6SAlexandre Bounine 
2139fdf90abcSAlexandre Bounine 	return hdid[index];
2140569fccb6SAlexandre Bounine }
2141569fccb6SAlexandre Bounine 
rio_mport_initialize(struct rio_mport * mport)2142b77a2030SAlexandre Bounine int rio_mport_initialize(struct rio_mport *mport)
2143b77a2030SAlexandre Bounine {
2144b77a2030SAlexandre Bounine 	if (next_portid >= RIO_MAX_MPORTS) {
2145b77a2030SAlexandre Bounine 		pr_err("RIO: reached specified max number of mports\n");
2146b77a2030SAlexandre Bounine 		return -ENODEV;
2147b77a2030SAlexandre Bounine 	}
2148b77a2030SAlexandre Bounine 
2149b77a2030SAlexandre Bounine 	atomic_set(&mport->state, RIO_DEVICE_INITIALIZING);
2150b77a2030SAlexandre Bounine 	mport->id = next_portid++;
2151b77a2030SAlexandre Bounine 	mport->host_deviceid = rio_get_hdid(mport->id);
2152b77a2030SAlexandre Bounine 	mport->nscan = NULL;
2153a7b4c636SAlexandre Bounine 	mutex_init(&mport->lock);
2154b6cb95e8SAlexandre Bounine 	mport->pwe_refcnt = 0;
21559a0b0627SAlexandre Bounine 	INIT_LIST_HEAD(&mport->pwrites);
2156b77a2030SAlexandre Bounine 
2157b77a2030SAlexandre Bounine 	return 0;
2158b77a2030SAlexandre Bounine }
2159b77a2030SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_mport_initialize);
2160b77a2030SAlexandre Bounine 
rio_register_mport(struct rio_mport * port)216159f99965SAlexandre Bounine int rio_register_mport(struct rio_mport *port)
2162394b701cSMatt Porter {
21639edbc30bSAlexandre Bounine 	struct rio_scan_node *scan = NULL;
21642aaf308bSAlexandre Bounine 	int res = 0;
21659edbc30bSAlexandre Bounine 
2166a11650e1SAlexandre Bounine 	mutex_lock(&rio_mport_list_lock);
21679edbc30bSAlexandre Bounine 
21689edbc30bSAlexandre Bounine 	/*
21699edbc30bSAlexandre Bounine 	 * Check if there are any registered enumeration/discovery operations
21709edbc30bSAlexandre Bounine 	 * that have to be attached to the added mport.
21719edbc30bSAlexandre Bounine 	 */
21729edbc30bSAlexandre Bounine 	list_for_each_entry(scan, &rio_scans, node) {
21739edbc30bSAlexandre Bounine 		if (port->id == scan->mport_id ||
21749edbc30bSAlexandre Bounine 		    scan->mport_id == RIO_MPORT_ANY) {
21759edbc30bSAlexandre Bounine 			port->nscan = scan->ops;
21769edbc30bSAlexandre Bounine 			if (port->id == scan->mport_id)
21779edbc30bSAlexandre Bounine 				break;
21789edbc30bSAlexandre Bounine 		}
21799edbc30bSAlexandre Bounine 	}
2180b77a2030SAlexandre Bounine 
2181b77a2030SAlexandre Bounine 	list_add_tail(&port->node, &rio_mports);
2182a11650e1SAlexandre Bounine 	mutex_unlock(&rio_mport_list_lock);
21839edbc30bSAlexandre Bounine 
2184b77a2030SAlexandre Bounine 	dev_set_name(&port->dev, "rapidio%d", port->id);
2185b77a2030SAlexandre Bounine 	port->dev.class = &rio_mport_class;
2186b77a2030SAlexandre Bounine 	atomic_set(&port->state, RIO_DEVICE_RUNNING);
2187b77a2030SAlexandre Bounine 
2188b77a2030SAlexandre Bounine 	res = device_register(&port->dev);
2189*e92a216dSYang Yingliang 	if (res) {
2190b77a2030SAlexandre Bounine 		dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
2191b77a2030SAlexandre Bounine 			port->id, res);
2192*e92a216dSYang Yingliang 		mutex_lock(&rio_mport_list_lock);
2193*e92a216dSYang Yingliang 		list_del(&port->node);
2194*e92a216dSYang Yingliang 		mutex_unlock(&rio_mport_list_lock);
2195*e92a216dSYang Yingliang 		put_device(&port->dev);
2196*e92a216dSYang Yingliang 	} else {
2197b77a2030SAlexandre Bounine 		dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id);
2198*e92a216dSYang Yingliang 	}
2199b77a2030SAlexandre Bounine 
2200b77a2030SAlexandre Bounine 	return res;
2201394b701cSMatt Porter }
220294d9bd45SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_register_mport);
2203394b701cSMatt Porter 
rio_mport_cleanup_callback(struct device * dev,void * data)2204b77a2030SAlexandre Bounine static int rio_mport_cleanup_callback(struct device *dev, void *data)
2205b77a2030SAlexandre Bounine {
2206b77a2030SAlexandre Bounine 	struct rio_dev *rdev = to_rio_dev(dev);
2207b77a2030SAlexandre Bounine 
2208b77a2030SAlexandre Bounine 	if (dev->bus == &rio_bus_type)
2209b77a2030SAlexandre Bounine 		rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
2210b77a2030SAlexandre Bounine 	return 0;
2211b77a2030SAlexandre Bounine }
2212b77a2030SAlexandre Bounine 
rio_net_remove_children(struct rio_net * net)2213b77a2030SAlexandre Bounine static int rio_net_remove_children(struct rio_net *net)
2214b77a2030SAlexandre Bounine {
2215b77a2030SAlexandre Bounine 	/*
2216b77a2030SAlexandre Bounine 	 * Unregister all RapidIO devices residing on this net (this will
2217b77a2030SAlexandre Bounine 	 * invoke notification of registered subsystem interfaces as well).
2218b77a2030SAlexandre Bounine 	 */
2219b77a2030SAlexandre Bounine 	device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback);
2220b77a2030SAlexandre Bounine 	return 0;
2221b77a2030SAlexandre Bounine }
2222b77a2030SAlexandre Bounine 
rio_unregister_mport(struct rio_mport * port)2223b77a2030SAlexandre Bounine int rio_unregister_mport(struct rio_mport *port)
2224b77a2030SAlexandre Bounine {
2225b77a2030SAlexandre Bounine 	pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
2226b77a2030SAlexandre Bounine 
2227b77a2030SAlexandre Bounine 	/* Transition mport to the SHUTDOWN state */
2228b77a2030SAlexandre Bounine 	if (atomic_cmpxchg(&port->state,
2229b77a2030SAlexandre Bounine 			   RIO_DEVICE_RUNNING,
2230b77a2030SAlexandre Bounine 			   RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) {
2231b77a2030SAlexandre Bounine 		pr_err("RIO: %s unexpected state transition for mport %s\n",
2232b77a2030SAlexandre Bounine 			__func__, port->name);
2233b77a2030SAlexandre Bounine 	}
2234b77a2030SAlexandre Bounine 
2235b77a2030SAlexandre Bounine 	if (port->net && port->net->hport == port) {
2236b77a2030SAlexandre Bounine 		rio_net_remove_children(port->net);
2237b77a2030SAlexandre Bounine 		rio_free_net(port->net);
2238b77a2030SAlexandre Bounine 	}
2239b77a2030SAlexandre Bounine 
2240b77a2030SAlexandre Bounine 	/*
2241b77a2030SAlexandre Bounine 	 * Unregister all RapidIO devices attached to this mport (this will
2242b77a2030SAlexandre Bounine 	 * invoke notification of registered subsystem interfaces as well).
2243b77a2030SAlexandre Bounine 	 */
2244b77a2030SAlexandre Bounine 	mutex_lock(&rio_mport_list_lock);
2245b77a2030SAlexandre Bounine 	list_del(&port->node);
2246b77a2030SAlexandre Bounine 	mutex_unlock(&rio_mport_list_lock);
2247b77a2030SAlexandre Bounine 	device_unregister(&port->dev);
2248b77a2030SAlexandre Bounine 
2249b77a2030SAlexandre Bounine 	return 0;
2250b77a2030SAlexandre Bounine }
2251b77a2030SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_unregister_mport);
2252