fman_port.c 52 KB

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