rapidio: return an error code only as a constant in two functions
[linux-2.6-block.git] / drivers / rapidio / rio.c
CommitLineData
394b701c
MP
1/*
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 *
fdf90abc 8 * Copyright 2009 - 2013 Integrated Device Technology, Inc.
e5cabeb3 9 * Alex Bounine <alexandre.bounine@idt.com>
e5cabeb3 10 *
394b701c
MP
11 * 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
394b701c
MP
17#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>
de25968c 28#include <linux/slab.h>
5febf1cd 29#include <linux/interrupt.h>
394b701c
MP
30
31#include "rio.h"
32
9a0b0627
AB
33/*
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
fdf90abc
AB
47MODULE_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
a11650e1 58static LIST_HEAD(rio_devices);
e6b585ca 59static LIST_HEAD(rio_nets);
a11650e1
AB
60static DEFINE_SPINLOCK(rio_global_list_lock);
61
394b701c 62static LIST_HEAD(rio_mports);
9edbc30b 63static LIST_HEAD(rio_scans);
a11650e1 64static DEFINE_MUTEX(rio_mport_list_lock);
569fccb6 65static unsigned char next_portid;
da1589f0 66static DEFINE_SPINLOCK(rio_mmap_lock);
394b701c
MP
67
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
e0423236 82 return (RIO_GET_DID(port->sys_size, result));
394b701c
MP
83}
84
8b189fdb
AB
85/**
86 * 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
e6b585ca
AB
102/**
103 * 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{
d1509c09 113 struct rio_net *net = kzalloc(sizeof(*net), GFP_KERNEL);
e6b585ca 114
e6b585ca
AB
115 if (net) {
116 INIT_LIST_HEAD(&net->node);
117 INIT_LIST_HEAD(&net->devices);
118 INIT_LIST_HEAD(&net->switches);
119 INIT_LIST_HEAD(&net->mports);
120 mport->net = net;
121 }
122 return net;
123}
124EXPORT_SYMBOL_GPL(rio_alloc_net);
125
126int rio_add_net(struct rio_net *net)
127{
128 int err;
129
130 err = device_register(&net->dev);
131 if (err)
132 return err;
133 spin_lock(&rio_global_list_lock);
134 list_add_tail(&net->node, &rio_nets);
135 spin_unlock(&rio_global_list_lock);
136
137 return 0;
138}
139EXPORT_SYMBOL_GPL(rio_add_net);
140
141void rio_free_net(struct rio_net *net)
142{
143 spin_lock(&rio_global_list_lock);
144 if (!list_empty(&net->node))
145 list_del(&net->node);
146 spin_unlock(&rio_global_list_lock);
147 if (net->release)
148 net->release(net);
149 device_unregister(&net->dev);
150}
151EXPORT_SYMBOL_GPL(rio_free_net);
152
5024622f
AB
153/**
154 * rio_local_set_device_id - Set the base/extended device id for a port
155 * @port: RIO master port
156 * @did: Device ID value to be written
157 *
158 * Writes the base/extended device id from a device.
159 */
160void rio_local_set_device_id(struct rio_mport *port, u16 did)
161{
162 rio_local_write_config_32(port, RIO_DID_CSR,
163 RIO_SET_DID(port->sys_size, did));
164}
165EXPORT_SYMBOL_GPL(rio_local_set_device_id);
166
a11650e1
AB
167/**
168 * rio_add_device- Adds a RIO device to the device model
169 * @rdev: RIO device
170 *
171 * Adds the RIO device to the global device list and adds the RIO
172 * device to the RIO device list. Creates the generic sysfs nodes
173 * for an RIO device.
174 */
175int rio_add_device(struct rio_dev *rdev)
176{
177 int err;
178
b77a2030 179 atomic_set(&rdev->state, RIO_DEVICE_RUNNING);
b74ec56e 180 err = device_register(&rdev->dev);
a11650e1
AB
181 if (err)
182 return err;
183
184 spin_lock(&rio_global_list_lock);
185 list_add_tail(&rdev->global_list, &rio_devices);
b74ec56e
AB
186 if (rdev->net) {
187 list_add_tail(&rdev->net_list, &rdev->net->devices);
188 if (rdev->pef & RIO_PEF_SWITCH)
189 list_add_tail(&rdev->rswitch->node,
190 &rdev->net->switches);
191 }
a11650e1
AB
192 spin_unlock(&rio_global_list_lock);
193
a11650e1
AB
194 return 0;
195}
196EXPORT_SYMBOL_GPL(rio_add_device);
197
b74ec56e
AB
198/*
199 * rio_del_device - removes a RIO device from the device model
200 * @rdev: RIO device
b77a2030 201 * @state: device state to set during removal process
b74ec56e
AB
202 *
203 * Removes the RIO device to the kernel device list and subsystem's device list.
204 * Clears sysfs entries for the removed device.
205 */
b77a2030 206void rio_del_device(struct rio_dev *rdev, enum rio_device_state state)
b74ec56e
AB
207{
208 pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev));
b77a2030 209 atomic_set(&rdev->state, state);
b74ec56e
AB
210 spin_lock(&rio_global_list_lock);
211 list_del(&rdev->global_list);
212 if (rdev->net) {
213 list_del(&rdev->net_list);
214 if (rdev->pef & RIO_PEF_SWITCH) {
215 list_del(&rdev->rswitch->node);
216 kfree(rdev->rswitch->route_table);
217 }
218 }
219 spin_unlock(&rio_global_list_lock);
b74ec56e
AB
220 device_unregister(&rdev->dev);
221}
222EXPORT_SYMBOL_GPL(rio_del_device);
223
394b701c
MP
224/**
225 * rio_request_inb_mbox - request inbound mailbox service
226 * @mport: RIO master port from which to allocate the mailbox resource
6978bbc0 227 * @dev_id: Device specific pointer to pass on event
394b701c
MP
228 * @mbox: Mailbox number to claim
229 * @entries: Number of entries in inbound mailbox queue
230 * @minb: Callback to execute when inbound message is received
231 *
232 * Requests ownership of an inbound mailbox resource and binds
233 * a callback function to the resource. Returns %0 on success.
234 */
235int rio_request_inb_mbox(struct rio_mport *mport,
6978bbc0 236 void *dev_id,
394b701c
MP
237 int mbox,
238 int entries,
6978bbc0 239 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
394b701c
MP
240 int slot))
241{
f8f06269
AB
242 int rc = -ENOSYS;
243 struct resource *res;
394b701c 244
93dd49af 245 if (!mport->ops->open_inb_mbox)
f8f06269
AB
246 goto out;
247
d1509c09 248 res = kzalloc(sizeof(*res), GFP_KERNEL);
394b701c
MP
249 if (res) {
250 rio_init_mbox_res(res, mbox, mbox);
251
252 /* Make sure this mailbox isn't in use */
e1d66d04
ME
253 rc = request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
254 res);
255 if (rc < 0) {
394b701c
MP
256 kfree(res);
257 goto out;
258 }
259
260 mport->inb_msg[mbox].res = res;
261
262 /* Hook the inbound message callback */
263 mport->inb_msg[mbox].mcback = minb;
264
f8f06269 265 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
06e1b249
AB
266 if (rc) {
267 mport->inb_msg[mbox].mcback = NULL;
268 mport->inb_msg[mbox].res = NULL;
269 release_resource(res);
270 kfree(res);
271 }
394b701c
MP
272 } else
273 rc = -ENOMEM;
274
275 out:
276 return rc;
277}
278
279/**
280 * rio_release_inb_mbox - release inbound mailbox message service
281 * @mport: RIO master port from which to release the mailbox resource
282 * @mbox: Mailbox number to release
283 *
284 * Releases ownership of an inbound mailbox resource. Returns 0
285 * if the request has been satisfied.
286 */
287int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
288{
06e1b249 289 int rc;
394b701c 290
06e1b249
AB
291 if (!mport->ops->close_inb_mbox || !mport->inb_msg[mbox].res)
292 return -EINVAL;
293
294 mport->ops->close_inb_mbox(mport, mbox);
295 mport->inb_msg[mbox].mcback = NULL;
296
297 rc = release_resource(mport->inb_msg[mbox].res);
298 if (rc)
299 return rc;
300
301 kfree(mport->inb_msg[mbox].res);
302 mport->inb_msg[mbox].res = NULL;
303
304 return 0;
394b701c
MP
305}
306
307/**
308 * rio_request_outb_mbox - request outbound mailbox service
309 * @mport: RIO master port from which to allocate the mailbox resource
6978bbc0 310 * @dev_id: Device specific pointer to pass on event
394b701c
MP
311 * @mbox: Mailbox number to claim
312 * @entries: Number of entries in outbound mailbox queue
313 * @moutb: Callback to execute when outbound message is sent
314 *
315 * Requests ownership of an outbound mailbox resource and binds
316 * a callback function to the resource. Returns 0 on success.
317 */
318int rio_request_outb_mbox(struct rio_mport *mport,
6978bbc0 319 void *dev_id,
394b701c
MP
320 int mbox,
321 int entries,
6978bbc0 322 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
394b701c 323{
f8f06269
AB
324 int rc = -ENOSYS;
325 struct resource *res;
394b701c 326
93dd49af 327 if (!mport->ops->open_outb_mbox)
f8f06269
AB
328 goto out;
329
d1509c09 330 res = kzalloc(sizeof(*res), GFP_KERNEL);
394b701c
MP
331 if (res) {
332 rio_init_mbox_res(res, mbox, mbox);
333
334 /* Make sure this outbound mailbox isn't in use */
e1d66d04
ME
335 rc = request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
336 res);
337 if (rc < 0) {
394b701c
MP
338 kfree(res);
339 goto out;
340 }
341
342 mport->outb_msg[mbox].res = res;
343
344 /* Hook the inbound message callback */
345 mport->outb_msg[mbox].mcback = moutb;
346
f8f06269 347 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
06e1b249
AB
348 if (rc) {
349 mport->outb_msg[mbox].mcback = NULL;
350 mport->outb_msg[mbox].res = NULL;
351 release_resource(res);
352 kfree(res);
353 }
394b701c
MP
354 } else
355 rc = -ENOMEM;
356
357 out:
358 return rc;
359}
360
361/**
362 * rio_release_outb_mbox - release outbound mailbox message service
363 * @mport: RIO master port from which to release the mailbox resource
364 * @mbox: Mailbox number to release
365 *
366 * Releases ownership of an inbound mailbox resource. Returns 0
367 * if the request has been satisfied.
368 */
369int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
370{
06e1b249 371 int rc;
394b701c 372
06e1b249
AB
373 if (!mport->ops->close_outb_mbox || !mport->outb_msg[mbox].res)
374 return -EINVAL;
375
376 mport->ops->close_outb_mbox(mport, mbox);
377 mport->outb_msg[mbox].mcback = NULL;
378
379 rc = release_resource(mport->outb_msg[mbox].res);
380 if (rc)
381 return rc;
382
383 kfree(mport->outb_msg[mbox].res);
384 mport->outb_msg[mbox].res = NULL;
385
386 return 0;
394b701c
MP
387}
388
389/**
390 * rio_setup_inb_dbell - bind inbound doorbell callback
391 * @mport: RIO master port to bind the doorbell callback
6978bbc0 392 * @dev_id: Device specific pointer to pass on event
394b701c
MP
393 * @res: Doorbell message resource
394 * @dinb: Callback to execute when doorbell is received
395 *
396 * Adds a doorbell resource/callback pair into a port's
397 * doorbell event list. Returns 0 if the request has been
398 * satisfied.
399 */
400static int
6978bbc0
MP
401rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
402 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
394b701c
MP
403 u16 info))
404{
e1d66d04 405 struct rio_dbell *dbell = kmalloc(sizeof(*dbell), GFP_KERNEL);
394b701c 406
1acd14bf
ME
407 if (!dbell)
408 return -ENOMEM;
394b701c
MP
409
410 dbell->res = res;
411 dbell->dinb = dinb;
6978bbc0 412 dbell->dev_id = dev_id;
394b701c 413
a7b4c636 414 mutex_lock(&mport->lock);
394b701c 415 list_add_tail(&dbell->node, &mport->dbells);
a7b4c636 416 mutex_unlock(&mport->lock);
1acd14bf 417 return 0;
394b701c
MP
418}
419
420/**
421 * rio_request_inb_dbell - request inbound doorbell message service
422 * @mport: RIO master port from which to allocate the doorbell resource
6978bbc0 423 * @dev_id: Device specific pointer to pass on event
394b701c
MP
424 * @start: Doorbell info range start
425 * @end: Doorbell info range end
426 * @dinb: Callback to execute when doorbell is received
427 *
428 * Requests ownership of an inbound doorbell resource and binds
429 * a callback function to the resource. Returns 0 if the request
430 * has been satisfied.
431 */
432int rio_request_inb_dbell(struct rio_mport *mport,
6978bbc0 433 void *dev_id,
394b701c
MP
434 u16 start,
435 u16 end,
6978bbc0 436 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
394b701c
MP
437 u16 dst, u16 info))
438{
002f6f40 439 int rc;
d1509c09 440 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
394b701c
MP
441
442 if (res) {
443 rio_init_dbell_res(res, start, end);
444
445 /* Make sure these doorbells aren't in use */
e1d66d04
ME
446 rc = request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
447 res);
448 if (rc < 0) {
394b701c
MP
449 kfree(res);
450 goto out;
451 }
452
453 /* Hook the doorbell callback */
6978bbc0 454 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
394b701c
MP
455 } else
456 rc = -ENOMEM;
457
458 out:
459 return rc;
460}
461
462/**
463 * rio_release_inb_dbell - release inbound doorbell message service
464 * @mport: RIO master port from which to release the doorbell resource
465 * @start: Doorbell info range start
466 * @end: Doorbell info range end
467 *
468 * Releases ownership of an inbound doorbell resource and removes
469 * callback from the doorbell event list. Returns 0 if the request
470 * has been satisfied.
471 */
472int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
473{
474 int rc = 0, found = 0;
475 struct rio_dbell *dbell;
476
a7b4c636 477 mutex_lock(&mport->lock);
394b701c
MP
478 list_for_each_entry(dbell, &mport->dbells, node) {
479 if ((dbell->res->start == start) && (dbell->res->end == end)) {
a7b4c636 480 list_del(&dbell->node);
394b701c
MP
481 found = 1;
482 break;
483 }
484 }
a7b4c636 485 mutex_unlock(&mport->lock);
394b701c
MP
486
487 /* If we can't find an exact match, fail */
488 if (!found) {
489 rc = -EINVAL;
490 goto out;
491 }
492
394b701c
MP
493 /* Release the doorbell resource */
494 rc = release_resource(dbell->res);
495
496 /* Free the doorbell event */
497 kfree(dbell);
498
499 out:
500 return rc;
501}
502
503/**
504 * rio_request_outb_dbell - request outbound doorbell message range
505 * @rdev: RIO device from which to allocate the doorbell resource
506 * @start: Doorbell message range start
507 * @end: Doorbell message range end
508 *
509 * Requests ownership of a doorbell message range. Returns a resource
510 * if the request has been satisfied or %NULL on failure.
511 */
512struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
513 u16 end)
514{
9a975bee 515 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
394b701c
MP
516
517 if (res) {
518 rio_init_dbell_res(res, start, end);
519
520 /* Make sure these doorbells aren't in use */
521 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
522 < 0) {
523 kfree(res);
524 res = NULL;
525 }
526 }
527
528 return res;
529}
530
531/**
532 * rio_release_outb_dbell - release outbound doorbell message range
533 * @rdev: RIO device from which to release the doorbell resource
534 * @res: Doorbell resource to be freed
535 *
536 * Releases ownership of a doorbell message range. Returns 0 if the
537 * request has been satisfied.
538 */
539int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
540{
541 int rc = release_resource(res);
542
543 kfree(res);
544
545 return rc;
546}
547
e5cabeb3 548/**
9a0b0627
AB
549 * rio_add_mport_pw_handler - add port-write message handler into the list
550 * of mport specific pw handlers
551 * @mport: RIO master port to bind the portwrite callback
552 * @context: Handler specific context to pass on event
553 * @pwcback: Callback to execute when portwrite is received
554 *
555 * Returns 0 if the request has been satisfied.
556 */
557int rio_add_mport_pw_handler(struct rio_mport *mport, void *context,
558 int (*pwcback)(struct rio_mport *mport,
559 void *context, union rio_pw_msg *msg, int step))
560{
d1509c09 561 struct rio_pwrite *pwrite = kzalloc(sizeof(*pwrite), GFP_KERNEL);
9a0b0627 562
1acd14bf
ME
563 if (!pwrite)
564 return -ENOMEM;
9a0b0627
AB
565
566 pwrite->pwcback = pwcback;
567 pwrite->context = context;
568 mutex_lock(&mport->lock);
569 list_add_tail(&pwrite->node, &mport->pwrites);
570 mutex_unlock(&mport->lock);
1acd14bf 571 return 0;
9a0b0627
AB
572}
573EXPORT_SYMBOL_GPL(rio_add_mport_pw_handler);
574
575/**
576 * rio_del_mport_pw_handler - remove port-write message handler from the list
577 * of mport specific pw handlers
578 * @mport: RIO master port to bind the portwrite callback
579 * @context: Registered handler specific context to pass on event
580 * @pwcback: Registered callback function
581 *
582 * Returns 0 if the request has been satisfied.
583 */
584int rio_del_mport_pw_handler(struct rio_mport *mport, void *context,
585 int (*pwcback)(struct rio_mport *mport,
586 void *context, union rio_pw_msg *msg, int step))
587{
588 int rc = -EINVAL;
589 struct rio_pwrite *pwrite;
590
591 mutex_lock(&mport->lock);
592 list_for_each_entry(pwrite, &mport->pwrites, node) {
593 if (pwrite->pwcback == pwcback && pwrite->context == context) {
594 list_del(&pwrite->node);
595 kfree(pwrite);
596 rc = 0;
597 break;
598 }
599 }
600 mutex_unlock(&mport->lock);
601
602 return rc;
603}
604EXPORT_SYMBOL_GPL(rio_del_mport_pw_handler);
605
606/**
607 * rio_request_inb_pwrite - request inbound port-write message service for
608 * specific RapidIO device
97ef6f74 609 * @rdev: RIO device to which register inbound port-write callback routine
e5cabeb3
AB
610 * @pwcback: Callback routine to execute when port-write is received
611 *
612 * Binds a port-write callback function to the RapidIO device.
613 * Returns 0 if the request has been satisfied.
614 */
615int rio_request_inb_pwrite(struct rio_dev *rdev,
616 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
617{
618 int rc = 0;
619
620 spin_lock(&rio_global_list_lock);
93dd49af 621 if (rdev->pwcback)
e5cabeb3
AB
622 rc = -ENOMEM;
623 else
624 rdev->pwcback = pwcback;
625
626 spin_unlock(&rio_global_list_lock);
627 return rc;
628}
629EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
630
631/**
632 * rio_release_inb_pwrite - release inbound port-write message service
9a0b0627 633 * associated with specific RapidIO device
e5cabeb3
AB
634 * @rdev: RIO device which registered for inbound port-write callback
635 *
636 * Removes callback from the rio_dev structure. Returns 0 if the request
637 * has been satisfied.
638 */
639int rio_release_inb_pwrite(struct rio_dev *rdev)
640{
641 int rc = -ENOMEM;
642
643 spin_lock(&rio_global_list_lock);
644 if (rdev->pwcback) {
645 rdev->pwcback = NULL;
646 rc = 0;
647 }
648
649 spin_unlock(&rio_global_list_lock);
650 return rc;
651}
652EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
653
b6cb95e8
AB
654/**
655 * rio_pw_enable - Enables/disables port-write handling by a master port
656 * @mport: Master port associated with port-write handling
657 * @enable: 1=enable, 0=disable
658 */
659void rio_pw_enable(struct rio_mport *mport, int enable)
660{
661 if (mport->ops->pwenable) {
662 mutex_lock(&mport->lock);
663
664 if ((enable && ++mport->pwe_refcnt == 1) ||
665 (!enable && mport->pwe_refcnt && --mport->pwe_refcnt == 0))
666 mport->ops->pwenable(mport, enable);
667 mutex_unlock(&mport->lock);
668 }
669}
670EXPORT_SYMBOL_GPL(rio_pw_enable);
671
da1589f0
AB
672/**
673 * rio_map_inb_region -- Map inbound memory region.
674 * @mport: Master port.
2ca3cb50 675 * @local: physical address of memory region to be mapped
da1589f0
AB
676 * @rbase: RIO base address assigned to this window
677 * @size: Size of the memory region
678 * @rflags: Flags for mapping.
679 *
680 * Return: 0 -- Success.
681 *
682 * This function will create the mapping from RIO space to local memory.
683 */
684int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
685 u64 rbase, u32 size, u32 rflags)
686{
002f6f40 687 int rc;
da1589f0
AB
688 unsigned long flags;
689
690 if (!mport->ops->map_inb)
691 return -1;
692 spin_lock_irqsave(&rio_mmap_lock, flags);
693 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
694 spin_unlock_irqrestore(&rio_mmap_lock, flags);
695 return rc;
696}
697EXPORT_SYMBOL_GPL(rio_map_inb_region);
698
699/**
700 * rio_unmap_inb_region -- Unmap the inbound memory region
701 * @mport: Master port
702 * @lstart: physical address of memory region to be unmapped
703 */
704void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
705{
706 unsigned long flags;
707 if (!mport->ops->unmap_inb)
708 return;
709 spin_lock_irqsave(&rio_mmap_lock, flags);
710 mport->ops->unmap_inb(mport, lstart);
711 spin_unlock_irqrestore(&rio_mmap_lock, flags);
712}
713EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
714
93bdaca5
AB
715/**
716 * rio_map_outb_region -- Map outbound memory region.
717 * @mport: Master port.
718 * @destid: destination id window points to
719 * @rbase: RIO base address window translates to
720 * @size: Size of the memory region
721 * @rflags: Flags for mapping.
722 * @local: physical address of memory region mapped
723 *
724 * Return: 0 -- Success.
725 *
726 * This function will create the mapping from RIO space to local memory.
727 */
728int rio_map_outb_region(struct rio_mport *mport, u16 destid, u64 rbase,
729 u32 size, u32 rflags, dma_addr_t *local)
730{
002f6f40 731 int rc;
93bdaca5
AB
732 unsigned long flags;
733
734 if (!mport->ops->map_outb)
735 return -ENODEV;
736
737 spin_lock_irqsave(&rio_mmap_lock, flags);
738 rc = mport->ops->map_outb(mport, destid, rbase, size,
739 rflags, local);
740 spin_unlock_irqrestore(&rio_mmap_lock, flags);
741
742 return rc;
743}
744EXPORT_SYMBOL_GPL(rio_map_outb_region);
745
746/**
747 * rio_unmap_inb_region -- Unmap the inbound memory region
748 * @mport: Master port
749 * @destid: destination id mapping points to
750 * @rstart: RIO base address window translates to
751 */
752void rio_unmap_outb_region(struct rio_mport *mport, u16 destid, u64 rstart)
753{
754 unsigned long flags;
755
756 if (!mport->ops->unmap_outb)
757 return;
758
759 spin_lock_irqsave(&rio_mmap_lock, flags);
760 mport->ops->unmap_outb(mport, destid, rstart);
761 spin_unlock_irqrestore(&rio_mmap_lock, flags);
762}
763EXPORT_SYMBOL_GPL(rio_unmap_outb_region);
764
e5cabeb3
AB
765/**
766 * rio_mport_get_physefb - Helper function that returns register offset
767 * for Physical Layer Extended Features Block.
97ef6f74
RD
768 * @port: Master port to issue transaction
769 * @local: Indicate a local master port or remote device access
770 * @destid: Destination ID of the device
771 * @hopcount: Number of switch hops to the device
1ae842de 772 * @rmap: pointer to location to store register map type info
e5cabeb3
AB
773 */
774u32
775rio_mport_get_physefb(struct rio_mport *port, int local,
1ae842de 776 u16 destid, u8 hopcount, u32 *rmap)
e5cabeb3
AB
777{
778 u32 ext_ftr_ptr;
779 u32 ftr_header;
780
781 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
782
783 while (ext_ftr_ptr) {
784 if (local)
785 rio_local_read_config_32(port, ext_ftr_ptr,
786 &ftr_header);
787 else
788 rio_mport_read_config_32(port, destid, hopcount,
789 ext_ftr_ptr, &ftr_header);
790
791 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
792 switch (ftr_header) {
793
e5cabeb3
AB
794 case RIO_EFB_SER_EP_ID:
795 case RIO_EFB_SER_EP_REC_ID:
796 case RIO_EFB_SER_EP_FREE_ID:
1ae842de
AB
797 case RIO_EFB_SER_EP_M1_ID:
798 case RIO_EFB_SER_EP_SW_M1_ID:
799 case RIO_EFB_SER_EPF_M1_ID:
800 case RIO_EFB_SER_EPF_SW_M1_ID:
801 *rmap = 1;
802 return ext_ftr_ptr;
e5cabeb3 803
1ae842de
AB
804 case RIO_EFB_SER_EP_M2_ID:
805 case RIO_EFB_SER_EP_SW_M2_ID:
806 case RIO_EFB_SER_EPF_M2_ID:
807 case RIO_EFB_SER_EPF_SW_M2_ID:
808 *rmap = 2;
e5cabeb3
AB
809 return ext_ftr_ptr;
810
811 default:
812 break;
813 }
814
815 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
816 hopcount, ext_ftr_ptr);
817 }
818
819 return ext_ftr_ptr;
820}
a11650e1 821EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
e5cabeb3
AB
822
823/**
824 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
97ef6f74 825 * @comp_tag: RIO component tag to match
e5cabeb3
AB
826 * @from: Previous RIO device found in search, or %NULL for new search
827 *
828 * Iterates through the list of known RIO devices. If a RIO device is
829 * found with a matching @comp_tag, a pointer to its device
830 * structure is returned. Otherwise, %NULL is returned. A new search
831 * is initiated by passing %NULL to the @from argument. Otherwise, if
832 * @from is not %NULL, searches continue from next device on the global
833 * list.
834 */
af84ca38 835struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
e5cabeb3
AB
836{
837 struct list_head *n;
838 struct rio_dev *rdev;
839
e5cabeb3
AB
840 spin_lock(&rio_global_list_lock);
841 n = from ? from->global_list.next : rio_devices.next;
842
843 while (n && (n != &rio_devices)) {
844 rdev = rio_dev_g(n);
845 if (rdev->comp_tag == comp_tag)
846 goto exit;
847 n = n->next;
848 }
849 rdev = NULL;
850exit:
851 spin_unlock(&rio_global_list_lock);
852 return rdev;
853}
a11650e1 854EXPORT_SYMBOL_GPL(rio_get_comptag);
e5cabeb3
AB
855
856/**
857 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
858 * @rdev: Pointer to RIO device control structure
859 * @pnum: Switch port number to set LOCKOUT bit
860 * @lock: Operation : set (=1) or clear (=0)
861 */
862int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
863{
e5cabeb3
AB
864 u32 regval;
865
a93192a5 866 rio_read_config_32(rdev,
1ae842de
AB
867 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
868 &regval);
e5cabeb3
AB
869 if (lock)
870 regval |= RIO_PORT_N_CTL_LOCKOUT;
871 else
872 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
873
a93192a5 874 rio_write_config_32(rdev,
1ae842de
AB
875 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
876 regval);
e5cabeb3
AB
877 return 0;
878}
a11650e1
AB
879EXPORT_SYMBOL_GPL(rio_set_port_lockout);
880
a11650e1
AB
881/**
882 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
883 * given port
884 * @port: Master port associated with the RIO network
885 * @local: local=1 select local port otherwise a far device is reached
886 * @destid: Destination ID of the device to check host bit
887 * @hopcount: Number of hops to reach the target
888 * @port_num: Port (-number on switch) to enable on a far end device
889 *
890 * Returns 0 or 1 from on General Control Command and Status Register
891 * (EXT_PTR+0x3C)
892 */
893int rio_enable_rx_tx_port(struct rio_mport *port,
894 int local, u16 destid,
895 u8 hopcount, u8 port_num)
896{
897#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
898 u32 regval;
899 u32 ext_ftr_ptr;
1ae842de 900 u32 rmap;
a11650e1
AB
901
902 /*
903 * enable rx input tx output port
904 */
905 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
906 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
907
1ae842de
AB
908 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid,
909 hopcount, &rmap);
a11650e1
AB
910
911 if (local) {
1ae842de
AB
912 rio_local_read_config_32(port,
913 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap),
a11650e1
AB
914 &regval);
915 } else {
916 if (rio_mport_read_config_32(port, destid, hopcount,
1ae842de
AB
917 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
918 &regval) < 0)
a11650e1
AB
919 return -EIO;
920 }
921
1ae842de 922 regval = regval | RIO_PORT_N_CTL_EN_RX | RIO_PORT_N_CTL_EN_TX;
a11650e1
AB
923
924 if (local) {
1ae842de
AB
925 rio_local_write_config_32(port,
926 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap), regval);
a11650e1
AB
927 } else {
928 if (rio_mport_write_config_32(port, destid, hopcount,
1ae842de
AB
929 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
930 regval) < 0)
a11650e1
AB
931 return -EIO;
932 }
933#endif
934 return 0;
935}
936EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
937
e5cabeb3 938
6429cd49
AB
939/**
940 * rio_chk_dev_route - Validate route to the specified device.
941 * @rdev: RIO device failed to respond
942 * @nrdev: Last active device on the route to rdev
943 * @npnum: nrdev's port number on the route to rdev
944 *
945 * Follows a route to the specified RIO device to determine the last available
946 * device (and corresponding RIO port) on the route.
947 */
948static int
949rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
950{
951 u32 result;
a93192a5 952 int p_port, rc = -EIO;
6429cd49
AB
953 struct rio_dev *prev = NULL;
954
955 /* Find switch with failed RIO link */
956 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
957 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
958 prev = rdev->prev;
959 break;
960 }
961 rdev = rdev->prev;
962 }
963
93dd49af 964 if (!prev)
6429cd49
AB
965 goto err_out;
966
a93192a5 967 p_port = prev->rswitch->route_table[rdev->destid];
6429cd49 968
af84ca38 969 if (p_port != RIO_INVALID_ROUTE) {
6429cd49
AB
970 pr_debug("RIO: link failed on [%s]-P%d\n",
971 rio_name(prev), p_port);
972 *nrdev = prev;
973 *npnum = p_port;
974 rc = 0;
975 } else
af84ca38 976 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
6429cd49
AB
977err_out:
978 return rc;
979}
980
981/**
982 * rio_mport_chk_dev_access - Validate access to the specified device.
983 * @mport: Master port to send transactions
984 * @destid: Device destination ID in network
985 * @hopcount: Number of hops into the network
986 */
e274e0ed 987int
6429cd49
AB
988rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
989{
990 int i = 0;
991 u32 tmp;
992
993 while (rio_mport_read_config_32(mport, destid, hopcount,
994 RIO_DEV_ID_CAR, &tmp)) {
995 i++;
996 if (i == RIO_MAX_CHK_RETRY)
997 return -EIO;
998 mdelay(1);
999 }
1000
1001 return 0;
1002}
a11650e1 1003EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
6429cd49
AB
1004
1005/**
1006 * rio_chk_dev_access - Validate access to the specified device.
1007 * @rdev: Pointer to RIO device control structure
1008 */
1009static int rio_chk_dev_access(struct rio_dev *rdev)
1010{
a93192a5
AB
1011 return rio_mport_chk_dev_access(rdev->net->hport,
1012 rdev->destid, rdev->hopcount);
6429cd49
AB
1013}
1014
dd5648c9
AB
1015/**
1016 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
1017 * returns link-response (if requested).
1018 * @rdev: RIO devive to issue Input-status command
1019 * @pnum: Device port number to issue the command
1020 * @lnkresp: Response from a link partner
1021 */
1022static int
1023rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
1024{
dd5648c9
AB
1025 u32 regval;
1026 int checkcount;
1027
1028 if (lnkresp) {
1029 /* Read from link maintenance response register
1030 * to clear valid bit */
a93192a5 1031 rio_read_config_32(rdev,
1ae842de 1032 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
dd5648c9
AB
1033 &regval);
1034 udelay(50);
1035 }
1036
1037 /* Issue Input-status command */
a93192a5 1038 rio_write_config_32(rdev,
1ae842de 1039 RIO_DEV_PORT_N_MNT_REQ_CSR(rdev, pnum),
dd5648c9
AB
1040 RIO_MNT_REQ_CMD_IS);
1041
1042 /* Exit if the response is not expected */
93dd49af 1043 if (!lnkresp)
dd5648c9
AB
1044 return 0;
1045
1046 checkcount = 3;
1047 while (checkcount--) {
1048 udelay(50);
a93192a5 1049 rio_read_config_32(rdev,
1ae842de 1050 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
dd5648c9
AB
1051 &regval);
1052 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
1053 *lnkresp = regval;
1054 return 0;
1055 }
1056 }
1057
1058 return -EIO;
1059}
1060
1061/**
1062 * rio_clr_err_stopped - Clears port Error-stopped states.
1063 * @rdev: Pointer to RIO device control structure
1064 * @pnum: Switch port number to clear errors
1065 * @err_status: port error status (if 0 reads register from device)
1ae842de
AB
1066 *
1067 * TODO: Currently this routine is not compatible with recovery process
1068 * specified for idt_gen3 RapidIO switch devices. It has to be reviewed
1069 * to implement universal recovery process that is compatible full range
1070 * off available devices.
1071 * IDT gen3 switch driver now implements HW-specific error handler that
1072 * issues soft port reset to the port to reset ERR_STOP bits and ackIDs.
dd5648c9
AB
1073 */
1074static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
1075{
dd5648c9
AB
1076 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
1077 u32 regval;
1078 u32 far_ackid, far_linkstat, near_ackid;
1079
1080 if (err_status == 0)
a93192a5 1081 rio_read_config_32(rdev,
1ae842de 1082 RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
dd5648c9
AB
1083 &err_status);
1084
1ae842de 1085 if (err_status & RIO_PORT_N_ERR_STS_OUT_ES) {
dd5648c9
AB
1086 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
1087 /*
1088 * Send a Link-Request/Input-Status control symbol
1089 */
1090 if (rio_get_input_status(rdev, pnum, &regval)) {
1091 pr_debug("RIO_EM: Input-status response timeout\n");
1092 goto rd_err;
1093 }
1094
1095 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
1096 pnum, regval);
1097 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
1098 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
a93192a5 1099 rio_read_config_32(rdev,
1ae842de 1100 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
dd5648c9
AB
1101 &regval);
1102 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
1103 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
1104 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
1105 " near_ackID=0x%02x\n",
1106 pnum, far_ackid, far_linkstat, near_ackid);
1107
1108 /*
1109 * If required, synchronize ackIDs of near and
1110 * far sides.
1111 */
1112 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
1113 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
1114 /* Align near outstanding/outbound ackIDs with
1115 * far inbound.
1116 */
a93192a5 1117 rio_write_config_32(rdev,
1ae842de 1118 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
dd5648c9
AB
1119 (near_ackid << 24) |
1120 (far_ackid << 8) | far_ackid);
1121 /* Align far outstanding/outbound ackIDs with
1122 * near inbound.
1123 */
1124 far_ackid++;
1ae842de
AB
1125 if (!nextdev) {
1126 pr_debug("RIO_EM: nextdev pointer == NULL\n");
1127 goto rd_err;
1128 }
1129
1130 rio_write_config_32(nextdev,
1131 RIO_DEV_PORT_N_ACK_STS_CSR(nextdev,
1132 RIO_GET_PORT_NUM(nextdev->swpinfo)),
1133 (far_ackid << 24) |
1134 (near_ackid << 8) | near_ackid);
dd5648c9
AB
1135 }
1136rd_err:
1ae842de
AB
1137 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1138 &err_status);
dd5648c9
AB
1139 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1140 }
1141
1ae842de 1142 if ((err_status & RIO_PORT_N_ERR_STS_INP_ES) && nextdev) {
dd5648c9
AB
1143 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
1144 rio_get_input_status(nextdev,
1145 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
1146 udelay(50);
1147
1ae842de
AB
1148 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1149 &err_status);
dd5648c9
AB
1150 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1151 }
1152
1ae842de
AB
1153 return (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
1154 RIO_PORT_N_ERR_STS_INP_ES)) ? 1 : 0;
dd5648c9
AB
1155}
1156
e5cabeb3 1157/**
9a0b0627
AB
1158 * rio_inb_pwrite_handler - inbound port-write message handler
1159 * @mport: mport device associated with port-write
e5cabeb3
AB
1160 * @pw_msg: pointer to inbound port-write message
1161 *
1162 * Processes an inbound port-write message. Returns 0 if the request
1163 * has been satisfied.
1164 */
9a0b0627 1165int rio_inb_pwrite_handler(struct rio_mport *mport, union rio_pw_msg *pw_msg)
e5cabeb3
AB
1166{
1167 struct rio_dev *rdev;
dd5648c9 1168 u32 err_status, em_perrdet, em_ltlerrdet;
e5cabeb3 1169 int rc, portnum;
9a0b0627 1170 struct rio_pwrite *pwrite;
e5cabeb3
AB
1171
1172#ifdef DEBUG_PW
1173 {
9a0b0627
AB
1174 u32 i;
1175
1176 pr_debug("%s: PW to mport_%d:\n", __func__, mport->id);
1177 for (i = 0; i < RIO_PW_MSG_SIZE / sizeof(u32); i = i + 4) {
dd5648c9 1178 pr_debug("0x%02x: %08x %08x %08x %08x\n",
9a0b0627
AB
1179 i * 4, pw_msg->raw[i], pw_msg->raw[i + 1],
1180 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
1181 }
e5cabeb3
AB
1182 }
1183#endif
1184
9a0b0627
AB
1185 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
1186 if (rdev) {
1187 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
1188 } else {
1189 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
1190 __func__, pw_msg->em.comptag);
1191 }
1192
1193 /* Call a device-specific handler (if it is registered for the device).
1194 * This may be the service for endpoints that send device-specific
1195 * port-write messages. End-point messages expected to be handled
1196 * completely by EP specific device driver.
e5cabeb3
AB
1197 * For switches rc==0 signals that no standard processing required.
1198 */
9a0b0627 1199 if (rdev && rdev->pwcback) {
e5cabeb3
AB
1200 rc = rdev->pwcback(rdev, pw_msg, 0);
1201 if (rc == 0)
1202 return 0;
1203 }
1204
9a0b0627
AB
1205 mutex_lock(&mport->lock);
1206 list_for_each_entry(pwrite, &mport->pwrites, node)
1207 pwrite->pwcback(mport, pwrite->context, pw_msg, 0);
1208 mutex_unlock(&mport->lock);
1209
1210 if (!rdev)
1211 return 0;
1212
1213 /*
1214 * FIXME: The code below stays as it was before for now until we decide
1215 * how to do default PW handling in combination with per-mport callbacks
1216 */
1217
6429cd49
AB
1218 portnum = pw_msg->em.is_port & 0xFF;
1219
1220 /* Check if device and route to it are functional:
1221 * Sometimes devices may send PW message(s) just before being
1222 * powered down (or link being lost).
1223 */
1224 if (rio_chk_dev_access(rdev)) {
1225 pr_debug("RIO: device access failed - get link partner\n");
1226 /* Scan route to the device and identify failed link.
1227 * This will replace device and port reported in PW message.
1228 * PW message should not be used after this point.
1229 */
1230 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
1231 pr_err("RIO: Route trace for %s failed\n",
1232 rio_name(rdev));
1233 return -EIO;
1234 }
1235 pw_msg = NULL;
1236 }
1237
e5cabeb3
AB
1238 /* For End-point devices processing stops here */
1239 if (!(rdev->pef & RIO_PEF_SWITCH))
1240 return 0;
1241
1242 if (rdev->phys_efptr == 0) {
1243 pr_err("RIO_PW: Bad switch initialization for %s\n",
1244 rio_name(rdev));
1245 return 0;
1246 }
1247
e5cabeb3
AB
1248 /*
1249 * Process the port-write notification from switch
1250 */
2ec3ba69
AB
1251 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
1252 rdev->rswitch->ops->em_handle(rdev, portnum);
e5cabeb3 1253
1ae842de
AB
1254 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1255 &err_status);
e5cabeb3
AB
1256 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
1257
dd5648c9 1258 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
e5cabeb3 1259
dd5648c9
AB
1260 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
1261 rdev->rswitch->port_ok |= (1 << portnum);
1262 rio_set_port_lockout(rdev, portnum, 0);
1263 /* Schedule Insertion Service */
1264 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
1265 rio_name(rdev), portnum);
1266 }
e5cabeb3 1267
dd5648c9
AB
1268 /* Clear error-stopped states (if reported).
1269 * Depending on the link partner state, two attempts
1270 * may be needed for successful recovery.
1271 */
1ae842de
AB
1272 if (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
1273 RIO_PORT_N_ERR_STS_INP_ES)) {
dd5648c9
AB
1274 if (rio_clr_err_stopped(rdev, portnum, err_status))
1275 rio_clr_err_stopped(rdev, portnum, 0);
1276 }
1277 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
e5cabeb3 1278
dd5648c9 1279 if (rdev->rswitch->port_ok & (1 << portnum)) {
e5cabeb3
AB
1280 rdev->rswitch->port_ok &= ~(1 << portnum);
1281 rio_set_port_lockout(rdev, portnum, 1);
1282
1ae842de 1283 if (rdev->phys_rmap == 1) {
a93192a5 1284 rio_write_config_32(rdev,
1ae842de 1285 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, portnum),
e5cabeb3 1286 RIO_PORT_N_ACK_CLEAR);
1ae842de
AB
1287 } else {
1288 rio_write_config_32(rdev,
1289 RIO_DEV_PORT_N_OB_ACK_CSR(rdev, portnum),
1290 RIO_PORT_N_OB_ACK_CLEAR);
1291 rio_write_config_32(rdev,
1292 RIO_DEV_PORT_N_IB_ACK_CSR(rdev, portnum),
1293 0);
1294 }
e5cabeb3
AB
1295
1296 /* Schedule Extraction Service */
1297 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
1298 rio_name(rdev), portnum);
1299 }
dd5648c9 1300 }
e5cabeb3 1301
a93192a5 1302 rio_read_config_32(rdev,
dd5648c9
AB
1303 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
1304 if (em_perrdet) {
1305 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
1306 portnum, em_perrdet);
1307 /* Clear EM Port N Error Detect CSR */
a93192a5 1308 rio_write_config_32(rdev,
dd5648c9
AB
1309 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
1310 }
1311
a93192a5 1312 rio_read_config_32(rdev,
dd5648c9
AB
1313 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1314 if (em_ltlerrdet) {
1315 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1316 em_ltlerrdet);
1317 /* Clear EM L/T Layer Error Detect CSR */
a93192a5 1318 rio_write_config_32(rdev,
dd5648c9 1319 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
e5cabeb3
AB
1320 }
1321
388c45cc 1322 /* Clear remaining error bits and Port-Write Pending bit */
1ae842de
AB
1323 rio_write_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1324 err_status);
e5cabeb3
AB
1325
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}
a11650e1 1362EXPORT_SYMBOL_GPL(rio_mport_get_efb);
e5cabeb3 1363
394b701c
MP
1364/**
1365 * 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
1ae842de 1375 * 0 in case the device does not support it.
394b701c
MP
1376 */
1377u32
1378rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1379 u8 hopcount, int ftr)
1380{
1381 u32 asm_info, ext_ftr_ptr, ftr_header;
1382
1383 if (local)
1384 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1385 else
1386 rio_mport_read_config_32(port, destid, hopcount,
1387 RIO_ASM_INFO_CAR, &asm_info);
1388
1389 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1390
1391 while (ext_ftr_ptr) {
1392 if (local)
1393 rio_local_read_config_32(port, ext_ftr_ptr,
1394 &ftr_header);
1395 else
1396 rio_mport_read_config_32(port, destid, hopcount,
1397 ext_ftr_ptr, &ftr_header);
1398 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1399 return ext_ftr_ptr;
e1d66d04
ME
1400
1401 ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header);
1402 if (!ext_ftr_ptr)
394b701c
MP
1403 break;
1404 }
1405
1406 return 0;
1407}
a11650e1 1408EXPORT_SYMBOL_GPL(rio_mport_get_feature);
394b701c
MP
1409
1410/**
1411 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
1412 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1413 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1414 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
1415 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
1416 * @from: Previous RIO device found in search, or %NULL for new search
1417 *
1418 * Iterates through the list of known RIO devices. If a RIO device is
1419 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
1420 * count to the device is incrememted and a pointer to its device
1421 * structure is returned. Otherwise, %NULL is returned. A new search
1422 * is initiated by passing %NULL to the @from argument. Otherwise, if
1423 * @from is not %NULL, searches continue from next device on the global
1424 * list. The reference count for @from is always decremented if it is
1425 * not %NULL.
1426 */
1427struct rio_dev *rio_get_asm(u16 vid, u16 did,
1428 u16 asm_vid, u16 asm_did, struct rio_dev *from)
1429{
1430 struct list_head *n;
1431 struct rio_dev *rdev;
1432
1433 WARN_ON(in_interrupt());
1434 spin_lock(&rio_global_list_lock);
1435 n = from ? from->global_list.next : rio_devices.next;
1436
1437 while (n && (n != &rio_devices)) {
1438 rdev = rio_dev_g(n);
1439 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1440 (did == RIO_ANY_ID || rdev->did == did) &&
1441 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1442 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1443 goto exit;
1444 n = n->next;
1445 }
1446 rdev = NULL;
1447 exit:
1448 rio_dev_put(from);
1449 rdev = rio_dev_get(rdev);
1450 spin_unlock(&rio_global_list_lock);
1451 return rdev;
1452}
1453
1454/**
1455 * rio_get_device - Begin or continue searching for a RIO device by vid/did
1456 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1457 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1458 * @from: Previous RIO device found in search, or %NULL for new search
1459 *
1460 * Iterates through the list of known RIO devices. If a RIO device is
1461 * found with a matching @vid and @did, the reference count to the
1462 * device is incrememted and a pointer to its device structure is returned.
1463 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
1464 * to the @from argument. Otherwise, if @from is not %NULL, searches
1465 * continue from next device on the global list. The reference count for
1466 * @from is always decremented if it is not %NULL.
1467 */
1468struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1469{
1470 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1471}
1472
07590ff0
AB
1473/**
1474 * rio_std_route_add_entry - Add switch route table entry using standard
1475 * registers defined in RIO specification rev.1.3
1476 * @mport: Master port to issue transaction
1477 * @destid: Destination ID of the device
1478 * @hopcount: Number of switch hops to the device
1479 * @table: routing table ID (global or port-specific)
1480 * @route_destid: destID entry in the RT
1481 * @route_port: destination port for specified destID
1482 */
2ec3ba69
AB
1483static int
1484rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1485 u16 table, u16 route_destid, u8 route_port)
07590ff0
AB
1486{
1487 if (table == RIO_GLOBAL_TABLE) {
1488 rio_mport_write_config_32(mport, destid, hopcount,
1489 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1490 (u32)route_destid);
1491 rio_mport_write_config_32(mport, destid, hopcount,
1492 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1493 (u32)route_port);
1494 }
e5cabeb3 1495
07590ff0
AB
1496 udelay(10);
1497 return 0;
1498}
1499
1500/**
1501 * rio_std_route_get_entry - Read switch route table entry (port number)
638c5945 1502 * associated with specified destID using standard registers defined in RIO
07590ff0
AB
1503 * specification rev.1.3
1504 * @mport: Master port to issue transaction
1505 * @destid: Destination ID of the device
1506 * @hopcount: Number of switch hops to the device
1507 * @table: routing table ID (global or port-specific)
1508 * @route_destid: destID entry in the RT
1509 * @route_port: returned destination port for specified destID
1510 */
2ec3ba69
AB
1511static int
1512rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1513 u16 table, u16 route_destid, u8 *route_port)
07590ff0
AB
1514{
1515 u32 result;
1516
1517 if (table == RIO_GLOBAL_TABLE) {
1518 rio_mport_write_config_32(mport, destid, hopcount,
1519 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1520 rio_mport_read_config_32(mport, destid, hopcount,
1521 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1522
1523 *route_port = (u8)result;
1524 }
1525
1526 return 0;
1527}
1528
1529/**
1530 * rio_std_route_clr_table - Clear swotch route table using standard registers
1531 * defined in RIO specification rev.1.3.
1532 * @mport: Master port to issue transaction
07590ff0
AB
1533 * @destid: Destination ID of the device
1534 * @hopcount: Number of switch hops to the device
1535 * @table: routing table ID (global or port-specific)
1536 */
2ec3ba69
AB
1537static int
1538rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1539 u16 table)
07590ff0
AB
1540{
1541 u32 max_destid = 0xff;
1542 u32 i, pef, id_inc = 1, ext_cfg = 0;
1543 u32 port_sel = RIO_INVALID_ROUTE;
1544
1545 if (table == RIO_GLOBAL_TABLE) {
1546 rio_mport_read_config_32(mport, destid, hopcount,
1547 RIO_PEF_CAR, &pef);
1548
1549 if (mport->sys_size) {
1550 rio_mport_read_config_32(mport, destid, hopcount,
1551 RIO_SWITCH_RT_LIMIT,
1552 &max_destid);
1553 max_destid &= RIO_RT_MAX_DESTID;
1554 }
1555
1556 if (pef & RIO_PEF_EXT_RT) {
1557 ext_cfg = 0x80000000;
1558 id_inc = 4;
1559 port_sel = (RIO_INVALID_ROUTE << 24) |
1560 (RIO_INVALID_ROUTE << 16) |
1561 (RIO_INVALID_ROUTE << 8) |
1562 RIO_INVALID_ROUTE;
1563 }
1564
1565 for (i = 0; i <= max_destid;) {
1566 rio_mport_write_config_32(mport, destid, hopcount,
1567 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1568 ext_cfg | i);
1569 rio_mport_write_config_32(mport, destid, hopcount,
1570 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1571 port_sel);
1572 i += id_inc;
1573 }
1574 }
1575
1576 udelay(10);
1577 return 0;
1578}
1579
2ec3ba69
AB
1580/**
1581 * rio_lock_device - Acquires host device lock for specified device
1582 * @port: Master port to send transaction
1583 * @destid: Destination ID for device/switch
1584 * @hopcount: Hopcount to reach switch
1585 * @wait_ms: Max wait time in msec (0 = no timeout)
1586 *
1587 * Attepts to acquire host device lock for specified device
1588 * Returns 0 if device lock acquired or EINVAL if timeout expires.
1589 */
1590int rio_lock_device(struct rio_mport *port, u16 destid,
1591 u8 hopcount, int wait_ms)
1592{
1593 u32 result;
1594 int tcnt = 0;
1595
1596 /* Attempt to acquire device lock */
1597 rio_mport_write_config_32(port, destid, hopcount,
1598 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1599 rio_mport_read_config_32(port, destid, hopcount,
1600 RIO_HOST_DID_LOCK_CSR, &result);
1601
1602 while (result != port->host_deviceid) {
1603 if (wait_ms != 0 && tcnt == wait_ms) {
1604 pr_debug("RIO: timeout when locking device %x:%x\n",
1605 destid, hopcount);
1606 return -EINVAL;
1607 }
1608
1609 /* Delay a bit */
1610 mdelay(1);
1611 tcnt++;
1612 /* Try to acquire device lock again */
1613 rio_mport_write_config_32(port, destid,
1614 hopcount,
1615 RIO_HOST_DID_LOCK_CSR,
1616 port->host_deviceid);
1617 rio_mport_read_config_32(port, destid,
1618 hopcount,
1619 RIO_HOST_DID_LOCK_CSR, &result);
1620 }
1621
1622 return 0;
1623}
1624EXPORT_SYMBOL_GPL(rio_lock_device);
1625
1626/**
1627 * rio_unlock_device - Releases host device lock for specified device
1628 * @port: Master port to send transaction
1629 * @destid: Destination ID for device/switch
1630 * @hopcount: Hopcount to reach switch
1631 *
1632 * Returns 0 if device lock released or EINVAL if fails.
1633 */
1634int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1635{
1636 u32 result;
1637
1638 /* Release device lock */
1639 rio_mport_write_config_32(port, destid,
1640 hopcount,
1641 RIO_HOST_DID_LOCK_CSR,
1642 port->host_deviceid);
1643 rio_mport_read_config_32(port, destid, hopcount,
1644 RIO_HOST_DID_LOCK_CSR, &result);
1645 if ((result & 0xffff) != 0xffff) {
1646 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1647 destid, hopcount);
1648 return -EINVAL;
1649 }
1650
1651 return 0;
1652}
1653EXPORT_SYMBOL_GPL(rio_unlock_device);
1654
1655/**
1656 * rio_route_add_entry- Add a route entry to a switch routing table
1657 * @rdev: RIO device
1658 * @table: Routing table ID
1659 * @route_destid: Destination ID to be routed
1660 * @route_port: Port number to be routed
1661 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1662 *
1663 * If available calls the switch specific add_entry() method to add a route
1664 * entry into a switch routing table. Otherwise uses standard RT update method
1665 * as defined by RapidIO specification. A specific routing table can be selected
1666 * using the @table argument if a switch has per port routing tables or
1667 * the standard (or global) table may be used by passing
1668 * %RIO_GLOBAL_TABLE in @table.
1669 *
1670 * Returns %0 on success or %-EINVAL on failure.
1671 */
1672int rio_route_add_entry(struct rio_dev *rdev,
1673 u16 table, u16 route_destid, u8 route_port, int lock)
1674{
1675 int rc = -EINVAL;
1676 struct rio_switch_ops *ops = rdev->rswitch->ops;
1677
1678 if (lock) {
1679 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1680 rdev->hopcount, 1000);
1681 if (rc)
1682 return rc;
1683 }
1684
1685 spin_lock(&rdev->rswitch->lock);
1686
93dd49af 1687 if (!ops || !ops->add_entry) {
2ec3ba69
AB
1688 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1689 rdev->hopcount, table,
1690 route_destid, route_port);
1691 } else if (try_module_get(ops->owner)) {
1692 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1693 rdev->hopcount, table, route_destid,
1694 route_port);
1695 module_put(ops->owner);
1696 }
1697
1698 spin_unlock(&rdev->rswitch->lock);
1699
1700 if (lock)
1701 rio_unlock_device(rdev->net->hport, rdev->destid,
1702 rdev->hopcount);
1703
1704 return rc;
1705}
1706EXPORT_SYMBOL_GPL(rio_route_add_entry);
1707
1708/**
1709 * rio_route_get_entry- Read an entry from a switch routing table
1710 * @rdev: RIO device
1711 * @table: Routing table ID
1712 * @route_destid: Destination ID to be routed
1713 * @route_port: Pointer to read port number into
1714 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1715 *
1716 * If available calls the switch specific get_entry() method to fetch a route
1717 * entry from a switch routing table. Otherwise uses standard RT read method
1718 * as defined by RapidIO specification. A specific routing table can be selected
1719 * using the @table argument if a switch has per port routing tables or
1720 * the standard (or global) table may be used by passing
1721 * %RIO_GLOBAL_TABLE in @table.
1722 *
1723 * Returns %0 on success or %-EINVAL on failure.
1724 */
1725int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1726 u16 route_destid, u8 *route_port, int lock)
1727{
1728 int rc = -EINVAL;
1729 struct rio_switch_ops *ops = rdev->rswitch->ops;
1730
1731 if (lock) {
1732 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1733 rdev->hopcount, 1000);
1734 if (rc)
1735 return rc;
1736 }
1737
1738 spin_lock(&rdev->rswitch->lock);
1739
93dd49af 1740 if (!ops || !ops->get_entry) {
2ec3ba69
AB
1741 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1742 rdev->hopcount, table,
1743 route_destid, route_port);
1744 } else if (try_module_get(ops->owner)) {
1745 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1746 rdev->hopcount, table, route_destid,
1747 route_port);
1748 module_put(ops->owner);
1749 }
1750
1751 spin_unlock(&rdev->rswitch->lock);
1752
1753 if (lock)
1754 rio_unlock_device(rdev->net->hport, rdev->destid,
1755 rdev->hopcount);
1756 return rc;
1757}
1758EXPORT_SYMBOL_GPL(rio_route_get_entry);
1759
1760/**
1761 * rio_route_clr_table - Clear a switch routing table
1762 * @rdev: RIO device
1763 * @table: Routing table ID
1764 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1765 *
1766 * If available calls the switch specific clr_table() method to clear a switch
1767 * routing table. Otherwise uses standard RT write method as defined by RapidIO
1768 * specification. A specific routing table can be selected using the @table
1769 * argument if a switch has per port routing tables or the standard (or global)
1770 * table may be used by passing %RIO_GLOBAL_TABLE in @table.
1771 *
1772 * Returns %0 on success or %-EINVAL on failure.
1773 */
1774int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1775{
1776 int rc = -EINVAL;
1777 struct rio_switch_ops *ops = rdev->rswitch->ops;
1778
1779 if (lock) {
1780 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1781 rdev->hopcount, 1000);
1782 if (rc)
1783 return rc;
1784 }
1785
1786 spin_lock(&rdev->rswitch->lock);
1787
93dd49af 1788 if (!ops || !ops->clr_table) {
2ec3ba69
AB
1789 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1790 rdev->hopcount, table);
1791 } else if (try_module_get(ops->owner)) {
1792 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1793 rdev->hopcount, table);
1794
1795 module_put(ops->owner);
1796 }
1797
1798 spin_unlock(&rdev->rswitch->lock);
1799
1800 if (lock)
1801 rio_unlock_device(rdev->net->hport, rdev->destid,
1802 rdev->hopcount);
1803
1804 return rc;
1805}
1806EXPORT_SYMBOL_GPL(rio_route_clr_table);
1807
e42d98eb
AB
1808#ifdef CONFIG_RAPIDIO_DMA_ENGINE
1809
1810static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1811{
4aff1ce7 1812 struct rio_mport *mport = arg;
e42d98eb
AB
1813
1814 /* Check that DMA device belongs to the right MPORT */
4aff1ce7 1815 return mport == container_of(chan->device, struct rio_mport, dma);
e42d98eb
AB
1816}
1817
1818/**
4aff1ce7
AB
1819 * rio_request_mport_dma - request RapidIO capable DMA channel associated
1820 * with specified local RapidIO mport device.
1821 * @mport: RIO mport to perform DMA data transfers
e42d98eb
AB
1822 *
1823 * Returns pointer to allocated DMA channel or NULL if failed.
1824 */
4aff1ce7 1825struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
e42d98eb
AB
1826{
1827 dma_cap_mask_t mask;
e42d98eb
AB
1828
1829 dma_cap_zero(mask);
1830 dma_cap_set(DMA_SLAVE, mask);
4aff1ce7
AB
1831 return dma_request_channel(mask, rio_chan_filter, mport);
1832}
1833EXPORT_SYMBOL_GPL(rio_request_mport_dma);
e42d98eb 1834
4aff1ce7
AB
1835/**
1836 * rio_request_dma - request RapidIO capable DMA channel that supports
1837 * specified target RapidIO device.
1838 * @rdev: RIO device associated with DMA transfer
1839 *
1840 * Returns pointer to allocated DMA channel or NULL if failed.
1841 */
1842struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1843{
1844 return rio_request_mport_dma(rdev->net->hport);
e42d98eb
AB
1845}
1846EXPORT_SYMBOL_GPL(rio_request_dma);
1847
1848/**
1849 * rio_release_dma - release specified DMA channel
1850 * @dchan: DMA channel to release
1851 */
1852void rio_release_dma(struct dma_chan *dchan)
1853{
1854 dma_release_channel(dchan);
1855}
1856EXPORT_SYMBOL_GPL(rio_release_dma);
1857
1858/**
4aff1ce7 1859 * rio_dma_prep_xfer - RapidIO specific wrapper
e42d98eb 1860 * for device_prep_slave_sg callback defined by DMAENGINE.
e42d98eb 1861 * @dchan: DMA channel to configure
4aff1ce7 1862 * @destid: target RapidIO device destination ID
e42d98eb
AB
1863 * @data: RIO specific data descriptor
1864 * @direction: DMA data transfer direction (TO or FROM the device)
1865 * @flags: dmaengine defined flags
1866 *
1867 * Initializes RapidIO capable DMA channel for the specified data transfer.
1868 * Uses DMA channel private extension to pass information related to remote
1869 * target RIO device.
f8e3a68c
AB
1870 *
1871 * Returns: pointer to DMA transaction descriptor if successful,
1872 * error-valued pointer or NULL if failed.
e42d98eb 1873 */
4aff1ce7
AB
1874struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
1875 u16 destid, struct rio_dma_data *data,
e42d98eb
AB
1876 enum dma_transfer_direction direction, unsigned long flags)
1877{
e42d98eb
AB
1878 struct rio_dma_ext rio_ext;
1879
93dd49af 1880 if (!dchan->device->device_prep_slave_sg) {
e42d98eb
AB
1881 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1882 return NULL;
1883 }
1884
4aff1ce7 1885 rio_ext.destid = destid;
e42d98eb
AB
1886 rio_ext.rio_addr_u = data->rio_addr_u;
1887 rio_ext.rio_addr = data->rio_addr;
1888 rio_ext.wr_type = data->wr_type;
1889
4aff1ce7
AB
1890 return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1891 direction, flags, &rio_ext);
1892}
1893EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
e42d98eb 1894
4aff1ce7
AB
1895/**
1896 * rio_dma_prep_slave_sg - RapidIO specific wrapper
1897 * for device_prep_slave_sg callback defined by DMAENGINE.
1898 * @rdev: RIO device control structure
1899 * @dchan: DMA channel to configure
1900 * @data: RIO specific data descriptor
1901 * @direction: DMA data transfer direction (TO or FROM the device)
1902 * @flags: dmaengine defined flags
1903 *
1904 * Initializes RapidIO capable DMA channel for the specified data transfer.
1905 * Uses DMA channel private extension to pass information related to remote
1906 * target RIO device.
f8e3a68c
AB
1907 *
1908 * Returns: pointer to DMA transaction descriptor if successful,
1909 * error-valued pointer or NULL if failed.
4aff1ce7
AB
1910 */
1911struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1912 struct dma_chan *dchan, struct rio_dma_data *data,
1913 enum dma_transfer_direction direction, unsigned long flags)
1914{
1915 return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags);
e42d98eb
AB
1916}
1917EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1918
1919#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1920
bc8fcfea
AB
1921/**
1922 * rio_find_mport - find RIO mport by its ID
1923 * @mport_id: number (ID) of mport device
1924 *
1925 * Given a RIO mport number, the desired mport is located
1926 * in the global list of mports. If the mport is found, a pointer to its
1927 * data structure is returned. If no mport is found, %NULL is returned.
1928 */
1929struct rio_mport *rio_find_mport(int mport_id)
1930{
1931 struct rio_mport *port;
1932
1933 mutex_lock(&rio_mport_list_lock);
1934 list_for_each_entry(port, &rio_mports, node) {
1935 if (port->id == mport_id)
1936 goto found;
1937 }
1938 port = NULL;
1939found:
1940 mutex_unlock(&rio_mport_list_lock);
1941
1942 return port;
1943}
1944
a11650e1
AB
1945/**
1946 * rio_register_scan - enumeration/discovery method registration interface
1947 * @mport_id: mport device ID for which fabric scan routine has to be set
1948 * (RIO_MPORT_ANY = set for all available mports)
9edbc30b
AB
1949 * @scan_ops: enumeration/discovery operations structure
1950 *
1951 * Registers enumeration/discovery operations with RapidIO subsystem and
1952 * attaches it to the specified mport device (or all available mports
1953 * if RIO_MPORT_ANY is specified).
a11650e1 1954 *
a11650e1 1955 * Returns error if the mport already has an enumerator attached to it.
9edbc30b 1956 * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
a11650e1
AB
1957 */
1958int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1959{
1960 struct rio_mport *port;
9edbc30b
AB
1961 struct rio_scan_node *scan;
1962 int rc = 0;
a11650e1 1963
9edbc30b 1964 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
a11650e1 1965
9edbc30b
AB
1966 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1967 !scan_ops)
1968 return -EINVAL;
a11650e1 1969
9edbc30b
AB
1970 mutex_lock(&rio_mport_list_lock);
1971
1972 /*
1973 * Check if there is another enumerator already registered for
1974 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
1975 * for the same mport ID are not supported.
1976 */
1977 list_for_each_entry(scan, &rio_scans, node) {
1978 if (scan->mport_id == mport_id) {
1979 rc = -EBUSY;
1980 goto err_out;
a11650e1
AB
1981 }
1982 }
9edbc30b
AB
1983
1984 /*
1985 * Allocate and initialize new scan registration node.
1986 */
1987 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
1988 if (!scan) {
1989 rc = -ENOMEM;
1990 goto err_out;
1991 }
1992
1993 scan->mport_id = mport_id;
1994 scan->ops = scan_ops;
1995
1996 /*
1997 * Traverse the list of registered mports to attach this new scan.
1998 *
1999 * The new scan with matching mport ID overrides any previously attached
2000 * scan assuming that old scan (if any) is the default one (based on the
2001 * enumerator registration check above).
2002 * If the new scan is the global one, it will be attached only to mports
2003 * that do not have their own individual operations already attached.
2004 */
2005 list_for_each_entry(port, &rio_mports, node) {
2006 if (port->id == mport_id) {
2007 port->nscan = scan_ops;
2008 break;
2009 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
2010 port->nscan = scan_ops;
2011 }
2012
2013 list_add_tail(&scan->node, &rio_scans);
2014
2015err_out:
a11650e1
AB
2016 mutex_unlock(&rio_mport_list_lock);
2017
2018 return rc;
2019}
2020EXPORT_SYMBOL_GPL(rio_register_scan);
2021
2022/**
2023 * rio_unregister_scan - removes enumeration/discovery method from mport
2024 * @mport_id: mport device ID for which fabric scan routine has to be
9edbc30b
AB
2025 * unregistered (RIO_MPORT_ANY = apply to all mports that use
2026 * the specified scan_ops)
2027 * @scan_ops: enumeration/discovery operations structure
a11650e1
AB
2028 *
2029 * Removes enumeration or discovery method assigned to the specified mport
9edbc30b
AB
2030 * device. If RIO_MPORT_ANY is specified, removes the specified operations from
2031 * all mports that have them attached.
a11650e1 2032 */
9edbc30b 2033int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
a11650e1
AB
2034{
2035 struct rio_mport *port;
9edbc30b
AB
2036 struct rio_scan_node *scan;
2037
2038 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
2039
2040 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
2041 return -EINVAL;
a11650e1
AB
2042
2043 mutex_lock(&rio_mport_list_lock);
9edbc30b
AB
2044
2045 list_for_each_entry(port, &rio_mports, node)
2046 if (port->id == mport_id ||
2047 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
2048 port->nscan = NULL;
2049
f93f3c4e 2050 list_for_each_entry(scan, &rio_scans, node) {
9edbc30b
AB
2051 if (scan->mport_id == mport_id) {
2052 list_del(&scan->node);
2053 kfree(scan);
f93f3c4e 2054 break;
a11650e1 2055 }
f93f3c4e 2056 }
9edbc30b 2057
a11650e1
AB
2058 mutex_unlock(&rio_mport_list_lock);
2059
2060 return 0;
2061}
2062EXPORT_SYMBOL_GPL(rio_unregister_scan);
2063
9edbc30b
AB
2064/**
2065 * rio_mport_scan - execute enumeration/discovery on the specified mport
2066 * @mport_id: number (ID) of mport device
2067 */
2068int rio_mport_scan(int mport_id)
2069{
2070 struct rio_mport *port = NULL;
2071 int rc;
2072
2073 mutex_lock(&rio_mport_list_lock);
2074 list_for_each_entry(port, &rio_mports, node) {
2075 if (port->id == mport_id)
2076 goto found;
2077 }
2078 mutex_unlock(&rio_mport_list_lock);
2079 return -ENODEV;
2080found:
2081 if (!port->nscan) {
2082 mutex_unlock(&rio_mport_list_lock);
2083 return -EINVAL;
2084 }
2085
2086 if (!try_module_get(port->nscan->owner)) {
2087 mutex_unlock(&rio_mport_list_lock);
2088 return -ENODEV;
2089 }
2090
2091 mutex_unlock(&rio_mport_list_lock);
2092
2093 if (port->host_deviceid >= 0)
2094 rc = port->nscan->enumerate(port, 0);
2095 else
2096 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
2097
2098 module_put(port->nscan->owner);
2099 return rc;
2100}
2101
394b701c
MP
2102static void rio_fixup_device(struct rio_dev *dev)
2103{
2104}
2105
305c891e 2106static int rio_init(void)
394b701c
MP
2107{
2108 struct rio_dev *dev = NULL;
2109
2110 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
2111 rio_fixup_device(dev);
2112 }
2113 return 0;
2114}
2115
005842ef
AB
2116static struct workqueue_struct *rio_wq;
2117
2118struct rio_disc_work {
2119 struct work_struct work;
2120 struct rio_mport *mport;
2121};
2122
305c891e 2123static void disc_work_handler(struct work_struct *_work)
005842ef
AB
2124{
2125 struct rio_disc_work *work;
2126
2127 work = container_of(_work, struct rio_disc_work, work);
2128 pr_debug("RIO: discovery work for mport %d %s\n",
2129 work->mport->id, work->mport->name);
9edbc30b
AB
2130 if (try_module_get(work->mport->nscan->owner)) {
2131 work->mport->nscan->discover(work->mport, 0);
2132 module_put(work->mport->nscan->owner);
2133 }
005842ef
AB
2134}
2135
305c891e 2136int rio_init_mports(void)
394b701c 2137{
394b701c 2138 struct rio_mport *port;
005842ef 2139 struct rio_disc_work *work;
2574740d
AB
2140 int n = 0;
2141
2142 if (!next_portid)
2143 return -ENODEV;
394b701c 2144
2574740d
AB
2145 /*
2146 * First, run enumerations and check if we need to perform discovery
2147 * on any of the registered mports.
2148 */
a11650e1 2149 mutex_lock(&rio_mport_list_lock);
394b701c 2150 list_for_each_entry(port, &rio_mports, node) {
a11650e1 2151 if (port->host_deviceid >= 0) {
9edbc30b 2152 if (port->nscan && try_module_get(port->nscan->owner)) {
bc8fcfea 2153 port->nscan->enumerate(port, 0);
9edbc30b
AB
2154 module_put(port->nscan->owner);
2155 }
a11650e1 2156 } else
2574740d
AB
2157 n++;
2158 }
a11650e1 2159 mutex_unlock(&rio_mport_list_lock);
2574740d
AB
2160
2161 if (!n)
2162 goto no_disc;
2163
2164 /*
2165 * If we have mports that require discovery schedule a discovery work
2166 * for each of them. If the code below fails to allocate needed
2167 * resources, exit without error to keep results of enumeration
2168 * process (if any).
9edbc30b 2169 * TODO: Implement restart of discovery process for all or
2574740d
AB
2170 * individual discovering mports.
2171 */
2172 rio_wq = alloc_workqueue("riodisc", 0, 0);
2173 if (!rio_wq) {
2174 pr_err("RIO: unable allocate rio_wq\n");
2175 goto no_disc;
005842ef
AB
2176 }
2177
2574740d
AB
2178 work = kcalloc(n, sizeof *work, GFP_KERNEL);
2179 if (!work) {
005842ef 2180 destroy_workqueue(rio_wq);
2574740d 2181 goto no_disc;
394b701c
MP
2182 }
2183
2574740d 2184 n = 0;
a11650e1 2185 mutex_lock(&rio_mport_list_lock);
2574740d 2186 list_for_each_entry(port, &rio_mports, node) {
a11650e1 2187 if (port->host_deviceid < 0 && port->nscan) {
2574740d
AB
2188 work[n].mport = port;
2189 INIT_WORK(&work[n].work, disc_work_handler);
2190 queue_work(rio_wq, &work[n].work);
2191 n++;
2192 }
2193 }
2194
2195 flush_workqueue(rio_wq);
9edbc30b 2196 mutex_unlock(&rio_mport_list_lock);
2574740d
AB
2197 pr_debug("RIO: destroy discovery workqueue\n");
2198 destroy_workqueue(rio_wq);
2199 kfree(work);
2200
2201no_disc:
2f809985
AB
2202 rio_init();
2203
c1256ebe 2204 return 0;
394b701c
MP
2205}
2206
569fccb6
AB
2207static int rio_get_hdid(int index)
2208{
fdf90abc 2209 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
569fccb6
AB
2210 return -1;
2211
fdf90abc 2212 return hdid[index];
569fccb6
AB
2213}
2214
b77a2030 2215int rio_mport_initialize(struct rio_mport *mport)
394b701c 2216{
569fccb6
AB
2217 if (next_portid >= RIO_MAX_MPORTS) {
2218 pr_err("RIO: reached specified max number of mports\n");
b77a2030 2219 return -ENODEV;
569fccb6
AB
2220 }
2221
b77a2030
AB
2222 atomic_set(&mport->state, RIO_DEVICE_INITIALIZING);
2223 mport->id = next_portid++;
2224 mport->host_deviceid = rio_get_hdid(mport->id);
2225 mport->nscan = NULL;
a7b4c636 2226 mutex_init(&mport->lock);
b6cb95e8 2227 mport->pwe_refcnt = 0;
9a0b0627 2228 INIT_LIST_HEAD(&mport->pwrites);
9edbc30b 2229
b77a2030
AB
2230 return 0;
2231}
2232EXPORT_SYMBOL_GPL(rio_mport_initialize);
2aaf308b 2233
b77a2030
AB
2234int rio_register_mport(struct rio_mport *port)
2235{
2236 struct rio_scan_node *scan = NULL;
2237 int res = 0;
2aaf308b 2238
a11650e1 2239 mutex_lock(&rio_mport_list_lock);
9edbc30b
AB
2240
2241 /*
2242 * Check if there are any registered enumeration/discovery operations
2243 * that have to be attached to the added mport.
2244 */
2245 list_for_each_entry(scan, &rio_scans, node) {
2246 if (port->id == scan->mport_id ||
2247 scan->mport_id == RIO_MPORT_ANY) {
2248 port->nscan = scan->ops;
2249 if (port->id == scan->mport_id)
2250 break;
2251 }
2252 }
b77a2030
AB
2253
2254 list_add_tail(&port->node, &rio_mports);
a11650e1 2255 mutex_unlock(&rio_mport_list_lock);
9edbc30b 2256
b77a2030
AB
2257 dev_set_name(&port->dev, "rapidio%d", port->id);
2258 port->dev.class = &rio_mport_class;
2259 atomic_set(&port->state, RIO_DEVICE_RUNNING);
2260
2261 res = device_register(&port->dev);
2262 if (res)
2263 dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
2264 port->id, res);
2265 else
2266 dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id);
2267
2268 return res;
2269}
2270EXPORT_SYMBOL_GPL(rio_register_mport);
2271
2272static int rio_mport_cleanup_callback(struct device *dev, void *data)
2273{
2274 struct rio_dev *rdev = to_rio_dev(dev);
2275
2276 if (dev->bus == &rio_bus_type)
2277 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
2278 return 0;
2279}
2280
2281static int rio_net_remove_children(struct rio_net *net)
2282{
2283 /*
2284 * Unregister all RapidIO devices residing on this net (this will
2285 * invoke notification of registered subsystem interfaces as well).
2286 */
2287 device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback);
2288 return 0;
2289}
2290
2291int rio_unregister_mport(struct rio_mport *port)
2292{
9edbc30b 2293 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
b77a2030
AB
2294
2295 /* Transition mport to the SHUTDOWN state */
2296 if (atomic_cmpxchg(&port->state,
2297 RIO_DEVICE_RUNNING,
2298 RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) {
2299 pr_err("RIO: %s unexpected state transition for mport %s\n",
2300 __func__, port->name);
2301 }
2302
2303 if (port->net && port->net->hport == port) {
2304 rio_net_remove_children(port->net);
2305 rio_free_net(port->net);
2306 }
2307
2308 /*
2309 * Unregister all RapidIO devices attached to this mport (this will
2310 * invoke notification of registered subsystem interfaces as well).
2311 */
2312 mutex_lock(&rio_mport_list_lock);
2313 list_del(&port->node);
2314 mutex_unlock(&rio_mport_list_lock);
2315 device_unregister(&port->dev);
2316
59f99965 2317 return 0;
394b701c 2318}
b77a2030 2319EXPORT_SYMBOL_GPL(rio_unregister_mport);
394b701c
MP
2320
2321EXPORT_SYMBOL_GPL(rio_local_get_device_id);
2322EXPORT_SYMBOL_GPL(rio_get_device);
2323EXPORT_SYMBOL_GPL(rio_get_asm);
2324EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
2325EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
2326EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
2327EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
2328EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
2329EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
2330EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
2331EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
a11650e1 2332EXPORT_SYMBOL_GPL(rio_init_mports);