emac-mac.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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. /* TPD (Transmit Packet Descriptor) */
  259. writel(upper_32_bits(adpt->tx_q.tpd.dma_addr),
  260. adpt->base + EMAC_DESC_CTRL_1);
  261. writel(lower_32_bits(adpt->tx_q.tpd.dma_addr),
  262. adpt->base + EMAC_DESC_CTRL_8);
  263. writel(adpt->tx_q.tpd.count & TPD_RING_SIZE_BMSK,
  264. adpt->base + EMAC_DESC_CTRL_9);
  265. /* RFD (Receive Free Descriptor) & RRD (Receive Return Descriptor) */
  266. writel(upper_32_bits(adpt->rx_q.rfd.dma_addr),
  267. adpt->base + EMAC_DESC_CTRL_0);
  268. writel(lower_32_bits(adpt->rx_q.rfd.dma_addr),
  269. adpt->base + EMAC_DESC_CTRL_2);
  270. writel(lower_32_bits(adpt->rx_q.rrd.dma_addr),
  271. adpt->base + EMAC_DESC_CTRL_5);
  272. writel(adpt->rx_q.rfd.count & RFD_RING_SIZE_BMSK,
  273. adpt->base + EMAC_DESC_CTRL_3);
  274. writel(adpt->rx_q.rrd.count & RRD_RING_SIZE_BMSK,
  275. adpt->base + EMAC_DESC_CTRL_6);
  276. writel(adpt->rxbuf_size & RX_BUFFER_SIZE_BMSK,
  277. adpt->base + EMAC_DESC_CTRL_4);
  278. writel(0, adpt->base + EMAC_DESC_CTRL_11);
  279. /* Load all of the base addresses above and ensure that triggering HW to
  280. * read ring pointers is flushed
  281. */
  282. writel(1, adpt->base + EMAC_INTER_SRAM_PART9);
  283. }
  284. /* Config transmit parameters */
  285. static void emac_mac_tx_config(struct emac_adapter *adpt)
  286. {
  287. u32 val;
  288. writel((EMAC_MAX_TX_OFFLOAD_THRESH >> 3) &
  289. JUMBO_TASK_OFFLOAD_THRESHOLD_BMSK, adpt->base + EMAC_TXQ_CTRL_1);
  290. val = (adpt->tpd_burst << NUM_TPD_BURST_PREF_SHFT) &
  291. NUM_TPD_BURST_PREF_BMSK;
  292. val |= TXQ_MODE | LS_8023_SP;
  293. val |= (0x0100 << NUM_TXF_BURST_PREF_SHFT) &
  294. NUM_TXF_BURST_PREF_BMSK;
  295. writel(val, adpt->base + EMAC_TXQ_CTRL_0);
  296. emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_2,
  297. (TXF_HWM_BMSK | TXF_LWM_BMSK), 0);
  298. }
  299. /* Config receive parameters */
  300. static void emac_mac_rx_config(struct emac_adapter *adpt)
  301. {
  302. u32 val;
  303. val = (adpt->rfd_burst << NUM_RFD_BURST_PREF_SHFT) &
  304. NUM_RFD_BURST_PREF_BMSK;
  305. val |= (SP_IPV6 | CUT_THRU_EN);
  306. writel(val, adpt->base + EMAC_RXQ_CTRL_0);
  307. val = readl(adpt->base + EMAC_RXQ_CTRL_1);
  308. val &= ~(JUMBO_1KAH_BMSK | RFD_PREF_LOW_THRESHOLD_BMSK |
  309. RFD_PREF_UP_THRESHOLD_BMSK);
  310. val |= (JUMBO_1KAH << JUMBO_1KAH_SHFT) |
  311. (RFD_PREF_LOW_TH << RFD_PREF_LOW_THRESHOLD_SHFT) |
  312. (RFD_PREF_UP_TH << RFD_PREF_UP_THRESHOLD_SHFT);
  313. writel(val, adpt->base + EMAC_RXQ_CTRL_1);
  314. val = readl(adpt->base + EMAC_RXQ_CTRL_2);
  315. val &= ~(RXF_DOF_THRESHOLD_BMSK | RXF_UOF_THRESHOLD_BMSK);
  316. val |= (RXF_DOF_THRESFHOLD << RXF_DOF_THRESHOLD_SHFT) |
  317. (RXF_UOF_THRESFHOLD << RXF_UOF_THRESHOLD_SHFT);
  318. writel(val, adpt->base + EMAC_RXQ_CTRL_2);
  319. val = readl(adpt->base + EMAC_RXQ_CTRL_3);
  320. val &= ~(RXD_TIMER_BMSK | RXD_THRESHOLD_BMSK);
  321. val |= RXD_TH << RXD_THRESHOLD_SHFT;
  322. writel(val, adpt->base + EMAC_RXQ_CTRL_3);
  323. }
  324. /* Config dma */
  325. static void emac_mac_dma_config(struct emac_adapter *adpt)
  326. {
  327. u32 dma_ctrl = DMAR_REQ_PRI;
  328. switch (adpt->dma_order) {
  329. case emac_dma_ord_in:
  330. dma_ctrl |= IN_ORDER_MODE;
  331. break;
  332. case emac_dma_ord_enh:
  333. dma_ctrl |= ENH_ORDER_MODE;
  334. break;
  335. case emac_dma_ord_out:
  336. dma_ctrl |= OUT_ORDER_MODE;
  337. break;
  338. default:
  339. break;
  340. }
  341. dma_ctrl |= (((u32)adpt->dmar_block) << REGRDBLEN_SHFT) &
  342. REGRDBLEN_BMSK;
  343. dma_ctrl |= (((u32)adpt->dmaw_block) << REGWRBLEN_SHFT) &
  344. REGWRBLEN_BMSK;
  345. dma_ctrl |= (((u32)adpt->dmar_dly_cnt) << DMAR_DLY_CNT_SHFT) &
  346. DMAR_DLY_CNT_BMSK;
  347. dma_ctrl |= (((u32)adpt->dmaw_dly_cnt) << DMAW_DLY_CNT_SHFT) &
  348. DMAW_DLY_CNT_BMSK;
  349. /* config DMA and ensure that configuration is flushed to HW */
  350. writel(dma_ctrl, adpt->base + EMAC_DMA_CTRL);
  351. }
  352. /* set MAC address */
  353. static void emac_set_mac_address(struct emac_adapter *adpt, u8 *addr)
  354. {
  355. u32 sta;
  356. /* for example: 00-A0-C6-11-22-33
  357. * 0<-->C6112233, 1<-->00A0.
  358. */
  359. /* low 32bit word */
  360. sta = (((u32)addr[2]) << 24) | (((u32)addr[3]) << 16) |
  361. (((u32)addr[4]) << 8) | (((u32)addr[5]));
  362. writel(sta, adpt->base + EMAC_MAC_STA_ADDR0);
  363. /* hight 32bit word */
  364. sta = (((u32)addr[0]) << 8) | (u32)addr[1];
  365. writel(sta, adpt->base + EMAC_MAC_STA_ADDR1);
  366. }
  367. static void emac_mac_config(struct emac_adapter *adpt)
  368. {
  369. struct net_device *netdev = adpt->netdev;
  370. unsigned int max_frame;
  371. u32 val;
  372. emac_set_mac_address(adpt, netdev->dev_addr);
  373. max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  374. adpt->rxbuf_size = netdev->mtu > EMAC_DEF_RX_BUF_SIZE ?
  375. ALIGN(max_frame, 8) : EMAC_DEF_RX_BUF_SIZE;
  376. emac_mac_dma_rings_config(adpt);
  377. writel(netdev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
  378. adpt->base + EMAC_MAX_FRAM_LEN_CTRL);
  379. emac_mac_tx_config(adpt);
  380. emac_mac_rx_config(adpt);
  381. emac_mac_dma_config(adpt);
  382. val = readl(adpt->base + EMAC_AXI_MAST_CTRL);
  383. val &= ~(DATA_BYTE_SWAP | MAX_BOUND);
  384. val |= MAX_BTYPE;
  385. writel(val, adpt->base + EMAC_AXI_MAST_CTRL);
  386. writel(0, adpt->base + EMAC_CLK_GATE_CTRL);
  387. writel(RX_UNCPL_INT_EN, adpt->base + EMAC_MISC_CTRL);
  388. }
  389. void emac_mac_reset(struct emac_adapter *adpt)
  390. {
  391. emac_mac_stop(adpt);
  392. emac_reg_update32(adpt->base + EMAC_DMA_MAS_CTRL, 0, SOFT_RST);
  393. usleep_range(100, 150); /* reset may take up to 100usec */
  394. /* interrupt clear-on-read */
  395. emac_reg_update32(adpt->base + EMAC_DMA_MAS_CTRL, 0, INT_RD_CLR_EN);
  396. }
  397. static void emac_mac_start(struct emac_adapter *adpt)
  398. {
  399. struct phy_device *phydev = adpt->phydev;
  400. u32 mac, csr1;
  401. /* enable tx queue */
  402. emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, 0, TXQ_EN);
  403. /* enable rx queue */
  404. emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, 0, RXQ_EN);
  405. /* enable mac control */
  406. mac = readl(adpt->base + EMAC_MAC_CTRL);
  407. csr1 = readl(adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
  408. mac |= TXEN | RXEN; /* enable RX/TX */
  409. /* Configure MAC flow control. If set to automatic, then match
  410. * whatever the PHY does. Otherwise, enable or disable it, depending
  411. * on what the user configured via ethtool.
  412. */
  413. mac &= ~(RXFC | TXFC);
  414. if (adpt->automatic) {
  415. /* If it's set to automatic, then update our local values */
  416. adpt->rx_flow_control = phydev->pause;
  417. adpt->tx_flow_control = phydev->pause != phydev->asym_pause;
  418. }
  419. mac |= adpt->rx_flow_control ? RXFC : 0;
  420. mac |= adpt->tx_flow_control ? TXFC : 0;
  421. /* setup link speed */
  422. mac &= ~SPEED_MASK;
  423. if (phydev->speed == SPEED_1000) {
  424. mac |= SPEED(2);
  425. csr1 |= FREQ_MODE;
  426. } else {
  427. mac |= SPEED(1);
  428. csr1 &= ~FREQ_MODE;
  429. }
  430. if (phydev->duplex == DUPLEX_FULL)
  431. mac |= FULLD;
  432. else
  433. mac &= ~FULLD;
  434. /* other parameters */
  435. mac |= (CRCE | PCRCE);
  436. mac |= ((adpt->preamble << PRLEN_SHFT) & PRLEN_BMSK);
  437. mac |= BROAD_EN;
  438. mac |= FLCHK;
  439. mac &= ~RX_CHKSUM_EN;
  440. mac &= ~(HUGEN | VLAN_STRIP | TPAUSE | SIMR | HUGE | MULTI_ALL |
  441. DEBUG_MODE | SINGLE_PAUSE_MODE);
  442. /* Enable single-pause-frame mode if requested.
  443. *
  444. * If enabled, the EMAC will send a single pause frame when the RX
  445. * queue is full. This normally leads to packet loss because
  446. * the pause frame disables the remote MAC only for 33ms (the quanta),
  447. * and then the remote MAC continues sending packets even though
  448. * the RX queue is still full.
  449. *
  450. * If disabled, the EMAC sends a pause frame every 31ms until the RX
  451. * queue is no longer full. Normally, this is the preferred
  452. * method of operation. However, when the system is hung (e.g.
  453. * cores are halted), the EMAC interrupt handler is never called
  454. * and so the RX queue fills up quickly and stays full. The resuling
  455. * non-stop "flood" of pause frames sometimes has the effect of
  456. * disabling nearby switches. In some cases, other nearby switches
  457. * are also affected, shutting down the entire network.
  458. *
  459. * The user can enable or disable single-pause-frame mode
  460. * via ethtool.
  461. */
  462. mac |= adpt->single_pause_mode ? SINGLE_PAUSE_MODE : 0;
  463. writel_relaxed(csr1, adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
  464. writel_relaxed(mac, adpt->base + EMAC_MAC_CTRL);
  465. /* enable interrupt read clear, low power sleep mode and
  466. * the irq moderators
  467. */
  468. writel_relaxed(adpt->irq_mod, adpt->base + EMAC_IRQ_MOD_TIM_INIT);
  469. writel_relaxed(INT_RD_CLR_EN | LPW_MODE | IRQ_MODERATOR_EN |
  470. IRQ_MODERATOR2_EN, adpt->base + EMAC_DMA_MAS_CTRL);
  471. emac_mac_mode_config(adpt);
  472. emac_reg_update32(adpt->base + EMAC_ATHR_HEADER_CTRL,
  473. (HEADER_ENABLE | HEADER_CNT_EN), 0);
  474. }
  475. void emac_mac_stop(struct emac_adapter *adpt)
  476. {
  477. emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, RXQ_EN, 0);
  478. emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, TXQ_EN, 0);
  479. emac_reg_update32(adpt->base + EMAC_MAC_CTRL, TXEN | RXEN, 0);
  480. usleep_range(1000, 1050); /* stopping mac may take upto 1msec */
  481. }
  482. /* Free all descriptors of given transmit queue */
  483. static void emac_tx_q_descs_free(struct emac_adapter *adpt)
  484. {
  485. struct emac_tx_queue *tx_q = &adpt->tx_q;
  486. unsigned int i;
  487. size_t size;
  488. /* ring already cleared, nothing to do */
  489. if (!tx_q->tpd.tpbuff)
  490. return;
  491. for (i = 0; i < tx_q->tpd.count; i++) {
  492. struct emac_buffer *tpbuf = GET_TPD_BUFFER(tx_q, i);
  493. if (tpbuf->dma_addr) {
  494. dma_unmap_single(adpt->netdev->dev.parent,
  495. tpbuf->dma_addr, tpbuf->length,
  496. DMA_TO_DEVICE);
  497. tpbuf->dma_addr = 0;
  498. }
  499. if (tpbuf->skb) {
  500. dev_kfree_skb_any(tpbuf->skb);
  501. tpbuf->skb = NULL;
  502. }
  503. }
  504. size = sizeof(struct emac_buffer) * tx_q->tpd.count;
  505. memset(tx_q->tpd.tpbuff, 0, size);
  506. /* clear the descriptor ring */
  507. memset(tx_q->tpd.v_addr, 0, tx_q->tpd.size);
  508. tx_q->tpd.consume_idx = 0;
  509. tx_q->tpd.produce_idx = 0;
  510. }
  511. /* Free all descriptors of given receive queue */
  512. static void emac_rx_q_free_descs(struct emac_adapter *adpt)
  513. {
  514. struct device *dev = adpt->netdev->dev.parent;
  515. struct emac_rx_queue *rx_q = &adpt->rx_q;
  516. unsigned int i;
  517. size_t size;
  518. /* ring already cleared, nothing to do */
  519. if (!rx_q->rfd.rfbuff)
  520. return;
  521. for (i = 0; i < rx_q->rfd.count; i++) {
  522. struct emac_buffer *rfbuf = GET_RFD_BUFFER(rx_q, i);
  523. if (rfbuf->dma_addr) {
  524. dma_unmap_single(dev, rfbuf->dma_addr, rfbuf->length,
  525. DMA_FROM_DEVICE);
  526. rfbuf->dma_addr = 0;
  527. }
  528. if (rfbuf->skb) {
  529. dev_kfree_skb(rfbuf->skb);
  530. rfbuf->skb = NULL;
  531. }
  532. }
  533. size = sizeof(struct emac_buffer) * rx_q->rfd.count;
  534. memset(rx_q->rfd.rfbuff, 0, size);
  535. /* clear the descriptor rings */
  536. memset(rx_q->rrd.v_addr, 0, rx_q->rrd.size);
  537. rx_q->rrd.produce_idx = 0;
  538. rx_q->rrd.consume_idx = 0;
  539. memset(rx_q->rfd.v_addr, 0, rx_q->rfd.size);
  540. rx_q->rfd.produce_idx = 0;
  541. rx_q->rfd.consume_idx = 0;
  542. }
  543. /* Free all buffers associated with given transmit queue */
  544. static void emac_tx_q_bufs_free(struct emac_adapter *adpt)
  545. {
  546. struct emac_tx_queue *tx_q = &adpt->tx_q;
  547. emac_tx_q_descs_free(adpt);
  548. kfree(tx_q->tpd.tpbuff);
  549. tx_q->tpd.tpbuff = NULL;
  550. tx_q->tpd.v_addr = NULL;
  551. tx_q->tpd.dma_addr = 0;
  552. tx_q->tpd.size = 0;
  553. }
  554. /* Allocate TX descriptor ring for the given transmit queue */
  555. static int emac_tx_q_desc_alloc(struct emac_adapter *adpt,
  556. struct emac_tx_queue *tx_q)
  557. {
  558. struct emac_ring_header *ring_header = &adpt->ring_header;
  559. int node = dev_to_node(adpt->netdev->dev.parent);
  560. size_t size;
  561. size = sizeof(struct emac_buffer) * tx_q->tpd.count;
  562. tx_q->tpd.tpbuff = kzalloc_node(size, GFP_KERNEL, node);
  563. if (!tx_q->tpd.tpbuff)
  564. return -ENOMEM;
  565. tx_q->tpd.size = tx_q->tpd.count * (adpt->tpd_size * 4);
  566. tx_q->tpd.dma_addr = ring_header->dma_addr + ring_header->used;
  567. tx_q->tpd.v_addr = ring_header->v_addr + ring_header->used;
  568. ring_header->used += ALIGN(tx_q->tpd.size, 8);
  569. tx_q->tpd.produce_idx = 0;
  570. tx_q->tpd.consume_idx = 0;
  571. return 0;
  572. }
  573. /* Free all buffers associated with given transmit queue */
  574. static void emac_rx_q_bufs_free(struct emac_adapter *adpt)
  575. {
  576. struct emac_rx_queue *rx_q = &adpt->rx_q;
  577. emac_rx_q_free_descs(adpt);
  578. kfree(rx_q->rfd.rfbuff);
  579. rx_q->rfd.rfbuff = NULL;
  580. rx_q->rfd.v_addr = NULL;
  581. rx_q->rfd.dma_addr = 0;
  582. rx_q->rfd.size = 0;
  583. rx_q->rrd.v_addr = NULL;
  584. rx_q->rrd.dma_addr = 0;
  585. rx_q->rrd.size = 0;
  586. }
  587. /* Allocate RX descriptor rings for the given receive queue */
  588. static int emac_rx_descs_alloc(struct emac_adapter *adpt)
  589. {
  590. struct emac_ring_header *ring_header = &adpt->ring_header;
  591. int node = dev_to_node(adpt->netdev->dev.parent);
  592. struct emac_rx_queue *rx_q = &adpt->rx_q;
  593. size_t size;
  594. size = sizeof(struct emac_buffer) * rx_q->rfd.count;
  595. rx_q->rfd.rfbuff = kzalloc_node(size, GFP_KERNEL, node);
  596. if (!rx_q->rfd.rfbuff)
  597. return -ENOMEM;
  598. rx_q->rrd.size = rx_q->rrd.count * (adpt->rrd_size * 4);
  599. rx_q->rfd.size = rx_q->rfd.count * (adpt->rfd_size * 4);
  600. rx_q->rrd.dma_addr = ring_header->dma_addr + ring_header->used;
  601. rx_q->rrd.v_addr = ring_header->v_addr + ring_header->used;
  602. ring_header->used += ALIGN(rx_q->rrd.size, 8);
  603. rx_q->rfd.dma_addr = ring_header->dma_addr + ring_header->used;
  604. rx_q->rfd.v_addr = ring_header->v_addr + ring_header->used;
  605. ring_header->used += ALIGN(rx_q->rfd.size, 8);
  606. rx_q->rrd.produce_idx = 0;
  607. rx_q->rrd.consume_idx = 0;
  608. rx_q->rfd.produce_idx = 0;
  609. rx_q->rfd.consume_idx = 0;
  610. return 0;
  611. }
  612. /* Allocate all TX and RX descriptor rings */
  613. int emac_mac_rx_tx_rings_alloc_all(struct emac_adapter *adpt)
  614. {
  615. struct emac_ring_header *ring_header = &adpt->ring_header;
  616. struct device *dev = adpt->netdev->dev.parent;
  617. unsigned int num_tx_descs = adpt->tx_desc_cnt;
  618. unsigned int num_rx_descs = adpt->rx_desc_cnt;
  619. int ret;
  620. adpt->tx_q.tpd.count = adpt->tx_desc_cnt;
  621. adpt->rx_q.rrd.count = adpt->rx_desc_cnt;
  622. adpt->rx_q.rfd.count = adpt->rx_desc_cnt;
  623. /* Ring DMA buffer. Each ring may need up to 8 bytes for alignment,
  624. * hence the additional padding bytes are allocated.
  625. */
  626. ring_header->size = num_tx_descs * (adpt->tpd_size * 4) +
  627. num_rx_descs * (adpt->rfd_size * 4) +
  628. num_rx_descs * (adpt->rrd_size * 4) +
  629. 8 + 2 * 8; /* 8 byte per one Tx and two Rx rings */
  630. ring_header->used = 0;
  631. ring_header->v_addr = dma_zalloc_coherent(dev, ring_header->size,
  632. &ring_header->dma_addr,
  633. GFP_KERNEL);
  634. if (!ring_header->v_addr)
  635. return -ENOMEM;
  636. ring_header->used = ALIGN(ring_header->dma_addr, 8) -
  637. ring_header->dma_addr;
  638. ret = emac_tx_q_desc_alloc(adpt, &adpt->tx_q);
  639. if (ret) {
  640. netdev_err(adpt->netdev, "error: Tx Queue alloc failed\n");
  641. goto err_alloc_tx;
  642. }
  643. ret = emac_rx_descs_alloc(adpt);
  644. if (ret) {
  645. netdev_err(adpt->netdev, "error: Rx Queue alloc failed\n");
  646. goto err_alloc_rx;
  647. }
  648. return 0;
  649. err_alloc_rx:
  650. emac_tx_q_bufs_free(adpt);
  651. err_alloc_tx:
  652. dma_free_coherent(dev, ring_header->size,
  653. ring_header->v_addr, ring_header->dma_addr);
  654. ring_header->v_addr = NULL;
  655. ring_header->dma_addr = 0;
  656. ring_header->size = 0;
  657. ring_header->used = 0;
  658. return ret;
  659. }
  660. /* Free all TX and RX descriptor rings */
  661. void emac_mac_rx_tx_rings_free_all(struct emac_adapter *adpt)
  662. {
  663. struct emac_ring_header *ring_header = &adpt->ring_header;
  664. struct device *dev = adpt->netdev->dev.parent;
  665. emac_tx_q_bufs_free(adpt);
  666. emac_rx_q_bufs_free(adpt);
  667. dma_free_coherent(dev, ring_header->size,
  668. ring_header->v_addr, ring_header->dma_addr);
  669. ring_header->v_addr = NULL;
  670. ring_header->dma_addr = 0;
  671. ring_header->size = 0;
  672. ring_header->used = 0;
  673. }
  674. /* Initialize descriptor rings */
  675. static void emac_mac_rx_tx_ring_reset_all(struct emac_adapter *adpt)
  676. {
  677. unsigned int i;
  678. adpt->tx_q.tpd.produce_idx = 0;
  679. adpt->tx_q.tpd.consume_idx = 0;
  680. for (i = 0; i < adpt->tx_q.tpd.count; i++)
  681. adpt->tx_q.tpd.tpbuff[i].dma_addr = 0;
  682. adpt->rx_q.rrd.produce_idx = 0;
  683. adpt->rx_q.rrd.consume_idx = 0;
  684. adpt->rx_q.rfd.produce_idx = 0;
  685. adpt->rx_q.rfd.consume_idx = 0;
  686. for (i = 0; i < adpt->rx_q.rfd.count; i++)
  687. adpt->rx_q.rfd.rfbuff[i].dma_addr = 0;
  688. }
  689. /* Produce new receive free descriptor */
  690. static void emac_mac_rx_rfd_create(struct emac_adapter *adpt,
  691. struct emac_rx_queue *rx_q,
  692. dma_addr_t addr)
  693. {
  694. u32 *hw_rfd = EMAC_RFD(rx_q, adpt->rfd_size, rx_q->rfd.produce_idx);
  695. *(hw_rfd++) = lower_32_bits(addr);
  696. *hw_rfd = upper_32_bits(addr);
  697. if (++rx_q->rfd.produce_idx == rx_q->rfd.count)
  698. rx_q->rfd.produce_idx = 0;
  699. }
  700. /* Fill up receive queue's RFD with preallocated receive buffers */
  701. static void emac_mac_rx_descs_refill(struct emac_adapter *adpt,
  702. struct emac_rx_queue *rx_q)
  703. {
  704. struct emac_buffer *curr_rxbuf;
  705. struct emac_buffer *next_rxbuf;
  706. unsigned int count = 0;
  707. u32 next_produce_idx;
  708. next_produce_idx = rx_q->rfd.produce_idx + 1;
  709. if (next_produce_idx == rx_q->rfd.count)
  710. next_produce_idx = 0;
  711. curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
  712. next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
  713. /* this always has a blank rx_buffer*/
  714. while (!next_rxbuf->dma_addr) {
  715. struct sk_buff *skb;
  716. int ret;
  717. skb = netdev_alloc_skb_ip_align(adpt->netdev, adpt->rxbuf_size);
  718. if (!skb)
  719. break;
  720. curr_rxbuf->dma_addr =
  721. dma_map_single(adpt->netdev->dev.parent, skb->data,
  722. adpt->rxbuf_size, DMA_FROM_DEVICE);
  723. ret = dma_mapping_error(adpt->netdev->dev.parent,
  724. curr_rxbuf->dma_addr);
  725. if (ret) {
  726. dev_kfree_skb(skb);
  727. break;
  728. }
  729. curr_rxbuf->skb = skb;
  730. curr_rxbuf->length = adpt->rxbuf_size;
  731. emac_mac_rx_rfd_create(adpt, rx_q, curr_rxbuf->dma_addr);
  732. next_produce_idx = rx_q->rfd.produce_idx + 1;
  733. if (next_produce_idx == rx_q->rfd.count)
  734. next_produce_idx = 0;
  735. curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
  736. next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
  737. count++;
  738. }
  739. if (count) {
  740. u32 prod_idx = (rx_q->rfd.produce_idx << rx_q->produce_shift) &
  741. rx_q->produce_mask;
  742. emac_reg_update32(adpt->base + rx_q->produce_reg,
  743. rx_q->produce_mask, prod_idx);
  744. }
  745. }
  746. static void emac_adjust_link(struct net_device *netdev)
  747. {
  748. struct emac_adapter *adpt = netdev_priv(netdev);
  749. struct phy_device *phydev = netdev->phydev;
  750. if (phydev->link) {
  751. emac_mac_start(adpt);
  752. emac_sgmii_link_change(adpt, true);
  753. } else {
  754. emac_sgmii_link_change(adpt, false);
  755. emac_mac_stop(adpt);
  756. }
  757. phy_print_status(phydev);
  758. }
  759. /* Bringup the interface/HW */
  760. int emac_mac_up(struct emac_adapter *adpt)
  761. {
  762. struct net_device *netdev = adpt->netdev;
  763. int ret;
  764. emac_mac_rx_tx_ring_reset_all(adpt);
  765. emac_mac_config(adpt);
  766. emac_mac_rx_descs_refill(adpt, &adpt->rx_q);
  767. adpt->phydev->irq = PHY_POLL;
  768. ret = phy_connect_direct(netdev, adpt->phydev, emac_adjust_link,
  769. PHY_INTERFACE_MODE_SGMII);
  770. if (ret) {
  771. netdev_err(adpt->netdev, "could not connect phy\n");
  772. return ret;
  773. }
  774. phy_attached_print(adpt->phydev, NULL);
  775. /* enable mac irq */
  776. writel((u32)~DIS_INT, adpt->base + EMAC_INT_STATUS);
  777. writel(adpt->irq.mask, adpt->base + EMAC_INT_MASK);
  778. phy_start(adpt->phydev);
  779. napi_enable(&adpt->rx_q.napi);
  780. netif_start_queue(netdev);
  781. return 0;
  782. }
  783. /* Bring down the interface/HW */
  784. void emac_mac_down(struct emac_adapter *adpt)
  785. {
  786. struct net_device *netdev = adpt->netdev;
  787. netif_stop_queue(netdev);
  788. napi_disable(&adpt->rx_q.napi);
  789. phy_stop(adpt->phydev);
  790. /* Interrupts must be disabled before the PHY is disconnected, to
  791. * avoid a race condition where adjust_link is null when we get
  792. * an interrupt.
  793. */
  794. writel(DIS_INT, adpt->base + EMAC_INT_STATUS);
  795. writel(0, adpt->base + EMAC_INT_MASK);
  796. synchronize_irq(adpt->irq.irq);
  797. phy_disconnect(adpt->phydev);
  798. emac_mac_reset(adpt);
  799. emac_tx_q_descs_free(adpt);
  800. netdev_reset_queue(adpt->netdev);
  801. emac_rx_q_free_descs(adpt);
  802. }
  803. /* Consume next received packet descriptor */
  804. static bool emac_rx_process_rrd(struct emac_adapter *adpt,
  805. struct emac_rx_queue *rx_q,
  806. struct emac_rrd *rrd)
  807. {
  808. u32 *hw_rrd = EMAC_RRD(rx_q, adpt->rrd_size, rx_q->rrd.consume_idx);
  809. rrd->word[3] = *(hw_rrd + 3);
  810. if (!RRD_UPDT(rrd))
  811. return false;
  812. rrd->word[4] = 0;
  813. rrd->word[5] = 0;
  814. rrd->word[0] = *(hw_rrd++);
  815. rrd->word[1] = *(hw_rrd++);
  816. rrd->word[2] = *(hw_rrd++);
  817. if (unlikely(RRD_NOR(rrd) != 1)) {
  818. netdev_err(adpt->netdev,
  819. "error: multi-RFD not support yet! nor:%lu\n",
  820. RRD_NOR(rrd));
  821. }
  822. /* mark rrd as processed */
  823. RRD_UPDT_SET(rrd, 0);
  824. *hw_rrd = rrd->word[3];
  825. if (++rx_q->rrd.consume_idx == rx_q->rrd.count)
  826. rx_q->rrd.consume_idx = 0;
  827. return true;
  828. }
  829. /* Produce new transmit descriptor */
  830. static void emac_tx_tpd_create(struct emac_adapter *adpt,
  831. struct emac_tx_queue *tx_q, struct emac_tpd *tpd)
  832. {
  833. u32 *hw_tpd;
  834. tx_q->tpd.last_produce_idx = tx_q->tpd.produce_idx;
  835. hw_tpd = EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.produce_idx);
  836. if (++tx_q->tpd.produce_idx == tx_q->tpd.count)
  837. tx_q->tpd.produce_idx = 0;
  838. *(hw_tpd++) = tpd->word[0];
  839. *(hw_tpd++) = tpd->word[1];
  840. *(hw_tpd++) = tpd->word[2];
  841. *hw_tpd = tpd->word[3];
  842. }
  843. /* Mark the last transmit descriptor as such (for the transmit packet) */
  844. static void emac_tx_tpd_mark_last(struct emac_adapter *adpt,
  845. struct emac_tx_queue *tx_q)
  846. {
  847. u32 *hw_tpd =
  848. EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.last_produce_idx);
  849. u32 tmp_tpd;
  850. tmp_tpd = *(hw_tpd + 1);
  851. tmp_tpd |= EMAC_TPD_LAST_FRAGMENT;
  852. *(hw_tpd + 1) = tmp_tpd;
  853. }
  854. static void emac_rx_rfd_clean(struct emac_rx_queue *rx_q, struct emac_rrd *rrd)
  855. {
  856. struct emac_buffer *rfbuf = rx_q->rfd.rfbuff;
  857. u32 consume_idx = RRD_SI(rrd);
  858. unsigned int i;
  859. for (i = 0; i < RRD_NOR(rrd); i++) {
  860. rfbuf[consume_idx].skb = NULL;
  861. if (++consume_idx == rx_q->rfd.count)
  862. consume_idx = 0;
  863. }
  864. rx_q->rfd.consume_idx = consume_idx;
  865. rx_q->rfd.process_idx = consume_idx;
  866. }
  867. /* Push the received skb to upper layers */
  868. static void emac_receive_skb(struct emac_rx_queue *rx_q,
  869. struct sk_buff *skb,
  870. u16 vlan_tag, bool vlan_flag)
  871. {
  872. if (vlan_flag) {
  873. u16 vlan;
  874. EMAC_TAG_TO_VLAN(vlan_tag, vlan);
  875. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
  876. }
  877. napi_gro_receive(&rx_q->napi, skb);
  878. }
  879. /* Process receive event */
  880. void emac_mac_rx_process(struct emac_adapter *adpt, struct emac_rx_queue *rx_q,
  881. int *num_pkts, int max_pkts)
  882. {
  883. u32 proc_idx, hw_consume_idx, num_consume_pkts;
  884. struct net_device *netdev = adpt->netdev;
  885. struct emac_buffer *rfbuf;
  886. unsigned int count = 0;
  887. struct emac_rrd rrd;
  888. struct sk_buff *skb;
  889. u32 reg;
  890. reg = readl_relaxed(adpt->base + rx_q->consume_reg);
  891. hw_consume_idx = (reg & rx_q->consume_mask) >> rx_q->consume_shift;
  892. num_consume_pkts = (hw_consume_idx >= rx_q->rrd.consume_idx) ?
  893. (hw_consume_idx - rx_q->rrd.consume_idx) :
  894. (hw_consume_idx + rx_q->rrd.count - rx_q->rrd.consume_idx);
  895. do {
  896. if (!num_consume_pkts)
  897. break;
  898. if (!emac_rx_process_rrd(adpt, rx_q, &rrd))
  899. break;
  900. if (likely(RRD_NOR(&rrd) == 1)) {
  901. /* good receive */
  902. rfbuf = GET_RFD_BUFFER(rx_q, RRD_SI(&rrd));
  903. dma_unmap_single(adpt->netdev->dev.parent,
  904. rfbuf->dma_addr, rfbuf->length,
  905. DMA_FROM_DEVICE);
  906. rfbuf->dma_addr = 0;
  907. skb = rfbuf->skb;
  908. } else {
  909. netdev_err(adpt->netdev,
  910. "error: multi-RFD not support yet!\n");
  911. break;
  912. }
  913. emac_rx_rfd_clean(rx_q, &rrd);
  914. num_consume_pkts--;
  915. count++;
  916. /* Due to a HW issue in L4 check sum detection (UDP/TCP frags
  917. * with DF set are marked as error), drop packets based on the
  918. * error mask rather than the summary bit (ignoring L4F errors)
  919. */
  920. if (rrd.word[EMAC_RRD_STATS_DW_IDX] & EMAC_RRD_ERROR) {
  921. netif_dbg(adpt, rx_status, adpt->netdev,
  922. "Drop error packet[RRD: 0x%x:0x%x:0x%x:0x%x]\n",
  923. rrd.word[0], rrd.word[1],
  924. rrd.word[2], rrd.word[3]);
  925. dev_kfree_skb(skb);
  926. continue;
  927. }
  928. skb_put(skb, RRD_PKT_SIZE(&rrd) - ETH_FCS_LEN);
  929. skb->dev = netdev;
  930. skb->protocol = eth_type_trans(skb, skb->dev);
  931. if (netdev->features & NETIF_F_RXCSUM)
  932. skb->ip_summed = RRD_L4F(&rrd) ?
  933. CHECKSUM_NONE : CHECKSUM_UNNECESSARY;
  934. else
  935. skb_checksum_none_assert(skb);
  936. emac_receive_skb(rx_q, skb, (u16)RRD_CVALN_TAG(&rrd),
  937. (bool)RRD_CVTAG(&rrd));
  938. (*num_pkts)++;
  939. } while (*num_pkts < max_pkts);
  940. if (count) {
  941. proc_idx = (rx_q->rfd.process_idx << rx_q->process_shft) &
  942. rx_q->process_mask;
  943. emac_reg_update32(adpt->base + rx_q->process_reg,
  944. rx_q->process_mask, proc_idx);
  945. emac_mac_rx_descs_refill(adpt, rx_q);
  946. }
  947. }
  948. /* get the number of free transmit descriptors */
  949. static unsigned int emac_tpd_num_free_descs(struct emac_tx_queue *tx_q)
  950. {
  951. u32 produce_idx = tx_q->tpd.produce_idx;
  952. u32 consume_idx = tx_q->tpd.consume_idx;
  953. return (consume_idx > produce_idx) ?
  954. (consume_idx - produce_idx - 1) :
  955. (tx_q->tpd.count + consume_idx - produce_idx - 1);
  956. }
  957. /* Process transmit event */
  958. void emac_mac_tx_process(struct emac_adapter *adpt, struct emac_tx_queue *tx_q)
  959. {
  960. u32 reg = readl_relaxed(adpt->base + tx_q->consume_reg);
  961. u32 hw_consume_idx, pkts_compl = 0, bytes_compl = 0;
  962. struct emac_buffer *tpbuf;
  963. hw_consume_idx = (reg & tx_q->consume_mask) >> tx_q->consume_shift;
  964. while (tx_q->tpd.consume_idx != hw_consume_idx) {
  965. tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.consume_idx);
  966. if (tpbuf->dma_addr) {
  967. dma_unmap_page(adpt->netdev->dev.parent,
  968. tpbuf->dma_addr, tpbuf->length,
  969. DMA_TO_DEVICE);
  970. tpbuf->dma_addr = 0;
  971. }
  972. if (tpbuf->skb) {
  973. pkts_compl++;
  974. bytes_compl += tpbuf->skb->len;
  975. dev_kfree_skb_irq(tpbuf->skb);
  976. tpbuf->skb = NULL;
  977. }
  978. if (++tx_q->tpd.consume_idx == tx_q->tpd.count)
  979. tx_q->tpd.consume_idx = 0;
  980. }
  981. netdev_completed_queue(adpt->netdev, pkts_compl, bytes_compl);
  982. if (netif_queue_stopped(adpt->netdev))
  983. if (emac_tpd_num_free_descs(tx_q) > (MAX_SKB_FRAGS + 1))
  984. netif_wake_queue(adpt->netdev);
  985. }
  986. /* Initialize all queue data structures */
  987. void emac_mac_rx_tx_ring_init_all(struct platform_device *pdev,
  988. struct emac_adapter *adpt)
  989. {
  990. adpt->rx_q.netdev = adpt->netdev;
  991. adpt->rx_q.produce_reg = EMAC_MAILBOX_0;
  992. adpt->rx_q.produce_mask = RFD0_PROD_IDX_BMSK;
  993. adpt->rx_q.produce_shift = RFD0_PROD_IDX_SHFT;
  994. adpt->rx_q.process_reg = EMAC_MAILBOX_0;
  995. adpt->rx_q.process_mask = RFD0_PROC_IDX_BMSK;
  996. adpt->rx_q.process_shft = RFD0_PROC_IDX_SHFT;
  997. adpt->rx_q.consume_reg = EMAC_MAILBOX_3;
  998. adpt->rx_q.consume_mask = RFD0_CONS_IDX_BMSK;
  999. adpt->rx_q.consume_shift = RFD0_CONS_IDX_SHFT;
  1000. adpt->rx_q.irq = &adpt->irq;
  1001. adpt->rx_q.intr = adpt->irq.mask & ISR_RX_PKT;
  1002. adpt->tx_q.produce_reg = EMAC_MAILBOX_15;
  1003. adpt->tx_q.produce_mask = NTPD_PROD_IDX_BMSK;
  1004. adpt->tx_q.produce_shift = NTPD_PROD_IDX_SHFT;
  1005. adpt->tx_q.consume_reg = EMAC_MAILBOX_2;
  1006. adpt->tx_q.consume_mask = NTPD_CONS_IDX_BMSK;
  1007. adpt->tx_q.consume_shift = NTPD_CONS_IDX_SHFT;
  1008. }
  1009. /* Fill up transmit descriptors with TSO and Checksum offload information */
  1010. static int emac_tso_csum(struct emac_adapter *adpt,
  1011. struct emac_tx_queue *tx_q,
  1012. struct sk_buff *skb,
  1013. struct emac_tpd *tpd)
  1014. {
  1015. unsigned int hdr_len;
  1016. int ret;
  1017. if (skb_is_gso(skb)) {
  1018. if (skb_header_cloned(skb)) {
  1019. ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  1020. if (unlikely(ret))
  1021. return ret;
  1022. }
  1023. if (skb->protocol == htons(ETH_P_IP)) {
  1024. u32 pkt_len = ((unsigned char *)ip_hdr(skb) - skb->data)
  1025. + ntohs(ip_hdr(skb)->tot_len);
  1026. if (skb->len > pkt_len)
  1027. pskb_trim(skb, pkt_len);
  1028. }
  1029. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1030. if (unlikely(skb->len == hdr_len)) {
  1031. /* we only need to do csum */
  1032. netif_warn(adpt, tx_err, adpt->netdev,
  1033. "tso not needed for packet with 0 data\n");
  1034. goto do_csum;
  1035. }
  1036. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
  1037. ip_hdr(skb)->check = 0;
  1038. tcp_hdr(skb)->check =
  1039. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  1040. ip_hdr(skb)->daddr,
  1041. 0, IPPROTO_TCP, 0);
  1042. TPD_IPV4_SET(tpd, 1);
  1043. }
  1044. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
  1045. /* ipv6 tso need an extra tpd */
  1046. struct emac_tpd extra_tpd;
  1047. memset(tpd, 0, sizeof(*tpd));
  1048. memset(&extra_tpd, 0, sizeof(extra_tpd));
  1049. ipv6_hdr(skb)->payload_len = 0;
  1050. tcp_hdr(skb)->check =
  1051. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  1052. &ipv6_hdr(skb)->daddr,
  1053. 0, IPPROTO_TCP, 0);
  1054. TPD_PKT_LEN_SET(&extra_tpd, skb->len);
  1055. TPD_LSO_SET(&extra_tpd, 1);
  1056. TPD_LSOV_SET(&extra_tpd, 1);
  1057. emac_tx_tpd_create(adpt, tx_q, &extra_tpd);
  1058. TPD_LSOV_SET(tpd, 1);
  1059. }
  1060. TPD_LSO_SET(tpd, 1);
  1061. TPD_TCPHDR_OFFSET_SET(tpd, skb_transport_offset(skb));
  1062. TPD_MSS_SET(tpd, skb_shinfo(skb)->gso_size);
  1063. return 0;
  1064. }
  1065. do_csum:
  1066. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  1067. unsigned int css, cso;
  1068. cso = skb_transport_offset(skb);
  1069. if (unlikely(cso & 0x1)) {
  1070. netdev_err(adpt->netdev,
  1071. "error: payload offset should be even\n");
  1072. return -EINVAL;
  1073. }
  1074. css = cso + skb->csum_offset;
  1075. TPD_PAYLOAD_OFFSET_SET(tpd, cso >> 1);
  1076. TPD_CXSUM_OFFSET_SET(tpd, css >> 1);
  1077. TPD_CSX_SET(tpd, 1);
  1078. }
  1079. return 0;
  1080. }
  1081. /* Fill up transmit descriptors */
  1082. static void emac_tx_fill_tpd(struct emac_adapter *adpt,
  1083. struct emac_tx_queue *tx_q, struct sk_buff *skb,
  1084. struct emac_tpd *tpd)
  1085. {
  1086. unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
  1087. unsigned int first = tx_q->tpd.produce_idx;
  1088. unsigned int len = skb_headlen(skb);
  1089. struct emac_buffer *tpbuf = NULL;
  1090. unsigned int mapped_len = 0;
  1091. unsigned int i;
  1092. int count = 0;
  1093. int ret;
  1094. /* if Large Segment Offload is (in TCP Segmentation Offload struct) */
  1095. if (TPD_LSO(tpd)) {
  1096. mapped_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1097. tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
  1098. tpbuf->length = mapped_len;
  1099. tpbuf->dma_addr = dma_map_page(adpt->netdev->dev.parent,
  1100. virt_to_page(skb->data),
  1101. offset_in_page(skb->data),
  1102. tpbuf->length,
  1103. DMA_TO_DEVICE);
  1104. ret = dma_mapping_error(adpt->netdev->dev.parent,
  1105. tpbuf->dma_addr);
  1106. if (ret)
  1107. goto error;
  1108. TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
  1109. TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
  1110. TPD_BUF_LEN_SET(tpd, tpbuf->length);
  1111. emac_tx_tpd_create(adpt, tx_q, tpd);
  1112. count++;
  1113. }
  1114. if (mapped_len < len) {
  1115. tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
  1116. tpbuf->length = len - mapped_len;
  1117. tpbuf->dma_addr = dma_map_page(adpt->netdev->dev.parent,
  1118. virt_to_page(skb->data +
  1119. mapped_len),
  1120. offset_in_page(skb->data +
  1121. mapped_len),
  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. for (i = 0; i < nr_frags; i++) {
  1134. struct skb_frag_struct *frag;
  1135. frag = &skb_shinfo(skb)->frags[i];
  1136. tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
  1137. tpbuf->length = frag->size;
  1138. tpbuf->dma_addr = dma_map_page(adpt->netdev->dev.parent,
  1139. frag->page.p, frag->page_offset,
  1140. tpbuf->length, DMA_TO_DEVICE);
  1141. ret = dma_mapping_error(adpt->netdev->dev.parent,
  1142. tpbuf->dma_addr);
  1143. if (ret)
  1144. goto error;
  1145. TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
  1146. TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
  1147. TPD_BUF_LEN_SET(tpd, tpbuf->length);
  1148. emac_tx_tpd_create(adpt, tx_q, tpd);
  1149. count++;
  1150. }
  1151. /* The last tpd */
  1152. wmb();
  1153. emac_tx_tpd_mark_last(adpt, tx_q);
  1154. /* The last buffer info contain the skb address,
  1155. * so it will be freed after unmap
  1156. */
  1157. tpbuf->skb = skb;
  1158. return;
  1159. error:
  1160. /* One of the memory mappings failed, so undo everything */
  1161. tx_q->tpd.produce_idx = first;
  1162. while (count--) {
  1163. tpbuf = GET_TPD_BUFFER(tx_q, first);
  1164. dma_unmap_page(adpt->netdev->dev.parent, tpbuf->dma_addr,
  1165. tpbuf->length, DMA_TO_DEVICE);
  1166. tpbuf->dma_addr = 0;
  1167. tpbuf->length = 0;
  1168. if (++first == tx_q->tpd.count)
  1169. first = 0;
  1170. }
  1171. dev_kfree_skb(skb);
  1172. }
  1173. /* Transmit the packet using specified transmit queue */
  1174. int emac_mac_tx_buf_send(struct emac_adapter *adpt, struct emac_tx_queue *tx_q,
  1175. struct sk_buff *skb)
  1176. {
  1177. struct emac_tpd tpd;
  1178. u32 prod_idx;
  1179. memset(&tpd, 0, sizeof(tpd));
  1180. if (emac_tso_csum(adpt, tx_q, skb, &tpd) != 0) {
  1181. dev_kfree_skb_any(skb);
  1182. return NETDEV_TX_OK;
  1183. }
  1184. if (skb_vlan_tag_present(skb)) {
  1185. u16 tag;
  1186. EMAC_VLAN_TO_TAG(skb_vlan_tag_get(skb), tag);
  1187. TPD_CVLAN_TAG_SET(&tpd, tag);
  1188. TPD_INSTC_SET(&tpd, 1);
  1189. }
  1190. if (skb_network_offset(skb) != ETH_HLEN)
  1191. TPD_TYP_SET(&tpd, 1);
  1192. emac_tx_fill_tpd(adpt, tx_q, skb, &tpd);
  1193. netdev_sent_queue(adpt->netdev, skb->len);
  1194. /* Make sure the are enough free descriptors to hold one
  1195. * maximum-sized SKB. We need one desc for each fragment,
  1196. * one for the checksum (emac_tso_csum), one for TSO, and
  1197. * and one for the SKB header.
  1198. */
  1199. if (emac_tpd_num_free_descs(tx_q) < (MAX_SKB_FRAGS + 3))
  1200. netif_stop_queue(adpt->netdev);
  1201. /* update produce idx */
  1202. prod_idx = (tx_q->tpd.produce_idx << tx_q->produce_shift) &
  1203. tx_q->produce_mask;
  1204. emac_reg_update32(adpt->base + tx_q->produce_reg,
  1205. tx_q->produce_mask, prod_idx);
  1206. return NETDEV_TX_OK;
  1207. }