xgmac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * This file is part of the Chelsio T3 Ethernet driver.
  3. *
  4. * Copyright (C) 2005-2006 Chelsio Communications. All rights reserved.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. * FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE file included in this
  9. * release for licensing terms and conditions.
  10. */
  11. #include "common.h"
  12. #include "regs.h"
  13. /*
  14. * # of exact address filters. The first one is used for the station address,
  15. * the rest are available for multicast addresses.
  16. */
  17. #define EXACT_ADDR_FILTERS 8
  18. static inline int macidx(const struct cmac *mac)
  19. {
  20. return mac->offset / (XGMAC0_1_BASE_ADDR - XGMAC0_0_BASE_ADDR);
  21. }
  22. static void xaui_serdes_reset(struct cmac *mac)
  23. {
  24. static const unsigned int clear[] = {
  25. F_PWRDN0 | F_PWRDN1, F_RESETPLL01, F_RESET0 | F_RESET1,
  26. F_PWRDN2 | F_PWRDN3, F_RESETPLL23, F_RESET2 | F_RESET3
  27. };
  28. int i;
  29. struct adapter *adap = mac->adapter;
  30. u32 ctrl = A_XGM_SERDES_CTRL0 + mac->offset;
  31. t3_write_reg(adap, ctrl, adap->params.vpd.xauicfg[macidx(mac)] |
  32. F_RESET3 | F_RESET2 | F_RESET1 | F_RESET0 |
  33. F_PWRDN3 | F_PWRDN2 | F_PWRDN1 | F_PWRDN0 |
  34. F_RESETPLL23 | F_RESETPLL01);
  35. t3_read_reg(adap, ctrl);
  36. udelay(15);
  37. for (i = 0; i < ARRAY_SIZE(clear); i++) {
  38. t3_set_reg_field(adap, ctrl, clear[i], 0);
  39. udelay(15);
  40. }
  41. }
  42. void t3b_pcs_reset(struct cmac *mac)
  43. {
  44. t3_set_reg_field(mac->adapter, A_XGM_RESET_CTRL + mac->offset,
  45. F_PCS_RESET_, 0);
  46. udelay(20);
  47. t3_set_reg_field(mac->adapter, A_XGM_RESET_CTRL + mac->offset, 0,
  48. F_PCS_RESET_);
  49. }
  50. int t3_mac_reset(struct cmac *mac)
  51. {
  52. static const struct addr_val_pair mac_reset_avp[] = {
  53. {A_XGM_TX_CTRL, 0},
  54. {A_XGM_RX_CTRL, 0},
  55. {A_XGM_RX_CFG, F_DISPAUSEFRAMES | F_EN1536BFRAMES |
  56. F_RMFCS | F_ENJUMBO | F_ENHASHMCAST},
  57. {A_XGM_RX_HASH_LOW, 0},
  58. {A_XGM_RX_HASH_HIGH, 0},
  59. {A_XGM_RX_EXACT_MATCH_LOW_1, 0},
  60. {A_XGM_RX_EXACT_MATCH_LOW_2, 0},
  61. {A_XGM_RX_EXACT_MATCH_LOW_3, 0},
  62. {A_XGM_RX_EXACT_MATCH_LOW_4, 0},
  63. {A_XGM_RX_EXACT_MATCH_LOW_5, 0},
  64. {A_XGM_RX_EXACT_MATCH_LOW_6, 0},
  65. {A_XGM_RX_EXACT_MATCH_LOW_7, 0},
  66. {A_XGM_RX_EXACT_MATCH_LOW_8, 0},
  67. {A_XGM_STAT_CTRL, F_CLRSTATS}
  68. };
  69. u32 val;
  70. struct adapter *adap = mac->adapter;
  71. unsigned int oft = mac->offset;
  72. t3_write_reg(adap, A_XGM_RESET_CTRL + oft, F_MAC_RESET_);
  73. t3_read_reg(adap, A_XGM_RESET_CTRL + oft); /* flush */
  74. t3_write_regs(adap, mac_reset_avp, ARRAY_SIZE(mac_reset_avp), oft);
  75. t3_set_reg_field(adap, A_XGM_RXFIFO_CFG + oft,
  76. F_RXSTRFRWRD | F_DISERRFRAMES,
  77. uses_xaui(adap) ? 0 : F_RXSTRFRWRD);
  78. if (uses_xaui(adap)) {
  79. if (adap->params.rev == 0) {
  80. t3_set_reg_field(adap, A_XGM_SERDES_CTRL + oft, 0,
  81. F_RXENABLE | F_TXENABLE);
  82. if (t3_wait_op_done(adap, A_XGM_SERDES_STATUS1 + oft,
  83. F_CMULOCK, 1, 5, 2)) {
  84. CH_ERR(adap,
  85. "MAC %d XAUI SERDES CMU lock failed\n",
  86. macidx(mac));
  87. return -1;
  88. }
  89. t3_set_reg_field(adap, A_XGM_SERDES_CTRL + oft, 0,
  90. F_SERDESRESET_);
  91. } else
  92. xaui_serdes_reset(mac);
  93. }
  94. if (adap->params.rev > 0)
  95. t3_write_reg(adap, A_XGM_PAUSE_TIMER + oft, 0xf000);
  96. val = F_MAC_RESET_;
  97. if (is_10G(adap))
  98. val |= F_PCS_RESET_;
  99. else if (uses_xaui(adap))
  100. val |= F_PCS_RESET_ | F_XG2G_RESET_;
  101. else
  102. val |= F_RGMII_RESET_ | F_XG2G_RESET_;
  103. t3_write_reg(adap, A_XGM_RESET_CTRL + oft, val);
  104. t3_read_reg(adap, A_XGM_RESET_CTRL + oft); /* flush */
  105. if ((val & F_PCS_RESET_) && adap->params.rev) {
  106. msleep(1);
  107. t3b_pcs_reset(mac);
  108. }
  109. memset(&mac->stats, 0, sizeof(mac->stats));
  110. return 0;
  111. }
  112. /*
  113. * Set the exact match register 'idx' to recognize the given Ethernet address.
  114. */
  115. static void set_addr_filter(struct cmac *mac, int idx, const u8 * addr)
  116. {
  117. u32 addr_lo, addr_hi;
  118. unsigned int oft = mac->offset + idx * 8;
  119. addr_lo = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  120. addr_hi = (addr[5] << 8) | addr[4];
  121. t3_write_reg(mac->adapter, A_XGM_RX_EXACT_MATCH_LOW_1 + oft, addr_lo);
  122. t3_write_reg(mac->adapter, A_XGM_RX_EXACT_MATCH_HIGH_1 + oft, addr_hi);
  123. }
  124. /* Set one of the station's unicast MAC addresses. */
  125. int t3_mac_set_address(struct cmac *mac, unsigned int idx, u8 addr[6])
  126. {
  127. if (idx >= mac->nucast)
  128. return -EINVAL;
  129. set_addr_filter(mac, idx, addr);
  130. return 0;
  131. }
  132. /*
  133. * Specify the number of exact address filters that should be reserved for
  134. * unicast addresses. Caller should reload the unicast and multicast addresses
  135. * after calling this.
  136. */
  137. int t3_mac_set_num_ucast(struct cmac *mac, int n)
  138. {
  139. if (n > EXACT_ADDR_FILTERS)
  140. return -EINVAL;
  141. mac->nucast = n;
  142. return 0;
  143. }
  144. /* Calculate the RX hash filter index of an Ethernet address */
  145. static int hash_hw_addr(const u8 * addr)
  146. {
  147. int hash = 0, octet, bit, i = 0, c;
  148. for (octet = 0; octet < 6; ++octet)
  149. for (c = addr[octet], bit = 0; bit < 8; c >>= 1, ++bit) {
  150. hash ^= (c & 1) << i;
  151. if (++i == 6)
  152. i = 0;
  153. }
  154. return hash;
  155. }
  156. int t3_mac_set_rx_mode(struct cmac *mac, struct t3_rx_mode *rm)
  157. {
  158. u32 val, hash_lo, hash_hi;
  159. struct adapter *adap = mac->adapter;
  160. unsigned int oft = mac->offset;
  161. val = t3_read_reg(adap, A_XGM_RX_CFG + oft) & ~F_COPYALLFRAMES;
  162. if (rm->dev->flags & IFF_PROMISC)
  163. val |= F_COPYALLFRAMES;
  164. t3_write_reg(adap, A_XGM_RX_CFG + oft, val);
  165. if (rm->dev->flags & IFF_ALLMULTI)
  166. hash_lo = hash_hi = 0xffffffff;
  167. else {
  168. u8 *addr;
  169. int exact_addr_idx = mac->nucast;
  170. hash_lo = hash_hi = 0;
  171. while ((addr = t3_get_next_mcaddr(rm)))
  172. if (exact_addr_idx < EXACT_ADDR_FILTERS)
  173. set_addr_filter(mac, exact_addr_idx++, addr);
  174. else {
  175. int hash = hash_hw_addr(addr);
  176. if (hash < 32)
  177. hash_lo |= (1 << hash);
  178. else
  179. hash_hi |= (1 << (hash - 32));
  180. }
  181. }
  182. t3_write_reg(adap, A_XGM_RX_HASH_LOW + oft, hash_lo);
  183. t3_write_reg(adap, A_XGM_RX_HASH_HIGH + oft, hash_hi);
  184. return 0;
  185. }
  186. int t3_mac_set_mtu(struct cmac *mac, unsigned int mtu)
  187. {
  188. int hwm, lwm;
  189. unsigned int thres, v;
  190. struct adapter *adap = mac->adapter;
  191. /*
  192. * MAX_FRAME_SIZE inludes header + FCS, mtu doesn't. The HW max
  193. * packet size register includes header, but not FCS.
  194. */
  195. mtu += 14;
  196. if (mtu > MAX_FRAME_SIZE - 4)
  197. return -EINVAL;
  198. t3_write_reg(adap, A_XGM_RX_MAX_PKT_SIZE + mac->offset, mtu);
  199. /*
  200. * Adjust the PAUSE frame watermarks. We always set the LWM, and the
  201. * HWM only if flow-control is enabled.
  202. */
  203. hwm = max(MAC_RXFIFO_SIZE - 3 * mtu, MAC_RXFIFO_SIZE / 2U);
  204. hwm = min(hwm, 3 * MAC_RXFIFO_SIZE / 4 + 1024);
  205. lwm = hwm - 1024;
  206. v = t3_read_reg(adap, A_XGM_RXFIFO_CFG + mac->offset);
  207. v &= ~V_RXFIFOPAUSELWM(M_RXFIFOPAUSELWM);
  208. v |= V_RXFIFOPAUSELWM(lwm / 8);
  209. if (G_RXFIFOPAUSEHWM(v))
  210. v = (v & ~V_RXFIFOPAUSEHWM(M_RXFIFOPAUSEHWM)) |
  211. V_RXFIFOPAUSEHWM(hwm / 8);
  212. t3_write_reg(adap, A_XGM_RXFIFO_CFG + mac->offset, v);
  213. /* Adjust the TX FIFO threshold based on the MTU */
  214. thres = (adap->params.vpd.cclk * 1000) / 15625;
  215. thres = (thres * mtu) / 1000;
  216. if (is_10G(adap))
  217. thres /= 10;
  218. thres = mtu > thres ? (mtu - thres + 7) / 8 : 0;
  219. thres = max(thres, 8U); /* need at least 8 */
  220. t3_set_reg_field(adap, A_XGM_TXFIFO_CFG + mac->offset,
  221. V_TXFIFOTHRESH(M_TXFIFOTHRESH), V_TXFIFOTHRESH(thres));
  222. return 0;
  223. }
  224. int t3_mac_set_speed_duplex_fc(struct cmac *mac, int speed, int duplex, int fc)
  225. {
  226. u32 val;
  227. struct adapter *adap = mac->adapter;
  228. unsigned int oft = mac->offset;
  229. if (duplex >= 0 && duplex != DUPLEX_FULL)
  230. return -EINVAL;
  231. if (speed >= 0) {
  232. if (speed == SPEED_10)
  233. val = V_PORTSPEED(0);
  234. else if (speed == SPEED_100)
  235. val = V_PORTSPEED(1);
  236. else if (speed == SPEED_1000)
  237. val = V_PORTSPEED(2);
  238. else if (speed == SPEED_10000)
  239. val = V_PORTSPEED(3);
  240. else
  241. return -EINVAL;
  242. t3_set_reg_field(adap, A_XGM_PORT_CFG + oft,
  243. V_PORTSPEED(M_PORTSPEED), val);
  244. }
  245. val = t3_read_reg(adap, A_XGM_RXFIFO_CFG + oft);
  246. val &= ~V_RXFIFOPAUSEHWM(M_RXFIFOPAUSEHWM);
  247. if (fc & PAUSE_TX)
  248. val |= V_RXFIFOPAUSEHWM(G_RXFIFOPAUSELWM(val) + 128); /* +1KB */
  249. t3_write_reg(adap, A_XGM_RXFIFO_CFG + oft, val);
  250. t3_set_reg_field(adap, A_XGM_TX_CFG + oft, F_TXPAUSEEN,
  251. (fc & PAUSE_RX) ? F_TXPAUSEEN : 0);
  252. return 0;
  253. }
  254. int t3_mac_enable(struct cmac *mac, int which)
  255. {
  256. int idx = macidx(mac);
  257. struct adapter *adap = mac->adapter;
  258. unsigned int oft = mac->offset;
  259. if (which & MAC_DIRECTION_TX) {
  260. t3_write_reg(adap, A_XGM_TX_CTRL + oft, F_TXEN);
  261. t3_write_reg(adap, A_TP_PIO_ADDR, A_TP_TX_DROP_CFG_CH0 + idx);
  262. t3_write_reg(adap, A_TP_PIO_DATA, 0xbf000001);
  263. t3_write_reg(adap, A_TP_PIO_ADDR, A_TP_TX_DROP_MODE);
  264. t3_set_reg_field(adap, A_TP_PIO_DATA, 1 << idx, 1 << idx);
  265. }
  266. if (which & MAC_DIRECTION_RX)
  267. t3_write_reg(adap, A_XGM_RX_CTRL + oft, F_RXEN);
  268. return 0;
  269. }
  270. int t3_mac_disable(struct cmac *mac, int which)
  271. {
  272. int idx = macidx(mac);
  273. struct adapter *adap = mac->adapter;
  274. if (which & MAC_DIRECTION_TX) {
  275. t3_write_reg(adap, A_XGM_TX_CTRL + mac->offset, 0);
  276. t3_write_reg(adap, A_TP_PIO_ADDR, A_TP_TX_DROP_CFG_CH0 + idx);
  277. t3_write_reg(adap, A_TP_PIO_DATA, 0xc000001f);
  278. t3_write_reg(adap, A_TP_PIO_ADDR, A_TP_TX_DROP_MODE);
  279. t3_set_reg_field(adap, A_TP_PIO_DATA, 1 << idx, 0);
  280. }
  281. if (which & MAC_DIRECTION_RX)
  282. t3_write_reg(adap, A_XGM_RX_CTRL + mac->offset, 0);
  283. return 0;
  284. }
  285. /*
  286. * This function is called periodically to accumulate the current values of the
  287. * RMON counters into the port statistics. Since the packet counters are only
  288. * 32 bits they can overflow in ~286 secs at 10G, so the function should be
  289. * called more frequently than that. The byte counters are 45-bit wide, they
  290. * would overflow in ~7.8 hours.
  291. */
  292. const struct mac_stats *t3_mac_update_stats(struct cmac *mac)
  293. {
  294. #define RMON_READ(mac, addr) t3_read_reg(mac->adapter, addr + mac->offset)
  295. #define RMON_UPDATE(mac, name, reg) \
  296. (mac)->stats.name += (u64)RMON_READ(mac, A_XGM_STAT_##reg)
  297. #define RMON_UPDATE64(mac, name, reg_lo, reg_hi) \
  298. (mac)->stats.name += RMON_READ(mac, A_XGM_STAT_##reg_lo) + \
  299. ((u64)RMON_READ(mac, A_XGM_STAT_##reg_hi) << 32)
  300. u32 v, lo;
  301. RMON_UPDATE64(mac, rx_octets, RX_BYTES_LOW, RX_BYTES_HIGH);
  302. RMON_UPDATE64(mac, rx_frames, RX_FRAMES_LOW, RX_FRAMES_HIGH);
  303. RMON_UPDATE(mac, rx_mcast_frames, RX_MCAST_FRAMES);
  304. RMON_UPDATE(mac, rx_bcast_frames, RX_BCAST_FRAMES);
  305. RMON_UPDATE(mac, rx_fcs_errs, RX_CRC_ERR_FRAMES);
  306. RMON_UPDATE(mac, rx_pause, RX_PAUSE_FRAMES);
  307. RMON_UPDATE(mac, rx_jabber, RX_JABBER_FRAMES);
  308. RMON_UPDATE(mac, rx_short, RX_SHORT_FRAMES);
  309. RMON_UPDATE(mac, rx_symbol_errs, RX_SYM_CODE_ERR_FRAMES);
  310. RMON_UPDATE(mac, rx_too_long, RX_OVERSIZE_FRAMES);
  311. mac->stats.rx_too_long += RMON_READ(mac, A_XGM_RX_MAX_PKT_SIZE_ERR_CNT);
  312. RMON_UPDATE(mac, rx_frames_64, RX_64B_FRAMES);
  313. RMON_UPDATE(mac, rx_frames_65_127, RX_65_127B_FRAMES);
  314. RMON_UPDATE(mac, rx_frames_128_255, RX_128_255B_FRAMES);
  315. RMON_UPDATE(mac, rx_frames_256_511, RX_256_511B_FRAMES);
  316. RMON_UPDATE(mac, rx_frames_512_1023, RX_512_1023B_FRAMES);
  317. RMON_UPDATE(mac, rx_frames_1024_1518, RX_1024_1518B_FRAMES);
  318. RMON_UPDATE(mac, rx_frames_1519_max, RX_1519_MAXB_FRAMES);
  319. RMON_UPDATE64(mac, tx_octets, TX_BYTE_LOW, TX_BYTE_HIGH);
  320. RMON_UPDATE64(mac, tx_frames, TX_FRAME_LOW, TX_FRAME_HIGH);
  321. RMON_UPDATE(mac, tx_mcast_frames, TX_MCAST);
  322. RMON_UPDATE(mac, tx_bcast_frames, TX_BCAST);
  323. RMON_UPDATE(mac, tx_pause, TX_PAUSE);
  324. /* This counts error frames in general (bad FCS, underrun, etc). */
  325. RMON_UPDATE(mac, tx_underrun, TX_ERR_FRAMES);
  326. RMON_UPDATE(mac, tx_frames_64, TX_64B_FRAMES);
  327. RMON_UPDATE(mac, tx_frames_65_127, TX_65_127B_FRAMES);
  328. RMON_UPDATE(mac, tx_frames_128_255, TX_128_255B_FRAMES);
  329. RMON_UPDATE(mac, tx_frames_256_511, TX_256_511B_FRAMES);
  330. RMON_UPDATE(mac, tx_frames_512_1023, TX_512_1023B_FRAMES);
  331. RMON_UPDATE(mac, tx_frames_1024_1518, TX_1024_1518B_FRAMES);
  332. RMON_UPDATE(mac, tx_frames_1519_max, TX_1519_MAXB_FRAMES);
  333. /* The next stat isn't clear-on-read. */
  334. t3_write_reg(mac->adapter, A_TP_MIB_INDEX, mac->offset ? 51 : 50);
  335. v = t3_read_reg(mac->adapter, A_TP_MIB_RDATA);
  336. lo = (u32) mac->stats.rx_cong_drops;
  337. mac->stats.rx_cong_drops += (u64) (v - lo);
  338. return &mac->stats;
  339. }