dpmac.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /* Copyright 2013-2016 Freescale Semiconductor Inc.
  3. * Copyright 2019 NXP
  4. */
  5. #include <linux/fsl/mc.h>
  6. #include "dpmac.h"
  7. #include "dpmac-cmd.h"
  8. /**
  9. * dpmac_open() - Open a control session for the specified object.
  10. * @mc_io: Pointer to MC portal's I/O object
  11. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  12. * @dpmac_id: DPMAC unique ID
  13. * @token: Returned token; use in subsequent API calls
  14. *
  15. * This function can be used to open a control session for an
  16. * already created object; an object may have been declared in
  17. * the DPL or by calling the dpmac_create function.
  18. * This function returns a unique authentication token,
  19. * associated with the specific object ID and the specific MC
  20. * portal; this token must be used in all subsequent commands for
  21. * this specific object
  22. *
  23. * Return: '0' on Success; Error code otherwise.
  24. */
  25. int dpmac_open(struct fsl_mc_io *mc_io,
  26. u32 cmd_flags,
  27. int dpmac_id,
  28. u16 *token)
  29. {
  30. struct dpmac_cmd_open *cmd_params;
  31. struct fsl_mc_command cmd = { 0 };
  32. int err;
  33. /* prepare command */
  34. cmd.header = mc_encode_cmd_header(DPMAC_CMDID_OPEN,
  35. cmd_flags,
  36. 0);
  37. cmd_params = (struct dpmac_cmd_open *)cmd.params;
  38. cmd_params->dpmac_id = cpu_to_le32(dpmac_id);
  39. /* send command to mc*/
  40. err = mc_send_command(mc_io, &cmd);
  41. if (err)
  42. return err;
  43. /* retrieve response parameters */
  44. *token = mc_cmd_hdr_read_token(&cmd);
  45. return err;
  46. }
  47. /**
  48. * dpmac_close() - Close the control session of the 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. * @token: Token of DPMAC object
  52. *
  53. * After this function is called, no further operations are
  54. * allowed on the object without opening a new control session.
  55. *
  56. * Return: '0' on Success; Error code otherwise.
  57. */
  58. int dpmac_close(struct fsl_mc_io *mc_io,
  59. u32 cmd_flags,
  60. u16 token)
  61. {
  62. struct fsl_mc_command cmd = { 0 };
  63. /* prepare command */
  64. cmd.header = mc_encode_cmd_header(DPMAC_CMDID_CLOSE, cmd_flags,
  65. token);
  66. /* send command to mc*/
  67. return mc_send_command(mc_io, &cmd);
  68. }
  69. /**
  70. * dpmac_get_attributes - Retrieve DPMAC attributes.
  71. *
  72. * @mc_io: Pointer to MC portal's I/O object
  73. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  74. * @token: Token of DPMAC object
  75. * @attr: Returned object's attributes
  76. *
  77. * Return: '0' on Success; Error code otherwise.
  78. */
  79. int dpmac_get_attributes(struct fsl_mc_io *mc_io,
  80. u32 cmd_flags,
  81. u16 token,
  82. struct dpmac_attr *attr)
  83. {
  84. struct dpmac_rsp_get_attributes *rsp_params;
  85. struct fsl_mc_command cmd = { 0 };
  86. int err;
  87. /* prepare command */
  88. cmd.header = mc_encode_cmd_header(DPMAC_CMDID_GET_ATTR,
  89. cmd_flags,
  90. token);
  91. /* send command to mc*/
  92. err = mc_send_command(mc_io, &cmd);
  93. if (err)
  94. return err;
  95. /* retrieve response parameters */
  96. rsp_params = (struct dpmac_rsp_get_attributes *)cmd.params;
  97. attr->eth_if = rsp_params->eth_if;
  98. attr->link_type = rsp_params->link_type;
  99. attr->id = le16_to_cpu(rsp_params->id);
  100. attr->max_rate = le32_to_cpu(rsp_params->max_rate);
  101. return 0;
  102. }
  103. /**
  104. * dpmac_set_link_state() - Set the Ethernet link status
  105. * @mc_io: Pointer to opaque I/O object
  106. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  107. * @token: Token of DPMAC object
  108. * @link_state: Link state configuration
  109. *
  110. * Return: '0' on Success; Error code otherwise.
  111. */
  112. int dpmac_set_link_state(struct fsl_mc_io *mc_io,
  113. u32 cmd_flags,
  114. u16 token,
  115. struct dpmac_link_state *link_state)
  116. {
  117. struct dpmac_cmd_set_link_state *cmd_params;
  118. struct fsl_mc_command cmd = { 0 };
  119. /* prepare command */
  120. cmd.header = mc_encode_cmd_header(DPMAC_CMDID_SET_LINK_STATE,
  121. cmd_flags,
  122. token);
  123. cmd_params = (struct dpmac_cmd_set_link_state *)cmd.params;
  124. cmd_params->options = cpu_to_le64(link_state->options);
  125. cmd_params->rate = cpu_to_le32(link_state->rate);
  126. dpmac_set_field(cmd_params->state, STATE, link_state->up);
  127. dpmac_set_field(cmd_params->state, STATE_VALID,
  128. link_state->state_valid);
  129. cmd_params->supported = cpu_to_le64(link_state->supported);
  130. cmd_params->advertising = cpu_to_le64(link_state->advertising);
  131. /* send command to mc*/
  132. return mc_send_command(mc_io, &cmd);
  133. }
  134. /**
  135. * dpmac_get_counter() - Read a specific DPMAC counter
  136. * @mc_io: Pointer to opaque I/O object
  137. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  138. * @token: Token of DPMAC object
  139. * @id: The requested counter ID
  140. * @value: Returned counter value
  141. *
  142. * Return: The requested counter; '0' otherwise.
  143. */
  144. int dpmac_get_counter(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
  145. enum dpmac_counter_id id, u64 *value)
  146. {
  147. struct dpmac_cmd_get_counter *dpmac_cmd;
  148. struct dpmac_rsp_get_counter *dpmac_rsp;
  149. struct fsl_mc_command cmd = { 0 };
  150. int err = 0;
  151. cmd.header = mc_encode_cmd_header(DPMAC_CMDID_GET_COUNTER,
  152. cmd_flags,
  153. token);
  154. dpmac_cmd = (struct dpmac_cmd_get_counter *)cmd.params;
  155. dpmac_cmd->id = id;
  156. err = mc_send_command(mc_io, &cmd);
  157. if (err)
  158. return err;
  159. dpmac_rsp = (struct dpmac_rsp_get_counter *)cmd.params;
  160. *value = le64_to_cpu(dpmac_rsp->counter);
  161. return 0;
  162. }
  163. /**
  164. * dpmac_get_api_version() - Get Data Path MAC version
  165. * @mc_io: Pointer to MC portal's I/O object
  166. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  167. * @major_ver: Major version of data path mac API
  168. * @minor_ver: Minor version of data path mac API
  169. *
  170. * Return: '0' on Success; Error code otherwise.
  171. */
  172. int dpmac_get_api_version(struct fsl_mc_io *mc_io, u32 cmd_flags,
  173. u16 *major_ver, u16 *minor_ver)
  174. {
  175. struct dpmac_rsp_get_api_version *rsp_params;
  176. struct fsl_mc_command cmd = { 0 };
  177. int err;
  178. cmd.header = mc_encode_cmd_header(DPMAC_CMDID_GET_API_VERSION,
  179. cmd_flags,
  180. 0);
  181. err = mc_send_command(mc_io, &cmd);
  182. if (err)
  183. return err;
  184. rsp_params = (struct dpmac_rsp_get_api_version *)cmd.params;
  185. *major_ver = le16_to_cpu(rsp_params->major);
  186. *minor_ver = le16_to_cpu(rsp_params->minor);
  187. return 0;
  188. }
  189. /**
  190. * dpmac_set_protocol() - Reconfigure the DPMAC protocol
  191. * @mc_io: Pointer to opaque I/O object
  192. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  193. * @token: Token of DPMAC object
  194. * @protocol: New protocol for the DPMAC to be reconfigured in.
  195. *
  196. * Return: '0' on Success; Error code otherwise.
  197. */
  198. int dpmac_set_protocol(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
  199. enum dpmac_eth_if protocol)
  200. {
  201. struct dpmac_cmd_set_protocol *cmd_params;
  202. struct fsl_mc_command cmd = { 0 };
  203. cmd.header = mc_encode_cmd_header(DPMAC_CMDID_SET_PROTOCOL,
  204. cmd_flags, token);
  205. cmd_params = (struct dpmac_cmd_set_protocol *)cmd.params;
  206. cmd_params->eth_if = protocol;
  207. return mc_send_command(mc_io, &cmd);
  208. }