blob: 1cd32603259fb68df6e84bb9623d3be281223ede [file] [log] [blame]
Matt Porter394b7012005-11-07 01:00:15 -08001/*
2 * RapidIO interconnect services
3 * (RapidIO Interconnect Specification, http://www.rapidio.org)
4 *
5 * Copyright 2005 MontaVista Software, Inc.
6 * Matt Porter <mporter@kernel.crashing.org>
7 *
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07008 * Copyright 2009 - 2013 Integrated Device Technology, Inc.
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07009 * Alex Bounine <alexandre.bounine@idt.com>
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -070010 *
Matt Porter394b7012005-11-07 01:00:15 -080011 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
Matt Porter394b7012005-11-07 01:00:15 -080017#include <linux/types.h>
18#include <linux/kernel.h>
19
20#include <linux/delay.h>
21#include <linux/init.h>
22#include <linux/rio.h>
23#include <linux/rio_drv.h>
24#include <linux/rio_ids.h>
25#include <linux/rio_regs.h>
26#include <linux/module.h>
27#include <linux/spinlock.h>
Tim Schmielaude259682006-01-08 01:02:05 -080028#include <linux/slab.h>
Kumar Gala5febf1c2008-01-23 05:53:47 -060029#include <linux/interrupt.h>
Matt Porter394b7012005-11-07 01:00:15 -080030
31#include "rio.h"
32
Alexandre Bounine9a0b0622016-03-22 14:26:44 -070033/*
34 * struct rio_pwrite - RIO portwrite event
35 * @node: Node in list of doorbell events
36 * @pwcback: Doorbell event callback
37 * @context: Handler specific context to pass on event
38 */
39struct rio_pwrite {
40 struct list_head node;
41
42 int (*pwcback)(struct rio_mport *mport, void *context,
43 union rio_pw_msg *msg, int step);
44 void *context;
45};
46
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -070047MODULE_DESCRIPTION("RapidIO Subsystem Core");
48MODULE_AUTHOR("Matt Porter <mporter@kernel.crashing.org>");
49MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
50MODULE_LICENSE("GPL");
51
52static int hdid[RIO_MAX_MPORTS];
53static int ids_num;
54module_param_array(hdid, int, &ids_num, 0);
55MODULE_PARM_DESC(hdid,
56 "Destination ID assignment to local RapidIO controllers");
57
Alexandre Bouninea11650e2013-05-24 15:55:05 -070058static LIST_HEAD(rio_devices);
Alexandre Bouninee6b585c2016-03-22 14:26:17 -070059static LIST_HEAD(rio_nets);
Alexandre Bouninea11650e2013-05-24 15:55:05 -070060static DEFINE_SPINLOCK(rio_global_list_lock);
61
Matt Porter394b7012005-11-07 01:00:15 -080062static LIST_HEAD(rio_mports);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -070063static LIST_HEAD(rio_scans);
Alexandre Bouninea11650e2013-05-24 15:55:05 -070064static DEFINE_MUTEX(rio_mport_list_lock);
Alexandre Bounine569fccb2011-03-23 16:43:05 -070065static unsigned char next_portid;
Alexandre Bounineda1589f2012-10-04 17:15:57 -070066static DEFINE_SPINLOCK(rio_mmap_lock);
Matt Porter394b7012005-11-07 01:00:15 -080067
68/**
69 * rio_local_get_device_id - Get the base/extended device id for a port
70 * @port: RIO master port from which to get the deviceid
71 *
72 * Reads the base/extended device id from the local device
73 * implementing the master port. Returns the 8/16-bit device
74 * id.
75 */
76u16 rio_local_get_device_id(struct rio_mport *port)
77{
78 u32 result;
79
80 rio_local_read_config_32(port, RIO_DID_CSR, &result);
81
Zhang Weie0423232008-04-18 13:33:42 -070082 return (RIO_GET_DID(port->sys_size, result));
Matt Porter394b7012005-11-07 01:00:15 -080083}
84
85/**
Alexandre Bounine8b189fd2016-03-22 14:26:00 -070086 * rio_query_mport - Query mport device attributes
87 * @port: mport device to query
88 * @mport_attr: mport attributes data structure
89 *
90 * Returns attributes of specified mport through the
91 * pointer to attributes data structure.
92 */
93int rio_query_mport(struct rio_mport *port,
94 struct rio_mport_attr *mport_attr)
95{
96 if (!port->ops->query_mport)
97 return -ENODATA;
98 return port->ops->query_mport(port, mport_attr);
99}
100EXPORT_SYMBOL(rio_query_mport);
101
102/**
Alexandre Bouninee6b585c2016-03-22 14:26:17 -0700103 * rio_alloc_net- Allocate and initialize a new RIO network data structure
104 * @mport: Master port associated with the RIO network
105 *
106 * Allocates a RIO network structure, initializes per-network
107 * list heads, and adds the associated master port to the
108 * network list of associated master ports. Returns a
109 * RIO network pointer on success or %NULL on failure.
110 */
111struct rio_net *rio_alloc_net(struct rio_mport *mport)
112{
113 struct rio_net *net;
114
115 net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
116 if (net) {
117 INIT_LIST_HEAD(&net->node);
118 INIT_LIST_HEAD(&net->devices);
119 INIT_LIST_HEAD(&net->switches);
120 INIT_LIST_HEAD(&net->mports);
121 mport->net = net;
122 }
123 return net;
124}
125EXPORT_SYMBOL_GPL(rio_alloc_net);
126
127int rio_add_net(struct rio_net *net)
128{
129 int err;
130
131 err = device_register(&net->dev);
132 if (err)
133 return err;
134 spin_lock(&rio_global_list_lock);
135 list_add_tail(&net->node, &rio_nets);
136 spin_unlock(&rio_global_list_lock);
137
138 return 0;
139}
140EXPORT_SYMBOL_GPL(rio_add_net);
141
142void rio_free_net(struct rio_net *net)
143{
144 spin_lock(&rio_global_list_lock);
145 if (!list_empty(&net->node))
146 list_del(&net->node);
147 spin_unlock(&rio_global_list_lock);
148 if (net->release)
149 net->release(net);
150 device_unregister(&net->dev);
151}
152EXPORT_SYMBOL_GPL(rio_free_net);
153
154/**
Alexandre Bounine50246222016-03-22 14:26:38 -0700155 * rio_local_set_device_id - Set the base/extended device id for a port
156 * @port: RIO master port
157 * @did: Device ID value to be written
158 *
159 * Writes the base/extended device id from a device.
160 */
161void rio_local_set_device_id(struct rio_mport *port, u16 did)
162{
163 rio_local_write_config_32(port, RIO_DID_CSR,
164 RIO_SET_DID(port->sys_size, did));
165}
166EXPORT_SYMBOL_GPL(rio_local_set_device_id);
167
168/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700169 * rio_add_device- Adds a RIO device to the device model
170 * @rdev: RIO device
171 *
172 * Adds the RIO device to the global device list and adds the RIO
173 * device to the RIO device list. Creates the generic sysfs nodes
174 * for an RIO device.
175 */
176int rio_add_device(struct rio_dev *rdev)
177{
178 int err;
179
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700180 atomic_set(&rdev->state, RIO_DEVICE_RUNNING);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700181 err = device_register(&rdev->dev);
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700182 if (err)
183 return err;
184
185 spin_lock(&rio_global_list_lock);
186 list_add_tail(&rdev->global_list, &rio_devices);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700187 if (rdev->net) {
188 list_add_tail(&rdev->net_list, &rdev->net->devices);
189 if (rdev->pef & RIO_PEF_SWITCH)
190 list_add_tail(&rdev->rswitch->node,
191 &rdev->net->switches);
192 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700193 spin_unlock(&rio_global_list_lock);
194
195 rio_create_sysfs_dev_files(rdev);
196
197 return 0;
198}
199EXPORT_SYMBOL_GPL(rio_add_device);
200
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700201/*
202 * rio_del_device - removes a RIO device from the device model
203 * @rdev: RIO device
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700204 * @state: device state to set during removal process
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700205 *
206 * Removes the RIO device to the kernel device list and subsystem's device list.
207 * Clears sysfs entries for the removed device.
208 */
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700209void rio_del_device(struct rio_dev *rdev, enum rio_device_state state)
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700210{
211 pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev));
Alexandre Bounineb77a2032016-03-22 14:26:20 -0700212 atomic_set(&rdev->state, state);
Alexandre Bounineb74ec562016-03-22 14:26:14 -0700213 spin_lock(&rio_global_list_lock);
214 list_del(&rdev->global_list);
215 if (rdev->net) {
216 list_del(&rdev->net_list);
217 if (rdev->pef & RIO_PEF_SWITCH) {
218 list_del(&rdev->rswitch->node);
219 kfree(rdev->rswitch->route_table);
220 }
221 }
222 spin_unlock(&rio_global_list_lock);
223 rio_remove_sysfs_dev_files(rdev);
224 device_unregister(&rdev->dev);
225}
226EXPORT_SYMBOL_GPL(rio_del_device);
227
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700228/**
Matt Porter394b7012005-11-07 01:00:15 -0800229 * rio_request_inb_mbox - request inbound mailbox service
230 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800231 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800232 * @mbox: Mailbox number to claim
233 * @entries: Number of entries in inbound mailbox queue
234 * @minb: Callback to execute when inbound message is received
235 *
236 * Requests ownership of an inbound mailbox resource and binds
237 * a callback function to the resource. Returns %0 on success.
238 */
239int rio_request_inb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800240 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800241 int mbox,
242 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800243 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
Matt Porter394b7012005-11-07 01:00:15 -0800244 int slot))
245{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700246 int rc = -ENOSYS;
247 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800248
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700249 if (mport->ops->open_inb_mbox == NULL)
250 goto out;
251
Toshi Kani9a975be2016-01-26 21:57:25 +0100252 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800253
254 if (res) {
255 rio_init_mbox_res(res, mbox, mbox);
256
257 /* Make sure this mailbox isn't in use */
258 if ((rc =
259 request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
260 res)) < 0) {
261 kfree(res);
262 goto out;
263 }
264
265 mport->inb_msg[mbox].res = res;
266
267 /* Hook the inbound message callback */
268 mport->inb_msg[mbox].mcback = minb;
269
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700270 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700271 if (rc) {
272 mport->inb_msg[mbox].mcback = NULL;
273 mport->inb_msg[mbox].res = NULL;
274 release_resource(res);
275 kfree(res);
276 }
Matt Porter394b7012005-11-07 01:00:15 -0800277 } else
278 rc = -ENOMEM;
279
280 out:
281 return rc;
282}
283
284/**
285 * rio_release_inb_mbox - release inbound mailbox message service
286 * @mport: RIO master port from which to release the mailbox resource
287 * @mbox: Mailbox number to release
288 *
289 * Releases ownership of an inbound mailbox resource. Returns 0
290 * if the request has been satisfied.
291 */
292int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
293{
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700294 int rc;
Matt Porter394b7012005-11-07 01:00:15 -0800295
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700296 if (!mport->ops->close_inb_mbox || !mport->inb_msg[mbox].res)
297 return -EINVAL;
298
299 mport->ops->close_inb_mbox(mport, mbox);
300 mport->inb_msg[mbox].mcback = NULL;
301
302 rc = release_resource(mport->inb_msg[mbox].res);
303 if (rc)
304 return rc;
305
306 kfree(mport->inb_msg[mbox].res);
307 mport->inb_msg[mbox].res = NULL;
308
309 return 0;
Matt Porter394b7012005-11-07 01:00:15 -0800310}
311
312/**
313 * rio_request_outb_mbox - request outbound mailbox service
314 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800315 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800316 * @mbox: Mailbox number to claim
317 * @entries: Number of entries in outbound mailbox queue
318 * @moutb: Callback to execute when outbound message is sent
319 *
320 * Requests ownership of an outbound mailbox resource and binds
321 * a callback function to the resource. Returns 0 on success.
322 */
323int rio_request_outb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800324 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800325 int mbox,
326 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800327 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
Matt Porter394b7012005-11-07 01:00:15 -0800328{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700329 int rc = -ENOSYS;
330 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800331
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700332 if (mport->ops->open_outb_mbox == NULL)
333 goto out;
334
Toshi Kani9a975be2016-01-26 21:57:25 +0100335 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800336
337 if (res) {
338 rio_init_mbox_res(res, mbox, mbox);
339
340 /* Make sure this outbound mailbox isn't in use */
341 if ((rc =
342 request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
343 res)) < 0) {
344 kfree(res);
345 goto out;
346 }
347
348 mport->outb_msg[mbox].res = res;
349
350 /* Hook the inbound message callback */
351 mport->outb_msg[mbox].mcback = moutb;
352
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700353 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700354 if (rc) {
355 mport->outb_msg[mbox].mcback = NULL;
356 mport->outb_msg[mbox].res = NULL;
357 release_resource(res);
358 kfree(res);
359 }
Matt Porter394b7012005-11-07 01:00:15 -0800360 } else
361 rc = -ENOMEM;
362
363 out:
364 return rc;
365}
366
367/**
368 * rio_release_outb_mbox - release outbound mailbox message service
369 * @mport: RIO master port from which to release the mailbox resource
370 * @mbox: Mailbox number to release
371 *
372 * Releases ownership of an inbound mailbox resource. Returns 0
373 * if the request has been satisfied.
374 */
375int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
376{
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700377 int rc;
Matt Porter394b7012005-11-07 01:00:15 -0800378
Alexandre Bounine06e1b242016-08-02 14:06:49 -0700379 if (!mport->ops->close_outb_mbox || !mport->outb_msg[mbox].res)
380 return -EINVAL;
381
382 mport->ops->close_outb_mbox(mport, mbox);
383 mport->outb_msg[mbox].mcback = NULL;
384
385 rc = release_resource(mport->outb_msg[mbox].res);
386 if (rc)
387 return rc;
388
389 kfree(mport->outb_msg[mbox].res);
390 mport->outb_msg[mbox].res = NULL;
391
392 return 0;
Matt Porter394b7012005-11-07 01:00:15 -0800393}
394
395/**
396 * rio_setup_inb_dbell - bind inbound doorbell callback
397 * @mport: RIO master port to bind the doorbell callback
Matt Porter6978bbc2005-11-07 01:00:20 -0800398 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800399 * @res: Doorbell message resource
400 * @dinb: Callback to execute when doorbell is received
401 *
402 * Adds a doorbell resource/callback pair into a port's
403 * doorbell event list. Returns 0 if the request has been
404 * satisfied.
405 */
406static int
Matt Porter6978bbc2005-11-07 01:00:20 -0800407rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
408 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
Matt Porter394b7012005-11-07 01:00:15 -0800409 u16 info))
410{
411 int rc = 0;
412 struct rio_dbell *dbell;
413
414 if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
415 rc = -ENOMEM;
416 goto out;
417 }
418
419 dbell->res = res;
420 dbell->dinb = dinb;
Matt Porter6978bbc2005-11-07 01:00:20 -0800421 dbell->dev_id = dev_id;
Matt Porter394b7012005-11-07 01:00:15 -0800422
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700423 mutex_lock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800424 list_add_tail(&dbell->node, &mport->dbells);
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700425 mutex_unlock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800426
427 out:
428 return rc;
429}
430
431/**
432 * rio_request_inb_dbell - request inbound doorbell message service
433 * @mport: RIO master port from which to allocate the doorbell resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800434 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800435 * @start: Doorbell info range start
436 * @end: Doorbell info range end
437 * @dinb: Callback to execute when doorbell is received
438 *
439 * Requests ownership of an inbound doorbell resource and binds
440 * a callback function to the resource. Returns 0 if the request
441 * has been satisfied.
442 */
443int rio_request_inb_dbell(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800444 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800445 u16 start,
446 u16 end,
Matt Porter6978bbc2005-11-07 01:00:20 -0800447 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
Matt Porter394b7012005-11-07 01:00:15 -0800448 u16 dst, u16 info))
449{
450 int rc = 0;
451
Toshi Kani9a975be2016-01-26 21:57:25 +0100452 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800453
454 if (res) {
455 rio_init_dbell_res(res, start, end);
456
457 /* Make sure these doorbells aren't in use */
458 if ((rc =
459 request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
460 res)) < 0) {
461 kfree(res);
462 goto out;
463 }
464
465 /* Hook the doorbell callback */
Matt Porter6978bbc2005-11-07 01:00:20 -0800466 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
Matt Porter394b7012005-11-07 01:00:15 -0800467 } else
468 rc = -ENOMEM;
469
470 out:
471 return rc;
472}
473
474/**
475 * rio_release_inb_dbell - release inbound doorbell message service
476 * @mport: RIO master port from which to release the doorbell resource
477 * @start: Doorbell info range start
478 * @end: Doorbell info range end
479 *
480 * Releases ownership of an inbound doorbell resource and removes
481 * callback from the doorbell event list. Returns 0 if the request
482 * has been satisfied.
483 */
484int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
485{
486 int rc = 0, found = 0;
487 struct rio_dbell *dbell;
488
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700489 mutex_lock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800490 list_for_each_entry(dbell, &mport->dbells, node) {
491 if ((dbell->res->start == start) && (dbell->res->end == end)) {
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700492 list_del(&dbell->node);
Matt Porter394b7012005-11-07 01:00:15 -0800493 found = 1;
494 break;
495 }
496 }
Alexandre Bouninea7b4c632016-03-22 14:26:35 -0700497 mutex_unlock(&mport->lock);
Matt Porter394b7012005-11-07 01:00:15 -0800498
499 /* If we can't find an exact match, fail */
500 if (!found) {
501 rc = -EINVAL;
502 goto out;
503 }
504
Matt Porter394b7012005-11-07 01:00:15 -0800505 /* Release the doorbell resource */
506 rc = release_resource(dbell->res);
507
508 /* Free the doorbell event */
509 kfree(dbell);
510
511 out:
512 return rc;
513}
514
515/**
516 * rio_request_outb_dbell - request outbound doorbell message range
517 * @rdev: RIO device from which to allocate the doorbell resource
518 * @start: Doorbell message range start
519 * @end: Doorbell message range end
520 *
521 * Requests ownership of a doorbell message range. Returns a resource
522 * if the request has been satisfied or %NULL on failure.
523 */
524struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
525 u16 end)
526{
Toshi Kani9a975be2016-01-26 21:57:25 +0100527 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800528
529 if (res) {
530 rio_init_dbell_res(res, start, end);
531
532 /* Make sure these doorbells aren't in use */
533 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
534 < 0) {
535 kfree(res);
536 res = NULL;
537 }
538 }
539
540 return res;
541}
542
543/**
544 * rio_release_outb_dbell - release outbound doorbell message range
545 * @rdev: RIO device from which to release the doorbell resource
546 * @res: Doorbell resource to be freed
547 *
548 * Releases ownership of a doorbell message range. Returns 0 if the
549 * request has been satisfied.
550 */
551int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
552{
553 int rc = release_resource(res);
554
555 kfree(res);
556
557 return rc;
558}
559
560/**
Alexandre Bounine9a0b0622016-03-22 14:26:44 -0700561 * rio_add_mport_pw_handler - add port-write message handler into the list
562 * of mport specific pw handlers
563 * @mport: RIO master port to bind the portwrite callback
564 * @context: Handler specific context to pass on event
565 * @pwcback: Callback to execute when portwrite is received
566 *
567 * Returns 0 if the request has been satisfied.
568 */
569int rio_add_mport_pw_handler(struct rio_mport *mport, void *context,
570 int (*pwcback)(struct rio_mport *mport,
571 void *context, union rio_pw_msg *msg, int step))
572{
573 int rc = 0;
574 struct rio_pwrite *pwrite;
575
576 pwrite = kzalloc(sizeof(struct rio_pwrite), GFP_KERNEL);
577 if (!pwrite) {
578 rc = -ENOMEM;
579 goto out;
580 }
581
582 pwrite->pwcback = pwcback;
583 pwrite->context = context;
584 mutex_lock(&mport->lock);
585 list_add_tail(&pwrite->node, &mport->pwrites);
586 mutex_unlock(&mport->lock);
587out:
588 return rc;
589}
590EXPORT_SYMBOL_GPL(rio_add_mport_pw_handler);
591
592/**
593 * rio_del_mport_pw_handler - remove port-write message handler from the list
594 * of mport specific pw handlers
595 * @mport: RIO master port to bind the portwrite callback
596 * @context: Registered handler specific context to pass on event
597 * @pwcback: Registered callback function
598 *
599 * Returns 0 if the request has been satisfied.
600 */
601int rio_del_mport_pw_handler(struct rio_mport *mport, void *context,
602 int (*pwcback)(struct rio_mport *mport,
603 void *context, union rio_pw_msg *msg, int step))
604{
605 int rc = -EINVAL;
606 struct rio_pwrite *pwrite;
607
608 mutex_lock(&mport->lock);
609 list_for_each_entry(pwrite, &mport->pwrites, node) {
610 if (pwrite->pwcback == pwcback && pwrite->context == context) {
611 list_del(&pwrite->node);
612 kfree(pwrite);
613 rc = 0;
614 break;
615 }
616 }
617 mutex_unlock(&mport->lock);
618
619 return rc;
620}
621EXPORT_SYMBOL_GPL(rio_del_mport_pw_handler);
622
623/**
624 * rio_request_inb_pwrite - request inbound port-write message service for
625 * specific RapidIO device
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700626 * @rdev: RIO device to which register inbound port-write callback routine
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700627 * @pwcback: Callback routine to execute when port-write is received
628 *
629 * Binds a port-write callback function to the RapidIO device.
630 * Returns 0 if the request has been satisfied.
631 */
632int rio_request_inb_pwrite(struct rio_dev *rdev,
633 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
634{
635 int rc = 0;
636
637 spin_lock(&rio_global_list_lock);
638 if (rdev->pwcback != NULL)
639 rc = -ENOMEM;
640 else
641 rdev->pwcback = pwcback;
642
643 spin_unlock(&rio_global_list_lock);
644 return rc;
645}
646EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
647
648/**
649 * rio_release_inb_pwrite - release inbound port-write message service
Alexandre Bounine9a0b0622016-03-22 14:26:44 -0700650 * associated with specific RapidIO device
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700651 * @rdev: RIO device which registered for inbound port-write callback
652 *
653 * Removes callback from the rio_dev structure. Returns 0 if the request
654 * has been satisfied.
655 */
656int rio_release_inb_pwrite(struct rio_dev *rdev)
657{
658 int rc = -ENOMEM;
659
660 spin_lock(&rio_global_list_lock);
661 if (rdev->pwcback) {
662 rdev->pwcback = NULL;
663 rc = 0;
664 }
665
666 spin_unlock(&rio_global_list_lock);
667 return rc;
668}
669EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
670
671/**
Alexandre Bounineb6cb95e2016-03-22 14:26:41 -0700672 * rio_pw_enable - Enables/disables port-write handling by a master port
673 * @mport: Master port associated with port-write handling
674 * @enable: 1=enable, 0=disable
675 */
676void rio_pw_enable(struct rio_mport *mport, int enable)
677{
678 if (mport->ops->pwenable) {
679 mutex_lock(&mport->lock);
680
681 if ((enable && ++mport->pwe_refcnt == 1) ||
682 (!enable && mport->pwe_refcnt && --mport->pwe_refcnt == 0))
683 mport->ops->pwenable(mport, enable);
684 mutex_unlock(&mport->lock);
685 }
686}
687EXPORT_SYMBOL_GPL(rio_pw_enable);
688
689/**
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700690 * rio_map_inb_region -- Map inbound memory region.
691 * @mport: Master port.
Randy Dunlap2ca3cb52012-11-16 14:14:56 -0800692 * @local: physical address of memory region to be mapped
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700693 * @rbase: RIO base address assigned to this window
694 * @size: Size of the memory region
695 * @rflags: Flags for mapping.
696 *
697 * Return: 0 -- Success.
698 *
699 * This function will create the mapping from RIO space to local memory.
700 */
701int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
702 u64 rbase, u32 size, u32 rflags)
703{
704 int rc = 0;
705 unsigned long flags;
706
707 if (!mport->ops->map_inb)
708 return -1;
709 spin_lock_irqsave(&rio_mmap_lock, flags);
710 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
711 spin_unlock_irqrestore(&rio_mmap_lock, flags);
712 return rc;
713}
714EXPORT_SYMBOL_GPL(rio_map_inb_region);
715
716/**
717 * rio_unmap_inb_region -- Unmap the inbound memory region
718 * @mport: Master port
719 * @lstart: physical address of memory region to be unmapped
720 */
721void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
722{
723 unsigned long flags;
724 if (!mport->ops->unmap_inb)
725 return;
726 spin_lock_irqsave(&rio_mmap_lock, flags);
727 mport->ops->unmap_inb(mport, lstart);
728 spin_unlock_irqrestore(&rio_mmap_lock, flags);
729}
730EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
731
732/**
Alexandre Bounine93bdaca2016-03-22 14:26:50 -0700733 * rio_map_outb_region -- Map outbound memory region.
734 * @mport: Master port.
735 * @destid: destination id window points to
736 * @rbase: RIO base address window translates to
737 * @size: Size of the memory region
738 * @rflags: Flags for mapping.
739 * @local: physical address of memory region mapped
740 *
741 * Return: 0 -- Success.
742 *
743 * This function will create the mapping from RIO space to local memory.
744 */
745int rio_map_outb_region(struct rio_mport *mport, u16 destid, u64 rbase,
746 u32 size, u32 rflags, dma_addr_t *local)
747{
748 int rc = 0;
749 unsigned long flags;
750
751 if (!mport->ops->map_outb)
752 return -ENODEV;
753
754 spin_lock_irqsave(&rio_mmap_lock, flags);
755 rc = mport->ops->map_outb(mport, destid, rbase, size,
756 rflags, local);
757 spin_unlock_irqrestore(&rio_mmap_lock, flags);
758
759 return rc;
760}
761EXPORT_SYMBOL_GPL(rio_map_outb_region);
762
763/**
764 * rio_unmap_inb_region -- Unmap the inbound memory region
765 * @mport: Master port
766 * @destid: destination id mapping points to
767 * @rstart: RIO base address window translates to
768 */
769void rio_unmap_outb_region(struct rio_mport *mport, u16 destid, u64 rstart)
770{
771 unsigned long flags;
772
773 if (!mport->ops->unmap_outb)
774 return;
775
776 spin_lock_irqsave(&rio_mmap_lock, flags);
777 mport->ops->unmap_outb(mport, destid, rstart);
778 spin_unlock_irqrestore(&rio_mmap_lock, flags);
779}
780EXPORT_SYMBOL_GPL(rio_unmap_outb_region);
781
782/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700783 * rio_mport_get_physefb - Helper function that returns register offset
784 * for Physical Layer Extended Features Block.
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700785 * @port: Master port to issue transaction
786 * @local: Indicate a local master port or remote device access
787 * @destid: Destination ID of the device
788 * @hopcount: Number of switch hops to the device
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700789 */
790u32
791rio_mport_get_physefb(struct rio_mport *port, int local,
792 u16 destid, u8 hopcount)
793{
794 u32 ext_ftr_ptr;
795 u32 ftr_header;
796
797 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
798
799 while (ext_ftr_ptr) {
800 if (local)
801 rio_local_read_config_32(port, ext_ftr_ptr,
802 &ftr_header);
803 else
804 rio_mport_read_config_32(port, destid, hopcount,
805 ext_ftr_ptr, &ftr_header);
806
807 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
808 switch (ftr_header) {
809
810 case RIO_EFB_SER_EP_ID_V13P:
811 case RIO_EFB_SER_EP_REC_ID_V13P:
812 case RIO_EFB_SER_EP_FREE_ID_V13P:
813 case RIO_EFB_SER_EP_ID:
814 case RIO_EFB_SER_EP_REC_ID:
815 case RIO_EFB_SER_EP_FREE_ID:
816 case RIO_EFB_SER_EP_FREC_ID:
817
818 return ext_ftr_ptr;
819
820 default:
821 break;
822 }
823
824 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
825 hopcount, ext_ftr_ptr);
826 }
827
828 return ext_ftr_ptr;
829}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700830EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700831
832/**
833 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700834 * @comp_tag: RIO component tag to match
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700835 * @from: Previous RIO device found in search, or %NULL for new search
836 *
837 * Iterates through the list of known RIO devices. If a RIO device is
838 * found with a matching @comp_tag, a pointer to its device
839 * structure is returned. Otherwise, %NULL is returned. A new search
840 * is initiated by passing %NULL to the @from argument. Otherwise, if
841 * @from is not %NULL, searches continue from next device on the global
842 * list.
843 */
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700844struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700845{
846 struct list_head *n;
847 struct rio_dev *rdev;
848
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700849 spin_lock(&rio_global_list_lock);
850 n = from ? from->global_list.next : rio_devices.next;
851
852 while (n && (n != &rio_devices)) {
853 rdev = rio_dev_g(n);
854 if (rdev->comp_tag == comp_tag)
855 goto exit;
856 n = n->next;
857 }
858 rdev = NULL;
859exit:
860 spin_unlock(&rio_global_list_lock);
861 return rdev;
862}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700863EXPORT_SYMBOL_GPL(rio_get_comptag);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700864
865/**
866 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
867 * @rdev: Pointer to RIO device control structure
868 * @pnum: Switch port number to set LOCKOUT bit
869 * @lock: Operation : set (=1) or clear (=0)
870 */
871int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
872{
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700873 u32 regval;
874
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800875 rio_read_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700876 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
877 &regval);
878 if (lock)
879 regval |= RIO_PORT_N_CTL_LOCKOUT;
880 else
881 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
882
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800883 rio_write_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700884 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
885 regval);
886 return 0;
887}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700888EXPORT_SYMBOL_GPL(rio_set_port_lockout);
889
890/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700891 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
892 * given port
893 * @port: Master port associated with the RIO network
894 * @local: local=1 select local port otherwise a far device is reached
895 * @destid: Destination ID of the device to check host bit
896 * @hopcount: Number of hops to reach the target
897 * @port_num: Port (-number on switch) to enable on a far end device
898 *
899 * Returns 0 or 1 from on General Control Command and Status Register
900 * (EXT_PTR+0x3C)
901 */
902int rio_enable_rx_tx_port(struct rio_mport *port,
903 int local, u16 destid,
904 u8 hopcount, u8 port_num)
905{
906#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
907 u32 regval;
908 u32 ext_ftr_ptr;
909
910 /*
911 * enable rx input tx output port
912 */
913 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
914 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
915
916 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
917
918 if (local) {
919 rio_local_read_config_32(port, ext_ftr_ptr +
920 RIO_PORT_N_CTL_CSR(0),
921 &regval);
922 } else {
923 if (rio_mport_read_config_32(port, destid, hopcount,
924 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), &regval) < 0)
925 return -EIO;
926 }
927
928 if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
929 /* serial */
930 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
931 | RIO_PORT_N_CTL_EN_TX_SER;
932 } else {
933 /* parallel */
934 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
935 | RIO_PORT_N_CTL_EN_TX_PAR;
936 }
937
938 if (local) {
939 rio_local_write_config_32(port, ext_ftr_ptr +
940 RIO_PORT_N_CTL_CSR(0), regval);
941 } else {
942 if (rio_mport_write_config_32(port, destid, hopcount,
943 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
944 return -EIO;
945 }
946#endif
947 return 0;
948}
949EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
950
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700951
952/**
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700953 * rio_chk_dev_route - Validate route to the specified device.
954 * @rdev: RIO device failed to respond
955 * @nrdev: Last active device on the route to rdev
956 * @npnum: nrdev's port number on the route to rdev
957 *
958 * Follows a route to the specified RIO device to determine the last available
959 * device (and corresponding RIO port) on the route.
960 */
961static int
962rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
963{
964 u32 result;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800965 int p_port, rc = -EIO;
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700966 struct rio_dev *prev = NULL;
967
968 /* Find switch with failed RIO link */
969 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
970 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
971 prev = rdev->prev;
972 break;
973 }
974 rdev = rdev->prev;
975 }
976
977 if (prev == NULL)
978 goto err_out;
979
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800980 p_port = prev->rswitch->route_table[rdev->destid];
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700981
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700982 if (p_port != RIO_INVALID_ROUTE) {
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700983 pr_debug("RIO: link failed on [%s]-P%d\n",
984 rio_name(prev), p_port);
985 *nrdev = prev;
986 *npnum = p_port;
987 rc = 0;
988 } else
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700989 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700990err_out:
991 return rc;
992}
993
994/**
995 * rio_mport_chk_dev_access - Validate access to the specified device.
996 * @mport: Master port to send transactions
997 * @destid: Device destination ID in network
998 * @hopcount: Number of hops into the network
999 */
Alexandre Bouninee274e0e2010-10-27 15:34:32 -07001000int
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001001rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
1002{
1003 int i = 0;
1004 u32 tmp;
1005
1006 while (rio_mport_read_config_32(mport, destid, hopcount,
1007 RIO_DEV_ID_CAR, &tmp)) {
1008 i++;
1009 if (i == RIO_MAX_CHK_RETRY)
1010 return -EIO;
1011 mdelay(1);
1012 }
1013
1014 return 0;
1015}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001016EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001017
1018/**
1019 * rio_chk_dev_access - Validate access to the specified device.
1020 * @rdev: Pointer to RIO device control structure
1021 */
1022static int rio_chk_dev_access(struct rio_dev *rdev)
1023{
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001024 return rio_mport_chk_dev_access(rdev->net->hport,
1025 rdev->destid, rdev->hopcount);
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001026}
1027
1028/**
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001029 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
1030 * returns link-response (if requested).
1031 * @rdev: RIO devive to issue Input-status command
1032 * @pnum: Device port number to issue the command
1033 * @lnkresp: Response from a link partner
1034 */
1035static int
1036rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
1037{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001038 u32 regval;
1039 int checkcount;
1040
1041 if (lnkresp) {
1042 /* Read from link maintenance response register
1043 * to clear valid bit */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001044 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001045 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
1046 &regval);
1047 udelay(50);
1048 }
1049
1050 /* Issue Input-status command */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001051 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001052 rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
1053 RIO_MNT_REQ_CMD_IS);
1054
1055 /* Exit if the response is not expected */
1056 if (lnkresp == NULL)
1057 return 0;
1058
1059 checkcount = 3;
1060 while (checkcount--) {
1061 udelay(50);
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001062 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001063 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
1064 &regval);
1065 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
1066 *lnkresp = regval;
1067 return 0;
1068 }
1069 }
1070
1071 return -EIO;
1072}
1073
1074/**
1075 * rio_clr_err_stopped - Clears port Error-stopped states.
1076 * @rdev: Pointer to RIO device control structure
1077 * @pnum: Switch port number to clear errors
1078 * @err_status: port error status (if 0 reads register from device)
1079 */
1080static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
1081{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001082 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
1083 u32 regval;
1084 u32 far_ackid, far_linkstat, near_ackid;
1085
1086 if (err_status == 0)
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001087 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001088 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
1089 &err_status);
1090
1091 if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
1092 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
1093 /*
1094 * Send a Link-Request/Input-Status control symbol
1095 */
1096 if (rio_get_input_status(rdev, pnum, &regval)) {
1097 pr_debug("RIO_EM: Input-status response timeout\n");
1098 goto rd_err;
1099 }
1100
1101 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
1102 pnum, regval);
1103 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
1104 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001105 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001106 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
1107 &regval);
1108 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
1109 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
1110 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
1111 " near_ackID=0x%02x\n",
1112 pnum, far_ackid, far_linkstat, near_ackid);
1113
1114 /*
1115 * If required, synchronize ackIDs of near and
1116 * far sides.
1117 */
1118 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
1119 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
1120 /* Align near outstanding/outbound ackIDs with
1121 * far inbound.
1122 */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001123 rio_write_config_32(rdev,
1124 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001125 (near_ackid << 24) |
1126 (far_ackid << 8) | far_ackid);
1127 /* Align far outstanding/outbound ackIDs with
1128 * near inbound.
1129 */
1130 far_ackid++;
1131 if (nextdev)
1132 rio_write_config_32(nextdev,
1133 nextdev->phys_efptr +
1134 RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
1135 (far_ackid << 24) |
1136 (near_ackid << 8) | near_ackid);
1137 else
1138 pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
1139 }
1140rd_err:
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001141 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001142 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
1143 &err_status);
1144 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1145 }
1146
1147 if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
1148 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
1149 rio_get_input_status(nextdev,
1150 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
1151 udelay(50);
1152
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001153 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001154 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
1155 &err_status);
1156 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1157 }
1158
1159 return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
1160 RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
1161}
1162
1163/**
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001164 * rio_inb_pwrite_handler - inbound port-write message handler
1165 * @mport: mport device associated with port-write
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001166 * @pw_msg: pointer to inbound port-write message
1167 *
1168 * Processes an inbound port-write message. Returns 0 if the request
1169 * has been satisfied.
1170 */
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001171int rio_inb_pwrite_handler(struct rio_mport *mport, union rio_pw_msg *pw_msg)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001172{
1173 struct rio_dev *rdev;
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001174 u32 err_status, em_perrdet, em_ltlerrdet;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001175 int rc, portnum;
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001176 struct rio_pwrite *pwrite;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001177
1178#ifdef DEBUG_PW
1179 {
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001180 u32 i;
1181
1182 pr_debug("%s: PW to mport_%d:\n", __func__, mport->id);
1183 for (i = 0; i < RIO_PW_MSG_SIZE / sizeof(u32); i = i + 4) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001184 pr_debug("0x%02x: %08x %08x %08x %08x\n",
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001185 i * 4, pw_msg->raw[i], pw_msg->raw[i + 1],
1186 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
1187 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001188 }
1189#endif
1190
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001191 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
1192 if (rdev) {
1193 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
1194 } else {
1195 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
1196 __func__, pw_msg->em.comptag);
1197 }
1198
1199 /* Call a device-specific handler (if it is registered for the device).
1200 * This may be the service for endpoints that send device-specific
1201 * port-write messages. End-point messages expected to be handled
1202 * completely by EP specific device driver.
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001203 * For switches rc==0 signals that no standard processing required.
1204 */
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001205 if (rdev && rdev->pwcback) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001206 rc = rdev->pwcback(rdev, pw_msg, 0);
1207 if (rc == 0)
1208 return 0;
1209 }
1210
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07001211 mutex_lock(&mport->lock);
1212 list_for_each_entry(pwrite, &mport->pwrites, node)
1213 pwrite->pwcback(mport, pwrite->context, pw_msg, 0);
1214 mutex_unlock(&mport->lock);
1215
1216 if (!rdev)
1217 return 0;
1218
1219 /*
1220 * FIXME: The code below stays as it was before for now until we decide
1221 * how to do default PW handling in combination with per-mport callbacks
1222 */
1223
Alexandre Bounine6429cd42010-10-27 15:34:32 -07001224 portnum = pw_msg->em.is_port & 0xFF;
1225
1226 /* Check if device and route to it are functional:
1227 * Sometimes devices may send PW message(s) just before being
1228 * powered down (or link being lost).
1229 */
1230 if (rio_chk_dev_access(rdev)) {
1231 pr_debug("RIO: device access failed - get link partner\n");
1232 /* Scan route to the device and identify failed link.
1233 * This will replace device and port reported in PW message.
1234 * PW message should not be used after this point.
1235 */
1236 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
1237 pr_err("RIO: Route trace for %s failed\n",
1238 rio_name(rdev));
1239 return -EIO;
1240 }
1241 pw_msg = NULL;
1242 }
1243
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001244 /* For End-point devices processing stops here */
1245 if (!(rdev->pef & RIO_PEF_SWITCH))
1246 return 0;
1247
1248 if (rdev->phys_efptr == 0) {
1249 pr_err("RIO_PW: Bad switch initialization for %s\n",
1250 rio_name(rdev));
1251 return 0;
1252 }
1253
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001254 /*
1255 * Process the port-write notification from switch
1256 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001257 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
1258 rdev->rswitch->ops->em_handle(rdev, portnum);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001259
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001260 rio_read_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001261 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
1262 &err_status);
1263 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
1264
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001265 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001266
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001267 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
1268 rdev->rswitch->port_ok |= (1 << portnum);
1269 rio_set_port_lockout(rdev, portnum, 0);
1270 /* Schedule Insertion Service */
1271 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
1272 rio_name(rdev), portnum);
1273 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001274
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001275 /* Clear error-stopped states (if reported).
1276 * Depending on the link partner state, two attempts
1277 * may be needed for successful recovery.
1278 */
1279 if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
1280 RIO_PORT_N_ERR_STS_PW_INP_ES)) {
1281 if (rio_clr_err_stopped(rdev, portnum, err_status))
1282 rio_clr_err_stopped(rdev, portnum, 0);
1283 }
1284 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001285
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001286 if (rdev->rswitch->port_ok & (1 << portnum)) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001287 rdev->rswitch->port_ok &= ~(1 << portnum);
1288 rio_set_port_lockout(rdev, portnum, 1);
1289
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001290 rio_write_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001291 rdev->phys_efptr +
1292 RIO_PORT_N_ACK_STS_CSR(portnum),
1293 RIO_PORT_N_ACK_CLEAR);
1294
1295 /* Schedule Extraction Service */
1296 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
1297 rio_name(rdev), portnum);
1298 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001299 }
1300
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001301 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001302 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
1303 if (em_perrdet) {
1304 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
1305 portnum, em_perrdet);
1306 /* Clear EM Port N Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001307 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001308 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
1309 }
1310
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001311 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001312 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1313 if (em_ltlerrdet) {
1314 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1315 em_ltlerrdet);
1316 /* Clear EM L/T Layer Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001317 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001318 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1319 }
1320
Alexandre Bounine388c45c2010-10-27 15:34:35 -07001321 /* Clear remaining error bits and Port-Write Pending bit */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001322 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001323 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
Alexandre Bounine388c45c2010-10-27 15:34:35 -07001324 err_status);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001325
1326 return 0;
1327}
1328EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1329
1330/**
1331 * rio_mport_get_efb - get pointer to next extended features block
1332 * @port: Master port to issue transaction
1333 * @local: Indicate a local master port or remote device access
1334 * @destid: Destination ID of the device
1335 * @hopcount: Number of switch hops to the device
1336 * @from: Offset of current Extended Feature block header (if 0 starts
1337 * from ExtFeaturePtr)
1338 */
1339u32
1340rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1341 u8 hopcount, u32 from)
1342{
1343 u32 reg_val;
1344
1345 if (from == 0) {
1346 if (local)
1347 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1348 &reg_val);
1349 else
1350 rio_mport_read_config_32(port, destid, hopcount,
1351 RIO_ASM_INFO_CAR, &reg_val);
1352 return reg_val & RIO_EXT_FTR_PTR_MASK;
1353 } else {
1354 if (local)
1355 rio_local_read_config_32(port, from, &reg_val);
1356 else
1357 rio_mport_read_config_32(port, destid, hopcount,
1358 from, &reg_val);
1359 return RIO_GET_BLOCK_ID(reg_val);
1360 }
1361}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001362EXPORT_SYMBOL_GPL(rio_mport_get_efb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001363
1364/**
Matt Porter394b7012005-11-07 01:00:15 -08001365 * rio_mport_get_feature - query for devices' extended features
1366 * @port: Master port to issue transaction
1367 * @local: Indicate a local master port or remote device access
1368 * @destid: Destination ID of the device
1369 * @hopcount: Number of switch hops to the device
1370 * @ftr: Extended feature code
1371 *
1372 * Tell if a device supports a given RapidIO capability.
1373 * Returns the offset of the requested extended feature
1374 * block within the device's RIO configuration space or
1375 * 0 in case the device does not support it. Possible
1376 * values for @ftr:
1377 *
1378 * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
1379 *
1380 * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
1381 *
1382 * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
1383 *
1384 * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
1385 *
1386 * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
1387 *
1388 * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
1389 */
1390u32
1391rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1392 u8 hopcount, int ftr)
1393{
1394 u32 asm_info, ext_ftr_ptr, ftr_header;
1395
1396 if (local)
1397 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1398 else
1399 rio_mport_read_config_32(port, destid, hopcount,
1400 RIO_ASM_INFO_CAR, &asm_info);
1401
1402 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1403
1404 while (ext_ftr_ptr) {
1405 if (local)
1406 rio_local_read_config_32(port, ext_ftr_ptr,
1407 &ftr_header);
1408 else
1409 rio_mport_read_config_32(port, destid, hopcount,
1410 ext_ftr_ptr, &ftr_header);
1411 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1412 return ext_ftr_ptr;
1413 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
1414 break;
1415 }
1416
1417 return 0;
1418}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001419EXPORT_SYMBOL_GPL(rio_mport_get_feature);
Matt Porter394b7012005-11-07 01:00:15 -08001420
1421/**
1422 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
1423 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1424 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1425 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
1426 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
1427 * @from: Previous RIO device found in search, or %NULL for new search
1428 *
1429 * Iterates through the list of known RIO devices. If a RIO device is
1430 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
1431 * count to the device is incrememted and a pointer to its device
1432 * structure is returned. Otherwise, %NULL is returned. A new search
1433 * is initiated by passing %NULL to the @from argument. Otherwise, if
1434 * @from is not %NULL, searches continue from next device on the global
1435 * list. The reference count for @from is always decremented if it is
1436 * not %NULL.
1437 */
1438struct rio_dev *rio_get_asm(u16 vid, u16 did,
1439 u16 asm_vid, u16 asm_did, struct rio_dev *from)
1440{
1441 struct list_head *n;
1442 struct rio_dev *rdev;
1443
1444 WARN_ON(in_interrupt());
1445 spin_lock(&rio_global_list_lock);
1446 n = from ? from->global_list.next : rio_devices.next;
1447
1448 while (n && (n != &rio_devices)) {
1449 rdev = rio_dev_g(n);
1450 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1451 (did == RIO_ANY_ID || rdev->did == did) &&
1452 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1453 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1454 goto exit;
1455 n = n->next;
1456 }
1457 rdev = NULL;
1458 exit:
1459 rio_dev_put(from);
1460 rdev = rio_dev_get(rdev);
1461 spin_unlock(&rio_global_list_lock);
1462 return rdev;
1463}
1464
1465/**
1466 * rio_get_device - Begin or continue searching for a RIO device by vid/did
1467 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1468 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1469 * @from: Previous RIO device found in search, or %NULL for new search
1470 *
1471 * Iterates through the list of known RIO devices. If a RIO device is
1472 * found with a matching @vid and @did, the reference count to the
1473 * device is incrememted and a pointer to its device structure is returned.
1474 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
1475 * to the @from argument. Otherwise, if @from is not %NULL, searches
1476 * continue from next device on the global list. The reference count for
1477 * @from is always decremented if it is not %NULL.
1478 */
1479struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1480{
1481 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1482}
1483
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001484/**
1485 * rio_std_route_add_entry - Add switch route table entry using standard
1486 * registers defined in RIO specification rev.1.3
1487 * @mport: Master port to issue transaction
1488 * @destid: Destination ID of the device
1489 * @hopcount: Number of switch hops to the device
1490 * @table: routing table ID (global or port-specific)
1491 * @route_destid: destID entry in the RT
1492 * @route_port: destination port for specified destID
1493 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001494static int
1495rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1496 u16 table, u16 route_destid, u8 route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001497{
1498 if (table == RIO_GLOBAL_TABLE) {
1499 rio_mport_write_config_32(mport, destid, hopcount,
1500 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1501 (u32)route_destid);
1502 rio_mport_write_config_32(mport, destid, hopcount,
1503 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1504 (u32)route_port);
1505 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001506
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001507 udelay(10);
1508 return 0;
1509}
1510
1511/**
1512 * rio_std_route_get_entry - Read switch route table entry (port number)
Uwe Kleine-König638c5942010-06-11 12:16:58 +02001513 * associated with specified destID using standard registers defined in RIO
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001514 * specification rev.1.3
1515 * @mport: Master port to issue transaction
1516 * @destid: Destination ID of the device
1517 * @hopcount: Number of switch hops to the device
1518 * @table: routing table ID (global or port-specific)
1519 * @route_destid: destID entry in the RT
1520 * @route_port: returned destination port for specified destID
1521 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001522static int
1523rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1524 u16 table, u16 route_destid, u8 *route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001525{
1526 u32 result;
1527
1528 if (table == RIO_GLOBAL_TABLE) {
1529 rio_mport_write_config_32(mport, destid, hopcount,
1530 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1531 rio_mport_read_config_32(mport, destid, hopcount,
1532 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1533
1534 *route_port = (u8)result;
1535 }
1536
1537 return 0;
1538}
1539
1540/**
1541 * rio_std_route_clr_table - Clear swotch route table using standard registers
1542 * defined in RIO specification rev.1.3.
1543 * @mport: Master port to issue transaction
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001544 * @destid: Destination ID of the device
1545 * @hopcount: Number of switch hops to the device
1546 * @table: routing table ID (global or port-specific)
1547 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001548static int
1549rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1550 u16 table)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001551{
1552 u32 max_destid = 0xff;
1553 u32 i, pef, id_inc = 1, ext_cfg = 0;
1554 u32 port_sel = RIO_INVALID_ROUTE;
1555
1556 if (table == RIO_GLOBAL_TABLE) {
1557 rio_mport_read_config_32(mport, destid, hopcount,
1558 RIO_PEF_CAR, &pef);
1559
1560 if (mport->sys_size) {
1561 rio_mport_read_config_32(mport, destid, hopcount,
1562 RIO_SWITCH_RT_LIMIT,
1563 &max_destid);
1564 max_destid &= RIO_RT_MAX_DESTID;
1565 }
1566
1567 if (pef & RIO_PEF_EXT_RT) {
1568 ext_cfg = 0x80000000;
1569 id_inc = 4;
1570 port_sel = (RIO_INVALID_ROUTE << 24) |
1571 (RIO_INVALID_ROUTE << 16) |
1572 (RIO_INVALID_ROUTE << 8) |
1573 RIO_INVALID_ROUTE;
1574 }
1575
1576 for (i = 0; i <= max_destid;) {
1577 rio_mport_write_config_32(mport, destid, hopcount,
1578 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1579 ext_cfg | i);
1580 rio_mport_write_config_32(mport, destid, hopcount,
1581 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1582 port_sel);
1583 i += id_inc;
1584 }
1585 }
1586
1587 udelay(10);
1588 return 0;
1589}
1590
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001591/**
1592 * rio_lock_device - Acquires host device lock for specified device
1593 * @port: Master port to send transaction
1594 * @destid: Destination ID for device/switch
1595 * @hopcount: Hopcount to reach switch
1596 * @wait_ms: Max wait time in msec (0 = no timeout)
1597 *
1598 * Attepts to acquire host device lock for specified device
1599 * Returns 0 if device lock acquired or EINVAL if timeout expires.
1600 */
1601int rio_lock_device(struct rio_mport *port, u16 destid,
1602 u8 hopcount, int wait_ms)
1603{
1604 u32 result;
1605 int tcnt = 0;
1606
1607 /* Attempt to acquire device lock */
1608 rio_mport_write_config_32(port, destid, hopcount,
1609 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1610 rio_mport_read_config_32(port, destid, hopcount,
1611 RIO_HOST_DID_LOCK_CSR, &result);
1612
1613 while (result != port->host_deviceid) {
1614 if (wait_ms != 0 && tcnt == wait_ms) {
1615 pr_debug("RIO: timeout when locking device %x:%x\n",
1616 destid, hopcount);
1617 return -EINVAL;
1618 }
1619
1620 /* Delay a bit */
1621 mdelay(1);
1622 tcnt++;
1623 /* Try to acquire device lock again */
1624 rio_mport_write_config_32(port, destid,
1625 hopcount,
1626 RIO_HOST_DID_LOCK_CSR,
1627 port->host_deviceid);
1628 rio_mport_read_config_32(port, destid,
1629 hopcount,
1630 RIO_HOST_DID_LOCK_CSR, &result);
1631 }
1632
1633 return 0;
1634}
1635EXPORT_SYMBOL_GPL(rio_lock_device);
1636
1637/**
1638 * rio_unlock_device - Releases host device lock for specified device
1639 * @port: Master port to send transaction
1640 * @destid: Destination ID for device/switch
1641 * @hopcount: Hopcount to reach switch
1642 *
1643 * Returns 0 if device lock released or EINVAL if fails.
1644 */
1645int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1646{
1647 u32 result;
1648
1649 /* Release device lock */
1650 rio_mport_write_config_32(port, destid,
1651 hopcount,
1652 RIO_HOST_DID_LOCK_CSR,
1653 port->host_deviceid);
1654 rio_mport_read_config_32(port, destid, hopcount,
1655 RIO_HOST_DID_LOCK_CSR, &result);
1656 if ((result & 0xffff) != 0xffff) {
1657 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1658 destid, hopcount);
1659 return -EINVAL;
1660 }
1661
1662 return 0;
1663}
1664EXPORT_SYMBOL_GPL(rio_unlock_device);
1665
1666/**
1667 * rio_route_add_entry- Add a route entry to a switch routing table
1668 * @rdev: RIO device
1669 * @table: Routing table ID
1670 * @route_destid: Destination ID to be routed
1671 * @route_port: Port number to be routed
1672 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1673 *
1674 * If available calls the switch specific add_entry() method to add a route
1675 * entry into a switch routing table. Otherwise uses standard RT update method
1676 * as defined by RapidIO specification. A specific routing table can be selected
1677 * using the @table argument if a switch has per port routing tables or
1678 * the standard (or global) table may be used by passing
1679 * %RIO_GLOBAL_TABLE in @table.
1680 *
1681 * Returns %0 on success or %-EINVAL on failure.
1682 */
1683int rio_route_add_entry(struct rio_dev *rdev,
1684 u16 table, u16 route_destid, u8 route_port, int lock)
1685{
1686 int rc = -EINVAL;
1687 struct rio_switch_ops *ops = rdev->rswitch->ops;
1688
1689 if (lock) {
1690 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1691 rdev->hopcount, 1000);
1692 if (rc)
1693 return rc;
1694 }
1695
1696 spin_lock(&rdev->rswitch->lock);
1697
1698 if (ops == NULL || ops->add_entry == NULL) {
1699 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1700 rdev->hopcount, table,
1701 route_destid, route_port);
1702 } else if (try_module_get(ops->owner)) {
1703 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1704 rdev->hopcount, table, route_destid,
1705 route_port);
1706 module_put(ops->owner);
1707 }
1708
1709 spin_unlock(&rdev->rswitch->lock);
1710
1711 if (lock)
1712 rio_unlock_device(rdev->net->hport, rdev->destid,
1713 rdev->hopcount);
1714
1715 return rc;
1716}
1717EXPORT_SYMBOL_GPL(rio_route_add_entry);
1718
1719/**
1720 * rio_route_get_entry- Read an entry from a switch routing table
1721 * @rdev: RIO device
1722 * @table: Routing table ID
1723 * @route_destid: Destination ID to be routed
1724 * @route_port: Pointer to read port number into
1725 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1726 *
1727 * If available calls the switch specific get_entry() method to fetch a route
1728 * entry from a switch routing table. Otherwise uses standard RT read method
1729 * as defined by RapidIO specification. A specific routing table can be selected
1730 * using the @table argument if a switch has per port routing tables or
1731 * the standard (or global) table may be used by passing
1732 * %RIO_GLOBAL_TABLE in @table.
1733 *
1734 * Returns %0 on success or %-EINVAL on failure.
1735 */
1736int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1737 u16 route_destid, u8 *route_port, int lock)
1738{
1739 int rc = -EINVAL;
1740 struct rio_switch_ops *ops = rdev->rswitch->ops;
1741
1742 if (lock) {
1743 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1744 rdev->hopcount, 1000);
1745 if (rc)
1746 return rc;
1747 }
1748
1749 spin_lock(&rdev->rswitch->lock);
1750
1751 if (ops == NULL || ops->get_entry == NULL) {
1752 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1753 rdev->hopcount, table,
1754 route_destid, route_port);
1755 } else if (try_module_get(ops->owner)) {
1756 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1757 rdev->hopcount, table, route_destid,
1758 route_port);
1759 module_put(ops->owner);
1760 }
1761
1762 spin_unlock(&rdev->rswitch->lock);
1763
1764 if (lock)
1765 rio_unlock_device(rdev->net->hport, rdev->destid,
1766 rdev->hopcount);
1767 return rc;
1768}
1769EXPORT_SYMBOL_GPL(rio_route_get_entry);
1770
1771/**
1772 * rio_route_clr_table - Clear a switch routing table
1773 * @rdev: RIO device
1774 * @table: Routing table ID
1775 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1776 *
1777 * If available calls the switch specific clr_table() method to clear a switch
1778 * routing table. Otherwise uses standard RT write method as defined by RapidIO
1779 * specification. A specific routing table can be selected using the @table
1780 * argument if a switch has per port routing tables or the standard (or global)
1781 * table may be used by passing %RIO_GLOBAL_TABLE in @table.
1782 *
1783 * Returns %0 on success or %-EINVAL on failure.
1784 */
1785int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1786{
1787 int rc = -EINVAL;
1788 struct rio_switch_ops *ops = rdev->rswitch->ops;
1789
1790 if (lock) {
1791 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1792 rdev->hopcount, 1000);
1793 if (rc)
1794 return rc;
1795 }
1796
1797 spin_lock(&rdev->rswitch->lock);
1798
1799 if (ops == NULL || ops->clr_table == NULL) {
1800 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1801 rdev->hopcount, table);
1802 } else if (try_module_get(ops->owner)) {
1803 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1804 rdev->hopcount, table);
1805
1806 module_put(ops->owner);
1807 }
1808
1809 spin_unlock(&rdev->rswitch->lock);
1810
1811 if (lock)
1812 rio_unlock_device(rdev->net->hport, rdev->destid,
1813 rdev->hopcount);
1814
1815 return rc;
1816}
1817EXPORT_SYMBOL_GPL(rio_route_clr_table);
1818
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001819#ifdef CONFIG_RAPIDIO_DMA_ENGINE
1820
1821static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1822{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001823 struct rio_mport *mport = arg;
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001824
1825 /* Check that DMA device belongs to the right MPORT */
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001826 return mport == container_of(chan->device, struct rio_mport, dma);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001827}
1828
1829/**
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001830 * rio_request_mport_dma - request RapidIO capable DMA channel associated
1831 * with specified local RapidIO mport device.
1832 * @mport: RIO mport to perform DMA data transfers
1833 *
1834 * Returns pointer to allocated DMA channel or NULL if failed.
1835 */
1836struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
1837{
1838 dma_cap_mask_t mask;
1839
1840 dma_cap_zero(mask);
1841 dma_cap_set(DMA_SLAVE, mask);
1842 return dma_request_channel(mask, rio_chan_filter, mport);
1843}
1844EXPORT_SYMBOL_GPL(rio_request_mport_dma);
1845
1846/**
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001847 * rio_request_dma - request RapidIO capable DMA channel that supports
1848 * specified target RapidIO device.
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001849 * @rdev: RIO device associated with DMA transfer
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001850 *
1851 * Returns pointer to allocated DMA channel or NULL if failed.
1852 */
1853struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1854{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001855 return rio_request_mport_dma(rdev->net->hport);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001856}
1857EXPORT_SYMBOL_GPL(rio_request_dma);
1858
1859/**
1860 * rio_release_dma - release specified DMA channel
1861 * @dchan: DMA channel to release
1862 */
1863void rio_release_dma(struct dma_chan *dchan)
1864{
1865 dma_release_channel(dchan);
1866}
1867EXPORT_SYMBOL_GPL(rio_release_dma);
1868
1869/**
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001870 * rio_dma_prep_xfer - RapidIO specific wrapper
1871 * for device_prep_slave_sg callback defined by DMAENGINE.
1872 * @dchan: DMA channel to configure
1873 * @destid: target RapidIO device destination ID
1874 * @data: RIO specific data descriptor
1875 * @direction: DMA data transfer direction (TO or FROM the device)
1876 * @flags: dmaengine defined flags
1877 *
1878 * Initializes RapidIO capable DMA channel for the specified data transfer.
1879 * Uses DMA channel private extension to pass information related to remote
1880 * target RIO device.
Alexandre Bouninef8e3a682016-08-02 14:06:34 -07001881 *
1882 * Returns: pointer to DMA transaction descriptor if successful,
1883 * error-valued pointer or NULL if failed.
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001884 */
1885struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
1886 u16 destid, struct rio_dma_data *data,
1887 enum dma_transfer_direction direction, unsigned long flags)
1888{
1889 struct rio_dma_ext rio_ext;
1890
1891 if (dchan->device->device_prep_slave_sg == NULL) {
1892 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1893 return NULL;
1894 }
1895
1896 rio_ext.destid = destid;
1897 rio_ext.rio_addr_u = data->rio_addr_u;
1898 rio_ext.rio_addr = data->rio_addr;
1899 rio_ext.wr_type = data->wr_type;
1900
1901 return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1902 direction, flags, &rio_ext);
1903}
1904EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
1905
1906/**
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001907 * rio_dma_prep_slave_sg - RapidIO specific wrapper
1908 * for device_prep_slave_sg callback defined by DMAENGINE.
1909 * @rdev: RIO device control structure
1910 * @dchan: DMA channel to configure
1911 * @data: RIO specific data descriptor
1912 * @direction: DMA data transfer direction (TO or FROM the device)
1913 * @flags: dmaengine defined flags
1914 *
1915 * Initializes RapidIO capable DMA channel for the specified data transfer.
1916 * Uses DMA channel private extension to pass information related to remote
1917 * target RIO device.
Alexandre Bouninef8e3a682016-08-02 14:06:34 -07001918 *
1919 * Returns: pointer to DMA transaction descriptor if successful,
1920 * error-valued pointer or NULL if failed.
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001921 */
1922struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1923 struct dma_chan *dchan, struct rio_dma_data *data,
1924 enum dma_transfer_direction direction, unsigned long flags)
1925{
Alexandre Bounine4aff1ce2014-08-08 14:22:09 -07001926 return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags);
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001927}
1928EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1929
1930#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1931
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001932/**
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07001933 * rio_find_mport - find RIO mport by its ID
1934 * @mport_id: number (ID) of mport device
1935 *
1936 * Given a RIO mport number, the desired mport is located
1937 * in the global list of mports. If the mport is found, a pointer to its
1938 * data structure is returned. If no mport is found, %NULL is returned.
1939 */
1940struct rio_mport *rio_find_mport(int mport_id)
1941{
1942 struct rio_mport *port;
1943
1944 mutex_lock(&rio_mport_list_lock);
1945 list_for_each_entry(port, &rio_mports, node) {
1946 if (port->id == mport_id)
1947 goto found;
1948 }
1949 port = NULL;
1950found:
1951 mutex_unlock(&rio_mport_list_lock);
1952
1953 return port;
1954}
1955
1956/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001957 * rio_register_scan - enumeration/discovery method registration interface
1958 * @mport_id: mport device ID for which fabric scan routine has to be set
1959 * (RIO_MPORT_ANY = set for all available mports)
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001960 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001961 *
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001962 * Registers enumeration/discovery operations with RapidIO subsystem and
1963 * attaches it to the specified mport device (or all available mports
1964 * if RIO_MPORT_ANY is specified).
1965 *
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001966 * Returns error if the mport already has an enumerator attached to it.
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001967 * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001968 */
1969int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1970{
1971 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001972 struct rio_scan_node *scan;
1973 int rc = 0;
1974
1975 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1976
1977 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1978 !scan_ops)
1979 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001980
1981 mutex_lock(&rio_mport_list_lock);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001982
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001983 /*
1984 * Check if there is another enumerator already registered for
1985 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
1986 * for the same mport ID are not supported.
1987 */
1988 list_for_each_entry(scan, &rio_scans, node) {
1989 if (scan->mport_id == mport_id) {
1990 rc = -EBUSY;
1991 goto err_out;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001992 }
1993 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001994
1995 /*
1996 * Allocate and initialize new scan registration node.
1997 */
1998 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
1999 if (!scan) {
2000 rc = -ENOMEM;
2001 goto err_out;
2002 }
2003
2004 scan->mport_id = mport_id;
2005 scan->ops = scan_ops;
2006
2007 /*
2008 * Traverse the list of registered mports to attach this new scan.
2009 *
2010 * The new scan with matching mport ID overrides any previously attached
2011 * scan assuming that old scan (if any) is the default one (based on the
2012 * enumerator registration check above).
2013 * If the new scan is the global one, it will be attached only to mports
2014 * that do not have their own individual operations already attached.
2015 */
2016 list_for_each_entry(port, &rio_mports, node) {
2017 if (port->id == mport_id) {
2018 port->nscan = scan_ops;
2019 break;
2020 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
2021 port->nscan = scan_ops;
2022 }
2023
2024 list_add_tail(&scan->node, &rio_scans);
2025
2026err_out:
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002027 mutex_unlock(&rio_mport_list_lock);
2028
2029 return rc;
2030}
2031EXPORT_SYMBOL_GPL(rio_register_scan);
2032
2033/**
2034 * rio_unregister_scan - removes enumeration/discovery method from mport
2035 * @mport_id: mport device ID for which fabric scan routine has to be
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002036 * unregistered (RIO_MPORT_ANY = apply to all mports that use
2037 * the specified scan_ops)
2038 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002039 *
2040 * Removes enumeration or discovery method assigned to the specified mport
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002041 * device. If RIO_MPORT_ANY is specified, removes the specified operations from
2042 * all mports that have them attached.
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002043 */
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002044int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002045{
2046 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002047 struct rio_scan_node *scan;
2048
2049 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
2050
2051 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
2052 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002053
2054 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002055
2056 list_for_each_entry(port, &rio_mports, node)
2057 if (port->id == mport_id ||
2058 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
2059 port->nscan = NULL;
2060
Dan Carpenterf93f3c42013-07-31 13:53:34 -07002061 list_for_each_entry(scan, &rio_scans, node) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002062 if (scan->mport_id == mport_id) {
2063 list_del(&scan->node);
2064 kfree(scan);
Dan Carpenterf93f3c42013-07-31 13:53:34 -07002065 break;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002066 }
Dan Carpenterf93f3c42013-07-31 13:53:34 -07002067 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002068
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002069 mutex_unlock(&rio_mport_list_lock);
2070
2071 return 0;
2072}
2073EXPORT_SYMBOL_GPL(rio_unregister_scan);
2074
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002075/**
2076 * rio_mport_scan - execute enumeration/discovery on the specified mport
2077 * @mport_id: number (ID) of mport device
2078 */
2079int rio_mport_scan(int mport_id)
2080{
2081 struct rio_mport *port = NULL;
2082 int rc;
2083
2084 mutex_lock(&rio_mport_list_lock);
2085 list_for_each_entry(port, &rio_mports, node) {
2086 if (port->id == mport_id)
2087 goto found;
2088 }
2089 mutex_unlock(&rio_mport_list_lock);
2090 return -ENODEV;
2091found:
2092 if (!port->nscan) {
2093 mutex_unlock(&rio_mport_list_lock);
2094 return -EINVAL;
2095 }
2096
2097 if (!try_module_get(port->nscan->owner)) {
2098 mutex_unlock(&rio_mport_list_lock);
2099 return -ENODEV;
2100 }
2101
2102 mutex_unlock(&rio_mport_list_lock);
2103
2104 if (port->host_deviceid >= 0)
2105 rc = port->nscan->enumerate(port, 0);
2106 else
2107 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
2108
2109 module_put(port->nscan->owner);
2110 return rc;
2111}
2112
Matt Porter394b7012005-11-07 01:00:15 -08002113static void rio_fixup_device(struct rio_dev *dev)
2114{
2115}
2116
Bill Pemberton305c8912012-11-19 13:23:25 -05002117static int rio_init(void)
Matt Porter394b7012005-11-07 01:00:15 -08002118{
2119 struct rio_dev *dev = NULL;
2120
2121 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
2122 rio_fixup_device(dev);
2123 }
2124 return 0;
2125}
2126
Alexandre Bounine005842e2012-10-04 17:16:08 -07002127static struct workqueue_struct *rio_wq;
2128
2129struct rio_disc_work {
2130 struct work_struct work;
2131 struct rio_mport *mport;
2132};
2133
Bill Pemberton305c8912012-11-19 13:23:25 -05002134static void disc_work_handler(struct work_struct *_work)
Alexandre Bounine005842e2012-10-04 17:16:08 -07002135{
2136 struct rio_disc_work *work;
2137
2138 work = container_of(_work, struct rio_disc_work, work);
2139 pr_debug("RIO: discovery work for mport %d %s\n",
2140 work->mport->id, work->mport->name);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002141 if (try_module_get(work->mport->nscan->owner)) {
2142 work->mport->nscan->discover(work->mport, 0);
2143 module_put(work->mport->nscan->owner);
2144 }
Alexandre Bounine005842e2012-10-04 17:16:08 -07002145}
2146
Bill Pemberton305c8912012-11-19 13:23:25 -05002147int rio_init_mports(void)
Matt Porter394b7012005-11-07 01:00:15 -08002148{
Matt Porter394b7012005-11-07 01:00:15 -08002149 struct rio_mport *port;
Alexandre Bounine005842e2012-10-04 17:16:08 -07002150 struct rio_disc_work *work;
Alexandre Bounine25747402012-10-10 15:53:59 -07002151 int n = 0;
Matt Porter394b7012005-11-07 01:00:15 -08002152
Alexandre Bounine25747402012-10-10 15:53:59 -07002153 if (!next_portid)
2154 return -ENODEV;
2155
2156 /*
2157 * First, run enumerations and check if we need to perform discovery
2158 * on any of the registered mports.
2159 */
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002160 mutex_lock(&rio_mport_list_lock);
Matt Porter394b7012005-11-07 01:00:15 -08002161 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002162 if (port->host_deviceid >= 0) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002163 if (port->nscan && try_module_get(port->nscan->owner)) {
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07002164 port->nscan->enumerate(port, 0);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002165 module_put(port->nscan->owner);
2166 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002167 } else
Alexandre Bounine25747402012-10-10 15:53:59 -07002168 n++;
2169 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002170 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine005842e2012-10-04 17:16:08 -07002171
Alexandre Bounine25747402012-10-10 15:53:59 -07002172 if (!n)
2173 goto no_disc;
Alexandre Bounine005842e2012-10-04 17:16:08 -07002174
Alexandre Bounine25747402012-10-10 15:53:59 -07002175 /*
2176 * If we have mports that require discovery schedule a discovery work
2177 * for each of them. If the code below fails to allocate needed
2178 * resources, exit without error to keep results of enumeration
2179 * process (if any).
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002180 * TODO: Implement restart of discovery process for all or
Alexandre Bounine25747402012-10-10 15:53:59 -07002181 * individual discovering mports.
2182 */
2183 rio_wq = alloc_workqueue("riodisc", 0, 0);
2184 if (!rio_wq) {
2185 pr_err("RIO: unable allocate rio_wq\n");
2186 goto no_disc;
2187 }
2188
2189 work = kcalloc(n, sizeof *work, GFP_KERNEL);
2190 if (!work) {
2191 pr_err("RIO: no memory for work struct\n");
2192 destroy_workqueue(rio_wq);
2193 goto no_disc;
2194 }
2195
2196 n = 0;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002197 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07002198 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002199 if (port->host_deviceid < 0 && port->nscan) {
Alexandre Bounine25747402012-10-10 15:53:59 -07002200 work[n].mport = port;
2201 INIT_WORK(&work[n].work, disc_work_handler);
2202 queue_work(rio_wq, &work[n].work);
2203 n++;
Alexandre Bounine005842e2012-10-04 17:16:08 -07002204 }
2205 }
2206
Alexandre Bounine25747402012-10-10 15:53:59 -07002207 flush_workqueue(rio_wq);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002208 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07002209 pr_debug("RIO: destroy discovery workqueue\n");
2210 destroy_workqueue(rio_wq);
2211 kfree(work);
Matt Porter394b7012005-11-07 01:00:15 -08002212
Alexandre Bounine25747402012-10-10 15:53:59 -07002213no_disc:
Alexandre Bounine2f809982011-03-23 16:43:04 -07002214 rio_init();
2215
Alexandre Bouninec1256eb2011-03-23 16:43:06 -07002216 return 0;
Matt Porter394b7012005-11-07 01:00:15 -08002217}
2218
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002219static int rio_get_hdid(int index)
2220{
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07002221 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002222 return -1;
2223
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07002224 return hdid[index];
Alexandre Bounine569fccb2011-03-23 16:43:05 -07002225}
2226
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002227int rio_mport_initialize(struct rio_mport *mport)
2228{
2229 if (next_portid >= RIO_MAX_MPORTS) {
2230 pr_err("RIO: reached specified max number of mports\n");
2231 return -ENODEV;
2232 }
2233
2234 atomic_set(&mport->state, RIO_DEVICE_INITIALIZING);
2235 mport->id = next_portid++;
2236 mport->host_deviceid = rio_get_hdid(mport->id);
2237 mport->nscan = NULL;
Alexandre Bouninea7b4c632016-03-22 14:26:35 -07002238 mutex_init(&mport->lock);
Alexandre Bounineb6cb95e2016-03-22 14:26:41 -07002239 mport->pwe_refcnt = 0;
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07002240 INIT_LIST_HEAD(&mport->pwrites);
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002241
2242 return 0;
2243}
2244EXPORT_SYMBOL_GPL(rio_mport_initialize);
2245
Alexandre Bounine59f99962011-04-14 15:22:14 -07002246int rio_register_mport(struct rio_mport *port)
Matt Porter394b7012005-11-07 01:00:15 -08002247{
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002248 struct rio_scan_node *scan = NULL;
Alexandre Bounine2aaf3082014-04-07 15:38:56 -07002249 int res = 0;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002250
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002251 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002252
2253 /*
2254 * Check if there are any registered enumeration/discovery operations
2255 * that have to be attached to the added mport.
2256 */
2257 list_for_each_entry(scan, &rio_scans, node) {
2258 if (port->id == scan->mport_id ||
2259 scan->mport_id == RIO_MPORT_ANY) {
2260 port->nscan = scan->ops;
2261 if (port->id == scan->mport_id)
2262 break;
2263 }
2264 }
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002265
2266 list_add_tail(&port->node, &rio_mports);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002267 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07002268
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002269 dev_set_name(&port->dev, "rapidio%d", port->id);
2270 port->dev.class = &rio_mport_class;
2271 atomic_set(&port->state, RIO_DEVICE_RUNNING);
2272
2273 res = device_register(&port->dev);
2274 if (res)
2275 dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
2276 port->id, res);
2277 else
2278 dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id);
2279
2280 return res;
Matt Porter394b7012005-11-07 01:00:15 -08002281}
Alexandre Bounine94d9bd42013-07-03 15:08:55 -07002282EXPORT_SYMBOL_GPL(rio_register_mport);
Matt Porter394b7012005-11-07 01:00:15 -08002283
Alexandre Bounineb77a2032016-03-22 14:26:20 -07002284static int rio_mport_cleanup_callback(struct device *dev, void *data)
2285{
2286 struct rio_dev *rdev = to_rio_dev(dev);
2287
2288 if (dev->bus == &rio_bus_type)
2289 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
2290 return 0;
2291}
2292
2293static int rio_net_remove_children(struct rio_net *net)
2294{
2295 /*
2296 * Unregister all RapidIO devices residing on this net (this will
2297 * invoke notification of registered subsystem interfaces as well).
2298 */
2299 device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback);
2300 return 0;
2301}
2302
2303int rio_unregister_mport(struct rio_mport *port)
2304{
2305 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
2306
2307 /* Transition mport to the SHUTDOWN state */
2308 if (atomic_cmpxchg(&port->state,
2309 RIO_DEVICE_RUNNING,
2310 RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) {
2311 pr_err("RIO: %s unexpected state transition for mport %s\n",
2312 __func__, port->name);
2313 }
2314
2315 if (port->net && port->net->hport == port) {
2316 rio_net_remove_children(port->net);
2317 rio_free_net(port->net);
2318 }
2319
2320 /*
2321 * Unregister all RapidIO devices attached to this mport (this will
2322 * invoke notification of registered subsystem interfaces as well).
2323 */
2324 mutex_lock(&rio_mport_list_lock);
2325 list_del(&port->node);
2326 mutex_unlock(&rio_mport_list_lock);
2327 device_unregister(&port->dev);
2328
2329 return 0;
2330}
2331EXPORT_SYMBOL_GPL(rio_unregister_mport);
2332
Matt Porter394b7012005-11-07 01:00:15 -08002333EXPORT_SYMBOL_GPL(rio_local_get_device_id);
2334EXPORT_SYMBOL_GPL(rio_get_device);
2335EXPORT_SYMBOL_GPL(rio_get_asm);
2336EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
2337EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
2338EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
2339EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
2340EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
2341EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
2342EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
2343EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07002344EXPORT_SYMBOL_GPL(rio_init_mports);