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 
21 FILE_LICENCE ( GPL2_OR_LATER );
22 
23 #include "../../include/public/mlx_bail.h"
24 #include "../../include/public/mlx_icmd.h"
25 #include "../../include/public/mlx_pci_gw.h"
26 #include "../../include/public/mlx_utils.h"
27 
28 static
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  }
70 read_err:
71 set_err:
72 rand_err:
73 invalid_param:
74  return status;
75 }
76 static
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;
97 read_err:
98 semaphore_not_taken:
99 invalid_param:
100  return status;
101 }
102 
103 static
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;
126 read_err:
127 already_opened:
128 invalid_param:
129  return status;
130 }
131 
132 static
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");
160 write_err:
161 read_err:
162 invalid_param:
163  return status;
164 }
165 
166 static
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) {
191  status = MLX_FAILED;
192  goto already_busy;
193  }
194 
195  buffer = buffer | (1 << MLX_ICMD_BUSY_ALIGN);
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) {
204  status = MLX_FAILED;
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 
216 busy_timeout:
217 write_err:
218 already_busy:
219 read_err:
220 invalid_param:
221  return status;
222 }
223 
224 static
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 
248 read_err:
249 invalid_param:
250  return status;
251 }
252 
253 static
256  IN mlx_utils *utils,
257  IN mlx_void* data,
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  }
276 write_err:
277 invalid_param:
278  return status;
279 }
280 
281 
282 static
285  IN mlx_utils *utils,
286  OUT mlx_void* data,
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  }
305 read_err:
306 invalid_param:
307  return status;
308 }
311  IN mlx_utils *utils,
313  IN OUT mlx_void* data,
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);
353  status = MLX_FAILED;
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  }
360 read_err:
361 icmd_failed:
362 get_status_err:
363 go_err:
364 opcode_err:
366 semaphore_err:
367 size_err:
368 open_err:
369 invalid_param:
370  return status;
371 }
#define ICMD_GET_SEMAPHORE_TRIES
uint8_t opcode
Opcode.
Definition: ena.h:16
#define MLX_INVALID_PARAMETER
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_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
mlx_status mlx_utils_delay_in_ms(IN mlx_uint32 msecs)
Definition: mlx_utils.c:57
FILE_LICENCE(GPL2_OR_LATER)
#define MLX_ICMD_BUSY_ALIGN
uint32_t buffer
Buffer index (or NETVSC_RNDIS_NO_BUFFER)
Definition: netvsc.h:16
#define MLX_ICMD_OPCODE_MASK
static mlx_status mlx_icmd_init(IN mlx_utils *utils)
Definition: mlx_icmd.c:105
#define MLX_ICMD_MB_ADDR
Definition: mlx_icmd.h:27
uint8_t status
Status.
Definition: ena.h:16
uint32_t mlx_uint32
#define MLX_ICMD_SEMAPHORE_ADDR
Definition: mlx_icmd.h:31
#define MLX_ICMD_BUSY_MAX_ITERATIONS
#define OUT
Definition: mlx_utils.h:29
#define MLX_ICMD_CTRL_ADDR
Definition: mlx_icmd.h:29
#define MLX_SUCCESS
#define MLX_ICMD_MB_SIZE_ADDR
Definition: mlx_icmd.h:28
static mlx_status mlx_icmd_get_status(IN mlx_utils *utils, OUT mlx_uint32 *out_status)
Definition: mlx_icmd.c:226
#define PCI_GW_SPACE_ALL_ICMD
Definition: mlx_pci_gw.h:48
#define MLX_ICMD_BUSY_MASK
size_t mlx_size
static mlx_status mlx_icmd_set_opcode(IN mlx_utils *utils, IN mlx_uint16 opcode)
Definition: mlx_icmd.c:134
#define MLX_FAILED
#define TRUE
Definition: tlan.h:46
uint16_t mlx_uint16
void mlx_void
#define IN
Definition: mlx_utils.h:28
static mlx_status mlx_icmd_go(IN mlx_utils *utils)
Definition: mlx_icmd.c:168
#define MLX_ICMD_STATUS_MASK
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
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
static mlx_status mlx_icmd_get_semaphore(IN mlx_utils *utils)
Definition: mlx_icmd.c:30
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
#define MLX_DEBUG_ERROR(...)
Definition: mlx_logging.h:29
mlx_status mlx_utils_rand(IN mlx_utils *utils, OUT mlx_uint32 *rand_num)
Definition: mlx_utils.c:116
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define FALSE
Definition: tlan.h:45
int mlx_status
#define MLX_ICMD_STATUS_ALIGN
static mlx_status mlx_icmd_clear_semaphore(IN mlx_utils *utils)
Definition: mlx_icmd.c:78
#define MLX_CHECK_STATUS(id, status, label, message)
Definition: mlx_bail.h:37
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define MLX_ICMD_OPCODE_ALIGN
#define PCI_GW_SPACE_SEMAPHORE
Definition: mlx_pci_gw.h:49