emac-mac.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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. writel_relaxed(csr1, adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
  452. writel_relaxed(mac, adpt->base + EMAC_MAC_CTRL);
  453. /* enable interrupt read clear, low power sleep mode and
  454. * the irq moderators
  455. */
  456. writel_relaxed(adpt->irq_mod, adpt->base + EMAC_IRQ_MOD_TIM_INIT);
  457. writel_relaxed(INT_RD_CLR_EN | LPW_MODE | IRQ_MODERATOR_EN |
  458. IRQ_MODERATOR2_EN, adpt->base + EMAC_DMA_MAS_CTRL);
  459. emac_mac_mode_config(adpt);
  460. emac_reg_update32(adpt->base + EMAC_ATHR_HEADER_CTRL,
  461. (HEADER_ENABLE | HEADER_CNT_EN), 0);
  462. }
  463. void emac_mac_stop(struct emac_adapter *adpt)
  464. {
  465. emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, RXQ_EN, 0);
  466. emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, TXQ_EN, 0);
  467. emac_reg_update32(adpt->base + EMAC_MAC_CTRL, TXEN | RXEN, 0);
  468. usleep_range(1000, 1050); /* stopping mac may take upto 1msec */
  469. }
  470. /* Free all descriptors of given transmit queue */
  471. static void emac_tx_q_descs_free(struct emac_adapter *adpt)
  472. {
  473. struct emac_tx_queue *tx_q = &adpt->tx_q;
  474. unsigned int i;
  475. size_t size;
  476. /* ring already cleared, nothing to do */
  477. if (!tx_q->tpd.tpbuff)
  478. return;
  479. for (i = 0; i < tx_q->tpd.count; i++) {
  480. struct emac_buffer *tpbuf = GET_TPD_BUFFER(tx_q, i);
  481. if (tpbuf->dma_addr) {
  482. dma_unmap_single(adpt->netdev->dev.parent,
  483. tpbuf->dma_addr, tpbuf->length,
  484. DMA_TO_DEVICE);
  485. tpbuf->dma_addr = 0;
  486. }
  487. if (tpbuf->skb) {
  488. dev_kfree_skb_any(tpbuf->skb);
  489. tpbuf->skb = NULL;
  490. }
  491. }
  492. size = sizeof(struct emac_buffer) * tx_q->tpd.count;
  493. memset(tx_q->tpd.tpbuff, 0, size);
  494. /* clear the descriptor ring */
  495. memset(tx_q->tpd.v_addr, 0, tx_q->tpd.size);
  496. tx_q->tpd.consume_idx = 0;
  497. tx_q->tpd.produce_idx = 0;
  498. }
  499. /* Free all descriptors of given receive queue */
  500. static void emac_rx_q_free_descs(struct emac_adapter *adpt)
  501. {
  502. struct device *dev = adpt->netdev->dev.parent;
  503. struct emac_rx_queue *rx_q = &adpt->rx_q;
  504. unsigned int i;
  505. size_t size;
  506. /* ring already cleared, nothing to do */
  507. if (!rx_q->rfd.rfbuff)
  508. return;
  509. for (i = 0; i < rx_q->rfd.count; i++) {
  510. struct emac_buffer *rfbuf = GET_RFD_BUFFER(rx_q, i);
  511. if (rfbuf->dma_addr) {
  512. dma_unmap_single(dev, rfbuf->dma_addr, rfbuf->length,
  513. DMA_FROM_DEVICE);
  514. rfbuf->dma_addr = 0;
  515. }
  516. if (rfbuf->skb) {
  517. dev_kfree_skb(rfbuf->skb);
  518. rfbuf->skb = NULL;
  519. }
  520. }
  521. size = sizeof(struct emac_buffer) * rx_q->rfd.count;
  522. memset(rx_q->rfd.rfbuff, 0, size);
  523. /* clear the descriptor rings */
  524. memset(rx_q->rrd.v_addr, 0, rx_q->rrd.size);
  525. rx_q->rrd.produce_idx = 0;
  526. rx_q->rrd.consume_idx = 0;
  527. memset(rx_q->rfd.v_addr, 0, rx_q->rfd.size);
  528. rx_q->rfd.produce_idx = 0;
  529. rx_q->rfd.consume_idx = 0;
  530. }
  531. /* Free all buffers associated with given transmit queue */
  532. static void emac_tx_q_bufs_free(struct emac_adapter *adpt)
  533. {
  534. struct emac_tx_queue *tx_q = &adpt->tx_q;
  535. emac_tx_q_descs_free(adpt);
  536. kfree(tx_q->tpd.tpbuff);
  537. tx_q->tpd.tpbuff = NULL;
  538. tx_q->tpd.v_addr = NULL;
  539. tx_q->tpd.dma_addr = 0;
  540. tx_q->tpd.size = 0;
  541. }
  542. /* Allocate TX descriptor ring for the given transmit queue */
  543. static int emac_tx_q_desc_alloc(struct emac_adapter *adpt,
  544. struct emac_tx_queue *tx_q)
  545. {
  546. struct emac_ring_header *ring_header = &adpt->ring_header;
  547. size_t size;
  548. size = sizeof(struct emac_buffer) * tx_q->tpd.count;
  549. tx_q->tpd.tpbuff = kzalloc(size, GFP_KERNEL);
  550. if (!tx_q->tpd.tpbuff)
  551. return -ENOMEM;
  552. tx_q->tpd.size = tx_q->tpd.count * (adpt->tpd_size * 4);
  553. tx_q->tpd.dma_addr = ring_header->dma_addr + ring_header->used;
  554. tx_q->tpd.v_addr = ring_header->v_addr + ring_header->used;
  555. ring_header->used += ALIGN(tx_q->tpd.size, 8);
  556. tx_q->tpd.produce_idx = 0;
  557. tx_q->tpd.consume_idx = 0;
  558. return 0;
  559. }
  560. /* Free all buffers associated with given transmit queue */
  561. static void emac_rx_q_bufs_free(struct emac_adapter *adpt)
  562. {
  563. struct emac_rx_queue *rx_q = &adpt->rx_q;
  564. emac_rx_q_free_descs(adpt);
  565. kfree(rx_q->rfd.rfbuff);
  566. rx_q->rfd.rfbuff = NULL;
  567. rx_q->rfd.v_addr = NULL;
  568. rx_q->rfd.dma_addr = 0;
  569. rx_q->rfd.size = 0;
  570. rx_q->rrd.v_addr = NULL;
  571. rx_q->rrd.dma_addr = 0;
  572. rx_q->rrd.size = 0;
  573. }
  574. /* Allocate RX descriptor rings for the given receive queue */
  575. static int emac_rx_descs_alloc(struct emac_adapter *adpt)
  576. {
  577. struct emac_ring_header *ring_header = &adpt->ring_header;
  578. struct emac_rx_queue *rx_q = &adpt->rx_q;
  579. size_t size;
  580. size = sizeof(struct emac_buffer) * rx_q->rfd.count;
  581. rx_q->rfd.rfbuff = kzalloc(size, GFP_KERNEL);
  582. if (!rx_q->rfd.rfbuff)
  583. return -ENOMEM;
  584. rx_q->rrd.size = rx_q->rrd.count * (adpt->rrd_size * 4);
  585. rx_q->rfd.size = rx_q->rfd.count * (adpt->rfd_size * 4);
  586. rx_q->rrd.dma_addr = ring_header->dma_addr + ring_header->used;
  587. rx_q->rrd.v_addr = ring_header->v_addr + ring_header->used;
  588. ring_header->used += ALIGN(rx_q->rrd.size, 8);
  589. rx_q->rfd.dma_addr = ring_header->dma_addr + ring_header->used;
  590. rx_q->rfd.v_addr = ring_header->v_addr + ring_header->used;
  591. ring_header->used += ALIGN(rx_q->rfd.size, 8);
  592. rx_q->rrd.produce_idx = 0;
  593. rx_q->rrd.consume_idx = 0;
  594. rx_q->rfd.produce_idx = 0;
  595. rx_q->rfd.consume_idx = 0;
  596. return 0;
  597. }
  598. /* Allocate all TX and RX descriptor rings */
  599. int emac_mac_rx_tx_rings_alloc_all(struct emac_adapter *adpt)
  600. {
  601. struct emac_ring_header *ring_header = &adpt->ring_header;
  602. struct device *dev = adpt->netdev->dev.parent;
  603. unsigned int num_tx_descs = adpt->tx_desc_cnt;
  604. unsigned int num_rx_descs = adpt->rx_desc_cnt;
  605. int ret;
  606. adpt->tx_q.tpd.count = adpt->tx_desc_cnt;
  607. adpt->rx_q.rrd.count = adpt->rx_desc_cnt;
  608. adpt->rx_q.rfd.count = adpt->rx_desc_cnt;
  609. /* Ring DMA buffer. Each ring may need up to 8 bytes for alignment,
  610. * hence the additional padding bytes are allocated.
  611. */
  612. ring_header->size = num_tx_descs * (adpt->tpd_size * 4) +
  613. num_rx_descs * (adpt->rfd_size * 4) +
  614. num_rx_descs * (adpt->rrd_size * 4) +
  615. 8 + 2 * 8; /* 8 byte per one Tx and two Rx rings */
  616. ring_header->used = 0;
  617. ring_header->v_addr = dma_zalloc_coherent(dev, ring_header->size,
  618. &ring_header->dma_addr,
  619. GFP_KERNEL);
  620. if (!ring_header->v_addr)
  621. return -ENOMEM;
  622. ring_header->used = ALIGN(ring_header->dma_addr, 8) -
  623. ring_header->dma_addr;
  624. ret = emac_tx_q_desc_alloc(adpt, &adpt->tx_q);
  625. if (ret) {
  626. netdev_err(adpt->netdev, "error: Tx Queue alloc failed\n");
  627. goto err_alloc_tx;
  628. }
  629. ret = emac_rx_descs_alloc(adpt);
  630. if (ret) {
  631. netdev_err(adpt->netdev, "error: Rx Queue alloc failed\n");
  632. goto err_alloc_rx;
  633. }
  634. return 0;
  635. err_alloc_rx:
  636. emac_tx_q_bufs_free(adpt);
  637. err_alloc_tx:
  638. dma_free_coherent(dev, ring_header->size,
  639. ring_header->v_addr, ring_header->dma_addr);
  640. ring_header->v_addr = NULL;
  641. ring_header->dma_addr = 0;
  642. ring_header->size = 0;
  643. ring_header->used = 0;
  644. return ret;
  645. }
  646. /* Free all TX and RX descriptor rings */
  647. void emac_mac_rx_tx_rings_free_all(struct emac_adapter *adpt)
  648. {
  649. struct emac_ring_header *ring_header = &adpt->ring_header;
  650. struct device *dev = adpt->netdev->dev.parent;
  651. emac_tx_q_bufs_free(adpt);
  652. emac_rx_q_bufs_free(adpt);
  653. dma_free_coherent(dev, ring_header->size,
  654. ring_header->v_addr, ring_header->dma_addr);
  655. ring_header->v_addr = NULL;
  656. ring_header->dma_addr = 0;
  657. ring_header->size = 0;
  658. ring_header->used = 0;
  659. }
  660. /* Initialize descriptor rings */
  661. static void emac_mac_rx_tx_ring_reset_all(struct emac_adapter *adpt)
  662. {
  663. unsigned int i;
  664. adpt->tx_q.tpd.produce_idx = 0;
  665. adpt->tx_q.tpd.consume_idx = 0;
  666. for (i = 0; i < adpt->tx_q.tpd.count; i++)
  667. adpt->tx_q.tpd.tpbuff[i].dma_addr = 0;
  668. adpt->rx_q.rrd.produce_idx = 0;
  669. adpt->rx_q.rrd.consume_idx = 0;
  670. adpt->rx_q.rfd.produce_idx = 0;
  671. adpt->rx_q.rfd.consume_idx = 0;
  672. for (i = 0; i < adpt->rx_q.rfd.count; i++)
  673. adpt->rx_q.rfd.rfbuff[i].dma_addr = 0;
  674. }
  675. /* Produce new receive free descriptor */
  676. static void emac_mac_rx_rfd_create(struct emac_adapter *adpt,
  677. struct emac_rx_queue *rx_q,
  678. dma_addr_t addr)
  679. {
  680. u32 *hw_rfd = EMAC_RFD(rx_q, adpt->rfd_size, rx_q->rfd.produce_idx);
  681. *(hw_rfd++) = lower_32_bits(addr);
  682. *hw_rfd = upper_32_bits(addr);
  683. if (++rx_q->rfd.produce_idx == rx_q->rfd.count)
  684. rx_q->rfd.produce_idx = 0;
  685. }
  686. /* Fill up receive queue's RFD with preallocated receive buffers */
  687. static void emac_mac_rx_descs_refill(struct emac_adapter *adpt,
  688. struct emac_rx_queue *rx_q)
  689. {
  690. struct emac_buffer *curr_rxbuf;
  691. struct emac_buffer *next_rxbuf;
  692. unsigned int count = 0;
  693. u32 next_produce_idx;
  694. next_produce_idx = rx_q->rfd.produce_idx + 1;
  695. if (next_produce_idx == rx_q->rfd.count)
  696. next_produce_idx = 0;
  697. curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
  698. next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
  699. /* this always has a blank rx_buffer*/
  700. while (!next_rxbuf->dma_addr) {
  701. struct sk_buff *skb;
  702. int ret;
  703. skb = netdev_alloc_skb_ip_align(adpt->netdev, adpt->rxbuf_size);
  704. if (!skb)
  705. break;
  706. curr_rxbuf->dma_addr =
  707. dma_map_single(adpt->netdev->dev.parent, skb->data,
  708. curr_rxbuf->length, DMA_FROM_DEVICE);
  709. ret = dma_mapping_error(adpt->netdev->dev.parent,
  710. curr_rxbuf->dma_addr);
  711. if (ret) {
  712. dev_kfree_skb(skb);
  713. break;
  714. }
  715. curr_rxbuf->skb = skb;
  716. curr_rxbuf->length = adpt->rxbuf_size;
  717. emac_mac_rx_rfd_create(adpt, rx_q, curr_rxbuf->dma_addr);
  718. next_produce_idx = rx_q->rfd.produce_idx + 1;
  719. if (next_produce_idx == rx_q->rfd.count)
  720. next_produce_idx = 0;
  721. curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
  722. next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
  723. count++;
  724. }
  725. if (count) {
  726. u32 prod_idx = (rx_q->rfd.produce_idx << rx_q->produce_shift) &
  727. rx_q->produce_mask;
  728. emac_reg_update32(adpt->base + rx_q->produce_reg,
  729. rx_q->produce_mask, prod_idx);
  730. }
  731. }
  732. static void emac_adjust_link(struct net_device *netdev)
  733. {
  734. struct emac_adapter *adpt = netdev_priv(netdev);
  735. struct emac_sgmii *sgmii = &adpt->phy;
  736. struct phy_device *phydev = netdev->phydev;
  737. if (phydev->link) {
  738. emac_mac_start(adpt);
  739. sgmii->link_up(adpt);
  740. } else {
  741. sgmii->link_down(adpt);
  742. emac_mac_stop(adpt);
  743. }
  744. phy_print_status(phydev);
  745. }
  746. /* Bringup the interface/HW */
  747. int emac_mac_up(struct emac_adapter *adpt)
  748. {
  749. struct net_device *netdev = adpt->netdev;
  750. int ret;
  751. emac_mac_rx_tx_ring_reset_all(adpt);
  752. emac_mac_config(adpt);
  753. emac_mac_rx_descs_refill(adpt, &adpt->rx_q);
  754. adpt->phydev->irq = PHY_IGNORE_INTERRUPT;
  755. ret = phy_connect_direct(netdev, adpt->phydev, emac_adjust_link,
  756. PHY_INTERFACE_MODE_SGMII);
  757. if (ret) {
  758. netdev_err(adpt->netdev, "could not connect phy\n");
  759. return ret;
  760. }
  761. phy_attached_print(adpt->phydev, NULL);
  762. /* enable mac irq */
  763. writel((u32)~DIS_INT, adpt->base + EMAC_INT_STATUS);
  764. writel(adpt->irq.mask, adpt->base + EMAC_INT_MASK);
  765. phy_start(adpt->phydev);
  766. napi_enable(&adpt->rx_q.napi);
  767. netif_start_queue(netdev);
  768. return 0;
  769. }
  770. /* Bring down the interface/HW */
  771. void emac_mac_down(struct emac_adapter *adpt)
  772. {
  773. struct net_device *netdev = adpt->netdev;
  774. netif_stop_queue(netdev);
  775. napi_disable(&adpt->rx_q.napi);
  776. phy_stop(adpt->phydev);
  777. /* Interrupts must be disabled before the PHY is disconnected, to
  778. * avoid a race condition where adjust_link is null when we get
  779. * an interrupt.
  780. */
  781. writel(DIS_INT, adpt->base + EMAC_INT_STATUS);
  782. writel(0, adpt->base + EMAC_INT_MASK);
  783. synchronize_irq(adpt->irq.irq);
  784. phy_disconnect(adpt->phydev);
  785. emac_mac_reset(adpt);
  786. emac_tx_q_descs_free(adpt);
  787. netdev_reset_queue(adpt->netdev);
  788. emac_rx_q_free_descs(adpt);
  789. }
  790. /* Consume next received packet descriptor */
  791. static bool emac_rx_process_rrd(struct emac_adapter *adpt,
  792. struct emac_rx_queue *rx_q,
  793. struct emac_rrd *rrd)
  794. {
  795. u32 *hw_rrd = EMAC_RRD(rx_q, adpt->rrd_size, rx_q->rrd.consume_idx);
  796. rrd->word[3] = *(hw_rrd + 3);
  797. if (!RRD_UPDT(rrd))
  798. return false;
  799. rrd->word[4] = 0;
  800. rrd->word[5] = 0;
  801. rrd->word[0] = *(hw_rrd++);
  802. rrd->word[1] = *(hw_rrd++);
  803. rrd->word[2] = *(hw_rrd++);
  804. if (unlikely(RRD_NOR(rrd) != 1)) {
  805. netdev_err(adpt->netdev,
  806. "error: multi-RFD not support yet! nor:%lu\n",
  807. RRD_NOR(rrd));
  808. }
  809. /* mark rrd as processed */
  810. RRD_UPDT_SET(rrd, 0);
  811. *hw_rrd = rrd->word[3];
  812. if (++rx_q->rrd.consume_idx == rx_q->rrd.count)
  813. rx_q->rrd.consume_idx = 0;
  814. return true;
  815. }
  816. /* Produce new transmit descriptor */
  817. static void emac_tx_tpd_create(struct emac_adapter *adpt,
  818. struct emac_tx_queue *tx_q, struct emac_tpd *tpd)
  819. {
  820. u32 *hw_tpd;
  821. tx_q->tpd.last_produce_idx = tx_q->tpd.produce_idx;
  822. hw_tpd = EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.produce_idx);
  823. if (++tx_q->tpd.produce_idx == tx_q->tpd.count)
  824. tx_q->tpd.produce_idx = 0;
  825. *(hw_tpd++) = tpd->word[0];
  826. *(hw_tpd++) = tpd->word[1];
  827. *(hw_tpd++) = tpd->word[2];
  828. *hw_tpd = tpd->word[3];
  829. }
  830. /* Mark the last transmit descriptor as such (for the transmit packet) */
  831. static void emac_tx_tpd_mark_last(struct emac_adapter *adpt,
  832. struct emac_tx_queue *tx_q)
  833. {
  834. u32 *hw_tpd =
  835. EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.last_produce_idx);
  836. u32 tmp_tpd;
  837. tmp_tpd = *(hw_tpd + 1);
  838. tmp_tpd |= EMAC_TPD_LAST_FRAGMENT;
  839. *(hw_tpd + 1) = tmp_tpd;
  840. }
  841. static void emac_rx_rfd_clean(struct emac_rx_queue *rx_q, struct emac_rrd *rrd)
  842. {
  843. struct emac_buffer *rfbuf = rx_q->rfd.rfbuff;
  844. u32 consume_idx = RRD_SI(rrd);
  845. unsigned int i;
  846. for (i = 0; i < RRD_NOR(rrd); i++) {
  847. rfbuf[consume_idx].skb = NULL;
  848. if (++consume_idx == rx_q->rfd.count)
  849. consume_idx = 0;
  850. }
  851. rx_q->rfd.consume_idx = consume_idx;
  852. rx_q->rfd.process_idx = consume_idx;
  853. }
  854. /* Push the received skb to upper layers */
  855. static void emac_receive_skb(struct emac_rx_queue *rx_q,
  856. struct sk_buff *skb,
  857. u16 vlan_tag, bool vlan_flag)
  858. {
  859. if (vlan_flag) {
  860. u16 vlan;
  861. EMAC_TAG_TO_VLAN(vlan_tag, vlan);
  862. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
  863. }
  864. napi_gro_receive(&rx_q->napi, skb);
  865. }
  866. /* Process receive event */
  867. void emac_mac_rx_process(struct emac_adapter *adpt, struct emac_rx_queue *rx_q,
  868. int *num_pkts, int max_pkts)
  869. {
  870. u32 proc_idx, hw_consume_idx, num_consume_pkts;
  871. struct net_device *netdev = adpt->netdev;
  872. struct emac_buffer *rfbuf;
  873. unsigned int count = 0;
  874. struct emac_rrd rrd;
  875. struct sk_buff *skb;
  876. u32 reg;
  877. reg = readl_relaxed(adpt->base + rx_q->consume_reg);
  878. hw_consume_idx = (reg & rx_q->consume_mask) >> rx_q->consume_shift;
  879. num_consume_pkts = (hw_consume_idx >= rx_q->rrd.consume_idx) ?
  880. (hw_consume_idx - rx_q->rrd.consume_idx) :
  881. (hw_consume_idx + rx_q->rrd.count - rx_q->rrd.consume_idx);
  882. do {
  883. if (!num_consume_pkts)
  884. break;
  885. if (!emac_rx_process_rrd(adpt, rx_q, &rrd))
  886. break;
  887. if (likely(RRD_NOR(&rrd) == 1)) {
  888. /* good receive */
  889. rfbuf = GET_RFD_BUFFER(rx_q, RRD_SI(&rrd));
  890. dma_unmap_single(adpt->netdev->dev.parent,
  891. rfbuf->dma_addr, rfbuf->length,
  892. DMA_FROM_DEVICE);
  893. rfbuf->dma_addr = 0;
  894. skb = rfbuf->skb;
  895. } else {
  896. netdev_err(adpt->netdev,
  897. "error: multi-RFD not support yet!\n");
  898. break;
  899. }
  900. emac_rx_rfd_clean(rx_q, &rrd);
  901. num_consume_pkts--;
  902. count++;
  903. /* Due to a HW issue in L4 check sum detection (UDP/TCP frags
  904. * with DF set are marked as error), drop packets based on the
  905. * error mask rather than the summary bit (ignoring L4F errors)
  906. */
  907. if (rrd.word[EMAC_RRD_STATS_DW_IDX] & EMAC_RRD_ERROR) {
  908. netif_dbg(adpt, rx_status, adpt->netdev,
  909. "Drop error packet[RRD: 0x%x:0x%x:0x%x:0x%x]\n",
  910. rrd.word[0], rrd.word[1],
  911. rrd.word[2], rrd.word[3]);
  912. dev_kfree_skb(skb);
  913. continue;
  914. }
  915. skb_put(skb, RRD_PKT_SIZE(&rrd) - ETH_FCS_LEN);
  916. skb->dev = netdev;
  917. skb->protocol = eth_type_trans(skb, skb->dev);
  918. if (netdev->features & NETIF_F_RXCSUM)
  919. skb->ip_summed = RRD_L4F(&rrd) ?
  920. CHECKSUM_NONE : CHECKSUM_UNNECESSARY;
  921. else
  922. skb_checksum_none_assert(skb);
  923. emac_receive_skb(rx_q, skb, (u16)RRD_CVALN_TAG(&rrd),
  924. (bool)RRD_CVTAG(&rrd));
  925. (*num_pkts)++;
  926. } while (*num_pkts < max_pkts);
  927. if (count) {
  928. proc_idx = (rx_q->rfd.process_idx << rx_q->process_shft) &
  929. rx_q->process_mask;
  930. emac_reg_update32(adpt->base + rx_q->process_reg,
  931. rx_q->process_mask, proc_idx);
  932. emac_mac_rx_descs_refill(adpt, rx_q);
  933. }
  934. }
  935. /* get the number of free transmit descriptors */
  936. static unsigned int emac_tpd_num_free_descs(struct emac_tx_queue *tx_q)
  937. {
  938. u32 produce_idx = tx_q->tpd.produce_idx;
  939. u32 consume_idx = tx_q->tpd.consume_idx;
  940. return (consume_idx > produce_idx) ?
  941. (consume_idx - produce_idx - 1) :
  942. (tx_q->tpd.count + consume_idx - produce_idx - 1);
  943. }
  944. /* Process transmit event */
  945. void emac_mac_tx_process(struct emac_adapter *adpt, struct emac_tx_queue *tx_q)
  946. {
  947. u32 reg = readl_relaxed(adpt->base + tx_q->consume_reg);
  948. u32 hw_consume_idx, pkts_compl = 0, bytes_compl = 0;
  949. struct emac_buffer *tpbuf;
  950. hw_consume_idx = (reg & tx_q->consume_mask) >> tx_q->consume_shift;
  951. while (tx_q->tpd.consume_idx != hw_consume_idx) {
  952. tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.consume_idx);
  953. if (tpbuf->dma_addr) {
  954. dma_unmap_single(adpt->netdev->dev.parent,
  955. tpbuf->dma_addr, tpbuf->length,
  956. DMA_TO_DEVICE);
  957. tpbuf->dma_addr = 0;
  958. }
  959. if (tpbuf->skb) {
  960. pkts_compl++;
  961. bytes_compl += tpbuf->skb->len;
  962. dev_kfree_skb_irq(tpbuf->skb);
  963. tpbuf->skb = NULL;
  964. }
  965. if (++tx_q->tpd.consume_idx == tx_q->tpd.count)
  966. tx_q->tpd.consume_idx = 0;
  967. }
  968. netdev_completed_queue(adpt->netdev, pkts_compl, bytes_compl);
  969. if (netif_queue_stopped(adpt->netdev))
  970. if (emac_tpd_num_free_descs(tx_q) > (MAX_SKB_FRAGS + 1))
  971. netif_wake_queue(adpt->netdev);
  972. }
  973. /* Initialize all queue data structures */
  974. void emac_mac_rx_tx_ring_init_all(struct platform_device *pdev,
  975. struct emac_adapter *adpt)
  976. {
  977. adpt->rx_q.netdev = adpt->netdev;
  978. adpt->rx_q.produce_reg = EMAC_MAILBOX_0;
  979. adpt->rx_q.produce_mask = RFD0_PROD_IDX_BMSK;
  980. adpt->rx_q.produce_shift = RFD0_PROD_IDX_SHFT;
  981. adpt->rx_q.process_reg = EMAC_MAILBOX_0;
  982. adpt->rx_q.process_mask = RFD0_PROC_IDX_BMSK;
  983. adpt->rx_q.process_shft = RFD0_PROC_IDX_SHFT;
  984. adpt->rx_q.consume_reg = EMAC_MAILBOX_3;
  985. adpt->rx_q.consume_mask = RFD0_CONS_IDX_BMSK;
  986. adpt->rx_q.consume_shift = RFD0_CONS_IDX_SHFT;
  987. adpt->rx_q.irq = &adpt->irq;
  988. adpt->rx_q.intr = adpt->irq.mask & ISR_RX_PKT;
  989. adpt->tx_q.produce_reg = EMAC_MAILBOX_15;
  990. adpt->tx_q.produce_mask = NTPD_PROD_IDX_BMSK;
  991. adpt->tx_q.produce_shift = NTPD_PROD_IDX_SHFT;
  992. adpt->tx_q.consume_reg = EMAC_MAILBOX_2;
  993. adpt->tx_q.consume_mask = NTPD_CONS_IDX_BMSK;
  994. adpt->tx_q.consume_shift = NTPD_CONS_IDX_SHFT;
  995. }
  996. /* Fill up transmit descriptors with TSO and Checksum offload information */
  997. static int emac_tso_csum(struct emac_adapter *adpt,
  998. struct emac_tx_queue *tx_q,
  999. struct sk_buff *skb,
  1000. struct emac_tpd *tpd)
  1001. {
  1002. unsigned int hdr_len;
  1003. int ret;
  1004. if (skb_is_gso(skb)) {
  1005. if (skb_header_cloned(skb)) {
  1006. ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  1007. if (unlikely(ret))
  1008. return ret;
  1009. }
  1010. if (skb->protocol == htons(ETH_P_IP)) {
  1011. u32 pkt_len = ((unsigned char *)ip_hdr(skb) - skb->data)
  1012. + ntohs(ip_hdr(skb)->tot_len);
  1013. if (skb->len > pkt_len)
  1014. pskb_trim(skb, pkt_len);
  1015. }
  1016. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1017. if (unlikely(skb->len == hdr_len)) {
  1018. /* we only need to do csum */
  1019. netif_warn(adpt, tx_err, adpt->netdev,
  1020. "tso not needed for packet with 0 data\n");
  1021. goto do_csum;
  1022. }
  1023. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
  1024. ip_hdr(skb)->check = 0;
  1025. tcp_hdr(skb)->check =
  1026. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  1027. ip_hdr(skb)->daddr,
  1028. 0, IPPROTO_TCP, 0);
  1029. TPD_IPV4_SET(tpd, 1);
  1030. }
  1031. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
  1032. /* ipv6 tso need an extra tpd */
  1033. struct emac_tpd extra_tpd;
  1034. memset(tpd, 0, sizeof(*tpd));
  1035. memset(&extra_tpd, 0, sizeof(extra_tpd));
  1036. ipv6_hdr(skb)->payload_len = 0;
  1037. tcp_hdr(skb)->check =
  1038. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  1039. &ipv6_hdr(skb)->daddr,
  1040. 0, IPPROTO_TCP, 0);
  1041. TPD_PKT_LEN_SET(&extra_tpd, skb->len);
  1042. TPD_LSO_SET(&extra_tpd, 1);
  1043. TPD_LSOV_SET(&extra_tpd, 1);
  1044. emac_tx_tpd_create(adpt, tx_q, &extra_tpd);
  1045. TPD_LSOV_SET(tpd, 1);
  1046. }
  1047. TPD_LSO_SET(tpd, 1);
  1048. TPD_TCPHDR_OFFSET_SET(tpd, skb_transport_offset(skb));
  1049. TPD_MSS_SET(tpd, skb_shinfo(skb)->gso_size);
  1050. return 0;
  1051. }
  1052. do_csum:
  1053. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  1054. unsigned int css, cso;
  1055. cso = skb_transport_offset(skb);
  1056. if (unlikely(cso & 0x1)) {
  1057. netdev_err(adpt->netdev,
  1058. "error: payload offset should be even\n");
  1059. return -EINVAL;
  1060. }
  1061. css = cso + skb->csum_offset;
  1062. TPD_PAYLOAD_OFFSET_SET(tpd, cso >> 1);
  1063. TPD_CXSUM_OFFSET_SET(tpd, css >> 1);
  1064. TPD_CSX_SET(tpd, 1);
  1065. }
  1066. return 0;
  1067. }
  1068. /* Fill up transmit descriptors */
  1069. static void emac_tx_fill_tpd(struct emac_adapter *adpt,
  1070. struct emac_tx_queue *tx_q, struct sk_buff *skb,
  1071. struct emac_tpd *tpd)
  1072. {
  1073. unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
  1074. unsigned int first = tx_q->tpd.produce_idx;
  1075. unsigned int len = skb_headlen(skb);
  1076. struct emac_buffer *tpbuf = NULL;
  1077. unsigned int mapped_len = 0;
  1078. unsigned int i;
  1079. int count = 0;
  1080. int ret;
  1081. /* if Large Segment Offload is (in TCP Segmentation Offload struct) */
  1082. if (TPD_LSO(tpd)) {
  1083. mapped_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1084. tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
  1085. tpbuf->length = mapped_len;
  1086. tpbuf->dma_addr = dma_map_single(adpt->netdev->dev.parent,
  1087. skb->data, tpbuf->length,
  1088. DMA_TO_DEVICE);
  1089. ret = dma_mapping_error(adpt->netdev->dev.parent,
  1090. tpbuf->dma_addr);
  1091. if (ret)
  1092. goto error;
  1093. TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
  1094. TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
  1095. TPD_BUF_LEN_SET(tpd, tpbuf->length);
  1096. emac_tx_tpd_create(adpt, tx_q, tpd);
  1097. count++;
  1098. }
  1099. if (mapped_len < len) {
  1100. tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
  1101. tpbuf->length = len - mapped_len;
  1102. tpbuf->dma_addr = dma_map_single(adpt->netdev->dev.parent,
  1103. skb->data + mapped_len,
  1104. tpbuf->length, DMA_TO_DEVICE);
  1105. ret = dma_mapping_error(adpt->netdev->dev.parent,
  1106. tpbuf->dma_addr);
  1107. if (ret)
  1108. goto error;
  1109. TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
  1110. TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
  1111. TPD_BUF_LEN_SET(tpd, tpbuf->length);
  1112. emac_tx_tpd_create(adpt, tx_q, tpd);
  1113. count++;
  1114. }
  1115. for (i = 0; i < nr_frags; i++) {
  1116. struct skb_frag_struct *frag;
  1117. frag = &skb_shinfo(skb)->frags[i];
  1118. tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
  1119. tpbuf->length = frag->size;
  1120. tpbuf->dma_addr = dma_map_page(adpt->netdev->dev.parent,
  1121. frag->page.p, frag->page_offset,
  1122. tpbuf->length, DMA_TO_DEVICE);
  1123. ret = dma_mapping_error(adpt->netdev->dev.parent,
  1124. tpbuf->dma_addr);
  1125. if (ret)
  1126. goto error;
  1127. TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
  1128. TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
  1129. TPD_BUF_LEN_SET(tpd, tpbuf->length);
  1130. emac_tx_tpd_create(adpt, tx_q, tpd);
  1131. count++;
  1132. }
  1133. /* The last tpd */
  1134. wmb();
  1135. emac_tx_tpd_mark_last(adpt, tx_q);
  1136. /* The last buffer info contain the skb address,
  1137. * so it will be freed after unmap
  1138. */
  1139. tpbuf->skb = skb;
  1140. return;
  1141. error:
  1142. /* One of the memory mappings failed, so undo everything */
  1143. tx_q->tpd.produce_idx = first;
  1144. while (count--) {
  1145. tpbuf = GET_TPD_BUFFER(tx_q, first);
  1146. dma_unmap_page(adpt->netdev->dev.parent, tpbuf->dma_addr,
  1147. tpbuf->length, DMA_TO_DEVICE);
  1148. tpbuf->dma_addr = 0;
  1149. tpbuf->length = 0;
  1150. if (++first == tx_q->tpd.count)
  1151. first = 0;
  1152. }
  1153. dev_kfree_skb(skb);
  1154. }
  1155. /* Transmit the packet using specified transmit queue */
  1156. int emac_mac_tx_buf_send(struct emac_adapter *adpt, struct emac_tx_queue *tx_q,
  1157. struct sk_buff *skb)
  1158. {
  1159. struct emac_tpd tpd;
  1160. u32 prod_idx;
  1161. memset(&tpd, 0, sizeof(tpd));
  1162. if (emac_tso_csum(adpt, tx_q, skb, &tpd) != 0) {
  1163. dev_kfree_skb_any(skb);
  1164. return NETDEV_TX_OK;
  1165. }
  1166. if (skb_vlan_tag_present(skb)) {
  1167. u16 tag;
  1168. EMAC_VLAN_TO_TAG(skb_vlan_tag_get(skb), tag);
  1169. TPD_CVLAN_TAG_SET(&tpd, tag);
  1170. TPD_INSTC_SET(&tpd, 1);
  1171. }
  1172. if (skb_network_offset(skb) != ETH_HLEN)
  1173. TPD_TYP_SET(&tpd, 1);
  1174. emac_tx_fill_tpd(adpt, tx_q, skb, &tpd);
  1175. netdev_sent_queue(adpt->netdev, skb->len);
  1176. /* Make sure the are enough free descriptors to hold one
  1177. * maximum-sized SKB. We need one desc for each fragment,
  1178. * one for the checksum (emac_tso_csum), one for TSO, and
  1179. * and one for the SKB header.
  1180. */
  1181. if (emac_tpd_num_free_descs(tx_q) < (MAX_SKB_FRAGS + 3))
  1182. netif_stop_queue(adpt->netdev);
  1183. /* update produce idx */
  1184. prod_idx = (tx_q->tpd.produce_idx << tx_q->produce_shift) &
  1185. tx_q->produce_mask;
  1186. emac_reg_update32(adpt->base + tx_q->produce_reg,
  1187. tx_q->produce_mask, prod_idx);
  1188. return NETDEV_TX_OK;
  1189. }