fman_tgec.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. * Copyright 2008-2015 Freescale Semiconductor Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of Freescale Semiconductor nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. *
  16. * ALTERNATIVELY, this software may be distributed under the terms of the
  17. * GNU General Public License ("GPL") as published by the Free Software
  18. * Foundation, either version 2 of that License or (at your option) any
  19. * later version.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
  22. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
  25. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include "fman_tgec.h"
  34. #include "fman.h"
  35. #include <linux/slab.h>
  36. #include <linux/bitrev.h>
  37. #include <linux/io.h>
  38. #include <linux/crc32.h>
  39. /* Transmit Inter-Packet Gap Length Register (TX_IPG_LENGTH) */
  40. #define TGEC_TX_IPG_LENGTH_MASK 0x000003ff
  41. /* Command and Configuration Register (COMMAND_CONFIG) */
  42. #define CMD_CFG_EN_TIMESTAMP 0x00100000
  43. #define CMD_CFG_NO_LEN_CHK 0x00020000
  44. #define CMD_CFG_PAUSE_IGNORE 0x00000100
  45. #define CMF_CFG_CRC_FWD 0x00000040
  46. #define CMD_CFG_PROMIS_EN 0x00000010
  47. #define CMD_CFG_RX_EN 0x00000002
  48. #define CMD_CFG_TX_EN 0x00000001
  49. /* Interrupt Mask Register (IMASK) */
  50. #define TGEC_IMASK_MDIO_SCAN_EVENT 0x00010000
  51. #define TGEC_IMASK_MDIO_CMD_CMPL 0x00008000
  52. #define TGEC_IMASK_REM_FAULT 0x00004000
  53. #define TGEC_IMASK_LOC_FAULT 0x00002000
  54. #define TGEC_IMASK_TX_ECC_ER 0x00001000
  55. #define TGEC_IMASK_TX_FIFO_UNFL 0x00000800
  56. #define TGEC_IMASK_TX_FIFO_OVFL 0x00000400
  57. #define TGEC_IMASK_TX_ER 0x00000200
  58. #define TGEC_IMASK_RX_FIFO_OVFL 0x00000100
  59. #define TGEC_IMASK_RX_ECC_ER 0x00000080
  60. #define TGEC_IMASK_RX_JAB_FRM 0x00000040
  61. #define TGEC_IMASK_RX_OVRSZ_FRM 0x00000020
  62. #define TGEC_IMASK_RX_RUNT_FRM 0x00000010
  63. #define TGEC_IMASK_RX_FRAG_FRM 0x00000008
  64. #define TGEC_IMASK_RX_LEN_ER 0x00000004
  65. #define TGEC_IMASK_RX_CRC_ER 0x00000002
  66. #define TGEC_IMASK_RX_ALIGN_ER 0x00000001
  67. /* Hashtable Control Register (HASHTABLE_CTRL) */
  68. #define TGEC_HASH_MCAST_SHIFT 23
  69. #define TGEC_HASH_MCAST_EN 0x00000200
  70. #define TGEC_HASH_ADR_MSK 0x000001ff
  71. #define DEFAULT_TX_IPG_LENGTH 12
  72. #define DEFAULT_MAX_FRAME_LENGTH 0x600
  73. #define DEFAULT_PAUSE_QUANT 0xf000
  74. /* number of pattern match registers (entries) */
  75. #define TGEC_NUM_OF_PADDRS 1
  76. /* Group address bit indication */
  77. #define GROUP_ADDRESS 0x0000010000000000LL
  78. /* Hash table size (= 32 bits*8 regs) */
  79. #define TGEC_HASH_TABLE_SIZE 512
  80. /* tGEC memory map */
  81. struct tgec_regs {
  82. u32 tgec_id; /* 0x000 Controller ID */
  83. u32 reserved001[1]; /* 0x004 */
  84. u32 command_config; /* 0x008 Control and configuration */
  85. u32 mac_addr_0; /* 0x00c Lower 32 bits of the MAC adr */
  86. u32 mac_addr_1; /* 0x010 Upper 16 bits of the MAC adr */
  87. u32 maxfrm; /* 0x014 Maximum frame length */
  88. u32 pause_quant; /* 0x018 Pause quanta */
  89. u32 rx_fifo_sections; /* 0x01c */
  90. u32 tx_fifo_sections; /* 0x020 */
  91. u32 rx_fifo_almost_f_e; /* 0x024 */
  92. u32 tx_fifo_almost_f_e; /* 0x028 */
  93. u32 hashtable_ctrl; /* 0x02c Hash table control */
  94. u32 mdio_cfg_status; /* 0x030 */
  95. u32 mdio_command; /* 0x034 */
  96. u32 mdio_data; /* 0x038 */
  97. u32 mdio_regaddr; /* 0x03c */
  98. u32 status; /* 0x040 */
  99. u32 tx_ipg_len; /* 0x044 Transmitter inter-packet-gap */
  100. u32 mac_addr_2; /* 0x048 Lower 32 bits of 2nd MAC adr */
  101. u32 mac_addr_3; /* 0x04c Upper 16 bits of 2nd MAC adr */
  102. u32 rx_fifo_ptr_rd; /* 0x050 */
  103. u32 rx_fifo_ptr_wr; /* 0x054 */
  104. u32 tx_fifo_ptr_rd; /* 0x058 */
  105. u32 tx_fifo_ptr_wr; /* 0x05c */
  106. u32 imask; /* 0x060 Interrupt mask */
  107. u32 ievent; /* 0x064 Interrupt event */
  108. u32 udp_port; /* 0x068 Defines a UDP Port number */
  109. u32 type_1588v2; /* 0x06c Type field for 1588v2 */
  110. u32 reserved070[4]; /* 0x070 */
  111. /* 10Ge Statistics Counter */
  112. u32 tfrm_u; /* 80 aFramesTransmittedOK */
  113. u32 tfrm_l; /* 84 aFramesTransmittedOK */
  114. u32 rfrm_u; /* 88 aFramesReceivedOK */
  115. u32 rfrm_l; /* 8c aFramesReceivedOK */
  116. u32 rfcs_u; /* 90 aFrameCheckSequenceErrors */
  117. u32 rfcs_l; /* 94 aFrameCheckSequenceErrors */
  118. u32 raln_u; /* 98 aAlignmentErrors */
  119. u32 raln_l; /* 9c aAlignmentErrors */
  120. u32 txpf_u; /* A0 aPAUSEMACCtrlFramesTransmitted */
  121. u32 txpf_l; /* A4 aPAUSEMACCtrlFramesTransmitted */
  122. u32 rxpf_u; /* A8 aPAUSEMACCtrlFramesReceived */
  123. u32 rxpf_l; /* Ac aPAUSEMACCtrlFramesReceived */
  124. u32 rlong_u; /* B0 aFrameTooLongErrors */
  125. u32 rlong_l; /* B4 aFrameTooLongErrors */
  126. u32 rflr_u; /* B8 aInRangeLengthErrors */
  127. u32 rflr_l; /* Bc aInRangeLengthErrors */
  128. u32 tvlan_u; /* C0 VLANTransmittedOK */
  129. u32 tvlan_l; /* C4 VLANTransmittedOK */
  130. u32 rvlan_u; /* C8 VLANReceivedOK */
  131. u32 rvlan_l; /* Cc VLANReceivedOK */
  132. u32 toct_u; /* D0 if_out_octets */
  133. u32 toct_l; /* D4 if_out_octets */
  134. u32 roct_u; /* D8 if_in_octets */
  135. u32 roct_l; /* Dc if_in_octets */
  136. u32 ruca_u; /* E0 if_in_ucast_pkts */
  137. u32 ruca_l; /* E4 if_in_ucast_pkts */
  138. u32 rmca_u; /* E8 ifInMulticastPkts */
  139. u32 rmca_l; /* Ec ifInMulticastPkts */
  140. u32 rbca_u; /* F0 ifInBroadcastPkts */
  141. u32 rbca_l; /* F4 ifInBroadcastPkts */
  142. u32 terr_u; /* F8 if_out_errors */
  143. u32 terr_l; /* Fc if_out_errors */
  144. u32 reserved100[2]; /* 100-108 */
  145. u32 tuca_u; /* 108 if_out_ucast_pkts */
  146. u32 tuca_l; /* 10c if_out_ucast_pkts */
  147. u32 tmca_u; /* 110 ifOutMulticastPkts */
  148. u32 tmca_l; /* 114 ifOutMulticastPkts */
  149. u32 tbca_u; /* 118 ifOutBroadcastPkts */
  150. u32 tbca_l; /* 11c ifOutBroadcastPkts */
  151. u32 rdrp_u; /* 120 etherStatsDropEvents */
  152. u32 rdrp_l; /* 124 etherStatsDropEvents */
  153. u32 reoct_u; /* 128 etherStatsOctets */
  154. u32 reoct_l; /* 12c etherStatsOctets */
  155. u32 rpkt_u; /* 130 etherStatsPkts */
  156. u32 rpkt_l; /* 134 etherStatsPkts */
  157. u32 trund_u; /* 138 etherStatsUndersizePkts */
  158. u32 trund_l; /* 13c etherStatsUndersizePkts */
  159. u32 r64_u; /* 140 etherStatsPkts64Octets */
  160. u32 r64_l; /* 144 etherStatsPkts64Octets */
  161. u32 r127_u; /* 148 etherStatsPkts65to127Octets */
  162. u32 r127_l; /* 14c etherStatsPkts65to127Octets */
  163. u32 r255_u; /* 150 etherStatsPkts128to255Octets */
  164. u32 r255_l; /* 154 etherStatsPkts128to255Octets */
  165. u32 r511_u; /* 158 etherStatsPkts256to511Octets */
  166. u32 r511_l; /* 15c etherStatsPkts256to511Octets */
  167. u32 r1023_u; /* 160 etherStatsPkts512to1023Octets */
  168. u32 r1023_l; /* 164 etherStatsPkts512to1023Octets */
  169. u32 r1518_u; /* 168 etherStatsPkts1024to1518Octets */
  170. u32 r1518_l; /* 16c etherStatsPkts1024to1518Octets */
  171. u32 r1519x_u; /* 170 etherStatsPkts1519toX */
  172. u32 r1519x_l; /* 174 etherStatsPkts1519toX */
  173. u32 trovr_u; /* 178 etherStatsOversizePkts */
  174. u32 trovr_l; /* 17c etherStatsOversizePkts */
  175. u32 trjbr_u; /* 180 etherStatsJabbers */
  176. u32 trjbr_l; /* 184 etherStatsJabbers */
  177. u32 trfrg_u; /* 188 etherStatsFragments */
  178. u32 trfrg_l; /* 18C etherStatsFragments */
  179. u32 rerr_u; /* 190 if_in_errors */
  180. u32 rerr_l; /* 194 if_in_errors */
  181. };
  182. struct tgec_cfg {
  183. bool pause_ignore;
  184. bool promiscuous_mode_enable;
  185. u16 max_frame_length;
  186. u16 pause_quant;
  187. u32 tx_ipg_length;
  188. };
  189. struct fman_mac {
  190. /* Pointer to the memory mapped registers. */
  191. struct tgec_regs __iomem *regs;
  192. /* MAC address of device; */
  193. u64 addr;
  194. u16 max_speed;
  195. void *dev_id; /* device cookie used by the exception cbs */
  196. fman_mac_exception_cb *exception_cb;
  197. fman_mac_exception_cb *event_cb;
  198. /* pointer to driver's global address hash table */
  199. struct eth_hash_t *multicast_addr_hash;
  200. /* pointer to driver's individual address hash table */
  201. struct eth_hash_t *unicast_addr_hash;
  202. u8 mac_id;
  203. u32 exceptions;
  204. struct tgec_cfg *cfg;
  205. void *fm;
  206. struct fman_rev_info fm_rev_info;
  207. bool allmulti_enabled;
  208. };
  209. static void set_mac_address(struct tgec_regs __iomem *regs, u8 *adr)
  210. {
  211. u32 tmp0, tmp1;
  212. tmp0 = (u32)(adr[0] | adr[1] << 8 | adr[2] << 16 | adr[3] << 24);
  213. tmp1 = (u32)(adr[4] | adr[5] << 8);
  214. iowrite32be(tmp0, &regs->mac_addr_0);
  215. iowrite32be(tmp1, &regs->mac_addr_1);
  216. }
  217. static void set_dflts(struct tgec_cfg *cfg)
  218. {
  219. cfg->promiscuous_mode_enable = false;
  220. cfg->pause_ignore = false;
  221. cfg->tx_ipg_length = DEFAULT_TX_IPG_LENGTH;
  222. cfg->max_frame_length = DEFAULT_MAX_FRAME_LENGTH;
  223. cfg->pause_quant = DEFAULT_PAUSE_QUANT;
  224. }
  225. static int init(struct tgec_regs __iomem *regs, struct tgec_cfg *cfg,
  226. u32 exception_mask)
  227. {
  228. u32 tmp;
  229. /* Config */
  230. tmp = CMF_CFG_CRC_FWD;
  231. if (cfg->promiscuous_mode_enable)
  232. tmp |= CMD_CFG_PROMIS_EN;
  233. if (cfg->pause_ignore)
  234. tmp |= CMD_CFG_PAUSE_IGNORE;
  235. /* Payload length check disable */
  236. tmp |= CMD_CFG_NO_LEN_CHK;
  237. iowrite32be(tmp, &regs->command_config);
  238. /* Max Frame Length */
  239. iowrite32be((u32)cfg->max_frame_length, &regs->maxfrm);
  240. /* Pause Time */
  241. iowrite32be(cfg->pause_quant, &regs->pause_quant);
  242. /* clear all pending events and set-up interrupts */
  243. iowrite32be(0xffffffff, &regs->ievent);
  244. iowrite32be(ioread32be(&regs->imask) | exception_mask, &regs->imask);
  245. return 0;
  246. }
  247. static int check_init_parameters(struct fman_mac *tgec)
  248. {
  249. if (tgec->max_speed < SPEED_10000) {
  250. pr_err("10G MAC driver only support 10G speed\n");
  251. return -EINVAL;
  252. }
  253. if (tgec->addr == 0) {
  254. pr_err("Ethernet 10G MAC Must have valid MAC Address\n");
  255. return -EINVAL;
  256. }
  257. if (!tgec->exception_cb) {
  258. pr_err("uninitialized exception_cb\n");
  259. return -EINVAL;
  260. }
  261. if (!tgec->event_cb) {
  262. pr_err("uninitialized event_cb\n");
  263. return -EINVAL;
  264. }
  265. return 0;
  266. }
  267. static int get_exception_flag(enum fman_mac_exceptions exception)
  268. {
  269. u32 bit_mask;
  270. switch (exception) {
  271. case FM_MAC_EX_10G_MDIO_SCAN_EVENT:
  272. bit_mask = TGEC_IMASK_MDIO_SCAN_EVENT;
  273. break;
  274. case FM_MAC_EX_10G_MDIO_CMD_CMPL:
  275. bit_mask = TGEC_IMASK_MDIO_CMD_CMPL;
  276. break;
  277. case FM_MAC_EX_10G_REM_FAULT:
  278. bit_mask = TGEC_IMASK_REM_FAULT;
  279. break;
  280. case FM_MAC_EX_10G_LOC_FAULT:
  281. bit_mask = TGEC_IMASK_LOC_FAULT;
  282. break;
  283. case FM_MAC_EX_10G_TX_ECC_ER:
  284. bit_mask = TGEC_IMASK_TX_ECC_ER;
  285. break;
  286. case FM_MAC_EX_10G_TX_FIFO_UNFL:
  287. bit_mask = TGEC_IMASK_TX_FIFO_UNFL;
  288. break;
  289. case FM_MAC_EX_10G_TX_FIFO_OVFL:
  290. bit_mask = TGEC_IMASK_TX_FIFO_OVFL;
  291. break;
  292. case FM_MAC_EX_10G_TX_ER:
  293. bit_mask = TGEC_IMASK_TX_ER;
  294. break;
  295. case FM_MAC_EX_10G_RX_FIFO_OVFL:
  296. bit_mask = TGEC_IMASK_RX_FIFO_OVFL;
  297. break;
  298. case FM_MAC_EX_10G_RX_ECC_ER:
  299. bit_mask = TGEC_IMASK_RX_ECC_ER;
  300. break;
  301. case FM_MAC_EX_10G_RX_JAB_FRM:
  302. bit_mask = TGEC_IMASK_RX_JAB_FRM;
  303. break;
  304. case FM_MAC_EX_10G_RX_OVRSZ_FRM:
  305. bit_mask = TGEC_IMASK_RX_OVRSZ_FRM;
  306. break;
  307. case FM_MAC_EX_10G_RX_RUNT_FRM:
  308. bit_mask = TGEC_IMASK_RX_RUNT_FRM;
  309. break;
  310. case FM_MAC_EX_10G_RX_FRAG_FRM:
  311. bit_mask = TGEC_IMASK_RX_FRAG_FRM;
  312. break;
  313. case FM_MAC_EX_10G_RX_LEN_ER:
  314. bit_mask = TGEC_IMASK_RX_LEN_ER;
  315. break;
  316. case FM_MAC_EX_10G_RX_CRC_ER:
  317. bit_mask = TGEC_IMASK_RX_CRC_ER;
  318. break;
  319. case FM_MAC_EX_10G_RX_ALIGN_ER:
  320. bit_mask = TGEC_IMASK_RX_ALIGN_ER;
  321. break;
  322. default:
  323. bit_mask = 0;
  324. break;
  325. }
  326. return bit_mask;
  327. }
  328. static void tgec_err_exception(void *handle)
  329. {
  330. struct fman_mac *tgec = (struct fman_mac *)handle;
  331. struct tgec_regs __iomem *regs = tgec->regs;
  332. u32 event;
  333. /* do not handle MDIO events */
  334. event = ioread32be(&regs->ievent) &
  335. ~(TGEC_IMASK_MDIO_SCAN_EVENT |
  336. TGEC_IMASK_MDIO_CMD_CMPL);
  337. event &= ioread32be(&regs->imask);
  338. iowrite32be(event, &regs->ievent);
  339. if (event & TGEC_IMASK_REM_FAULT)
  340. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_REM_FAULT);
  341. if (event & TGEC_IMASK_LOC_FAULT)
  342. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_LOC_FAULT);
  343. if (event & TGEC_IMASK_TX_ECC_ER)
  344. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_TX_ECC_ER);
  345. if (event & TGEC_IMASK_TX_FIFO_UNFL)
  346. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_TX_FIFO_UNFL);
  347. if (event & TGEC_IMASK_TX_FIFO_OVFL)
  348. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_TX_FIFO_OVFL);
  349. if (event & TGEC_IMASK_TX_ER)
  350. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_TX_ER);
  351. if (event & TGEC_IMASK_RX_FIFO_OVFL)
  352. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_RX_FIFO_OVFL);
  353. if (event & TGEC_IMASK_RX_ECC_ER)
  354. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_RX_ECC_ER);
  355. if (event & TGEC_IMASK_RX_JAB_FRM)
  356. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_RX_JAB_FRM);
  357. if (event & TGEC_IMASK_RX_OVRSZ_FRM)
  358. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_RX_OVRSZ_FRM);
  359. if (event & TGEC_IMASK_RX_RUNT_FRM)
  360. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_RX_RUNT_FRM);
  361. if (event & TGEC_IMASK_RX_FRAG_FRM)
  362. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_RX_FRAG_FRM);
  363. if (event & TGEC_IMASK_RX_LEN_ER)
  364. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_RX_LEN_ER);
  365. if (event & TGEC_IMASK_RX_CRC_ER)
  366. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_RX_CRC_ER);
  367. if (event & TGEC_IMASK_RX_ALIGN_ER)
  368. tgec->exception_cb(tgec->dev_id, FM_MAC_EX_10G_RX_ALIGN_ER);
  369. }
  370. static void free_init_resources(struct fman_mac *tgec)
  371. {
  372. fman_unregister_intr(tgec->fm, FMAN_MOD_MAC, tgec->mac_id,
  373. FMAN_INTR_TYPE_ERR);
  374. /* release the driver's group hash table */
  375. free_hash_table(tgec->multicast_addr_hash);
  376. tgec->multicast_addr_hash = NULL;
  377. /* release the driver's individual hash table */
  378. free_hash_table(tgec->unicast_addr_hash);
  379. tgec->unicast_addr_hash = NULL;
  380. }
  381. static bool is_init_done(struct tgec_cfg *cfg)
  382. {
  383. /* Checks if tGEC driver parameters were initialized */
  384. if (!cfg)
  385. return true;
  386. return false;
  387. }
  388. int tgec_enable(struct fman_mac *tgec, enum comm_mode mode)
  389. {
  390. struct tgec_regs __iomem *regs = tgec->regs;
  391. u32 tmp;
  392. if (!is_init_done(tgec->cfg))
  393. return -EINVAL;
  394. tmp = ioread32be(&regs->command_config);
  395. if (mode & COMM_MODE_RX)
  396. tmp |= CMD_CFG_RX_EN;
  397. if (mode & COMM_MODE_TX)
  398. tmp |= CMD_CFG_TX_EN;
  399. iowrite32be(tmp, &regs->command_config);
  400. return 0;
  401. }
  402. int tgec_disable(struct fman_mac *tgec, enum comm_mode mode)
  403. {
  404. struct tgec_regs __iomem *regs = tgec->regs;
  405. u32 tmp;
  406. if (!is_init_done(tgec->cfg))
  407. return -EINVAL;
  408. tmp = ioread32be(&regs->command_config);
  409. if (mode & COMM_MODE_RX)
  410. tmp &= ~CMD_CFG_RX_EN;
  411. if (mode & COMM_MODE_TX)
  412. tmp &= ~CMD_CFG_TX_EN;
  413. iowrite32be(tmp, &regs->command_config);
  414. return 0;
  415. }
  416. int tgec_set_promiscuous(struct fman_mac *tgec, bool new_val)
  417. {
  418. struct tgec_regs __iomem *regs = tgec->regs;
  419. u32 tmp;
  420. if (!is_init_done(tgec->cfg))
  421. return -EINVAL;
  422. tmp = ioread32be(&regs->command_config);
  423. if (new_val)
  424. tmp |= CMD_CFG_PROMIS_EN;
  425. else
  426. tmp &= ~CMD_CFG_PROMIS_EN;
  427. iowrite32be(tmp, &regs->command_config);
  428. return 0;
  429. }
  430. int tgec_cfg_max_frame_len(struct fman_mac *tgec, u16 new_val)
  431. {
  432. if (is_init_done(tgec->cfg))
  433. return -EINVAL;
  434. tgec->cfg->max_frame_length = new_val;
  435. return 0;
  436. }
  437. int tgec_set_tx_pause_frames(struct fman_mac *tgec, u8 __maybe_unused priority,
  438. u16 pause_time, u16 __maybe_unused thresh_time)
  439. {
  440. struct tgec_regs __iomem *regs = tgec->regs;
  441. if (!is_init_done(tgec->cfg))
  442. return -EINVAL;
  443. iowrite32be((u32)pause_time, &regs->pause_quant);
  444. return 0;
  445. }
  446. int tgec_accept_rx_pause_frames(struct fman_mac *tgec, bool en)
  447. {
  448. struct tgec_regs __iomem *regs = tgec->regs;
  449. u32 tmp;
  450. if (!is_init_done(tgec->cfg))
  451. return -EINVAL;
  452. tmp = ioread32be(&regs->command_config);
  453. if (!en)
  454. tmp |= CMD_CFG_PAUSE_IGNORE;
  455. else
  456. tmp &= ~CMD_CFG_PAUSE_IGNORE;
  457. iowrite32be(tmp, &regs->command_config);
  458. return 0;
  459. }
  460. int tgec_modify_mac_address(struct fman_mac *tgec, enet_addr_t *p_enet_addr)
  461. {
  462. if (!is_init_done(tgec->cfg))
  463. return -EINVAL;
  464. tgec->addr = ENET_ADDR_TO_UINT64(*p_enet_addr);
  465. set_mac_address(tgec->regs, (u8 *)(*p_enet_addr));
  466. return 0;
  467. }
  468. int tgec_add_hash_mac_address(struct fman_mac *tgec, enet_addr_t *eth_addr)
  469. {
  470. struct tgec_regs __iomem *regs = tgec->regs;
  471. struct eth_hash_entry *hash_entry;
  472. u32 crc = 0xFFFFFFFF, hash;
  473. u64 addr;
  474. if (!is_init_done(tgec->cfg))
  475. return -EINVAL;
  476. addr = ENET_ADDR_TO_UINT64(*eth_addr);
  477. if (!(addr & GROUP_ADDRESS)) {
  478. /* Unicast addresses not supported in hash */
  479. pr_err("Unicast Address\n");
  480. return -EINVAL;
  481. }
  482. /* CRC calculation */
  483. crc = crc32_le(crc, (u8 *)eth_addr, ETH_ALEN);
  484. crc = bitrev32(crc);
  485. /* Take 9 MSB bits */
  486. hash = (crc >> TGEC_HASH_MCAST_SHIFT) & TGEC_HASH_ADR_MSK;
  487. /* Create element to be added to the driver hash table */
  488. hash_entry = kmalloc(sizeof(*hash_entry), GFP_ATOMIC);
  489. if (!hash_entry)
  490. return -ENOMEM;
  491. hash_entry->addr = addr;
  492. INIT_LIST_HEAD(&hash_entry->node);
  493. list_add_tail(&hash_entry->node,
  494. &tgec->multicast_addr_hash->lsts[hash]);
  495. iowrite32be((hash | TGEC_HASH_MCAST_EN), &regs->hashtable_ctrl);
  496. return 0;
  497. }
  498. int tgec_set_allmulti(struct fman_mac *tgec, bool enable)
  499. {
  500. u32 entry;
  501. struct tgec_regs __iomem *regs = tgec->regs;
  502. if (!is_init_done(tgec->cfg))
  503. return -EINVAL;
  504. if (enable) {
  505. for (entry = 0; entry < TGEC_HASH_TABLE_SIZE; entry++)
  506. iowrite32be(entry | TGEC_HASH_MCAST_EN,
  507. &regs->hashtable_ctrl);
  508. } else {
  509. for (entry = 0; entry < TGEC_HASH_TABLE_SIZE; entry++)
  510. iowrite32be(entry & ~TGEC_HASH_MCAST_EN,
  511. &regs->hashtable_ctrl);
  512. }
  513. tgec->allmulti_enabled = enable;
  514. return 0;
  515. }
  516. int tgec_set_tstamp(struct fman_mac *tgec, bool enable)
  517. {
  518. struct tgec_regs __iomem *regs = tgec->regs;
  519. u32 tmp;
  520. if (!is_init_done(tgec->cfg))
  521. return -EINVAL;
  522. tmp = ioread32be(&regs->command_config);
  523. if (enable)
  524. tmp |= CMD_CFG_EN_TIMESTAMP;
  525. else
  526. tmp &= ~CMD_CFG_EN_TIMESTAMP;
  527. iowrite32be(tmp, &regs->command_config);
  528. return 0;
  529. }
  530. int tgec_del_hash_mac_address(struct fman_mac *tgec, enet_addr_t *eth_addr)
  531. {
  532. struct tgec_regs __iomem *regs = tgec->regs;
  533. struct eth_hash_entry *hash_entry = NULL;
  534. struct list_head *pos;
  535. u32 crc = 0xFFFFFFFF, hash;
  536. u64 addr;
  537. if (!is_init_done(tgec->cfg))
  538. return -EINVAL;
  539. addr = ((*(u64 *)eth_addr) >> 16);
  540. /* CRC calculation */
  541. crc = crc32_le(crc, (u8 *)eth_addr, ETH_ALEN);
  542. crc = bitrev32(crc);
  543. /* Take 9 MSB bits */
  544. hash = (crc >> TGEC_HASH_MCAST_SHIFT) & TGEC_HASH_ADR_MSK;
  545. list_for_each(pos, &tgec->multicast_addr_hash->lsts[hash]) {
  546. hash_entry = ETH_HASH_ENTRY_OBJ(pos);
  547. if (hash_entry->addr == addr) {
  548. list_del_init(&hash_entry->node);
  549. kfree(hash_entry);
  550. break;
  551. }
  552. }
  553. if (!tgec->allmulti_enabled) {
  554. if (list_empty(&tgec->multicast_addr_hash->lsts[hash]))
  555. iowrite32be((hash & ~TGEC_HASH_MCAST_EN),
  556. &regs->hashtable_ctrl);
  557. }
  558. return 0;
  559. }
  560. int tgec_get_version(struct fman_mac *tgec, u32 *mac_version)
  561. {
  562. struct tgec_regs __iomem *regs = tgec->regs;
  563. if (!is_init_done(tgec->cfg))
  564. return -EINVAL;
  565. *mac_version = ioread32be(&regs->tgec_id);
  566. return 0;
  567. }
  568. int tgec_set_exception(struct fman_mac *tgec,
  569. enum fman_mac_exceptions exception, bool enable)
  570. {
  571. struct tgec_regs __iomem *regs = tgec->regs;
  572. u32 bit_mask = 0;
  573. if (!is_init_done(tgec->cfg))
  574. return -EINVAL;
  575. bit_mask = get_exception_flag(exception);
  576. if (bit_mask) {
  577. if (enable)
  578. tgec->exceptions |= bit_mask;
  579. else
  580. tgec->exceptions &= ~bit_mask;
  581. } else {
  582. pr_err("Undefined exception\n");
  583. return -EINVAL;
  584. }
  585. if (enable)
  586. iowrite32be(ioread32be(&regs->imask) | bit_mask, &regs->imask);
  587. else
  588. iowrite32be(ioread32be(&regs->imask) & ~bit_mask, &regs->imask);
  589. return 0;
  590. }
  591. int tgec_init(struct fman_mac *tgec)
  592. {
  593. struct tgec_cfg *cfg;
  594. enet_addr_t eth_addr;
  595. int err;
  596. if (is_init_done(tgec->cfg))
  597. return -EINVAL;
  598. if (DEFAULT_RESET_ON_INIT &&
  599. (fman_reset_mac(tgec->fm, tgec->mac_id) != 0)) {
  600. pr_err("Can't reset MAC!\n");
  601. return -EINVAL;
  602. }
  603. err = check_init_parameters(tgec);
  604. if (err)
  605. return err;
  606. cfg = tgec->cfg;
  607. MAKE_ENET_ADDR_FROM_UINT64(tgec->addr, eth_addr);
  608. set_mac_address(tgec->regs, (u8 *)eth_addr);
  609. /* interrupts */
  610. /* FM_10G_REM_N_LCL_FLT_EX_10GMAC_ERRATA_SW005 Errata workaround */
  611. if (tgec->fm_rev_info.major <= 2)
  612. tgec->exceptions &= ~(TGEC_IMASK_REM_FAULT |
  613. TGEC_IMASK_LOC_FAULT);
  614. err = init(tgec->regs, cfg, tgec->exceptions);
  615. if (err) {
  616. free_init_resources(tgec);
  617. pr_err("TGEC version doesn't support this i/f mode\n");
  618. return err;
  619. }
  620. /* Max Frame Length */
  621. err = fman_set_mac_max_frame(tgec->fm, tgec->mac_id,
  622. cfg->max_frame_length);
  623. if (err) {
  624. pr_err("Setting max frame length FAILED\n");
  625. free_init_resources(tgec);
  626. return -EINVAL;
  627. }
  628. /* FM_TX_FIFO_CORRUPTION_ERRATA_10GMAC_A007 Errata workaround */
  629. if (tgec->fm_rev_info.major == 2) {
  630. struct tgec_regs __iomem *regs = tgec->regs;
  631. u32 tmp;
  632. /* restore the default tx ipg Length */
  633. tmp = (ioread32be(&regs->tx_ipg_len) &
  634. ~TGEC_TX_IPG_LENGTH_MASK) | 12;
  635. iowrite32be(tmp, &regs->tx_ipg_len);
  636. }
  637. tgec->multicast_addr_hash = alloc_hash_table(TGEC_HASH_TABLE_SIZE);
  638. if (!tgec->multicast_addr_hash) {
  639. free_init_resources(tgec);
  640. pr_err("allocation hash table is FAILED\n");
  641. return -ENOMEM;
  642. }
  643. tgec->unicast_addr_hash = alloc_hash_table(TGEC_HASH_TABLE_SIZE);
  644. if (!tgec->unicast_addr_hash) {
  645. free_init_resources(tgec);
  646. pr_err("allocation hash table is FAILED\n");
  647. return -ENOMEM;
  648. }
  649. fman_register_intr(tgec->fm, FMAN_MOD_MAC, tgec->mac_id,
  650. FMAN_INTR_TYPE_ERR, tgec_err_exception, tgec);
  651. kfree(cfg);
  652. tgec->cfg = NULL;
  653. return 0;
  654. }
  655. int tgec_free(struct fman_mac *tgec)
  656. {
  657. free_init_resources(tgec);
  658. kfree(tgec->cfg);
  659. kfree(tgec);
  660. return 0;
  661. }
  662. struct fman_mac *tgec_config(struct fman_mac_params *params)
  663. {
  664. struct fman_mac *tgec;
  665. struct tgec_cfg *cfg;
  666. void __iomem *base_addr;
  667. base_addr = params->base_addr;
  668. /* allocate memory for the UCC GETH data structure. */
  669. tgec = kzalloc(sizeof(*tgec), GFP_KERNEL);
  670. if (!tgec)
  671. return NULL;
  672. /* allocate memory for the 10G MAC driver parameters data structure. */
  673. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  674. if (!cfg) {
  675. tgec_free(tgec);
  676. return NULL;
  677. }
  678. /* Plant parameter structure pointer */
  679. tgec->cfg = cfg;
  680. set_dflts(cfg);
  681. tgec->regs = base_addr;
  682. tgec->addr = ENET_ADDR_TO_UINT64(params->addr);
  683. tgec->max_speed = params->max_speed;
  684. tgec->mac_id = params->mac_id;
  685. tgec->exceptions = (TGEC_IMASK_MDIO_SCAN_EVENT |
  686. TGEC_IMASK_REM_FAULT |
  687. TGEC_IMASK_LOC_FAULT |
  688. TGEC_IMASK_TX_ECC_ER |
  689. TGEC_IMASK_TX_FIFO_UNFL |
  690. TGEC_IMASK_TX_FIFO_OVFL |
  691. TGEC_IMASK_TX_ER |
  692. TGEC_IMASK_RX_FIFO_OVFL |
  693. TGEC_IMASK_RX_ECC_ER |
  694. TGEC_IMASK_RX_JAB_FRM |
  695. TGEC_IMASK_RX_OVRSZ_FRM |
  696. TGEC_IMASK_RX_RUNT_FRM |
  697. TGEC_IMASK_RX_FRAG_FRM |
  698. TGEC_IMASK_RX_CRC_ER |
  699. TGEC_IMASK_RX_ALIGN_ER);
  700. tgec->exception_cb = params->exception_cb;
  701. tgec->event_cb = params->event_cb;
  702. tgec->dev_id = params->dev_id;
  703. tgec->fm = params->fm;
  704. /* Save FMan revision */
  705. fman_get_revision(tgec->fm, &tgec->fm_rev_info);
  706. return tgec;
  707. }