blob: e42f97e9e62a76ed33006919397344c283ff19cf [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 Bouninea11650e2013-05-24 15:55:05 -0700141 * rio_add_device- Adds a RIO device to the device model
142 * @rdev: RIO device
143 *
144 * Adds the RIO device to the global device list and adds the RIO
145 * device to the RIO device list. Creates the generic sysfs nodes
146 * for an RIO device.
147 */
148int rio_add_device(struct rio_dev *rdev)
149{
150 int err;
151
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700152 atomic_set(&rdev->state, RIO_DEVICE_RUNNING);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700153 err = device_register(&rdev->dev);
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700154 if (err)
155 return err;
156
157 spin_lock(&rio_global_list_lock);
158 list_add_tail(&rdev->global_list, &rio_devices);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700159 if (rdev->net) {
160 list_add_tail(&rdev->net_list, &rdev->net->devices);
161 if (rdev->pef & RIO_PEF_SWITCH)
162 list_add_tail(&rdev->rswitch->node,
163 &rdev->net->switches);
164 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700165 spin_unlock(&rio_global_list_lock);
166
167 rio_create_sysfs_dev_files(rdev);
168
169 return 0;
170}
171EXPORT_SYMBOL_GPL(rio_add_device);
172
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700173/*
174 * rio_del_device - removes a RIO device from the device model
175 * @rdev: RIO device
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700176 * @state: device state to set during removal process
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700177 *
178 * Removes the RIO device to the kernel device list and subsystem's device list.
179 * Clears sysfs entries for the removed device.
180 */
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700181void rio_del_device(struct rio_dev *rdev, enum rio_device_state state)
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700182{
183 pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev));
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700184 atomic_set(&rdev->state, state);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700185 spin_lock(&rio_global_list_lock);
186 list_del(&rdev->global_list);
187 if (rdev->net) {
188 list_del(&rdev->net_list);
189 if (rdev->pef & RIO_PEF_SWITCH) {
190 list_del(&rdev->rswitch->node);
191 kfree(rdev->rswitch->route_table);
192 }
193 }
194 spin_unlock(&rio_global_list_lock);
195 rio_remove_sysfs_dev_files(rdev);
196 device_unregister(&rdev->dev);
197}
198EXPORT_SYMBOL_GPL(rio_del_device);
199
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700200/**
Matt Porter394b7012005-11-07 01:00:15 -0800201 * rio_request_inb_mbox - request inbound mailbox service
202 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800203 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800204 * @mbox: Mailbox number to claim
205 * @entries: Number of entries in inbound mailbox queue
206 * @minb: Callback to execute when inbound message is received
207 *
208 * Requests ownership of an inbound mailbox resource and binds
209 * a callback function to the resource. Returns %0 on success.
210 */
211int rio_request_inb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800212 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800213 int mbox,
214 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800215 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
Matt Porter394b7012005-11-07 01:00:15 -0800216 int slot))
217{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700218 int rc = -ENOSYS;
219 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800220
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700221 if (mport->ops->open_inb_mbox == NULL)
222 goto out;
223
Toshi Kani9a975be2016-01-26 21:57:25 +0100224 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800225
226 if (res) {
227 rio_init_mbox_res(res, mbox, mbox);
228
229 /* Make sure this mailbox isn't in use */
230 if ((rc =
231 request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
232 res)) < 0) {
233 kfree(res);
234 goto out;
235 }
236
237 mport->inb_msg[mbox].res = res;
238
239 /* Hook the inbound message callback */
240 mport->inb_msg[mbox].mcback = minb;
241
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700242 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
Matt Porter394b7012005-11-07 01:00:15 -0800243 } else
244 rc = -ENOMEM;
245
246 out:
247 return rc;
248}
249
250/**
251 * rio_release_inb_mbox - release inbound mailbox message service
252 * @mport: RIO master port from which to release the mailbox resource
253 * @mbox: Mailbox number to release
254 *
255 * Releases ownership of an inbound mailbox resource. Returns 0
256 * if the request has been satisfied.
257 */
258int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
259{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700260 if (mport->ops->close_inb_mbox) {
261 mport->ops->close_inb_mbox(mport, mbox);
Matt Porter394b7012005-11-07 01:00:15 -0800262
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700263 /* Release the mailbox resource */
264 return release_resource(mport->inb_msg[mbox].res);
265 } else
266 return -ENOSYS;
Matt Porter394b7012005-11-07 01:00:15 -0800267}
268
269/**
270 * rio_request_outb_mbox - request outbound mailbox service
271 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800272 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800273 * @mbox: Mailbox number to claim
274 * @entries: Number of entries in outbound mailbox queue
275 * @moutb: Callback to execute when outbound message is sent
276 *
277 * Requests ownership of an outbound mailbox resource and binds
278 * a callback function to the resource. Returns 0 on success.
279 */
280int rio_request_outb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800281 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800282 int mbox,
283 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800284 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
Matt Porter394b7012005-11-07 01:00:15 -0800285{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700286 int rc = -ENOSYS;
287 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800288
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700289 if (mport->ops->open_outb_mbox == NULL)
290 goto out;
291
Toshi Kani9a975be2016-01-26 21:57:25 +0100292 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800293
294 if (res) {
295 rio_init_mbox_res(res, mbox, mbox);
296
297 /* Make sure this outbound mailbox isn't in use */
298 if ((rc =
299 request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
300 res)) < 0) {
301 kfree(res);
302 goto out;
303 }
304
305 mport->outb_msg[mbox].res = res;
306
307 /* Hook the inbound message callback */
308 mport->outb_msg[mbox].mcback = moutb;
309
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700310 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
Matt Porter394b7012005-11-07 01:00:15 -0800311 } else
312 rc = -ENOMEM;
313
314 out:
315 return rc;
316}
317
318/**
319 * rio_release_outb_mbox - release outbound mailbox message service
320 * @mport: RIO master port from which to release the mailbox resource
321 * @mbox: Mailbox number to release
322 *
323 * Releases ownership of an inbound mailbox resource. Returns 0
324 * if the request has been satisfied.
325 */
326int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
327{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700328 if (mport->ops->close_outb_mbox) {
329 mport->ops->close_outb_mbox(mport, mbox);
Matt Porter394b7012005-11-07 01:00:15 -0800330
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700331 /* Release the mailbox resource */
332 return release_resource(mport->outb_msg[mbox].res);
333 } else
334 return -ENOSYS;
Matt Porter394b7012005-11-07 01:00:15 -0800335}
336
337/**
338 * rio_setup_inb_dbell - bind inbound doorbell callback
339 * @mport: RIO master port to bind the doorbell callback
Matt Porter6978bbc2005-11-07 01:00:20 -0800340 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800341 * @res: Doorbell message resource
342 * @dinb: Callback to execute when doorbell is received
343 *
344 * Adds a doorbell resource/callback pair into a port's
345 * doorbell event list. Returns 0 if the request has been
346 * satisfied.
347 */
348static int
Matt Porter6978bbc2005-11-07 01:00:20 -0800349rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
350 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
Matt Porter394b7012005-11-07 01:00:15 -0800351 u16 info))
352{
353 int rc = 0;
354 struct rio_dbell *dbell;
355
356 if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
357 rc = -ENOMEM;
358 goto out;
359 }
360
361 dbell->res = res;
362 dbell->dinb = dinb;
Matt Porter6978bbc2005-11-07 01:00:20 -0800363 dbell->dev_id = dev_id;
Matt Porter394b7012005-11-07 01:00:15 -0800364
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700365 mutex_lock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800366 list_add_tail(&dbell->node, &mport->dbells);
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700367 mutex_unlock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800368
369 out:
370 return rc;
371}
372
373/**
374 * rio_request_inb_dbell - request inbound doorbell message service
375 * @mport: RIO master port from which to allocate the doorbell resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800376 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800377 * @start: Doorbell info range start
378 * @end: Doorbell info range end
379 * @dinb: Callback to execute when doorbell is received
380 *
381 * Requests ownership of an inbound doorbell resource and binds
382 * a callback function to the resource. Returns 0 if the request
383 * has been satisfied.
384 */
385int rio_request_inb_dbell(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800386 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800387 u16 start,
388 u16 end,
Matt Porter6978bbc2005-11-07 01:00:20 -0800389 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
Matt Porter394b7012005-11-07 01:00:15 -0800390 u16 dst, u16 info))
391{
392 int rc = 0;
393
Toshi Kani9a975be2016-01-26 21:57:25 +0100394 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800395
396 if (res) {
397 rio_init_dbell_res(res, start, end);
398
399 /* Make sure these doorbells aren't in use */
400 if ((rc =
401 request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
402 res)) < 0) {
403 kfree(res);
404 goto out;
405 }
406
407 /* Hook the doorbell callback */
Matt Porter6978bbc2005-11-07 01:00:20 -0800408 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
Matt Porter394b7012005-11-07 01:00:15 -0800409 } else
410 rc = -ENOMEM;
411
412 out:
413 return rc;
414}
415
416/**
417 * rio_release_inb_dbell - release inbound doorbell message service
418 * @mport: RIO master port from which to release the doorbell resource
419 * @start: Doorbell info range start
420 * @end: Doorbell info range end
421 *
422 * Releases ownership of an inbound doorbell resource and removes
423 * callback from the doorbell event list. Returns 0 if the request
424 * has been satisfied.
425 */
426int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
427{
428 int rc = 0, found = 0;
429 struct rio_dbell *dbell;
430
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700431 mutex_lock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800432 list_for_each_entry(dbell, &mport->dbells, node) {
433 if ((dbell->res->start == start) && (dbell->res->end == end)) {
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700434 list_del(&dbell->node);
Matt Porter394b7012005-11-07 01:00:15 -0800435 found = 1;
436 break;
437 }
438 }
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700439 mutex_unlock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800440
441 /* If we can't find an exact match, fail */
442 if (!found) {
443 rc = -EINVAL;
444 goto out;
445 }
446
Matt Porter394b7012005-11-07 01:00:15 -0800447 /* Release the doorbell resource */
448 rc = release_resource(dbell->res);
449
450 /* Free the doorbell event */
451 kfree(dbell);
452
453 out:
454 return rc;
455}
456
457/**
458 * rio_request_outb_dbell - request outbound doorbell message range
459 * @rdev: RIO device from which to allocate the doorbell resource
460 * @start: Doorbell message range start
461 * @end: Doorbell message range end
462 *
463 * Requests ownership of a doorbell message range. Returns a resource
464 * if the request has been satisfied or %NULL on failure.
465 */
466struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
467 u16 end)
468{
Toshi Kani9a975be2016-01-26 21:57:25 +0100469 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800470
471 if (res) {
472 rio_init_dbell_res(res, start, end);
473
474 /* Make sure these doorbells aren't in use */
475 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
476 < 0) {
477 kfree(res);
478 res = NULL;
479 }
480 }
481
482 return res;
483}
484
485/**
486 * rio_release_outb_dbell - release outbound doorbell message range
487 * @rdev: RIO device from which to release the doorbell resource
488 * @res: Doorbell resource to be freed
489 *
490 * Releases ownership of a doorbell message range. Returns 0 if the
491 * request has been satisfied.
492 */
493int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
494{
495 int rc = release_resource(res);
496
497 kfree(res);
498
499 return rc;
500}
501
502/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700503 * rio_request_inb_pwrite - request inbound port-write message service
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700504 * @rdev: RIO device to which register inbound port-write callback routine
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700505 * @pwcback: Callback routine to execute when port-write is received
506 *
507 * Binds a port-write callback function to the RapidIO device.
508 * Returns 0 if the request has been satisfied.
509 */
510int rio_request_inb_pwrite(struct rio_dev *rdev,
511 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
512{
513 int rc = 0;
514
515 spin_lock(&rio_global_list_lock);
516 if (rdev->pwcback != NULL)
517 rc = -ENOMEM;
518 else
519 rdev->pwcback = pwcback;
520
521 spin_unlock(&rio_global_list_lock);
522 return rc;
523}
524EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
525
526/**
527 * rio_release_inb_pwrite - release inbound port-write message service
528 * @rdev: RIO device which registered for inbound port-write callback
529 *
530 * Removes callback from the rio_dev structure. Returns 0 if the request
531 * has been satisfied.
532 */
533int rio_release_inb_pwrite(struct rio_dev *rdev)
534{
535 int rc = -ENOMEM;
536
537 spin_lock(&rio_global_list_lock);
538 if (rdev->pwcback) {
539 rdev->pwcback = NULL;
540 rc = 0;
541 }
542
543 spin_unlock(&rio_global_list_lock);
544 return rc;
545}
546EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
547
548/**
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700549 * rio_map_inb_region -- Map inbound memory region.
550 * @mport: Master port.
Randy Dunlap2ca3cb52012-11-16 14:14:56 -0800551 * @local: physical address of memory region to be mapped
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700552 * @rbase: RIO base address assigned to this window
553 * @size: Size of the memory region
554 * @rflags: Flags for mapping.
555 *
556 * Return: 0 -- Success.
557 *
558 * This function will create the mapping from RIO space to local memory.
559 */
560int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
561 u64 rbase, u32 size, u32 rflags)
562{
563 int rc = 0;
564 unsigned long flags;
565
566 if (!mport->ops->map_inb)
567 return -1;
568 spin_lock_irqsave(&rio_mmap_lock, flags);
569 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
570 spin_unlock_irqrestore(&rio_mmap_lock, flags);
571 return rc;
572}
573EXPORT_SYMBOL_GPL(rio_map_inb_region);
574
575/**
576 * rio_unmap_inb_region -- Unmap the inbound memory region
577 * @mport: Master port
578 * @lstart: physical address of memory region to be unmapped
579 */
580void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
581{
582 unsigned long flags;
583 if (!mport->ops->unmap_inb)
584 return;
585 spin_lock_irqsave(&rio_mmap_lock, flags);
586 mport->ops->unmap_inb(mport, lstart);
587 spin_unlock_irqrestore(&rio_mmap_lock, flags);
588}
589EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
590
591/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700592 * rio_mport_get_physefb - Helper function that returns register offset
593 * for Physical Layer Extended Features Block.
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700594 * @port: Master port to issue transaction
595 * @local: Indicate a local master port or remote device access
596 * @destid: Destination ID of the device
597 * @hopcount: Number of switch hops to the device
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700598 */
599u32
600rio_mport_get_physefb(struct rio_mport *port, int local,
601 u16 destid, u8 hopcount)
602{
603 u32 ext_ftr_ptr;
604 u32 ftr_header;
605
606 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
607
608 while (ext_ftr_ptr) {
609 if (local)
610 rio_local_read_config_32(port, ext_ftr_ptr,
611 &ftr_header);
612 else
613 rio_mport_read_config_32(port, destid, hopcount,
614 ext_ftr_ptr, &ftr_header);
615
616 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
617 switch (ftr_header) {
618
619 case RIO_EFB_SER_EP_ID_V13P:
620 case RIO_EFB_SER_EP_REC_ID_V13P:
621 case RIO_EFB_SER_EP_FREE_ID_V13P:
622 case RIO_EFB_SER_EP_ID:
623 case RIO_EFB_SER_EP_REC_ID:
624 case RIO_EFB_SER_EP_FREE_ID:
625 case RIO_EFB_SER_EP_FREC_ID:
626
627 return ext_ftr_ptr;
628
629 default:
630 break;
631 }
632
633 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
634 hopcount, ext_ftr_ptr);
635 }
636
637 return ext_ftr_ptr;
638}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700639EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700640
641/**
642 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700643 * @comp_tag: RIO component tag to match
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700644 * @from: Previous RIO device found in search, or %NULL for new search
645 *
646 * Iterates through the list of known RIO devices. If a RIO device is
647 * found with a matching @comp_tag, a pointer to its device
648 * structure is returned. Otherwise, %NULL is returned. A new search
649 * is initiated by passing %NULL to the @from argument. Otherwise, if
650 * @from is not %NULL, searches continue from next device on the global
651 * list.
652 */
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700653struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700654{
655 struct list_head *n;
656 struct rio_dev *rdev;
657
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700658 spin_lock(&rio_global_list_lock);
659 n = from ? from->global_list.next : rio_devices.next;
660
661 while (n && (n != &rio_devices)) {
662 rdev = rio_dev_g(n);
663 if (rdev->comp_tag == comp_tag)
664 goto exit;
665 n = n->next;
666 }
667 rdev = NULL;
668exit:
669 spin_unlock(&rio_global_list_lock);
670 return rdev;
671}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700672EXPORT_SYMBOL_GPL(rio_get_comptag);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700673
674/**
675 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
676 * @rdev: Pointer to RIO device control structure
677 * @pnum: Switch port number to set LOCKOUT bit
678 * @lock: Operation : set (=1) or clear (=0)
679 */
680int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
681{
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700682 u32 regval;
683
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800684 rio_read_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700685 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
686 &regval);
687 if (lock)
688 regval |= RIO_PORT_N_CTL_LOCKOUT;
689 else
690 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
691
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800692 rio_write_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700693 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
694 regval);
695 return 0;
696}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700697EXPORT_SYMBOL_GPL(rio_set_port_lockout);
698
699/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700700 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
701 * given port
702 * @port: Master port associated with the RIO network
703 * @local: local=1 select local port otherwise a far device is reached
704 * @destid: Destination ID of the device to check host bit
705 * @hopcount: Number of hops to reach the target
706 * @port_num: Port (-number on switch) to enable on a far end device
707 *
708 * Returns 0 or 1 from on General Control Command and Status Register
709 * (EXT_PTR+0x3C)
710 */
711int rio_enable_rx_tx_port(struct rio_mport *port,
712 int local, u16 destid,
713 u8 hopcount, u8 port_num)
714{
715#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
716 u32 regval;
717 u32 ext_ftr_ptr;
718
719 /*
720 * enable rx input tx output port
721 */
722 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
723 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
724
725 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
726
727 if (local) {
728 rio_local_read_config_32(port, ext_ftr_ptr +
729 RIO_PORT_N_CTL_CSR(0),
730 &regval);
731 } else {
732 if (rio_mport_read_config_32(port, destid, hopcount,
733 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), &regval) < 0)
734 return -EIO;
735 }
736
737 if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
738 /* serial */
739 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
740 | RIO_PORT_N_CTL_EN_TX_SER;
741 } else {
742 /* parallel */
743 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
744 | RIO_PORT_N_CTL_EN_TX_PAR;
745 }
746
747 if (local) {
748 rio_local_write_config_32(port, ext_ftr_ptr +
749 RIO_PORT_N_CTL_CSR(0), regval);
750 } else {
751 if (rio_mport_write_config_32(port, destid, hopcount,
752 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
753 return -EIO;
754 }
755#endif
756 return 0;
757}
758EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
759
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700760
761/**
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700762 * rio_chk_dev_route - Validate route to the specified device.
763 * @rdev: RIO device failed to respond
764 * @nrdev: Last active device on the route to rdev
765 * @npnum: nrdev's port number on the route to rdev
766 *
767 * Follows a route to the specified RIO device to determine the last available
768 * device (and corresponding RIO port) on the route.
769 */
770static int
771rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
772{
773 u32 result;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800774 int p_port, rc = -EIO;
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700775 struct rio_dev *prev = NULL;
776
777 /* Find switch with failed RIO link */
778 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
779 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
780 prev = rdev->prev;
781 break;
782 }
783 rdev = rdev->prev;
784 }
785
786 if (prev == NULL)
787 goto err_out;
788
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800789 p_port = prev->rswitch->route_table[rdev->destid];
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700790
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700791 if (p_port != RIO_INVALID_ROUTE) {
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700792 pr_debug("RIO: link failed on [%s]-P%d\n",
793 rio_name(prev), p_port);
794 *nrdev = prev;
795 *npnum = p_port;
796 rc = 0;
797 } else
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700798 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700799err_out:
800 return rc;
801}
802
803/**
804 * rio_mport_chk_dev_access - Validate access to the specified device.
805 * @mport: Master port to send transactions
806 * @destid: Device destination ID in network
807 * @hopcount: Number of hops into the network
808 */
Alexandre Bouninee274e0e2010-10-27 15:34:32 -0700809int
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700810rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
811{
812 int i = 0;
813 u32 tmp;
814
815 while (rio_mport_read_config_32(mport, destid, hopcount,
816 RIO_DEV_ID_CAR, &tmp)) {
817 i++;
818 if (i == RIO_MAX_CHK_RETRY)
819 return -EIO;
820 mdelay(1);
821 }
822
823 return 0;
824}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700825EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700826
827/**
828 * rio_chk_dev_access - Validate access to the specified device.
829 * @rdev: Pointer to RIO device control structure
830 */
831static int rio_chk_dev_access(struct rio_dev *rdev)
832{
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800833 return rio_mport_chk_dev_access(rdev->net->hport,
834 rdev->destid, rdev->hopcount);
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700835}
836
837/**
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700838 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
839 * returns link-response (if requested).
840 * @rdev: RIO devive to issue Input-status command
841 * @pnum: Device port number to issue the command
842 * @lnkresp: Response from a link partner
843 */
844static int
845rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
846{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700847 u32 regval;
848 int checkcount;
849
850 if (lnkresp) {
851 /* Read from link maintenance response register
852 * to clear valid bit */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800853 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700854 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
855 &regval);
856 udelay(50);
857 }
858
859 /* Issue Input-status command */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800860 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700861 rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
862 RIO_MNT_REQ_CMD_IS);
863
864 /* Exit if the response is not expected */
865 if (lnkresp == NULL)
866 return 0;
867
868 checkcount = 3;
869 while (checkcount--) {
870 udelay(50);
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800871 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700872 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
873 &regval);
874 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
875 *lnkresp = regval;
876 return 0;
877 }
878 }
879
880 return -EIO;
881}
882
883/**
884 * rio_clr_err_stopped - Clears port Error-stopped states.
885 * @rdev: Pointer to RIO device control structure
886 * @pnum: Switch port number to clear errors
887 * @err_status: port error status (if 0 reads register from device)
888 */
889static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
890{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700891 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
892 u32 regval;
893 u32 far_ackid, far_linkstat, near_ackid;
894
895 if (err_status == 0)
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800896 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700897 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
898 &err_status);
899
900 if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
901 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
902 /*
903 * Send a Link-Request/Input-Status control symbol
904 */
905 if (rio_get_input_status(rdev, pnum, &regval)) {
906 pr_debug("RIO_EM: Input-status response timeout\n");
907 goto rd_err;
908 }
909
910 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
911 pnum, regval);
912 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
913 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800914 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700915 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
916 &regval);
917 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
918 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
919 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
920 " near_ackID=0x%02x\n",
921 pnum, far_ackid, far_linkstat, near_ackid);
922
923 /*
924 * If required, synchronize ackIDs of near and
925 * far sides.
926 */
927 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
928 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
929 /* Align near outstanding/outbound ackIDs with
930 * far inbound.
931 */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800932 rio_write_config_32(rdev,
933 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700934 (near_ackid << 24) |
935 (far_ackid << 8) | far_ackid);
936 /* Align far outstanding/outbound ackIDs with
937 * near inbound.
938 */
939 far_ackid++;
940 if (nextdev)
941 rio_write_config_32(nextdev,
942 nextdev->phys_efptr +
943 RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
944 (far_ackid << 24) |
945 (near_ackid << 8) | near_ackid);
946 else
947 pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
948 }
949rd_err:
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800950 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700951 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
952 &err_status);
953 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
954 }
955
956 if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
957 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
958 rio_get_input_status(nextdev,
959 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
960 udelay(50);
961
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800962 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700963 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
964 &err_status);
965 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
966 }
967
968 return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
969 RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
970}
971
972/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700973 * rio_inb_pwrite_handler - process inbound port-write message
974 * @pw_msg: pointer to inbound port-write message
975 *
976 * Processes an inbound port-write message. Returns 0 if the request
977 * has been satisfied.
978 */
979int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
980{
981 struct rio_dev *rdev;
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700982 u32 err_status, em_perrdet, em_ltlerrdet;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700983 int rc, portnum;
984
Alexandre Bouninee6536922011-01-12 17:00:40 -0800985 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700986 if (rdev == NULL) {
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700987 /* Device removed or enumeration error */
988 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700989 __func__, pw_msg->em.comptag);
990 return -EIO;
991 }
992
993 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
994
995#ifdef DEBUG_PW
996 {
997 u32 i;
998 for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700999 pr_debug("0x%02x: %08x %08x %08x %08x\n",
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001000 i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
1001 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
1002 i += 4;
1003 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001004 }
1005#endif
1006
1007 /* Call an external service function (if such is registered
1008 * for this device). This may be the service for endpoints that send
1009 * device-specific port-write messages. End-point messages expected
1010 * to be handled completely by EP specific device driver.
1011 * For switches rc==0 signals that no standard processing required.
1012 */
1013 if (rdev->pwcback != NULL) {
1014 rc = rdev->pwcback(rdev, pw_msg, 0);
1015 if (rc == 0)
1016 return 0;
1017 }
1018
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001019 portnum = pw_msg->em.is_port & 0xFF;
1020
1021 /* Check if device and route to it are functional:
1022 * Sometimes devices may send PW message(s) just before being
1023 * powered down (or link being lost).
1024 */
1025 if (rio_chk_dev_access(rdev)) {
1026 pr_debug("RIO: device access failed - get link partner\n");
1027 /* Scan route to the device and identify failed link.
1028 * This will replace device and port reported in PW message.
1029 * PW message should not be used after this point.
1030 */
1031 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
1032 pr_err("RIO: Route trace for %s failed\n",
1033 rio_name(rdev));
1034 return -EIO;
1035 }
1036 pw_msg = NULL;
1037 }
1038
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001039 /* For End-point devices processing stops here */
1040 if (!(rdev->pef & RIO_PEF_SWITCH))
1041 return 0;
1042
1043 if (rdev->phys_efptr == 0) {
1044 pr_err("RIO_PW: Bad switch initialization for %s\n",
1045 rio_name(rdev));
1046 return 0;
1047 }
1048
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001049 /*
1050 * Process the port-write notification from switch
1051 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001052 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
1053 rdev->rswitch->ops->em_handle(rdev, portnum);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001054
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001055 rio_read_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001056 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
1057 &err_status);
1058 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
1059
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001060 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001061
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001062 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
1063 rdev->rswitch->port_ok |= (1 << portnum);
1064 rio_set_port_lockout(rdev, portnum, 0);
1065 /* Schedule Insertion Service */
1066 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
1067 rio_name(rdev), portnum);
1068 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001069
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001070 /* Clear error-stopped states (if reported).
1071 * Depending on the link partner state, two attempts
1072 * may be needed for successful recovery.
1073 */
1074 if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
1075 RIO_PORT_N_ERR_STS_PW_INP_ES)) {
1076 if (rio_clr_err_stopped(rdev, portnum, err_status))
1077 rio_clr_err_stopped(rdev, portnum, 0);
1078 }
1079 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001080
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001081 if (rdev->rswitch->port_ok & (1 << portnum)) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001082 rdev->rswitch->port_ok &= ~(1 << portnum);
1083 rio_set_port_lockout(rdev, portnum, 1);
1084
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001085 rio_write_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001086 rdev->phys_efptr +
1087 RIO_PORT_N_ACK_STS_CSR(portnum),
1088 RIO_PORT_N_ACK_CLEAR);
1089
1090 /* Schedule Extraction Service */
1091 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
1092 rio_name(rdev), portnum);
1093 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001094 }
1095
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001096 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001097 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
1098 if (em_perrdet) {
1099 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
1100 portnum, em_perrdet);
1101 /* Clear EM Port N Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001102 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001103 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
1104 }
1105
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001106 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001107 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1108 if (em_ltlerrdet) {
1109 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1110 em_ltlerrdet);
1111 /* Clear EM L/T Layer Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001112 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001113 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1114 }
1115
Alexandre Bounine388c45c2010-10-27 15:34:35 -07001116 /* Clear remaining error bits and Port-Write Pending bit */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001117 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001118 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
Alexandre Bounine388c45c2010-10-27 15:34:35 -07001119 err_status);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001120
1121 return 0;
1122}
1123EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1124
1125/**
1126 * rio_mport_get_efb - get pointer to next extended features block
1127 * @port: Master port to issue transaction
1128 * @local: Indicate a local master port or remote device access
1129 * @destid: Destination ID of the device
1130 * @hopcount: Number of switch hops to the device
1131 * @from: Offset of current Extended Feature block header (if 0 starts
1132 * from ExtFeaturePtr)
1133 */
1134u32
1135rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1136 u8 hopcount, u32 from)
1137{
1138 u32 reg_val;
1139
1140 if (from == 0) {
1141 if (local)
1142 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1143 &reg_val);
1144 else
1145 rio_mport_read_config_32(port, destid, hopcount,
1146 RIO_ASM_INFO_CAR, &reg_val);
1147 return reg_val & RIO_EXT_FTR_PTR_MASK;
1148 } else {
1149 if (local)
1150 rio_local_read_config_32(port, from, &reg_val);
1151 else
1152 rio_mport_read_config_32(port, destid, hopcount,
1153 from, &reg_val);
1154 return RIO_GET_BLOCK_ID(reg_val);
1155 }
1156}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001157EXPORT_SYMBOL_GPL(rio_mport_get_efb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001158
1159/**
Matt Porter394b7012005-11-07 01:00:15 -08001160 * rio_mport_get_feature - query for devices' extended features
1161 * @port: Master port to issue transaction
1162 * @local: Indicate a local master port or remote device access
1163 * @destid: Destination ID of the device
1164 * @hopcount: Number of switch hops to the device
1165 * @ftr: Extended feature code
1166 *
1167 * Tell if a device supports a given RapidIO capability.
1168 * Returns the offset of the requested extended feature
1169 * block within the device's RIO configuration space or
1170 * 0 in case the device does not support it. Possible
1171 * values for @ftr:
1172 *
1173 * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
1174 *
1175 * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
1176 *
1177 * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
1178 *
1179 * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
1180 *
1181 * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
1182 *
1183 * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
1184 */
1185u32
1186rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1187 u8 hopcount, int ftr)
1188{
1189 u32 asm_info, ext_ftr_ptr, ftr_header;
1190
1191 if (local)
1192 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1193 else
1194 rio_mport_read_config_32(port, destid, hopcount,
1195 RIO_ASM_INFO_CAR, &asm_info);
1196
1197 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1198
1199 while (ext_ftr_ptr) {
1200 if (local)
1201 rio_local_read_config_32(port, ext_ftr_ptr,
1202 &ftr_header);
1203 else
1204 rio_mport_read_config_32(port, destid, hopcount,
1205 ext_ftr_ptr, &ftr_header);
1206 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1207 return ext_ftr_ptr;
1208 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
1209 break;
1210 }
1211
1212 return 0;
1213}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001214EXPORT_SYMBOL_GPL(rio_mport_get_feature);
Matt Porter394b7012005-11-07 01:00:15 -08001215
1216/**
1217 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
1218 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1219 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1220 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
1221 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
1222 * @from: Previous RIO device found in search, or %NULL for new search
1223 *
1224 * Iterates through the list of known RIO devices. If a RIO device is
1225 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
1226 * count to the device is incrememted and a pointer to its device
1227 * structure is returned. Otherwise, %NULL is returned. A new search
1228 * is initiated by passing %NULL to the @from argument. Otherwise, if
1229 * @from is not %NULL, searches continue from next device on the global
1230 * list. The reference count for @from is always decremented if it is
1231 * not %NULL.
1232 */
1233struct rio_dev *rio_get_asm(u16 vid, u16 did,
1234 u16 asm_vid, u16 asm_did, struct rio_dev *from)
1235{
1236 struct list_head *n;
1237 struct rio_dev *rdev;
1238
1239 WARN_ON(in_interrupt());
1240 spin_lock(&rio_global_list_lock);
1241 n = from ? from->global_list.next : rio_devices.next;
1242
1243 while (n && (n != &rio_devices)) {
1244 rdev = rio_dev_g(n);
1245 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1246 (did == RIO_ANY_ID || rdev->did == did) &&
1247 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1248 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1249 goto exit;
1250 n = n->next;
1251 }
1252 rdev = NULL;
1253 exit:
1254 rio_dev_put(from);
1255 rdev = rio_dev_get(rdev);
1256 spin_unlock(&rio_global_list_lock);
1257 return rdev;
1258}
1259
1260/**
1261 * rio_get_device - Begin or continue searching for a RIO device by vid/did
1262 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1263 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1264 * @from: Previous RIO device found in search, or %NULL for new search
1265 *
1266 * Iterates through the list of known RIO devices. If a RIO device is
1267 * found with a matching @vid and @did, the reference count to the
1268 * device is incrememted and a pointer to its device structure is returned.
1269 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
1270 * to the @from argument. Otherwise, if @from is not %NULL, searches
1271 * continue from next device on the global list. The reference count for
1272 * @from is always decremented if it is not %NULL.
1273 */
1274struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1275{
1276 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1277}
1278
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001279/**
1280 * rio_std_route_add_entry - Add switch route table entry using standard
1281 * registers defined in RIO specification rev.1.3
1282 * @mport: Master port to issue transaction
1283 * @destid: Destination ID of the device
1284 * @hopcount: Number of switch hops to the device
1285 * @table: routing table ID (global or port-specific)
1286 * @route_destid: destID entry in the RT
1287 * @route_port: destination port for specified destID
1288 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001289static int
1290rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1291 u16 table, u16 route_destid, u8 route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001292{
1293 if (table == RIO_GLOBAL_TABLE) {
1294 rio_mport_write_config_32(mport, destid, hopcount,
1295 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1296 (u32)route_destid);
1297 rio_mport_write_config_32(mport, destid, hopcount,
1298 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1299 (u32)route_port);
1300 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001301
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001302 udelay(10);
1303 return 0;
1304}
1305
1306/**
1307 * rio_std_route_get_entry - Read switch route table entry (port number)
Uwe Kleine-König638c5942010-06-11 12:16:58 +02001308 * associated with specified destID using standard registers defined in RIO
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001309 * specification rev.1.3
1310 * @mport: Master port to issue transaction
1311 * @destid: Destination ID of the device
1312 * @hopcount: Number of switch hops to the device
1313 * @table: routing table ID (global or port-specific)
1314 * @route_destid: destID entry in the RT
1315 * @route_port: returned destination port for specified destID
1316 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001317static int
1318rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1319 u16 table, u16 route_destid, u8 *route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001320{
1321 u32 result;
1322
1323 if (table == RIO_GLOBAL_TABLE) {
1324 rio_mport_write_config_32(mport, destid, hopcount,
1325 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1326 rio_mport_read_config_32(mport, destid, hopcount,
1327 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1328
1329 *route_port = (u8)result;
1330 }
1331
1332 return 0;
1333}
1334
1335/**
1336 * rio_std_route_clr_table - Clear swotch route table using standard registers
1337 * defined in RIO specification rev.1.3.
1338 * @mport: Master port to issue transaction
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001339 * @destid: Destination ID of the device
1340 * @hopcount: Number of switch hops to the device
1341 * @table: routing table ID (global or port-specific)
1342 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001343static int
1344rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1345 u16 table)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001346{
1347 u32 max_destid = 0xff;
1348 u32 i, pef, id_inc = 1, ext_cfg = 0;
1349 u32 port_sel = RIO_INVALID_ROUTE;
1350
1351 if (table == RIO_GLOBAL_TABLE) {
1352 rio_mport_read_config_32(mport, destid, hopcount,
1353 RIO_PEF_CAR, &pef);
1354
1355 if (mport->sys_size) {
1356 rio_mport_read_config_32(mport, destid, hopcount,
1357 RIO_SWITCH_RT_LIMIT,
1358 &max_destid);
1359 max_destid &= RIO_RT_MAX_DESTID;
1360 }
1361
1362 if (pef & RIO_PEF_EXT_RT) {
1363 ext_cfg = 0x80000000;
1364 id_inc = 4;
1365 port_sel = (RIO_INVALID_ROUTE << 24) |
1366 (RIO_INVALID_ROUTE << 16) |
1367 (RIO_INVALID_ROUTE << 8) |
1368 RIO_INVALID_ROUTE;
1369 }
1370
1371 for (i = 0; i <= max_destid;) {
1372 rio_mport_write_config_32(mport, destid, hopcount,
1373 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1374 ext_cfg | i);
1375 rio_mport_write_config_32(mport, destid, hopcount,
1376 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1377 port_sel);
1378 i += id_inc;
1379 }
1380 }
1381
1382 udelay(10);
1383 return 0;
1384}
1385
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001386/**
1387 * rio_lock_device - Acquires host device lock for specified device
1388 * @port: Master port to send transaction
1389 * @destid: Destination ID for device/switch
1390 * @hopcount: Hopcount to reach switch
1391 * @wait_ms: Max wait time in msec (0 = no timeout)
1392 *
1393 * Attepts to acquire host device lock for specified device
1394 * Returns 0 if device lock acquired or EINVAL if timeout expires.
1395 */
1396int rio_lock_device(struct rio_mport *port, u16 destid,
1397 u8 hopcount, int wait_ms)
1398{
1399 u32 result;
1400 int tcnt = 0;
1401
1402 /* Attempt to acquire device lock */
1403 rio_mport_write_config_32(port, destid, hopcount,
1404 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1405 rio_mport_read_config_32(port, destid, hopcount,
1406 RIO_HOST_DID_LOCK_CSR, &result);
1407
1408 while (result != port->host_deviceid) {
1409 if (wait_ms != 0 && tcnt == wait_ms) {
1410 pr_debug("RIO: timeout when locking device %x:%x\n",
1411 destid, hopcount);
1412 return -EINVAL;
1413 }
1414
1415 /* Delay a bit */
1416 mdelay(1);
1417 tcnt++;
1418 /* Try to acquire device lock again */
1419 rio_mport_write_config_32(port, destid,
1420 hopcount,
1421 RIO_HOST_DID_LOCK_CSR,
1422 port->host_deviceid);
1423 rio_mport_read_config_32(port, destid,
1424 hopcount,
1425 RIO_HOST_DID_LOCK_CSR, &result);
1426 }
1427
1428 return 0;
1429}
1430EXPORT_SYMBOL_GPL(rio_lock_device);
1431
1432/**
1433 * rio_unlock_device - Releases host device lock for specified device
1434 * @port: Master port to send transaction
1435 * @destid: Destination ID for device/switch
1436 * @hopcount: Hopcount to reach switch
1437 *
1438 * Returns 0 if device lock released or EINVAL if fails.
1439 */
1440int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1441{
1442 u32 result;
1443
1444 /* Release device lock */
1445 rio_mport_write_config_32(port, destid,
1446 hopcount,
1447 RIO_HOST_DID_LOCK_CSR,
1448 port->host_deviceid);
1449 rio_mport_read_config_32(port, destid, hopcount,
1450 RIO_HOST_DID_LOCK_CSR, &result);
1451 if ((result & 0xffff) != 0xffff) {
1452 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1453 destid, hopcount);
1454 return -EINVAL;
1455 }
1456
1457 return 0;
1458}
1459EXPORT_SYMBOL_GPL(rio_unlock_device);
1460
1461/**
1462 * rio_route_add_entry- Add a route entry to a switch routing table
1463 * @rdev: RIO device
1464 * @table: Routing table ID
1465 * @route_destid: Destination ID to be routed
1466 * @route_port: Port number to be routed
1467 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1468 *
1469 * If available calls the switch specific add_entry() method to add a route
1470 * entry into a switch routing table. Otherwise uses standard RT update method
1471 * as defined by RapidIO specification. A specific routing table can be selected
1472 * using the @table argument if a switch has per port routing tables or
1473 * the standard (or global) table may be used by passing
1474 * %RIO_GLOBAL_TABLE in @table.
1475 *
1476 * Returns %0 on success or %-EINVAL on failure.
1477 */
1478int rio_route_add_entry(struct rio_dev *rdev,
1479 u16 table, u16 route_destid, u8 route_port, int lock)
1480{
1481 int rc = -EINVAL;
1482 struct rio_switch_ops *ops = rdev->rswitch->ops;
1483
1484 if (lock) {
1485 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1486 rdev->hopcount, 1000);
1487 if (rc)
1488 return rc;
1489 }
1490
1491 spin_lock(&rdev->rswitch->lock);
1492
1493 if (ops == NULL || ops->add_entry == NULL) {
1494 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1495 rdev->hopcount, table,
1496 route_destid, route_port);
1497 } else if (try_module_get(ops->owner)) {
1498 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1499 rdev->hopcount, table, route_destid,
1500 route_port);
1501 module_put(ops->owner);
1502 }
1503
1504 spin_unlock(&rdev->rswitch->lock);
1505
1506 if (lock)
1507 rio_unlock_device(rdev->net->hport, rdev->destid,
1508 rdev->hopcount);
1509
1510 return rc;
1511}
1512EXPORT_SYMBOL_GPL(rio_route_add_entry);
1513
1514/**
1515 * rio_route_get_entry- Read an entry from a switch routing table
1516 * @rdev: RIO device
1517 * @table: Routing table ID
1518 * @route_destid: Destination ID to be routed
1519 * @route_port: Pointer to read port number into
1520 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1521 *
1522 * If available calls the switch specific get_entry() method to fetch a route
1523 * entry from a switch routing table. Otherwise uses standard RT read method
1524 * as defined by RapidIO specification. A specific routing table can be selected
1525 * using the @table argument if a switch has per port routing tables or
1526 * the standard (or global) table may be used by passing
1527 * %RIO_GLOBAL_TABLE in @table.
1528 *
1529 * Returns %0 on success or %-EINVAL on failure.
1530 */
1531int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1532 u16 route_destid, u8 *route_port, int lock)
1533{
1534 int rc = -EINVAL;
1535 struct rio_switch_ops *ops = rdev->rswitch->ops;
1536
1537 if (lock) {
1538 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1539 rdev->hopcount, 1000);
1540 if (rc)
1541 return rc;
1542 }
1543
1544 spin_lock(&rdev->rswitch->lock);
1545
1546 if (ops == NULL || ops->get_entry == NULL) {
1547 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1548 rdev->hopcount, table,
1549 route_destid, route_port);
1550 } else if (try_module_get(ops->owner)) {
1551 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1552 rdev->hopcount, table, route_destid,
1553 route_port);
1554 module_put(ops->owner);
1555 }
1556
1557 spin_unlock(&rdev->rswitch->lock);
1558
1559 if (lock)
1560 rio_unlock_device(rdev->net->hport, rdev->destid,
1561 rdev->hopcount);
1562 return rc;
1563}
1564EXPORT_SYMBOL_GPL(rio_route_get_entry);
1565
1566/**
1567 * rio_route_clr_table - Clear a switch routing table
1568 * @rdev: RIO device
1569 * @table: Routing table ID
1570 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1571 *
1572 * If available calls the switch specific clr_table() method to clear a switch
1573 * routing table. Otherwise uses standard RT write method as defined by RapidIO
1574 * specification. A specific routing table can be selected using the @table
1575 * argument if a switch has per port routing tables or the standard (or global)
1576 * table may be used by passing %RIO_GLOBAL_TABLE in @table.
1577 *
1578 * Returns %0 on success or %-EINVAL on failure.
1579 */
1580int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1581{
1582 int rc = -EINVAL;
1583 struct rio_switch_ops *ops = rdev->rswitch->ops;
1584
1585 if (lock) {
1586 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1587 rdev->hopcount, 1000);
1588 if (rc)
1589 return rc;
1590 }
1591
1592 spin_lock(&rdev->rswitch->lock);
1593
1594 if (ops == NULL || ops->clr_table == NULL) {
1595 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1596 rdev->hopcount, table);
1597 } else if (try_module_get(ops->owner)) {
1598 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1599 rdev->hopcount, table);
1600
1601 module_put(ops->owner);
1602 }
1603
1604 spin_unlock(&rdev->rswitch->lock);
1605
1606 if (lock)
1607 rio_unlock_device(rdev->net->hport, rdev->destid,
1608 rdev->hopcount);
1609
1610 return rc;
1611}
1612EXPORT_SYMBOL_GPL(rio_route_clr_table);
1613
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001614#ifdef CONFIG_RAPIDIO_DMA_ENGINE
1615
1616static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1617{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001618 struct rio_mport *mport = arg;
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001619
1620 /* Check that DMA device belongs to the right MPORT */
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001621 return mport == container_of(chan->device, struct rio_mport, dma);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001622}
1623
1624/**
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001625 * rio_request_mport_dma - request RapidIO capable DMA channel associated
1626 * with specified local RapidIO mport device.
1627 * @mport: RIO mport to perform DMA data transfers
1628 *
1629 * Returns pointer to allocated DMA channel or NULL if failed.
1630 */
1631struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
1632{
1633 dma_cap_mask_t mask;
1634
1635 dma_cap_zero(mask);
1636 dma_cap_set(DMA_SLAVE, mask);
1637 return dma_request_channel(mask, rio_chan_filter, mport);
1638}
1639EXPORT_SYMBOL_GPL(rio_request_mport_dma);
1640
1641/**
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001642 * rio_request_dma - request RapidIO capable DMA channel that supports
1643 * specified target RapidIO device.
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001644 * @rdev: RIO device associated with DMA transfer
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001645 *
1646 * Returns pointer to allocated DMA channel or NULL if failed.
1647 */
1648struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1649{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001650 return rio_request_mport_dma(rdev->net->hport);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001651}
1652EXPORT_SYMBOL_GPL(rio_request_dma);
1653
1654/**
1655 * rio_release_dma - release specified DMA channel
1656 * @dchan: DMA channel to release
1657 */
1658void rio_release_dma(struct dma_chan *dchan)
1659{
1660 dma_release_channel(dchan);
1661}
1662EXPORT_SYMBOL_GPL(rio_release_dma);
1663
1664/**
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001665 * rio_dma_prep_xfer - RapidIO specific wrapper
1666 * for device_prep_slave_sg callback defined by DMAENGINE.
1667 * @dchan: DMA channel to configure
1668 * @destid: target RapidIO device destination ID
1669 * @data: RIO specific data descriptor
1670 * @direction: DMA data transfer direction (TO or FROM the device)
1671 * @flags: dmaengine defined flags
1672 *
1673 * Initializes RapidIO capable DMA channel for the specified data transfer.
1674 * Uses DMA channel private extension to pass information related to remote
1675 * target RIO device.
1676 * Returns pointer to DMA transaction descriptor or NULL if failed.
1677 */
1678struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
1679 u16 destid, struct rio_dma_data *data,
1680 enum dma_transfer_direction direction, unsigned long flags)
1681{
1682 struct rio_dma_ext rio_ext;
1683
1684 if (dchan->device->device_prep_slave_sg == NULL) {
1685 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1686 return NULL;
1687 }
1688
1689 rio_ext.destid = destid;
1690 rio_ext.rio_addr_u = data->rio_addr_u;
1691 rio_ext.rio_addr = data->rio_addr;
1692 rio_ext.wr_type = data->wr_type;
1693
1694 return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1695 direction, flags, &rio_ext);
1696}
1697EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
1698
1699/**
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001700 * rio_dma_prep_slave_sg - RapidIO specific wrapper
1701 * for device_prep_slave_sg callback defined by DMAENGINE.
1702 * @rdev: RIO device control structure
1703 * @dchan: DMA channel to configure
1704 * @data: RIO specific data descriptor
1705 * @direction: DMA data transfer direction (TO or FROM the device)
1706 * @flags: dmaengine defined flags
1707 *
1708 * Initializes RapidIO capable DMA channel for the specified data transfer.
1709 * Uses DMA channel private extension to pass information related to remote
1710 * target RIO device.
1711 * Returns pointer to DMA transaction descriptor or NULL if failed.
1712 */
1713struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1714 struct dma_chan *dchan, struct rio_dma_data *data,
1715 enum dma_transfer_direction direction, unsigned long flags)
1716{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001717 return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001718}
1719EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1720
1721#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1722
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001723/**
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07001724 * rio_find_mport - find RIO mport by its ID
1725 * @mport_id: number (ID) of mport device
1726 *
1727 * Given a RIO mport number, the desired mport is located
1728 * in the global list of mports. If the mport is found, a pointer to its
1729 * data structure is returned. If no mport is found, %NULL is returned.
1730 */
1731struct rio_mport *rio_find_mport(int mport_id)
1732{
1733 struct rio_mport *port;
1734
1735 mutex_lock(&rio_mport_list_lock);
1736 list_for_each_entry(port, &rio_mports, node) {
1737 if (port->id == mport_id)
1738 goto found;
1739 }
1740 port = NULL;
1741found:
1742 mutex_unlock(&rio_mport_list_lock);
1743
1744 return port;
1745}
1746
1747/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001748 * rio_register_scan - enumeration/discovery method registration interface
1749 * @mport_id: mport device ID for which fabric scan routine has to be set
1750 * (RIO_MPORT_ANY = set for all available mports)
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001751 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001752 *
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001753 * Registers enumeration/discovery operations with RapidIO subsystem and
1754 * attaches it to the specified mport device (or all available mports
1755 * if RIO_MPORT_ANY is specified).
1756 *
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001757 * Returns error if the mport already has an enumerator attached to it.
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001758 * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001759 */
1760int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1761{
1762 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001763 struct rio_scan_node *scan;
1764 int rc = 0;
1765
1766 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1767
1768 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1769 !scan_ops)
1770 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001771
1772 mutex_lock(&rio_mport_list_lock);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001773
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001774 /*
1775 * Check if there is another enumerator already registered for
1776 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
1777 * for the same mport ID are not supported.
1778 */
1779 list_for_each_entry(scan, &rio_scans, node) {
1780 if (scan->mport_id == mport_id) {
1781 rc = -EBUSY;
1782 goto err_out;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001783 }
1784 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001785
1786 /*
1787 * Allocate and initialize new scan registration node.
1788 */
1789 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
1790 if (!scan) {
1791 rc = -ENOMEM;
1792 goto err_out;
1793 }
1794
1795 scan->mport_id = mport_id;
1796 scan->ops = scan_ops;
1797
1798 /*
1799 * Traverse the list of registered mports to attach this new scan.
1800 *
1801 * The new scan with matching mport ID overrides any previously attached
1802 * scan assuming that old scan (if any) is the default one (based on the
1803 * enumerator registration check above).
1804 * If the new scan is the global one, it will be attached only to mports
1805 * that do not have their own individual operations already attached.
1806 */
1807 list_for_each_entry(port, &rio_mports, node) {
1808 if (port->id == mport_id) {
1809 port->nscan = scan_ops;
1810 break;
1811 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
1812 port->nscan = scan_ops;
1813 }
1814
1815 list_add_tail(&scan->node, &rio_scans);
1816
1817err_out:
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001818 mutex_unlock(&rio_mport_list_lock);
1819
1820 return rc;
1821}
1822EXPORT_SYMBOL_GPL(rio_register_scan);
1823
1824/**
1825 * rio_unregister_scan - removes enumeration/discovery method from mport
1826 * @mport_id: mport device ID for which fabric scan routine has to be
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001827 * unregistered (RIO_MPORT_ANY = apply to all mports that use
1828 * the specified scan_ops)
1829 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001830 *
1831 * Removes enumeration or discovery method assigned to the specified mport
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001832 * device. If RIO_MPORT_ANY is specified, removes the specified operations from
1833 * all mports that have them attached.
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001834 */
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001835int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001836{
1837 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001838 struct rio_scan_node *scan;
1839
1840 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1841
1842 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
1843 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001844
1845 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001846
1847 list_for_each_entry(port, &rio_mports, node)
1848 if (port->id == mport_id ||
1849 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
1850 port->nscan = NULL;
1851
Dan Carpenterf93f3c42013-07-31 13:53:34 -07001852 list_for_each_entry(scan, &rio_scans, node) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001853 if (scan->mport_id == mport_id) {
1854 list_del(&scan->node);
1855 kfree(scan);
Dan Carpenterf93f3c42013-07-31 13:53:34 -07001856 break;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001857 }
Dan Carpenterf93f3c42013-07-31 13:53:34 -07001858 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001859
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001860 mutex_unlock(&rio_mport_list_lock);
1861
1862 return 0;
1863}
1864EXPORT_SYMBOL_GPL(rio_unregister_scan);
1865
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001866/**
1867 * rio_mport_scan - execute enumeration/discovery on the specified mport
1868 * @mport_id: number (ID) of mport device
1869 */
1870int rio_mport_scan(int mport_id)
1871{
1872 struct rio_mport *port = NULL;
1873 int rc;
1874
1875 mutex_lock(&rio_mport_list_lock);
1876 list_for_each_entry(port, &rio_mports, node) {
1877 if (port->id == mport_id)
1878 goto found;
1879 }
1880 mutex_unlock(&rio_mport_list_lock);
1881 return -ENODEV;
1882found:
1883 if (!port->nscan) {
1884 mutex_unlock(&rio_mport_list_lock);
1885 return -EINVAL;
1886 }
1887
1888 if (!try_module_get(port->nscan->owner)) {
1889 mutex_unlock(&rio_mport_list_lock);
1890 return -ENODEV;
1891 }
1892
1893 mutex_unlock(&rio_mport_list_lock);
1894
1895 if (port->host_deviceid >= 0)
1896 rc = port->nscan->enumerate(port, 0);
1897 else
1898 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
1899
1900 module_put(port->nscan->owner);
1901 return rc;
1902}
1903
Matt Porter394b7012005-11-07 01:00:15 -08001904static void rio_fixup_device(struct rio_dev *dev)
1905{
1906}
1907
Bill Pemberton305c8912012-11-19 13:23:25 -05001908static int rio_init(void)
Matt Porter394b7012005-11-07 01:00:15 -08001909{
1910 struct rio_dev *dev = NULL;
1911
1912 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
1913 rio_fixup_device(dev);
1914 }
1915 return 0;
1916}
1917
Alexandre Bounine005842e2012-10-04 17:16:08 -07001918static struct workqueue_struct *rio_wq;
1919
1920struct rio_disc_work {
1921 struct work_struct work;
1922 struct rio_mport *mport;
1923};
1924
Bill Pemberton305c8912012-11-19 13:23:25 -05001925static void disc_work_handler(struct work_struct *_work)
Alexandre Bounine005842e2012-10-04 17:16:08 -07001926{
1927 struct rio_disc_work *work;
1928
1929 work = container_of(_work, struct rio_disc_work, work);
1930 pr_debug("RIO: discovery work for mport %d %s\n",
1931 work->mport->id, work->mport->name);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001932 if (try_module_get(work->mport->nscan->owner)) {
1933 work->mport->nscan->discover(work->mport, 0);
1934 module_put(work->mport->nscan->owner);
1935 }
Alexandre Bounine005842e2012-10-04 17:16:08 -07001936}
1937
Bill Pemberton305c8912012-11-19 13:23:25 -05001938int rio_init_mports(void)
Matt Porter394b7012005-11-07 01:00:15 -08001939{
Matt Porter394b7012005-11-07 01:00:15 -08001940 struct rio_mport *port;
Alexandre Bounine005842e2012-10-04 17:16:08 -07001941 struct rio_disc_work *work;
Alexandre Bounine25747402012-10-10 15:53:59 -07001942 int n = 0;
Matt Porter394b7012005-11-07 01:00:15 -08001943
Alexandre Bounine25747402012-10-10 15:53:59 -07001944 if (!next_portid)
1945 return -ENODEV;
1946
1947 /*
1948 * First, run enumerations and check if we need to perform discovery
1949 * on any of the registered mports.
1950 */
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001951 mutex_lock(&rio_mport_list_lock);
Matt Porter394b7012005-11-07 01:00:15 -08001952 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001953 if (port->host_deviceid >= 0) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001954 if (port->nscan && try_module_get(port->nscan->owner)) {
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07001955 port->nscan->enumerate(port, 0);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001956 module_put(port->nscan->owner);
1957 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001958 } else
Alexandre Bounine25747402012-10-10 15:53:59 -07001959 n++;
1960 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001961 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine005842e2012-10-04 17:16:08 -07001962
Alexandre Bounine25747402012-10-10 15:53:59 -07001963 if (!n)
1964 goto no_disc;
Alexandre Bounine005842e2012-10-04 17:16:08 -07001965
Alexandre Bounine25747402012-10-10 15:53:59 -07001966 /*
1967 * If we have mports that require discovery schedule a discovery work
1968 * for each of them. If the code below fails to allocate needed
1969 * resources, exit without error to keep results of enumeration
1970 * process (if any).
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001971 * TODO: Implement restart of discovery process for all or
Alexandre Bounine25747402012-10-10 15:53:59 -07001972 * individual discovering mports.
1973 */
1974 rio_wq = alloc_workqueue("riodisc", 0, 0);
1975 if (!rio_wq) {
1976 pr_err("RIO: unable allocate rio_wq\n");
1977 goto no_disc;
1978 }
1979
1980 work = kcalloc(n, sizeof *work, GFP_KERNEL);
1981 if (!work) {
1982 pr_err("RIO: no memory for work struct\n");
1983 destroy_workqueue(rio_wq);
1984 goto no_disc;
1985 }
1986
1987 n = 0;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001988 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07001989 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001990 if (port->host_deviceid < 0 && port->nscan) {
Alexandre Bounine25747402012-10-10 15:53:59 -07001991 work[n].mport = port;
1992 INIT_WORK(&work[n].work, disc_work_handler);
1993 queue_work(rio_wq, &work[n].work);
1994 n++;
Alexandre Bounine005842e2012-10-04 17:16:08 -07001995 }
1996 }
1997
Alexandre Bounine25747402012-10-10 15:53:59 -07001998 flush_workqueue(rio_wq);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001999 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07002000 pr_debug("RIO: destroy discovery workqueue\n");
2001 destroy_workqueue(rio_wq);
2002 kfree(work);
Matt Porter394b7012005-11-07 01:00:15 -08002003
Alexandre Bounine25747402012-10-10 15:53:59 -07002004no_disc:
Alexandre Bounine2f809982011-03-23 16:43:04 -07002005 rio_init();
2006
Alexandre Bouninec1256eb2011-03-23 16:43:06 -07002007 return 0;
Matt Porter394b7012005-11-07 01:00:15 -08002008}
2009
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002010static int rio_get_hdid(int index)
2011{
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07002012 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002013 return -1;
2014
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07002015 return hdid[index];
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002016}
2017
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002018int rio_mport_initialize(struct rio_mport *mport)
2019{
2020 if (next_portid >= RIO_MAX_MPORTS) {
2021 pr_err("RIO: reached specified max number of mports\n");
2022 return -ENODEV;
2023 }
2024
2025 atomic_set(&mport->state, RIO_DEVICE_INITIALIZING);
2026 mport->id = next_portid++;
2027 mport->host_deviceid = rio_get_hdid(mport->id);
2028 mport->nscan = NULL;
Alexandre Bouninea7b4c632016-03-22 14:26:35 -07002029 mutex_init(&mport->lock);
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002030
2031 return 0;
2032}
2033EXPORT_SYMBOL_GPL(rio_mport_initialize);
2034
Alexandre Bounine59f99962011-04-14 15:22:14 -07002035int rio_register_mport(struct rio_mport *port)
Matt Porter394b7012005-11-07 01:00:15 -08002036{
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002037 struct rio_scan_node *scan = NULL;
Alexandre Bounine2aaf3082014-04-07 15:38:56 -07002038 int res = 0;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002039
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002040 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002041
2042 /*
2043 * Check if there are any registered enumeration/discovery operations
2044 * that have to be attached to the added mport.
2045 */
2046 list_for_each_entry(scan, &rio_scans, node) {
2047 if (port->id == scan->mport_id ||
2048 scan->mport_id == RIO_MPORT_ANY) {
2049 port->nscan = scan->ops;
2050 if (port->id == scan->mport_id)
2051 break;
2052 }
2053 }
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002054
2055 list_add_tail(&port->node, &rio_mports);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002056 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002057
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002058 dev_set_name(&port->dev, "rapidio%d", port->id);
2059 port->dev.class = &rio_mport_class;
2060 atomic_set(&port->state, RIO_DEVICE_RUNNING);
2061
2062 res = device_register(&port->dev);
2063 if (res)
2064 dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
2065 port->id, res);
2066 else
2067 dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id);
2068
2069 return res;
Matt Porter394b7012005-11-07 01:00:15 -08002070}
Alexandre Bounine94d9bd42013-07-03 15:08:55 -07002071EXPORT_SYMBOL_GPL(rio_register_mport);
Matt Porter394b7012005-11-07 01:00:15 -08002072
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002073static int rio_mport_cleanup_callback(struct device *dev, void *data)
2074{
2075 struct rio_dev *rdev = to_rio_dev(dev);
2076
2077 if (dev->bus == &rio_bus_type)
2078 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
2079 return 0;
2080}
2081
2082static int rio_net_remove_children(struct rio_net *net)
2083{
2084 /*
2085 * Unregister all RapidIO devices residing on this net (this will
2086 * invoke notification of registered subsystem interfaces as well).
2087 */
2088 device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback);
2089 return 0;
2090}
2091
2092int rio_unregister_mport(struct rio_mport *port)
2093{
2094 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
2095
2096 /* Transition mport to the SHUTDOWN state */
2097 if (atomic_cmpxchg(&port->state,
2098 RIO_DEVICE_RUNNING,
2099 RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) {
2100 pr_err("RIO: %s unexpected state transition for mport %s\n",
2101 __func__, port->name);
2102 }
2103
2104 if (port->net && port->net->hport == port) {
2105 rio_net_remove_children(port->net);
2106 rio_free_net(port->net);
2107 }
2108
2109 /*
2110 * Unregister all RapidIO devices attached to this mport (this will
2111 * invoke notification of registered subsystem interfaces as well).
2112 */
2113 mutex_lock(&rio_mport_list_lock);
2114 list_del(&port->node);
2115 mutex_unlock(&rio_mport_list_lock);
2116 device_unregister(&port->dev);
2117
2118 return 0;
2119}
2120EXPORT_SYMBOL_GPL(rio_unregister_mport);
2121
Matt Porter394b7012005-11-07 01:00:15 -08002122EXPORT_SYMBOL_GPL(rio_local_get_device_id);
2123EXPORT_SYMBOL_GPL(rio_get_device);
2124EXPORT_SYMBOL_GPL(rio_get_asm);
2125EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
2126EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
2127EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
2128EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
2129EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
2130EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
2131EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
2132EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002133EXPORT_SYMBOL_GPL(rio_init_mports);