blob: 2054b620882300df67e59cd0e74cc85cfdced56a [file] [log] [blame]
Matt Porter394b7012005-11-07 01:00:15 -08001/*
2 * RapidIO interconnect services
3 * (RapidIO Interconnect Specification, http://www.rapidio.org)
4 *
5 * Copyright 2005 MontaVista Software, Inc.
6 * Matt Porter <mporter@kernel.crashing.org>
7 *
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07008 * Copyright 2009 Integrated Device Technology, Inc.
9 * Alex Bounine <alexandre.bounine@idt.com>
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -070010 *
Matt Porter394b7012005-11-07 01:00:15 -080011 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
Matt Porter394b7012005-11-07 01:00:15 -080017#include <linux/types.h>
18#include <linux/kernel.h>
19
20#include <linux/delay.h>
21#include <linux/init.h>
22#include <linux/rio.h>
23#include <linux/rio_drv.h>
24#include <linux/rio_ids.h>
25#include <linux/rio_regs.h>
26#include <linux/module.h>
27#include <linux/spinlock.h>
Tim Schmielaude259682006-01-08 01:02:05 -080028#include <linux/slab.h>
Kumar Gala5febf1c2008-01-23 05:53:47 -060029#include <linux/interrupt.h>
Matt Porter394b7012005-11-07 01:00:15 -080030
31#include "rio.h"
32
Alexandre Bouninea11650e2013-05-24 15:55:05 -070033static LIST_HEAD(rio_devices);
34static DEFINE_SPINLOCK(rio_global_list_lock);
35
Matt Porter394b7012005-11-07 01:00:15 -080036static LIST_HEAD(rio_mports);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -070037static LIST_HEAD(rio_scans);
Alexandre Bouninea11650e2013-05-24 15:55:05 -070038static DEFINE_MUTEX(rio_mport_list_lock);
Alexandre Bounine569fccb2011-03-23 16:43:05 -070039static unsigned char next_portid;
Alexandre Bounineda1589f2012-10-04 17:15:57 -070040static DEFINE_SPINLOCK(rio_mmap_lock);
Matt Porter394b7012005-11-07 01:00:15 -080041
42/**
43 * rio_local_get_device_id - Get the base/extended device id for a port
44 * @port: RIO master port from which to get the deviceid
45 *
46 * Reads the base/extended device id from the local device
47 * implementing the master port. Returns the 8/16-bit device
48 * id.
49 */
50u16 rio_local_get_device_id(struct rio_mport *port)
51{
52 u32 result;
53
54 rio_local_read_config_32(port, RIO_DID_CSR, &result);
55
Zhang Weie0423232008-04-18 13:33:42 -070056 return (RIO_GET_DID(port->sys_size, result));
Matt Porter394b7012005-11-07 01:00:15 -080057}
58
59/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -070060 * rio_add_device- Adds a RIO device to the device model
61 * @rdev: RIO device
62 *
63 * Adds the RIO device to the global device list and adds the RIO
64 * device to the RIO device list. Creates the generic sysfs nodes
65 * for an RIO device.
66 */
67int rio_add_device(struct rio_dev *rdev)
68{
69 int err;
70
71 err = device_add(&rdev->dev);
72 if (err)
73 return err;
74
75 spin_lock(&rio_global_list_lock);
76 list_add_tail(&rdev->global_list, &rio_devices);
77 spin_unlock(&rio_global_list_lock);
78
79 rio_create_sysfs_dev_files(rdev);
80
81 return 0;
82}
83EXPORT_SYMBOL_GPL(rio_add_device);
84
85/**
Matt Porter394b7012005-11-07 01:00:15 -080086 * rio_request_inb_mbox - request inbound mailbox service
87 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -080088 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -080089 * @mbox: Mailbox number to claim
90 * @entries: Number of entries in inbound mailbox queue
91 * @minb: Callback to execute when inbound message is received
92 *
93 * Requests ownership of an inbound mailbox resource and binds
94 * a callback function to the resource. Returns %0 on success.
95 */
96int rio_request_inb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -080097 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -080098 int mbox,
99 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800100 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
Matt Porter394b7012005-11-07 01:00:15 -0800101 int slot))
102{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700103 int rc = -ENOSYS;
104 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800105
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700106 if (mport->ops->open_inb_mbox == NULL)
107 goto out;
108
109 res = kmalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800110
111 if (res) {
112 rio_init_mbox_res(res, mbox, mbox);
113
114 /* Make sure this mailbox isn't in use */
115 if ((rc =
116 request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
117 res)) < 0) {
118 kfree(res);
119 goto out;
120 }
121
122 mport->inb_msg[mbox].res = res;
123
124 /* Hook the inbound message callback */
125 mport->inb_msg[mbox].mcback = minb;
126
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700127 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
Matt Porter394b7012005-11-07 01:00:15 -0800128 } else
129 rc = -ENOMEM;
130
131 out:
132 return rc;
133}
134
135/**
136 * rio_release_inb_mbox - release inbound mailbox message service
137 * @mport: RIO master port from which to release the mailbox resource
138 * @mbox: Mailbox number to release
139 *
140 * Releases ownership of an inbound mailbox resource. Returns 0
141 * if the request has been satisfied.
142 */
143int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
144{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700145 if (mport->ops->close_inb_mbox) {
146 mport->ops->close_inb_mbox(mport, mbox);
Matt Porter394b7012005-11-07 01:00:15 -0800147
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700148 /* Release the mailbox resource */
149 return release_resource(mport->inb_msg[mbox].res);
150 } else
151 return -ENOSYS;
Matt Porter394b7012005-11-07 01:00:15 -0800152}
153
154/**
155 * rio_request_outb_mbox - request outbound mailbox service
156 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800157 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800158 * @mbox: Mailbox number to claim
159 * @entries: Number of entries in outbound mailbox queue
160 * @moutb: Callback to execute when outbound message is sent
161 *
162 * Requests ownership of an outbound mailbox resource and binds
163 * a callback function to the resource. Returns 0 on success.
164 */
165int rio_request_outb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800166 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800167 int mbox,
168 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800169 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
Matt Porter394b7012005-11-07 01:00:15 -0800170{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700171 int rc = -ENOSYS;
172 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800173
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700174 if (mport->ops->open_outb_mbox == NULL)
175 goto out;
176
177 res = kmalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800178
179 if (res) {
180 rio_init_mbox_res(res, mbox, mbox);
181
182 /* Make sure this outbound mailbox isn't in use */
183 if ((rc =
184 request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
185 res)) < 0) {
186 kfree(res);
187 goto out;
188 }
189
190 mport->outb_msg[mbox].res = res;
191
192 /* Hook the inbound message callback */
193 mport->outb_msg[mbox].mcback = moutb;
194
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700195 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
Matt Porter394b7012005-11-07 01:00:15 -0800196 } else
197 rc = -ENOMEM;
198
199 out:
200 return rc;
201}
202
203/**
204 * rio_release_outb_mbox - release outbound mailbox message service
205 * @mport: RIO master port from which to release the mailbox resource
206 * @mbox: Mailbox number to release
207 *
208 * Releases ownership of an inbound mailbox resource. Returns 0
209 * if the request has been satisfied.
210 */
211int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
212{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700213 if (mport->ops->close_outb_mbox) {
214 mport->ops->close_outb_mbox(mport, mbox);
Matt Porter394b7012005-11-07 01:00:15 -0800215
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700216 /* Release the mailbox resource */
217 return release_resource(mport->outb_msg[mbox].res);
218 } else
219 return -ENOSYS;
Matt Porter394b7012005-11-07 01:00:15 -0800220}
221
222/**
223 * rio_setup_inb_dbell - bind inbound doorbell callback
224 * @mport: RIO master port to bind the doorbell callback
Matt Porter6978bbc2005-11-07 01:00:20 -0800225 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800226 * @res: Doorbell message resource
227 * @dinb: Callback to execute when doorbell is received
228 *
229 * Adds a doorbell resource/callback pair into a port's
230 * doorbell event list. Returns 0 if the request has been
231 * satisfied.
232 */
233static int
Matt Porter6978bbc2005-11-07 01:00:20 -0800234rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
235 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
Matt Porter394b7012005-11-07 01:00:15 -0800236 u16 info))
237{
238 int rc = 0;
239 struct rio_dbell *dbell;
240
241 if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
242 rc = -ENOMEM;
243 goto out;
244 }
245
246 dbell->res = res;
247 dbell->dinb = dinb;
Matt Porter6978bbc2005-11-07 01:00:20 -0800248 dbell->dev_id = dev_id;
Matt Porter394b7012005-11-07 01:00:15 -0800249
250 list_add_tail(&dbell->node, &mport->dbells);
251
252 out:
253 return rc;
254}
255
256/**
257 * rio_request_inb_dbell - request inbound doorbell message service
258 * @mport: RIO master port from which to allocate the doorbell resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800259 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800260 * @start: Doorbell info range start
261 * @end: Doorbell info range end
262 * @dinb: Callback to execute when doorbell is received
263 *
264 * Requests ownership of an inbound doorbell resource and binds
265 * a callback function to the resource. Returns 0 if the request
266 * has been satisfied.
267 */
268int rio_request_inb_dbell(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800269 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800270 u16 start,
271 u16 end,
Matt Porter6978bbc2005-11-07 01:00:20 -0800272 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
Matt Porter394b7012005-11-07 01:00:15 -0800273 u16 dst, u16 info))
274{
275 int rc = 0;
276
277 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
278
279 if (res) {
280 rio_init_dbell_res(res, start, end);
281
282 /* Make sure these doorbells aren't in use */
283 if ((rc =
284 request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
285 res)) < 0) {
286 kfree(res);
287 goto out;
288 }
289
290 /* Hook the doorbell callback */
Matt Porter6978bbc2005-11-07 01:00:20 -0800291 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
Matt Porter394b7012005-11-07 01:00:15 -0800292 } else
293 rc = -ENOMEM;
294
295 out:
296 return rc;
297}
298
299/**
300 * rio_release_inb_dbell - release inbound doorbell message service
301 * @mport: RIO master port from which to release the doorbell resource
302 * @start: Doorbell info range start
303 * @end: Doorbell info range end
304 *
305 * Releases ownership of an inbound doorbell resource and removes
306 * callback from the doorbell event list. Returns 0 if the request
307 * has been satisfied.
308 */
309int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
310{
311 int rc = 0, found = 0;
312 struct rio_dbell *dbell;
313
314 list_for_each_entry(dbell, &mport->dbells, node) {
315 if ((dbell->res->start == start) && (dbell->res->end == end)) {
316 found = 1;
317 break;
318 }
319 }
320
321 /* If we can't find an exact match, fail */
322 if (!found) {
323 rc = -EINVAL;
324 goto out;
325 }
326
327 /* Delete from list */
328 list_del(&dbell->node);
329
330 /* Release the doorbell resource */
331 rc = release_resource(dbell->res);
332
333 /* Free the doorbell event */
334 kfree(dbell);
335
336 out:
337 return rc;
338}
339
340/**
341 * rio_request_outb_dbell - request outbound doorbell message range
342 * @rdev: RIO device from which to allocate the doorbell resource
343 * @start: Doorbell message range start
344 * @end: Doorbell message range end
345 *
346 * Requests ownership of a doorbell message range. Returns a resource
347 * if the request has been satisfied or %NULL on failure.
348 */
349struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
350 u16 end)
351{
352 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
353
354 if (res) {
355 rio_init_dbell_res(res, start, end);
356
357 /* Make sure these doorbells aren't in use */
358 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
359 < 0) {
360 kfree(res);
361 res = NULL;
362 }
363 }
364
365 return res;
366}
367
368/**
369 * rio_release_outb_dbell - release outbound doorbell message range
370 * @rdev: RIO device from which to release the doorbell resource
371 * @res: Doorbell resource to be freed
372 *
373 * Releases ownership of a doorbell message range. Returns 0 if the
374 * request has been satisfied.
375 */
376int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
377{
378 int rc = release_resource(res);
379
380 kfree(res);
381
382 return rc;
383}
384
385/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700386 * rio_request_inb_pwrite - request inbound port-write message service
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700387 * @rdev: RIO device to which register inbound port-write callback routine
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700388 * @pwcback: Callback routine to execute when port-write is received
389 *
390 * Binds a port-write callback function to the RapidIO device.
391 * Returns 0 if the request has been satisfied.
392 */
393int rio_request_inb_pwrite(struct rio_dev *rdev,
394 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
395{
396 int rc = 0;
397
398 spin_lock(&rio_global_list_lock);
399 if (rdev->pwcback != NULL)
400 rc = -ENOMEM;
401 else
402 rdev->pwcback = pwcback;
403
404 spin_unlock(&rio_global_list_lock);
405 return rc;
406}
407EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
408
409/**
410 * rio_release_inb_pwrite - release inbound port-write message service
411 * @rdev: RIO device which registered for inbound port-write callback
412 *
413 * Removes callback from the rio_dev structure. Returns 0 if the request
414 * has been satisfied.
415 */
416int rio_release_inb_pwrite(struct rio_dev *rdev)
417{
418 int rc = -ENOMEM;
419
420 spin_lock(&rio_global_list_lock);
421 if (rdev->pwcback) {
422 rdev->pwcback = NULL;
423 rc = 0;
424 }
425
426 spin_unlock(&rio_global_list_lock);
427 return rc;
428}
429EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
430
431/**
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700432 * rio_map_inb_region -- Map inbound memory region.
433 * @mport: Master port.
Randy Dunlap2ca3cb52012-11-16 14:14:56 -0800434 * @local: physical address of memory region to be mapped
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700435 * @rbase: RIO base address assigned to this window
436 * @size: Size of the memory region
437 * @rflags: Flags for mapping.
438 *
439 * Return: 0 -- Success.
440 *
441 * This function will create the mapping from RIO space to local memory.
442 */
443int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
444 u64 rbase, u32 size, u32 rflags)
445{
446 int rc = 0;
447 unsigned long flags;
448
449 if (!mport->ops->map_inb)
450 return -1;
451 spin_lock_irqsave(&rio_mmap_lock, flags);
452 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
453 spin_unlock_irqrestore(&rio_mmap_lock, flags);
454 return rc;
455}
456EXPORT_SYMBOL_GPL(rio_map_inb_region);
457
458/**
459 * rio_unmap_inb_region -- Unmap the inbound memory region
460 * @mport: Master port
461 * @lstart: physical address of memory region to be unmapped
462 */
463void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
464{
465 unsigned long flags;
466 if (!mport->ops->unmap_inb)
467 return;
468 spin_lock_irqsave(&rio_mmap_lock, flags);
469 mport->ops->unmap_inb(mport, lstart);
470 spin_unlock_irqrestore(&rio_mmap_lock, flags);
471}
472EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
473
474/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700475 * rio_mport_get_physefb - Helper function that returns register offset
476 * for Physical Layer Extended Features Block.
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700477 * @port: Master port to issue transaction
478 * @local: Indicate a local master port or remote device access
479 * @destid: Destination ID of the device
480 * @hopcount: Number of switch hops to the device
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700481 */
482u32
483rio_mport_get_physefb(struct rio_mport *port, int local,
484 u16 destid, u8 hopcount)
485{
486 u32 ext_ftr_ptr;
487 u32 ftr_header;
488
489 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
490
491 while (ext_ftr_ptr) {
492 if (local)
493 rio_local_read_config_32(port, ext_ftr_ptr,
494 &ftr_header);
495 else
496 rio_mport_read_config_32(port, destid, hopcount,
497 ext_ftr_ptr, &ftr_header);
498
499 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
500 switch (ftr_header) {
501
502 case RIO_EFB_SER_EP_ID_V13P:
503 case RIO_EFB_SER_EP_REC_ID_V13P:
504 case RIO_EFB_SER_EP_FREE_ID_V13P:
505 case RIO_EFB_SER_EP_ID:
506 case RIO_EFB_SER_EP_REC_ID:
507 case RIO_EFB_SER_EP_FREE_ID:
508 case RIO_EFB_SER_EP_FREC_ID:
509
510 return ext_ftr_ptr;
511
512 default:
513 break;
514 }
515
516 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
517 hopcount, ext_ftr_ptr);
518 }
519
520 return ext_ftr_ptr;
521}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700522EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700523
524/**
525 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700526 * @comp_tag: RIO component tag to match
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700527 * @from: Previous RIO device found in search, or %NULL for new search
528 *
529 * Iterates through the list of known RIO devices. If a RIO device is
530 * found with a matching @comp_tag, a pointer to its device
531 * structure is returned. Otherwise, %NULL is returned. A new search
532 * is initiated by passing %NULL to the @from argument. Otherwise, if
533 * @from is not %NULL, searches continue from next device on the global
534 * list.
535 */
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700536struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700537{
538 struct list_head *n;
539 struct rio_dev *rdev;
540
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700541 spin_lock(&rio_global_list_lock);
542 n = from ? from->global_list.next : rio_devices.next;
543
544 while (n && (n != &rio_devices)) {
545 rdev = rio_dev_g(n);
546 if (rdev->comp_tag == comp_tag)
547 goto exit;
548 n = n->next;
549 }
550 rdev = NULL;
551exit:
552 spin_unlock(&rio_global_list_lock);
553 return rdev;
554}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700555EXPORT_SYMBOL_GPL(rio_get_comptag);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700556
557/**
558 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
559 * @rdev: Pointer to RIO device control structure
560 * @pnum: Switch port number to set LOCKOUT bit
561 * @lock: Operation : set (=1) or clear (=0)
562 */
563int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
564{
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700565 u32 regval;
566
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800567 rio_read_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700568 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
569 &regval);
570 if (lock)
571 regval |= RIO_PORT_N_CTL_LOCKOUT;
572 else
573 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
574
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800575 rio_write_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700576 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
577 regval);
578 return 0;
579}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700580EXPORT_SYMBOL_GPL(rio_set_port_lockout);
581
582/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700583 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
584 * given port
585 * @port: Master port associated with the RIO network
586 * @local: local=1 select local port otherwise a far device is reached
587 * @destid: Destination ID of the device to check host bit
588 * @hopcount: Number of hops to reach the target
589 * @port_num: Port (-number on switch) to enable on a far end device
590 *
591 * Returns 0 or 1 from on General Control Command and Status Register
592 * (EXT_PTR+0x3C)
593 */
594int rio_enable_rx_tx_port(struct rio_mport *port,
595 int local, u16 destid,
596 u8 hopcount, u8 port_num)
597{
598#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
599 u32 regval;
600 u32 ext_ftr_ptr;
601
602 /*
603 * enable rx input tx output port
604 */
605 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
606 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
607
608 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
609
610 if (local) {
611 rio_local_read_config_32(port, ext_ftr_ptr +
612 RIO_PORT_N_CTL_CSR(0),
613 &regval);
614 } else {
615 if (rio_mport_read_config_32(port, destid, hopcount,
616 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), &regval) < 0)
617 return -EIO;
618 }
619
620 if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
621 /* serial */
622 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
623 | RIO_PORT_N_CTL_EN_TX_SER;
624 } else {
625 /* parallel */
626 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
627 | RIO_PORT_N_CTL_EN_TX_PAR;
628 }
629
630 if (local) {
631 rio_local_write_config_32(port, ext_ftr_ptr +
632 RIO_PORT_N_CTL_CSR(0), regval);
633 } else {
634 if (rio_mport_write_config_32(port, destid, hopcount,
635 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
636 return -EIO;
637 }
638#endif
639 return 0;
640}
641EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
642
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700643
644/**
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700645 * rio_chk_dev_route - Validate route to the specified device.
646 * @rdev: RIO device failed to respond
647 * @nrdev: Last active device on the route to rdev
648 * @npnum: nrdev's port number on the route to rdev
649 *
650 * Follows a route to the specified RIO device to determine the last available
651 * device (and corresponding RIO port) on the route.
652 */
653static int
654rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
655{
656 u32 result;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800657 int p_port, rc = -EIO;
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700658 struct rio_dev *prev = NULL;
659
660 /* Find switch with failed RIO link */
661 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
662 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
663 prev = rdev->prev;
664 break;
665 }
666 rdev = rdev->prev;
667 }
668
669 if (prev == NULL)
670 goto err_out;
671
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800672 p_port = prev->rswitch->route_table[rdev->destid];
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700673
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700674 if (p_port != RIO_INVALID_ROUTE) {
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700675 pr_debug("RIO: link failed on [%s]-P%d\n",
676 rio_name(prev), p_port);
677 *nrdev = prev;
678 *npnum = p_port;
679 rc = 0;
680 } else
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700681 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700682err_out:
683 return rc;
684}
685
686/**
687 * rio_mport_chk_dev_access - Validate access to the specified device.
688 * @mport: Master port to send transactions
689 * @destid: Device destination ID in network
690 * @hopcount: Number of hops into the network
691 */
Alexandre Bouninee274e0e2010-10-27 15:34:32 -0700692int
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700693rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
694{
695 int i = 0;
696 u32 tmp;
697
698 while (rio_mport_read_config_32(mport, destid, hopcount,
699 RIO_DEV_ID_CAR, &tmp)) {
700 i++;
701 if (i == RIO_MAX_CHK_RETRY)
702 return -EIO;
703 mdelay(1);
704 }
705
706 return 0;
707}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700708EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700709
710/**
711 * rio_chk_dev_access - Validate access to the specified device.
712 * @rdev: Pointer to RIO device control structure
713 */
714static int rio_chk_dev_access(struct rio_dev *rdev)
715{
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800716 return rio_mport_chk_dev_access(rdev->net->hport,
717 rdev->destid, rdev->hopcount);
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700718}
719
720/**
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700721 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
722 * returns link-response (if requested).
723 * @rdev: RIO devive to issue Input-status command
724 * @pnum: Device port number to issue the command
725 * @lnkresp: Response from a link partner
726 */
727static int
728rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
729{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700730 u32 regval;
731 int checkcount;
732
733 if (lnkresp) {
734 /* Read from link maintenance response register
735 * to clear valid bit */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800736 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700737 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
738 &regval);
739 udelay(50);
740 }
741
742 /* Issue Input-status command */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800743 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700744 rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
745 RIO_MNT_REQ_CMD_IS);
746
747 /* Exit if the response is not expected */
748 if (lnkresp == NULL)
749 return 0;
750
751 checkcount = 3;
752 while (checkcount--) {
753 udelay(50);
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800754 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700755 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
756 &regval);
757 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
758 *lnkresp = regval;
759 return 0;
760 }
761 }
762
763 return -EIO;
764}
765
766/**
767 * rio_clr_err_stopped - Clears port Error-stopped states.
768 * @rdev: Pointer to RIO device control structure
769 * @pnum: Switch port number to clear errors
770 * @err_status: port error status (if 0 reads register from device)
771 */
772static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
773{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700774 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
775 u32 regval;
776 u32 far_ackid, far_linkstat, near_ackid;
777
778 if (err_status == 0)
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800779 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700780 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
781 &err_status);
782
783 if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
784 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
785 /*
786 * Send a Link-Request/Input-Status control symbol
787 */
788 if (rio_get_input_status(rdev, pnum, &regval)) {
789 pr_debug("RIO_EM: Input-status response timeout\n");
790 goto rd_err;
791 }
792
793 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
794 pnum, regval);
795 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
796 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800797 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700798 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
799 &regval);
800 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
801 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
802 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
803 " near_ackID=0x%02x\n",
804 pnum, far_ackid, far_linkstat, near_ackid);
805
806 /*
807 * If required, synchronize ackIDs of near and
808 * far sides.
809 */
810 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
811 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
812 /* Align near outstanding/outbound ackIDs with
813 * far inbound.
814 */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800815 rio_write_config_32(rdev,
816 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700817 (near_ackid << 24) |
818 (far_ackid << 8) | far_ackid);
819 /* Align far outstanding/outbound ackIDs with
820 * near inbound.
821 */
822 far_ackid++;
823 if (nextdev)
824 rio_write_config_32(nextdev,
825 nextdev->phys_efptr +
826 RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
827 (far_ackid << 24) |
828 (near_ackid << 8) | near_ackid);
829 else
830 pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
831 }
832rd_err:
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800833 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700834 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
835 &err_status);
836 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
837 }
838
839 if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
840 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
841 rio_get_input_status(nextdev,
842 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
843 udelay(50);
844
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800845 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700846 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
847 &err_status);
848 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
849 }
850
851 return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
852 RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
853}
854
855/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700856 * rio_inb_pwrite_handler - process inbound port-write message
857 * @pw_msg: pointer to inbound port-write message
858 *
859 * Processes an inbound port-write message. Returns 0 if the request
860 * has been satisfied.
861 */
862int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
863{
864 struct rio_dev *rdev;
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700865 u32 err_status, em_perrdet, em_ltlerrdet;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700866 int rc, portnum;
867
Alexandre Bouninee6536922011-01-12 17:00:40 -0800868 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700869 if (rdev == NULL) {
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700870 /* Device removed or enumeration error */
871 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700872 __func__, pw_msg->em.comptag);
873 return -EIO;
874 }
875
876 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
877
878#ifdef DEBUG_PW
879 {
880 u32 i;
881 for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700882 pr_debug("0x%02x: %08x %08x %08x %08x\n",
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700883 i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
884 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
885 i += 4;
886 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700887 }
888#endif
889
890 /* Call an external service function (if such is registered
891 * for this device). This may be the service for endpoints that send
892 * device-specific port-write messages. End-point messages expected
893 * to be handled completely by EP specific device driver.
894 * For switches rc==0 signals that no standard processing required.
895 */
896 if (rdev->pwcback != NULL) {
897 rc = rdev->pwcback(rdev, pw_msg, 0);
898 if (rc == 0)
899 return 0;
900 }
901
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700902 portnum = pw_msg->em.is_port & 0xFF;
903
904 /* Check if device and route to it are functional:
905 * Sometimes devices may send PW message(s) just before being
906 * powered down (or link being lost).
907 */
908 if (rio_chk_dev_access(rdev)) {
909 pr_debug("RIO: device access failed - get link partner\n");
910 /* Scan route to the device and identify failed link.
911 * This will replace device and port reported in PW message.
912 * PW message should not be used after this point.
913 */
914 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
915 pr_err("RIO: Route trace for %s failed\n",
916 rio_name(rdev));
917 return -EIO;
918 }
919 pw_msg = NULL;
920 }
921
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700922 /* For End-point devices processing stops here */
923 if (!(rdev->pef & RIO_PEF_SWITCH))
924 return 0;
925
926 if (rdev->phys_efptr == 0) {
927 pr_err("RIO_PW: Bad switch initialization for %s\n",
928 rio_name(rdev));
929 return 0;
930 }
931
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700932 /*
933 * Process the port-write notification from switch
934 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -0700935 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
936 rdev->rswitch->ops->em_handle(rdev, portnum);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700937
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800938 rio_read_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700939 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
940 &err_status);
941 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
942
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700943 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700944
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700945 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
946 rdev->rswitch->port_ok |= (1 << portnum);
947 rio_set_port_lockout(rdev, portnum, 0);
948 /* Schedule Insertion Service */
949 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
950 rio_name(rdev), portnum);
951 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700952
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700953 /* Clear error-stopped states (if reported).
954 * Depending on the link partner state, two attempts
955 * may be needed for successful recovery.
956 */
957 if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
958 RIO_PORT_N_ERR_STS_PW_INP_ES)) {
959 if (rio_clr_err_stopped(rdev, portnum, err_status))
960 rio_clr_err_stopped(rdev, portnum, 0);
961 }
962 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700963
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700964 if (rdev->rswitch->port_ok & (1 << portnum)) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700965 rdev->rswitch->port_ok &= ~(1 << portnum);
966 rio_set_port_lockout(rdev, portnum, 1);
967
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800968 rio_write_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700969 rdev->phys_efptr +
970 RIO_PORT_N_ACK_STS_CSR(portnum),
971 RIO_PORT_N_ACK_CLEAR);
972
973 /* Schedule Extraction Service */
974 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
975 rio_name(rdev), portnum);
976 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700977 }
978
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800979 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700980 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
981 if (em_perrdet) {
982 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
983 portnum, em_perrdet);
984 /* Clear EM Port N Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800985 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700986 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
987 }
988
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800989 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700990 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
991 if (em_ltlerrdet) {
992 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
993 em_ltlerrdet);
994 /* Clear EM L/T Layer Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800995 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700996 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
997 }
998
Alexandre Bounine388c45c2010-10-27 15:34:35 -0700999 /* Clear remaining error bits and Port-Write Pending bit */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001000 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001001 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
Alexandre Bounine388c45c2010-10-27 15:34:35 -07001002 err_status);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001003
1004 return 0;
1005}
1006EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1007
1008/**
1009 * rio_mport_get_efb - get pointer to next extended features block
1010 * @port: Master port to issue transaction
1011 * @local: Indicate a local master port or remote device access
1012 * @destid: Destination ID of the device
1013 * @hopcount: Number of switch hops to the device
1014 * @from: Offset of current Extended Feature block header (if 0 starts
1015 * from ExtFeaturePtr)
1016 */
1017u32
1018rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1019 u8 hopcount, u32 from)
1020{
1021 u32 reg_val;
1022
1023 if (from == 0) {
1024 if (local)
1025 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1026 &reg_val);
1027 else
1028 rio_mport_read_config_32(port, destid, hopcount,
1029 RIO_ASM_INFO_CAR, &reg_val);
1030 return reg_val & RIO_EXT_FTR_PTR_MASK;
1031 } else {
1032 if (local)
1033 rio_local_read_config_32(port, from, &reg_val);
1034 else
1035 rio_mport_read_config_32(port, destid, hopcount,
1036 from, &reg_val);
1037 return RIO_GET_BLOCK_ID(reg_val);
1038 }
1039}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001040EXPORT_SYMBOL_GPL(rio_mport_get_efb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001041
1042/**
Matt Porter394b7012005-11-07 01:00:15 -08001043 * rio_mport_get_feature - query for devices' extended features
1044 * @port: Master port to issue transaction
1045 * @local: Indicate a local master port or remote device access
1046 * @destid: Destination ID of the device
1047 * @hopcount: Number of switch hops to the device
1048 * @ftr: Extended feature code
1049 *
1050 * Tell if a device supports a given RapidIO capability.
1051 * Returns the offset of the requested extended feature
1052 * block within the device's RIO configuration space or
1053 * 0 in case the device does not support it. Possible
1054 * values for @ftr:
1055 *
1056 * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
1057 *
1058 * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
1059 *
1060 * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
1061 *
1062 * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
1063 *
1064 * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
1065 *
1066 * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
1067 */
1068u32
1069rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1070 u8 hopcount, int ftr)
1071{
1072 u32 asm_info, ext_ftr_ptr, ftr_header;
1073
1074 if (local)
1075 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1076 else
1077 rio_mport_read_config_32(port, destid, hopcount,
1078 RIO_ASM_INFO_CAR, &asm_info);
1079
1080 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1081
1082 while (ext_ftr_ptr) {
1083 if (local)
1084 rio_local_read_config_32(port, ext_ftr_ptr,
1085 &ftr_header);
1086 else
1087 rio_mport_read_config_32(port, destid, hopcount,
1088 ext_ftr_ptr, &ftr_header);
1089 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1090 return ext_ftr_ptr;
1091 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
1092 break;
1093 }
1094
1095 return 0;
1096}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001097EXPORT_SYMBOL_GPL(rio_mport_get_feature);
Matt Porter394b7012005-11-07 01:00:15 -08001098
1099/**
1100 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
1101 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1102 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1103 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
1104 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
1105 * @from: Previous RIO device found in search, or %NULL for new search
1106 *
1107 * Iterates through the list of known RIO devices. If a RIO device is
1108 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
1109 * count to the device is incrememted and a pointer to its device
1110 * structure is returned. Otherwise, %NULL is returned. A new search
1111 * is initiated by passing %NULL to the @from argument. Otherwise, if
1112 * @from is not %NULL, searches continue from next device on the global
1113 * list. The reference count for @from is always decremented if it is
1114 * not %NULL.
1115 */
1116struct rio_dev *rio_get_asm(u16 vid, u16 did,
1117 u16 asm_vid, u16 asm_did, struct rio_dev *from)
1118{
1119 struct list_head *n;
1120 struct rio_dev *rdev;
1121
1122 WARN_ON(in_interrupt());
1123 spin_lock(&rio_global_list_lock);
1124 n = from ? from->global_list.next : rio_devices.next;
1125
1126 while (n && (n != &rio_devices)) {
1127 rdev = rio_dev_g(n);
1128 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1129 (did == RIO_ANY_ID || rdev->did == did) &&
1130 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1131 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1132 goto exit;
1133 n = n->next;
1134 }
1135 rdev = NULL;
1136 exit:
1137 rio_dev_put(from);
1138 rdev = rio_dev_get(rdev);
1139 spin_unlock(&rio_global_list_lock);
1140 return rdev;
1141}
1142
1143/**
1144 * rio_get_device - Begin or continue searching for a RIO device by vid/did
1145 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1146 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1147 * @from: Previous RIO device found in search, or %NULL for new search
1148 *
1149 * Iterates through the list of known RIO devices. If a RIO device is
1150 * found with a matching @vid and @did, the reference count to the
1151 * device is incrememted and a pointer to its device structure is returned.
1152 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
1153 * to the @from argument. Otherwise, if @from is not %NULL, searches
1154 * continue from next device on the global list. The reference count for
1155 * @from is always decremented if it is not %NULL.
1156 */
1157struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1158{
1159 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1160}
1161
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001162/**
1163 * rio_std_route_add_entry - Add switch route table entry using standard
1164 * registers defined in RIO specification rev.1.3
1165 * @mport: Master port to issue transaction
1166 * @destid: Destination ID of the device
1167 * @hopcount: Number of switch hops to the device
1168 * @table: routing table ID (global or port-specific)
1169 * @route_destid: destID entry in the RT
1170 * @route_port: destination port for specified destID
1171 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001172static int
1173rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1174 u16 table, u16 route_destid, u8 route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001175{
1176 if (table == RIO_GLOBAL_TABLE) {
1177 rio_mport_write_config_32(mport, destid, hopcount,
1178 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1179 (u32)route_destid);
1180 rio_mport_write_config_32(mport, destid, hopcount,
1181 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1182 (u32)route_port);
1183 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001184
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001185 udelay(10);
1186 return 0;
1187}
1188
1189/**
1190 * rio_std_route_get_entry - Read switch route table entry (port number)
Uwe Kleine-König638c5942010-06-11 12:16:58 +02001191 * associated with specified destID using standard registers defined in RIO
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001192 * specification rev.1.3
1193 * @mport: Master port to issue transaction
1194 * @destid: Destination ID of the device
1195 * @hopcount: Number of switch hops to the device
1196 * @table: routing table ID (global or port-specific)
1197 * @route_destid: destID entry in the RT
1198 * @route_port: returned destination port for specified destID
1199 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001200static int
1201rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1202 u16 table, u16 route_destid, u8 *route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001203{
1204 u32 result;
1205
1206 if (table == RIO_GLOBAL_TABLE) {
1207 rio_mport_write_config_32(mport, destid, hopcount,
1208 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1209 rio_mport_read_config_32(mport, destid, hopcount,
1210 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1211
1212 *route_port = (u8)result;
1213 }
1214
1215 return 0;
1216}
1217
1218/**
1219 * rio_std_route_clr_table - Clear swotch route table using standard registers
1220 * defined in RIO specification rev.1.3.
1221 * @mport: Master port to issue transaction
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001222 * @destid: Destination ID of the device
1223 * @hopcount: Number of switch hops to the device
1224 * @table: routing table ID (global or port-specific)
1225 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001226static int
1227rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1228 u16 table)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001229{
1230 u32 max_destid = 0xff;
1231 u32 i, pef, id_inc = 1, ext_cfg = 0;
1232 u32 port_sel = RIO_INVALID_ROUTE;
1233
1234 if (table == RIO_GLOBAL_TABLE) {
1235 rio_mport_read_config_32(mport, destid, hopcount,
1236 RIO_PEF_CAR, &pef);
1237
1238 if (mport->sys_size) {
1239 rio_mport_read_config_32(mport, destid, hopcount,
1240 RIO_SWITCH_RT_LIMIT,
1241 &max_destid);
1242 max_destid &= RIO_RT_MAX_DESTID;
1243 }
1244
1245 if (pef & RIO_PEF_EXT_RT) {
1246 ext_cfg = 0x80000000;
1247 id_inc = 4;
1248 port_sel = (RIO_INVALID_ROUTE << 24) |
1249 (RIO_INVALID_ROUTE << 16) |
1250 (RIO_INVALID_ROUTE << 8) |
1251 RIO_INVALID_ROUTE;
1252 }
1253
1254 for (i = 0; i <= max_destid;) {
1255 rio_mport_write_config_32(mport, destid, hopcount,
1256 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1257 ext_cfg | i);
1258 rio_mport_write_config_32(mport, destid, hopcount,
1259 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1260 port_sel);
1261 i += id_inc;
1262 }
1263 }
1264
1265 udelay(10);
1266 return 0;
1267}
1268
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001269/**
1270 * rio_lock_device - Acquires host device lock for specified device
1271 * @port: Master port to send transaction
1272 * @destid: Destination ID for device/switch
1273 * @hopcount: Hopcount to reach switch
1274 * @wait_ms: Max wait time in msec (0 = no timeout)
1275 *
1276 * Attepts to acquire host device lock for specified device
1277 * Returns 0 if device lock acquired or EINVAL if timeout expires.
1278 */
1279int rio_lock_device(struct rio_mport *port, u16 destid,
1280 u8 hopcount, int wait_ms)
1281{
1282 u32 result;
1283 int tcnt = 0;
1284
1285 /* Attempt to acquire device lock */
1286 rio_mport_write_config_32(port, destid, hopcount,
1287 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1288 rio_mport_read_config_32(port, destid, hopcount,
1289 RIO_HOST_DID_LOCK_CSR, &result);
1290
1291 while (result != port->host_deviceid) {
1292 if (wait_ms != 0 && tcnt == wait_ms) {
1293 pr_debug("RIO: timeout when locking device %x:%x\n",
1294 destid, hopcount);
1295 return -EINVAL;
1296 }
1297
1298 /* Delay a bit */
1299 mdelay(1);
1300 tcnt++;
1301 /* Try to acquire device lock again */
1302 rio_mport_write_config_32(port, destid,
1303 hopcount,
1304 RIO_HOST_DID_LOCK_CSR,
1305 port->host_deviceid);
1306 rio_mport_read_config_32(port, destid,
1307 hopcount,
1308 RIO_HOST_DID_LOCK_CSR, &result);
1309 }
1310
1311 return 0;
1312}
1313EXPORT_SYMBOL_GPL(rio_lock_device);
1314
1315/**
1316 * rio_unlock_device - Releases host device lock for specified device
1317 * @port: Master port to send transaction
1318 * @destid: Destination ID for device/switch
1319 * @hopcount: Hopcount to reach switch
1320 *
1321 * Returns 0 if device lock released or EINVAL if fails.
1322 */
1323int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1324{
1325 u32 result;
1326
1327 /* Release device lock */
1328 rio_mport_write_config_32(port, destid,
1329 hopcount,
1330 RIO_HOST_DID_LOCK_CSR,
1331 port->host_deviceid);
1332 rio_mport_read_config_32(port, destid, hopcount,
1333 RIO_HOST_DID_LOCK_CSR, &result);
1334 if ((result & 0xffff) != 0xffff) {
1335 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1336 destid, hopcount);
1337 return -EINVAL;
1338 }
1339
1340 return 0;
1341}
1342EXPORT_SYMBOL_GPL(rio_unlock_device);
1343
1344/**
1345 * rio_route_add_entry- Add a route entry to a switch routing table
1346 * @rdev: RIO device
1347 * @table: Routing table ID
1348 * @route_destid: Destination ID to be routed
1349 * @route_port: Port number to be routed
1350 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1351 *
1352 * If available calls the switch specific add_entry() method to add a route
1353 * entry into a switch routing table. Otherwise uses standard RT update method
1354 * as defined by RapidIO specification. A specific routing table can be selected
1355 * using the @table argument if a switch has per port routing tables or
1356 * the standard (or global) table may be used by passing
1357 * %RIO_GLOBAL_TABLE in @table.
1358 *
1359 * Returns %0 on success or %-EINVAL on failure.
1360 */
1361int rio_route_add_entry(struct rio_dev *rdev,
1362 u16 table, u16 route_destid, u8 route_port, int lock)
1363{
1364 int rc = -EINVAL;
1365 struct rio_switch_ops *ops = rdev->rswitch->ops;
1366
1367 if (lock) {
1368 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1369 rdev->hopcount, 1000);
1370 if (rc)
1371 return rc;
1372 }
1373
1374 spin_lock(&rdev->rswitch->lock);
1375
1376 if (ops == NULL || ops->add_entry == NULL) {
1377 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1378 rdev->hopcount, table,
1379 route_destid, route_port);
1380 } else if (try_module_get(ops->owner)) {
1381 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1382 rdev->hopcount, table, route_destid,
1383 route_port);
1384 module_put(ops->owner);
1385 }
1386
1387 spin_unlock(&rdev->rswitch->lock);
1388
1389 if (lock)
1390 rio_unlock_device(rdev->net->hport, rdev->destid,
1391 rdev->hopcount);
1392
1393 return rc;
1394}
1395EXPORT_SYMBOL_GPL(rio_route_add_entry);
1396
1397/**
1398 * rio_route_get_entry- Read an entry from a switch routing table
1399 * @rdev: RIO device
1400 * @table: Routing table ID
1401 * @route_destid: Destination ID to be routed
1402 * @route_port: Pointer to read port number into
1403 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1404 *
1405 * If available calls the switch specific get_entry() method to fetch a route
1406 * entry from a switch routing table. Otherwise uses standard RT read method
1407 * as defined by RapidIO specification. A specific routing table can be selected
1408 * using the @table argument if a switch has per port routing tables or
1409 * the standard (or global) table may be used by passing
1410 * %RIO_GLOBAL_TABLE in @table.
1411 *
1412 * Returns %0 on success or %-EINVAL on failure.
1413 */
1414int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1415 u16 route_destid, u8 *route_port, int lock)
1416{
1417 int rc = -EINVAL;
1418 struct rio_switch_ops *ops = rdev->rswitch->ops;
1419
1420 if (lock) {
1421 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1422 rdev->hopcount, 1000);
1423 if (rc)
1424 return rc;
1425 }
1426
1427 spin_lock(&rdev->rswitch->lock);
1428
1429 if (ops == NULL || ops->get_entry == NULL) {
1430 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1431 rdev->hopcount, table,
1432 route_destid, route_port);
1433 } else if (try_module_get(ops->owner)) {
1434 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1435 rdev->hopcount, table, route_destid,
1436 route_port);
1437 module_put(ops->owner);
1438 }
1439
1440 spin_unlock(&rdev->rswitch->lock);
1441
1442 if (lock)
1443 rio_unlock_device(rdev->net->hport, rdev->destid,
1444 rdev->hopcount);
1445 return rc;
1446}
1447EXPORT_SYMBOL_GPL(rio_route_get_entry);
1448
1449/**
1450 * rio_route_clr_table - Clear a switch routing table
1451 * @rdev: RIO device
1452 * @table: Routing table ID
1453 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1454 *
1455 * If available calls the switch specific clr_table() method to clear a switch
1456 * routing table. Otherwise uses standard RT write method as defined by RapidIO
1457 * specification. A specific routing table can be selected using the @table
1458 * argument if a switch has per port routing tables or the standard (or global)
1459 * table may be used by passing %RIO_GLOBAL_TABLE in @table.
1460 *
1461 * Returns %0 on success or %-EINVAL on failure.
1462 */
1463int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1464{
1465 int rc = -EINVAL;
1466 struct rio_switch_ops *ops = rdev->rswitch->ops;
1467
1468 if (lock) {
1469 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1470 rdev->hopcount, 1000);
1471 if (rc)
1472 return rc;
1473 }
1474
1475 spin_lock(&rdev->rswitch->lock);
1476
1477 if (ops == NULL || ops->clr_table == NULL) {
1478 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1479 rdev->hopcount, table);
1480 } else if (try_module_get(ops->owner)) {
1481 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1482 rdev->hopcount, table);
1483
1484 module_put(ops->owner);
1485 }
1486
1487 spin_unlock(&rdev->rswitch->lock);
1488
1489 if (lock)
1490 rio_unlock_device(rdev->net->hport, rdev->destid,
1491 rdev->hopcount);
1492
1493 return rc;
1494}
1495EXPORT_SYMBOL_GPL(rio_route_clr_table);
1496
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001497#ifdef CONFIG_RAPIDIO_DMA_ENGINE
1498
1499static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1500{
1501 struct rio_dev *rdev = arg;
1502
1503 /* Check that DMA device belongs to the right MPORT */
1504 return (rdev->net->hport ==
1505 container_of(chan->device, struct rio_mport, dma));
1506}
1507
1508/**
1509 * rio_request_dma - request RapidIO capable DMA channel that supports
1510 * specified target RapidIO device.
1511 * @rdev: RIO device control structure
1512 *
1513 * Returns pointer to allocated DMA channel or NULL if failed.
1514 */
1515struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1516{
1517 dma_cap_mask_t mask;
1518 struct dma_chan *dchan;
1519
1520 dma_cap_zero(mask);
1521 dma_cap_set(DMA_SLAVE, mask);
1522 dchan = dma_request_channel(mask, rio_chan_filter, rdev);
1523
1524 return dchan;
1525}
1526EXPORT_SYMBOL_GPL(rio_request_dma);
1527
1528/**
1529 * rio_release_dma - release specified DMA channel
1530 * @dchan: DMA channel to release
1531 */
1532void rio_release_dma(struct dma_chan *dchan)
1533{
1534 dma_release_channel(dchan);
1535}
1536EXPORT_SYMBOL_GPL(rio_release_dma);
1537
1538/**
1539 * rio_dma_prep_slave_sg - RapidIO specific wrapper
1540 * for device_prep_slave_sg callback defined by DMAENGINE.
1541 * @rdev: RIO device control structure
1542 * @dchan: DMA channel to configure
1543 * @data: RIO specific data descriptor
1544 * @direction: DMA data transfer direction (TO or FROM the device)
1545 * @flags: dmaengine defined flags
1546 *
1547 * Initializes RapidIO capable DMA channel for the specified data transfer.
1548 * Uses DMA channel private extension to pass information related to remote
1549 * target RIO device.
1550 * Returns pointer to DMA transaction descriptor or NULL if failed.
1551 */
1552struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1553 struct dma_chan *dchan, struct rio_dma_data *data,
1554 enum dma_transfer_direction direction, unsigned long flags)
1555{
1556 struct dma_async_tx_descriptor *txd = NULL;
1557 struct rio_dma_ext rio_ext;
1558
1559 if (dchan->device->device_prep_slave_sg == NULL) {
1560 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1561 return NULL;
1562 }
1563
1564 rio_ext.destid = rdev->destid;
1565 rio_ext.rio_addr_u = data->rio_addr_u;
1566 rio_ext.rio_addr = data->rio_addr;
1567 rio_ext.wr_type = data->wr_type;
1568
1569 txd = dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1570 direction, flags, &rio_ext);
1571
1572 return txd;
1573}
1574EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1575
1576#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1577
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001578/**
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07001579 * rio_find_mport - find RIO mport by its ID
1580 * @mport_id: number (ID) of mport device
1581 *
1582 * Given a RIO mport number, the desired mport is located
1583 * in the global list of mports. If the mport is found, a pointer to its
1584 * data structure is returned. If no mport is found, %NULL is returned.
1585 */
1586struct rio_mport *rio_find_mport(int mport_id)
1587{
1588 struct rio_mport *port;
1589
1590 mutex_lock(&rio_mport_list_lock);
1591 list_for_each_entry(port, &rio_mports, node) {
1592 if (port->id == mport_id)
1593 goto found;
1594 }
1595 port = NULL;
1596found:
1597 mutex_unlock(&rio_mport_list_lock);
1598
1599 return port;
1600}
1601
1602/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001603 * rio_register_scan - enumeration/discovery method registration interface
1604 * @mport_id: mport device ID for which fabric scan routine has to be set
1605 * (RIO_MPORT_ANY = set for all available mports)
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001606 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001607 *
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001608 * Registers enumeration/discovery operations with RapidIO subsystem and
1609 * attaches it to the specified mport device (or all available mports
1610 * if RIO_MPORT_ANY is specified).
1611 *
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001612 * Returns error if the mport already has an enumerator attached to it.
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001613 * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001614 */
1615int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1616{
1617 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001618 struct rio_scan_node *scan;
1619 int rc = 0;
1620
1621 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1622
1623 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1624 !scan_ops)
1625 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001626
1627 mutex_lock(&rio_mport_list_lock);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001628
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001629 /*
1630 * Check if there is another enumerator already registered for
1631 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
1632 * for the same mport ID are not supported.
1633 */
1634 list_for_each_entry(scan, &rio_scans, node) {
1635 if (scan->mport_id == mport_id) {
1636 rc = -EBUSY;
1637 goto err_out;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001638 }
1639 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001640
1641 /*
1642 * Allocate and initialize new scan registration node.
1643 */
1644 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
1645 if (!scan) {
1646 rc = -ENOMEM;
1647 goto err_out;
1648 }
1649
1650 scan->mport_id = mport_id;
1651 scan->ops = scan_ops;
1652
1653 /*
1654 * Traverse the list of registered mports to attach this new scan.
1655 *
1656 * The new scan with matching mport ID overrides any previously attached
1657 * scan assuming that old scan (if any) is the default one (based on the
1658 * enumerator registration check above).
1659 * If the new scan is the global one, it will be attached only to mports
1660 * that do not have their own individual operations already attached.
1661 */
1662 list_for_each_entry(port, &rio_mports, node) {
1663 if (port->id == mport_id) {
1664 port->nscan = scan_ops;
1665 break;
1666 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
1667 port->nscan = scan_ops;
1668 }
1669
1670 list_add_tail(&scan->node, &rio_scans);
1671
1672err_out:
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001673 mutex_unlock(&rio_mport_list_lock);
1674
1675 return rc;
1676}
1677EXPORT_SYMBOL_GPL(rio_register_scan);
1678
1679/**
1680 * rio_unregister_scan - removes enumeration/discovery method from mport
1681 * @mport_id: mport device ID for which fabric scan routine has to be
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001682 * unregistered (RIO_MPORT_ANY = apply to all mports that use
1683 * the specified scan_ops)
1684 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001685 *
1686 * Removes enumeration or discovery method assigned to the specified mport
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001687 * device. If RIO_MPORT_ANY is specified, removes the specified operations from
1688 * all mports that have them attached.
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001689 */
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001690int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001691{
1692 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001693 struct rio_scan_node *scan;
1694
1695 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1696
1697 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
1698 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001699
1700 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001701
1702 list_for_each_entry(port, &rio_mports, node)
1703 if (port->id == mport_id ||
1704 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
1705 port->nscan = NULL;
1706
1707 list_for_each_entry(scan, &rio_scans, node)
1708 if (scan->mport_id == mport_id) {
1709 list_del(&scan->node);
1710 kfree(scan);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001711 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001712
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001713 mutex_unlock(&rio_mport_list_lock);
1714
1715 return 0;
1716}
1717EXPORT_SYMBOL_GPL(rio_unregister_scan);
1718
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001719/**
1720 * rio_mport_scan - execute enumeration/discovery on the specified mport
1721 * @mport_id: number (ID) of mport device
1722 */
1723int rio_mport_scan(int mport_id)
1724{
1725 struct rio_mport *port = NULL;
1726 int rc;
1727
1728 mutex_lock(&rio_mport_list_lock);
1729 list_for_each_entry(port, &rio_mports, node) {
1730 if (port->id == mport_id)
1731 goto found;
1732 }
1733 mutex_unlock(&rio_mport_list_lock);
1734 return -ENODEV;
1735found:
1736 if (!port->nscan) {
1737 mutex_unlock(&rio_mport_list_lock);
1738 return -EINVAL;
1739 }
1740
1741 if (!try_module_get(port->nscan->owner)) {
1742 mutex_unlock(&rio_mport_list_lock);
1743 return -ENODEV;
1744 }
1745
1746 mutex_unlock(&rio_mport_list_lock);
1747
1748 if (port->host_deviceid >= 0)
1749 rc = port->nscan->enumerate(port, 0);
1750 else
1751 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
1752
1753 module_put(port->nscan->owner);
1754 return rc;
1755}
1756
Matt Porter394b7012005-11-07 01:00:15 -08001757static void rio_fixup_device(struct rio_dev *dev)
1758{
1759}
1760
Bill Pemberton305c8912012-11-19 13:23:25 -05001761static int rio_init(void)
Matt Porter394b7012005-11-07 01:00:15 -08001762{
1763 struct rio_dev *dev = NULL;
1764
1765 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
1766 rio_fixup_device(dev);
1767 }
1768 return 0;
1769}
1770
Alexandre Bounine005842e2012-10-04 17:16:08 -07001771static struct workqueue_struct *rio_wq;
1772
1773struct rio_disc_work {
1774 struct work_struct work;
1775 struct rio_mport *mport;
1776};
1777
Bill Pemberton305c8912012-11-19 13:23:25 -05001778static void disc_work_handler(struct work_struct *_work)
Alexandre Bounine005842e2012-10-04 17:16:08 -07001779{
1780 struct rio_disc_work *work;
1781
1782 work = container_of(_work, struct rio_disc_work, work);
1783 pr_debug("RIO: discovery work for mport %d %s\n",
1784 work->mport->id, work->mport->name);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001785 if (try_module_get(work->mport->nscan->owner)) {
1786 work->mport->nscan->discover(work->mport, 0);
1787 module_put(work->mport->nscan->owner);
1788 }
Alexandre Bounine005842e2012-10-04 17:16:08 -07001789}
1790
Bill Pemberton305c8912012-11-19 13:23:25 -05001791int rio_init_mports(void)
Matt Porter394b7012005-11-07 01:00:15 -08001792{
Matt Porter394b7012005-11-07 01:00:15 -08001793 struct rio_mport *port;
Alexandre Bounine005842e2012-10-04 17:16:08 -07001794 struct rio_disc_work *work;
Alexandre Bounine25747402012-10-10 15:53:59 -07001795 int n = 0;
Matt Porter394b7012005-11-07 01:00:15 -08001796
Alexandre Bounine25747402012-10-10 15:53:59 -07001797 if (!next_portid)
1798 return -ENODEV;
1799
1800 /*
1801 * First, run enumerations and check if we need to perform discovery
1802 * on any of the registered mports.
1803 */
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001804 mutex_lock(&rio_mport_list_lock);
Matt Porter394b7012005-11-07 01:00:15 -08001805 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001806 if (port->host_deviceid >= 0) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001807 if (port->nscan && try_module_get(port->nscan->owner)) {
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07001808 port->nscan->enumerate(port, 0);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001809 module_put(port->nscan->owner);
1810 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001811 } else
Alexandre Bounine25747402012-10-10 15:53:59 -07001812 n++;
1813 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001814 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine005842e2012-10-04 17:16:08 -07001815
Alexandre Bounine25747402012-10-10 15:53:59 -07001816 if (!n)
1817 goto no_disc;
Alexandre Bounine005842e2012-10-04 17:16:08 -07001818
Alexandre Bounine25747402012-10-10 15:53:59 -07001819 /*
1820 * If we have mports that require discovery schedule a discovery work
1821 * for each of them. If the code below fails to allocate needed
1822 * resources, exit without error to keep results of enumeration
1823 * process (if any).
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001824 * TODO: Implement restart of discovery process for all or
Alexandre Bounine25747402012-10-10 15:53:59 -07001825 * individual discovering mports.
1826 */
1827 rio_wq = alloc_workqueue("riodisc", 0, 0);
1828 if (!rio_wq) {
1829 pr_err("RIO: unable allocate rio_wq\n");
1830 goto no_disc;
1831 }
1832
1833 work = kcalloc(n, sizeof *work, GFP_KERNEL);
1834 if (!work) {
1835 pr_err("RIO: no memory for work struct\n");
1836 destroy_workqueue(rio_wq);
1837 goto no_disc;
1838 }
1839
1840 n = 0;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001841 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07001842 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001843 if (port->host_deviceid < 0 && port->nscan) {
Alexandre Bounine25747402012-10-10 15:53:59 -07001844 work[n].mport = port;
1845 INIT_WORK(&work[n].work, disc_work_handler);
1846 queue_work(rio_wq, &work[n].work);
1847 n++;
Alexandre Bounine005842e2012-10-04 17:16:08 -07001848 }
1849 }
1850
Alexandre Bounine25747402012-10-10 15:53:59 -07001851 flush_workqueue(rio_wq);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001852 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07001853 pr_debug("RIO: destroy discovery workqueue\n");
1854 destroy_workqueue(rio_wq);
1855 kfree(work);
Matt Porter394b7012005-11-07 01:00:15 -08001856
Alexandre Bounine25747402012-10-10 15:53:59 -07001857no_disc:
Alexandre Bounine2f809982011-03-23 16:43:04 -07001858 rio_init();
1859
Alexandre Bouninec1256eb2011-03-23 16:43:06 -07001860 return 0;
Matt Porter394b7012005-11-07 01:00:15 -08001861}
1862
Alexandre Bounine569fccb2011-03-23 16:43:05 -07001863static int hdids[RIO_MAX_MPORTS + 1];
1864
1865static int rio_get_hdid(int index)
1866{
1867 if (!hdids[0] || hdids[0] <= index || index >= RIO_MAX_MPORTS)
1868 return -1;
1869
1870 return hdids[index + 1];
1871}
1872
1873static int rio_hdid_setup(char *str)
1874{
1875 (void)get_options(str, ARRAY_SIZE(hdids), hdids);
1876 return 1;
1877}
1878
1879__setup("riohdid=", rio_hdid_setup);
1880
Alexandre Bounine59f99962011-04-14 15:22:14 -07001881int rio_register_mport(struct rio_mport *port)
Matt Porter394b7012005-11-07 01:00:15 -08001882{
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001883 struct rio_scan_node *scan = NULL;
1884
Alexandre Bounine569fccb2011-03-23 16:43:05 -07001885 if (next_portid >= RIO_MAX_MPORTS) {
1886 pr_err("RIO: reached specified max number of mports\n");
Alexandre Bounine59f99962011-04-14 15:22:14 -07001887 return 1;
Alexandre Bounine569fccb2011-03-23 16:43:05 -07001888 }
1889
1890 port->id = next_portid++;
1891 port->host_deviceid = rio_get_hdid(port->id);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001892 port->nscan = NULL;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001893
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001894 mutex_lock(&rio_mport_list_lock);
Matt Porter394b7012005-11-07 01:00:15 -08001895 list_add_tail(&port->node, &rio_mports);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001896
1897 /*
1898 * Check if there are any registered enumeration/discovery operations
1899 * that have to be attached to the added mport.
1900 */
1901 list_for_each_entry(scan, &rio_scans, node) {
1902 if (port->id == scan->mport_id ||
1903 scan->mport_id == RIO_MPORT_ANY) {
1904 port->nscan = scan->ops;
1905 if (port->id == scan->mport_id)
1906 break;
1907 }
1908 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001909 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001910
1911 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
Alexandre Bounine59f99962011-04-14 15:22:14 -07001912 return 0;
Matt Porter394b7012005-11-07 01:00:15 -08001913}
Alexandre Bounine94d9bd42013-07-03 15:08:55 -07001914EXPORT_SYMBOL_GPL(rio_register_mport);
Matt Porter394b7012005-11-07 01:00:15 -08001915
1916EXPORT_SYMBOL_GPL(rio_local_get_device_id);
1917EXPORT_SYMBOL_GPL(rio_get_device);
1918EXPORT_SYMBOL_GPL(rio_get_asm);
1919EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
1920EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
1921EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
1922EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
1923EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
1924EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
1925EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
1926EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001927EXPORT_SYMBOL_GPL(rio_init_mports);