sja1105_dynamic_config.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
  3. */
  4. #include "sja1105.h"
  5. /* In the dynamic configuration interface, the switch exposes a register-like
  6. * view of some of the static configuration tables.
  7. * Many times the field organization of the dynamic tables is abbreviated (not
  8. * all fields are dynamically reconfigurable) and different from the static
  9. * ones, but the key reason for having it is that we can spare a switch reset
  10. * for settings that can be changed dynamically.
  11. *
  12. * This file creates a per-switch-family abstraction called
  13. * struct sja1105_dynamic_table_ops and two operations that work with it:
  14. * - sja1105_dynamic_config_write
  15. * - sja1105_dynamic_config_read
  16. *
  17. * Compared to the struct sja1105_table_ops from sja1105_static_config.c,
  18. * the dynamic accessors work with a compound buffer:
  19. *
  20. * packed_buf
  21. *
  22. * |
  23. * V
  24. * +-----------------------------------------+------------------+
  25. * | ENTRY BUFFER | COMMAND BUFFER |
  26. * +-----------------------------------------+------------------+
  27. *
  28. * <----------------------- packed_size ------------------------>
  29. *
  30. * The ENTRY BUFFER may or may not have the same layout, or size, as its static
  31. * configuration table entry counterpart. When it does, the same packing
  32. * function is reused (bar exceptional cases - see
  33. * sja1105pqrs_dyn_l2_lookup_entry_packing).
  34. *
  35. * The reason for the COMMAND BUFFER being at the end is to be able to send
  36. * a dynamic write command through a single SPI burst. By the time the switch
  37. * reacts to the command, the ENTRY BUFFER is already populated with the data
  38. * sent by the core.
  39. *
  40. * The COMMAND BUFFER is always SJA1105_SIZE_DYN_CMD bytes (one 32-bit word) in
  41. * size.
  42. *
  43. * Sometimes the ENTRY BUFFER does not really exist (when the number of fields
  44. * that can be reconfigured is small), then the switch repurposes some of the
  45. * unused 32 bits of the COMMAND BUFFER to hold ENTRY data.
  46. *
  47. * The key members of struct sja1105_dynamic_table_ops are:
  48. * - .entry_packing: A function that deals with packing an ENTRY structure
  49. * into an SPI buffer, or retrieving an ENTRY structure
  50. * from one.
  51. * The @packed_buf pointer it's given does always point to
  52. * the ENTRY portion of the buffer.
  53. * - .cmd_packing: A function that deals with packing/unpacking the COMMAND
  54. * structure to/from the SPI buffer.
  55. * It is given the same @packed_buf pointer as .entry_packing,
  56. * so most of the time, the @packed_buf points *behind* the
  57. * COMMAND offset inside the buffer.
  58. * To access the COMMAND portion of the buffer, the function
  59. * knows its correct offset.
  60. * Giving both functions the same pointer is handy because in
  61. * extreme cases (see sja1105pqrs_dyn_l2_lookup_entry_packing)
  62. * the .entry_packing is able to jump to the COMMAND portion,
  63. * or vice-versa (sja1105pqrs_l2_lookup_cmd_packing).
  64. * - .access: A bitmap of:
  65. * OP_READ: Set if the hardware manual marks the ENTRY portion of the
  66. * dynamic configuration table buffer as R (readable) after
  67. * an SPI read command (the switch will populate the buffer).
  68. * OP_WRITE: Set if the manual marks the ENTRY portion of the dynamic
  69. * table buffer as W (writable) after an SPI write command
  70. * (the switch will read the fields provided in the buffer).
  71. * OP_DEL: Set if the manual says the VALIDENT bit is supported in the
  72. * COMMAND portion of this dynamic config buffer (i.e. the
  73. * specified entry can be invalidated through a SPI write
  74. * command).
  75. * OP_SEARCH: Set if the manual says that the index of an entry can
  76. * be retrieved in the COMMAND portion of the buffer based
  77. * on its ENTRY portion, as a result of a SPI write command.
  78. * Only the TCAM-based FDB table on SJA1105 P/Q/R/S supports
  79. * this.
  80. * OP_VALID_ANYWAY: Reading some tables through the dynamic config
  81. * interface is possible even if the VALIDENT bit is not
  82. * set in the writeback. So don't error out in that case.
  83. * - .max_entry_count: The number of entries, counting from zero, that can be
  84. * reconfigured through the dynamic interface. If a static
  85. * table can be reconfigured at all dynamically, this
  86. * number always matches the maximum number of supported
  87. * static entries.
  88. * - .packed_size: The length in bytes of the compound ENTRY + COMMAND BUFFER.
  89. * Note that sometimes the compound buffer may contain holes in
  90. * it (see sja1105_vlan_lookup_cmd_packing). The @packed_buf is
  91. * contiguous however, so @packed_size includes any unused
  92. * bytes.
  93. * - .addr: The base SPI address at which the buffer must be written to the
  94. * switch's memory. When looking at the hardware manual, this must
  95. * always match the lowest documented address for the ENTRY, and not
  96. * that of the COMMAND, since the other 32-bit words will follow along
  97. * at the correct addresses.
  98. */
  99. #define SJA1105_SIZE_DYN_CMD 4
  100. #define SJA1105ET_SIZE_VL_LOOKUP_DYN_CMD \
  101. SJA1105_SIZE_DYN_CMD
  102. #define SJA1105PQRS_SIZE_VL_LOOKUP_DYN_CMD \
  103. (SJA1105_SIZE_DYN_CMD + SJA1105_SIZE_VL_LOOKUP_ENTRY)
  104. #define SJA1110_SIZE_VL_POLICING_DYN_CMD \
  105. (SJA1105_SIZE_DYN_CMD + SJA1105_SIZE_VL_POLICING_ENTRY)
  106. #define SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY \
  107. SJA1105_SIZE_DYN_CMD
  108. #define SJA1105ET_SIZE_L2_LOOKUP_DYN_CMD \
  109. (SJA1105_SIZE_DYN_CMD + SJA1105ET_SIZE_L2_LOOKUP_ENTRY)
  110. #define SJA1105PQRS_SIZE_L2_LOOKUP_DYN_CMD \
  111. (SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY)
  112. #define SJA1110_SIZE_L2_LOOKUP_DYN_CMD \
  113. (SJA1105_SIZE_DYN_CMD + SJA1110_SIZE_L2_LOOKUP_ENTRY)
  114. #define SJA1105_SIZE_VLAN_LOOKUP_DYN_CMD \
  115. (SJA1105_SIZE_DYN_CMD + 4 + SJA1105_SIZE_VLAN_LOOKUP_ENTRY)
  116. #define SJA1110_SIZE_VLAN_LOOKUP_DYN_CMD \
  117. (SJA1105_SIZE_DYN_CMD + SJA1110_SIZE_VLAN_LOOKUP_ENTRY)
  118. #define SJA1105_SIZE_L2_FORWARDING_DYN_CMD \
  119. (SJA1105_SIZE_DYN_CMD + SJA1105_SIZE_L2_FORWARDING_ENTRY)
  120. #define SJA1105ET_SIZE_MAC_CONFIG_DYN_CMD \
  121. (SJA1105_SIZE_DYN_CMD + SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY)
  122. #define SJA1105PQRS_SIZE_MAC_CONFIG_DYN_CMD \
  123. (SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY)
  124. #define SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD \
  125. SJA1105_SIZE_DYN_CMD
  126. #define SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_DYN_CMD \
  127. (SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY)
  128. #define SJA1110_SIZE_L2_LOOKUP_PARAMS_DYN_CMD \
  129. (SJA1105_SIZE_DYN_CMD + SJA1110_SIZE_L2_LOOKUP_PARAMS_ENTRY)
  130. #define SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD \
  131. SJA1105_SIZE_DYN_CMD
  132. #define SJA1105PQRS_SIZE_GENERAL_PARAMS_DYN_CMD \
  133. (SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY)
  134. #define SJA1110_SIZE_GENERAL_PARAMS_DYN_CMD \
  135. (SJA1105_SIZE_DYN_CMD + SJA1110_SIZE_GENERAL_PARAMS_ENTRY)
  136. #define SJA1105PQRS_SIZE_AVB_PARAMS_DYN_CMD \
  137. (SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY)
  138. #define SJA1105_SIZE_RETAGGING_DYN_CMD \
  139. (SJA1105_SIZE_DYN_CMD + SJA1105_SIZE_RETAGGING_ENTRY)
  140. #define SJA1105ET_SIZE_CBS_DYN_CMD \
  141. (SJA1105_SIZE_DYN_CMD + SJA1105ET_SIZE_CBS_ENTRY)
  142. #define SJA1105PQRS_SIZE_CBS_DYN_CMD \
  143. (SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_CBS_ENTRY)
  144. #define SJA1110_SIZE_XMII_PARAMS_DYN_CMD \
  145. SJA1110_SIZE_XMII_PARAMS_ENTRY
  146. #define SJA1110_SIZE_L2_POLICING_DYN_CMD \
  147. (SJA1105_SIZE_DYN_CMD + SJA1105_SIZE_L2_POLICING_ENTRY)
  148. #define SJA1110_SIZE_L2_FORWARDING_PARAMS_DYN_CMD \
  149. SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY
  150. #define SJA1105_MAX_DYN_CMD_SIZE \
  151. SJA1110_SIZE_GENERAL_PARAMS_DYN_CMD
  152. struct sja1105_dyn_cmd {
  153. bool search;
  154. u64 valid;
  155. u64 rdwrset;
  156. u64 errors;
  157. u64 valident;
  158. u64 index;
  159. };
  160. enum sja1105_hostcmd {
  161. SJA1105_HOSTCMD_SEARCH = 1,
  162. SJA1105_HOSTCMD_READ = 2,
  163. SJA1105_HOSTCMD_WRITE = 3,
  164. SJA1105_HOSTCMD_INVALIDATE = 4,
  165. };
  166. /* Command and entry overlap */
  167. static void
  168. sja1105et_vl_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  169. enum packing_op op)
  170. {
  171. const int size = SJA1105_SIZE_DYN_CMD;
  172. sja1105_packing(buf, &cmd->valid, 31, 31, size, op);
  173. sja1105_packing(buf, &cmd->errors, 30, 30, size, op);
  174. sja1105_packing(buf, &cmd->rdwrset, 29, 29, size, op);
  175. sja1105_packing(buf, &cmd->index, 9, 0, size, op);
  176. }
  177. /* Command and entry are separate */
  178. static void
  179. sja1105pqrs_vl_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  180. enum packing_op op)
  181. {
  182. u8 *p = buf + SJA1105_SIZE_VL_LOOKUP_ENTRY;
  183. const int size = SJA1105_SIZE_DYN_CMD;
  184. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  185. sja1105_packing(p, &cmd->errors, 30, 30, size, op);
  186. sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
  187. sja1105_packing(p, &cmd->index, 9, 0, size, op);
  188. }
  189. static void
  190. sja1110_vl_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  191. enum packing_op op)
  192. {
  193. u8 *p = buf + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
  194. const int size = SJA1105_SIZE_DYN_CMD;
  195. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  196. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  197. sja1105_packing(p, &cmd->errors, 29, 29, size, op);
  198. sja1105_packing(p, &cmd->index, 11, 0, size, op);
  199. }
  200. static size_t sja1105et_vl_lookup_entry_packing(void *buf, void *entry_ptr,
  201. enum packing_op op)
  202. {
  203. struct sja1105_vl_lookup_entry *entry = entry_ptr;
  204. const int size = SJA1105ET_SIZE_VL_LOOKUP_DYN_CMD;
  205. sja1105_packing(buf, &entry->egrmirr, 21, 17, size, op);
  206. sja1105_packing(buf, &entry->ingrmirr, 16, 16, size, op);
  207. return size;
  208. }
  209. static void
  210. sja1110_vl_policing_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  211. enum packing_op op)
  212. {
  213. u8 *p = buf + SJA1105_SIZE_VL_LOOKUP_ENTRY;
  214. const int size = SJA1105_SIZE_DYN_CMD;
  215. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  216. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  217. sja1105_packing(p, &cmd->index, 11, 0, size, op);
  218. }
  219. static void
  220. sja1105pqrs_common_l2_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  221. enum packing_op op, int entry_size)
  222. {
  223. const int size = SJA1105_SIZE_DYN_CMD;
  224. u8 *p = buf + entry_size;
  225. u64 hostcmd;
  226. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  227. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  228. sja1105_packing(p, &cmd->errors, 29, 29, size, op);
  229. sja1105_packing(p, &cmd->valident, 27, 27, size, op);
  230. /* VALIDENT is supposed to indicate "keep or not", but in SJA1105 E/T,
  231. * using it to delete a management route was unsupported. UM10944
  232. * said about it:
  233. *
  234. * In case of a write access with the MGMTROUTE flag set,
  235. * the flag will be ignored. It will always be found cleared
  236. * for read accesses with the MGMTROUTE flag set.
  237. *
  238. * SJA1105 P/Q/R/S keeps the same behavior w.r.t. VALIDENT, but there
  239. * is now another flag called HOSTCMD which does more stuff (quoting
  240. * from UM11040):
  241. *
  242. * A write request is accepted only when HOSTCMD is set to write host
  243. * or invalid. A read request is accepted only when HOSTCMD is set to
  244. * search host or read host.
  245. *
  246. * So it is possible to translate a RDWRSET/VALIDENT combination into
  247. * HOSTCMD so that we keep the dynamic command API in place, and
  248. * at the same time achieve compatibility with the management route
  249. * command structure.
  250. */
  251. if (cmd->rdwrset == SPI_READ) {
  252. if (cmd->search)
  253. hostcmd = SJA1105_HOSTCMD_SEARCH;
  254. else
  255. hostcmd = SJA1105_HOSTCMD_READ;
  256. } else {
  257. /* SPI_WRITE */
  258. if (cmd->valident)
  259. hostcmd = SJA1105_HOSTCMD_WRITE;
  260. else
  261. hostcmd = SJA1105_HOSTCMD_INVALIDATE;
  262. }
  263. sja1105_packing(p, &hostcmd, 25, 23, size, op);
  264. }
  265. static void
  266. sja1105pqrs_l2_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  267. enum packing_op op)
  268. {
  269. int entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
  270. sja1105pqrs_common_l2_lookup_cmd_packing(buf, cmd, op, entry_size);
  271. /* Hack - The hardware takes the 'index' field within
  272. * struct sja1105_l2_lookup_entry as the index on which this command
  273. * will operate. However it will ignore everything else, so 'index'
  274. * is logically part of command but physically part of entry.
  275. * Populate the 'index' entry field from within the command callback,
  276. * such that our API doesn't need to ask for a full-blown entry
  277. * structure when e.g. a delete is requested.
  278. */
  279. sja1105_packing(buf, &cmd->index, 15, 6, entry_size, op);
  280. }
  281. static void
  282. sja1110_l2_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  283. enum packing_op op)
  284. {
  285. int entry_size = SJA1110_SIZE_L2_LOOKUP_ENTRY;
  286. sja1105pqrs_common_l2_lookup_cmd_packing(buf, cmd, op, entry_size);
  287. sja1105_packing(buf, &cmd->index, 10, 1, entry_size, op);
  288. }
  289. /* The switch is so retarded that it makes our command/entry abstraction
  290. * crumble apart.
  291. *
  292. * On P/Q/R/S, the switch tries to say whether a FDB entry
  293. * is statically programmed or dynamically learned via a flag called LOCKEDS.
  294. * The hardware manual says about this fiels:
  295. *
  296. * On write will specify the format of ENTRY.
  297. * On read the flag will be found cleared at times the VALID flag is found
  298. * set. The flag will also be found cleared in response to a read having the
  299. * MGMTROUTE flag set. In response to a read with the MGMTROUTE flag
  300. * cleared, the flag be set if the most recent access operated on an entry
  301. * that was either loaded by configuration or through dynamic reconfiguration
  302. * (as opposed to automatically learned entries).
  303. *
  304. * The trouble with this flag is that it's part of the *command* to access the
  305. * dynamic interface, and not part of the *entry* retrieved from it.
  306. * Otherwise said, for a sja1105_dynamic_config_read, LOCKEDS is supposed to be
  307. * an output from the switch into the command buffer, and for a
  308. * sja1105_dynamic_config_write, the switch treats LOCKEDS as an input
  309. * (hence we can write either static, or automatically learned entries, from
  310. * the core).
  311. * But the manual contradicts itself in the last phrase where it says that on
  312. * read, LOCKEDS will be set to 1 for all FDB entries written through the
  313. * dynamic interface (therefore, the value of LOCKEDS from the
  314. * sja1105_dynamic_config_write is not really used for anything, it'll store a
  315. * 1 anyway).
  316. * This means you can't really write a FDB entry with LOCKEDS=0 (automatically
  317. * learned) into the switch, which kind of makes sense.
  318. * As for reading through the dynamic interface, it doesn't make too much sense
  319. * to put LOCKEDS into the command, since the switch will inevitably have to
  320. * ignore it (otherwise a command would be like "read the FDB entry 123, but
  321. * only if it's dynamically learned" <- well how am I supposed to know?) and
  322. * just use it as an output buffer for its findings. But guess what... that's
  323. * what the entry buffer is for!
  324. * Unfortunately, what really breaks this abstraction is the fact that it
  325. * wasn't designed having the fact in mind that the switch can output
  326. * entry-related data as writeback through the command buffer.
  327. * However, whether a FDB entry is statically or dynamically learned *is* part
  328. * of the entry and not the command data, no matter what the switch thinks.
  329. * In order to do that, we'll need to wrap around the
  330. * sja1105pqrs_l2_lookup_entry_packing from sja1105_static_config.c, and take
  331. * a peek outside of the caller-supplied @buf (the entry buffer), to reach the
  332. * command buffer.
  333. */
  334. static size_t
  335. sja1105pqrs_dyn_l2_lookup_entry_packing(void *buf, void *entry_ptr,
  336. enum packing_op op)
  337. {
  338. struct sja1105_l2_lookup_entry *entry = entry_ptr;
  339. u8 *cmd = buf + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
  340. const int size = SJA1105_SIZE_DYN_CMD;
  341. sja1105_packing(cmd, &entry->lockeds, 28, 28, size, op);
  342. return sja1105pqrs_l2_lookup_entry_packing(buf, entry_ptr, op);
  343. }
  344. static size_t sja1110_dyn_l2_lookup_entry_packing(void *buf, void *entry_ptr,
  345. enum packing_op op)
  346. {
  347. struct sja1105_l2_lookup_entry *entry = entry_ptr;
  348. u8 *cmd = buf + SJA1110_SIZE_L2_LOOKUP_ENTRY;
  349. const int size = SJA1105_SIZE_DYN_CMD;
  350. sja1105_packing(cmd, &entry->lockeds, 28, 28, size, op);
  351. return sja1110_l2_lookup_entry_packing(buf, entry_ptr, op);
  352. }
  353. static void
  354. sja1105et_l2_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  355. enum packing_op op)
  356. {
  357. u8 *p = buf + SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
  358. const int size = SJA1105_SIZE_DYN_CMD;
  359. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  360. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  361. sja1105_packing(p, &cmd->errors, 29, 29, size, op);
  362. sja1105_packing(p, &cmd->valident, 27, 27, size, op);
  363. /* Hack - see comments above. */
  364. sja1105_packing(buf, &cmd->index, 29, 20,
  365. SJA1105ET_SIZE_L2_LOOKUP_ENTRY, op);
  366. }
  367. static size_t sja1105et_dyn_l2_lookup_entry_packing(void *buf, void *entry_ptr,
  368. enum packing_op op)
  369. {
  370. struct sja1105_l2_lookup_entry *entry = entry_ptr;
  371. u8 *cmd = buf + SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
  372. const int size = SJA1105_SIZE_DYN_CMD;
  373. sja1105_packing(cmd, &entry->lockeds, 28, 28, size, op);
  374. return sja1105et_l2_lookup_entry_packing(buf, entry_ptr, op);
  375. }
  376. static void
  377. sja1105et_mgmt_route_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  378. enum packing_op op)
  379. {
  380. u8 *p = buf + SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
  381. u64 mgmtroute = 1;
  382. sja1105et_l2_lookup_cmd_packing(buf, cmd, op);
  383. if (op == PACK)
  384. sja1105_pack(p, &mgmtroute, 26, 26, SJA1105_SIZE_DYN_CMD);
  385. }
  386. static size_t sja1105et_mgmt_route_entry_packing(void *buf, void *entry_ptr,
  387. enum packing_op op)
  388. {
  389. struct sja1105_mgmt_entry *entry = entry_ptr;
  390. const size_t size = SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
  391. /* UM10944: To specify if a PTP egress timestamp shall be captured on
  392. * each port upon transmission of the frame, the LSB of VLANID in the
  393. * ENTRY field provided by the host must be set.
  394. * Bit 1 of VLANID then specifies the register where the timestamp for
  395. * this port is stored in.
  396. */
  397. sja1105_packing(buf, &entry->tsreg, 85, 85, size, op);
  398. sja1105_packing(buf, &entry->takets, 84, 84, size, op);
  399. sja1105_packing(buf, &entry->macaddr, 83, 36, size, op);
  400. sja1105_packing(buf, &entry->destports, 35, 31, size, op);
  401. sja1105_packing(buf, &entry->enfport, 30, 30, size, op);
  402. return size;
  403. }
  404. static void
  405. sja1105pqrs_mgmt_route_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  406. enum packing_op op)
  407. {
  408. u8 *p = buf + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
  409. u64 mgmtroute = 1;
  410. sja1105pqrs_l2_lookup_cmd_packing(buf, cmd, op);
  411. if (op == PACK)
  412. sja1105_pack(p, &mgmtroute, 26, 26, SJA1105_SIZE_DYN_CMD);
  413. }
  414. static size_t sja1105pqrs_mgmt_route_entry_packing(void *buf, void *entry_ptr,
  415. enum packing_op op)
  416. {
  417. const size_t size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
  418. struct sja1105_mgmt_entry *entry = entry_ptr;
  419. /* In P/Q/R/S, enfport got renamed to mgmtvalid, but its purpose
  420. * is the same (driver uses it to confirm that frame was sent).
  421. * So just keep the name from E/T.
  422. */
  423. sja1105_packing(buf, &entry->tsreg, 71, 71, size, op);
  424. sja1105_packing(buf, &entry->takets, 70, 70, size, op);
  425. sja1105_packing(buf, &entry->macaddr, 69, 22, size, op);
  426. sja1105_packing(buf, &entry->destports, 21, 17, size, op);
  427. sja1105_packing(buf, &entry->enfport, 16, 16, size, op);
  428. return size;
  429. }
  430. /* In E/T, entry is at addresses 0x27-0x28. There is a 4 byte gap at 0x29,
  431. * and command is at 0x2a. Similarly in P/Q/R/S there is a 1 register gap
  432. * between entry (0x2d, 0x2e) and command (0x30).
  433. */
  434. static void
  435. sja1105_vlan_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  436. enum packing_op op)
  437. {
  438. u8 *p = buf + SJA1105_SIZE_VLAN_LOOKUP_ENTRY + 4;
  439. const int size = SJA1105_SIZE_DYN_CMD;
  440. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  441. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  442. sja1105_packing(p, &cmd->valident, 27, 27, size, op);
  443. /* Hack - see comments above, applied for 'vlanid' field of
  444. * struct sja1105_vlan_lookup_entry.
  445. */
  446. sja1105_packing(buf, &cmd->index, 38, 27,
  447. SJA1105_SIZE_VLAN_LOOKUP_ENTRY, op);
  448. }
  449. /* In SJA1110 there is no gap between the command and the data, yay... */
  450. static void
  451. sja1110_vlan_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  452. enum packing_op op)
  453. {
  454. u8 *p = buf + SJA1110_SIZE_VLAN_LOOKUP_ENTRY;
  455. const int size = SJA1105_SIZE_DYN_CMD;
  456. u64 type_entry = 0;
  457. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  458. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  459. sja1105_packing(p, &cmd->errors, 29, 29, size, op);
  460. /* Hack: treat 'vlanid' field of struct sja1105_vlan_lookup_entry as
  461. * cmd->index.
  462. */
  463. sja1105_packing(buf, &cmd->index, 38, 27,
  464. SJA1110_SIZE_VLAN_LOOKUP_ENTRY, op);
  465. /* But the VALIDENT bit has disappeared, now we are supposed to
  466. * invalidate an entry through the TYPE_ENTRY field of the entry..
  467. * This is a hack to transform the non-zero quality of the TYPE_ENTRY
  468. * field into a VALIDENT bit.
  469. */
  470. if (op == PACK && !cmd->valident) {
  471. sja1105_packing(buf, &type_entry, 40, 39,
  472. SJA1110_SIZE_VLAN_LOOKUP_ENTRY, PACK);
  473. } else if (op == UNPACK) {
  474. sja1105_packing(buf, &type_entry, 40, 39,
  475. SJA1110_SIZE_VLAN_LOOKUP_ENTRY, UNPACK);
  476. cmd->valident = !!type_entry;
  477. }
  478. }
  479. static void
  480. sja1105_l2_forwarding_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  481. enum packing_op op)
  482. {
  483. u8 *p = buf + SJA1105_SIZE_L2_FORWARDING_ENTRY;
  484. const int size = SJA1105_SIZE_DYN_CMD;
  485. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  486. sja1105_packing(p, &cmd->errors, 30, 30, size, op);
  487. sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
  488. sja1105_packing(p, &cmd->index, 4, 0, size, op);
  489. }
  490. static void
  491. sja1110_l2_forwarding_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  492. enum packing_op op)
  493. {
  494. u8 *p = buf + SJA1105_SIZE_L2_FORWARDING_ENTRY;
  495. const int size = SJA1105_SIZE_DYN_CMD;
  496. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  497. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  498. sja1105_packing(p, &cmd->errors, 29, 29, size, op);
  499. sja1105_packing(p, &cmd->index, 4, 0, size, op);
  500. }
  501. static void
  502. sja1105et_mac_config_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  503. enum packing_op op)
  504. {
  505. const int size = SJA1105_SIZE_DYN_CMD;
  506. /* Yup, user manual definitions are reversed */
  507. u8 *reg1 = buf + 4;
  508. sja1105_packing(reg1, &cmd->valid, 31, 31, size, op);
  509. sja1105_packing(reg1, &cmd->index, 26, 24, size, op);
  510. }
  511. static size_t sja1105et_mac_config_entry_packing(void *buf, void *entry_ptr,
  512. enum packing_op op)
  513. {
  514. const int size = SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY;
  515. struct sja1105_mac_config_entry *entry = entry_ptr;
  516. /* Yup, user manual definitions are reversed */
  517. u8 *reg1 = buf + 4;
  518. u8 *reg2 = buf;
  519. sja1105_packing(reg1, &entry->speed, 30, 29, size, op);
  520. sja1105_packing(reg1, &entry->drpdtag, 23, 23, size, op);
  521. sja1105_packing(reg1, &entry->drpuntag, 22, 22, size, op);
  522. sja1105_packing(reg1, &entry->retag, 21, 21, size, op);
  523. sja1105_packing(reg1, &entry->dyn_learn, 20, 20, size, op);
  524. sja1105_packing(reg1, &entry->egress, 19, 19, size, op);
  525. sja1105_packing(reg1, &entry->ingress, 18, 18, size, op);
  526. sja1105_packing(reg1, &entry->ing_mirr, 17, 17, size, op);
  527. sja1105_packing(reg1, &entry->egr_mirr, 16, 16, size, op);
  528. sja1105_packing(reg1, &entry->vlanprio, 14, 12, size, op);
  529. sja1105_packing(reg1, &entry->vlanid, 11, 0, size, op);
  530. sja1105_packing(reg2, &entry->tp_delin, 31, 16, size, op);
  531. sja1105_packing(reg2, &entry->tp_delout, 15, 0, size, op);
  532. /* MAC configuration table entries which can't be reconfigured:
  533. * top, base, enabled, ifg, maxage, drpnona664
  534. */
  535. /* Bogus return value, not used anywhere */
  536. return 0;
  537. }
  538. static void
  539. sja1105pqrs_mac_config_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  540. enum packing_op op)
  541. {
  542. const int size = SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY;
  543. u8 *p = buf + SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY;
  544. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  545. sja1105_packing(p, &cmd->errors, 30, 30, size, op);
  546. sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
  547. sja1105_packing(p, &cmd->index, 2, 0, size, op);
  548. }
  549. static void
  550. sja1110_mac_config_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  551. enum packing_op op)
  552. {
  553. u8 *p = buf + SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY;
  554. const int size = SJA1105_SIZE_DYN_CMD;
  555. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  556. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  557. sja1105_packing(p, &cmd->errors, 29, 29, size, op);
  558. sja1105_packing(p, &cmd->index, 3, 0, size, op);
  559. }
  560. static void
  561. sja1105et_l2_lookup_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  562. enum packing_op op)
  563. {
  564. sja1105_packing(buf, &cmd->valid, 31, 31,
  565. SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD, op);
  566. }
  567. static size_t
  568. sja1105et_l2_lookup_params_entry_packing(void *buf, void *entry_ptr,
  569. enum packing_op op)
  570. {
  571. struct sja1105_l2_lookup_params_entry *entry = entry_ptr;
  572. sja1105_packing(buf, &entry->poly, 7, 0,
  573. SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD, op);
  574. /* Bogus return value, not used anywhere */
  575. return 0;
  576. }
  577. static void
  578. sja1105pqrs_l2_lookup_params_cmd_packing(void *buf,
  579. struct sja1105_dyn_cmd *cmd,
  580. enum packing_op op)
  581. {
  582. u8 *p = buf + SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY;
  583. const int size = SJA1105_SIZE_DYN_CMD;
  584. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  585. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  586. }
  587. static void
  588. sja1110_l2_lookup_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  589. enum packing_op op)
  590. {
  591. u8 *p = buf + SJA1110_SIZE_L2_LOOKUP_PARAMS_ENTRY;
  592. const int size = SJA1105_SIZE_DYN_CMD;
  593. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  594. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  595. sja1105_packing(p, &cmd->errors, 29, 29, size, op);
  596. }
  597. static void
  598. sja1105et_general_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  599. enum packing_op op)
  600. {
  601. const int size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD;
  602. sja1105_packing(buf, &cmd->valid, 31, 31, size, op);
  603. sja1105_packing(buf, &cmd->errors, 30, 30, size, op);
  604. }
  605. static size_t
  606. sja1105et_general_params_entry_packing(void *buf, void *entry_ptr,
  607. enum packing_op op)
  608. {
  609. struct sja1105_general_params_entry *entry = entry_ptr;
  610. const int size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD;
  611. sja1105_packing(buf, &entry->mirr_port, 2, 0, size, op);
  612. /* Bogus return value, not used anywhere */
  613. return 0;
  614. }
  615. static void
  616. sja1105pqrs_general_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  617. enum packing_op op)
  618. {
  619. u8 *p = buf + SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY;
  620. const int size = SJA1105_SIZE_DYN_CMD;
  621. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  622. sja1105_packing(p, &cmd->errors, 30, 30, size, op);
  623. sja1105_packing(p, &cmd->rdwrset, 28, 28, size, op);
  624. }
  625. static void
  626. sja1110_general_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  627. enum packing_op op)
  628. {
  629. u8 *p = buf + SJA1110_SIZE_GENERAL_PARAMS_ENTRY;
  630. const int size = SJA1105_SIZE_DYN_CMD;
  631. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  632. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  633. sja1105_packing(p, &cmd->errors, 29, 29, size, op);
  634. }
  635. static void
  636. sja1105pqrs_avb_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  637. enum packing_op op)
  638. {
  639. u8 *p = buf + SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY;
  640. const int size = SJA1105_SIZE_DYN_CMD;
  641. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  642. sja1105_packing(p, &cmd->errors, 30, 30, size, op);
  643. sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
  644. }
  645. static void
  646. sja1105_retagging_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  647. enum packing_op op)
  648. {
  649. u8 *p = buf + SJA1105_SIZE_RETAGGING_ENTRY;
  650. const int size = SJA1105_SIZE_DYN_CMD;
  651. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  652. sja1105_packing(p, &cmd->errors, 30, 30, size, op);
  653. sja1105_packing(p, &cmd->valident, 29, 29, size, op);
  654. sja1105_packing(p, &cmd->rdwrset, 28, 28, size, op);
  655. sja1105_packing(p, &cmd->index, 5, 0, size, op);
  656. }
  657. static void
  658. sja1110_retagging_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  659. enum packing_op op)
  660. {
  661. u8 *p = buf + SJA1105_SIZE_RETAGGING_ENTRY;
  662. const int size = SJA1105_SIZE_DYN_CMD;
  663. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  664. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  665. sja1105_packing(p, &cmd->errors, 29, 29, size, op);
  666. sja1105_packing(p, &cmd->valident, 28, 28, size, op);
  667. sja1105_packing(p, &cmd->index, 4, 0, size, op);
  668. }
  669. static void sja1105et_cbs_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  670. enum packing_op op)
  671. {
  672. u8 *p = buf + SJA1105ET_SIZE_CBS_ENTRY;
  673. const int size = SJA1105_SIZE_DYN_CMD;
  674. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  675. sja1105_packing(p, &cmd->index, 19, 16, size, op);
  676. }
  677. static size_t sja1105et_cbs_entry_packing(void *buf, void *entry_ptr,
  678. enum packing_op op)
  679. {
  680. const size_t size = SJA1105ET_SIZE_CBS_ENTRY;
  681. struct sja1105_cbs_entry *entry = entry_ptr;
  682. u8 *cmd = buf + size;
  683. u32 *p = buf;
  684. sja1105_packing(cmd, &entry->port, 5, 3, SJA1105_SIZE_DYN_CMD, op);
  685. sja1105_packing(cmd, &entry->prio, 2, 0, SJA1105_SIZE_DYN_CMD, op);
  686. sja1105_packing(p + 3, &entry->credit_lo, 31, 0, size, op);
  687. sja1105_packing(p + 2, &entry->credit_hi, 31, 0, size, op);
  688. sja1105_packing(p + 1, &entry->send_slope, 31, 0, size, op);
  689. sja1105_packing(p + 0, &entry->idle_slope, 31, 0, size, op);
  690. return size;
  691. }
  692. static void sja1105pqrs_cbs_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  693. enum packing_op op)
  694. {
  695. u8 *p = buf + SJA1105PQRS_SIZE_CBS_ENTRY;
  696. const int size = SJA1105_SIZE_DYN_CMD;
  697. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  698. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  699. sja1105_packing(p, &cmd->errors, 29, 29, size, op);
  700. sja1105_packing(p, &cmd->index, 3, 0, size, op);
  701. }
  702. static void sja1110_cbs_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  703. enum packing_op op)
  704. {
  705. u8 *p = buf + SJA1105PQRS_SIZE_CBS_ENTRY;
  706. const int size = SJA1105_SIZE_DYN_CMD;
  707. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  708. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  709. sja1105_packing(p, &cmd->errors, 29, 29, size, op);
  710. sja1105_packing(p, &cmd->index, 7, 0, size, op);
  711. }
  712. static size_t sja1105pqrs_cbs_entry_packing(void *buf, void *entry_ptr,
  713. enum packing_op op)
  714. {
  715. const size_t size = SJA1105PQRS_SIZE_CBS_ENTRY;
  716. struct sja1105_cbs_entry *entry = entry_ptr;
  717. sja1105_packing(buf, &entry->port, 159, 157, size, op);
  718. sja1105_packing(buf, &entry->prio, 156, 154, size, op);
  719. sja1105_packing(buf, &entry->credit_lo, 153, 122, size, op);
  720. sja1105_packing(buf, &entry->credit_hi, 121, 90, size, op);
  721. sja1105_packing(buf, &entry->send_slope, 89, 58, size, op);
  722. sja1105_packing(buf, &entry->idle_slope, 57, 26, size, op);
  723. return size;
  724. }
  725. static size_t sja1110_cbs_entry_packing(void *buf, void *entry_ptr,
  726. enum packing_op op)
  727. {
  728. const size_t size = SJA1105PQRS_SIZE_CBS_ENTRY;
  729. struct sja1105_cbs_entry *entry = entry_ptr;
  730. u64 entry_type = SJA1110_CBS_SHAPER;
  731. sja1105_packing(buf, &entry_type, 159, 159, size, op);
  732. sja1105_packing(buf, &entry->credit_lo, 151, 120, size, op);
  733. sja1105_packing(buf, &entry->credit_hi, 119, 88, size, op);
  734. sja1105_packing(buf, &entry->send_slope, 87, 56, size, op);
  735. sja1105_packing(buf, &entry->idle_slope, 55, 24, size, op);
  736. return size;
  737. }
  738. static void sja1110_dummy_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  739. enum packing_op op)
  740. {
  741. }
  742. static void
  743. sja1110_l2_policing_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
  744. enum packing_op op)
  745. {
  746. u8 *p = buf + SJA1105_SIZE_L2_POLICING_ENTRY;
  747. const int size = SJA1105_SIZE_DYN_CMD;
  748. sja1105_packing(p, &cmd->valid, 31, 31, size, op);
  749. sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
  750. sja1105_packing(p, &cmd->errors, 29, 29, size, op);
  751. sja1105_packing(p, &cmd->index, 6, 0, size, op);
  752. }
  753. #define OP_READ BIT(0)
  754. #define OP_WRITE BIT(1)
  755. #define OP_DEL BIT(2)
  756. #define OP_SEARCH BIT(3)
  757. #define OP_VALID_ANYWAY BIT(4)
  758. /* SJA1105E/T: First generation */
  759. const struct sja1105_dynamic_table_ops sja1105et_dyn_ops[BLK_IDX_MAX_DYN] = {
  760. [BLK_IDX_VL_LOOKUP] = {
  761. .entry_packing = sja1105et_vl_lookup_entry_packing,
  762. .cmd_packing = sja1105et_vl_lookup_cmd_packing,
  763. .access = OP_WRITE,
  764. .max_entry_count = SJA1105_MAX_VL_LOOKUP_COUNT,
  765. .packed_size = SJA1105ET_SIZE_VL_LOOKUP_DYN_CMD,
  766. .addr = 0x35,
  767. },
  768. [BLK_IDX_L2_LOOKUP] = {
  769. .entry_packing = sja1105et_dyn_l2_lookup_entry_packing,
  770. .cmd_packing = sja1105et_l2_lookup_cmd_packing,
  771. .access = (OP_READ | OP_WRITE | OP_DEL),
  772. .max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
  773. .packed_size = SJA1105ET_SIZE_L2_LOOKUP_DYN_CMD,
  774. .addr = 0x20,
  775. },
  776. [BLK_IDX_MGMT_ROUTE] = {
  777. .entry_packing = sja1105et_mgmt_route_entry_packing,
  778. .cmd_packing = sja1105et_mgmt_route_cmd_packing,
  779. .access = (OP_READ | OP_WRITE | OP_VALID_ANYWAY),
  780. .max_entry_count = SJA1105_NUM_PORTS,
  781. .packed_size = SJA1105ET_SIZE_L2_LOOKUP_DYN_CMD,
  782. .addr = 0x20,
  783. },
  784. [BLK_IDX_VLAN_LOOKUP] = {
  785. .entry_packing = sja1105_vlan_lookup_entry_packing,
  786. .cmd_packing = sja1105_vlan_lookup_cmd_packing,
  787. .access = (OP_WRITE | OP_DEL),
  788. .max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
  789. .packed_size = SJA1105_SIZE_VLAN_LOOKUP_DYN_CMD,
  790. .addr = 0x27,
  791. },
  792. [BLK_IDX_L2_FORWARDING] = {
  793. .entry_packing = sja1105_l2_forwarding_entry_packing,
  794. .cmd_packing = sja1105_l2_forwarding_cmd_packing,
  795. .max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
  796. .access = OP_WRITE,
  797. .packed_size = SJA1105_SIZE_L2_FORWARDING_DYN_CMD,
  798. .addr = 0x24,
  799. },
  800. [BLK_IDX_MAC_CONFIG] = {
  801. .entry_packing = sja1105et_mac_config_entry_packing,
  802. .cmd_packing = sja1105et_mac_config_cmd_packing,
  803. .max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
  804. .access = OP_WRITE,
  805. .packed_size = SJA1105ET_SIZE_MAC_CONFIG_DYN_CMD,
  806. .addr = 0x36,
  807. },
  808. [BLK_IDX_L2_LOOKUP_PARAMS] = {
  809. .entry_packing = sja1105et_l2_lookup_params_entry_packing,
  810. .cmd_packing = sja1105et_l2_lookup_params_cmd_packing,
  811. .max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
  812. .access = OP_WRITE,
  813. .packed_size = SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD,
  814. .addr = 0x38,
  815. },
  816. [BLK_IDX_GENERAL_PARAMS] = {
  817. .entry_packing = sja1105et_general_params_entry_packing,
  818. .cmd_packing = sja1105et_general_params_cmd_packing,
  819. .max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
  820. .access = OP_WRITE,
  821. .packed_size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD,
  822. .addr = 0x34,
  823. },
  824. [BLK_IDX_RETAGGING] = {
  825. .entry_packing = sja1105_retagging_entry_packing,
  826. .cmd_packing = sja1105_retagging_cmd_packing,
  827. .max_entry_count = SJA1105_MAX_RETAGGING_COUNT,
  828. .access = (OP_WRITE | OP_DEL),
  829. .packed_size = SJA1105_SIZE_RETAGGING_DYN_CMD,
  830. .addr = 0x31,
  831. },
  832. [BLK_IDX_CBS] = {
  833. .entry_packing = sja1105et_cbs_entry_packing,
  834. .cmd_packing = sja1105et_cbs_cmd_packing,
  835. .max_entry_count = SJA1105ET_MAX_CBS_COUNT,
  836. .access = OP_WRITE,
  837. .packed_size = SJA1105ET_SIZE_CBS_DYN_CMD,
  838. .addr = 0x2c,
  839. },
  840. };
  841. /* SJA1105P/Q/R/S: Second generation */
  842. const struct sja1105_dynamic_table_ops sja1105pqrs_dyn_ops[BLK_IDX_MAX_DYN] = {
  843. [BLK_IDX_VL_LOOKUP] = {
  844. .entry_packing = sja1105_vl_lookup_entry_packing,
  845. .cmd_packing = sja1105pqrs_vl_lookup_cmd_packing,
  846. .access = (OP_READ | OP_WRITE),
  847. .max_entry_count = SJA1105_MAX_VL_LOOKUP_COUNT,
  848. .packed_size = SJA1105PQRS_SIZE_VL_LOOKUP_DYN_CMD,
  849. .addr = 0x47,
  850. },
  851. [BLK_IDX_L2_LOOKUP] = {
  852. .entry_packing = sja1105pqrs_dyn_l2_lookup_entry_packing,
  853. .cmd_packing = sja1105pqrs_l2_lookup_cmd_packing,
  854. .access = (OP_READ | OP_WRITE | OP_DEL | OP_SEARCH),
  855. .max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
  856. .packed_size = SJA1105PQRS_SIZE_L2_LOOKUP_DYN_CMD,
  857. .addr = 0x24,
  858. },
  859. [BLK_IDX_MGMT_ROUTE] = {
  860. .entry_packing = sja1105pqrs_mgmt_route_entry_packing,
  861. .cmd_packing = sja1105pqrs_mgmt_route_cmd_packing,
  862. .access = (OP_READ | OP_WRITE | OP_DEL | OP_SEARCH | OP_VALID_ANYWAY),
  863. .max_entry_count = SJA1105_NUM_PORTS,
  864. .packed_size = SJA1105PQRS_SIZE_L2_LOOKUP_DYN_CMD,
  865. .addr = 0x24,
  866. },
  867. [BLK_IDX_VLAN_LOOKUP] = {
  868. .entry_packing = sja1105_vlan_lookup_entry_packing,
  869. .cmd_packing = sja1105_vlan_lookup_cmd_packing,
  870. .access = (OP_READ | OP_WRITE | OP_DEL),
  871. .max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
  872. .packed_size = SJA1105_SIZE_VLAN_LOOKUP_DYN_CMD,
  873. .addr = 0x2D,
  874. },
  875. [BLK_IDX_L2_FORWARDING] = {
  876. .entry_packing = sja1105_l2_forwarding_entry_packing,
  877. .cmd_packing = sja1105_l2_forwarding_cmd_packing,
  878. .max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
  879. .access = OP_WRITE,
  880. .packed_size = SJA1105_SIZE_L2_FORWARDING_DYN_CMD,
  881. .addr = 0x2A,
  882. },
  883. [BLK_IDX_MAC_CONFIG] = {
  884. .entry_packing = sja1105pqrs_mac_config_entry_packing,
  885. .cmd_packing = sja1105pqrs_mac_config_cmd_packing,
  886. .max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
  887. .access = (OP_READ | OP_WRITE),
  888. .packed_size = SJA1105PQRS_SIZE_MAC_CONFIG_DYN_CMD,
  889. .addr = 0x4B,
  890. },
  891. [BLK_IDX_L2_LOOKUP_PARAMS] = {
  892. .entry_packing = sja1105pqrs_l2_lookup_params_entry_packing,
  893. .cmd_packing = sja1105pqrs_l2_lookup_params_cmd_packing,
  894. .max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
  895. .access = (OP_READ | OP_WRITE),
  896. .packed_size = SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_DYN_CMD,
  897. .addr = 0x54,
  898. },
  899. [BLK_IDX_AVB_PARAMS] = {
  900. .entry_packing = sja1105pqrs_avb_params_entry_packing,
  901. .cmd_packing = sja1105pqrs_avb_params_cmd_packing,
  902. .max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
  903. .access = (OP_READ | OP_WRITE),
  904. .packed_size = SJA1105PQRS_SIZE_AVB_PARAMS_DYN_CMD,
  905. .addr = 0x8003,
  906. },
  907. [BLK_IDX_GENERAL_PARAMS] = {
  908. .entry_packing = sja1105pqrs_general_params_entry_packing,
  909. .cmd_packing = sja1105pqrs_general_params_cmd_packing,
  910. .max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
  911. .access = (OP_READ | OP_WRITE),
  912. .packed_size = SJA1105PQRS_SIZE_GENERAL_PARAMS_DYN_CMD,
  913. .addr = 0x3B,
  914. },
  915. [BLK_IDX_RETAGGING] = {
  916. .entry_packing = sja1105_retagging_entry_packing,
  917. .cmd_packing = sja1105_retagging_cmd_packing,
  918. .max_entry_count = SJA1105_MAX_RETAGGING_COUNT,
  919. .access = (OP_READ | OP_WRITE | OP_DEL),
  920. .packed_size = SJA1105_SIZE_RETAGGING_DYN_CMD,
  921. .addr = 0x38,
  922. },
  923. [BLK_IDX_CBS] = {
  924. .entry_packing = sja1105pqrs_cbs_entry_packing,
  925. .cmd_packing = sja1105pqrs_cbs_cmd_packing,
  926. .max_entry_count = SJA1105PQRS_MAX_CBS_COUNT,
  927. .access = OP_WRITE,
  928. .packed_size = SJA1105PQRS_SIZE_CBS_DYN_CMD,
  929. .addr = 0x32,
  930. },
  931. };
  932. /* SJA1110: Third generation */
  933. const struct sja1105_dynamic_table_ops sja1110_dyn_ops[BLK_IDX_MAX_DYN] = {
  934. [BLK_IDX_VL_LOOKUP] = {
  935. .entry_packing = sja1110_vl_lookup_entry_packing,
  936. .cmd_packing = sja1110_vl_lookup_cmd_packing,
  937. .access = (OP_READ | OP_WRITE | OP_VALID_ANYWAY),
  938. .max_entry_count = SJA1110_MAX_VL_LOOKUP_COUNT,
  939. .packed_size = SJA1105PQRS_SIZE_VL_LOOKUP_DYN_CMD,
  940. .addr = SJA1110_SPI_ADDR(0x124),
  941. },
  942. [BLK_IDX_VL_POLICING] = {
  943. .entry_packing = sja1110_vl_policing_entry_packing,
  944. .cmd_packing = sja1110_vl_policing_cmd_packing,
  945. .access = (OP_READ | OP_WRITE | OP_VALID_ANYWAY),
  946. .max_entry_count = SJA1110_MAX_VL_POLICING_COUNT,
  947. .packed_size = SJA1110_SIZE_VL_POLICING_DYN_CMD,
  948. .addr = SJA1110_SPI_ADDR(0x310),
  949. },
  950. [BLK_IDX_L2_LOOKUP] = {
  951. .entry_packing = sja1110_dyn_l2_lookup_entry_packing,
  952. .cmd_packing = sja1110_l2_lookup_cmd_packing,
  953. .access = (OP_READ | OP_WRITE | OP_DEL | OP_SEARCH),
  954. .max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
  955. .packed_size = SJA1110_SIZE_L2_LOOKUP_DYN_CMD,
  956. .addr = SJA1110_SPI_ADDR(0x8c),
  957. },
  958. [BLK_IDX_VLAN_LOOKUP] = {
  959. .entry_packing = sja1110_vlan_lookup_entry_packing,
  960. .cmd_packing = sja1110_vlan_lookup_cmd_packing,
  961. .access = (OP_READ | OP_WRITE | OP_DEL),
  962. .max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
  963. .packed_size = SJA1110_SIZE_VLAN_LOOKUP_DYN_CMD,
  964. .addr = SJA1110_SPI_ADDR(0xb4),
  965. },
  966. [BLK_IDX_L2_FORWARDING] = {
  967. .entry_packing = sja1110_l2_forwarding_entry_packing,
  968. .cmd_packing = sja1110_l2_forwarding_cmd_packing,
  969. .max_entry_count = SJA1110_MAX_L2_FORWARDING_COUNT,
  970. .access = (OP_READ | OP_WRITE | OP_VALID_ANYWAY),
  971. .packed_size = SJA1105_SIZE_L2_FORWARDING_DYN_CMD,
  972. .addr = SJA1110_SPI_ADDR(0xa8),
  973. },
  974. [BLK_IDX_MAC_CONFIG] = {
  975. .entry_packing = sja1110_mac_config_entry_packing,
  976. .cmd_packing = sja1110_mac_config_cmd_packing,
  977. .max_entry_count = SJA1110_MAX_MAC_CONFIG_COUNT,
  978. .access = (OP_READ | OP_WRITE | OP_VALID_ANYWAY),
  979. .packed_size = SJA1105PQRS_SIZE_MAC_CONFIG_DYN_CMD,
  980. .addr = SJA1110_SPI_ADDR(0x134),
  981. },
  982. [BLK_IDX_L2_LOOKUP_PARAMS] = {
  983. .entry_packing = sja1110_l2_lookup_params_entry_packing,
  984. .cmd_packing = sja1110_l2_lookup_params_cmd_packing,
  985. .max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
  986. .access = (OP_READ | OP_WRITE | OP_VALID_ANYWAY),
  987. .packed_size = SJA1110_SIZE_L2_LOOKUP_PARAMS_DYN_CMD,
  988. .addr = SJA1110_SPI_ADDR(0x158),
  989. },
  990. [BLK_IDX_AVB_PARAMS] = {
  991. .entry_packing = sja1105pqrs_avb_params_entry_packing,
  992. .cmd_packing = sja1105pqrs_avb_params_cmd_packing,
  993. .max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
  994. .access = (OP_READ | OP_WRITE | OP_VALID_ANYWAY),
  995. .packed_size = SJA1105PQRS_SIZE_AVB_PARAMS_DYN_CMD,
  996. .addr = SJA1110_SPI_ADDR(0x2000C),
  997. },
  998. [BLK_IDX_GENERAL_PARAMS] = {
  999. .entry_packing = sja1110_general_params_entry_packing,
  1000. .cmd_packing = sja1110_general_params_cmd_packing,
  1001. .max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
  1002. .access = (OP_READ | OP_WRITE | OP_VALID_ANYWAY),
  1003. .packed_size = SJA1110_SIZE_GENERAL_PARAMS_DYN_CMD,
  1004. .addr = SJA1110_SPI_ADDR(0xe8),
  1005. },
  1006. [BLK_IDX_RETAGGING] = {
  1007. .entry_packing = sja1110_retagging_entry_packing,
  1008. .cmd_packing = sja1110_retagging_cmd_packing,
  1009. .max_entry_count = SJA1105_MAX_RETAGGING_COUNT,
  1010. .access = (OP_READ | OP_WRITE | OP_DEL),
  1011. .packed_size = SJA1105_SIZE_RETAGGING_DYN_CMD,
  1012. .addr = SJA1110_SPI_ADDR(0xdc),
  1013. },
  1014. [BLK_IDX_CBS] = {
  1015. .entry_packing = sja1110_cbs_entry_packing,
  1016. .cmd_packing = sja1110_cbs_cmd_packing,
  1017. .max_entry_count = SJA1110_MAX_CBS_COUNT,
  1018. .access = (OP_READ | OP_WRITE | OP_VALID_ANYWAY),
  1019. .packed_size = SJA1105PQRS_SIZE_CBS_DYN_CMD,
  1020. .addr = SJA1110_SPI_ADDR(0xc4),
  1021. },
  1022. [BLK_IDX_XMII_PARAMS] = {
  1023. .entry_packing = sja1110_xmii_params_entry_packing,
  1024. .cmd_packing = sja1110_dummy_cmd_packing,
  1025. .max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
  1026. .access = (OP_READ | OP_VALID_ANYWAY),
  1027. .packed_size = SJA1110_SIZE_XMII_PARAMS_DYN_CMD,
  1028. .addr = SJA1110_SPI_ADDR(0x3c),
  1029. },
  1030. [BLK_IDX_L2_POLICING] = {
  1031. .entry_packing = sja1110_l2_policing_entry_packing,
  1032. .cmd_packing = sja1110_l2_policing_cmd_packing,
  1033. .max_entry_count = SJA1110_MAX_L2_POLICING_COUNT,
  1034. .access = (OP_READ | OP_WRITE | OP_VALID_ANYWAY),
  1035. .packed_size = SJA1110_SIZE_L2_POLICING_DYN_CMD,
  1036. .addr = SJA1110_SPI_ADDR(0x2fc),
  1037. },
  1038. [BLK_IDX_L2_FORWARDING_PARAMS] = {
  1039. .entry_packing = sja1110_l2_forwarding_params_entry_packing,
  1040. .cmd_packing = sja1110_dummy_cmd_packing,
  1041. .max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
  1042. .access = (OP_READ | OP_VALID_ANYWAY),
  1043. .packed_size = SJA1110_SIZE_L2_FORWARDING_PARAMS_DYN_CMD,
  1044. .addr = SJA1110_SPI_ADDR(0x20000),
  1045. },
  1046. };
  1047. #define SJA1105_DYNAMIC_CONFIG_SLEEP_US 10
  1048. #define SJA1105_DYNAMIC_CONFIG_TIMEOUT_US 100000
  1049. static int
  1050. sja1105_dynamic_config_poll_valid(struct sja1105_private *priv,
  1051. const struct sja1105_dynamic_table_ops *ops,
  1052. void *entry, bool check_valident,
  1053. bool check_errors)
  1054. {
  1055. u8 packed_buf[SJA1105_MAX_DYN_CMD_SIZE] = {};
  1056. struct sja1105_dyn_cmd cmd = {};
  1057. int rc;
  1058. /* Read back the whole entry + command structure. */
  1059. rc = sja1105_xfer_buf(priv, SPI_READ, ops->addr, packed_buf,
  1060. ops->packed_size);
  1061. if (rc)
  1062. return rc;
  1063. /* Unpack the command structure, and return it to the caller in case it
  1064. * needs to perform further checks on it (VALIDENT).
  1065. */
  1066. ops->cmd_packing(packed_buf, &cmd, UNPACK);
  1067. /* Hardware hasn't cleared VALID => still working on it */
  1068. if (cmd.valid)
  1069. return -EAGAIN;
  1070. if (check_valident && !cmd.valident && !(ops->access & OP_VALID_ANYWAY))
  1071. return -ENOENT;
  1072. if (check_errors && cmd.errors)
  1073. return -EINVAL;
  1074. /* Don't dereference possibly NULL pointer - maybe caller
  1075. * only wanted to see whether the entry existed or not.
  1076. */
  1077. if (entry)
  1078. ops->entry_packing(packed_buf, entry, UNPACK);
  1079. return 0;
  1080. }
  1081. /* Poll the dynamic config entry's control area until the hardware has
  1082. * cleared the VALID bit, which means we have confirmation that it has
  1083. * finished processing the command.
  1084. */
  1085. static int
  1086. sja1105_dynamic_config_wait_complete(struct sja1105_private *priv,
  1087. const struct sja1105_dynamic_table_ops *ops,
  1088. void *entry, bool check_valident,
  1089. bool check_errors)
  1090. {
  1091. int err, rc;
  1092. err = read_poll_timeout(sja1105_dynamic_config_poll_valid,
  1093. rc, rc != -EAGAIN,
  1094. SJA1105_DYNAMIC_CONFIG_SLEEP_US,
  1095. SJA1105_DYNAMIC_CONFIG_TIMEOUT_US,
  1096. false, priv, ops, entry, check_valident,
  1097. check_errors);
  1098. return err < 0 ? err : rc;
  1099. }
  1100. /* Provides read access to the settings through the dynamic interface
  1101. * of the switch.
  1102. * @blk_idx is used as key to select from the sja1105_dynamic_table_ops.
  1103. * The selection is limited by the hardware in respect to which
  1104. * configuration blocks can be read through the dynamic interface.
  1105. * @index is used to retrieve a particular table entry. If negative,
  1106. * (and if the @blk_idx supports the searching operation) a search
  1107. * is performed by the @entry parameter.
  1108. * @entry Type-casted to an unpacked structure that holds a table entry
  1109. * of the type specified in @blk_idx.
  1110. * Usually an output argument. If @index is negative, then this
  1111. * argument is used as input/output: it should be pre-populated
  1112. * with the element to search for. Entries which support the
  1113. * search operation will have an "index" field (not the @index
  1114. * argument to this function) and that is where the found index
  1115. * will be returned (or left unmodified - thus negative - if not
  1116. * found).
  1117. */
  1118. int sja1105_dynamic_config_read(struct sja1105_private *priv,
  1119. enum sja1105_blk_idx blk_idx,
  1120. int index, void *entry)
  1121. {
  1122. const struct sja1105_dynamic_table_ops *ops;
  1123. struct sja1105_dyn_cmd cmd = {0};
  1124. /* SPI payload buffer */
  1125. u8 packed_buf[SJA1105_MAX_DYN_CMD_SIZE] = {0};
  1126. int rc;
  1127. if (blk_idx >= BLK_IDX_MAX_DYN)
  1128. return -ERANGE;
  1129. ops = &priv->info->dyn_ops[blk_idx];
  1130. if (index >= 0 && index >= ops->max_entry_count)
  1131. return -ERANGE;
  1132. if (index < 0 && !(ops->access & OP_SEARCH))
  1133. return -EOPNOTSUPP;
  1134. if (!(ops->access & OP_READ))
  1135. return -EOPNOTSUPP;
  1136. if (ops->packed_size > SJA1105_MAX_DYN_CMD_SIZE)
  1137. return -ERANGE;
  1138. if (!ops->cmd_packing)
  1139. return -EOPNOTSUPP;
  1140. if (!ops->entry_packing)
  1141. return -EOPNOTSUPP;
  1142. cmd.valid = true; /* Trigger action on table entry */
  1143. cmd.rdwrset = SPI_READ; /* Action is read */
  1144. if (index < 0) {
  1145. /* Avoid copying a signed negative number to an u64 */
  1146. cmd.index = 0;
  1147. cmd.search = true;
  1148. } else {
  1149. cmd.index = index;
  1150. cmd.search = false;
  1151. }
  1152. cmd.valident = true;
  1153. ops->cmd_packing(packed_buf, &cmd, PACK);
  1154. if (cmd.search)
  1155. ops->entry_packing(packed_buf, entry, PACK);
  1156. /* Send SPI write operation: read config table entry */
  1157. mutex_lock(&priv->dynamic_config_lock);
  1158. rc = sja1105_xfer_buf(priv, SPI_WRITE, ops->addr, packed_buf,
  1159. ops->packed_size);
  1160. if (rc < 0)
  1161. goto out;
  1162. rc = sja1105_dynamic_config_wait_complete(priv, ops, entry, true, false);
  1163. out:
  1164. mutex_unlock(&priv->dynamic_config_lock);
  1165. return rc;
  1166. }
  1167. int sja1105_dynamic_config_write(struct sja1105_private *priv,
  1168. enum sja1105_blk_idx blk_idx,
  1169. int index, void *entry, bool keep)
  1170. {
  1171. const struct sja1105_dynamic_table_ops *ops;
  1172. struct sja1105_dyn_cmd cmd = {0};
  1173. /* SPI payload buffer */
  1174. u8 packed_buf[SJA1105_MAX_DYN_CMD_SIZE] = {0};
  1175. int rc;
  1176. if (blk_idx >= BLK_IDX_MAX_DYN)
  1177. return -ERANGE;
  1178. ops = &priv->info->dyn_ops[blk_idx];
  1179. if (index >= ops->max_entry_count)
  1180. return -ERANGE;
  1181. if (index < 0)
  1182. return -ERANGE;
  1183. if (!(ops->access & OP_WRITE))
  1184. return -EOPNOTSUPP;
  1185. if (!keep && !(ops->access & OP_DEL))
  1186. return -EOPNOTSUPP;
  1187. if (ops->packed_size > SJA1105_MAX_DYN_CMD_SIZE)
  1188. return -ERANGE;
  1189. cmd.valident = keep; /* If false, deletes entry */
  1190. cmd.valid = true; /* Trigger action on table entry */
  1191. cmd.rdwrset = SPI_WRITE; /* Action is write */
  1192. cmd.index = index;
  1193. if (!ops->cmd_packing)
  1194. return -EOPNOTSUPP;
  1195. ops->cmd_packing(packed_buf, &cmd, PACK);
  1196. if (!ops->entry_packing)
  1197. return -EOPNOTSUPP;
  1198. /* Don't dereference potentially NULL pointer if just
  1199. * deleting a table entry is what was requested. For cases
  1200. * where 'index' field is physically part of entry structure,
  1201. * and needed here, we deal with that in the cmd_packing callback.
  1202. */
  1203. if (keep)
  1204. ops->entry_packing(packed_buf, entry, PACK);
  1205. /* Send SPI write operation: read config table entry */
  1206. mutex_lock(&priv->dynamic_config_lock);
  1207. rc = sja1105_xfer_buf(priv, SPI_WRITE, ops->addr, packed_buf,
  1208. ops->packed_size);
  1209. if (rc < 0)
  1210. goto out;
  1211. rc = sja1105_dynamic_config_wait_complete(priv, ops, NULL, false, true);
  1212. out:
  1213. mutex_unlock(&priv->dynamic_config_lock);
  1214. return rc;
  1215. }
  1216. static u8 sja1105_crc8_add(u8 crc, u8 byte, u8 poly)
  1217. {
  1218. int i;
  1219. for (i = 0; i < 8; i++) {
  1220. if ((crc ^ byte) & (1 << 7)) {
  1221. crc <<= 1;
  1222. crc ^= poly;
  1223. } else {
  1224. crc <<= 1;
  1225. }
  1226. byte <<= 1;
  1227. }
  1228. return crc;
  1229. }
  1230. /* CRC8 algorithm with non-reversed input, non-reversed output,
  1231. * no input xor and no output xor. Code customized for receiving
  1232. * the SJA1105 E/T FDB keys (vlanid, macaddr) as input. CRC polynomial
  1233. * is also received as argument in the Koopman notation that the switch
  1234. * hardware stores it in.
  1235. */
  1236. u8 sja1105et_fdb_hash(struct sja1105_private *priv, const u8 *addr, u16 vid)
  1237. {
  1238. struct sja1105_l2_lookup_params_entry *l2_lookup_params =
  1239. priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS].entries;
  1240. u64 input, poly_koopman = l2_lookup_params->poly;
  1241. /* Convert polynomial from Koopman to 'normal' notation */
  1242. u8 poly = (u8)(1 + (poly_koopman << 1));
  1243. u8 crc = 0; /* seed */
  1244. int i;
  1245. input = ((u64)vid << 48) | ether_addr_to_u64(addr);
  1246. /* Mask the eight bytes starting from MSB one at a time */
  1247. for (i = 56; i >= 0; i -= 8) {
  1248. u8 byte = (input & (0xffull << i)) >> i;
  1249. crc = sja1105_crc8_add(crc, byte, poly);
  1250. }
  1251. return crc;
  1252. }