fman_keygen.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * Copyright 2017 NXP
  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 NXP 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 NXP ``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 NXP 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/slab.h>
  34. #include "fman_keygen.h"
  35. /* Maximum number of HW Ports */
  36. #define FMAN_MAX_NUM_OF_HW_PORTS 64
  37. /* Maximum number of KeyGen Schemes */
  38. #define FM_KG_MAX_NUM_OF_SCHEMES 32
  39. /* Number of generic KeyGen Generic Extract Command Registers */
  40. #define FM_KG_NUM_OF_GENERIC_REGS 8
  41. /* Dummy port ID */
  42. #define DUMMY_PORT_ID 0
  43. /* Select Scheme Value Register */
  44. #define KG_SCH_DEF_USE_KGSE_DV_0 2
  45. #define KG_SCH_DEF_USE_KGSE_DV_1 3
  46. /* Registers Shifting values */
  47. #define FM_KG_KGAR_NUM_SHIFT 16
  48. #define KG_SCH_DEF_L4_PORT_SHIFT 8
  49. #define KG_SCH_DEF_IP_ADDR_SHIFT 18
  50. #define KG_SCH_HASH_CONFIG_SHIFT_SHIFT 24
  51. /* KeyGen Registers bit field masks: */
  52. /* Enable bit field mask for KeyGen General Configuration Register */
  53. #define FM_KG_KGGCR_EN 0x80000000
  54. /* KeyGen Global Registers bit field masks */
  55. #define FM_KG_KGAR_GO 0x80000000
  56. #define FM_KG_KGAR_READ 0x40000000
  57. #define FM_KG_KGAR_WRITE 0x00000000
  58. #define FM_KG_KGAR_SEL_SCHEME_ENTRY 0x00000000
  59. #define FM_KG_KGAR_SCM_WSEL_UPDATE_CNT 0x00008000
  60. #define FM_KG_KGAR_ERR 0x20000000
  61. #define FM_KG_KGAR_SEL_CLS_PLAN_ENTRY 0x01000000
  62. #define FM_KG_KGAR_SEL_PORT_ENTRY 0x02000000
  63. #define FM_KG_KGAR_SEL_PORT_WSEL_SP 0x00008000
  64. #define FM_KG_KGAR_SEL_PORT_WSEL_CPP 0x00004000
  65. /* Error events exceptions */
  66. #define FM_EX_KG_DOUBLE_ECC 0x80000000
  67. #define FM_EX_KG_KEYSIZE_OVERFLOW 0x40000000
  68. /* Scheme Registers bit field masks */
  69. #define KG_SCH_MODE_EN 0x80000000
  70. #define KG_SCH_VSP_NO_KSP_EN 0x80000000
  71. #define KG_SCH_HASH_CONFIG_SYM 0x40000000
  72. /* Known Protocol field codes */
  73. #define KG_SCH_KN_PORT_ID 0x80000000
  74. #define KG_SCH_KN_MACDST 0x40000000
  75. #define KG_SCH_KN_MACSRC 0x20000000
  76. #define KG_SCH_KN_TCI1 0x10000000
  77. #define KG_SCH_KN_TCI2 0x08000000
  78. #define KG_SCH_KN_ETYPE 0x04000000
  79. #define KG_SCH_KN_PPPSID 0x02000000
  80. #define KG_SCH_KN_PPPID 0x01000000
  81. #define KG_SCH_KN_MPLS1 0x00800000
  82. #define KG_SCH_KN_MPLS2 0x00400000
  83. #define KG_SCH_KN_MPLS_LAST 0x00200000
  84. #define KG_SCH_KN_IPSRC1 0x00100000
  85. #define KG_SCH_KN_IPDST1 0x00080000
  86. #define KG_SCH_KN_PTYPE1 0x00040000
  87. #define KG_SCH_KN_IPTOS_TC1 0x00020000
  88. #define KG_SCH_KN_IPV6FL1 0x00010000
  89. #define KG_SCH_KN_IPSRC2 0x00008000
  90. #define KG_SCH_KN_IPDST2 0x00004000
  91. #define KG_SCH_KN_PTYPE2 0x00002000
  92. #define KG_SCH_KN_IPTOS_TC2 0x00001000
  93. #define KG_SCH_KN_IPV6FL2 0x00000800
  94. #define KG_SCH_KN_GREPTYPE 0x00000400
  95. #define KG_SCH_KN_IPSEC_SPI 0x00000200
  96. #define KG_SCH_KN_IPSEC_NH 0x00000100
  97. #define KG_SCH_KN_IPPID 0x00000080
  98. #define KG_SCH_KN_L4PSRC 0x00000004
  99. #define KG_SCH_KN_L4PDST 0x00000002
  100. #define KG_SCH_KN_TFLG 0x00000001
  101. /* NIA values */
  102. #define NIA_ENG_BMI 0x00500000
  103. #define NIA_BMI_AC_ENQ_FRAME 0x00000002
  104. #define ENQUEUE_KG_DFLT_NIA (NIA_ENG_BMI | NIA_BMI_AC_ENQ_FRAME)
  105. /* Hard-coded configuration:
  106. * These values are used as hard-coded values for KeyGen configuration
  107. * and they replace user selections for this hard-coded version
  108. */
  109. /* Hash distribution shift */
  110. #define DEFAULT_HASH_DIST_FQID_SHIFT 0
  111. /* Hash shift */
  112. #define DEFAULT_HASH_SHIFT 0
  113. /* Symmetric hash usage:
  114. * Warning:
  115. * - the value for symmetric hash usage must be in accordance with hash
  116. * key defined below
  117. * - according to tests performed, spreading is not working if symmetric
  118. * hash is set on true
  119. * So ultimately symmetric hash functionality should be always disabled:
  120. */
  121. #define DEFAULT_SYMMETRIC_HASH false
  122. /* Hash Key extraction fields: */
  123. #define DEFAULT_HASH_KEY_EXTRACT_FIELDS \
  124. (KG_SCH_KN_IPSRC1 | KG_SCH_KN_IPDST1 | \
  125. KG_SCH_KN_L4PSRC | KG_SCH_KN_L4PDST)
  126. /* Default values to be used as hash key in case IPv4 or L4 (TCP, UDP)
  127. * don't exist in the frame
  128. */
  129. /* Default IPv4 address */
  130. #define DEFAULT_HASH_KEY_IPv4_ADDR 0x0A0A0A0A
  131. /* Default L4 port */
  132. #define DEFAULT_HASH_KEY_L4_PORT 0x0B0B0B0B
  133. /* KeyGen Memory Mapped Registers: */
  134. /* Scheme Configuration RAM Registers */
  135. struct fman_kg_scheme_regs {
  136. u32 kgse_mode; /* 0x100: MODE */
  137. u32 kgse_ekfc; /* 0x104: Extract Known Fields Command */
  138. u32 kgse_ekdv; /* 0x108: Extract Known Default Value */
  139. u32 kgse_bmch; /* 0x10C: Bit Mask Command High */
  140. u32 kgse_bmcl; /* 0x110: Bit Mask Command Low */
  141. u32 kgse_fqb; /* 0x114: Frame Queue Base */
  142. u32 kgse_hc; /* 0x118: Hash Command */
  143. u32 kgse_ppc; /* 0x11C: Policer Profile Command */
  144. u32 kgse_gec[FM_KG_NUM_OF_GENERIC_REGS];
  145. /* 0x120: Generic Extract Command */
  146. u32 kgse_spc;
  147. /* 0x140: KeyGen Scheme Entry Statistic Packet Counter */
  148. u32 kgse_dv0; /* 0x144: KeyGen Scheme Entry Default Value 0 */
  149. u32 kgse_dv1; /* 0x148: KeyGen Scheme Entry Default Value 1 */
  150. u32 kgse_ccbs;
  151. /* 0x14C: KeyGen Scheme Entry Coarse Classification Bit*/
  152. u32 kgse_mv; /* 0x150: KeyGen Scheme Entry Match vector */
  153. u32 kgse_om; /* 0x154: KeyGen Scheme Entry Operation Mode bits */
  154. u32 kgse_vsp;
  155. /* 0x158: KeyGen Scheme Entry Virtual Storage Profile */
  156. };
  157. /* Port Partition Configuration Registers */
  158. struct fman_kg_pe_regs {
  159. u32 fmkg_pe_sp; /* 0x100: KeyGen Port entry Scheme Partition */
  160. u32 fmkg_pe_cpp;
  161. /* 0x104: KeyGen Port Entry Classification Plan Partition */
  162. };
  163. /* General Configuration and Status Registers
  164. * Global Statistic Counters
  165. * KeyGen Global Registers
  166. */
  167. struct fman_kg_regs {
  168. u32 fmkg_gcr; /* 0x000: KeyGen General Configuration Register */
  169. u32 res004; /* 0x004: Reserved */
  170. u32 res008; /* 0x008: Reserved */
  171. u32 fmkg_eer; /* 0x00C: KeyGen Error Event Register */
  172. u32 fmkg_eeer; /* 0x010: KeyGen Error Event Enable Register */
  173. u32 res014; /* 0x014: Reserved */
  174. u32 res018; /* 0x018: Reserved */
  175. u32 fmkg_seer; /* 0x01C: KeyGen Scheme Error Event Register */
  176. u32 fmkg_seeer; /* 0x020: KeyGen Scheme Error Event Enable Register */
  177. u32 fmkg_gsr; /* 0x024: KeyGen Global Status Register */
  178. u32 fmkg_tpc; /* 0x028: Total Packet Counter Register */
  179. u32 fmkg_serc; /* 0x02C: Soft Error Capture Register */
  180. u32 res030[4]; /* 0x030: Reserved */
  181. u32 fmkg_fdor; /* 0x034: Frame Data Offset Register */
  182. u32 fmkg_gdv0r; /* 0x038: Global Default Value Register 0 */
  183. u32 fmkg_gdv1r; /* 0x03C: Global Default Value Register 1 */
  184. u32 res04c[6]; /* 0x040: Reserved */
  185. u32 fmkg_feer; /* 0x044: Force Error Event Register */
  186. u32 res068[38]; /* 0x048: Reserved */
  187. union {
  188. u32 fmkg_indirect[63]; /* 0x100: Indirect Access Registers */
  189. struct fman_kg_scheme_regs fmkg_sch; /* Scheme Registers */
  190. struct fman_kg_pe_regs fmkg_pe; /* Port Partition Registers */
  191. };
  192. u32 fmkg_ar; /* 0x1FC: KeyGen Action Register */
  193. };
  194. /* KeyGen Scheme data */
  195. struct keygen_scheme {
  196. bool used; /* Specifies if this scheme is used */
  197. u8 hw_port_id;
  198. /* Hardware port ID
  199. * schemes sharing between multiple ports is not
  200. * currently supported
  201. * so we have only one port id bound to a scheme
  202. */
  203. u32 base_fqid;
  204. /* Base FQID:
  205. * Must be between 1 and 2^24-1
  206. * If hash is used and an even distribution is
  207. * expected according to hash_fqid_count,
  208. * base_fqid must be aligned to hash_fqid_count
  209. */
  210. u32 hash_fqid_count;
  211. /* FQ range for hash distribution:
  212. * Must be a power of 2
  213. * Represents the range of queues for spreading
  214. */
  215. bool use_hashing; /* Usage of Hashing and spreading over FQ */
  216. bool symmetric_hash; /* Symmetric Hash option usage */
  217. u8 hashShift;
  218. /* Hash result right shift.
  219. * Select the 24 bits out of the 64 hash result.
  220. * 0 means using the 24 LSB's, otherwise
  221. * use the 24 LSB's after shifting right
  222. */
  223. u32 match_vector; /* Match Vector */
  224. };
  225. /* KeyGen driver data */
  226. struct fman_keygen {
  227. struct keygen_scheme schemes[FM_KG_MAX_NUM_OF_SCHEMES];
  228. /* Array of schemes */
  229. struct fman_kg_regs __iomem *keygen_regs; /* KeyGen registers */
  230. };
  231. /* keygen_write_ar_wait
  232. *
  233. * Write Action Register with specified value, wait for GO bit field to be
  234. * idle and then read the error
  235. *
  236. * regs: KeyGen registers
  237. * fmkg_ar: Action Register value
  238. *
  239. * Return: Zero for success or error code in case of failure
  240. */
  241. static int keygen_write_ar_wait(struct fman_kg_regs __iomem *regs, u32 fmkg_ar)
  242. {
  243. iowrite32be(fmkg_ar, &regs->fmkg_ar);
  244. /* Wait for GO bit field to be idle */
  245. while (fmkg_ar & FM_KG_KGAR_GO)
  246. fmkg_ar = ioread32be(&regs->fmkg_ar);
  247. if (fmkg_ar & FM_KG_KGAR_ERR)
  248. return -EINVAL;
  249. return 0;
  250. }
  251. /* build_ar_scheme
  252. *
  253. * Build Action Register value for scheme settings
  254. *
  255. * scheme_id: Scheme ID
  256. * update_counter: update scheme counter
  257. * write: true for action to write the scheme or false for read action
  258. *
  259. * Return: AR value
  260. */
  261. static u32 build_ar_scheme(u8 scheme_id, bool update_counter, bool write)
  262. {
  263. u32 rw = (u32)(write ? FM_KG_KGAR_WRITE : FM_KG_KGAR_READ);
  264. return (u32)(FM_KG_KGAR_GO |
  265. rw |
  266. FM_KG_KGAR_SEL_SCHEME_ENTRY |
  267. DUMMY_PORT_ID |
  268. ((u32)scheme_id << FM_KG_KGAR_NUM_SHIFT) |
  269. (update_counter ? FM_KG_KGAR_SCM_WSEL_UPDATE_CNT : 0));
  270. }
  271. /* build_ar_bind_scheme
  272. *
  273. * Build Action Register value for port binding to schemes
  274. *
  275. * hwport_id: HW Port ID
  276. * write: true for action to write the bind or false for read action
  277. *
  278. * Return: AR value
  279. */
  280. static u32 build_ar_bind_scheme(u8 hwport_id, bool write)
  281. {
  282. u32 rw = write ? (u32)FM_KG_KGAR_WRITE : (u32)FM_KG_KGAR_READ;
  283. return (u32)(FM_KG_KGAR_GO |
  284. rw |
  285. FM_KG_KGAR_SEL_PORT_ENTRY |
  286. hwport_id |
  287. FM_KG_KGAR_SEL_PORT_WSEL_SP);
  288. }
  289. /* keygen_write_sp
  290. *
  291. * Write Scheme Partition Register with specified value
  292. *
  293. * regs: KeyGen Registers
  294. * sp: Scheme Partition register value
  295. * add: true to add a scheme partition or false to clear
  296. *
  297. * Return: none
  298. */
  299. static void keygen_write_sp(struct fman_kg_regs __iomem *regs, u32 sp, bool add)
  300. {
  301. u32 tmp;
  302. tmp = ioread32be(&regs->fmkg_pe.fmkg_pe_sp);
  303. if (add)
  304. tmp |= sp;
  305. else
  306. tmp &= ~sp;
  307. iowrite32be(tmp, &regs->fmkg_pe.fmkg_pe_sp);
  308. }
  309. /* build_ar_bind_cls_plan
  310. *
  311. * Build Action Register value for Classification Plan
  312. *
  313. * hwport_id: HW Port ID
  314. * write: true for action to write the CP or false for read action
  315. *
  316. * Return: AR value
  317. */
  318. static u32 build_ar_bind_cls_plan(u8 hwport_id, bool write)
  319. {
  320. u32 rw = write ? (u32)FM_KG_KGAR_WRITE : (u32)FM_KG_KGAR_READ;
  321. return (u32)(FM_KG_KGAR_GO |
  322. rw |
  323. FM_KG_KGAR_SEL_PORT_ENTRY |
  324. hwport_id |
  325. FM_KG_KGAR_SEL_PORT_WSEL_CPP);
  326. }
  327. /* keygen_write_cpp
  328. *
  329. * Write Classification Plan Partition Register with specified value
  330. *
  331. * regs: KeyGen Registers
  332. * cpp: CPP register value
  333. *
  334. * Return: none
  335. */
  336. static void keygen_write_cpp(struct fman_kg_regs __iomem *regs, u32 cpp)
  337. {
  338. iowrite32be(cpp, &regs->fmkg_pe.fmkg_pe_cpp);
  339. }
  340. /* keygen_write_scheme
  341. *
  342. * Write all Schemes Registers with specified values
  343. *
  344. * regs: KeyGen Registers
  345. * scheme_id: Scheme ID
  346. * scheme_regs: Scheme registers values desired to be written
  347. * update_counter: update scheme counter
  348. *
  349. * Return: Zero for success or error code in case of failure
  350. */
  351. static int keygen_write_scheme(struct fman_kg_regs __iomem *regs, u8 scheme_id,
  352. struct fman_kg_scheme_regs *scheme_regs,
  353. bool update_counter)
  354. {
  355. u32 ar_reg;
  356. int err, i;
  357. /* Write indirect scheme registers */
  358. iowrite32be(scheme_regs->kgse_mode, &regs->fmkg_sch.kgse_mode);
  359. iowrite32be(scheme_regs->kgse_ekfc, &regs->fmkg_sch.kgse_ekfc);
  360. iowrite32be(scheme_regs->kgse_ekdv, &regs->fmkg_sch.kgse_ekdv);
  361. iowrite32be(scheme_regs->kgse_bmch, &regs->fmkg_sch.kgse_bmch);
  362. iowrite32be(scheme_regs->kgse_bmcl, &regs->fmkg_sch.kgse_bmcl);
  363. iowrite32be(scheme_regs->kgse_fqb, &regs->fmkg_sch.kgse_fqb);
  364. iowrite32be(scheme_regs->kgse_hc, &regs->fmkg_sch.kgse_hc);
  365. iowrite32be(scheme_regs->kgse_ppc, &regs->fmkg_sch.kgse_ppc);
  366. iowrite32be(scheme_regs->kgse_spc, &regs->fmkg_sch.kgse_spc);
  367. iowrite32be(scheme_regs->kgse_dv0, &regs->fmkg_sch.kgse_dv0);
  368. iowrite32be(scheme_regs->kgse_dv1, &regs->fmkg_sch.kgse_dv1);
  369. iowrite32be(scheme_regs->kgse_ccbs, &regs->fmkg_sch.kgse_ccbs);
  370. iowrite32be(scheme_regs->kgse_mv, &regs->fmkg_sch.kgse_mv);
  371. iowrite32be(scheme_regs->kgse_om, &regs->fmkg_sch.kgse_om);
  372. iowrite32be(scheme_regs->kgse_vsp, &regs->fmkg_sch.kgse_vsp);
  373. for (i = 0 ; i < FM_KG_NUM_OF_GENERIC_REGS ; i++)
  374. iowrite32be(scheme_regs->kgse_gec[i],
  375. &regs->fmkg_sch.kgse_gec[i]);
  376. /* Write AR (Action register) */
  377. ar_reg = build_ar_scheme(scheme_id, update_counter, true);
  378. err = keygen_write_ar_wait(regs, ar_reg);
  379. if (err != 0) {
  380. pr_err("Writing Action Register failed\n");
  381. return err;
  382. }
  383. return err;
  384. }
  385. /* get_free_scheme_id
  386. *
  387. * Find the first free scheme available to be used
  388. *
  389. * keygen: KeyGen handle
  390. * scheme_id: pointer to scheme id
  391. *
  392. * Return: 0 on success, -EINVAL when the are no available free schemes
  393. */
  394. static int get_free_scheme_id(struct fman_keygen *keygen, u8 *scheme_id)
  395. {
  396. u8 i;
  397. for (i = 0; i < FM_KG_MAX_NUM_OF_SCHEMES; i++)
  398. if (!keygen->schemes[i].used) {
  399. *scheme_id = i;
  400. return 0;
  401. }
  402. return -EINVAL;
  403. }
  404. /* get_scheme
  405. *
  406. * Provides the scheme for specified ID
  407. *
  408. * keygen: KeyGen handle
  409. * scheme_id: Scheme ID
  410. *
  411. * Return: handle to required scheme
  412. */
  413. static struct keygen_scheme *get_scheme(struct fman_keygen *keygen,
  414. u8 scheme_id)
  415. {
  416. if (scheme_id >= FM_KG_MAX_NUM_OF_SCHEMES)
  417. return NULL;
  418. return &keygen->schemes[scheme_id];
  419. }
  420. /* keygen_bind_port_to_schemes
  421. *
  422. * Bind the port to schemes
  423. *
  424. * keygen: KeyGen handle
  425. * scheme_id: id of the scheme to bind to
  426. * bind: true to bind the port or false to unbind it
  427. *
  428. * Return: Zero for success or error code in case of failure
  429. */
  430. static int keygen_bind_port_to_schemes(struct fman_keygen *keygen,
  431. u8 scheme_id,
  432. bool bind)
  433. {
  434. struct fman_kg_regs __iomem *keygen_regs = keygen->keygen_regs;
  435. struct keygen_scheme *scheme;
  436. u32 ar_reg;
  437. u32 schemes_vector = 0;
  438. int err;
  439. scheme = get_scheme(keygen, scheme_id);
  440. if (!scheme) {
  441. pr_err("Requested Scheme does not exist\n");
  442. return -EINVAL;
  443. }
  444. if (!scheme->used) {
  445. pr_err("Cannot bind port to an invalid scheme\n");
  446. return -EINVAL;
  447. }
  448. schemes_vector |= 1 << (31 - scheme_id);
  449. ar_reg = build_ar_bind_scheme(scheme->hw_port_id, false);
  450. err = keygen_write_ar_wait(keygen_regs, ar_reg);
  451. if (err != 0) {
  452. pr_err("Reading Action Register failed\n");
  453. return err;
  454. }
  455. keygen_write_sp(keygen_regs, schemes_vector, bind);
  456. ar_reg = build_ar_bind_scheme(scheme->hw_port_id, true);
  457. err = keygen_write_ar_wait(keygen_regs, ar_reg);
  458. if (err != 0) {
  459. pr_err("Writing Action Register failed\n");
  460. return err;
  461. }
  462. return 0;
  463. }
  464. /* keygen_scheme_setup
  465. *
  466. * Setup the scheme according to required configuration
  467. *
  468. * keygen: KeyGen handle
  469. * scheme_id: scheme ID
  470. * enable: true to enable scheme or false to disable it
  471. *
  472. * Return: Zero for success or error code in case of failure
  473. */
  474. static int keygen_scheme_setup(struct fman_keygen *keygen, u8 scheme_id,
  475. bool enable)
  476. {
  477. struct fman_kg_regs __iomem *keygen_regs = keygen->keygen_regs;
  478. struct fman_kg_scheme_regs scheme_regs;
  479. struct keygen_scheme *scheme;
  480. u32 tmp_reg;
  481. int err;
  482. scheme = get_scheme(keygen, scheme_id);
  483. if (!scheme) {
  484. pr_err("Requested Scheme does not exist\n");
  485. return -EINVAL;
  486. }
  487. if (enable && scheme->used) {
  488. pr_err("The requested Scheme is already used\n");
  489. return -EINVAL;
  490. }
  491. /* Clear scheme registers */
  492. memset(&scheme_regs, 0, sizeof(struct fman_kg_scheme_regs));
  493. /* Setup all scheme registers: */
  494. tmp_reg = 0;
  495. if (enable) {
  496. /* Enable Scheme */
  497. tmp_reg |= KG_SCH_MODE_EN;
  498. /* Enqueue frame NIA */
  499. tmp_reg |= ENQUEUE_KG_DFLT_NIA;
  500. }
  501. scheme_regs.kgse_mode = tmp_reg;
  502. scheme_regs.kgse_mv = scheme->match_vector;
  503. /* Scheme don't override StorageProfile:
  504. * valid only for DPAA_VERSION >= 11
  505. */
  506. scheme_regs.kgse_vsp = KG_SCH_VSP_NO_KSP_EN;
  507. /* Configure Hard-Coded Rx Hashing: */
  508. if (scheme->use_hashing) {
  509. /* configure kgse_ekfc */
  510. scheme_regs.kgse_ekfc = DEFAULT_HASH_KEY_EXTRACT_FIELDS;
  511. /* configure kgse_ekdv */
  512. tmp_reg = 0;
  513. tmp_reg |= (KG_SCH_DEF_USE_KGSE_DV_0 <<
  514. KG_SCH_DEF_IP_ADDR_SHIFT);
  515. tmp_reg |= (KG_SCH_DEF_USE_KGSE_DV_1 <<
  516. KG_SCH_DEF_L4_PORT_SHIFT);
  517. scheme_regs.kgse_ekdv = tmp_reg;
  518. /* configure kgse_dv0 */
  519. scheme_regs.kgse_dv0 = DEFAULT_HASH_KEY_IPv4_ADDR;
  520. /* configure kgse_dv1 */
  521. scheme_regs.kgse_dv1 = DEFAULT_HASH_KEY_L4_PORT;
  522. /* configure kgse_hc */
  523. tmp_reg = 0;
  524. tmp_reg |= ((scheme->hash_fqid_count - 1) <<
  525. DEFAULT_HASH_DIST_FQID_SHIFT);
  526. tmp_reg |= scheme->hashShift << KG_SCH_HASH_CONFIG_SHIFT_SHIFT;
  527. if (scheme->symmetric_hash) {
  528. /* Normally extraction key should be verified if
  529. * complies with symmetric hash
  530. * But because extraction is hard-coded, we are sure
  531. * the key is symmetric
  532. */
  533. tmp_reg |= KG_SCH_HASH_CONFIG_SYM;
  534. }
  535. scheme_regs.kgse_hc = tmp_reg;
  536. } else {
  537. scheme_regs.kgse_ekfc = 0;
  538. scheme_regs.kgse_hc = 0;
  539. scheme_regs.kgse_ekdv = 0;
  540. scheme_regs.kgse_dv0 = 0;
  541. scheme_regs.kgse_dv1 = 0;
  542. }
  543. /* configure kgse_fqb: Scheme FQID base */
  544. tmp_reg = 0;
  545. tmp_reg |= scheme->base_fqid;
  546. scheme_regs.kgse_fqb = tmp_reg;
  547. /* features not used by hard-coded configuration */
  548. scheme_regs.kgse_bmch = 0;
  549. scheme_regs.kgse_bmcl = 0;
  550. scheme_regs.kgse_spc = 0;
  551. /* Write scheme registers */
  552. err = keygen_write_scheme(keygen_regs, scheme_id, &scheme_regs, true);
  553. if (err != 0) {
  554. pr_err("Writing scheme registers failed\n");
  555. return err;
  556. }
  557. /* Update used field for Scheme */
  558. scheme->used = enable;
  559. return 0;
  560. }
  561. /* keygen_init
  562. *
  563. * KeyGen initialization:
  564. * Initializes and enables KeyGen, allocate driver memory, setup registers,
  565. * clear port bindings, invalidate all schemes
  566. *
  567. * keygen_regs: KeyGen registers base address
  568. *
  569. * Return: Handle to KeyGen driver
  570. */
  571. struct fman_keygen *keygen_init(struct fman_kg_regs __iomem *keygen_regs)
  572. {
  573. struct fman_keygen *keygen;
  574. u32 ar;
  575. int i;
  576. /* Allocate memory for KeyGen driver */
  577. keygen = kzalloc(sizeof(*keygen), GFP_KERNEL);
  578. if (!keygen)
  579. return NULL;
  580. keygen->keygen_regs = keygen_regs;
  581. /* KeyGen initialization (for Master partition):
  582. * Setup KeyGen registers
  583. */
  584. iowrite32be(ENQUEUE_KG_DFLT_NIA, &keygen_regs->fmkg_gcr);
  585. iowrite32be(FM_EX_KG_DOUBLE_ECC | FM_EX_KG_KEYSIZE_OVERFLOW,
  586. &keygen_regs->fmkg_eer);
  587. iowrite32be(0, &keygen_regs->fmkg_fdor);
  588. iowrite32be(0, &keygen_regs->fmkg_gdv0r);
  589. iowrite32be(0, &keygen_regs->fmkg_gdv1r);
  590. /* Clear binding between ports to schemes and classification plans
  591. * so that all ports are not bound to any scheme/classification plan
  592. */
  593. for (i = 0; i < FMAN_MAX_NUM_OF_HW_PORTS; i++) {
  594. /* Clear all pe sp schemes registers */
  595. keygen_write_sp(keygen_regs, 0xffffffff, false);
  596. ar = build_ar_bind_scheme(i, true);
  597. keygen_write_ar_wait(keygen_regs, ar);
  598. /* Clear all pe cpp classification plans registers */
  599. keygen_write_cpp(keygen_regs, 0);
  600. ar = build_ar_bind_cls_plan(i, true);
  601. keygen_write_ar_wait(keygen_regs, ar);
  602. }
  603. /* Enable all scheme interrupts */
  604. iowrite32be(0xFFFFFFFF, &keygen_regs->fmkg_seer);
  605. iowrite32be(0xFFFFFFFF, &keygen_regs->fmkg_seeer);
  606. /* Enable KyeGen */
  607. iowrite32be(ioread32be(&keygen_regs->fmkg_gcr) | FM_KG_KGGCR_EN,
  608. &keygen_regs->fmkg_gcr);
  609. return keygen;
  610. }
  611. EXPORT_SYMBOL(keygen_init);
  612. /* keygen_port_hashing_init
  613. *
  614. * Initializes a port for Rx Hashing with specified configuration parameters
  615. *
  616. * keygen: KeyGen handle
  617. * hw_port_id: HW Port ID
  618. * hash_base_fqid: Hashing Base FQID used for spreading
  619. * hash_size: Hashing size
  620. *
  621. * Return: Zero for success or error code in case of failure
  622. */
  623. int keygen_port_hashing_init(struct fman_keygen *keygen, u8 hw_port_id,
  624. u32 hash_base_fqid, u32 hash_size)
  625. {
  626. struct keygen_scheme *scheme;
  627. u8 scheme_id;
  628. int err;
  629. /* Validate Scheme configuration parameters */
  630. if (hash_base_fqid == 0 || (hash_base_fqid & ~0x00FFFFFF)) {
  631. pr_err("Base FQID must be between 1 and 2^24-1\n");
  632. return -EINVAL;
  633. }
  634. if (hash_size == 0 || (hash_size & (hash_size - 1)) != 0) {
  635. pr_err("Hash size must be power of two\n");
  636. return -EINVAL;
  637. }
  638. /* Find a free scheme */
  639. err = get_free_scheme_id(keygen, &scheme_id);
  640. if (err) {
  641. pr_err("The maximum number of available Schemes has been exceeded\n");
  642. return -EINVAL;
  643. }
  644. /* Create and configure Hard-Coded Scheme: */
  645. scheme = get_scheme(keygen, scheme_id);
  646. if (!scheme) {
  647. pr_err("Requested Scheme does not exist\n");
  648. return -EINVAL;
  649. }
  650. if (scheme->used) {
  651. pr_err("The requested Scheme is already used\n");
  652. return -EINVAL;
  653. }
  654. /* Clear all scheme fields because the scheme may have been
  655. * previously used
  656. */
  657. memset(scheme, 0, sizeof(struct keygen_scheme));
  658. /* Setup scheme: */
  659. scheme->hw_port_id = hw_port_id;
  660. scheme->use_hashing = true;
  661. scheme->base_fqid = hash_base_fqid;
  662. scheme->hash_fqid_count = hash_size;
  663. scheme->symmetric_hash = DEFAULT_SYMMETRIC_HASH;
  664. scheme->hashShift = DEFAULT_HASH_SHIFT;
  665. /* All Schemes in hard-coded configuration
  666. * are Indirect Schemes
  667. */
  668. scheme->match_vector = 0;
  669. err = keygen_scheme_setup(keygen, scheme_id, true);
  670. if (err != 0) {
  671. pr_err("Scheme setup failed\n");
  672. return err;
  673. }
  674. /* Bind Rx port to Scheme */
  675. err = keygen_bind_port_to_schemes(keygen, scheme_id, true);
  676. if (err != 0) {
  677. pr_err("Binding port to schemes failed\n");
  678. return err;
  679. }
  680. return 0;
  681. }
  682. EXPORT_SYMBOL(keygen_port_hashing_init);