emac-mac.c 45 KB

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