mc5.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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_addr3(struct adapter *adapter, u32 v1, u32 v2,
  86. u32 v3)
  87. {
  88. t3_write_reg(adapter, A_MC5_DB_DBGI_REQ_ADDR0, v1);
  89. t3_write_reg(adapter, A_MC5_DB_DBGI_REQ_ADDR1, v2);
  90. t3_write_reg(adapter, A_MC5_DB_DBGI_REQ_ADDR2, v3);
  91. }
  92. static inline void dbgi_wr_data3(struct adapter *adapter, u32 v1, u32 v2,
  93. u32 v3)
  94. {
  95. t3_write_reg(adapter, A_MC5_DB_DBGI_REQ_DATA0, v1);
  96. t3_write_reg(adapter, A_MC5_DB_DBGI_REQ_DATA1, v2);
  97. t3_write_reg(adapter, A_MC5_DB_DBGI_REQ_DATA2, v3);
  98. }
  99. static inline void dbgi_rd_rsp3(struct adapter *adapter, u32 *v1, u32 *v2,
  100. u32 *v3)
  101. {
  102. *v1 = t3_read_reg(adapter, A_MC5_DB_DBGI_RSP_DATA0);
  103. *v2 = t3_read_reg(adapter, A_MC5_DB_DBGI_RSP_DATA1);
  104. *v3 = t3_read_reg(adapter, A_MC5_DB_DBGI_RSP_DATA2);
  105. }
  106. /*
  107. * Write data to the TCAM register at address (0, 0, addr_lo) using the TCAM
  108. * command cmd. The data to be written must have been set up by the caller.
  109. * Returns -1 on failure, 0 on success.
  110. */
  111. static int mc5_write(struct adapter *adapter, u32 addr_lo, u32 cmd)
  112. {
  113. t3_write_reg(adapter, A_MC5_DB_DBGI_REQ_ADDR0, addr_lo);
  114. if (mc5_cmd_write(adapter, cmd) == 0)
  115. return 0;
  116. CH_ERR(adapter, "MC5 timeout writing to TCAM address 0x%x\n",
  117. addr_lo);
  118. return -1;
  119. }
  120. static int init_mask_data_array(struct mc5 *mc5, u32 mask_array_base,
  121. u32 data_array_base, u32 write_cmd,
  122. int addr_shift)
  123. {
  124. unsigned int i;
  125. struct adapter *adap = mc5->adapter;
  126. /*
  127. * We need the size of the TCAM data and mask arrays in terms of
  128. * 72-bit entries.
  129. */
  130. unsigned int size72 = mc5->tcam_size;
  131. unsigned int server_base = t3_read_reg(adap, A_MC5_DB_SERVER_INDEX);
  132. if (mc5->mode == MC5_MODE_144_BIT) {
  133. size72 *= 2; /* 1 144-bit entry is 2 72-bit entries */
  134. server_base *= 2;
  135. }
  136. /* Clear the data array */
  137. dbgi_wr_data3(adap, 0, 0, 0);
  138. for (i = 0; i < size72; i++)
  139. if (mc5_write(adap, data_array_base + (i << addr_shift),
  140. write_cmd))
  141. return -1;
  142. /* Initialize the mask array. */
  143. dbgi_wr_data3(adap, 0xffffffff, 0xffffffff, 0xff);
  144. for (i = 0; i < size72; i++) {
  145. if (i == server_base) /* entering server or routing region */
  146. t3_write_reg(adap, A_MC5_DB_DBGI_REQ_DATA0,
  147. mc5->mode == MC5_MODE_144_BIT ?
  148. 0xfffffff9 : 0xfffffffd);
  149. if (mc5_write(adap, mask_array_base + (i << addr_shift),
  150. write_cmd))
  151. return -1;
  152. }
  153. return 0;
  154. }
  155. static int init_idt52100(struct mc5 *mc5)
  156. {
  157. int i;
  158. struct adapter *adap = mc5->adapter;
  159. t3_write_reg(adap, A_MC5_DB_RSP_LATENCY,
  160. V_RDLAT(0x15) | V_LRNLAT(0x15) | V_SRCHLAT(0x15));
  161. t3_write_reg(adap, A_MC5_DB_PART_ID_INDEX, 2);
  162. /*
  163. * Use GMRs 14-15 for ELOOKUP, GMRs 12-13 for SYN lookups, and
  164. * GMRs 8-9 for ACK- and AOPEN searches.
  165. */
  166. t3_write_reg(adap, A_MC5_DB_POPEN_DATA_WR_CMD, IDT_CMD_WRITE);
  167. t3_write_reg(adap, A_MC5_DB_POPEN_MASK_WR_CMD, IDT_CMD_WRITE);
  168. t3_write_reg(adap, A_MC5_DB_AOPEN_SRCH_CMD, IDT_CMD_SEARCH);
  169. t3_write_reg(adap, A_MC5_DB_AOPEN_LRN_CMD, IDT_CMD_LEARN);
  170. t3_write_reg(adap, A_MC5_DB_SYN_SRCH_CMD, IDT_CMD_SEARCH | 0x6000);
  171. t3_write_reg(adap, A_MC5_DB_SYN_LRN_CMD, IDT_CMD_LEARN);
  172. t3_write_reg(adap, A_MC5_DB_ACK_SRCH_CMD, IDT_CMD_SEARCH);
  173. t3_write_reg(adap, A_MC5_DB_ACK_LRN_CMD, IDT_CMD_LEARN);
  174. t3_write_reg(adap, A_MC5_DB_ILOOKUP_CMD, IDT_CMD_SEARCH);
  175. t3_write_reg(adap, A_MC5_DB_ELOOKUP_CMD, IDT_CMD_SEARCH | 0x7000);
  176. t3_write_reg(adap, A_MC5_DB_DATA_WRITE_CMD, IDT_CMD_WRITE);
  177. t3_write_reg(adap, A_MC5_DB_DATA_READ_CMD, IDT_CMD_READ);
  178. /* Set DBGI command mode for IDT TCAM. */
  179. t3_write_reg(adap, A_MC5_DB_DBGI_CONFIG, DBGI_MODE_IDT52100);
  180. /* Set up LAR */
  181. dbgi_wr_data3(adap, IDT_LAR_MODE144, 0, 0);
  182. if (mc5_write(adap, IDT_LAR_ADR0, IDT_CMD_WRITE))
  183. goto err;
  184. /* Set up SSRs */
  185. dbgi_wr_data3(adap, 0xffffffff, 0xffffffff, 0);
  186. if (mc5_write(adap, IDT_SSR0_ADR0, IDT_CMD_WRITE) ||
  187. mc5_write(adap, IDT_SSR1_ADR0, IDT_CMD_WRITE))
  188. goto err;
  189. /* Set up GMRs */
  190. for (i = 0; i < 32; ++i) {
  191. if (i >= 12 && i < 15)
  192. dbgi_wr_data3(adap, 0xfffffff9, 0xffffffff, 0xff);
  193. else if (i == 15)
  194. dbgi_wr_data3(adap, 0xfffffff9, 0xffff8007, 0xff);
  195. else
  196. dbgi_wr_data3(adap, 0xffffffff, 0xffffffff, 0xff);
  197. if (mc5_write(adap, IDT_GMR_BASE_ADR0 + i, IDT_CMD_WRITE))
  198. goto err;
  199. }
  200. /* Set up SCR */
  201. dbgi_wr_data3(adap, 1, 0, 0);
  202. if (mc5_write(adap, IDT_SCR_ADR0, IDT_CMD_WRITE))
  203. goto err;
  204. return init_mask_data_array(mc5, IDT_MSKARY_BASE_ADR0,
  205. IDT_DATARY_BASE_ADR0, IDT_CMD_WRITE, 0);
  206. err:
  207. return -EIO;
  208. }
  209. static int init_idt43102(struct mc5 *mc5)
  210. {
  211. int i;
  212. struct adapter *adap = mc5->adapter;
  213. t3_write_reg(adap, A_MC5_DB_RSP_LATENCY,
  214. adap->params.rev == 0 ? V_RDLAT(0xd) | V_SRCHLAT(0x11) :
  215. V_RDLAT(0xd) | V_SRCHLAT(0x12));
  216. /*
  217. * Use GMRs 24-25 for ELOOKUP, GMRs 20-21 for SYN lookups, and no mask
  218. * for ACK- and AOPEN searches.
  219. */
  220. t3_write_reg(adap, A_MC5_DB_POPEN_DATA_WR_CMD, IDT4_CMD_WRITE);
  221. t3_write_reg(adap, A_MC5_DB_POPEN_MASK_WR_CMD, IDT4_CMD_WRITE);
  222. t3_write_reg(adap, A_MC5_DB_AOPEN_SRCH_CMD,
  223. IDT4_CMD_SEARCH144 | 0x3800);
  224. t3_write_reg(adap, A_MC5_DB_SYN_SRCH_CMD, IDT4_CMD_SEARCH144);
  225. t3_write_reg(adap, A_MC5_DB_ACK_SRCH_CMD, IDT4_CMD_SEARCH144 | 0x3800);
  226. t3_write_reg(adap, A_MC5_DB_ILOOKUP_CMD, IDT4_CMD_SEARCH144 | 0x3800);
  227. t3_write_reg(adap, A_MC5_DB_ELOOKUP_CMD, IDT4_CMD_SEARCH144 | 0x800);
  228. t3_write_reg(adap, A_MC5_DB_DATA_WRITE_CMD, IDT4_CMD_WRITE);
  229. t3_write_reg(adap, A_MC5_DB_DATA_READ_CMD, IDT4_CMD_READ);
  230. t3_write_reg(adap, A_MC5_DB_PART_ID_INDEX, 3);
  231. /* Set DBGI command mode for IDT TCAM. */
  232. t3_write_reg(adap, A_MC5_DB_DBGI_CONFIG, DBGI_MODE_IDT52100);
  233. /* Set up GMRs */
  234. dbgi_wr_data3(adap, 0xffffffff, 0xffffffff, 0xff);
  235. for (i = 0; i < 7; ++i)
  236. if (mc5_write(adap, IDT4_GMR_BASE0 + i, IDT4_CMD_WRITE))
  237. goto err;
  238. for (i = 0; i < 4; ++i)
  239. if (mc5_write(adap, IDT4_GMR_BASE2 + i, IDT4_CMD_WRITE))
  240. goto err;
  241. dbgi_wr_data3(adap, 0xfffffff9, 0xffffffff, 0xff);
  242. if (mc5_write(adap, IDT4_GMR_BASE1, IDT4_CMD_WRITE) ||
  243. mc5_write(adap, IDT4_GMR_BASE1 + 1, IDT4_CMD_WRITE) ||
  244. mc5_write(adap, IDT4_GMR_BASE1 + 4, IDT4_CMD_WRITE))
  245. goto err;
  246. dbgi_wr_data3(adap, 0xfffffff9, 0xffff8007, 0xff);
  247. if (mc5_write(adap, IDT4_GMR_BASE1 + 5, IDT4_CMD_WRITE))
  248. goto err;
  249. /* Set up SCR */
  250. dbgi_wr_data3(adap, 0xf0000000, 0, 0);
  251. if (mc5_write(adap, IDT4_SCR_ADR0, IDT4_CMD_WRITE))
  252. goto err;
  253. return init_mask_data_array(mc5, IDT4_MSKARY_BASE_ADR0,
  254. IDT4_DATARY_BASE_ADR0, IDT4_CMD_WRITE, 1);
  255. err:
  256. return -EIO;
  257. }
  258. /* Put MC5 in DBGI mode. */
  259. static inline void mc5_dbgi_mode_enable(const struct mc5 *mc5)
  260. {
  261. t3_write_reg(mc5->adapter, A_MC5_DB_CONFIG,
  262. V_TMMODE(mc5->mode == MC5_MODE_72_BIT) | F_DBGIEN);
  263. }
  264. /* Put MC5 in M-Bus mode. */
  265. static void mc5_dbgi_mode_disable(const struct mc5 *mc5)
  266. {
  267. t3_write_reg(mc5->adapter, A_MC5_DB_CONFIG,
  268. V_TMMODE(mc5->mode == MC5_MODE_72_BIT) |
  269. V_COMPEN(mc5->mode == MC5_MODE_72_BIT) |
  270. V_PRTYEN(mc5->parity_enabled) | F_MBUSEN);
  271. }
  272. /*
  273. * Initialization that requires the OS and protocol layers to already
  274. * be initialized goes here.
  275. */
  276. int t3_mc5_init(struct mc5 *mc5, unsigned int nservers, unsigned int nfilters,
  277. unsigned int nroutes)
  278. {
  279. u32 cfg;
  280. int err;
  281. unsigned int tcam_size = mc5->tcam_size;
  282. struct adapter *adap = mc5->adapter;
  283. if (!tcam_size)
  284. return 0;
  285. if (nroutes > MAX_ROUTES || nroutes + nservers + nfilters > tcam_size)
  286. return -EINVAL;
  287. /* Reset the TCAM */
  288. cfg = t3_read_reg(adap, A_MC5_DB_CONFIG) & ~F_TMMODE;
  289. cfg |= V_TMMODE(mc5->mode == MC5_MODE_72_BIT) | F_TMRST;
  290. t3_write_reg(adap, A_MC5_DB_CONFIG, cfg);
  291. if (t3_wait_op_done(adap, A_MC5_DB_CONFIG, F_TMRDY, 1, 500, 0)) {
  292. CH_ERR(adap, "TCAM reset timed out\n");
  293. return -1;
  294. }
  295. t3_write_reg(adap, A_MC5_DB_ROUTING_TABLE_INDEX, tcam_size - nroutes);
  296. t3_write_reg(adap, A_MC5_DB_FILTER_TABLE,
  297. tcam_size - nroutes - nfilters);
  298. t3_write_reg(adap, A_MC5_DB_SERVER_INDEX,
  299. tcam_size - nroutes - nfilters - nservers);
  300. mc5->parity_enabled = 1;
  301. /* All the TCAM addresses we access have only the low 32 bits non 0 */
  302. t3_write_reg(adap, A_MC5_DB_DBGI_REQ_ADDR1, 0);
  303. t3_write_reg(adap, A_MC5_DB_DBGI_REQ_ADDR2, 0);
  304. mc5_dbgi_mode_enable(mc5);
  305. switch (mc5->part_type) {
  306. case IDT75P52100:
  307. err = init_idt52100(mc5);
  308. break;
  309. case IDT75N43102:
  310. err = init_idt43102(mc5);
  311. break;
  312. default:
  313. CH_ERR(adap, "Unsupported TCAM type %d\n", mc5->part_type);
  314. err = -EINVAL;
  315. break;
  316. }
  317. mc5_dbgi_mode_disable(mc5);
  318. return err;
  319. }
  320. #define MC5_INT_FATAL (F_PARITYERR | F_REQQPARERR | F_DISPQPARERR)
  321. /*
  322. * MC5 interrupt handler
  323. */
  324. void t3_mc5_intr_handler(struct mc5 *mc5)
  325. {
  326. struct adapter *adap = mc5->adapter;
  327. u32 cause = t3_read_reg(adap, A_MC5_DB_INT_CAUSE);
  328. if ((cause & F_PARITYERR) && mc5->parity_enabled) {
  329. CH_ALERT(adap, "MC5 parity error\n");
  330. mc5->stats.parity_err++;
  331. }
  332. if (cause & F_REQQPARERR) {
  333. CH_ALERT(adap, "MC5 request queue parity error\n");
  334. mc5->stats.reqq_parity_err++;
  335. }
  336. if (cause & F_DISPQPARERR) {
  337. CH_ALERT(adap, "MC5 dispatch queue parity error\n");
  338. mc5->stats.dispq_parity_err++;
  339. }
  340. if (cause & F_ACTRGNFULL)
  341. mc5->stats.active_rgn_full++;
  342. if (cause & F_NFASRCHFAIL)
  343. mc5->stats.nfa_srch_err++;
  344. if (cause & F_UNKNOWNCMD)
  345. mc5->stats.unknown_cmd++;
  346. if (cause & F_DELACTEMPTY)
  347. mc5->stats.del_act_empty++;
  348. if (cause & MC5_INT_FATAL)
  349. t3_fatal_err(adap);
  350. t3_write_reg(adap, A_MC5_DB_INT_CAUSE, cause);
  351. }
  352. void t3_mc5_prep(struct adapter *adapter, struct mc5 *mc5, int mode)
  353. {
  354. #define K * 1024
  355. static unsigned int tcam_part_size[] = { /* in K 72-bit entries */
  356. 64 K, 128 K, 256 K, 32 K
  357. };
  358. #undef K
  359. u32 cfg = t3_read_reg(adapter, A_MC5_DB_CONFIG);
  360. mc5->adapter = adapter;
  361. mc5->mode = (unsigned char)mode;
  362. mc5->part_type = (unsigned char)G_TMTYPE(cfg);
  363. if (cfg & F_TMTYPEHI)
  364. mc5->part_type |= 4;
  365. mc5->tcam_size = tcam_part_size[G_TMPARTSIZE(cfg)];
  366. if (mode == MC5_MODE_144_BIT)
  367. mc5->tcam_size /= 2;
  368. }