blob: 90534365e46cea2bba6997a192e237ce919c8371 [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{
Markus Elfringd1509c02018-02-06 15:39:51 -0800113 struct rio_net *net = kzalloc(sizeof(*net), GFP_KERNEL);
Alexandre Bouninee6b585c2016-03-22 14:26:17 -0700114
Alexandre Bouninee6b585c2016-03-22 14:26:17 -0700115 if (net) {
116 INIT_LIST_HEAD(&net->node);
117 INIT_LIST_HEAD(&net->devices);
118 INIT_LIST_HEAD(&net->switches);
119 INIT_LIST_HEAD(&net->mports);
120 mport->net = net;
121 }
122 return net;
123}
124EXPORT_SYMBOL_GPL(rio_alloc_net);
125
126int rio_add_net(struct rio_net *net)
127{
128 int err;
129
130 err = device_register(&net->dev);
131 if (err)
132 return err;
133 spin_lock(&rio_global_list_lock);
134 list_add_tail(&net->node, &rio_nets);
135 spin_unlock(&rio_global_list_lock);
136
137 return 0;
138}
139EXPORT_SYMBOL_GPL(rio_add_net);
140
141void rio_free_net(struct rio_net *net)
142{
143 spin_lock(&rio_global_list_lock);
144 if (!list_empty(&net->node))
145 list_del(&net->node);
146 spin_unlock(&rio_global_list_lock);
147 if (net->release)
148 net->release(net);
149 device_unregister(&net->dev);
150}
151EXPORT_SYMBOL_GPL(rio_free_net);
152
153/**
Alexandre Bounine50246222016-03-22 14:26:38 -0700154 * rio_local_set_device_id - Set the base/extended device id for a port
155 * @port: RIO master port
156 * @did: Device ID value to be written
157 *
158 * Writes the base/extended device id from a device.
159 */
160void rio_local_set_device_id(struct rio_mport *port, u16 did)
161{
162 rio_local_write_config_32(port, RIO_DID_CSR,
163 RIO_SET_DID(port->sys_size, did));
164}
165EXPORT_SYMBOL_GPL(rio_local_set_device_id);
166
167/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700168 * rio_add_device- Adds a RIO device to the device model
169 * @rdev: RIO device
170 *
171 * Adds the RIO device to the global device list and adds the RIO
172 * device to the RIO device list. Creates the generic sysfs nodes
173 * for an RIO device.
174 */
175int rio_add_device(struct rio_dev *rdev)
176{
177 int err;
178
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700179 atomic_set(&rdev->state, RIO_DEVICE_RUNNING);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700180 err = device_register(&rdev->dev);
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700181 if (err)
182 return err;
183
184 spin_lock(&rio_global_list_lock);
185 list_add_tail(&rdev->global_list, &rio_devices);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700186 if (rdev->net) {
187 list_add_tail(&rdev->net_list, &rdev->net->devices);
188 if (rdev->pef & RIO_PEF_SWITCH)
189 list_add_tail(&rdev->rswitch->node,
190 &rdev->net->switches);
191 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700192 spin_unlock(&rio_global_list_lock);
193
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700194 return 0;
195}
196EXPORT_SYMBOL_GPL(rio_add_device);
197
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700198/*
199 * rio_del_device - removes a RIO device from the device model
200 * @rdev: RIO device
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700201 * @state: device state to set during removal process
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700202 *
203 * Removes the RIO device to the kernel device list and subsystem's device list.
204 * Clears sysfs entries for the removed device.
205 */
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700206void rio_del_device(struct rio_dev *rdev, enum rio_device_state state)
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700207{
208 pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev));
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700209 atomic_set(&rdev->state, state);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700210 spin_lock(&rio_global_list_lock);
211 list_del(&rdev->global_list);
212 if (rdev->net) {
213 list_del(&rdev->net_list);
214 if (rdev->pef & RIO_PEF_SWITCH) {
215 list_del(&rdev->rswitch->node);
216 kfree(rdev->rswitch->route_table);
217 }
218 }
219 spin_unlock(&rio_global_list_lock);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700220 device_unregister(&rdev->dev);
221}
222EXPORT_SYMBOL_GPL(rio_del_device);
223
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700224/**
Matt Porter394b7012005-11-07 01:00:15 -0800225 * rio_request_inb_mbox - request inbound mailbox service
226 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800227 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800228 * @mbox: Mailbox number to claim
229 * @entries: Number of entries in inbound mailbox queue
230 * @minb: Callback to execute when inbound message is received
231 *
232 * Requests ownership of an inbound mailbox resource and binds
233 * a callback function to the resource. Returns %0 on success.
234 */
235int rio_request_inb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800236 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800237 int mbox,
238 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800239 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
Matt Porter394b7012005-11-07 01:00:15 -0800240 int slot))
241{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700242 int rc = -ENOSYS;
243 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800244
Markus Elfring93dd49a2018-02-06 15:39:44 -0800245 if (!mport->ops->open_inb_mbox)
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700246 goto out;
247
Markus Elfringd1509c02018-02-06 15:39:51 -0800248 res = kzalloc(sizeof(*res), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800249 if (res) {
250 rio_init_mbox_res(res, mbox, mbox);
251
252 /* Make sure this mailbox isn't in use */
Markus Elfringe1d66d02018-02-06 15:39:48 -0800253 rc = request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
254 res);
255 if (rc < 0) {
Matt Porter394b7012005-11-07 01:00:15 -0800256 kfree(res);
257 goto out;
258 }
259
260 mport->inb_msg[mbox].res = res;
261
262 /* Hook the inbound message callback */
263 mport->inb_msg[mbox].mcback = minb;
264
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700265 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700266 if (rc) {
267 mport->inb_msg[mbox].mcback = NULL;
268 mport->inb_msg[mbox].res = NULL;
269 release_resource(res);
270 kfree(res);
271 }
Matt Porter394b7012005-11-07 01:00:15 -0800272 } else
273 rc = -ENOMEM;
274
275 out:
276 return rc;
277}
278
279/**
280 * rio_release_inb_mbox - release inbound mailbox message service
281 * @mport: RIO master port from which to release the mailbox resource
282 * @mbox: Mailbox number to release
283 *
284 * Releases ownership of an inbound mailbox resource. Returns 0
285 * if the request has been satisfied.
286 */
287int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
288{
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700289 int rc;
Matt Porter394b7012005-11-07 01:00:15 -0800290
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700291 if (!mport->ops->close_inb_mbox || !mport->inb_msg[mbox].res)
292 return -EINVAL;
293
294 mport->ops->close_inb_mbox(mport, mbox);
295 mport->inb_msg[mbox].mcback = NULL;
296
297 rc = release_resource(mport->inb_msg[mbox].res);
298 if (rc)
299 return rc;
300
301 kfree(mport->inb_msg[mbox].res);
302 mport->inb_msg[mbox].res = NULL;
303
304 return 0;
Matt Porter394b7012005-11-07 01:00:15 -0800305}
306
307/**
308 * rio_request_outb_mbox - request outbound mailbox service
309 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800310 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800311 * @mbox: Mailbox number to claim
312 * @entries: Number of entries in outbound mailbox queue
313 * @moutb: Callback to execute when outbound message is sent
314 *
315 * Requests ownership of an outbound mailbox resource and binds
316 * a callback function to the resource. Returns 0 on success.
317 */
318int rio_request_outb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800319 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800320 int mbox,
321 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800322 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
Matt Porter394b7012005-11-07 01:00:15 -0800323{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700324 int rc = -ENOSYS;
325 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800326
Markus Elfring93dd49a2018-02-06 15:39:44 -0800327 if (!mport->ops->open_outb_mbox)
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700328 goto out;
329
Markus Elfringd1509c02018-02-06 15:39:51 -0800330 res = kzalloc(sizeof(*res), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800331 if (res) {
332 rio_init_mbox_res(res, mbox, mbox);
333
334 /* Make sure this outbound mailbox isn't in use */
Markus Elfringe1d66d02018-02-06 15:39:48 -0800335 rc = request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
336 res);
337 if (rc < 0) {
Matt Porter394b7012005-11-07 01:00:15 -0800338 kfree(res);
339 goto out;
340 }
341
342 mport->outb_msg[mbox].res = res;
343
344 /* Hook the inbound message callback */
345 mport->outb_msg[mbox].mcback = moutb;
346
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700347 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700348 if (rc) {
349 mport->outb_msg[mbox].mcback = NULL;
350 mport->outb_msg[mbox].res = NULL;
351 release_resource(res);
352 kfree(res);
353 }
Matt Porter394b7012005-11-07 01:00:15 -0800354 } else
355 rc = -ENOMEM;
356
357 out:
358 return rc;
359}
360
361/**
362 * rio_release_outb_mbox - release outbound mailbox message service
363 * @mport: RIO master port from which to release the mailbox resource
364 * @mbox: Mailbox number to release
365 *
366 * Releases ownership of an inbound mailbox resource. Returns 0
367 * if the request has been satisfied.
368 */
369int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
370{
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700371 int rc;
Matt Porter394b7012005-11-07 01:00:15 -0800372
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700373 if (!mport->ops->close_outb_mbox || !mport->outb_msg[mbox].res)
374 return -EINVAL;
375
376 mport->ops->close_outb_mbox(mport, mbox);
377 mport->outb_msg[mbox].mcback = NULL;
378
379 rc = release_resource(mport->outb_msg[mbox].res);
380 if (rc)
381 return rc;
382
383 kfree(mport->outb_msg[mbox].res);
384 mport->outb_msg[mbox].res = NULL;
385
386 return 0;
Matt Porter394b7012005-11-07 01:00:15 -0800387}
388
389/**
390 * rio_setup_inb_dbell - bind inbound doorbell callback
391 * @mport: RIO master port to bind the doorbell callback
Matt Porter6978bbc2005-11-07 01:00:20 -0800392 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800393 * @res: Doorbell message resource
394 * @dinb: Callback to execute when doorbell is received
395 *
396 * Adds a doorbell resource/callback pair into a port's
397 * doorbell event list. Returns 0 if the request has been
398 * satisfied.
399 */
400static int
Matt Porter6978bbc2005-11-07 01:00:20 -0800401rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
402 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
Matt Porter394b7012005-11-07 01:00:15 -0800403 u16 info))
404{
405 int rc = 0;
Markus Elfringe1d66d02018-02-06 15:39:48 -0800406 struct rio_dbell *dbell = kmalloc(sizeof(*dbell), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800407
Markus Elfringe1d66d02018-02-06 15:39:48 -0800408 if (!dbell) {
Matt Porter394b7012005-11-07 01:00:15 -0800409 rc = -ENOMEM;
410 goto out;
411 }
412
413 dbell->res = res;
414 dbell->dinb = dinb;
Matt Porter6978bbc2005-11-07 01:00:20 -0800415 dbell->dev_id = dev_id;
Matt Porter394b7012005-11-07 01:00:15 -0800416
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700417 mutex_lock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800418 list_add_tail(&dbell->node, &mport->dbells);
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700419 mutex_unlock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800420
421 out:
422 return rc;
423}
424
425/**
426 * rio_request_inb_dbell - request inbound doorbell message service
427 * @mport: RIO master port from which to allocate the doorbell resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800428 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800429 * @start: Doorbell info range start
430 * @end: Doorbell info range end
431 * @dinb: Callback to execute when doorbell is received
432 *
433 * Requests ownership of an inbound doorbell resource and binds
434 * a callback function to the resource. Returns 0 if the request
435 * has been satisfied.
436 */
437int rio_request_inb_dbell(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800438 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800439 u16 start,
440 u16 end,
Matt Porter6978bbc2005-11-07 01:00:20 -0800441 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
Matt Porter394b7012005-11-07 01:00:15 -0800442 u16 dst, u16 info))
443{
444 int rc = 0;
Markus Elfringd1509c02018-02-06 15:39:51 -0800445 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800446
447 if (res) {
448 rio_init_dbell_res(res, start, end);
449
450 /* Make sure these doorbells aren't in use */
Markus Elfringe1d66d02018-02-06 15:39:48 -0800451 rc = request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
452 res);
453 if (rc < 0) {
Matt Porter394b7012005-11-07 01:00:15 -0800454 kfree(res);
455 goto out;
456 }
457
458 /* Hook the doorbell callback */
Matt Porter6978bbc2005-11-07 01:00:20 -0800459 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
Matt Porter394b7012005-11-07 01:00:15 -0800460 } else
461 rc = -ENOMEM;
462
463 out:
464 return rc;
465}
466
467/**
468 * rio_release_inb_dbell - release inbound doorbell message service
469 * @mport: RIO master port from which to release the doorbell resource
470 * @start: Doorbell info range start
471 * @end: Doorbell info range end
472 *
473 * Releases ownership of an inbound doorbell resource and removes
474 * callback from the doorbell event list. Returns 0 if the request
475 * has been satisfied.
476 */
477int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
478{
479 int rc = 0, found = 0;
480 struct rio_dbell *dbell;
481
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700482 mutex_lock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800483 list_for_each_entry(dbell, &mport->dbells, node) {
484 if ((dbell->res->start == start) && (dbell->res->end == end)) {
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700485 list_del(&dbell->node);
Matt Porter394b7012005-11-07 01:00:15 -0800486 found = 1;
487 break;
488 }
489 }
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700490 mutex_unlock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800491
492 /* If we can't find an exact match, fail */
493 if (!found) {
494 rc = -EINVAL;
495 goto out;
496 }
497
Matt Porter394b7012005-11-07 01:00:15 -0800498 /* Release the doorbell resource */
499 rc = release_resource(dbell->res);
500
501 /* Free the doorbell event */
502 kfree(dbell);
503
504 out:
505 return rc;
506}
507
508/**
509 * rio_request_outb_dbell - request outbound doorbell message range
510 * @rdev: RIO device from which to allocate the doorbell resource
511 * @start: Doorbell message range start
512 * @end: Doorbell message range end
513 *
514 * Requests ownership of a doorbell message range. Returns a resource
515 * if the request has been satisfied or %NULL on failure.
516 */
517struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
518 u16 end)
519{
Toshi Kani9a975be2016-01-26 21:57:25 +0100520 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800521
522 if (res) {
523 rio_init_dbell_res(res, start, end);
524
525 /* Make sure these doorbells aren't in use */
526 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
527 < 0) {
528 kfree(res);
529 res = NULL;
530 }
531 }
532
533 return res;
534}
535
536/**
537 * rio_release_outb_dbell - release outbound doorbell message range
538 * @rdev: RIO device from which to release the doorbell resource
539 * @res: Doorbell resource to be freed
540 *
541 * Releases ownership of a doorbell message range. Returns 0 if the
542 * request has been satisfied.
543 */
544int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
545{
546 int rc = release_resource(res);
547
548 kfree(res);
549
550 return rc;
551}
552
553/**
Alexandre Bounine9a0b0622016-03-22 14:26:44 -0700554 * rio_add_mport_pw_handler - add port-write message handler into the list
555 * of mport specific pw handlers
556 * @mport: RIO master port to bind the portwrite callback
557 * @context: Handler specific context to pass on event
558 * @pwcback: Callback to execute when portwrite is received
559 *
560 * Returns 0 if the request has been satisfied.
561 */
562int rio_add_mport_pw_handler(struct rio_mport *mport, void *context,
563 int (*pwcback)(struct rio_mport *mport,
564 void *context, union rio_pw_msg *msg, int step))
565{
566 int rc = 0;
Markus Elfringd1509c02018-02-06 15:39:51 -0800567 struct rio_pwrite *pwrite = kzalloc(sizeof(*pwrite), GFP_KERNEL);
Alexandre Bounine9a0b0622016-03-22 14:26:44 -0700568
Alexandre Bounine9a0b0622016-03-22 14:26:44 -0700569 if (!pwrite) {
570 rc = -ENOMEM;
571 goto out;
572 }
573
574 pwrite->pwcback = pwcback;
575 pwrite->context = context;
576 mutex_lock(&mport->lock);
577 list_add_tail(&pwrite->node, &mport->pwrites);
578 mutex_unlock(&mport->lock);
579out:
580 return rc;
581}
582EXPORT_SYMBOL_GPL(rio_add_mport_pw_handler);
583
584/**
585 * rio_del_mport_pw_handler - remove port-write message handler from the list
586 * of mport specific pw handlers
587 * @mport: RIO master port to bind the portwrite callback
588 * @context: Registered handler specific context to pass on event
589 * @pwcback: Registered callback function
590 *
591 * Returns 0 if the request has been satisfied.
592 */
593int rio_del_mport_pw_handler(struct rio_mport *mport, void *context,
594 int (*pwcback)(struct rio_mport *mport,
595 void *context, union rio_pw_msg *msg, int step))
596{
597 int rc = -EINVAL;
598 struct rio_pwrite *pwrite;
599
600 mutex_lock(&mport->lock);
601 list_for_each_entry(pwrite, &mport->pwrites, node) {
602 if (pwrite->pwcback == pwcback && pwrite->context == context) {
603 list_del(&pwrite->node);
604 kfree(pwrite);
605 rc = 0;
606 break;
607 }
608 }
609 mutex_unlock(&mport->lock);
610
611 return rc;
612}
613EXPORT_SYMBOL_GPL(rio_del_mport_pw_handler);
614
615/**
616 * rio_request_inb_pwrite - request inbound port-write message service for
617 * specific RapidIO device
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700618 * @rdev: RIO device to which register inbound port-write callback routine
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700619 * @pwcback: Callback routine to execute when port-write is received
620 *
621 * Binds a port-write callback function to the RapidIO device.
622 * Returns 0 if the request has been satisfied.
623 */
624int rio_request_inb_pwrite(struct rio_dev *rdev,
625 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
626{
627 int rc = 0;
628
629 spin_lock(&rio_global_list_lock);
Markus Elfring93dd49a2018-02-06 15:39:44 -0800630 if (rdev->pwcback)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700631 rc = -ENOMEM;
632 else
633 rdev->pwcback = pwcback;
634
635 spin_unlock(&rio_global_list_lock);
636 return rc;
637}
638EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
639
640/**
641 * rio_release_inb_pwrite - release inbound port-write message service
Alexandre Bounine9a0b0622016-03-22 14:26:44 -0700642 * associated with specific RapidIO device
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700643 * @rdev: RIO device which registered for inbound port-write callback
644 *
645 * Removes callback from the rio_dev structure. Returns 0 if the request
646 * has been satisfied.
647 */
648int rio_release_inb_pwrite(struct rio_dev *rdev)
649{
650 int rc = -ENOMEM;
651
652 spin_lock(&rio_global_list_lock);
653 if (rdev->pwcback) {
654 rdev->pwcback = NULL;
655 rc = 0;
656 }
657
658 spin_unlock(&rio_global_list_lock);
659 return rc;
660}
661EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
662
663/**
Alexandre Bounineb6cb95e2016-03-22 14:26:41 -0700664 * rio_pw_enable - Enables/disables port-write handling by a master port
665 * @mport: Master port associated with port-write handling
666 * @enable: 1=enable, 0=disable
667 */
668void rio_pw_enable(struct rio_mport *mport, int enable)
669{
670 if (mport->ops->pwenable) {
671 mutex_lock(&mport->lock);
672
673 if ((enable && ++mport->pwe_refcnt == 1) ||
674 (!enable && mport->pwe_refcnt && --mport->pwe_refcnt == 0))
675 mport->ops->pwenable(mport, enable);
676 mutex_unlock(&mport->lock);
677 }
678}
679EXPORT_SYMBOL_GPL(rio_pw_enable);
680
681/**
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700682 * rio_map_inb_region -- Map inbound memory region.
683 * @mport: Master port.
Randy Dunlap2ca3cb52012-11-16 14:14:56 -0800684 * @local: physical address of memory region to be mapped
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700685 * @rbase: RIO base address assigned to this window
686 * @size: Size of the memory region
687 * @rflags: Flags for mapping.
688 *
689 * Return: 0 -- Success.
690 *
691 * This function will create the mapping from RIO space to local memory.
692 */
693int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
694 u64 rbase, u32 size, u32 rflags)
695{
696 int rc = 0;
697 unsigned long flags;
698
699 if (!mport->ops->map_inb)
700 return -1;
701 spin_lock_irqsave(&rio_mmap_lock, flags);
702 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
703 spin_unlock_irqrestore(&rio_mmap_lock, flags);
704 return rc;
705}
706EXPORT_SYMBOL_GPL(rio_map_inb_region);
707
708/**
709 * rio_unmap_inb_region -- Unmap the inbound memory region
710 * @mport: Master port
711 * @lstart: physical address of memory region to be unmapped
712 */
713void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
714{
715 unsigned long flags;
716 if (!mport->ops->unmap_inb)
717 return;
718 spin_lock_irqsave(&rio_mmap_lock, flags);
719 mport->ops->unmap_inb(mport, lstart);
720 spin_unlock_irqrestore(&rio_mmap_lock, flags);
721}
722EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
723
724/**
Alexandre Bounine93bdaca2016-03-22 14:26:50 -0700725 * rio_map_outb_region -- Map outbound memory region.
726 * @mport: Master port.
727 * @destid: destination id window points to
728 * @rbase: RIO base address window translates to
729 * @size: Size of the memory region
730 * @rflags: Flags for mapping.
731 * @local: physical address of memory region mapped
732 *
733 * Return: 0 -- Success.
734 *
735 * This function will create the mapping from RIO space to local memory.
736 */
737int rio_map_outb_region(struct rio_mport *mport, u16 destid, u64 rbase,
738 u32 size, u32 rflags, dma_addr_t *local)
739{
740 int rc = 0;
741 unsigned long flags;
742
743 if (!mport->ops->map_outb)
744 return -ENODEV;
745
746 spin_lock_irqsave(&rio_mmap_lock, flags);
747 rc = mport->ops->map_outb(mport, destid, rbase, size,
748 rflags, local);
749 spin_unlock_irqrestore(&rio_mmap_lock, flags);
750
751 return rc;
752}
753EXPORT_SYMBOL_GPL(rio_map_outb_region);
754
755/**
756 * rio_unmap_inb_region -- Unmap the inbound memory region
757 * @mport: Master port
758 * @destid: destination id mapping points to
759 * @rstart: RIO base address window translates to
760 */
761void rio_unmap_outb_region(struct rio_mport *mport, u16 destid, u64 rstart)
762{
763 unsigned long flags;
764
765 if (!mport->ops->unmap_outb)
766 return;
767
768 spin_lock_irqsave(&rio_mmap_lock, flags);
769 mport->ops->unmap_outb(mport, destid, rstart);
770 spin_unlock_irqrestore(&rio_mmap_lock, flags);
771}
772EXPORT_SYMBOL_GPL(rio_unmap_outb_region);
773
774/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700775 * rio_mport_get_physefb - Helper function that returns register offset
776 * for Physical Layer Extended Features Block.
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700777 * @port: Master port to issue transaction
778 * @local: Indicate a local master port or remote device access
779 * @destid: Destination ID of the device
780 * @hopcount: Number of switch hops to the device
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700781 * @rmap: pointer to location to store register map type info
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700782 */
783u32
784rio_mport_get_physefb(struct rio_mport *port, int local,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700785 u16 destid, u8 hopcount, u32 *rmap)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700786{
787 u32 ext_ftr_ptr;
788 u32 ftr_header;
789
790 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
791
792 while (ext_ftr_ptr) {
793 if (local)
794 rio_local_read_config_32(port, ext_ftr_ptr,
795 &ftr_header);
796 else
797 rio_mport_read_config_32(port, destid, hopcount,
798 ext_ftr_ptr, &ftr_header);
799
800 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
801 switch (ftr_header) {
802
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700803 case RIO_EFB_SER_EP_ID:
804 case RIO_EFB_SER_EP_REC_ID:
805 case RIO_EFB_SER_EP_FREE_ID:
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700806 case RIO_EFB_SER_EP_M1_ID:
807 case RIO_EFB_SER_EP_SW_M1_ID:
808 case RIO_EFB_SER_EPF_M1_ID:
809 case RIO_EFB_SER_EPF_SW_M1_ID:
810 *rmap = 1;
811 return ext_ftr_ptr;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700812
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700813 case RIO_EFB_SER_EP_M2_ID:
814 case RIO_EFB_SER_EP_SW_M2_ID:
815 case RIO_EFB_SER_EPF_M2_ID:
816 case RIO_EFB_SER_EPF_SW_M2_ID:
817 *rmap = 2;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700818 return ext_ftr_ptr;
819
820 default:
821 break;
822 }
823
824 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
825 hopcount, ext_ftr_ptr);
826 }
827
828 return ext_ftr_ptr;
829}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700830EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700831
832/**
833 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700834 * @comp_tag: RIO component tag to match
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700835 * @from: Previous RIO device found in search, or %NULL for new search
836 *
837 * Iterates through the list of known RIO devices. If a RIO device is
838 * found with a matching @comp_tag, a pointer to its device
839 * structure is returned. Otherwise, %NULL is returned. A new search
840 * is initiated by passing %NULL to the @from argument. Otherwise, if
841 * @from is not %NULL, searches continue from next device on the global
842 * list.
843 */
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700844struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700845{
846 struct list_head *n;
847 struct rio_dev *rdev;
848
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700849 spin_lock(&rio_global_list_lock);
850 n = from ? from->global_list.next : rio_devices.next;
851
852 while (n && (n != &rio_devices)) {
853 rdev = rio_dev_g(n);
854 if (rdev->comp_tag == comp_tag)
855 goto exit;
856 n = n->next;
857 }
858 rdev = NULL;
859exit:
860 spin_unlock(&rio_global_list_lock);
861 return rdev;
862}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700863EXPORT_SYMBOL_GPL(rio_get_comptag);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700864
865/**
866 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
867 * @rdev: Pointer to RIO device control structure
868 * @pnum: Switch port number to set LOCKOUT bit
869 * @lock: Operation : set (=1) or clear (=0)
870 */
871int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
872{
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700873 u32 regval;
874
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800875 rio_read_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700876 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
877 &regval);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700878 if (lock)
879 regval |= RIO_PORT_N_CTL_LOCKOUT;
880 else
881 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
882
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800883 rio_write_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700884 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
885 regval);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700886 return 0;
887}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700888EXPORT_SYMBOL_GPL(rio_set_port_lockout);
889
890/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700891 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
892 * given port
893 * @port: Master port associated with the RIO network
894 * @local: local=1 select local port otherwise a far device is reached
895 * @destid: Destination ID of the device to check host bit
896 * @hopcount: Number of hops to reach the target
897 * @port_num: Port (-number on switch) to enable on a far end device
898 *
899 * Returns 0 or 1 from on General Control Command and Status Register
900 * (EXT_PTR+0x3C)
901 */
902int rio_enable_rx_tx_port(struct rio_mport *port,
903 int local, u16 destid,
904 u8 hopcount, u8 port_num)
905{
906#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
907 u32 regval;
908 u32 ext_ftr_ptr;
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700909 u32 rmap;
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700910
911 /*
912 * enable rx input tx output port
913 */
914 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
915 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
916
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700917 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid,
918 hopcount, &rmap);
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700919
920 if (local) {
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700921 rio_local_read_config_32(port,
922 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap),
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700923 &regval);
924 } else {
925 if (rio_mport_read_config_32(port, destid, hopcount,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700926 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
927 &regval) < 0)
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700928 return -EIO;
929 }
930
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700931 regval = regval | RIO_PORT_N_CTL_EN_RX | RIO_PORT_N_CTL_EN_TX;
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700932
933 if (local) {
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700934 rio_local_write_config_32(port,
935 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap), regval);
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700936 } else {
937 if (rio_mport_write_config_32(port, destid, hopcount,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -0700938 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
939 regval) < 0)
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700940 return -EIO;
941 }
942#endif
943 return 0;
944}
945EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
946
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700947
948/**
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700949 * rio_chk_dev_route - Validate route to the specified device.
950 * @rdev: RIO device failed to respond
951 * @nrdev: Last active device on the route to rdev
952 * @npnum: nrdev's port number on the route to rdev
953 *
954 * Follows a route to the specified RIO device to determine the last available
955 * device (and corresponding RIO port) on the route.
956 */
957static int
958rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
959{
960 u32 result;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800961 int p_port, rc = -EIO;
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700962 struct rio_dev *prev = NULL;
963
964 /* Find switch with failed RIO link */
965 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
966 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
967 prev = rdev->prev;
968 break;
969 }
970 rdev = rdev->prev;
971 }
972
Markus Elfring93dd49a2018-02-06 15:39:44 -0800973 if (!prev)
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700974 goto err_out;
975
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800976 p_port = prev->rswitch->route_table[rdev->destid];
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700977
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700978 if (p_port != RIO_INVALID_ROUTE) {
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700979 pr_debug("RIO: link failed on [%s]-P%d\n",
980 rio_name(prev), p_port);
981 *nrdev = prev;
982 *npnum = p_port;
983 rc = 0;
984 } else
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700985 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700986err_out:
987 return rc;
988}
989
990/**
991 * rio_mport_chk_dev_access - Validate access to the specified device.
992 * @mport: Master port to send transactions
993 * @destid: Device destination ID in network
994 * @hopcount: Number of hops into the network
995 */
Alexandre Bouninee274e0e2010-10-27 15:34:32 -0700996int
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700997rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
998{
999 int i = 0;
1000 u32 tmp;
1001
1002 while (rio_mport_read_config_32(mport, destid, hopcount,
1003 RIO_DEV_ID_CAR, &tmp)) {
1004 i++;
1005 if (i == RIO_MAX_CHK_RETRY)
1006 return -EIO;
1007 mdelay(1);
1008 }
1009
1010 return 0;
1011}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001012EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001013
1014/**
1015 * rio_chk_dev_access - Validate access to the specified device.
1016 * @rdev: Pointer to RIO device control structure
1017 */
1018static int rio_chk_dev_access(struct rio_dev *rdev)
1019{
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001020 return rio_mport_chk_dev_access(rdev->net->hport,
1021 rdev->destid, rdev->hopcount);
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001022}
1023
1024/**
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001025 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
1026 * returns link-response (if requested).
1027 * @rdev: RIO devive to issue Input-status command
1028 * @pnum: Device port number to issue the command
1029 * @lnkresp: Response from a link partner
1030 */
1031static int
1032rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
1033{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001034 u32 regval;
1035 int checkcount;
1036
1037 if (lnkresp) {
1038 /* Read from link maintenance response register
1039 * to clear valid bit */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001040 rio_read_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001041 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001042 &regval);
1043 udelay(50);
1044 }
1045
1046 /* Issue Input-status command */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001047 rio_write_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001048 RIO_DEV_PORT_N_MNT_REQ_CSR(rdev, pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001049 RIO_MNT_REQ_CMD_IS);
1050
1051 /* Exit if the response is not expected */
Markus Elfring93dd49a2018-02-06 15:39:44 -08001052 if (!lnkresp)
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001053 return 0;
1054
1055 checkcount = 3;
1056 while (checkcount--) {
1057 udelay(50);
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001058 rio_read_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001059 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001060 &regval);
1061 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
1062 *lnkresp = regval;
1063 return 0;
1064 }
1065 }
1066
1067 return -EIO;
1068}
1069
1070/**
1071 * rio_clr_err_stopped - Clears port Error-stopped states.
1072 * @rdev: Pointer to RIO device control structure
1073 * @pnum: Switch port number to clear errors
1074 * @err_status: port error status (if 0 reads register from device)
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001075 *
1076 * TODO: Currently this routine is not compatible with recovery process
1077 * specified for idt_gen3 RapidIO switch devices. It has to be reviewed
1078 * to implement universal recovery process that is compatible full range
1079 * off available devices.
1080 * IDT gen3 switch driver now implements HW-specific error handler that
1081 * issues soft port reset to the port to reset ERR_STOP bits and ackIDs.
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001082 */
1083static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
1084{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001085 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
1086 u32 regval;
1087 u32 far_ackid, far_linkstat, near_ackid;
1088
1089 if (err_status == 0)
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001090 rio_read_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001091 RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001092 &err_status);
1093
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001094 if (err_status & RIO_PORT_N_ERR_STS_OUT_ES) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001095 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
1096 /*
1097 * Send a Link-Request/Input-Status control symbol
1098 */
1099 if (rio_get_input_status(rdev, pnum, &regval)) {
1100 pr_debug("RIO_EM: Input-status response timeout\n");
1101 goto rd_err;
1102 }
1103
1104 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
1105 pnum, regval);
1106 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
1107 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001108 rio_read_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001109 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001110 &regval);
1111 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
1112 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
1113 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
1114 " near_ackID=0x%02x\n",
1115 pnum, far_ackid, far_linkstat, near_ackid);
1116
1117 /*
1118 * If required, synchronize ackIDs of near and
1119 * far sides.
1120 */
1121 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
1122 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
1123 /* Align near outstanding/outbound ackIDs with
1124 * far inbound.
1125 */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001126 rio_write_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001127 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001128 (near_ackid << 24) |
1129 (far_ackid << 8) | far_ackid);
1130 /* Align far outstanding/outbound ackIDs with
1131 * near inbound.
1132 */
1133 far_ackid++;
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001134 if (!nextdev) {
1135 pr_debug("RIO_EM: nextdev pointer == NULL\n");
1136 goto rd_err;
1137 }
1138
1139 rio_write_config_32(nextdev,
1140 RIO_DEV_PORT_N_ACK_STS_CSR(nextdev,
1141 RIO_GET_PORT_NUM(nextdev->swpinfo)),
1142 (far_ackid << 24) |
1143 (near_ackid << 8) | near_ackid);
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001144 }
1145rd_err:
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001146 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1147 &err_status);
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001148 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1149 }
1150
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001151 if ((err_status & RIO_PORT_N_ERR_STS_INP_ES) && nextdev) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001152 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
1153 rio_get_input_status(nextdev,
1154 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
1155 udelay(50);
1156
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001157 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1158 &err_status);
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001159 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1160 }
1161
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001162 return (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
1163 RIO_PORT_N_ERR_STS_INP_ES)) ? 1 : 0;
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001164}
1165
1166/**
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001167 * rio_inb_pwrite_handler - inbound port-write message handler
1168 * @mport: mport device associated with port-write
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001169 * @pw_msg: pointer to inbound port-write message
1170 *
1171 * Processes an inbound port-write message. Returns 0 if the request
1172 * has been satisfied.
1173 */
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001174int rio_inb_pwrite_handler(struct rio_mport *mport, union rio_pw_msg *pw_msg)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001175{
1176 struct rio_dev *rdev;
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001177 u32 err_status, em_perrdet, em_ltlerrdet;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001178 int rc, portnum;
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001179 struct rio_pwrite *pwrite;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001180
1181#ifdef DEBUG_PW
1182 {
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001183 u32 i;
1184
1185 pr_debug("%s: PW to mport_%d:\n", __func__, mport->id);
1186 for (i = 0; i < RIO_PW_MSG_SIZE / sizeof(u32); i = i + 4) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001187 pr_debug("0x%02x: %08x %08x %08x %08x\n",
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001188 i * 4, pw_msg->raw[i], pw_msg->raw[i + 1],
1189 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
1190 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001191 }
1192#endif
1193
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001194 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
1195 if (rdev) {
1196 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
1197 } else {
1198 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
1199 __func__, pw_msg->em.comptag);
1200 }
1201
1202 /* Call a device-specific handler (if it is registered for the device).
1203 * This may be the service for endpoints that send device-specific
1204 * port-write messages. End-point messages expected to be handled
1205 * completely by EP specific device driver.
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001206 * For switches rc==0 signals that no standard processing required.
1207 */
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001208 if (rdev && rdev->pwcback) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001209 rc = rdev->pwcback(rdev, pw_msg, 0);
1210 if (rc == 0)
1211 return 0;
1212 }
1213
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001214 mutex_lock(&mport->lock);
1215 list_for_each_entry(pwrite, &mport->pwrites, node)
1216 pwrite->pwcback(mport, pwrite->context, pw_msg, 0);
1217 mutex_unlock(&mport->lock);
1218
1219 if (!rdev)
1220 return 0;
1221
1222 /*
1223 * FIXME: The code below stays as it was before for now until we decide
1224 * how to do default PW handling in combination with per-mport callbacks
1225 */
1226
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001227 portnum = pw_msg->em.is_port & 0xFF;
1228
1229 /* Check if device and route to it are functional:
1230 * Sometimes devices may send PW message(s) just before being
1231 * powered down (or link being lost).
1232 */
1233 if (rio_chk_dev_access(rdev)) {
1234 pr_debug("RIO: device access failed - get link partner\n");
1235 /* Scan route to the device and identify failed link.
1236 * This will replace device and port reported in PW message.
1237 * PW message should not be used after this point.
1238 */
1239 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
1240 pr_err("RIO: Route trace for %s failed\n",
1241 rio_name(rdev));
1242 return -EIO;
1243 }
1244 pw_msg = NULL;
1245 }
1246
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001247 /* For End-point devices processing stops here */
1248 if (!(rdev->pef & RIO_PEF_SWITCH))
1249 return 0;
1250
1251 if (rdev->phys_efptr == 0) {
1252 pr_err("RIO_PW: Bad switch initialization for %s\n",
1253 rio_name(rdev));
1254 return 0;
1255 }
1256
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001257 /*
1258 * Process the port-write notification from switch
1259 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001260 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
1261 rdev->rswitch->ops->em_handle(rdev, portnum);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001262
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001263 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1264 &err_status);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001265 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
1266
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001267 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001268
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001269 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
1270 rdev->rswitch->port_ok |= (1 << portnum);
1271 rio_set_port_lockout(rdev, portnum, 0);
1272 /* Schedule Insertion Service */
1273 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
1274 rio_name(rdev), portnum);
1275 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001276
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001277 /* Clear error-stopped states (if reported).
1278 * Depending on the link partner state, two attempts
1279 * may be needed for successful recovery.
1280 */
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001281 if (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
1282 RIO_PORT_N_ERR_STS_INP_ES)) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001283 if (rio_clr_err_stopped(rdev, portnum, err_status))
1284 rio_clr_err_stopped(rdev, portnum, 0);
1285 }
1286 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001287
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001288 if (rdev->rswitch->port_ok & (1 << portnum)) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001289 rdev->rswitch->port_ok &= ~(1 << portnum);
1290 rio_set_port_lockout(rdev, portnum, 1);
1291
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001292 if (rdev->phys_rmap == 1) {
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001293 rio_write_config_32(rdev,
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001294 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, portnum),
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001295 RIO_PORT_N_ACK_CLEAR);
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001296 } else {
1297 rio_write_config_32(rdev,
1298 RIO_DEV_PORT_N_OB_ACK_CSR(rdev, portnum),
1299 RIO_PORT_N_OB_ACK_CLEAR);
1300 rio_write_config_32(rdev,
1301 RIO_DEV_PORT_N_IB_ACK_CSR(rdev, portnum),
1302 0);
1303 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001304
1305 /* Schedule Extraction Service */
1306 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
1307 rio_name(rdev), portnum);
1308 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001309 }
1310
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001311 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001312 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
1313 if (em_perrdet) {
1314 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
1315 portnum, em_perrdet);
1316 /* Clear EM Port N Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001317 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001318 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
1319 }
1320
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001321 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001322 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1323 if (em_ltlerrdet) {
1324 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1325 em_ltlerrdet);
1326 /* Clear EM L/T Layer Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001327 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001328 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1329 }
1330
Alexandre Bounine388c45c2010-10-27 15:34:35 -07001331 /* Clear remaining error bits and Port-Write Pending bit */
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001332 rio_write_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1333 err_status);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001334
1335 return 0;
1336}
1337EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1338
1339/**
1340 * rio_mport_get_efb - get pointer to next extended features block
1341 * @port: Master port to issue transaction
1342 * @local: Indicate a local master port or remote device access
1343 * @destid: Destination ID of the device
1344 * @hopcount: Number of switch hops to the device
1345 * @from: Offset of current Extended Feature block header (if 0 starts
1346 * from ExtFeaturePtr)
1347 */
1348u32
1349rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1350 u8 hopcount, u32 from)
1351{
1352 u32 reg_val;
1353
1354 if (from == 0) {
1355 if (local)
1356 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1357 &reg_val);
1358 else
1359 rio_mport_read_config_32(port, destid, hopcount,
1360 RIO_ASM_INFO_CAR, &reg_val);
1361 return reg_val & RIO_EXT_FTR_PTR_MASK;
1362 } else {
1363 if (local)
1364 rio_local_read_config_32(port, from, &reg_val);
1365 else
1366 rio_mport_read_config_32(port, destid, hopcount,
1367 from, &reg_val);
1368 return RIO_GET_BLOCK_ID(reg_val);
1369 }
1370}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001371EXPORT_SYMBOL_GPL(rio_mport_get_efb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001372
1373/**
Matt Porter394b7012005-11-07 01:00:15 -08001374 * rio_mport_get_feature - query for devices' extended features
1375 * @port: Master port to issue transaction
1376 * @local: Indicate a local master port or remote device access
1377 * @destid: Destination ID of the device
1378 * @hopcount: Number of switch hops to the device
1379 * @ftr: Extended feature code
1380 *
1381 * Tell if a device supports a given RapidIO capability.
1382 * Returns the offset of the requested extended feature
1383 * block within the device's RIO configuration space or
Alexandre Bounine1ae842d2016-08-02 14:06:57 -07001384 * 0 in case the device does not support it.
Matt Porter394b7012005-11-07 01:00:15 -08001385 */
1386u32
1387rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1388 u8 hopcount, int ftr)
1389{
1390 u32 asm_info, ext_ftr_ptr, ftr_header;
1391
1392 if (local)
1393 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1394 else
1395 rio_mport_read_config_32(port, destid, hopcount,
1396 RIO_ASM_INFO_CAR, &asm_info);
1397
1398 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1399
1400 while (ext_ftr_ptr) {
1401 if (local)
1402 rio_local_read_config_32(port, ext_ftr_ptr,
1403 &ftr_header);
1404 else
1405 rio_mport_read_config_32(port, destid, hopcount,
1406 ext_ftr_ptr, &ftr_header);
1407 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1408 return ext_ftr_ptr;
Markus Elfringe1d66d02018-02-06 15:39:48 -08001409
1410 ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header);
1411 if (!ext_ftr_ptr)
Matt Porter394b7012005-11-07 01:00:15 -08001412 break;
1413 }
1414
1415 return 0;
1416}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001417EXPORT_SYMBOL_GPL(rio_mport_get_feature);
Matt Porter394b7012005-11-07 01:00:15 -08001418
1419/**
1420 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
1421 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1422 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1423 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
1424 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
1425 * @from: Previous RIO device found in search, or %NULL for new search
1426 *
1427 * Iterates through the list of known RIO devices. If a RIO device is
1428 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
1429 * count to the device is incrememted and a pointer to its device
1430 * structure is returned. Otherwise, %NULL is returned. A new search
1431 * is initiated by passing %NULL to the @from argument. Otherwise, if
1432 * @from is not %NULL, searches continue from next device on the global
1433 * list. The reference count for @from is always decremented if it is
1434 * not %NULL.
1435 */
1436struct rio_dev *rio_get_asm(u16 vid, u16 did,
1437 u16 asm_vid, u16 asm_did, struct rio_dev *from)
1438{
1439 struct list_head *n;
1440 struct rio_dev *rdev;
1441
1442 WARN_ON(in_interrupt());
1443 spin_lock(&rio_global_list_lock);
1444 n = from ? from->global_list.next : rio_devices.next;
1445
1446 while (n && (n != &rio_devices)) {
1447 rdev = rio_dev_g(n);
1448 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1449 (did == RIO_ANY_ID || rdev->did == did) &&
1450 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1451 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1452 goto exit;
1453 n = n->next;
1454 }
1455 rdev = NULL;
1456 exit:
1457 rio_dev_put(from);
1458 rdev = rio_dev_get(rdev);
1459 spin_unlock(&rio_global_list_lock);
1460 return rdev;
1461}
1462
1463/**
1464 * rio_get_device - Begin or continue searching for a RIO device by vid/did
1465 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1466 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1467 * @from: Previous RIO device found in search, or %NULL for new search
1468 *
1469 * Iterates through the list of known RIO devices. If a RIO device is
1470 * found with a matching @vid and @did, the reference count to the
1471 * device is incrememted and a pointer to its device structure is returned.
1472 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
1473 * to the @from argument. Otherwise, if @from is not %NULL, searches
1474 * continue from next device on the global list. The reference count for
1475 * @from is always decremented if it is not %NULL.
1476 */
1477struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1478{
1479 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1480}
1481
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001482/**
1483 * rio_std_route_add_entry - Add switch route table entry using standard
1484 * registers defined in RIO specification rev.1.3
1485 * @mport: Master port to issue transaction
1486 * @destid: Destination ID of the device
1487 * @hopcount: Number of switch hops to the device
1488 * @table: routing table ID (global or port-specific)
1489 * @route_destid: destID entry in the RT
1490 * @route_port: destination port for specified destID
1491 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001492static int
1493rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1494 u16 table, u16 route_destid, u8 route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001495{
1496 if (table == RIO_GLOBAL_TABLE) {
1497 rio_mport_write_config_32(mport, destid, hopcount,
1498 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1499 (u32)route_destid);
1500 rio_mport_write_config_32(mport, destid, hopcount,
1501 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1502 (u32)route_port);
1503 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001504
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001505 udelay(10);
1506 return 0;
1507}
1508
1509/**
1510 * rio_std_route_get_entry - Read switch route table entry (port number)
Uwe Kleine-König638c5942010-06-11 12:16:58 +02001511 * associated with specified destID using standard registers defined in RIO
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001512 * specification rev.1.3
1513 * @mport: Master port to issue transaction
1514 * @destid: Destination ID of the device
1515 * @hopcount: Number of switch hops to the device
1516 * @table: routing table ID (global or port-specific)
1517 * @route_destid: destID entry in the RT
1518 * @route_port: returned destination port for specified destID
1519 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001520static int
1521rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1522 u16 table, u16 route_destid, u8 *route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001523{
1524 u32 result;
1525
1526 if (table == RIO_GLOBAL_TABLE) {
1527 rio_mport_write_config_32(mport, destid, hopcount,
1528 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1529 rio_mport_read_config_32(mport, destid, hopcount,
1530 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1531
1532 *route_port = (u8)result;
1533 }
1534
1535 return 0;
1536}
1537
1538/**
1539 * rio_std_route_clr_table - Clear swotch route table using standard registers
1540 * defined in RIO specification rev.1.3.
1541 * @mport: Master port to issue transaction
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001542 * @destid: Destination ID of the device
1543 * @hopcount: Number of switch hops to the device
1544 * @table: routing table ID (global or port-specific)
1545 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001546static int
1547rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1548 u16 table)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001549{
1550 u32 max_destid = 0xff;
1551 u32 i, pef, id_inc = 1, ext_cfg = 0;
1552 u32 port_sel = RIO_INVALID_ROUTE;
1553
1554 if (table == RIO_GLOBAL_TABLE) {
1555 rio_mport_read_config_32(mport, destid, hopcount,
1556 RIO_PEF_CAR, &pef);
1557
1558 if (mport->sys_size) {
1559 rio_mport_read_config_32(mport, destid, hopcount,
1560 RIO_SWITCH_RT_LIMIT,
1561 &max_destid);
1562 max_destid &= RIO_RT_MAX_DESTID;
1563 }
1564
1565 if (pef & RIO_PEF_EXT_RT) {
1566 ext_cfg = 0x80000000;
1567 id_inc = 4;
1568 port_sel = (RIO_INVALID_ROUTE << 24) |
1569 (RIO_INVALID_ROUTE << 16) |
1570 (RIO_INVALID_ROUTE << 8) |
1571 RIO_INVALID_ROUTE;
1572 }
1573
1574 for (i = 0; i <= max_destid;) {
1575 rio_mport_write_config_32(mport, destid, hopcount,
1576 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1577 ext_cfg | i);
1578 rio_mport_write_config_32(mport, destid, hopcount,
1579 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1580 port_sel);
1581 i += id_inc;
1582 }
1583 }
1584
1585 udelay(10);
1586 return 0;
1587}
1588
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001589/**
1590 * rio_lock_device - Acquires host device lock for specified device
1591 * @port: Master port to send transaction
1592 * @destid: Destination ID for device/switch
1593 * @hopcount: Hopcount to reach switch
1594 * @wait_ms: Max wait time in msec (0 = no timeout)
1595 *
1596 * Attepts to acquire host device lock for specified device
1597 * Returns 0 if device lock acquired or EINVAL if timeout expires.
1598 */
1599int rio_lock_device(struct rio_mport *port, u16 destid,
1600 u8 hopcount, int wait_ms)
1601{
1602 u32 result;
1603 int tcnt = 0;
1604
1605 /* Attempt to acquire device lock */
1606 rio_mport_write_config_32(port, destid, hopcount,
1607 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1608 rio_mport_read_config_32(port, destid, hopcount,
1609 RIO_HOST_DID_LOCK_CSR, &result);
1610
1611 while (result != port->host_deviceid) {
1612 if (wait_ms != 0 && tcnt == wait_ms) {
1613 pr_debug("RIO: timeout when locking device %x:%x\n",
1614 destid, hopcount);
1615 return -EINVAL;
1616 }
1617
1618 /* Delay a bit */
1619 mdelay(1);
1620 tcnt++;
1621 /* Try to acquire device lock again */
1622 rio_mport_write_config_32(port, destid,
1623 hopcount,
1624 RIO_HOST_DID_LOCK_CSR,
1625 port->host_deviceid);
1626 rio_mport_read_config_32(port, destid,
1627 hopcount,
1628 RIO_HOST_DID_LOCK_CSR, &result);
1629 }
1630
1631 return 0;
1632}
1633EXPORT_SYMBOL_GPL(rio_lock_device);
1634
1635/**
1636 * rio_unlock_device - Releases host device lock for specified device
1637 * @port: Master port to send transaction
1638 * @destid: Destination ID for device/switch
1639 * @hopcount: Hopcount to reach switch
1640 *
1641 * Returns 0 if device lock released or EINVAL if fails.
1642 */
1643int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1644{
1645 u32 result;
1646
1647 /* Release device lock */
1648 rio_mport_write_config_32(port, destid,
1649 hopcount,
1650 RIO_HOST_DID_LOCK_CSR,
1651 port->host_deviceid);
1652 rio_mport_read_config_32(port, destid, hopcount,
1653 RIO_HOST_DID_LOCK_CSR, &result);
1654 if ((result & 0xffff) != 0xffff) {
1655 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1656 destid, hopcount);
1657 return -EINVAL;
1658 }
1659
1660 return 0;
1661}
1662EXPORT_SYMBOL_GPL(rio_unlock_device);
1663
1664/**
1665 * rio_route_add_entry- Add a route entry to a switch routing table
1666 * @rdev: RIO device
1667 * @table: Routing table ID
1668 * @route_destid: Destination ID to be routed
1669 * @route_port: Port number to be routed
1670 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1671 *
1672 * If available calls the switch specific add_entry() method to add a route
1673 * entry into a switch routing table. Otherwise uses standard RT update method
1674 * as defined by RapidIO specification. A specific routing table can be selected
1675 * using the @table argument if a switch has per port routing tables or
1676 * the standard (or global) table may be used by passing
1677 * %RIO_GLOBAL_TABLE in @table.
1678 *
1679 * Returns %0 on success or %-EINVAL on failure.
1680 */
1681int rio_route_add_entry(struct rio_dev *rdev,
1682 u16 table, u16 route_destid, u8 route_port, int lock)
1683{
1684 int rc = -EINVAL;
1685 struct rio_switch_ops *ops = rdev->rswitch->ops;
1686
1687 if (lock) {
1688 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1689 rdev->hopcount, 1000);
1690 if (rc)
1691 return rc;
1692 }
1693
1694 spin_lock(&rdev->rswitch->lock);
1695
Markus Elfring93dd49a2018-02-06 15:39:44 -08001696 if (!ops || !ops->add_entry) {
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001697 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1698 rdev->hopcount, table,
1699 route_destid, route_port);
1700 } else if (try_module_get(ops->owner)) {
1701 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1702 rdev->hopcount, table, route_destid,
1703 route_port);
1704 module_put(ops->owner);
1705 }
1706
1707 spin_unlock(&rdev->rswitch->lock);
1708
1709 if (lock)
1710 rio_unlock_device(rdev->net->hport, rdev->destid,
1711 rdev->hopcount);
1712
1713 return rc;
1714}
1715EXPORT_SYMBOL_GPL(rio_route_add_entry);
1716
1717/**
1718 * rio_route_get_entry- Read an entry from a switch routing table
1719 * @rdev: RIO device
1720 * @table: Routing table ID
1721 * @route_destid: Destination ID to be routed
1722 * @route_port: Pointer to read port number into
1723 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1724 *
1725 * If available calls the switch specific get_entry() method to fetch a route
1726 * entry from a switch routing table. Otherwise uses standard RT read method
1727 * as defined by RapidIO specification. A specific routing table can be selected
1728 * using the @table argument if a switch has per port routing tables or
1729 * the standard (or global) table may be used by passing
1730 * %RIO_GLOBAL_TABLE in @table.
1731 *
1732 * Returns %0 on success or %-EINVAL on failure.
1733 */
1734int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1735 u16 route_destid, u8 *route_port, int lock)
1736{
1737 int rc = -EINVAL;
1738 struct rio_switch_ops *ops = rdev->rswitch->ops;
1739
1740 if (lock) {
1741 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1742 rdev->hopcount, 1000);
1743 if (rc)
1744 return rc;
1745 }
1746
1747 spin_lock(&rdev->rswitch->lock);
1748
Markus Elfring93dd49a2018-02-06 15:39:44 -08001749 if (!ops || !ops->get_entry) {
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001750 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1751 rdev->hopcount, table,
1752 route_destid, route_port);
1753 } else if (try_module_get(ops->owner)) {
1754 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1755 rdev->hopcount, table, route_destid,
1756 route_port);
1757 module_put(ops->owner);
1758 }
1759
1760 spin_unlock(&rdev->rswitch->lock);
1761
1762 if (lock)
1763 rio_unlock_device(rdev->net->hport, rdev->destid,
1764 rdev->hopcount);
1765 return rc;
1766}
1767EXPORT_SYMBOL_GPL(rio_route_get_entry);
1768
1769/**
1770 * rio_route_clr_table - Clear a switch routing table
1771 * @rdev: RIO device
1772 * @table: Routing table ID
1773 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1774 *
1775 * If available calls the switch specific clr_table() method to clear a switch
1776 * routing table. Otherwise uses standard RT write method as defined by RapidIO
1777 * specification. A specific routing table can be selected using the @table
1778 * argument if a switch has per port routing tables or the standard (or global)
1779 * table may be used by passing %RIO_GLOBAL_TABLE in @table.
1780 *
1781 * Returns %0 on success or %-EINVAL on failure.
1782 */
1783int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1784{
1785 int rc = -EINVAL;
1786 struct rio_switch_ops *ops = rdev->rswitch->ops;
1787
1788 if (lock) {
1789 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1790 rdev->hopcount, 1000);
1791 if (rc)
1792 return rc;
1793 }
1794
1795 spin_lock(&rdev->rswitch->lock);
1796
Markus Elfring93dd49a2018-02-06 15:39:44 -08001797 if (!ops || !ops->clr_table) {
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001798 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1799 rdev->hopcount, table);
1800 } else if (try_module_get(ops->owner)) {
1801 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1802 rdev->hopcount, table);
1803
1804 module_put(ops->owner);
1805 }
1806
1807 spin_unlock(&rdev->rswitch->lock);
1808
1809 if (lock)
1810 rio_unlock_device(rdev->net->hport, rdev->destid,
1811 rdev->hopcount);
1812
1813 return rc;
1814}
1815EXPORT_SYMBOL_GPL(rio_route_clr_table);
1816
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001817#ifdef CONFIG_RAPIDIO_DMA_ENGINE
1818
1819static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1820{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001821 struct rio_mport *mport = arg;
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001822
1823 /* Check that DMA device belongs to the right MPORT */
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001824 return mport == container_of(chan->device, struct rio_mport, dma);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001825}
1826
1827/**
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001828 * rio_request_mport_dma - request RapidIO capable DMA channel associated
1829 * with specified local RapidIO mport device.
1830 * @mport: RIO mport to perform DMA data transfers
1831 *
1832 * Returns pointer to allocated DMA channel or NULL if failed.
1833 */
1834struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
1835{
1836 dma_cap_mask_t mask;
1837
1838 dma_cap_zero(mask);
1839 dma_cap_set(DMA_SLAVE, mask);
1840 return dma_request_channel(mask, rio_chan_filter, mport);
1841}
1842EXPORT_SYMBOL_GPL(rio_request_mport_dma);
1843
1844/**
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001845 * rio_request_dma - request RapidIO capable DMA channel that supports
1846 * specified target RapidIO device.
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001847 * @rdev: RIO device associated with DMA transfer
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001848 *
1849 * Returns pointer to allocated DMA channel or NULL if failed.
1850 */
1851struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1852{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001853 return rio_request_mport_dma(rdev->net->hport);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001854}
1855EXPORT_SYMBOL_GPL(rio_request_dma);
1856
1857/**
1858 * rio_release_dma - release specified DMA channel
1859 * @dchan: DMA channel to release
1860 */
1861void rio_release_dma(struct dma_chan *dchan)
1862{
1863 dma_release_channel(dchan);
1864}
1865EXPORT_SYMBOL_GPL(rio_release_dma);
1866
1867/**
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001868 * rio_dma_prep_xfer - RapidIO specific wrapper
1869 * for device_prep_slave_sg callback defined by DMAENGINE.
1870 * @dchan: DMA channel to configure
1871 * @destid: target RapidIO device destination ID
1872 * @data: RIO specific data descriptor
1873 * @direction: DMA data transfer direction (TO or FROM the device)
1874 * @flags: dmaengine defined flags
1875 *
1876 * Initializes RapidIO capable DMA channel for the specified data transfer.
1877 * Uses DMA channel private extension to pass information related to remote
1878 * target RIO device.
Alexandre Bouninef8e3a682016-08-02 14:06:34 -07001879 *
1880 * Returns: pointer to DMA transaction descriptor if successful,
1881 * error-valued pointer or NULL if failed.
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001882 */
1883struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
1884 u16 destid, struct rio_dma_data *data,
1885 enum dma_transfer_direction direction, unsigned long flags)
1886{
1887 struct rio_dma_ext rio_ext;
1888
Markus Elfring93dd49a2018-02-06 15:39:44 -08001889 if (!dchan->device->device_prep_slave_sg) {
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001890 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1891 return NULL;
1892 }
1893
1894 rio_ext.destid = destid;
1895 rio_ext.rio_addr_u = data->rio_addr_u;
1896 rio_ext.rio_addr = data->rio_addr;
1897 rio_ext.wr_type = data->wr_type;
1898
1899 return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1900 direction, flags, &rio_ext);
1901}
1902EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
1903
1904/**
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001905 * rio_dma_prep_slave_sg - RapidIO specific wrapper
1906 * for device_prep_slave_sg callback defined by DMAENGINE.
1907 * @rdev: RIO device control structure
1908 * @dchan: DMA channel to configure
1909 * @data: RIO specific data descriptor
1910 * @direction: DMA data transfer direction (TO or FROM the device)
1911 * @flags: dmaengine defined flags
1912 *
1913 * Initializes RapidIO capable DMA channel for the specified data transfer.
1914 * Uses DMA channel private extension to pass information related to remote
1915 * target RIO device.
Alexandre Bouninef8e3a682016-08-02 14:06:34 -07001916 *
1917 * Returns: pointer to DMA transaction descriptor if successful,
1918 * error-valued pointer or NULL if failed.
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001919 */
1920struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1921 struct dma_chan *dchan, struct rio_dma_data *data,
1922 enum dma_transfer_direction direction, unsigned long flags)
1923{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001924 return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001925}
1926EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1927
1928#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1929
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001930/**
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07001931 * rio_find_mport - find RIO mport by its ID
1932 * @mport_id: number (ID) of mport device
1933 *
1934 * Given a RIO mport number, the desired mport is located
1935 * in the global list of mports. If the mport is found, a pointer to its
1936 * data structure is returned. If no mport is found, %NULL is returned.
1937 */
1938struct rio_mport *rio_find_mport(int mport_id)
1939{
1940 struct rio_mport *port;
1941
1942 mutex_lock(&rio_mport_list_lock);
1943 list_for_each_entry(port, &rio_mports, node) {
1944 if (port->id == mport_id)
1945 goto found;
1946 }
1947 port = NULL;
1948found:
1949 mutex_unlock(&rio_mport_list_lock);
1950
1951 return port;
1952}
1953
1954/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001955 * rio_register_scan - enumeration/discovery method registration interface
1956 * @mport_id: mport device ID for which fabric scan routine has to be set
1957 * (RIO_MPORT_ANY = set for all available mports)
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001958 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001959 *
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001960 * Registers enumeration/discovery operations with RapidIO subsystem and
1961 * attaches it to the specified mport device (or all available mports
1962 * if RIO_MPORT_ANY is specified).
1963 *
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001964 * Returns error if the mport already has an enumerator attached to it.
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001965 * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001966 */
1967int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1968{
1969 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001970 struct rio_scan_node *scan;
1971 int rc = 0;
1972
1973 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1974
1975 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1976 !scan_ops)
1977 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001978
1979 mutex_lock(&rio_mport_list_lock);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001980
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001981 /*
1982 * Check if there is another enumerator already registered for
1983 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
1984 * for the same mport ID are not supported.
1985 */
1986 list_for_each_entry(scan, &rio_scans, node) {
1987 if (scan->mport_id == mport_id) {
1988 rc = -EBUSY;
1989 goto err_out;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001990 }
1991 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001992
1993 /*
1994 * Allocate and initialize new scan registration node.
1995 */
1996 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
1997 if (!scan) {
1998 rc = -ENOMEM;
1999 goto err_out;
2000 }
2001
2002 scan->mport_id = mport_id;
2003 scan->ops = scan_ops;
2004
2005 /*
2006 * Traverse the list of registered mports to attach this new scan.
2007 *
2008 * The new scan with matching mport ID overrides any previously attached
2009 * scan assuming that old scan (if any) is the default one (based on the
2010 * enumerator registration check above).
2011 * If the new scan is the global one, it will be attached only to mports
2012 * that do not have their own individual operations already attached.
2013 */
2014 list_for_each_entry(port, &rio_mports, node) {
2015 if (port->id == mport_id) {
2016 port->nscan = scan_ops;
2017 break;
2018 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
2019 port->nscan = scan_ops;
2020 }
2021
2022 list_add_tail(&scan->node, &rio_scans);
2023
2024err_out:
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002025 mutex_unlock(&rio_mport_list_lock);
2026
2027 return rc;
2028}
2029EXPORT_SYMBOL_GPL(rio_register_scan);
2030
2031/**
2032 * rio_unregister_scan - removes enumeration/discovery method from mport
2033 * @mport_id: mport device ID for which fabric scan routine has to be
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002034 * unregistered (RIO_MPORT_ANY = apply to all mports that use
2035 * the specified scan_ops)
2036 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002037 *
2038 * Removes enumeration or discovery method assigned to the specified mport
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002039 * device. If RIO_MPORT_ANY is specified, removes the specified operations from
2040 * all mports that have them attached.
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002041 */
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002042int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002043{
2044 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002045 struct rio_scan_node *scan;
2046
2047 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
2048
2049 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
2050 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002051
2052 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002053
2054 list_for_each_entry(port, &rio_mports, node)
2055 if (port->id == mport_id ||
2056 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
2057 port->nscan = NULL;
2058
Dan Carpenterf93f3c42013-07-31 13:53:34 -07002059 list_for_each_entry(scan, &rio_scans, node) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002060 if (scan->mport_id == mport_id) {
2061 list_del(&scan->node);
2062 kfree(scan);
Dan Carpenterf93f3c42013-07-31 13:53:34 -07002063 break;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002064 }
Dan Carpenterf93f3c42013-07-31 13:53:34 -07002065 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002066
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002067 mutex_unlock(&rio_mport_list_lock);
2068
2069 return 0;
2070}
2071EXPORT_SYMBOL_GPL(rio_unregister_scan);
2072
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002073/**
2074 * rio_mport_scan - execute enumeration/discovery on the specified mport
2075 * @mport_id: number (ID) of mport device
2076 */
2077int rio_mport_scan(int mport_id)
2078{
2079 struct rio_mport *port = NULL;
2080 int rc;
2081
2082 mutex_lock(&rio_mport_list_lock);
2083 list_for_each_entry(port, &rio_mports, node) {
2084 if (port->id == mport_id)
2085 goto found;
2086 }
2087 mutex_unlock(&rio_mport_list_lock);
2088 return -ENODEV;
2089found:
2090 if (!port->nscan) {
2091 mutex_unlock(&rio_mport_list_lock);
2092 return -EINVAL;
2093 }
2094
2095 if (!try_module_get(port->nscan->owner)) {
2096 mutex_unlock(&rio_mport_list_lock);
2097 return -ENODEV;
2098 }
2099
2100 mutex_unlock(&rio_mport_list_lock);
2101
2102 if (port->host_deviceid >= 0)
2103 rc = port->nscan->enumerate(port, 0);
2104 else
2105 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
2106
2107 module_put(port->nscan->owner);
2108 return rc;
2109}
2110
Matt Porter394b7012005-11-07 01:00:15 -08002111static void rio_fixup_device(struct rio_dev *dev)
2112{
2113}
2114
Bill Pemberton305c891e2012-11-19 13:23:25 -05002115static int rio_init(void)
Matt Porter394b7012005-11-07 01:00:15 -08002116{
2117 struct rio_dev *dev = NULL;
2118
2119 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
2120 rio_fixup_device(dev);
2121 }
2122 return 0;
2123}
2124
Alexandre Bounine005842e2012-10-04 17:16:08 -07002125static struct workqueue_struct *rio_wq;
2126
2127struct rio_disc_work {
2128 struct work_struct work;
2129 struct rio_mport *mport;
2130};
2131
Bill Pemberton305c891e2012-11-19 13:23:25 -05002132static void disc_work_handler(struct work_struct *_work)
Alexandre Bounine005842e2012-10-04 17:16:08 -07002133{
2134 struct rio_disc_work *work;
2135
2136 work = container_of(_work, struct rio_disc_work, work);
2137 pr_debug("RIO: discovery work for mport %d %s\n",
2138 work->mport->id, work->mport->name);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002139 if (try_module_get(work->mport->nscan->owner)) {
2140 work->mport->nscan->discover(work->mport, 0);
2141 module_put(work->mport->nscan->owner);
2142 }
Alexandre Bounine005842e2012-10-04 17:16:08 -07002143}
2144
Bill Pemberton305c891e2012-11-19 13:23:25 -05002145int rio_init_mports(void)
Matt Porter394b7012005-11-07 01:00:15 -08002146{
Matt Porter394b7012005-11-07 01:00:15 -08002147 struct rio_mport *port;
Alexandre Bounine005842e2012-10-04 17:16:08 -07002148 struct rio_disc_work *work;
Alexandre Bounine25747402012-10-10 15:53:59 -07002149 int n = 0;
Matt Porter394b7012005-11-07 01:00:15 -08002150
Alexandre Bounine25747402012-10-10 15:53:59 -07002151 if (!next_portid)
2152 return -ENODEV;
2153
2154 /*
2155 * First, run enumerations and check if we need to perform discovery
2156 * on any of the registered mports.
2157 */
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002158 mutex_lock(&rio_mport_list_lock);
Matt Porter394b7012005-11-07 01:00:15 -08002159 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002160 if (port->host_deviceid >= 0) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002161 if (port->nscan && try_module_get(port->nscan->owner)) {
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07002162 port->nscan->enumerate(port, 0);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002163 module_put(port->nscan->owner);
2164 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002165 } else
Alexandre Bounine25747402012-10-10 15:53:59 -07002166 n++;
2167 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002168 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine005842e2012-10-04 17:16:08 -07002169
Alexandre Bounine25747402012-10-10 15:53:59 -07002170 if (!n)
2171 goto no_disc;
Alexandre Bounine005842e2012-10-04 17:16:08 -07002172
Alexandre Bounine25747402012-10-10 15:53:59 -07002173 /*
2174 * If we have mports that require discovery schedule a discovery work
2175 * for each of them. If the code below fails to allocate needed
2176 * resources, exit without error to keep results of enumeration
2177 * process (if any).
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002178 * TODO: Implement restart of discovery process for all or
Alexandre Bounine25747402012-10-10 15:53:59 -07002179 * individual discovering mports.
2180 */
2181 rio_wq = alloc_workqueue("riodisc", 0, 0);
2182 if (!rio_wq) {
2183 pr_err("RIO: unable allocate rio_wq\n");
2184 goto no_disc;
2185 }
2186
2187 work = kcalloc(n, sizeof *work, GFP_KERNEL);
2188 if (!work) {
Alexandre Bounine25747402012-10-10 15:53:59 -07002189 destroy_workqueue(rio_wq);
2190 goto no_disc;
2191 }
2192
2193 n = 0;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002194 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07002195 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002196 if (port->host_deviceid < 0 && port->nscan) {
Alexandre Bounine25747402012-10-10 15:53:59 -07002197 work[n].mport = port;
2198 INIT_WORK(&work[n].work, disc_work_handler);
2199 queue_work(rio_wq, &work[n].work);
2200 n++;
Alexandre Bounine005842e2012-10-04 17:16:08 -07002201 }
2202 }
2203
Alexandre Bounine25747402012-10-10 15:53:59 -07002204 flush_workqueue(rio_wq);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002205 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07002206 pr_debug("RIO: destroy discovery workqueue\n");
2207 destroy_workqueue(rio_wq);
2208 kfree(work);
Matt Porter394b7012005-11-07 01:00:15 -08002209
Alexandre Bounine25747402012-10-10 15:53:59 -07002210no_disc:
Alexandre Bounine2f809982011-03-23 16:43:04 -07002211 rio_init();
2212
Alexandre Bouninec1256eb2011-03-23 16:43:06 -07002213 return 0;
Matt Porter394b7012005-11-07 01:00:15 -08002214}
2215
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002216static int rio_get_hdid(int index)
2217{
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07002218 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002219 return -1;
2220
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07002221 return hdid[index];
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002222}
2223
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002224int rio_mport_initialize(struct rio_mport *mport)
2225{
2226 if (next_portid >= RIO_MAX_MPORTS) {
2227 pr_err("RIO: reached specified max number of mports\n");
2228 return -ENODEV;
2229 }
2230
2231 atomic_set(&mport->state, RIO_DEVICE_INITIALIZING);
2232 mport->id = next_portid++;
2233 mport->host_deviceid = rio_get_hdid(mport->id);
2234 mport->nscan = NULL;
Alexandre Bouninea7b4c632016-03-22 14:26:35 -07002235 mutex_init(&mport->lock);
Alexandre Bounineb6cb95e2016-03-22 14:26:41 -07002236 mport->pwe_refcnt = 0;
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07002237 INIT_LIST_HEAD(&mport->pwrites);
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002238
2239 return 0;
2240}
2241EXPORT_SYMBOL_GPL(rio_mport_initialize);
2242
Alexandre Bounine59f99962011-04-14 15:22:14 -07002243int rio_register_mport(struct rio_mport *port)
Matt Porter394b7012005-11-07 01:00:15 -08002244{
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002245 struct rio_scan_node *scan = NULL;
Alexandre Bounine2aaf3082014-04-07 15:38:56 -07002246 int res = 0;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002247
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002248 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002249
2250 /*
2251 * Check if there are any registered enumeration/discovery operations
2252 * that have to be attached to the added mport.
2253 */
2254 list_for_each_entry(scan, &rio_scans, node) {
2255 if (port->id == scan->mport_id ||
2256 scan->mport_id == RIO_MPORT_ANY) {
2257 port->nscan = scan->ops;
2258 if (port->id == scan->mport_id)
2259 break;
2260 }
2261 }
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002262
2263 list_add_tail(&port->node, &rio_mports);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002264 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002265
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002266 dev_set_name(&port->dev, "rapidio%d", port->id);
2267 port->dev.class = &rio_mport_class;
2268 atomic_set(&port->state, RIO_DEVICE_RUNNING);
2269
2270 res = device_register(&port->dev);
2271 if (res)
2272 dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
2273 port->id, res);
2274 else
2275 dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id);
2276
2277 return res;
Matt Porter394b7012005-11-07 01:00:15 -08002278}
Alexandre Bounine94d9bd42013-07-03 15:08:55 -07002279EXPORT_SYMBOL_GPL(rio_register_mport);
Matt Porter394b7012005-11-07 01:00:15 -08002280
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002281static int rio_mport_cleanup_callback(struct device *dev, void *data)
2282{
2283 struct rio_dev *rdev = to_rio_dev(dev);
2284
2285 if (dev->bus == &rio_bus_type)
2286 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
2287 return 0;
2288}
2289
2290static int rio_net_remove_children(struct rio_net *net)
2291{
2292 /*
2293 * Unregister all RapidIO devices residing on this net (this will
2294 * invoke notification of registered subsystem interfaces as well).
2295 */
2296 device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback);
2297 return 0;
2298}
2299
2300int rio_unregister_mport(struct rio_mport *port)
2301{
2302 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
2303
2304 /* Transition mport to the SHUTDOWN state */
2305 if (atomic_cmpxchg(&port->state,
2306 RIO_DEVICE_RUNNING,
2307 RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) {
2308 pr_err("RIO: %s unexpected state transition for mport %s\n",
2309 __func__, port->name);
2310 }
2311
2312 if (port->net && port->net->hport == port) {
2313 rio_net_remove_children(port->net);
2314 rio_free_net(port->net);
2315 }
2316
2317 /*
2318 * Unregister all RapidIO devices attached to this mport (this will
2319 * invoke notification of registered subsystem interfaces as well).
2320 */
2321 mutex_lock(&rio_mport_list_lock);
2322 list_del(&port->node);
2323 mutex_unlock(&rio_mport_list_lock);
2324 device_unregister(&port->dev);
2325
2326 return 0;
2327}
2328EXPORT_SYMBOL_GPL(rio_unregister_mport);
2329
Matt Porter394b7012005-11-07 01:00:15 -08002330EXPORT_SYMBOL_GPL(rio_local_get_device_id);
2331EXPORT_SYMBOL_GPL(rio_get_device);
2332EXPORT_SYMBOL_GPL(rio_get_asm);
2333EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
2334EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
2335EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
2336EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
2337EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
2338EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
2339EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
2340EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002341EXPORT_SYMBOL_GPL(rio_init_mports);