dpdmai.c 10 KB

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