dpdmai.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright 2019 NXP
  3. #include <linux/module.h>
  4. #include <linux/types.h>
  5. #include <linux/io.h>
  6. #include <linux/fsl/mc.h>
  7. #include "dpdmai.h"
  8. struct dpdmai_rsp_get_attributes {
  9. __le32 id;
  10. u8 num_of_priorities;
  11. u8 pad0[3];
  12. __le16 major;
  13. __le16 minor;
  14. };
  15. struct dpdmai_cmd_queue {
  16. __le32 dest_id;
  17. u8 priority;
  18. u8 queue;
  19. u8 dest_type;
  20. u8 pad;
  21. __le64 user_ctx;
  22. union {
  23. __le32 options;
  24. __le32 fqid;
  25. };
  26. };
  27. struct dpdmai_rsp_get_tx_queue {
  28. __le64 pad;
  29. __le32 fqid;
  30. };
  31. #define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \
  32. ((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg))
  33. /* cmd, param, offset, width, type, arg_name */
  34. #define DPDMAI_CMD_CREATE(cmd, cfg) \
  35. do { \
  36. MC_CMD_OP(cmd, 0, 8, 8, u8, (cfg)->priorities[0]);\
  37. MC_CMD_OP(cmd, 0, 16, 8, u8, (cfg)->priorities[1]);\
  38. } while (0)
  39. static inline u64 mc_enc(int lsoffset, int width, u64 val)
  40. {
  41. return (val & MAKE_UMASK64(width)) << lsoffset;
  42. }
  43. /**
  44. * dpdmai_open() - Open a control session for the specified object
  45. * @mc_io: Pointer to MC portal's I/O object
  46. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  47. * @dpdmai_id: DPDMAI unique ID
  48. * @token: Returned token; use in subsequent API calls
  49. *
  50. * This function can be used to open a control session for an
  51. * already created object; an object may have been declared in
  52. * the DPL or by calling the dpdmai_create() function.
  53. * This function returns a unique authentication token,
  54. * associated with the specific object ID and the specific MC
  55. * portal; this token must be used in all subsequent commands for
  56. * this specific object.
  57. *
  58. * Return: '0' on Success; Error code otherwise.
  59. */
  60. int dpdmai_open(struct fsl_mc_io *mc_io, u32 cmd_flags,
  61. int dpdmai_id, u16 *token)
  62. {
  63. struct fsl_mc_command cmd = { 0 };
  64. __le64 *cmd_dpdmai_id;
  65. int err;
  66. /* prepare command */
  67. cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_OPEN,
  68. cmd_flags, 0);
  69. cmd_dpdmai_id = cmd.params;
  70. *cmd_dpdmai_id = cpu_to_le32(dpdmai_id);
  71. /* send command to mc*/
  72. err = mc_send_command(mc_io, &cmd);
  73. if (err)
  74. return err;
  75. /* retrieve response parameters */
  76. *token = mc_cmd_hdr_read_token(&cmd);
  77. return 0;
  78. }
  79. EXPORT_SYMBOL_GPL(dpdmai_open);
  80. /**
  81. * dpdmai_close() - Close the control session of the object
  82. * @mc_io: Pointer to MC portal's I/O object
  83. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  84. * @token: Token of DPDMAI object
  85. *
  86. * After this function is called, no further operations are
  87. * allowed on the object without opening a new control session.
  88. *
  89. * Return: '0' on Success; Error code otherwise.
  90. */
  91. int dpdmai_close(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
  92. {
  93. struct fsl_mc_command cmd = { 0 };
  94. /* prepare command */
  95. cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_CLOSE,
  96. cmd_flags, token);
  97. /* send command to mc*/
  98. return mc_send_command(mc_io, &cmd);
  99. }
  100. EXPORT_SYMBOL_GPL(dpdmai_close);
  101. /**
  102. * dpdmai_create() - Create the DPDMAI object
  103. * @mc_io: Pointer to MC portal's I/O object
  104. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  105. * @cfg: Configuration structure
  106. * @token: Returned token; use in subsequent API calls
  107. *
  108. * Create the DPDMAI object, allocate required resources and
  109. * perform required initialization.
  110. *
  111. * The object can be created either by declaring it in the
  112. * DPL file, or by calling this function.
  113. *
  114. * This function returns a unique authentication token,
  115. * associated with the specific object ID and the specific MC
  116. * portal; this token must be used in all subsequent calls to
  117. * this specific object. For objects that are created using the
  118. * DPL file, call dpdmai_open() function to get an authentication
  119. * token first.
  120. *
  121. * Return: '0' on Success; Error code otherwise.
  122. */
  123. int dpdmai_create(struct fsl_mc_io *mc_io, u32 cmd_flags,
  124. const struct dpdmai_cfg *cfg, u16 *token)
  125. {
  126. struct fsl_mc_command cmd = { 0 };
  127. int err;
  128. /* prepare command */
  129. cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_CREATE,
  130. cmd_flags, 0);
  131. DPDMAI_CMD_CREATE(cmd, cfg);
  132. /* send command to mc*/
  133. err = mc_send_command(mc_io, &cmd);
  134. if (err)
  135. return err;
  136. /* retrieve response parameters */
  137. *token = mc_cmd_hdr_read_token(&cmd);
  138. return 0;
  139. }
  140. /**
  141. * dpdmai_destroy() - Destroy the DPDMAI object and release all its resources.
  142. * @mc_io: Pointer to MC portal's I/O object
  143. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  144. * @token: Token of DPDMAI object
  145. *
  146. * Return: '0' on Success; error code otherwise.
  147. */
  148. int dpdmai_destroy(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
  149. {
  150. struct fsl_mc_command cmd = { 0 };
  151. /* prepare command */
  152. cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_DESTROY,
  153. cmd_flags, token);
  154. /* send command to mc*/
  155. return mc_send_command(mc_io, &cmd);
  156. }
  157. EXPORT_SYMBOL_GPL(dpdmai_destroy);
  158. /**
  159. * dpdmai_enable() - Enable the DPDMAI, allow sending and receiving frames.
  160. * @mc_io: Pointer to MC portal's I/O object
  161. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  162. * @token: Token of DPDMAI object
  163. *
  164. * Return: '0' on Success; Error code otherwise.
  165. */
  166. int dpdmai_enable(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
  167. {
  168. struct fsl_mc_command cmd = { 0 };
  169. /* prepare command */
  170. cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_ENABLE,
  171. cmd_flags, token);
  172. /* send command to mc*/
  173. return mc_send_command(mc_io, &cmd);
  174. }
  175. EXPORT_SYMBOL_GPL(dpdmai_enable);
  176. /**
  177. * dpdmai_disable() - Disable the DPDMAI, stop sending and receiving frames.
  178. * @mc_io: Pointer to MC portal's I/O object
  179. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  180. * @token: Token of DPDMAI object
  181. *
  182. * Return: '0' on Success; Error code otherwise.
  183. */
  184. int dpdmai_disable(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
  185. {
  186. struct fsl_mc_command cmd = { 0 };
  187. /* prepare command */
  188. cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_DISABLE,
  189. cmd_flags, token);
  190. /* send command to mc*/
  191. return mc_send_command(mc_io, &cmd);
  192. }
  193. EXPORT_SYMBOL_GPL(dpdmai_disable);
  194. /**
  195. * dpdmai_reset() - Reset the DPDMAI, returns the object to initial state.
  196. * @mc_io: Pointer to MC portal's I/O object
  197. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  198. * @token: Token of DPDMAI object
  199. *
  200. * Return: '0' on Success; Error code otherwise.
  201. */
  202. int dpdmai_reset(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
  203. {
  204. struct fsl_mc_command cmd = { 0 };
  205. /* prepare command */
  206. cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_RESET,
  207. cmd_flags, token);
  208. /* send command to mc*/
  209. return mc_send_command(mc_io, &cmd);
  210. }
  211. EXPORT_SYMBOL_GPL(dpdmai_reset);
  212. /**
  213. * dpdmai_get_attributes() - Retrieve DPDMAI attributes.
  214. * @mc_io: Pointer to MC portal's I/O object
  215. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  216. * @token: Token of DPDMAI object
  217. * @attr: Returned object's attributes
  218. *
  219. * Return: '0' on Success; Error code otherwise.
  220. */
  221. int dpdmai_get_attributes(struct fsl_mc_io *mc_io, u32 cmd_flags,
  222. u16 token, struct dpdmai_attr *attr)
  223. {
  224. struct dpdmai_rsp_get_attributes *rsp_params;
  225. struct fsl_mc_command cmd = { 0 };
  226. int err;
  227. /* prepare command */
  228. cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_ATTR,
  229. cmd_flags, token);
  230. /* send command to mc*/
  231. err = mc_send_command(mc_io, &cmd);
  232. if (err)
  233. return err;
  234. /* retrieve response parameters */
  235. rsp_params = (struct dpdmai_rsp_get_attributes *)cmd.params;
  236. attr->id = le32_to_cpu(rsp_params->id);
  237. attr->version.major = le16_to_cpu(rsp_params->major);
  238. attr->version.minor = le16_to_cpu(rsp_params->minor);
  239. attr->num_of_priorities = rsp_params->num_of_priorities;
  240. return 0;
  241. }
  242. EXPORT_SYMBOL_GPL(dpdmai_get_attributes);
  243. /**
  244. * dpdmai_set_rx_queue() - Set Rx queue configuration
  245. * @mc_io: Pointer to MC portal's I/O object
  246. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  247. * @token: Token of DPDMAI object
  248. * @priority: Select the queue relative to number of
  249. * priorities configured at DPDMAI creation
  250. * @cfg: Rx queue configuration
  251. *
  252. * Return: '0' on Success; Error code otherwise.
  253. */
  254. int dpdmai_set_rx_queue(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
  255. u8 priority, const struct dpdmai_rx_queue_cfg *cfg)
  256. {
  257. struct dpdmai_cmd_queue *cmd_params;
  258. struct fsl_mc_command cmd = { 0 };
  259. /* prepare command */
  260. cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_SET_RX_QUEUE,
  261. cmd_flags, token);
  262. cmd_params = (struct dpdmai_cmd_queue *)cmd.params;
  263. cmd_params->dest_id = cpu_to_le32(cfg->dest_cfg.dest_id);
  264. cmd_params->priority = cfg->dest_cfg.priority;
  265. cmd_params->queue = priority;
  266. cmd_params->dest_type = cfg->dest_cfg.dest_type;
  267. cmd_params->user_ctx = cpu_to_le64(cfg->user_ctx);
  268. cmd_params->options = cpu_to_le32(cfg->options);
  269. /* send command to mc*/
  270. return mc_send_command(mc_io, &cmd);
  271. }
  272. EXPORT_SYMBOL_GPL(dpdmai_set_rx_queue);
  273. /**
  274. * dpdmai_get_rx_queue() - Retrieve Rx queue attributes.
  275. * @mc_io: Pointer to MC portal's I/O object
  276. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  277. * @token: Token of DPDMAI object
  278. * @priority: Select the queue relative to number of
  279. * priorities configured at DPDMAI creation
  280. * @attr: Returned Rx queue attributes
  281. *
  282. * Return: '0' on Success; Error code otherwise.
  283. */
  284. int dpdmai_get_rx_queue(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
  285. u8 priority, struct dpdmai_rx_queue_attr *attr)
  286. {
  287. struct dpdmai_cmd_queue *cmd_params;
  288. struct fsl_mc_command cmd = { 0 };
  289. int err;
  290. /* prepare command */
  291. cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_RX_QUEUE,
  292. cmd_flags, token);
  293. cmd_params = (struct dpdmai_cmd_queue *)cmd.params;
  294. cmd_params->queue = priority;
  295. /* send command to mc*/
  296. err = mc_send_command(mc_io, &cmd);
  297. if (err)
  298. return err;
  299. /* retrieve response parameters */
  300. attr->dest_cfg.dest_id = le32_to_cpu(cmd_params->dest_id);
  301. attr->dest_cfg.priority = cmd_params->priority;
  302. attr->dest_cfg.dest_type = cmd_params->dest_type;
  303. attr->user_ctx = le64_to_cpu(cmd_params->user_ctx);
  304. attr->fqid = le32_to_cpu(cmd_params->fqid);
  305. return 0;
  306. }
  307. EXPORT_SYMBOL_GPL(dpdmai_get_rx_queue);
  308. /**
  309. * dpdmai_get_tx_queue() - Retrieve Tx queue attributes.
  310. * @mc_io: Pointer to MC portal's I/O object
  311. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  312. * @token: Token of DPDMAI object
  313. * @priority: Select the queue relative to number of
  314. * priorities configured at DPDMAI creation
  315. * @fqid: Returned Tx queue
  316. *
  317. * Return: '0' on Success; Error code otherwise.
  318. */
  319. int dpdmai_get_tx_queue(struct fsl_mc_io *mc_io, u32 cmd_flags,
  320. u16 token, u8 priority, u32 *fqid)
  321. {
  322. struct dpdmai_rsp_get_tx_queue *rsp_params;
  323. struct dpdmai_cmd_queue *cmd_params;
  324. struct fsl_mc_command cmd = { 0 };
  325. int err;
  326. /* prepare command */
  327. cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_TX_QUEUE,
  328. cmd_flags, token);
  329. cmd_params = (struct dpdmai_cmd_queue *)cmd.params;
  330. cmd_params->queue = priority;
  331. /* send command to mc*/
  332. err = mc_send_command(mc_io, &cmd);
  333. if (err)
  334. return err;
  335. /* retrieve response parameters */
  336. rsp_params = (struct dpdmai_rsp_get_tx_queue *)cmd.params;
  337. *fqid = le32_to_cpu(rsp_params->fqid);
  338. return 0;
  339. }
  340. EXPORT_SYMBOL_GPL(dpdmai_get_tx_queue);
  341. MODULE_LICENSE("GPL v2");