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