iPXE
mlx_icmd.c
Go to the documentation of this file.
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
21FILE_LICENCE ( GPL2_OR_LATER );
22
27
28static
31 IN mlx_utils *utils
32 )
33{
35 mlx_uint32 retries = 0;
36 mlx_uint32 semaphore_id;
38 if (utils == NULL) {
40 goto invalid_param;
41 }
42
43 status = mlx_utils_rand(utils, &semaphore_id);
44 MLX_CHECK_STATUS(utils, status, rand_err, "failed to get random number");
45#define ICMD_GET_SEMAPHORE_TRIES 2560
46 for (retries = 0 ; retries < ICMD_GET_SEMAPHORE_TRIES ; retries++) {
49 MLX_CHECK_STATUS(utils, status, read_err, "failed to read icmd semaphore");
50 if (buffer != 0) {
52 continue;
53 }
55 MLX_ICMD_SEMAPHORE_ADDR, semaphore_id);
56 MLX_CHECK_STATUS(utils, status, set_err, "failed to set icmd semaphore");
59 MLX_CHECK_STATUS(utils, status, read_err, "failed to read icmd semaphore");
60 if (semaphore_id == buffer) {
62 utils->icmd.took_semaphore = TRUE;
63 break;
64 }
66 }
67 if (semaphore_id != buffer) {
69 }
70read_err:
71set_err:
72rand_err:
73invalid_param:
74 return status;
75}
76static
79 IN mlx_utils *utils
80 )
81{
83
84 if (utils == NULL) {
86 goto invalid_param;
87 }
88
89 if (utils->icmd.took_semaphore == FALSE) {
90 goto semaphore_not_taken;
91 }
94 MLX_CHECK_STATUS(utils, status, read_err, "failed to clear icmd semaphore");
95
96 utils->icmd.took_semaphore = FALSE;
97read_err:
98semaphore_not_taken:
99invalid_param:
100 return status;
101}
102
103static
106 IN mlx_utils *utils
107 )
108{
110
111 if (utils == NULL) {
113 goto invalid_param;
114 }
115 if (utils->icmd.icmd_opened == TRUE) {
116 goto already_opened;
117 }
118
119 utils->icmd.took_semaphore = FALSE;
120
122 MLX_ICMD_MB_SIZE_ADDR, &utils->icmd.max_cmd_size);
123 MLX_CHECK_STATUS(utils, status, read_err, "failed to read icmd mail box size");
124
125 utils->icmd.icmd_opened = TRUE;
126read_err:
127already_opened:
128invalid_param:
129 return status;
130}
131
132static
135 IN mlx_utils *utils,
137 )
138{
141
142 if (utils == NULL) {
144 goto invalid_param;
145 }
146
149 MLX_CHECK_STATUS(utils, status, read_err, "failed to read icmd ctrl");
150
151#define MLX_ICMD_OPCODE_ALIGN 16
152#define MLX_ICMD_OPCODE_MASK 0xffff
153
156
159 MLX_CHECK_STATUS(utils, status, write_err, "failed to write icmd ctrl");
160write_err:
161read_err:
162invalid_param:
163 return status;
164}
165
166static
169 IN mlx_utils *utils
170 )
171{
174 mlx_uint32 busy;
175 mlx_uint32 wait_iteration = 0;
176
177 if (utils == NULL) {
179 goto invalid_param;
180 }
181
184 MLX_CHECK_STATUS(utils, status, read_err, "failed to read icmd ctrl");
185
186#define MLX_ICMD_BUSY_ALIGN 0
187#define MLX_ICMD_BUSY_MASK 0x1
188
190 if (busy != 0) {
192 goto already_busy;
193 }
194
196
199 MLX_CHECK_STATUS(utils, status, write_err, "failed to write icmd ctrl");
200
201#define MLX_ICMD_BUSY_MAX_ITERATIONS 1024
202 do {
203 if (++wait_iteration > MLX_ICMD_BUSY_MAX_ITERATIONS) {
205 MLX_DEBUG_ERROR(utils, "ICMD time out");
206 goto busy_timeout;
207 }
208
212 MLX_CHECK_STATUS(utils, status, read_err, "failed to read icmd ctrl");
214 } while (busy != 0);
215
216busy_timeout:
217write_err:
218already_busy:
219read_err:
220invalid_param:
221 return status;
222}
223
224static
227 IN mlx_utils *utils,
228 OUT mlx_uint32 *out_status
229 )
230{
233
234 if (utils == NULL || out_status == NULL) {
236 goto invalid_param;
237 }
238
241 MLX_CHECK_STATUS(utils, status, read_err, "failed to read icmd ctrl");
242
243#define MLX_ICMD_STATUS_ALIGN 8
244#define MLX_ICMD_STATUS_MASK 0xff
245
247
248read_err:
249invalid_param:
250 return status;
251}
252
253static
256 IN mlx_utils *utils,
258 IN mlx_uint32 data_size
259 )
260{
262 mlx_uint32 data_offset = 0;
263 mlx_size dword_size = sizeof(mlx_uint32);
264
265 if (utils == NULL || data == NULL) {
267 goto invalid_param;
268 }
269
270 for (data_offset = 0 ; data_offset*dword_size < data_size ; data_offset++) {
272 MLX_ICMD_MB_ADDR + data_offset*dword_size,
273 ((mlx_uint32*)data)[data_offset]);
274 MLX_CHECK_STATUS(utils, status, write_err, "failed to write icmd MB");
275 }
276write_err:
277invalid_param:
278 return status;
279}
280
281
282static
285 IN mlx_utils *utils,
287 IN mlx_uint32 data_size
288 )
289{
291 mlx_uint32 data_offset = 0;
292 mlx_size dword_size = sizeof(mlx_uint32);
293
294 if (utils == NULL || data == NULL) {
296 goto invalid_param;
297 }
298
299 for (data_offset = 0 ; data_offset*dword_size < data_size ; data_offset++) {
301 MLX_ICMD_MB_ADDR + data_offset*dword_size,
302 (mlx_uint32*)data + data_offset);
303 MLX_CHECK_STATUS(utils, status, read_err, "failed to read icmd MB");
304 }
305read_err:
306invalid_param:
307 return status;
308}
311 IN mlx_utils *utils,
314 IN mlx_uint32 write_data_size,
315 IN mlx_uint32 read_data_size
316 )
317{
319 mlx_uint32 icmd_status = 0;
320
321 if (utils == NULL || data == NULL) {
323 goto invalid_param;
324 }
325 status = mlx_icmd_init(utils);
326 MLX_CHECK_STATUS(utils, status, open_err, "failed to open icmd");
327
328 if (write_data_size > utils->icmd.max_cmd_size ||
329 read_data_size > utils->icmd.max_cmd_size) {
331 goto size_err;
332 }
333
335 MLX_CHECK_STATUS(utils, status, semaphore_err, "failed to get icmd semaphore");
336
338 MLX_CHECK_STATUS(utils, status, opcode_err, "failed to set icmd opcode");
339
340 if (write_data_size != 0) {
341 status = mlx_icmd_write_buffer(utils, data, write_data_size);
342 MLX_CHECK_STATUS(utils, status, opcode_err, "failed to write icmd MB");
343 }
344
345 status = mlx_icmd_go(utils);
346 MLX_CHECK_STATUS(utils, status, go_err, "failed to activate icmd");
347
348 status = mlx_icmd_get_status(utils, &icmd_status);
349 MLX_CHECK_STATUS(utils, status, get_status_err, "failed to set icmd opcode");
350
351 if (icmd_status != 0) {
352 MLX_DEBUG_ERROR(utils, "icmd failed with status = %d\n", icmd_status);
354 goto icmd_failed;
355 }
356 if (read_data_size != 0) {
357 status = mlx_icmd_read_buffer(utils, data, read_data_size);
358 MLX_CHECK_STATUS(utils, status, read_err, "failed to read icmd MB");
359 }
360read_err:
361icmd_failed:
362get_status_err:
363go_err:
364opcode_err:
366semaphore_err:
367size_err:
368open_err:
369invalid_param:
370 return status;
371}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
uint8_t data[48]
Additional event data.
Definition ena.h:11
uint8_t status
Status.
Definition ena.h:5
uint8_t opcode
Opcode.
Definition ena.h:5
uint32_t buffer
Buffer index (or NETVSC_RNDIS_NO_BUFFER)
Definition netvsc.h:5
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define MLX_CHECK_STATUS(id, status, label, message)
Definition mlx_bail.h:37
#define MLX_ICMD_BUSY_MASK
static mlx_status mlx_icmd_go(IN mlx_utils *utils)
Definition mlx_icmd.c:168
#define MLX_ICMD_STATUS_ALIGN
static mlx_status mlx_icmd_write_buffer(IN mlx_utils *utils, IN mlx_void *data, IN mlx_uint32 data_size)
Definition mlx_icmd.c:255
static mlx_status mlx_icmd_clear_semaphore(IN mlx_utils *utils)
Definition mlx_icmd.c:78
#define MLX_ICMD_BUSY_ALIGN
#define MLX_ICMD_OPCODE_MASK
static mlx_status mlx_icmd_get_status(IN mlx_utils *utils, OUT mlx_uint32 *out_status)
Definition mlx_icmd.c:226
static mlx_status mlx_icmd_set_opcode(IN mlx_utils *utils, IN mlx_uint16 opcode)
Definition mlx_icmd.c:134
static mlx_status mlx_icmd_read_buffer(IN mlx_utils *utils, OUT mlx_void *data, IN mlx_uint32 data_size)
Definition mlx_icmd.c:284
#define ICMD_GET_SEMAPHORE_TRIES
mlx_status mlx_icmd_send_command(IN mlx_utils *utils, IN mlx_uint16 opcode, IN OUT mlx_void *data, IN mlx_uint32 write_data_size, IN mlx_uint32 read_data_size)
Definition mlx_icmd.c:310
#define MLX_ICMD_STATUS_MASK
static mlx_status mlx_icmd_init(IN mlx_utils *utils)
Definition mlx_icmd.c:105
static mlx_status mlx_icmd_get_semaphore(IN mlx_utils *utils)
Definition mlx_icmd.c:30
#define MLX_ICMD_BUSY_MAX_ITERATIONS
#define MLX_ICMD_OPCODE_ALIGN
#define MLX_ICMD_CTRL_ADDR
Definition mlx_icmd.h:29
#define MLX_ICMD_SEMAPHORE_ADDR
Definition mlx_icmd.h:31
#define MLX_ICMD_MB_ADDR
Definition mlx_icmd.h:27
#define MLX_ICMD_MB_SIZE_ADDR
Definition mlx_icmd.h:28
#define MLX_DEBUG_ERROR(...)
Definition mlx_logging.h:29
#define PCI_GW_SPACE_SEMAPHORE
Definition mlx_pci_gw.h:49
mlx_status mlx_pci_gw_read(IN mlx_utils *utils, IN mlx_pci_gw_space space, IN mlx_uint32 address, OUT mlx_pci_gw_buffer *buffer)
Definition mlx_pci_gw.c:258
mlx_status mlx_pci_gw_write(IN mlx_utils *utils, IN mlx_pci_gw_space space, IN mlx_uint32 address, IN mlx_pci_gw_buffer buffer)
Definition mlx_pci_gw.c:328
#define PCI_GW_SPACE_ALL_ICMD
Definition mlx_pci_gw.h:48
uint16_t mlx_uint16
uint32_t mlx_uint32
size_t mlx_size
#define MLX_FAILED
#define MLX_INVALID_PARAMETER
void mlx_void
#define MLX_SUCCESS
int mlx_status
mlx_status mlx_utils_delay_in_ms(IN mlx_uint32 msecs)
Definition mlx_utils.c:57
mlx_status mlx_utils_rand(IN mlx_utils *utils, OUT mlx_uint32 *rand_num)
Definition mlx_utils.c:116
#define IN
Definition mlx_utils.h:28
#define OUT
Definition mlx_utils.h:29
#define TRUE
Definition tlan.h:46
#define FALSE
Definition tlan.h:45