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