blob: 673774bf6dd676b2d7a37b2b6dddf480e5eba2d3 [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 Bouninefdf90ab2013-07-03 15:08:56 -070033MODULE_DESCRIPTION("RapidIO Subsystem Core");
34MODULE_AUTHOR("Matt Porter <mporter@kernel.crashing.org>");
35MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
36MODULE_LICENSE("GPL");
37
38static int hdid[RIO_MAX_MPORTS];
39static int ids_num;
40module_param_array(hdid, int, &ids_num, 0);
41MODULE_PARM_DESC(hdid,
42 "Destination ID assignment to local RapidIO controllers");
43
Alexandre Bouninea11650e2013-05-24 15:55:05 -070044static LIST_HEAD(rio_devices);
Alexandre Bouninee6b585c2016-03-22 14:26:17 -070045static LIST_HEAD(rio_nets);
Alexandre Bouninea11650e2013-05-24 15:55:05 -070046static DEFINE_SPINLOCK(rio_global_list_lock);
47
Matt Porter394b7012005-11-07 01:00:15 -080048static LIST_HEAD(rio_mports);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -070049static LIST_HEAD(rio_scans);
Alexandre Bouninea11650e2013-05-24 15:55:05 -070050static DEFINE_MUTEX(rio_mport_list_lock);
Alexandre Bounine569fccb2011-03-23 16:43:05 -070051static unsigned char next_portid;
Alexandre Bounineda1589f2012-10-04 17:15:57 -070052static DEFINE_SPINLOCK(rio_mmap_lock);
Matt Porter394b7012005-11-07 01:00:15 -080053
54/**
55 * rio_local_get_device_id - Get the base/extended device id for a port
56 * @port: RIO master port from which to get the deviceid
57 *
58 * Reads the base/extended device id from the local device
59 * implementing the master port. Returns the 8/16-bit device
60 * id.
61 */
62u16 rio_local_get_device_id(struct rio_mport *port)
63{
64 u32 result;
65
66 rio_local_read_config_32(port, RIO_DID_CSR, &result);
67
Zhang Weie0423232008-04-18 13:33:42 -070068 return (RIO_GET_DID(port->sys_size, result));
Matt Porter394b7012005-11-07 01:00:15 -080069}
70
71/**
Alexandre Bounine8b189fd2016-03-22 14:26:00 -070072 * rio_query_mport - Query mport device attributes
73 * @port: mport device to query
74 * @mport_attr: mport attributes data structure
75 *
76 * Returns attributes of specified mport through the
77 * pointer to attributes data structure.
78 */
79int rio_query_mport(struct rio_mport *port,
80 struct rio_mport_attr *mport_attr)
81{
82 if (!port->ops->query_mport)
83 return -ENODATA;
84 return port->ops->query_mport(port, mport_attr);
85}
86EXPORT_SYMBOL(rio_query_mport);
87
88/**
Alexandre Bouninee6b585c2016-03-22 14:26:17 -070089 * rio_alloc_net- Allocate and initialize a new RIO network data structure
90 * @mport: Master port associated with the RIO network
91 *
92 * Allocates a RIO network structure, initializes per-network
93 * list heads, and adds the associated master port to the
94 * network list of associated master ports. Returns a
95 * RIO network pointer on success or %NULL on failure.
96 */
97struct rio_net *rio_alloc_net(struct rio_mport *mport)
98{
99 struct rio_net *net;
100
101 net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
102 if (net) {
103 INIT_LIST_HEAD(&net->node);
104 INIT_LIST_HEAD(&net->devices);
105 INIT_LIST_HEAD(&net->switches);
106 INIT_LIST_HEAD(&net->mports);
107 mport->net = net;
108 }
109 return net;
110}
111EXPORT_SYMBOL_GPL(rio_alloc_net);
112
113int rio_add_net(struct rio_net *net)
114{
115 int err;
116
117 err = device_register(&net->dev);
118 if (err)
119 return err;
120 spin_lock(&rio_global_list_lock);
121 list_add_tail(&net->node, &rio_nets);
122 spin_unlock(&rio_global_list_lock);
123
124 return 0;
125}
126EXPORT_SYMBOL_GPL(rio_add_net);
127
128void rio_free_net(struct rio_net *net)
129{
130 spin_lock(&rio_global_list_lock);
131 if (!list_empty(&net->node))
132 list_del(&net->node);
133 spin_unlock(&rio_global_list_lock);
134 if (net->release)
135 net->release(net);
136 device_unregister(&net->dev);
137}
138EXPORT_SYMBOL_GPL(rio_free_net);
139
140/**
Alexandre Bounine50246222016-03-22 14:26:38 -0700141 * rio_local_set_device_id - Set the base/extended device id for a port
142 * @port: RIO master port
143 * @did: Device ID value to be written
144 *
145 * Writes the base/extended device id from a device.
146 */
147void rio_local_set_device_id(struct rio_mport *port, u16 did)
148{
149 rio_local_write_config_32(port, RIO_DID_CSR,
150 RIO_SET_DID(port->sys_size, did));
151}
152EXPORT_SYMBOL_GPL(rio_local_set_device_id);
153
154/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700155 * rio_add_device- Adds a RIO device to the device model
156 * @rdev: RIO device
157 *
158 * Adds the RIO device to the global device list and adds the RIO
159 * device to the RIO device list. Creates the generic sysfs nodes
160 * for an RIO device.
161 */
162int rio_add_device(struct rio_dev *rdev)
163{
164 int err;
165
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700166 atomic_set(&rdev->state, RIO_DEVICE_RUNNING);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700167 err = device_register(&rdev->dev);
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700168 if (err)
169 return err;
170
171 spin_lock(&rio_global_list_lock);
172 list_add_tail(&rdev->global_list, &rio_devices);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700173 if (rdev->net) {
174 list_add_tail(&rdev->net_list, &rdev->net->devices);
175 if (rdev->pef & RIO_PEF_SWITCH)
176 list_add_tail(&rdev->rswitch->node,
177 &rdev->net->switches);
178 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700179 spin_unlock(&rio_global_list_lock);
180
181 rio_create_sysfs_dev_files(rdev);
182
183 return 0;
184}
185EXPORT_SYMBOL_GPL(rio_add_device);
186
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700187/*
188 * rio_del_device - removes a RIO device from the device model
189 * @rdev: RIO device
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700190 * @state: device state to set during removal process
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700191 *
192 * Removes the RIO device to the kernel device list and subsystem's device list.
193 * Clears sysfs entries for the removed device.
194 */
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700195void rio_del_device(struct rio_dev *rdev, enum rio_device_state state)
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700196{
197 pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev));
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700198 atomic_set(&rdev->state, state);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700199 spin_lock(&rio_global_list_lock);
200 list_del(&rdev->global_list);
201 if (rdev->net) {
202 list_del(&rdev->net_list);
203 if (rdev->pef & RIO_PEF_SWITCH) {
204 list_del(&rdev->rswitch->node);
205 kfree(rdev->rswitch->route_table);
206 }
207 }
208 spin_unlock(&rio_global_list_lock);
209 rio_remove_sysfs_dev_files(rdev);
210 device_unregister(&rdev->dev);
211}
212EXPORT_SYMBOL_GPL(rio_del_device);
213
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700214/**
Matt Porter394b7012005-11-07 01:00:15 -0800215 * rio_request_inb_mbox - request inbound mailbox service
216 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800217 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800218 * @mbox: Mailbox number to claim
219 * @entries: Number of entries in inbound mailbox queue
220 * @minb: Callback to execute when inbound message is received
221 *
222 * Requests ownership of an inbound mailbox resource and binds
223 * a callback function to the resource. Returns %0 on success.
224 */
225int rio_request_inb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800226 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800227 int mbox,
228 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800229 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
Matt Porter394b7012005-11-07 01:00:15 -0800230 int slot))
231{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700232 int rc = -ENOSYS;
233 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800234
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700235 if (mport->ops->open_inb_mbox == NULL)
236 goto out;
237
Toshi Kani9a975be2016-01-26 21:57:25 +0100238 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800239
240 if (res) {
241 rio_init_mbox_res(res, mbox, mbox);
242
243 /* Make sure this mailbox isn't in use */
244 if ((rc =
245 request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
246 res)) < 0) {
247 kfree(res);
248 goto out;
249 }
250
251 mport->inb_msg[mbox].res = res;
252
253 /* Hook the inbound message callback */
254 mport->inb_msg[mbox].mcback = minb;
255
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700256 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
Matt Porter394b7012005-11-07 01:00:15 -0800257 } else
258 rc = -ENOMEM;
259
260 out:
261 return rc;
262}
263
264/**
265 * rio_release_inb_mbox - release inbound mailbox message service
266 * @mport: RIO master port from which to release the mailbox resource
267 * @mbox: Mailbox number to release
268 *
269 * Releases ownership of an inbound mailbox resource. Returns 0
270 * if the request has been satisfied.
271 */
272int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
273{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700274 if (mport->ops->close_inb_mbox) {
275 mport->ops->close_inb_mbox(mport, mbox);
Matt Porter394b7012005-11-07 01:00:15 -0800276
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700277 /* Release the mailbox resource */
278 return release_resource(mport->inb_msg[mbox].res);
279 } else
280 return -ENOSYS;
Matt Porter394b7012005-11-07 01:00:15 -0800281}
282
283/**
284 * rio_request_outb_mbox - request outbound mailbox service
285 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800286 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800287 * @mbox: Mailbox number to claim
288 * @entries: Number of entries in outbound mailbox queue
289 * @moutb: Callback to execute when outbound message is sent
290 *
291 * Requests ownership of an outbound mailbox resource and binds
292 * a callback function to the resource. Returns 0 on success.
293 */
294int rio_request_outb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800295 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800296 int mbox,
297 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800298 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
Matt Porter394b7012005-11-07 01:00:15 -0800299{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700300 int rc = -ENOSYS;
301 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800302
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700303 if (mport->ops->open_outb_mbox == NULL)
304 goto out;
305
Toshi Kani9a975be2016-01-26 21:57:25 +0100306 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800307
308 if (res) {
309 rio_init_mbox_res(res, mbox, mbox);
310
311 /* Make sure this outbound mailbox isn't in use */
312 if ((rc =
313 request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
314 res)) < 0) {
315 kfree(res);
316 goto out;
317 }
318
319 mport->outb_msg[mbox].res = res;
320
321 /* Hook the inbound message callback */
322 mport->outb_msg[mbox].mcback = moutb;
323
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700324 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
Matt Porter394b7012005-11-07 01:00:15 -0800325 } else
326 rc = -ENOMEM;
327
328 out:
329 return rc;
330}
331
332/**
333 * rio_release_outb_mbox - release outbound mailbox message service
334 * @mport: RIO master port from which to release the mailbox resource
335 * @mbox: Mailbox number to release
336 *
337 * Releases ownership of an inbound mailbox resource. Returns 0
338 * if the request has been satisfied.
339 */
340int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
341{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700342 if (mport->ops->close_outb_mbox) {
343 mport->ops->close_outb_mbox(mport, mbox);
Matt Porter394b7012005-11-07 01:00:15 -0800344
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700345 /* Release the mailbox resource */
346 return release_resource(mport->outb_msg[mbox].res);
347 } else
348 return -ENOSYS;
Matt Porter394b7012005-11-07 01:00:15 -0800349}
350
351/**
352 * rio_setup_inb_dbell - bind inbound doorbell callback
353 * @mport: RIO master port to bind the doorbell callback
Matt Porter6978bbc2005-11-07 01:00:20 -0800354 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800355 * @res: Doorbell message resource
356 * @dinb: Callback to execute when doorbell is received
357 *
358 * Adds a doorbell resource/callback pair into a port's
359 * doorbell event list. Returns 0 if the request has been
360 * satisfied.
361 */
362static int
Matt Porter6978bbc2005-11-07 01:00:20 -0800363rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
364 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
Matt Porter394b7012005-11-07 01:00:15 -0800365 u16 info))
366{
367 int rc = 0;
368 struct rio_dbell *dbell;
369
370 if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
371 rc = -ENOMEM;
372 goto out;
373 }
374
375 dbell->res = res;
376 dbell->dinb = dinb;
Matt Porter6978bbc2005-11-07 01:00:20 -0800377 dbell->dev_id = dev_id;
Matt Porter394b7012005-11-07 01:00:15 -0800378
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700379 mutex_lock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800380 list_add_tail(&dbell->node, &mport->dbells);
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700381 mutex_unlock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800382
383 out:
384 return rc;
385}
386
387/**
388 * rio_request_inb_dbell - request inbound doorbell message service
389 * @mport: RIO master port from which to allocate the doorbell resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800390 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800391 * @start: Doorbell info range start
392 * @end: Doorbell info range end
393 * @dinb: Callback to execute when doorbell is received
394 *
395 * Requests ownership of an inbound doorbell resource and binds
396 * a callback function to the resource. Returns 0 if the request
397 * has been satisfied.
398 */
399int rio_request_inb_dbell(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800400 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800401 u16 start,
402 u16 end,
Matt Porter6978bbc2005-11-07 01:00:20 -0800403 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
Matt Porter394b7012005-11-07 01:00:15 -0800404 u16 dst, u16 info))
405{
406 int rc = 0;
407
Toshi Kani9a975be2016-01-26 21:57:25 +0100408 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800409
410 if (res) {
411 rio_init_dbell_res(res, start, end);
412
413 /* Make sure these doorbells aren't in use */
414 if ((rc =
415 request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
416 res)) < 0) {
417 kfree(res);
418 goto out;
419 }
420
421 /* Hook the doorbell callback */
Matt Porter6978bbc2005-11-07 01:00:20 -0800422 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
Matt Porter394b7012005-11-07 01:00:15 -0800423 } else
424 rc = -ENOMEM;
425
426 out:
427 return rc;
428}
429
430/**
431 * rio_release_inb_dbell - release inbound doorbell message service
432 * @mport: RIO master port from which to release the doorbell resource
433 * @start: Doorbell info range start
434 * @end: Doorbell info range end
435 *
436 * Releases ownership of an inbound doorbell resource and removes
437 * callback from the doorbell event list. Returns 0 if the request
438 * has been satisfied.
439 */
440int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
441{
442 int rc = 0, found = 0;
443 struct rio_dbell *dbell;
444
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700445 mutex_lock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800446 list_for_each_entry(dbell, &mport->dbells, node) {
447 if ((dbell->res->start == start) && (dbell->res->end == end)) {
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700448 list_del(&dbell->node);
Matt Porter394b7012005-11-07 01:00:15 -0800449 found = 1;
450 break;
451 }
452 }
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700453 mutex_unlock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800454
455 /* If we can't find an exact match, fail */
456 if (!found) {
457 rc = -EINVAL;
458 goto out;
459 }
460
Matt Porter394b7012005-11-07 01:00:15 -0800461 /* Release the doorbell resource */
462 rc = release_resource(dbell->res);
463
464 /* Free the doorbell event */
465 kfree(dbell);
466
467 out:
468 return rc;
469}
470
471/**
472 * rio_request_outb_dbell - request outbound doorbell message range
473 * @rdev: RIO device from which to allocate the doorbell resource
474 * @start: Doorbell message range start
475 * @end: Doorbell message range end
476 *
477 * Requests ownership of a doorbell message range. Returns a resource
478 * if the request has been satisfied or %NULL on failure.
479 */
480struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
481 u16 end)
482{
Toshi Kani9a975be2016-01-26 21:57:25 +0100483 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800484
485 if (res) {
486 rio_init_dbell_res(res, start, end);
487
488 /* Make sure these doorbells aren't in use */
489 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
490 < 0) {
491 kfree(res);
492 res = NULL;
493 }
494 }
495
496 return res;
497}
498
499/**
500 * rio_release_outb_dbell - release outbound doorbell message range
501 * @rdev: RIO device from which to release the doorbell resource
502 * @res: Doorbell resource to be freed
503 *
504 * Releases ownership of a doorbell message range. Returns 0 if the
505 * request has been satisfied.
506 */
507int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
508{
509 int rc = release_resource(res);
510
511 kfree(res);
512
513 return rc;
514}
515
516/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700517 * rio_request_inb_pwrite - request inbound port-write message service
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700518 * @rdev: RIO device to which register inbound port-write callback routine
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700519 * @pwcback: Callback routine to execute when port-write is received
520 *
521 * Binds a port-write callback function to the RapidIO device.
522 * Returns 0 if the request has been satisfied.
523 */
524int rio_request_inb_pwrite(struct rio_dev *rdev,
525 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
526{
527 int rc = 0;
528
529 spin_lock(&rio_global_list_lock);
530 if (rdev->pwcback != NULL)
531 rc = -ENOMEM;
532 else
533 rdev->pwcback = pwcback;
534
535 spin_unlock(&rio_global_list_lock);
536 return rc;
537}
538EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
539
540/**
541 * rio_release_inb_pwrite - release inbound port-write message service
542 * @rdev: RIO device which registered for inbound port-write callback
543 *
544 * Removes callback from the rio_dev structure. Returns 0 if the request
545 * has been satisfied.
546 */
547int rio_release_inb_pwrite(struct rio_dev *rdev)
548{
549 int rc = -ENOMEM;
550
551 spin_lock(&rio_global_list_lock);
552 if (rdev->pwcback) {
553 rdev->pwcback = NULL;
554 rc = 0;
555 }
556
557 spin_unlock(&rio_global_list_lock);
558 return rc;
559}
560EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
561
562/**
Alexandre Bounineb6cb95e2016-03-22 14:26:41 -0700563 * rio_pw_enable - Enables/disables port-write handling by a master port
564 * @mport: Master port associated with port-write handling
565 * @enable: 1=enable, 0=disable
566 */
567void rio_pw_enable(struct rio_mport *mport, int enable)
568{
569 if (mport->ops->pwenable) {
570 mutex_lock(&mport->lock);
571
572 if ((enable && ++mport->pwe_refcnt == 1) ||
573 (!enable && mport->pwe_refcnt && --mport->pwe_refcnt == 0))
574 mport->ops->pwenable(mport, enable);
575 mutex_unlock(&mport->lock);
576 }
577}
578EXPORT_SYMBOL_GPL(rio_pw_enable);
579
580/**
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700581 * rio_map_inb_region -- Map inbound memory region.
582 * @mport: Master port.
Randy Dunlap2ca3cb52012-11-16 14:14:56 -0800583 * @local: physical address of memory region to be mapped
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700584 * @rbase: RIO base address assigned to this window
585 * @size: Size of the memory region
586 * @rflags: Flags for mapping.
587 *
588 * Return: 0 -- Success.
589 *
590 * This function will create the mapping from RIO space to local memory.
591 */
592int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
593 u64 rbase, u32 size, u32 rflags)
594{
595 int rc = 0;
596 unsigned long flags;
597
598 if (!mport->ops->map_inb)
599 return -1;
600 spin_lock_irqsave(&rio_mmap_lock, flags);
601 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
602 spin_unlock_irqrestore(&rio_mmap_lock, flags);
603 return rc;
604}
605EXPORT_SYMBOL_GPL(rio_map_inb_region);
606
607/**
608 * rio_unmap_inb_region -- Unmap the inbound memory region
609 * @mport: Master port
610 * @lstart: physical address of memory region to be unmapped
611 */
612void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
613{
614 unsigned long flags;
615 if (!mport->ops->unmap_inb)
616 return;
617 spin_lock_irqsave(&rio_mmap_lock, flags);
618 mport->ops->unmap_inb(mport, lstart);
619 spin_unlock_irqrestore(&rio_mmap_lock, flags);
620}
621EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
622
623/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700624 * rio_mport_get_physefb - Helper function that returns register offset
625 * for Physical Layer Extended Features Block.
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700626 * @port: Master port to issue transaction
627 * @local: Indicate a local master port or remote device access
628 * @destid: Destination ID of the device
629 * @hopcount: Number of switch hops to the device
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700630 */
631u32
632rio_mport_get_physefb(struct rio_mport *port, int local,
633 u16 destid, u8 hopcount)
634{
635 u32 ext_ftr_ptr;
636 u32 ftr_header;
637
638 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
639
640 while (ext_ftr_ptr) {
641 if (local)
642 rio_local_read_config_32(port, ext_ftr_ptr,
643 &ftr_header);
644 else
645 rio_mport_read_config_32(port, destid, hopcount,
646 ext_ftr_ptr, &ftr_header);
647
648 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
649 switch (ftr_header) {
650
651 case RIO_EFB_SER_EP_ID_V13P:
652 case RIO_EFB_SER_EP_REC_ID_V13P:
653 case RIO_EFB_SER_EP_FREE_ID_V13P:
654 case RIO_EFB_SER_EP_ID:
655 case RIO_EFB_SER_EP_REC_ID:
656 case RIO_EFB_SER_EP_FREE_ID:
657 case RIO_EFB_SER_EP_FREC_ID:
658
659 return ext_ftr_ptr;
660
661 default:
662 break;
663 }
664
665 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
666 hopcount, ext_ftr_ptr);
667 }
668
669 return ext_ftr_ptr;
670}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700671EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700672
673/**
674 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700675 * @comp_tag: RIO component tag to match
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700676 * @from: Previous RIO device found in search, or %NULL for new search
677 *
678 * Iterates through the list of known RIO devices. If a RIO device is
679 * found with a matching @comp_tag, a pointer to its device
680 * structure is returned. Otherwise, %NULL is returned. A new search
681 * is initiated by passing %NULL to the @from argument. Otherwise, if
682 * @from is not %NULL, searches continue from next device on the global
683 * list.
684 */
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700685struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700686{
687 struct list_head *n;
688 struct rio_dev *rdev;
689
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700690 spin_lock(&rio_global_list_lock);
691 n = from ? from->global_list.next : rio_devices.next;
692
693 while (n && (n != &rio_devices)) {
694 rdev = rio_dev_g(n);
695 if (rdev->comp_tag == comp_tag)
696 goto exit;
697 n = n->next;
698 }
699 rdev = NULL;
700exit:
701 spin_unlock(&rio_global_list_lock);
702 return rdev;
703}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700704EXPORT_SYMBOL_GPL(rio_get_comptag);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700705
706/**
707 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
708 * @rdev: Pointer to RIO device control structure
709 * @pnum: Switch port number to set LOCKOUT bit
710 * @lock: Operation : set (=1) or clear (=0)
711 */
712int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
713{
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700714 u32 regval;
715
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800716 rio_read_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700717 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
718 &regval);
719 if (lock)
720 regval |= RIO_PORT_N_CTL_LOCKOUT;
721 else
722 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
723
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800724 rio_write_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700725 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
726 regval);
727 return 0;
728}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700729EXPORT_SYMBOL_GPL(rio_set_port_lockout);
730
731/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700732 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
733 * given port
734 * @port: Master port associated with the RIO network
735 * @local: local=1 select local port otherwise a far device is reached
736 * @destid: Destination ID of the device to check host bit
737 * @hopcount: Number of hops to reach the target
738 * @port_num: Port (-number on switch) to enable on a far end device
739 *
740 * Returns 0 or 1 from on General Control Command and Status Register
741 * (EXT_PTR+0x3C)
742 */
743int rio_enable_rx_tx_port(struct rio_mport *port,
744 int local, u16 destid,
745 u8 hopcount, u8 port_num)
746{
747#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
748 u32 regval;
749 u32 ext_ftr_ptr;
750
751 /*
752 * enable rx input tx output port
753 */
754 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
755 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
756
757 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
758
759 if (local) {
760 rio_local_read_config_32(port, ext_ftr_ptr +
761 RIO_PORT_N_CTL_CSR(0),
762 &regval);
763 } else {
764 if (rio_mport_read_config_32(port, destid, hopcount,
765 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), &regval) < 0)
766 return -EIO;
767 }
768
769 if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
770 /* serial */
771 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
772 | RIO_PORT_N_CTL_EN_TX_SER;
773 } else {
774 /* parallel */
775 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
776 | RIO_PORT_N_CTL_EN_TX_PAR;
777 }
778
779 if (local) {
780 rio_local_write_config_32(port, ext_ftr_ptr +
781 RIO_PORT_N_CTL_CSR(0), regval);
782 } else {
783 if (rio_mport_write_config_32(port, destid, hopcount,
784 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
785 return -EIO;
786 }
787#endif
788 return 0;
789}
790EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
791
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700792
793/**
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700794 * rio_chk_dev_route - Validate route to the specified device.
795 * @rdev: RIO device failed to respond
796 * @nrdev: Last active device on the route to rdev
797 * @npnum: nrdev's port number on the route to rdev
798 *
799 * Follows a route to the specified RIO device to determine the last available
800 * device (and corresponding RIO port) on the route.
801 */
802static int
803rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
804{
805 u32 result;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800806 int p_port, rc = -EIO;
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700807 struct rio_dev *prev = NULL;
808
809 /* Find switch with failed RIO link */
810 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
811 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
812 prev = rdev->prev;
813 break;
814 }
815 rdev = rdev->prev;
816 }
817
818 if (prev == NULL)
819 goto err_out;
820
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800821 p_port = prev->rswitch->route_table[rdev->destid];
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700822
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700823 if (p_port != RIO_INVALID_ROUTE) {
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700824 pr_debug("RIO: link failed on [%s]-P%d\n",
825 rio_name(prev), p_port);
826 *nrdev = prev;
827 *npnum = p_port;
828 rc = 0;
829 } else
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700830 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700831err_out:
832 return rc;
833}
834
835/**
836 * rio_mport_chk_dev_access - Validate access to the specified device.
837 * @mport: Master port to send transactions
838 * @destid: Device destination ID in network
839 * @hopcount: Number of hops into the network
840 */
Alexandre Bouninee274e0e2010-10-27 15:34:32 -0700841int
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700842rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
843{
844 int i = 0;
845 u32 tmp;
846
847 while (rio_mport_read_config_32(mport, destid, hopcount,
848 RIO_DEV_ID_CAR, &tmp)) {
849 i++;
850 if (i == RIO_MAX_CHK_RETRY)
851 return -EIO;
852 mdelay(1);
853 }
854
855 return 0;
856}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700857EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700858
859/**
860 * rio_chk_dev_access - Validate access to the specified device.
861 * @rdev: Pointer to RIO device control structure
862 */
863static int rio_chk_dev_access(struct rio_dev *rdev)
864{
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800865 return rio_mport_chk_dev_access(rdev->net->hport,
866 rdev->destid, rdev->hopcount);
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700867}
868
869/**
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700870 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
871 * returns link-response (if requested).
872 * @rdev: RIO devive to issue Input-status command
873 * @pnum: Device port number to issue the command
874 * @lnkresp: Response from a link partner
875 */
876static int
877rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
878{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700879 u32 regval;
880 int checkcount;
881
882 if (lnkresp) {
883 /* Read from link maintenance response register
884 * to clear valid bit */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800885 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700886 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
887 &regval);
888 udelay(50);
889 }
890
891 /* Issue Input-status command */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800892 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700893 rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
894 RIO_MNT_REQ_CMD_IS);
895
896 /* Exit if the response is not expected */
897 if (lnkresp == NULL)
898 return 0;
899
900 checkcount = 3;
901 while (checkcount--) {
902 udelay(50);
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800903 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700904 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
905 &regval);
906 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
907 *lnkresp = regval;
908 return 0;
909 }
910 }
911
912 return -EIO;
913}
914
915/**
916 * rio_clr_err_stopped - Clears port Error-stopped states.
917 * @rdev: Pointer to RIO device control structure
918 * @pnum: Switch port number to clear errors
919 * @err_status: port error status (if 0 reads register from device)
920 */
921static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
922{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700923 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
924 u32 regval;
925 u32 far_ackid, far_linkstat, near_ackid;
926
927 if (err_status == 0)
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800928 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700929 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
930 &err_status);
931
932 if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
933 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
934 /*
935 * Send a Link-Request/Input-Status control symbol
936 */
937 if (rio_get_input_status(rdev, pnum, &regval)) {
938 pr_debug("RIO_EM: Input-status response timeout\n");
939 goto rd_err;
940 }
941
942 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
943 pnum, regval);
944 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
945 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800946 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700947 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
948 &regval);
949 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
950 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
951 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
952 " near_ackID=0x%02x\n",
953 pnum, far_ackid, far_linkstat, near_ackid);
954
955 /*
956 * If required, synchronize ackIDs of near and
957 * far sides.
958 */
959 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
960 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
961 /* Align near outstanding/outbound ackIDs with
962 * far inbound.
963 */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800964 rio_write_config_32(rdev,
965 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700966 (near_ackid << 24) |
967 (far_ackid << 8) | far_ackid);
968 /* Align far outstanding/outbound ackIDs with
969 * near inbound.
970 */
971 far_ackid++;
972 if (nextdev)
973 rio_write_config_32(nextdev,
974 nextdev->phys_efptr +
975 RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
976 (far_ackid << 24) |
977 (near_ackid << 8) | near_ackid);
978 else
979 pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
980 }
981rd_err:
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800982 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700983 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
984 &err_status);
985 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
986 }
987
988 if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
989 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
990 rio_get_input_status(nextdev,
991 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
992 udelay(50);
993
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800994 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700995 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
996 &err_status);
997 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
998 }
999
1000 return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
1001 RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
1002}
1003
1004/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001005 * rio_inb_pwrite_handler - process inbound port-write message
1006 * @pw_msg: pointer to inbound port-write message
1007 *
1008 * Processes an inbound port-write message. Returns 0 if the request
1009 * has been satisfied.
1010 */
1011int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
1012{
1013 struct rio_dev *rdev;
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001014 u32 err_status, em_perrdet, em_ltlerrdet;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001015 int rc, portnum;
1016
Alexandre Bouninee6536922011-01-12 17:00:40 -08001017 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001018 if (rdev == NULL) {
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001019 /* Device removed or enumeration error */
1020 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001021 __func__, pw_msg->em.comptag);
1022 return -EIO;
1023 }
1024
1025 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
1026
1027#ifdef DEBUG_PW
1028 {
1029 u32 i;
1030 for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001031 pr_debug("0x%02x: %08x %08x %08x %08x\n",
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001032 i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
1033 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
1034 i += 4;
1035 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001036 }
1037#endif
1038
1039 /* Call an external service function (if such is registered
1040 * for this device). This may be the service for endpoints that send
1041 * device-specific port-write messages. End-point messages expected
1042 * to be handled completely by EP specific device driver.
1043 * For switches rc==0 signals that no standard processing required.
1044 */
1045 if (rdev->pwcback != NULL) {
1046 rc = rdev->pwcback(rdev, pw_msg, 0);
1047 if (rc == 0)
1048 return 0;
1049 }
1050
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001051 portnum = pw_msg->em.is_port & 0xFF;
1052
1053 /* Check if device and route to it are functional:
1054 * Sometimes devices may send PW message(s) just before being
1055 * powered down (or link being lost).
1056 */
1057 if (rio_chk_dev_access(rdev)) {
1058 pr_debug("RIO: device access failed - get link partner\n");
1059 /* Scan route to the device and identify failed link.
1060 * This will replace device and port reported in PW message.
1061 * PW message should not be used after this point.
1062 */
1063 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
1064 pr_err("RIO: Route trace for %s failed\n",
1065 rio_name(rdev));
1066 return -EIO;
1067 }
1068 pw_msg = NULL;
1069 }
1070
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001071 /* For End-point devices processing stops here */
1072 if (!(rdev->pef & RIO_PEF_SWITCH))
1073 return 0;
1074
1075 if (rdev->phys_efptr == 0) {
1076 pr_err("RIO_PW: Bad switch initialization for %s\n",
1077 rio_name(rdev));
1078 return 0;
1079 }
1080
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001081 /*
1082 * Process the port-write notification from switch
1083 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001084 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
1085 rdev->rswitch->ops->em_handle(rdev, portnum);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001086
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001087 rio_read_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001088 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
1089 &err_status);
1090 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
1091
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001092 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001093
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001094 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
1095 rdev->rswitch->port_ok |= (1 << portnum);
1096 rio_set_port_lockout(rdev, portnum, 0);
1097 /* Schedule Insertion Service */
1098 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
1099 rio_name(rdev), portnum);
1100 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001101
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001102 /* Clear error-stopped states (if reported).
1103 * Depending on the link partner state, two attempts
1104 * may be needed for successful recovery.
1105 */
1106 if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
1107 RIO_PORT_N_ERR_STS_PW_INP_ES)) {
1108 if (rio_clr_err_stopped(rdev, portnum, err_status))
1109 rio_clr_err_stopped(rdev, portnum, 0);
1110 }
1111 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001112
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001113 if (rdev->rswitch->port_ok & (1 << portnum)) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001114 rdev->rswitch->port_ok &= ~(1 << portnum);
1115 rio_set_port_lockout(rdev, portnum, 1);
1116
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001117 rio_write_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001118 rdev->phys_efptr +
1119 RIO_PORT_N_ACK_STS_CSR(portnum),
1120 RIO_PORT_N_ACK_CLEAR);
1121
1122 /* Schedule Extraction Service */
1123 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
1124 rio_name(rdev), portnum);
1125 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001126 }
1127
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001128 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001129 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
1130 if (em_perrdet) {
1131 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
1132 portnum, em_perrdet);
1133 /* Clear EM Port N Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001134 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001135 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
1136 }
1137
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001138 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001139 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1140 if (em_ltlerrdet) {
1141 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1142 em_ltlerrdet);
1143 /* Clear EM L/T Layer Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001144 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001145 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1146 }
1147
Alexandre Bounine388c45c2010-10-27 15:34:35 -07001148 /* Clear remaining error bits and Port-Write Pending bit */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001149 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001150 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
Alexandre Bounine388c45c2010-10-27 15:34:35 -07001151 err_status);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001152
1153 return 0;
1154}
1155EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1156
1157/**
1158 * rio_mport_get_efb - get pointer to next extended features block
1159 * @port: Master port to issue transaction
1160 * @local: Indicate a local master port or remote device access
1161 * @destid: Destination ID of the device
1162 * @hopcount: Number of switch hops to the device
1163 * @from: Offset of current Extended Feature block header (if 0 starts
1164 * from ExtFeaturePtr)
1165 */
1166u32
1167rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1168 u8 hopcount, u32 from)
1169{
1170 u32 reg_val;
1171
1172 if (from == 0) {
1173 if (local)
1174 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1175 &reg_val);
1176 else
1177 rio_mport_read_config_32(port, destid, hopcount,
1178 RIO_ASM_INFO_CAR, &reg_val);
1179 return reg_val & RIO_EXT_FTR_PTR_MASK;
1180 } else {
1181 if (local)
1182 rio_local_read_config_32(port, from, &reg_val);
1183 else
1184 rio_mport_read_config_32(port, destid, hopcount,
1185 from, &reg_val);
1186 return RIO_GET_BLOCK_ID(reg_val);
1187 }
1188}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001189EXPORT_SYMBOL_GPL(rio_mport_get_efb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001190
1191/**
Matt Porter394b7012005-11-07 01:00:15 -08001192 * rio_mport_get_feature - query for devices' extended features
1193 * @port: Master port to issue transaction
1194 * @local: Indicate a local master port or remote device access
1195 * @destid: Destination ID of the device
1196 * @hopcount: Number of switch hops to the device
1197 * @ftr: Extended feature code
1198 *
1199 * Tell if a device supports a given RapidIO capability.
1200 * Returns the offset of the requested extended feature
1201 * block within the device's RIO configuration space or
1202 * 0 in case the device does not support it. Possible
1203 * values for @ftr:
1204 *
1205 * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
1206 *
1207 * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
1208 *
1209 * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
1210 *
1211 * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
1212 *
1213 * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
1214 *
1215 * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
1216 */
1217u32
1218rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1219 u8 hopcount, int ftr)
1220{
1221 u32 asm_info, ext_ftr_ptr, ftr_header;
1222
1223 if (local)
1224 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1225 else
1226 rio_mport_read_config_32(port, destid, hopcount,
1227 RIO_ASM_INFO_CAR, &asm_info);
1228
1229 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1230
1231 while (ext_ftr_ptr) {
1232 if (local)
1233 rio_local_read_config_32(port, ext_ftr_ptr,
1234 &ftr_header);
1235 else
1236 rio_mport_read_config_32(port, destid, hopcount,
1237 ext_ftr_ptr, &ftr_header);
1238 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1239 return ext_ftr_ptr;
1240 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
1241 break;
1242 }
1243
1244 return 0;
1245}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001246EXPORT_SYMBOL_GPL(rio_mport_get_feature);
Matt Porter394b7012005-11-07 01:00:15 -08001247
1248/**
1249 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
1250 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1251 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1252 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
1253 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
1254 * @from: Previous RIO device found in search, or %NULL for new search
1255 *
1256 * Iterates through the list of known RIO devices. If a RIO device is
1257 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
1258 * count to the device is incrememted and a pointer to its device
1259 * structure is returned. Otherwise, %NULL is returned. A new search
1260 * is initiated by passing %NULL to the @from argument. Otherwise, if
1261 * @from is not %NULL, searches continue from next device on the global
1262 * list. The reference count for @from is always decremented if it is
1263 * not %NULL.
1264 */
1265struct rio_dev *rio_get_asm(u16 vid, u16 did,
1266 u16 asm_vid, u16 asm_did, struct rio_dev *from)
1267{
1268 struct list_head *n;
1269 struct rio_dev *rdev;
1270
1271 WARN_ON(in_interrupt());
1272 spin_lock(&rio_global_list_lock);
1273 n = from ? from->global_list.next : rio_devices.next;
1274
1275 while (n && (n != &rio_devices)) {
1276 rdev = rio_dev_g(n);
1277 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1278 (did == RIO_ANY_ID || rdev->did == did) &&
1279 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1280 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1281 goto exit;
1282 n = n->next;
1283 }
1284 rdev = NULL;
1285 exit:
1286 rio_dev_put(from);
1287 rdev = rio_dev_get(rdev);
1288 spin_unlock(&rio_global_list_lock);
1289 return rdev;
1290}
1291
1292/**
1293 * rio_get_device - Begin or continue searching for a RIO device by vid/did
1294 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1295 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1296 * @from: Previous RIO device found in search, or %NULL for new search
1297 *
1298 * Iterates through the list of known RIO devices. If a RIO device is
1299 * found with a matching @vid and @did, the reference count to the
1300 * device is incrememted and a pointer to its device structure is returned.
1301 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
1302 * to the @from argument. Otherwise, if @from is not %NULL, searches
1303 * continue from next device on the global list. The reference count for
1304 * @from is always decremented if it is not %NULL.
1305 */
1306struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1307{
1308 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1309}
1310
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001311/**
1312 * rio_std_route_add_entry - Add switch route table entry using standard
1313 * registers defined in RIO specification rev.1.3
1314 * @mport: Master port to issue transaction
1315 * @destid: Destination ID of the device
1316 * @hopcount: Number of switch hops to the device
1317 * @table: routing table ID (global or port-specific)
1318 * @route_destid: destID entry in the RT
1319 * @route_port: destination port for specified destID
1320 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001321static int
1322rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1323 u16 table, u16 route_destid, u8 route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001324{
1325 if (table == RIO_GLOBAL_TABLE) {
1326 rio_mport_write_config_32(mport, destid, hopcount,
1327 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1328 (u32)route_destid);
1329 rio_mport_write_config_32(mport, destid, hopcount,
1330 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1331 (u32)route_port);
1332 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001333
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001334 udelay(10);
1335 return 0;
1336}
1337
1338/**
1339 * rio_std_route_get_entry - Read switch route table entry (port number)
Uwe Kleine-König638c5942010-06-11 12:16:58 +02001340 * associated with specified destID using standard registers defined in RIO
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001341 * specification rev.1.3
1342 * @mport: Master port to issue transaction
1343 * @destid: Destination ID of the device
1344 * @hopcount: Number of switch hops to the device
1345 * @table: routing table ID (global or port-specific)
1346 * @route_destid: destID entry in the RT
1347 * @route_port: returned destination port for specified destID
1348 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001349static int
1350rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1351 u16 table, u16 route_destid, u8 *route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001352{
1353 u32 result;
1354
1355 if (table == RIO_GLOBAL_TABLE) {
1356 rio_mport_write_config_32(mport, destid, hopcount,
1357 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1358 rio_mport_read_config_32(mport, destid, hopcount,
1359 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1360
1361 *route_port = (u8)result;
1362 }
1363
1364 return 0;
1365}
1366
1367/**
1368 * rio_std_route_clr_table - Clear swotch route table using standard registers
1369 * defined in RIO specification rev.1.3.
1370 * @mport: Master port to issue transaction
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001371 * @destid: Destination ID of the device
1372 * @hopcount: Number of switch hops to the device
1373 * @table: routing table ID (global or port-specific)
1374 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001375static int
1376rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1377 u16 table)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001378{
1379 u32 max_destid = 0xff;
1380 u32 i, pef, id_inc = 1, ext_cfg = 0;
1381 u32 port_sel = RIO_INVALID_ROUTE;
1382
1383 if (table == RIO_GLOBAL_TABLE) {
1384 rio_mport_read_config_32(mport, destid, hopcount,
1385 RIO_PEF_CAR, &pef);
1386
1387 if (mport->sys_size) {
1388 rio_mport_read_config_32(mport, destid, hopcount,
1389 RIO_SWITCH_RT_LIMIT,
1390 &max_destid);
1391 max_destid &= RIO_RT_MAX_DESTID;
1392 }
1393
1394 if (pef & RIO_PEF_EXT_RT) {
1395 ext_cfg = 0x80000000;
1396 id_inc = 4;
1397 port_sel = (RIO_INVALID_ROUTE << 24) |
1398 (RIO_INVALID_ROUTE << 16) |
1399 (RIO_INVALID_ROUTE << 8) |
1400 RIO_INVALID_ROUTE;
1401 }
1402
1403 for (i = 0; i <= max_destid;) {
1404 rio_mport_write_config_32(mport, destid, hopcount,
1405 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1406 ext_cfg | i);
1407 rio_mport_write_config_32(mport, destid, hopcount,
1408 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1409 port_sel);
1410 i += id_inc;
1411 }
1412 }
1413
1414 udelay(10);
1415 return 0;
1416}
1417
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001418/**
1419 * rio_lock_device - Acquires host device lock for specified device
1420 * @port: Master port to send transaction
1421 * @destid: Destination ID for device/switch
1422 * @hopcount: Hopcount to reach switch
1423 * @wait_ms: Max wait time in msec (0 = no timeout)
1424 *
1425 * Attepts to acquire host device lock for specified device
1426 * Returns 0 if device lock acquired or EINVAL if timeout expires.
1427 */
1428int rio_lock_device(struct rio_mport *port, u16 destid,
1429 u8 hopcount, int wait_ms)
1430{
1431 u32 result;
1432 int tcnt = 0;
1433
1434 /* Attempt to acquire device lock */
1435 rio_mport_write_config_32(port, destid, hopcount,
1436 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1437 rio_mport_read_config_32(port, destid, hopcount,
1438 RIO_HOST_DID_LOCK_CSR, &result);
1439
1440 while (result != port->host_deviceid) {
1441 if (wait_ms != 0 && tcnt == wait_ms) {
1442 pr_debug("RIO: timeout when locking device %x:%x\n",
1443 destid, hopcount);
1444 return -EINVAL;
1445 }
1446
1447 /* Delay a bit */
1448 mdelay(1);
1449 tcnt++;
1450 /* Try to acquire device lock again */
1451 rio_mport_write_config_32(port, destid,
1452 hopcount,
1453 RIO_HOST_DID_LOCK_CSR,
1454 port->host_deviceid);
1455 rio_mport_read_config_32(port, destid,
1456 hopcount,
1457 RIO_HOST_DID_LOCK_CSR, &result);
1458 }
1459
1460 return 0;
1461}
1462EXPORT_SYMBOL_GPL(rio_lock_device);
1463
1464/**
1465 * rio_unlock_device - Releases host device lock for specified device
1466 * @port: Master port to send transaction
1467 * @destid: Destination ID for device/switch
1468 * @hopcount: Hopcount to reach switch
1469 *
1470 * Returns 0 if device lock released or EINVAL if fails.
1471 */
1472int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1473{
1474 u32 result;
1475
1476 /* Release device lock */
1477 rio_mport_write_config_32(port, destid,
1478 hopcount,
1479 RIO_HOST_DID_LOCK_CSR,
1480 port->host_deviceid);
1481 rio_mport_read_config_32(port, destid, hopcount,
1482 RIO_HOST_DID_LOCK_CSR, &result);
1483 if ((result & 0xffff) != 0xffff) {
1484 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1485 destid, hopcount);
1486 return -EINVAL;
1487 }
1488
1489 return 0;
1490}
1491EXPORT_SYMBOL_GPL(rio_unlock_device);
1492
1493/**
1494 * rio_route_add_entry- Add a route entry to a switch routing table
1495 * @rdev: RIO device
1496 * @table: Routing table ID
1497 * @route_destid: Destination ID to be routed
1498 * @route_port: Port number to be routed
1499 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1500 *
1501 * If available calls the switch specific add_entry() method to add a route
1502 * entry into a switch routing table. Otherwise uses standard RT update method
1503 * as defined by RapidIO specification. A specific routing table can be selected
1504 * using the @table argument if a switch has per port routing tables or
1505 * the standard (or global) table may be used by passing
1506 * %RIO_GLOBAL_TABLE in @table.
1507 *
1508 * Returns %0 on success or %-EINVAL on failure.
1509 */
1510int rio_route_add_entry(struct rio_dev *rdev,
1511 u16 table, u16 route_destid, u8 route_port, int lock)
1512{
1513 int rc = -EINVAL;
1514 struct rio_switch_ops *ops = rdev->rswitch->ops;
1515
1516 if (lock) {
1517 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1518 rdev->hopcount, 1000);
1519 if (rc)
1520 return rc;
1521 }
1522
1523 spin_lock(&rdev->rswitch->lock);
1524
1525 if (ops == NULL || ops->add_entry == NULL) {
1526 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1527 rdev->hopcount, table,
1528 route_destid, route_port);
1529 } else if (try_module_get(ops->owner)) {
1530 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1531 rdev->hopcount, table, route_destid,
1532 route_port);
1533 module_put(ops->owner);
1534 }
1535
1536 spin_unlock(&rdev->rswitch->lock);
1537
1538 if (lock)
1539 rio_unlock_device(rdev->net->hport, rdev->destid,
1540 rdev->hopcount);
1541
1542 return rc;
1543}
1544EXPORT_SYMBOL_GPL(rio_route_add_entry);
1545
1546/**
1547 * rio_route_get_entry- Read an entry from a switch routing table
1548 * @rdev: RIO device
1549 * @table: Routing table ID
1550 * @route_destid: Destination ID to be routed
1551 * @route_port: Pointer to read port number into
1552 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1553 *
1554 * If available calls the switch specific get_entry() method to fetch a route
1555 * entry from a switch routing table. Otherwise uses standard RT read method
1556 * as defined by RapidIO specification. A specific routing table can be selected
1557 * using the @table argument if a switch has per port routing tables or
1558 * the standard (or global) table may be used by passing
1559 * %RIO_GLOBAL_TABLE in @table.
1560 *
1561 * Returns %0 on success or %-EINVAL on failure.
1562 */
1563int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1564 u16 route_destid, u8 *route_port, int lock)
1565{
1566 int rc = -EINVAL;
1567 struct rio_switch_ops *ops = rdev->rswitch->ops;
1568
1569 if (lock) {
1570 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1571 rdev->hopcount, 1000);
1572 if (rc)
1573 return rc;
1574 }
1575
1576 spin_lock(&rdev->rswitch->lock);
1577
1578 if (ops == NULL || ops->get_entry == NULL) {
1579 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1580 rdev->hopcount, table,
1581 route_destid, route_port);
1582 } else if (try_module_get(ops->owner)) {
1583 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1584 rdev->hopcount, table, route_destid,
1585 route_port);
1586 module_put(ops->owner);
1587 }
1588
1589 spin_unlock(&rdev->rswitch->lock);
1590
1591 if (lock)
1592 rio_unlock_device(rdev->net->hport, rdev->destid,
1593 rdev->hopcount);
1594 return rc;
1595}
1596EXPORT_SYMBOL_GPL(rio_route_get_entry);
1597
1598/**
1599 * rio_route_clr_table - Clear a switch routing table
1600 * @rdev: RIO device
1601 * @table: Routing table ID
1602 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1603 *
1604 * If available calls the switch specific clr_table() method to clear a switch
1605 * routing table. Otherwise uses standard RT write method as defined by RapidIO
1606 * specification. A specific routing table can be selected using the @table
1607 * argument if a switch has per port routing tables or the standard (or global)
1608 * table may be used by passing %RIO_GLOBAL_TABLE in @table.
1609 *
1610 * Returns %0 on success or %-EINVAL on failure.
1611 */
1612int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1613{
1614 int rc = -EINVAL;
1615 struct rio_switch_ops *ops = rdev->rswitch->ops;
1616
1617 if (lock) {
1618 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1619 rdev->hopcount, 1000);
1620 if (rc)
1621 return rc;
1622 }
1623
1624 spin_lock(&rdev->rswitch->lock);
1625
1626 if (ops == NULL || ops->clr_table == NULL) {
1627 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1628 rdev->hopcount, table);
1629 } else if (try_module_get(ops->owner)) {
1630 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1631 rdev->hopcount, table);
1632
1633 module_put(ops->owner);
1634 }
1635
1636 spin_unlock(&rdev->rswitch->lock);
1637
1638 if (lock)
1639 rio_unlock_device(rdev->net->hport, rdev->destid,
1640 rdev->hopcount);
1641
1642 return rc;
1643}
1644EXPORT_SYMBOL_GPL(rio_route_clr_table);
1645
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001646#ifdef CONFIG_RAPIDIO_DMA_ENGINE
1647
1648static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1649{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001650 struct rio_mport *mport = arg;
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001651
1652 /* Check that DMA device belongs to the right MPORT */
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001653 return mport == container_of(chan->device, struct rio_mport, dma);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001654}
1655
1656/**
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001657 * rio_request_mport_dma - request RapidIO capable DMA channel associated
1658 * with specified local RapidIO mport device.
1659 * @mport: RIO mport to perform DMA data transfers
1660 *
1661 * Returns pointer to allocated DMA channel or NULL if failed.
1662 */
1663struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
1664{
1665 dma_cap_mask_t mask;
1666
1667 dma_cap_zero(mask);
1668 dma_cap_set(DMA_SLAVE, mask);
1669 return dma_request_channel(mask, rio_chan_filter, mport);
1670}
1671EXPORT_SYMBOL_GPL(rio_request_mport_dma);
1672
1673/**
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001674 * rio_request_dma - request RapidIO capable DMA channel that supports
1675 * specified target RapidIO device.
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001676 * @rdev: RIO device associated with DMA transfer
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001677 *
1678 * Returns pointer to allocated DMA channel or NULL if failed.
1679 */
1680struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1681{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001682 return rio_request_mport_dma(rdev->net->hport);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001683}
1684EXPORT_SYMBOL_GPL(rio_request_dma);
1685
1686/**
1687 * rio_release_dma - release specified DMA channel
1688 * @dchan: DMA channel to release
1689 */
1690void rio_release_dma(struct dma_chan *dchan)
1691{
1692 dma_release_channel(dchan);
1693}
1694EXPORT_SYMBOL_GPL(rio_release_dma);
1695
1696/**
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001697 * rio_dma_prep_xfer - RapidIO specific wrapper
1698 * for device_prep_slave_sg callback defined by DMAENGINE.
1699 * @dchan: DMA channel to configure
1700 * @destid: target RapidIO device destination ID
1701 * @data: RIO specific data descriptor
1702 * @direction: DMA data transfer direction (TO or FROM the device)
1703 * @flags: dmaengine defined flags
1704 *
1705 * Initializes RapidIO capable DMA channel for the specified data transfer.
1706 * Uses DMA channel private extension to pass information related to remote
1707 * target RIO device.
1708 * Returns pointer to DMA transaction descriptor or NULL if failed.
1709 */
1710struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
1711 u16 destid, struct rio_dma_data *data,
1712 enum dma_transfer_direction direction, unsigned long flags)
1713{
1714 struct rio_dma_ext rio_ext;
1715
1716 if (dchan->device->device_prep_slave_sg == NULL) {
1717 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1718 return NULL;
1719 }
1720
1721 rio_ext.destid = destid;
1722 rio_ext.rio_addr_u = data->rio_addr_u;
1723 rio_ext.rio_addr = data->rio_addr;
1724 rio_ext.wr_type = data->wr_type;
1725
1726 return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1727 direction, flags, &rio_ext);
1728}
1729EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
1730
1731/**
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001732 * rio_dma_prep_slave_sg - RapidIO specific wrapper
1733 * for device_prep_slave_sg callback defined by DMAENGINE.
1734 * @rdev: RIO device control structure
1735 * @dchan: DMA channel to configure
1736 * @data: RIO specific data descriptor
1737 * @direction: DMA data transfer direction (TO or FROM the device)
1738 * @flags: dmaengine defined flags
1739 *
1740 * Initializes RapidIO capable DMA channel for the specified data transfer.
1741 * Uses DMA channel private extension to pass information related to remote
1742 * target RIO device.
1743 * Returns pointer to DMA transaction descriptor or NULL if failed.
1744 */
1745struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1746 struct dma_chan *dchan, struct rio_dma_data *data,
1747 enum dma_transfer_direction direction, unsigned long flags)
1748{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001749 return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001750}
1751EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1752
1753#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1754
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001755/**
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07001756 * rio_find_mport - find RIO mport by its ID
1757 * @mport_id: number (ID) of mport device
1758 *
1759 * Given a RIO mport number, the desired mport is located
1760 * in the global list of mports. If the mport is found, a pointer to its
1761 * data structure is returned. If no mport is found, %NULL is returned.
1762 */
1763struct rio_mport *rio_find_mport(int mport_id)
1764{
1765 struct rio_mport *port;
1766
1767 mutex_lock(&rio_mport_list_lock);
1768 list_for_each_entry(port, &rio_mports, node) {
1769 if (port->id == mport_id)
1770 goto found;
1771 }
1772 port = NULL;
1773found:
1774 mutex_unlock(&rio_mport_list_lock);
1775
1776 return port;
1777}
1778
1779/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001780 * rio_register_scan - enumeration/discovery method registration interface
1781 * @mport_id: mport device ID for which fabric scan routine has to be set
1782 * (RIO_MPORT_ANY = set for all available mports)
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001783 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001784 *
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001785 * Registers enumeration/discovery operations with RapidIO subsystem and
1786 * attaches it to the specified mport device (or all available mports
1787 * if RIO_MPORT_ANY is specified).
1788 *
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001789 * Returns error if the mport already has an enumerator attached to it.
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001790 * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001791 */
1792int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1793{
1794 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001795 struct rio_scan_node *scan;
1796 int rc = 0;
1797
1798 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1799
1800 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1801 !scan_ops)
1802 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001803
1804 mutex_lock(&rio_mport_list_lock);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001805
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001806 /*
1807 * Check if there is another enumerator already registered for
1808 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
1809 * for the same mport ID are not supported.
1810 */
1811 list_for_each_entry(scan, &rio_scans, node) {
1812 if (scan->mport_id == mport_id) {
1813 rc = -EBUSY;
1814 goto err_out;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001815 }
1816 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001817
1818 /*
1819 * Allocate and initialize new scan registration node.
1820 */
1821 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
1822 if (!scan) {
1823 rc = -ENOMEM;
1824 goto err_out;
1825 }
1826
1827 scan->mport_id = mport_id;
1828 scan->ops = scan_ops;
1829
1830 /*
1831 * Traverse the list of registered mports to attach this new scan.
1832 *
1833 * The new scan with matching mport ID overrides any previously attached
1834 * scan assuming that old scan (if any) is the default one (based on the
1835 * enumerator registration check above).
1836 * If the new scan is the global one, it will be attached only to mports
1837 * that do not have their own individual operations already attached.
1838 */
1839 list_for_each_entry(port, &rio_mports, node) {
1840 if (port->id == mport_id) {
1841 port->nscan = scan_ops;
1842 break;
1843 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
1844 port->nscan = scan_ops;
1845 }
1846
1847 list_add_tail(&scan->node, &rio_scans);
1848
1849err_out:
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001850 mutex_unlock(&rio_mport_list_lock);
1851
1852 return rc;
1853}
1854EXPORT_SYMBOL_GPL(rio_register_scan);
1855
1856/**
1857 * rio_unregister_scan - removes enumeration/discovery method from mport
1858 * @mport_id: mport device ID for which fabric scan routine has to be
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001859 * unregistered (RIO_MPORT_ANY = apply to all mports that use
1860 * the specified scan_ops)
1861 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001862 *
1863 * Removes enumeration or discovery method assigned to the specified mport
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001864 * device. If RIO_MPORT_ANY is specified, removes the specified operations from
1865 * all mports that have them attached.
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001866 */
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001867int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001868{
1869 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001870 struct rio_scan_node *scan;
1871
1872 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1873
1874 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
1875 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001876
1877 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001878
1879 list_for_each_entry(port, &rio_mports, node)
1880 if (port->id == mport_id ||
1881 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
1882 port->nscan = NULL;
1883
Dan Carpenterf93f3c42013-07-31 13:53:34 -07001884 list_for_each_entry(scan, &rio_scans, node) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001885 if (scan->mport_id == mport_id) {
1886 list_del(&scan->node);
1887 kfree(scan);
Dan Carpenterf93f3c42013-07-31 13:53:34 -07001888 break;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001889 }
Dan Carpenterf93f3c42013-07-31 13:53:34 -07001890 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001891
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001892 mutex_unlock(&rio_mport_list_lock);
1893
1894 return 0;
1895}
1896EXPORT_SYMBOL_GPL(rio_unregister_scan);
1897
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001898/**
1899 * rio_mport_scan - execute enumeration/discovery on the specified mport
1900 * @mport_id: number (ID) of mport device
1901 */
1902int rio_mport_scan(int mport_id)
1903{
1904 struct rio_mport *port = NULL;
1905 int rc;
1906
1907 mutex_lock(&rio_mport_list_lock);
1908 list_for_each_entry(port, &rio_mports, node) {
1909 if (port->id == mport_id)
1910 goto found;
1911 }
1912 mutex_unlock(&rio_mport_list_lock);
1913 return -ENODEV;
1914found:
1915 if (!port->nscan) {
1916 mutex_unlock(&rio_mport_list_lock);
1917 return -EINVAL;
1918 }
1919
1920 if (!try_module_get(port->nscan->owner)) {
1921 mutex_unlock(&rio_mport_list_lock);
1922 return -ENODEV;
1923 }
1924
1925 mutex_unlock(&rio_mport_list_lock);
1926
1927 if (port->host_deviceid >= 0)
1928 rc = port->nscan->enumerate(port, 0);
1929 else
1930 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
1931
1932 module_put(port->nscan->owner);
1933 return rc;
1934}
1935
Matt Porter394b7012005-11-07 01:00:15 -08001936static void rio_fixup_device(struct rio_dev *dev)
1937{
1938}
1939
Bill Pemberton305c8912012-11-19 13:23:25 -05001940static int rio_init(void)
Matt Porter394b7012005-11-07 01:00:15 -08001941{
1942 struct rio_dev *dev = NULL;
1943
1944 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
1945 rio_fixup_device(dev);
1946 }
1947 return 0;
1948}
1949
Alexandre Bounine005842e2012-10-04 17:16:08 -07001950static struct workqueue_struct *rio_wq;
1951
1952struct rio_disc_work {
1953 struct work_struct work;
1954 struct rio_mport *mport;
1955};
1956
Bill Pemberton305c8912012-11-19 13:23:25 -05001957static void disc_work_handler(struct work_struct *_work)
Alexandre Bounine005842e2012-10-04 17:16:08 -07001958{
1959 struct rio_disc_work *work;
1960
1961 work = container_of(_work, struct rio_disc_work, work);
1962 pr_debug("RIO: discovery work for mport %d %s\n",
1963 work->mport->id, work->mport->name);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001964 if (try_module_get(work->mport->nscan->owner)) {
1965 work->mport->nscan->discover(work->mport, 0);
1966 module_put(work->mport->nscan->owner);
1967 }
Alexandre Bounine005842e2012-10-04 17:16:08 -07001968}
1969
Bill Pemberton305c8912012-11-19 13:23:25 -05001970int rio_init_mports(void)
Matt Porter394b7012005-11-07 01:00:15 -08001971{
Matt Porter394b7012005-11-07 01:00:15 -08001972 struct rio_mport *port;
Alexandre Bounine005842e2012-10-04 17:16:08 -07001973 struct rio_disc_work *work;
Alexandre Bounine25747402012-10-10 15:53:59 -07001974 int n = 0;
Matt Porter394b7012005-11-07 01:00:15 -08001975
Alexandre Bounine25747402012-10-10 15:53:59 -07001976 if (!next_portid)
1977 return -ENODEV;
1978
1979 /*
1980 * First, run enumerations and check if we need to perform discovery
1981 * on any of the registered mports.
1982 */
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001983 mutex_lock(&rio_mport_list_lock);
Matt Porter394b7012005-11-07 01:00:15 -08001984 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001985 if (port->host_deviceid >= 0) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001986 if (port->nscan && try_module_get(port->nscan->owner)) {
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07001987 port->nscan->enumerate(port, 0);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001988 module_put(port->nscan->owner);
1989 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001990 } else
Alexandre Bounine25747402012-10-10 15:53:59 -07001991 n++;
1992 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001993 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine005842e2012-10-04 17:16:08 -07001994
Alexandre Bounine25747402012-10-10 15:53:59 -07001995 if (!n)
1996 goto no_disc;
Alexandre Bounine005842e2012-10-04 17:16:08 -07001997
Alexandre Bounine25747402012-10-10 15:53:59 -07001998 /*
1999 * If we have mports that require discovery schedule a discovery work
2000 * for each of them. If the code below fails to allocate needed
2001 * resources, exit without error to keep results of enumeration
2002 * process (if any).
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002003 * TODO: Implement restart of discovery process for all or
Alexandre Bounine25747402012-10-10 15:53:59 -07002004 * individual discovering mports.
2005 */
2006 rio_wq = alloc_workqueue("riodisc", 0, 0);
2007 if (!rio_wq) {
2008 pr_err("RIO: unable allocate rio_wq\n");
2009 goto no_disc;
2010 }
2011
2012 work = kcalloc(n, sizeof *work, GFP_KERNEL);
2013 if (!work) {
2014 pr_err("RIO: no memory for work struct\n");
2015 destroy_workqueue(rio_wq);
2016 goto no_disc;
2017 }
2018
2019 n = 0;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002020 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07002021 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002022 if (port->host_deviceid < 0 && port->nscan) {
Alexandre Bounine25747402012-10-10 15:53:59 -07002023 work[n].mport = port;
2024 INIT_WORK(&work[n].work, disc_work_handler);
2025 queue_work(rio_wq, &work[n].work);
2026 n++;
Alexandre Bounine005842e2012-10-04 17:16:08 -07002027 }
2028 }
2029
Alexandre Bounine25747402012-10-10 15:53:59 -07002030 flush_workqueue(rio_wq);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002031 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07002032 pr_debug("RIO: destroy discovery workqueue\n");
2033 destroy_workqueue(rio_wq);
2034 kfree(work);
Matt Porter394b7012005-11-07 01:00:15 -08002035
Alexandre Bounine25747402012-10-10 15:53:59 -07002036no_disc:
Alexandre Bounine2f809982011-03-23 16:43:04 -07002037 rio_init();
2038
Alexandre Bouninec1256eb2011-03-23 16:43:06 -07002039 return 0;
Matt Porter394b7012005-11-07 01:00:15 -08002040}
2041
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002042static int rio_get_hdid(int index)
2043{
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07002044 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002045 return -1;
2046
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07002047 return hdid[index];
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002048}
2049
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002050int rio_mport_initialize(struct rio_mport *mport)
2051{
2052 if (next_portid >= RIO_MAX_MPORTS) {
2053 pr_err("RIO: reached specified max number of mports\n");
2054 return -ENODEV;
2055 }
2056
2057 atomic_set(&mport->state, RIO_DEVICE_INITIALIZING);
2058 mport->id = next_portid++;
2059 mport->host_deviceid = rio_get_hdid(mport->id);
2060 mport->nscan = NULL;
Alexandre Bouninea7b4c632016-03-22 14:26:35 -07002061 mutex_init(&mport->lock);
Alexandre Bounineb6cb95e2016-03-22 14:26:41 -07002062 mport->pwe_refcnt = 0;
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002063
2064 return 0;
2065}
2066EXPORT_SYMBOL_GPL(rio_mport_initialize);
2067
Alexandre Bounine59f99962011-04-14 15:22:14 -07002068int rio_register_mport(struct rio_mport *port)
Matt Porter394b7012005-11-07 01:00:15 -08002069{
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002070 struct rio_scan_node *scan = NULL;
Alexandre Bounine2aaf3082014-04-07 15:38:56 -07002071 int res = 0;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002072
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002073 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002074
2075 /*
2076 * Check if there are any registered enumeration/discovery operations
2077 * that have to be attached to the added mport.
2078 */
2079 list_for_each_entry(scan, &rio_scans, node) {
2080 if (port->id == scan->mport_id ||
2081 scan->mport_id == RIO_MPORT_ANY) {
2082 port->nscan = scan->ops;
2083 if (port->id == scan->mport_id)
2084 break;
2085 }
2086 }
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002087
2088 list_add_tail(&port->node, &rio_mports);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002089 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002090
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002091 dev_set_name(&port->dev, "rapidio%d", port->id);
2092 port->dev.class = &rio_mport_class;
2093 atomic_set(&port->state, RIO_DEVICE_RUNNING);
2094
2095 res = device_register(&port->dev);
2096 if (res)
2097 dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
2098 port->id, res);
2099 else
2100 dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id);
2101
2102 return res;
Matt Porter394b7012005-11-07 01:00:15 -08002103}
Alexandre Bounine94d9bd42013-07-03 15:08:55 -07002104EXPORT_SYMBOL_GPL(rio_register_mport);
Matt Porter394b7012005-11-07 01:00:15 -08002105
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002106static int rio_mport_cleanup_callback(struct device *dev, void *data)
2107{
2108 struct rio_dev *rdev = to_rio_dev(dev);
2109
2110 if (dev->bus == &rio_bus_type)
2111 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
2112 return 0;
2113}
2114
2115static int rio_net_remove_children(struct rio_net *net)
2116{
2117 /*
2118 * Unregister all RapidIO devices residing on this net (this will
2119 * invoke notification of registered subsystem interfaces as well).
2120 */
2121 device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback);
2122 return 0;
2123}
2124
2125int rio_unregister_mport(struct rio_mport *port)
2126{
2127 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
2128
2129 /* Transition mport to the SHUTDOWN state */
2130 if (atomic_cmpxchg(&port->state,
2131 RIO_DEVICE_RUNNING,
2132 RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) {
2133 pr_err("RIO: %s unexpected state transition for mport %s\n",
2134 __func__, port->name);
2135 }
2136
2137 if (port->net && port->net->hport == port) {
2138 rio_net_remove_children(port->net);
2139 rio_free_net(port->net);
2140 }
2141
2142 /*
2143 * Unregister all RapidIO devices attached to this mport (this will
2144 * invoke notification of registered subsystem interfaces as well).
2145 */
2146 mutex_lock(&rio_mport_list_lock);
2147 list_del(&port->node);
2148 mutex_unlock(&rio_mport_list_lock);
2149 device_unregister(&port->dev);
2150
2151 return 0;
2152}
2153EXPORT_SYMBOL_GPL(rio_unregister_mport);
2154
Matt Porter394b7012005-11-07 01:00:15 -08002155EXPORT_SYMBOL_GPL(rio_local_get_device_id);
2156EXPORT_SYMBOL_GPL(rio_get_device);
2157EXPORT_SYMBOL_GPL(rio_get_asm);
2158EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
2159EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
2160EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
2161EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
2162EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
2163EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
2164EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
2165EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002166EXPORT_SYMBOL_GPL(rio_init_mports);