rapidio: modify subsystem and driver initialization sequence
[pandora-kernel.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 Integrated Device Technology, Inc.
9  * Alex Bounine <alexandre.bounine@idt.com>
10  * - Added Port-Write/Error Management initialization and handling
11  *
12  * This program is free software; you can redistribute  it and/or modify it
13  * under  the terms of  the GNU General  Public License as published by the
14  * Free Software Foundation;  either version 2 of the  License, or (at your
15  * option) any later version.
16  */
17
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20
21 #include <linux/delay.h>
22 #include <linux/init.h>
23 #include <linux/rio.h>
24 #include <linux/rio_drv.h>
25 #include <linux/rio_ids.h>
26 #include <linux/rio_regs.h>
27 #include <linux/module.h>
28 #include <linux/spinlock.h>
29 #include <linux/slab.h>
30 #include <linux/interrupt.h>
31
32 #include "rio.h"
33
34 static LIST_HEAD(rio_mports);
35
36 /**
37  * rio_local_get_device_id - Get the base/extended device id for a port
38  * @port: RIO master port from which to get the deviceid
39  *
40  * Reads the base/extended device id from the local device
41  * implementing the master port. Returns the 8/16-bit device
42  * id.
43  */
44 u16 rio_local_get_device_id(struct rio_mport *port)
45 {
46         u32 result;
47
48         rio_local_read_config_32(port, RIO_DID_CSR, &result);
49
50         return (RIO_GET_DID(port->sys_size, result));
51 }
52
53 /**
54  * rio_request_inb_mbox - request inbound mailbox service
55  * @mport: RIO master port from which to allocate the mailbox resource
56  * @dev_id: Device specific pointer to pass on event
57  * @mbox: Mailbox number to claim
58  * @entries: Number of entries in inbound mailbox queue
59  * @minb: Callback to execute when inbound message is received
60  *
61  * Requests ownership of an inbound mailbox resource and binds
62  * a callback function to the resource. Returns %0 on success.
63  */
64 int rio_request_inb_mbox(struct rio_mport *mport,
65                          void *dev_id,
66                          int mbox,
67                          int entries,
68                          void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
69                                        int slot))
70 {
71         int rc = -ENOSYS;
72         struct resource *res;
73
74         if (mport->ops->open_inb_mbox == NULL)
75                 goto out;
76
77         res = kmalloc(sizeof(struct resource), GFP_KERNEL);
78
79         if (res) {
80                 rio_init_mbox_res(res, mbox, mbox);
81
82                 /* Make sure this mailbox isn't in use */
83                 if ((rc =
84                      request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
85                                       res)) < 0) {
86                         kfree(res);
87                         goto out;
88                 }
89
90                 mport->inb_msg[mbox].res = res;
91
92                 /* Hook the inbound message callback */
93                 mport->inb_msg[mbox].mcback = minb;
94
95                 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
96         } else
97                 rc = -ENOMEM;
98
99       out:
100         return rc;
101 }
102
103 /**
104  * rio_release_inb_mbox - release inbound mailbox message service
105  * @mport: RIO master port from which to release the mailbox resource
106  * @mbox: Mailbox number to release
107  *
108  * Releases ownership of an inbound mailbox resource. Returns 0
109  * if the request has been satisfied.
110  */
111 int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
112 {
113         if (mport->ops->close_inb_mbox) {
114                 mport->ops->close_inb_mbox(mport, mbox);
115
116                 /* Release the mailbox resource */
117                 return release_resource(mport->inb_msg[mbox].res);
118         } else
119                 return -ENOSYS;
120 }
121
122 /**
123  * rio_request_outb_mbox - request outbound mailbox service
124  * @mport: RIO master port from which to allocate the mailbox resource
125  * @dev_id: Device specific pointer to pass on event
126  * @mbox: Mailbox number to claim
127  * @entries: Number of entries in outbound mailbox queue
128  * @moutb: Callback to execute when outbound message is sent
129  *
130  * Requests ownership of an outbound mailbox resource and binds
131  * a callback function to the resource. Returns 0 on success.
132  */
133 int rio_request_outb_mbox(struct rio_mport *mport,
134                           void *dev_id,
135                           int mbox,
136                           int entries,
137                           void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
138 {
139         int rc = -ENOSYS;
140         struct resource *res;
141
142         if (mport->ops->open_outb_mbox == NULL)
143                 goto out;
144
145         res = kmalloc(sizeof(struct resource), GFP_KERNEL);
146
147         if (res) {
148                 rio_init_mbox_res(res, mbox, mbox);
149
150                 /* Make sure this outbound mailbox isn't in use */
151                 if ((rc =
152                      request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
153                                       res)) < 0) {
154                         kfree(res);
155                         goto out;
156                 }
157
158                 mport->outb_msg[mbox].res = res;
159
160                 /* Hook the inbound message callback */
161                 mport->outb_msg[mbox].mcback = moutb;
162
163                 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
164         } else
165                 rc = -ENOMEM;
166
167       out:
168         return rc;
169 }
170
171 /**
172  * rio_release_outb_mbox - release outbound mailbox message service
173  * @mport: RIO master port from which to release the mailbox resource
174  * @mbox: Mailbox number to release
175  *
176  * Releases ownership of an inbound mailbox resource. Returns 0
177  * if the request has been satisfied.
178  */
179 int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
180 {
181         if (mport->ops->close_outb_mbox) {
182                 mport->ops->close_outb_mbox(mport, mbox);
183
184                 /* Release the mailbox resource */
185                 return release_resource(mport->outb_msg[mbox].res);
186         } else
187                 return -ENOSYS;
188 }
189
190 /**
191  * rio_setup_inb_dbell - bind inbound doorbell callback
192  * @mport: RIO master port to bind the doorbell callback
193  * @dev_id: Device specific pointer to pass on event
194  * @res: Doorbell message resource
195  * @dinb: Callback to execute when doorbell is received
196  *
197  * Adds a doorbell resource/callback pair into a port's
198  * doorbell event list. Returns 0 if the request has been
199  * satisfied.
200  */
201 static int
202 rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
203                     void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
204                                   u16 info))
205 {
206         int rc = 0;
207         struct rio_dbell *dbell;
208
209         if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
210                 rc = -ENOMEM;
211                 goto out;
212         }
213
214         dbell->res = res;
215         dbell->dinb = dinb;
216         dbell->dev_id = dev_id;
217
218         list_add_tail(&dbell->node, &mport->dbells);
219
220       out:
221         return rc;
222 }
223
224 /**
225  * rio_request_inb_dbell - request inbound doorbell message service
226  * @mport: RIO master port from which to allocate the doorbell resource
227  * @dev_id: Device specific pointer to pass on event
228  * @start: Doorbell info range start
229  * @end: Doorbell info range end
230  * @dinb: Callback to execute when doorbell is received
231  *
232  * Requests ownership of an inbound doorbell resource and binds
233  * a callback function to the resource. Returns 0 if the request
234  * has been satisfied.
235  */
236 int rio_request_inb_dbell(struct rio_mport *mport,
237                           void *dev_id,
238                           u16 start,
239                           u16 end,
240                           void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
241                                         u16 dst, u16 info))
242 {
243         int rc = 0;
244
245         struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
246
247         if (res) {
248                 rio_init_dbell_res(res, start, end);
249
250                 /* Make sure these doorbells aren't in use */
251                 if ((rc =
252                      request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
253                                       res)) < 0) {
254                         kfree(res);
255                         goto out;
256                 }
257
258                 /* Hook the doorbell callback */
259                 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
260         } else
261                 rc = -ENOMEM;
262
263       out:
264         return rc;
265 }
266
267 /**
268  * rio_release_inb_dbell - release inbound doorbell message service
269  * @mport: RIO master port from which to release the doorbell resource
270  * @start: Doorbell info range start
271  * @end: Doorbell info range end
272  *
273  * Releases ownership of an inbound doorbell resource and removes
274  * callback from the doorbell event list. Returns 0 if the request
275  * has been satisfied.
276  */
277 int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
278 {
279         int rc = 0, found = 0;
280         struct rio_dbell *dbell;
281
282         list_for_each_entry(dbell, &mport->dbells, node) {
283                 if ((dbell->res->start == start) && (dbell->res->end == end)) {
284                         found = 1;
285                         break;
286                 }
287         }
288
289         /* If we can't find an exact match, fail */
290         if (!found) {
291                 rc = -EINVAL;
292                 goto out;
293         }
294
295         /* Delete from list */
296         list_del(&dbell->node);
297
298         /* Release the doorbell resource */
299         rc = release_resource(dbell->res);
300
301         /* Free the doorbell event */
302         kfree(dbell);
303
304       out:
305         return rc;
306 }
307
308 /**
309  * rio_request_outb_dbell - request outbound doorbell message range
310  * @rdev: RIO device from which to allocate the doorbell resource
311  * @start: Doorbell message range start
312  * @end: Doorbell message range end
313  *
314  * Requests ownership of a doorbell message range. Returns a resource
315  * if the request has been satisfied or %NULL on failure.
316  */
317 struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
318                                         u16 end)
319 {
320         struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
321
322         if (res) {
323                 rio_init_dbell_res(res, start, end);
324
325                 /* Make sure these doorbells aren't in use */
326                 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
327                     < 0) {
328                         kfree(res);
329                         res = NULL;
330                 }
331         }
332
333         return res;
334 }
335
336 /**
337  * rio_release_outb_dbell - release outbound doorbell message range
338  * @rdev: RIO device from which to release the doorbell resource
339  * @res: Doorbell resource to be freed
340  *
341  * Releases ownership of a doorbell message range. Returns 0 if the
342  * request has been satisfied.
343  */
344 int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
345 {
346         int rc = release_resource(res);
347
348         kfree(res);
349
350         return rc;
351 }
352
353 /**
354  * rio_request_inb_pwrite - request inbound port-write message service
355  * @rdev: RIO device to which register inbound port-write callback routine
356  * @pwcback: Callback routine to execute when port-write is received
357  *
358  * Binds a port-write callback function to the RapidIO device.
359  * Returns 0 if the request has been satisfied.
360  */
361 int rio_request_inb_pwrite(struct rio_dev *rdev,
362         int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
363 {
364         int rc = 0;
365
366         spin_lock(&rio_global_list_lock);
367         if (rdev->pwcback != NULL)
368                 rc = -ENOMEM;
369         else
370                 rdev->pwcback = pwcback;
371
372         spin_unlock(&rio_global_list_lock);
373         return rc;
374 }
375 EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
376
377 /**
378  * rio_release_inb_pwrite - release inbound port-write message service
379  * @rdev: RIO device which registered for inbound port-write callback
380  *
381  * Removes callback from the rio_dev structure. Returns 0 if the request
382  * has been satisfied.
383  */
384 int rio_release_inb_pwrite(struct rio_dev *rdev)
385 {
386         int rc = -ENOMEM;
387
388         spin_lock(&rio_global_list_lock);
389         if (rdev->pwcback) {
390                 rdev->pwcback = NULL;
391                 rc = 0;
392         }
393
394         spin_unlock(&rio_global_list_lock);
395         return rc;
396 }
397 EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
398
399 /**
400  * rio_mport_get_physefb - Helper function that returns register offset
401  *                      for Physical Layer Extended Features Block.
402  * @port: Master port to issue transaction
403  * @local: Indicate a local master port or remote device access
404  * @destid: Destination ID of the device
405  * @hopcount: Number of switch hops to the device
406  */
407 u32
408 rio_mport_get_physefb(struct rio_mport *port, int local,
409                       u16 destid, u8 hopcount)
410 {
411         u32 ext_ftr_ptr;
412         u32 ftr_header;
413
414         ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
415
416         while (ext_ftr_ptr)  {
417                 if (local)
418                         rio_local_read_config_32(port, ext_ftr_ptr,
419                                                  &ftr_header);
420                 else
421                         rio_mport_read_config_32(port, destid, hopcount,
422                                                  ext_ftr_ptr, &ftr_header);
423
424                 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
425                 switch (ftr_header) {
426
427                 case RIO_EFB_SER_EP_ID_V13P:
428                 case RIO_EFB_SER_EP_REC_ID_V13P:
429                 case RIO_EFB_SER_EP_FREE_ID_V13P:
430                 case RIO_EFB_SER_EP_ID:
431                 case RIO_EFB_SER_EP_REC_ID:
432                 case RIO_EFB_SER_EP_FREE_ID:
433                 case RIO_EFB_SER_EP_FREC_ID:
434
435                         return ext_ftr_ptr;
436
437                 default:
438                         break;
439                 }
440
441                 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
442                                                 hopcount, ext_ftr_ptr);
443         }
444
445         return ext_ftr_ptr;
446 }
447
448 /**
449  * rio_get_comptag - Begin or continue searching for a RIO device by component tag
450  * @comp_tag: RIO component tag to match
451  * @from: Previous RIO device found in search, or %NULL for new search
452  *
453  * Iterates through the list of known RIO devices. If a RIO device is
454  * found with a matching @comp_tag, a pointer to its device
455  * structure is returned. Otherwise, %NULL is returned. A new search
456  * is initiated by passing %NULL to the @from argument. Otherwise, if
457  * @from is not %NULL, searches continue from next device on the global
458  * list.
459  */
460 struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
461 {
462         struct list_head *n;
463         struct rio_dev *rdev;
464
465         spin_lock(&rio_global_list_lock);
466         n = from ? from->global_list.next : rio_devices.next;
467
468         while (n && (n != &rio_devices)) {
469                 rdev = rio_dev_g(n);
470                 if (rdev->comp_tag == comp_tag)
471                         goto exit;
472                 n = n->next;
473         }
474         rdev = NULL;
475 exit:
476         spin_unlock(&rio_global_list_lock);
477         return rdev;
478 }
479
480 /**
481  * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
482  * @rdev: Pointer to RIO device control structure
483  * @pnum: Switch port number to set LOCKOUT bit
484  * @lock: Operation : set (=1) or clear (=0)
485  */
486 int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
487 {
488         u32 regval;
489
490         rio_read_config_32(rdev,
491                                  rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
492                                  &regval);
493         if (lock)
494                 regval |= RIO_PORT_N_CTL_LOCKOUT;
495         else
496                 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
497
498         rio_write_config_32(rdev,
499                                   rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
500                                   regval);
501         return 0;
502 }
503
504 /**
505  * rio_chk_dev_route - Validate route to the specified device.
506  * @rdev:  RIO device failed to respond
507  * @nrdev: Last active device on the route to rdev
508  * @npnum: nrdev's port number on the route to rdev
509  *
510  * Follows a route to the specified RIO device to determine the last available
511  * device (and corresponding RIO port) on the route.
512  */
513 static int
514 rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
515 {
516         u32 result;
517         int p_port, rc = -EIO;
518         struct rio_dev *prev = NULL;
519
520         /* Find switch with failed RIO link */
521         while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
522                 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
523                         prev = rdev->prev;
524                         break;
525                 }
526                 rdev = rdev->prev;
527         }
528
529         if (prev == NULL)
530                 goto err_out;
531
532         p_port = prev->rswitch->route_table[rdev->destid];
533
534         if (p_port != RIO_INVALID_ROUTE) {
535                 pr_debug("RIO: link failed on [%s]-P%d\n",
536                          rio_name(prev), p_port);
537                 *nrdev = prev;
538                 *npnum = p_port;
539                 rc = 0;
540         } else
541                 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
542 err_out:
543         return rc;
544 }
545
546 /**
547  * rio_mport_chk_dev_access - Validate access to the specified device.
548  * @mport: Master port to send transactions
549  * @destid: Device destination ID in network
550  * @hopcount: Number of hops into the network
551  */
552 int
553 rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
554 {
555         int i = 0;
556         u32 tmp;
557
558         while (rio_mport_read_config_32(mport, destid, hopcount,
559                                         RIO_DEV_ID_CAR, &tmp)) {
560                 i++;
561                 if (i == RIO_MAX_CHK_RETRY)
562                         return -EIO;
563                 mdelay(1);
564         }
565
566         return 0;
567 }
568
569 /**
570  * rio_chk_dev_access - Validate access to the specified device.
571  * @rdev: Pointer to RIO device control structure
572  */
573 static int rio_chk_dev_access(struct rio_dev *rdev)
574 {
575         return rio_mport_chk_dev_access(rdev->net->hport,
576                                         rdev->destid, rdev->hopcount);
577 }
578
579 /**
580  * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
581  *                        returns link-response (if requested).
582  * @rdev: RIO devive to issue Input-status command
583  * @pnum: Device port number to issue the command
584  * @lnkresp: Response from a link partner
585  */
586 static int
587 rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
588 {
589         u32 regval;
590         int checkcount;
591
592         if (lnkresp) {
593                 /* Read from link maintenance response register
594                  * to clear valid bit */
595                 rio_read_config_32(rdev,
596                         rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
597                         &regval);
598                 udelay(50);
599         }
600
601         /* Issue Input-status command */
602         rio_write_config_32(rdev,
603                 rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
604                 RIO_MNT_REQ_CMD_IS);
605
606         /* Exit if the response is not expected */
607         if (lnkresp == NULL)
608                 return 0;
609
610         checkcount = 3;
611         while (checkcount--) {
612                 udelay(50);
613                 rio_read_config_32(rdev,
614                         rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
615                         &regval);
616                 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
617                         *lnkresp = regval;
618                         return 0;
619                 }
620         }
621
622         return -EIO;
623 }
624
625 /**
626  * rio_clr_err_stopped - Clears port Error-stopped states.
627  * @rdev: Pointer to RIO device control structure
628  * @pnum: Switch port number to clear errors
629  * @err_status: port error status (if 0 reads register from device)
630  */
631 static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
632 {
633         struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
634         u32 regval;
635         u32 far_ackid, far_linkstat, near_ackid;
636
637         if (err_status == 0)
638                 rio_read_config_32(rdev,
639                         rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
640                         &err_status);
641
642         if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
643                 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
644                 /*
645                  * Send a Link-Request/Input-Status control symbol
646                  */
647                 if (rio_get_input_status(rdev, pnum, &regval)) {
648                         pr_debug("RIO_EM: Input-status response timeout\n");
649                         goto rd_err;
650                 }
651
652                 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
653                          pnum, regval);
654                 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
655                 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
656                 rio_read_config_32(rdev,
657                         rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
658                         &regval);
659                 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
660                 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
661                 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
662                          " near_ackID=0x%02x\n",
663                         pnum, far_ackid, far_linkstat, near_ackid);
664
665                 /*
666                  * If required, synchronize ackIDs of near and
667                  * far sides.
668                  */
669                 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
670                     (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
671                         /* Align near outstanding/outbound ackIDs with
672                          * far inbound.
673                          */
674                         rio_write_config_32(rdev,
675                                 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
676                                 (near_ackid << 24) |
677                                         (far_ackid << 8) | far_ackid);
678                         /* Align far outstanding/outbound ackIDs with
679                          * near inbound.
680                          */
681                         far_ackid++;
682                         if (nextdev)
683                                 rio_write_config_32(nextdev,
684                                         nextdev->phys_efptr +
685                                         RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
686                                         (far_ackid << 24) |
687                                         (near_ackid << 8) | near_ackid);
688                         else
689                                 pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
690                 }
691 rd_err:
692                 rio_read_config_32(rdev,
693                         rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
694                         &err_status);
695                 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
696         }
697
698         if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
699                 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
700                 rio_get_input_status(nextdev,
701                                      RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
702                 udelay(50);
703
704                 rio_read_config_32(rdev,
705                         rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
706                         &err_status);
707                 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
708         }
709
710         return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
711                               RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
712 }
713
714 /**
715  * rio_inb_pwrite_handler - process inbound port-write message
716  * @pw_msg: pointer to inbound port-write message
717  *
718  * Processes an inbound port-write message. Returns 0 if the request
719  * has been satisfied.
720  */
721 int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
722 {
723         struct rio_dev *rdev;
724         u32 err_status, em_perrdet, em_ltlerrdet;
725         int rc, portnum;
726
727         rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
728         if (rdev == NULL) {
729                 /* Device removed or enumeration error */
730                 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
731                         __func__, pw_msg->em.comptag);
732                 return -EIO;
733         }
734
735         pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
736
737 #ifdef DEBUG_PW
738         {
739         u32 i;
740         for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
741                         pr_debug("0x%02x: %08x %08x %08x %08x\n",
742                                  i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
743                                  pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
744                         i += 4;
745         }
746         }
747 #endif
748
749         /* Call an external service function (if such is registered
750          * for this device). This may be the service for endpoints that send
751          * device-specific port-write messages. End-point messages expected
752          * to be handled completely by EP specific device driver.
753          * For switches rc==0 signals that no standard processing required.
754          */
755         if (rdev->pwcback != NULL) {
756                 rc = rdev->pwcback(rdev, pw_msg, 0);
757                 if (rc == 0)
758                         return 0;
759         }
760
761         portnum = pw_msg->em.is_port & 0xFF;
762
763         /* Check if device and route to it are functional:
764          * Sometimes devices may send PW message(s) just before being
765          * powered down (or link being lost).
766          */
767         if (rio_chk_dev_access(rdev)) {
768                 pr_debug("RIO: device access failed - get link partner\n");
769                 /* Scan route to the device and identify failed link.
770                  * This will replace device and port reported in PW message.
771                  * PW message should not be used after this point.
772                  */
773                 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
774                         pr_err("RIO: Route trace for %s failed\n",
775                                 rio_name(rdev));
776                         return -EIO;
777                 }
778                 pw_msg = NULL;
779         }
780
781         /* For End-point devices processing stops here */
782         if (!(rdev->pef & RIO_PEF_SWITCH))
783                 return 0;
784
785         if (rdev->phys_efptr == 0) {
786                 pr_err("RIO_PW: Bad switch initialization for %s\n",
787                         rio_name(rdev));
788                 return 0;
789         }
790
791         /*
792          * Process the port-write notification from switch
793          */
794         if (rdev->rswitch->em_handle)
795                 rdev->rswitch->em_handle(rdev, portnum);
796
797         rio_read_config_32(rdev,
798                         rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
799                         &err_status);
800         pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
801
802         if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
803
804                 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
805                         rdev->rswitch->port_ok |= (1 << portnum);
806                         rio_set_port_lockout(rdev, portnum, 0);
807                         /* Schedule Insertion Service */
808                         pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
809                                rio_name(rdev), portnum);
810                 }
811
812                 /* Clear error-stopped states (if reported).
813                  * Depending on the link partner state, two attempts
814                  * may be needed for successful recovery.
815                  */
816                 if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
817                                   RIO_PORT_N_ERR_STS_PW_INP_ES)) {
818                         if (rio_clr_err_stopped(rdev, portnum, err_status))
819                                 rio_clr_err_stopped(rdev, portnum, 0);
820                 }
821         }  else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
822
823                 if (rdev->rswitch->port_ok & (1 << portnum)) {
824                         rdev->rswitch->port_ok &= ~(1 << portnum);
825                         rio_set_port_lockout(rdev, portnum, 1);
826
827                         rio_write_config_32(rdev,
828                                 rdev->phys_efptr +
829                                         RIO_PORT_N_ACK_STS_CSR(portnum),
830                                 RIO_PORT_N_ACK_CLEAR);
831
832                         /* Schedule Extraction Service */
833                         pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
834                                rio_name(rdev), portnum);
835                 }
836         }
837
838         rio_read_config_32(rdev,
839                 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
840         if (em_perrdet) {
841                 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
842                          portnum, em_perrdet);
843                 /* Clear EM Port N Error Detect CSR */
844                 rio_write_config_32(rdev,
845                         rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
846         }
847
848         rio_read_config_32(rdev,
849                 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
850         if (em_ltlerrdet) {
851                 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
852                          em_ltlerrdet);
853                 /* Clear EM L/T Layer Error Detect CSR */
854                 rio_write_config_32(rdev,
855                         rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
856         }
857
858         /* Clear remaining error bits and Port-Write Pending bit */
859         rio_write_config_32(rdev,
860                         rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
861                         err_status);
862
863         return 0;
864 }
865 EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
866
867 /**
868  * rio_mport_get_efb - get pointer to next extended features block
869  * @port: Master port to issue transaction
870  * @local: Indicate a local master port or remote device access
871  * @destid: Destination ID of the device
872  * @hopcount: Number of switch hops to the device
873  * @from: Offset of  current Extended Feature block header (if 0 starts
874  * from ExtFeaturePtr)
875  */
876 u32
877 rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
878                       u8 hopcount, u32 from)
879 {
880         u32 reg_val;
881
882         if (from == 0) {
883                 if (local)
884                         rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
885                                                  &reg_val);
886                 else
887                         rio_mport_read_config_32(port, destid, hopcount,
888                                                  RIO_ASM_INFO_CAR, &reg_val);
889                 return reg_val & RIO_EXT_FTR_PTR_MASK;
890         } else {
891                 if (local)
892                         rio_local_read_config_32(port, from, &reg_val);
893                 else
894                         rio_mport_read_config_32(port, destid, hopcount,
895                                                  from, &reg_val);
896                 return RIO_GET_BLOCK_ID(reg_val);
897         }
898 }
899
900 /**
901  * rio_mport_get_feature - query for devices' extended features
902  * @port: Master port to issue transaction
903  * @local: Indicate a local master port or remote device access
904  * @destid: Destination ID of the device
905  * @hopcount: Number of switch hops to the device
906  * @ftr: Extended feature code
907  *
908  * Tell if a device supports a given RapidIO capability.
909  * Returns the offset of the requested extended feature
910  * block within the device's RIO configuration space or
911  * 0 in case the device does not support it.  Possible
912  * values for @ftr:
913  *
914  * %RIO_EFB_PAR_EP_ID           LP/LVDS EP Devices
915  *
916  * %RIO_EFB_PAR_EP_REC_ID       LP/LVDS EP Recovery Devices
917  *
918  * %RIO_EFB_PAR_EP_FREE_ID      LP/LVDS EP Free Devices
919  *
920  * %RIO_EFB_SER_EP_ID           LP/Serial EP Devices
921  *
922  * %RIO_EFB_SER_EP_REC_ID       LP/Serial EP Recovery Devices
923  *
924  * %RIO_EFB_SER_EP_FREE_ID      LP/Serial EP Free Devices
925  */
926 u32
927 rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
928                       u8 hopcount, int ftr)
929 {
930         u32 asm_info, ext_ftr_ptr, ftr_header;
931
932         if (local)
933                 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
934         else
935                 rio_mport_read_config_32(port, destid, hopcount,
936                                          RIO_ASM_INFO_CAR, &asm_info);
937
938         ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
939
940         while (ext_ftr_ptr) {
941                 if (local)
942                         rio_local_read_config_32(port, ext_ftr_ptr,
943                                                  &ftr_header);
944                 else
945                         rio_mport_read_config_32(port, destid, hopcount,
946                                                  ext_ftr_ptr, &ftr_header);
947                 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
948                         return ext_ftr_ptr;
949                 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
950                         break;
951         }
952
953         return 0;
954 }
955
956 /**
957  * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
958  * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
959  * @did: RIO did to match or %RIO_ANY_ID to match all dids
960  * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
961  * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
962  * @from: Previous RIO device found in search, or %NULL for new search
963  *
964  * Iterates through the list of known RIO devices. If a RIO device is
965  * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
966  * count to the device is incrememted and a pointer to its device
967  * structure is returned. Otherwise, %NULL is returned. A new search
968  * is initiated by passing %NULL to the @from argument. Otherwise, if
969  * @from is not %NULL, searches continue from next device on the global
970  * list. The reference count for @from is always decremented if it is
971  * not %NULL.
972  */
973 struct rio_dev *rio_get_asm(u16 vid, u16 did,
974                             u16 asm_vid, u16 asm_did, struct rio_dev *from)
975 {
976         struct list_head *n;
977         struct rio_dev *rdev;
978
979         WARN_ON(in_interrupt());
980         spin_lock(&rio_global_list_lock);
981         n = from ? from->global_list.next : rio_devices.next;
982
983         while (n && (n != &rio_devices)) {
984                 rdev = rio_dev_g(n);
985                 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
986                     (did == RIO_ANY_ID || rdev->did == did) &&
987                     (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
988                     (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
989                         goto exit;
990                 n = n->next;
991         }
992         rdev = NULL;
993       exit:
994         rio_dev_put(from);
995         rdev = rio_dev_get(rdev);
996         spin_unlock(&rio_global_list_lock);
997         return rdev;
998 }
999
1000 /**
1001  * rio_get_device - Begin or continue searching for a RIO device by vid/did
1002  * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1003  * @did: RIO did to match or %RIO_ANY_ID to match all dids
1004  * @from: Previous RIO device found in search, or %NULL for new search
1005  *
1006  * Iterates through the list of known RIO devices. If a RIO device is
1007  * found with a matching @vid and @did, the reference count to the
1008  * device is incrememted and a pointer to its device structure is returned.
1009  * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
1010  * to the @from argument. Otherwise, if @from is not %NULL, searches
1011  * continue from next device on the global list. The reference count for
1012  * @from is always decremented if it is not %NULL.
1013  */
1014 struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1015 {
1016         return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1017 }
1018
1019 /**
1020  * rio_std_route_add_entry - Add switch route table entry using standard
1021  *   registers defined in RIO specification rev.1.3
1022  * @mport: Master port to issue transaction
1023  * @destid: Destination ID of the device
1024  * @hopcount: Number of switch hops to the device
1025  * @table: routing table ID (global or port-specific)
1026  * @route_destid: destID entry in the RT
1027  * @route_port: destination port for specified destID
1028  */
1029 int rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1030                        u16 table, u16 route_destid, u8 route_port)
1031 {
1032         if (table == RIO_GLOBAL_TABLE) {
1033                 rio_mport_write_config_32(mport, destid, hopcount,
1034                                 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1035                                 (u32)route_destid);
1036                 rio_mport_write_config_32(mport, destid, hopcount,
1037                                 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1038                                 (u32)route_port);
1039         }
1040
1041         udelay(10);
1042         return 0;
1043 }
1044
1045 /**
1046  * rio_std_route_get_entry - Read switch route table entry (port number)
1047  *   associated with specified destID using standard registers defined in RIO
1048  *   specification rev.1.3
1049  * @mport: Master port to issue transaction
1050  * @destid: Destination ID of the device
1051  * @hopcount: Number of switch hops to the device
1052  * @table: routing table ID (global or port-specific)
1053  * @route_destid: destID entry in the RT
1054  * @route_port: returned destination port for specified destID
1055  */
1056 int rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1057                        u16 table, u16 route_destid, u8 *route_port)
1058 {
1059         u32 result;
1060
1061         if (table == RIO_GLOBAL_TABLE) {
1062                 rio_mport_write_config_32(mport, destid, hopcount,
1063                                 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1064                 rio_mport_read_config_32(mport, destid, hopcount,
1065                                 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1066
1067                 *route_port = (u8)result;
1068         }
1069
1070         return 0;
1071 }
1072
1073 /**
1074  * rio_std_route_clr_table - Clear swotch route table using standard registers
1075  *   defined in RIO specification rev.1.3.
1076  * @mport: Master port to issue transaction
1077  * @destid: Destination ID of the device
1078  * @hopcount: Number of switch hops to the device
1079  * @table: routing table ID (global or port-specific)
1080  */
1081 int rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1082                        u16 table)
1083 {
1084         u32 max_destid = 0xff;
1085         u32 i, pef, id_inc = 1, ext_cfg = 0;
1086         u32 port_sel = RIO_INVALID_ROUTE;
1087
1088         if (table == RIO_GLOBAL_TABLE) {
1089                 rio_mport_read_config_32(mport, destid, hopcount,
1090                                          RIO_PEF_CAR, &pef);
1091
1092                 if (mport->sys_size) {
1093                         rio_mport_read_config_32(mport, destid, hopcount,
1094                                                  RIO_SWITCH_RT_LIMIT,
1095                                                  &max_destid);
1096                         max_destid &= RIO_RT_MAX_DESTID;
1097                 }
1098
1099                 if (pef & RIO_PEF_EXT_RT) {
1100                         ext_cfg = 0x80000000;
1101                         id_inc = 4;
1102                         port_sel = (RIO_INVALID_ROUTE << 24) |
1103                                    (RIO_INVALID_ROUTE << 16) |
1104                                    (RIO_INVALID_ROUTE << 8) |
1105                                    RIO_INVALID_ROUTE;
1106                 }
1107
1108                 for (i = 0; i <= max_destid;) {
1109                         rio_mport_write_config_32(mport, destid, hopcount,
1110                                         RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1111                                         ext_cfg | i);
1112                         rio_mport_write_config_32(mport, destid, hopcount,
1113                                         RIO_STD_RTE_CONF_PORT_SEL_CSR,
1114                                         port_sel);
1115                         i += id_inc;
1116                 }
1117         }
1118
1119         udelay(10);
1120         return 0;
1121 }
1122
1123 static void rio_fixup_device(struct rio_dev *dev)
1124 {
1125 }
1126
1127 static int __devinit rio_init(void)
1128 {
1129         struct rio_dev *dev = NULL;
1130
1131         while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
1132                 rio_fixup_device(dev);
1133         }
1134         return 0;
1135 }
1136
1137 int __devinit rio_init_mports(void)
1138 {
1139         int rc = 0;
1140         struct rio_mport *port;
1141
1142         list_for_each_entry(port, &rio_mports, node) {
1143                 if (!request_mem_region(port->iores.start,
1144                                         resource_size(&port->iores),
1145                                         port->name)) {
1146                         printk(KERN_ERR
1147                                "RIO: Error requesting master port region 0x%016llx-0x%016llx\n",
1148                                (u64)port->iores.start, (u64)port->iores.end);
1149                         rc = -ENOMEM;
1150                         goto out;
1151                 }
1152
1153                 if (port->host_deviceid >= 0)
1154                         rio_enum_mport(port);
1155                 else
1156                         rio_disc_mport(port);
1157         }
1158
1159         rio_init();
1160
1161       out:
1162         return rc;
1163 }
1164
1165 device_initcall_sync(rio_init_mports);
1166
1167 void rio_register_mport(struct rio_mport *port)
1168 {
1169         list_add_tail(&port->node, &rio_mports);
1170 }
1171
1172 EXPORT_SYMBOL_GPL(rio_local_get_device_id);
1173 EXPORT_SYMBOL_GPL(rio_get_device);
1174 EXPORT_SYMBOL_GPL(rio_get_asm);
1175 EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
1176 EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
1177 EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
1178 EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
1179 EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
1180 EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
1181 EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
1182 EXPORT_SYMBOL_GPL(rio_release_outb_mbox);