emac-mac.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. /* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. /* Qualcomm Technologies, Inc. EMAC Ethernet Controller MAC layer support
  13. */
  14. #include <linux/tcp.h>
  15. #include <linux/ip.h>
  16. #include <linux/ipv6.h>
  17. #include <linux/crc32.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/phy.h>
  21. #include <linux/of.h>
  22. #include <net/ip6_checksum.h>
  23. #include "emac.h"
  24. #include "emac-sgmii.h"
  25. /* EMAC_MAC_CTRL */
  26. #define SINGLE_PAUSE_MODE 0x10000000
  27. #define DEBUG_MODE 0x08000000
  28. #define BROAD_EN 0x04000000
  29. #define MULTI_ALL 0x02000000
  30. #define RX_CHKSUM_EN 0x01000000
  31. #define HUGE 0x00800000
  32. #define SPEED(x) (((x) & 0x3) << 20)
  33. #define SPEED_MASK SPEED(0x3)
  34. #define SIMR 0x00080000
  35. #define TPAUSE 0x00010000
  36. #define PROM_MODE 0x00008000
  37. #define VLAN_STRIP 0x00004000
  38. #define PRLEN_BMSK 0x00003c00
  39. #define PRLEN_SHFT 10
  40. #define HUGEN 0x00000200
  41. #define FLCHK 0x00000100
  42. #define PCRCE 0x00000080
  43. #define CRCE 0x00000040
  44. #define FULLD 0x00000020
  45. #define MAC_LP_EN 0x00000010
  46. #define RXFC 0x00000008
  47. #define TXFC 0x00000004
  48. #define RXEN 0x00000002
  49. #define TXEN 0x00000001
  50. /* EMAC_DESC_CTRL_3 */
  51. #define RFD_RING_SIZE_BMSK 0xfff
  52. /* EMAC_DESC_CTRL_4 */
  53. #define RX_BUFFER_SIZE_BMSK 0xffff
  54. /* EMAC_DESC_CTRL_6 */
  55. #define RRD_RING_SIZE_BMSK 0xfff
  56. /* EMAC_DESC_CTRL_9 */
  57. #define TPD_RING_SIZE_BMSK 0xffff
  58. /* EMAC_TXQ_CTRL_0 */
  59. #define NUM_TXF_BURST_PREF_BMSK 0xffff0000
  60. #define NUM_TXF_BURST_PREF_SHFT 16
  61. #define LS_8023_SP 0x80
  62. #define TXQ_MODE 0x40
  63. #define TXQ_EN 0x20
  64. #define IP_OP_SP 0x10
  65. #define NUM_TPD_BURST_PREF_BMSK 0xf
  66. #define NUM_TPD_BURST_PREF_SHFT 0
  67. /* EMAC_TXQ_CTRL_1 */
  68. #define JUMBO_TASK_OFFLOAD_THRESHOLD_BMSK 0x7ff
  69. /* EMAC_TXQ_CTRL_2 */
  70. #define TXF_HWM_BMSK 0xfff0000
  71. #define TXF_LWM_BMSK 0xfff
  72. /* EMAC_RXQ_CTRL_0 */
  73. #define RXQ_EN BIT(31)
  74. #define CUT_THRU_EN BIT(30)
  75. #define RSS_HASH_EN BIT(29)
  76. #define NUM_RFD_BURST_PREF_BMSK 0x3f00000
  77. #define NUM_RFD_BURST_PREF_SHFT 20
  78. #define IDT_TABLE_SIZE_BMSK 0x1ff00
  79. #define IDT_TABLE_SIZE_SHFT 8
  80. #define SP_IPV6 0x80
  81. /* EMAC_RXQ_CTRL_1 */
  82. #define JUMBO_1KAH_BMSK 0xf000
  83. #define JUMBO_1KAH_SHFT 12
  84. #define RFD_PREF_LOW_TH 0x10
  85. #define RFD_PREF_LOW_THRESHOLD_BMSK 0xfc0
  86. #define RFD_PREF_LOW_THRESHOLD_SHFT 6
  87. #define RFD_PREF_UP_TH 0x10
  88. #define RFD_PREF_UP_THRESHOLD_BMSK 0x3f
  89. #define RFD_PREF_UP_THRESHOLD_SHFT 0
  90. /* EMAC_RXQ_CTRL_2 */
  91. #define RXF_DOF_THRESFHOLD 0x1a0
  92. #define RXF_DOF_THRESHOLD_BMSK 0xfff0000
  93. #define RXF_DOF_THRESHOLD_SHFT 16
  94. #define RXF_UOF_THRESFHOLD 0xbe
  95. #define RXF_UOF_THRESHOLD_BMSK 0xfff
  96. #define RXF_UOF_THRESHOLD_SHFT 0
  97. /* EMAC_RXQ_CTRL_3 */
  98. #define RXD_TIMER_BMSK 0xffff0000
  99. #define RXD_THRESHOLD_BMSK 0xfff
  100. #define RXD_THRESHOLD_SHFT 0
  101. /* EMAC_DMA_CTRL */
  102. #define DMAW_DLY_CNT_BMSK 0xf0000
  103. #define DMAW_DLY_CNT_SHFT 16
  104. #define DMAR_DLY_CNT_BMSK 0xf800
  105. #define DMAR_DLY_CNT_SHFT 11
  106. #define DMAR_REQ_PRI 0x400
  107. #define REGWRBLEN_BMSK 0x380
  108. #define REGWRBLEN_SHFT 7
  109. #define REGRDBLEN_BMSK 0x70
  110. #define REGRDBLEN_SHFT 4
  111. #define OUT_ORDER_MODE 0x4
  112. #define ENH_ORDER_MODE 0x2
  113. #define IN_ORDER_MODE 0x1
  114. /* EMAC_MAILBOX_13 */
  115. #define RFD3_PROC_IDX_BMSK 0xfff0000
  116. #define RFD3_PROC_IDX_SHFT 16
  117. #define RFD3_PROD_IDX_BMSK 0xfff
  118. #define RFD3_PROD_IDX_SHFT 0
  119. /* EMAC_MAILBOX_2 */
  120. #define NTPD_CONS_IDX_BMSK 0xffff0000
  121. #define NTPD_CONS_IDX_SHFT 16
  122. /* EMAC_MAILBOX_3 */
  123. #define RFD0_CONS_IDX_BMSK 0xfff
  124. #define RFD0_CONS_IDX_SHFT 0
  125. /* EMAC_MAILBOX_11 */
  126. #define H3TPD_PROD_IDX_BMSK 0xffff0000
  127. #define H3TPD_PROD_IDX_SHFT 16
  128. /* EMAC_AXI_MAST_CTRL */
  129. #define DATA_BYTE_SWAP 0x8
  130. #define MAX_BOUND 0x2
  131. #define MAX_BTYPE 0x1
  132. /* EMAC_MAILBOX_12 */
  133. #define H3TPD_CONS_IDX_BMSK 0xffff0000
  134. #define H3TPD_CONS_IDX_SHFT 16
  135. /* EMAC_MAILBOX_9 */
  136. #define H2TPD_PROD_IDX_BMSK 0xffff
  137. #define H2TPD_PROD_IDX_SHFT 0
  138. /* EMAC_MAILBOX_10 */
  139. #define H1TPD_CONS_IDX_BMSK 0xffff0000
  140. #define H1TPD_CONS_IDX_SHFT 16
  141. #define H2TPD_CONS_IDX_BMSK 0xffff
  142. #define H2TPD_CONS_IDX_SHFT 0
  143. /* EMAC_ATHR_HEADER_CTRL */
  144. #define HEADER_CNT_EN 0x2
  145. #define HEADER_ENABLE 0x1
  146. /* EMAC_MAILBOX_0 */
  147. #define RFD0_PROC_IDX_BMSK 0xfff0000
  148. #define RFD0_PROC_IDX_SHFT 16
  149. #define RFD0_PROD_IDX_BMSK 0xfff
  150. #define RFD0_PROD_IDX_SHFT 0
  151. /* EMAC_MAILBOX_5 */
  152. #define RFD1_PROC_IDX_BMSK 0xfff0000
  153. #define RFD1_PROC_IDX_SHFT 16
  154. #define RFD1_PROD_IDX_BMSK 0xfff
  155. #define RFD1_PROD_IDX_SHFT 0
  156. /* EMAC_MISC_CTRL */
  157. #define RX_UNCPL_INT_EN 0x1
  158. /* EMAC_MAILBOX_7 */
  159. #define RFD2_CONS_IDX_BMSK 0xfff0000
  160. #define RFD2_CONS_IDX_SHFT 16
  161. #define RFD1_CONS_IDX_BMSK 0xfff
  162. #define RFD1_CONS_IDX_SHFT 0
  163. /* EMAC_MAILBOX_8 */
  164. #define RFD3_CONS_IDX_BMSK 0xfff
  165. #define RFD3_CONS_IDX_SHFT 0
  166. /* EMAC_MAILBOX_15 */
  167. #define NTPD_PROD_IDX_BMSK 0xffff
  168. #define NTPD_PROD_IDX_SHFT 0
  169. /* EMAC_MAILBOX_16 */
  170. #define H1TPD_PROD_IDX_BMSK 0xffff
  171. #define H1TPD_PROD_IDX_SHFT 0
  172. #define RXQ0_RSS_HSTYP_IPV6_TCP_EN 0x20
  173. #define RXQ0_RSS_HSTYP_IPV6_EN 0x10
  174. #define RXQ0_RSS_HSTYP_IPV4_TCP_EN 0x8
  175. #define RXQ0_RSS_HSTYP_IPV4_EN 0x4
  176. /* EMAC_EMAC_WRAPPER_TX_TS_INX */
  177. #define EMAC_WRAPPER_TX_TS_EMPTY BIT(31)
  178. #define EMAC_WRAPPER_TX_TS_INX_BMSK 0xffff
  179. struct emac_skb_cb {
  180. u32 tpd_idx;
  181. unsigned long jiffies;
  182. };
  183. #define EMAC_SKB_CB(skb) ((struct emac_skb_cb *)(skb)->cb)
  184. #define EMAC_RSS_IDT_SIZE 256
  185. #define JUMBO_1KAH 0x4
  186. #define RXD_TH 0x100
  187. #define EMAC_TPD_LAST_FRAGMENT 0x80000000
  188. #define EMAC_TPD_TSTAMP_SAVE 0x80000000
  189. /* EMAC Errors in emac_rrd.word[3] */
  190. #define EMAC_RRD_L4F BIT(14)
  191. #define EMAC_RRD_IPF BIT(15)
  192. #define EMAC_RRD_CRC BIT(21)
  193. #define EMAC_RRD_FAE BIT(22)
  194. #define EMAC_RRD_TRN BIT(23)
  195. #define EMAC_RRD_RNT BIT(24)
  196. #define EMAC_RRD_INC BIT(25)
  197. #define EMAC_RRD_FOV BIT(29)
  198. #define EMAC_RRD_LEN BIT(30)
  199. /* Error bits that will result in a received frame being discarded */
  200. #define EMAC_RRD_ERROR (EMAC_RRD_IPF | EMAC_RRD_CRC | EMAC_RRD_FAE | \
  201. EMAC_RRD_TRN | EMAC_RRD_RNT | EMAC_RRD_INC | \
  202. EMAC_RRD_FOV | EMAC_RRD_LEN)
  203. #define EMAC_RRD_STATS_DW_IDX 3
  204. #define EMAC_RRD(RXQ, SIZE, IDX) ((RXQ)->rrd.v_addr + (SIZE * (IDX)))
  205. #define EMAC_RFD(RXQ, SIZE, IDX) ((RXQ)->rfd.v_addr + (SIZE * (IDX)))
  206. #define EMAC_TPD(TXQ, SIZE, IDX) ((TXQ)->tpd.v_addr + (SIZE * (IDX)))
  207. #define GET_RFD_BUFFER(RXQ, IDX) (&((RXQ)->rfd.rfbuff[(IDX)]))
  208. #define GET_TPD_BUFFER(RTQ, IDX) (&((RTQ)->tpd.tpbuff[(IDX)]))
  209. #define EMAC_TX_POLL_HWTXTSTAMP_THRESHOLD 8
  210. #define ISR_RX_PKT (\
  211. RX_PKT_INT0 |\
  212. RX_PKT_INT1 |\
  213. RX_PKT_INT2 |\
  214. RX_PKT_INT3)
  215. void emac_mac_multicast_addr_set(struct emac_adapter *adpt, u8 *addr)
  216. {
  217. u32 crc32, bit, reg, mta;
  218. /* Calculate the CRC of the MAC address */
  219. crc32 = ether_crc(ETH_ALEN, addr);
  220. /* The HASH Table is an array of 2 32-bit registers. It is
  221. * treated like an array of 64 bits (BitArray[hash_value]).
  222. * Use the upper 6 bits of the above CRC as the hash value.
  223. */
  224. reg = (crc32 >> 31) & 0x1;
  225. bit = (crc32 >> 26) & 0x1F;
  226. mta = readl(adpt->base + EMAC_HASH_TAB_REG0 + (reg << 2));
  227. mta |= BIT(bit);
  228. writel(mta, adpt->base + EMAC_HASH_TAB_REG0 + (reg << 2));
  229. }
  230. void emac_mac_multicast_addr_clear(struct emac_adapter *adpt)
  231. {
  232. writel(0, adpt->base + EMAC_HASH_TAB_REG0);
  233. writel(0, adpt->base + EMAC_HASH_TAB_REG1);
  234. }
  235. /* definitions for RSS */
  236. #define EMAC_RSS_KEY(_i, _type) \
  237. (EMAC_RSS_KEY0 + ((_i) * sizeof(_type)))
  238. #define EMAC_RSS_TBL(_i, _type) \
  239. (EMAC_IDT_TABLE0 + ((_i) * sizeof(_type)))
  240. /* Config MAC modes */
  241. void emac_mac_mode_config(struct emac_adapter *adpt)
  242. {
  243. struct net_device *netdev = adpt->netdev;
  244. u32 mac;
  245. mac = readl(adpt->base + EMAC_MAC_CTRL);
  246. mac &= ~(VLAN_STRIP | PROM_MODE | MULTI_ALL | MAC_LP_EN);
  247. if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  248. mac |= VLAN_STRIP;
  249. if (netdev->flags & IFF_PROMISC)
  250. mac |= PROM_MODE;
  251. if (netdev->flags & IFF_ALLMULTI)
  252. mac |= MULTI_ALL;
  253. writel(mac, adpt->base + EMAC_MAC_CTRL);
  254. }
  255. /* Config descriptor rings */
  256. static void emac_mac_dma_rings_config(struct emac_adapter *adpt)
  257. {
  258. static const unsigned short tpd_q_offset[] = {
  259. EMAC_DESC_CTRL_8, EMAC_H1TPD_BASE_ADDR_LO,
  260. EMAC_H2TPD_BASE_ADDR_LO, EMAC_H3TPD_BASE_ADDR_LO};
  261. static const unsigned short rfd_q_offset[] = {
  262. EMAC_DESC_CTRL_2, EMAC_DESC_CTRL_10,
  263. EMAC_DESC_CTRL_12, EMAC_DESC_CTRL_13};
  264. static const unsigned short rrd_q_offset[] = {
  265. EMAC_DESC_CTRL_5, EMAC_DESC_CTRL_14,
  266. EMAC_DESC_CTRL_15, EMAC_DESC_CTRL_16};
  267. /* TPD (Transmit Packet Descriptor) */
  268. writel(upper_32_bits(adpt->tx_q.tpd.dma_addr),
  269. adpt->base + EMAC_DESC_CTRL_1);
  270. writel(lower_32_bits(adpt->tx_q.tpd.dma_addr),
  271. adpt->base + tpd_q_offset[0]);
  272. writel(adpt->tx_q.tpd.count & TPD_RING_SIZE_BMSK,
  273. adpt->base + EMAC_DESC_CTRL_9);
  274. /* RFD (Receive Free Descriptor) & RRD (Receive Return Descriptor) */
  275. writel(upper_32_bits(adpt->rx_q.rfd.dma_addr),
  276. adpt->base + EMAC_DESC_CTRL_0);
  277. writel(lower_32_bits(adpt->rx_q.rfd.dma_addr),
  278. adpt->base + rfd_q_offset[0]);
  279. writel(lower_32_bits(adpt->rx_q.rrd.dma_addr),
  280. adpt->base + rrd_q_offset[0]);
  281. writel(adpt->rx_q.rfd.count & RFD_RING_SIZE_BMSK,
  282. adpt->base + EMAC_DESC_CTRL_3);
  283. writel(adpt->rx_q.rrd.count & RRD_RING_SIZE_BMSK,
  284. adpt->base + EMAC_DESC_CTRL_6);
  285. writel(adpt->rxbuf_size & RX_BUFFER_SIZE_BMSK,
  286. adpt->base + EMAC_DESC_CTRL_4);
  287. writel(0, adpt->base + EMAC_DESC_CTRL_11);
  288. /* Load all of the base addresses above and ensure that triggering HW to
  289. * read ring pointers is flushed
  290. */
  291. writel(1, adpt->base + EMAC_INTER_SRAM_PART9);
  292. }
  293. /* Config transmit parameters */
  294. static void emac_mac_tx_config(struct emac_adapter *adpt)
  295. {
  296. u32 val;
  297. writel((EMAC_MAX_TX_OFFLOAD_THRESH >> 3) &
  298. JUMBO_TASK_OFFLOAD_THRESHOLD_BMSK, adpt->base + EMAC_TXQ_CTRL_1);
  299. val = (adpt->tpd_burst << NUM_TPD_BURST_PREF_SHFT) &
  300. NUM_TPD_BURST_PREF_BMSK;
  301. val |= TXQ_MODE | LS_8023_SP;
  302. val |= (0x0100 << NUM_TXF_BURST_PREF_SHFT) &
  303. NUM_TXF_BURST_PREF_BMSK;
  304. writel(val, adpt->base + EMAC_TXQ_CTRL_0);
  305. emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_2,
  306. (TXF_HWM_BMSK | TXF_LWM_BMSK), 0);
  307. }
  308. /* Config receive parameters */
  309. static void emac_mac_rx_config(struct emac_adapter *adpt)
  310. {
  311. u32 val;
  312. val = (adpt->rfd_burst << NUM_RFD_BURST_PREF_SHFT) &
  313. NUM_RFD_BURST_PREF_BMSK;
  314. val |= (SP_IPV6 | CUT_THRU_EN);
  315. writel(val, adpt->base + EMAC_RXQ_CTRL_0);
  316. val = readl(adpt->base + EMAC_RXQ_CTRL_1);
  317. val &= ~(JUMBO_1KAH_BMSK | RFD_PREF_LOW_THRESHOLD_BMSK |
  318. RFD_PREF_UP_THRESHOLD_BMSK);
  319. val |= (JUMBO_1KAH << JUMBO_1KAH_SHFT) |
  320. (RFD_PREF_LOW_TH << RFD_PREF_LOW_THRESHOLD_SHFT) |
  321. (RFD_PREF_UP_TH << RFD_PREF_UP_THRESHOLD_SHFT);
  322. writel(val, adpt->base + EMAC_RXQ_CTRL_1);
  323. val = readl(adpt->base + EMAC_RXQ_CTRL_2);
  324. val &= ~(RXF_DOF_THRESHOLD_BMSK | RXF_UOF_THRESHOLD_BMSK);
  325. val |= (RXF_DOF_THRESFHOLD << RXF_DOF_THRESHOLD_SHFT) |
  326. (RXF_UOF_THRESFHOLD << RXF_UOF_THRESHOLD_SHFT);
  327. writel(val, adpt->base + EMAC_RXQ_CTRL_2);
  328. val = readl(adpt->base + EMAC_RXQ_CTRL_3);
  329. val &= ~(RXD_TIMER_BMSK | RXD_THRESHOLD_BMSK);
  330. val |= RXD_TH << RXD_THRESHOLD_SHFT;
  331. writel(val, adpt->base + EMAC_RXQ_CTRL_3);
  332. }
  333. /* Config dma */
  334. static void emac_mac_dma_config(struct emac_adapter *adpt)
  335. {
  336. u32 dma_ctrl = DMAR_REQ_PRI;
  337. switch (adpt->dma_order) {
  338. case emac_dma_ord_in:
  339. dma_ctrl |= IN_ORDER_MODE;
  340. break;
  341. case emac_dma_ord_enh:
  342. dma_ctrl |= ENH_ORDER_MODE;
  343. break;
  344. case emac_dma_ord_out:
  345. dma_ctrl |= OUT_ORDER_MODE;
  346. break;
  347. default:
  348. break;
  349. }
  350. dma_ctrl |= (((u32)adpt->dmar_block) << REGRDBLEN_SHFT) &
  351. REGRDBLEN_BMSK;
  352. dma_ctrl |= (((u32)adpt->dmaw_block) << REGWRBLEN_SHFT) &
  353. REGWRBLEN_BMSK;
  354. dma_ctrl |= (((u32)adpt->dmar_dly_cnt) << DMAR_DLY_CNT_SHFT) &
  355. DMAR_DLY_CNT_BMSK;
  356. dma_ctrl |= (((u32)adpt->dmaw_dly_cnt) << DMAW_DLY_CNT_SHFT) &
  357. DMAW_DLY_CNT_BMSK;
  358. /* config DMA and ensure that configuration is flushed to HW */
  359. writel(dma_ctrl, adpt->base + EMAC_DMA_CTRL);
  360. }
  361. /* set MAC address */
  362. static void emac_set_mac_address(struct emac_adapter *adpt, u8 *addr)
  363. {
  364. u32 sta;
  365. /* for example: 00-A0-C6-11-22-33
  366. * 0<-->C6112233, 1<-->00A0.
  367. */
  368. /* low 32bit word */
  369. sta = (((u32)addr[2]) << 24) | (((u32)addr[3]) << 16) |
  370. (((u32)addr[4]) << 8) | (((u32)addr[5]));
  371. writel(sta, adpt->base + EMAC_MAC_STA_ADDR0);
  372. /* hight 32bit word */
  373. sta = (((u32)addr[0]) << 8) | (u32)addr[1];
  374. writel(sta, adpt->base + EMAC_MAC_STA_ADDR1);
  375. }
  376. static void emac_mac_config(struct emac_adapter *adpt)
  377. {
  378. struct net_device *netdev = adpt->netdev;
  379. unsigned int max_frame;
  380. u32 val;
  381. emac_set_mac_address(adpt, netdev->dev_addr);
  382. max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  383. adpt->rxbuf_size = netdev->mtu > EMAC_DEF_RX_BUF_SIZE ?
  384. ALIGN(max_frame, 8) : EMAC_DEF_RX_BUF_SIZE;
  385. emac_mac_dma_rings_config(adpt);
  386. writel(netdev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
  387. adpt->base + EMAC_MAX_FRAM_LEN_CTRL);
  388. emac_mac_tx_config(adpt);
  389. emac_mac_rx_config(adpt);
  390. emac_mac_dma_config(adpt);
  391. val = readl(adpt->base + EMAC_AXI_MAST_CTRL);
  392. val &= ~(DATA_BYTE_SWAP | MAX_BOUND);
  393. val |= MAX_BTYPE;
  394. writel(val, adpt->base + EMAC_AXI_MAST_CTRL);
  395. writel(0, adpt->base + EMAC_CLK_GATE_CTRL);
  396. writel(RX_UNCPL_INT_EN, adpt->base + EMAC_MISC_CTRL);
  397. }
  398. void emac_mac_reset(struct emac_adapter *adpt)
  399. {
  400. emac_mac_stop(adpt);
  401. emac_reg_update32(adpt->base + EMAC_DMA_MAS_CTRL, 0, SOFT_RST);
  402. usleep_range(100, 150); /* reset may take up to 100usec */
  403. /* interrupt clear-on-read */
  404. emac_reg_update32(adpt->base + EMAC_DMA_MAS_CTRL, 0, INT_RD_CLR_EN);
  405. }
  406. static void emac_mac_start(struct emac_adapter *adpt)
  407. {
  408. struct phy_device *phydev = adpt->phydev;
  409. u32 mac, csr1;
  410. /* enable tx queue */
  411. emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, 0, TXQ_EN);
  412. /* enable rx queue */
  413. emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, 0, RXQ_EN);
  414. /* enable mac control */
  415. mac = readl(adpt->base + EMAC_MAC_CTRL);
  416. csr1 = readl(adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
  417. mac |= TXEN | RXEN; /* enable RX/TX */
  418. /* Configure MAC flow control. If set to automatic, then match
  419. * whatever the PHY does. Otherwise, enable or disable it, depending
  420. * on what the user configured via ethtool.
  421. */
  422. mac &= ~(RXFC | TXFC);
  423. if (adpt->automatic) {
  424. /* If it's set to automatic, then update our local values */
  425. adpt->rx_flow_control = phydev->pause;
  426. adpt->tx_flow_control = phydev->pause != phydev->asym_pause;
  427. }
  428. mac |= adpt->rx_flow_control ? RXFC : 0;
  429. mac |= adpt->tx_flow_control ? TXFC : 0;
  430. /* setup link speed */
  431. mac &= ~SPEED_MASK;
  432. if (phydev->speed == SPEED_1000) {
  433. mac |= SPEED(2);
  434. csr1 |= FREQ_MODE;
  435. } else {
  436. mac |= SPEED(1);
  437. csr1 &= ~FREQ_MODE;
  438. }
  439. if (phydev->duplex == DUPLEX_FULL)
  440. mac |= FULLD;
  441. else
  442. mac &= ~FULLD;
  443. /* other parameters */
  444. mac |= (CRCE | PCRCE);
  445. mac |= ((adpt->preamble << PRLEN_SHFT) & PRLEN_BMSK);
  446. mac |= BROAD_EN;
  447. mac |= FLCHK;
  448. mac &= ~RX_CHKSUM_EN;
  449. mac &= ~(HUGEN | VLAN_STRIP | TPAUSE | SIMR | HUGE | MULTI_ALL |
  450. DEBUG_MODE | SINGLE_PAUSE_MODE);
  451. /* Enable single-pause-frame mode if requested.
  452. *
  453. * If enabled, the EMAC will send a single pause frame when the RX
  454. * queue is full. This normally leads to packet loss because
  455. * the pause frame disables the remote MAC only for 33ms (the quanta),
  456. * and then the remote MAC continues sending packets even though
  457. * the RX queue is still full.
  458. *
  459. * If disabled, the EMAC sends a pause frame every 31ms until the RX
  460. * queue is no longer full. Normally, this is the preferred
  461. * method of operation. However, when the system is hung (e.g.
  462. * cores are halted), the EMAC interrupt handler is never called
  463. * and so the RX queue fills up quickly and stays full. The resuling
  464. * non-stop "flood" of pause frames sometimes has the effect of
  465. * disabling nearby switches. In some cases, other nearby switches
  466. * are also affected, shutting down the entire network.
  467. *
  468. * The user can enable or disable single-pause-frame mode
  469. * via ethtool.
  470. */
  471. mac |= adpt->single_pause_mode ? SINGLE_PAUSE_MODE : 0;
  472. writel_relaxed(csr1, adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
  473. writel_relaxed(mac, adpt->base + EMAC_MAC_CTRL);
  474. /* enable interrupt read clear, low power sleep mode and
  475. * the irq moderators
  476. */
  477. writel_relaxed(adpt->irq_mod, adpt->base + EMAC_IRQ_MOD_TIM_INIT);
  478. writel_relaxed(INT_RD_CLR_EN | LPW_MODE | IRQ_MODERATOR_EN |
  479. IRQ_MODERATOR2_EN, adpt->base + EMAC_DMA_MAS_CTRL);
  480. emac_mac_mode_config(adpt);
  481. emac_reg_update32(adpt->base + EMAC_ATHR_HEADER_CTRL,
  482. (HEADER_ENABLE | HEADER_CNT_EN), 0);
  483. }
  484. void emac_mac_stop(struct emac_adapter *adpt)
  485. {
  486. emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, RXQ_EN, 0);
  487. emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, TXQ_EN, 0);
  488. emac_reg_update32(adpt->base + EMAC_MAC_CTRL, TXEN | RXEN, 0);
  489. usleep_range(1000, 1050); /* stopping mac may take upto 1msec */
  490. }
  491. /* Free all descriptors of given transmit queue */
  492. static void emac_tx_q_descs_free(struct emac_adapter *adpt)
  493. {
  494. struct emac_tx_queue *tx_q = &adpt->tx_q;
  495. unsigned int i;
  496. size_t size;
  497. /* ring already cleared, nothing to do */
  498. if (!tx_q->tpd.tpbuff)
  499. return;
  500. for (i = 0; i < tx_q->tpd.count; i++) {
  501. struct emac_buffer *tpbuf = GET_TPD_BUFFER(tx_q, i);
  502. if (tpbuf->dma_addr) {
  503. dma_unmap_single(adpt->netdev->dev.parent,
  504. tpbuf->dma_addr, tpbuf->length,
  505. DMA_TO_DEVICE);
  506. tpbuf->dma_addr = 0;
  507. }
  508. if (tpbuf->skb) {
  509. dev_kfree_skb_any(tpbuf->skb);
  510. tpbuf->skb = NULL;
  511. }
  512. }
  513. size = sizeof(struct emac_buffer) * tx_q->tpd.count;
  514. memset(tx_q->tpd.tpbuff, 0, size);
  515. /* clear the descriptor ring */
  516. memset(tx_q->tpd.v_addr, 0, tx_q->tpd.size);
  517. tx_q->tpd.consume_idx = 0;
  518. tx_q->tpd.produce_idx = 0;
  519. }
  520. /* Free all descriptors of given receive queue */
  521. static void emac_rx_q_free_descs(struct emac_adapter *adpt)
  522. {
  523. struct device *dev = adpt->netdev->dev.parent;
  524. struct emac_rx_queue *rx_q = &adpt->rx_q;
  525. unsigned int i;
  526. size_t size;
  527. /* ring already cleared, nothing to do */
  528. if (!rx_q->rfd.rfbuff)
  529. return;
  530. for (i = 0; i < rx_q->rfd.count; i++) {
  531. struct emac_buffer *rfbuf = GET_RFD_BUFFER(rx_q, i);
  532. if (rfbuf->dma_addr) {
  533. dma_unmap_single(dev, rfbuf->dma_addr, rfbuf->length,
  534. DMA_FROM_DEVICE);
  535. rfbuf->dma_addr = 0;
  536. }
  537. if (rfbuf->skb) {
  538. dev_kfree_skb(rfbuf->skb);
  539. rfbuf->skb = NULL;
  540. }
  541. }
  542. size = sizeof(struct emac_buffer) * rx_q->rfd.count;
  543. memset(rx_q->rfd.rfbuff, 0, size);
  544. /* clear the descriptor rings */
  545. memset(rx_q->rrd.v_addr, 0, rx_q->rrd.size);
  546. rx_q->rrd.produce_idx = 0;
  547. rx_q->rrd.consume_idx = 0;
  548. memset(rx_q->rfd.v_addr, 0, rx_q->rfd.size);
  549. rx_q->rfd.produce_idx = 0;
  550. rx_q->rfd.consume_idx = 0;
  551. }
  552. /* Free all buffers associated with given transmit queue */
  553. static void emac_tx_q_bufs_free(struct emac_adapter *adpt)
  554. {
  555. struct emac_tx_queue *tx_q = &adpt->tx_q;
  556. emac_tx_q_descs_free(adpt);
  557. kfree(tx_q->tpd.tpbuff);
  558. tx_q->tpd.tpbuff = NULL;
  559. tx_q->tpd.v_addr = NULL;
  560. tx_q->tpd.dma_addr = 0;
  561. tx_q->tpd.size = 0;
  562. }
  563. /* Allocate TX descriptor ring for the given transmit queue */
  564. static int emac_tx_q_desc_alloc(struct emac_adapter *adpt,
  565. struct emac_tx_queue *tx_q)
  566. {
  567. struct emac_ring_header *ring_header = &adpt->ring_header;
  568. size_t size;
  569. size = sizeof(struct emac_buffer) * tx_q->tpd.count;
  570. tx_q->tpd.tpbuff = kzalloc(size, GFP_KERNEL);
  571. if (!tx_q->tpd.tpbuff)
  572. return -ENOMEM;
  573. tx_q->tpd.size = tx_q->tpd.count * (adpt->tpd_size * 4);
  574. tx_q->tpd.dma_addr = ring_header->dma_addr + ring_header->used;
  575. tx_q->tpd.v_addr = ring_header->v_addr + ring_header->used;
  576. ring_header->used += ALIGN(tx_q->tpd.size, 8);
  577. tx_q->tpd.produce_idx = 0;
  578. tx_q->tpd.consume_idx = 0;
  579. return 0;
  580. }
  581. /* Free all buffers associated with given transmit queue */
  582. static void emac_rx_q_bufs_free(struct emac_adapter *adpt)
  583. {
  584. struct emac_rx_queue *rx_q = &adpt->rx_q;
  585. emac_rx_q_free_descs(adpt);
  586. kfree(rx_q->rfd.rfbuff);
  587. rx_q->rfd.rfbuff = NULL;
  588. rx_q->rfd.v_addr = NULL;
  589. rx_q->rfd.dma_addr = 0;
  590. rx_q->rfd.size = 0;
  591. rx_q->rrd.v_addr = NULL;
  592. rx_q->rrd.dma_addr = 0;
  593. rx_q->rrd.size = 0;
  594. }
  595. /* Allocate RX descriptor rings for the given receive queue */
  596. static int emac_rx_descs_alloc(struct emac_adapter *adpt)
  597. {
  598. struct emac_ring_header *ring_header = &adpt->ring_header;
  599. struct emac_rx_queue *rx_q = &adpt->rx_q;
  600. size_t size;
  601. size = sizeof(struct emac_buffer) * rx_q->rfd.count;
  602. rx_q->rfd.rfbuff = kzalloc(size, GFP_KERNEL);
  603. if (!rx_q->rfd.rfbuff)
  604. return -ENOMEM;
  605. rx_q->rrd.size = rx_q->rrd.count * (adpt->rrd_size * 4);
  606. rx_q->rfd.size = rx_q->rfd.count * (adpt->rfd_size * 4);
  607. rx_q->rrd.dma_addr = ring_header->dma_addr + ring_header->used;
  608. rx_q->rrd.v_addr = ring_header->v_addr + ring_header->used;
  609. ring_header->used += ALIGN(rx_q->rrd.size, 8);
  610. rx_q->rfd.dma_addr = ring_header->dma_addr + ring_header->used;
  611. rx_q->rfd.v_addr = ring_header->v_addr + ring_header->used;
  612. ring_header->used += ALIGN(rx_q->rfd.size, 8);
  613. rx_q->rrd.produce_idx = 0;
  614. rx_q->rrd.consume_idx = 0;
  615. rx_q->rfd.produce_idx = 0;
  616. rx_q->rfd.consume_idx = 0;
  617. return 0;
  618. }
  619. /* Allocate all TX and RX descriptor rings */
  620. int emac_mac_rx_tx_rings_alloc_all(struct emac_adapter *adpt)
  621. {
  622. struct emac_ring_header *ring_header = &adpt->ring_header;
  623. struct device *dev = adpt->netdev->dev.parent;
  624. unsigned int num_tx_descs = adpt->tx_desc_cnt;
  625. unsigned int num_rx_descs = adpt->rx_desc_cnt;
  626. int ret;
  627. adpt->tx_q.tpd.count = adpt->tx_desc_cnt;
  628. adpt->rx_q.rrd.count = adpt->rx_desc_cnt;
  629. adpt->rx_q.rfd.count = adpt->rx_desc_cnt;
  630. /* Ring DMA buffer. Each ring may need up to 8 bytes for alignment,
  631. * hence the additional padding bytes are allocated.
  632. */
  633. ring_header->size = num_tx_descs * (adpt->tpd_size * 4) +
  634. num_rx_descs * (adpt->rfd_size * 4) +
  635. num_rx_descs * (adpt->rrd_size * 4) +
  636. 8 + 2 * 8; /* 8 byte per one Tx and two Rx rings */
  637. ring_header->used = 0;
  638. ring_header->v_addr = dma_zalloc_coherent(dev, ring_header->size,
  639. &ring_header->dma_addr,
  640. GFP_KERNEL);
  641. if (!ring_header->v_addr)
  642. return -ENOMEM;
  643. ring_header->used = ALIGN(ring_header->dma_addr, 8) -
  644. ring_header->dma_addr;
  645. ret = emac_tx_q_desc_alloc(adpt, &adpt->tx_q);
  646. if (ret) {
  647. netdev_err(adpt->netdev, "error: Tx Queue alloc failed\n");
  648. goto err_alloc_tx;
  649. }
  650. ret = emac_rx_descs_alloc(adpt);
  651. if (ret) {
  652. netdev_err(adpt->netdev, "error: Rx Queue alloc failed\n");
  653. goto err_alloc_rx;
  654. }
  655. return 0;
  656. err_alloc_rx:
  657. emac_tx_q_bufs_free(adpt);
  658. err_alloc_tx:
  659. dma_free_coherent(dev, ring_header->size,
  660. ring_header->v_addr, ring_header->dma_addr);
  661. ring_header->v_addr = NULL;
  662. ring_header->dma_addr = 0;
  663. ring_header->size = 0;
  664. ring_header->used = 0;
  665. return ret;
  666. }
  667. /* Free all TX and RX descriptor rings */
  668. void emac_mac_rx_tx_rings_free_all(struct emac_adapter *adpt)
  669. {
  670. struct emac_ring_header *ring_header = &adpt->ring_header;
  671. struct device *dev = adpt->netdev->dev.parent;
  672. emac_tx_q_bufs_free(adpt);
  673. emac_rx_q_bufs_free(adpt);
  674. dma_free_coherent(dev, ring_header->size,
  675. ring_header->v_addr, ring_header->dma_addr);
  676. ring_header->v_addr = NULL;
  677. ring_header->dma_addr = 0;
  678. ring_header->size = 0;
  679. ring_header->used = 0;
  680. }
  681. /* Initialize descriptor rings */
  682. static void emac_mac_rx_tx_ring_reset_all(struct emac_adapter *adpt)
  683. {
  684. unsigned int i;
  685. adpt->tx_q.tpd.produce_idx = 0;
  686. adpt->tx_q.tpd.consume_idx = 0;
  687. for (i = 0; i < adpt->tx_q.tpd.count; i++)
  688. adpt->tx_q.tpd.tpbuff[i].dma_addr = 0;
  689. adpt->rx_q.rrd.produce_idx = 0;
  690. adpt->rx_q.rrd.consume_idx = 0;
  691. adpt->rx_q.rfd.produce_idx = 0;
  692. adpt->rx_q.rfd.consume_idx = 0;
  693. for (i = 0; i < adpt->rx_q.rfd.count; i++)
  694. adpt->rx_q.rfd.rfbuff[i].dma_addr = 0;
  695. }
  696. /* Produce new receive free descriptor */
  697. static void emac_mac_rx_rfd_create(struct emac_adapter *adpt,
  698. struct emac_rx_queue *rx_q,
  699. dma_addr_t addr)
  700. {
  701. u32 *hw_rfd = EMAC_RFD(rx_q, adpt->rfd_size, rx_q->rfd.produce_idx);
  702. *(hw_rfd++) = lower_32_bits(addr);
  703. *hw_rfd = upper_32_bits(addr);
  704. if (++rx_q->rfd.produce_idx == rx_q->rfd.count)
  705. rx_q->rfd.produce_idx = 0;
  706. }
  707. /* Fill up receive queue's RFD with preallocated receive buffers */
  708. static void emac_mac_rx_descs_refill(struct emac_adapter *adpt,
  709. struct emac_rx_queue *rx_q)
  710. {
  711. struct emac_buffer *curr_rxbuf;
  712. struct emac_buffer *next_rxbuf;
  713. unsigned int count = 0;
  714. u32 next_produce_idx;
  715. next_produce_idx = rx_q->rfd.produce_idx + 1;
  716. if (next_produce_idx == rx_q->rfd.count)
  717. next_produce_idx = 0;
  718. curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
  719. next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
  720. /* this always has a blank rx_buffer*/
  721. while (!next_rxbuf->dma_addr) {
  722. struct sk_buff *skb;
  723. int ret;
  724. skb = netdev_alloc_skb_ip_align(adpt->netdev, adpt->rxbuf_size);
  725. if (!skb)
  726. break;
  727. curr_rxbuf->dma_addr =
  728. dma_map_single(adpt->netdev->dev.parent, skb->data,
  729. adpt->rxbuf_size, DMA_FROM_DEVICE);
  730. ret = dma_mapping_error(adpt->netdev->dev.parent,
  731. curr_rxbuf->dma_addr);
  732. if (ret) {
  733. dev_kfree_skb(skb);
  734. break;
  735. }
  736. curr_rxbuf->skb = skb;
  737. curr_rxbuf->length = adpt->rxbuf_size;
  738. emac_mac_rx_rfd_create(adpt, rx_q, curr_rxbuf->dma_addr);
  739. next_produce_idx = rx_q->rfd.produce_idx + 1;
  740. if (next_produce_idx == rx_q->rfd.count)
  741. next_produce_idx = 0;
  742. curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
  743. next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
  744. count++;
  745. }
  746. if (count) {
  747. u32 prod_idx = (rx_q->rfd.produce_idx << rx_q->produce_shift) &
  748. rx_q->produce_mask;
  749. emac_reg_update32(adpt->base + rx_q->produce_reg,
  750. rx_q->produce_mask, prod_idx);
  751. }
  752. }
  753. static void emac_adjust_link(struct net_device *netdev)
  754. {
  755. struct emac_adapter *adpt = netdev_priv(netdev);
  756. struct emac_sgmii *sgmii = &adpt->phy;
  757. struct phy_device *phydev = netdev->phydev;
  758. if (phydev->link) {
  759. emac_mac_start(adpt);
  760. sgmii->link_up(adpt);
  761. } else {
  762. sgmii->link_down(adpt);
  763. emac_mac_stop(adpt);
  764. }
  765. phy_print_status(phydev);
  766. }
  767. /* Bringup the interface/HW */
  768. int emac_mac_up(struct emac_adapter *adpt)
  769. {
  770. struct net_device *netdev = adpt->netdev;
  771. int ret;
  772. emac_mac_rx_tx_ring_reset_all(adpt);
  773. emac_mac_config(adpt);
  774. emac_mac_rx_descs_refill(adpt, &adpt->rx_q);
  775. adpt->phydev->irq = PHY_POLL;
  776. ret = phy_connect_direct(netdev, adpt->phydev, emac_adjust_link,
  777. PHY_INTERFACE_MODE_SGMII);
  778. if (ret) {
  779. netdev_err(adpt->netdev, "could not connect phy\n");
  780. return ret;
  781. }
  782. phy_attached_print(adpt->phydev, NULL);
  783. /* enable mac irq */
  784. writel((u32)~DIS_INT, adpt->base + EMAC_INT_STATUS);
  785. writel(adpt->irq.mask, adpt->base + EMAC_INT_MASK);
  786. phy_start(adpt->phydev);
  787. napi_enable(&adpt->rx_q.napi);
  788. netif_start_queue(netdev);
  789. return 0;
  790. }
  791. /* Bring down the interface/HW */
  792. void emac_mac_down(struct emac_adapter *adpt)
  793. {
  794. struct net_device *netdev = adpt->netdev;
  795. netif_stop_queue(netdev);
  796. napi_disable(&adpt->rx_q.napi);
  797. phy_stop(adpt->phydev);
  798. /* Interrupts must be disabled before the PHY is disconnected, to
  799. * avoid a race condition where adjust_link is null when we get
  800. * an interrupt.
  801. */
  802. writel(DIS_INT, adpt->base + EMAC_INT_STATUS);
  803. writel(0, adpt->base + EMAC_INT_MASK);
  804. synchronize_irq(adpt->irq.irq);
  805. phy_disconnect(adpt->phydev);
  806. emac_mac_reset(adpt);
  807. emac_tx_q_descs_free(adpt);
  808. netdev_reset_queue(adpt->netdev);
  809. emac_rx_q_free_descs(adpt);
  810. }
  811. /* Consume next received packet descriptor */
  812. static bool emac_rx_process_rrd(struct emac_adapter *adpt,
  813. struct emac_rx_queue *rx_q,
  814. struct emac_rrd *rrd)
  815. {
  816. u32 *hw_rrd = EMAC_RRD(rx_q, adpt->rrd_size, rx_q->rrd.consume_idx);
  817. rrd->word[3] = *(hw_rrd + 3);
  818. if (!RRD_UPDT(rrd))
  819. return false;
  820. rrd->word[4] = 0;
  821. rrd->word[5] = 0;
  822. rrd->word[0] = *(hw_rrd++);
  823. rrd->word[1] = *(hw_rrd++);
  824. rrd->word[2] = *(hw_rrd++);
  825. if (unlikely(RRD_NOR(rrd) != 1)) {
  826. netdev_err(adpt->netdev,
  827. "error: multi-RFD not support yet! nor:%lu\n",
  828. RRD_NOR(rrd));
  829. }
  830. /* mark rrd as processed */
  831. RRD_UPDT_SET(rrd, 0);
  832. *hw_rrd = rrd->word[3];
  833. if (++rx_q->rrd.consume_idx == rx_q->rrd.count)
  834. rx_q->rrd.consume_idx = 0;
  835. return true;
  836. }
  837. /* Produce new transmit descriptor */
  838. static void emac_tx_tpd_create(struct emac_adapter *adpt,
  839. struct emac_tx_queue *tx_q, struct emac_tpd *tpd)
  840. {
  841. u32 *hw_tpd;
  842. tx_q->tpd.last_produce_idx = tx_q->tpd.produce_idx;
  843. hw_tpd = EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.produce_idx);
  844. if (++tx_q->tpd.produce_idx == tx_q->tpd.count)
  845. tx_q->tpd.produce_idx = 0;
  846. *(hw_tpd++) = tpd->word[0];
  847. *(hw_tpd++) = tpd->word[1];
  848. *(hw_tpd++) = tpd->word[2];
  849. *hw_tpd = tpd->word[3];
  850. }
  851. /* Mark the last transmit descriptor as such (for the transmit packet) */
  852. static void emac_tx_tpd_mark_last(struct emac_adapter *adpt,
  853. struct emac_tx_queue *tx_q)
  854. {
  855. u32 *hw_tpd =
  856. EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.last_produce_idx);
  857. u32 tmp_tpd;
  858. tmp_tpd = *(hw_tpd + 1);
  859. tmp_tpd |= EMAC_TPD_LAST_FRAGMENT;
  860. *(hw_tpd + 1) = tmp_tpd;
  861. }
  862. static void emac_rx_rfd_clean(struct emac_rx_queue *rx_q, struct emac_rrd *rrd)
  863. {
  864. struct emac_buffer *rfbuf = rx_q->rfd.rfbuff;
  865. u32 consume_idx = RRD_SI(rrd);
  866. unsigned int i;
  867. for (i = 0; i < RRD_NOR(rrd); i++) {
  868. rfbuf[consume_idx].skb = NULL;
  869. if (++consume_idx == rx_q->rfd.count)
  870. consume_idx = 0;
  871. }
  872. rx_q->rfd.consume_idx = consume_idx;
  873. rx_q->rfd.process_idx = consume_idx;
  874. }
  875. /* Push the received skb to upper layers */
  876. static void emac_receive_skb(struct emac_rx_queue *rx_q,
  877. struct sk_buff *skb,
  878. u16 vlan_tag, bool vlan_flag)
  879. {
  880. if (vlan_flag) {
  881. u16 vlan;
  882. EMAC_TAG_TO_VLAN(vlan_tag, vlan);
  883. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
  884. }
  885. napi_gro_receive(&rx_q->napi, skb);
  886. }
  887. /* Process receive event */
  888. void emac_mac_rx_process(struct emac_adapter *adpt, struct emac_rx_queue *rx_q,
  889. int *num_pkts, int max_pkts)
  890. {
  891. u32 proc_idx, hw_consume_idx, num_consume_pkts;
  892. struct net_device *netdev = adpt->netdev;
  893. struct emac_buffer *rfbuf;
  894. unsigned int count = 0;
  895. struct emac_rrd rrd;
  896. struct sk_buff *skb;
  897. u32 reg;
  898. reg = readl_relaxed(adpt->base + rx_q->consume_reg);
  899. hw_consume_idx = (reg & rx_q->consume_mask) >> rx_q->consume_shift;
  900. num_consume_pkts = (hw_consume_idx >= rx_q->rrd.consume_idx) ?
  901. (hw_consume_idx - rx_q->rrd.consume_idx) :
  902. (hw_consume_idx + rx_q->rrd.count - rx_q->rrd.consume_idx);
  903. do {
  904. if (!num_consume_pkts)
  905. break;
  906. if (!emac_rx_process_rrd(adpt, rx_q, &rrd))
  907. break;
  908. if (likely(RRD_NOR(&rrd) == 1)) {
  909. /* good receive */
  910. rfbuf = GET_RFD_BUFFER(rx_q, RRD_SI(&rrd));
  911. dma_unmap_single(adpt->netdev->dev.parent,
  912. rfbuf->dma_addr, rfbuf->length,
  913. DMA_FROM_DEVICE);
  914. rfbuf->dma_addr = 0;
  915. skb = rfbuf->skb;
  916. } else {
  917. netdev_err(adpt->netdev,
  918. "error: multi-RFD not support yet!\n");
  919. break;
  920. }
  921. emac_rx_rfd_clean(rx_q, &rrd);
  922. num_consume_pkts--;
  923. count++;
  924. /* Due to a HW issue in L4 check sum detection (UDP/TCP frags
  925. * with DF set are marked as error), drop packets based on the
  926. * error mask rather than the summary bit (ignoring L4F errors)
  927. */
  928. if (rrd.word[EMAC_RRD_STATS_DW_IDX] & EMAC_RRD_ERROR) {
  929. netif_dbg(adpt, rx_status, adpt->netdev,
  930. "Drop error packet[RRD: 0x%x:0x%x:0x%x:0x%x]\n",
  931. rrd.word[0], rrd.word[1],
  932. rrd.word[2], rrd.word[3]);
  933. dev_kfree_skb(skb);
  934. continue;
  935. }
  936. skb_put(skb, RRD_PKT_SIZE(&rrd) - ETH_FCS_LEN);
  937. skb->dev = netdev;
  938. skb->protocol = eth_type_trans(skb, skb->dev);
  939. if (netdev->features & NETIF_F_RXCSUM)
  940. skb->ip_summed = RRD_L4F(&rrd) ?
  941. CHECKSUM_NONE : CHECKSUM_UNNECESSARY;
  942. else
  943. skb_checksum_none_assert(skb);
  944. emac_receive_skb(rx_q, skb, (u16)RRD_CVALN_TAG(&rrd),
  945. (bool)RRD_CVTAG(&rrd));
  946. (*num_pkts)++;
  947. } while (*num_pkts < max_pkts);
  948. if (count) {
  949. proc_idx = (rx_q->rfd.process_idx << rx_q->process_shft) &
  950. rx_q->process_mask;
  951. emac_reg_update32(adpt->base + rx_q->process_reg,
  952. rx_q->process_mask, proc_idx);
  953. emac_mac_rx_descs_refill(adpt, rx_q);
  954. }
  955. }
  956. /* get the number of free transmit descriptors */
  957. static unsigned int emac_tpd_num_free_descs(struct emac_tx_queue *tx_q)
  958. {
  959. u32 produce_idx = tx_q->tpd.produce_idx;
  960. u32 consume_idx = tx_q->tpd.consume_idx;
  961. return (consume_idx > produce_idx) ?
  962. (consume_idx - produce_idx - 1) :
  963. (tx_q->tpd.count + consume_idx - produce_idx - 1);
  964. }
  965. /* Process transmit event */
  966. void emac_mac_tx_process(struct emac_adapter *adpt, struct emac_tx_queue *tx_q)
  967. {
  968. u32 reg = readl_relaxed(adpt->base + tx_q->consume_reg);
  969. u32 hw_consume_idx, pkts_compl = 0, bytes_compl = 0;
  970. struct emac_buffer *tpbuf;
  971. hw_consume_idx = (reg & tx_q->consume_mask) >> tx_q->consume_shift;
  972. while (tx_q->tpd.consume_idx != hw_consume_idx) {
  973. tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.consume_idx);
  974. if (tpbuf->dma_addr) {
  975. dma_unmap_single(adpt->netdev->dev.parent,
  976. tpbuf->dma_addr, tpbuf->length,
  977. DMA_TO_DEVICE);
  978. tpbuf->dma_addr = 0;
  979. }
  980. if (tpbuf->skb) {
  981. pkts_compl++;
  982. bytes_compl += tpbuf->skb->len;
  983. dev_kfree_skb_irq(tpbuf->skb);
  984. tpbuf->skb = NULL;
  985. }
  986. if (++tx_q->tpd.consume_idx == tx_q->tpd.count)
  987. tx_q->tpd.consume_idx = 0;
  988. }
  989. netdev_completed_queue(adpt->netdev, pkts_compl, bytes_compl);
  990. if (netif_queue_stopped(adpt->netdev))
  991. if (emac_tpd_num_free_descs(tx_q) > (MAX_SKB_FRAGS + 1))
  992. netif_wake_queue(adpt->netdev);
  993. }
  994. /* Initialize all queue data structures */
  995. void emac_mac_rx_tx_ring_init_all(struct platform_device *pdev,
  996. struct emac_adapter *adpt)
  997. {
  998. adpt->rx_q.netdev = adpt->netdev;
  999. adpt->rx_q.produce_reg = EMAC_MAILBOX_0;
  1000. adpt->rx_q.produce_mask = RFD0_PROD_IDX_BMSK;
  1001. adpt->rx_q.produce_shift = RFD0_PROD_IDX_SHFT;
  1002. adpt->rx_q.process_reg = EMAC_MAILBOX_0;
  1003. adpt->rx_q.process_mask = RFD0_PROC_IDX_BMSK;
  1004. adpt->rx_q.process_shft = RFD0_PROC_IDX_SHFT;
  1005. adpt->rx_q.consume_reg = EMAC_MAILBOX_3;
  1006. adpt->rx_q.consume_mask = RFD0_CONS_IDX_BMSK;
  1007. adpt->rx_q.consume_shift = RFD0_CONS_IDX_SHFT;
  1008. adpt->rx_q.irq = &adpt->irq;
  1009. adpt->rx_q.intr = adpt->irq.mask & ISR_RX_PKT;
  1010. adpt->tx_q.produce_reg = EMAC_MAILBOX_15;
  1011. adpt->tx_q.produce_mask = NTPD_PROD_IDX_BMSK;
  1012. adpt->tx_q.produce_shift = NTPD_PROD_IDX_SHFT;
  1013. adpt->tx_q.consume_reg = EMAC_MAILBOX_2;
  1014. adpt->tx_q.consume_mask = NTPD_CONS_IDX_BMSK;
  1015. adpt->tx_q.consume_shift = NTPD_CONS_IDX_SHFT;
  1016. }
  1017. /* Fill up transmit descriptors with TSO and Checksum offload information */
  1018. static int emac_tso_csum(struct emac_adapter *adpt,
  1019. struct emac_tx_queue *tx_q,
  1020. struct sk_buff *skb,
  1021. struct emac_tpd *tpd)
  1022. {
  1023. unsigned int hdr_len;
  1024. int ret;
  1025. if (skb_is_gso(skb)) {
  1026. if (skb_header_cloned(skb)) {
  1027. ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  1028. if (unlikely(ret))
  1029. return ret;
  1030. }
  1031. if (skb->protocol == htons(ETH_P_IP)) {
  1032. u32 pkt_len = ((unsigned char *)ip_hdr(skb) - skb->data)
  1033. + ntohs(ip_hdr(skb)->tot_len);
  1034. if (skb->len > pkt_len)
  1035. pskb_trim(skb, pkt_len);
  1036. }
  1037. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1038. if (unlikely(skb->len == hdr_len)) {
  1039. /* we only need to do csum */
  1040. netif_warn(adpt, tx_err, adpt->netdev,
  1041. "tso not needed for packet with 0 data\n");
  1042. goto do_csum;
  1043. }
  1044. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
  1045. ip_hdr(skb)->check = 0;
  1046. tcp_hdr(skb)->check =
  1047. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  1048. ip_hdr(skb)->daddr,
  1049. 0, IPPROTO_TCP, 0);
  1050. TPD_IPV4_SET(tpd, 1);
  1051. }
  1052. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
  1053. /* ipv6 tso need an extra tpd */
  1054. struct emac_tpd extra_tpd;
  1055. memset(tpd, 0, sizeof(*tpd));
  1056. memset(&extra_tpd, 0, sizeof(extra_tpd));
  1057. ipv6_hdr(skb)->payload_len = 0;
  1058. tcp_hdr(skb)->check =
  1059. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  1060. &ipv6_hdr(skb)->daddr,
  1061. 0, IPPROTO_TCP, 0);
  1062. TPD_PKT_LEN_SET(&extra_tpd, skb->len);
  1063. TPD_LSO_SET(&extra_tpd, 1);
  1064. TPD_LSOV_SET(&extra_tpd, 1);
  1065. emac_tx_tpd_create(adpt, tx_q, &extra_tpd);
  1066. TPD_LSOV_SET(tpd, 1);
  1067. }
  1068. TPD_LSO_SET(tpd, 1);
  1069. TPD_TCPHDR_OFFSET_SET(tpd, skb_transport_offset(skb));
  1070. TPD_MSS_SET(tpd, skb_shinfo(skb)->gso_size);
  1071. return 0;
  1072. }
  1073. do_csum:
  1074. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  1075. unsigned int css, cso;
  1076. cso = skb_transport_offset(skb);
  1077. if (unlikely(cso & 0x1)) {
  1078. netdev_err(adpt->netdev,
  1079. "error: payload offset should be even\n");
  1080. return -EINVAL;
  1081. }
  1082. css = cso + skb->csum_offset;
  1083. TPD_PAYLOAD_OFFSET_SET(tpd, cso >> 1);
  1084. TPD_CXSUM_OFFSET_SET(tpd, css >> 1);
  1085. TPD_CSX_SET(tpd, 1);
  1086. }
  1087. return 0;
  1088. }
  1089. /* Fill up transmit descriptors */
  1090. static void emac_tx_fill_tpd(struct emac_adapter *adpt,
  1091. struct emac_tx_queue *tx_q, struct sk_buff *skb,
  1092. struct emac_tpd *tpd)
  1093. {
  1094. unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
  1095. unsigned int first = tx_q->tpd.produce_idx;
  1096. unsigned int len = skb_headlen(skb);
  1097. struct emac_buffer *tpbuf = NULL;
  1098. unsigned int mapped_len = 0;
  1099. unsigned int i;
  1100. int count = 0;
  1101. int ret;
  1102. /* if Large Segment Offload is (in TCP Segmentation Offload struct) */
  1103. if (TPD_LSO(tpd)) {
  1104. mapped_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1105. tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
  1106. tpbuf->length = mapped_len;
  1107. tpbuf->dma_addr = dma_map_single(adpt->netdev->dev.parent,
  1108. skb->data, tpbuf->length,
  1109. DMA_TO_DEVICE);
  1110. ret = dma_mapping_error(adpt->netdev->dev.parent,
  1111. tpbuf->dma_addr);
  1112. if (ret)
  1113. goto error;
  1114. TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
  1115. TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
  1116. TPD_BUF_LEN_SET(tpd, tpbuf->length);
  1117. emac_tx_tpd_create(adpt, tx_q, tpd);
  1118. count++;
  1119. }
  1120. if (mapped_len < len) {
  1121. tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
  1122. tpbuf->length = len - mapped_len;
  1123. tpbuf->dma_addr = dma_map_single(adpt->netdev->dev.parent,
  1124. skb->data + mapped_len,
  1125. tpbuf->length, DMA_TO_DEVICE);
  1126. ret = dma_mapping_error(adpt->netdev->dev.parent,
  1127. tpbuf->dma_addr);
  1128. if (ret)
  1129. goto error;
  1130. TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
  1131. TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
  1132. TPD_BUF_LEN_SET(tpd, tpbuf->length);
  1133. emac_tx_tpd_create(adpt, tx_q, tpd);
  1134. count++;
  1135. }
  1136. for (i = 0; i < nr_frags; i++) {
  1137. struct skb_frag_struct *frag;
  1138. frag = &skb_shinfo(skb)->frags[i];
  1139. tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
  1140. tpbuf->length = frag->size;
  1141. tpbuf->dma_addr = dma_map_page(adpt->netdev->dev.parent,
  1142. frag->page.p, frag->page_offset,
  1143. tpbuf->length, DMA_TO_DEVICE);
  1144. ret = dma_mapping_error(adpt->netdev->dev.parent,
  1145. tpbuf->dma_addr);
  1146. if (ret)
  1147. goto error;
  1148. TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
  1149. TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
  1150. TPD_BUF_LEN_SET(tpd, tpbuf->length);
  1151. emac_tx_tpd_create(adpt, tx_q, tpd);
  1152. count++;
  1153. }
  1154. /* The last tpd */
  1155. wmb();
  1156. emac_tx_tpd_mark_last(adpt, tx_q);
  1157. /* The last buffer info contain the skb address,
  1158. * so it will be freed after unmap
  1159. */
  1160. tpbuf->skb = skb;
  1161. return;
  1162. error:
  1163. /* One of the memory mappings failed, so undo everything */
  1164. tx_q->tpd.produce_idx = first;
  1165. while (count--) {
  1166. tpbuf = GET_TPD_BUFFER(tx_q, first);
  1167. dma_unmap_page(adpt->netdev->dev.parent, tpbuf->dma_addr,
  1168. tpbuf->length, DMA_TO_DEVICE);
  1169. tpbuf->dma_addr = 0;
  1170. tpbuf->length = 0;
  1171. if (++first == tx_q->tpd.count)
  1172. first = 0;
  1173. }
  1174. dev_kfree_skb(skb);
  1175. }
  1176. /* Transmit the packet using specified transmit queue */
  1177. int emac_mac_tx_buf_send(struct emac_adapter *adpt, struct emac_tx_queue *tx_q,
  1178. struct sk_buff *skb)
  1179. {
  1180. struct emac_tpd tpd;
  1181. u32 prod_idx;
  1182. memset(&tpd, 0, sizeof(tpd));
  1183. if (emac_tso_csum(adpt, tx_q, skb, &tpd) != 0) {
  1184. dev_kfree_skb_any(skb);
  1185. return NETDEV_TX_OK;
  1186. }
  1187. if (skb_vlan_tag_present(skb)) {
  1188. u16 tag;
  1189. EMAC_VLAN_TO_TAG(skb_vlan_tag_get(skb), tag);
  1190. TPD_CVLAN_TAG_SET(&tpd, tag);
  1191. TPD_INSTC_SET(&tpd, 1);
  1192. }
  1193. if (skb_network_offset(skb) != ETH_HLEN)
  1194. TPD_TYP_SET(&tpd, 1);
  1195. emac_tx_fill_tpd(adpt, tx_q, skb, &tpd);
  1196. netdev_sent_queue(adpt->netdev, skb->len);
  1197. /* Make sure the are enough free descriptors to hold one
  1198. * maximum-sized SKB. We need one desc for each fragment,
  1199. * one for the checksum (emac_tso_csum), one for TSO, and
  1200. * and one for the SKB header.
  1201. */
  1202. if (emac_tpd_num_free_descs(tx_q) < (MAX_SKB_FRAGS + 3))
  1203. netif_stop_queue(adpt->netdev);
  1204. /* update produce idx */
  1205. prod_idx = (tx_q->tpd.produce_idx << tx_q->produce_shift) &
  1206. tx_q->produce_mask;
  1207. emac_reg_update32(adpt->base + tx_q->produce_reg,
  1208. tx_q->produce_mask, prod_idx);
  1209. return NETDEV_TX_OK;
  1210. }