fman_port.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  1. /*
  2. * Copyright 2008 - 2015 Freescale Semiconductor Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of Freescale Semiconductor nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. *
  16. * ALTERNATIVELY, this software may be distributed under the terms of the
  17. * GNU General Public License ("GPL") as published by the Free Software
  18. * Foundation, either version 2 of that License or (at your option) any
  19. * later version.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
  22. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
  25. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/io.h>
  34. #include <linux/slab.h>
  35. #include <linux/module.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/of_platform.h>
  38. #include <linux/of_address.h>
  39. #include <linux/delay.h>
  40. #include <linux/libfdt_env.h>
  41. #include "fman.h"
  42. #include "fman_port.h"
  43. #include "fman_sp.h"
  44. #include "fman_keygen.h"
  45. /* Queue ID */
  46. #define DFLT_FQ_ID 0x00FFFFFF
  47. /* General defines */
  48. #define PORT_BMI_FIFO_UNITS 0x100
  49. #define MAX_PORT_FIFO_SIZE(bmi_max_fifo_size) \
  50. min((u32)bmi_max_fifo_size, (u32)1024 * FMAN_BMI_FIFO_UNITS)
  51. #define PORT_CG_MAP_NUM 8
  52. #define PORT_PRS_RESULT_WORDS_NUM 8
  53. #define PORT_IC_OFFSET_UNITS 0x10
  54. #define MIN_EXT_BUF_SIZE 64
  55. #define BMI_PORT_REGS_OFFSET 0
  56. #define QMI_PORT_REGS_OFFSET 0x400
  57. #define HWP_PORT_REGS_OFFSET 0x800
  58. /* Default values */
  59. #define DFLT_PORT_BUFFER_PREFIX_CONTEXT_DATA_ALIGN \
  60. DFLT_FM_SP_BUFFER_PREFIX_CONTEXT_DATA_ALIGN
  61. #define DFLT_PORT_CUT_BYTES_FROM_END 4
  62. #define DFLT_PORT_ERRORS_TO_DISCARD FM_PORT_FRM_ERR_CLS_DISCARD
  63. #define DFLT_PORT_MAX_FRAME_LENGTH 9600
  64. #define DFLT_PORT_RX_FIFO_PRI_ELEVATION_LEV(bmi_max_fifo_size) \
  65. MAX_PORT_FIFO_SIZE(bmi_max_fifo_size)
  66. #define DFLT_PORT_RX_FIFO_THRESHOLD(major, bmi_max_fifo_size) \
  67. (major == 6 ? \
  68. MAX_PORT_FIFO_SIZE(bmi_max_fifo_size) : \
  69. (MAX_PORT_FIFO_SIZE(bmi_max_fifo_size) * 3 / 4)) \
  70. #define DFLT_PORT_EXTRA_NUM_OF_FIFO_BUFS 0
  71. /* QMI defines */
  72. #define QMI_DEQ_CFG_SUBPORTAL_MASK 0x1f
  73. #define QMI_PORT_CFG_EN 0x80000000
  74. #define QMI_PORT_STATUS_DEQ_FD_BSY 0x20000000
  75. #define QMI_DEQ_CFG_PRI 0x80000000
  76. #define QMI_DEQ_CFG_TYPE1 0x10000000
  77. #define QMI_DEQ_CFG_TYPE2 0x20000000
  78. #define QMI_DEQ_CFG_TYPE3 0x30000000
  79. #define QMI_DEQ_CFG_PREFETCH_PARTIAL 0x01000000
  80. #define QMI_DEQ_CFG_PREFETCH_FULL 0x03000000
  81. #define QMI_DEQ_CFG_SP_MASK 0xf
  82. #define QMI_DEQ_CFG_SP_SHIFT 20
  83. #define QMI_BYTE_COUNT_LEVEL_CONTROL(_type) \
  84. (_type == FMAN_PORT_TYPE_TX ? 0x1400 : 0x400)
  85. /* BMI defins */
  86. #define BMI_EBD_EN 0x80000000
  87. #define BMI_PORT_CFG_EN 0x80000000
  88. #define BMI_PORT_STATUS_BSY 0x80000000
  89. #define BMI_DMA_ATTR_SWP_SHIFT FMAN_SP_DMA_ATTR_SWP_SHIFT
  90. #define BMI_DMA_ATTR_WRITE_OPTIMIZE FMAN_SP_DMA_ATTR_WRITE_OPTIMIZE
  91. #define BMI_RX_FIFO_PRI_ELEVATION_SHIFT 16
  92. #define BMI_RX_FIFO_THRESHOLD_ETHE 0x80000000
  93. #define BMI_FRAME_END_CS_IGNORE_SHIFT 24
  94. #define BMI_FRAME_END_CS_IGNORE_MASK 0x0000001f
  95. #define BMI_RX_FRAME_END_CUT_SHIFT 16
  96. #define BMI_RX_FRAME_END_CUT_MASK 0x0000001f
  97. #define BMI_IC_TO_EXT_SHIFT FMAN_SP_IC_TO_EXT_SHIFT
  98. #define BMI_IC_TO_EXT_MASK 0x0000001f
  99. #define BMI_IC_FROM_INT_SHIFT FMAN_SP_IC_FROM_INT_SHIFT
  100. #define BMI_IC_FROM_INT_MASK 0x0000000f
  101. #define BMI_IC_SIZE_MASK 0x0000001f
  102. #define BMI_INT_BUF_MARG_SHIFT 28
  103. #define BMI_INT_BUF_MARG_MASK 0x0000000f
  104. #define BMI_EXT_BUF_MARG_START_SHIFT FMAN_SP_EXT_BUF_MARG_START_SHIFT
  105. #define BMI_EXT_BUF_MARG_START_MASK 0x000001ff
  106. #define BMI_EXT_BUF_MARG_END_MASK 0x000001ff
  107. #define BMI_CMD_MR_LEAC 0x00200000
  108. #define BMI_CMD_MR_SLEAC 0x00100000
  109. #define BMI_CMD_MR_MA 0x00080000
  110. #define BMI_CMD_MR_DEAS 0x00040000
  111. #define BMI_CMD_RX_MR_DEF (BMI_CMD_MR_LEAC | \
  112. BMI_CMD_MR_SLEAC | \
  113. BMI_CMD_MR_MA | \
  114. BMI_CMD_MR_DEAS)
  115. #define BMI_CMD_TX_MR_DEF 0
  116. #define BMI_CMD_ATTR_ORDER 0x80000000
  117. #define BMI_CMD_ATTR_SYNC 0x02000000
  118. #define BMI_CMD_ATTR_COLOR_SHIFT 26
  119. #define BMI_FIFO_PIPELINE_DEPTH_SHIFT 12
  120. #define BMI_FIFO_PIPELINE_DEPTH_MASK 0x0000000f
  121. #define BMI_NEXT_ENG_FD_BITS_SHIFT 24
  122. #define BMI_EXT_BUF_POOL_VALID FMAN_SP_EXT_BUF_POOL_VALID
  123. #define BMI_EXT_BUF_POOL_EN_COUNTER FMAN_SP_EXT_BUF_POOL_EN_COUNTER
  124. #define BMI_EXT_BUF_POOL_BACKUP FMAN_SP_EXT_BUF_POOL_BACKUP
  125. #define BMI_EXT_BUF_POOL_ID_SHIFT 16
  126. #define BMI_EXT_BUF_POOL_ID_MASK 0x003F0000
  127. #define BMI_POOL_DEP_NUM_OF_POOLS_SHIFT 16
  128. #define BMI_TX_FIFO_MIN_FILL_SHIFT 16
  129. #define BMI_PRIORITY_ELEVATION_LEVEL ((0x3FF + 1) * PORT_BMI_FIFO_UNITS)
  130. #define BMI_FIFO_THRESHOLD ((0x3FF + 1) * PORT_BMI_FIFO_UNITS)
  131. #define BMI_DEQUEUE_PIPELINE_DEPTH(_type, _speed) \
  132. ((_type == FMAN_PORT_TYPE_TX && _speed == 10000) ? 4 : 1)
  133. #define RX_ERRS_TO_ENQ \
  134. (FM_PORT_FRM_ERR_DMA | \
  135. FM_PORT_FRM_ERR_PHYSICAL | \
  136. FM_PORT_FRM_ERR_SIZE | \
  137. FM_PORT_FRM_ERR_EXTRACTION | \
  138. FM_PORT_FRM_ERR_NO_SCHEME | \
  139. FM_PORT_FRM_ERR_PRS_TIMEOUT | \
  140. FM_PORT_FRM_ERR_PRS_ILL_INSTRUCT | \
  141. FM_PORT_FRM_ERR_BLOCK_LIMIT_EXCEEDED | \
  142. FM_PORT_FRM_ERR_PRS_HDR_ERR | \
  143. FM_PORT_FRM_ERR_KEYSIZE_OVERFLOW | \
  144. FM_PORT_FRM_ERR_IPRE)
  145. /* NIA defines */
  146. #define NIA_ORDER_RESTOR 0x00800000
  147. #define NIA_ENG_BMI 0x00500000
  148. #define NIA_ENG_QMI_ENQ 0x00540000
  149. #define NIA_ENG_QMI_DEQ 0x00580000
  150. #define NIA_ENG_HWP 0x00440000
  151. #define NIA_ENG_HWK 0x00480000
  152. #define NIA_BMI_AC_ENQ_FRAME 0x00000002
  153. #define NIA_BMI_AC_TX_RELEASE 0x000002C0
  154. #define NIA_BMI_AC_RELEASE 0x000000C0
  155. #define NIA_BMI_AC_TX 0x00000274
  156. #define NIA_BMI_AC_FETCH_ALL_FRAME 0x0000020c
  157. /* Port IDs */
  158. #define TX_10G_PORT_BASE 0x30
  159. #define RX_10G_PORT_BASE 0x10
  160. /* BMI Rx port register map */
  161. struct fman_port_rx_bmi_regs {
  162. u32 fmbm_rcfg; /* Rx Configuration */
  163. u32 fmbm_rst; /* Rx Status */
  164. u32 fmbm_rda; /* Rx DMA attributes */
  165. u32 fmbm_rfp; /* Rx FIFO Parameters */
  166. u32 fmbm_rfed; /* Rx Frame End Data */
  167. u32 fmbm_ricp; /* Rx Internal Context Parameters */
  168. u32 fmbm_rim; /* Rx Internal Buffer Margins */
  169. u32 fmbm_rebm; /* Rx External Buffer Margins */
  170. u32 fmbm_rfne; /* Rx Frame Next Engine */
  171. u32 fmbm_rfca; /* Rx Frame Command Attributes. */
  172. u32 fmbm_rfpne; /* Rx Frame Parser Next Engine */
  173. u32 fmbm_rpso; /* Rx Parse Start Offset */
  174. u32 fmbm_rpp; /* Rx Policer Profile */
  175. u32 fmbm_rccb; /* Rx Coarse Classification Base */
  176. u32 fmbm_reth; /* Rx Excessive Threshold */
  177. u32 reserved003c[1]; /* (0x03C 0x03F) */
  178. u32 fmbm_rprai[PORT_PRS_RESULT_WORDS_NUM];
  179. /* Rx Parse Results Array Init */
  180. u32 fmbm_rfqid; /* Rx Frame Queue ID */
  181. u32 fmbm_refqid; /* Rx Error Frame Queue ID */
  182. u32 fmbm_rfsdm; /* Rx Frame Status Discard Mask */
  183. u32 fmbm_rfsem; /* Rx Frame Status Error Mask */
  184. u32 fmbm_rfene; /* Rx Frame Enqueue Next Engine */
  185. u32 reserved0074[0x2]; /* (0x074-0x07C) */
  186. u32 fmbm_rcmne; /* Rx Frame Continuous Mode Next Engine */
  187. u32 reserved0080[0x20]; /* (0x080 0x0FF) */
  188. u32 fmbm_ebmpi[FMAN_PORT_MAX_EXT_POOLS_NUM];
  189. /* Buffer Manager pool Information- */
  190. u32 fmbm_acnt[FMAN_PORT_MAX_EXT_POOLS_NUM]; /* Allocate Counter- */
  191. u32 reserved0130[8]; /* 0x130/0x140 - 0x15F reserved - */
  192. u32 fmbm_rcgm[PORT_CG_MAP_NUM]; /* Congestion Group Map */
  193. u32 fmbm_mpd; /* BM Pool Depletion */
  194. u32 reserved0184[0x1F]; /* (0x184 0x1FF) */
  195. u32 fmbm_rstc; /* Rx Statistics Counters */
  196. u32 fmbm_rfrc; /* Rx Frame Counter */
  197. u32 fmbm_rfbc; /* Rx Bad Frames Counter */
  198. u32 fmbm_rlfc; /* Rx Large Frames Counter */
  199. u32 fmbm_rffc; /* Rx Filter Frames Counter */
  200. u32 fmbm_rfdc; /* Rx Frame Discard Counter */
  201. u32 fmbm_rfldec; /* Rx Frames List DMA Error Counter */
  202. u32 fmbm_rodc; /* Rx Out of Buffers Discard nntr */
  203. u32 fmbm_rbdc; /* Rx Buffers Deallocate Counter */
  204. u32 fmbm_rpec; /* RX Prepare to enqueue Counte */
  205. u32 reserved0224[0x16]; /* (0x224 0x27F) */
  206. u32 fmbm_rpc; /* Rx Performance Counters */
  207. u32 fmbm_rpcp; /* Rx Performance Count Parameters */
  208. u32 fmbm_rccn; /* Rx Cycle Counter */
  209. u32 fmbm_rtuc; /* Rx Tasks Utilization Counter */
  210. u32 fmbm_rrquc; /* Rx Receive Queue Utilization cntr */
  211. u32 fmbm_rduc; /* Rx DMA Utilization Counter */
  212. u32 fmbm_rfuc; /* Rx FIFO Utilization Counter */
  213. u32 fmbm_rpac; /* Rx Pause Activation Counter */
  214. u32 reserved02a0[0x18]; /* (0x2A0 0x2FF) */
  215. u32 fmbm_rdcfg[0x3]; /* Rx Debug Configuration */
  216. u32 fmbm_rgpr; /* Rx General Purpose Register */
  217. u32 reserved0310[0x3a];
  218. };
  219. /* BMI Tx port register map */
  220. struct fman_port_tx_bmi_regs {
  221. u32 fmbm_tcfg; /* Tx Configuration */
  222. u32 fmbm_tst; /* Tx Status */
  223. u32 fmbm_tda; /* Tx DMA attributes */
  224. u32 fmbm_tfp; /* Tx FIFO Parameters */
  225. u32 fmbm_tfed; /* Tx Frame End Data */
  226. u32 fmbm_ticp; /* Tx Internal Context Parameters */
  227. u32 fmbm_tfdne; /* Tx Frame Dequeue Next Engine. */
  228. u32 fmbm_tfca; /* Tx Frame Command attribute. */
  229. u32 fmbm_tcfqid; /* Tx Confirmation Frame Queue ID. */
  230. u32 fmbm_tefqid; /* Tx Frame Error Queue ID */
  231. u32 fmbm_tfene; /* Tx Frame Enqueue Next Engine */
  232. u32 fmbm_trlmts; /* Tx Rate Limiter Scale */
  233. u32 fmbm_trlmt; /* Tx Rate Limiter */
  234. u32 reserved0034[0x0e]; /* (0x034-0x6c) */
  235. u32 fmbm_tccb; /* Tx Coarse Classification base */
  236. u32 fmbm_tfne; /* Tx Frame Next Engine */
  237. u32 fmbm_tpfcm[0x02];
  238. /* Tx Priority based Flow Control (PFC) Mapping */
  239. u32 fmbm_tcmne; /* Tx Frame Continuous Mode Next Engine */
  240. u32 reserved0080[0x60]; /* (0x080-0x200) */
  241. u32 fmbm_tstc; /* Tx Statistics Counters */
  242. u32 fmbm_tfrc; /* Tx Frame Counter */
  243. u32 fmbm_tfdc; /* Tx Frames Discard Counter */
  244. u32 fmbm_tfledc; /* Tx Frame len error discard cntr */
  245. u32 fmbm_tfufdc; /* Tx Frame unsprt frmt discard cntr */
  246. u32 fmbm_tbdc; /* Tx Buffers Deallocate Counter */
  247. u32 reserved0218[0x1A]; /* (0x218-0x280) */
  248. u32 fmbm_tpc; /* Tx Performance Counters */
  249. u32 fmbm_tpcp; /* Tx Performance Count Parameters */
  250. u32 fmbm_tccn; /* Tx Cycle Counter */
  251. u32 fmbm_ttuc; /* Tx Tasks Utilization Counter */
  252. u32 fmbm_ttcquc; /* Tx Transmit conf Q util Counter */
  253. u32 fmbm_tduc; /* Tx DMA Utilization Counter */
  254. u32 fmbm_tfuc; /* Tx FIFO Utilization Counter */
  255. u32 reserved029c[16]; /* (0x29C-0x2FF) */
  256. u32 fmbm_tdcfg[0x3]; /* Tx Debug Configuration */
  257. u32 fmbm_tgpr; /* Tx General Purpose Register */
  258. u32 reserved0310[0x3a]; /* (0x310-0x3FF) */
  259. };
  260. /* BMI port register map */
  261. union fman_port_bmi_regs {
  262. struct fman_port_rx_bmi_regs rx;
  263. struct fman_port_tx_bmi_regs tx;
  264. };
  265. /* QMI port register map */
  266. struct fman_port_qmi_regs {
  267. u32 fmqm_pnc; /* PortID n Configuration Register */
  268. u32 fmqm_pns; /* PortID n Status Register */
  269. u32 fmqm_pnts; /* PortID n Task Status Register */
  270. u32 reserved00c[4]; /* 0xn00C - 0xn01B */
  271. u32 fmqm_pnen; /* PortID n Enqueue NIA Register */
  272. u32 fmqm_pnetfc; /* PortID n Enq Total Frame Counter */
  273. u32 reserved024[2]; /* 0xn024 - 0x02B */
  274. u32 fmqm_pndn; /* PortID n Dequeue NIA Register */
  275. u32 fmqm_pndc; /* PortID n Dequeue Config Register */
  276. u32 fmqm_pndtfc; /* PortID n Dequeue tot Frame cntr */
  277. u32 fmqm_pndfdc; /* PortID n Dequeue FQID Dflt Cntr */
  278. u32 fmqm_pndcc; /* PortID n Dequeue Confirm Counter */
  279. };
  280. #define HWP_HXS_COUNT 16
  281. #define HWP_HXS_PHE_REPORT 0x00000800
  282. #define HWP_HXS_PCAC_PSTAT 0x00000100
  283. #define HWP_HXS_PCAC_PSTOP 0x00000001
  284. #define HWP_HXS_TCP_OFFSET 0xA
  285. #define HWP_HXS_UDP_OFFSET 0xB
  286. #define HWP_HXS_SH_PAD_REM 0x80000000
  287. struct fman_port_hwp_regs {
  288. struct {
  289. u32 ssa; /* Soft Sequence Attachment */
  290. u32 lcv; /* Line-up Enable Confirmation Mask */
  291. } pmda[HWP_HXS_COUNT]; /* Parse Memory Direct Access Registers */
  292. u32 reserved080[(0x3f8 - 0x080) / 4]; /* (0x080-0x3f7) */
  293. u32 fmpr_pcac; /* Configuration Access Control */
  294. };
  295. /* QMI dequeue prefetch modes */
  296. enum fman_port_deq_prefetch {
  297. FMAN_PORT_DEQ_NO_PREFETCH, /* No prefetch mode */
  298. FMAN_PORT_DEQ_PART_PREFETCH, /* Partial prefetch mode */
  299. FMAN_PORT_DEQ_FULL_PREFETCH /* Full prefetch mode */
  300. };
  301. /* A structure for defining FM port resources */
  302. struct fman_port_rsrc {
  303. u32 num; /* Committed required resource */
  304. u32 extra; /* Extra (not committed) required resource */
  305. };
  306. enum fman_port_dma_swap {
  307. FMAN_PORT_DMA_NO_SWAP, /* No swap, transfer data as is */
  308. FMAN_PORT_DMA_SWAP_LE,
  309. /* The transferred data should be swapped in PPC Little Endian mode */
  310. FMAN_PORT_DMA_SWAP_BE
  311. /* The transferred data should be swapped in Big Endian mode */
  312. };
  313. /* Default port color */
  314. enum fman_port_color {
  315. FMAN_PORT_COLOR_GREEN, /* Default port color is green */
  316. FMAN_PORT_COLOR_YELLOW, /* Default port color is yellow */
  317. FMAN_PORT_COLOR_RED, /* Default port color is red */
  318. FMAN_PORT_COLOR_OVERRIDE /* Ignore color */
  319. };
  320. /* QMI dequeue from the SP channel - types */
  321. enum fman_port_deq_type {
  322. FMAN_PORT_DEQ_BY_PRI,
  323. /* Priority precedence and Intra-Class scheduling */
  324. FMAN_PORT_DEQ_ACTIVE_FQ,
  325. /* Active FQ precedence and Intra-Class scheduling */
  326. FMAN_PORT_DEQ_ACTIVE_FQ_NO_ICS
  327. /* Active FQ precedence and override Intra-Class scheduling */
  328. };
  329. /* External buffer pools configuration */
  330. struct fman_port_bpools {
  331. u8 count; /* Num of pools to set up */
  332. bool counters_enable; /* Enable allocate counters */
  333. u8 grp_bp_depleted_num;
  334. /* Number of depleted pools - if reached the BMI indicates
  335. * the MAC to send a pause frame
  336. */
  337. struct {
  338. u8 bpid; /* BM pool ID */
  339. u16 size;
  340. /* Pool's size - must be in ascending order */
  341. bool is_backup;
  342. /* If this is a backup pool */
  343. bool grp_bp_depleted;
  344. /* Consider this buffer in multiple pools depletion criteria */
  345. bool single_bp_depleted;
  346. /* Consider this buffer in single pool depletion criteria */
  347. } bpool[FMAN_PORT_MAX_EXT_POOLS_NUM];
  348. };
  349. struct fman_port_cfg {
  350. u32 dflt_fqid;
  351. u32 err_fqid;
  352. u32 pcd_base_fqid;
  353. u32 pcd_fqs_count;
  354. u8 deq_sp;
  355. bool deq_high_priority;
  356. enum fman_port_deq_type deq_type;
  357. enum fman_port_deq_prefetch deq_prefetch_option;
  358. u16 deq_byte_cnt;
  359. u8 cheksum_last_bytes_ignore;
  360. u8 rx_cut_end_bytes;
  361. struct fman_buf_pool_depletion buf_pool_depletion;
  362. struct fman_ext_pools ext_buf_pools;
  363. u32 tx_fifo_min_level;
  364. u32 tx_fifo_low_comf_level;
  365. u32 rx_pri_elevation;
  366. u32 rx_fifo_thr;
  367. struct fman_sp_buf_margins buf_margins;
  368. u32 int_buf_start_margin;
  369. struct fman_sp_int_context_data_copy int_context;
  370. u32 discard_mask;
  371. u32 err_mask;
  372. struct fman_buffer_prefix_content buffer_prefix_content;
  373. bool dont_release_buf;
  374. u8 rx_fd_bits;
  375. u32 tx_fifo_deq_pipeline_depth;
  376. bool errata_A006320;
  377. bool excessive_threshold_register;
  378. bool fmbm_tfne_has_features;
  379. enum fman_port_dma_swap dma_swap_data;
  380. enum fman_port_color color;
  381. };
  382. struct fman_port_rx_pools_params {
  383. u8 num_of_pools;
  384. u16 second_largest_buf_size;
  385. u16 largest_buf_size;
  386. };
  387. struct fman_port_dts_params {
  388. void __iomem *base_addr; /* FMan port virtual memory */
  389. enum fman_port_type type; /* Port type */
  390. u16 speed; /* Port speed */
  391. u8 id; /* HW Port Id */
  392. u32 qman_channel_id; /* QMan channel id (non RX only) */
  393. struct fman *fman; /* FMan Handle */
  394. };
  395. struct fman_port {
  396. void *fm;
  397. struct device *dev;
  398. struct fman_rev_info rev_info;
  399. u8 port_id;
  400. enum fman_port_type port_type;
  401. u16 port_speed;
  402. union fman_port_bmi_regs __iomem *bmi_regs;
  403. struct fman_port_qmi_regs __iomem *qmi_regs;
  404. struct fman_port_hwp_regs __iomem *hwp_regs;
  405. struct fman_sp_buffer_offsets buffer_offsets;
  406. u8 internal_buf_offset;
  407. struct fman_ext_pools ext_buf_pools;
  408. u16 max_frame_length;
  409. struct fman_port_rsrc open_dmas;
  410. struct fman_port_rsrc tasks;
  411. struct fman_port_rsrc fifo_bufs;
  412. struct fman_port_rx_pools_params rx_pools_params;
  413. struct fman_port_cfg *cfg;
  414. struct fman_port_dts_params dts_params;
  415. u8 ext_pools_num;
  416. u32 max_port_fifo_size;
  417. u32 max_num_of_ext_pools;
  418. u32 max_num_of_sub_portals;
  419. u32 bm_max_num_of_pools;
  420. };
  421. static int init_bmi_rx(struct fman_port *port)
  422. {
  423. struct fman_port_rx_bmi_regs __iomem *regs = &port->bmi_regs->rx;
  424. struct fman_port_cfg *cfg = port->cfg;
  425. u32 tmp;
  426. /* DMA attributes */
  427. tmp = (u32)cfg->dma_swap_data << BMI_DMA_ATTR_SWP_SHIFT;
  428. /* Enable write optimization */
  429. tmp |= BMI_DMA_ATTR_WRITE_OPTIMIZE;
  430. iowrite32be(tmp, &regs->fmbm_rda);
  431. /* Rx FIFO parameters */
  432. tmp = (cfg->rx_pri_elevation / PORT_BMI_FIFO_UNITS - 1) <<
  433. BMI_RX_FIFO_PRI_ELEVATION_SHIFT;
  434. tmp |= cfg->rx_fifo_thr / PORT_BMI_FIFO_UNITS - 1;
  435. iowrite32be(tmp, &regs->fmbm_rfp);
  436. if (cfg->excessive_threshold_register)
  437. /* always allow access to the extra resources */
  438. iowrite32be(BMI_RX_FIFO_THRESHOLD_ETHE, &regs->fmbm_reth);
  439. /* Frame end data */
  440. tmp = (cfg->cheksum_last_bytes_ignore & BMI_FRAME_END_CS_IGNORE_MASK) <<
  441. BMI_FRAME_END_CS_IGNORE_SHIFT;
  442. tmp |= (cfg->rx_cut_end_bytes & BMI_RX_FRAME_END_CUT_MASK) <<
  443. BMI_RX_FRAME_END_CUT_SHIFT;
  444. if (cfg->errata_A006320)
  445. tmp &= 0xffe0ffff;
  446. iowrite32be(tmp, &regs->fmbm_rfed);
  447. /* Internal context parameters */
  448. tmp = ((cfg->int_context.ext_buf_offset / PORT_IC_OFFSET_UNITS) &
  449. BMI_IC_TO_EXT_MASK) << BMI_IC_TO_EXT_SHIFT;
  450. tmp |= ((cfg->int_context.int_context_offset / PORT_IC_OFFSET_UNITS) &
  451. BMI_IC_FROM_INT_MASK) << BMI_IC_FROM_INT_SHIFT;
  452. tmp |= (cfg->int_context.size / PORT_IC_OFFSET_UNITS) &
  453. BMI_IC_SIZE_MASK;
  454. iowrite32be(tmp, &regs->fmbm_ricp);
  455. /* Internal buffer offset */
  456. tmp = ((cfg->int_buf_start_margin / PORT_IC_OFFSET_UNITS) &
  457. BMI_INT_BUF_MARG_MASK) << BMI_INT_BUF_MARG_SHIFT;
  458. iowrite32be(tmp, &regs->fmbm_rim);
  459. /* External buffer margins */
  460. tmp = (cfg->buf_margins.start_margins & BMI_EXT_BUF_MARG_START_MASK) <<
  461. BMI_EXT_BUF_MARG_START_SHIFT;
  462. tmp |= cfg->buf_margins.end_margins & BMI_EXT_BUF_MARG_END_MASK;
  463. iowrite32be(tmp, &regs->fmbm_rebm);
  464. /* Frame attributes */
  465. tmp = BMI_CMD_RX_MR_DEF;
  466. tmp |= BMI_CMD_ATTR_ORDER;
  467. tmp |= (u32)cfg->color << BMI_CMD_ATTR_COLOR_SHIFT;
  468. /* Synchronization request */
  469. tmp |= BMI_CMD_ATTR_SYNC;
  470. iowrite32be(tmp, &regs->fmbm_rfca);
  471. /* NIA */
  472. tmp = (u32)cfg->rx_fd_bits << BMI_NEXT_ENG_FD_BITS_SHIFT;
  473. tmp |= NIA_ENG_HWP;
  474. iowrite32be(tmp, &regs->fmbm_rfne);
  475. /* Parser Next Engine NIA */
  476. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_ENQ_FRAME, &regs->fmbm_rfpne);
  477. /* Enqueue NIA */
  478. iowrite32be(NIA_ENG_QMI_ENQ | NIA_ORDER_RESTOR, &regs->fmbm_rfene);
  479. /* Default/error queues */
  480. iowrite32be((cfg->dflt_fqid & DFLT_FQ_ID), &regs->fmbm_rfqid);
  481. iowrite32be((cfg->err_fqid & DFLT_FQ_ID), &regs->fmbm_refqid);
  482. /* Discard/error masks */
  483. iowrite32be(cfg->discard_mask, &regs->fmbm_rfsdm);
  484. iowrite32be(cfg->err_mask, &regs->fmbm_rfsem);
  485. return 0;
  486. }
  487. static int init_bmi_tx(struct fman_port *port)
  488. {
  489. struct fman_port_tx_bmi_regs __iomem *regs = &port->bmi_regs->tx;
  490. struct fman_port_cfg *cfg = port->cfg;
  491. u32 tmp;
  492. /* Tx Configuration register */
  493. tmp = 0;
  494. iowrite32be(tmp, &regs->fmbm_tcfg);
  495. /* DMA attributes */
  496. tmp = (u32)cfg->dma_swap_data << BMI_DMA_ATTR_SWP_SHIFT;
  497. iowrite32be(tmp, &regs->fmbm_tda);
  498. /* Tx FIFO parameters */
  499. tmp = (cfg->tx_fifo_min_level / PORT_BMI_FIFO_UNITS) <<
  500. BMI_TX_FIFO_MIN_FILL_SHIFT;
  501. tmp |= ((cfg->tx_fifo_deq_pipeline_depth - 1) &
  502. BMI_FIFO_PIPELINE_DEPTH_MASK) << BMI_FIFO_PIPELINE_DEPTH_SHIFT;
  503. tmp |= (cfg->tx_fifo_low_comf_level / PORT_BMI_FIFO_UNITS) - 1;
  504. iowrite32be(tmp, &regs->fmbm_tfp);
  505. /* Frame end data */
  506. tmp = (cfg->cheksum_last_bytes_ignore & BMI_FRAME_END_CS_IGNORE_MASK) <<
  507. BMI_FRAME_END_CS_IGNORE_SHIFT;
  508. iowrite32be(tmp, &regs->fmbm_tfed);
  509. /* Internal context parameters */
  510. tmp = ((cfg->int_context.ext_buf_offset / PORT_IC_OFFSET_UNITS) &
  511. BMI_IC_TO_EXT_MASK) << BMI_IC_TO_EXT_SHIFT;
  512. tmp |= ((cfg->int_context.int_context_offset / PORT_IC_OFFSET_UNITS) &
  513. BMI_IC_FROM_INT_MASK) << BMI_IC_FROM_INT_SHIFT;
  514. tmp |= (cfg->int_context.size / PORT_IC_OFFSET_UNITS) &
  515. BMI_IC_SIZE_MASK;
  516. iowrite32be(tmp, &regs->fmbm_ticp);
  517. /* Frame attributes */
  518. tmp = BMI_CMD_TX_MR_DEF;
  519. tmp |= BMI_CMD_ATTR_ORDER;
  520. tmp |= (u32)cfg->color << BMI_CMD_ATTR_COLOR_SHIFT;
  521. iowrite32be(tmp, &regs->fmbm_tfca);
  522. /* Dequeue NIA + enqueue NIA */
  523. iowrite32be(NIA_ENG_QMI_DEQ, &regs->fmbm_tfdne);
  524. iowrite32be(NIA_ENG_QMI_ENQ | NIA_ORDER_RESTOR, &regs->fmbm_tfene);
  525. if (cfg->fmbm_tfne_has_features)
  526. iowrite32be(!cfg->dflt_fqid ?
  527. BMI_EBD_EN | NIA_BMI_AC_FETCH_ALL_FRAME :
  528. NIA_BMI_AC_FETCH_ALL_FRAME, &regs->fmbm_tfne);
  529. if (!cfg->dflt_fqid && cfg->dont_release_buf) {
  530. iowrite32be(DFLT_FQ_ID, &regs->fmbm_tcfqid);
  531. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE,
  532. &regs->fmbm_tfene);
  533. if (cfg->fmbm_tfne_has_features)
  534. iowrite32be(ioread32be(&regs->fmbm_tfne) & ~BMI_EBD_EN,
  535. &regs->fmbm_tfne);
  536. }
  537. /* Confirmation/error queues */
  538. if (cfg->dflt_fqid || !cfg->dont_release_buf)
  539. iowrite32be(cfg->dflt_fqid & DFLT_FQ_ID, &regs->fmbm_tcfqid);
  540. iowrite32be((cfg->err_fqid & DFLT_FQ_ID), &regs->fmbm_tefqid);
  541. return 0;
  542. }
  543. static int init_qmi(struct fman_port *port)
  544. {
  545. struct fman_port_qmi_regs __iomem *regs = port->qmi_regs;
  546. struct fman_port_cfg *cfg = port->cfg;
  547. u32 tmp;
  548. /* Rx port configuration */
  549. if (port->port_type == FMAN_PORT_TYPE_RX) {
  550. /* Enqueue NIA */
  551. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_RELEASE, &regs->fmqm_pnen);
  552. return 0;
  553. }
  554. /* Continue with Tx port configuration */
  555. if (port->port_type == FMAN_PORT_TYPE_TX) {
  556. /* Enqueue NIA */
  557. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE,
  558. &regs->fmqm_pnen);
  559. /* Dequeue NIA */
  560. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX, &regs->fmqm_pndn);
  561. }
  562. /* Dequeue Configuration register */
  563. tmp = 0;
  564. if (cfg->deq_high_priority)
  565. tmp |= QMI_DEQ_CFG_PRI;
  566. switch (cfg->deq_type) {
  567. case FMAN_PORT_DEQ_BY_PRI:
  568. tmp |= QMI_DEQ_CFG_TYPE1;
  569. break;
  570. case FMAN_PORT_DEQ_ACTIVE_FQ:
  571. tmp |= QMI_DEQ_CFG_TYPE2;
  572. break;
  573. case FMAN_PORT_DEQ_ACTIVE_FQ_NO_ICS:
  574. tmp |= QMI_DEQ_CFG_TYPE3;
  575. break;
  576. default:
  577. return -EINVAL;
  578. }
  579. switch (cfg->deq_prefetch_option) {
  580. case FMAN_PORT_DEQ_NO_PREFETCH:
  581. break;
  582. case FMAN_PORT_DEQ_PART_PREFETCH:
  583. tmp |= QMI_DEQ_CFG_PREFETCH_PARTIAL;
  584. break;
  585. case FMAN_PORT_DEQ_FULL_PREFETCH:
  586. tmp |= QMI_DEQ_CFG_PREFETCH_FULL;
  587. break;
  588. default:
  589. return -EINVAL;
  590. }
  591. tmp |= (cfg->deq_sp & QMI_DEQ_CFG_SP_MASK) << QMI_DEQ_CFG_SP_SHIFT;
  592. tmp |= cfg->deq_byte_cnt;
  593. iowrite32be(tmp, &regs->fmqm_pndc);
  594. return 0;
  595. }
  596. static void stop_port_hwp(struct fman_port *port)
  597. {
  598. struct fman_port_hwp_regs __iomem *regs = port->hwp_regs;
  599. int cnt = 100;
  600. iowrite32be(HWP_HXS_PCAC_PSTOP, &regs->fmpr_pcac);
  601. while (cnt-- > 0 &&
  602. (ioread32be(&regs->fmpr_pcac) & HWP_HXS_PCAC_PSTAT))
  603. udelay(10);
  604. if (!cnt)
  605. pr_err("Timeout stopping HW Parser\n");
  606. }
  607. static void start_port_hwp(struct fman_port *port)
  608. {
  609. struct fman_port_hwp_regs __iomem *regs = port->hwp_regs;
  610. int cnt = 100;
  611. iowrite32be(0, &regs->fmpr_pcac);
  612. while (cnt-- > 0 &&
  613. !(ioread32be(&regs->fmpr_pcac) & HWP_HXS_PCAC_PSTAT))
  614. udelay(10);
  615. if (!cnt)
  616. pr_err("Timeout starting HW Parser\n");
  617. }
  618. static void init_hwp(struct fman_port *port)
  619. {
  620. struct fman_port_hwp_regs __iomem *regs = port->hwp_regs;
  621. int i;
  622. stop_port_hwp(port);
  623. for (i = 0; i < HWP_HXS_COUNT; i++) {
  624. /* enable HXS error reporting into FD[STATUS] PHE */
  625. iowrite32be(0x00000000, &regs->pmda[i].ssa);
  626. iowrite32be(0xffffffff, &regs->pmda[i].lcv);
  627. }
  628. /* Short packet padding removal from checksum calculation */
  629. iowrite32be(HWP_HXS_SH_PAD_REM, &regs->pmda[HWP_HXS_TCP_OFFSET].ssa);
  630. iowrite32be(HWP_HXS_SH_PAD_REM, &regs->pmda[HWP_HXS_UDP_OFFSET].ssa);
  631. start_port_hwp(port);
  632. }
  633. static int init(struct fman_port *port)
  634. {
  635. int err;
  636. /* Init BMI registers */
  637. switch (port->port_type) {
  638. case FMAN_PORT_TYPE_RX:
  639. err = init_bmi_rx(port);
  640. if (!err)
  641. init_hwp(port);
  642. break;
  643. case FMAN_PORT_TYPE_TX:
  644. err = init_bmi_tx(port);
  645. break;
  646. default:
  647. return -EINVAL;
  648. }
  649. if (err)
  650. return err;
  651. /* Init QMI registers */
  652. err = init_qmi(port);
  653. if (err)
  654. return err;
  655. return 0;
  656. }
  657. static int set_bpools(const struct fman_port *port,
  658. const struct fman_port_bpools *bp)
  659. {
  660. u32 __iomem *bp_reg, *bp_depl_reg;
  661. u32 tmp;
  662. u8 i, max_bp_num;
  663. bool grp_depl_used = false, rx_port;
  664. switch (port->port_type) {
  665. case FMAN_PORT_TYPE_RX:
  666. max_bp_num = port->ext_pools_num;
  667. rx_port = true;
  668. bp_reg = port->bmi_regs->rx.fmbm_ebmpi;
  669. bp_depl_reg = &port->bmi_regs->rx.fmbm_mpd;
  670. break;
  671. default:
  672. return -EINVAL;
  673. }
  674. if (rx_port) {
  675. /* Check buffers are provided in ascending order */
  676. for (i = 0; (i < (bp->count - 1) &&
  677. (i < FMAN_PORT_MAX_EXT_POOLS_NUM - 1)); i++) {
  678. if (bp->bpool[i].size > bp->bpool[i + 1].size)
  679. return -EINVAL;
  680. }
  681. }
  682. /* Set up external buffers pools */
  683. for (i = 0; i < bp->count; i++) {
  684. tmp = BMI_EXT_BUF_POOL_VALID;
  685. tmp |= ((u32)bp->bpool[i].bpid <<
  686. BMI_EXT_BUF_POOL_ID_SHIFT) & BMI_EXT_BUF_POOL_ID_MASK;
  687. if (rx_port) {
  688. if (bp->counters_enable)
  689. tmp |= BMI_EXT_BUF_POOL_EN_COUNTER;
  690. if (bp->bpool[i].is_backup)
  691. tmp |= BMI_EXT_BUF_POOL_BACKUP;
  692. tmp |= (u32)bp->bpool[i].size;
  693. }
  694. iowrite32be(tmp, &bp_reg[i]);
  695. }
  696. /* Clear unused pools */
  697. for (i = bp->count; i < max_bp_num; i++)
  698. iowrite32be(0, &bp_reg[i]);
  699. /* Pools depletion */
  700. tmp = 0;
  701. for (i = 0; i < FMAN_PORT_MAX_EXT_POOLS_NUM; i++) {
  702. if (bp->bpool[i].grp_bp_depleted) {
  703. grp_depl_used = true;
  704. tmp |= 0x80000000 >> i;
  705. }
  706. if (bp->bpool[i].single_bp_depleted)
  707. tmp |= 0x80 >> i;
  708. }
  709. if (grp_depl_used)
  710. tmp |= ((u32)bp->grp_bp_depleted_num - 1) <<
  711. BMI_POOL_DEP_NUM_OF_POOLS_SHIFT;
  712. iowrite32be(tmp, bp_depl_reg);
  713. return 0;
  714. }
  715. static bool is_init_done(struct fman_port_cfg *cfg)
  716. {
  717. /* Checks if FMan port driver parameters were initialized */
  718. if (!cfg)
  719. return true;
  720. return false;
  721. }
  722. static int verify_size_of_fifo(struct fman_port *port)
  723. {
  724. u32 min_fifo_size_required = 0, opt_fifo_size_for_b2b = 0;
  725. /* TX Ports */
  726. if (port->port_type == FMAN_PORT_TYPE_TX) {
  727. min_fifo_size_required = (u32)
  728. (roundup(port->max_frame_length,
  729. FMAN_BMI_FIFO_UNITS) + (3 * FMAN_BMI_FIFO_UNITS));
  730. min_fifo_size_required +=
  731. port->cfg->tx_fifo_deq_pipeline_depth *
  732. FMAN_BMI_FIFO_UNITS;
  733. opt_fifo_size_for_b2b = min_fifo_size_required;
  734. /* Add some margin for back-to-back capability to improve
  735. * performance, allows the hardware to pipeline new frame dma
  736. * while the previous frame not yet transmitted.
  737. */
  738. if (port->port_speed == 10000)
  739. opt_fifo_size_for_b2b += 3 * FMAN_BMI_FIFO_UNITS;
  740. else
  741. opt_fifo_size_for_b2b += 2 * FMAN_BMI_FIFO_UNITS;
  742. }
  743. /* RX Ports */
  744. else if (port->port_type == FMAN_PORT_TYPE_RX) {
  745. if (port->rev_info.major >= 6)
  746. min_fifo_size_required = (u32)
  747. (roundup(port->max_frame_length,
  748. FMAN_BMI_FIFO_UNITS) +
  749. (5 * FMAN_BMI_FIFO_UNITS));
  750. /* 4 according to spec + 1 for FOF>0 */
  751. else
  752. min_fifo_size_required = (u32)
  753. (roundup(min(port->max_frame_length,
  754. port->rx_pools_params.largest_buf_size),
  755. FMAN_BMI_FIFO_UNITS) +
  756. (7 * FMAN_BMI_FIFO_UNITS));
  757. opt_fifo_size_for_b2b = min_fifo_size_required;
  758. /* Add some margin for back-to-back capability to improve
  759. * performance,allows the hardware to pipeline new frame dma
  760. * while the previous frame not yet transmitted.
  761. */
  762. if (port->port_speed == 10000)
  763. opt_fifo_size_for_b2b += 8 * FMAN_BMI_FIFO_UNITS;
  764. else
  765. opt_fifo_size_for_b2b += 3 * FMAN_BMI_FIFO_UNITS;
  766. }
  767. WARN_ON(min_fifo_size_required <= 0);
  768. WARN_ON(opt_fifo_size_for_b2b < min_fifo_size_required);
  769. /* Verify the size */
  770. if (port->fifo_bufs.num < min_fifo_size_required)
  771. dev_dbg(port->dev, "%s: FIFO size should be enlarged to %d bytes\n",
  772. __func__, min_fifo_size_required);
  773. else if (port->fifo_bufs.num < opt_fifo_size_for_b2b)
  774. dev_dbg(port->dev, "%s: For b2b processing,FIFO may be enlarged to %d bytes\n",
  775. __func__, opt_fifo_size_for_b2b);
  776. return 0;
  777. }
  778. static int set_ext_buffer_pools(struct fman_port *port)
  779. {
  780. struct fman_ext_pools *ext_buf_pools = &port->cfg->ext_buf_pools;
  781. struct fman_buf_pool_depletion *buf_pool_depletion =
  782. &port->cfg->buf_pool_depletion;
  783. u8 ordered_array[FMAN_PORT_MAX_EXT_POOLS_NUM];
  784. u16 sizes_array[BM_MAX_NUM_OF_POOLS];
  785. int i = 0, j = 0, err;
  786. struct fman_port_bpools bpools;
  787. memset(&ordered_array, 0, sizeof(u8) * FMAN_PORT_MAX_EXT_POOLS_NUM);
  788. memset(&sizes_array, 0, sizeof(u16) * BM_MAX_NUM_OF_POOLS);
  789. memcpy(&port->ext_buf_pools, ext_buf_pools,
  790. sizeof(struct fman_ext_pools));
  791. fman_sp_set_buf_pools_in_asc_order_of_buf_sizes(ext_buf_pools,
  792. ordered_array,
  793. sizes_array);
  794. memset(&bpools, 0, sizeof(struct fman_port_bpools));
  795. bpools.count = ext_buf_pools->num_of_pools_used;
  796. bpools.counters_enable = true;
  797. for (i = 0; i < ext_buf_pools->num_of_pools_used; i++) {
  798. bpools.bpool[i].bpid = ordered_array[i];
  799. bpools.bpool[i].size = sizes_array[ordered_array[i]];
  800. }
  801. /* save pools parameters for later use */
  802. port->rx_pools_params.num_of_pools = ext_buf_pools->num_of_pools_used;
  803. port->rx_pools_params.largest_buf_size =
  804. sizes_array[ordered_array[ext_buf_pools->num_of_pools_used - 1]];
  805. port->rx_pools_params.second_largest_buf_size =
  806. sizes_array[ordered_array[ext_buf_pools->num_of_pools_used - 2]];
  807. /* FMBM_RMPD reg. - pool depletion */
  808. if (buf_pool_depletion->pools_grp_mode_enable) {
  809. bpools.grp_bp_depleted_num = buf_pool_depletion->num_of_pools;
  810. for (i = 0; i < port->bm_max_num_of_pools; i++) {
  811. if (buf_pool_depletion->pools_to_consider[i]) {
  812. for (j = 0; j < ext_buf_pools->
  813. num_of_pools_used; j++) {
  814. if (i == ordered_array[j]) {
  815. bpools.bpool[j].
  816. grp_bp_depleted = true;
  817. break;
  818. }
  819. }
  820. }
  821. }
  822. }
  823. if (buf_pool_depletion->single_pool_mode_enable) {
  824. for (i = 0; i < port->bm_max_num_of_pools; i++) {
  825. if (buf_pool_depletion->
  826. pools_to_consider_for_single_mode[i]) {
  827. for (j = 0; j < ext_buf_pools->
  828. num_of_pools_used; j++) {
  829. if (i == ordered_array[j]) {
  830. bpools.bpool[j].
  831. single_bp_depleted = true;
  832. break;
  833. }
  834. }
  835. }
  836. }
  837. }
  838. err = set_bpools(port, &bpools);
  839. if (err != 0) {
  840. dev_err(port->dev, "%s: set_bpools() failed\n", __func__);
  841. return -EINVAL;
  842. }
  843. return 0;
  844. }
  845. static int init_low_level_driver(struct fman_port *port)
  846. {
  847. struct fman_port_cfg *cfg = port->cfg;
  848. u32 tmp_val;
  849. switch (port->port_type) {
  850. case FMAN_PORT_TYPE_RX:
  851. cfg->err_mask = (RX_ERRS_TO_ENQ & ~cfg->discard_mask);
  852. break;
  853. default:
  854. break;
  855. }
  856. tmp_val = (u32)((port->internal_buf_offset % OFFSET_UNITS) ?
  857. (port->internal_buf_offset / OFFSET_UNITS + 1) :
  858. (port->internal_buf_offset / OFFSET_UNITS));
  859. port->internal_buf_offset = (u8)(tmp_val * OFFSET_UNITS);
  860. port->cfg->int_buf_start_margin = port->internal_buf_offset;
  861. if (init(port) != 0) {
  862. dev_err(port->dev, "%s: fman port initialization failed\n",
  863. __func__);
  864. return -ENODEV;
  865. }
  866. /* The code bellow is a trick so the FM will not release the buffer
  867. * to BM nor will try to enqueue the frame to QM
  868. */
  869. if (port->port_type == FMAN_PORT_TYPE_TX) {
  870. if (!cfg->dflt_fqid && cfg->dont_release_buf) {
  871. /* override fmbm_tcfqid 0 with a false non-0 value.
  872. * This will force FM to act according to tfene.
  873. * Otherwise, if fmbm_tcfqid is 0 the FM will release
  874. * buffers to BM regardless of fmbm_tfene
  875. */
  876. iowrite32be(0xFFFFFF, &port->bmi_regs->tx.fmbm_tcfqid);
  877. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE,
  878. &port->bmi_regs->tx.fmbm_tfene);
  879. }
  880. }
  881. return 0;
  882. }
  883. static int fill_soc_specific_params(struct fman_port *port)
  884. {
  885. u32 bmi_max_fifo_size;
  886. bmi_max_fifo_size = fman_get_bmi_max_fifo_size(port->fm);
  887. port->max_port_fifo_size = MAX_PORT_FIFO_SIZE(bmi_max_fifo_size);
  888. port->bm_max_num_of_pools = 64;
  889. /* P4080 - Major 2
  890. * P2041/P3041/P5020/P5040 - Major 3
  891. * Tx/Bx - Major 6
  892. */
  893. switch (port->rev_info.major) {
  894. case 2:
  895. case 3:
  896. port->max_num_of_ext_pools = 4;
  897. port->max_num_of_sub_portals = 12;
  898. break;
  899. case 6:
  900. port->max_num_of_ext_pools = 8;
  901. port->max_num_of_sub_portals = 16;
  902. break;
  903. default:
  904. dev_err(port->dev, "%s: Unsupported FMan version\n", __func__);
  905. return -EINVAL;
  906. }
  907. return 0;
  908. }
  909. static int get_dflt_fifo_deq_pipeline_depth(u8 major, enum fman_port_type type,
  910. u16 speed)
  911. {
  912. switch (type) {
  913. case FMAN_PORT_TYPE_RX:
  914. case FMAN_PORT_TYPE_TX:
  915. switch (speed) {
  916. case 10000:
  917. return 4;
  918. case 1000:
  919. if (major >= 6)
  920. return 2;
  921. else
  922. return 1;
  923. default:
  924. return 0;
  925. }
  926. default:
  927. return 0;
  928. }
  929. }
  930. static int get_dflt_num_of_tasks(u8 major, enum fman_port_type type,
  931. u16 speed)
  932. {
  933. switch (type) {
  934. case FMAN_PORT_TYPE_RX:
  935. case FMAN_PORT_TYPE_TX:
  936. switch (speed) {
  937. case 10000:
  938. return 16;
  939. case 1000:
  940. if (major >= 6)
  941. return 4;
  942. else
  943. return 3;
  944. default:
  945. return 0;
  946. }
  947. default:
  948. return 0;
  949. }
  950. }
  951. static int get_dflt_extra_num_of_tasks(u8 major, enum fman_port_type type,
  952. u16 speed)
  953. {
  954. switch (type) {
  955. case FMAN_PORT_TYPE_RX:
  956. /* FMan V3 */
  957. if (major >= 6)
  958. return 0;
  959. /* FMan V2 */
  960. if (speed == 10000)
  961. return 8;
  962. else
  963. return 2;
  964. case FMAN_PORT_TYPE_TX:
  965. default:
  966. return 0;
  967. }
  968. }
  969. static int get_dflt_num_of_open_dmas(u8 major, enum fman_port_type type,
  970. u16 speed)
  971. {
  972. int val;
  973. if (major >= 6) {
  974. switch (type) {
  975. case FMAN_PORT_TYPE_TX:
  976. if (speed == 10000)
  977. val = 12;
  978. else
  979. val = 3;
  980. break;
  981. case FMAN_PORT_TYPE_RX:
  982. if (speed == 10000)
  983. val = 8;
  984. else
  985. val = 2;
  986. break;
  987. default:
  988. return 0;
  989. }
  990. } else {
  991. switch (type) {
  992. case FMAN_PORT_TYPE_TX:
  993. case FMAN_PORT_TYPE_RX:
  994. if (speed == 10000)
  995. val = 8;
  996. else
  997. val = 1;
  998. break;
  999. default:
  1000. val = 0;
  1001. }
  1002. }
  1003. return val;
  1004. }
  1005. static int get_dflt_extra_num_of_open_dmas(u8 major, enum fman_port_type type,
  1006. u16 speed)
  1007. {
  1008. /* FMan V3 */
  1009. if (major >= 6)
  1010. return 0;
  1011. /* FMan V2 */
  1012. switch (type) {
  1013. case FMAN_PORT_TYPE_RX:
  1014. case FMAN_PORT_TYPE_TX:
  1015. if (speed == 10000)
  1016. return 8;
  1017. else
  1018. return 1;
  1019. default:
  1020. return 0;
  1021. }
  1022. }
  1023. static int get_dflt_num_of_fifo_bufs(u8 major, enum fman_port_type type,
  1024. u16 speed)
  1025. {
  1026. int val;
  1027. if (major >= 6) {
  1028. switch (type) {
  1029. case FMAN_PORT_TYPE_TX:
  1030. if (speed == 10000)
  1031. val = 64;
  1032. else
  1033. val = 50;
  1034. break;
  1035. case FMAN_PORT_TYPE_RX:
  1036. if (speed == 10000)
  1037. val = 96;
  1038. else
  1039. val = 50;
  1040. break;
  1041. default:
  1042. val = 0;
  1043. }
  1044. } else {
  1045. switch (type) {
  1046. case FMAN_PORT_TYPE_TX:
  1047. if (speed == 10000)
  1048. val = 48;
  1049. else
  1050. val = 44;
  1051. break;
  1052. case FMAN_PORT_TYPE_RX:
  1053. if (speed == 10000)
  1054. val = 48;
  1055. else
  1056. val = 45;
  1057. break;
  1058. default:
  1059. val = 0;
  1060. }
  1061. }
  1062. return val;
  1063. }
  1064. static void set_dflt_cfg(struct fman_port *port,
  1065. struct fman_port_params *port_params)
  1066. {
  1067. struct fman_port_cfg *cfg = port->cfg;
  1068. cfg->dma_swap_data = FMAN_PORT_DMA_NO_SWAP;
  1069. cfg->color = FMAN_PORT_COLOR_GREEN;
  1070. cfg->rx_cut_end_bytes = DFLT_PORT_CUT_BYTES_FROM_END;
  1071. cfg->rx_pri_elevation = BMI_PRIORITY_ELEVATION_LEVEL;
  1072. cfg->rx_fifo_thr = BMI_FIFO_THRESHOLD;
  1073. cfg->tx_fifo_low_comf_level = (5 * 1024);
  1074. cfg->deq_type = FMAN_PORT_DEQ_BY_PRI;
  1075. cfg->deq_prefetch_option = FMAN_PORT_DEQ_FULL_PREFETCH;
  1076. cfg->tx_fifo_deq_pipeline_depth =
  1077. BMI_DEQUEUE_PIPELINE_DEPTH(port->port_type, port->port_speed);
  1078. cfg->deq_byte_cnt = QMI_BYTE_COUNT_LEVEL_CONTROL(port->port_type);
  1079. cfg->rx_pri_elevation =
  1080. DFLT_PORT_RX_FIFO_PRI_ELEVATION_LEV(port->max_port_fifo_size);
  1081. port->cfg->rx_fifo_thr =
  1082. DFLT_PORT_RX_FIFO_THRESHOLD(port->rev_info.major,
  1083. port->max_port_fifo_size);
  1084. if ((port->rev_info.major == 6) &&
  1085. ((port->rev_info.minor == 0) || (port->rev_info.minor == 3)))
  1086. cfg->errata_A006320 = true;
  1087. /* Excessive Threshold register - exists for pre-FMv3 chips only */
  1088. if (port->rev_info.major < 6)
  1089. cfg->excessive_threshold_register = true;
  1090. else
  1091. cfg->fmbm_tfne_has_features = true;
  1092. cfg->buffer_prefix_content.data_align =
  1093. DFLT_PORT_BUFFER_PREFIX_CONTEXT_DATA_ALIGN;
  1094. }
  1095. static void set_rx_dflt_cfg(struct fman_port *port,
  1096. struct fman_port_params *port_params)
  1097. {
  1098. port->cfg->discard_mask = DFLT_PORT_ERRORS_TO_DISCARD;
  1099. memcpy(&port->cfg->ext_buf_pools,
  1100. &port_params->specific_params.rx_params.ext_buf_pools,
  1101. sizeof(struct fman_ext_pools));
  1102. port->cfg->err_fqid =
  1103. port_params->specific_params.rx_params.err_fqid;
  1104. port->cfg->dflt_fqid =
  1105. port_params->specific_params.rx_params.dflt_fqid;
  1106. port->cfg->pcd_base_fqid =
  1107. port_params->specific_params.rx_params.pcd_base_fqid;
  1108. port->cfg->pcd_fqs_count =
  1109. port_params->specific_params.rx_params.pcd_fqs_count;
  1110. }
  1111. static void set_tx_dflt_cfg(struct fman_port *port,
  1112. struct fman_port_params *port_params,
  1113. struct fman_port_dts_params *dts_params)
  1114. {
  1115. port->cfg->tx_fifo_deq_pipeline_depth =
  1116. get_dflt_fifo_deq_pipeline_depth(port->rev_info.major,
  1117. port->port_type,
  1118. port->port_speed);
  1119. port->cfg->err_fqid =
  1120. port_params->specific_params.non_rx_params.err_fqid;
  1121. port->cfg->deq_sp =
  1122. (u8)(dts_params->qman_channel_id & QMI_DEQ_CFG_SUBPORTAL_MASK);
  1123. port->cfg->dflt_fqid =
  1124. port_params->specific_params.non_rx_params.dflt_fqid;
  1125. port->cfg->deq_high_priority = true;
  1126. }
  1127. /**
  1128. * fman_port_config
  1129. * @port: Pointer to the port structure
  1130. * @params: Pointer to data structure of parameters
  1131. *
  1132. * Creates a descriptor for the FM PORT module.
  1133. * The routine returns a pointer to the FM PORT object.
  1134. * This descriptor must be passed as first parameter to all other FM PORT
  1135. * function calls.
  1136. * No actual initialization or configuration of FM hardware is done by this
  1137. * routine.
  1138. *
  1139. * Return: 0 on success; Error code otherwise.
  1140. */
  1141. int fman_port_config(struct fman_port *port, struct fman_port_params *params)
  1142. {
  1143. void __iomem *base_addr = port->dts_params.base_addr;
  1144. int err;
  1145. /* Allocate the FM driver's parameters structure */
  1146. port->cfg = kzalloc(sizeof(*port->cfg), GFP_KERNEL);
  1147. if (!port->cfg)
  1148. return -EINVAL;
  1149. /* Initialize FM port parameters which will be kept by the driver */
  1150. port->port_type = port->dts_params.type;
  1151. port->port_speed = port->dts_params.speed;
  1152. port->port_id = port->dts_params.id;
  1153. port->fm = port->dts_params.fman;
  1154. port->ext_pools_num = (u8)8;
  1155. /* get FM revision */
  1156. fman_get_revision(port->fm, &port->rev_info);
  1157. err = fill_soc_specific_params(port);
  1158. if (err)
  1159. goto err_port_cfg;
  1160. switch (port->port_type) {
  1161. case FMAN_PORT_TYPE_RX:
  1162. set_rx_dflt_cfg(port, params);
  1163. /* fall through */
  1164. case FMAN_PORT_TYPE_TX:
  1165. set_tx_dflt_cfg(port, params, &port->dts_params);
  1166. /* fall through */
  1167. default:
  1168. set_dflt_cfg(port, params);
  1169. }
  1170. /* Continue with other parameters */
  1171. /* set memory map pointers */
  1172. port->bmi_regs = base_addr + BMI_PORT_REGS_OFFSET;
  1173. port->qmi_regs = base_addr + QMI_PORT_REGS_OFFSET;
  1174. port->hwp_regs = base_addr + HWP_PORT_REGS_OFFSET;
  1175. port->max_frame_length = DFLT_PORT_MAX_FRAME_LENGTH;
  1176. /* resource distribution. */
  1177. port->fifo_bufs.num =
  1178. get_dflt_num_of_fifo_bufs(port->rev_info.major, port->port_type,
  1179. port->port_speed) * FMAN_BMI_FIFO_UNITS;
  1180. port->fifo_bufs.extra =
  1181. DFLT_PORT_EXTRA_NUM_OF_FIFO_BUFS * FMAN_BMI_FIFO_UNITS;
  1182. port->open_dmas.num =
  1183. get_dflt_num_of_open_dmas(port->rev_info.major,
  1184. port->port_type, port->port_speed);
  1185. port->open_dmas.extra =
  1186. get_dflt_extra_num_of_open_dmas(port->rev_info.major,
  1187. port->port_type, port->port_speed);
  1188. port->tasks.num =
  1189. get_dflt_num_of_tasks(port->rev_info.major,
  1190. port->port_type, port->port_speed);
  1191. port->tasks.extra =
  1192. get_dflt_extra_num_of_tasks(port->rev_info.major,
  1193. port->port_type, port->port_speed);
  1194. /* FM_HEAVY_TRAFFIC_SEQUENCER_HANG_ERRATA_FMAN_A006981 errata
  1195. * workaround
  1196. */
  1197. if ((port->rev_info.major == 6) && (port->rev_info.minor == 0) &&
  1198. (((port->port_type == FMAN_PORT_TYPE_TX) &&
  1199. (port->port_speed == 1000)))) {
  1200. port->open_dmas.num = 16;
  1201. port->open_dmas.extra = 0;
  1202. }
  1203. if (port->rev_info.major >= 6 &&
  1204. port->port_type == FMAN_PORT_TYPE_TX &&
  1205. port->port_speed == 1000) {
  1206. /* FM_WRONG_RESET_VALUES_ERRATA_FMAN_A005127 Errata
  1207. * workaround
  1208. */
  1209. u32 reg;
  1210. reg = 0x00001013;
  1211. iowrite32be(reg, &port->bmi_regs->tx.fmbm_tfp);
  1212. }
  1213. return 0;
  1214. err_port_cfg:
  1215. kfree(port->cfg);
  1216. return -EINVAL;
  1217. }
  1218. EXPORT_SYMBOL(fman_port_config);
  1219. /**
  1220. * fman_port_use_kg_hash
  1221. * port: A pointer to a FM Port module.
  1222. * Sets the HW KeyGen or the BMI as HW Parser next engine, enabling
  1223. * or bypassing the KeyGen hashing of Rx traffic
  1224. */
  1225. void fman_port_use_kg_hash(struct fman_port *port, bool enable)
  1226. {
  1227. if (enable)
  1228. /* After the Parser frames go to KeyGen */
  1229. iowrite32be(NIA_ENG_HWK, &port->bmi_regs->rx.fmbm_rfpne);
  1230. else
  1231. /* After the Parser frames go to BMI */
  1232. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_ENQ_FRAME,
  1233. &port->bmi_regs->rx.fmbm_rfpne);
  1234. }
  1235. EXPORT_SYMBOL(fman_port_use_kg_hash);
  1236. /**
  1237. * fman_port_init
  1238. * port: A pointer to a FM Port module.
  1239. * Initializes the FM PORT module by defining the software structure and
  1240. * configuring the hardware registers.
  1241. *
  1242. * Return: 0 on success; Error code otherwise.
  1243. */
  1244. int fman_port_init(struct fman_port *port)
  1245. {
  1246. struct fman_port_init_params params;
  1247. struct fman_keygen *keygen;
  1248. struct fman_port_cfg *cfg;
  1249. int err;
  1250. if (is_init_done(port->cfg))
  1251. return -EINVAL;
  1252. err = fman_sp_build_buffer_struct(&port->cfg->int_context,
  1253. &port->cfg->buffer_prefix_content,
  1254. &port->cfg->buf_margins,
  1255. &port->buffer_offsets,
  1256. &port->internal_buf_offset);
  1257. if (err)
  1258. return err;
  1259. cfg = port->cfg;
  1260. if (port->port_type == FMAN_PORT_TYPE_RX) {
  1261. /* Call the external Buffer routine which also checks fifo
  1262. * size and updates it if necessary
  1263. */
  1264. /* define external buffer pools and pool depletion */
  1265. err = set_ext_buffer_pools(port);
  1266. if (err)
  1267. return err;
  1268. /* check if the largest external buffer pool is large enough */
  1269. if (cfg->buf_margins.start_margins + MIN_EXT_BUF_SIZE +
  1270. cfg->buf_margins.end_margins >
  1271. port->rx_pools_params.largest_buf_size) {
  1272. dev_err(port->dev, "%s: buf_margins.start_margins (%d) + minimum buf size (64) + buf_margins.end_margins (%d) is larger than maximum external buffer size (%d)\n",
  1273. __func__, cfg->buf_margins.start_margins,
  1274. cfg->buf_margins.end_margins,
  1275. port->rx_pools_params.largest_buf_size);
  1276. return -EINVAL;
  1277. }
  1278. }
  1279. /* Call FM module routine for communicating parameters */
  1280. memset(&params, 0, sizeof(params));
  1281. params.port_id = port->port_id;
  1282. params.port_type = port->port_type;
  1283. params.port_speed = port->port_speed;
  1284. params.num_of_tasks = (u8)port->tasks.num;
  1285. params.num_of_extra_tasks = (u8)port->tasks.extra;
  1286. params.num_of_open_dmas = (u8)port->open_dmas.num;
  1287. params.num_of_extra_open_dmas = (u8)port->open_dmas.extra;
  1288. if (port->fifo_bufs.num) {
  1289. err = verify_size_of_fifo(port);
  1290. if (err)
  1291. return err;
  1292. }
  1293. params.size_of_fifo = port->fifo_bufs.num;
  1294. params.extra_size_of_fifo = port->fifo_bufs.extra;
  1295. params.deq_pipeline_depth = port->cfg->tx_fifo_deq_pipeline_depth;
  1296. params.max_frame_length = port->max_frame_length;
  1297. err = fman_set_port_params(port->fm, &params);
  1298. if (err)
  1299. return err;
  1300. err = init_low_level_driver(port);
  1301. if (err)
  1302. return err;
  1303. if (port->cfg->pcd_fqs_count) {
  1304. keygen = port->dts_params.fman->keygen;
  1305. err = keygen_port_hashing_init(keygen, port->port_id,
  1306. port->cfg->pcd_base_fqid,
  1307. port->cfg->pcd_fqs_count);
  1308. if (err)
  1309. return err;
  1310. fman_port_use_kg_hash(port, true);
  1311. }
  1312. kfree(port->cfg);
  1313. port->cfg = NULL;
  1314. return 0;
  1315. }
  1316. EXPORT_SYMBOL(fman_port_init);
  1317. /**
  1318. * fman_port_cfg_buf_prefix_content
  1319. * @port A pointer to a FM Port module.
  1320. * @buffer_prefix_content A structure of parameters describing
  1321. * the structure of the buffer.
  1322. * Out parameter:
  1323. * Start margin - offset of data from
  1324. * start of external buffer.
  1325. * Defines the structure, size and content of the application buffer.
  1326. * The prefix, in Tx ports, if 'pass_prs_result', the application should set
  1327. * a value to their offsets in the prefix of the FM will save the first
  1328. * 'priv_data_size', than, depending on 'pass_prs_result' and
  1329. * 'pass_time_stamp', copy parse result and timeStamp, and the packet itself
  1330. * (in this order), to the application buffer, and to offset.
  1331. * Calling this routine changes the buffer margins definitions in the internal
  1332. * driver data base from its default configuration:
  1333. * Data size: [DEFAULT_PORT_BUFFER_PREFIX_CONTENT_PRIV_DATA_SIZE]
  1334. * Pass Parser result: [DEFAULT_PORT_BUFFER_PREFIX_CONTENT_PASS_PRS_RESULT].
  1335. * Pass timestamp: [DEFAULT_PORT_BUFFER_PREFIX_CONTENT_PASS_TIME_STAMP].
  1336. * May be used for all ports
  1337. *
  1338. * Allowed only following fman_port_config() and before fman_port_init().
  1339. *
  1340. * Return: 0 on success; Error code otherwise.
  1341. */
  1342. int fman_port_cfg_buf_prefix_content(struct fman_port *port,
  1343. struct fman_buffer_prefix_content *
  1344. buffer_prefix_content)
  1345. {
  1346. if (is_init_done(port->cfg))
  1347. return -EINVAL;
  1348. memcpy(&port->cfg->buffer_prefix_content,
  1349. buffer_prefix_content,
  1350. sizeof(struct fman_buffer_prefix_content));
  1351. /* if data_align was not initialized by user,
  1352. * we return to driver's default
  1353. */
  1354. if (!port->cfg->buffer_prefix_content.data_align)
  1355. port->cfg->buffer_prefix_content.data_align =
  1356. DFLT_PORT_BUFFER_PREFIX_CONTEXT_DATA_ALIGN;
  1357. return 0;
  1358. }
  1359. EXPORT_SYMBOL(fman_port_cfg_buf_prefix_content);
  1360. /**
  1361. * fman_port_disable
  1362. * port: A pointer to a FM Port module.
  1363. *
  1364. * Gracefully disable an FM port. The port will not start new tasks after all
  1365. * tasks associated with the port are terminated.
  1366. *
  1367. * This is a blocking routine, it returns after port is gracefully stopped,
  1368. * i.e. the port will not except new frames, but it will finish all frames
  1369. * or tasks which were already began.
  1370. * Allowed only following fman_port_init().
  1371. *
  1372. * Return: 0 on success; Error code otherwise.
  1373. */
  1374. int fman_port_disable(struct fman_port *port)
  1375. {
  1376. u32 __iomem *bmi_cfg_reg, *bmi_status_reg;
  1377. u32 tmp;
  1378. bool rx_port, failure = false;
  1379. int count;
  1380. if (!is_init_done(port->cfg))
  1381. return -EINVAL;
  1382. switch (port->port_type) {
  1383. case FMAN_PORT_TYPE_RX:
  1384. bmi_cfg_reg = &port->bmi_regs->rx.fmbm_rcfg;
  1385. bmi_status_reg = &port->bmi_regs->rx.fmbm_rst;
  1386. rx_port = true;
  1387. break;
  1388. case FMAN_PORT_TYPE_TX:
  1389. bmi_cfg_reg = &port->bmi_regs->tx.fmbm_tcfg;
  1390. bmi_status_reg = &port->bmi_regs->tx.fmbm_tst;
  1391. rx_port = false;
  1392. break;
  1393. default:
  1394. return -EINVAL;
  1395. }
  1396. /* Disable QMI */
  1397. if (!rx_port) {
  1398. tmp = ioread32be(&port->qmi_regs->fmqm_pnc) & ~QMI_PORT_CFG_EN;
  1399. iowrite32be(tmp, &port->qmi_regs->fmqm_pnc);
  1400. /* Wait for QMI to finish FD handling */
  1401. count = 100;
  1402. do {
  1403. udelay(10);
  1404. tmp = ioread32be(&port->qmi_regs->fmqm_pns);
  1405. } while ((tmp & QMI_PORT_STATUS_DEQ_FD_BSY) && --count);
  1406. if (count == 0) {
  1407. /* Timeout */
  1408. failure = true;
  1409. }
  1410. }
  1411. /* Disable BMI */
  1412. tmp = ioread32be(bmi_cfg_reg) & ~BMI_PORT_CFG_EN;
  1413. iowrite32be(tmp, bmi_cfg_reg);
  1414. /* Wait for graceful stop end */
  1415. count = 500;
  1416. do {
  1417. udelay(10);
  1418. tmp = ioread32be(bmi_status_reg);
  1419. } while ((tmp & BMI_PORT_STATUS_BSY) && --count);
  1420. if (count == 0) {
  1421. /* Timeout */
  1422. failure = true;
  1423. }
  1424. if (failure)
  1425. dev_dbg(port->dev, "%s: FMan Port[%d]: BMI or QMI is Busy. Port forced down\n",
  1426. __func__, port->port_id);
  1427. return 0;
  1428. }
  1429. EXPORT_SYMBOL(fman_port_disable);
  1430. /**
  1431. * fman_port_enable
  1432. * port: A pointer to a FM Port module.
  1433. *
  1434. * A runtime routine provided to allow disable/enable of port.
  1435. *
  1436. * Allowed only following fman_port_init().
  1437. *
  1438. * Return: 0 on success; Error code otherwise.
  1439. */
  1440. int fman_port_enable(struct fman_port *port)
  1441. {
  1442. u32 __iomem *bmi_cfg_reg;
  1443. u32 tmp;
  1444. bool rx_port;
  1445. if (!is_init_done(port->cfg))
  1446. return -EINVAL;
  1447. switch (port->port_type) {
  1448. case FMAN_PORT_TYPE_RX:
  1449. bmi_cfg_reg = &port->bmi_regs->rx.fmbm_rcfg;
  1450. rx_port = true;
  1451. break;
  1452. case FMAN_PORT_TYPE_TX:
  1453. bmi_cfg_reg = &port->bmi_regs->tx.fmbm_tcfg;
  1454. rx_port = false;
  1455. break;
  1456. default:
  1457. return -EINVAL;
  1458. }
  1459. /* Enable QMI */
  1460. if (!rx_port) {
  1461. tmp = ioread32be(&port->qmi_regs->fmqm_pnc) | QMI_PORT_CFG_EN;
  1462. iowrite32be(tmp, &port->qmi_regs->fmqm_pnc);
  1463. }
  1464. /* Enable BMI */
  1465. tmp = ioread32be(bmi_cfg_reg) | BMI_PORT_CFG_EN;
  1466. iowrite32be(tmp, bmi_cfg_reg);
  1467. return 0;
  1468. }
  1469. EXPORT_SYMBOL(fman_port_enable);
  1470. /**
  1471. * fman_port_bind
  1472. * dev: FMan Port OF device pointer
  1473. *
  1474. * Bind to a specific FMan Port.
  1475. *
  1476. * Allowed only after the port was created.
  1477. *
  1478. * Return: A pointer to the FMan port device.
  1479. */
  1480. struct fman_port *fman_port_bind(struct device *dev)
  1481. {
  1482. return (struct fman_port *)(dev_get_drvdata(get_device(dev)));
  1483. }
  1484. EXPORT_SYMBOL(fman_port_bind);
  1485. /**
  1486. * fman_port_get_qman_channel_id
  1487. * port: Pointer to the FMan port devuce
  1488. *
  1489. * Get the QMan channel ID for the specific port
  1490. *
  1491. * Return: QMan channel ID
  1492. */
  1493. u32 fman_port_get_qman_channel_id(struct fman_port *port)
  1494. {
  1495. return port->dts_params.qman_channel_id;
  1496. }
  1497. EXPORT_SYMBOL(fman_port_get_qman_channel_id);
  1498. int fman_port_get_hash_result_offset(struct fman_port *port, u32 *offset)
  1499. {
  1500. if (port->buffer_offsets.hash_result_offset == ILLEGAL_BASE)
  1501. return -EINVAL;
  1502. *offset = port->buffer_offsets.hash_result_offset;
  1503. return 0;
  1504. }
  1505. EXPORT_SYMBOL(fman_port_get_hash_result_offset);
  1506. int fman_port_get_tstamp(struct fman_port *port, const void *data, u64 *tstamp)
  1507. {
  1508. if (port->buffer_offsets.time_stamp_offset == ILLEGAL_BASE)
  1509. return -EINVAL;
  1510. *tstamp = be64_to_cpu(*(__be64 *)(data +
  1511. port->buffer_offsets.time_stamp_offset));
  1512. return 0;
  1513. }
  1514. EXPORT_SYMBOL(fman_port_get_tstamp);
  1515. static int fman_port_probe(struct platform_device *of_dev)
  1516. {
  1517. struct fman_port *port;
  1518. struct fman *fman;
  1519. struct device_node *fm_node, *port_node;
  1520. struct resource res;
  1521. struct resource *dev_res;
  1522. u32 val;
  1523. int err = 0, lenp;
  1524. enum fman_port_type port_type;
  1525. u16 port_speed;
  1526. u8 port_id;
  1527. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1528. if (!port)
  1529. return -ENOMEM;
  1530. port->dev = &of_dev->dev;
  1531. port_node = of_node_get(of_dev->dev.of_node);
  1532. /* Get the FM node */
  1533. fm_node = of_get_parent(port_node);
  1534. if (!fm_node) {
  1535. dev_err(port->dev, "%s: of_get_parent() failed\n", __func__);
  1536. err = -ENODEV;
  1537. goto return_err;
  1538. }
  1539. fman = dev_get_drvdata(&of_find_device_by_node(fm_node)->dev);
  1540. of_node_put(fm_node);
  1541. if (!fman) {
  1542. err = -EINVAL;
  1543. goto return_err;
  1544. }
  1545. err = of_property_read_u32(port_node, "cell-index", &val);
  1546. if (err) {
  1547. dev_err(port->dev, "%s: reading cell-index for %pOF failed\n",
  1548. __func__, port_node);
  1549. err = -EINVAL;
  1550. goto return_err;
  1551. }
  1552. port_id = (u8)val;
  1553. port->dts_params.id = port_id;
  1554. if (of_device_is_compatible(port_node, "fsl,fman-v3-port-tx")) {
  1555. port_type = FMAN_PORT_TYPE_TX;
  1556. port_speed = 1000;
  1557. if (of_find_property(port_node, "fsl,fman-10g-port", &lenp))
  1558. port_speed = 10000;
  1559. } else if (of_device_is_compatible(port_node, "fsl,fman-v2-port-tx")) {
  1560. if (port_id >= TX_10G_PORT_BASE)
  1561. port_speed = 10000;
  1562. else
  1563. port_speed = 1000;
  1564. port_type = FMAN_PORT_TYPE_TX;
  1565. } else if (of_device_is_compatible(port_node, "fsl,fman-v3-port-rx")) {
  1566. port_type = FMAN_PORT_TYPE_RX;
  1567. port_speed = 1000;
  1568. if (of_find_property(port_node, "fsl,fman-10g-port", &lenp))
  1569. port_speed = 10000;
  1570. } else if (of_device_is_compatible(port_node, "fsl,fman-v2-port-rx")) {
  1571. if (port_id >= RX_10G_PORT_BASE)
  1572. port_speed = 10000;
  1573. else
  1574. port_speed = 1000;
  1575. port_type = FMAN_PORT_TYPE_RX;
  1576. } else {
  1577. dev_err(port->dev, "%s: Illegal port type\n", __func__);
  1578. err = -EINVAL;
  1579. goto return_err;
  1580. }
  1581. port->dts_params.type = port_type;
  1582. port->dts_params.speed = port_speed;
  1583. if (port_type == FMAN_PORT_TYPE_TX) {
  1584. u32 qman_channel_id;
  1585. qman_channel_id = fman_get_qman_channel_id(fman, port_id);
  1586. if (qman_channel_id == 0) {
  1587. dev_err(port->dev, "%s: incorrect qman-channel-id\n",
  1588. __func__);
  1589. err = -EINVAL;
  1590. goto return_err;
  1591. }
  1592. port->dts_params.qman_channel_id = qman_channel_id;
  1593. }
  1594. err = of_address_to_resource(port_node, 0, &res);
  1595. if (err < 0) {
  1596. dev_err(port->dev, "%s: of_address_to_resource() failed\n",
  1597. __func__);
  1598. err = -ENOMEM;
  1599. goto return_err;
  1600. }
  1601. port->dts_params.fman = fman;
  1602. of_node_put(port_node);
  1603. dev_res = __devm_request_region(port->dev, &res, res.start,
  1604. resource_size(&res), "fman-port");
  1605. if (!dev_res) {
  1606. dev_err(port->dev, "%s: __devm_request_region() failed\n",
  1607. __func__);
  1608. err = -EINVAL;
  1609. goto free_port;
  1610. }
  1611. port->dts_params.base_addr = devm_ioremap(port->dev, res.start,
  1612. resource_size(&res));
  1613. if (!port->dts_params.base_addr)
  1614. dev_err(port->dev, "%s: devm_ioremap() failed\n", __func__);
  1615. dev_set_drvdata(&of_dev->dev, port);
  1616. return 0;
  1617. return_err:
  1618. of_node_put(port_node);
  1619. free_port:
  1620. kfree(port);
  1621. return err;
  1622. }
  1623. static const struct of_device_id fman_port_match[] = {
  1624. {.compatible = "fsl,fman-v3-port-rx"},
  1625. {.compatible = "fsl,fman-v2-port-rx"},
  1626. {.compatible = "fsl,fman-v3-port-tx"},
  1627. {.compatible = "fsl,fman-v2-port-tx"},
  1628. {}
  1629. };
  1630. MODULE_DEVICE_TABLE(of, fman_port_match);
  1631. static struct platform_driver fman_port_driver = {
  1632. .driver = {
  1633. .name = "fsl-fman-port",
  1634. .of_match_table = fman_port_match,
  1635. },
  1636. .probe = fman_port_probe,
  1637. };
  1638. static int __init fman_port_load(void)
  1639. {
  1640. int err;
  1641. pr_debug("FSL DPAA FMan driver\n");
  1642. err = platform_driver_register(&fman_port_driver);
  1643. if (err < 0)
  1644. pr_err("Error, platform_driver_register() = %d\n", err);
  1645. return err;
  1646. }
  1647. module_init(fman_port_load);
  1648. static void __exit fman_port_unload(void)
  1649. {
  1650. platform_driver_unregister(&fman_port_driver);
  1651. }
  1652. module_exit(fman_port_unload);
  1653. MODULE_LICENSE("Dual BSD/GPL");
  1654. MODULE_DESCRIPTION("Freescale DPAA Frame Manager Port driver");