• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2016, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/highmem.h>
34 #include <linux/errno.h>
35 #include <linux/pci.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/slab.h>
38 #include <linux/delay.h>
39 #include <linux/random.h>
40 #include <linux/io-mapping.h>
41 #include <linux/mlx5/driver.h>
42 #include <linux/mlx5/eq.h>
43 #include <linux/debugfs.h>
44 
45 #include "mlx5_core.h"
46 #include "lib/eq.h"
47 #include "lib/tout.h"
48 #define CREATE_TRACE_POINTS
49 #include "diag/cmd_tracepoint.h"
50 
51 struct mlx5_ifc_mbox_out_bits {
52 	u8         status[0x8];
53 	u8         reserved_at_8[0x18];
54 
55 	u8         syndrome[0x20];
56 
57 	u8         reserved_at_40[0x40];
58 };
59 
60 struct mlx5_ifc_mbox_in_bits {
61 	u8         opcode[0x10];
62 	u8         uid[0x10];
63 
64 	u8         reserved_at_20[0x10];
65 	u8         op_mod[0x10];
66 
67 	u8         reserved_at_40[0x40];
68 };
69 
70 enum {
71 	CMD_IF_REV = 5,
72 };
73 
74 enum {
75 	CMD_MODE_POLLING,
76 	CMD_MODE_EVENTS
77 };
78 
79 enum {
80 	MLX5_CMD_DELIVERY_STAT_OK			= 0x0,
81 	MLX5_CMD_DELIVERY_STAT_SIGNAT_ERR		= 0x1,
82 	MLX5_CMD_DELIVERY_STAT_TOK_ERR			= 0x2,
83 	MLX5_CMD_DELIVERY_STAT_BAD_BLK_NUM_ERR		= 0x3,
84 	MLX5_CMD_DELIVERY_STAT_OUT_PTR_ALIGN_ERR	= 0x4,
85 	MLX5_CMD_DELIVERY_STAT_IN_PTR_ALIGN_ERR		= 0x5,
86 	MLX5_CMD_DELIVERY_STAT_FW_ERR			= 0x6,
87 	MLX5_CMD_DELIVERY_STAT_IN_LENGTH_ERR		= 0x7,
88 	MLX5_CMD_DELIVERY_STAT_OUT_LENGTH_ERR		= 0x8,
89 	MLX5_CMD_DELIVERY_STAT_RES_FLD_NOT_CLR_ERR	= 0x9,
90 	MLX5_CMD_DELIVERY_STAT_CMD_DESCR_ERR		= 0x10,
91 };
92 
in_to_opcode(void * in)93 static u16 in_to_opcode(void *in)
94 {
95 	return MLX5_GET(mbox_in, in, opcode);
96 }
97 
98 /* Returns true for opcodes that might be triggered very frequently and throttle
99  * the command interface. Limit their command slots usage.
100  */
mlx5_cmd_is_throttle_opcode(u16 op)101 static bool mlx5_cmd_is_throttle_opcode(u16 op)
102 {
103 	switch (op) {
104 	case MLX5_CMD_OP_CREATE_GENERAL_OBJECT:
105 	case MLX5_CMD_OP_DESTROY_GENERAL_OBJECT:
106 	case MLX5_CMD_OP_MODIFY_GENERAL_OBJECT:
107 	case MLX5_CMD_OP_QUERY_GENERAL_OBJECT:
108 		return true;
109 	}
110 	return false;
111 }
112 
113 static struct mlx5_cmd_work_ent *
cmd_alloc_ent(struct mlx5_cmd * cmd,struct mlx5_cmd_msg * in,struct mlx5_cmd_msg * out,void * uout,int uout_size,mlx5_cmd_cbk_t cbk,void * context,int page_queue)114 cmd_alloc_ent(struct mlx5_cmd *cmd, struct mlx5_cmd_msg *in,
115 	      struct mlx5_cmd_msg *out, void *uout, int uout_size,
116 	      mlx5_cmd_cbk_t cbk, void *context, int page_queue)
117 {
118 	gfp_t alloc_flags = cbk ? GFP_ATOMIC : GFP_KERNEL;
119 	struct mlx5_cmd_work_ent *ent;
120 
121 	ent = kzalloc(sizeof(*ent), alloc_flags);
122 	if (!ent)
123 		return ERR_PTR(-ENOMEM);
124 
125 	ent->idx	= -EINVAL;
126 	ent->in		= in;
127 	ent->out	= out;
128 	ent->uout	= uout;
129 	ent->uout_size	= uout_size;
130 	ent->callback	= cbk;
131 	ent->context	= context;
132 	ent->cmd	= cmd;
133 	ent->page_queue = page_queue;
134 	ent->op         = in_to_opcode(in->first.data);
135 	refcount_set(&ent->refcnt, 1);
136 
137 	return ent;
138 }
139 
cmd_free_ent(struct mlx5_cmd_work_ent * ent)140 static void cmd_free_ent(struct mlx5_cmd_work_ent *ent)
141 {
142 	kfree(ent);
143 }
144 
alloc_token(struct mlx5_cmd * cmd)145 static u8 alloc_token(struct mlx5_cmd *cmd)
146 {
147 	u8 token;
148 
149 	spin_lock(&cmd->token_lock);
150 	cmd->token++;
151 	if (cmd->token == 0)
152 		cmd->token++;
153 	token = cmd->token;
154 	spin_unlock(&cmd->token_lock);
155 
156 	return token;
157 }
158 
cmd_alloc_index(struct mlx5_cmd * cmd,struct mlx5_cmd_work_ent * ent)159 static int cmd_alloc_index(struct mlx5_cmd *cmd, struct mlx5_cmd_work_ent *ent)
160 {
161 	unsigned long flags;
162 	int ret;
163 
164 	spin_lock_irqsave(&cmd->alloc_lock, flags);
165 	ret = find_first_bit(&cmd->vars.bitmask, cmd->vars.max_reg_cmds);
166 	if (ret < cmd->vars.max_reg_cmds) {
167 		clear_bit(ret, &cmd->vars.bitmask);
168 		ent->idx = ret;
169 		cmd->ent_arr[ent->idx] = ent;
170 	}
171 	spin_unlock_irqrestore(&cmd->alloc_lock, flags);
172 
173 	return ret < cmd->vars.max_reg_cmds ? ret : -ENOMEM;
174 }
175 
cmd_free_index(struct mlx5_cmd * cmd,int idx)176 static void cmd_free_index(struct mlx5_cmd *cmd, int idx)
177 {
178 	lockdep_assert_held(&cmd->alloc_lock);
179 	set_bit(idx, &cmd->vars.bitmask);
180 }
181 
cmd_ent_get(struct mlx5_cmd_work_ent * ent)182 static void cmd_ent_get(struct mlx5_cmd_work_ent *ent)
183 {
184 	refcount_inc(&ent->refcnt);
185 }
186 
cmd_ent_put(struct mlx5_cmd_work_ent * ent)187 static void cmd_ent_put(struct mlx5_cmd_work_ent *ent)
188 {
189 	struct mlx5_cmd *cmd = ent->cmd;
190 	unsigned long flags;
191 
192 	spin_lock_irqsave(&cmd->alloc_lock, flags);
193 	if (!refcount_dec_and_test(&ent->refcnt))
194 		goto out;
195 
196 	if (ent->idx >= 0) {
197 		cmd_free_index(cmd, ent->idx);
198 		up(ent->page_queue ? &cmd->vars.pages_sem : &cmd->vars.sem);
199 	}
200 
201 	cmd_free_ent(ent);
202 out:
203 	spin_unlock_irqrestore(&cmd->alloc_lock, flags);
204 }
205 
get_inst(struct mlx5_cmd * cmd,int idx)206 static struct mlx5_cmd_layout *get_inst(struct mlx5_cmd *cmd, int idx)
207 {
208 	return cmd->cmd_buf + (idx << cmd->vars.log_stride);
209 }
210 
mlx5_calc_cmd_blocks(struct mlx5_cmd_msg * msg)211 static int mlx5_calc_cmd_blocks(struct mlx5_cmd_msg *msg)
212 {
213 	int size = msg->len;
214 	int blen = size - min_t(int, sizeof(msg->first.data), size);
215 
216 	return DIV_ROUND_UP(blen, MLX5_CMD_DATA_BLOCK_SIZE);
217 }
218 
xor8_buf(void * buf,size_t offset,int len)219 static u8 xor8_buf(void *buf, size_t offset, int len)
220 {
221 	u8 *ptr = buf;
222 	u8 sum = 0;
223 	int i;
224 	int end = len + offset;
225 
226 	for (i = offset; i < end; i++)
227 		sum ^= ptr[i];
228 
229 	return sum;
230 }
231 
verify_block_sig(struct mlx5_cmd_prot_block * block)232 static int verify_block_sig(struct mlx5_cmd_prot_block *block)
233 {
234 	size_t rsvd0_off = offsetof(struct mlx5_cmd_prot_block, rsvd0);
235 	int xor_len = sizeof(*block) - sizeof(block->data) - 1;
236 
237 	if (xor8_buf(block, rsvd0_off, xor_len) != 0xff)
238 		return -EHWPOISON;
239 
240 	if (xor8_buf(block, 0, sizeof(*block)) != 0xff)
241 		return -EHWPOISON;
242 
243 	return 0;
244 }
245 
calc_block_sig(struct mlx5_cmd_prot_block * block)246 static void calc_block_sig(struct mlx5_cmd_prot_block *block)
247 {
248 	int ctrl_xor_len = sizeof(*block) - sizeof(block->data) - 2;
249 	size_t rsvd0_off = offsetof(struct mlx5_cmd_prot_block, rsvd0);
250 
251 	block->ctrl_sig = ~xor8_buf(block, rsvd0_off, ctrl_xor_len);
252 	block->sig = ~xor8_buf(block, 0, sizeof(*block) - 1);
253 }
254 
calc_chain_sig(struct mlx5_cmd_msg * msg)255 static void calc_chain_sig(struct mlx5_cmd_msg *msg)
256 {
257 	struct mlx5_cmd_mailbox *next = msg->next;
258 	int n = mlx5_calc_cmd_blocks(msg);
259 	int i = 0;
260 
261 	for (i = 0; i < n && next; i++)  {
262 		calc_block_sig(next->buf);
263 		next = next->next;
264 	}
265 }
266 
set_signature(struct mlx5_cmd_work_ent * ent,int csum)267 static void set_signature(struct mlx5_cmd_work_ent *ent, int csum)
268 {
269 	ent->lay->sig = ~xor8_buf(ent->lay, 0,  sizeof(*ent->lay));
270 	if (csum) {
271 		calc_chain_sig(ent->in);
272 		calc_chain_sig(ent->out);
273 	}
274 }
275 
poll_timeout(struct mlx5_cmd_work_ent * ent)276 static void poll_timeout(struct mlx5_cmd_work_ent *ent)
277 {
278 	struct mlx5_core_dev *dev = container_of(ent->cmd, struct mlx5_core_dev, cmd);
279 	u64 cmd_to_ms = mlx5_tout_ms(dev, CMD);
280 	unsigned long poll_end;
281 	u8 own;
282 
283 	poll_end = jiffies + msecs_to_jiffies(cmd_to_ms + 1000);
284 
285 	do {
286 		own = READ_ONCE(ent->lay->status_own);
287 		if (!(own & CMD_OWNER_HW)) {
288 			ent->ret = 0;
289 			return;
290 		}
291 		cond_resched();
292 	} while (time_before(jiffies, poll_end));
293 
294 	ent->ret = -ETIMEDOUT;
295 }
296 
verify_signature(struct mlx5_cmd_work_ent * ent)297 static int verify_signature(struct mlx5_cmd_work_ent *ent)
298 {
299 	struct mlx5_cmd_mailbox *next = ent->out->next;
300 	int n = mlx5_calc_cmd_blocks(ent->out);
301 	int err;
302 	u8 sig;
303 	int i = 0;
304 
305 	sig = xor8_buf(ent->lay, 0, sizeof(*ent->lay));
306 	if (sig != 0xff)
307 		return -EHWPOISON;
308 
309 	for (i = 0; i < n && next; i++) {
310 		err = verify_block_sig(next->buf);
311 		if (err)
312 			return -EHWPOISON;
313 
314 		next = next->next;
315 	}
316 
317 	return 0;
318 }
319 
dump_buf(void * buf,int size,int data_only,int offset,int idx)320 static void dump_buf(void *buf, int size, int data_only, int offset, int idx)
321 {
322 	__be32 *p = buf;
323 	int i;
324 
325 	for (i = 0; i < size; i += 16) {
326 		pr_debug("cmd[%d]: %03x: %08x %08x %08x %08x\n", idx, offset,
327 			 be32_to_cpu(p[0]), be32_to_cpu(p[1]),
328 			 be32_to_cpu(p[2]), be32_to_cpu(p[3]));
329 		p += 4;
330 		offset += 16;
331 	}
332 	if (!data_only)
333 		pr_debug("\n");
334 }
335 
mlx5_internal_err_ret_value(struct mlx5_core_dev * dev,u16 op,u32 * synd,u8 * status)336 static int mlx5_internal_err_ret_value(struct mlx5_core_dev *dev, u16 op,
337 				       u32 *synd, u8 *status)
338 {
339 	*synd = 0;
340 	*status = 0;
341 
342 	switch (op) {
343 	case MLX5_CMD_OP_TEARDOWN_HCA:
344 	case MLX5_CMD_OP_DISABLE_HCA:
345 	case MLX5_CMD_OP_MANAGE_PAGES:
346 	case MLX5_CMD_OP_DESTROY_MKEY:
347 	case MLX5_CMD_OP_DESTROY_EQ:
348 	case MLX5_CMD_OP_DESTROY_CQ:
349 	case MLX5_CMD_OP_DESTROY_QP:
350 	case MLX5_CMD_OP_DESTROY_PSV:
351 	case MLX5_CMD_OP_DESTROY_SRQ:
352 	case MLX5_CMD_OP_DESTROY_XRC_SRQ:
353 	case MLX5_CMD_OP_DESTROY_XRQ:
354 	case MLX5_CMD_OP_DESTROY_DCT:
355 	case MLX5_CMD_OP_DEALLOC_Q_COUNTER:
356 	case MLX5_CMD_OP_DESTROY_SCHEDULING_ELEMENT:
357 	case MLX5_CMD_OP_DESTROY_QOS_PARA_VPORT:
358 	case MLX5_CMD_OP_DEALLOC_PD:
359 	case MLX5_CMD_OP_DEALLOC_UAR:
360 	case MLX5_CMD_OP_DETACH_FROM_MCG:
361 	case MLX5_CMD_OP_DEALLOC_XRCD:
362 	case MLX5_CMD_OP_DEALLOC_TRANSPORT_DOMAIN:
363 	case MLX5_CMD_OP_DELETE_VXLAN_UDP_DPORT:
364 	case MLX5_CMD_OP_DELETE_L2_TABLE_ENTRY:
365 	case MLX5_CMD_OP_DESTROY_LAG:
366 	case MLX5_CMD_OP_DESTROY_VPORT_LAG:
367 	case MLX5_CMD_OP_DESTROY_TIR:
368 	case MLX5_CMD_OP_DESTROY_SQ:
369 	case MLX5_CMD_OP_DESTROY_RQ:
370 	case MLX5_CMD_OP_DESTROY_RMP:
371 	case MLX5_CMD_OP_DESTROY_TIS:
372 	case MLX5_CMD_OP_DESTROY_RQT:
373 	case MLX5_CMD_OP_DESTROY_FLOW_TABLE:
374 	case MLX5_CMD_OP_DESTROY_FLOW_GROUP:
375 	case MLX5_CMD_OP_DELETE_FLOW_TABLE_ENTRY:
376 	case MLX5_CMD_OP_DEALLOC_FLOW_COUNTER:
377 	case MLX5_CMD_OP_2ERR_QP:
378 	case MLX5_CMD_OP_2RST_QP:
379 	case MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT:
380 	case MLX5_CMD_OP_MODIFY_FLOW_TABLE:
381 	case MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY:
382 	case MLX5_CMD_OP_SET_FLOW_TABLE_ROOT:
383 	case MLX5_CMD_OP_DEALLOC_PACKET_REFORMAT_CONTEXT:
384 	case MLX5_CMD_OP_DEALLOC_MODIFY_HEADER_CONTEXT:
385 	case MLX5_CMD_OP_FPGA_DESTROY_QP:
386 	case MLX5_CMD_OP_DESTROY_GENERAL_OBJECT:
387 	case MLX5_CMD_OP_DEALLOC_MEMIC:
388 	case MLX5_CMD_OP_PAGE_FAULT_RESUME:
389 	case MLX5_CMD_OP_QUERY_ESW_FUNCTIONS:
390 	case MLX5_CMD_OP_DEALLOC_SF:
391 	case MLX5_CMD_OP_DESTROY_UCTX:
392 	case MLX5_CMD_OP_DESTROY_UMEM:
393 	case MLX5_CMD_OP_MODIFY_RQT:
394 		return MLX5_CMD_STAT_OK;
395 
396 	case MLX5_CMD_OP_QUERY_HCA_CAP:
397 	case MLX5_CMD_OP_QUERY_ADAPTER:
398 	case MLX5_CMD_OP_INIT_HCA:
399 	case MLX5_CMD_OP_ENABLE_HCA:
400 	case MLX5_CMD_OP_QUERY_PAGES:
401 	case MLX5_CMD_OP_SET_HCA_CAP:
402 	case MLX5_CMD_OP_QUERY_ISSI:
403 	case MLX5_CMD_OP_SET_ISSI:
404 	case MLX5_CMD_OP_CREATE_MKEY:
405 	case MLX5_CMD_OP_QUERY_MKEY:
406 	case MLX5_CMD_OP_QUERY_SPECIAL_CONTEXTS:
407 	case MLX5_CMD_OP_CREATE_EQ:
408 	case MLX5_CMD_OP_QUERY_EQ:
409 	case MLX5_CMD_OP_GEN_EQE:
410 	case MLX5_CMD_OP_CREATE_CQ:
411 	case MLX5_CMD_OP_QUERY_CQ:
412 	case MLX5_CMD_OP_MODIFY_CQ:
413 	case MLX5_CMD_OP_CREATE_QP:
414 	case MLX5_CMD_OP_RST2INIT_QP:
415 	case MLX5_CMD_OP_INIT2RTR_QP:
416 	case MLX5_CMD_OP_RTR2RTS_QP:
417 	case MLX5_CMD_OP_RTS2RTS_QP:
418 	case MLX5_CMD_OP_SQERR2RTS_QP:
419 	case MLX5_CMD_OP_QUERY_QP:
420 	case MLX5_CMD_OP_SQD_RTS_QP:
421 	case MLX5_CMD_OP_INIT2INIT_QP:
422 	case MLX5_CMD_OP_CREATE_PSV:
423 	case MLX5_CMD_OP_CREATE_SRQ:
424 	case MLX5_CMD_OP_QUERY_SRQ:
425 	case MLX5_CMD_OP_ARM_RQ:
426 	case MLX5_CMD_OP_CREATE_XRC_SRQ:
427 	case MLX5_CMD_OP_QUERY_XRC_SRQ:
428 	case MLX5_CMD_OP_ARM_XRC_SRQ:
429 	case MLX5_CMD_OP_CREATE_XRQ:
430 	case MLX5_CMD_OP_QUERY_XRQ:
431 	case MLX5_CMD_OP_ARM_XRQ:
432 	case MLX5_CMD_OP_CREATE_DCT:
433 	case MLX5_CMD_OP_DRAIN_DCT:
434 	case MLX5_CMD_OP_QUERY_DCT:
435 	case MLX5_CMD_OP_ARM_DCT_FOR_KEY_VIOLATION:
436 	case MLX5_CMD_OP_QUERY_VPORT_STATE:
437 	case MLX5_CMD_OP_MODIFY_VPORT_STATE:
438 	case MLX5_CMD_OP_QUERY_ESW_VPORT_CONTEXT:
439 	case MLX5_CMD_OP_MODIFY_ESW_VPORT_CONTEXT:
440 	case MLX5_CMD_OP_QUERY_NIC_VPORT_CONTEXT:
441 	case MLX5_CMD_OP_QUERY_ROCE_ADDRESS:
442 	case MLX5_CMD_OP_SET_ROCE_ADDRESS:
443 	case MLX5_CMD_OP_QUERY_HCA_VPORT_CONTEXT:
444 	case MLX5_CMD_OP_MODIFY_HCA_VPORT_CONTEXT:
445 	case MLX5_CMD_OP_QUERY_HCA_VPORT_GID:
446 	case MLX5_CMD_OP_QUERY_HCA_VPORT_PKEY:
447 	case MLX5_CMD_OP_QUERY_VNIC_ENV:
448 	case MLX5_CMD_OP_QUERY_VPORT_COUNTER:
449 	case MLX5_CMD_OP_ALLOC_Q_COUNTER:
450 	case MLX5_CMD_OP_QUERY_Q_COUNTER:
451 	case MLX5_CMD_OP_SET_MONITOR_COUNTER:
452 	case MLX5_CMD_OP_ARM_MONITOR_COUNTER:
453 	case MLX5_CMD_OP_SET_PP_RATE_LIMIT:
454 	case MLX5_CMD_OP_QUERY_RATE_LIMIT:
455 	case MLX5_CMD_OP_CREATE_SCHEDULING_ELEMENT:
456 	case MLX5_CMD_OP_QUERY_SCHEDULING_ELEMENT:
457 	case MLX5_CMD_OP_MODIFY_SCHEDULING_ELEMENT:
458 	case MLX5_CMD_OP_CREATE_QOS_PARA_VPORT:
459 	case MLX5_CMD_OP_ALLOC_PD:
460 	case MLX5_CMD_OP_ALLOC_UAR:
461 	case MLX5_CMD_OP_CONFIG_INT_MODERATION:
462 	case MLX5_CMD_OP_ACCESS_REG:
463 	case MLX5_CMD_OP_ATTACH_TO_MCG:
464 	case MLX5_CMD_OP_GET_DROPPED_PACKET_LOG:
465 	case MLX5_CMD_OP_MAD_IFC:
466 	case MLX5_CMD_OP_QUERY_MAD_DEMUX:
467 	case MLX5_CMD_OP_SET_MAD_DEMUX:
468 	case MLX5_CMD_OP_NOP:
469 	case MLX5_CMD_OP_ALLOC_XRCD:
470 	case MLX5_CMD_OP_ALLOC_TRANSPORT_DOMAIN:
471 	case MLX5_CMD_OP_QUERY_CONG_STATUS:
472 	case MLX5_CMD_OP_MODIFY_CONG_STATUS:
473 	case MLX5_CMD_OP_QUERY_CONG_PARAMS:
474 	case MLX5_CMD_OP_MODIFY_CONG_PARAMS:
475 	case MLX5_CMD_OP_QUERY_CONG_STATISTICS:
476 	case MLX5_CMD_OP_ADD_VXLAN_UDP_DPORT:
477 	case MLX5_CMD_OP_SET_L2_TABLE_ENTRY:
478 	case MLX5_CMD_OP_QUERY_L2_TABLE_ENTRY:
479 	case MLX5_CMD_OP_CREATE_LAG:
480 	case MLX5_CMD_OP_MODIFY_LAG:
481 	case MLX5_CMD_OP_QUERY_LAG:
482 	case MLX5_CMD_OP_CREATE_VPORT_LAG:
483 	case MLX5_CMD_OP_CREATE_TIR:
484 	case MLX5_CMD_OP_MODIFY_TIR:
485 	case MLX5_CMD_OP_QUERY_TIR:
486 	case MLX5_CMD_OP_CREATE_SQ:
487 	case MLX5_CMD_OP_MODIFY_SQ:
488 	case MLX5_CMD_OP_QUERY_SQ:
489 	case MLX5_CMD_OP_CREATE_RQ:
490 	case MLX5_CMD_OP_MODIFY_RQ:
491 	case MLX5_CMD_OP_QUERY_RQ:
492 	case MLX5_CMD_OP_CREATE_RMP:
493 	case MLX5_CMD_OP_MODIFY_RMP:
494 	case MLX5_CMD_OP_QUERY_RMP:
495 	case MLX5_CMD_OP_CREATE_TIS:
496 	case MLX5_CMD_OP_MODIFY_TIS:
497 	case MLX5_CMD_OP_QUERY_TIS:
498 	case MLX5_CMD_OP_CREATE_RQT:
499 	case MLX5_CMD_OP_QUERY_RQT:
500 
501 	case MLX5_CMD_OP_CREATE_FLOW_TABLE:
502 	case MLX5_CMD_OP_QUERY_FLOW_TABLE:
503 	case MLX5_CMD_OP_CREATE_FLOW_GROUP:
504 	case MLX5_CMD_OP_QUERY_FLOW_GROUP:
505 	case MLX5_CMD_OP_QUERY_FLOW_TABLE_ENTRY:
506 	case MLX5_CMD_OP_ALLOC_FLOW_COUNTER:
507 	case MLX5_CMD_OP_QUERY_FLOW_COUNTER:
508 	case MLX5_CMD_OP_ALLOC_PACKET_REFORMAT_CONTEXT:
509 	case MLX5_CMD_OP_ALLOC_MODIFY_HEADER_CONTEXT:
510 	case MLX5_CMD_OP_FPGA_CREATE_QP:
511 	case MLX5_CMD_OP_FPGA_MODIFY_QP:
512 	case MLX5_CMD_OP_FPGA_QUERY_QP:
513 	case MLX5_CMD_OP_FPGA_QUERY_QP_COUNTERS:
514 	case MLX5_CMD_OP_CREATE_GENERAL_OBJECT:
515 	case MLX5_CMD_OP_MODIFY_GENERAL_OBJECT:
516 	case MLX5_CMD_OP_QUERY_GENERAL_OBJECT:
517 	case MLX5_CMD_OP_CREATE_UCTX:
518 	case MLX5_CMD_OP_CREATE_UMEM:
519 	case MLX5_CMD_OP_ALLOC_MEMIC:
520 	case MLX5_CMD_OP_MODIFY_XRQ:
521 	case MLX5_CMD_OP_RELEASE_XRQ_ERROR:
522 	case MLX5_CMD_OP_QUERY_VHCA_STATE:
523 	case MLX5_CMD_OP_MODIFY_VHCA_STATE:
524 	case MLX5_CMD_OP_ALLOC_SF:
525 	case MLX5_CMD_OP_SUSPEND_VHCA:
526 	case MLX5_CMD_OP_RESUME_VHCA:
527 	case MLX5_CMD_OP_QUERY_VHCA_MIGRATION_STATE:
528 	case MLX5_CMD_OP_SAVE_VHCA_STATE:
529 	case MLX5_CMD_OP_LOAD_VHCA_STATE:
530 		*status = MLX5_DRIVER_STATUS_ABORTED;
531 		*synd = MLX5_DRIVER_SYND;
532 		return -ENOLINK;
533 	default:
534 		mlx5_core_err(dev, "Unknown FW command (%d)\n", op);
535 		return -EINVAL;
536 	}
537 }
538 
mlx5_command_str(int command)539 const char *mlx5_command_str(int command)
540 {
541 #define MLX5_COMMAND_STR_CASE(__cmd) case MLX5_CMD_OP_ ## __cmd: return #__cmd
542 
543 	switch (command) {
544 	MLX5_COMMAND_STR_CASE(QUERY_HCA_CAP);
545 	MLX5_COMMAND_STR_CASE(QUERY_ADAPTER);
546 	MLX5_COMMAND_STR_CASE(INIT_HCA);
547 	MLX5_COMMAND_STR_CASE(TEARDOWN_HCA);
548 	MLX5_COMMAND_STR_CASE(ENABLE_HCA);
549 	MLX5_COMMAND_STR_CASE(DISABLE_HCA);
550 	MLX5_COMMAND_STR_CASE(QUERY_PAGES);
551 	MLX5_COMMAND_STR_CASE(MANAGE_PAGES);
552 	MLX5_COMMAND_STR_CASE(SET_HCA_CAP);
553 	MLX5_COMMAND_STR_CASE(QUERY_ISSI);
554 	MLX5_COMMAND_STR_CASE(SET_ISSI);
555 	MLX5_COMMAND_STR_CASE(SET_DRIVER_VERSION);
556 	MLX5_COMMAND_STR_CASE(CREATE_MKEY);
557 	MLX5_COMMAND_STR_CASE(QUERY_MKEY);
558 	MLX5_COMMAND_STR_CASE(DESTROY_MKEY);
559 	MLX5_COMMAND_STR_CASE(QUERY_SPECIAL_CONTEXTS);
560 	MLX5_COMMAND_STR_CASE(PAGE_FAULT_RESUME);
561 	MLX5_COMMAND_STR_CASE(CREATE_EQ);
562 	MLX5_COMMAND_STR_CASE(DESTROY_EQ);
563 	MLX5_COMMAND_STR_CASE(QUERY_EQ);
564 	MLX5_COMMAND_STR_CASE(GEN_EQE);
565 	MLX5_COMMAND_STR_CASE(CREATE_CQ);
566 	MLX5_COMMAND_STR_CASE(DESTROY_CQ);
567 	MLX5_COMMAND_STR_CASE(QUERY_CQ);
568 	MLX5_COMMAND_STR_CASE(MODIFY_CQ);
569 	MLX5_COMMAND_STR_CASE(CREATE_QP);
570 	MLX5_COMMAND_STR_CASE(DESTROY_QP);
571 	MLX5_COMMAND_STR_CASE(RST2INIT_QP);
572 	MLX5_COMMAND_STR_CASE(INIT2RTR_QP);
573 	MLX5_COMMAND_STR_CASE(RTR2RTS_QP);
574 	MLX5_COMMAND_STR_CASE(RTS2RTS_QP);
575 	MLX5_COMMAND_STR_CASE(SQERR2RTS_QP);
576 	MLX5_COMMAND_STR_CASE(2ERR_QP);
577 	MLX5_COMMAND_STR_CASE(2RST_QP);
578 	MLX5_COMMAND_STR_CASE(QUERY_QP);
579 	MLX5_COMMAND_STR_CASE(SQD_RTS_QP);
580 	MLX5_COMMAND_STR_CASE(INIT2INIT_QP);
581 	MLX5_COMMAND_STR_CASE(CREATE_PSV);
582 	MLX5_COMMAND_STR_CASE(DESTROY_PSV);
583 	MLX5_COMMAND_STR_CASE(CREATE_SRQ);
584 	MLX5_COMMAND_STR_CASE(DESTROY_SRQ);
585 	MLX5_COMMAND_STR_CASE(QUERY_SRQ);
586 	MLX5_COMMAND_STR_CASE(ARM_RQ);
587 	MLX5_COMMAND_STR_CASE(CREATE_XRC_SRQ);
588 	MLX5_COMMAND_STR_CASE(DESTROY_XRC_SRQ);
589 	MLX5_COMMAND_STR_CASE(QUERY_XRC_SRQ);
590 	MLX5_COMMAND_STR_CASE(ARM_XRC_SRQ);
591 	MLX5_COMMAND_STR_CASE(CREATE_DCT);
592 	MLX5_COMMAND_STR_CASE(DESTROY_DCT);
593 	MLX5_COMMAND_STR_CASE(DRAIN_DCT);
594 	MLX5_COMMAND_STR_CASE(QUERY_DCT);
595 	MLX5_COMMAND_STR_CASE(ARM_DCT_FOR_KEY_VIOLATION);
596 	MLX5_COMMAND_STR_CASE(QUERY_VPORT_STATE);
597 	MLX5_COMMAND_STR_CASE(MODIFY_VPORT_STATE);
598 	MLX5_COMMAND_STR_CASE(QUERY_ESW_VPORT_CONTEXT);
599 	MLX5_COMMAND_STR_CASE(MODIFY_ESW_VPORT_CONTEXT);
600 	MLX5_COMMAND_STR_CASE(QUERY_NIC_VPORT_CONTEXT);
601 	MLX5_COMMAND_STR_CASE(MODIFY_NIC_VPORT_CONTEXT);
602 	MLX5_COMMAND_STR_CASE(QUERY_ROCE_ADDRESS);
603 	MLX5_COMMAND_STR_CASE(SET_ROCE_ADDRESS);
604 	MLX5_COMMAND_STR_CASE(QUERY_HCA_VPORT_CONTEXT);
605 	MLX5_COMMAND_STR_CASE(MODIFY_HCA_VPORT_CONTEXT);
606 	MLX5_COMMAND_STR_CASE(QUERY_HCA_VPORT_GID);
607 	MLX5_COMMAND_STR_CASE(QUERY_HCA_VPORT_PKEY);
608 	MLX5_COMMAND_STR_CASE(QUERY_VNIC_ENV);
609 	MLX5_COMMAND_STR_CASE(QUERY_VPORT_COUNTER);
610 	MLX5_COMMAND_STR_CASE(ALLOC_Q_COUNTER);
611 	MLX5_COMMAND_STR_CASE(DEALLOC_Q_COUNTER);
612 	MLX5_COMMAND_STR_CASE(QUERY_Q_COUNTER);
613 	MLX5_COMMAND_STR_CASE(SET_MONITOR_COUNTER);
614 	MLX5_COMMAND_STR_CASE(ARM_MONITOR_COUNTER);
615 	MLX5_COMMAND_STR_CASE(SET_PP_RATE_LIMIT);
616 	MLX5_COMMAND_STR_CASE(QUERY_RATE_LIMIT);
617 	MLX5_COMMAND_STR_CASE(CREATE_SCHEDULING_ELEMENT);
618 	MLX5_COMMAND_STR_CASE(DESTROY_SCHEDULING_ELEMENT);
619 	MLX5_COMMAND_STR_CASE(QUERY_SCHEDULING_ELEMENT);
620 	MLX5_COMMAND_STR_CASE(MODIFY_SCHEDULING_ELEMENT);
621 	MLX5_COMMAND_STR_CASE(CREATE_QOS_PARA_VPORT);
622 	MLX5_COMMAND_STR_CASE(DESTROY_QOS_PARA_VPORT);
623 	MLX5_COMMAND_STR_CASE(ALLOC_PD);
624 	MLX5_COMMAND_STR_CASE(DEALLOC_PD);
625 	MLX5_COMMAND_STR_CASE(ALLOC_UAR);
626 	MLX5_COMMAND_STR_CASE(DEALLOC_UAR);
627 	MLX5_COMMAND_STR_CASE(CONFIG_INT_MODERATION);
628 	MLX5_COMMAND_STR_CASE(ACCESS_REG);
629 	MLX5_COMMAND_STR_CASE(ATTACH_TO_MCG);
630 	MLX5_COMMAND_STR_CASE(DETACH_FROM_MCG);
631 	MLX5_COMMAND_STR_CASE(GET_DROPPED_PACKET_LOG);
632 	MLX5_COMMAND_STR_CASE(MAD_IFC);
633 	MLX5_COMMAND_STR_CASE(QUERY_MAD_DEMUX);
634 	MLX5_COMMAND_STR_CASE(SET_MAD_DEMUX);
635 	MLX5_COMMAND_STR_CASE(NOP);
636 	MLX5_COMMAND_STR_CASE(ALLOC_XRCD);
637 	MLX5_COMMAND_STR_CASE(DEALLOC_XRCD);
638 	MLX5_COMMAND_STR_CASE(ALLOC_TRANSPORT_DOMAIN);
639 	MLX5_COMMAND_STR_CASE(DEALLOC_TRANSPORT_DOMAIN);
640 	MLX5_COMMAND_STR_CASE(QUERY_CONG_STATUS);
641 	MLX5_COMMAND_STR_CASE(MODIFY_CONG_STATUS);
642 	MLX5_COMMAND_STR_CASE(QUERY_CONG_PARAMS);
643 	MLX5_COMMAND_STR_CASE(MODIFY_CONG_PARAMS);
644 	MLX5_COMMAND_STR_CASE(QUERY_CONG_STATISTICS);
645 	MLX5_COMMAND_STR_CASE(ADD_VXLAN_UDP_DPORT);
646 	MLX5_COMMAND_STR_CASE(DELETE_VXLAN_UDP_DPORT);
647 	MLX5_COMMAND_STR_CASE(SET_L2_TABLE_ENTRY);
648 	MLX5_COMMAND_STR_CASE(QUERY_L2_TABLE_ENTRY);
649 	MLX5_COMMAND_STR_CASE(DELETE_L2_TABLE_ENTRY);
650 	MLX5_COMMAND_STR_CASE(SET_WOL_ROL);
651 	MLX5_COMMAND_STR_CASE(QUERY_WOL_ROL);
652 	MLX5_COMMAND_STR_CASE(CREATE_LAG);
653 	MLX5_COMMAND_STR_CASE(MODIFY_LAG);
654 	MLX5_COMMAND_STR_CASE(QUERY_LAG);
655 	MLX5_COMMAND_STR_CASE(DESTROY_LAG);
656 	MLX5_COMMAND_STR_CASE(CREATE_VPORT_LAG);
657 	MLX5_COMMAND_STR_CASE(DESTROY_VPORT_LAG);
658 	MLX5_COMMAND_STR_CASE(CREATE_TIR);
659 	MLX5_COMMAND_STR_CASE(MODIFY_TIR);
660 	MLX5_COMMAND_STR_CASE(DESTROY_TIR);
661 	MLX5_COMMAND_STR_CASE(QUERY_TIR);
662 	MLX5_COMMAND_STR_CASE(CREATE_SQ);
663 	MLX5_COMMAND_STR_CASE(MODIFY_SQ);
664 	MLX5_COMMAND_STR_CASE(DESTROY_SQ);
665 	MLX5_COMMAND_STR_CASE(QUERY_SQ);
666 	MLX5_COMMAND_STR_CASE(CREATE_RQ);
667 	MLX5_COMMAND_STR_CASE(MODIFY_RQ);
668 	MLX5_COMMAND_STR_CASE(DESTROY_RQ);
669 	MLX5_COMMAND_STR_CASE(QUERY_RQ);
670 	MLX5_COMMAND_STR_CASE(CREATE_RMP);
671 	MLX5_COMMAND_STR_CASE(MODIFY_RMP);
672 	MLX5_COMMAND_STR_CASE(DESTROY_RMP);
673 	MLX5_COMMAND_STR_CASE(QUERY_RMP);
674 	MLX5_COMMAND_STR_CASE(CREATE_TIS);
675 	MLX5_COMMAND_STR_CASE(MODIFY_TIS);
676 	MLX5_COMMAND_STR_CASE(DESTROY_TIS);
677 	MLX5_COMMAND_STR_CASE(QUERY_TIS);
678 	MLX5_COMMAND_STR_CASE(CREATE_RQT);
679 	MLX5_COMMAND_STR_CASE(MODIFY_RQT);
680 	MLX5_COMMAND_STR_CASE(DESTROY_RQT);
681 	MLX5_COMMAND_STR_CASE(QUERY_RQT);
682 	MLX5_COMMAND_STR_CASE(SET_FLOW_TABLE_ROOT);
683 	MLX5_COMMAND_STR_CASE(CREATE_FLOW_TABLE);
684 	MLX5_COMMAND_STR_CASE(DESTROY_FLOW_TABLE);
685 	MLX5_COMMAND_STR_CASE(QUERY_FLOW_TABLE);
686 	MLX5_COMMAND_STR_CASE(CREATE_FLOW_GROUP);
687 	MLX5_COMMAND_STR_CASE(DESTROY_FLOW_GROUP);
688 	MLX5_COMMAND_STR_CASE(QUERY_FLOW_GROUP);
689 	MLX5_COMMAND_STR_CASE(SET_FLOW_TABLE_ENTRY);
690 	MLX5_COMMAND_STR_CASE(QUERY_FLOW_TABLE_ENTRY);
691 	MLX5_COMMAND_STR_CASE(DELETE_FLOW_TABLE_ENTRY);
692 	MLX5_COMMAND_STR_CASE(ALLOC_FLOW_COUNTER);
693 	MLX5_COMMAND_STR_CASE(DEALLOC_FLOW_COUNTER);
694 	MLX5_COMMAND_STR_CASE(QUERY_FLOW_COUNTER);
695 	MLX5_COMMAND_STR_CASE(MODIFY_FLOW_TABLE);
696 	MLX5_COMMAND_STR_CASE(ALLOC_PACKET_REFORMAT_CONTEXT);
697 	MLX5_COMMAND_STR_CASE(DEALLOC_PACKET_REFORMAT_CONTEXT);
698 	MLX5_COMMAND_STR_CASE(ALLOC_MODIFY_HEADER_CONTEXT);
699 	MLX5_COMMAND_STR_CASE(DEALLOC_MODIFY_HEADER_CONTEXT);
700 	MLX5_COMMAND_STR_CASE(FPGA_CREATE_QP);
701 	MLX5_COMMAND_STR_CASE(FPGA_MODIFY_QP);
702 	MLX5_COMMAND_STR_CASE(FPGA_QUERY_QP);
703 	MLX5_COMMAND_STR_CASE(FPGA_QUERY_QP_COUNTERS);
704 	MLX5_COMMAND_STR_CASE(FPGA_DESTROY_QP);
705 	MLX5_COMMAND_STR_CASE(CREATE_XRQ);
706 	MLX5_COMMAND_STR_CASE(DESTROY_XRQ);
707 	MLX5_COMMAND_STR_CASE(QUERY_XRQ);
708 	MLX5_COMMAND_STR_CASE(ARM_XRQ);
709 	MLX5_COMMAND_STR_CASE(CREATE_GENERAL_OBJECT);
710 	MLX5_COMMAND_STR_CASE(DESTROY_GENERAL_OBJECT);
711 	MLX5_COMMAND_STR_CASE(MODIFY_GENERAL_OBJECT);
712 	MLX5_COMMAND_STR_CASE(QUERY_GENERAL_OBJECT);
713 	MLX5_COMMAND_STR_CASE(QUERY_MODIFY_HEADER_CONTEXT);
714 	MLX5_COMMAND_STR_CASE(ALLOC_MEMIC);
715 	MLX5_COMMAND_STR_CASE(DEALLOC_MEMIC);
716 	MLX5_COMMAND_STR_CASE(QUERY_ESW_FUNCTIONS);
717 	MLX5_COMMAND_STR_CASE(CREATE_UCTX);
718 	MLX5_COMMAND_STR_CASE(DESTROY_UCTX);
719 	MLX5_COMMAND_STR_CASE(CREATE_UMEM);
720 	MLX5_COMMAND_STR_CASE(DESTROY_UMEM);
721 	MLX5_COMMAND_STR_CASE(RELEASE_XRQ_ERROR);
722 	MLX5_COMMAND_STR_CASE(MODIFY_XRQ);
723 	MLX5_COMMAND_STR_CASE(QUERY_VHCA_STATE);
724 	MLX5_COMMAND_STR_CASE(MODIFY_VHCA_STATE);
725 	MLX5_COMMAND_STR_CASE(ALLOC_SF);
726 	MLX5_COMMAND_STR_CASE(DEALLOC_SF);
727 	MLX5_COMMAND_STR_CASE(SUSPEND_VHCA);
728 	MLX5_COMMAND_STR_CASE(RESUME_VHCA);
729 	MLX5_COMMAND_STR_CASE(QUERY_VHCA_MIGRATION_STATE);
730 	MLX5_COMMAND_STR_CASE(SAVE_VHCA_STATE);
731 	MLX5_COMMAND_STR_CASE(LOAD_VHCA_STATE);
732 	default: return "unknown command opcode";
733 	}
734 }
735 
cmd_status_str(u8 status)736 static const char *cmd_status_str(u8 status)
737 {
738 	switch (status) {
739 	case MLX5_CMD_STAT_OK:
740 		return "OK";
741 	case MLX5_CMD_STAT_INT_ERR:
742 		return "internal error";
743 	case MLX5_CMD_STAT_BAD_OP_ERR:
744 		return "bad operation";
745 	case MLX5_CMD_STAT_BAD_PARAM_ERR:
746 		return "bad parameter";
747 	case MLX5_CMD_STAT_BAD_SYS_STATE_ERR:
748 		return "bad system state";
749 	case MLX5_CMD_STAT_BAD_RES_ERR:
750 		return "bad resource";
751 	case MLX5_CMD_STAT_RES_BUSY:
752 		return "resource busy";
753 	case MLX5_CMD_STAT_LIM_ERR:
754 		return "limits exceeded";
755 	case MLX5_CMD_STAT_BAD_RES_STATE_ERR:
756 		return "bad resource state";
757 	case MLX5_CMD_STAT_IX_ERR:
758 		return "bad index";
759 	case MLX5_CMD_STAT_NO_RES_ERR:
760 		return "no resources";
761 	case MLX5_CMD_STAT_BAD_INP_LEN_ERR:
762 		return "bad input length";
763 	case MLX5_CMD_STAT_BAD_OUTP_LEN_ERR:
764 		return "bad output length";
765 	case MLX5_CMD_STAT_BAD_QP_STATE_ERR:
766 		return "bad QP state";
767 	case MLX5_CMD_STAT_BAD_PKT_ERR:
768 		return "bad packet (discarded)";
769 	case MLX5_CMD_STAT_BAD_SIZE_OUTS_CQES_ERR:
770 		return "bad size too many outstanding CQEs";
771 	default:
772 		return "unknown status";
773 	}
774 }
775 
cmd_status_to_err(u8 status)776 static int cmd_status_to_err(u8 status)
777 {
778 	switch (status) {
779 	case MLX5_CMD_STAT_OK:				return 0;
780 	case MLX5_CMD_STAT_INT_ERR:			return -EIO;
781 	case MLX5_CMD_STAT_BAD_OP_ERR:			return -EINVAL;
782 	case MLX5_CMD_STAT_BAD_PARAM_ERR:		return -EINVAL;
783 	case MLX5_CMD_STAT_BAD_SYS_STATE_ERR:		return -EIO;
784 	case MLX5_CMD_STAT_BAD_RES_ERR:			return -EINVAL;
785 	case MLX5_CMD_STAT_RES_BUSY:			return -EBUSY;
786 	case MLX5_CMD_STAT_LIM_ERR:			return -ENOMEM;
787 	case MLX5_CMD_STAT_BAD_RES_STATE_ERR:		return -EINVAL;
788 	case MLX5_CMD_STAT_IX_ERR:			return -EINVAL;
789 	case MLX5_CMD_STAT_NO_RES_ERR:			return -EAGAIN;
790 	case MLX5_CMD_STAT_BAD_INP_LEN_ERR:		return -EIO;
791 	case MLX5_CMD_STAT_BAD_OUTP_LEN_ERR:		return -EIO;
792 	case MLX5_CMD_STAT_BAD_QP_STATE_ERR:		return -EINVAL;
793 	case MLX5_CMD_STAT_BAD_PKT_ERR:			return -EINVAL;
794 	case MLX5_CMD_STAT_BAD_SIZE_OUTS_CQES_ERR:	return -EINVAL;
795 	default:					return -EIO;
796 	}
797 }
798 
mlx5_cmd_out_err(struct mlx5_core_dev * dev,u16 opcode,u16 op_mod,void * out)799 void mlx5_cmd_out_err(struct mlx5_core_dev *dev, u16 opcode, u16 op_mod, void *out)
800 {
801 	u32 syndrome = MLX5_GET(mbox_out, out, syndrome);
802 	u8 status = MLX5_GET(mbox_out, out, status);
803 
804 	mlx5_core_err_rl(dev,
805 			 "%s(0x%x) op_mod(0x%x) failed, status %s(0x%x), syndrome (0x%x), err(%d)\n",
806 			 mlx5_command_str(opcode), opcode, op_mod,
807 			 cmd_status_str(status), status, syndrome, cmd_status_to_err(status));
808 }
809 EXPORT_SYMBOL(mlx5_cmd_out_err);
810 
cmd_status_print(struct mlx5_core_dev * dev,void * in,void * out)811 static void cmd_status_print(struct mlx5_core_dev *dev, void *in, void *out)
812 {
813 	u16 opcode, op_mod;
814 	u16 uid;
815 
816 	opcode = in_to_opcode(in);
817 	op_mod = MLX5_GET(mbox_in, in, op_mod);
818 	uid    = MLX5_GET(mbox_in, in, uid);
819 
820 	if (!uid && opcode != MLX5_CMD_OP_DESTROY_MKEY)
821 		mlx5_cmd_out_err(dev, opcode, op_mod, out);
822 }
823 
mlx5_cmd_check(struct mlx5_core_dev * dev,int err,void * in,void * out)824 int mlx5_cmd_check(struct mlx5_core_dev *dev, int err, void *in, void *out)
825 {
826 	/* aborted due to PCI error or via reset flow mlx5_cmd_trigger_completions() */
827 	if (err == -ENXIO) {
828 		u16 opcode = in_to_opcode(in);
829 		u32 syndrome;
830 		u8 status;
831 
832 		/* PCI Error, emulate command return status, for smooth reset */
833 		err = mlx5_internal_err_ret_value(dev, opcode, &syndrome, &status);
834 		MLX5_SET(mbox_out, out, status, status);
835 		MLX5_SET(mbox_out, out, syndrome, syndrome);
836 		if (!err)
837 			return 0;
838 	}
839 
840 	/* driver or FW delivery error */
841 	if (err != -EREMOTEIO && err)
842 		return err;
843 
844 	/* check outbox status */
845 	err = cmd_status_to_err(MLX5_GET(mbox_out, out, status));
846 	if (err)
847 		cmd_status_print(dev, in, out);
848 
849 	return err;
850 }
851 EXPORT_SYMBOL(mlx5_cmd_check);
852 
dump_command(struct mlx5_core_dev * dev,struct mlx5_cmd_work_ent * ent,int input)853 static void dump_command(struct mlx5_core_dev *dev,
854 			 struct mlx5_cmd_work_ent *ent, int input)
855 {
856 	struct mlx5_cmd_msg *msg = input ? ent->in : ent->out;
857 	struct mlx5_cmd_mailbox *next = msg->next;
858 	int n = mlx5_calc_cmd_blocks(msg);
859 	u16 op = ent->op;
860 	int data_only;
861 	u32 offset = 0;
862 	int dump_len;
863 	int i;
864 
865 	mlx5_core_dbg(dev, "cmd[%d]: start dump\n", ent->idx);
866 	data_only = !!(mlx5_core_debug_mask & (1 << MLX5_CMD_DATA));
867 
868 	if (data_only)
869 		mlx5_core_dbg_mask(dev, 1 << MLX5_CMD_DATA,
870 				   "cmd[%d]: dump command data %s(0x%x) %s\n",
871 				   ent->idx, mlx5_command_str(op), op,
872 				   input ? "INPUT" : "OUTPUT");
873 	else
874 		mlx5_core_dbg(dev, "cmd[%d]: dump command %s(0x%x) %s\n",
875 			      ent->idx, mlx5_command_str(op), op,
876 			      input ? "INPUT" : "OUTPUT");
877 
878 	if (data_only) {
879 		if (input) {
880 			dump_buf(ent->lay->in, sizeof(ent->lay->in), 1, offset, ent->idx);
881 			offset += sizeof(ent->lay->in);
882 		} else {
883 			dump_buf(ent->lay->out, sizeof(ent->lay->out), 1, offset, ent->idx);
884 			offset += sizeof(ent->lay->out);
885 		}
886 	} else {
887 		dump_buf(ent->lay, sizeof(*ent->lay), 0, offset, ent->idx);
888 		offset += sizeof(*ent->lay);
889 	}
890 
891 	for (i = 0; i < n && next; i++)  {
892 		if (data_only) {
893 			dump_len = min_t(int, MLX5_CMD_DATA_BLOCK_SIZE, msg->len - offset);
894 			dump_buf(next->buf, dump_len, 1, offset, ent->idx);
895 			offset += MLX5_CMD_DATA_BLOCK_SIZE;
896 		} else {
897 			mlx5_core_dbg(dev, "cmd[%d]: command block:\n", ent->idx);
898 			dump_buf(next->buf, sizeof(struct mlx5_cmd_prot_block), 0, offset,
899 				 ent->idx);
900 			offset += sizeof(struct mlx5_cmd_prot_block);
901 		}
902 		next = next->next;
903 	}
904 
905 	if (data_only)
906 		pr_debug("\n");
907 
908 	mlx5_core_dbg(dev, "cmd[%d]: end dump\n", ent->idx);
909 }
910 
911 static void mlx5_cmd_comp_handler(struct mlx5_core_dev *dev, u64 vec, bool forced);
912 
cb_timeout_handler(struct work_struct * work)913 static void cb_timeout_handler(struct work_struct *work)
914 {
915 	struct delayed_work *dwork = container_of(work, struct delayed_work,
916 						  work);
917 	struct mlx5_cmd_work_ent *ent = container_of(dwork,
918 						     struct mlx5_cmd_work_ent,
919 						     cb_timeout_work);
920 	struct mlx5_core_dev *dev = container_of(ent->cmd, struct mlx5_core_dev,
921 						 cmd);
922 
923 	mlx5_cmd_eq_recover(dev);
924 
925 	/* Maybe got handled by eq recover ? */
926 	if (!test_bit(MLX5_CMD_ENT_STATE_PENDING_COMP, &ent->state)) {
927 		mlx5_core_warn(dev, "cmd[%d]: %s(0x%x) Async, recovered after timeout\n", ent->idx,
928 			       mlx5_command_str(ent->op), ent->op);
929 		goto out; /* phew, already handled */
930 	}
931 
932 	ent->ret = -ETIMEDOUT;
933 	mlx5_core_warn(dev, "cmd[%d]: %s(0x%x) Async, timeout. Will cause a leak of a command resource\n",
934 		       ent->idx, mlx5_command_str(ent->op), ent->op);
935 	mlx5_cmd_comp_handler(dev, 1ULL << ent->idx, true);
936 
937 out:
938 	cmd_ent_put(ent); /* for the cmd_ent_get() took on schedule delayed work */
939 }
940 
941 static void free_msg(struct mlx5_core_dev *dev, struct mlx5_cmd_msg *msg);
942 static void mlx5_free_cmd_msg(struct mlx5_core_dev *dev,
943 			      struct mlx5_cmd_msg *msg);
944 
opcode_allowed(struct mlx5_cmd * cmd,u16 opcode)945 static bool opcode_allowed(struct mlx5_cmd *cmd, u16 opcode)
946 {
947 	if (cmd->allowed_opcode == CMD_ALLOWED_OPCODE_ALL)
948 		return true;
949 
950 	return cmd->allowed_opcode == opcode;
951 }
952 
mlx5_cmd_is_down(struct mlx5_core_dev * dev)953 bool mlx5_cmd_is_down(struct mlx5_core_dev *dev)
954 {
955 	return pci_channel_offline(dev->pdev) ||
956 	       dev->cmd.state != MLX5_CMDIF_STATE_UP ||
957 	       dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR;
958 }
959 
cmd_work_handler(struct work_struct * work)960 static void cmd_work_handler(struct work_struct *work)
961 {
962 	struct mlx5_cmd_work_ent *ent = container_of(work, struct mlx5_cmd_work_ent, work);
963 	struct mlx5_cmd *cmd = ent->cmd;
964 	bool poll_cmd = ent->polling;
965 	struct mlx5_cmd_layout *lay;
966 	struct mlx5_core_dev *dev;
967 	unsigned long cb_timeout;
968 	struct semaphore *sem;
969 	unsigned long flags;
970 	int alloc_ret;
971 	int cmd_mode;
972 
973 	dev = container_of(cmd, struct mlx5_core_dev, cmd);
974 	cb_timeout = msecs_to_jiffies(mlx5_tout_ms(dev, CMD));
975 
976 	complete(&ent->handling);
977 	sem = ent->page_queue ? &cmd->vars.pages_sem : &cmd->vars.sem;
978 	down(sem);
979 	if (!ent->page_queue) {
980 		alloc_ret = cmd_alloc_index(cmd, ent);
981 		if (alloc_ret < 0) {
982 			mlx5_core_err_rl(dev, "failed to allocate command entry\n");
983 			if (ent->callback) {
984 				ent->callback(-EAGAIN, ent->context);
985 				mlx5_free_cmd_msg(dev, ent->out);
986 				free_msg(dev, ent->in);
987 				cmd_ent_put(ent);
988 			} else {
989 				ent->ret = -EAGAIN;
990 				complete(&ent->done);
991 			}
992 			up(sem);
993 			return;
994 		}
995 	} else {
996 		ent->idx = cmd->vars.max_reg_cmds;
997 		spin_lock_irqsave(&cmd->alloc_lock, flags);
998 		clear_bit(ent->idx, &cmd->vars.bitmask);
999 		cmd->ent_arr[ent->idx] = ent;
1000 		spin_unlock_irqrestore(&cmd->alloc_lock, flags);
1001 	}
1002 
1003 	lay = get_inst(cmd, ent->idx);
1004 	ent->lay = lay;
1005 	memset(lay, 0, sizeof(*lay));
1006 	memcpy(lay->in, ent->in->first.data, sizeof(lay->in));
1007 	if (ent->in->next)
1008 		lay->in_ptr = cpu_to_be64(ent->in->next->dma);
1009 	lay->inlen = cpu_to_be32(ent->in->len);
1010 	if (ent->out->next)
1011 		lay->out_ptr = cpu_to_be64(ent->out->next->dma);
1012 	lay->outlen = cpu_to_be32(ent->out->len);
1013 	lay->type = MLX5_PCI_CMD_XPORT;
1014 	lay->token = ent->token;
1015 	lay->status_own = CMD_OWNER_HW;
1016 	set_signature(ent, !cmd->checksum_disabled);
1017 	dump_command(dev, ent, 1);
1018 	ent->ts1 = ktime_get_ns();
1019 	cmd_mode = cmd->mode;
1020 
1021 	if (ent->callback && schedule_delayed_work(&ent->cb_timeout_work, cb_timeout))
1022 		cmd_ent_get(ent);
1023 	set_bit(MLX5_CMD_ENT_STATE_PENDING_COMP, &ent->state);
1024 
1025 	cmd_ent_get(ent); /* for the _real_ FW event on completion */
1026 	/* Skip sending command to fw if internal error */
1027 	if (mlx5_cmd_is_down(dev) || !opcode_allowed(&dev->cmd, ent->op)) {
1028 		ent->ret = -ENXIO;
1029 		mlx5_cmd_comp_handler(dev, 1ULL << ent->idx, true);
1030 		return;
1031 	}
1032 
1033 	/* ring doorbell after the descriptor is valid */
1034 	mlx5_core_dbg(dev, "writing 0x%x to command doorbell\n", 1 << ent->idx);
1035 	wmb();
1036 	iowrite32be(1 << ent->idx, &dev->iseg->cmd_dbell);
1037 	/* if not in polling don't use ent after this point */
1038 	if (cmd_mode == CMD_MODE_POLLING || poll_cmd) {
1039 		poll_timeout(ent);
1040 		/* make sure we read the descriptor after ownership is SW */
1041 		rmb();
1042 		mlx5_cmd_comp_handler(dev, 1ULL << ent->idx, (ent->ret == -ETIMEDOUT));
1043 	}
1044 }
1045 
deliv_status_to_err(u8 status)1046 static int deliv_status_to_err(u8 status)
1047 {
1048 	switch (status) {
1049 	case MLX5_CMD_DELIVERY_STAT_OK:
1050 	case MLX5_DRIVER_STATUS_ABORTED:
1051 		return 0;
1052 	case MLX5_CMD_DELIVERY_STAT_SIGNAT_ERR:
1053 	case MLX5_CMD_DELIVERY_STAT_TOK_ERR:
1054 		return -EBADR;
1055 	case MLX5_CMD_DELIVERY_STAT_BAD_BLK_NUM_ERR:
1056 	case MLX5_CMD_DELIVERY_STAT_OUT_PTR_ALIGN_ERR:
1057 	case MLX5_CMD_DELIVERY_STAT_IN_PTR_ALIGN_ERR:
1058 		return -EFAULT; /* Bad address */
1059 	case MLX5_CMD_DELIVERY_STAT_IN_LENGTH_ERR:
1060 	case MLX5_CMD_DELIVERY_STAT_OUT_LENGTH_ERR:
1061 	case MLX5_CMD_DELIVERY_STAT_CMD_DESCR_ERR:
1062 	case MLX5_CMD_DELIVERY_STAT_RES_FLD_NOT_CLR_ERR:
1063 		return -ENOMSG;
1064 	case MLX5_CMD_DELIVERY_STAT_FW_ERR:
1065 		return -EIO;
1066 	default:
1067 		return -EINVAL;
1068 	}
1069 }
1070 
deliv_status_to_str(u8 status)1071 static const char *deliv_status_to_str(u8 status)
1072 {
1073 	switch (status) {
1074 	case MLX5_CMD_DELIVERY_STAT_OK:
1075 		return "no errors";
1076 	case MLX5_CMD_DELIVERY_STAT_SIGNAT_ERR:
1077 		return "signature error";
1078 	case MLX5_CMD_DELIVERY_STAT_TOK_ERR:
1079 		return "token error";
1080 	case MLX5_CMD_DELIVERY_STAT_BAD_BLK_NUM_ERR:
1081 		return "bad block number";
1082 	case MLX5_CMD_DELIVERY_STAT_OUT_PTR_ALIGN_ERR:
1083 		return "output pointer not aligned to block size";
1084 	case MLX5_CMD_DELIVERY_STAT_IN_PTR_ALIGN_ERR:
1085 		return "input pointer not aligned to block size";
1086 	case MLX5_CMD_DELIVERY_STAT_FW_ERR:
1087 		return "firmware internal error";
1088 	case MLX5_CMD_DELIVERY_STAT_IN_LENGTH_ERR:
1089 		return "command input length error";
1090 	case MLX5_CMD_DELIVERY_STAT_OUT_LENGTH_ERR:
1091 		return "command output length error";
1092 	case MLX5_CMD_DELIVERY_STAT_RES_FLD_NOT_CLR_ERR:
1093 		return "reserved fields not cleared";
1094 	case MLX5_CMD_DELIVERY_STAT_CMD_DESCR_ERR:
1095 		return "bad command descriptor type";
1096 	default:
1097 		return "unknown status code";
1098 	}
1099 }
1100 
1101 enum {
1102 	MLX5_CMD_TIMEOUT_RECOVER_MSEC   = 5 * 1000,
1103 };
1104 
wait_func_handle_exec_timeout(struct mlx5_core_dev * dev,struct mlx5_cmd_work_ent * ent)1105 static void wait_func_handle_exec_timeout(struct mlx5_core_dev *dev,
1106 					  struct mlx5_cmd_work_ent *ent)
1107 {
1108 	unsigned long timeout = msecs_to_jiffies(MLX5_CMD_TIMEOUT_RECOVER_MSEC);
1109 
1110 	mlx5_cmd_eq_recover(dev);
1111 
1112 	/* Re-wait on the ent->done after executing the recovery flow. If the
1113 	 * recovery flow (or any other recovery flow running simultaneously)
1114 	 * has recovered an EQE, it should cause the entry to be completed by
1115 	 * the command interface.
1116 	 */
1117 	if (wait_for_completion_timeout(&ent->done, timeout)) {
1118 		mlx5_core_warn(dev, "cmd[%d]: %s(0x%x) recovered after timeout\n", ent->idx,
1119 			       mlx5_command_str(ent->op), ent->op);
1120 		return;
1121 	}
1122 
1123 	mlx5_core_warn(dev, "cmd[%d]: %s(0x%x) No done completion\n", ent->idx,
1124 		       mlx5_command_str(ent->op), ent->op);
1125 
1126 	ent->ret = -ETIMEDOUT;
1127 	mlx5_cmd_comp_handler(dev, 1ULL << ent->idx, true);
1128 }
1129 
wait_func(struct mlx5_core_dev * dev,struct mlx5_cmd_work_ent * ent)1130 static int wait_func(struct mlx5_core_dev *dev, struct mlx5_cmd_work_ent *ent)
1131 {
1132 	unsigned long timeout = msecs_to_jiffies(mlx5_tout_ms(dev, CMD));
1133 	struct mlx5_cmd *cmd = &dev->cmd;
1134 	int err;
1135 
1136 	if (!wait_for_completion_timeout(&ent->handling, timeout) &&
1137 	    cancel_work_sync(&ent->work)) {
1138 		ent->ret = -ECANCELED;
1139 		goto out_err;
1140 	}
1141 	if (cmd->mode == CMD_MODE_POLLING || ent->polling)
1142 		wait_for_completion(&ent->done);
1143 	else if (!wait_for_completion_timeout(&ent->done, timeout))
1144 		wait_func_handle_exec_timeout(dev, ent);
1145 
1146 out_err:
1147 	err = ent->ret;
1148 
1149 	if (err == -ETIMEDOUT) {
1150 		mlx5_core_warn(dev, "%s(0x%x) timeout. Will cause a leak of a command resource\n",
1151 			       mlx5_command_str(ent->op), ent->op);
1152 	} else if (err == -ECANCELED) {
1153 		mlx5_core_warn(dev, "%s(0x%x) canceled on out of queue timeout.\n",
1154 			       mlx5_command_str(ent->op), ent->op);
1155 	}
1156 	mlx5_core_dbg(dev, "err %d, delivery status %s(%d)\n",
1157 		      err, deliv_status_to_str(ent->status), ent->status);
1158 
1159 	return err;
1160 }
1161 
1162 /*  Notes:
1163  *    1. Callback functions may not sleep
1164  *    2. page queue commands do not support asynchrous completion
1165  *
1166  * return value in case (!callback):
1167  *	ret < 0 : Command execution couldn't be submitted by driver
1168  *	ret > 0 : Command execution couldn't be performed by firmware
1169  *	ret == 0: Command was executed by FW, Caller must check FW outbox status.
1170  *
1171  * return value in case (callback):
1172  *	ret < 0 : Command execution couldn't be submitted by driver
1173  *	ret == 0: Command will be submitted to FW for execution
1174  *		  and the callback will be called for further status updates
1175  */
mlx5_cmd_invoke(struct mlx5_core_dev * dev,struct mlx5_cmd_msg * in,struct mlx5_cmd_msg * out,void * uout,int uout_size,mlx5_cmd_cbk_t callback,void * context,int page_queue,u8 token,bool force_polling)1176 static int mlx5_cmd_invoke(struct mlx5_core_dev *dev, struct mlx5_cmd_msg *in,
1177 			   struct mlx5_cmd_msg *out, void *uout, int uout_size,
1178 			   mlx5_cmd_cbk_t callback,
1179 			   void *context, int page_queue,
1180 			   u8 token, bool force_polling)
1181 {
1182 	struct mlx5_cmd *cmd = &dev->cmd;
1183 	struct mlx5_cmd_work_ent *ent;
1184 	struct mlx5_cmd_stats *stats;
1185 	u8 status = 0;
1186 	int err = 0;
1187 	s64 ds;
1188 
1189 	if (callback && page_queue)
1190 		return -EINVAL;
1191 
1192 	ent = cmd_alloc_ent(cmd, in, out, uout, uout_size,
1193 			    callback, context, page_queue);
1194 	if (IS_ERR(ent))
1195 		return PTR_ERR(ent);
1196 
1197 	/* put for this ent is when consumed, depending on the use case
1198 	 * 1) (!callback) blocking flow: by caller after wait_func completes
1199 	 * 2) (callback) flow: by mlx5_cmd_comp_handler() when ent is handled
1200 	 */
1201 
1202 	ent->token = token;
1203 	ent->polling = force_polling;
1204 
1205 	init_completion(&ent->handling);
1206 	if (!callback)
1207 		init_completion(&ent->done);
1208 
1209 	INIT_DELAYED_WORK(&ent->cb_timeout_work, cb_timeout_handler);
1210 	INIT_WORK(&ent->work, cmd_work_handler);
1211 	if (page_queue) {
1212 		cmd_work_handler(&ent->work);
1213 	} else if (!queue_work(cmd->wq, &ent->work)) {
1214 		mlx5_core_warn(dev, "failed to queue work\n");
1215 		err = -EALREADY;
1216 		goto out_free;
1217 	}
1218 
1219 	if (callback)
1220 		return 0; /* mlx5_cmd_comp_handler() will put(ent) */
1221 
1222 	err = wait_func(dev, ent);
1223 	if (err == -ETIMEDOUT || err == -ECANCELED)
1224 		goto out_free;
1225 
1226 	ds = ent->ts2 - ent->ts1;
1227 	if (ent->op < MLX5_CMD_OP_MAX) {
1228 		stats = &cmd->stats[ent->op];
1229 		spin_lock_irq(&stats->lock);
1230 		stats->sum += ds;
1231 		++stats->n;
1232 		spin_unlock_irq(&stats->lock);
1233 	}
1234 	mlx5_core_dbg_mask(dev, 1 << MLX5_CMD_TIME,
1235 			   "fw exec time for %s is %lld nsec\n",
1236 			   mlx5_command_str(ent->op), ds);
1237 
1238 out_free:
1239 	status = ent->status;
1240 	cmd_ent_put(ent);
1241 	return err ? : status;
1242 }
1243 
dbg_write(struct file * filp,const char __user * buf,size_t count,loff_t * pos)1244 static ssize_t dbg_write(struct file *filp, const char __user *buf,
1245 			 size_t count, loff_t *pos)
1246 {
1247 	struct mlx5_core_dev *dev = filp->private_data;
1248 	struct mlx5_cmd_debug *dbg = &dev->cmd.dbg;
1249 	char lbuf[3];
1250 	int err;
1251 
1252 	if (!dbg->in_msg || !dbg->out_msg)
1253 		return -ENOMEM;
1254 
1255 	if (count < sizeof(lbuf) - 1)
1256 		return -EINVAL;
1257 
1258 	if (copy_from_user(lbuf, buf, sizeof(lbuf) - 1))
1259 		return -EFAULT;
1260 
1261 	lbuf[sizeof(lbuf) - 1] = 0;
1262 
1263 	if (strcmp(lbuf, "go"))
1264 		return -EINVAL;
1265 
1266 	err = mlx5_cmd_exec(dev, dbg->in_msg, dbg->inlen, dbg->out_msg, dbg->outlen);
1267 
1268 	return err ? err : count;
1269 }
1270 
1271 static const struct file_operations fops = {
1272 	.owner	= THIS_MODULE,
1273 	.open	= simple_open,
1274 	.write	= dbg_write,
1275 };
1276 
mlx5_copy_to_msg(struct mlx5_cmd_msg * to,void * from,int size,u8 token)1277 static int mlx5_copy_to_msg(struct mlx5_cmd_msg *to, void *from, int size,
1278 			    u8 token)
1279 {
1280 	struct mlx5_cmd_prot_block *block;
1281 	struct mlx5_cmd_mailbox *next;
1282 	int copy;
1283 
1284 	if (!to || !from)
1285 		return -ENOMEM;
1286 
1287 	copy = min_t(int, size, sizeof(to->first.data));
1288 	memcpy(to->first.data, from, copy);
1289 	size -= copy;
1290 	from += copy;
1291 
1292 	next = to->next;
1293 	while (size) {
1294 		if (!next) {
1295 			/* this is a BUG */
1296 			return -ENOMEM;
1297 		}
1298 
1299 		copy = min_t(int, size, MLX5_CMD_DATA_BLOCK_SIZE);
1300 		block = next->buf;
1301 		memcpy(block->data, from, copy);
1302 		from += copy;
1303 		size -= copy;
1304 		block->token = token;
1305 		next = next->next;
1306 	}
1307 
1308 	return 0;
1309 }
1310 
mlx5_copy_from_msg(void * to,struct mlx5_cmd_msg * from,int size)1311 static int mlx5_copy_from_msg(void *to, struct mlx5_cmd_msg *from, int size)
1312 {
1313 	struct mlx5_cmd_prot_block *block;
1314 	struct mlx5_cmd_mailbox *next;
1315 	int copy;
1316 
1317 	if (!to || !from)
1318 		return -ENOMEM;
1319 
1320 	copy = min_t(int, size, sizeof(from->first.data));
1321 	memcpy(to, from->first.data, copy);
1322 	size -= copy;
1323 	to += copy;
1324 
1325 	next = from->next;
1326 	while (size) {
1327 		if (!next) {
1328 			/* this is a BUG */
1329 			return -ENOMEM;
1330 		}
1331 
1332 		copy = min_t(int, size, MLX5_CMD_DATA_BLOCK_SIZE);
1333 		block = next->buf;
1334 
1335 		memcpy(to, block->data, copy);
1336 		to += copy;
1337 		size -= copy;
1338 		next = next->next;
1339 	}
1340 
1341 	return 0;
1342 }
1343 
alloc_cmd_box(struct mlx5_core_dev * dev,gfp_t flags)1344 static struct mlx5_cmd_mailbox *alloc_cmd_box(struct mlx5_core_dev *dev,
1345 					      gfp_t flags)
1346 {
1347 	struct mlx5_cmd_mailbox *mailbox;
1348 
1349 	mailbox = kmalloc(sizeof(*mailbox), flags);
1350 	if (!mailbox)
1351 		return ERR_PTR(-ENOMEM);
1352 
1353 	mailbox->buf = dma_pool_zalloc(dev->cmd.pool, flags,
1354 				       &mailbox->dma);
1355 	if (!mailbox->buf) {
1356 		mlx5_core_dbg(dev, "failed allocation\n");
1357 		kfree(mailbox);
1358 		return ERR_PTR(-ENOMEM);
1359 	}
1360 	mailbox->next = NULL;
1361 
1362 	return mailbox;
1363 }
1364 
free_cmd_box(struct mlx5_core_dev * dev,struct mlx5_cmd_mailbox * mailbox)1365 static void free_cmd_box(struct mlx5_core_dev *dev,
1366 			 struct mlx5_cmd_mailbox *mailbox)
1367 {
1368 	dma_pool_free(dev->cmd.pool, mailbox->buf, mailbox->dma);
1369 	kfree(mailbox);
1370 }
1371 
mlx5_alloc_cmd_msg(struct mlx5_core_dev * dev,gfp_t flags,int size,u8 token)1372 static struct mlx5_cmd_msg *mlx5_alloc_cmd_msg(struct mlx5_core_dev *dev,
1373 					       gfp_t flags, int size,
1374 					       u8 token)
1375 {
1376 	struct mlx5_cmd_mailbox *tmp, *head = NULL;
1377 	struct mlx5_cmd_prot_block *block;
1378 	struct mlx5_cmd_msg *msg;
1379 	int err;
1380 	int n;
1381 	int i;
1382 
1383 	msg = kzalloc(sizeof(*msg), flags);
1384 	if (!msg)
1385 		return ERR_PTR(-ENOMEM);
1386 
1387 	msg->len = size;
1388 	n = mlx5_calc_cmd_blocks(msg);
1389 
1390 	for (i = 0; i < n; i++) {
1391 		tmp = alloc_cmd_box(dev, flags);
1392 		if (IS_ERR(tmp)) {
1393 			mlx5_core_warn(dev, "failed allocating block\n");
1394 			err = PTR_ERR(tmp);
1395 			goto err_alloc;
1396 		}
1397 
1398 		block = tmp->buf;
1399 		tmp->next = head;
1400 		block->next = cpu_to_be64(tmp->next ? tmp->next->dma : 0);
1401 		block->block_num = cpu_to_be32(n - i - 1);
1402 		block->token = token;
1403 		head = tmp;
1404 	}
1405 	msg->next = head;
1406 	return msg;
1407 
1408 err_alloc:
1409 	while (head) {
1410 		tmp = head->next;
1411 		free_cmd_box(dev, head);
1412 		head = tmp;
1413 	}
1414 	kfree(msg);
1415 
1416 	return ERR_PTR(err);
1417 }
1418 
mlx5_free_cmd_msg(struct mlx5_core_dev * dev,struct mlx5_cmd_msg * msg)1419 static void mlx5_free_cmd_msg(struct mlx5_core_dev *dev,
1420 			      struct mlx5_cmd_msg *msg)
1421 {
1422 	struct mlx5_cmd_mailbox *head = msg->next;
1423 	struct mlx5_cmd_mailbox *next;
1424 
1425 	while (head) {
1426 		next = head->next;
1427 		free_cmd_box(dev, head);
1428 		head = next;
1429 	}
1430 	kfree(msg);
1431 }
1432 
data_write(struct file * filp,const char __user * buf,size_t count,loff_t * pos)1433 static ssize_t data_write(struct file *filp, const char __user *buf,
1434 			  size_t count, loff_t *pos)
1435 {
1436 	struct mlx5_core_dev *dev = filp->private_data;
1437 	struct mlx5_cmd_debug *dbg = &dev->cmd.dbg;
1438 	void *ptr;
1439 
1440 	if (*pos != 0)
1441 		return -EINVAL;
1442 
1443 	kfree(dbg->in_msg);
1444 	dbg->in_msg = NULL;
1445 	dbg->inlen = 0;
1446 	ptr = memdup_user(buf, count);
1447 	if (IS_ERR(ptr))
1448 		return PTR_ERR(ptr);
1449 	dbg->in_msg = ptr;
1450 	dbg->inlen = count;
1451 
1452 	*pos = count;
1453 
1454 	return count;
1455 }
1456 
data_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)1457 static ssize_t data_read(struct file *filp, char __user *buf, size_t count,
1458 			 loff_t *pos)
1459 {
1460 	struct mlx5_core_dev *dev = filp->private_data;
1461 	struct mlx5_cmd_debug *dbg = &dev->cmd.dbg;
1462 
1463 	if (!dbg->out_msg)
1464 		return -ENOMEM;
1465 
1466 	return simple_read_from_buffer(buf, count, pos, dbg->out_msg,
1467 				       dbg->outlen);
1468 }
1469 
1470 static const struct file_operations dfops = {
1471 	.owner	= THIS_MODULE,
1472 	.open	= simple_open,
1473 	.write	= data_write,
1474 	.read	= data_read,
1475 };
1476 
outlen_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)1477 static ssize_t outlen_read(struct file *filp, char __user *buf, size_t count,
1478 			   loff_t *pos)
1479 {
1480 	struct mlx5_core_dev *dev = filp->private_data;
1481 	struct mlx5_cmd_debug *dbg = &dev->cmd.dbg;
1482 	char outlen[8];
1483 	int err;
1484 
1485 	err = snprintf(outlen, sizeof(outlen), "%d", dbg->outlen);
1486 	if (err < 0)
1487 		return err;
1488 
1489 	return simple_read_from_buffer(buf, count, pos, outlen, err);
1490 }
1491 
outlen_write(struct file * filp,const char __user * buf,size_t count,loff_t * pos)1492 static ssize_t outlen_write(struct file *filp, const char __user *buf,
1493 			    size_t count, loff_t *pos)
1494 {
1495 	struct mlx5_core_dev *dev = filp->private_data;
1496 	struct mlx5_cmd_debug *dbg = &dev->cmd.dbg;
1497 	char outlen_str[8] = {0};
1498 	int outlen;
1499 	void *ptr;
1500 	int err;
1501 
1502 	if (*pos != 0 || count > 6)
1503 		return -EINVAL;
1504 
1505 	kfree(dbg->out_msg);
1506 	dbg->out_msg = NULL;
1507 	dbg->outlen = 0;
1508 
1509 	if (copy_from_user(outlen_str, buf, count))
1510 		return -EFAULT;
1511 
1512 	err = sscanf(outlen_str, "%d", &outlen);
1513 	if (err != 1)
1514 		return -EINVAL;
1515 
1516 	ptr = kzalloc(outlen, GFP_KERNEL);
1517 	if (!ptr)
1518 		return -ENOMEM;
1519 
1520 	dbg->out_msg = ptr;
1521 	dbg->outlen = outlen;
1522 
1523 	*pos = count;
1524 
1525 	return count;
1526 }
1527 
1528 static const struct file_operations olfops = {
1529 	.owner	= THIS_MODULE,
1530 	.open	= simple_open,
1531 	.write	= outlen_write,
1532 	.read	= outlen_read,
1533 };
1534 
set_wqname(struct mlx5_core_dev * dev)1535 static void set_wqname(struct mlx5_core_dev *dev)
1536 {
1537 	struct mlx5_cmd *cmd = &dev->cmd;
1538 
1539 	snprintf(cmd->wq_name, sizeof(cmd->wq_name), "mlx5_cmd_%s",
1540 		 dev_name(dev->device));
1541 }
1542 
clean_debug_files(struct mlx5_core_dev * dev)1543 static void clean_debug_files(struct mlx5_core_dev *dev)
1544 {
1545 	struct mlx5_cmd_debug *dbg = &dev->cmd.dbg;
1546 
1547 	if (!mlx5_debugfs_root)
1548 		return;
1549 
1550 	mlx5_cmdif_debugfs_cleanup(dev);
1551 	debugfs_remove_recursive(dbg->dbg_root);
1552 }
1553 
create_debugfs_files(struct mlx5_core_dev * dev)1554 static void create_debugfs_files(struct mlx5_core_dev *dev)
1555 {
1556 	struct mlx5_cmd_debug *dbg = &dev->cmd.dbg;
1557 
1558 	dbg->dbg_root = debugfs_create_dir("cmd", mlx5_debugfs_get_dev_root(dev));
1559 
1560 	debugfs_create_file("in", 0400, dbg->dbg_root, dev, &dfops);
1561 	debugfs_create_file("out", 0200, dbg->dbg_root, dev, &dfops);
1562 	debugfs_create_file("out_len", 0600, dbg->dbg_root, dev, &olfops);
1563 	debugfs_create_u8("status", 0600, dbg->dbg_root, &dbg->status);
1564 	debugfs_create_file("run", 0200, dbg->dbg_root, dev, &fops);
1565 
1566 	mlx5_cmdif_debugfs_init(dev);
1567 }
1568 
mlx5_cmd_allowed_opcode(struct mlx5_core_dev * dev,u16 opcode)1569 void mlx5_cmd_allowed_opcode(struct mlx5_core_dev *dev, u16 opcode)
1570 {
1571 	struct mlx5_cmd *cmd = &dev->cmd;
1572 	int i;
1573 
1574 	for (i = 0; i < cmd->vars.max_reg_cmds; i++)
1575 		down(&cmd->vars.sem);
1576 	down(&cmd->vars.pages_sem);
1577 
1578 	cmd->allowed_opcode = opcode;
1579 
1580 	up(&cmd->vars.pages_sem);
1581 	for (i = 0; i < cmd->vars.max_reg_cmds; i++)
1582 		up(&cmd->vars.sem);
1583 }
1584 
mlx5_cmd_change_mod(struct mlx5_core_dev * dev,int mode)1585 static void mlx5_cmd_change_mod(struct mlx5_core_dev *dev, int mode)
1586 {
1587 	struct mlx5_cmd *cmd = &dev->cmd;
1588 	int i;
1589 
1590 	for (i = 0; i < cmd->vars.max_reg_cmds; i++)
1591 		down(&cmd->vars.sem);
1592 	down(&cmd->vars.pages_sem);
1593 
1594 	cmd->mode = mode;
1595 
1596 	up(&cmd->vars.pages_sem);
1597 	for (i = 0; i < cmd->vars.max_reg_cmds; i++)
1598 		up(&cmd->vars.sem);
1599 }
1600 
cmd_comp_notifier(struct notifier_block * nb,unsigned long type,void * data)1601 static int cmd_comp_notifier(struct notifier_block *nb,
1602 			     unsigned long type, void *data)
1603 {
1604 	struct mlx5_core_dev *dev;
1605 	struct mlx5_cmd *cmd;
1606 	struct mlx5_eqe *eqe;
1607 
1608 	cmd = mlx5_nb_cof(nb, struct mlx5_cmd, nb);
1609 	dev = container_of(cmd, struct mlx5_core_dev, cmd);
1610 	eqe = data;
1611 
1612 	mlx5_cmd_comp_handler(dev, be32_to_cpu(eqe->data.cmd.vector), false);
1613 
1614 	return NOTIFY_OK;
1615 }
mlx5_cmd_use_events(struct mlx5_core_dev * dev)1616 void mlx5_cmd_use_events(struct mlx5_core_dev *dev)
1617 {
1618 	MLX5_NB_INIT(&dev->cmd.nb, cmd_comp_notifier, CMD);
1619 	mlx5_eq_notifier_register(dev, &dev->cmd.nb);
1620 	mlx5_cmd_change_mod(dev, CMD_MODE_EVENTS);
1621 }
1622 
mlx5_cmd_use_polling(struct mlx5_core_dev * dev)1623 void mlx5_cmd_use_polling(struct mlx5_core_dev *dev)
1624 {
1625 	mlx5_cmd_change_mod(dev, CMD_MODE_POLLING);
1626 	mlx5_eq_notifier_unregister(dev, &dev->cmd.nb);
1627 }
1628 
free_msg(struct mlx5_core_dev * dev,struct mlx5_cmd_msg * msg)1629 static void free_msg(struct mlx5_core_dev *dev, struct mlx5_cmd_msg *msg)
1630 {
1631 	unsigned long flags;
1632 
1633 	if (msg->parent) {
1634 		spin_lock_irqsave(&msg->parent->lock, flags);
1635 		list_add_tail(&msg->list, &msg->parent->head);
1636 		spin_unlock_irqrestore(&msg->parent->lock, flags);
1637 	} else {
1638 		mlx5_free_cmd_msg(dev, msg);
1639 	}
1640 }
1641 
mlx5_cmd_comp_handler(struct mlx5_core_dev * dev,u64 vec,bool forced)1642 static void mlx5_cmd_comp_handler(struct mlx5_core_dev *dev, u64 vec, bool forced)
1643 {
1644 	struct mlx5_cmd *cmd = &dev->cmd;
1645 	struct mlx5_cmd_work_ent *ent;
1646 	mlx5_cmd_cbk_t callback;
1647 	void *context;
1648 	int err;
1649 	int i;
1650 	s64 ds;
1651 	struct mlx5_cmd_stats *stats;
1652 	unsigned long flags;
1653 	unsigned long vector;
1654 
1655 	/* there can be at most 32 command queues */
1656 	vector = vec & 0xffffffff;
1657 	for (i = 0; i < (1 << cmd->vars.log_sz); i++) {
1658 		if (test_bit(i, &vector)) {
1659 			ent = cmd->ent_arr[i];
1660 
1661 			/* if we already completed the command, ignore it */
1662 			if (!test_and_clear_bit(MLX5_CMD_ENT_STATE_PENDING_COMP,
1663 						&ent->state)) {
1664 				/* only real completion can free the cmd slot */
1665 				if (!forced) {
1666 					mlx5_core_err(dev, "Command completion arrived after timeout (entry idx = %d).\n",
1667 						      ent->idx);
1668 					cmd_ent_put(ent);
1669 				}
1670 				continue;
1671 			}
1672 
1673 			if (ent->callback && cancel_delayed_work(&ent->cb_timeout_work))
1674 				cmd_ent_put(ent); /* timeout work was canceled */
1675 
1676 			if (!forced || /* Real FW completion */
1677 			     mlx5_cmd_is_down(dev) || /* No real FW completion is expected */
1678 			     !opcode_allowed(cmd, ent->op))
1679 				cmd_ent_put(ent);
1680 
1681 			ent->ts2 = ktime_get_ns();
1682 			memcpy(ent->out->first.data, ent->lay->out, sizeof(ent->lay->out));
1683 			dump_command(dev, ent, 0);
1684 
1685 			if (vec & MLX5_TRIGGERED_CMD_COMP)
1686 				ent->ret = -ENXIO;
1687 
1688 			if (!ent->ret) { /* Command completed by FW */
1689 				if (!cmd->checksum_disabled)
1690 					ent->ret = verify_signature(ent);
1691 
1692 				ent->status = ent->lay->status_own >> 1;
1693 
1694 				mlx5_core_dbg(dev, "command completed. ret 0x%x, delivery status %s(0x%x)\n",
1695 					      ent->ret, deliv_status_to_str(ent->status), ent->status);
1696 			}
1697 
1698 			if (ent->callback) {
1699 				ds = ent->ts2 - ent->ts1;
1700 				if (ent->op < MLX5_CMD_OP_MAX) {
1701 					stats = &cmd->stats[ent->op];
1702 					spin_lock_irqsave(&stats->lock, flags);
1703 					stats->sum += ds;
1704 					++stats->n;
1705 					spin_unlock_irqrestore(&stats->lock, flags);
1706 				}
1707 
1708 				callback = ent->callback;
1709 				context = ent->context;
1710 				err = ent->ret ? : ent->status;
1711 				if (err > 0) /* Failed in FW, command didn't execute */
1712 					err = deliv_status_to_err(err);
1713 
1714 				if (!err)
1715 					err = mlx5_copy_from_msg(ent->uout,
1716 								 ent->out,
1717 								 ent->uout_size);
1718 
1719 				mlx5_free_cmd_msg(dev, ent->out);
1720 				free_msg(dev, ent->in);
1721 
1722 				/* final consumer is done, release ent */
1723 				cmd_ent_put(ent);
1724 				callback(err, context);
1725 			} else {
1726 				/* release wait_func() so mlx5_cmd_invoke()
1727 				 * can make the final ent_put()
1728 				 */
1729 				complete(&ent->done);
1730 			}
1731 		}
1732 	}
1733 }
1734 
mlx5_cmd_trigger_completions(struct mlx5_core_dev * dev)1735 static void mlx5_cmd_trigger_completions(struct mlx5_core_dev *dev)
1736 {
1737 	struct mlx5_cmd *cmd = &dev->cmd;
1738 	unsigned long bitmask;
1739 	unsigned long flags;
1740 	u64 vector;
1741 	int i;
1742 
1743 	/* wait for pending handlers to complete */
1744 	mlx5_eq_synchronize_cmd_irq(dev);
1745 	spin_lock_irqsave(&dev->cmd.alloc_lock, flags);
1746 	vector = ~dev->cmd.vars.bitmask & ((1ul << (1 << dev->cmd.vars.log_sz)) - 1);
1747 	if (!vector)
1748 		goto no_trig;
1749 
1750 	bitmask = vector;
1751 	/* we must increment the allocated entries refcount before triggering the completions
1752 	 * to guarantee pending commands will not get freed in the meanwhile.
1753 	 * For that reason, it also has to be done inside the alloc_lock.
1754 	 */
1755 	for_each_set_bit(i, &bitmask, (1 << cmd->vars.log_sz))
1756 		cmd_ent_get(cmd->ent_arr[i]);
1757 	vector |= MLX5_TRIGGERED_CMD_COMP;
1758 	spin_unlock_irqrestore(&dev->cmd.alloc_lock, flags);
1759 
1760 	mlx5_core_dbg(dev, "vector 0x%llx\n", vector);
1761 	mlx5_cmd_comp_handler(dev, vector, true);
1762 	for_each_set_bit(i, &bitmask, (1 << cmd->vars.log_sz))
1763 		cmd_ent_put(cmd->ent_arr[i]);
1764 	return;
1765 
1766 no_trig:
1767 	spin_unlock_irqrestore(&dev->cmd.alloc_lock, flags);
1768 }
1769 
mlx5_cmd_flush(struct mlx5_core_dev * dev)1770 void mlx5_cmd_flush(struct mlx5_core_dev *dev)
1771 {
1772 	struct mlx5_cmd *cmd = &dev->cmd;
1773 	int i;
1774 
1775 	for (i = 0; i < cmd->vars.max_reg_cmds; i++) {
1776 		while (down_trylock(&cmd->vars.sem)) {
1777 			mlx5_cmd_trigger_completions(dev);
1778 			cond_resched();
1779 		}
1780 	}
1781 
1782 	while (down_trylock(&cmd->vars.pages_sem)) {
1783 		mlx5_cmd_trigger_completions(dev);
1784 		cond_resched();
1785 	}
1786 
1787 	/* Unlock cmdif */
1788 	up(&cmd->vars.pages_sem);
1789 	for (i = 0; i < cmd->vars.max_reg_cmds; i++)
1790 		up(&cmd->vars.sem);
1791 }
1792 
alloc_msg(struct mlx5_core_dev * dev,int in_size,gfp_t gfp)1793 static struct mlx5_cmd_msg *alloc_msg(struct mlx5_core_dev *dev, int in_size,
1794 				      gfp_t gfp)
1795 {
1796 	struct mlx5_cmd_msg *msg = ERR_PTR(-ENOMEM);
1797 	struct cmd_msg_cache *ch = NULL;
1798 	struct mlx5_cmd *cmd = &dev->cmd;
1799 	int i;
1800 
1801 	if (in_size <= 16)
1802 		goto cache_miss;
1803 
1804 	for (i = 0; i < MLX5_NUM_COMMAND_CACHES; i++) {
1805 		ch = &cmd->cache[i];
1806 		if (in_size > ch->max_inbox_size)
1807 			continue;
1808 		spin_lock_irq(&ch->lock);
1809 		if (list_empty(&ch->head)) {
1810 			spin_unlock_irq(&ch->lock);
1811 			continue;
1812 		}
1813 		msg = list_entry(ch->head.next, typeof(*msg), list);
1814 		/* For cached lists, we must explicitly state what is
1815 		 * the real size
1816 		 */
1817 		msg->len = in_size;
1818 		list_del(&msg->list);
1819 		spin_unlock_irq(&ch->lock);
1820 		break;
1821 	}
1822 
1823 	if (!IS_ERR(msg))
1824 		return msg;
1825 
1826 cache_miss:
1827 	msg = mlx5_alloc_cmd_msg(dev, gfp, in_size, 0);
1828 	return msg;
1829 }
1830 
is_manage_pages(void * in)1831 static int is_manage_pages(void *in)
1832 {
1833 	return in_to_opcode(in) == MLX5_CMD_OP_MANAGE_PAGES;
1834 }
1835 
1836 /*  Notes:
1837  *    1. Callback functions may not sleep
1838  *    2. Page queue commands do not support asynchrous completion
1839  */
cmd_exec(struct mlx5_core_dev * dev,void * in,int in_size,void * out,int out_size,mlx5_cmd_cbk_t callback,void * context,bool force_polling)1840 static int cmd_exec(struct mlx5_core_dev *dev, void *in, int in_size, void *out,
1841 		    int out_size, mlx5_cmd_cbk_t callback, void *context,
1842 		    bool force_polling)
1843 {
1844 	struct mlx5_cmd_msg *inb, *outb;
1845 	u16 opcode = in_to_opcode(in);
1846 	bool throttle_op;
1847 	int pages_queue;
1848 	gfp_t gfp;
1849 	u8 token;
1850 	int err;
1851 
1852 	if (mlx5_cmd_is_down(dev) || !opcode_allowed(&dev->cmd, opcode))
1853 		return -ENXIO;
1854 
1855 	throttle_op = mlx5_cmd_is_throttle_opcode(opcode);
1856 	if (throttle_op) {
1857 		/* atomic context may not sleep */
1858 		if (callback)
1859 			return -EINVAL;
1860 		down(&dev->cmd.vars.throttle_sem);
1861 	}
1862 
1863 	pages_queue = is_manage_pages(in);
1864 	gfp = callback ? GFP_ATOMIC : GFP_KERNEL;
1865 
1866 	inb = alloc_msg(dev, in_size, gfp);
1867 	if (IS_ERR(inb)) {
1868 		err = PTR_ERR(inb);
1869 		goto out_up;
1870 	}
1871 
1872 	token = alloc_token(&dev->cmd);
1873 
1874 	err = mlx5_copy_to_msg(inb, in, in_size, token);
1875 	if (err) {
1876 		mlx5_core_warn(dev, "err %d\n", err);
1877 		goto out_in;
1878 	}
1879 
1880 	outb = mlx5_alloc_cmd_msg(dev, gfp, out_size, token);
1881 	if (IS_ERR(outb)) {
1882 		err = PTR_ERR(outb);
1883 		goto out_in;
1884 	}
1885 
1886 	err = mlx5_cmd_invoke(dev, inb, outb, out, out_size, callback, context,
1887 			      pages_queue, token, force_polling);
1888 	if (callback)
1889 		return err;
1890 
1891 	if (err > 0) /* Failed in FW, command didn't execute */
1892 		err = deliv_status_to_err(err);
1893 
1894 	if (err)
1895 		goto out_out;
1896 
1897 	/* command completed by FW */
1898 	err = mlx5_copy_from_msg(out, outb, out_size);
1899 out_out:
1900 	mlx5_free_cmd_msg(dev, outb);
1901 out_in:
1902 	free_msg(dev, inb);
1903 out_up:
1904 	if (throttle_op)
1905 		up(&dev->cmd.vars.throttle_sem);
1906 	return err;
1907 }
1908 
mlx5_cmd_err_trace(struct mlx5_core_dev * dev,u16 opcode,u16 op_mod,void * out)1909 static void mlx5_cmd_err_trace(struct mlx5_core_dev *dev, u16 opcode, u16 op_mod, void *out)
1910 {
1911 	u32 syndrome = MLX5_GET(mbox_out, out, syndrome);
1912 	u8 status = MLX5_GET(mbox_out, out, status);
1913 
1914 	trace_mlx5_cmd(mlx5_command_str(opcode), opcode, op_mod,
1915 		       cmd_status_str(status), status, syndrome,
1916 		       cmd_status_to_err(status));
1917 }
1918 
cmd_status_log(struct mlx5_core_dev * dev,u16 opcode,u8 status,u32 syndrome,int err)1919 static void cmd_status_log(struct mlx5_core_dev *dev, u16 opcode, u8 status,
1920 			   u32 syndrome, int err)
1921 {
1922 	const char *namep = mlx5_command_str(opcode);
1923 	struct mlx5_cmd_stats *stats;
1924 
1925 	if (!err || !(strcmp(namep, "unknown command opcode")))
1926 		return;
1927 
1928 	stats = &dev->cmd.stats[opcode];
1929 	spin_lock_irq(&stats->lock);
1930 	stats->failed++;
1931 	if (err < 0)
1932 		stats->last_failed_errno = -err;
1933 	if (err == -EREMOTEIO) {
1934 		stats->failed_mbox_status++;
1935 		stats->last_failed_mbox_status = status;
1936 		stats->last_failed_syndrome = syndrome;
1937 	}
1938 	spin_unlock_irq(&stats->lock);
1939 }
1940 
1941 /* preserve -EREMOTEIO for outbox.status != OK, otherwise return err as is */
cmd_status_err(struct mlx5_core_dev * dev,int err,u16 opcode,u16 op_mod,void * out)1942 static int cmd_status_err(struct mlx5_core_dev *dev, int err, u16 opcode, u16 op_mod, void *out)
1943 {
1944 	u32 syndrome = MLX5_GET(mbox_out, out, syndrome);
1945 	u8 status = MLX5_GET(mbox_out, out, status);
1946 
1947 	if (err == -EREMOTEIO) /* -EREMOTEIO is preserved */
1948 		err = -EIO;
1949 
1950 	if (!err && status != MLX5_CMD_STAT_OK) {
1951 		err = -EREMOTEIO;
1952 		mlx5_cmd_err_trace(dev, opcode, op_mod, out);
1953 	}
1954 
1955 	cmd_status_log(dev, opcode, status, syndrome, err);
1956 	return err;
1957 }
1958 
1959 /**
1960  * mlx5_cmd_do - Executes a fw command, wait for completion.
1961  * Unlike mlx5_cmd_exec, this function will not translate or intercept
1962  * outbox.status and will return -EREMOTEIO when
1963  * outbox.status != MLX5_CMD_STAT_OK
1964  *
1965  * @dev: mlx5 core device
1966  * @in: inbox mlx5_ifc command buffer
1967  * @in_size: inbox buffer size
1968  * @out: outbox mlx5_ifc buffer
1969  * @out_size: outbox size
1970  *
1971  * @return:
1972  * -EREMOTEIO : Command executed by FW, outbox.status != MLX5_CMD_STAT_OK.
1973  *              Caller must check FW outbox status.
1974  *   0 : Command execution successful, outbox.status == MLX5_CMD_STAT_OK.
1975  * < 0 : Command execution couldn't be performed by firmware or driver
1976  */
mlx5_cmd_do(struct mlx5_core_dev * dev,void * in,int in_size,void * out,int out_size)1977 int mlx5_cmd_do(struct mlx5_core_dev *dev, void *in, int in_size, void *out, int out_size)
1978 {
1979 	int err = cmd_exec(dev, in, in_size, out, out_size, NULL, NULL, false);
1980 	u16 op_mod = MLX5_GET(mbox_in, in, op_mod);
1981 	u16 opcode = in_to_opcode(in);
1982 
1983 	return cmd_status_err(dev, err, opcode, op_mod, out);
1984 }
1985 EXPORT_SYMBOL(mlx5_cmd_do);
1986 
1987 /**
1988  * mlx5_cmd_exec - Executes a fw command, wait for completion
1989  *
1990  * @dev: mlx5 core device
1991  * @in: inbox mlx5_ifc command buffer
1992  * @in_size: inbox buffer size
1993  * @out: outbox mlx5_ifc buffer
1994  * @out_size: outbox size
1995  *
1996  * @return: 0 if no error, FW command execution was successful
1997  *          and outbox status is ok.
1998  */
mlx5_cmd_exec(struct mlx5_core_dev * dev,void * in,int in_size,void * out,int out_size)1999 int mlx5_cmd_exec(struct mlx5_core_dev *dev, void *in, int in_size, void *out,
2000 		  int out_size)
2001 {
2002 	int err = mlx5_cmd_do(dev, in, in_size, out, out_size);
2003 
2004 	return mlx5_cmd_check(dev, err, in, out);
2005 }
2006 EXPORT_SYMBOL(mlx5_cmd_exec);
2007 
2008 /**
2009  * mlx5_cmd_exec_polling - Executes a fw command, poll for completion
2010  *	Needed for driver force teardown, when command completion EQ
2011  *	will not be available to complete the command
2012  *
2013  * @dev: mlx5 core device
2014  * @in: inbox mlx5_ifc command buffer
2015  * @in_size: inbox buffer size
2016  * @out: outbox mlx5_ifc buffer
2017  * @out_size: outbox size
2018  *
2019  * @return: 0 if no error, FW command execution was successful
2020  *          and outbox status is ok.
2021  */
mlx5_cmd_exec_polling(struct mlx5_core_dev * dev,void * in,int in_size,void * out,int out_size)2022 int mlx5_cmd_exec_polling(struct mlx5_core_dev *dev, void *in, int in_size,
2023 			  void *out, int out_size)
2024 {
2025 	int err = cmd_exec(dev, in, in_size, out, out_size, NULL, NULL, true);
2026 	u16 op_mod = MLX5_GET(mbox_in, in, op_mod);
2027 	u16 opcode = in_to_opcode(in);
2028 
2029 	err = cmd_status_err(dev, err, opcode, op_mod, out);
2030 	return mlx5_cmd_check(dev, err, in, out);
2031 }
2032 EXPORT_SYMBOL(mlx5_cmd_exec_polling);
2033 
mlx5_cmd_init_async_ctx(struct mlx5_core_dev * dev,struct mlx5_async_ctx * ctx)2034 void mlx5_cmd_init_async_ctx(struct mlx5_core_dev *dev,
2035 			     struct mlx5_async_ctx *ctx)
2036 {
2037 	ctx->dev = dev;
2038 	/* Starts at 1 to avoid doing wake_up if we are not cleaning up */
2039 	atomic_set(&ctx->num_inflight, 1);
2040 	init_completion(&ctx->inflight_done);
2041 }
2042 EXPORT_SYMBOL(mlx5_cmd_init_async_ctx);
2043 
2044 /**
2045  * mlx5_cmd_cleanup_async_ctx - Clean up an async_ctx
2046  * @ctx: The ctx to clean
2047  *
2048  * Upon return all callbacks given to mlx5_cmd_exec_cb() have been called. The
2049  * caller must ensure that mlx5_cmd_exec_cb() is not called during or after
2050  * the call mlx5_cleanup_async_ctx().
2051  */
mlx5_cmd_cleanup_async_ctx(struct mlx5_async_ctx * ctx)2052 void mlx5_cmd_cleanup_async_ctx(struct mlx5_async_ctx *ctx)
2053 {
2054 	if (!atomic_dec_and_test(&ctx->num_inflight))
2055 		wait_for_completion(&ctx->inflight_done);
2056 }
2057 EXPORT_SYMBOL(mlx5_cmd_cleanup_async_ctx);
2058 
mlx5_cmd_exec_cb_handler(int status,void * _work)2059 static void mlx5_cmd_exec_cb_handler(int status, void *_work)
2060 {
2061 	struct mlx5_async_work *work = _work;
2062 	struct mlx5_async_ctx *ctx;
2063 
2064 	ctx = work->ctx;
2065 	status = cmd_status_err(ctx->dev, status, work->opcode, work->op_mod, work->out);
2066 	work->user_callback(status, work);
2067 	if (atomic_dec_and_test(&ctx->num_inflight))
2068 		complete(&ctx->inflight_done);
2069 }
2070 
mlx5_cmd_exec_cb(struct mlx5_async_ctx * ctx,void * in,int in_size,void * out,int out_size,mlx5_async_cbk_t callback,struct mlx5_async_work * work)2071 int mlx5_cmd_exec_cb(struct mlx5_async_ctx *ctx, void *in, int in_size,
2072 		     void *out, int out_size, mlx5_async_cbk_t callback,
2073 		     struct mlx5_async_work *work)
2074 {
2075 	int ret;
2076 
2077 	work->ctx = ctx;
2078 	work->user_callback = callback;
2079 	work->opcode = in_to_opcode(in);
2080 	work->op_mod = MLX5_GET(mbox_in, in, op_mod);
2081 	work->out = out;
2082 	if (WARN_ON(!atomic_inc_not_zero(&ctx->num_inflight)))
2083 		return -EIO;
2084 	ret = cmd_exec(ctx->dev, in, in_size, out, out_size,
2085 		       mlx5_cmd_exec_cb_handler, work, false);
2086 	if (ret && atomic_dec_and_test(&ctx->num_inflight))
2087 		complete(&ctx->inflight_done);
2088 
2089 	return ret;
2090 }
2091 EXPORT_SYMBOL(mlx5_cmd_exec_cb);
2092 
destroy_msg_cache(struct mlx5_core_dev * dev)2093 static void destroy_msg_cache(struct mlx5_core_dev *dev)
2094 {
2095 	struct cmd_msg_cache *ch;
2096 	struct mlx5_cmd_msg *msg;
2097 	struct mlx5_cmd_msg *n;
2098 	int i;
2099 
2100 	for (i = 0; i < MLX5_NUM_COMMAND_CACHES; i++) {
2101 		ch = &dev->cmd.cache[i];
2102 		list_for_each_entry_safe(msg, n, &ch->head, list) {
2103 			list_del(&msg->list);
2104 			mlx5_free_cmd_msg(dev, msg);
2105 		}
2106 	}
2107 }
2108 
2109 static unsigned cmd_cache_num_ent[MLX5_NUM_COMMAND_CACHES] = {
2110 	512, 32, 16, 8, 2
2111 };
2112 
2113 static unsigned cmd_cache_ent_size[MLX5_NUM_COMMAND_CACHES] = {
2114 	16 + MLX5_CMD_DATA_BLOCK_SIZE,
2115 	16 + MLX5_CMD_DATA_BLOCK_SIZE * 2,
2116 	16 + MLX5_CMD_DATA_BLOCK_SIZE * 16,
2117 	16 + MLX5_CMD_DATA_BLOCK_SIZE * 256,
2118 	16 + MLX5_CMD_DATA_BLOCK_SIZE * 512,
2119 };
2120 
create_msg_cache(struct mlx5_core_dev * dev)2121 static void create_msg_cache(struct mlx5_core_dev *dev)
2122 {
2123 	struct mlx5_cmd *cmd = &dev->cmd;
2124 	struct cmd_msg_cache *ch;
2125 	struct mlx5_cmd_msg *msg;
2126 	int i;
2127 	int k;
2128 
2129 	/* Initialize and fill the caches with initial entries */
2130 	for (k = 0; k < MLX5_NUM_COMMAND_CACHES; k++) {
2131 		ch = &cmd->cache[k];
2132 		spin_lock_init(&ch->lock);
2133 		INIT_LIST_HEAD(&ch->head);
2134 		ch->num_ent = cmd_cache_num_ent[k];
2135 		ch->max_inbox_size = cmd_cache_ent_size[k];
2136 		for (i = 0; i < ch->num_ent; i++) {
2137 			msg = mlx5_alloc_cmd_msg(dev, GFP_KERNEL | __GFP_NOWARN,
2138 						 ch->max_inbox_size, 0);
2139 			if (IS_ERR(msg))
2140 				break;
2141 			msg->parent = ch;
2142 			list_add_tail(&msg->list, &ch->head);
2143 		}
2144 	}
2145 }
2146 
alloc_cmd_page(struct mlx5_core_dev * dev,struct mlx5_cmd * cmd)2147 static int alloc_cmd_page(struct mlx5_core_dev *dev, struct mlx5_cmd *cmd)
2148 {
2149 	cmd->cmd_alloc_buf = dma_alloc_coherent(mlx5_core_dma_dev(dev), MLX5_ADAPTER_PAGE_SIZE,
2150 						&cmd->alloc_dma, GFP_KERNEL);
2151 	if (!cmd->cmd_alloc_buf)
2152 		return -ENOMEM;
2153 
2154 	/* make sure it is aligned to 4K */
2155 	if (!((uintptr_t)cmd->cmd_alloc_buf & (MLX5_ADAPTER_PAGE_SIZE - 1))) {
2156 		cmd->cmd_buf = cmd->cmd_alloc_buf;
2157 		cmd->dma = cmd->alloc_dma;
2158 		cmd->alloc_size = MLX5_ADAPTER_PAGE_SIZE;
2159 		return 0;
2160 	}
2161 
2162 	dma_free_coherent(mlx5_core_dma_dev(dev), MLX5_ADAPTER_PAGE_SIZE, cmd->cmd_alloc_buf,
2163 			  cmd->alloc_dma);
2164 	cmd->cmd_alloc_buf = dma_alloc_coherent(mlx5_core_dma_dev(dev),
2165 						2 * MLX5_ADAPTER_PAGE_SIZE - 1,
2166 						&cmd->alloc_dma, GFP_KERNEL);
2167 	if (!cmd->cmd_alloc_buf)
2168 		return -ENOMEM;
2169 
2170 	cmd->cmd_buf = PTR_ALIGN(cmd->cmd_alloc_buf, MLX5_ADAPTER_PAGE_SIZE);
2171 	cmd->dma = ALIGN(cmd->alloc_dma, MLX5_ADAPTER_PAGE_SIZE);
2172 	cmd->alloc_size = 2 * MLX5_ADAPTER_PAGE_SIZE - 1;
2173 	return 0;
2174 }
2175 
free_cmd_page(struct mlx5_core_dev * dev,struct mlx5_cmd * cmd)2176 static void free_cmd_page(struct mlx5_core_dev *dev, struct mlx5_cmd *cmd)
2177 {
2178 	dma_free_coherent(mlx5_core_dma_dev(dev), cmd->alloc_size, cmd->cmd_alloc_buf,
2179 			  cmd->alloc_dma);
2180 }
2181 
cmdif_rev(struct mlx5_core_dev * dev)2182 static u16 cmdif_rev(struct mlx5_core_dev *dev)
2183 {
2184 	return ioread32be(&dev->iseg->cmdif_rev_fw_sub) >> 16;
2185 }
2186 
mlx5_cmd_init(struct mlx5_core_dev * dev)2187 int mlx5_cmd_init(struct mlx5_core_dev *dev)
2188 {
2189 	int size = sizeof(struct mlx5_cmd_prot_block);
2190 	int align = roundup_pow_of_two(size);
2191 	struct mlx5_cmd *cmd = &dev->cmd;
2192 	u32 cmd_h, cmd_l;
2193 	u16 cmd_if_rev;
2194 	int err;
2195 	int i;
2196 
2197 	memset(cmd, 0, sizeof(*cmd));
2198 	cmd_if_rev = cmdif_rev(dev);
2199 	if (cmd_if_rev != CMD_IF_REV) {
2200 		mlx5_core_err(dev,
2201 			      "Driver cmdif rev(%d) differs from firmware's(%d)\n",
2202 			      CMD_IF_REV, cmd_if_rev);
2203 		return -EINVAL;
2204 	}
2205 
2206 	cmd->pool = dma_pool_create("mlx5_cmd", mlx5_core_dma_dev(dev), size, align, 0);
2207 	if (!cmd->pool)
2208 		return -ENOMEM;
2209 
2210 	err = alloc_cmd_page(dev, cmd);
2211 	if (err)
2212 		goto err_free_pool;
2213 
2214 	cmd_l = ioread32be(&dev->iseg->cmdq_addr_l_sz) & 0xff;
2215 	cmd->vars.log_sz = cmd_l >> 4 & 0xf;
2216 	cmd->vars.log_stride = cmd_l & 0xf;
2217 	if (1 << cmd->vars.log_sz > MLX5_MAX_COMMANDS) {
2218 		mlx5_core_err(dev, "firmware reports too many outstanding commands %d\n",
2219 			      1 << cmd->vars.log_sz);
2220 		err = -EINVAL;
2221 		goto err_free_page;
2222 	}
2223 
2224 	if (cmd->vars.log_sz + cmd->vars.log_stride > MLX5_ADAPTER_PAGE_SHIFT) {
2225 		mlx5_core_err(dev, "command queue size overflow\n");
2226 		err = -EINVAL;
2227 		goto err_free_page;
2228 	}
2229 
2230 	cmd->state = MLX5_CMDIF_STATE_DOWN;
2231 	cmd->checksum_disabled = 1;
2232 	cmd->vars.max_reg_cmds = (1 << cmd->vars.log_sz) - 1;
2233 	cmd->vars.bitmask = (1UL << cmd->vars.max_reg_cmds) - 1;
2234 
2235 	cmd->vars.cmdif_rev = ioread32be(&dev->iseg->cmdif_rev_fw_sub) >> 16;
2236 	if (cmd->vars.cmdif_rev > CMD_IF_REV) {
2237 		mlx5_core_err(dev, "driver does not support command interface version. driver %d, firmware %d\n",
2238 			      CMD_IF_REV, cmd->vars.cmdif_rev);
2239 		err = -EOPNOTSUPP;
2240 		goto err_free_page;
2241 	}
2242 
2243 	spin_lock_init(&cmd->alloc_lock);
2244 	spin_lock_init(&cmd->token_lock);
2245 	for (i = 0; i < MLX5_CMD_OP_MAX; i++)
2246 		spin_lock_init(&cmd->stats[i].lock);
2247 
2248 	sema_init(&cmd->vars.sem, cmd->vars.max_reg_cmds);
2249 	sema_init(&cmd->vars.pages_sem, 1);
2250 	sema_init(&cmd->vars.throttle_sem, DIV_ROUND_UP(cmd->vars.max_reg_cmds, 2));
2251 
2252 	cmd_h = (u32)((u64)(cmd->dma) >> 32);
2253 	cmd_l = (u32)(cmd->dma);
2254 	if (cmd_l & 0xfff) {
2255 		mlx5_core_err(dev, "invalid command queue address\n");
2256 		err = -ENOMEM;
2257 		goto err_free_page;
2258 	}
2259 
2260 	iowrite32be(cmd_h, &dev->iseg->cmdq_addr_h);
2261 	iowrite32be(cmd_l, &dev->iseg->cmdq_addr_l_sz);
2262 
2263 	/* Make sure firmware sees the complete address before we proceed */
2264 	wmb();
2265 
2266 	mlx5_core_dbg(dev, "descriptor at dma 0x%llx\n", (unsigned long long)(cmd->dma));
2267 
2268 	cmd->mode = CMD_MODE_POLLING;
2269 	cmd->allowed_opcode = CMD_ALLOWED_OPCODE_ALL;
2270 
2271 	create_msg_cache(dev);
2272 
2273 	set_wqname(dev);
2274 	cmd->wq = create_singlethread_workqueue(cmd->wq_name);
2275 	if (!cmd->wq) {
2276 		mlx5_core_err(dev, "failed to create command workqueue\n");
2277 		err = -ENOMEM;
2278 		goto err_cache;
2279 	}
2280 
2281 	create_debugfs_files(dev);
2282 
2283 	return 0;
2284 
2285 err_cache:
2286 	destroy_msg_cache(dev);
2287 
2288 err_free_page:
2289 	free_cmd_page(dev, cmd);
2290 
2291 err_free_pool:
2292 	dma_pool_destroy(cmd->pool);
2293 	return err;
2294 }
2295 
mlx5_cmd_cleanup(struct mlx5_core_dev * dev)2296 void mlx5_cmd_cleanup(struct mlx5_core_dev *dev)
2297 {
2298 	struct mlx5_cmd *cmd = &dev->cmd;
2299 
2300 	clean_debug_files(dev);
2301 	destroy_workqueue(cmd->wq);
2302 	destroy_msg_cache(dev);
2303 	free_cmd_page(dev, cmd);
2304 	dma_pool_destroy(cmd->pool);
2305 }
2306 
mlx5_cmd_set_state(struct mlx5_core_dev * dev,enum mlx5_cmdif_state cmdif_state)2307 void mlx5_cmd_set_state(struct mlx5_core_dev *dev,
2308 			enum mlx5_cmdif_state cmdif_state)
2309 {
2310 	dev->cmd.state = cmdif_state;
2311 }
2312