blob: b9dc932ce19e039dc862085d84339dce2b587e39 [file] [log] [blame]
Matt Porter394b7012005-11-07 01:00:15 -08001/*
2 * RapidIO interconnect services
3 * (RapidIO Interconnect Specification, http://www.rapidio.org)
4 *
5 * Copyright 2005 MontaVista Software, Inc.
6 * Matt Porter <mporter@kernel.crashing.org>
7 *
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07008 * Copyright 2009 - 2013 Integrated Device Technology, Inc.
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07009 * Alex Bounine <alexandre.bounine@idt.com>
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -070010 *
Matt Porter394b7012005-11-07 01:00:15 -080011 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
Matt Porter394b7012005-11-07 01:00:15 -080017#include <linux/types.h>
18#include <linux/kernel.h>
19
20#include <linux/delay.h>
21#include <linux/init.h>
22#include <linux/rio.h>
23#include <linux/rio_drv.h>
24#include <linux/rio_ids.h>
25#include <linux/rio_regs.h>
26#include <linux/module.h>
27#include <linux/spinlock.h>
Tim Schmielaude259682006-01-08 01:02:05 -080028#include <linux/slab.h>
Kumar Gala5febf1c2008-01-23 05:53:47 -060029#include <linux/interrupt.h>
Matt Porter394b7012005-11-07 01:00:15 -080030
31#include "rio.h"
32
Alexandre Bounine9a0b0622016-03-22 14:26:44 -070033/*
34 * struct rio_pwrite - RIO portwrite event
35 * @node: Node in list of doorbell events
36 * @pwcback: Doorbell event callback
37 * @context: Handler specific context to pass on event
38 */
39struct rio_pwrite {
40 struct list_head node;
41
42 int (*pwcback)(struct rio_mport *mport, void *context,
43 union rio_pw_msg *msg, int step);
44 void *context;
45};
46
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -070047MODULE_DESCRIPTION("RapidIO Subsystem Core");
48MODULE_AUTHOR("Matt Porter <mporter@kernel.crashing.org>");
49MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
50MODULE_LICENSE("GPL");
51
52static int hdid[RIO_MAX_MPORTS];
53static int ids_num;
54module_param_array(hdid, int, &ids_num, 0);
55MODULE_PARM_DESC(hdid,
56 "Destination ID assignment to local RapidIO controllers");
57
Alexandre Bouninea11650e2013-05-24 15:55:05 -070058static LIST_HEAD(rio_devices);
Alexandre Bouninee6b585c2016-03-22 14:26:17 -070059static LIST_HEAD(rio_nets);
Alexandre Bouninea11650e2013-05-24 15:55:05 -070060static DEFINE_SPINLOCK(rio_global_list_lock);
61
Matt Porter394b7012005-11-07 01:00:15 -080062static LIST_HEAD(rio_mports);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -070063static LIST_HEAD(rio_scans);
Alexandre Bouninea11650e2013-05-24 15:55:05 -070064static DEFINE_MUTEX(rio_mport_list_lock);
Alexandre Bounine569fccb2011-03-23 16:43:05 -070065static unsigned char next_portid;
Alexandre Bounineda1589f2012-10-04 17:15:57 -070066static DEFINE_SPINLOCK(rio_mmap_lock);
Matt Porter394b7012005-11-07 01:00:15 -080067
68/**
69 * rio_local_get_device_id - Get the base/extended device id for a port
70 * @port: RIO master port from which to get the deviceid
71 *
72 * Reads the base/extended device id from the local device
73 * implementing the master port. Returns the 8/16-bit device
74 * id.
75 */
76u16 rio_local_get_device_id(struct rio_mport *port)
77{
78 u32 result;
79
80 rio_local_read_config_32(port, RIO_DID_CSR, &result);
81
Zhang Weie0423232008-04-18 13:33:42 -070082 return (RIO_GET_DID(port->sys_size, result));
Matt Porter394b7012005-11-07 01:00:15 -080083}
84
85/**
Alexandre Bounine8b189fd2016-03-22 14:26:00 -070086 * rio_query_mport - Query mport device attributes
87 * @port: mport device to query
88 * @mport_attr: mport attributes data structure
89 *
90 * Returns attributes of specified mport through the
91 * pointer to attributes data structure.
92 */
93int rio_query_mport(struct rio_mport *port,
94 struct rio_mport_attr *mport_attr)
95{
96 if (!port->ops->query_mport)
97 return -ENODATA;
98 return port->ops->query_mport(port, mport_attr);
99}
100EXPORT_SYMBOL(rio_query_mport);
101
102/**
Alexandre Bouninee6b585c2016-03-22 14:26:17 -0700103 * rio_alloc_net- Allocate and initialize a new RIO network data structure
104 * @mport: Master port associated with the RIO network
105 *
106 * Allocates a RIO network structure, initializes per-network
107 * list heads, and adds the associated master port to the
108 * network list of associated master ports. Returns a
109 * RIO network pointer on success or %NULL on failure.
110 */
111struct rio_net *rio_alloc_net(struct rio_mport *mport)
112{
113 struct rio_net *net;
114
115 net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
116 if (net) {
117 INIT_LIST_HEAD(&net->node);
118 INIT_LIST_HEAD(&net->devices);
119 INIT_LIST_HEAD(&net->switches);
120 INIT_LIST_HEAD(&net->mports);
121 mport->net = net;
122 }
123 return net;
124}
125EXPORT_SYMBOL_GPL(rio_alloc_net);
126
127int rio_add_net(struct rio_net *net)
128{
129 int err;
130
131 err = device_register(&net->dev);
132 if (err)
133 return err;
134 spin_lock(&rio_global_list_lock);
135 list_add_tail(&net->node, &rio_nets);
136 spin_unlock(&rio_global_list_lock);
137
138 return 0;
139}
140EXPORT_SYMBOL_GPL(rio_add_net);
141
142void rio_free_net(struct rio_net *net)
143{
144 spin_lock(&rio_global_list_lock);
145 if (!list_empty(&net->node))
146 list_del(&net->node);
147 spin_unlock(&rio_global_list_lock);
148 if (net->release)
149 net->release(net);
150 device_unregister(&net->dev);
151}
152EXPORT_SYMBOL_GPL(rio_free_net);
153
154/**
Alexandre Bounine50246222016-03-22 14:26:38 -0700155 * rio_local_set_device_id - Set the base/extended device id for a port
156 * @port: RIO master port
157 * @did: Device ID value to be written
158 *
159 * Writes the base/extended device id from a device.
160 */
161void rio_local_set_device_id(struct rio_mport *port, u16 did)
162{
163 rio_local_write_config_32(port, RIO_DID_CSR,
164 RIO_SET_DID(port->sys_size, did));
165}
166EXPORT_SYMBOL_GPL(rio_local_set_device_id);
167
168/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700169 * rio_add_device- Adds a RIO device to the device model
170 * @rdev: RIO device
171 *
172 * Adds the RIO device to the global device list and adds the RIO
173 * device to the RIO device list. Creates the generic sysfs nodes
174 * for an RIO device.
175 */
176int rio_add_device(struct rio_dev *rdev)
177{
178 int err;
179
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700180 atomic_set(&rdev->state, RIO_DEVICE_RUNNING);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700181 err = device_register(&rdev->dev);
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700182 if (err)
183 return err;
184
185 spin_lock(&rio_global_list_lock);
186 list_add_tail(&rdev->global_list, &rio_devices);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700187 if (rdev->net) {
188 list_add_tail(&rdev->net_list, &rdev->net->devices);
189 if (rdev->pef & RIO_PEF_SWITCH)
190 list_add_tail(&rdev->rswitch->node,
191 &rdev->net->switches);
192 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700193 spin_unlock(&rio_global_list_lock);
194
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700195 return 0;
196}
197EXPORT_SYMBOL_GPL(rio_add_device);
198
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700199/*
200 * rio_del_device - removes a RIO device from the device model
201 * @rdev: RIO device
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700202 * @state: device state to set during removal process
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700203 *
204 * Removes the RIO device to the kernel device list and subsystem's device list.
205 * Clears sysfs entries for the removed device.
206 */
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700207void rio_del_device(struct rio_dev *rdev, enum rio_device_state state)
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700208{
209 pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev));
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700210 atomic_set(&rdev->state, state);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700211 spin_lock(&rio_global_list_lock);
212 list_del(&rdev->global_list);
213 if (rdev->net) {
214 list_del(&rdev->net_list);
215 if (rdev->pef & RIO_PEF_SWITCH) {
216 list_del(&rdev->rswitch->node);
217 kfree(rdev->rswitch->route_table);
218 }
219 }
220 spin_unlock(&rio_global_list_lock);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700221 device_unregister(&rdev->dev);
222}
223EXPORT_SYMBOL_GPL(rio_del_device);
224
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700225/**
Matt Porter394b7012005-11-07 01:00:15 -0800226 * rio_request_inb_mbox - request inbound mailbox service
227 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800228 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800229 * @mbox: Mailbox number to claim
230 * @entries: Number of entries in inbound mailbox queue
231 * @minb: Callback to execute when inbound message is received
232 *
233 * Requests ownership of an inbound mailbox resource and binds
234 * a callback function to the resource. Returns %0 on success.
235 */
236int rio_request_inb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800237 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800238 int mbox,
239 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800240 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
Matt Porter394b7012005-11-07 01:00:15 -0800241 int slot))
242{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700243 int rc = -ENOSYS;
244 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800245
Markus Elfring93dd49a2018-02-06 15:39:44 -0800246 if (!mport->ops->open_inb_mbox)
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700247 goto out;
248
Toshi Kani9a975be2016-01-26 21:57:25 +0100249 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800250
251 if (res) {
252 rio_init_mbox_res(res, mbox, mbox);
253
254 /* Make sure this mailbox isn't in use */
Markus Elfringe1d66d02018-02-06 15:39:48 -0800255 rc = request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
256 res);
257 if (rc < 0) {
Matt Porter394b7012005-11-07 01:00:15 -0800258 kfree(res);
259 goto out;
260 }
261
262 mport->inb_msg[mbox].res = res;
263
264 /* Hook the inbound message callback */
265 mport->inb_msg[mbox].mcback = minb;
266
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700267 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700268 if (rc) {
269 mport->inb_msg[mbox].mcback = NULL;
270 mport->inb_msg[mbox].res = NULL;
271 release_resource(res);
272 kfree(res);
273 }
Matt Porter394b7012005-11-07 01:00:15 -0800274 } else
275 rc = -ENOMEM;
276
277 out:
278 return rc;
279}
280
281/**
282 * rio_release_inb_mbox - release inbound mailbox message service
283 * @mport: RIO master port from which to release the mailbox resource
284 * @mbox: Mailbox number to release
285 *
286 * Releases ownership of an inbound mailbox resource. Returns 0
287 * if the request has been satisfied.
288 */
289int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
290{
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700291 int rc;
Matt Porter394b7012005-11-07 01:00:15 -0800292
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700293 if (!mport->ops->close_inb_mbox || !mport->inb_msg[mbox].res)
294 return -EINVAL;
295
296 mport->ops->close_inb_mbox(mport, mbox);
297 mport->inb_msg[mbox].mcback = NULL;
298
299 rc = release_resource(mport->inb_msg[mbox].res);
300 if (rc)
301 return rc;
302
303 kfree(mport->inb_msg[mbox].res);
304 mport->inb_msg[mbox].res = NULL;
305
306 return 0;
Matt Porter394b7012005-11-07 01:00:15 -0800307}
308
309/**
310 * rio_request_outb_mbox - request outbound mailbox service
311 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800312 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800313 * @mbox: Mailbox number to claim
314 * @entries: Number of entries in outbound mailbox queue
315 * @moutb: Callback to execute when outbound message is sent
316 *
317 * Requests ownership of an outbound mailbox resource and binds
318 * a callback function to the resource. Returns 0 on success.
319 */
320int rio_request_outb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800321 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800322 int mbox,
323 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800324 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
Matt Porter394b7012005-11-07 01:00:15 -0800325{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700326 int rc = -ENOSYS;
327 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800328
Markus Elfring93dd49a2018-02-06 15:39:44 -0800329 if (!mport->ops->open_outb_mbox)
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700330 goto out;
331
Toshi Kani9a975be2016-01-26 21:57:25 +0100332 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800333
334 if (res) {
335 rio_init_mbox_res(res, mbox, mbox);
336
337 /* Make sure this outbound mailbox isn't in use */
Markus Elfringe1d66d02018-02-06 15:39:48 -0800338 rc = request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
339 res);
340 if (rc < 0) {
Matt Porter394b7012005-11-07 01:00:15 -0800341 kfree(res);
342 goto out;
343 }
344
345 mport->outb_msg[mbox].res = res;
346
347 /* Hook the inbound message callback */
348 mport->outb_msg[mbox].mcback = moutb;
349
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700350 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700351 if (rc) {
352 mport->outb_msg[mbox].mcback = NULL;
353 mport->outb_msg[mbox].res = NULL;
354 release_resource(res);
355 kfree(res);
356 }
Matt Porter394b7012005-11-07 01:00:15 -0800357 } else
358 rc = -ENOMEM;
359
360 out:
361 return rc;
362}
363
364/**
365 * rio_release_outb_mbox - release outbound mailbox message service
366 * @mport: RIO master port from which to release the mailbox resource
367 * @mbox: Mailbox number to release
368 *
369 * Releases ownership of an inbound mailbox resource. Returns 0
370 * if the request has been satisfied.
371 */
372int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
373{
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700374 int rc;
Matt Porter394b7012005-11-07 01:00:15 -0800375
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700376 if (!mport->ops->close_outb_mbox || !mport->outb_msg[mbox].res)
377 return -EINVAL;
378
379 mport->ops->close_outb_mbox(mport, mbox);
380 mport->outb_msg[mbox].mcback = NULL;
381
382 rc = release_resource(mport->outb_msg[mbox].res);
383 if (rc)
384 return rc;
385
386 kfree(mport->outb_msg[mbox].res);
387 mport->outb_msg[mbox].res = NULL;
388
389 return 0;
Matt Porter394b7012005-11-07 01:00:15 -0800390}
391
392/**
393 * rio_setup_inb_dbell - bind inbound doorbell callback
394 * @mport: RIO master port to bind the doorbell callback
Matt Porter6978bbc2005-11-07 01:00:20 -0800395 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800396 * @res: Doorbell message resource
397 * @dinb: Callback to execute when doorbell is received
398 *
399 * Adds a doorbell resource/callback pair into a port's
400 * doorbell event list. Returns 0 if the request has been
401 * satisfied.
402 */
403static int
Matt Porter6978bbc2005-11-07 01:00:20 -0800404rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
405 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
Matt Porter394b7012005-11-07 01:00:15 -0800406 u16 info))
407{
408 int rc = 0;
Markus Elfringe1d66d02018-02-06 15:39:48 -0800409 struct rio_dbell *dbell = kmalloc(sizeof(*dbell), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800410
Markus Elfringe1d66d02018-02-06 15:39:48 -0800411 if (!dbell) {
Matt Porter394b7012005-11-07 01:00:15 -0800412 rc = -ENOMEM;
413 goto out;
414 }
415
416 dbell->res = res;
417 dbell->dinb = dinb;
Matt Porter6978bbc2005-11-07 01:00:20 -0800418 dbell->dev_id = dev_id;
Matt Porter394b7012005-11-07 01:00:15 -0800419
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700420 mutex_lock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800421 list_add_tail(&dbell->node, &mport->dbells);
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700422 mutex_unlock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800423
424 out:
425 return rc;
426}
427
428/**
429 * rio_request_inb_dbell - request inbound doorbell message service
430 * @mport: RIO master port from which to allocate the doorbell resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800431 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800432 * @start: Doorbell info range start
433 * @end: Doorbell info range end
434 * @dinb: Callback to execute when doorbell is received
435 *
436 * Requests ownership of an inbound doorbell resource and binds
437 * a callback function to the resource. Returns 0 if the request
438 * has been satisfied.
439 */
440int rio_request_inb_dbell(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800441 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800442 u16 start,
443 u16 end,
Matt Porter6978bbc2005-11-07 01:00:20 -0800444 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
Matt Porter394b7012005-11-07 01:00:15 -0800445 u16 dst, u16 info))
446{
447 int rc = 0;
448
Toshi Kani9a975be2016-01-26 21:57:25 +0100449 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800450
451 if (res) {
452 rio_init_dbell_res(res, start, end);
453
454 /* Make sure these doorbells aren't in use */
Markus Elfringe1d66d02018-02-06 15:39:48 -0800455 rc = request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
456 res);
457 if (rc < 0) {
Matt Porter394b7012005-11-07 01:00:15 -0800458 kfree(res);
459 goto out;
460 }
461
462 /* Hook the doorbell callback */
Matt Porter6978bbc2005-11-07 01:00:20 -0800463 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
Matt Porter394b7012005-11-07 01:00:15 -0800464 } else
465 rc = -ENOMEM;
466
467 out:
468 return rc;
469}
470
471/**
472 * rio_release_inb_dbell - release inbound doorbell message service
473 * @mport: RIO master port from which to release the doorbell resource
474 * @start: Doorbell info range start
475 * @end: Doorbell info range end
476 *
477 * Releases ownership of an inbound doorbell resource and removes
478 * callback from the doorbell event list. Returns 0 if the request
479 * has been satisfied.
480 */
481int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
482{
483 int rc = 0, found = 0;
484 struct rio_dbell *dbell;
485
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700486 mutex_lock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800487 list_for_each_entry(dbell, &mport->dbells, node) {
488 if ((dbell->res->start == start) && (dbell->res->end == end)) {
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700489 list_del(&dbell->node);
Matt Porter394b7012005-11-07 01:00:15 -0800490 found = 1;
491 break;
492 }
493 }
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700494 mutex_unlock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800495
496 /* If we can't find an exact match, fail */
497 if (!found) {
498 rc = -EINVAL;
499 goto out;
500 }
501
Matt Porter394b7012005-11-07 01:00:15 -0800502 /* Release the doorbell resource */
503 rc = release_resource(dbell->res);
504
505 /* Free the doorbell event */
506 kfree(dbell);
507
508 out:
509 return rc;
510}
511
512/**
513 * rio_request_outb_dbell - request outbound doorbell message range
514 * @rdev: RIO device from which to allocate the doorbell resource
515 * @start: Doorbell message range start
516 * @end: Doorbell message range end
517 *
518 * Requests ownership of a doorbell message range. Returns a resource
519 * if the request has been satisfied or %NULL on failure.
520 */
521struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
522 u16 end)
523{
Toshi Kani9a975be2016-01-26 21:57:25 +0100524 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800525
526 if (res) {
527 rio_init_dbell_res(res, start, end);
528
529 /* Make sure these doorbells aren't in use */
530 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
531 < 0) {
532 kfree(res);
533 res = NULL;
534 }
535 }
536
537 return res;
538}
539
540/**
541 * rio_release_outb_dbell - release outbound doorbell message range
542 * @rdev: RIO device from which to release the doorbell resource
543 * @res: Doorbell resource to be freed
544 *
545 * Releases ownership of a doorbell message range. Returns 0 if the
546 * request has been satisfied.
547 */
548int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
549{
550 int rc = release_resource(res);
551
552 kfree(res);
553
554 return rc;
555}
556
557/**
Alexandre Bounine9a0b0622016-03-22 14:26:44 -0700558 * rio_add_mport_pw_handler - add port-write message handler into the list
559 * of mport specific pw handlers
560 * @mport: RIO master port to bind the portwrite callback
561 * @context: Handler specific context to pass on event
562 * @pwcback: Callback to execute when portwrite is received
563 *
564 * Returns 0 if the request has been satisfied.
565 */
566int rio_add_mport_pw_handler(struct rio_mport *mport, void *context,
567 int (*pwcback)(struct rio_mport *mport,
568 void *context, union rio_pw_msg *msg, int step))
569{
570 int rc = 0;
571 struct rio_pwrite *pwrite;
572
573 pwrite = kzalloc(sizeof(struct rio_pwrite), GFP_KERNEL);
574 if (!pwrite) {
575 rc = -ENOMEM;
576 goto out;
577 }
578
579 pwrite->pwcback = pwcback;
580 pwrite->context = context;
581 mutex_lock(&mport->lock);
582 list_add_tail(&pwrite->node, &mport->pwrites);
583 mutex_unlock(&mport->lock);
584out:
585 return rc;
586}
587EXPORT_SYMBOL_GPL(rio_add_mport_pw_handler);
588
589/**
590 * rio_del_mport_pw_handler - remove port-write message handler from the list
591 * of mport specific pw handlers
592 * @mport: RIO master port to bind the portwrite callback
593 * @context: Registered handler specific context to pass on event
594 * @pwcback: Registered callback function
595 *
596 * Returns 0 if the request has been satisfied.
597 */
598int rio_del_mport_pw_handler(struct rio_mport *mport, void *context,
599 int (*pwcback)(struct rio_mport *mport,
600 void *context, union rio_pw_msg *msg, int step))
601{
602 int rc = -EINVAL;
603 struct rio_pwrite *pwrite;
604
605 mutex_lock(&mport->lock);
606 list_for_each_entry(pwrite, &mport->pwrites, node) {
607 if (pwrite->pwcback == pwcback && pwrite->context == context) {
608 list_del(&pwrite->node);
609 kfree(pwrite);
610 rc = 0;
611 break;
612 }
613 }
614 mutex_unlock(&mport->lock);
615
616 return rc;
617}
618EXPORT_SYMBOL_GPL(rio_del_mport_pw_handler);
619
620/**
621 * rio_request_inb_pwrite - request inbound port-write message service for
622 * specific RapidIO device
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700623 * @rdev: RIO device to which register inbound port-write callback routine
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700624 * @pwcback: Callback routine to execute when port-write is received
625 *
626 * Binds a port-write callback function to the RapidIO device.
627 * Returns 0 if the request has been satisfied.
628 */
629int rio_request_inb_pwrite(struct rio_dev *rdev,
630 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
631{
632 int rc = 0;
633
634 spin_lock(&rio_global_list_lock);
Markus Elfring93dd49a2018-02-06 15:39:44 -0800635 if (rdev->pwcback)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700636 rc = -ENOMEM;
637 else
638 rdev->pwcback = pwcback;
639
640 spin_unlock(&rio_global_list_lock);
641 return rc;
642}
643EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
644
645/**
646 * rio_release_inb_pwrite - release inbound port-write message service
Alexandre Bounine9a0b0622016-03-22 14:26:44 -0700647 * associated with specific RapidIO device
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700648 * @rdev: RIO device which registered for inbound port-write callback
649 *
650 * Removes callback from the rio_dev structure. Returns 0 if the request
651 * has been satisfied.
652 */
653int rio_release_inb_pwrite(struct rio_dev *rdev)
654{
655 int rc = -ENOMEM;
656
657 spin_lock(&rio_global_list_lock);
658 if (rdev->pwcback) {
659 rdev->pwcback = NULL;
660 rc = 0;
661 }
662
663 spin_unlock(&rio_global_list_lock);
664 return rc;
665}
666EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
667
668/**
Alexandre Bounineb6cb95e2016-03-22 14:26:41 -0700669 * rio_pw_enable - Enables/disables port-write handling by a master port
670 * @mport: Master port associated with port-write handling
671 * @enable: 1=enable, 0=disable
672 */
673void rio_pw_enable(struct rio_mport *mport, int enable)
674{
675 if (mport->ops->pwenable) {
676 mutex_lock(&mport->lock);
677
678 if ((enable && ++mport->pwe_refcnt == 1) ||
679 (!enable && mport->pwe_refcnt && --mport->pwe_refcnt == 0))
680 mport->ops->pwenable(mport, enable);
681 mutex_unlock(&mport->lock);
682 }
683}
684EXPORT_SYMBOL_GPL(rio_pw_enable);
685
686/**
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700687 * rio_map_inb_region -- Map inbound memory region.
688 * @mport: Master port.
Randy Dunlap2ca3cb52012-11-16 14:14:56 -0800689 * @local: physical address of memory region to be mapped
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700690 * @rbase: RIO base address assigned to this window
691 * @size: Size of the memory region
692 * @rflags: Flags for mapping.
693 *
694 * Return: 0 -- Success.
695 *
696 * This function will create the mapping from RIO space to local memory.
697 */
698int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
699 u64 rbase, u32 size, u32 rflags)
700{
701 int rc = 0;
702 unsigned long flags;
703
704 if (!mport->ops->map_inb)
705 return -1;
706 spin_lock_irqsave(&rio_mmap_lock, flags);
707 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
708 spin_unlock_irqrestore(&rio_mmap_lock, flags);
709 return rc;
710}
711EXPORT_SYMBOL_GPL(rio_map_inb_region);
712
713/**
714 * rio_unmap_inb_region -- Unmap the inbound memory region
715 * @mport: Master port
716 * @lstart: physical address of memory region to be unmapped
717 */
718void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
719{
720 unsigned long flags;
721 if (!mport->ops->unmap_inb)
722 return;
723 spin_lock_irqsave(&rio_mmap_lock, flags);
724 mport->ops->unmap_inb(mport, lstart);
725 spin_unlock_irqrestore(&rio_mmap_lock, flags);
726}
727EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
728
729/**
Alexandre Bounine93bdaca2016-03-22 14:26:50 -0700730 * rio_map_outb_region -- Map outbound memory region.
731 * @mport: Master port.
732 * @destid: destination id window points to
733 * @rbase: RIO base address window translates to
734 * @size: Size of the memory region
735 * @rflags: Flags for mapping.
736 * @local: physical address of memory region mapped
737 *
738 * Return: 0 -- Success.
739 *
740 * This function will create the mapping from RIO space to local memory.
741 */
742int rio_map_outb_region(struct rio_mport *mport, u16 destid, u64 rbase,
743 u32 size, u32 rflags, dma_addr_t *local)
744{
745 int rc = 0;
746 unsigned long flags;
747
748 if (!mport->ops->map_outb)
749 return -ENODEV;
750
751 spin_lock_irqsave(&rio_mmap_lock, flags);
752 rc = mport->ops->map_outb(mport, destid, rbase, size,
753 rflags, local);
754 spin_unlock_irqrestore(&rio_mmap_lock, flags);
755
756 return rc;
757}
758EXPORT_SYMBOL_GPL(rio_map_outb_region);
759
760/**
761 * rio_unmap_inb_region -- Unmap the inbound memory region
762 * @mport: Master port
763 * @destid: destination id mapping points to
764 * @rstart: RIO base address window translates to
765 */
766void rio_unmap_outb_region(struct rio_mport *mport, u16 destid, u64 rstart)
767{
768 unsigned long flags;
769
770 if (!mport->ops->unmap_outb)
771 return;
772
773 spin_lock_irqsave(&rio_mmap_lock, flags);
774 mport->ops->unmap_outb(mport, destid, rstart);
775 spin_unlock_irqrestore(&rio_mmap_lock, flags);
776}
777EXPORT_SYMBOL_GPL(rio_unmap_outb_region);
778
779/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700780 * rio_mport_get_physefb - Helper function that returns register offset
781 * for Physical Layer Extended Features Block.
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700782 * @port: Master port to issue transaction
783 * @local: Indicate a local master port or remote device access
784 * @destid: Destination ID of the device
785 * @hopcount: Number of switch hops to the device
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700786 * @rmap: pointer to location to store register map type info
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700787 */
788u32
789rio_mport_get_physefb(struct rio_mport *port, int local,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700790 u16 destid, u8 hopcount, u32 *rmap)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700791{
792 u32 ext_ftr_ptr;
793 u32 ftr_header;
794
795 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
796
797 while (ext_ftr_ptr) {
798 if (local)
799 rio_local_read_config_32(port, ext_ftr_ptr,
800 &ftr_header);
801 else
802 rio_mport_read_config_32(port, destid, hopcount,
803 ext_ftr_ptr, &ftr_header);
804
805 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
806 switch (ftr_header) {
807
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700808 case RIO_EFB_SER_EP_ID:
809 case RIO_EFB_SER_EP_REC_ID:
810 case RIO_EFB_SER_EP_FREE_ID:
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700811 case RIO_EFB_SER_EP_M1_ID:
812 case RIO_EFB_SER_EP_SW_M1_ID:
813 case RIO_EFB_SER_EPF_M1_ID:
814 case RIO_EFB_SER_EPF_SW_M1_ID:
815 *rmap = 1;
816 return ext_ftr_ptr;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700817
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700818 case RIO_EFB_SER_EP_M2_ID:
819 case RIO_EFB_SER_EP_SW_M2_ID:
820 case RIO_EFB_SER_EPF_M2_ID:
821 case RIO_EFB_SER_EPF_SW_M2_ID:
822 *rmap = 2;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700823 return ext_ftr_ptr;
824
825 default:
826 break;
827 }
828
829 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
830 hopcount, ext_ftr_ptr);
831 }
832
833 return ext_ftr_ptr;
834}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700835EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700836
837/**
838 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700839 * @comp_tag: RIO component tag to match
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700840 * @from: Previous RIO device found in search, or %NULL for new search
841 *
842 * Iterates through the list of known RIO devices. If a RIO device is
843 * found with a matching @comp_tag, a pointer to its device
844 * structure is returned. Otherwise, %NULL is returned. A new search
845 * is initiated by passing %NULL to the @from argument. Otherwise, if
846 * @from is not %NULL, searches continue from next device on the global
847 * list.
848 */
Alexandre Bounineaf84ca382010-10-27 15:34:34 -0700849struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700850{
851 struct list_head *n;
852 struct rio_dev *rdev;
853
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700854 spin_lock(&rio_global_list_lock);
855 n = from ? from->global_list.next : rio_devices.next;
856
857 while (n && (n != &rio_devices)) {
858 rdev = rio_dev_g(n);
859 if (rdev->comp_tag == comp_tag)
860 goto exit;
861 n = n->next;
862 }
863 rdev = NULL;
864exit:
865 spin_unlock(&rio_global_list_lock);
866 return rdev;
867}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700868EXPORT_SYMBOL_GPL(rio_get_comptag);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700869
870/**
871 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
872 * @rdev: Pointer to RIO device control structure
873 * @pnum: Switch port number to set LOCKOUT bit
874 * @lock: Operation : set (=1) or clear (=0)
875 */
876int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
877{
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700878 u32 regval;
879
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800880 rio_read_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700881 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
882 &regval);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700883 if (lock)
884 regval |= RIO_PORT_N_CTL_LOCKOUT;
885 else
886 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
887
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800888 rio_write_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700889 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
890 regval);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700891 return 0;
892}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700893EXPORT_SYMBOL_GPL(rio_set_port_lockout);
894
895/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700896 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
897 * given port
898 * @port: Master port associated with the RIO network
899 * @local: local=1 select local port otherwise a far device is reached
900 * @destid: Destination ID of the device to check host bit
901 * @hopcount: Number of hops to reach the target
902 * @port_num: Port (-number on switch) to enable on a far end device
903 *
904 * Returns 0 or 1 from on General Control Command and Status Register
905 * (EXT_PTR+0x3C)
906 */
907int rio_enable_rx_tx_port(struct rio_mport *port,
908 int local, u16 destid,
909 u8 hopcount, u8 port_num)
910{
911#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
912 u32 regval;
913 u32 ext_ftr_ptr;
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700914 u32 rmap;
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700915
916 /*
917 * enable rx input tx output port
918 */
919 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
920 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
921
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700922 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid,
923 hopcount, &rmap);
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700924
925 if (local) {
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700926 rio_local_read_config_32(port,
927 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap),
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700928 &regval);
929 } else {
930 if (rio_mport_read_config_32(port, destid, hopcount,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700931 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
932 &regval) < 0)
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700933 return -EIO;
934 }
935
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700936 regval = regval | RIO_PORT_N_CTL_EN_RX | RIO_PORT_N_CTL_EN_TX;
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700937
938 if (local) {
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700939 rio_local_write_config_32(port,
940 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap), regval);
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700941 } else {
942 if (rio_mport_write_config_32(port, destid, hopcount,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700943 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
944 regval) < 0)
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700945 return -EIO;
946 }
947#endif
948 return 0;
949}
950EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
951
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700952
953/**
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700954 * rio_chk_dev_route - Validate route to the specified device.
955 * @rdev: RIO device failed to respond
956 * @nrdev: Last active device on the route to rdev
957 * @npnum: nrdev's port number on the route to rdev
958 *
959 * Follows a route to the specified RIO device to determine the last available
960 * device (and corresponding RIO port) on the route.
961 */
962static int
963rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
964{
965 u32 result;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800966 int p_port, rc = -EIO;
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700967 struct rio_dev *prev = NULL;
968
969 /* Find switch with failed RIO link */
970 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
971 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
972 prev = rdev->prev;
973 break;
974 }
975 rdev = rdev->prev;
976 }
977
Markus Elfring93dd49a2018-02-06 15:39:44 -0800978 if (!prev)
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700979 goto err_out;
980
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800981 p_port = prev->rswitch->route_table[rdev->destid];
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700982
Alexandre Bounineaf84ca382010-10-27 15:34:34 -0700983 if (p_port != RIO_INVALID_ROUTE) {
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700984 pr_debug("RIO: link failed on [%s]-P%d\n",
985 rio_name(prev), p_port);
986 *nrdev = prev;
987 *npnum = p_port;
988 rc = 0;
989 } else
Alexandre Bounineaf84ca382010-10-27 15:34:34 -0700990 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700991err_out:
992 return rc;
993}
994
995/**
996 * rio_mport_chk_dev_access - Validate access to the specified device.
997 * @mport: Master port to send transactions
998 * @destid: Device destination ID in network
999 * @hopcount: Number of hops into the network
1000 */
Alexandre Bouninee274e0e2010-10-27 15:34:32 -07001001int
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001002rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
1003{
1004 int i = 0;
1005 u32 tmp;
1006
1007 while (rio_mport_read_config_32(mport, destid, hopcount,
1008 RIO_DEV_ID_CAR, &tmp)) {
1009 i++;
1010 if (i == RIO_MAX_CHK_RETRY)
1011 return -EIO;
1012 mdelay(1);
1013 }
1014
1015 return 0;
1016}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001017EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001018
1019/**
1020 * rio_chk_dev_access - Validate access to the specified device.
1021 * @rdev: Pointer to RIO device control structure
1022 */
1023static int rio_chk_dev_access(struct rio_dev *rdev)
1024{
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001025 return rio_mport_chk_dev_access(rdev->net->hport,
1026 rdev->destid, rdev->hopcount);
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001027}
1028
1029/**
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001030 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
1031 * returns link-response (if requested).
1032 * @rdev: RIO devive to issue Input-status command
1033 * @pnum: Device port number to issue the command
1034 * @lnkresp: Response from a link partner
1035 */
1036static int
1037rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
1038{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001039 u32 regval;
1040 int checkcount;
1041
1042 if (lnkresp) {
1043 /* Read from link maintenance response register
1044 * to clear valid bit */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001045 rio_read_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001046 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001047 &regval);
1048 udelay(50);
1049 }
1050
1051 /* Issue Input-status command */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001052 rio_write_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001053 RIO_DEV_PORT_N_MNT_REQ_CSR(rdev, pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001054 RIO_MNT_REQ_CMD_IS);
1055
1056 /* Exit if the response is not expected */
Markus Elfring93dd49a2018-02-06 15:39:44 -08001057 if (!lnkresp)
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001058 return 0;
1059
1060 checkcount = 3;
1061 while (checkcount--) {
1062 udelay(50);
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001063 rio_read_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001064 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001065 &regval);
1066 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
1067 *lnkresp = regval;
1068 return 0;
1069 }
1070 }
1071
1072 return -EIO;
1073}
1074
1075/**
1076 * rio_clr_err_stopped - Clears port Error-stopped states.
1077 * @rdev: Pointer to RIO device control structure
1078 * @pnum: Switch port number to clear errors
1079 * @err_status: port error status (if 0 reads register from device)
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001080 *
1081 * TODO: Currently this routine is not compatible with recovery process
1082 * specified for idt_gen3 RapidIO switch devices. It has to be reviewed
1083 * to implement universal recovery process that is compatible full range
1084 * off available devices.
1085 * IDT gen3 switch driver now implements HW-specific error handler that
1086 * issues soft port reset to the port to reset ERR_STOP bits and ackIDs.
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001087 */
1088static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
1089{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001090 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
1091 u32 regval;
1092 u32 far_ackid, far_linkstat, near_ackid;
1093
1094 if (err_status == 0)
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001095 rio_read_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001096 RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001097 &err_status);
1098
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001099 if (err_status & RIO_PORT_N_ERR_STS_OUT_ES) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001100 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
1101 /*
1102 * Send a Link-Request/Input-Status control symbol
1103 */
1104 if (rio_get_input_status(rdev, pnum, &regval)) {
1105 pr_debug("RIO_EM: Input-status response timeout\n");
1106 goto rd_err;
1107 }
1108
1109 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
1110 pnum, regval);
1111 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
1112 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001113 rio_read_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001114 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001115 &regval);
1116 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
1117 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
1118 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
1119 " near_ackID=0x%02x\n",
1120 pnum, far_ackid, far_linkstat, near_ackid);
1121
1122 /*
1123 * If required, synchronize ackIDs of near and
1124 * far sides.
1125 */
1126 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
1127 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
1128 /* Align near outstanding/outbound ackIDs with
1129 * far inbound.
1130 */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001131 rio_write_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001132 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001133 (near_ackid << 24) |
1134 (far_ackid << 8) | far_ackid);
1135 /* Align far outstanding/outbound ackIDs with
1136 * near inbound.
1137 */
1138 far_ackid++;
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001139 if (!nextdev) {
1140 pr_debug("RIO_EM: nextdev pointer == NULL\n");
1141 goto rd_err;
1142 }
1143
1144 rio_write_config_32(nextdev,
1145 RIO_DEV_PORT_N_ACK_STS_CSR(nextdev,
1146 RIO_GET_PORT_NUM(nextdev->swpinfo)),
1147 (far_ackid << 24) |
1148 (near_ackid << 8) | near_ackid);
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001149 }
1150rd_err:
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001151 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1152 &err_status);
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001153 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1154 }
1155
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001156 if ((err_status & RIO_PORT_N_ERR_STS_INP_ES) && nextdev) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001157 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
1158 rio_get_input_status(nextdev,
1159 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
1160 udelay(50);
1161
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001162 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1163 &err_status);
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001164 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1165 }
1166
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001167 return (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
1168 RIO_PORT_N_ERR_STS_INP_ES)) ? 1 : 0;
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001169}
1170
1171/**
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001172 * rio_inb_pwrite_handler - inbound port-write message handler
1173 * @mport: mport device associated with port-write
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001174 * @pw_msg: pointer to inbound port-write message
1175 *
1176 * Processes an inbound port-write message. Returns 0 if the request
1177 * has been satisfied.
1178 */
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001179int rio_inb_pwrite_handler(struct rio_mport *mport, union rio_pw_msg *pw_msg)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001180{
1181 struct rio_dev *rdev;
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001182 u32 err_status, em_perrdet, em_ltlerrdet;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001183 int rc, portnum;
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001184 struct rio_pwrite *pwrite;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001185
1186#ifdef DEBUG_PW
1187 {
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001188 u32 i;
1189
1190 pr_debug("%s: PW to mport_%d:\n", __func__, mport->id);
1191 for (i = 0; i < RIO_PW_MSG_SIZE / sizeof(u32); i = i + 4) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001192 pr_debug("0x%02x: %08x %08x %08x %08x\n",
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001193 i * 4, pw_msg->raw[i], pw_msg->raw[i + 1],
1194 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
1195 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001196 }
1197#endif
1198
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001199 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
1200 if (rdev) {
1201 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
1202 } else {
1203 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
1204 __func__, pw_msg->em.comptag);
1205 }
1206
1207 /* Call a device-specific handler (if it is registered for the device).
1208 * This may be the service for endpoints that send device-specific
1209 * port-write messages. End-point messages expected to be handled
1210 * completely by EP specific device driver.
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001211 * For switches rc==0 signals that no standard processing required.
1212 */
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001213 if (rdev && rdev->pwcback) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001214 rc = rdev->pwcback(rdev, pw_msg, 0);
1215 if (rc == 0)
1216 return 0;
1217 }
1218
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001219 mutex_lock(&mport->lock);
1220 list_for_each_entry(pwrite, &mport->pwrites, node)
1221 pwrite->pwcback(mport, pwrite->context, pw_msg, 0);
1222 mutex_unlock(&mport->lock);
1223
1224 if (!rdev)
1225 return 0;
1226
1227 /*
1228 * FIXME: The code below stays as it was before for now until we decide
1229 * how to do default PW handling in combination with per-mport callbacks
1230 */
1231
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001232 portnum = pw_msg->em.is_port & 0xFF;
1233
1234 /* Check if device and route to it are functional:
1235 * Sometimes devices may send PW message(s) just before being
1236 * powered down (or link being lost).
1237 */
1238 if (rio_chk_dev_access(rdev)) {
1239 pr_debug("RIO: device access failed - get link partner\n");
1240 /* Scan route to the device and identify failed link.
1241 * This will replace device and port reported in PW message.
1242 * PW message should not be used after this point.
1243 */
1244 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
1245 pr_err("RIO: Route trace for %s failed\n",
1246 rio_name(rdev));
1247 return -EIO;
1248 }
1249 pw_msg = NULL;
1250 }
1251
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001252 /* For End-point devices processing stops here */
1253 if (!(rdev->pef & RIO_PEF_SWITCH))
1254 return 0;
1255
1256 if (rdev->phys_efptr == 0) {
1257 pr_err("RIO_PW: Bad switch initialization for %s\n",
1258 rio_name(rdev));
1259 return 0;
1260 }
1261
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001262 /*
1263 * Process the port-write notification from switch
1264 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001265 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
1266 rdev->rswitch->ops->em_handle(rdev, portnum);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001267
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001268 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1269 &err_status);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001270 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
1271
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001272 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001273
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001274 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
1275 rdev->rswitch->port_ok |= (1 << portnum);
1276 rio_set_port_lockout(rdev, portnum, 0);
1277 /* Schedule Insertion Service */
1278 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
1279 rio_name(rdev), portnum);
1280 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001281
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001282 /* Clear error-stopped states (if reported).
1283 * Depending on the link partner state, two attempts
1284 * may be needed for successful recovery.
1285 */
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001286 if (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
1287 RIO_PORT_N_ERR_STS_INP_ES)) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001288 if (rio_clr_err_stopped(rdev, portnum, err_status))
1289 rio_clr_err_stopped(rdev, portnum, 0);
1290 }
1291 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001292
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001293 if (rdev->rswitch->port_ok & (1 << portnum)) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001294 rdev->rswitch->port_ok &= ~(1 << portnum);
1295 rio_set_port_lockout(rdev, portnum, 1);
1296
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001297 if (rdev->phys_rmap == 1) {
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001298 rio_write_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001299 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, portnum),
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001300 RIO_PORT_N_ACK_CLEAR);
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001301 } else {
1302 rio_write_config_32(rdev,
1303 RIO_DEV_PORT_N_OB_ACK_CSR(rdev, portnum),
1304 RIO_PORT_N_OB_ACK_CLEAR);
1305 rio_write_config_32(rdev,
1306 RIO_DEV_PORT_N_IB_ACK_CSR(rdev, portnum),
1307 0);
1308 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001309
1310 /* Schedule Extraction Service */
1311 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
1312 rio_name(rdev), portnum);
1313 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001314 }
1315
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001316 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001317 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
1318 if (em_perrdet) {
1319 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
1320 portnum, em_perrdet);
1321 /* Clear EM Port N Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001322 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001323 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
1324 }
1325
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001326 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001327 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1328 if (em_ltlerrdet) {
1329 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1330 em_ltlerrdet);
1331 /* Clear EM L/T Layer Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001332 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001333 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1334 }
1335
Alexandre Bounine388c45c2010-10-27 15:34:35 -07001336 /* Clear remaining error bits and Port-Write Pending bit */
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001337 rio_write_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1338 err_status);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001339
1340 return 0;
1341}
1342EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1343
1344/**
1345 * rio_mport_get_efb - get pointer to next extended features block
1346 * @port: Master port to issue transaction
1347 * @local: Indicate a local master port or remote device access
1348 * @destid: Destination ID of the device
1349 * @hopcount: Number of switch hops to the device
1350 * @from: Offset of current Extended Feature block header (if 0 starts
1351 * from ExtFeaturePtr)
1352 */
1353u32
1354rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1355 u8 hopcount, u32 from)
1356{
1357 u32 reg_val;
1358
1359 if (from == 0) {
1360 if (local)
1361 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1362 &reg_val);
1363 else
1364 rio_mport_read_config_32(port, destid, hopcount,
1365 RIO_ASM_INFO_CAR, &reg_val);
1366 return reg_val & RIO_EXT_FTR_PTR_MASK;
1367 } else {
1368 if (local)
1369 rio_local_read_config_32(port, from, &reg_val);
1370 else
1371 rio_mport_read_config_32(port, destid, hopcount,
1372 from, &reg_val);
1373 return RIO_GET_BLOCK_ID(reg_val);
1374 }
1375}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001376EXPORT_SYMBOL_GPL(rio_mport_get_efb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001377
1378/**
Matt Porter394b7012005-11-07 01:00:15 -08001379 * rio_mport_get_feature - query for devices' extended features
1380 * @port: Master port to issue transaction
1381 * @local: Indicate a local master port or remote device access
1382 * @destid: Destination ID of the device
1383 * @hopcount: Number of switch hops to the device
1384 * @ftr: Extended feature code
1385 *
1386 * Tell if a device supports a given RapidIO capability.
1387 * Returns the offset of the requested extended feature
1388 * block within the device's RIO configuration space or
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001389 * 0 in case the device does not support it.
Matt Porter394b7012005-11-07 01:00:15 -08001390 */
1391u32
1392rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1393 u8 hopcount, int ftr)
1394{
1395 u32 asm_info, ext_ftr_ptr, ftr_header;
1396
1397 if (local)
1398 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1399 else
1400 rio_mport_read_config_32(port, destid, hopcount,
1401 RIO_ASM_INFO_CAR, &asm_info);
1402
1403 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1404
1405 while (ext_ftr_ptr) {
1406 if (local)
1407 rio_local_read_config_32(port, ext_ftr_ptr,
1408 &ftr_header);
1409 else
1410 rio_mport_read_config_32(port, destid, hopcount,
1411 ext_ftr_ptr, &ftr_header);
1412 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1413 return ext_ftr_ptr;
Markus Elfringe1d66d02018-02-06 15:39:48 -08001414
1415 ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header);
1416 if (!ext_ftr_ptr)
Matt Porter394b7012005-11-07 01:00:15 -08001417 break;
1418 }
1419
1420 return 0;
1421}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001422EXPORT_SYMBOL_GPL(rio_mport_get_feature);
Matt Porter394b7012005-11-07 01:00:15 -08001423
1424/**
1425 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
1426 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1427 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1428 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
1429 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
1430 * @from: Previous RIO device found in search, or %NULL for new search
1431 *
1432 * Iterates through the list of known RIO devices. If a RIO device is
1433 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
1434 * count to the device is incrememted and a pointer to its device
1435 * structure is returned. Otherwise, %NULL is returned. A new search
1436 * is initiated by passing %NULL to the @from argument. Otherwise, if
1437 * @from is not %NULL, searches continue from next device on the global
1438 * list. The reference count for @from is always decremented if it is
1439 * not %NULL.
1440 */
1441struct rio_dev *rio_get_asm(u16 vid, u16 did,
1442 u16 asm_vid, u16 asm_did, struct rio_dev *from)
1443{
1444 struct list_head *n;
1445 struct rio_dev *rdev;
1446
1447 WARN_ON(in_interrupt());
1448 spin_lock(&rio_global_list_lock);
1449 n = from ? from->global_list.next : rio_devices.next;
1450
1451 while (n && (n != &rio_devices)) {
1452 rdev = rio_dev_g(n);
1453 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1454 (did == RIO_ANY_ID || rdev->did == did) &&
1455 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1456 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1457 goto exit;
1458 n = n->next;
1459 }
1460 rdev = NULL;
1461 exit:
1462 rio_dev_put(from);
1463 rdev = rio_dev_get(rdev);
1464 spin_unlock(&rio_global_list_lock);
1465 return rdev;
1466}
1467
1468/**
1469 * rio_get_device - Begin or continue searching for a RIO device by vid/did
1470 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1471 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1472 * @from: Previous RIO device found in search, or %NULL for new search
1473 *
1474 * Iterates through the list of known RIO devices. If a RIO device is
1475 * found with a matching @vid and @did, the reference count to the
1476 * device is incrememted and a pointer to its device structure is returned.
1477 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
1478 * to the @from argument. Otherwise, if @from is not %NULL, searches
1479 * continue from next device on the global list. The reference count for
1480 * @from is always decremented if it is not %NULL.
1481 */
1482struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1483{
1484 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1485}
1486
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001487/**
1488 * rio_std_route_add_entry - Add switch route table entry using standard
1489 * registers defined in RIO specification rev.1.3
1490 * @mport: Master port to issue transaction
1491 * @destid: Destination ID of the device
1492 * @hopcount: Number of switch hops to the device
1493 * @table: routing table ID (global or port-specific)
1494 * @route_destid: destID entry in the RT
1495 * @route_port: destination port for specified destID
1496 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001497static int
1498rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1499 u16 table, u16 route_destid, u8 route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001500{
1501 if (table == RIO_GLOBAL_TABLE) {
1502 rio_mport_write_config_32(mport, destid, hopcount,
1503 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1504 (u32)route_destid);
1505 rio_mport_write_config_32(mport, destid, hopcount,
1506 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1507 (u32)route_port);
1508 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001509
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001510 udelay(10);
1511 return 0;
1512}
1513
1514/**
1515 * rio_std_route_get_entry - Read switch route table entry (port number)
Uwe Kleine-König638c5942010-06-11 12:16:58 +02001516 * associated with specified destID using standard registers defined in RIO
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001517 * specification rev.1.3
1518 * @mport: Master port to issue transaction
1519 * @destid: Destination ID of the device
1520 * @hopcount: Number of switch hops to the device
1521 * @table: routing table ID (global or port-specific)
1522 * @route_destid: destID entry in the RT
1523 * @route_port: returned destination port for specified destID
1524 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001525static int
1526rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1527 u16 table, u16 route_destid, u8 *route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001528{
1529 u32 result;
1530
1531 if (table == RIO_GLOBAL_TABLE) {
1532 rio_mport_write_config_32(mport, destid, hopcount,
1533 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1534 rio_mport_read_config_32(mport, destid, hopcount,
1535 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1536
1537 *route_port = (u8)result;
1538 }
1539
1540 return 0;
1541}
1542
1543/**
1544 * rio_std_route_clr_table - Clear swotch route table using standard registers
1545 * defined in RIO specification rev.1.3.
1546 * @mport: Master port to issue transaction
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001547 * @destid: Destination ID of the device
1548 * @hopcount: Number of switch hops to the device
1549 * @table: routing table ID (global or port-specific)
1550 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001551static int
1552rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1553 u16 table)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001554{
1555 u32 max_destid = 0xff;
1556 u32 i, pef, id_inc = 1, ext_cfg = 0;
1557 u32 port_sel = RIO_INVALID_ROUTE;
1558
1559 if (table == RIO_GLOBAL_TABLE) {
1560 rio_mport_read_config_32(mport, destid, hopcount,
1561 RIO_PEF_CAR, &pef);
1562
1563 if (mport->sys_size) {
1564 rio_mport_read_config_32(mport, destid, hopcount,
1565 RIO_SWITCH_RT_LIMIT,
1566 &max_destid);
1567 max_destid &= RIO_RT_MAX_DESTID;
1568 }
1569
1570 if (pef & RIO_PEF_EXT_RT) {
1571 ext_cfg = 0x80000000;
1572 id_inc = 4;
1573 port_sel = (RIO_INVALID_ROUTE << 24) |
1574 (RIO_INVALID_ROUTE << 16) |
1575 (RIO_INVALID_ROUTE << 8) |
1576 RIO_INVALID_ROUTE;
1577 }
1578
1579 for (i = 0; i <= max_destid;) {
1580 rio_mport_write_config_32(mport, destid, hopcount,
1581 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1582 ext_cfg | i);
1583 rio_mport_write_config_32(mport, destid, hopcount,
1584 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1585 port_sel);
1586 i += id_inc;
1587 }
1588 }
1589
1590 udelay(10);
1591 return 0;
1592}
1593
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001594/**
1595 * rio_lock_device - Acquires host device lock for specified device
1596 * @port: Master port to send transaction
1597 * @destid: Destination ID for device/switch
1598 * @hopcount: Hopcount to reach switch
1599 * @wait_ms: Max wait time in msec (0 = no timeout)
1600 *
1601 * Attepts to acquire host device lock for specified device
1602 * Returns 0 if device lock acquired or EINVAL if timeout expires.
1603 */
1604int rio_lock_device(struct rio_mport *port, u16 destid,
1605 u8 hopcount, int wait_ms)
1606{
1607 u32 result;
1608 int tcnt = 0;
1609
1610 /* Attempt to acquire device lock */
1611 rio_mport_write_config_32(port, destid, hopcount,
1612 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1613 rio_mport_read_config_32(port, destid, hopcount,
1614 RIO_HOST_DID_LOCK_CSR, &result);
1615
1616 while (result != port->host_deviceid) {
1617 if (wait_ms != 0 && tcnt == wait_ms) {
1618 pr_debug("RIO: timeout when locking device %x:%x\n",
1619 destid, hopcount);
1620 return -EINVAL;
1621 }
1622
1623 /* Delay a bit */
1624 mdelay(1);
1625 tcnt++;
1626 /* Try to acquire device lock again */
1627 rio_mport_write_config_32(port, destid,
1628 hopcount,
1629 RIO_HOST_DID_LOCK_CSR,
1630 port->host_deviceid);
1631 rio_mport_read_config_32(port, destid,
1632 hopcount,
1633 RIO_HOST_DID_LOCK_CSR, &result);
1634 }
1635
1636 return 0;
1637}
1638EXPORT_SYMBOL_GPL(rio_lock_device);
1639
1640/**
1641 * rio_unlock_device - Releases host device lock for specified device
1642 * @port: Master port to send transaction
1643 * @destid: Destination ID for device/switch
1644 * @hopcount: Hopcount to reach switch
1645 *
1646 * Returns 0 if device lock released or EINVAL if fails.
1647 */
1648int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1649{
1650 u32 result;
1651
1652 /* Release device lock */
1653 rio_mport_write_config_32(port, destid,
1654 hopcount,
1655 RIO_HOST_DID_LOCK_CSR,
1656 port->host_deviceid);
1657 rio_mport_read_config_32(port, destid, hopcount,
1658 RIO_HOST_DID_LOCK_CSR, &result);
1659 if ((result & 0xffff) != 0xffff) {
1660 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1661 destid, hopcount);
1662 return -EINVAL;
1663 }
1664
1665 return 0;
1666}
1667EXPORT_SYMBOL_GPL(rio_unlock_device);
1668
1669/**
1670 * rio_route_add_entry- Add a route entry to a switch routing table
1671 * @rdev: RIO device
1672 * @table: Routing table ID
1673 * @route_destid: Destination ID to be routed
1674 * @route_port: Port number to be routed
1675 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1676 *
1677 * If available calls the switch specific add_entry() method to add a route
1678 * entry into a switch routing table. Otherwise uses standard RT update method
1679 * as defined by RapidIO specification. A specific routing table can be selected
1680 * using the @table argument if a switch has per port routing tables or
1681 * the standard (or global) table may be used by passing
1682 * %RIO_GLOBAL_TABLE in @table.
1683 *
1684 * Returns %0 on success or %-EINVAL on failure.
1685 */
1686int rio_route_add_entry(struct rio_dev *rdev,
1687 u16 table, u16 route_destid, u8 route_port, int lock)
1688{
1689 int rc = -EINVAL;
1690 struct rio_switch_ops *ops = rdev->rswitch->ops;
1691
1692 if (lock) {
1693 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1694 rdev->hopcount, 1000);
1695 if (rc)
1696 return rc;
1697 }
1698
1699 spin_lock(&rdev->rswitch->lock);
1700
Markus Elfring93dd49a2018-02-06 15:39:44 -08001701 if (!ops || !ops->add_entry) {
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001702 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1703 rdev->hopcount, table,
1704 route_destid, route_port);
1705 } else if (try_module_get(ops->owner)) {
1706 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1707 rdev->hopcount, table, route_destid,
1708 route_port);
1709 module_put(ops->owner);
1710 }
1711
1712 spin_unlock(&rdev->rswitch->lock);
1713
1714 if (lock)
1715 rio_unlock_device(rdev->net->hport, rdev->destid,
1716 rdev->hopcount);
1717
1718 return rc;
1719}
1720EXPORT_SYMBOL_GPL(rio_route_add_entry);
1721
1722/**
1723 * rio_route_get_entry- Read an entry from a switch routing table
1724 * @rdev: RIO device
1725 * @table: Routing table ID
1726 * @route_destid: Destination ID to be routed
1727 * @route_port: Pointer to read port number into
1728 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1729 *
1730 * If available calls the switch specific get_entry() method to fetch a route
1731 * entry from a switch routing table. Otherwise uses standard RT read method
1732 * as defined by RapidIO specification. A specific routing table can be selected
1733 * using the @table argument if a switch has per port routing tables or
1734 * the standard (or global) table may be used by passing
1735 * %RIO_GLOBAL_TABLE in @table.
1736 *
1737 * Returns %0 on success or %-EINVAL on failure.
1738 */
1739int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1740 u16 route_destid, u8 *route_port, int lock)
1741{
1742 int rc = -EINVAL;
1743 struct rio_switch_ops *ops = rdev->rswitch->ops;
1744
1745 if (lock) {
1746 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1747 rdev->hopcount, 1000);
1748 if (rc)
1749 return rc;
1750 }
1751
1752 spin_lock(&rdev->rswitch->lock);
1753
Markus Elfring93dd49a2018-02-06 15:39:44 -08001754 if (!ops || !ops->get_entry) {
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001755 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1756 rdev->hopcount, table,
1757 route_destid, route_port);
1758 } else if (try_module_get(ops->owner)) {
1759 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1760 rdev->hopcount, table, route_destid,
1761 route_port);
1762 module_put(ops->owner);
1763 }
1764
1765 spin_unlock(&rdev->rswitch->lock);
1766
1767 if (lock)
1768 rio_unlock_device(rdev->net->hport, rdev->destid,
1769 rdev->hopcount);
1770 return rc;
1771}
1772EXPORT_SYMBOL_GPL(rio_route_get_entry);
1773
1774/**
1775 * rio_route_clr_table - Clear a switch routing table
1776 * @rdev: RIO device
1777 * @table: Routing table ID
1778 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1779 *
1780 * If available calls the switch specific clr_table() method to clear a switch
1781 * routing table. Otherwise uses standard RT write method as defined by RapidIO
1782 * specification. A specific routing table can be selected using the @table
1783 * argument if a switch has per port routing tables or the standard (or global)
1784 * table may be used by passing %RIO_GLOBAL_TABLE in @table.
1785 *
1786 * Returns %0 on success or %-EINVAL on failure.
1787 */
1788int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1789{
1790 int rc = -EINVAL;
1791 struct rio_switch_ops *ops = rdev->rswitch->ops;
1792
1793 if (lock) {
1794 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1795 rdev->hopcount, 1000);
1796 if (rc)
1797 return rc;
1798 }
1799
1800 spin_lock(&rdev->rswitch->lock);
1801
Markus Elfring93dd49a2018-02-06 15:39:44 -08001802 if (!ops || !ops->clr_table) {
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001803 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1804 rdev->hopcount, table);
1805 } else if (try_module_get(ops->owner)) {
1806 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1807 rdev->hopcount, table);
1808
1809 module_put(ops->owner);
1810 }
1811
1812 spin_unlock(&rdev->rswitch->lock);
1813
1814 if (lock)
1815 rio_unlock_device(rdev->net->hport, rdev->destid,
1816 rdev->hopcount);
1817
1818 return rc;
1819}
1820EXPORT_SYMBOL_GPL(rio_route_clr_table);
1821
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001822#ifdef CONFIG_RAPIDIO_DMA_ENGINE
1823
1824static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1825{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001826 struct rio_mport *mport = arg;
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001827
1828 /* Check that DMA device belongs to the right MPORT */
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001829 return mport == container_of(chan->device, struct rio_mport, dma);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001830}
1831
1832/**
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001833 * rio_request_mport_dma - request RapidIO capable DMA channel associated
1834 * with specified local RapidIO mport device.
1835 * @mport: RIO mport to perform DMA data transfers
1836 *
1837 * Returns pointer to allocated DMA channel or NULL if failed.
1838 */
1839struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
1840{
1841 dma_cap_mask_t mask;
1842
1843 dma_cap_zero(mask);
1844 dma_cap_set(DMA_SLAVE, mask);
1845 return dma_request_channel(mask, rio_chan_filter, mport);
1846}
1847EXPORT_SYMBOL_GPL(rio_request_mport_dma);
1848
1849/**
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001850 * rio_request_dma - request RapidIO capable DMA channel that supports
1851 * specified target RapidIO device.
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001852 * @rdev: RIO device associated with DMA transfer
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001853 *
1854 * Returns pointer to allocated DMA channel or NULL if failed.
1855 */
1856struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1857{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001858 return rio_request_mport_dma(rdev->net->hport);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001859}
1860EXPORT_SYMBOL_GPL(rio_request_dma);
1861
1862/**
1863 * rio_release_dma - release specified DMA channel
1864 * @dchan: DMA channel to release
1865 */
1866void rio_release_dma(struct dma_chan *dchan)
1867{
1868 dma_release_channel(dchan);
1869}
1870EXPORT_SYMBOL_GPL(rio_release_dma);
1871
1872/**
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001873 * rio_dma_prep_xfer - RapidIO specific wrapper
1874 * for device_prep_slave_sg callback defined by DMAENGINE.
1875 * @dchan: DMA channel to configure
1876 * @destid: target RapidIO device destination ID
1877 * @data: RIO specific data descriptor
1878 * @direction: DMA data transfer direction (TO or FROM the device)
1879 * @flags: dmaengine defined flags
1880 *
1881 * Initializes RapidIO capable DMA channel for the specified data transfer.
1882 * Uses DMA channel private extension to pass information related to remote
1883 * target RIO device.
Alexandre Bouninef8e3a682016-08-02 14:06:34 -07001884 *
1885 * Returns: pointer to DMA transaction descriptor if successful,
1886 * error-valued pointer or NULL if failed.
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001887 */
1888struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
1889 u16 destid, struct rio_dma_data *data,
1890 enum dma_transfer_direction direction, unsigned long flags)
1891{
1892 struct rio_dma_ext rio_ext;
1893
Markus Elfring93dd49a2018-02-06 15:39:44 -08001894 if (!dchan->device->device_prep_slave_sg) {
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001895 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1896 return NULL;
1897 }
1898
1899 rio_ext.destid = destid;
1900 rio_ext.rio_addr_u = data->rio_addr_u;
1901 rio_ext.rio_addr = data->rio_addr;
1902 rio_ext.wr_type = data->wr_type;
1903
1904 return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1905 direction, flags, &rio_ext);
1906}
1907EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
1908
1909/**
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001910 * rio_dma_prep_slave_sg - RapidIO specific wrapper
1911 * for device_prep_slave_sg callback defined by DMAENGINE.
1912 * @rdev: RIO device control structure
1913 * @dchan: DMA channel to configure
1914 * @data: RIO specific data descriptor
1915 * @direction: DMA data transfer direction (TO or FROM the device)
1916 * @flags: dmaengine defined flags
1917 *
1918 * Initializes RapidIO capable DMA channel for the specified data transfer.
1919 * Uses DMA channel private extension to pass information related to remote
1920 * target RIO device.
Alexandre Bouninef8e3a682016-08-02 14:06:34 -07001921 *
1922 * Returns: pointer to DMA transaction descriptor if successful,
1923 * error-valued pointer or NULL if failed.
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001924 */
1925struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1926 struct dma_chan *dchan, struct rio_dma_data *data,
1927 enum dma_transfer_direction direction, unsigned long flags)
1928{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001929 return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001930}
1931EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1932
1933#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1934
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001935/**
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07001936 * rio_find_mport - find RIO mport by its ID
1937 * @mport_id: number (ID) of mport device
1938 *
1939 * Given a RIO mport number, the desired mport is located
1940 * in the global list of mports. If the mport is found, a pointer to its
1941 * data structure is returned. If no mport is found, %NULL is returned.
1942 */
1943struct rio_mport *rio_find_mport(int mport_id)
1944{
1945 struct rio_mport *port;
1946
1947 mutex_lock(&rio_mport_list_lock);
1948 list_for_each_entry(port, &rio_mports, node) {
1949 if (port->id == mport_id)
1950 goto found;
1951 }
1952 port = NULL;
1953found:
1954 mutex_unlock(&rio_mport_list_lock);
1955
1956 return port;
1957}
1958
1959/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001960 * rio_register_scan - enumeration/discovery method registration interface
1961 * @mport_id: mport device ID for which fabric scan routine has to be set
1962 * (RIO_MPORT_ANY = set for all available mports)
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001963 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001964 *
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001965 * Registers enumeration/discovery operations with RapidIO subsystem and
1966 * attaches it to the specified mport device (or all available mports
1967 * if RIO_MPORT_ANY is specified).
1968 *
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001969 * Returns error if the mport already has an enumerator attached to it.
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001970 * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001971 */
1972int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1973{
1974 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001975 struct rio_scan_node *scan;
1976 int rc = 0;
1977
1978 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1979
1980 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1981 !scan_ops)
1982 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001983
1984 mutex_lock(&rio_mport_list_lock);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001985
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001986 /*
1987 * Check if there is another enumerator already registered for
1988 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
1989 * for the same mport ID are not supported.
1990 */
1991 list_for_each_entry(scan, &rio_scans, node) {
1992 if (scan->mport_id == mport_id) {
1993 rc = -EBUSY;
1994 goto err_out;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001995 }
1996 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001997
1998 /*
1999 * Allocate and initialize new scan registration node.
2000 */
2001 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
2002 if (!scan) {
2003 rc = -ENOMEM;
2004 goto err_out;
2005 }
2006
2007 scan->mport_id = mport_id;
2008 scan->ops = scan_ops;
2009
2010 /*
2011 * Traverse the list of registered mports to attach this new scan.
2012 *
2013 * The new scan with matching mport ID overrides any previously attached
2014 * scan assuming that old scan (if any) is the default one (based on the
2015 * enumerator registration check above).
2016 * If the new scan is the global one, it will be attached only to mports
2017 * that do not have their own individual operations already attached.
2018 */
2019 list_for_each_entry(port, &rio_mports, node) {
2020 if (port->id == mport_id) {
2021 port->nscan = scan_ops;
2022 break;
2023 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
2024 port->nscan = scan_ops;
2025 }
2026
2027 list_add_tail(&scan->node, &rio_scans);
2028
2029err_out:
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002030 mutex_unlock(&rio_mport_list_lock);
2031
2032 return rc;
2033}
2034EXPORT_SYMBOL_GPL(rio_register_scan);
2035
2036/**
2037 * rio_unregister_scan - removes enumeration/discovery method from mport
2038 * @mport_id: mport device ID for which fabric scan routine has to be
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002039 * unregistered (RIO_MPORT_ANY = apply to all mports that use
2040 * the specified scan_ops)
2041 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002042 *
2043 * Removes enumeration or discovery method assigned to the specified mport
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002044 * device. If RIO_MPORT_ANY is specified, removes the specified operations from
2045 * all mports that have them attached.
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002046 */
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002047int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002048{
2049 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002050 struct rio_scan_node *scan;
2051
2052 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
2053
2054 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
2055 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002056
2057 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002058
2059 list_for_each_entry(port, &rio_mports, node)
2060 if (port->id == mport_id ||
2061 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
2062 port->nscan = NULL;
2063
Dan Carpenterf93f3c42013-07-31 13:53:34 -07002064 list_for_each_entry(scan, &rio_scans, node) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002065 if (scan->mport_id == mport_id) {
2066 list_del(&scan->node);
2067 kfree(scan);
Dan Carpenterf93f3c42013-07-31 13:53:34 -07002068 break;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002069 }
Dan Carpenterf93f3c42013-07-31 13:53:34 -07002070 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002071
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002072 mutex_unlock(&rio_mport_list_lock);
2073
2074 return 0;
2075}
2076EXPORT_SYMBOL_GPL(rio_unregister_scan);
2077
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002078/**
2079 * rio_mport_scan - execute enumeration/discovery on the specified mport
2080 * @mport_id: number (ID) of mport device
2081 */
2082int rio_mport_scan(int mport_id)
2083{
2084 struct rio_mport *port = NULL;
2085 int rc;
2086
2087 mutex_lock(&rio_mport_list_lock);
2088 list_for_each_entry(port, &rio_mports, node) {
2089 if (port->id == mport_id)
2090 goto found;
2091 }
2092 mutex_unlock(&rio_mport_list_lock);
2093 return -ENODEV;
2094found:
2095 if (!port->nscan) {
2096 mutex_unlock(&rio_mport_list_lock);
2097 return -EINVAL;
2098 }
2099
2100 if (!try_module_get(port->nscan->owner)) {
2101 mutex_unlock(&rio_mport_list_lock);
2102 return -ENODEV;
2103 }
2104
2105 mutex_unlock(&rio_mport_list_lock);
2106
2107 if (port->host_deviceid >= 0)
2108 rc = port->nscan->enumerate(port, 0);
2109 else
2110 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
2111
2112 module_put(port->nscan->owner);
2113 return rc;
2114}
2115
Matt Porter394b7012005-11-07 01:00:15 -08002116static void rio_fixup_device(struct rio_dev *dev)
2117{
2118}
2119
Bill Pemberton305c8912012-11-19 13:23:25 -05002120static int rio_init(void)
Matt Porter394b7012005-11-07 01:00:15 -08002121{
2122 struct rio_dev *dev = NULL;
2123
2124 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
2125 rio_fixup_device(dev);
2126 }
2127 return 0;
2128}
2129
Alexandre Bounine005842e2012-10-04 17:16:08 -07002130static struct workqueue_struct *rio_wq;
2131
2132struct rio_disc_work {
2133 struct work_struct work;
2134 struct rio_mport *mport;
2135};
2136
Bill Pemberton305c8912012-11-19 13:23:25 -05002137static void disc_work_handler(struct work_struct *_work)
Alexandre Bounine005842e2012-10-04 17:16:08 -07002138{
2139 struct rio_disc_work *work;
2140
2141 work = container_of(_work, struct rio_disc_work, work);
2142 pr_debug("RIO: discovery work for mport %d %s\n",
2143 work->mport->id, work->mport->name);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002144 if (try_module_get(work->mport->nscan->owner)) {
2145 work->mport->nscan->discover(work->mport, 0);
2146 module_put(work->mport->nscan->owner);
2147 }
Alexandre Bounine005842e2012-10-04 17:16:08 -07002148}
2149
Bill Pemberton305c8912012-11-19 13:23:25 -05002150int rio_init_mports(void)
Matt Porter394b7012005-11-07 01:00:15 -08002151{
Matt Porter394b7012005-11-07 01:00:15 -08002152 struct rio_mport *port;
Alexandre Bounine005842e2012-10-04 17:16:08 -07002153 struct rio_disc_work *work;
Alexandre Bounine25747402012-10-10 15:53:59 -07002154 int n = 0;
Matt Porter394b7012005-11-07 01:00:15 -08002155
Alexandre Bounine25747402012-10-10 15:53:59 -07002156 if (!next_portid)
2157 return -ENODEV;
2158
2159 /*
2160 * First, run enumerations and check if we need to perform discovery
2161 * on any of the registered mports.
2162 */
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002163 mutex_lock(&rio_mport_list_lock);
Matt Porter394b7012005-11-07 01:00:15 -08002164 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002165 if (port->host_deviceid >= 0) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002166 if (port->nscan && try_module_get(port->nscan->owner)) {
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07002167 port->nscan->enumerate(port, 0);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002168 module_put(port->nscan->owner);
2169 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002170 } else
Alexandre Bounine25747402012-10-10 15:53:59 -07002171 n++;
2172 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002173 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine005842e2012-10-04 17:16:08 -07002174
Alexandre Bounine25747402012-10-10 15:53:59 -07002175 if (!n)
2176 goto no_disc;
Alexandre Bounine005842e2012-10-04 17:16:08 -07002177
Alexandre Bounine25747402012-10-10 15:53:59 -07002178 /*
2179 * If we have mports that require discovery schedule a discovery work
2180 * for each of them. If the code below fails to allocate needed
2181 * resources, exit without error to keep results of enumeration
2182 * process (if any).
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002183 * TODO: Implement restart of discovery process for all or
Alexandre Bounine25747402012-10-10 15:53:59 -07002184 * individual discovering mports.
2185 */
2186 rio_wq = alloc_workqueue("riodisc", 0, 0);
2187 if (!rio_wq) {
2188 pr_err("RIO: unable allocate rio_wq\n");
2189 goto no_disc;
2190 }
2191
2192 work = kcalloc(n, sizeof *work, GFP_KERNEL);
2193 if (!work) {
Alexandre Bounine25747402012-10-10 15:53:59 -07002194 destroy_workqueue(rio_wq);
2195 goto no_disc;
2196 }
2197
2198 n = 0;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002199 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07002200 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002201 if (port->host_deviceid < 0 && port->nscan) {
Alexandre Bounine25747402012-10-10 15:53:59 -07002202 work[n].mport = port;
2203 INIT_WORK(&work[n].work, disc_work_handler);
2204 queue_work(rio_wq, &work[n].work);
2205 n++;
Alexandre Bounine005842e2012-10-04 17:16:08 -07002206 }
2207 }
2208
Alexandre Bounine25747402012-10-10 15:53:59 -07002209 flush_workqueue(rio_wq);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002210 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07002211 pr_debug("RIO: destroy discovery workqueue\n");
2212 destroy_workqueue(rio_wq);
2213 kfree(work);
Matt Porter394b7012005-11-07 01:00:15 -08002214
Alexandre Bounine25747402012-10-10 15:53:59 -07002215no_disc:
Alexandre Bounine2f809982011-03-23 16:43:04 -07002216 rio_init();
2217
Alexandre Bouninec1256eb2011-03-23 16:43:06 -07002218 return 0;
Matt Porter394b7012005-11-07 01:00:15 -08002219}
2220
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002221static int rio_get_hdid(int index)
2222{
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07002223 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002224 return -1;
2225
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07002226 return hdid[index];
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002227}
2228
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002229int rio_mport_initialize(struct rio_mport *mport)
2230{
2231 if (next_portid >= RIO_MAX_MPORTS) {
2232 pr_err("RIO: reached specified max number of mports\n");
2233 return -ENODEV;
2234 }
2235
2236 atomic_set(&mport->state, RIO_DEVICE_INITIALIZING);
2237 mport->id = next_portid++;
2238 mport->host_deviceid = rio_get_hdid(mport->id);
2239 mport->nscan = NULL;
Alexandre Bouninea7b4c632016-03-22 14:26:35 -07002240 mutex_init(&mport->lock);
Alexandre Bounineb6cb95e2016-03-22 14:26:41 -07002241 mport->pwe_refcnt = 0;
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07002242 INIT_LIST_HEAD(&mport->pwrites);
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002243
2244 return 0;
2245}
2246EXPORT_SYMBOL_GPL(rio_mport_initialize);
2247
Alexandre Bounine59f99962011-04-14 15:22:14 -07002248int rio_register_mport(struct rio_mport *port)
Matt Porter394b7012005-11-07 01:00:15 -08002249{
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002250 struct rio_scan_node *scan = NULL;
Alexandre Bounine2aaf3082014-04-07 15:38:56 -07002251 int res = 0;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002252
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002253 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002254
2255 /*
2256 * Check if there are any registered enumeration/discovery operations
2257 * that have to be attached to the added mport.
2258 */
2259 list_for_each_entry(scan, &rio_scans, node) {
2260 if (port->id == scan->mport_id ||
2261 scan->mport_id == RIO_MPORT_ANY) {
2262 port->nscan = scan->ops;
2263 if (port->id == scan->mport_id)
2264 break;
2265 }
2266 }
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002267
2268 list_add_tail(&port->node, &rio_mports);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002269 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002270
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002271 dev_set_name(&port->dev, "rapidio%d", port->id);
2272 port->dev.class = &rio_mport_class;
2273 atomic_set(&port->state, RIO_DEVICE_RUNNING);
2274
2275 res = device_register(&port->dev);
2276 if (res)
2277 dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
2278 port->id, res);
2279 else
2280 dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id);
2281
2282 return res;
Matt Porter394b7012005-11-07 01:00:15 -08002283}
Alexandre Bounine94d9bd42013-07-03 15:08:55 -07002284EXPORT_SYMBOL_GPL(rio_register_mport);
Matt Porter394b7012005-11-07 01:00:15 -08002285
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002286static int rio_mport_cleanup_callback(struct device *dev, void *data)
2287{
2288 struct rio_dev *rdev = to_rio_dev(dev);
2289
2290 if (dev->bus == &rio_bus_type)
2291 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
2292 return 0;
2293}
2294
2295static int rio_net_remove_children(struct rio_net *net)
2296{
2297 /*
2298 * Unregister all RapidIO devices residing on this net (this will
2299 * invoke notification of registered subsystem interfaces as well).
2300 */
2301 device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback);
2302 return 0;
2303}
2304
2305int rio_unregister_mport(struct rio_mport *port)
2306{
2307 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
2308
2309 /* Transition mport to the SHUTDOWN state */
2310 if (atomic_cmpxchg(&port->state,
2311 RIO_DEVICE_RUNNING,
2312 RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) {
2313 pr_err("RIO: %s unexpected state transition for mport %s\n",
2314 __func__, port->name);
2315 }
2316
2317 if (port->net && port->net->hport == port) {
2318 rio_net_remove_children(port->net);
2319 rio_free_net(port->net);
2320 }
2321
2322 /*
2323 * Unregister all RapidIO devices attached to this mport (this will
2324 * invoke notification of registered subsystem interfaces as well).
2325 */
2326 mutex_lock(&rio_mport_list_lock);
2327 list_del(&port->node);
2328 mutex_unlock(&rio_mport_list_lock);
2329 device_unregister(&port->dev);
2330
2331 return 0;
2332}
2333EXPORT_SYMBOL_GPL(rio_unregister_mport);
2334
Matt Porter394b7012005-11-07 01:00:15 -08002335EXPORT_SYMBOL_GPL(rio_local_get_device_id);
2336EXPORT_SYMBOL_GPL(rio_get_device);
2337EXPORT_SYMBOL_GPL(rio_get_asm);
2338EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
2339EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
2340EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
2341EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
2342EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
2343EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
2344EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
2345EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002346EXPORT_SYMBOL_GPL(rio_init_mports);