emac-mac.c 44 KB

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