1 /*
2  * Copyright (C) 2015 Mellanox Technologies Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 FILE_LICENCE ( GPL2_OR_LATER );
21 
22 #include "../../mlx_lib/mlx_reg_access/mlx_reg_access.h"
23 #include "../../include/public/mlx_icmd.h"
24 #include "../../include/public/mlx_bail.h"
25 #include "../../include/public/mlx_memory.h"
26 
27 static
28 mlx_status
init_operation_tlv(IN struct mail_box_tlv * mail_box_tlv,IN mlx_uint16 reg_id,IN REG_ACCESS_OPT reg_opt)29 init_operation_tlv(
30 		IN struct mail_box_tlv *mail_box_tlv,
31 		IN mlx_uint16  reg_id,
32 		IN REG_ACCESS_OPT reg_opt
33 		)
34 {
35 #define TLV_OPERATION 1
36 	mail_box_tlv->operation_tlv.Type			= TLV_OPERATION;
37 #define MAD_CLASS_REG_ACCESS 1
38 	mail_box_tlv->operation_tlv.cls			= MAD_CLASS_REG_ACCESS;
39 #define TLV_OPERATION_SIZE 4
40 	mail_box_tlv->operation_tlv.len			= TLV_OPERATION_SIZE;
41 	mail_box_tlv->operation_tlv.method			= reg_opt;
42 	mail_box_tlv->operation_tlv.register_id	= reg_id;
43 	return MLX_SUCCESS;
44 }
45 
46 mlx_status
mlx_reg_access(IN mlx_utils * utils,IN mlx_uint16 reg_id,IN REG_ACCESS_OPT reg_opt,IN OUT mlx_void * reg_data,IN mlx_size reg_size,OUT mlx_uint32 * reg_status)47 mlx_reg_access(
48 		IN mlx_utils *utils,
49 		IN mlx_uint16  reg_id,
50 		IN REG_ACCESS_OPT reg_opt,
51 		IN OUT mlx_void	*reg_data,
52         IN mlx_size reg_size,
53         OUT mlx_uint32 *reg_status
54 		)
55 {
56 	mlx_status status = MLX_SUCCESS;
57 	struct mail_box_tlv mail_box_tlv;
58 
59 	if (utils == NULL || reg_data == NULL || reg_status == NULL
60 			|| reg_size > REG_ACCESS_MAX_REG_SIZE) {
61 		status = MLX_INVALID_PARAMETER;
62 		goto bad_param;
63 	}
64 
65 	mlx_memory_set(utils, &mail_box_tlv, 0, sizeof(mail_box_tlv));
66 
67 	init_operation_tlv(&mail_box_tlv, reg_id, reg_opt);
68 
69 #define REG_ACCESS_TLV_REG 3
70 #define REG_TLV_HEADER_LEN 4
71 #define OP_TLV_SIZE 16
72 	mail_box_tlv.reg_tlv.Type = REG_ACCESS_TLV_REG;
73 	mail_box_tlv.reg_tlv.len  = ((reg_size + REG_TLV_HEADER_LEN + 3) >> 2); // length is in dwords round up
74 	mlx_memory_cpy(utils, &mail_box_tlv.reg_tlv.data, reg_data, reg_size);
75 
76 	reg_size += OP_TLV_SIZE + REG_TLV_HEADER_LEN;
77 
78 	status = mlx_icmd_send_command(utils, FLASH_REG_ACCESS, &mail_box_tlv, reg_size, reg_size);
79 	MLX_CHECK_STATUS(utils, status, icmd_err, "failed to send icmd");
80 
81 	mlx_memory_cpy(utils, reg_data, &mail_box_tlv.reg_tlv.data,
82 			reg_size - (OP_TLV_SIZE + REG_TLV_HEADER_LEN));
83 
84 	*reg_status = mail_box_tlv.operation_tlv.status;
85 icmd_err:
86 bad_param:
87 	return status;
88 }
89 
90 
91