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