mc5.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (c) 2003-2008 Chelsio, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include "common.h"
  33. #include "regs.h"
  34. enum {
  35. IDT75P52100 = 4,
  36. IDT75N43102 = 5
  37. };
  38. /* DBGI command mode */
  39. enum {
  40. DBGI_MODE_MBUS = 0,
  41. DBGI_MODE_IDT52100 = 5
  42. };
  43. /* IDT 75P52100 commands */
  44. #define IDT_CMD_READ 0
  45. #define IDT_CMD_WRITE 1
  46. #define IDT_CMD_SEARCH 2
  47. #define IDT_CMD_LEARN 3
  48. /* IDT LAR register address and value for 144-bit mode (low 32 bits) */
  49. #define IDT_LAR_ADR0 0x180006
  50. #define IDT_LAR_MODE144 0xffff0000
  51. /* IDT SCR and SSR addresses (low 32 bits) */
  52. #define IDT_SCR_ADR0 0x180000
  53. #define IDT_SSR0_ADR0 0x180002
  54. #define IDT_SSR1_ADR0 0x180004
  55. /* IDT GMR base address (low 32 bits) */
  56. #define IDT_GMR_BASE_ADR0 0x180020
  57. /* IDT data and mask array base addresses (low 32 bits) */
  58. #define IDT_DATARY_BASE_ADR0 0
  59. #define IDT_MSKARY_BASE_ADR0 0x80000
  60. /* IDT 75N43102 commands */
  61. #define IDT4_CMD_SEARCH144 3
  62. #define IDT4_CMD_WRITE 4
  63. #define IDT4_CMD_READ 5
  64. /* IDT 75N43102 SCR address (low 32 bits) */
  65. #define IDT4_SCR_ADR0 0x3
  66. /* IDT 75N43102 GMR base addresses (low 32 bits) */
  67. #define IDT4_GMR_BASE0 0x10
  68. #define IDT4_GMR_BASE1 0x20
  69. #define IDT4_GMR_BASE2 0x30
  70. /* IDT 75N43102 data and mask array base addresses (low 32 bits) */
  71. #define IDT4_DATARY_BASE_ADR0 0x1000000
  72. #define IDT4_MSKARY_BASE_ADR0 0x2000000
  73. #define MAX_WRITE_ATTEMPTS 5
  74. #define MAX_ROUTES 2048
  75. /*
  76. * Issue a command to the TCAM and wait for its completion. The address and
  77. * any data required by the command must have been setup by the caller.
  78. */
  79. static int mc5_cmd_write(struct adapter *adapter, u32 cmd)
  80. {
  81. t3_write_reg(adapter, A_MC5_DB_DBGI_REQ_CMD, cmd);
  82. return t3_wait_op_done(adapter, A_MC5_DB_DBGI_RSP_STATUS,
  83. F_DBGIRSPVALID, 1, MAX_WRITE_ATTEMPTS, 1);
  84. }
  85. static inline void dbgi_wr_data3(struct adapter *adapter, u32 v1, u32 v2,
  86. u32 v3)
  87. {
  88. t3_write_reg(adapter, A_MC5_DB_DBGI_REQ_DATA0, v1);
  89. t3_write_reg(adapter, A_MC5_DB_DBGI_REQ_DATA1, v2);
  90. t3_write_reg(adapter, A_MC5_DB_DBGI_REQ_DATA2, v3);
  91. }
  92. /*
  93. * Write data to the TCAM register at address (0, 0, addr_lo) using the TCAM
  94. * command cmd. The data to be written must have been set up by the caller.
  95. * Returns -1 on failure, 0 on success.
  96. */
  97. static int mc5_write(struct adapter *adapter, u32 addr_lo, u32 cmd)
  98. {
  99. t3_write_reg(adapter, A_MC5_DB_DBGI_REQ_ADDR0, addr_lo);
  100. if (mc5_cmd_write(adapter, cmd) == 0)
  101. return 0;
  102. CH_ERR(adapter, "MC5 timeout writing to TCAM address 0x%x\n",
  103. addr_lo);
  104. return -1;
  105. }
  106. static int init_mask_data_array(struct mc5 *mc5, u32 mask_array_base,
  107. u32 data_array_base, u32 write_cmd,
  108. int addr_shift)
  109. {
  110. unsigned int i;
  111. struct adapter *adap = mc5->adapter;
  112. /*
  113. * We need the size of the TCAM data and mask arrays in terms of
  114. * 72-bit entries.
  115. */
  116. unsigned int size72 = mc5->tcam_size;
  117. unsigned int server_base = t3_read_reg(adap, A_MC5_DB_SERVER_INDEX);
  118. if (mc5->mode == MC5_MODE_144_BIT) {
  119. size72 *= 2; /* 1 144-bit entry is 2 72-bit entries */
  120. server_base *= 2;
  121. }
  122. /* Clear the data array */
  123. dbgi_wr_data3(adap, 0, 0, 0);
  124. for (i = 0; i < size72; i++)
  125. if (mc5_write(adap, data_array_base + (i << addr_shift),
  126. write_cmd))
  127. return -1;
  128. /* Initialize the mask array. */
  129. dbgi_wr_data3(adap, 0xffffffff, 0xffffffff, 0xff);
  130. for (i = 0; i < size72; i++) {
  131. if (i == server_base) /* entering server or routing region */
  132. t3_write_reg(adap, A_MC5_DB_DBGI_REQ_DATA0,
  133. mc5->mode == MC5_MODE_144_BIT ?
  134. 0xfffffff9 : 0xfffffffd);
  135. if (mc5_write(adap, mask_array_base + (i << addr_shift),
  136. write_cmd))
  137. return -1;
  138. }
  139. return 0;
  140. }
  141. static int init_idt52100(struct mc5 *mc5)
  142. {
  143. int i;
  144. struct adapter *adap = mc5->adapter;
  145. t3_write_reg(adap, A_MC5_DB_RSP_LATENCY,
  146. V_RDLAT(0x15) | V_LRNLAT(0x15) | V_SRCHLAT(0x15));
  147. t3_write_reg(adap, A_MC5_DB_PART_ID_INDEX, 2);
  148. /*
  149. * Use GMRs 14-15 for ELOOKUP, GMRs 12-13 for SYN lookups, and
  150. * GMRs 8-9 for ACK- and AOPEN searches.
  151. */
  152. t3_write_reg(adap, A_MC5_DB_POPEN_DATA_WR_CMD, IDT_CMD_WRITE);
  153. t3_write_reg(adap, A_MC5_DB_POPEN_MASK_WR_CMD, IDT_CMD_WRITE);
  154. t3_write_reg(adap, A_MC5_DB_AOPEN_SRCH_CMD, IDT_CMD_SEARCH);
  155. t3_write_reg(adap, A_MC5_DB_AOPEN_LRN_CMD, IDT_CMD_LEARN);
  156. t3_write_reg(adap, A_MC5_DB_SYN_SRCH_CMD, IDT_CMD_SEARCH | 0x6000);
  157. t3_write_reg(adap, A_MC5_DB_SYN_LRN_CMD, IDT_CMD_LEARN);
  158. t3_write_reg(adap, A_MC5_DB_ACK_SRCH_CMD, IDT_CMD_SEARCH);
  159. t3_write_reg(adap, A_MC5_DB_ACK_LRN_CMD, IDT_CMD_LEARN);
  160. t3_write_reg(adap, A_MC5_DB_ILOOKUP_CMD, IDT_CMD_SEARCH);
  161. t3_write_reg(adap, A_MC5_DB_ELOOKUP_CMD, IDT_CMD_SEARCH | 0x7000);
  162. t3_write_reg(adap, A_MC5_DB_DATA_WRITE_CMD, IDT_CMD_WRITE);
  163. t3_write_reg(adap, A_MC5_DB_DATA_READ_CMD, IDT_CMD_READ);
  164. /* Set DBGI command mode for IDT TCAM. */
  165. t3_write_reg(adap, A_MC5_DB_DBGI_CONFIG, DBGI_MODE_IDT52100);
  166. /* Set up LAR */
  167. dbgi_wr_data3(adap, IDT_LAR_MODE144, 0, 0);
  168. if (mc5_write(adap, IDT_LAR_ADR0, IDT_CMD_WRITE))
  169. goto err;
  170. /* Set up SSRs */
  171. dbgi_wr_data3(adap, 0xffffffff, 0xffffffff, 0);
  172. if (mc5_write(adap, IDT_SSR0_ADR0, IDT_CMD_WRITE) ||
  173. mc5_write(adap, IDT_SSR1_ADR0, IDT_CMD_WRITE))
  174. goto err;
  175. /* Set up GMRs */
  176. for (i = 0; i < 32; ++i) {
  177. if (i >= 12 && i < 15)
  178. dbgi_wr_data3(adap, 0xfffffff9, 0xffffffff, 0xff);
  179. else if (i == 15)
  180. dbgi_wr_data3(adap, 0xfffffff9, 0xffff8007, 0xff);
  181. else
  182. dbgi_wr_data3(adap, 0xffffffff, 0xffffffff, 0xff);
  183. if (mc5_write(adap, IDT_GMR_BASE_ADR0 + i, IDT_CMD_WRITE))
  184. goto err;
  185. }
  186. /* Set up SCR */
  187. dbgi_wr_data3(adap, 1, 0, 0);
  188. if (mc5_write(adap, IDT_SCR_ADR0, IDT_CMD_WRITE))
  189. goto err;
  190. return init_mask_data_array(mc5, IDT_MSKARY_BASE_ADR0,
  191. IDT_DATARY_BASE_ADR0, IDT_CMD_WRITE, 0);
  192. err:
  193. return -EIO;
  194. }
  195. static int init_idt43102(struct mc5 *mc5)
  196. {
  197. int i;
  198. struct adapter *adap = mc5->adapter;
  199. t3_write_reg(adap, A_MC5_DB_RSP_LATENCY,
  200. adap->params.rev == 0 ? V_RDLAT(0xd) | V_SRCHLAT(0x11) :
  201. V_RDLAT(0xd) | V_SRCHLAT(0x12));
  202. /*
  203. * Use GMRs 24-25 for ELOOKUP, GMRs 20-21 for SYN lookups, and no mask
  204. * for ACK- and AOPEN searches.
  205. */
  206. t3_write_reg(adap, A_MC5_DB_POPEN_DATA_WR_CMD, IDT4_CMD_WRITE);
  207. t3_write_reg(adap, A_MC5_DB_POPEN_MASK_WR_CMD, IDT4_CMD_WRITE);
  208. t3_write_reg(adap, A_MC5_DB_AOPEN_SRCH_CMD,
  209. IDT4_CMD_SEARCH144 | 0x3800);
  210. t3_write_reg(adap, A_MC5_DB_SYN_SRCH_CMD, IDT4_CMD_SEARCH144);
  211. t3_write_reg(adap, A_MC5_DB_ACK_SRCH_CMD, IDT4_CMD_SEARCH144 | 0x3800);
  212. t3_write_reg(adap, A_MC5_DB_ILOOKUP_CMD, IDT4_CMD_SEARCH144 | 0x3800);
  213. t3_write_reg(adap, A_MC5_DB_ELOOKUP_CMD, IDT4_CMD_SEARCH144 | 0x800);
  214. t3_write_reg(adap, A_MC5_DB_DATA_WRITE_CMD, IDT4_CMD_WRITE);
  215. t3_write_reg(adap, A_MC5_DB_DATA_READ_CMD, IDT4_CMD_READ);
  216. t3_write_reg(adap, A_MC5_DB_PART_ID_INDEX, 3);
  217. /* Set DBGI command mode for IDT TCAM. */
  218. t3_write_reg(adap, A_MC5_DB_DBGI_CONFIG, DBGI_MODE_IDT52100);
  219. /* Set up GMRs */
  220. dbgi_wr_data3(adap, 0xffffffff, 0xffffffff, 0xff);
  221. for (i = 0; i < 7; ++i)
  222. if (mc5_write(adap, IDT4_GMR_BASE0 + i, IDT4_CMD_WRITE))
  223. goto err;
  224. for (i = 0; i < 4; ++i)
  225. if (mc5_write(adap, IDT4_GMR_BASE2 + i, IDT4_CMD_WRITE))
  226. goto err;
  227. dbgi_wr_data3(adap, 0xfffffff9, 0xffffffff, 0xff);
  228. if (mc5_write(adap, IDT4_GMR_BASE1, IDT4_CMD_WRITE) ||
  229. mc5_write(adap, IDT4_GMR_BASE1 + 1, IDT4_CMD_WRITE) ||
  230. mc5_write(adap, IDT4_GMR_BASE1 + 4, IDT4_CMD_WRITE))
  231. goto err;
  232. dbgi_wr_data3(adap, 0xfffffff9, 0xffff8007, 0xff);
  233. if (mc5_write(adap, IDT4_GMR_BASE1 + 5, IDT4_CMD_WRITE))
  234. goto err;
  235. /* Set up SCR */
  236. dbgi_wr_data3(adap, 0xf0000000, 0, 0);
  237. if (mc5_write(adap, IDT4_SCR_ADR0, IDT4_CMD_WRITE))
  238. goto err;
  239. return init_mask_data_array(mc5, IDT4_MSKARY_BASE_ADR0,
  240. IDT4_DATARY_BASE_ADR0, IDT4_CMD_WRITE, 1);
  241. err:
  242. return -EIO;
  243. }
  244. /* Put MC5 in DBGI mode. */
  245. static inline void mc5_dbgi_mode_enable(const struct mc5 *mc5)
  246. {
  247. t3_write_reg(mc5->adapter, A_MC5_DB_CONFIG,
  248. V_TMMODE(mc5->mode == MC5_MODE_72_BIT) | F_DBGIEN);
  249. }
  250. /* Put MC5 in M-Bus mode. */
  251. static void mc5_dbgi_mode_disable(const struct mc5 *mc5)
  252. {
  253. t3_write_reg(mc5->adapter, A_MC5_DB_CONFIG,
  254. V_TMMODE(mc5->mode == MC5_MODE_72_BIT) |
  255. V_COMPEN(mc5->mode == MC5_MODE_72_BIT) |
  256. V_PRTYEN(mc5->parity_enabled) | F_MBUSEN);
  257. }
  258. /*
  259. * Initialization that requires the OS and protocol layers to already
  260. * be initialized goes here.
  261. */
  262. int t3_mc5_init(struct mc5 *mc5, unsigned int nservers, unsigned int nfilters,
  263. unsigned int nroutes)
  264. {
  265. u32 cfg;
  266. int err;
  267. unsigned int tcam_size = mc5->tcam_size;
  268. struct adapter *adap = mc5->adapter;
  269. if (!tcam_size)
  270. return 0;
  271. if (nroutes > MAX_ROUTES || nroutes + nservers + nfilters > tcam_size)
  272. return -EINVAL;
  273. /* Reset the TCAM */
  274. cfg = t3_read_reg(adap, A_MC5_DB_CONFIG) & ~F_TMMODE;
  275. cfg |= V_TMMODE(mc5->mode == MC5_MODE_72_BIT) | F_TMRST;
  276. t3_write_reg(adap, A_MC5_DB_CONFIG, cfg);
  277. if (t3_wait_op_done(adap, A_MC5_DB_CONFIG, F_TMRDY, 1, 500, 0)) {
  278. CH_ERR(adap, "TCAM reset timed out\n");
  279. return -1;
  280. }
  281. t3_write_reg(adap, A_MC5_DB_ROUTING_TABLE_INDEX, tcam_size - nroutes);
  282. t3_write_reg(adap, A_MC5_DB_FILTER_TABLE,
  283. tcam_size - nroutes - nfilters);
  284. t3_write_reg(adap, A_MC5_DB_SERVER_INDEX,
  285. tcam_size - nroutes - nfilters - nservers);
  286. mc5->parity_enabled = 1;
  287. /* All the TCAM addresses we access have only the low 32 bits non 0 */
  288. t3_write_reg(adap, A_MC5_DB_DBGI_REQ_ADDR1, 0);
  289. t3_write_reg(adap, A_MC5_DB_DBGI_REQ_ADDR2, 0);
  290. mc5_dbgi_mode_enable(mc5);
  291. switch (mc5->part_type) {
  292. case IDT75P52100:
  293. err = init_idt52100(mc5);
  294. break;
  295. case IDT75N43102:
  296. err = init_idt43102(mc5);
  297. break;
  298. default:
  299. CH_ERR(adap, "Unsupported TCAM type %d\n", mc5->part_type);
  300. err = -EINVAL;
  301. break;
  302. }
  303. mc5_dbgi_mode_disable(mc5);
  304. return err;
  305. }
  306. #define MC5_INT_FATAL (F_PARITYERR | F_REQQPARERR | F_DISPQPARERR)
  307. /*
  308. * MC5 interrupt handler
  309. */
  310. void t3_mc5_intr_handler(struct mc5 *mc5)
  311. {
  312. struct adapter *adap = mc5->adapter;
  313. u32 cause = t3_read_reg(adap, A_MC5_DB_INT_CAUSE);
  314. if ((cause & F_PARITYERR) && mc5->parity_enabled) {
  315. CH_ALERT(adap, "MC5 parity error\n");
  316. mc5->stats.parity_err++;
  317. }
  318. if (cause & F_REQQPARERR) {
  319. CH_ALERT(adap, "MC5 request queue parity error\n");
  320. mc5->stats.reqq_parity_err++;
  321. }
  322. if (cause & F_DISPQPARERR) {
  323. CH_ALERT(adap, "MC5 dispatch queue parity error\n");
  324. mc5->stats.dispq_parity_err++;
  325. }
  326. if (cause & F_ACTRGNFULL)
  327. mc5->stats.active_rgn_full++;
  328. if (cause & F_NFASRCHFAIL)
  329. mc5->stats.nfa_srch_err++;
  330. if (cause & F_UNKNOWNCMD)
  331. mc5->stats.unknown_cmd++;
  332. if (cause & F_DELACTEMPTY)
  333. mc5->stats.del_act_empty++;
  334. if (cause & MC5_INT_FATAL)
  335. t3_fatal_err(adap);
  336. t3_write_reg(adap, A_MC5_DB_INT_CAUSE, cause);
  337. }
  338. void t3_mc5_prep(struct adapter *adapter, struct mc5 *mc5, int mode)
  339. {
  340. #define K * 1024
  341. static unsigned int tcam_part_size[] = { /* in K 72-bit entries */
  342. 64 K, 128 K, 256 K, 32 K
  343. };
  344. #undef K
  345. u32 cfg = t3_read_reg(adapter, A_MC5_DB_CONFIG);
  346. mc5->adapter = adapter;
  347. mc5->mode = (unsigned char)mode;
  348. mc5->part_type = (unsigned char)G_TMTYPE(cfg);
  349. if (cfg & F_TMTYPEHI)
  350. mc5->part_type |= 4;
  351. mc5->tcam_size = tcam_part_size[G_TMPARTSIZE(cfg)];
  352. if (mode == MC5_MODE_144_BIT)
  353. mc5->tcam_size /= 2;
  354. }