blob: 2e8a20cac58848cdd0e2afa6201f3f82f4a88e84 [file] [log] [blame]
Matt Porter394b7012005-11-07 01:00:15 -08001/*
2 * RapidIO interconnect services
3 * (RapidIO Interconnect Specification, http://www.rapidio.org)
4 *
5 * Copyright 2005 MontaVista Software, Inc.
6 * Matt Porter <mporter@kernel.crashing.org>
7 *
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07008 * Copyright 2009 - 2013 Integrated Device Technology, Inc.
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07009 * Alex Bounine <alexandre.bounine@idt.com>
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -070010 *
Matt Porter394b7012005-11-07 01:00:15 -080011 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
Matt Porter394b7012005-11-07 01:00:15 -080017#include <linux/types.h>
18#include <linux/kernel.h>
19
20#include <linux/delay.h>
21#include <linux/init.h>
22#include <linux/rio.h>
23#include <linux/rio_drv.h>
24#include <linux/rio_ids.h>
25#include <linux/rio_regs.h>
26#include <linux/module.h>
27#include <linux/spinlock.h>
Tim Schmielaude259682006-01-08 01:02:05 -080028#include <linux/slab.h>
Kumar Gala5febf1c2008-01-23 05:53:47 -060029#include <linux/interrupt.h>
Matt Porter394b7012005-11-07 01:00:15 -080030
31#include "rio.h"
32
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -070033MODULE_DESCRIPTION("RapidIO Subsystem Core");
34MODULE_AUTHOR("Matt Porter <mporter@kernel.crashing.org>");
35MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
36MODULE_LICENSE("GPL");
37
38static int hdid[RIO_MAX_MPORTS];
39static int ids_num;
40module_param_array(hdid, int, &ids_num, 0);
41MODULE_PARM_DESC(hdid,
42 "Destination ID assignment to local RapidIO controllers");
43
Alexandre Bouninea11650e2013-05-24 15:55:05 -070044static LIST_HEAD(rio_devices);
45static DEFINE_SPINLOCK(rio_global_list_lock);
46
Matt Porter394b7012005-11-07 01:00:15 -080047static LIST_HEAD(rio_mports);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -070048static LIST_HEAD(rio_scans);
Alexandre Bouninea11650e2013-05-24 15:55:05 -070049static DEFINE_MUTEX(rio_mport_list_lock);
Alexandre Bounine569fccb2011-03-23 16:43:05 -070050static unsigned char next_portid;
Alexandre Bounineda1589f2012-10-04 17:15:57 -070051static DEFINE_SPINLOCK(rio_mmap_lock);
Matt Porter394b7012005-11-07 01:00:15 -080052
53/**
54 * rio_local_get_device_id - Get the base/extended device id for a port
55 * @port: RIO master port from which to get the deviceid
56 *
57 * Reads the base/extended device id from the local device
58 * implementing the master port. Returns the 8/16-bit device
59 * id.
60 */
61u16 rio_local_get_device_id(struct rio_mport *port)
62{
63 u32 result;
64
65 rio_local_read_config_32(port, RIO_DID_CSR, &result);
66
Zhang Weie0423232008-04-18 13:33:42 -070067 return (RIO_GET_DID(port->sys_size, result));
Matt Porter394b7012005-11-07 01:00:15 -080068}
69
70/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -070071 * rio_add_device- Adds a RIO device to the device model
72 * @rdev: RIO device
73 *
74 * Adds the RIO device to the global device list and adds the RIO
75 * device to the RIO device list. Creates the generic sysfs nodes
76 * for an RIO device.
77 */
78int rio_add_device(struct rio_dev *rdev)
79{
80 int err;
81
82 err = device_add(&rdev->dev);
83 if (err)
84 return err;
85
86 spin_lock(&rio_global_list_lock);
87 list_add_tail(&rdev->global_list, &rio_devices);
88 spin_unlock(&rio_global_list_lock);
89
90 rio_create_sysfs_dev_files(rdev);
91
92 return 0;
93}
94EXPORT_SYMBOL_GPL(rio_add_device);
95
96/**
Matt Porter394b7012005-11-07 01:00:15 -080097 * rio_request_inb_mbox - request inbound mailbox service
98 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -080099 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800100 * @mbox: Mailbox number to claim
101 * @entries: Number of entries in inbound mailbox queue
102 * @minb: Callback to execute when inbound message is received
103 *
104 * Requests ownership of an inbound mailbox resource and binds
105 * a callback function to the resource. Returns %0 on success.
106 */
107int rio_request_inb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800108 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800109 int mbox,
110 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800111 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
Matt Porter394b7012005-11-07 01:00:15 -0800112 int slot))
113{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700114 int rc = -ENOSYS;
115 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800116
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700117 if (mport->ops->open_inb_mbox == NULL)
118 goto out;
119
120 res = kmalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800121
122 if (res) {
123 rio_init_mbox_res(res, mbox, mbox);
124
125 /* Make sure this mailbox isn't in use */
126 if ((rc =
127 request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
128 res)) < 0) {
129 kfree(res);
130 goto out;
131 }
132
133 mport->inb_msg[mbox].res = res;
134
135 /* Hook the inbound message callback */
136 mport->inb_msg[mbox].mcback = minb;
137
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700138 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
Matt Porter394b7012005-11-07 01:00:15 -0800139 } else
140 rc = -ENOMEM;
141
142 out:
143 return rc;
144}
145
146/**
147 * rio_release_inb_mbox - release inbound mailbox message service
148 * @mport: RIO master port from which to release the mailbox resource
149 * @mbox: Mailbox number to release
150 *
151 * Releases ownership of an inbound mailbox resource. Returns 0
152 * if the request has been satisfied.
153 */
154int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
155{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700156 if (mport->ops->close_inb_mbox) {
157 mport->ops->close_inb_mbox(mport, mbox);
Matt Porter394b7012005-11-07 01:00:15 -0800158
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700159 /* Release the mailbox resource */
160 return release_resource(mport->inb_msg[mbox].res);
161 } else
162 return -ENOSYS;
Matt Porter394b7012005-11-07 01:00:15 -0800163}
164
165/**
166 * rio_request_outb_mbox - request outbound mailbox service
167 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800168 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800169 * @mbox: Mailbox number to claim
170 * @entries: Number of entries in outbound mailbox queue
171 * @moutb: Callback to execute when outbound message is sent
172 *
173 * Requests ownership of an outbound mailbox resource and binds
174 * a callback function to the resource. Returns 0 on success.
175 */
176int rio_request_outb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800177 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800178 int mbox,
179 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800180 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
Matt Porter394b7012005-11-07 01:00:15 -0800181{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700182 int rc = -ENOSYS;
183 struct resource *res;
Matt Porter394b7012005-11-07 01:00:15 -0800184
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700185 if (mport->ops->open_outb_mbox == NULL)
186 goto out;
187
188 res = kmalloc(sizeof(struct resource), GFP_KERNEL);
Matt Porter394b7012005-11-07 01:00:15 -0800189
190 if (res) {
191 rio_init_mbox_res(res, mbox, mbox);
192
193 /* Make sure this outbound mailbox isn't in use */
194 if ((rc =
195 request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
196 res)) < 0) {
197 kfree(res);
198 goto out;
199 }
200
201 mport->outb_msg[mbox].res = res;
202
203 /* Hook the inbound message callback */
204 mport->outb_msg[mbox].mcback = moutb;
205
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700206 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
Matt Porter394b7012005-11-07 01:00:15 -0800207 } else
208 rc = -ENOMEM;
209
210 out:
211 return rc;
212}
213
214/**
215 * rio_release_outb_mbox - release outbound mailbox message service
216 * @mport: RIO master port from which to release the mailbox resource
217 * @mbox: Mailbox number to release
218 *
219 * Releases ownership of an inbound mailbox resource. Returns 0
220 * if the request has been satisfied.
221 */
222int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
223{
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700224 if (mport->ops->close_outb_mbox) {
225 mport->ops->close_outb_mbox(mport, mbox);
Matt Porter394b7012005-11-07 01:00:15 -0800226
Alexandre Bouninef8f06262011-03-23 16:43:02 -0700227 /* Release the mailbox resource */
228 return release_resource(mport->outb_msg[mbox].res);
229 } else
230 return -ENOSYS;
Matt Porter394b7012005-11-07 01:00:15 -0800231}
232
233/**
234 * rio_setup_inb_dbell - bind inbound doorbell callback
235 * @mport: RIO master port to bind the doorbell callback
Matt Porter6978bbc2005-11-07 01:00:20 -0800236 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800237 * @res: Doorbell message resource
238 * @dinb: Callback to execute when doorbell is received
239 *
240 * Adds a doorbell resource/callback pair into a port's
241 * doorbell event list. Returns 0 if the request has been
242 * satisfied.
243 */
244static int
Matt Porter6978bbc2005-11-07 01:00:20 -0800245rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
246 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
Matt Porter394b7012005-11-07 01:00:15 -0800247 u16 info))
248{
249 int rc = 0;
250 struct rio_dbell *dbell;
251
252 if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
253 rc = -ENOMEM;
254 goto out;
255 }
256
257 dbell->res = res;
258 dbell->dinb = dinb;
Matt Porter6978bbc2005-11-07 01:00:20 -0800259 dbell->dev_id = dev_id;
Matt Porter394b7012005-11-07 01:00:15 -0800260
261 list_add_tail(&dbell->node, &mport->dbells);
262
263 out:
264 return rc;
265}
266
267/**
268 * rio_request_inb_dbell - request inbound doorbell message service
269 * @mport: RIO master port from which to allocate the doorbell resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800270 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800271 * @start: Doorbell info range start
272 * @end: Doorbell info range end
273 * @dinb: Callback to execute when doorbell is received
274 *
275 * Requests ownership of an inbound doorbell resource and binds
276 * a callback function to the resource. Returns 0 if the request
277 * has been satisfied.
278 */
279int rio_request_inb_dbell(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800280 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800281 u16 start,
282 u16 end,
Matt Porter6978bbc2005-11-07 01:00:20 -0800283 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
Matt Porter394b7012005-11-07 01:00:15 -0800284 u16 dst, u16 info))
285{
286 int rc = 0;
287
288 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
289
290 if (res) {
291 rio_init_dbell_res(res, start, end);
292
293 /* Make sure these doorbells aren't in use */
294 if ((rc =
295 request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
296 res)) < 0) {
297 kfree(res);
298 goto out;
299 }
300
301 /* Hook the doorbell callback */
Matt Porter6978bbc2005-11-07 01:00:20 -0800302 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
Matt Porter394b7012005-11-07 01:00:15 -0800303 } else
304 rc = -ENOMEM;
305
306 out:
307 return rc;
308}
309
310/**
311 * rio_release_inb_dbell - release inbound doorbell message service
312 * @mport: RIO master port from which to release the doorbell resource
313 * @start: Doorbell info range start
314 * @end: Doorbell info range end
315 *
316 * Releases ownership of an inbound doorbell resource and removes
317 * callback from the doorbell event list. Returns 0 if the request
318 * has been satisfied.
319 */
320int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
321{
322 int rc = 0, found = 0;
323 struct rio_dbell *dbell;
324
325 list_for_each_entry(dbell, &mport->dbells, node) {
326 if ((dbell->res->start == start) && (dbell->res->end == end)) {
327 found = 1;
328 break;
329 }
330 }
331
332 /* If we can't find an exact match, fail */
333 if (!found) {
334 rc = -EINVAL;
335 goto out;
336 }
337
338 /* Delete from list */
339 list_del(&dbell->node);
340
341 /* Release the doorbell resource */
342 rc = release_resource(dbell->res);
343
344 /* Free the doorbell event */
345 kfree(dbell);
346
347 out:
348 return rc;
349}
350
351/**
352 * rio_request_outb_dbell - request outbound doorbell message range
353 * @rdev: RIO device from which to allocate the doorbell resource
354 * @start: Doorbell message range start
355 * @end: Doorbell message range end
356 *
357 * Requests ownership of a doorbell message range. Returns a resource
358 * if the request has been satisfied or %NULL on failure.
359 */
360struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
361 u16 end)
362{
363 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
364
365 if (res) {
366 rio_init_dbell_res(res, start, end);
367
368 /* Make sure these doorbells aren't in use */
369 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
370 < 0) {
371 kfree(res);
372 res = NULL;
373 }
374 }
375
376 return res;
377}
378
379/**
380 * rio_release_outb_dbell - release outbound doorbell message range
381 * @rdev: RIO device from which to release the doorbell resource
382 * @res: Doorbell resource to be freed
383 *
384 * Releases ownership of a doorbell message range. Returns 0 if the
385 * request has been satisfied.
386 */
387int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
388{
389 int rc = release_resource(res);
390
391 kfree(res);
392
393 return rc;
394}
395
396/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700397 * rio_request_inb_pwrite - request inbound port-write message service
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700398 * @rdev: RIO device to which register inbound port-write callback routine
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700399 * @pwcback: Callback routine to execute when port-write is received
400 *
401 * Binds a port-write callback function to the RapidIO device.
402 * Returns 0 if the request has been satisfied.
403 */
404int rio_request_inb_pwrite(struct rio_dev *rdev,
405 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
406{
407 int rc = 0;
408
409 spin_lock(&rio_global_list_lock);
410 if (rdev->pwcback != NULL)
411 rc = -ENOMEM;
412 else
413 rdev->pwcback = pwcback;
414
415 spin_unlock(&rio_global_list_lock);
416 return rc;
417}
418EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
419
420/**
421 * rio_release_inb_pwrite - release inbound port-write message service
422 * @rdev: RIO device which registered for inbound port-write callback
423 *
424 * Removes callback from the rio_dev structure. Returns 0 if the request
425 * has been satisfied.
426 */
427int rio_release_inb_pwrite(struct rio_dev *rdev)
428{
429 int rc = -ENOMEM;
430
431 spin_lock(&rio_global_list_lock);
432 if (rdev->pwcback) {
433 rdev->pwcback = NULL;
434 rc = 0;
435 }
436
437 spin_unlock(&rio_global_list_lock);
438 return rc;
439}
440EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
441
442/**
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700443 * rio_map_inb_region -- Map inbound memory region.
444 * @mport: Master port.
Randy Dunlap2ca3cb52012-11-16 14:14:56 -0800445 * @local: physical address of memory region to be mapped
Alexandre Bounineda1589f2012-10-04 17:15:57 -0700446 * @rbase: RIO base address assigned to this window
447 * @size: Size of the memory region
448 * @rflags: Flags for mapping.
449 *
450 * Return: 0 -- Success.
451 *
452 * This function will create the mapping from RIO space to local memory.
453 */
454int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
455 u64 rbase, u32 size, u32 rflags)
456{
457 int rc = 0;
458 unsigned long flags;
459
460 if (!mport->ops->map_inb)
461 return -1;
462 spin_lock_irqsave(&rio_mmap_lock, flags);
463 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
464 spin_unlock_irqrestore(&rio_mmap_lock, flags);
465 return rc;
466}
467EXPORT_SYMBOL_GPL(rio_map_inb_region);
468
469/**
470 * rio_unmap_inb_region -- Unmap the inbound memory region
471 * @mport: Master port
472 * @lstart: physical address of memory region to be unmapped
473 */
474void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
475{
476 unsigned long flags;
477 if (!mport->ops->unmap_inb)
478 return;
479 spin_lock_irqsave(&rio_mmap_lock, flags);
480 mport->ops->unmap_inb(mport, lstart);
481 spin_unlock_irqrestore(&rio_mmap_lock, flags);
482}
483EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
484
485/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700486 * rio_mport_get_physefb - Helper function that returns register offset
487 * for Physical Layer Extended Features Block.
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700488 * @port: Master port to issue transaction
489 * @local: Indicate a local master port or remote device access
490 * @destid: Destination ID of the device
491 * @hopcount: Number of switch hops to the device
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700492 */
493u32
494rio_mport_get_physefb(struct rio_mport *port, int local,
495 u16 destid, u8 hopcount)
496{
497 u32 ext_ftr_ptr;
498 u32 ftr_header;
499
500 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
501
502 while (ext_ftr_ptr) {
503 if (local)
504 rio_local_read_config_32(port, ext_ftr_ptr,
505 &ftr_header);
506 else
507 rio_mport_read_config_32(port, destid, hopcount,
508 ext_ftr_ptr, &ftr_header);
509
510 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
511 switch (ftr_header) {
512
513 case RIO_EFB_SER_EP_ID_V13P:
514 case RIO_EFB_SER_EP_REC_ID_V13P:
515 case RIO_EFB_SER_EP_FREE_ID_V13P:
516 case RIO_EFB_SER_EP_ID:
517 case RIO_EFB_SER_EP_REC_ID:
518 case RIO_EFB_SER_EP_FREE_ID:
519 case RIO_EFB_SER_EP_FREC_ID:
520
521 return ext_ftr_ptr;
522
523 default:
524 break;
525 }
526
527 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
528 hopcount, ext_ftr_ptr);
529 }
530
531 return ext_ftr_ptr;
532}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700533EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700534
535/**
536 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700537 * @comp_tag: RIO component tag to match
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700538 * @from: Previous RIO device found in search, or %NULL for new search
539 *
540 * Iterates through the list of known RIO devices. If a RIO device is
541 * found with a matching @comp_tag, a pointer to its device
542 * structure is returned. Otherwise, %NULL is returned. A new search
543 * is initiated by passing %NULL to the @from argument. Otherwise, if
544 * @from is not %NULL, searches continue from next device on the global
545 * list.
546 */
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700547struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700548{
549 struct list_head *n;
550 struct rio_dev *rdev;
551
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700552 spin_lock(&rio_global_list_lock);
553 n = from ? from->global_list.next : rio_devices.next;
554
555 while (n && (n != &rio_devices)) {
556 rdev = rio_dev_g(n);
557 if (rdev->comp_tag == comp_tag)
558 goto exit;
559 n = n->next;
560 }
561 rdev = NULL;
562exit:
563 spin_unlock(&rio_global_list_lock);
564 return rdev;
565}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700566EXPORT_SYMBOL_GPL(rio_get_comptag);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700567
568/**
569 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
570 * @rdev: Pointer to RIO device control structure
571 * @pnum: Switch port number to set LOCKOUT bit
572 * @lock: Operation : set (=1) or clear (=0)
573 */
574int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
575{
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700576 u32 regval;
577
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800578 rio_read_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700579 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
580 &regval);
581 if (lock)
582 regval |= RIO_PORT_N_CTL_LOCKOUT;
583 else
584 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
585
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800586 rio_write_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700587 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
588 regval);
589 return 0;
590}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700591EXPORT_SYMBOL_GPL(rio_set_port_lockout);
592
593/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700594 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
595 * given port
596 * @port: Master port associated with the RIO network
597 * @local: local=1 select local port otherwise a far device is reached
598 * @destid: Destination ID of the device to check host bit
599 * @hopcount: Number of hops to reach the target
600 * @port_num: Port (-number on switch) to enable on a far end device
601 *
602 * Returns 0 or 1 from on General Control Command and Status Register
603 * (EXT_PTR+0x3C)
604 */
605int rio_enable_rx_tx_port(struct rio_mport *port,
606 int local, u16 destid,
607 u8 hopcount, u8 port_num)
608{
609#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
610 u32 regval;
611 u32 ext_ftr_ptr;
612
613 /*
614 * enable rx input tx output port
615 */
616 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
617 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
618
619 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
620
621 if (local) {
622 rio_local_read_config_32(port, ext_ftr_ptr +
623 RIO_PORT_N_CTL_CSR(0),
624 &regval);
625 } else {
626 if (rio_mport_read_config_32(port, destid, hopcount,
627 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), &regval) < 0)
628 return -EIO;
629 }
630
631 if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
632 /* serial */
633 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
634 | RIO_PORT_N_CTL_EN_TX_SER;
635 } else {
636 /* parallel */
637 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
638 | RIO_PORT_N_CTL_EN_TX_PAR;
639 }
640
641 if (local) {
642 rio_local_write_config_32(port, ext_ftr_ptr +
643 RIO_PORT_N_CTL_CSR(0), regval);
644 } else {
645 if (rio_mport_write_config_32(port, destid, hopcount,
646 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
647 return -EIO;
648 }
649#endif
650 return 0;
651}
652EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
653
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700654
655/**
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700656 * rio_chk_dev_route - Validate route to the specified device.
657 * @rdev: RIO device failed to respond
658 * @nrdev: Last active device on the route to rdev
659 * @npnum: nrdev's port number on the route to rdev
660 *
661 * Follows a route to the specified RIO device to determine the last available
662 * device (and corresponding RIO port) on the route.
663 */
664static int
665rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
666{
667 u32 result;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800668 int p_port, rc = -EIO;
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700669 struct rio_dev *prev = NULL;
670
671 /* Find switch with failed RIO link */
672 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
673 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
674 prev = rdev->prev;
675 break;
676 }
677 rdev = rdev->prev;
678 }
679
680 if (prev == NULL)
681 goto err_out;
682
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800683 p_port = prev->rswitch->route_table[rdev->destid];
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700684
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700685 if (p_port != RIO_INVALID_ROUTE) {
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700686 pr_debug("RIO: link failed on [%s]-P%d\n",
687 rio_name(prev), p_port);
688 *nrdev = prev;
689 *npnum = p_port;
690 rc = 0;
691 } else
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700692 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700693err_out:
694 return rc;
695}
696
697/**
698 * rio_mport_chk_dev_access - Validate access to the specified device.
699 * @mport: Master port to send transactions
700 * @destid: Device destination ID in network
701 * @hopcount: Number of hops into the network
702 */
Alexandre Bouninee274e0e2010-10-27 15:34:32 -0700703int
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700704rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
705{
706 int i = 0;
707 u32 tmp;
708
709 while (rio_mport_read_config_32(mport, destid, hopcount,
710 RIO_DEV_ID_CAR, &tmp)) {
711 i++;
712 if (i == RIO_MAX_CHK_RETRY)
713 return -EIO;
714 mdelay(1);
715 }
716
717 return 0;
718}
Alexandre Bouninea11650e2013-05-24 15:55:05 -0700719EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700720
721/**
722 * rio_chk_dev_access - Validate access to the specified device.
723 * @rdev: Pointer to RIO device control structure
724 */
725static int rio_chk_dev_access(struct rio_dev *rdev)
726{
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800727 return rio_mport_chk_dev_access(rdev->net->hport,
728 rdev->destid, rdev->hopcount);
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700729}
730
731/**
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700732 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
733 * returns link-response (if requested).
734 * @rdev: RIO devive to issue Input-status command
735 * @pnum: Device port number to issue the command
736 * @lnkresp: Response from a link partner
737 */
738static int
739rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
740{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700741 u32 regval;
742 int checkcount;
743
744 if (lnkresp) {
745 /* Read from link maintenance response register
746 * to clear valid bit */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800747 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700748 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
749 &regval);
750 udelay(50);
751 }
752
753 /* Issue Input-status command */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800754 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700755 rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
756 RIO_MNT_REQ_CMD_IS);
757
758 /* Exit if the response is not expected */
759 if (lnkresp == NULL)
760 return 0;
761
762 checkcount = 3;
763 while (checkcount--) {
764 udelay(50);
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800765 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700766 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
767 &regval);
768 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
769 *lnkresp = regval;
770 return 0;
771 }
772 }
773
774 return -EIO;
775}
776
777/**
778 * rio_clr_err_stopped - Clears port Error-stopped states.
779 * @rdev: Pointer to RIO device control structure
780 * @pnum: Switch port number to clear errors
781 * @err_status: port error status (if 0 reads register from device)
782 */
783static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
784{
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700785 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
786 u32 regval;
787 u32 far_ackid, far_linkstat, near_ackid;
788
789 if (err_status == 0)
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800790 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700791 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
792 &err_status);
793
794 if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
795 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
796 /*
797 * Send a Link-Request/Input-Status control symbol
798 */
799 if (rio_get_input_status(rdev, pnum, &regval)) {
800 pr_debug("RIO_EM: Input-status response timeout\n");
801 goto rd_err;
802 }
803
804 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
805 pnum, regval);
806 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
807 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800808 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700809 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
810 &regval);
811 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
812 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
813 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
814 " near_ackID=0x%02x\n",
815 pnum, far_ackid, far_linkstat, near_ackid);
816
817 /*
818 * If required, synchronize ackIDs of near and
819 * far sides.
820 */
821 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
822 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
823 /* Align near outstanding/outbound ackIDs with
824 * far inbound.
825 */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800826 rio_write_config_32(rdev,
827 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700828 (near_ackid << 24) |
829 (far_ackid << 8) | far_ackid);
830 /* Align far outstanding/outbound ackIDs with
831 * near inbound.
832 */
833 far_ackid++;
834 if (nextdev)
835 rio_write_config_32(nextdev,
836 nextdev->phys_efptr +
837 RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
838 (far_ackid << 24) |
839 (near_ackid << 8) | near_ackid);
840 else
841 pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
842 }
843rd_err:
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800844 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700845 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
846 &err_status);
847 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
848 }
849
850 if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
851 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
852 rio_get_input_status(nextdev,
853 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
854 udelay(50);
855
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800856 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700857 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
858 &err_status);
859 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
860 }
861
862 return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
863 RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
864}
865
866/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700867 * rio_inb_pwrite_handler - process inbound port-write message
868 * @pw_msg: pointer to inbound port-write message
869 *
870 * Processes an inbound port-write message. Returns 0 if the request
871 * has been satisfied.
872 */
873int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
874{
875 struct rio_dev *rdev;
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700876 u32 err_status, em_perrdet, em_ltlerrdet;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700877 int rc, portnum;
878
Alexandre Bouninee6536922011-01-12 17:00:40 -0800879 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700880 if (rdev == NULL) {
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700881 /* Device removed or enumeration error */
882 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700883 __func__, pw_msg->em.comptag);
884 return -EIO;
885 }
886
887 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
888
889#ifdef DEBUG_PW
890 {
891 u32 i;
892 for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700893 pr_debug("0x%02x: %08x %08x %08x %08x\n",
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700894 i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
895 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
896 i += 4;
897 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700898 }
899#endif
900
901 /* Call an external service function (if such is registered
902 * for this device). This may be the service for endpoints that send
903 * device-specific port-write messages. End-point messages expected
904 * to be handled completely by EP specific device driver.
905 * For switches rc==0 signals that no standard processing required.
906 */
907 if (rdev->pwcback != NULL) {
908 rc = rdev->pwcback(rdev, pw_msg, 0);
909 if (rc == 0)
910 return 0;
911 }
912
Alexandre Bounine6429cd42010-10-27 15:34:32 -0700913 portnum = pw_msg->em.is_port & 0xFF;
914
915 /* Check if device and route to it are functional:
916 * Sometimes devices may send PW message(s) just before being
917 * powered down (or link being lost).
918 */
919 if (rio_chk_dev_access(rdev)) {
920 pr_debug("RIO: device access failed - get link partner\n");
921 /* Scan route to the device and identify failed link.
922 * This will replace device and port reported in PW message.
923 * PW message should not be used after this point.
924 */
925 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
926 pr_err("RIO: Route trace for %s failed\n",
927 rio_name(rdev));
928 return -EIO;
929 }
930 pw_msg = NULL;
931 }
932
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700933 /* For End-point devices processing stops here */
934 if (!(rdev->pef & RIO_PEF_SWITCH))
935 return 0;
936
937 if (rdev->phys_efptr == 0) {
938 pr_err("RIO_PW: Bad switch initialization for %s\n",
939 rio_name(rdev));
940 return 0;
941 }
942
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700943 /*
944 * Process the port-write notification from switch
945 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -0700946 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
947 rdev->rswitch->ops->em_handle(rdev, portnum);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700948
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800949 rio_read_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700950 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
951 &err_status);
952 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
953
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700954 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700955
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700956 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
957 rdev->rswitch->port_ok |= (1 << portnum);
958 rio_set_port_lockout(rdev, portnum, 0);
959 /* Schedule Insertion Service */
960 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
961 rio_name(rdev), portnum);
962 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700963
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700964 /* Clear error-stopped states (if reported).
965 * Depending on the link partner state, two attempts
966 * may be needed for successful recovery.
967 */
968 if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
969 RIO_PORT_N_ERR_STS_PW_INP_ES)) {
970 if (rio_clr_err_stopped(rdev, portnum, err_status))
971 rio_clr_err_stopped(rdev, portnum, 0);
972 }
973 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700974
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700975 if (rdev->rswitch->port_ok & (1 << portnum)) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700976 rdev->rswitch->port_ok &= ~(1 << portnum);
977 rio_set_port_lockout(rdev, portnum, 1);
978
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800979 rio_write_config_32(rdev,
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700980 rdev->phys_efptr +
981 RIO_PORT_N_ACK_STS_CSR(portnum),
982 RIO_PORT_N_ACK_CLEAR);
983
984 /* Schedule Extraction Service */
985 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
986 rio_name(rdev), portnum);
987 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700988 }
989
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800990 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700991 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
992 if (em_perrdet) {
993 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
994 portnum, em_perrdet);
995 /* Clear EM Port N Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800996 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700997 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
998 }
999
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001000 rio_read_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001001 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1002 if (em_ltlerrdet) {
1003 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1004 em_ltlerrdet);
1005 /* Clear EM L/T Layer Error Detect CSR */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001006 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001007 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1008 }
1009
Alexandre Bounine388c45c2010-10-27 15:34:35 -07001010 /* Clear remaining error bits and Port-Write Pending bit */
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001011 rio_write_config_32(rdev,
Alexandre Bouninedd5648c2010-10-27 15:34:30 -07001012 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
Alexandre Bounine388c45c2010-10-27 15:34:35 -07001013 err_status);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001014
1015 return 0;
1016}
1017EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1018
1019/**
1020 * rio_mport_get_efb - get pointer to next extended features block
1021 * @port: Master port to issue transaction
1022 * @local: Indicate a local master port or remote device access
1023 * @destid: Destination ID of the device
1024 * @hopcount: Number of switch hops to the device
1025 * @from: Offset of current Extended Feature block header (if 0 starts
1026 * from ExtFeaturePtr)
1027 */
1028u32
1029rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1030 u8 hopcount, u32 from)
1031{
1032 u32 reg_val;
1033
1034 if (from == 0) {
1035 if (local)
1036 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1037 &reg_val);
1038 else
1039 rio_mport_read_config_32(port, destid, hopcount,
1040 RIO_ASM_INFO_CAR, &reg_val);
1041 return reg_val & RIO_EXT_FTR_PTR_MASK;
1042 } else {
1043 if (local)
1044 rio_local_read_config_32(port, from, &reg_val);
1045 else
1046 rio_mport_read_config_32(port, destid, hopcount,
1047 from, &reg_val);
1048 return RIO_GET_BLOCK_ID(reg_val);
1049 }
1050}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001051EXPORT_SYMBOL_GPL(rio_mport_get_efb);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001052
1053/**
Matt Porter394b7012005-11-07 01:00:15 -08001054 * rio_mport_get_feature - query for devices' extended features
1055 * @port: Master port to issue transaction
1056 * @local: Indicate a local master port or remote device access
1057 * @destid: Destination ID of the device
1058 * @hopcount: Number of switch hops to the device
1059 * @ftr: Extended feature code
1060 *
1061 * Tell if a device supports a given RapidIO capability.
1062 * Returns the offset of the requested extended feature
1063 * block within the device's RIO configuration space or
1064 * 0 in case the device does not support it. Possible
1065 * values for @ftr:
1066 *
1067 * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
1068 *
1069 * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
1070 *
1071 * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
1072 *
1073 * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
1074 *
1075 * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
1076 *
1077 * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
1078 */
1079u32
1080rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1081 u8 hopcount, int ftr)
1082{
1083 u32 asm_info, ext_ftr_ptr, ftr_header;
1084
1085 if (local)
1086 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1087 else
1088 rio_mport_read_config_32(port, destid, hopcount,
1089 RIO_ASM_INFO_CAR, &asm_info);
1090
1091 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1092
1093 while (ext_ftr_ptr) {
1094 if (local)
1095 rio_local_read_config_32(port, ext_ftr_ptr,
1096 &ftr_header);
1097 else
1098 rio_mport_read_config_32(port, destid, hopcount,
1099 ext_ftr_ptr, &ftr_header);
1100 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1101 return ext_ftr_ptr;
1102 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
1103 break;
1104 }
1105
1106 return 0;
1107}
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001108EXPORT_SYMBOL_GPL(rio_mport_get_feature);
Matt Porter394b7012005-11-07 01:00:15 -08001109
1110/**
1111 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
1112 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1113 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1114 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
1115 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
1116 * @from: Previous RIO device found in search, or %NULL for new search
1117 *
1118 * Iterates through the list of known RIO devices. If a RIO device is
1119 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
1120 * count to the device is incrememted and a pointer to its device
1121 * structure is returned. Otherwise, %NULL is returned. A new search
1122 * is initiated by passing %NULL to the @from argument. Otherwise, if
1123 * @from is not %NULL, searches continue from next device on the global
1124 * list. The reference count for @from is always decremented if it is
1125 * not %NULL.
1126 */
1127struct rio_dev *rio_get_asm(u16 vid, u16 did,
1128 u16 asm_vid, u16 asm_did, struct rio_dev *from)
1129{
1130 struct list_head *n;
1131 struct rio_dev *rdev;
1132
1133 WARN_ON(in_interrupt());
1134 spin_lock(&rio_global_list_lock);
1135 n = from ? from->global_list.next : rio_devices.next;
1136
1137 while (n && (n != &rio_devices)) {
1138 rdev = rio_dev_g(n);
1139 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1140 (did == RIO_ANY_ID || rdev->did == did) &&
1141 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1142 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1143 goto exit;
1144 n = n->next;
1145 }
1146 rdev = NULL;
1147 exit:
1148 rio_dev_put(from);
1149 rdev = rio_dev_get(rdev);
1150 spin_unlock(&rio_global_list_lock);
1151 return rdev;
1152}
1153
1154/**
1155 * rio_get_device - Begin or continue searching for a RIO device by vid/did
1156 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1157 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1158 * @from: Previous RIO device found in search, or %NULL for new search
1159 *
1160 * Iterates through the list of known RIO devices. If a RIO device is
1161 * found with a matching @vid and @did, the reference count to the
1162 * device is incrememted and a pointer to its device structure is returned.
1163 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
1164 * to the @from argument. Otherwise, if @from is not %NULL, searches
1165 * continue from next device on the global list. The reference count for
1166 * @from is always decremented if it is not %NULL.
1167 */
1168struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1169{
1170 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1171}
1172
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001173/**
1174 * rio_std_route_add_entry - Add switch route table entry using standard
1175 * registers defined in RIO specification rev.1.3
1176 * @mport: Master port to issue transaction
1177 * @destid: Destination ID of the device
1178 * @hopcount: Number of switch hops to the device
1179 * @table: routing table ID (global or port-specific)
1180 * @route_destid: destID entry in the RT
1181 * @route_port: destination port for specified destID
1182 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001183static int
1184rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1185 u16 table, u16 route_destid, u8 route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001186{
1187 if (table == RIO_GLOBAL_TABLE) {
1188 rio_mport_write_config_32(mport, destid, hopcount,
1189 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1190 (u32)route_destid);
1191 rio_mport_write_config_32(mport, destid, hopcount,
1192 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1193 (u32)route_port);
1194 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001195
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001196 udelay(10);
1197 return 0;
1198}
1199
1200/**
1201 * rio_std_route_get_entry - Read switch route table entry (port number)
Uwe Kleine-König638c5942010-06-11 12:16:58 +02001202 * associated with specified destID using standard registers defined in RIO
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001203 * specification rev.1.3
1204 * @mport: Master port to issue transaction
1205 * @destid: Destination ID of the device
1206 * @hopcount: Number of switch hops to the device
1207 * @table: routing table ID (global or port-specific)
1208 * @route_destid: destID entry in the RT
1209 * @route_port: returned destination port for specified destID
1210 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001211static int
1212rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1213 u16 table, u16 route_destid, u8 *route_port)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001214{
1215 u32 result;
1216
1217 if (table == RIO_GLOBAL_TABLE) {
1218 rio_mport_write_config_32(mport, destid, hopcount,
1219 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1220 rio_mport_read_config_32(mport, destid, hopcount,
1221 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1222
1223 *route_port = (u8)result;
1224 }
1225
1226 return 0;
1227}
1228
1229/**
1230 * rio_std_route_clr_table - Clear swotch route table using standard registers
1231 * defined in RIO specification rev.1.3.
1232 * @mport: Master port to issue transaction
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001233 * @destid: Destination ID of the device
1234 * @hopcount: Number of switch hops to the device
1235 * @table: routing table ID (global or port-specific)
1236 */
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001237static int
1238rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1239 u16 table)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001240{
1241 u32 max_destid = 0xff;
1242 u32 i, pef, id_inc = 1, ext_cfg = 0;
1243 u32 port_sel = RIO_INVALID_ROUTE;
1244
1245 if (table == RIO_GLOBAL_TABLE) {
1246 rio_mport_read_config_32(mport, destid, hopcount,
1247 RIO_PEF_CAR, &pef);
1248
1249 if (mport->sys_size) {
1250 rio_mport_read_config_32(mport, destid, hopcount,
1251 RIO_SWITCH_RT_LIMIT,
1252 &max_destid);
1253 max_destid &= RIO_RT_MAX_DESTID;
1254 }
1255
1256 if (pef & RIO_PEF_EXT_RT) {
1257 ext_cfg = 0x80000000;
1258 id_inc = 4;
1259 port_sel = (RIO_INVALID_ROUTE << 24) |
1260 (RIO_INVALID_ROUTE << 16) |
1261 (RIO_INVALID_ROUTE << 8) |
1262 RIO_INVALID_ROUTE;
1263 }
1264
1265 for (i = 0; i <= max_destid;) {
1266 rio_mport_write_config_32(mport, destid, hopcount,
1267 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1268 ext_cfg | i);
1269 rio_mport_write_config_32(mport, destid, hopcount,
1270 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1271 port_sel);
1272 i += id_inc;
1273 }
1274 }
1275
1276 udelay(10);
1277 return 0;
1278}
1279
Alexandre Bounine2ec3ba62013-07-03 15:08:50 -07001280/**
1281 * rio_lock_device - Acquires host device lock for specified device
1282 * @port: Master port to send transaction
1283 * @destid: Destination ID for device/switch
1284 * @hopcount: Hopcount to reach switch
1285 * @wait_ms: Max wait time in msec (0 = no timeout)
1286 *
1287 * Attepts to acquire host device lock for specified device
1288 * Returns 0 if device lock acquired or EINVAL if timeout expires.
1289 */
1290int rio_lock_device(struct rio_mport *port, u16 destid,
1291 u8 hopcount, int wait_ms)
1292{
1293 u32 result;
1294 int tcnt = 0;
1295
1296 /* Attempt to acquire device lock */
1297 rio_mport_write_config_32(port, destid, hopcount,
1298 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1299 rio_mport_read_config_32(port, destid, hopcount,
1300 RIO_HOST_DID_LOCK_CSR, &result);
1301
1302 while (result != port->host_deviceid) {
1303 if (wait_ms != 0 && tcnt == wait_ms) {
1304 pr_debug("RIO: timeout when locking device %x:%x\n",
1305 destid, hopcount);
1306 return -EINVAL;
1307 }
1308
1309 /* Delay a bit */
1310 mdelay(1);
1311 tcnt++;
1312 /* Try to acquire device lock again */
1313 rio_mport_write_config_32(port, destid,
1314 hopcount,
1315 RIO_HOST_DID_LOCK_CSR,
1316 port->host_deviceid);
1317 rio_mport_read_config_32(port, destid,
1318 hopcount,
1319 RIO_HOST_DID_LOCK_CSR, &result);
1320 }
1321
1322 return 0;
1323}
1324EXPORT_SYMBOL_GPL(rio_lock_device);
1325
1326/**
1327 * rio_unlock_device - Releases host device lock for specified device
1328 * @port: Master port to send transaction
1329 * @destid: Destination ID for device/switch
1330 * @hopcount: Hopcount to reach switch
1331 *
1332 * Returns 0 if device lock released or EINVAL if fails.
1333 */
1334int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1335{
1336 u32 result;
1337
1338 /* Release device lock */
1339 rio_mport_write_config_32(port, destid,
1340 hopcount,
1341 RIO_HOST_DID_LOCK_CSR,
1342 port->host_deviceid);
1343 rio_mport_read_config_32(port, destid, hopcount,
1344 RIO_HOST_DID_LOCK_CSR, &result);
1345 if ((result & 0xffff) != 0xffff) {
1346 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1347 destid, hopcount);
1348 return -EINVAL;
1349 }
1350
1351 return 0;
1352}
1353EXPORT_SYMBOL_GPL(rio_unlock_device);
1354
1355/**
1356 * rio_route_add_entry- Add a route entry to a switch routing table
1357 * @rdev: RIO device
1358 * @table: Routing table ID
1359 * @route_destid: Destination ID to be routed
1360 * @route_port: Port number to be routed
1361 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1362 *
1363 * If available calls the switch specific add_entry() method to add a route
1364 * entry into a switch routing table. Otherwise uses standard RT update method
1365 * as defined by RapidIO specification. A specific routing table can be selected
1366 * using the @table argument if a switch has per port routing tables or
1367 * the standard (or global) table may be used by passing
1368 * %RIO_GLOBAL_TABLE in @table.
1369 *
1370 * Returns %0 on success or %-EINVAL on failure.
1371 */
1372int rio_route_add_entry(struct rio_dev *rdev,
1373 u16 table, u16 route_destid, u8 route_port, int lock)
1374{
1375 int rc = -EINVAL;
1376 struct rio_switch_ops *ops = rdev->rswitch->ops;
1377
1378 if (lock) {
1379 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1380 rdev->hopcount, 1000);
1381 if (rc)
1382 return rc;
1383 }
1384
1385 spin_lock(&rdev->rswitch->lock);
1386
1387 if (ops == NULL || ops->add_entry == NULL) {
1388 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1389 rdev->hopcount, table,
1390 route_destid, route_port);
1391 } else if (try_module_get(ops->owner)) {
1392 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1393 rdev->hopcount, table, route_destid,
1394 route_port);
1395 module_put(ops->owner);
1396 }
1397
1398 spin_unlock(&rdev->rswitch->lock);
1399
1400 if (lock)
1401 rio_unlock_device(rdev->net->hport, rdev->destid,
1402 rdev->hopcount);
1403
1404 return rc;
1405}
1406EXPORT_SYMBOL_GPL(rio_route_add_entry);
1407
1408/**
1409 * rio_route_get_entry- Read an entry from a switch routing table
1410 * @rdev: RIO device
1411 * @table: Routing table ID
1412 * @route_destid: Destination ID to be routed
1413 * @route_port: Pointer to read port number into
1414 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1415 *
1416 * If available calls the switch specific get_entry() method to fetch a route
1417 * entry from a switch routing table. Otherwise uses standard RT read method
1418 * as defined by RapidIO specification. A specific routing table can be selected
1419 * using the @table argument if a switch has per port routing tables or
1420 * the standard (or global) table may be used by passing
1421 * %RIO_GLOBAL_TABLE in @table.
1422 *
1423 * Returns %0 on success or %-EINVAL on failure.
1424 */
1425int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1426 u16 route_destid, u8 *route_port, int lock)
1427{
1428 int rc = -EINVAL;
1429 struct rio_switch_ops *ops = rdev->rswitch->ops;
1430
1431 if (lock) {
1432 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1433 rdev->hopcount, 1000);
1434 if (rc)
1435 return rc;
1436 }
1437
1438 spin_lock(&rdev->rswitch->lock);
1439
1440 if (ops == NULL || ops->get_entry == NULL) {
1441 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1442 rdev->hopcount, table,
1443 route_destid, route_port);
1444 } else if (try_module_get(ops->owner)) {
1445 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1446 rdev->hopcount, table, route_destid,
1447 route_port);
1448 module_put(ops->owner);
1449 }
1450
1451 spin_unlock(&rdev->rswitch->lock);
1452
1453 if (lock)
1454 rio_unlock_device(rdev->net->hport, rdev->destid,
1455 rdev->hopcount);
1456 return rc;
1457}
1458EXPORT_SYMBOL_GPL(rio_route_get_entry);
1459
1460/**
1461 * rio_route_clr_table - Clear a switch routing table
1462 * @rdev: RIO device
1463 * @table: Routing table ID
1464 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1465 *
1466 * If available calls the switch specific clr_table() method to clear a switch
1467 * routing table. Otherwise uses standard RT write method as defined by RapidIO
1468 * specification. A specific routing table can be selected using the @table
1469 * argument if a switch has per port routing tables or the standard (or global)
1470 * table may be used by passing %RIO_GLOBAL_TABLE in @table.
1471 *
1472 * Returns %0 on success or %-EINVAL on failure.
1473 */
1474int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1475{
1476 int rc = -EINVAL;
1477 struct rio_switch_ops *ops = rdev->rswitch->ops;
1478
1479 if (lock) {
1480 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1481 rdev->hopcount, 1000);
1482 if (rc)
1483 return rc;
1484 }
1485
1486 spin_lock(&rdev->rswitch->lock);
1487
1488 if (ops == NULL || ops->clr_table == NULL) {
1489 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1490 rdev->hopcount, table);
1491 } else if (try_module_get(ops->owner)) {
1492 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1493 rdev->hopcount, table);
1494
1495 module_put(ops->owner);
1496 }
1497
1498 spin_unlock(&rdev->rswitch->lock);
1499
1500 if (lock)
1501 rio_unlock_device(rdev->net->hport, rdev->destid,
1502 rdev->hopcount);
1503
1504 return rc;
1505}
1506EXPORT_SYMBOL_GPL(rio_route_clr_table);
1507
Alexandre Bouninee42d98e2012-05-31 16:26:38 -07001508#ifdef CONFIG_RAPIDIO_DMA_ENGINE
1509
1510static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1511{
1512 struct rio_dev *rdev = arg;
1513
1514 /* Check that DMA device belongs to the right MPORT */
1515 return (rdev->net->hport ==
1516 container_of(chan->device, struct rio_mport, dma));
1517}
1518
1519/**
1520 * rio_request_dma - request RapidIO capable DMA channel that supports
1521 * specified target RapidIO device.
1522 * @rdev: RIO device control structure
1523 *
1524 * Returns pointer to allocated DMA channel or NULL if failed.
1525 */
1526struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1527{
1528 dma_cap_mask_t mask;
1529 struct dma_chan *dchan;
1530
1531 dma_cap_zero(mask);
1532 dma_cap_set(DMA_SLAVE, mask);
1533 dchan = dma_request_channel(mask, rio_chan_filter, rdev);
1534
1535 return dchan;
1536}
1537EXPORT_SYMBOL_GPL(rio_request_dma);
1538
1539/**
1540 * rio_release_dma - release specified DMA channel
1541 * @dchan: DMA channel to release
1542 */
1543void rio_release_dma(struct dma_chan *dchan)
1544{
1545 dma_release_channel(dchan);
1546}
1547EXPORT_SYMBOL_GPL(rio_release_dma);
1548
1549/**
1550 * rio_dma_prep_slave_sg - RapidIO specific wrapper
1551 * for device_prep_slave_sg callback defined by DMAENGINE.
1552 * @rdev: RIO device control structure
1553 * @dchan: DMA channel to configure
1554 * @data: RIO specific data descriptor
1555 * @direction: DMA data transfer direction (TO or FROM the device)
1556 * @flags: dmaengine defined flags
1557 *
1558 * Initializes RapidIO capable DMA channel for the specified data transfer.
1559 * Uses DMA channel private extension to pass information related to remote
1560 * target RIO device.
1561 * Returns pointer to DMA transaction descriptor or NULL if failed.
1562 */
1563struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1564 struct dma_chan *dchan, struct rio_dma_data *data,
1565 enum dma_transfer_direction direction, unsigned long flags)
1566{
1567 struct dma_async_tx_descriptor *txd = NULL;
1568 struct rio_dma_ext rio_ext;
1569
1570 if (dchan->device->device_prep_slave_sg == NULL) {
1571 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1572 return NULL;
1573 }
1574
1575 rio_ext.destid = rdev->destid;
1576 rio_ext.rio_addr_u = data->rio_addr_u;
1577 rio_ext.rio_addr = data->rio_addr;
1578 rio_ext.wr_type = data->wr_type;
1579
1580 txd = dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1581 direction, flags, &rio_ext);
1582
1583 return txd;
1584}
1585EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1586
1587#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1588
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001589/**
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07001590 * rio_find_mport - find RIO mport by its ID
1591 * @mport_id: number (ID) of mport device
1592 *
1593 * Given a RIO mport number, the desired mport is located
1594 * in the global list of mports. If the mport is found, a pointer to its
1595 * data structure is returned. If no mport is found, %NULL is returned.
1596 */
1597struct rio_mport *rio_find_mport(int mport_id)
1598{
1599 struct rio_mport *port;
1600
1601 mutex_lock(&rio_mport_list_lock);
1602 list_for_each_entry(port, &rio_mports, node) {
1603 if (port->id == mport_id)
1604 goto found;
1605 }
1606 port = NULL;
1607found:
1608 mutex_unlock(&rio_mport_list_lock);
1609
1610 return port;
1611}
1612
1613/**
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001614 * rio_register_scan - enumeration/discovery method registration interface
1615 * @mport_id: mport device ID for which fabric scan routine has to be set
1616 * (RIO_MPORT_ANY = set for all available mports)
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001617 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001618 *
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001619 * Registers enumeration/discovery operations with RapidIO subsystem and
1620 * attaches it to the specified mport device (or all available mports
1621 * if RIO_MPORT_ANY is specified).
1622 *
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001623 * Returns error if the mport already has an enumerator attached to it.
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001624 * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001625 */
1626int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1627{
1628 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001629 struct rio_scan_node *scan;
1630 int rc = 0;
1631
1632 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1633
1634 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1635 !scan_ops)
1636 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001637
1638 mutex_lock(&rio_mport_list_lock);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001639
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001640 /*
1641 * Check if there is another enumerator already registered for
1642 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
1643 * for the same mport ID are not supported.
1644 */
1645 list_for_each_entry(scan, &rio_scans, node) {
1646 if (scan->mport_id == mport_id) {
1647 rc = -EBUSY;
1648 goto err_out;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001649 }
1650 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001651
1652 /*
1653 * Allocate and initialize new scan registration node.
1654 */
1655 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
1656 if (!scan) {
1657 rc = -ENOMEM;
1658 goto err_out;
1659 }
1660
1661 scan->mport_id = mport_id;
1662 scan->ops = scan_ops;
1663
1664 /*
1665 * Traverse the list of registered mports to attach this new scan.
1666 *
1667 * The new scan with matching mport ID overrides any previously attached
1668 * scan assuming that old scan (if any) is the default one (based on the
1669 * enumerator registration check above).
1670 * If the new scan is the global one, it will be attached only to mports
1671 * that do not have their own individual operations already attached.
1672 */
1673 list_for_each_entry(port, &rio_mports, node) {
1674 if (port->id == mport_id) {
1675 port->nscan = scan_ops;
1676 break;
1677 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
1678 port->nscan = scan_ops;
1679 }
1680
1681 list_add_tail(&scan->node, &rio_scans);
1682
1683err_out:
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001684 mutex_unlock(&rio_mport_list_lock);
1685
1686 return rc;
1687}
1688EXPORT_SYMBOL_GPL(rio_register_scan);
1689
1690/**
1691 * rio_unregister_scan - removes enumeration/discovery method from mport
1692 * @mport_id: mport device ID for which fabric scan routine has to be
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001693 * unregistered (RIO_MPORT_ANY = apply to all mports that use
1694 * the specified scan_ops)
1695 * @scan_ops: enumeration/discovery operations structure
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001696 *
1697 * Removes enumeration or discovery method assigned to the specified mport
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001698 * device. If RIO_MPORT_ANY is specified, removes the specified operations from
1699 * all mports that have them attached.
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001700 */
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001701int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001702{
1703 struct rio_mport *port;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001704 struct rio_scan_node *scan;
1705
1706 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1707
1708 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
1709 return -EINVAL;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001710
1711 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001712
1713 list_for_each_entry(port, &rio_mports, node)
1714 if (port->id == mport_id ||
1715 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
1716 port->nscan = NULL;
1717
Dan Carpenterf93f3c42013-07-31 13:53:34 -07001718 list_for_each_entry(scan, &rio_scans, node) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001719 if (scan->mport_id == mport_id) {
1720 list_del(&scan->node);
1721 kfree(scan);
Dan Carpenterf93f3c42013-07-31 13:53:34 -07001722 break;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001723 }
Dan Carpenterf93f3c42013-07-31 13:53:34 -07001724 }
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001725
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001726 mutex_unlock(&rio_mport_list_lock);
1727
1728 return 0;
1729}
1730EXPORT_SYMBOL_GPL(rio_unregister_scan);
1731
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001732/**
1733 * rio_mport_scan - execute enumeration/discovery on the specified mport
1734 * @mport_id: number (ID) of mport device
1735 */
1736int rio_mport_scan(int mport_id)
1737{
1738 struct rio_mport *port = NULL;
1739 int rc;
1740
1741 mutex_lock(&rio_mport_list_lock);
1742 list_for_each_entry(port, &rio_mports, node) {
1743 if (port->id == mport_id)
1744 goto found;
1745 }
1746 mutex_unlock(&rio_mport_list_lock);
1747 return -ENODEV;
1748found:
1749 if (!port->nscan) {
1750 mutex_unlock(&rio_mport_list_lock);
1751 return -EINVAL;
1752 }
1753
1754 if (!try_module_get(port->nscan->owner)) {
1755 mutex_unlock(&rio_mport_list_lock);
1756 return -ENODEV;
1757 }
1758
1759 mutex_unlock(&rio_mport_list_lock);
1760
1761 if (port->host_deviceid >= 0)
1762 rc = port->nscan->enumerate(port, 0);
1763 else
1764 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
1765
1766 module_put(port->nscan->owner);
1767 return rc;
1768}
1769
Matt Porter394b7012005-11-07 01:00:15 -08001770static void rio_fixup_device(struct rio_dev *dev)
1771{
1772}
1773
Bill Pemberton305c8912012-11-19 13:23:25 -05001774static int rio_init(void)
Matt Porter394b7012005-11-07 01:00:15 -08001775{
1776 struct rio_dev *dev = NULL;
1777
1778 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
1779 rio_fixup_device(dev);
1780 }
1781 return 0;
1782}
1783
Alexandre Bounine005842e2012-10-04 17:16:08 -07001784static struct workqueue_struct *rio_wq;
1785
1786struct rio_disc_work {
1787 struct work_struct work;
1788 struct rio_mport *mport;
1789};
1790
Bill Pemberton305c8912012-11-19 13:23:25 -05001791static void disc_work_handler(struct work_struct *_work)
Alexandre Bounine005842e2012-10-04 17:16:08 -07001792{
1793 struct rio_disc_work *work;
1794
1795 work = container_of(_work, struct rio_disc_work, work);
1796 pr_debug("RIO: discovery work for mport %d %s\n",
1797 work->mport->id, work->mport->name);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001798 if (try_module_get(work->mport->nscan->owner)) {
1799 work->mport->nscan->discover(work->mport, 0);
1800 module_put(work->mport->nscan->owner);
1801 }
Alexandre Bounine005842e2012-10-04 17:16:08 -07001802}
1803
Bill Pemberton305c8912012-11-19 13:23:25 -05001804int rio_init_mports(void)
Matt Porter394b7012005-11-07 01:00:15 -08001805{
Matt Porter394b7012005-11-07 01:00:15 -08001806 struct rio_mport *port;
Alexandre Bounine005842e2012-10-04 17:16:08 -07001807 struct rio_disc_work *work;
Alexandre Bounine25747402012-10-10 15:53:59 -07001808 int n = 0;
Matt Porter394b7012005-11-07 01:00:15 -08001809
Alexandre Bounine25747402012-10-10 15:53:59 -07001810 if (!next_portid)
1811 return -ENODEV;
1812
1813 /*
1814 * First, run enumerations and check if we need to perform discovery
1815 * on any of the registered mports.
1816 */
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001817 mutex_lock(&rio_mport_list_lock);
Matt Porter394b7012005-11-07 01:00:15 -08001818 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001819 if (port->host_deviceid >= 0) {
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001820 if (port->nscan && try_module_get(port->nscan->owner)) {
Alexandre Bouninebc8fcfe2013-05-24 15:55:06 -07001821 port->nscan->enumerate(port, 0);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001822 module_put(port->nscan->owner);
1823 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001824 } else
Alexandre Bounine25747402012-10-10 15:53:59 -07001825 n++;
1826 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001827 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine005842e2012-10-04 17:16:08 -07001828
Alexandre Bounine25747402012-10-10 15:53:59 -07001829 if (!n)
1830 goto no_disc;
Alexandre Bounine005842e2012-10-04 17:16:08 -07001831
Alexandre Bounine25747402012-10-10 15:53:59 -07001832 /*
1833 * If we have mports that require discovery schedule a discovery work
1834 * for each of them. If the code below fails to allocate needed
1835 * resources, exit without error to keep results of enumeration
1836 * process (if any).
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001837 * TODO: Implement restart of discovery process for all or
Alexandre Bounine25747402012-10-10 15:53:59 -07001838 * individual discovering mports.
1839 */
1840 rio_wq = alloc_workqueue("riodisc", 0, 0);
1841 if (!rio_wq) {
1842 pr_err("RIO: unable allocate rio_wq\n");
1843 goto no_disc;
1844 }
1845
1846 work = kcalloc(n, sizeof *work, GFP_KERNEL);
1847 if (!work) {
1848 pr_err("RIO: no memory for work struct\n");
1849 destroy_workqueue(rio_wq);
1850 goto no_disc;
1851 }
1852
1853 n = 0;
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001854 mutex_lock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07001855 list_for_each_entry(port, &rio_mports, node) {
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001856 if (port->host_deviceid < 0 && port->nscan) {
Alexandre Bounine25747402012-10-10 15:53:59 -07001857 work[n].mport = port;
1858 INIT_WORK(&work[n].work, disc_work_handler);
1859 queue_work(rio_wq, &work[n].work);
1860 n++;
Alexandre Bounine005842e2012-10-04 17:16:08 -07001861 }
1862 }
1863
Alexandre Bounine25747402012-10-10 15:53:59 -07001864 flush_workqueue(rio_wq);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001865 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine25747402012-10-10 15:53:59 -07001866 pr_debug("RIO: destroy discovery workqueue\n");
1867 destroy_workqueue(rio_wq);
1868 kfree(work);
Matt Porter394b7012005-11-07 01:00:15 -08001869
Alexandre Bounine25747402012-10-10 15:53:59 -07001870no_disc:
Alexandre Bounine2f809982011-03-23 16:43:04 -07001871 rio_init();
1872
Alexandre Bouninec1256eb2011-03-23 16:43:06 -07001873 return 0;
Matt Porter394b7012005-11-07 01:00:15 -08001874}
1875
Alexandre Bounine569fccb2011-03-23 16:43:05 -07001876static int rio_get_hdid(int index)
1877{
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07001878 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
Alexandre Bounine569fccb2011-03-23 16:43:05 -07001879 return -1;
1880
Alexandre Bouninefdf90ab2013-07-03 15:08:56 -07001881 return hdid[index];
Alexandre Bounine569fccb2011-03-23 16:43:05 -07001882}
1883
Alexandre Bounine59f99962011-04-14 15:22:14 -07001884int rio_register_mport(struct rio_mport *port)
Matt Porter394b7012005-11-07 01:00:15 -08001885{
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001886 struct rio_scan_node *scan = NULL;
1887
Alexandre Bounine569fccb2011-03-23 16:43:05 -07001888 if (next_portid >= RIO_MAX_MPORTS) {
1889 pr_err("RIO: reached specified max number of mports\n");
Alexandre Bounine59f99962011-04-14 15:22:14 -07001890 return 1;
Alexandre Bounine569fccb2011-03-23 16:43:05 -07001891 }
1892
1893 port->id = next_portid++;
1894 port->host_deviceid = rio_get_hdid(port->id);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001895 port->nscan = NULL;
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001896
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001897 mutex_lock(&rio_mport_list_lock);
Matt Porter394b7012005-11-07 01:00:15 -08001898 list_add_tail(&port->node, &rio_mports);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001899
1900 /*
1901 * Check if there are any registered enumeration/discovery operations
1902 * that have to be attached to the added mport.
1903 */
1904 list_for_each_entry(scan, &rio_scans, node) {
1905 if (port->id == scan->mport_id ||
1906 scan->mport_id == RIO_MPORT_ANY) {
1907 port->nscan = scan->ops;
1908 if (port->id == scan->mport_id)
1909 break;
1910 }
1911 }
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001912 mutex_unlock(&rio_mport_list_lock);
Alexandre Bounine9edbc30b2013-07-03 15:08:53 -07001913
1914 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
Alexandre Bounine59f99962011-04-14 15:22:14 -07001915 return 0;
Matt Porter394b7012005-11-07 01:00:15 -08001916}
Alexandre Bounine94d9bd42013-07-03 15:08:55 -07001917EXPORT_SYMBOL_GPL(rio_register_mport);
Matt Porter394b7012005-11-07 01:00:15 -08001918
1919EXPORT_SYMBOL_GPL(rio_local_get_device_id);
1920EXPORT_SYMBOL_GPL(rio_get_device);
1921EXPORT_SYMBOL_GPL(rio_get_asm);
1922EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
1923EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
1924EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
1925EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
1926EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
1927EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
1928EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
1929EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
Alexandre Bouninea11650e2013-05-24 15:55:05 -07001930EXPORT_SYMBOL_GPL(rio_init_mports);