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