Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright 2019 NXP
0003 
0004 #include <linux/module.h>
0005 #include <linux/types.h>
0006 #include <linux/io.h>
0007 #include <linux/fsl/mc.h>
0008 #include "dpdmai.h"
0009 
0010 struct dpdmai_rsp_get_attributes {
0011     __le32 id;
0012     u8 num_of_priorities;
0013     u8 pad0[3];
0014     __le16 major;
0015     __le16 minor;
0016 };
0017 
0018 struct dpdmai_cmd_queue {
0019     __le32 dest_id;
0020     u8 priority;
0021     u8 queue;
0022     u8 dest_type;
0023     u8 pad;
0024     __le64 user_ctx;
0025     union {
0026         __le32 options;
0027         __le32 fqid;
0028     };
0029 };
0030 
0031 struct dpdmai_rsp_get_tx_queue {
0032     __le64 pad;
0033     __le32 fqid;
0034 };
0035 
0036 #define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \
0037     ((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg))
0038 
0039 /* cmd, param, offset, width, type, arg_name */
0040 #define DPDMAI_CMD_CREATE(cmd, cfg) \
0041 do { \
0042     MC_CMD_OP(cmd, 0, 8,  8,  u8,  (cfg)->priorities[0]);\
0043     MC_CMD_OP(cmd, 0, 16, 8,  u8,  (cfg)->priorities[1]);\
0044 } while (0)
0045 
0046 static inline u64 mc_enc(int lsoffset, int width, u64 val)
0047 {
0048     return (val & MAKE_UMASK64(width)) << lsoffset;
0049 }
0050 
0051 /**
0052  * dpdmai_open() - Open a control session for the specified object
0053  * @mc_io:  Pointer to MC portal's I/O object
0054  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
0055  * @dpdmai_id:  DPDMAI unique ID
0056  * @token:  Returned token; use in subsequent API calls
0057  *
0058  * This function can be used to open a control session for an
0059  * already created object; an object may have been declared in
0060  * the DPL or by calling the dpdmai_create() function.
0061  * This function returns a unique authentication token,
0062  * associated with the specific object ID and the specific MC
0063  * portal; this token must be used in all subsequent commands for
0064  * this specific object.
0065  *
0066  * Return:  '0' on Success; Error code otherwise.
0067  */
0068 int dpdmai_open(struct fsl_mc_io *mc_io, u32 cmd_flags,
0069         int dpdmai_id, u16 *token)
0070 {
0071     struct fsl_mc_command cmd = { 0 };
0072     __le64 *cmd_dpdmai_id;
0073     int err;
0074 
0075     /* prepare command */
0076     cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_OPEN,
0077                       cmd_flags, 0);
0078 
0079     cmd_dpdmai_id = cmd.params;
0080     *cmd_dpdmai_id = cpu_to_le32(dpdmai_id);
0081 
0082     /* send command to mc*/
0083     err = mc_send_command(mc_io, &cmd);
0084     if (err)
0085         return err;
0086 
0087     /* retrieve response parameters */
0088     *token = mc_cmd_hdr_read_token(&cmd);
0089 
0090     return 0;
0091 }
0092 EXPORT_SYMBOL_GPL(dpdmai_open);
0093 
0094 /**
0095  * dpdmai_close() - Close the control session of the object
0096  * @mc_io:  Pointer to MC portal's I/O object
0097  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
0098  * @token:  Token of DPDMAI object
0099  *
0100  * After this function is called, no further operations are
0101  * allowed on the object without opening a new control session.
0102  *
0103  * Return:  '0' on Success; Error code otherwise.
0104  */
0105 int dpdmai_close(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
0106 {
0107     struct fsl_mc_command cmd = { 0 };
0108 
0109     /* prepare command */
0110     cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_CLOSE,
0111                       cmd_flags, token);
0112 
0113     /* send command to mc*/
0114     return mc_send_command(mc_io, &cmd);
0115 }
0116 EXPORT_SYMBOL_GPL(dpdmai_close);
0117 
0118 /**
0119  * dpdmai_create() - Create the DPDMAI object
0120  * @mc_io:  Pointer to MC portal's I/O object
0121  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
0122  * @cfg:    Configuration structure
0123  * @token:  Returned token; use in subsequent API calls
0124  *
0125  * Create the DPDMAI object, allocate required resources and
0126  * perform required initialization.
0127  *
0128  * The object can be created either by declaring it in the
0129  * DPL file, or by calling this function.
0130  *
0131  * This function returns a unique authentication token,
0132  * associated with the specific object ID and the specific MC
0133  * portal; this token must be used in all subsequent calls to
0134  * this specific object. For objects that are created using the
0135  * DPL file, call dpdmai_open() function to get an authentication
0136  * token first.
0137  *
0138  * Return:  '0' on Success; Error code otherwise.
0139  */
0140 int dpdmai_create(struct fsl_mc_io *mc_io, u32 cmd_flags,
0141           const struct dpdmai_cfg *cfg, u16 *token)
0142 {
0143     struct fsl_mc_command cmd = { 0 };
0144     int err;
0145 
0146     /* prepare command */
0147     cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_CREATE,
0148                       cmd_flags, 0);
0149     DPDMAI_CMD_CREATE(cmd, cfg);
0150 
0151     /* send command to mc*/
0152     err = mc_send_command(mc_io, &cmd);
0153     if (err)
0154         return err;
0155 
0156     /* retrieve response parameters */
0157     *token = mc_cmd_hdr_read_token(&cmd);
0158 
0159     return 0;
0160 }
0161 
0162 /**
0163  * dpdmai_destroy() - Destroy the DPDMAI object and release all its resources.
0164  * @mc_io:      Pointer to MC portal's I/O object
0165  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
0166  * @token:      Token of DPDMAI object
0167  *
0168  * Return:      '0' on Success; error code otherwise.
0169  */
0170 int dpdmai_destroy(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
0171 {
0172     struct fsl_mc_command cmd = { 0 };
0173 
0174     /* prepare command */
0175     cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_DESTROY,
0176                       cmd_flags, token);
0177 
0178     /* send command to mc*/
0179     return mc_send_command(mc_io, &cmd);
0180 }
0181 EXPORT_SYMBOL_GPL(dpdmai_destroy);
0182 
0183 /**
0184  * dpdmai_enable() - Enable the DPDMAI, allow sending and receiving frames.
0185  * @mc_io:  Pointer to MC portal's I/O object
0186  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
0187  * @token:  Token of DPDMAI object
0188  *
0189  * Return:  '0' on Success; Error code otherwise.
0190  */
0191 int dpdmai_enable(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
0192 {
0193     struct fsl_mc_command cmd = { 0 };
0194 
0195     /* prepare command */
0196     cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_ENABLE,
0197                       cmd_flags, token);
0198 
0199     /* send command to mc*/
0200     return mc_send_command(mc_io, &cmd);
0201 }
0202 EXPORT_SYMBOL_GPL(dpdmai_enable);
0203 
0204 /**
0205  * dpdmai_disable() - Disable the DPDMAI, stop sending and receiving frames.
0206  * @mc_io:  Pointer to MC portal's I/O object
0207  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
0208  * @token:  Token of DPDMAI object
0209  *
0210  * Return:  '0' on Success; Error code otherwise.
0211  */
0212 int dpdmai_disable(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
0213 {
0214     struct fsl_mc_command cmd = { 0 };
0215 
0216     /* prepare command */
0217     cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_DISABLE,
0218                       cmd_flags, token);
0219 
0220     /* send command to mc*/
0221     return mc_send_command(mc_io, &cmd);
0222 }
0223 EXPORT_SYMBOL_GPL(dpdmai_disable);
0224 
0225 /**
0226  * dpdmai_reset() - Reset the DPDMAI, returns the object to initial state.
0227  * @mc_io:  Pointer to MC portal's I/O object
0228  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
0229  * @token:  Token of DPDMAI object
0230  *
0231  * Return:  '0' on Success; Error code otherwise.
0232  */
0233 int dpdmai_reset(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
0234 {
0235     struct fsl_mc_command cmd = { 0 };
0236 
0237     /* prepare command */
0238     cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_RESET,
0239                       cmd_flags, token);
0240 
0241     /* send command to mc*/
0242     return mc_send_command(mc_io, &cmd);
0243 }
0244 EXPORT_SYMBOL_GPL(dpdmai_reset);
0245 
0246 /**
0247  * dpdmai_get_attributes() - Retrieve DPDMAI attributes.
0248  * @mc_io:  Pointer to MC portal's I/O object
0249  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
0250  * @token:  Token of DPDMAI object
0251  * @attr:   Returned object's attributes
0252  *
0253  * Return:  '0' on Success; Error code otherwise.
0254  */
0255 int dpdmai_get_attributes(struct fsl_mc_io *mc_io, u32 cmd_flags,
0256               u16 token, struct dpdmai_attr *attr)
0257 {
0258     struct dpdmai_rsp_get_attributes *rsp_params;
0259     struct fsl_mc_command cmd = { 0 };
0260     int err;
0261 
0262     /* prepare command */
0263     cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_ATTR,
0264                       cmd_flags, token);
0265 
0266     /* send command to mc*/
0267     err = mc_send_command(mc_io, &cmd);
0268     if (err)
0269         return err;
0270 
0271     /* retrieve response parameters */
0272     rsp_params = (struct dpdmai_rsp_get_attributes *)cmd.params;
0273     attr->id = le32_to_cpu(rsp_params->id);
0274     attr->version.major = le16_to_cpu(rsp_params->major);
0275     attr->version.minor = le16_to_cpu(rsp_params->minor);
0276     attr->num_of_priorities = rsp_params->num_of_priorities;
0277 
0278     return 0;
0279 }
0280 EXPORT_SYMBOL_GPL(dpdmai_get_attributes);
0281 
0282 /**
0283  * dpdmai_set_rx_queue() - Set Rx queue configuration
0284  * @mc_io:  Pointer to MC portal's I/O object
0285  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
0286  * @token:  Token of DPDMAI object
0287  * @priority:   Select the queue relative to number of
0288  *      priorities configured at DPDMAI creation
0289  * @cfg:    Rx queue configuration
0290  *
0291  * Return:  '0' on Success; Error code otherwise.
0292  */
0293 int dpdmai_set_rx_queue(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
0294             u8 priority, const struct dpdmai_rx_queue_cfg *cfg)
0295 {
0296     struct dpdmai_cmd_queue *cmd_params;
0297     struct fsl_mc_command cmd = { 0 };
0298 
0299     /* prepare command */
0300     cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_SET_RX_QUEUE,
0301                       cmd_flags, token);
0302 
0303     cmd_params = (struct dpdmai_cmd_queue *)cmd.params;
0304     cmd_params->dest_id = cpu_to_le32(cfg->dest_cfg.dest_id);
0305     cmd_params->priority = cfg->dest_cfg.priority;
0306     cmd_params->queue = priority;
0307     cmd_params->dest_type = cfg->dest_cfg.dest_type;
0308     cmd_params->user_ctx = cpu_to_le64(cfg->user_ctx);
0309     cmd_params->options = cpu_to_le32(cfg->options);
0310 
0311     /* send command to mc*/
0312     return mc_send_command(mc_io, &cmd);
0313 }
0314 EXPORT_SYMBOL_GPL(dpdmai_set_rx_queue);
0315 
0316 /**
0317  * dpdmai_get_rx_queue() - Retrieve Rx queue attributes.
0318  * @mc_io:  Pointer to MC portal's I/O object
0319  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
0320  * @token:  Token of DPDMAI object
0321  * @priority:   Select the queue relative to number of
0322  *              priorities configured at DPDMAI creation
0323  * @attr:   Returned Rx queue attributes
0324  *
0325  * Return:  '0' on Success; Error code otherwise.
0326  */
0327 int dpdmai_get_rx_queue(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
0328             u8 priority, struct dpdmai_rx_queue_attr *attr)
0329 {
0330     struct dpdmai_cmd_queue *cmd_params;
0331     struct fsl_mc_command cmd = { 0 };
0332     int err;
0333 
0334     /* prepare command */
0335     cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_RX_QUEUE,
0336                       cmd_flags, token);
0337 
0338     cmd_params = (struct dpdmai_cmd_queue *)cmd.params;
0339     cmd_params->queue = priority;
0340 
0341     /* send command to mc*/
0342     err = mc_send_command(mc_io, &cmd);
0343     if (err)
0344         return err;
0345 
0346     /* retrieve response parameters */
0347     attr->dest_cfg.dest_id = le32_to_cpu(cmd_params->dest_id);
0348     attr->dest_cfg.priority = cmd_params->priority;
0349     attr->dest_cfg.dest_type = cmd_params->dest_type;
0350     attr->user_ctx = le64_to_cpu(cmd_params->user_ctx);
0351     attr->fqid = le32_to_cpu(cmd_params->fqid);
0352 
0353     return 0;
0354 }
0355 EXPORT_SYMBOL_GPL(dpdmai_get_rx_queue);
0356 
0357 /**
0358  * dpdmai_get_tx_queue() - Retrieve Tx queue attributes.
0359  * @mc_io:  Pointer to MC portal's I/O object
0360  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
0361  * @token:  Token of DPDMAI object
0362  * @priority:   Select the queue relative to number of
0363  *          priorities configured at DPDMAI creation
0364  * @fqid:   Returned Tx queue
0365  *
0366  * Return:  '0' on Success; Error code otherwise.
0367  */
0368 int dpdmai_get_tx_queue(struct fsl_mc_io *mc_io, u32 cmd_flags,
0369             u16 token, u8 priority, u32 *fqid)
0370 {
0371     struct dpdmai_rsp_get_tx_queue *rsp_params;
0372     struct dpdmai_cmd_queue *cmd_params;
0373     struct fsl_mc_command cmd = { 0 };
0374     int err;
0375 
0376     /* prepare command */
0377     cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_TX_QUEUE,
0378                       cmd_flags, token);
0379 
0380     cmd_params = (struct dpdmai_cmd_queue *)cmd.params;
0381     cmd_params->queue = priority;
0382 
0383     /* send command to mc*/
0384     err = mc_send_command(mc_io, &cmd);
0385     if (err)
0386         return err;
0387 
0388     /* retrieve response parameters */
0389 
0390     rsp_params = (struct dpdmai_rsp_get_tx_queue *)cmd.params;
0391     *fqid = le32_to_cpu(rsp_params->fqid);
0392 
0393     return 0;
0394 }
0395 EXPORT_SYMBOL_GPL(dpdmai_get_tx_queue);
0396 
0397 MODULE_LICENSE("GPL v2");