fman_port.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  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. struct fman_port_hwp_regs {
  285. struct {
  286. u32 ssa; /* Soft Sequence Attachment */
  287. u32 lcv; /* Line-up Enable Confirmation Mask */
  288. } pmda[HWP_HXS_COUNT]; /* Parse Memory Direct Access Registers */
  289. u32 reserved080[(0x3f8 - 0x080) / 4]; /* (0x080-0x3f7) */
  290. u32 fmpr_pcac; /* Configuration Access Control */
  291. };
  292. /* QMI dequeue prefetch modes */
  293. enum fman_port_deq_prefetch {
  294. FMAN_PORT_DEQ_NO_PREFETCH, /* No prefetch mode */
  295. FMAN_PORT_DEQ_PART_PREFETCH, /* Partial prefetch mode */
  296. FMAN_PORT_DEQ_FULL_PREFETCH /* Full prefetch mode */
  297. };
  298. /* A structure for defining FM port resources */
  299. struct fman_port_rsrc {
  300. u32 num; /* Committed required resource */
  301. u32 extra; /* Extra (not committed) required resource */
  302. };
  303. enum fman_port_dma_swap {
  304. FMAN_PORT_DMA_NO_SWAP, /* No swap, transfer data as is */
  305. FMAN_PORT_DMA_SWAP_LE,
  306. /* The transferred data should be swapped in PPC Little Endian mode */
  307. FMAN_PORT_DMA_SWAP_BE
  308. /* The transferred data should be swapped in Big Endian mode */
  309. };
  310. /* Default port color */
  311. enum fman_port_color {
  312. FMAN_PORT_COLOR_GREEN, /* Default port color is green */
  313. FMAN_PORT_COLOR_YELLOW, /* Default port color is yellow */
  314. FMAN_PORT_COLOR_RED, /* Default port color is red */
  315. FMAN_PORT_COLOR_OVERRIDE /* Ignore color */
  316. };
  317. /* QMI dequeue from the SP channel - types */
  318. enum fman_port_deq_type {
  319. FMAN_PORT_DEQ_BY_PRI,
  320. /* Priority precedence and Intra-Class scheduling */
  321. FMAN_PORT_DEQ_ACTIVE_FQ,
  322. /* Active FQ precedence and Intra-Class scheduling */
  323. FMAN_PORT_DEQ_ACTIVE_FQ_NO_ICS
  324. /* Active FQ precedence and override Intra-Class scheduling */
  325. };
  326. /* External buffer pools configuration */
  327. struct fman_port_bpools {
  328. u8 count; /* Num of pools to set up */
  329. bool counters_enable; /* Enable allocate counters */
  330. u8 grp_bp_depleted_num;
  331. /* Number of depleted pools - if reached the BMI indicates
  332. * the MAC to send a pause frame
  333. */
  334. struct {
  335. u8 bpid; /* BM pool ID */
  336. u16 size;
  337. /* Pool's size - must be in ascending order */
  338. bool is_backup;
  339. /* If this is a backup pool */
  340. bool grp_bp_depleted;
  341. /* Consider this buffer in multiple pools depletion criteria */
  342. bool single_bp_depleted;
  343. /* Consider this buffer in single pool depletion criteria */
  344. } bpool[FMAN_PORT_MAX_EXT_POOLS_NUM];
  345. };
  346. struct fman_port_cfg {
  347. u32 dflt_fqid;
  348. u32 err_fqid;
  349. u32 pcd_base_fqid;
  350. u32 pcd_fqs_count;
  351. u8 deq_sp;
  352. bool deq_high_priority;
  353. enum fman_port_deq_type deq_type;
  354. enum fman_port_deq_prefetch deq_prefetch_option;
  355. u16 deq_byte_cnt;
  356. u8 cheksum_last_bytes_ignore;
  357. u8 rx_cut_end_bytes;
  358. struct fman_buf_pool_depletion buf_pool_depletion;
  359. struct fman_ext_pools ext_buf_pools;
  360. u32 tx_fifo_min_level;
  361. u32 tx_fifo_low_comf_level;
  362. u32 rx_pri_elevation;
  363. u32 rx_fifo_thr;
  364. struct fman_sp_buf_margins buf_margins;
  365. u32 int_buf_start_margin;
  366. struct fman_sp_int_context_data_copy int_context;
  367. u32 discard_mask;
  368. u32 err_mask;
  369. struct fman_buffer_prefix_content buffer_prefix_content;
  370. bool dont_release_buf;
  371. u8 rx_fd_bits;
  372. u32 tx_fifo_deq_pipeline_depth;
  373. bool errata_A006320;
  374. bool excessive_threshold_register;
  375. bool fmbm_tfne_has_features;
  376. enum fman_port_dma_swap dma_swap_data;
  377. enum fman_port_color color;
  378. };
  379. struct fman_port_rx_pools_params {
  380. u8 num_of_pools;
  381. u16 second_largest_buf_size;
  382. u16 largest_buf_size;
  383. };
  384. struct fman_port_dts_params {
  385. void __iomem *base_addr; /* FMan port virtual memory */
  386. enum fman_port_type type; /* Port type */
  387. u16 speed; /* Port speed */
  388. u8 id; /* HW Port Id */
  389. u32 qman_channel_id; /* QMan channel id (non RX only) */
  390. struct fman *fman; /* FMan Handle */
  391. };
  392. struct fman_port {
  393. void *fm;
  394. struct device *dev;
  395. struct fman_rev_info rev_info;
  396. u8 port_id;
  397. enum fman_port_type port_type;
  398. u16 port_speed;
  399. union fman_port_bmi_regs __iomem *bmi_regs;
  400. struct fman_port_qmi_regs __iomem *qmi_regs;
  401. struct fman_port_hwp_regs __iomem *hwp_regs;
  402. struct fman_sp_buffer_offsets buffer_offsets;
  403. u8 internal_buf_offset;
  404. struct fman_ext_pools ext_buf_pools;
  405. u16 max_frame_length;
  406. struct fman_port_rsrc open_dmas;
  407. struct fman_port_rsrc tasks;
  408. struct fman_port_rsrc fifo_bufs;
  409. struct fman_port_rx_pools_params rx_pools_params;
  410. struct fman_port_cfg *cfg;
  411. struct fman_port_dts_params dts_params;
  412. u8 ext_pools_num;
  413. u32 max_port_fifo_size;
  414. u32 max_num_of_ext_pools;
  415. u32 max_num_of_sub_portals;
  416. u32 bm_max_num_of_pools;
  417. };
  418. static int init_bmi_rx(struct fman_port *port)
  419. {
  420. struct fman_port_rx_bmi_regs __iomem *regs = &port->bmi_regs->rx;
  421. struct fman_port_cfg *cfg = port->cfg;
  422. u32 tmp;
  423. /* DMA attributes */
  424. tmp = (u32)cfg->dma_swap_data << BMI_DMA_ATTR_SWP_SHIFT;
  425. /* Enable write optimization */
  426. tmp |= BMI_DMA_ATTR_WRITE_OPTIMIZE;
  427. iowrite32be(tmp, &regs->fmbm_rda);
  428. /* Rx FIFO parameters */
  429. tmp = (cfg->rx_pri_elevation / PORT_BMI_FIFO_UNITS - 1) <<
  430. BMI_RX_FIFO_PRI_ELEVATION_SHIFT;
  431. tmp |= cfg->rx_fifo_thr / PORT_BMI_FIFO_UNITS - 1;
  432. iowrite32be(tmp, &regs->fmbm_rfp);
  433. if (cfg->excessive_threshold_register)
  434. /* always allow access to the extra resources */
  435. iowrite32be(BMI_RX_FIFO_THRESHOLD_ETHE, &regs->fmbm_reth);
  436. /* Frame end data */
  437. tmp = (cfg->cheksum_last_bytes_ignore & BMI_FRAME_END_CS_IGNORE_MASK) <<
  438. BMI_FRAME_END_CS_IGNORE_SHIFT;
  439. tmp |= (cfg->rx_cut_end_bytes & BMI_RX_FRAME_END_CUT_MASK) <<
  440. BMI_RX_FRAME_END_CUT_SHIFT;
  441. if (cfg->errata_A006320)
  442. tmp &= 0xffe0ffff;
  443. iowrite32be(tmp, &regs->fmbm_rfed);
  444. /* Internal context parameters */
  445. tmp = ((cfg->int_context.ext_buf_offset / PORT_IC_OFFSET_UNITS) &
  446. BMI_IC_TO_EXT_MASK) << BMI_IC_TO_EXT_SHIFT;
  447. tmp |= ((cfg->int_context.int_context_offset / PORT_IC_OFFSET_UNITS) &
  448. BMI_IC_FROM_INT_MASK) << BMI_IC_FROM_INT_SHIFT;
  449. tmp |= (cfg->int_context.size / PORT_IC_OFFSET_UNITS) &
  450. BMI_IC_SIZE_MASK;
  451. iowrite32be(tmp, &regs->fmbm_ricp);
  452. /* Internal buffer offset */
  453. tmp = ((cfg->int_buf_start_margin / PORT_IC_OFFSET_UNITS) &
  454. BMI_INT_BUF_MARG_MASK) << BMI_INT_BUF_MARG_SHIFT;
  455. iowrite32be(tmp, &regs->fmbm_rim);
  456. /* External buffer margins */
  457. tmp = (cfg->buf_margins.start_margins & BMI_EXT_BUF_MARG_START_MASK) <<
  458. BMI_EXT_BUF_MARG_START_SHIFT;
  459. tmp |= cfg->buf_margins.end_margins & BMI_EXT_BUF_MARG_END_MASK;
  460. iowrite32be(tmp, &regs->fmbm_rebm);
  461. /* Frame attributes */
  462. tmp = BMI_CMD_RX_MR_DEF;
  463. tmp |= BMI_CMD_ATTR_ORDER;
  464. tmp |= (u32)cfg->color << BMI_CMD_ATTR_COLOR_SHIFT;
  465. /* Synchronization request */
  466. tmp |= BMI_CMD_ATTR_SYNC;
  467. iowrite32be(tmp, &regs->fmbm_rfca);
  468. /* NIA */
  469. tmp = (u32)cfg->rx_fd_bits << BMI_NEXT_ENG_FD_BITS_SHIFT;
  470. tmp |= NIA_ENG_HWP;
  471. iowrite32be(tmp, &regs->fmbm_rfne);
  472. /* Parser Next Engine NIA */
  473. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_ENQ_FRAME, &regs->fmbm_rfpne);
  474. /* Enqueue NIA */
  475. iowrite32be(NIA_ENG_QMI_ENQ | NIA_ORDER_RESTOR, &regs->fmbm_rfene);
  476. /* Default/error queues */
  477. iowrite32be((cfg->dflt_fqid & DFLT_FQ_ID), &regs->fmbm_rfqid);
  478. iowrite32be((cfg->err_fqid & DFLT_FQ_ID), &regs->fmbm_refqid);
  479. /* Discard/error masks */
  480. iowrite32be(cfg->discard_mask, &regs->fmbm_rfsdm);
  481. iowrite32be(cfg->err_mask, &regs->fmbm_rfsem);
  482. return 0;
  483. }
  484. static int init_bmi_tx(struct fman_port *port)
  485. {
  486. struct fman_port_tx_bmi_regs __iomem *regs = &port->bmi_regs->tx;
  487. struct fman_port_cfg *cfg = port->cfg;
  488. u32 tmp;
  489. /* Tx Configuration register */
  490. tmp = 0;
  491. iowrite32be(tmp, &regs->fmbm_tcfg);
  492. /* DMA attributes */
  493. tmp = (u32)cfg->dma_swap_data << BMI_DMA_ATTR_SWP_SHIFT;
  494. iowrite32be(tmp, &regs->fmbm_tda);
  495. /* Tx FIFO parameters */
  496. tmp = (cfg->tx_fifo_min_level / PORT_BMI_FIFO_UNITS) <<
  497. BMI_TX_FIFO_MIN_FILL_SHIFT;
  498. tmp |= ((cfg->tx_fifo_deq_pipeline_depth - 1) &
  499. BMI_FIFO_PIPELINE_DEPTH_MASK) << BMI_FIFO_PIPELINE_DEPTH_SHIFT;
  500. tmp |= (cfg->tx_fifo_low_comf_level / PORT_BMI_FIFO_UNITS) - 1;
  501. iowrite32be(tmp, &regs->fmbm_tfp);
  502. /* Frame end data */
  503. tmp = (cfg->cheksum_last_bytes_ignore & BMI_FRAME_END_CS_IGNORE_MASK) <<
  504. BMI_FRAME_END_CS_IGNORE_SHIFT;
  505. iowrite32be(tmp, &regs->fmbm_tfed);
  506. /* Internal context parameters */
  507. tmp = ((cfg->int_context.ext_buf_offset / PORT_IC_OFFSET_UNITS) &
  508. BMI_IC_TO_EXT_MASK) << BMI_IC_TO_EXT_SHIFT;
  509. tmp |= ((cfg->int_context.int_context_offset / PORT_IC_OFFSET_UNITS) &
  510. BMI_IC_FROM_INT_MASK) << BMI_IC_FROM_INT_SHIFT;
  511. tmp |= (cfg->int_context.size / PORT_IC_OFFSET_UNITS) &
  512. BMI_IC_SIZE_MASK;
  513. iowrite32be(tmp, &regs->fmbm_ticp);
  514. /* Frame attributes */
  515. tmp = BMI_CMD_TX_MR_DEF;
  516. tmp |= BMI_CMD_ATTR_ORDER;
  517. tmp |= (u32)cfg->color << BMI_CMD_ATTR_COLOR_SHIFT;
  518. iowrite32be(tmp, &regs->fmbm_tfca);
  519. /* Dequeue NIA + enqueue NIA */
  520. iowrite32be(NIA_ENG_QMI_DEQ, &regs->fmbm_tfdne);
  521. iowrite32be(NIA_ENG_QMI_ENQ | NIA_ORDER_RESTOR, &regs->fmbm_tfene);
  522. if (cfg->fmbm_tfne_has_features)
  523. iowrite32be(!cfg->dflt_fqid ?
  524. BMI_EBD_EN | NIA_BMI_AC_FETCH_ALL_FRAME :
  525. NIA_BMI_AC_FETCH_ALL_FRAME, &regs->fmbm_tfne);
  526. if (!cfg->dflt_fqid && cfg->dont_release_buf) {
  527. iowrite32be(DFLT_FQ_ID, &regs->fmbm_tcfqid);
  528. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE,
  529. &regs->fmbm_tfene);
  530. if (cfg->fmbm_tfne_has_features)
  531. iowrite32be(ioread32be(&regs->fmbm_tfne) & ~BMI_EBD_EN,
  532. &regs->fmbm_tfne);
  533. }
  534. /* Confirmation/error queues */
  535. if (cfg->dflt_fqid || !cfg->dont_release_buf)
  536. iowrite32be(cfg->dflt_fqid & DFLT_FQ_ID, &regs->fmbm_tcfqid);
  537. iowrite32be((cfg->err_fqid & DFLT_FQ_ID), &regs->fmbm_tefqid);
  538. return 0;
  539. }
  540. static int init_qmi(struct fman_port *port)
  541. {
  542. struct fman_port_qmi_regs __iomem *regs = port->qmi_regs;
  543. struct fman_port_cfg *cfg = port->cfg;
  544. u32 tmp;
  545. /* Rx port configuration */
  546. if (port->port_type == FMAN_PORT_TYPE_RX) {
  547. /* Enqueue NIA */
  548. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_RELEASE, &regs->fmqm_pnen);
  549. return 0;
  550. }
  551. /* Continue with Tx port configuration */
  552. if (port->port_type == FMAN_PORT_TYPE_TX) {
  553. /* Enqueue NIA */
  554. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE,
  555. &regs->fmqm_pnen);
  556. /* Dequeue NIA */
  557. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX, &regs->fmqm_pndn);
  558. }
  559. /* Dequeue Configuration register */
  560. tmp = 0;
  561. if (cfg->deq_high_priority)
  562. tmp |= QMI_DEQ_CFG_PRI;
  563. switch (cfg->deq_type) {
  564. case FMAN_PORT_DEQ_BY_PRI:
  565. tmp |= QMI_DEQ_CFG_TYPE1;
  566. break;
  567. case FMAN_PORT_DEQ_ACTIVE_FQ:
  568. tmp |= QMI_DEQ_CFG_TYPE2;
  569. break;
  570. case FMAN_PORT_DEQ_ACTIVE_FQ_NO_ICS:
  571. tmp |= QMI_DEQ_CFG_TYPE3;
  572. break;
  573. default:
  574. return -EINVAL;
  575. }
  576. switch (cfg->deq_prefetch_option) {
  577. case FMAN_PORT_DEQ_NO_PREFETCH:
  578. break;
  579. case FMAN_PORT_DEQ_PART_PREFETCH:
  580. tmp |= QMI_DEQ_CFG_PREFETCH_PARTIAL;
  581. break;
  582. case FMAN_PORT_DEQ_FULL_PREFETCH:
  583. tmp |= QMI_DEQ_CFG_PREFETCH_FULL;
  584. break;
  585. default:
  586. return -EINVAL;
  587. }
  588. tmp |= (cfg->deq_sp & QMI_DEQ_CFG_SP_MASK) << QMI_DEQ_CFG_SP_SHIFT;
  589. tmp |= cfg->deq_byte_cnt;
  590. iowrite32be(tmp, &regs->fmqm_pndc);
  591. return 0;
  592. }
  593. static void stop_port_hwp(struct fman_port *port)
  594. {
  595. struct fman_port_hwp_regs __iomem *regs = port->hwp_regs;
  596. int cnt = 100;
  597. iowrite32be(HWP_HXS_PCAC_PSTOP, &regs->fmpr_pcac);
  598. while (cnt-- > 0 &&
  599. (ioread32be(&regs->fmpr_pcac) & HWP_HXS_PCAC_PSTAT))
  600. udelay(10);
  601. if (!cnt)
  602. pr_err("Timeout stopping HW Parser\n");
  603. }
  604. static void start_port_hwp(struct fman_port *port)
  605. {
  606. struct fman_port_hwp_regs __iomem *regs = port->hwp_regs;
  607. int cnt = 100;
  608. iowrite32be(0, &regs->fmpr_pcac);
  609. while (cnt-- > 0 &&
  610. !(ioread32be(&regs->fmpr_pcac) & HWP_HXS_PCAC_PSTAT))
  611. udelay(10);
  612. if (!cnt)
  613. pr_err("Timeout starting HW Parser\n");
  614. }
  615. static void init_hwp(struct fman_port *port)
  616. {
  617. struct fman_port_hwp_regs __iomem *regs = port->hwp_regs;
  618. int i;
  619. stop_port_hwp(port);
  620. for (i = 0; i < HWP_HXS_COUNT; i++) {
  621. /* enable HXS error reporting into FD[STATUS] PHE */
  622. iowrite32be(0x00000000, &regs->pmda[i].ssa);
  623. iowrite32be(0xffffffff, &regs->pmda[i].lcv);
  624. }
  625. start_port_hwp(port);
  626. }
  627. static int init(struct fman_port *port)
  628. {
  629. int err;
  630. /* Init BMI registers */
  631. switch (port->port_type) {
  632. case FMAN_PORT_TYPE_RX:
  633. err = init_bmi_rx(port);
  634. if (!err)
  635. init_hwp(port);
  636. break;
  637. case FMAN_PORT_TYPE_TX:
  638. err = init_bmi_tx(port);
  639. break;
  640. default:
  641. return -EINVAL;
  642. }
  643. if (err)
  644. return err;
  645. /* Init QMI registers */
  646. err = init_qmi(port);
  647. if (err)
  648. return err;
  649. return 0;
  650. }
  651. static int set_bpools(const struct fman_port *port,
  652. const struct fman_port_bpools *bp)
  653. {
  654. u32 __iomem *bp_reg, *bp_depl_reg;
  655. u32 tmp;
  656. u8 i, max_bp_num;
  657. bool grp_depl_used = false, rx_port;
  658. switch (port->port_type) {
  659. case FMAN_PORT_TYPE_RX:
  660. max_bp_num = port->ext_pools_num;
  661. rx_port = true;
  662. bp_reg = port->bmi_regs->rx.fmbm_ebmpi;
  663. bp_depl_reg = &port->bmi_regs->rx.fmbm_mpd;
  664. break;
  665. default:
  666. return -EINVAL;
  667. }
  668. if (rx_port) {
  669. /* Check buffers are provided in ascending order */
  670. for (i = 0; (i < (bp->count - 1) &&
  671. (i < FMAN_PORT_MAX_EXT_POOLS_NUM - 1)); i++) {
  672. if (bp->bpool[i].size > bp->bpool[i + 1].size)
  673. return -EINVAL;
  674. }
  675. }
  676. /* Set up external buffers pools */
  677. for (i = 0; i < bp->count; i++) {
  678. tmp = BMI_EXT_BUF_POOL_VALID;
  679. tmp |= ((u32)bp->bpool[i].bpid <<
  680. BMI_EXT_BUF_POOL_ID_SHIFT) & BMI_EXT_BUF_POOL_ID_MASK;
  681. if (rx_port) {
  682. if (bp->counters_enable)
  683. tmp |= BMI_EXT_BUF_POOL_EN_COUNTER;
  684. if (bp->bpool[i].is_backup)
  685. tmp |= BMI_EXT_BUF_POOL_BACKUP;
  686. tmp |= (u32)bp->bpool[i].size;
  687. }
  688. iowrite32be(tmp, &bp_reg[i]);
  689. }
  690. /* Clear unused pools */
  691. for (i = bp->count; i < max_bp_num; i++)
  692. iowrite32be(0, &bp_reg[i]);
  693. /* Pools depletion */
  694. tmp = 0;
  695. for (i = 0; i < FMAN_PORT_MAX_EXT_POOLS_NUM; i++) {
  696. if (bp->bpool[i].grp_bp_depleted) {
  697. grp_depl_used = true;
  698. tmp |= 0x80000000 >> i;
  699. }
  700. if (bp->bpool[i].single_bp_depleted)
  701. tmp |= 0x80 >> i;
  702. }
  703. if (grp_depl_used)
  704. tmp |= ((u32)bp->grp_bp_depleted_num - 1) <<
  705. BMI_POOL_DEP_NUM_OF_POOLS_SHIFT;
  706. iowrite32be(tmp, bp_depl_reg);
  707. return 0;
  708. }
  709. static bool is_init_done(struct fman_port_cfg *cfg)
  710. {
  711. /* Checks if FMan port driver parameters were initialized */
  712. if (!cfg)
  713. return true;
  714. return false;
  715. }
  716. static int verify_size_of_fifo(struct fman_port *port)
  717. {
  718. u32 min_fifo_size_required = 0, opt_fifo_size_for_b2b = 0;
  719. /* TX Ports */
  720. if (port->port_type == FMAN_PORT_TYPE_TX) {
  721. min_fifo_size_required = (u32)
  722. (roundup(port->max_frame_length,
  723. FMAN_BMI_FIFO_UNITS) + (3 * FMAN_BMI_FIFO_UNITS));
  724. min_fifo_size_required +=
  725. port->cfg->tx_fifo_deq_pipeline_depth *
  726. FMAN_BMI_FIFO_UNITS;
  727. opt_fifo_size_for_b2b = min_fifo_size_required;
  728. /* Add some margin for back-to-back capability to improve
  729. * performance, allows the hardware to pipeline new frame dma
  730. * while the previous frame not yet transmitted.
  731. */
  732. if (port->port_speed == 10000)
  733. opt_fifo_size_for_b2b += 3 * FMAN_BMI_FIFO_UNITS;
  734. else
  735. opt_fifo_size_for_b2b += 2 * FMAN_BMI_FIFO_UNITS;
  736. }
  737. /* RX Ports */
  738. else if (port->port_type == FMAN_PORT_TYPE_RX) {
  739. if (port->rev_info.major >= 6)
  740. min_fifo_size_required = (u32)
  741. (roundup(port->max_frame_length,
  742. FMAN_BMI_FIFO_UNITS) +
  743. (5 * FMAN_BMI_FIFO_UNITS));
  744. /* 4 according to spec + 1 for FOF>0 */
  745. else
  746. min_fifo_size_required = (u32)
  747. (roundup(min(port->max_frame_length,
  748. port->rx_pools_params.largest_buf_size),
  749. FMAN_BMI_FIFO_UNITS) +
  750. (7 * FMAN_BMI_FIFO_UNITS));
  751. opt_fifo_size_for_b2b = min_fifo_size_required;
  752. /* Add some margin for back-to-back capability to improve
  753. * performance,allows the hardware to pipeline new frame dma
  754. * while the previous frame not yet transmitted.
  755. */
  756. if (port->port_speed == 10000)
  757. opt_fifo_size_for_b2b += 8 * FMAN_BMI_FIFO_UNITS;
  758. else
  759. opt_fifo_size_for_b2b += 3 * FMAN_BMI_FIFO_UNITS;
  760. }
  761. WARN_ON(min_fifo_size_required <= 0);
  762. WARN_ON(opt_fifo_size_for_b2b < min_fifo_size_required);
  763. /* Verify the size */
  764. if (port->fifo_bufs.num < min_fifo_size_required)
  765. dev_dbg(port->dev, "%s: FIFO size should be enlarged to %d bytes\n",
  766. __func__, min_fifo_size_required);
  767. else if (port->fifo_bufs.num < opt_fifo_size_for_b2b)
  768. dev_dbg(port->dev, "%s: For b2b processing,FIFO may be enlarged to %d bytes\n",
  769. __func__, opt_fifo_size_for_b2b);
  770. return 0;
  771. }
  772. static int set_ext_buffer_pools(struct fman_port *port)
  773. {
  774. struct fman_ext_pools *ext_buf_pools = &port->cfg->ext_buf_pools;
  775. struct fman_buf_pool_depletion *buf_pool_depletion =
  776. &port->cfg->buf_pool_depletion;
  777. u8 ordered_array[FMAN_PORT_MAX_EXT_POOLS_NUM];
  778. u16 sizes_array[BM_MAX_NUM_OF_POOLS];
  779. int i = 0, j = 0, err;
  780. struct fman_port_bpools bpools;
  781. memset(&ordered_array, 0, sizeof(u8) * FMAN_PORT_MAX_EXT_POOLS_NUM);
  782. memset(&sizes_array, 0, sizeof(u16) * BM_MAX_NUM_OF_POOLS);
  783. memcpy(&port->ext_buf_pools, ext_buf_pools,
  784. sizeof(struct fman_ext_pools));
  785. fman_sp_set_buf_pools_in_asc_order_of_buf_sizes(ext_buf_pools,
  786. ordered_array,
  787. sizes_array);
  788. memset(&bpools, 0, sizeof(struct fman_port_bpools));
  789. bpools.count = ext_buf_pools->num_of_pools_used;
  790. bpools.counters_enable = true;
  791. for (i = 0; i < ext_buf_pools->num_of_pools_used; i++) {
  792. bpools.bpool[i].bpid = ordered_array[i];
  793. bpools.bpool[i].size = sizes_array[ordered_array[i]];
  794. }
  795. /* save pools parameters for later use */
  796. port->rx_pools_params.num_of_pools = ext_buf_pools->num_of_pools_used;
  797. port->rx_pools_params.largest_buf_size =
  798. sizes_array[ordered_array[ext_buf_pools->num_of_pools_used - 1]];
  799. port->rx_pools_params.second_largest_buf_size =
  800. sizes_array[ordered_array[ext_buf_pools->num_of_pools_used - 2]];
  801. /* FMBM_RMPD reg. - pool depletion */
  802. if (buf_pool_depletion->pools_grp_mode_enable) {
  803. bpools.grp_bp_depleted_num = buf_pool_depletion->num_of_pools;
  804. for (i = 0; i < port->bm_max_num_of_pools; i++) {
  805. if (buf_pool_depletion->pools_to_consider[i]) {
  806. for (j = 0; j < ext_buf_pools->
  807. num_of_pools_used; j++) {
  808. if (i == ordered_array[j]) {
  809. bpools.bpool[j].
  810. grp_bp_depleted = true;
  811. break;
  812. }
  813. }
  814. }
  815. }
  816. }
  817. if (buf_pool_depletion->single_pool_mode_enable) {
  818. for (i = 0; i < port->bm_max_num_of_pools; i++) {
  819. if (buf_pool_depletion->
  820. pools_to_consider_for_single_mode[i]) {
  821. for (j = 0; j < ext_buf_pools->
  822. num_of_pools_used; j++) {
  823. if (i == ordered_array[j]) {
  824. bpools.bpool[j].
  825. single_bp_depleted = true;
  826. break;
  827. }
  828. }
  829. }
  830. }
  831. }
  832. err = set_bpools(port, &bpools);
  833. if (err != 0) {
  834. dev_err(port->dev, "%s: set_bpools() failed\n", __func__);
  835. return -EINVAL;
  836. }
  837. return 0;
  838. }
  839. static int init_low_level_driver(struct fman_port *port)
  840. {
  841. struct fman_port_cfg *cfg = port->cfg;
  842. u32 tmp_val;
  843. switch (port->port_type) {
  844. case FMAN_PORT_TYPE_RX:
  845. cfg->err_mask = (RX_ERRS_TO_ENQ & ~cfg->discard_mask);
  846. break;
  847. default:
  848. break;
  849. }
  850. tmp_val = (u32)((port->internal_buf_offset % OFFSET_UNITS) ?
  851. (port->internal_buf_offset / OFFSET_UNITS + 1) :
  852. (port->internal_buf_offset / OFFSET_UNITS));
  853. port->internal_buf_offset = (u8)(tmp_val * OFFSET_UNITS);
  854. port->cfg->int_buf_start_margin = port->internal_buf_offset;
  855. if (init(port) != 0) {
  856. dev_err(port->dev, "%s: fman port initialization failed\n",
  857. __func__);
  858. return -ENODEV;
  859. }
  860. /* The code bellow is a trick so the FM will not release the buffer
  861. * to BM nor will try to enqueue the frame to QM
  862. */
  863. if (port->port_type == FMAN_PORT_TYPE_TX) {
  864. if (!cfg->dflt_fqid && cfg->dont_release_buf) {
  865. /* override fmbm_tcfqid 0 with a false non-0 value.
  866. * This will force FM to act according to tfene.
  867. * Otherwise, if fmbm_tcfqid is 0 the FM will release
  868. * buffers to BM regardless of fmbm_tfene
  869. */
  870. iowrite32be(0xFFFFFF, &port->bmi_regs->tx.fmbm_tcfqid);
  871. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_TX_RELEASE,
  872. &port->bmi_regs->tx.fmbm_tfene);
  873. }
  874. }
  875. return 0;
  876. }
  877. static int fill_soc_specific_params(struct fman_port *port)
  878. {
  879. u32 bmi_max_fifo_size;
  880. bmi_max_fifo_size = fman_get_bmi_max_fifo_size(port->fm);
  881. port->max_port_fifo_size = MAX_PORT_FIFO_SIZE(bmi_max_fifo_size);
  882. port->bm_max_num_of_pools = 64;
  883. /* P4080 - Major 2
  884. * P2041/P3041/P5020/P5040 - Major 3
  885. * Tx/Bx - Major 6
  886. */
  887. switch (port->rev_info.major) {
  888. case 2:
  889. case 3:
  890. port->max_num_of_ext_pools = 4;
  891. port->max_num_of_sub_portals = 12;
  892. break;
  893. case 6:
  894. port->max_num_of_ext_pools = 8;
  895. port->max_num_of_sub_portals = 16;
  896. break;
  897. default:
  898. dev_err(port->dev, "%s: Unsupported FMan version\n", __func__);
  899. return -EINVAL;
  900. }
  901. return 0;
  902. }
  903. static int get_dflt_fifo_deq_pipeline_depth(u8 major, enum fman_port_type type,
  904. u16 speed)
  905. {
  906. switch (type) {
  907. case FMAN_PORT_TYPE_RX:
  908. case FMAN_PORT_TYPE_TX:
  909. switch (speed) {
  910. case 10000:
  911. return 4;
  912. case 1000:
  913. if (major >= 6)
  914. return 2;
  915. else
  916. return 1;
  917. default:
  918. return 0;
  919. }
  920. default:
  921. return 0;
  922. }
  923. }
  924. static int get_dflt_num_of_tasks(u8 major, enum fman_port_type type,
  925. u16 speed)
  926. {
  927. switch (type) {
  928. case FMAN_PORT_TYPE_RX:
  929. case FMAN_PORT_TYPE_TX:
  930. switch (speed) {
  931. case 10000:
  932. return 16;
  933. case 1000:
  934. if (major >= 6)
  935. return 4;
  936. else
  937. return 3;
  938. default:
  939. return 0;
  940. }
  941. default:
  942. return 0;
  943. }
  944. }
  945. static int get_dflt_extra_num_of_tasks(u8 major, enum fman_port_type type,
  946. u16 speed)
  947. {
  948. switch (type) {
  949. case FMAN_PORT_TYPE_RX:
  950. /* FMan V3 */
  951. if (major >= 6)
  952. return 0;
  953. /* FMan V2 */
  954. if (speed == 10000)
  955. return 8;
  956. else
  957. return 2;
  958. case FMAN_PORT_TYPE_TX:
  959. default:
  960. return 0;
  961. }
  962. }
  963. static int get_dflt_num_of_open_dmas(u8 major, enum fman_port_type type,
  964. u16 speed)
  965. {
  966. int val;
  967. if (major >= 6) {
  968. switch (type) {
  969. case FMAN_PORT_TYPE_TX:
  970. if (speed == 10000)
  971. val = 12;
  972. else
  973. val = 3;
  974. break;
  975. case FMAN_PORT_TYPE_RX:
  976. if (speed == 10000)
  977. val = 8;
  978. else
  979. val = 2;
  980. break;
  981. default:
  982. return 0;
  983. }
  984. } else {
  985. switch (type) {
  986. case FMAN_PORT_TYPE_TX:
  987. case FMAN_PORT_TYPE_RX:
  988. if (speed == 10000)
  989. val = 8;
  990. else
  991. val = 1;
  992. break;
  993. default:
  994. val = 0;
  995. }
  996. }
  997. return val;
  998. }
  999. static int get_dflt_extra_num_of_open_dmas(u8 major, enum fman_port_type type,
  1000. u16 speed)
  1001. {
  1002. /* FMan V3 */
  1003. if (major >= 6)
  1004. return 0;
  1005. /* FMan V2 */
  1006. switch (type) {
  1007. case FMAN_PORT_TYPE_RX:
  1008. case FMAN_PORT_TYPE_TX:
  1009. if (speed == 10000)
  1010. return 8;
  1011. else
  1012. return 1;
  1013. default:
  1014. return 0;
  1015. }
  1016. }
  1017. static int get_dflt_num_of_fifo_bufs(u8 major, enum fman_port_type type,
  1018. u16 speed)
  1019. {
  1020. int val;
  1021. if (major >= 6) {
  1022. switch (type) {
  1023. case FMAN_PORT_TYPE_TX:
  1024. if (speed == 10000)
  1025. val = 64;
  1026. else
  1027. val = 50;
  1028. break;
  1029. case FMAN_PORT_TYPE_RX:
  1030. if (speed == 10000)
  1031. val = 96;
  1032. else
  1033. val = 50;
  1034. break;
  1035. default:
  1036. val = 0;
  1037. }
  1038. } else {
  1039. switch (type) {
  1040. case FMAN_PORT_TYPE_TX:
  1041. if (speed == 10000)
  1042. val = 48;
  1043. else
  1044. val = 44;
  1045. break;
  1046. case FMAN_PORT_TYPE_RX:
  1047. if (speed == 10000)
  1048. val = 48;
  1049. else
  1050. val = 45;
  1051. break;
  1052. default:
  1053. val = 0;
  1054. }
  1055. }
  1056. return val;
  1057. }
  1058. static void set_dflt_cfg(struct fman_port *port,
  1059. struct fman_port_params *port_params)
  1060. {
  1061. struct fman_port_cfg *cfg = port->cfg;
  1062. cfg->dma_swap_data = FMAN_PORT_DMA_NO_SWAP;
  1063. cfg->color = FMAN_PORT_COLOR_GREEN;
  1064. cfg->rx_cut_end_bytes = DFLT_PORT_CUT_BYTES_FROM_END;
  1065. cfg->rx_pri_elevation = BMI_PRIORITY_ELEVATION_LEVEL;
  1066. cfg->rx_fifo_thr = BMI_FIFO_THRESHOLD;
  1067. cfg->tx_fifo_low_comf_level = (5 * 1024);
  1068. cfg->deq_type = FMAN_PORT_DEQ_BY_PRI;
  1069. cfg->deq_prefetch_option = FMAN_PORT_DEQ_FULL_PREFETCH;
  1070. cfg->tx_fifo_deq_pipeline_depth =
  1071. BMI_DEQUEUE_PIPELINE_DEPTH(port->port_type, port->port_speed);
  1072. cfg->deq_byte_cnt = QMI_BYTE_COUNT_LEVEL_CONTROL(port->port_type);
  1073. cfg->rx_pri_elevation =
  1074. DFLT_PORT_RX_FIFO_PRI_ELEVATION_LEV(port->max_port_fifo_size);
  1075. port->cfg->rx_fifo_thr =
  1076. DFLT_PORT_RX_FIFO_THRESHOLD(port->rev_info.major,
  1077. port->max_port_fifo_size);
  1078. if ((port->rev_info.major == 6) &&
  1079. ((port->rev_info.minor == 0) || (port->rev_info.minor == 3)))
  1080. cfg->errata_A006320 = true;
  1081. /* Excessive Threshold register - exists for pre-FMv3 chips only */
  1082. if (port->rev_info.major < 6)
  1083. cfg->excessive_threshold_register = true;
  1084. else
  1085. cfg->fmbm_tfne_has_features = true;
  1086. cfg->buffer_prefix_content.data_align =
  1087. DFLT_PORT_BUFFER_PREFIX_CONTEXT_DATA_ALIGN;
  1088. }
  1089. static void set_rx_dflt_cfg(struct fman_port *port,
  1090. struct fman_port_params *port_params)
  1091. {
  1092. port->cfg->discard_mask = DFLT_PORT_ERRORS_TO_DISCARD;
  1093. memcpy(&port->cfg->ext_buf_pools,
  1094. &port_params->specific_params.rx_params.ext_buf_pools,
  1095. sizeof(struct fman_ext_pools));
  1096. port->cfg->err_fqid =
  1097. port_params->specific_params.rx_params.err_fqid;
  1098. port->cfg->dflt_fqid =
  1099. port_params->specific_params.rx_params.dflt_fqid;
  1100. port->cfg->pcd_base_fqid =
  1101. port_params->specific_params.rx_params.pcd_base_fqid;
  1102. port->cfg->pcd_fqs_count =
  1103. port_params->specific_params.rx_params.pcd_fqs_count;
  1104. }
  1105. static void set_tx_dflt_cfg(struct fman_port *port,
  1106. struct fman_port_params *port_params,
  1107. struct fman_port_dts_params *dts_params)
  1108. {
  1109. port->cfg->tx_fifo_deq_pipeline_depth =
  1110. get_dflt_fifo_deq_pipeline_depth(port->rev_info.major,
  1111. port->port_type,
  1112. port->port_speed);
  1113. port->cfg->err_fqid =
  1114. port_params->specific_params.non_rx_params.err_fqid;
  1115. port->cfg->deq_sp =
  1116. (u8)(dts_params->qman_channel_id & QMI_DEQ_CFG_SUBPORTAL_MASK);
  1117. port->cfg->dflt_fqid =
  1118. port_params->specific_params.non_rx_params.dflt_fqid;
  1119. port->cfg->deq_high_priority = true;
  1120. }
  1121. /**
  1122. * fman_port_config
  1123. * @port: Pointer to the port structure
  1124. * @params: Pointer to data structure of parameters
  1125. *
  1126. * Creates a descriptor for the FM PORT module.
  1127. * The routine returns a pointer to the FM PORT object.
  1128. * This descriptor must be passed as first parameter to all other FM PORT
  1129. * function calls.
  1130. * No actual initialization or configuration of FM hardware is done by this
  1131. * routine.
  1132. *
  1133. * Return: 0 on success; Error code otherwise.
  1134. */
  1135. int fman_port_config(struct fman_port *port, struct fman_port_params *params)
  1136. {
  1137. void __iomem *base_addr = port->dts_params.base_addr;
  1138. int err;
  1139. /* Allocate the FM driver's parameters structure */
  1140. port->cfg = kzalloc(sizeof(*port->cfg), GFP_KERNEL);
  1141. if (!port->cfg)
  1142. return -EINVAL;
  1143. /* Initialize FM port parameters which will be kept by the driver */
  1144. port->port_type = port->dts_params.type;
  1145. port->port_speed = port->dts_params.speed;
  1146. port->port_id = port->dts_params.id;
  1147. port->fm = port->dts_params.fman;
  1148. port->ext_pools_num = (u8)8;
  1149. /* get FM revision */
  1150. fman_get_revision(port->fm, &port->rev_info);
  1151. err = fill_soc_specific_params(port);
  1152. if (err)
  1153. goto err_port_cfg;
  1154. switch (port->port_type) {
  1155. case FMAN_PORT_TYPE_RX:
  1156. set_rx_dflt_cfg(port, params);
  1157. /* fall through */
  1158. case FMAN_PORT_TYPE_TX:
  1159. set_tx_dflt_cfg(port, params, &port->dts_params);
  1160. /* fall through */
  1161. default:
  1162. set_dflt_cfg(port, params);
  1163. }
  1164. /* Continue with other parameters */
  1165. /* set memory map pointers */
  1166. port->bmi_regs = base_addr + BMI_PORT_REGS_OFFSET;
  1167. port->qmi_regs = base_addr + QMI_PORT_REGS_OFFSET;
  1168. port->hwp_regs = base_addr + HWP_PORT_REGS_OFFSET;
  1169. port->max_frame_length = DFLT_PORT_MAX_FRAME_LENGTH;
  1170. /* resource distribution. */
  1171. port->fifo_bufs.num =
  1172. get_dflt_num_of_fifo_bufs(port->rev_info.major, port->port_type,
  1173. port->port_speed) * FMAN_BMI_FIFO_UNITS;
  1174. port->fifo_bufs.extra =
  1175. DFLT_PORT_EXTRA_NUM_OF_FIFO_BUFS * FMAN_BMI_FIFO_UNITS;
  1176. port->open_dmas.num =
  1177. get_dflt_num_of_open_dmas(port->rev_info.major,
  1178. port->port_type, port->port_speed);
  1179. port->open_dmas.extra =
  1180. get_dflt_extra_num_of_open_dmas(port->rev_info.major,
  1181. port->port_type, port->port_speed);
  1182. port->tasks.num =
  1183. get_dflt_num_of_tasks(port->rev_info.major,
  1184. port->port_type, port->port_speed);
  1185. port->tasks.extra =
  1186. get_dflt_extra_num_of_tasks(port->rev_info.major,
  1187. port->port_type, port->port_speed);
  1188. /* FM_HEAVY_TRAFFIC_SEQUENCER_HANG_ERRATA_FMAN_A006981 errata
  1189. * workaround
  1190. */
  1191. if ((port->rev_info.major == 6) && (port->rev_info.minor == 0) &&
  1192. (((port->port_type == FMAN_PORT_TYPE_TX) &&
  1193. (port->port_speed == 1000)))) {
  1194. port->open_dmas.num = 16;
  1195. port->open_dmas.extra = 0;
  1196. }
  1197. if (port->rev_info.major >= 6 &&
  1198. port->port_type == FMAN_PORT_TYPE_TX &&
  1199. port->port_speed == 1000) {
  1200. /* FM_WRONG_RESET_VALUES_ERRATA_FMAN_A005127 Errata
  1201. * workaround
  1202. */
  1203. if (port->rev_info.major >= 6) {
  1204. u32 reg;
  1205. reg = 0x00001013;
  1206. iowrite32be(reg, &port->bmi_regs->tx.fmbm_tfp);
  1207. }
  1208. }
  1209. return 0;
  1210. err_port_cfg:
  1211. kfree(port->cfg);
  1212. return -EINVAL;
  1213. }
  1214. EXPORT_SYMBOL(fman_port_config);
  1215. /**
  1216. * fman_port_use_kg_hash
  1217. * port: A pointer to a FM Port module.
  1218. * Sets the HW KeyGen or the BMI as HW Parser next engine, enabling
  1219. * or bypassing the KeyGen hashing of Rx traffic
  1220. */
  1221. void fman_port_use_kg_hash(struct fman_port *port, bool enable)
  1222. {
  1223. if (enable)
  1224. /* After the Parser frames go to KeyGen */
  1225. iowrite32be(NIA_ENG_HWK, &port->bmi_regs->rx.fmbm_rfpne);
  1226. else
  1227. /* After the Parser frames go to BMI */
  1228. iowrite32be(NIA_ENG_BMI | NIA_BMI_AC_ENQ_FRAME,
  1229. &port->bmi_regs->rx.fmbm_rfpne);
  1230. }
  1231. EXPORT_SYMBOL(fman_port_use_kg_hash);
  1232. /**
  1233. * fman_port_init
  1234. * port: A pointer to a FM Port module.
  1235. * Initializes the FM PORT module by defining the software structure and
  1236. * configuring the hardware registers.
  1237. *
  1238. * Return: 0 on success; Error code otherwise.
  1239. */
  1240. int fman_port_init(struct fman_port *port)
  1241. {
  1242. struct fman_port_init_params params;
  1243. struct fman_keygen *keygen;
  1244. struct fman_port_cfg *cfg;
  1245. int err;
  1246. if (is_init_done(port->cfg))
  1247. return -EINVAL;
  1248. err = fman_sp_build_buffer_struct(&port->cfg->int_context,
  1249. &port->cfg->buffer_prefix_content,
  1250. &port->cfg->buf_margins,
  1251. &port->buffer_offsets,
  1252. &port->internal_buf_offset);
  1253. if (err)
  1254. return err;
  1255. cfg = port->cfg;
  1256. if (port->port_type == FMAN_PORT_TYPE_RX) {
  1257. /* Call the external Buffer routine which also checks fifo
  1258. * size and updates it if necessary
  1259. */
  1260. /* define external buffer pools and pool depletion */
  1261. err = set_ext_buffer_pools(port);
  1262. if (err)
  1263. return err;
  1264. /* check if the largest external buffer pool is large enough */
  1265. if (cfg->buf_margins.start_margins + MIN_EXT_BUF_SIZE +
  1266. cfg->buf_margins.end_margins >
  1267. port->rx_pools_params.largest_buf_size) {
  1268. 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",
  1269. __func__, cfg->buf_margins.start_margins,
  1270. cfg->buf_margins.end_margins,
  1271. port->rx_pools_params.largest_buf_size);
  1272. return -EINVAL;
  1273. }
  1274. }
  1275. /* Call FM module routine for communicating parameters */
  1276. memset(&params, 0, sizeof(params));
  1277. params.port_id = port->port_id;
  1278. params.port_type = port->port_type;
  1279. params.port_speed = port->port_speed;
  1280. params.num_of_tasks = (u8)port->tasks.num;
  1281. params.num_of_extra_tasks = (u8)port->tasks.extra;
  1282. params.num_of_open_dmas = (u8)port->open_dmas.num;
  1283. params.num_of_extra_open_dmas = (u8)port->open_dmas.extra;
  1284. if (port->fifo_bufs.num) {
  1285. err = verify_size_of_fifo(port);
  1286. if (err)
  1287. return err;
  1288. }
  1289. params.size_of_fifo = port->fifo_bufs.num;
  1290. params.extra_size_of_fifo = port->fifo_bufs.extra;
  1291. params.deq_pipeline_depth = port->cfg->tx_fifo_deq_pipeline_depth;
  1292. params.max_frame_length = port->max_frame_length;
  1293. err = fman_set_port_params(port->fm, &params);
  1294. if (err)
  1295. return err;
  1296. err = init_low_level_driver(port);
  1297. if (err)
  1298. return err;
  1299. if (port->cfg->pcd_fqs_count) {
  1300. keygen = port->dts_params.fman->keygen;
  1301. err = keygen_port_hashing_init(keygen, port->port_id,
  1302. port->cfg->pcd_base_fqid,
  1303. port->cfg->pcd_fqs_count);
  1304. if (err)
  1305. return err;
  1306. fman_port_use_kg_hash(port, true);
  1307. }
  1308. kfree(port->cfg);
  1309. port->cfg = NULL;
  1310. return 0;
  1311. }
  1312. EXPORT_SYMBOL(fman_port_init);
  1313. /**
  1314. * fman_port_cfg_buf_prefix_content
  1315. * @port A pointer to a FM Port module.
  1316. * @buffer_prefix_content A structure of parameters describing
  1317. * the structure of the buffer.
  1318. * Out parameter:
  1319. * Start margin - offset of data from
  1320. * start of external buffer.
  1321. * Defines the structure, size and content of the application buffer.
  1322. * The prefix, in Tx ports, if 'pass_prs_result', the application should set
  1323. * a value to their offsets in the prefix of the FM will save the first
  1324. * 'priv_data_size', than, depending on 'pass_prs_result' and
  1325. * 'pass_time_stamp', copy parse result and timeStamp, and the packet itself
  1326. * (in this order), to the application buffer, and to offset.
  1327. * Calling this routine changes the buffer margins definitions in the internal
  1328. * driver data base from its default configuration:
  1329. * Data size: [DEFAULT_PORT_BUFFER_PREFIX_CONTENT_PRIV_DATA_SIZE]
  1330. * Pass Parser result: [DEFAULT_PORT_BUFFER_PREFIX_CONTENT_PASS_PRS_RESULT].
  1331. * Pass timestamp: [DEFAULT_PORT_BUFFER_PREFIX_CONTENT_PASS_TIME_STAMP].
  1332. * May be used for all ports
  1333. *
  1334. * Allowed only following fman_port_config() and before fman_port_init().
  1335. *
  1336. * Return: 0 on success; Error code otherwise.
  1337. */
  1338. int fman_port_cfg_buf_prefix_content(struct fman_port *port,
  1339. struct fman_buffer_prefix_content *
  1340. buffer_prefix_content)
  1341. {
  1342. if (is_init_done(port->cfg))
  1343. return -EINVAL;
  1344. memcpy(&port->cfg->buffer_prefix_content,
  1345. buffer_prefix_content,
  1346. sizeof(struct fman_buffer_prefix_content));
  1347. /* if data_align was not initialized by user,
  1348. * we return to driver's default
  1349. */
  1350. if (!port->cfg->buffer_prefix_content.data_align)
  1351. port->cfg->buffer_prefix_content.data_align =
  1352. DFLT_PORT_BUFFER_PREFIX_CONTEXT_DATA_ALIGN;
  1353. return 0;
  1354. }
  1355. EXPORT_SYMBOL(fman_port_cfg_buf_prefix_content);
  1356. /**
  1357. * fman_port_disable
  1358. * port: A pointer to a FM Port module.
  1359. *
  1360. * Gracefully disable an FM port. The port will not start new tasks after all
  1361. * tasks associated with the port are terminated.
  1362. *
  1363. * This is a blocking routine, it returns after port is gracefully stopped,
  1364. * i.e. the port will not except new frames, but it will finish all frames
  1365. * or tasks which were already began.
  1366. * Allowed only following fman_port_init().
  1367. *
  1368. * Return: 0 on success; Error code otherwise.
  1369. */
  1370. int fman_port_disable(struct fman_port *port)
  1371. {
  1372. u32 __iomem *bmi_cfg_reg, *bmi_status_reg;
  1373. u32 tmp;
  1374. bool rx_port, failure = false;
  1375. int count;
  1376. if (!is_init_done(port->cfg))
  1377. return -EINVAL;
  1378. switch (port->port_type) {
  1379. case FMAN_PORT_TYPE_RX:
  1380. bmi_cfg_reg = &port->bmi_regs->rx.fmbm_rcfg;
  1381. bmi_status_reg = &port->bmi_regs->rx.fmbm_rst;
  1382. rx_port = true;
  1383. break;
  1384. case FMAN_PORT_TYPE_TX:
  1385. bmi_cfg_reg = &port->bmi_regs->tx.fmbm_tcfg;
  1386. bmi_status_reg = &port->bmi_regs->tx.fmbm_tst;
  1387. rx_port = false;
  1388. break;
  1389. default:
  1390. return -EINVAL;
  1391. }
  1392. /* Disable QMI */
  1393. if (!rx_port) {
  1394. tmp = ioread32be(&port->qmi_regs->fmqm_pnc) & ~QMI_PORT_CFG_EN;
  1395. iowrite32be(tmp, &port->qmi_regs->fmqm_pnc);
  1396. /* Wait for QMI to finish FD handling */
  1397. count = 100;
  1398. do {
  1399. udelay(10);
  1400. tmp = ioread32be(&port->qmi_regs->fmqm_pns);
  1401. } while ((tmp & QMI_PORT_STATUS_DEQ_FD_BSY) && --count);
  1402. if (count == 0) {
  1403. /* Timeout */
  1404. failure = true;
  1405. }
  1406. }
  1407. /* Disable BMI */
  1408. tmp = ioread32be(bmi_cfg_reg) & ~BMI_PORT_CFG_EN;
  1409. iowrite32be(tmp, bmi_cfg_reg);
  1410. /* Wait for graceful stop end */
  1411. count = 500;
  1412. do {
  1413. udelay(10);
  1414. tmp = ioread32be(bmi_status_reg);
  1415. } while ((tmp & BMI_PORT_STATUS_BSY) && --count);
  1416. if (count == 0) {
  1417. /* Timeout */
  1418. failure = true;
  1419. }
  1420. if (failure)
  1421. dev_dbg(port->dev, "%s: FMan Port[%d]: BMI or QMI is Busy. Port forced down\n",
  1422. __func__, port->port_id);
  1423. return 0;
  1424. }
  1425. EXPORT_SYMBOL(fman_port_disable);
  1426. /**
  1427. * fman_port_enable
  1428. * port: A pointer to a FM Port module.
  1429. *
  1430. * A runtime routine provided to allow disable/enable of port.
  1431. *
  1432. * Allowed only following fman_port_init().
  1433. *
  1434. * Return: 0 on success; Error code otherwise.
  1435. */
  1436. int fman_port_enable(struct fman_port *port)
  1437. {
  1438. u32 __iomem *bmi_cfg_reg;
  1439. u32 tmp;
  1440. bool rx_port;
  1441. if (!is_init_done(port->cfg))
  1442. return -EINVAL;
  1443. switch (port->port_type) {
  1444. case FMAN_PORT_TYPE_RX:
  1445. bmi_cfg_reg = &port->bmi_regs->rx.fmbm_rcfg;
  1446. rx_port = true;
  1447. break;
  1448. case FMAN_PORT_TYPE_TX:
  1449. bmi_cfg_reg = &port->bmi_regs->tx.fmbm_tcfg;
  1450. rx_port = false;
  1451. break;
  1452. default:
  1453. return -EINVAL;
  1454. }
  1455. /* Enable QMI */
  1456. if (!rx_port) {
  1457. tmp = ioread32be(&port->qmi_regs->fmqm_pnc) | QMI_PORT_CFG_EN;
  1458. iowrite32be(tmp, &port->qmi_regs->fmqm_pnc);
  1459. }
  1460. /* Enable BMI */
  1461. tmp = ioread32be(bmi_cfg_reg) | BMI_PORT_CFG_EN;
  1462. iowrite32be(tmp, bmi_cfg_reg);
  1463. return 0;
  1464. }
  1465. EXPORT_SYMBOL(fman_port_enable);
  1466. /**
  1467. * fman_port_bind
  1468. * dev: FMan Port OF device pointer
  1469. *
  1470. * Bind to a specific FMan Port.
  1471. *
  1472. * Allowed only after the port was created.
  1473. *
  1474. * Return: A pointer to the FMan port device.
  1475. */
  1476. struct fman_port *fman_port_bind(struct device *dev)
  1477. {
  1478. return (struct fman_port *)(dev_get_drvdata(get_device(dev)));
  1479. }
  1480. EXPORT_SYMBOL(fman_port_bind);
  1481. /**
  1482. * fman_port_get_qman_channel_id
  1483. * port: Pointer to the FMan port devuce
  1484. *
  1485. * Get the QMan channel ID for the specific port
  1486. *
  1487. * Return: QMan channel ID
  1488. */
  1489. u32 fman_port_get_qman_channel_id(struct fman_port *port)
  1490. {
  1491. return port->dts_params.qman_channel_id;
  1492. }
  1493. EXPORT_SYMBOL(fman_port_get_qman_channel_id);
  1494. int fman_port_get_hash_result_offset(struct fman_port *port, u32 *offset)
  1495. {
  1496. if (port->buffer_offsets.hash_result_offset == ILLEGAL_BASE)
  1497. return -EINVAL;
  1498. *offset = port->buffer_offsets.hash_result_offset;
  1499. return 0;
  1500. }
  1501. EXPORT_SYMBOL(fman_port_get_hash_result_offset);
  1502. static int fman_port_probe(struct platform_device *of_dev)
  1503. {
  1504. struct fman_port *port;
  1505. struct fman *fman;
  1506. struct device_node *fm_node, *port_node;
  1507. struct resource res;
  1508. struct resource *dev_res;
  1509. u32 val;
  1510. int err = 0, lenp;
  1511. enum fman_port_type port_type;
  1512. u16 port_speed;
  1513. u8 port_id;
  1514. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1515. if (!port)
  1516. return -ENOMEM;
  1517. port->dev = &of_dev->dev;
  1518. port_node = of_node_get(of_dev->dev.of_node);
  1519. /* Get the FM node */
  1520. fm_node = of_get_parent(port_node);
  1521. if (!fm_node) {
  1522. dev_err(port->dev, "%s: of_get_parent() failed\n", __func__);
  1523. err = -ENODEV;
  1524. goto return_err;
  1525. }
  1526. fman = dev_get_drvdata(&of_find_device_by_node(fm_node)->dev);
  1527. of_node_put(fm_node);
  1528. if (!fman) {
  1529. err = -EINVAL;
  1530. goto return_err;
  1531. }
  1532. err = of_property_read_u32(port_node, "cell-index", &val);
  1533. if (err) {
  1534. dev_err(port->dev, "%s: reading cell-index for %pOF failed\n",
  1535. __func__, port_node);
  1536. err = -EINVAL;
  1537. goto return_err;
  1538. }
  1539. port_id = (u8)val;
  1540. port->dts_params.id = port_id;
  1541. if (of_device_is_compatible(port_node, "fsl,fman-v3-port-tx")) {
  1542. port_type = FMAN_PORT_TYPE_TX;
  1543. port_speed = 1000;
  1544. if (of_find_property(port_node, "fsl,fman-10g-port", &lenp))
  1545. port_speed = 10000;
  1546. } else if (of_device_is_compatible(port_node, "fsl,fman-v2-port-tx")) {
  1547. if (port_id >= TX_10G_PORT_BASE)
  1548. port_speed = 10000;
  1549. else
  1550. port_speed = 1000;
  1551. port_type = FMAN_PORT_TYPE_TX;
  1552. } else if (of_device_is_compatible(port_node, "fsl,fman-v3-port-rx")) {
  1553. port_type = FMAN_PORT_TYPE_RX;
  1554. port_speed = 1000;
  1555. if (of_find_property(port_node, "fsl,fman-10g-port", &lenp))
  1556. port_speed = 10000;
  1557. } else if (of_device_is_compatible(port_node, "fsl,fman-v2-port-rx")) {
  1558. if (port_id >= RX_10G_PORT_BASE)
  1559. port_speed = 10000;
  1560. else
  1561. port_speed = 1000;
  1562. port_type = FMAN_PORT_TYPE_RX;
  1563. } else {
  1564. dev_err(port->dev, "%s: Illegal port type\n", __func__);
  1565. err = -EINVAL;
  1566. goto return_err;
  1567. }
  1568. port->dts_params.type = port_type;
  1569. port->dts_params.speed = port_speed;
  1570. if (port_type == FMAN_PORT_TYPE_TX) {
  1571. u32 qman_channel_id;
  1572. qman_channel_id = fman_get_qman_channel_id(fman, port_id);
  1573. if (qman_channel_id == 0) {
  1574. dev_err(port->dev, "%s: incorrect qman-channel-id\n",
  1575. __func__);
  1576. err = -EINVAL;
  1577. goto return_err;
  1578. }
  1579. port->dts_params.qman_channel_id = qman_channel_id;
  1580. }
  1581. err = of_address_to_resource(port_node, 0, &res);
  1582. if (err < 0) {
  1583. dev_err(port->dev, "%s: of_address_to_resource() failed\n",
  1584. __func__);
  1585. err = -ENOMEM;
  1586. goto return_err;
  1587. }
  1588. port->dts_params.fman = fman;
  1589. of_node_put(port_node);
  1590. dev_res = __devm_request_region(port->dev, &res, res.start,
  1591. resource_size(&res), "fman-port");
  1592. if (!dev_res) {
  1593. dev_err(port->dev, "%s: __devm_request_region() failed\n",
  1594. __func__);
  1595. err = -EINVAL;
  1596. goto free_port;
  1597. }
  1598. port->dts_params.base_addr = devm_ioremap(port->dev, res.start,
  1599. resource_size(&res));
  1600. if (!port->dts_params.base_addr)
  1601. dev_err(port->dev, "%s: devm_ioremap() failed\n", __func__);
  1602. dev_set_drvdata(&of_dev->dev, port);
  1603. return 0;
  1604. return_err:
  1605. of_node_put(port_node);
  1606. free_port:
  1607. kfree(port);
  1608. return err;
  1609. }
  1610. static const struct of_device_id fman_port_match[] = {
  1611. {.compatible = "fsl,fman-v3-port-rx"},
  1612. {.compatible = "fsl,fman-v2-port-rx"},
  1613. {.compatible = "fsl,fman-v3-port-tx"},
  1614. {.compatible = "fsl,fman-v2-port-tx"},
  1615. {}
  1616. };
  1617. MODULE_DEVICE_TABLE(of, fman_port_match);
  1618. static struct platform_driver fman_port_driver = {
  1619. .driver = {
  1620. .name = "fsl-fman-port",
  1621. .of_match_table = fman_port_match,
  1622. },
  1623. .probe = fman_port_probe,
  1624. };
  1625. static int __init fman_port_load(void)
  1626. {
  1627. int err;
  1628. pr_debug("FSL DPAA FMan driver\n");
  1629. err = platform_driver_register(&fman_port_driver);
  1630. if (err < 0)
  1631. pr_err("Error, platform_driver_register() = %d\n", err);
  1632. return err;
  1633. }
  1634. module_init(fman_port_load);
  1635. static void __exit fman_port_unload(void)
  1636. {
  1637. platform_driver_unregister(&fman_port_driver);
  1638. }
  1639. module_exit(fman_port_unload);
  1640. MODULE_LICENSE("Dual BSD/GPL");
  1641. MODULE_DESCRIPTION("Freescale DPAA Frame Manager Port driver");