Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rio.c
Go to the documentation of this file.
1 /*
2  * RapidIO interconnect services
3  * (RapidIO Interconnect Specification, http://www.rapidio.org)
4  *
5  * Copyright 2005 MontaVista Software, Inc.
6  * Matt Porter <[email protected]>
7  *
8  * Copyright 2009 Integrated Device Technology, Inc.
9  * Alex Bounine <[email protected]>
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 static unsigned char next_portid;
36 static DEFINE_SPINLOCK(rio_mmap_lock);
37 
47 {
48  u32 result;
49 
50  rio_local_read_config_32(port, RIO_DID_CSR, &result);
51 
52  return (RIO_GET_DID(port->sys_size, result));
53 }
54 
66 int rio_request_inb_mbox(struct rio_mport *mport,
67  void *dev_id,
68  int mbox,
69  int entries,
70  void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
71  int slot))
72 {
73  int rc = -ENOSYS;
74  struct resource *res;
75 
76  if (mport->ops->open_inb_mbox == NULL)
77  goto out;
78 
79  res = kmalloc(sizeof(struct resource), GFP_KERNEL);
80 
81  if (res) {
82  rio_init_mbox_res(res, mbox, mbox);
83 
84  /* Make sure this mailbox isn't in use */
85  if ((rc =
87  res)) < 0) {
88  kfree(res);
89  goto out;
90  }
91 
92  mport->inb_msg[mbox].res = res;
93 
94  /* Hook the inbound message callback */
95  mport->inb_msg[mbox].mcback = minb;
96 
97  rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
98  } else
99  rc = -ENOMEM;
100 
101  out:
102  return rc;
103 }
104 
113 int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
114 {
115  if (mport->ops->close_inb_mbox) {
116  mport->ops->close_inb_mbox(mport, mbox);
117 
118  /* Release the mailbox resource */
119  return release_resource(mport->inb_msg[mbox].res);
120  } else
121  return -ENOSYS;
122 }
123 
136  void *dev_id,
137  int mbox,
138  int entries,
139  void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
140 {
141  int rc = -ENOSYS;
142  struct resource *res;
143 
144  if (mport->ops->open_outb_mbox == NULL)
145  goto out;
146 
147  res = kmalloc(sizeof(struct resource), GFP_KERNEL);
148 
149  if (res) {
150  rio_init_mbox_res(res, mbox, mbox);
151 
152  /* Make sure this outbound mailbox isn't in use */
153  if ((rc =
155  res)) < 0) {
156  kfree(res);
157  goto out;
158  }
159 
160  mport->outb_msg[mbox].res = res;
161 
162  /* Hook the inbound message callback */
163  mport->outb_msg[mbox].mcback = moutb;
164 
165  rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
166  } else
167  rc = -ENOMEM;
168 
169  out:
170  return rc;
171 }
172 
181 int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
182 {
183  if (mport->ops->close_outb_mbox) {
184  mport->ops->close_outb_mbox(mport, mbox);
185 
186  /* Release the mailbox resource */
187  return release_resource(mport->outb_msg[mbox].res);
188  } else
189  return -ENOSYS;
190 }
191 
203 static int
204 rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
205  void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
206  u16 info))
207 {
208  int rc = 0;
209  struct rio_dbell *dbell;
210 
211  if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
212  rc = -ENOMEM;
213  goto out;
214  }
215 
216  dbell->res = res;
217  dbell->dinb = dinb;
218  dbell->dev_id = dev_id;
219 
220  list_add_tail(&dbell->node, &mport->dbells);
221 
222  out:
223  return rc;
224 }
225 
239  void *dev_id,
240  u16 start,
241  u16 end,
242  void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
243  u16 dst, u16 info))
244 {
245  int rc = 0;
246 
247  struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
248 
249  if (res) {
250  rio_init_dbell_res(res, start, end);
251 
252  /* Make sure these doorbells aren't in use */
253  if ((rc =
255  res)) < 0) {
256  kfree(res);
257  goto out;
258  }
259 
260  /* Hook the doorbell callback */
261  rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
262  } else
263  rc = -ENOMEM;
264 
265  out:
266  return rc;
267 }
268 
279 int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
280 {
281  int rc = 0, found = 0;
282  struct rio_dbell *dbell;
283 
284  list_for_each_entry(dbell, &mport->dbells, node) {
285  if ((dbell->res->start == start) && (dbell->res->end == end)) {
286  found = 1;
287  break;
288  }
289  }
290 
291  /* If we can't find an exact match, fail */
292  if (!found) {
293  rc = -EINVAL;
294  goto out;
295  }
296 
297  /* Delete from list */
298  list_del(&dbell->node);
299 
300  /* Release the doorbell resource */
301  rc = release_resource(dbell->res);
302 
303  /* Free the doorbell event */
304  kfree(dbell);
305 
306  out:
307  return rc;
308 }
309 
320  u16 end)
321 {
322  struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
323 
324  if (res) {
325  rio_init_dbell_res(res, start, end);
326 
327  /* Make sure these doorbells aren't in use */
329  < 0) {
330  kfree(res);
331  res = NULL;
332  }
333  }
334 
335  return res;
336 }
337 
346 int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
347 {
348  int rc = release_resource(res);
349 
350  kfree(res);
351 
352  return rc;
353 }
354 
364  int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
365 {
366  int rc = 0;
367 
368  spin_lock(&rio_global_list_lock);
369  if (rdev->pwcback != NULL)
370  rc = -ENOMEM;
371  else
372  rdev->pwcback = pwcback;
373 
374  spin_unlock(&rio_global_list_lock);
375  return rc;
376 }
378 
387 {
388  int rc = -ENOMEM;
389 
390  spin_lock(&rio_global_list_lock);
391  if (rdev->pwcback) {
392  rdev->pwcback = NULL;
393  rc = 0;
394  }
395 
396  spin_unlock(&rio_global_list_lock);
397  return rc;
398 }
400 
413 int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
414  u64 rbase, u32 size, u32 rflags)
415 {
416  int rc = 0;
417  unsigned long flags;
418 
419  if (!mport->ops->map_inb)
420  return -1;
421  spin_lock_irqsave(&rio_mmap_lock, flags);
422  rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
423  spin_unlock_irqrestore(&rio_mmap_lock, flags);
424  return rc;
425 }
427 
433 void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
434 {
435  unsigned long flags;
436  if (!mport->ops->unmap_inb)
437  return;
438  spin_lock_irqsave(&rio_mmap_lock, flags);
439  mport->ops->unmap_inb(mport, lstart);
440  spin_unlock_irqrestore(&rio_mmap_lock, flags);
441 }
443 
452 u32
454  u16 destid, u8 hopcount)
455 {
456  u32 ext_ftr_ptr;
457  u32 ftr_header;
458 
459  ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
460 
461  while (ext_ftr_ptr) {
462  if (local)
463  rio_local_read_config_32(port, ext_ftr_ptr,
464  &ftr_header);
465  else
466  rio_mport_read_config_32(port, destid, hopcount,
467  ext_ftr_ptr, &ftr_header);
468 
469  ftr_header = RIO_GET_BLOCK_ID(ftr_header);
470  switch (ftr_header) {
471 
475  case RIO_EFB_SER_EP_ID:
479 
480  return ext_ftr_ptr;
481 
482  default:
483  break;
484  }
485 
486  ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
487  hopcount, ext_ftr_ptr);
488  }
489 
490  return ext_ftr_ptr;
491 }
492 
506 {
507  struct list_head *n;
508  struct rio_dev *rdev;
509 
510  spin_lock(&rio_global_list_lock);
511  n = from ? from->global_list.next : rio_devices.next;
512 
513  while (n && (n != &rio_devices)) {
514  rdev = rio_dev_g(n);
515  if (rdev->comp_tag == comp_tag)
516  goto exit;
517  n = n->next;
518  }
519  rdev = NULL;
520 exit:
521  spin_unlock(&rio_global_list_lock);
522  return rdev;
523 }
524 
531 int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
532 {
533  u32 regval;
534 
535  rio_read_config_32(rdev,
536  rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
537  &regval);
538  if (lock)
539  regval |= RIO_PORT_N_CTL_LOCKOUT;
540  else
541  regval &= ~RIO_PORT_N_CTL_LOCKOUT;
542 
543  rio_write_config_32(rdev,
544  rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
545  regval);
546  return 0;
547 }
548 
558 static int
559 rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
560 {
561  u32 result;
562  int p_port, rc = -EIO;
563  struct rio_dev *prev = NULL;
564 
565  /* Find switch with failed RIO link */
566  while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
567  if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
568  prev = rdev->prev;
569  break;
570  }
571  rdev = rdev->prev;
572  }
573 
574  if (prev == NULL)
575  goto err_out;
576 
577  p_port = prev->rswitch->route_table[rdev->destid];
578 
579  if (p_port != RIO_INVALID_ROUTE) {
580  pr_debug("RIO: link failed on [%s]-P%d\n",
581  rio_name(prev), p_port);
582  *nrdev = prev;
583  *npnum = p_port;
584  rc = 0;
585  } else
586  pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
587 err_out:
588  return rc;
589 }
590 
597 int
599 {
600  int i = 0;
601  u32 tmp;
602 
603  while (rio_mport_read_config_32(mport, destid, hopcount,
604  RIO_DEV_ID_CAR, &tmp)) {
605  i++;
606  if (i == RIO_MAX_CHK_RETRY)
607  return -EIO;
608  mdelay(1);
609  }
610 
611  return 0;
612 }
613 
618 static int rio_chk_dev_access(struct rio_dev *rdev)
619 {
620  return rio_mport_chk_dev_access(rdev->net->hport,
621  rdev->destid, rdev->hopcount);
622 }
623 
631 static int
632 rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
633 {
634  u32 regval;
635  int checkcount;
636 
637  if (lnkresp) {
638  /* Read from link maintenance response register
639  * to clear valid bit */
640  rio_read_config_32(rdev,
641  rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
642  &regval);
643  udelay(50);
644  }
645 
646  /* Issue Input-status command */
647  rio_write_config_32(rdev,
648  rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
650 
651  /* Exit if the response is not expected */
652  if (lnkresp == NULL)
653  return 0;
654 
655  checkcount = 3;
656  while (checkcount--) {
657  udelay(50);
658  rio_read_config_32(rdev,
659  rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
660  &regval);
661  if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
662  *lnkresp = regval;
663  return 0;
664  }
665  }
666 
667  return -EIO;
668 }
669 
676 static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
677 {
678  struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
679  u32 regval;
680  u32 far_ackid, far_linkstat, near_ackid;
681 
682  if (err_status == 0)
683  rio_read_config_32(rdev,
684  rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
685  &err_status);
686 
687  if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
688  pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
689  /*
690  * Send a Link-Request/Input-Status control symbol
691  */
692  if (rio_get_input_status(rdev, pnum, &regval)) {
693  pr_debug("RIO_EM: Input-status response timeout\n");
694  goto rd_err;
695  }
696 
697  pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
698  pnum, regval);
699  far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
700  far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
701  rio_read_config_32(rdev,
702  rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
703  &regval);
704  pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
705  near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
706  pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
707  " near_ackID=0x%02x\n",
708  pnum, far_ackid, far_linkstat, near_ackid);
709 
710  /*
711  * If required, synchronize ackIDs of near and
712  * far sides.
713  */
714  if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
715  (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
716  /* Align near outstanding/outbound ackIDs with
717  * far inbound.
718  */
719  rio_write_config_32(rdev,
720  rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
721  (near_ackid << 24) |
722  (far_ackid << 8) | far_ackid);
723  /* Align far outstanding/outbound ackIDs with
724  * near inbound.
725  */
726  far_ackid++;
727  if (nextdev)
728  rio_write_config_32(nextdev,
729  nextdev->phys_efptr +
731  (far_ackid << 24) |
732  (near_ackid << 8) | near_ackid);
733  else
734  pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
735  }
736 rd_err:
737  rio_read_config_32(rdev,
738  rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
739  &err_status);
740  pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
741  }
742 
743  if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
744  pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
745  rio_get_input_status(nextdev,
746  RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
747  udelay(50);
748 
749  rio_read_config_32(rdev,
750  rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
751  &err_status);
752  pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
753  }
754 
755  return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
756  RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
757 }
758 
767 {
768  struct rio_dev *rdev;
769  u32 err_status, em_perrdet, em_ltlerrdet;
770  int rc, portnum;
771 
772  rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
773  if (rdev == NULL) {
774  /* Device removed or enumeration error */
775  pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
776  __func__, pw_msg->em.comptag);
777  return -EIO;
778  }
779 
780  pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
781 
782 #ifdef DEBUG_PW
783  {
784  u32 i;
785  for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
786  pr_debug("0x%02x: %08x %08x %08x %08x\n",
787  i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
788  pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
789  i += 4;
790  }
791  }
792 #endif
793 
794  /* Call an external service function (if such is registered
795  * for this device). This may be the service for endpoints that send
796  * device-specific port-write messages. End-point messages expected
797  * to be handled completely by EP specific device driver.
798  * For switches rc==0 signals that no standard processing required.
799  */
800  if (rdev->pwcback != NULL) {
801  rc = rdev->pwcback(rdev, pw_msg, 0);
802  if (rc == 0)
803  return 0;
804  }
805 
806  portnum = pw_msg->em.is_port & 0xFF;
807 
808  /* Check if device and route to it are functional:
809  * Sometimes devices may send PW message(s) just before being
810  * powered down (or link being lost).
811  */
812  if (rio_chk_dev_access(rdev)) {
813  pr_debug("RIO: device access failed - get link partner\n");
814  /* Scan route to the device and identify failed link.
815  * This will replace device and port reported in PW message.
816  * PW message should not be used after this point.
817  */
818  if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
819  pr_err("RIO: Route trace for %s failed\n",
820  rio_name(rdev));
821  return -EIO;
822  }
823  pw_msg = NULL;
824  }
825 
826  /* For End-point devices processing stops here */
827  if (!(rdev->pef & RIO_PEF_SWITCH))
828  return 0;
829 
830  if (rdev->phys_efptr == 0) {
831  pr_err("RIO_PW: Bad switch initialization for %s\n",
832  rio_name(rdev));
833  return 0;
834  }
835 
836  /*
837  * Process the port-write notification from switch
838  */
839  if (rdev->rswitch->em_handle)
840  rdev->rswitch->em_handle(rdev, portnum);
841 
842  rio_read_config_32(rdev,
843  rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
844  &err_status);
845  pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
846 
847  if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
848 
849  if (!(rdev->rswitch->port_ok & (1 << portnum))) {
850  rdev->rswitch->port_ok |= (1 << portnum);
851  rio_set_port_lockout(rdev, portnum, 0);
852  /* Schedule Insertion Service */
853  pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
854  rio_name(rdev), portnum);
855  }
856 
857  /* Clear error-stopped states (if reported).
858  * Depending on the link partner state, two attempts
859  * may be needed for successful recovery.
860  */
861  if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
862  RIO_PORT_N_ERR_STS_PW_INP_ES)) {
863  if (rio_clr_err_stopped(rdev, portnum, err_status))
864  rio_clr_err_stopped(rdev, portnum, 0);
865  }
866  } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
867 
868  if (rdev->rswitch->port_ok & (1 << portnum)) {
869  rdev->rswitch->port_ok &= ~(1 << portnum);
870  rio_set_port_lockout(rdev, portnum, 1);
871 
872  rio_write_config_32(rdev,
873  rdev->phys_efptr +
874  RIO_PORT_N_ACK_STS_CSR(portnum),
876 
877  /* Schedule Extraction Service */
878  pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
879  rio_name(rdev), portnum);
880  }
881  }
882 
883  rio_read_config_32(rdev,
884  rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
885  if (em_perrdet) {
886  pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
887  portnum, em_perrdet);
888  /* Clear EM Port N Error Detect CSR */
889  rio_write_config_32(rdev,
890  rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
891  }
892 
893  rio_read_config_32(rdev,
894  rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
895  if (em_ltlerrdet) {
896  pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
897  em_ltlerrdet);
898  /* Clear EM L/T Layer Error Detect CSR */
899  rio_write_config_32(rdev,
900  rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
901  }
902 
903  /* Clear remaining error bits and Port-Write Pending bit */
904  rio_write_config_32(rdev,
905  rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
906  err_status);
907 
908  return 0;
909 }
911 
921 u32
923  u8 hopcount, u32 from)
924 {
925  u32 reg_val;
926 
927  if (from == 0) {
928  if (local)
929  rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
930  &reg_val);
931  else
932  rio_mport_read_config_32(port, destid, hopcount,
933  RIO_ASM_INFO_CAR, &reg_val);
934  return reg_val & RIO_EXT_FTR_PTR_MASK;
935  } else {
936  if (local)
937  rio_local_read_config_32(port, from, &reg_val);
938  else
939  rio_mport_read_config_32(port, destid, hopcount,
940  from, &reg_val);
941  return RIO_GET_BLOCK_ID(reg_val);
942  }
943 }
944 
971 u32
973  u8 hopcount, int ftr)
974 {
975  u32 asm_info, ext_ftr_ptr, ftr_header;
976 
977  if (local)
978  rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
979  else
980  rio_mport_read_config_32(port, destid, hopcount,
981  RIO_ASM_INFO_CAR, &asm_info);
982 
983  ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
984 
985  while (ext_ftr_ptr) {
986  if (local)
987  rio_local_read_config_32(port, ext_ftr_ptr,
988  &ftr_header);
989  else
990  rio_mport_read_config_32(port, destid, hopcount,
991  ext_ftr_ptr, &ftr_header);
992  if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
993  return ext_ftr_ptr;
994  if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
995  break;
996  }
997 
998  return 0;
999 }
1000 
1019  u16 asm_vid, u16 asm_did, struct rio_dev *from)
1020 {
1021  struct list_head *n;
1022  struct rio_dev *rdev;
1023 
1024  WARN_ON(in_interrupt());
1025  spin_lock(&rio_global_list_lock);
1026  n = from ? from->global_list.next : rio_devices.next;
1027 
1028  while (n && (n != &rio_devices)) {
1029  rdev = rio_dev_g(n);
1030  if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1031  (did == RIO_ANY_ID || rdev->did == did) &&
1032  (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1033  (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1034  goto exit;
1035  n = n->next;
1036  }
1037  rdev = NULL;
1038  exit:
1039  rio_dev_put(from);
1040  rdev = rio_dev_get(rdev);
1041  spin_unlock(&rio_global_list_lock);
1042  return rdev;
1043 }
1044 
1060 {
1061  return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1062 }
1063 
1075  u16 table, u16 route_destid, u8 route_port)
1076 {
1077  if (table == RIO_GLOBAL_TABLE) {
1078  rio_mport_write_config_32(mport, destid, hopcount,
1080  (u32)route_destid);
1081  rio_mport_write_config_32(mport, destid, hopcount,
1083  (u32)route_port);
1084  }
1085 
1086  udelay(10);
1087  return 0;
1088 }
1089 
1102  u16 table, u16 route_destid, u8 *route_port)
1103 {
1104  u32 result;
1105 
1106  if (table == RIO_GLOBAL_TABLE) {
1107  rio_mport_write_config_32(mport, destid, hopcount,
1108  RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1109  rio_mport_read_config_32(mport, destid, hopcount,
1111 
1112  *route_port = (u8)result;
1113  }
1114 
1115  return 0;
1116 }
1117 
1127  u16 table)
1128 {
1129  u32 max_destid = 0xff;
1130  u32 i, pef, id_inc = 1, ext_cfg = 0;
1132 
1133  if (table == RIO_GLOBAL_TABLE) {
1134  rio_mport_read_config_32(mport, destid, hopcount,
1135  RIO_PEF_CAR, &pef);
1136 
1137  if (mport->sys_size) {
1138  rio_mport_read_config_32(mport, destid, hopcount,
1140  &max_destid);
1141  max_destid &= RIO_RT_MAX_DESTID;
1142  }
1143 
1144  if (pef & RIO_PEF_EXT_RT) {
1145  ext_cfg = 0x80000000;
1146  id_inc = 4;
1147  port_sel = (RIO_INVALID_ROUTE << 24) |
1148  (RIO_INVALID_ROUTE << 16) |
1149  (RIO_INVALID_ROUTE << 8) |
1151  }
1152 
1153  for (i = 0; i <= max_destid;) {
1154  rio_mport_write_config_32(mport, destid, hopcount,
1156  ext_cfg | i);
1157  rio_mport_write_config_32(mport, destid, hopcount,
1159  port_sel);
1160  i += id_inc;
1161  }
1162  }
1163 
1164  udelay(10);
1165  return 0;
1166 }
1167 
1168 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
1169 
1170 static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1171 {
1172  struct rio_dev *rdev = arg;
1173 
1174  /* Check that DMA device belongs to the right MPORT */
1175  return (rdev->net->hport ==
1176  container_of(chan->device, struct rio_mport, dma));
1177 }
1178 
1186 struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1187 {
1189  struct dma_chan *dchan;
1190 
1191  dma_cap_zero(mask);
1192  dma_cap_set(DMA_SLAVE, mask);
1193  dchan = dma_request_channel(mask, rio_chan_filter, rdev);
1194 
1195  return dchan;
1196 }
1197 EXPORT_SYMBOL_GPL(rio_request_dma);
1198 
1203 void rio_release_dma(struct dma_chan *dchan)
1204 {
1205  dma_release_channel(dchan);
1206 }
1207 EXPORT_SYMBOL_GPL(rio_release_dma);
1208 
1223 struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1224  struct dma_chan *dchan, struct rio_dma_data *data,
1225  enum dma_transfer_direction direction, unsigned long flags)
1226 {
1227  struct dma_async_tx_descriptor *txd = NULL;
1228  struct rio_dma_ext rio_ext;
1229 
1230  if (dchan->device->device_prep_slave_sg == NULL) {
1231  pr_err("%s: prep_rio_sg == NULL\n", __func__);
1232  return NULL;
1233  }
1234 
1235  rio_ext.destid = rdev->destid;
1236  rio_ext.rio_addr_u = data->rio_addr_u;
1237  rio_ext.rio_addr = data->rio_addr;
1238  rio_ext.wr_type = data->wr_type;
1239 
1240  txd = dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1241  direction, flags, &rio_ext);
1242 
1243  return txd;
1244 }
1245 EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1246 
1247 #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1248 
1249 static void rio_fixup_device(struct rio_dev *dev)
1250 {
1251 }
1252 
1253 static int __devinit rio_init(void)
1254 {
1255  struct rio_dev *dev = NULL;
1256 
1257  while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
1258  rio_fixup_device(dev);
1259  }
1260  return 0;
1261 }
1262 
1263 static struct workqueue_struct *rio_wq;
1264 
1267  struct rio_mport *mport;
1268 };
1269 
1270 static void __devinit disc_work_handler(struct work_struct *_work)
1271 {
1272  struct rio_disc_work *work;
1273 
1274  work = container_of(_work, struct rio_disc_work, work);
1275  pr_debug("RIO: discovery work for mport %d %s\n",
1276  work->mport->id, work->mport->name);
1277  rio_disc_mport(work->mport);
1278 }
1279 
1281 {
1282  struct rio_mport *port;
1283  struct rio_disc_work *work;
1284  int n = 0;
1285 
1286  if (!next_portid)
1287  return -ENODEV;
1288 
1289  /*
1290  * First, run enumerations and check if we need to perform discovery
1291  * on any of the registered mports.
1292  */
1293  list_for_each_entry(port, &rio_mports, node) {
1294  if (port->host_deviceid >= 0)
1295  rio_enum_mport(port);
1296  else
1297  n++;
1298  }
1299 
1300  if (!n)
1301  goto no_disc;
1302 
1303  /*
1304  * If we have mports that require discovery schedule a discovery work
1305  * for each of them. If the code below fails to allocate needed
1306  * resources, exit without error to keep results of enumeration
1307  * process (if any).
1308  * TODO: Implement restart of dicovery process for all or
1309  * individual discovering mports.
1310  */
1311  rio_wq = alloc_workqueue("riodisc", 0, 0);
1312  if (!rio_wq) {
1313  pr_err("RIO: unable allocate rio_wq\n");
1314  goto no_disc;
1315  }
1316 
1317  work = kcalloc(n, sizeof *work, GFP_KERNEL);
1318  if (!work) {
1319  pr_err("RIO: no memory for work struct\n");
1320  destroy_workqueue(rio_wq);
1321  goto no_disc;
1322  }
1323 
1324  n = 0;
1325  list_for_each_entry(port, &rio_mports, node) {
1326  if (port->host_deviceid < 0) {
1327  work[n].mport = port;
1328  INIT_WORK(&work[n].work, disc_work_handler);
1329  queue_work(rio_wq, &work[n].work);
1330  n++;
1331  }
1332  }
1333 
1334  flush_workqueue(rio_wq);
1335  pr_debug("RIO: destroy discovery workqueue\n");
1336  destroy_workqueue(rio_wq);
1337  kfree(work);
1338 
1339 no_disc:
1340  rio_init();
1341 
1342  return 0;
1343 }
1344 
1346 
1347 static int hdids[RIO_MAX_MPORTS + 1];
1348 
1349 static int rio_get_hdid(int index)
1350 {
1351  if (!hdids[0] || hdids[0] <= index || index >= RIO_MAX_MPORTS)
1352  return -1;
1353 
1354  return hdids[index + 1];
1355 }
1356 
1357 static int rio_hdid_setup(char *str)
1358 {
1359  (void)get_options(str, ARRAY_SIZE(hdids), hdids);
1360  return 1;
1361 }
1362 
1363 __setup("riohdid=", rio_hdid_setup);
1364 
1366 {
1367  if (next_portid >= RIO_MAX_MPORTS) {
1368  pr_err("RIO: reached specified max number of mports\n");
1369  return 1;
1370  }
1371 
1372  port->id = next_portid++;
1373  port->host_deviceid = rio_get_hdid(port->id);
1374  list_add_tail(&port->node, &rio_mports);
1375  return 0;
1376 }
1377