idt_gen3.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IDT RXS Gen.3 Serial RapidIO switch family support
  4. *
  5. * Copyright 2016 Integrated Device Technology, Inc.
  6. */
  7. #include <linux/stat.h>
  8. #include <linux/module.h>
  9. #include <linux/rio.h>
  10. #include <linux/rio_drv.h>
  11. #include <linux/rio_ids.h>
  12. #include <linux/delay.h>
  13. #include <asm/page.h>
  14. #include "../rio.h"
  15. #define RIO_EM_PW_STAT 0x40020
  16. #define RIO_PW_CTL 0x40204
  17. #define RIO_PW_CTL_PW_TMR 0xffffff00
  18. #define RIO_PW_ROUTE 0x40208
  19. #define RIO_EM_DEV_INT_EN 0x40030
  20. #define RIO_PLM_SPx_IMP_SPEC_CTL(x) (0x10100 + (x)*0x100)
  21. #define RIO_PLM_SPx_IMP_SPEC_CTL_SOFT_RST 0x02000000
  22. #define RIO_PLM_SPx_PW_EN(x) (0x10118 + (x)*0x100)
  23. #define RIO_PLM_SPx_PW_EN_OK2U 0x40000000
  24. #define RIO_PLM_SPx_PW_EN_LINIT 0x10000000
  25. #define RIO_BC_L2_Gn_ENTRYx_CSR(n, x) (0x31000 + (n)*0x400 + (x)*0x4)
  26. #define RIO_SPx_L2_Gn_ENTRYy_CSR(x, n, y) \
  27. (0x51000 + (x)*0x2000 + (n)*0x400 + (y)*0x4)
  28. static int
  29. idtg3_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
  30. u16 table, u16 route_destid, u8 route_port)
  31. {
  32. u32 rval;
  33. u32 entry = route_port;
  34. int err = 0;
  35. pr_debug("RIO: %s t=0x%x did_%x to p_%x\n",
  36. __func__, table, route_destid, entry);
  37. if (route_destid > 0xFF)
  38. return -EINVAL;
  39. if (route_port == RIO_INVALID_ROUTE)
  40. entry = RIO_RT_ENTRY_DROP_PKT;
  41. if (table == RIO_GLOBAL_TABLE) {
  42. /* Use broadcast register to update all per-port tables */
  43. err = rio_mport_write_config_32(mport, destid, hopcount,
  44. RIO_BC_L2_Gn_ENTRYx_CSR(0, route_destid),
  45. entry);
  46. return err;
  47. }
  48. /*
  49. * Verify that specified port/table number is valid
  50. */
  51. err = rio_mport_read_config_32(mport, destid, hopcount,
  52. RIO_SWP_INFO_CAR, &rval);
  53. if (err)
  54. return err;
  55. if (table >= RIO_GET_TOTAL_PORTS(rval))
  56. return -EINVAL;
  57. err = rio_mport_write_config_32(mport, destid, hopcount,
  58. RIO_SPx_L2_Gn_ENTRYy_CSR(table, 0, route_destid),
  59. entry);
  60. return err;
  61. }
  62. static int
  63. idtg3_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
  64. u16 table, u16 route_destid, u8 *route_port)
  65. {
  66. u32 rval;
  67. int err;
  68. if (route_destid > 0xFF)
  69. return -EINVAL;
  70. err = rio_mport_read_config_32(mport, destid, hopcount,
  71. RIO_SWP_INFO_CAR, &rval);
  72. if (err)
  73. return err;
  74. /*
  75. * This switch device does not have the dedicated global routing table.
  76. * It is substituted by reading routing table of the ingress port of
  77. * maintenance read requests.
  78. */
  79. if (table == RIO_GLOBAL_TABLE)
  80. table = RIO_GET_PORT_NUM(rval);
  81. else if (table >= RIO_GET_TOTAL_PORTS(rval))
  82. return -EINVAL;
  83. err = rio_mport_read_config_32(mport, destid, hopcount,
  84. RIO_SPx_L2_Gn_ENTRYy_CSR(table, 0, route_destid),
  85. &rval);
  86. if (err)
  87. return err;
  88. if (rval == RIO_RT_ENTRY_DROP_PKT)
  89. *route_port = RIO_INVALID_ROUTE;
  90. else
  91. *route_port = (u8)rval;
  92. return 0;
  93. }
  94. static int
  95. idtg3_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
  96. u16 table)
  97. {
  98. u32 i;
  99. u32 rval;
  100. int err;
  101. if (table == RIO_GLOBAL_TABLE) {
  102. for (i = 0; i <= 0xff; i++) {
  103. err = rio_mport_write_config_32(mport, destid, hopcount,
  104. RIO_BC_L2_Gn_ENTRYx_CSR(0, i),
  105. RIO_RT_ENTRY_DROP_PKT);
  106. if (err)
  107. break;
  108. }
  109. return err;
  110. }
  111. err = rio_mport_read_config_32(mport, destid, hopcount,
  112. RIO_SWP_INFO_CAR, &rval);
  113. if (err)
  114. return err;
  115. if (table >= RIO_GET_TOTAL_PORTS(rval))
  116. return -EINVAL;
  117. for (i = 0; i <= 0xff; i++) {
  118. err = rio_mport_write_config_32(mport, destid, hopcount,
  119. RIO_SPx_L2_Gn_ENTRYy_CSR(table, 0, i),
  120. RIO_RT_ENTRY_DROP_PKT);
  121. if (err)
  122. break;
  123. }
  124. return err;
  125. }
  126. /*
  127. * This routine performs device-specific initialization only.
  128. * All standard EM configuration should be performed at upper level.
  129. */
  130. static int
  131. idtg3_em_init(struct rio_dev *rdev)
  132. {
  133. int i, tmp;
  134. u32 rval;
  135. pr_debug("RIO: %s [%d:%d]\n", __func__, rdev->destid, rdev->hopcount);
  136. /* Disable assertion of interrupt signal */
  137. rio_write_config_32(rdev, RIO_EM_DEV_INT_EN, 0);
  138. /* Disable port-write event notifications during initialization */
  139. rio_write_config_32(rdev, rdev->em_efptr + RIO_EM_PW_TX_CTRL,
  140. RIO_EM_PW_TX_CTRL_PW_DIS);
  141. /* Configure Port-Write notifications for hot-swap events */
  142. tmp = RIO_GET_TOTAL_PORTS(rdev->swpinfo);
  143. for (i = 0; i < tmp; i++) {
  144. rio_read_config_32(rdev,
  145. RIO_DEV_PORT_N_ERR_STS_CSR(rdev, i),
  146. &rval);
  147. if (rval & RIO_PORT_N_ERR_STS_PORT_UA)
  148. continue;
  149. /* Clear events signaled before enabling notification */
  150. rio_write_config_32(rdev,
  151. rdev->em_efptr + RIO_EM_PN_ERR_DETECT(i), 0);
  152. /* Enable event notifications */
  153. rio_write_config_32(rdev,
  154. rdev->em_efptr + RIO_EM_PN_ERRRATE_EN(i),
  155. RIO_EM_PN_ERRRATE_EN_OK2U | RIO_EM_PN_ERRRATE_EN_U2OK);
  156. /* Enable port-write generation on events */
  157. rio_write_config_32(rdev, RIO_PLM_SPx_PW_EN(i),
  158. RIO_PLM_SPx_PW_EN_OK2U | RIO_PLM_SPx_PW_EN_LINIT);
  159. }
  160. /* Set Port-Write destination port */
  161. tmp = RIO_GET_PORT_NUM(rdev->swpinfo);
  162. rio_write_config_32(rdev, RIO_PW_ROUTE, 1 << tmp);
  163. /* Enable sending port-write event notifications */
  164. rio_write_config_32(rdev, rdev->em_efptr + RIO_EM_PW_TX_CTRL, 0);
  165. /* set TVAL = ~50us */
  166. rio_write_config_32(rdev,
  167. rdev->phys_efptr + RIO_PORT_LINKTO_CTL_CSR, 0x8e << 8);
  168. return 0;
  169. }
  170. /*
  171. * idtg3_em_handler - device-specific error handler
  172. *
  173. * If the link is down (PORT_UNINIT) does nothing - this is considered
  174. * as link partner removal from the port.
  175. *
  176. * If the link is up (PORT_OK) - situation is handled as *new* device insertion.
  177. * In this case ERR_STOP bits are cleared by issuing soft reset command to the
  178. * reporting port. Inbound and outbound ackIDs are cleared by the reset as well.
  179. * This way the port is synchronized with freshly inserted device (assuming it
  180. * was reset/powered-up on insertion).
  181. *
  182. * TODO: This is not sufficient in a situation when a link between two devices
  183. * was down and up again (e.g. cable disconnect). For that situation full ackID
  184. * realignment process has to be implemented.
  185. */
  186. static int
  187. idtg3_em_handler(struct rio_dev *rdev, u8 pnum)
  188. {
  189. u32 err_status;
  190. u32 rval;
  191. rio_read_config_32(rdev,
  192. RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
  193. &err_status);
  194. /* Do nothing for device/link removal */
  195. if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT)
  196. return 0;
  197. /* When link is OK we have a device insertion.
  198. * Request port soft reset to clear errors if they present.
  199. * Inbound and outbound ackIDs will be 0 after reset.
  200. */
  201. if (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
  202. RIO_PORT_N_ERR_STS_INP_ES)) {
  203. rio_read_config_32(rdev, RIO_PLM_SPx_IMP_SPEC_CTL(pnum), &rval);
  204. rio_write_config_32(rdev, RIO_PLM_SPx_IMP_SPEC_CTL(pnum),
  205. rval | RIO_PLM_SPx_IMP_SPEC_CTL_SOFT_RST);
  206. udelay(10);
  207. rio_write_config_32(rdev, RIO_PLM_SPx_IMP_SPEC_CTL(pnum), rval);
  208. msleep(500);
  209. }
  210. return 0;
  211. }
  212. static struct rio_switch_ops idtg3_switch_ops = {
  213. .owner = THIS_MODULE,
  214. .add_entry = idtg3_route_add_entry,
  215. .get_entry = idtg3_route_get_entry,
  216. .clr_table = idtg3_route_clr_table,
  217. .em_init = idtg3_em_init,
  218. .em_handle = idtg3_em_handler,
  219. };
  220. static int idtg3_probe(struct rio_dev *rdev, const struct rio_device_id *id)
  221. {
  222. pr_debug("RIO: %s for %s\n", __func__, rio_name(rdev));
  223. spin_lock(&rdev->rswitch->lock);
  224. if (rdev->rswitch->ops) {
  225. spin_unlock(&rdev->rswitch->lock);
  226. return -EINVAL;
  227. }
  228. rdev->rswitch->ops = &idtg3_switch_ops;
  229. if (rdev->do_enum) {
  230. /* Disable hierarchical routing support: Existing fabric
  231. * enumeration/discovery process (see rio-scan.c) uses 8-bit
  232. * flat destination ID routing only.
  233. */
  234. rio_write_config_32(rdev, 0x5000 + RIO_BC_RT_CTL_CSR, 0);
  235. }
  236. spin_unlock(&rdev->rswitch->lock);
  237. return 0;
  238. }
  239. static void idtg3_remove(struct rio_dev *rdev)
  240. {
  241. pr_debug("RIO: %s for %s\n", __func__, rio_name(rdev));
  242. spin_lock(&rdev->rswitch->lock);
  243. if (rdev->rswitch->ops == &idtg3_switch_ops)
  244. rdev->rswitch->ops = NULL;
  245. spin_unlock(&rdev->rswitch->lock);
  246. }
  247. /*
  248. * Gen3 switches repeat sending PW messages until a corresponding event flag
  249. * is cleared. Use shutdown notification to disable generation of port-write
  250. * messages if their destination node is shut down.
  251. */
  252. static void idtg3_shutdown(struct rio_dev *rdev)
  253. {
  254. int i;
  255. u32 rval;
  256. u16 destid;
  257. /* Currently the enumerator node acts also as PW handler */
  258. if (!rdev->do_enum)
  259. return;
  260. pr_debug("RIO: %s(%s)\n", __func__, rio_name(rdev));
  261. rio_read_config_32(rdev, RIO_PW_ROUTE, &rval);
  262. i = RIO_GET_PORT_NUM(rdev->swpinfo);
  263. /* Check port-write destination port */
  264. if (!((1 << i) & rval))
  265. return;
  266. /* Disable sending port-write event notifications if PW destID
  267. * matches to one of the enumerator node
  268. */
  269. rio_read_config_32(rdev, rdev->em_efptr + RIO_EM_PW_TGT_DEVID, &rval);
  270. if (rval & RIO_EM_PW_TGT_DEVID_DEV16)
  271. destid = rval >> 16;
  272. else
  273. destid = ((rval & RIO_EM_PW_TGT_DEVID_D8) >> 16);
  274. if (rdev->net->hport->host_deviceid == destid) {
  275. rio_write_config_32(rdev,
  276. rdev->em_efptr + RIO_EM_PW_TX_CTRL, 0);
  277. pr_debug("RIO: %s(%s) PW transmission disabled\n",
  278. __func__, rio_name(rdev));
  279. }
  280. }
  281. static const struct rio_device_id idtg3_id_table[] = {
  282. {RIO_DEVICE(RIO_DID_IDTRXS1632, RIO_VID_IDT)},
  283. {RIO_DEVICE(RIO_DID_IDTRXS2448, RIO_VID_IDT)},
  284. { 0, } /* terminate list */
  285. };
  286. static struct rio_driver idtg3_driver = {
  287. .name = "idt_gen3",
  288. .id_table = idtg3_id_table,
  289. .probe = idtg3_probe,
  290. .remove = idtg3_remove,
  291. .shutdown = idtg3_shutdown,
  292. };
  293. static int __init idtg3_init(void)
  294. {
  295. return rio_register_driver(&idtg3_driver);
  296. }
  297. static void __exit idtg3_exit(void)
  298. {
  299. pr_debug("RIO: %s\n", __func__);
  300. rio_unregister_driver(&idtg3_driver);
  301. pr_debug("RIO: %s done\n", __func__);
  302. }
  303. device_initcall(idtg3_init);
  304. module_exit(idtg3_exit);
  305. MODULE_DESCRIPTION("IDT RXS Gen.3 Serial RapidIO switch family driver");
  306. MODULE_AUTHOR("Integrated Device Technology, Inc.");
  307. MODULE_LICENSE("GPL");