blob: 68ae765873a9617becd0804749718a5ae3529b11 [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Jack Morgenstein51a379d2008-07-25 10:32:52 -07003 * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
Roland Dreier225c7b12007-05-08 18:00:38 -07004 * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Paul Gortmakeree40fa02011-05-27 16:14:23 -040037#include <linux/export.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070038#include <linux/pci.h>
39#include <linux/errno.h>
40
41#include <linux/mlx4/cmd.h>
Rony Efraim948e3062013-06-13 13:19:11 +030042#include <linux/mlx4/device.h>
Yevgeny Petriline8f081a2011-12-13 04:12:25 +000043#include <linux/semaphore.h>
Jack Morgenstein0a9a0182012-08-03 08:40:45 +000044#include <rdma/ib_smi.h>
Yishai Hadas55ad3592015-01-25 16:59:42 +020045#include <linux/delay.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070046
47#include <asm/io.h>
48
49#include "mlx4.h"
Yevgeny Petriline8f081a2011-12-13 04:12:25 +000050#include "fw.h"
Ido Shamay08068cd2015-04-02 16:31:15 +030051#include "fw_qos.h"
Roland Dreier225c7b12007-05-08 18:00:38 -070052
53#define CMD_POLL_TOKEN 0xffff
Yevgeny Petriline8f081a2011-12-13 04:12:25 +000054#define INBOX_MASK 0xffffffffffffff00ULL
55
56#define CMD_CHAN_VER 1
57#define CMD_CHAN_IF_REV 1
Roland Dreier225c7b12007-05-08 18:00:38 -070058
59enum {
60 /* command completed successfully: */
61 CMD_STAT_OK = 0x00,
62 /* Internal error (such as a bus error) occurred while processing command: */
63 CMD_STAT_INTERNAL_ERR = 0x01,
64 /* Operation/command not supported or opcode modifier not supported: */
65 CMD_STAT_BAD_OP = 0x02,
66 /* Parameter not supported or parameter out of range: */
67 CMD_STAT_BAD_PARAM = 0x03,
68 /* System not enabled or bad system state: */
69 CMD_STAT_BAD_SYS_STATE = 0x04,
70 /* Attempt to access reserved or unallocaterd resource: */
71 CMD_STAT_BAD_RESOURCE = 0x05,
72 /* Requested resource is currently executing a command, or is otherwise busy: */
73 CMD_STAT_RESOURCE_BUSY = 0x06,
74 /* Required capability exceeds device limits: */
75 CMD_STAT_EXCEED_LIM = 0x08,
76 /* Resource is not in the appropriate state or ownership: */
77 CMD_STAT_BAD_RES_STATE = 0x09,
78 /* Index out of range: */
79 CMD_STAT_BAD_INDEX = 0x0a,
80 /* FW image corrupted: */
81 CMD_STAT_BAD_NVMEM = 0x0b,
Jack Morgenstein899698d2008-07-22 14:19:39 -070082 /* Error in ICM mapping (e.g. not enough auxiliary ICM pages to execute command): */
83 CMD_STAT_ICM_ERROR = 0x0c,
Roland Dreier225c7b12007-05-08 18:00:38 -070084 /* Attempt to modify a QP/EE which is not in the presumed state: */
85 CMD_STAT_BAD_QP_STATE = 0x10,
86 /* Bad segment parameters (Address/Size): */
87 CMD_STAT_BAD_SEG_PARAM = 0x20,
88 /* Memory Region has Memory Windows bound to: */
89 CMD_STAT_REG_BOUND = 0x21,
90 /* HCA local attached memory not present: */
91 CMD_STAT_LAM_NOT_PRE = 0x22,
92 /* Bad management packet (silently discarded): */
93 CMD_STAT_BAD_PKT = 0x30,
94 /* More outstanding CQEs in CQ than new CQ size: */
Yevgeny Petrilincc4ac2e2009-07-06 16:10:03 -070095 CMD_STAT_BAD_SIZE = 0x40,
96 /* Multi Function device support required: */
97 CMD_STAT_MULTI_FUNC_REQ = 0x50,
Roland Dreier225c7b12007-05-08 18:00:38 -070098};
99
100enum {
101 HCR_IN_PARAM_OFFSET = 0x00,
102 HCR_IN_MODIFIER_OFFSET = 0x08,
103 HCR_OUT_PARAM_OFFSET = 0x0c,
104 HCR_TOKEN_OFFSET = 0x14,
105 HCR_STATUS_OFFSET = 0x18,
106
107 HCR_OPMOD_SHIFT = 12,
108 HCR_T_BIT = 21,
109 HCR_E_BIT = 22,
110 HCR_GO_BIT = 23
111};
112
113enum {
Dotan Barak36ce10d2007-08-07 11:18:52 +0300114 GO_BIT_TIMEOUT_MSECS = 10000
Roland Dreier225c7b12007-05-08 18:00:38 -0700115};
116
Jack Morgensteinb01978c2013-06-27 19:05:21 +0300117enum mlx4_vlan_transition {
118 MLX4_VLAN_TRANSITION_VST_VST = 0,
119 MLX4_VLAN_TRANSITION_VST_VGT = 1,
120 MLX4_VLAN_TRANSITION_VGT_VST = 2,
121 MLX4_VLAN_TRANSITION_VGT_VGT = 3,
122};
123
124
Roland Dreier225c7b12007-05-08 18:00:38 -0700125struct mlx4_cmd_context {
126 struct completion done;
127 int result;
128 int next;
129 u64 out_param;
130 u16 token;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000131 u8 fw_status;
Roland Dreier225c7b12007-05-08 18:00:38 -0700132};
133
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000134static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
135 struct mlx4_vhcr_cmd *in_vhcr);
136
Roland Dreierca281212008-04-16 21:01:04 -0700137static int mlx4_status_to_errno(u8 status)
138{
Roland Dreier225c7b12007-05-08 18:00:38 -0700139 static const int trans_table[] = {
140 [CMD_STAT_INTERNAL_ERR] = -EIO,
141 [CMD_STAT_BAD_OP] = -EPERM,
142 [CMD_STAT_BAD_PARAM] = -EINVAL,
143 [CMD_STAT_BAD_SYS_STATE] = -ENXIO,
144 [CMD_STAT_BAD_RESOURCE] = -EBADF,
145 [CMD_STAT_RESOURCE_BUSY] = -EBUSY,
146 [CMD_STAT_EXCEED_LIM] = -ENOMEM,
147 [CMD_STAT_BAD_RES_STATE] = -EBADF,
148 [CMD_STAT_BAD_INDEX] = -EBADF,
149 [CMD_STAT_BAD_NVMEM] = -EFAULT,
Jack Morgenstein899698d2008-07-22 14:19:39 -0700150 [CMD_STAT_ICM_ERROR] = -ENFILE,
Roland Dreier225c7b12007-05-08 18:00:38 -0700151 [CMD_STAT_BAD_QP_STATE] = -EINVAL,
152 [CMD_STAT_BAD_SEG_PARAM] = -EFAULT,
153 [CMD_STAT_REG_BOUND] = -EBUSY,
154 [CMD_STAT_LAM_NOT_PRE] = -EAGAIN,
155 [CMD_STAT_BAD_PKT] = -EINVAL,
156 [CMD_STAT_BAD_SIZE] = -ENOMEM,
Yevgeny Petrilincc4ac2e2009-07-06 16:10:03 -0700157 [CMD_STAT_MULTI_FUNC_REQ] = -EACCES,
Roland Dreier225c7b12007-05-08 18:00:38 -0700158 };
159
160 if (status >= ARRAY_SIZE(trans_table) ||
161 (status != CMD_STAT_OK && trans_table[status] == 0))
162 return -EIO;
163
164 return trans_table[status];
165}
166
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000167static u8 mlx4_errno_to_status(int errno)
168{
169 switch (errno) {
170 case -EPERM:
171 return CMD_STAT_BAD_OP;
172 case -EINVAL:
173 return CMD_STAT_BAD_PARAM;
174 case -ENXIO:
175 return CMD_STAT_BAD_SYS_STATE;
176 case -EBUSY:
177 return CMD_STAT_RESOURCE_BUSY;
178 case -ENOMEM:
179 return CMD_STAT_EXCEED_LIM;
180 case -ENFILE:
181 return CMD_STAT_ICM_ERROR;
182 default:
183 return CMD_STAT_INTERNAL_ERR;
184 }
185}
186
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200187static int mlx4_internal_err_ret_value(struct mlx4_dev *dev, u16 op,
188 u8 op_modifier)
189{
190 switch (op) {
191 case MLX4_CMD_UNMAP_ICM:
192 case MLX4_CMD_UNMAP_ICM_AUX:
193 case MLX4_CMD_UNMAP_FA:
194 case MLX4_CMD_2RST_QP:
195 case MLX4_CMD_HW2SW_EQ:
196 case MLX4_CMD_HW2SW_CQ:
197 case MLX4_CMD_HW2SW_SRQ:
198 case MLX4_CMD_HW2SW_MPT:
199 case MLX4_CMD_CLOSE_HCA:
200 case MLX4_QP_FLOW_STEERING_DETACH:
201 case MLX4_CMD_FREE_RES:
202 case MLX4_CMD_CLOSE_PORT:
203 return CMD_STAT_OK;
204
205 case MLX4_CMD_QP_ATTACH:
206 /* On Detach case return success */
207 if (op_modifier == 0)
208 return CMD_STAT_OK;
209 return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
210
211 default:
212 return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
213 }
214}
215
216static int mlx4_closing_cmd_fatal_error(u16 op, u8 fw_status)
217{
218 /* Any error during the closing commands below is considered fatal */
219 if (op == MLX4_CMD_CLOSE_HCA ||
220 op == MLX4_CMD_HW2SW_EQ ||
221 op == MLX4_CMD_HW2SW_CQ ||
222 op == MLX4_CMD_2RST_QP ||
223 op == MLX4_CMD_HW2SW_SRQ ||
224 op == MLX4_CMD_SYNC_TPT ||
225 op == MLX4_CMD_UNMAP_ICM ||
226 op == MLX4_CMD_UNMAP_ICM_AUX ||
227 op == MLX4_CMD_UNMAP_FA)
228 return 1;
229 /* Error on MLX4_CMD_HW2SW_MPT is fatal except when fw status equals
230 * CMD_STAT_REG_BOUND.
231 * This status indicates that memory region has memory windows bound to it
232 * which may result from invalid user space usage and is not fatal.
233 */
234 if (op == MLX4_CMD_HW2SW_MPT && fw_status != CMD_STAT_REG_BOUND)
235 return 1;
236 return 0;
237}
238
239static int mlx4_cmd_reset_flow(struct mlx4_dev *dev, u16 op, u8 op_modifier,
240 int err)
241{
242 /* Only if reset flow is really active return code is based on
243 * command, otherwise current error code is returned.
244 */
245 if (mlx4_internal_err_reset) {
246 mlx4_enter_error_state(dev->persist);
247 err = mlx4_internal_err_ret_value(dev, op, op_modifier);
248 }
249
250 return err;
251}
252
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000253static int comm_pending(struct mlx4_dev *dev)
254{
255 struct mlx4_priv *priv = mlx4_priv(dev);
256 u32 status = readl(&priv->mfunc.comm->slave_read);
257
258 return (swab32(status) >> 31) != priv->cmd.comm_toggle;
259}
260
Yishai Hadas0cd93022015-01-25 16:59:43 +0200261static int mlx4_comm_cmd_post(struct mlx4_dev *dev, u8 cmd, u16 param)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000262{
263 struct mlx4_priv *priv = mlx4_priv(dev);
264 u32 val;
265
Yishai Hadas0cd93022015-01-25 16:59:43 +0200266 /* To avoid writing to unknown addresses after the device state was
267 * changed to internal error and the function was rest,
268 * check the INTERNAL_ERROR flag which is updated under
269 * device_state_mutex lock.
270 */
271 mutex_lock(&dev->persist->device_state_mutex);
272
273 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
274 mutex_unlock(&dev->persist->device_state_mutex);
275 return -EIO;
276 }
277
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000278 priv->cmd.comm_toggle ^= 1;
279 val = param | (cmd << 16) | (priv->cmd.comm_toggle << 31);
280 __raw_writel((__force u32) cpu_to_be32(val),
281 &priv->mfunc.comm->slave_write);
282 mmiowb();
Yishai Hadas0cd93022015-01-25 16:59:43 +0200283 mutex_unlock(&dev->persist->device_state_mutex);
284 return 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000285}
286
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000287static int mlx4_comm_cmd_poll(struct mlx4_dev *dev, u8 cmd, u16 param,
288 unsigned long timeout)
289{
290 struct mlx4_priv *priv = mlx4_priv(dev);
291 unsigned long end;
292 int err = 0;
293 int ret_from_pending = 0;
294
295 /* First, verify that the master reports correct status */
296 if (comm_pending(dev)) {
Joe Perches1a91de22014-05-07 12:52:57 -0700297 mlx4_warn(dev, "Communication channel is not idle - my toggle is %d (cmd:0x%x)\n",
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000298 priv->cmd.comm_toggle, cmd);
299 return -EAGAIN;
300 }
301
302 /* Write command */
303 down(&priv->cmd.poll_sem);
Yishai Hadas0cd93022015-01-25 16:59:43 +0200304 if (mlx4_comm_cmd_post(dev, cmd, param)) {
305 /* Only in case the device state is INTERNAL_ERROR,
306 * mlx4_comm_cmd_post returns with an error
307 */
308 err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
309 goto out;
310 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000311
312 end = msecs_to_jiffies(timeout) + jiffies;
313 while (comm_pending(dev) && time_before(jiffies, end))
314 cond_resched();
315 ret_from_pending = comm_pending(dev);
316 if (ret_from_pending) {
317 /* check if the slave is trying to boot in the middle of
318 * FLR process. The only non-zero result in the RESET command
319 * is MLX4_DELAY_RESET_SLAVE*/
320 if ((MLX4_COMM_CMD_RESET == cmd)) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000321 err = MLX4_DELAY_RESET_SLAVE;
Yishai Hadas0cd93022015-01-25 16:59:43 +0200322 goto out;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000323 } else {
Yishai Hadas0cd93022015-01-25 16:59:43 +0200324 mlx4_warn(dev, "Communication channel command 0x%x timed out\n",
325 cmd);
326 err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000327 }
328 }
329
Yishai Hadas0cd93022015-01-25 16:59:43 +0200330 if (err)
331 mlx4_enter_error_state(dev->persist);
332out:
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000333 up(&priv->cmd.poll_sem);
334 return err;
335}
336
Yishai Hadas0cd93022015-01-25 16:59:43 +0200337static int mlx4_comm_cmd_wait(struct mlx4_dev *dev, u8 vhcr_cmd,
338 u16 param, u16 op, unsigned long timeout)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000339{
340 struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
341 struct mlx4_cmd_context *context;
Eugenia Emantayev58a3de02012-03-18 04:32:08 +0000342 unsigned long end;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000343 int err = 0;
344
345 down(&cmd->event_sem);
346
347 spin_lock(&cmd->context_lock);
348 BUG_ON(cmd->free_head < 0);
349 context = &cmd->context[cmd->free_head];
350 context->token += cmd->token_mask + 1;
351 cmd->free_head = context->next;
352 spin_unlock(&cmd->context_lock);
353
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200354 reinit_completion(&context->done);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000355
Yishai Hadas0cd93022015-01-25 16:59:43 +0200356 if (mlx4_comm_cmd_post(dev, vhcr_cmd, param)) {
357 /* Only in case the device state is INTERNAL_ERROR,
358 * mlx4_comm_cmd_post returns with an error
359 */
360 err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
361 goto out;
362 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000363
364 if (!wait_for_completion_timeout(&context->done,
365 msecs_to_jiffies(timeout))) {
Yishai Hadas0cd93022015-01-25 16:59:43 +0200366 mlx4_warn(dev, "communication channel command 0x%x (op=0x%x) timed out\n",
367 vhcr_cmd, op);
368 goto out_reset;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000369 }
370
371 err = context->result;
372 if (err && context->fw_status != CMD_STAT_MULTI_FUNC_REQ) {
373 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
Yishai Hadas0cd93022015-01-25 16:59:43 +0200374 vhcr_cmd, context->fw_status);
375 if (mlx4_closing_cmd_fatal_error(op, context->fw_status))
376 goto out_reset;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000377 }
378
Eugenia Emantayev58a3de02012-03-18 04:32:08 +0000379 /* wait for comm channel ready
380 * this is necessary for prevention the race
381 * when switching between event to polling mode
Yishai Hadas0cd93022015-01-25 16:59:43 +0200382 * Skipping this section in case the device is in FATAL_ERROR state,
383 * In this state, no commands are sent via the comm channel until
384 * the device has returned from reset.
Eugenia Emantayev58a3de02012-03-18 04:32:08 +0000385 */
Yishai Hadas0cd93022015-01-25 16:59:43 +0200386 if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)) {
387 end = msecs_to_jiffies(timeout) + jiffies;
388 while (comm_pending(dev) && time_before(jiffies, end))
389 cond_resched();
390 }
391 goto out;
Eugenia Emantayev58a3de02012-03-18 04:32:08 +0000392
Yishai Hadas0cd93022015-01-25 16:59:43 +0200393out_reset:
394 err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
395 mlx4_enter_error_state(dev->persist);
396out:
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000397 spin_lock(&cmd->context_lock);
398 context->next = cmd->free_head;
399 cmd->free_head = context - cmd->context;
400 spin_unlock(&cmd->context_lock);
401
402 up(&cmd->event_sem);
403 return err;
404}
405
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000406int mlx4_comm_cmd(struct mlx4_dev *dev, u8 cmd, u16 param,
Yishai Hadas0cd93022015-01-25 16:59:43 +0200407 u16 op, unsigned long timeout)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000408{
Yishai Hadas0cd93022015-01-25 16:59:43 +0200409 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
410 return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
411
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000412 if (mlx4_priv(dev)->cmd.use_events)
Yishai Hadas0cd93022015-01-25 16:59:43 +0200413 return mlx4_comm_cmd_wait(dev, cmd, param, op, timeout);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000414 return mlx4_comm_cmd_poll(dev, cmd, param, timeout);
415}
416
Roland Dreier225c7b12007-05-08 18:00:38 -0700417static int cmd_pending(struct mlx4_dev *dev)
418{
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000419 u32 status;
420
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200421 if (pci_channel_offline(dev->persist->pdev))
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000422 return -EIO;
423
424 status = readl(mlx4_priv(dev)->cmd.hcr + HCR_STATUS_OFFSET);
Roland Dreier225c7b12007-05-08 18:00:38 -0700425
426 return (status & swab32(1 << HCR_GO_BIT)) ||
427 (mlx4_priv(dev)->cmd.toggle ==
428 !!(status & swab32(1 << HCR_T_BIT)));
429}
430
431static int mlx4_cmd_post(struct mlx4_dev *dev, u64 in_param, u64 out_param,
432 u32 in_modifier, u8 op_modifier, u16 op, u16 token,
433 int event)
434{
435 struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
436 u32 __iomem *hcr = cmd->hcr;
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200437 int ret = -EIO;
Roland Dreier225c7b12007-05-08 18:00:38 -0700438 unsigned long end;
439
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200440 mutex_lock(&dev->persist->device_state_mutex);
441 /* To avoid writing to unknown addresses after the device state was
442 * changed to internal error and the chip was reset,
443 * check the INTERNAL_ERROR flag which is updated under
444 * device_state_mutex lock.
445 */
446 if (pci_channel_offline(dev->persist->pdev) ||
447 (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)) {
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000448 /*
449 * Device is going through error recovery
450 * and cannot accept commands.
451 */
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000452 goto out;
453 }
454
Roland Dreier225c7b12007-05-08 18:00:38 -0700455 end = jiffies;
456 if (event)
Dotan Barak36ce10d2007-08-07 11:18:52 +0300457 end += msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS);
Roland Dreier225c7b12007-05-08 18:00:38 -0700458
459 while (cmd_pending(dev)) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200460 if (pci_channel_offline(dev->persist->pdev)) {
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000461 /*
462 * Device is going through error recovery
463 * and cannot accept commands.
464 */
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000465 goto out;
466 }
467
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000468 if (time_after_eq(jiffies, end)) {
469 mlx4_err(dev, "%s:cmd_pending failed\n", __func__);
Roland Dreier225c7b12007-05-08 18:00:38 -0700470 goto out;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000471 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700472 cond_resched();
473 }
474
475 /*
476 * We use writel (instead of something like memcpy_toio)
477 * because writes of less than 32 bits to the HCR don't work
478 * (and some architectures such as ia64 implement memcpy_toio
479 * in terms of writeb).
480 */
481 __raw_writel((__force u32) cpu_to_be32(in_param >> 32), hcr + 0);
482 __raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful), hcr + 1);
483 __raw_writel((__force u32) cpu_to_be32(in_modifier), hcr + 2);
484 __raw_writel((__force u32) cpu_to_be32(out_param >> 32), hcr + 3);
485 __raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), hcr + 4);
486 __raw_writel((__force u32) cpu_to_be32(token << 16), hcr + 5);
487
488 /* __raw_writel may not order writes. */
489 wmb();
490
491 __raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT) |
492 (cmd->toggle << HCR_T_BIT) |
493 (event ? (1 << HCR_E_BIT) : 0) |
494 (op_modifier << HCR_OPMOD_SHIFT) |
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000495 op), hcr + 6);
Roland Dreier2e61c642007-10-09 19:59:18 -0700496
497 /*
498 * Make sure that our HCR writes don't get mixed in with
499 * writes from another CPU starting a FW command.
500 */
501 mmiowb();
502
Roland Dreier225c7b12007-05-08 18:00:38 -0700503 cmd->toggle = cmd->toggle ^ 1;
504
505 ret = 0;
506
507out:
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200508 if (ret)
509 mlx4_warn(dev, "Could not post command 0x%x: ret=%d, in_param=0x%llx, in_mod=0x%x, op_mod=0x%x\n",
510 op, ret, in_param, in_modifier, op_modifier);
511 mutex_unlock(&dev->persist->device_state_mutex);
512
Roland Dreier225c7b12007-05-08 18:00:38 -0700513 return ret;
514}
515
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000516static int mlx4_slave_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
517 int out_is_imm, u32 in_modifier, u8 op_modifier,
518 u16 op, unsigned long timeout)
519{
520 struct mlx4_priv *priv = mlx4_priv(dev);
521 struct mlx4_vhcr_cmd *vhcr = priv->mfunc.vhcr;
522 int ret;
523
Roland Dreierf3d4c892012-09-25 21:24:07 -0700524 mutex_lock(&priv->cmd.slave_cmd_mutex);
525
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000526 vhcr->in_param = cpu_to_be64(in_param);
527 vhcr->out_param = out_param ? cpu_to_be64(*out_param) : 0;
528 vhcr->in_modifier = cpu_to_be32(in_modifier);
529 vhcr->opcode = cpu_to_be16((((u16) op_modifier) << 12) | (op & 0xfff));
530 vhcr->token = cpu_to_be16(CMD_POLL_TOKEN);
531 vhcr->status = 0;
532 vhcr->flags = !!(priv->cmd.use_events) << 6;
Roland Dreierf3d4c892012-09-25 21:24:07 -0700533
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000534 if (mlx4_is_master(dev)) {
535 ret = mlx4_master_process_vhcr(dev, dev->caps.function, vhcr);
536 if (!ret) {
537 if (out_is_imm) {
538 if (out_param)
539 *out_param =
540 be64_to_cpu(vhcr->out_param);
541 else {
Joe Perches1a91de22014-05-07 12:52:57 -0700542 mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
543 op);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000544 vhcr->status = CMD_STAT_BAD_PARAM;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000545 }
546 }
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000547 ret = mlx4_status_to_errno(vhcr->status);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000548 }
Yishai Hadas0cd93022015-01-25 16:59:43 +0200549 if (ret &&
550 dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
551 ret = mlx4_internal_err_ret_value(dev, op, op_modifier);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000552 } else {
Yishai Hadas0cd93022015-01-25 16:59:43 +0200553 ret = mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_POST, 0, op,
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000554 MLX4_COMM_TIME + timeout);
555 if (!ret) {
556 if (out_is_imm) {
557 if (out_param)
558 *out_param =
559 be64_to_cpu(vhcr->out_param);
560 else {
Joe Perches1a91de22014-05-07 12:52:57 -0700561 mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
562 op);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000563 vhcr->status = CMD_STAT_BAD_PARAM;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000564 }
565 }
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000566 ret = mlx4_status_to_errno(vhcr->status);
Yishai Hadas0cd93022015-01-25 16:59:43 +0200567 } else {
568 if (dev->persist->state &
569 MLX4_DEVICE_STATE_INTERNAL_ERROR)
570 ret = mlx4_internal_err_ret_value(dev, op,
571 op_modifier);
572 else
573 mlx4_err(dev, "failed execution of VHCR_POST command opcode 0x%x\n", op);
574 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000575 }
Roland Dreierf3d4c892012-09-25 21:24:07 -0700576
577 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000578 return ret;
579}
580
Roland Dreier225c7b12007-05-08 18:00:38 -0700581static int mlx4_cmd_poll(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
582 int out_is_imm, u32 in_modifier, u8 op_modifier,
583 u16 op, unsigned long timeout)
584{
585 struct mlx4_priv *priv = mlx4_priv(dev);
586 void __iomem *hcr = priv->cmd.hcr;
587 int err = 0;
588 unsigned long end;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000589 u32 stat;
Roland Dreier225c7b12007-05-08 18:00:38 -0700590
591 down(&priv->cmd.poll_sem);
592
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200593 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000594 /*
595 * Device is going through error recovery
596 * and cannot accept commands.
597 */
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200598 err = mlx4_internal_err_ret_value(dev, op, op_modifier);
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000599 goto out;
600 }
601
Eyal Perryc05a1162014-05-14 12:15:13 +0300602 if (out_is_imm && !out_param) {
603 mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
604 op);
605 err = -EINVAL;
606 goto out;
607 }
608
Roland Dreier225c7b12007-05-08 18:00:38 -0700609 err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
610 in_modifier, op_modifier, op, CMD_POLL_TOKEN, 0);
611 if (err)
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200612 goto out_reset;
Roland Dreier225c7b12007-05-08 18:00:38 -0700613
614 end = msecs_to_jiffies(timeout) + jiffies;
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000615 while (cmd_pending(dev) && time_before(jiffies, end)) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200616 if (pci_channel_offline(dev->persist->pdev)) {
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000617 /*
618 * Device is going through error recovery
619 * and cannot accept commands.
620 */
621 err = -EIO;
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200622 goto out_reset;
623 }
624
625 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
626 err = mlx4_internal_err_ret_value(dev, op, op_modifier);
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000627 goto out;
628 }
629
Roland Dreier225c7b12007-05-08 18:00:38 -0700630 cond_resched();
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000631 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700632
633 if (cmd_pending(dev)) {
Dotan Barak674925e2013-06-25 12:09:37 +0300634 mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
635 op);
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200636 err = -EIO;
637 goto out_reset;
Roland Dreier225c7b12007-05-08 18:00:38 -0700638 }
639
640 if (out_is_imm)
641 *out_param =
642 (u64) be32_to_cpu((__force __be32)
643 __raw_readl(hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
644 (u64) be32_to_cpu((__force __be32)
645 __raw_readl(hcr + HCR_OUT_PARAM_OFFSET + 4));
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000646 stat = be32_to_cpu((__force __be32)
647 __raw_readl(hcr + HCR_STATUS_OFFSET)) >> 24;
648 err = mlx4_status_to_errno(stat);
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200649 if (err) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000650 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
651 op, stat);
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200652 if (mlx4_closing_cmd_fatal_error(op, stat))
653 goto out_reset;
654 goto out;
655 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700656
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200657out_reset:
658 if (err)
659 err = mlx4_cmd_reset_flow(dev, op, op_modifier, err);
Roland Dreier225c7b12007-05-08 18:00:38 -0700660out:
661 up(&priv->cmd.poll_sem);
662 return err;
663}
664
665void mlx4_cmd_event(struct mlx4_dev *dev, u16 token, u8 status, u64 out_param)
666{
667 struct mlx4_priv *priv = mlx4_priv(dev);
668 struct mlx4_cmd_context *context =
669 &priv->cmd.context[token & priv->cmd.token_mask];
670
671 /* previously timed out command completing at long last */
672 if (token != context->token)
673 return;
674
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000675 context->fw_status = status;
Roland Dreier225c7b12007-05-08 18:00:38 -0700676 context->result = mlx4_status_to_errno(status);
677 context->out_param = out_param;
678
Roland Dreier225c7b12007-05-08 18:00:38 -0700679 complete(&context->done);
680}
681
682static int mlx4_cmd_wait(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
683 int out_is_imm, u32 in_modifier, u8 op_modifier,
684 u16 op, unsigned long timeout)
685{
686 struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
687 struct mlx4_cmd_context *context;
688 int err = 0;
689
690 down(&cmd->event_sem);
691
692 spin_lock(&cmd->context_lock);
693 BUG_ON(cmd->free_head < 0);
694 context = &cmd->context[cmd->free_head];
Roland Dreier09815822007-07-20 21:19:43 -0700695 context->token += cmd->token_mask + 1;
Roland Dreier225c7b12007-05-08 18:00:38 -0700696 cmd->free_head = context->next;
697 spin_unlock(&cmd->context_lock);
698
Eyal Perryc05a1162014-05-14 12:15:13 +0300699 if (out_is_imm && !out_param) {
700 mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
701 op);
702 err = -EINVAL;
703 goto out;
704 }
705
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200706 reinit_completion(&context->done);
Roland Dreier225c7b12007-05-08 18:00:38 -0700707
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200708 err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
709 in_modifier, op_modifier, op, context->token, 1);
710 if (err)
711 goto out_reset;
Roland Dreier225c7b12007-05-08 18:00:38 -0700712
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000713 if (!wait_for_completion_timeout(&context->done,
714 msecs_to_jiffies(timeout))) {
Dotan Barak674925e2013-06-25 12:09:37 +0300715 mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
716 op);
Benjamin Poirierf4ecf292015-05-22 16:12:26 -0700717 if (op == MLX4_CMD_NOP) {
718 err = -EBUSY;
719 goto out;
720 } else {
721 err = -EIO;
722 goto out_reset;
723 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700724 }
725
726 err = context->result;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000727 if (err) {
Jack Morgenstein1daa4302014-09-30 12:03:50 +0300728 /* Since we do not want to have this error message always
729 * displayed at driver start when there are ConnectX2 HCAs
730 * on the host, we deprecate the error message for this
731 * specific command/input_mod/opcode_mod/fw-status to be debug.
732 */
Jack Morgensteinfde913e2015-04-05 17:50:48 +0300733 if (op == MLX4_CMD_SET_PORT &&
734 (in_modifier == 1 || in_modifier == 2) &&
Ido Shamaya130b592015-04-02 16:31:19 +0300735 op_modifier == MLX4_SET_PORT_IB_OPCODE &&
736 context->fw_status == CMD_STAT_BAD_SIZE)
Jack Morgenstein1daa4302014-09-30 12:03:50 +0300737 mlx4_dbg(dev, "command 0x%x failed: fw status = 0x%x\n",
738 op, context->fw_status);
739 else
740 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
741 op, context->fw_status);
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200742 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
743 err = mlx4_internal_err_ret_value(dev, op, op_modifier);
744 else if (mlx4_closing_cmd_fatal_error(op, context->fw_status))
745 goto out_reset;
746
Roland Dreier225c7b12007-05-08 18:00:38 -0700747 goto out;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000748 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700749
750 if (out_is_imm)
751 *out_param = context->out_param;
752
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200753out_reset:
754 if (err)
755 err = mlx4_cmd_reset_flow(dev, op, op_modifier, err);
Roland Dreier225c7b12007-05-08 18:00:38 -0700756out:
757 spin_lock(&cmd->context_lock);
758 context->next = cmd->free_head;
759 cmd->free_head = context - cmd->context;
760 spin_unlock(&cmd->context_lock);
761
762 up(&cmd->event_sem);
763 return err;
764}
765
766int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
767 int out_is_imm, u32 in_modifier, u8 op_modifier,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000768 u16 op, unsigned long timeout, int native)
Roland Dreier225c7b12007-05-08 18:00:38 -0700769{
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200770 if (pci_channel_offline(dev->persist->pdev))
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200771 return mlx4_cmd_reset_flow(dev, op, op_modifier, -EIO);
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000772
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000773 if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200774 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
775 return mlx4_internal_err_ret_value(dev, op,
776 op_modifier);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000777 if (mlx4_priv(dev)->cmd.use_events)
778 return mlx4_cmd_wait(dev, in_param, out_param,
779 out_is_imm, in_modifier,
780 op_modifier, op, timeout);
781 else
782 return mlx4_cmd_poll(dev, in_param, out_param,
783 out_is_imm, in_modifier,
784 op_modifier, op, timeout);
785 }
786 return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
787 in_modifier, op_modifier, op, timeout);
Roland Dreier225c7b12007-05-08 18:00:38 -0700788}
789EXPORT_SYMBOL_GPL(__mlx4_cmd);
790
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000791
Yishai Hadas55ad3592015-01-25 16:59:42 +0200792int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000793{
794 return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
795 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
796}
797
798static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
799 int slave, u64 slave_addr,
800 int size, int is_read)
801{
802 u64 in_param;
803 u64 out_param;
804
805 if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
806 (slave & ~0x7f) | (size & 0xff)) {
Joe Perches1a91de22014-05-07 12:52:57 -0700807 mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx master_addr:0x%llx slave_id:%d size:%d\n",
808 slave_addr, master_addr, slave, size);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000809 return -EINVAL;
810 }
811
812 if (is_read) {
813 in_param = (u64) slave | slave_addr;
814 out_param = (u64) dev->caps.function | master_addr;
815 } else {
816 in_param = (u64) dev->caps.function | master_addr;
817 out_param = (u64) slave | slave_addr;
818 }
819
820 return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
821 MLX4_CMD_ACCESS_MEM,
822 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
823}
824
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000825static int query_pkey_block(struct mlx4_dev *dev, u8 port, u16 index, u16 *pkey,
826 struct mlx4_cmd_mailbox *inbox,
827 struct mlx4_cmd_mailbox *outbox)
828{
829 struct ib_smp *in_mad = (struct ib_smp *)(inbox->buf);
830 struct ib_smp *out_mad = (struct ib_smp *)(outbox->buf);
831 int err;
832 int i;
833
834 if (index & 0x1f)
835 return -EINVAL;
836
837 in_mad->attr_mod = cpu_to_be32(index / 32);
838
839 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
840 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
841 MLX4_CMD_NATIVE);
842 if (err)
843 return err;
844
845 for (i = 0; i < 32; ++i)
846 pkey[i] = be16_to_cpu(((__be16 *) out_mad->data)[i]);
847
848 return err;
849}
850
851static int get_full_pkey_table(struct mlx4_dev *dev, u8 port, u16 *table,
852 struct mlx4_cmd_mailbox *inbox,
853 struct mlx4_cmd_mailbox *outbox)
854{
855 int i;
856 int err;
857
858 for (i = 0; i < dev->caps.pkey_table_len[port]; i += 32) {
859 err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
860 if (err)
861 return err;
862 }
863
864 return 0;
865}
866#define PORT_CAPABILITY_LOCATION_IN_SMP 20
867#define PORT_STATE_OFFSET 32
868
869static enum ib_port_state vf_port_state(struct mlx4_dev *dev, int port, int vf)
870{
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000871 if (mlx4_get_slave_port_state(dev, vf, port) == SLAVE_PORT_UP)
872 return IB_PORT_ACTIVE;
873 else
874 return IB_PORT_DOWN;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000875}
876
877static int mlx4_MAD_IFC_wrapper(struct mlx4_dev *dev, int slave,
878 struct mlx4_vhcr *vhcr,
879 struct mlx4_cmd_mailbox *inbox,
880 struct mlx4_cmd_mailbox *outbox,
881 struct mlx4_cmd_info *cmd)
882{
883 struct ib_smp *smp = inbox->buf;
884 u32 index;
Or Gerlitz7c35ef42015-05-21 15:14:05 +0300885 u8 port, slave_port;
Jack Morgenstein97982f52014-05-29 16:31:02 +0300886 u8 opcode_modifier;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000887 u16 *table;
888 int err;
889 int vidx, pidx;
Jack Morgenstein97982f52014-05-29 16:31:02 +0300890 int network_view;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000891 struct mlx4_priv *priv = mlx4_priv(dev);
892 struct ib_smp *outsmp = outbox->buf;
893 __be16 *outtab = (__be16 *)(outsmp->data);
894 __be32 slave_cap_mask;
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000895 __be64 slave_node_guid;
Jack Morgenstein97982f52014-05-29 16:31:02 +0300896
Or Gerlitz7c35ef42015-05-21 15:14:05 +0300897 slave_port = vhcr->in_modifier;
898 port = mlx4_slave_convert_port(dev, slave, slave_port);
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000899
Jack Morgenstein97982f52014-05-29 16:31:02 +0300900 /* network-view bit is for driver use only, and should not be passed to FW */
901 opcode_modifier = vhcr->op_modifier & ~0x8; /* clear netw view bit */
902 network_view = !!(vhcr->op_modifier & 0x8);
903
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000904 if (smp->base_version == 1 &&
905 smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
906 smp->class_version == 1) {
Jack Morgenstein97982f52014-05-29 16:31:02 +0300907 /* host view is paravirtualized */
908 if (!network_view && smp->method == IB_MGMT_METHOD_GET) {
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000909 if (smp->attr_id == IB_SMP_ATTR_PKEY_TABLE) {
910 index = be32_to_cpu(smp->attr_mod);
911 if (port < 1 || port > dev->caps.num_ports)
912 return -EINVAL;
Matan Barak19ab5742015-01-27 15:58:07 +0200913 table = kcalloc((dev->caps.pkey_table_len[port] / 32) + 1,
914 sizeof(*table) * 32, GFP_KERNEL);
915
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000916 if (!table)
917 return -ENOMEM;
918 /* need to get the full pkey table because the paravirtualized
919 * pkeys may be scattered among several pkey blocks.
920 */
921 err = get_full_pkey_table(dev, port, table, inbox, outbox);
922 if (!err) {
923 for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
924 pidx = priv->virt2phys_pkey[slave][port - 1][vidx];
925 outtab[vidx % 32] = cpu_to_be16(table[pidx]);
926 }
927 }
928 kfree(table);
929 return err;
930 }
931 if (smp->attr_id == IB_SMP_ATTR_PORT_INFO) {
932 /*get the slave specific caps:*/
933 /*do the command */
Or Gerlitz7c35ef42015-05-21 15:14:05 +0300934 smp->attr_mod = cpu_to_be32(port);
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000935 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Or Gerlitz7c35ef42015-05-21 15:14:05 +0300936 port, opcode_modifier,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000937 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
938 /* modify the response for slaves */
939 if (!err && slave != mlx4_master_func_num(dev)) {
940 u8 *state = outsmp->data + PORT_STATE_OFFSET;
941
942 *state = (*state & 0xf0) | vf_port_state(dev, port, slave);
943 slave_cap_mask = priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
944 memcpy(outsmp->data + PORT_CAPABILITY_LOCATION_IN_SMP, &slave_cap_mask, 4);
945 }
946 return err;
947 }
948 if (smp->attr_id == IB_SMP_ATTR_GUID_INFO) {
Yishai Hadase9a7ff32015-02-02 15:07:23 +0200949 __be64 guid = mlx4_get_admin_guid(dev, slave,
950 port);
951
952 /* set the PF admin guid to the FW/HW burned
953 * GUID, if it wasn't yet set
954 */
955 if (slave == 0 && guid == 0) {
956 smp->attr_mod = 0;
957 err = mlx4_cmd_box(dev,
958 inbox->dma,
959 outbox->dma,
960 vhcr->in_modifier,
961 opcode_modifier,
962 vhcr->op,
963 MLX4_CMD_TIME_CLASS_C,
964 MLX4_CMD_NATIVE);
965 if (err)
966 return err;
967 mlx4_set_admin_guid(dev,
968 *(__be64 *)outsmp->
969 data, slave, port);
970 } else {
971 memcpy(outsmp->data, &guid, 8);
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000972 }
Yishai Hadase9a7ff32015-02-02 15:07:23 +0200973
974 /* clean all other gids */
975 memset(outsmp->data + 8, 0, 56);
976 return 0;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000977 }
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000978 if (smp->attr_id == IB_SMP_ATTR_NODE_INFO) {
979 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Or Gerlitz7c35ef42015-05-21 15:14:05 +0300980 port, opcode_modifier,
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000981 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
982 if (!err) {
983 slave_node_guid = mlx4_get_slave_node_guid(dev, slave);
984 memcpy(outsmp->data + 12, &slave_node_guid, 8);
985 }
986 return err;
987 }
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000988 }
989 }
Jack Morgenstein97982f52014-05-29 16:31:02 +0300990
991 /* Non-privileged VFs are only allowed "host" view LID-routed 'Get' MADs.
992 * These are the MADs used by ib verbs (such as ib_query_gids).
993 */
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000994 if (slave != mlx4_master_func_num(dev) &&
Jack Morgenstein97982f52014-05-29 16:31:02 +0300995 !mlx4_vf_smi_enabled(dev, slave, port)) {
996 if (!(smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
997 smp->method == IB_MGMT_METHOD_GET) || network_view) {
998 mlx4_err(dev, "Unprivileged slave %d is trying to execute a Subnet MGMT MAD, class 0x%x, method 0x%x, view=%s for attr 0x%x. Rejecting\n",
999 slave, smp->method, smp->mgmt_class,
1000 network_view ? "Network" : "Host",
1001 be16_to_cpu(smp->attr_id));
1002 return -EPERM;
1003 }
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001004 }
Jack Morgenstein97982f52014-05-29 16:31:02 +03001005
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001006 return mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Jack Morgenstein97982f52014-05-29 16:31:02 +03001007 vhcr->in_modifier, opcode_modifier,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001008 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
1009}
1010
Or Gerlitzb7475792014-03-27 14:02:02 +02001011static int mlx4_CMD_EPERM_wrapper(struct mlx4_dev *dev, int slave,
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001012 struct mlx4_vhcr *vhcr,
1013 struct mlx4_cmd_mailbox *inbox,
1014 struct mlx4_cmd_mailbox *outbox,
1015 struct mlx4_cmd_info *cmd)
1016{
1017 return -EPERM;
1018}
1019
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001020int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
1021 struct mlx4_vhcr *vhcr,
1022 struct mlx4_cmd_mailbox *inbox,
1023 struct mlx4_cmd_mailbox *outbox,
1024 struct mlx4_cmd_info *cmd)
1025{
1026 u64 in_param;
1027 u64 out_param;
1028 int err;
1029
1030 in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
1031 out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
1032 if (cmd->encode_slave_id) {
1033 in_param &= 0xffffffffffffff00ll;
1034 in_param |= slave;
1035 }
1036
1037 err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
1038 vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
1039 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
1040
1041 if (cmd->out_is_imm)
1042 vhcr->out_param = out_param;
1043
1044 return err;
1045}
1046
1047static struct mlx4_cmd_info cmd_info[] = {
1048 {
1049 .opcode = MLX4_CMD_QUERY_FW,
1050 .has_inbox = false,
1051 .has_outbox = true,
1052 .out_is_imm = false,
1053 .encode_slave_id = false,
1054 .verify = NULL,
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +00001055 .wrapper = mlx4_QUERY_FW_wrapper
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001056 },
1057 {
1058 .opcode = MLX4_CMD_QUERY_HCA,
1059 .has_inbox = false,
1060 .has_outbox = true,
1061 .out_is_imm = false,
1062 .encode_slave_id = false,
1063 .verify = NULL,
1064 .wrapper = NULL
1065 },
1066 {
1067 .opcode = MLX4_CMD_QUERY_DEV_CAP,
1068 .has_inbox = false,
1069 .has_outbox = true,
1070 .out_is_imm = false,
1071 .encode_slave_id = false,
1072 .verify = NULL,
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +00001073 .wrapper = mlx4_QUERY_DEV_CAP_wrapper
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001074 },
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001075 {
1076 .opcode = MLX4_CMD_QUERY_FUNC_CAP,
1077 .has_inbox = false,
1078 .has_outbox = true,
1079 .out_is_imm = false,
1080 .encode_slave_id = false,
1081 .verify = NULL,
1082 .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
1083 },
1084 {
1085 .opcode = MLX4_CMD_QUERY_ADAPTER,
1086 .has_inbox = false,
1087 .has_outbox = true,
1088 .out_is_imm = false,
1089 .encode_slave_id = false,
1090 .verify = NULL,
1091 .wrapper = NULL
1092 },
1093 {
1094 .opcode = MLX4_CMD_INIT_PORT,
1095 .has_inbox = false,
1096 .has_outbox = false,
1097 .out_is_imm = false,
1098 .encode_slave_id = false,
1099 .verify = NULL,
1100 .wrapper = mlx4_INIT_PORT_wrapper
1101 },
1102 {
1103 .opcode = MLX4_CMD_CLOSE_PORT,
1104 .has_inbox = false,
1105 .has_outbox = false,
1106 .out_is_imm = false,
1107 .encode_slave_id = false,
1108 .verify = NULL,
1109 .wrapper = mlx4_CLOSE_PORT_wrapper
1110 },
1111 {
1112 .opcode = MLX4_CMD_QUERY_PORT,
1113 .has_inbox = false,
1114 .has_outbox = true,
1115 .out_is_imm = false,
1116 .encode_slave_id = false,
1117 .verify = NULL,
1118 .wrapper = mlx4_QUERY_PORT_wrapper
1119 },
1120 {
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001121 .opcode = MLX4_CMD_SET_PORT,
1122 .has_inbox = true,
1123 .has_outbox = false,
1124 .out_is_imm = false,
1125 .encode_slave_id = false,
1126 .verify = NULL,
1127 .wrapper = mlx4_SET_PORT_wrapper
1128 },
1129 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001130 .opcode = MLX4_CMD_MAP_EQ,
1131 .has_inbox = false,
1132 .has_outbox = false,
1133 .out_is_imm = false,
1134 .encode_slave_id = false,
1135 .verify = NULL,
1136 .wrapper = mlx4_MAP_EQ_wrapper
1137 },
1138 {
1139 .opcode = MLX4_CMD_SW2HW_EQ,
1140 .has_inbox = true,
1141 .has_outbox = false,
1142 .out_is_imm = false,
1143 .encode_slave_id = true,
1144 .verify = NULL,
1145 .wrapper = mlx4_SW2HW_EQ_wrapper
1146 },
1147 {
1148 .opcode = MLX4_CMD_HW_HEALTH_CHECK,
1149 .has_inbox = false,
1150 .has_outbox = false,
1151 .out_is_imm = false,
1152 .encode_slave_id = false,
1153 .verify = NULL,
1154 .wrapper = NULL
1155 },
1156 {
1157 .opcode = MLX4_CMD_NOP,
1158 .has_inbox = false,
1159 .has_outbox = false,
1160 .out_is_imm = false,
1161 .encode_slave_id = false,
1162 .verify = NULL,
1163 .wrapper = NULL
1164 },
1165 {
Or Gerlitzd18f1412014-03-27 14:02:03 +02001166 .opcode = MLX4_CMD_CONFIG_DEV,
1167 .has_inbox = false,
Matan Barakd475c952014-11-02 16:26:17 +02001168 .has_outbox = true,
Or Gerlitzd18f1412014-03-27 14:02:03 +02001169 .out_is_imm = false,
1170 .encode_slave_id = false,
1171 .verify = NULL,
Matan Barakd475c952014-11-02 16:26:17 +02001172 .wrapper = mlx4_CONFIG_DEV_wrapper
Or Gerlitzd18f1412014-03-27 14:02:03 +02001173 },
1174 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001175 .opcode = MLX4_CMD_ALLOC_RES,
1176 .has_inbox = false,
1177 .has_outbox = false,
1178 .out_is_imm = true,
1179 .encode_slave_id = false,
1180 .verify = NULL,
1181 .wrapper = mlx4_ALLOC_RES_wrapper
1182 },
1183 {
1184 .opcode = MLX4_CMD_FREE_RES,
1185 .has_inbox = false,
1186 .has_outbox = false,
1187 .out_is_imm = false,
1188 .encode_slave_id = false,
1189 .verify = NULL,
1190 .wrapper = mlx4_FREE_RES_wrapper
1191 },
1192 {
1193 .opcode = MLX4_CMD_SW2HW_MPT,
1194 .has_inbox = true,
1195 .has_outbox = false,
1196 .out_is_imm = false,
1197 .encode_slave_id = true,
1198 .verify = NULL,
1199 .wrapper = mlx4_SW2HW_MPT_wrapper
1200 },
1201 {
1202 .opcode = MLX4_CMD_QUERY_MPT,
1203 .has_inbox = false,
1204 .has_outbox = true,
1205 .out_is_imm = false,
1206 .encode_slave_id = false,
1207 .verify = NULL,
1208 .wrapper = mlx4_QUERY_MPT_wrapper
1209 },
1210 {
1211 .opcode = MLX4_CMD_HW2SW_MPT,
1212 .has_inbox = false,
1213 .has_outbox = false,
1214 .out_is_imm = false,
1215 .encode_slave_id = false,
1216 .verify = NULL,
1217 .wrapper = mlx4_HW2SW_MPT_wrapper
1218 },
1219 {
1220 .opcode = MLX4_CMD_READ_MTT,
1221 .has_inbox = false,
1222 .has_outbox = true,
1223 .out_is_imm = false,
1224 .encode_slave_id = false,
1225 .verify = NULL,
1226 .wrapper = NULL
1227 },
1228 {
1229 .opcode = MLX4_CMD_WRITE_MTT,
1230 .has_inbox = true,
1231 .has_outbox = false,
1232 .out_is_imm = false,
1233 .encode_slave_id = false,
1234 .verify = NULL,
1235 .wrapper = mlx4_WRITE_MTT_wrapper
1236 },
1237 {
1238 .opcode = MLX4_CMD_SYNC_TPT,
1239 .has_inbox = true,
1240 .has_outbox = false,
1241 .out_is_imm = false,
1242 .encode_slave_id = false,
1243 .verify = NULL,
1244 .wrapper = NULL
1245 },
1246 {
1247 .opcode = MLX4_CMD_HW2SW_EQ,
1248 .has_inbox = false,
Jack Morgenstein30a5da52015-01-27 15:58:03 +02001249 .has_outbox = false,
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001250 .out_is_imm = false,
1251 .encode_slave_id = true,
1252 .verify = NULL,
1253 .wrapper = mlx4_HW2SW_EQ_wrapper
1254 },
1255 {
1256 .opcode = MLX4_CMD_QUERY_EQ,
1257 .has_inbox = false,
1258 .has_outbox = true,
1259 .out_is_imm = false,
1260 .encode_slave_id = true,
1261 .verify = NULL,
1262 .wrapper = mlx4_QUERY_EQ_wrapper
1263 },
1264 {
1265 .opcode = MLX4_CMD_SW2HW_CQ,
1266 .has_inbox = true,
1267 .has_outbox = false,
1268 .out_is_imm = false,
1269 .encode_slave_id = true,
1270 .verify = NULL,
1271 .wrapper = mlx4_SW2HW_CQ_wrapper
1272 },
1273 {
1274 .opcode = MLX4_CMD_HW2SW_CQ,
1275 .has_inbox = false,
1276 .has_outbox = false,
1277 .out_is_imm = false,
1278 .encode_slave_id = false,
1279 .verify = NULL,
1280 .wrapper = mlx4_HW2SW_CQ_wrapper
1281 },
1282 {
1283 .opcode = MLX4_CMD_QUERY_CQ,
1284 .has_inbox = false,
1285 .has_outbox = true,
1286 .out_is_imm = false,
1287 .encode_slave_id = false,
1288 .verify = NULL,
1289 .wrapper = mlx4_QUERY_CQ_wrapper
1290 },
1291 {
1292 .opcode = MLX4_CMD_MODIFY_CQ,
1293 .has_inbox = true,
1294 .has_outbox = false,
1295 .out_is_imm = true,
1296 .encode_slave_id = false,
1297 .verify = NULL,
1298 .wrapper = mlx4_MODIFY_CQ_wrapper
1299 },
1300 {
1301 .opcode = MLX4_CMD_SW2HW_SRQ,
1302 .has_inbox = true,
1303 .has_outbox = false,
1304 .out_is_imm = false,
1305 .encode_slave_id = true,
1306 .verify = NULL,
1307 .wrapper = mlx4_SW2HW_SRQ_wrapper
1308 },
1309 {
1310 .opcode = MLX4_CMD_HW2SW_SRQ,
1311 .has_inbox = false,
1312 .has_outbox = false,
1313 .out_is_imm = false,
1314 .encode_slave_id = false,
1315 .verify = NULL,
1316 .wrapper = mlx4_HW2SW_SRQ_wrapper
1317 },
1318 {
1319 .opcode = MLX4_CMD_QUERY_SRQ,
1320 .has_inbox = false,
1321 .has_outbox = true,
1322 .out_is_imm = false,
1323 .encode_slave_id = false,
1324 .verify = NULL,
1325 .wrapper = mlx4_QUERY_SRQ_wrapper
1326 },
1327 {
1328 .opcode = MLX4_CMD_ARM_SRQ,
1329 .has_inbox = false,
1330 .has_outbox = false,
1331 .out_is_imm = false,
1332 .encode_slave_id = false,
1333 .verify = NULL,
1334 .wrapper = mlx4_ARM_SRQ_wrapper
1335 },
1336 {
1337 .opcode = MLX4_CMD_RST2INIT_QP,
1338 .has_inbox = true,
1339 .has_outbox = false,
1340 .out_is_imm = false,
1341 .encode_slave_id = true,
1342 .verify = NULL,
1343 .wrapper = mlx4_RST2INIT_QP_wrapper
1344 },
1345 {
1346 .opcode = MLX4_CMD_INIT2INIT_QP,
1347 .has_inbox = true,
1348 .has_outbox = false,
1349 .out_is_imm = false,
1350 .encode_slave_id = false,
1351 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001352 .wrapper = mlx4_INIT2INIT_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001353 },
1354 {
1355 .opcode = MLX4_CMD_INIT2RTR_QP,
1356 .has_inbox = true,
1357 .has_outbox = false,
1358 .out_is_imm = false,
1359 .encode_slave_id = false,
1360 .verify = NULL,
1361 .wrapper = mlx4_INIT2RTR_QP_wrapper
1362 },
1363 {
1364 .opcode = MLX4_CMD_RTR2RTS_QP,
1365 .has_inbox = true,
1366 .has_outbox = false,
1367 .out_is_imm = false,
1368 .encode_slave_id = false,
1369 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001370 .wrapper = mlx4_RTR2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001371 },
1372 {
1373 .opcode = MLX4_CMD_RTS2RTS_QP,
1374 .has_inbox = true,
1375 .has_outbox = false,
1376 .out_is_imm = false,
1377 .encode_slave_id = false,
1378 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001379 .wrapper = mlx4_RTS2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001380 },
1381 {
1382 .opcode = MLX4_CMD_SQERR2RTS_QP,
1383 .has_inbox = true,
1384 .has_outbox = false,
1385 .out_is_imm = false,
1386 .encode_slave_id = false,
1387 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001388 .wrapper = mlx4_SQERR2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001389 },
1390 {
1391 .opcode = MLX4_CMD_2ERR_QP,
1392 .has_inbox = false,
1393 .has_outbox = false,
1394 .out_is_imm = false,
1395 .encode_slave_id = false,
1396 .verify = NULL,
1397 .wrapper = mlx4_GEN_QP_wrapper
1398 },
1399 {
1400 .opcode = MLX4_CMD_RTS2SQD_QP,
1401 .has_inbox = false,
1402 .has_outbox = false,
1403 .out_is_imm = false,
1404 .encode_slave_id = false,
1405 .verify = NULL,
1406 .wrapper = mlx4_GEN_QP_wrapper
1407 },
1408 {
1409 .opcode = MLX4_CMD_SQD2SQD_QP,
1410 .has_inbox = true,
1411 .has_outbox = false,
1412 .out_is_imm = false,
1413 .encode_slave_id = false,
1414 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001415 .wrapper = mlx4_SQD2SQD_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001416 },
1417 {
1418 .opcode = MLX4_CMD_SQD2RTS_QP,
1419 .has_inbox = true,
1420 .has_outbox = false,
1421 .out_is_imm = false,
1422 .encode_slave_id = false,
1423 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001424 .wrapper = mlx4_SQD2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001425 },
1426 {
1427 .opcode = MLX4_CMD_2RST_QP,
1428 .has_inbox = false,
1429 .has_outbox = false,
1430 .out_is_imm = false,
1431 .encode_slave_id = false,
1432 .verify = NULL,
1433 .wrapper = mlx4_2RST_QP_wrapper
1434 },
1435 {
1436 .opcode = MLX4_CMD_QUERY_QP,
1437 .has_inbox = false,
1438 .has_outbox = true,
1439 .out_is_imm = false,
1440 .encode_slave_id = false,
1441 .verify = NULL,
1442 .wrapper = mlx4_GEN_QP_wrapper
1443 },
1444 {
1445 .opcode = MLX4_CMD_SUSPEND_QP,
1446 .has_inbox = false,
1447 .has_outbox = false,
1448 .out_is_imm = false,
1449 .encode_slave_id = false,
1450 .verify = NULL,
1451 .wrapper = mlx4_GEN_QP_wrapper
1452 },
1453 {
1454 .opcode = MLX4_CMD_UNSUSPEND_QP,
1455 .has_inbox = false,
1456 .has_outbox = false,
1457 .out_is_imm = false,
1458 .encode_slave_id = false,
1459 .verify = NULL,
1460 .wrapper = mlx4_GEN_QP_wrapper
1461 },
1462 {
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001463 .opcode = MLX4_CMD_UPDATE_QP,
Matan Barakce8d9e02014-05-15 15:29:27 +03001464 .has_inbox = true,
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001465 .has_outbox = false,
1466 .out_is_imm = false,
1467 .encode_slave_id = false,
1468 .verify = NULL,
Matan Barakce8d9e02014-05-15 15:29:27 +03001469 .wrapper = mlx4_UPDATE_QP_wrapper
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001470 },
1471 {
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001472 .opcode = MLX4_CMD_GET_OP_REQ,
1473 .has_inbox = false,
1474 .has_outbox = false,
1475 .out_is_imm = false,
1476 .encode_slave_id = false,
1477 .verify = NULL,
Or Gerlitzb7475792014-03-27 14:02:02 +02001478 .wrapper = mlx4_CMD_EPERM_wrapper,
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001479 },
1480 {
Ido Shamay7e95bb92015-04-02 16:31:11 +03001481 .opcode = MLX4_CMD_ALLOCATE_VPP,
1482 .has_inbox = false,
1483 .has_outbox = true,
1484 .out_is_imm = false,
1485 .encode_slave_id = false,
1486 .verify = NULL,
1487 .wrapper = mlx4_CMD_EPERM_wrapper,
1488 },
1489 {
Ido Shamay1c291462015-04-02 16:31:12 +03001490 .opcode = MLX4_CMD_SET_VPORT_QOS,
1491 .has_inbox = false,
1492 .has_outbox = true,
1493 .out_is_imm = false,
1494 .encode_slave_id = false,
1495 .verify = NULL,
1496 .wrapper = mlx4_CMD_EPERM_wrapper,
1497 },
1498 {
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001499 .opcode = MLX4_CMD_CONF_SPECIAL_QP,
1500 .has_inbox = false,
1501 .has_outbox = false,
1502 .out_is_imm = false,
1503 .encode_slave_id = false,
1504 .verify = NULL, /* XXX verify: only demux can do this */
1505 .wrapper = NULL
1506 },
1507 {
1508 .opcode = MLX4_CMD_MAD_IFC,
1509 .has_inbox = true,
1510 .has_outbox = true,
1511 .out_is_imm = false,
1512 .encode_slave_id = false,
1513 .verify = NULL,
1514 .wrapper = mlx4_MAD_IFC_wrapper
1515 },
1516 {
Jack Morgenstein114840c2014-06-01 11:53:50 +03001517 .opcode = MLX4_CMD_MAD_DEMUX,
1518 .has_inbox = false,
1519 .has_outbox = false,
1520 .out_is_imm = false,
1521 .encode_slave_id = false,
1522 .verify = NULL,
1523 .wrapper = mlx4_CMD_EPERM_wrapper
1524 },
1525 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001526 .opcode = MLX4_CMD_QUERY_IF_STAT,
1527 .has_inbox = false,
1528 .has_outbox = true,
1529 .out_is_imm = false,
1530 .encode_slave_id = false,
1531 .verify = NULL,
1532 .wrapper = mlx4_QUERY_IF_STAT_wrapper
1533 },
Saeed Mahameedadbc7ac2014-10-27 11:37:37 +02001534 {
1535 .opcode = MLX4_CMD_ACCESS_REG,
1536 .has_inbox = true,
1537 .has_outbox = true,
1538 .out_is_imm = false,
1539 .encode_slave_id = false,
1540 .verify = NULL,
Saeed Mahameed6e806692014-11-02 16:26:13 +02001541 .wrapper = mlx4_ACCESS_REG_wrapper,
Saeed Mahameedadbc7ac2014-10-27 11:37:37 +02001542 },
Shani Michaelid237baa2015-03-05 20:16:12 +02001543 {
1544 .opcode = MLX4_CMD_CONGESTION_CTRL_OPCODE,
1545 .has_inbox = false,
1546 .has_outbox = false,
1547 .out_is_imm = false,
1548 .encode_slave_id = false,
1549 .verify = NULL,
1550 .wrapper = mlx4_CMD_EPERM_wrapper,
1551 },
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001552 /* Native multicast commands are not available for guests */
1553 {
1554 .opcode = MLX4_CMD_QP_ATTACH,
1555 .has_inbox = true,
1556 .has_outbox = false,
1557 .out_is_imm = false,
1558 .encode_slave_id = false,
1559 .verify = NULL,
1560 .wrapper = mlx4_QP_ATTACH_wrapper
1561 },
1562 {
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +00001563 .opcode = MLX4_CMD_PROMISC,
1564 .has_inbox = false,
1565 .has_outbox = false,
1566 .out_is_imm = false,
1567 .encode_slave_id = false,
1568 .verify = NULL,
1569 .wrapper = mlx4_PROMISC_wrapper
1570 },
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001571 /* Ethernet specific commands */
1572 {
1573 .opcode = MLX4_CMD_SET_VLAN_FLTR,
1574 .has_inbox = true,
1575 .has_outbox = false,
1576 .out_is_imm = false,
1577 .encode_slave_id = false,
1578 .verify = NULL,
1579 .wrapper = mlx4_SET_VLAN_FLTR_wrapper
1580 },
1581 {
1582 .opcode = MLX4_CMD_SET_MCAST_FLTR,
1583 .has_inbox = false,
1584 .has_outbox = false,
1585 .out_is_imm = false,
1586 .encode_slave_id = false,
1587 .verify = NULL,
1588 .wrapper = mlx4_SET_MCAST_FLTR_wrapper
1589 },
1590 {
1591 .opcode = MLX4_CMD_DUMP_ETH_STATS,
1592 .has_inbox = false,
1593 .has_outbox = true,
1594 .out_is_imm = false,
1595 .encode_slave_id = false,
1596 .verify = NULL,
1597 .wrapper = mlx4_DUMP_ETH_STATS_wrapper
1598 },
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +00001599 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001600 .opcode = MLX4_CMD_INFORM_FLR_DONE,
1601 .has_inbox = false,
1602 .has_outbox = false,
1603 .out_is_imm = false,
1604 .encode_slave_id = false,
1605 .verify = NULL,
1606 .wrapper = NULL
1607 },
Hadar Hen Zion8fcfb4d2012-07-05 04:03:45 +00001608 /* flow steering commands */
1609 {
1610 .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
1611 .has_inbox = true,
1612 .has_outbox = false,
1613 .out_is_imm = true,
1614 .encode_slave_id = false,
1615 .verify = NULL,
1616 .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
1617 },
1618 {
1619 .opcode = MLX4_QP_FLOW_STEERING_DETACH,
1620 .has_inbox = false,
1621 .has_outbox = false,
1622 .out_is_imm = false,
1623 .encode_slave_id = false,
1624 .verify = NULL,
1625 .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
1626 },
Matan Barak4de65802013-11-07 15:25:14 +02001627 {
1628 .opcode = MLX4_FLOW_STEERING_IB_UC_QP_RANGE,
1629 .has_inbox = false,
1630 .has_outbox = false,
1631 .out_is_imm = false,
1632 .encode_slave_id = false,
1633 .verify = NULL,
Or Gerlitzb7475792014-03-27 14:02:02 +02001634 .wrapper = mlx4_CMD_EPERM_wrapper
Matan Barak4de65802013-11-07 15:25:14 +02001635 },
Moni Shoua59e14e32015-02-03 16:48:32 +02001636 {
1637 .opcode = MLX4_CMD_VIRT_PORT_MAP,
1638 .has_inbox = false,
1639 .has_outbox = false,
1640 .out_is_imm = false,
1641 .encode_slave_id = false,
1642 .verify = NULL,
1643 .wrapper = mlx4_CMD_EPERM_wrapper
1644 },
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001645};
1646
1647static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
1648 struct mlx4_vhcr_cmd *in_vhcr)
1649{
1650 struct mlx4_priv *priv = mlx4_priv(dev);
1651 struct mlx4_cmd_info *cmd = NULL;
1652 struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
1653 struct mlx4_vhcr *vhcr;
1654 struct mlx4_cmd_mailbox *inbox = NULL;
1655 struct mlx4_cmd_mailbox *outbox = NULL;
1656 u64 in_param;
1657 u64 out_param;
1658 int ret = 0;
1659 int i;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001660 int err = 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001661
1662 /* Create sw representation of Virtual HCR */
1663 vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
1664 if (!vhcr)
1665 return -ENOMEM;
1666
1667 /* DMA in the vHCR */
1668 if (!in_vhcr) {
1669 ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1670 priv->mfunc.master.slave_state[slave].vhcr_dma,
1671 ALIGN(sizeof(struct mlx4_vhcr_cmd),
1672 MLX4_ACCESS_MEM_ALIGN), 1);
1673 if (ret) {
Yishai Hadas0cd93022015-01-25 16:59:43 +02001674 if (!(dev->persist->state &
1675 MLX4_DEVICE_STATE_INTERNAL_ERROR))
1676 mlx4_err(dev, "%s: Failed reading vhcr ret: 0x%x\n",
1677 __func__, ret);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001678 kfree(vhcr);
1679 return ret;
1680 }
1681 }
1682
1683 /* Fill SW VHCR fields */
1684 vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
1685 vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
1686 vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
1687 vhcr->token = be16_to_cpu(vhcr_cmd->token);
1688 vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
1689 vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
1690 vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
1691
1692 /* Lookup command */
1693 for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
1694 if (vhcr->op == cmd_info[i].opcode) {
1695 cmd = &cmd_info[i];
1696 break;
1697 }
1698 }
1699 if (!cmd) {
1700 mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
1701 vhcr->op, slave);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001702 vhcr_cmd->status = CMD_STAT_BAD_PARAM;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001703 goto out_status;
1704 }
1705
1706 /* Read inbox */
1707 if (cmd->has_inbox) {
1708 vhcr->in_param &= INBOX_MASK;
1709 inbox = mlx4_alloc_cmd_mailbox(dev);
1710 if (IS_ERR(inbox)) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001711 vhcr_cmd->status = CMD_STAT_BAD_SIZE;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001712 inbox = NULL;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001713 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001714 }
1715
Yishai Hadas0cd93022015-01-25 16:59:43 +02001716 ret = mlx4_ACCESS_MEM(dev, inbox->dma, slave,
1717 vhcr->in_param,
1718 MLX4_MAILBOX_SIZE, 1);
1719 if (ret) {
1720 if (!(dev->persist->state &
1721 MLX4_DEVICE_STATE_INTERNAL_ERROR))
1722 mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
1723 __func__, cmd->opcode);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001724 vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
1725 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001726 }
1727 }
1728
1729 /* Apply permission and bound checks if applicable */
1730 if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
Joe Perches1a91de22014-05-07 12:52:57 -07001731 mlx4_warn(dev, "Command:0x%x from slave: %d failed protection checks for resource_id:%d\n",
1732 vhcr->op, slave, vhcr->in_modifier);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001733 vhcr_cmd->status = CMD_STAT_BAD_OP;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001734 goto out_status;
1735 }
1736
1737 /* Allocate outbox */
1738 if (cmd->has_outbox) {
1739 outbox = mlx4_alloc_cmd_mailbox(dev);
1740 if (IS_ERR(outbox)) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001741 vhcr_cmd->status = CMD_STAT_BAD_SIZE;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001742 outbox = NULL;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001743 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001744 }
1745 }
1746
1747 /* Execute the command! */
1748 if (cmd->wrapper) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001749 err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
1750 cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001751 if (cmd->out_is_imm)
1752 vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1753 } else {
1754 in_param = cmd->has_inbox ? (u64) inbox->dma :
1755 vhcr->in_param;
1756 out_param = cmd->has_outbox ? (u64) outbox->dma :
1757 vhcr->out_param;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001758 err = __mlx4_cmd(dev, in_param, &out_param,
1759 cmd->out_is_imm, vhcr->in_modifier,
1760 vhcr->op_modifier, vhcr->op,
1761 MLX4_CMD_TIME_CLASS_A,
1762 MLX4_CMD_NATIVE);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001763
1764 if (cmd->out_is_imm) {
1765 vhcr->out_param = out_param;
1766 vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1767 }
1768 }
1769
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001770 if (err) {
Yishai Hadas0cd93022015-01-25 16:59:43 +02001771 if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR))
1772 mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with error:%d, status %d\n",
1773 vhcr->op, slave, vhcr->errno, err);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001774 vhcr_cmd->status = mlx4_errno_to_status(err);
1775 goto out_status;
1776 }
1777
1778
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001779 /* Write outbox if command completed successfully */
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001780 if (cmd->has_outbox && !vhcr_cmd->status) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001781 ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
1782 vhcr->out_param,
1783 MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
1784 if (ret) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001785 /* If we failed to write back the outbox after the
1786 *command was successfully executed, we must fail this
1787 * slave, as it is now in undefined state */
Yishai Hadas0cd93022015-01-25 16:59:43 +02001788 if (!(dev->persist->state &
1789 MLX4_DEVICE_STATE_INTERNAL_ERROR))
1790 mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001791 goto out;
1792 }
1793 }
1794
1795out_status:
1796 /* DMA back vhcr result */
1797 if (!in_vhcr) {
1798 ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1799 priv->mfunc.master.slave_state[slave].vhcr_dma,
1800 ALIGN(sizeof(struct mlx4_vhcr),
1801 MLX4_ACCESS_MEM_ALIGN),
1802 MLX4_CMD_WRAPPED);
1803 if (ret)
1804 mlx4_err(dev, "%s:Failed writing vhcr result\n",
1805 __func__);
1806 else if (vhcr->e_bit &&
1807 mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
Joe Perches1a91de22014-05-07 12:52:57 -07001808 mlx4_warn(dev, "Failed to generate command completion eqe for slave %d\n",
1809 slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001810 }
1811
1812out:
1813 kfree(vhcr);
1814 mlx4_free_cmd_mailbox(dev, inbox);
1815 mlx4_free_cmd_mailbox(dev, outbox);
1816 return ret;
1817}
1818
Jingoo Hanf0946682013-08-05 18:04:51 +09001819static int mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv *priv,
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001820 int slave, int port)
1821{
1822 struct mlx4_vport_oper_state *vp_oper;
1823 struct mlx4_vport_state *vp_admin;
1824 struct mlx4_vf_immed_vlan_work *work;
Rony Efraim0a6eac22013-06-27 19:05:22 +03001825 struct mlx4_dev *dev = &(priv->dev);
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001826 int err;
1827 int admin_vlan_ix = NO_INDX;
1828
1829 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1830 vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
1831
1832 if (vp_oper->state.default_vlan == vp_admin->default_vlan &&
Rony Efraim0a6eac22013-06-27 19:05:22 +03001833 vp_oper->state.default_qos == vp_admin->default_qos &&
Ido Shamay08068cd2015-04-02 16:31:15 +03001834 vp_oper->state.link_state == vp_admin->link_state &&
1835 vp_oper->state.qos_vport == vp_admin->qos_vport)
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001836 return 0;
1837
Rony Efraim0a6eac22013-06-27 19:05:22 +03001838 if (!(priv->mfunc.master.slave_state[slave].active &&
Rony Efraimf0f829b2013-11-07 12:19:51 +02001839 dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)) {
Rony Efraim0a6eac22013-06-27 19:05:22 +03001840 /* even if the UPDATE_QP command isn't supported, we still want
1841 * to set this VF link according to the admin directive
1842 */
1843 vp_oper->state.link_state = vp_admin->link_state;
1844 return -1;
1845 }
1846
1847 mlx4_dbg(dev, "updating immediately admin params slave %d port %d\n",
1848 slave, port);
Joe Perches1a91de22014-05-07 12:52:57 -07001849 mlx4_dbg(dev, "vlan %d QoS %d link down %d\n",
1850 vp_admin->default_vlan, vp_admin->default_qos,
1851 vp_admin->link_state);
Rony Efraim0a6eac22013-06-27 19:05:22 +03001852
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001853 work = kzalloc(sizeof(*work), GFP_KERNEL);
1854 if (!work)
1855 return -ENOMEM;
1856
1857 if (vp_oper->state.default_vlan != vp_admin->default_vlan) {
Rony Efraimf0f829b2013-11-07 12:19:51 +02001858 if (MLX4_VGT != vp_admin->default_vlan) {
1859 err = __mlx4_register_vlan(&priv->dev, port,
1860 vp_admin->default_vlan,
1861 &admin_vlan_ix);
1862 if (err) {
1863 kfree(work);
Joe Perches1a91de22014-05-07 12:52:57 -07001864 mlx4_warn(&priv->dev,
Rony Efraimf0f829b2013-11-07 12:19:51 +02001865 "No vlan resources slave %d, port %d\n",
1866 slave, port);
1867 return err;
1868 }
1869 } else {
1870 admin_vlan_ix = NO_INDX;
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001871 }
1872 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_VLAN;
Joe Perches1a91de22014-05-07 12:52:57 -07001873 mlx4_dbg(&priv->dev,
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001874 "alloc vlan %d idx %d slave %d port %d\n",
1875 (int)(vp_admin->default_vlan),
1876 admin_vlan_ix, slave, port);
1877 }
1878
1879 /* save original vlan ix and vlan id */
1880 work->orig_vlan_id = vp_oper->state.default_vlan;
1881 work->orig_vlan_ix = vp_oper->vlan_idx;
1882
1883 /* handle new qos */
1884 if (vp_oper->state.default_qos != vp_admin->default_qos)
1885 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_QOS;
1886
1887 if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN)
1888 vp_oper->vlan_idx = admin_vlan_ix;
1889
1890 vp_oper->state.default_vlan = vp_admin->default_vlan;
1891 vp_oper->state.default_qos = vp_admin->default_qos;
Rony Efraim0a6eac22013-06-27 19:05:22 +03001892 vp_oper->state.link_state = vp_admin->link_state;
Ido Shamay08068cd2015-04-02 16:31:15 +03001893 vp_oper->state.qos_vport = vp_admin->qos_vport;
Rony Efraim0a6eac22013-06-27 19:05:22 +03001894
1895 if (vp_admin->link_state == IFLA_VF_LINK_STATE_DISABLE)
1896 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE;
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001897
1898 /* iterate over QPs owned by this slave, using UPDATE_QP */
1899 work->port = port;
1900 work->slave = slave;
1901 work->qos = vp_oper->state.default_qos;
Ido Shamay08068cd2015-04-02 16:31:15 +03001902 work->qos_vport = vp_oper->state.qos_vport;
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001903 work->vlan_id = vp_oper->state.default_vlan;
1904 work->vlan_ix = vp_oper->vlan_idx;
1905 work->priv = priv;
1906 INIT_WORK(&work->work, mlx4_vf_immed_vlan_work_handler);
1907 queue_work(priv->mfunc.master.comm_wq, &work->work);
1908
1909 return 0;
1910}
1911
Ido Shamay666672d2015-04-02 16:31:14 +03001912static void mlx4_set_default_port_qos(struct mlx4_dev *dev, int port)
1913{
1914 struct mlx4_qos_manager *port_qos_ctl;
1915 struct mlx4_priv *priv = mlx4_priv(dev);
1916
1917 port_qos_ctl = &priv->mfunc.master.qos_ctl[port];
1918 bitmap_zero(port_qos_ctl->priority_bm, MLX4_NUM_UP);
1919
1920 /* Enable only default prio at PF init routine */
1921 set_bit(MLX4_DEFAULT_QOS_PRIO, port_qos_ctl->priority_bm);
1922}
1923
1924static void mlx4_allocate_port_vpps(struct mlx4_dev *dev, int port)
1925{
1926 int i;
1927 int err;
1928 int num_vfs;
1929 u16 availible_vpp;
1930 u8 vpp_param[MLX4_NUM_UP];
1931 struct mlx4_qos_manager *port_qos;
1932 struct mlx4_priv *priv = mlx4_priv(dev);
1933
1934 err = mlx4_ALLOCATE_VPP_get(dev, port, &availible_vpp, vpp_param);
1935 if (err) {
1936 mlx4_info(dev, "Failed query availible VPPs\n");
1937 return;
1938 }
1939
1940 port_qos = &priv->mfunc.master.qos_ctl[port];
1941 num_vfs = (availible_vpp /
1942 bitmap_weight(port_qos->priority_bm, MLX4_NUM_UP));
1943
1944 for (i = 0; i < MLX4_NUM_UP; i++) {
1945 if (test_bit(i, port_qos->priority_bm))
1946 vpp_param[i] = num_vfs;
1947 }
1948
1949 err = mlx4_ALLOCATE_VPP_set(dev, port, vpp_param);
1950 if (err) {
1951 mlx4_info(dev, "Failed allocating VPPs\n");
1952 return;
1953 }
1954
1955 /* Query actual allocated VPP, just to make sure */
1956 err = mlx4_ALLOCATE_VPP_get(dev, port, &availible_vpp, vpp_param);
1957 if (err) {
1958 mlx4_info(dev, "Failed query availible VPPs\n");
1959 return;
1960 }
1961
1962 port_qos->num_of_qos_vfs = num_vfs;
1963 mlx4_dbg(dev, "Port %d Availible VPPs %d\n", port, availible_vpp);
1964
1965 for (i = 0; i < MLX4_NUM_UP; i++)
1966 mlx4_dbg(dev, "Port %d UP %d Allocated %d VPPs\n", port, i,
1967 vpp_param[i]);
1968}
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001969
Rony Efraim0eb62b92013-04-25 05:22:26 +00001970static int mlx4_master_activate_admin_state(struct mlx4_priv *priv, int slave)
1971{
Rony Efraim3f7fb022013-04-25 05:22:28 +00001972 int port, err;
1973 struct mlx4_vport_state *vp_admin;
1974 struct mlx4_vport_oper_state *vp_oper;
Matan Barak449fc482014-03-19 18:11:52 +02001975 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
1976 &priv->dev, slave);
1977 int min_port = find_first_bit(actv_ports.ports,
1978 priv->dev.caps.num_ports) + 1;
1979 int max_port = min_port - 1 +
1980 bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
Rony Efraim3f7fb022013-04-25 05:22:28 +00001981
Matan Barak449fc482014-03-19 18:11:52 +02001982 for (port = min_port; port <= max_port; port++) {
1983 if (!test_bit(port - 1, actv_ports.ports))
1984 continue;
Jack Morgenstein99ec41d2014-05-29 16:31:03 +03001985 priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
1986 priv->mfunc.master.vf_admin[slave].enable_smi[port];
Rony Efraim3f7fb022013-04-25 05:22:28 +00001987 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1988 vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
1989 vp_oper->state = *vp_admin;
1990 if (MLX4_VGT != vp_admin->default_vlan) {
1991 err = __mlx4_register_vlan(&priv->dev, port,
1992 vp_admin->default_vlan, &(vp_oper->vlan_idx));
1993 if (err) {
1994 vp_oper->vlan_idx = NO_INDX;
Joe Perches1a91de22014-05-07 12:52:57 -07001995 mlx4_warn(&priv->dev,
Masanari Iida1a84db52014-08-29 23:37:33 +09001996 "No vlan resources slave %d, port %d\n",
Rony Efraim3f7fb022013-04-25 05:22:28 +00001997 slave, port);
1998 return err;
1999 }
Joe Perches1a91de22014-05-07 12:52:57 -07002000 mlx4_dbg(&priv->dev, "alloc vlan %d idx %d slave %d port %d\n",
Rony Efraim3f7fb022013-04-25 05:22:28 +00002001 (int)(vp_oper->state.default_vlan),
2002 vp_oper->vlan_idx, slave, port);
2003 }
Rony Efraime6b6a232013-04-25 05:22:29 +00002004 if (vp_admin->spoofchk) {
2005 vp_oper->mac_idx = __mlx4_register_mac(&priv->dev,
2006 port,
2007 vp_admin->mac);
2008 if (0 > vp_oper->mac_idx) {
2009 err = vp_oper->mac_idx;
2010 vp_oper->mac_idx = NO_INDX;
Joe Perches1a91de22014-05-07 12:52:57 -07002011 mlx4_warn(&priv->dev,
Masanari Iida1a84db52014-08-29 23:37:33 +09002012 "No mac resources slave %d, port %d\n",
Rony Efraime6b6a232013-04-25 05:22:29 +00002013 slave, port);
2014 return err;
2015 }
Joe Perches1a91de22014-05-07 12:52:57 -07002016 mlx4_dbg(&priv->dev, "alloc mac %llx idx %d slave %d port %d\n",
Rony Efraime6b6a232013-04-25 05:22:29 +00002017 vp_oper->state.mac, vp_oper->mac_idx, slave, port);
2018 }
Rony Efraim0eb62b92013-04-25 05:22:26 +00002019 }
2020 return 0;
2021}
2022
Rony Efraim3f7fb022013-04-25 05:22:28 +00002023static void mlx4_master_deactivate_admin_state(struct mlx4_priv *priv, int slave)
2024{
2025 int port;
2026 struct mlx4_vport_oper_state *vp_oper;
Matan Barak449fc482014-03-19 18:11:52 +02002027 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
2028 &priv->dev, slave);
2029 int min_port = find_first_bit(actv_ports.ports,
2030 priv->dev.caps.num_ports) + 1;
2031 int max_port = min_port - 1 +
2032 bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002033
Matan Barak449fc482014-03-19 18:11:52 +02002034
2035 for (port = min_port; port <= max_port; port++) {
2036 if (!test_bit(port - 1, actv_ports.ports))
2037 continue;
Jack Morgenstein99ec41d2014-05-29 16:31:03 +03002038 priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
2039 MLX4_VF_SMI_DISABLED;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002040 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
2041 if (NO_INDX != vp_oper->vlan_idx) {
2042 __mlx4_unregister_vlan(&priv->dev,
Jack Morgenstein2009d002013-11-03 10:03:19 +02002043 port, vp_oper->state.default_vlan);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002044 vp_oper->vlan_idx = NO_INDX;
2045 }
Rony Efraime6b6a232013-04-25 05:22:29 +00002046 if (NO_INDX != vp_oper->mac_idx) {
Jack Morgensteinc32b7df2013-11-03 10:04:07 +02002047 __mlx4_unregister_mac(&priv->dev, port, vp_oper->state.mac);
Rony Efraime6b6a232013-04-25 05:22:29 +00002048 vp_oper->mac_idx = NO_INDX;
2049 }
Rony Efraim3f7fb022013-04-25 05:22:28 +00002050 }
2051 return;
2052}
2053
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002054static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
2055 u16 param, u8 toggle)
2056{
2057 struct mlx4_priv *priv = mlx4_priv(dev);
2058 struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
2059 u32 reply;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002060 u8 is_going_down = 0;
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002061 int i;
Jack Morgenstein311f8132012-11-27 16:24:30 +00002062 unsigned long flags;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002063
2064 slave_state[slave].comm_toggle ^= 1;
2065 reply = (u32) slave_state[slave].comm_toggle << 31;
2066 if (toggle != slave_state[slave].comm_toggle) {
Joe Perches1a91de22014-05-07 12:52:57 -07002067 mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER STATE COMPROMISED ***\n",
2068 toggle, slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002069 goto reset_slave;
2070 }
2071 if (cmd == MLX4_COMM_CMD_RESET) {
2072 mlx4_warn(dev, "Received reset from slave:%d\n", slave);
2073 slave_state[slave].active = false;
Jack Morgenstein2c957ff2013-11-03 10:03:21 +02002074 slave_state[slave].old_vlan_api = false;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002075 mlx4_master_deactivate_admin_state(priv, slave);
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002076 for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
2077 slave_state[slave].event_eq[i].eqn = -1;
2078 slave_state[slave].event_eq[i].token = 0;
2079 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002080 /*check if we are in the middle of FLR process,
2081 if so return "retry" status to the slave*/
Or Gerlitz162344e2012-05-15 10:34:57 +00002082 if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002083 goto inform_slave_state;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002084
Jack Morgensteinfc065732012-08-03 08:40:42 +00002085 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
2086
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002087 /* write the version in the event field */
2088 reply |= mlx4_comm_get_version();
2089
2090 goto reset_slave;
2091 }
2092 /*command from slave in the middle of FLR*/
2093 if (cmd != MLX4_COMM_CMD_RESET &&
2094 MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
Joe Perches1a91de22014-05-07 12:52:57 -07002095 mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) in the middle of FLR\n",
2096 slave, cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002097 return;
2098 }
2099
2100 switch (cmd) {
2101 case MLX4_COMM_CMD_VHCR0:
2102 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
2103 goto reset_slave;
2104 slave_state[slave].vhcr_dma = ((u64) param) << 48;
2105 priv->mfunc.master.slave_state[slave].cookie = 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002106 break;
2107 case MLX4_COMM_CMD_VHCR1:
2108 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
2109 goto reset_slave;
2110 slave_state[slave].vhcr_dma |= ((u64) param) << 32;
2111 break;
2112 case MLX4_COMM_CMD_VHCR2:
2113 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
2114 goto reset_slave;
2115 slave_state[slave].vhcr_dma |= ((u64) param) << 16;
2116 break;
2117 case MLX4_COMM_CMD_VHCR_EN:
2118 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
2119 goto reset_slave;
2120 slave_state[slave].vhcr_dma |= param;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002121 if (mlx4_master_activate_admin_state(priv, slave))
2122 goto reset_slave;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002123 slave_state[slave].active = true;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002124 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002125 break;
2126 case MLX4_COMM_CMD_VHCR_POST:
2127 if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
Yishai Hadas55ad3592015-01-25 16:59:42 +02002128 (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST)) {
2129 mlx4_warn(dev, "slave:%d is out of sync, cmd=0x%x, last command=0x%x, reset is needed\n",
2130 slave, cmd, slave_state[slave].last_cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002131 goto reset_slave;
Yishai Hadas55ad3592015-01-25 16:59:42 +02002132 }
Roland Dreierf3d4c892012-09-25 21:24:07 -07002133
2134 mutex_lock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002135 if (mlx4_master_process_vhcr(dev, slave, NULL)) {
Joe Perches1a91de22014-05-07 12:52:57 -07002136 mlx4_err(dev, "Failed processing vhcr for slave:%d, resetting slave\n",
2137 slave);
Roland Dreierf3d4c892012-09-25 21:24:07 -07002138 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002139 goto reset_slave;
2140 }
Roland Dreierf3d4c892012-09-25 21:24:07 -07002141 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002142 break;
2143 default:
2144 mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
2145 goto reset_slave;
2146 }
Jack Morgenstein311f8132012-11-27 16:24:30 +00002147 spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002148 if (!slave_state[slave].is_slave_going_down)
2149 slave_state[slave].last_cmd = cmd;
2150 else
2151 is_going_down = 1;
Jack Morgenstein311f8132012-11-27 16:24:30 +00002152 spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002153 if (is_going_down) {
Joe Perches1a91de22014-05-07 12:52:57 -07002154 mlx4_warn(dev, "Slave is going down aborting command(%d) executing from slave:%d\n",
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002155 cmd, slave);
2156 return;
2157 }
2158 __raw_writel((__force u32) cpu_to_be32(reply),
2159 &priv->mfunc.comm[slave].slave_read);
2160 mmiowb();
2161
2162 return;
2163
2164reset_slave:
Eli Cohenc82e9aa2011-12-13 04:15:24 +00002165 /* cleanup any slave resources */
Yishai Hadas55ad3592015-01-25 16:59:42 +02002166 if (dev->persist->interface_state & MLX4_INTERFACE_STATE_UP)
2167 mlx4_delete_all_resources_for_slave(dev, slave);
2168
2169 if (cmd != MLX4_COMM_CMD_RESET) {
2170 mlx4_warn(dev, "Turn on internal error to force reset, slave=%d, cmd=0x%x\n",
2171 slave, cmd);
2172 /* Turn on internal error letting slave reset itself immeditaly,
2173 * otherwise it might take till timeout on command is passed
2174 */
2175 reply |= ((u32)COMM_CHAN_EVENT_INTERNAL_ERR);
2176 }
2177
Jack Morgenstein311f8132012-11-27 16:24:30 +00002178 spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002179 if (!slave_state[slave].is_slave_going_down)
2180 slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
Jack Morgenstein311f8132012-11-27 16:24:30 +00002181 spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002182 /*with slave in the middle of flr, no need to clean resources again.*/
2183inform_slave_state:
2184 memset(&slave_state[slave].event_eq, 0,
2185 sizeof(struct mlx4_slave_event_eq_info));
2186 __raw_writel((__force u32) cpu_to_be32(reply),
2187 &priv->mfunc.comm[slave].slave_read);
2188 wmb();
2189}
2190
2191/* master command processing */
2192void mlx4_master_comm_channel(struct work_struct *work)
2193{
2194 struct mlx4_mfunc_master_ctx *master =
2195 container_of(work,
2196 struct mlx4_mfunc_master_ctx,
2197 comm_work);
2198 struct mlx4_mfunc *mfunc =
2199 container_of(master, struct mlx4_mfunc, master);
2200 struct mlx4_priv *priv =
2201 container_of(mfunc, struct mlx4_priv, mfunc);
2202 struct mlx4_dev *dev = &priv->dev;
2203 __be32 *bit_vec;
2204 u32 comm_cmd;
2205 u32 vec;
2206 int i, j, slave;
2207 int toggle;
2208 int served = 0;
2209 int reported = 0;
2210 u32 slt;
2211
2212 bit_vec = master->comm_arm_bit_vector;
2213 for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
2214 vec = be32_to_cpu(bit_vec[i]);
2215 for (j = 0; j < 32; j++) {
2216 if (!(vec & (1 << j)))
2217 continue;
2218 ++reported;
2219 slave = (i * 32) + j;
2220 comm_cmd = swab32(readl(
2221 &mfunc->comm[slave].slave_write));
2222 slt = swab32(readl(&mfunc->comm[slave].slave_read))
2223 >> 31;
2224 toggle = comm_cmd >> 31;
2225 if (toggle != slt) {
2226 if (master->slave_state[slave].comm_toggle
2227 != slt) {
Amir Vadaic20862c2014-05-22 15:55:40 +03002228 pr_info("slave %d out of sync. read toggle %d, state toggle %d. Resynching.\n",
2229 slave, slt,
2230 master->slave_state[slave].comm_toggle);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002231 master->slave_state[slave].comm_toggle =
2232 slt;
2233 }
2234 mlx4_master_do_cmd(dev, slave,
2235 comm_cmd >> 16 & 0xff,
2236 comm_cmd & 0xffff, toggle);
2237 ++served;
2238 }
2239 }
2240 }
2241
2242 if (reported && reported != served)
Joe Perches1a91de22014-05-07 12:52:57 -07002243 mlx4_warn(dev, "Got command event with bitmask from %d slaves but %d were served\n",
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002244 reported, served);
2245
2246 if (mlx4_ARM_COMM_CHANNEL(dev))
2247 mlx4_warn(dev, "Failed to arm comm channel events\n");
2248}
2249
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002250static int sync_toggles(struct mlx4_dev *dev)
2251{
2252 struct mlx4_priv *priv = mlx4_priv(dev);
Yishai Hadas55ad3592015-01-25 16:59:42 +02002253 u32 wr_toggle;
2254 u32 rd_toggle;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002255 unsigned long end;
2256
Yishai Hadas55ad3592015-01-25 16:59:42 +02002257 wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write));
2258 if (wr_toggle == 0xffffffff)
2259 end = jiffies + msecs_to_jiffies(30000);
2260 else
2261 end = jiffies + msecs_to_jiffies(5000);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002262
2263 while (time_before(jiffies, end)) {
Yishai Hadas55ad3592015-01-25 16:59:42 +02002264 rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read));
2265 if (wr_toggle == 0xffffffff || rd_toggle == 0xffffffff) {
2266 /* PCI might be offline */
2267 msleep(100);
2268 wr_toggle = swab32(readl(&priv->mfunc.comm->
2269 slave_write));
2270 continue;
2271 }
2272
2273 if (rd_toggle >> 31 == wr_toggle >> 31) {
2274 priv->cmd.comm_toggle = rd_toggle >> 31;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002275 return 0;
2276 }
2277
2278 cond_resched();
2279 }
2280
2281 /*
2282 * we could reach here if for example the previous VM using this
2283 * function misbehaved and left the channel with unsynced state. We
2284 * should fix this here and give this VM a chance to use a properly
2285 * synced channel
2286 */
2287 mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
2288 __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
2289 __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
2290 priv->cmd.comm_toggle = 0;
2291
2292 return 0;
2293}
2294
2295int mlx4_multi_func_init(struct mlx4_dev *dev)
2296{
2297 struct mlx4_priv *priv = mlx4_priv(dev);
2298 struct mlx4_slave_state *s_state;
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002299 int i, j, err, port;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002300
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002301 if (mlx4_is_master(dev))
2302 priv->mfunc.comm =
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002303 ioremap(pci_resource_start(dev->persist->pdev,
2304 priv->fw.comm_bar) +
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002305 priv->fw.comm_base, MLX4_COMM_PAGESIZE);
2306 else
2307 priv->mfunc.comm =
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002308 ioremap(pci_resource_start(dev->persist->pdev, 2) +
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002309 MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
2310 if (!priv->mfunc.comm) {
Joe Perches1a91de22014-05-07 12:52:57 -07002311 mlx4_err(dev, "Couldn't map communication vector\n");
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002312 goto err_vhcr;
2313 }
2314
2315 if (mlx4_is_master(dev)) {
Ido Shamay4abccb62015-04-02 16:31:09 +03002316 struct mlx4_vf_oper_state *vf_oper;
2317 struct mlx4_vf_admin_state *vf_admin;
2318
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002319 priv->mfunc.master.slave_state =
2320 kzalloc(dev->num_slaves *
2321 sizeof(struct mlx4_slave_state), GFP_KERNEL);
2322 if (!priv->mfunc.master.slave_state)
2323 goto err_comm;
2324
Rony Efraim0eb62b92013-04-25 05:22:26 +00002325 priv->mfunc.master.vf_admin =
2326 kzalloc(dev->num_slaves *
2327 sizeof(struct mlx4_vf_admin_state), GFP_KERNEL);
2328 if (!priv->mfunc.master.vf_admin)
2329 goto err_comm_admin;
2330
2331 priv->mfunc.master.vf_oper =
2332 kzalloc(dev->num_slaves *
2333 sizeof(struct mlx4_vf_oper_state), GFP_KERNEL);
2334 if (!priv->mfunc.master.vf_oper)
2335 goto err_comm_oper;
2336
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002337 for (i = 0; i < dev->num_slaves; ++i) {
Ido Shamay4abccb62015-04-02 16:31:09 +03002338 vf_admin = &priv->mfunc.master.vf_admin[i];
2339 vf_oper = &priv->mfunc.master.vf_oper[i];
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002340 s_state = &priv->mfunc.master.slave_state[i];
2341 s_state->last_cmd = MLX4_COMM_CMD_RESET;
Jack Morgensteinbffb0232015-03-24 15:18:39 +02002342 mutex_init(&priv->mfunc.master.gen_eqe_mutex[i]);
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002343 for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
2344 s_state->event_eq[j].eqn = -1;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002345 __raw_writel((__force u32) 0,
2346 &priv->mfunc.comm[i].slave_write);
2347 __raw_writel((__force u32) 0,
2348 &priv->mfunc.comm[i].slave_read);
2349 mmiowb();
2350 for (port = 1; port <= MLX4_MAX_PORTS; port++) {
Ido Shamay4abccb62015-04-02 16:31:09 +03002351 struct mlx4_vport_state *admin_vport;
2352 struct mlx4_vport_state *oper_vport;
2353
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002354 s_state->vlan_filter[port] =
2355 kzalloc(sizeof(struct mlx4_vlan_fltr),
2356 GFP_KERNEL);
2357 if (!s_state->vlan_filter[port]) {
2358 if (--port)
2359 kfree(s_state->vlan_filter[port]);
2360 goto err_slaves;
2361 }
Ido Shamay4abccb62015-04-02 16:31:09 +03002362
2363 admin_vport = &vf_admin->vport[port];
2364 oper_vport = &vf_oper->vport[port].state;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002365 INIT_LIST_HEAD(&s_state->mcast_filters[port]);
Ido Shamay4abccb62015-04-02 16:31:09 +03002366 admin_vport->default_vlan = MLX4_VGT;
2367 oper_vport->default_vlan = MLX4_VGT;
Ido Shamay08068cd2015-04-02 16:31:15 +03002368 admin_vport->qos_vport =
2369 MLX4_VPP_DEFAULT_VPORT;
2370 oper_vport->qos_vport = MLX4_VPP_DEFAULT_VPORT;
Ido Shamay4abccb62015-04-02 16:31:09 +03002371 vf_oper->vport[port].vlan_idx = NO_INDX;
2372 vf_oper->vport[port].mac_idx = NO_INDX;
Yishai Hadasfb517a42015-03-03 11:23:32 +02002373 mlx4_set_random_admin_guid(dev, i, port);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002374 }
2375 spin_lock_init(&s_state->lock);
2376 }
2377
Ido Shamay666672d2015-04-02 16:31:14 +03002378 if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP) {
2379 for (port = 1; port <= dev->caps.num_ports; port++) {
2380 if (mlx4_is_eth(dev, port)) {
2381 mlx4_set_default_port_qos(dev, port);
2382 mlx4_allocate_port_vpps(dev, port);
2383 }
2384 }
2385 }
2386
Or Gerlitz08ff3232012-10-21 14:59:24 +00002387 memset(&priv->mfunc.master.cmd_eqe, 0, dev->caps.eqe_size);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002388 priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
2389 INIT_WORK(&priv->mfunc.master.comm_work,
2390 mlx4_master_comm_channel);
2391 INIT_WORK(&priv->mfunc.master.slave_event_work,
2392 mlx4_gen_slave_eqe);
2393 INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
2394 mlx4_master_handle_slave_flr);
2395 spin_lock_init(&priv->mfunc.master.slave_state_lock);
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00002396 spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002397 priv->mfunc.master.comm_wq =
2398 create_singlethread_workqueue("mlx4_comm");
2399 if (!priv->mfunc.master.comm_wq)
2400 goto err_slaves;
2401
2402 if (mlx4_init_resource_tracker(dev))
2403 goto err_thread;
2404
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002405 } else {
2406 err = sync_toggles(dev);
2407 if (err) {
2408 mlx4_err(dev, "Couldn't sync toggles\n");
2409 goto err_comm;
2410 }
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002411 }
2412 return 0;
2413
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002414err_thread:
2415 flush_workqueue(priv->mfunc.master.comm_wq);
2416 destroy_workqueue(priv->mfunc.master.comm_wq);
2417err_slaves:
2418 while (--i) {
2419 for (port = 1; port <= MLX4_MAX_PORTS; port++)
2420 kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
2421 }
Rony Efraim0eb62b92013-04-25 05:22:26 +00002422 kfree(priv->mfunc.master.vf_oper);
2423err_comm_oper:
2424 kfree(priv->mfunc.master.vf_admin);
2425err_comm_admin:
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002426 kfree(priv->mfunc.master.slave_state);
2427err_comm:
2428 iounmap(priv->mfunc.comm);
2429err_vhcr:
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002430 dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
2431 priv->mfunc.vhcr,
2432 priv->mfunc.vhcr_dma);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002433 priv->mfunc.vhcr = NULL;
2434 return -ENOMEM;
2435}
2436
Roland Dreier225c7b12007-05-08 18:00:38 -07002437int mlx4_cmd_init(struct mlx4_dev *dev)
2438{
2439 struct mlx4_priv *priv = mlx4_priv(dev);
Matan Barakffc39f62014-11-13 14:45:29 +02002440 int flags = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002441
Matan Barakffc39f62014-11-13 14:45:29 +02002442 if (!priv->cmd.initialized) {
Matan Barakffc39f62014-11-13 14:45:29 +02002443 mutex_init(&priv->cmd.slave_cmd_mutex);
2444 sema_init(&priv->cmd.poll_sem, 1);
2445 priv->cmd.use_events = 0;
2446 priv->cmd.toggle = 1;
2447 priv->cmd.initialized = 1;
2448 flags |= MLX4_CMD_CLEANUP_STRUCT;
2449 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002450
Matan Barakffc39f62014-11-13 14:45:29 +02002451 if (!mlx4_is_slave(dev) && !priv->cmd.hcr) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002452 priv->cmd.hcr = ioremap(pci_resource_start(dev->persist->pdev,
2453 0) + MLX4_HCR_BASE, MLX4_HCR_SIZE);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002454 if (!priv->cmd.hcr) {
Joe Perches1a91de22014-05-07 12:52:57 -07002455 mlx4_err(dev, "Couldn't map command register\n");
Matan Barakffc39f62014-11-13 14:45:29 +02002456 goto err;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002457 }
Matan Barakffc39f62014-11-13 14:45:29 +02002458 flags |= MLX4_CMD_CLEANUP_HCR;
Roland Dreier225c7b12007-05-08 18:00:38 -07002459 }
2460
Matan Barakffc39f62014-11-13 14:45:29 +02002461 if (mlx4_is_mfunc(dev) && !priv->mfunc.vhcr) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002462 priv->mfunc.vhcr = dma_alloc_coherent(&dev->persist->pdev->dev,
2463 PAGE_SIZE,
Roland Dreierf3d4c892012-09-25 21:24:07 -07002464 &priv->mfunc.vhcr_dma,
2465 GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002466 if (!priv->mfunc.vhcr)
Matan Barakffc39f62014-11-13 14:45:29 +02002467 goto err;
2468
2469 flags |= MLX4_CMD_CLEANUP_VHCR;
Roland Dreierf3d4c892012-09-25 21:24:07 -07002470 }
2471
Matan Barakffc39f62014-11-13 14:45:29 +02002472 if (!priv->cmd.pool) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002473 priv->cmd.pool = pci_pool_create("mlx4_cmd",
2474 dev->persist->pdev,
Matan Barakffc39f62014-11-13 14:45:29 +02002475 MLX4_MAILBOX_SIZE,
2476 MLX4_MAILBOX_SIZE, 0);
2477 if (!priv->cmd.pool)
2478 goto err;
2479
2480 flags |= MLX4_CMD_CLEANUP_POOL;
2481 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002482
2483 return 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002484
Matan Barakffc39f62014-11-13 14:45:29 +02002485err:
2486 mlx4_cmd_cleanup(dev, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002487 return -ENOMEM;
Roland Dreier225c7b12007-05-08 18:00:38 -07002488}
2489
Yishai Hadas55ad3592015-01-25 16:59:42 +02002490void mlx4_report_internal_err_comm_event(struct mlx4_dev *dev)
2491{
2492 struct mlx4_priv *priv = mlx4_priv(dev);
2493 int slave;
2494 u32 slave_read;
2495
2496 /* Report an internal error event to all
2497 * communication channels.
2498 */
2499 for (slave = 0; slave < dev->num_slaves; slave++) {
2500 slave_read = swab32(readl(&priv->mfunc.comm[slave].slave_read));
2501 slave_read |= (u32)COMM_CHAN_EVENT_INTERNAL_ERR;
2502 __raw_writel((__force u32)cpu_to_be32(slave_read),
2503 &priv->mfunc.comm[slave].slave_read);
2504 /* Make sure that our comm channel write doesn't
2505 * get mixed in with writes from another CPU.
2506 */
2507 mmiowb();
2508 }
2509}
2510
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002511void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
2512{
2513 struct mlx4_priv *priv = mlx4_priv(dev);
2514 int i, port;
2515
2516 if (mlx4_is_master(dev)) {
2517 flush_workqueue(priv->mfunc.master.comm_wq);
2518 destroy_workqueue(priv->mfunc.master.comm_wq);
2519 for (i = 0; i < dev->num_slaves; i++) {
2520 for (port = 1; port <= MLX4_MAX_PORTS; port++)
2521 kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
2522 }
2523 kfree(priv->mfunc.master.slave_state);
Rony Efraim0eb62b92013-04-25 05:22:26 +00002524 kfree(priv->mfunc.master.vf_admin);
2525 kfree(priv->mfunc.master.vf_oper);
Yishai Hadas55ad3592015-01-25 16:59:42 +02002526 dev->num_slaves = 0;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002527 }
Eugenia Emantayevf08ad062012-02-06 06:26:17 +00002528
2529 iounmap(priv->mfunc.comm);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002530}
2531
Matan Barakffc39f62014-11-13 14:45:29 +02002532void mlx4_cmd_cleanup(struct mlx4_dev *dev, int cleanup_mask)
Roland Dreier225c7b12007-05-08 18:00:38 -07002533{
2534 struct mlx4_priv *priv = mlx4_priv(dev);
2535
Matan Barakffc39f62014-11-13 14:45:29 +02002536 if (priv->cmd.pool && (cleanup_mask & MLX4_CMD_CLEANUP_POOL)) {
2537 pci_pool_destroy(priv->cmd.pool);
2538 priv->cmd.pool = NULL;
2539 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002540
Matan Barakffc39f62014-11-13 14:45:29 +02002541 if (!mlx4_is_slave(dev) && priv->cmd.hcr &&
2542 (cleanup_mask & MLX4_CMD_CLEANUP_HCR)) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002543 iounmap(priv->cmd.hcr);
Matan Barakffc39f62014-11-13 14:45:29 +02002544 priv->cmd.hcr = NULL;
2545 }
2546 if (mlx4_is_mfunc(dev) && priv->mfunc.vhcr &&
2547 (cleanup_mask & MLX4_CMD_CLEANUP_VHCR)) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002548 dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
Roland Dreierf3d4c892012-09-25 21:24:07 -07002549 priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
Matan Barakffc39f62014-11-13 14:45:29 +02002550 priv->mfunc.vhcr = NULL;
2551 }
2552 if (priv->cmd.initialized && (cleanup_mask & MLX4_CMD_CLEANUP_STRUCT))
2553 priv->cmd.initialized = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002554}
2555
2556/*
2557 * Switch to using events to issue FW commands (can only be called
2558 * after event queue for command events has been initialized).
2559 */
2560int mlx4_cmd_use_events(struct mlx4_dev *dev)
2561{
2562 struct mlx4_priv *priv = mlx4_priv(dev);
2563 int i;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002564 int err = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002565
2566 priv->cmd.context = kmalloc(priv->cmd.max_cmds *
2567 sizeof (struct mlx4_cmd_context),
2568 GFP_KERNEL);
2569 if (!priv->cmd.context)
2570 return -ENOMEM;
2571
2572 for (i = 0; i < priv->cmd.max_cmds; ++i) {
2573 priv->cmd.context[i].token = i;
2574 priv->cmd.context[i].next = i + 1;
Yishai Hadasf5aef5a2015-01-25 16:59:39 +02002575 /* To support fatal error flow, initialize all
2576 * cmd contexts to allow simulating completions
2577 * with complete() at any time.
2578 */
2579 init_completion(&priv->cmd.context[i].done);
Roland Dreier225c7b12007-05-08 18:00:38 -07002580 }
2581
2582 priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
2583 priv->cmd.free_head = 0;
2584
2585 sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
2586 spin_lock_init(&priv->cmd.context_lock);
2587
2588 for (priv->cmd.token_mask = 1;
2589 priv->cmd.token_mask < priv->cmd.max_cmds;
2590 priv->cmd.token_mask <<= 1)
2591 ; /* nothing */
2592 --priv->cmd.token_mask;
2593
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002594 down(&priv->cmd.poll_sem);
Roland Dreier225c7b12007-05-08 18:00:38 -07002595 priv->cmd.use_events = 1;
2596
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002597 return err;
Roland Dreier225c7b12007-05-08 18:00:38 -07002598}
2599
2600/*
2601 * Switch back to polling (used when shutting down the device)
2602 */
2603void mlx4_cmd_use_polling(struct mlx4_dev *dev)
2604{
2605 struct mlx4_priv *priv = mlx4_priv(dev);
2606 int i;
2607
2608 priv->cmd.use_events = 0;
2609
2610 for (i = 0; i < priv->cmd.max_cmds; ++i)
2611 down(&priv->cmd.event_sem);
2612
2613 kfree(priv->cmd.context);
2614
2615 up(&priv->cmd.poll_sem);
2616}
2617
2618struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
2619{
2620 struct mlx4_cmd_mailbox *mailbox;
2621
2622 mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
2623 if (!mailbox)
2624 return ERR_PTR(-ENOMEM);
2625
2626 mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
2627 &mailbox->dma);
2628 if (!mailbox->buf) {
2629 kfree(mailbox);
2630 return ERR_PTR(-ENOMEM);
2631 }
2632
Jack Morgenstein571b8b92013-11-07 12:19:50 +02002633 memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
2634
Roland Dreier225c7b12007-05-08 18:00:38 -07002635 return mailbox;
2636}
2637EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
2638
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002639void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
2640 struct mlx4_cmd_mailbox *mailbox)
Roland Dreier225c7b12007-05-08 18:00:38 -07002641{
2642 if (!mailbox)
2643 return;
2644
2645 pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
2646 kfree(mailbox);
2647}
2648EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002649
2650u32 mlx4_comm_get_version(void)
2651{
2652 return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
2653}
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002654
2655static int mlx4_get_slave_indx(struct mlx4_dev *dev, int vf)
2656{
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002657 if ((vf < 0) || (vf >= dev->persist->num_vfs)) {
2658 mlx4_err(dev, "Bad vf number:%d (number of activated vf: %d)\n",
2659 vf, dev->persist->num_vfs);
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002660 return -EINVAL;
2661 }
2662
2663 return vf+1;
2664}
2665
Matan Barakf74462a2014-03-19 18:11:51 +02002666int mlx4_get_vf_indx(struct mlx4_dev *dev, int slave)
2667{
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002668 if (slave < 1 || slave > dev->persist->num_vfs) {
Matan Barakf74462a2014-03-19 18:11:51 +02002669 mlx4_err(dev,
2670 "Bad slave number:%d (number of activated slaves: %lu)\n",
2671 slave, dev->num_slaves);
2672 return -EINVAL;
2673 }
2674 return slave - 1;
2675}
2676
Yishai Hadasf5aef5a2015-01-25 16:59:39 +02002677void mlx4_cmd_wake_completions(struct mlx4_dev *dev)
2678{
2679 struct mlx4_priv *priv = mlx4_priv(dev);
2680 struct mlx4_cmd_context *context;
2681 int i;
2682
2683 spin_lock(&priv->cmd.context_lock);
2684 if (priv->cmd.context) {
2685 for (i = 0; i < priv->cmd.max_cmds; ++i) {
2686 context = &priv->cmd.context[i];
2687 context->fw_status = CMD_STAT_INTERNAL_ERR;
2688 context->result =
2689 mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
2690 complete(&context->done);
2691 }
2692 }
2693 spin_unlock(&priv->cmd.context_lock);
2694}
2695
Matan Barakf74462a2014-03-19 18:11:51 +02002696struct mlx4_active_ports mlx4_get_active_ports(struct mlx4_dev *dev, int slave)
2697{
2698 struct mlx4_active_ports actv_ports;
2699 int vf;
2700
2701 bitmap_zero(actv_ports.ports, MLX4_MAX_PORTS);
2702
2703 if (slave == 0) {
2704 bitmap_fill(actv_ports.ports, dev->caps.num_ports);
2705 return actv_ports;
2706 }
2707
2708 vf = mlx4_get_vf_indx(dev, slave);
2709 if (vf < 0)
2710 return actv_ports;
2711
2712 bitmap_set(actv_ports.ports, dev->dev_vfs[vf].min_port - 1,
2713 min((int)dev->dev_vfs[mlx4_get_vf_indx(dev, slave)].n_ports,
2714 dev->caps.num_ports));
2715
2716 return actv_ports;
2717}
2718EXPORT_SYMBOL_GPL(mlx4_get_active_ports);
2719
2720int mlx4_slave_convert_port(struct mlx4_dev *dev, int slave, int port)
2721{
2722 unsigned n;
2723 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2724 unsigned m = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2725
2726 if (port <= 0 || port > m)
2727 return -EINVAL;
2728
2729 n = find_first_bit(actv_ports.ports, dev->caps.num_ports);
2730 if (port <= n)
2731 port = n + 1;
2732
2733 return port;
2734}
2735EXPORT_SYMBOL_GPL(mlx4_slave_convert_port);
2736
2737int mlx4_phys_to_slave_port(struct mlx4_dev *dev, int slave, int port)
2738{
2739 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2740 if (test_bit(port - 1, actv_ports.ports))
2741 return port -
2742 find_first_bit(actv_ports.ports, dev->caps.num_ports);
2743
2744 return -1;
2745}
2746EXPORT_SYMBOL_GPL(mlx4_phys_to_slave_port);
2747
2748struct mlx4_slaves_pport mlx4_phys_to_slaves_pport(struct mlx4_dev *dev,
2749 int port)
2750{
2751 unsigned i;
2752 struct mlx4_slaves_pport slaves_pport;
2753
2754 bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
2755
2756 if (port <= 0 || port > dev->caps.num_ports)
2757 return slaves_pport;
2758
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002759 for (i = 0; i < dev->persist->num_vfs + 1; i++) {
Matan Barakf74462a2014-03-19 18:11:51 +02002760 struct mlx4_active_ports actv_ports =
2761 mlx4_get_active_ports(dev, i);
2762 if (test_bit(port - 1, actv_ports.ports))
2763 set_bit(i, slaves_pport.slaves);
2764 }
2765
2766 return slaves_pport;
2767}
2768EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport);
2769
2770struct mlx4_slaves_pport mlx4_phys_to_slaves_pport_actv(
2771 struct mlx4_dev *dev,
2772 const struct mlx4_active_ports *crit_ports)
2773{
2774 unsigned i;
2775 struct mlx4_slaves_pport slaves_pport;
2776
2777 bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
2778
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002779 for (i = 0; i < dev->persist->num_vfs + 1; i++) {
Matan Barakf74462a2014-03-19 18:11:51 +02002780 struct mlx4_active_ports actv_ports =
2781 mlx4_get_active_ports(dev, i);
2782 if (bitmap_equal(crit_ports->ports, actv_ports.ports,
2783 dev->caps.num_ports))
2784 set_bit(i, slaves_pport.slaves);
2785 }
2786
2787 return slaves_pport;
2788}
2789EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport_actv);
2790
Matan Baraka91c7722014-09-10 16:41:53 +03002791static int mlx4_slaves_closest_port(struct mlx4_dev *dev, int slave, int port)
2792{
2793 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2794 int min_port = find_first_bit(actv_ports.ports, dev->caps.num_ports)
2795 + 1;
2796 int max_port = min_port +
2797 bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2798
2799 if (port < min_port)
2800 port = min_port;
2801 else if (port >= max_port)
2802 port = max_port - 1;
2803
2804 return port;
2805}
2806
Ido Shamaycda373f2015-04-02 16:31:16 +03002807static int mlx4_set_vport_qos(struct mlx4_priv *priv, int slave, int port,
2808 int max_tx_rate)
2809{
2810 int i;
2811 int err;
2812 struct mlx4_qos_manager *port_qos;
2813 struct mlx4_dev *dev = &priv->dev;
2814 struct mlx4_vport_qos_param vpp_qos[MLX4_NUM_UP];
2815
2816 port_qos = &priv->mfunc.master.qos_ctl[port];
2817 memset(vpp_qos, 0, sizeof(struct mlx4_vport_qos_param) * MLX4_NUM_UP);
2818
2819 if (slave > port_qos->num_of_qos_vfs) {
2820 mlx4_info(dev, "No availible VPP resources for this VF\n");
2821 return -EINVAL;
2822 }
2823
2824 /* Query for default QoS values from Vport 0 is needed */
2825 err = mlx4_SET_VPORT_QOS_get(dev, port, 0, vpp_qos);
2826 if (err) {
2827 mlx4_info(dev, "Failed to query Vport 0 QoS values\n");
2828 return err;
2829 }
2830
2831 for (i = 0; i < MLX4_NUM_UP; i++) {
2832 if (test_bit(i, port_qos->priority_bm) && max_tx_rate) {
2833 vpp_qos[i].max_avg_bw = max_tx_rate;
2834 vpp_qos[i].enable = 1;
2835 } else {
2836 /* if user supplied tx_rate == 0, meaning no rate limit
2837 * configuration is required. so we are leaving the
2838 * value of max_avg_bw as queried from Vport 0.
2839 */
2840 vpp_qos[i].enable = 0;
2841 }
2842 }
2843
2844 err = mlx4_SET_VPORT_QOS_set(dev, port, slave, vpp_qos);
2845 if (err) {
2846 mlx4_info(dev, "Failed to set Vport %d QoS values\n", slave);
2847 return err;
2848 }
2849
2850 return 0;
2851}
2852
2853static bool mlx4_is_vf_vst_and_prio_qos(struct mlx4_dev *dev, int port,
2854 struct mlx4_vport_state *vf_admin)
2855{
2856 struct mlx4_qos_manager *info;
2857 struct mlx4_priv *priv = mlx4_priv(dev);
2858
2859 if (!mlx4_is_master(dev) ||
2860 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
2861 return false;
2862
2863 info = &priv->mfunc.master.qos_ctl[port];
2864
2865 if (vf_admin->default_vlan != MLX4_VGT &&
2866 test_bit(vf_admin->default_qos, info->priority_bm))
2867 return true;
2868
2869 return false;
2870}
2871
2872static bool mlx4_valid_vf_state_change(struct mlx4_dev *dev, int port,
2873 struct mlx4_vport_state *vf_admin,
2874 int vlan, int qos)
2875{
2876 struct mlx4_vport_state dummy_admin = {0};
2877
2878 if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) ||
2879 !vf_admin->tx_rate)
2880 return true;
2881
2882 dummy_admin.default_qos = qos;
2883 dummy_admin.default_vlan = vlan;
2884
2885 /* VF wants to move to other VST state which is valid with current
2886 * rate limit. Either differnt default vlan in VST or other
2887 * supported QoS priority. Otherwise we don't allow this change when
2888 * the TX rate is still configured.
2889 */
2890 if (mlx4_is_vf_vst_and_prio_qos(dev, port, &dummy_admin))
2891 return true;
2892
2893 mlx4_info(dev, "Cannot change VF state to %s while rate is set\n",
2894 (vlan == MLX4_VGT) ? "VGT" : "VST");
2895
2896 if (vlan != MLX4_VGT)
2897 mlx4_info(dev, "VST priority %d not supported for QoS\n", qos);
2898
2899 mlx4_info(dev, "Please set rate to 0 prior to this VF state change\n");
2900
2901 return false;
2902}
2903
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002904int mlx4_set_vf_mac(struct mlx4_dev *dev, int port, int vf, u64 mac)
2905{
2906 struct mlx4_priv *priv = mlx4_priv(dev);
2907 struct mlx4_vport_state *s_info;
2908 int slave;
2909
2910 if (!mlx4_is_master(dev))
2911 return -EPROTONOSUPPORT;
2912
2913 slave = mlx4_get_slave_indx(dev, vf);
2914 if (slave < 0)
2915 return -EINVAL;
2916
Matan Baraka91c7722014-09-10 16:41:53 +03002917 port = mlx4_slaves_closest_port(dev, slave, port);
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002918 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
2919 s_info->mac = mac;
Carol Soto613d8c12015-06-02 16:07:25 -05002920 mlx4_info(dev, "default mac on vf %d port %d to %llX will take effect only after vf restart\n",
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002921 vf, port, s_info->mac);
2922 return 0;
2923}
2924EXPORT_SYMBOL_GPL(mlx4_set_vf_mac);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002925
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002926
Rony Efraim3f7fb022013-04-25 05:22:28 +00002927int mlx4_set_vf_vlan(struct mlx4_dev *dev, int port, int vf, u16 vlan, u8 qos)
2928{
2929 struct mlx4_priv *priv = mlx4_priv(dev);
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002930 struct mlx4_vport_state *vf_admin;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002931 int slave;
2932
2933 if ((!mlx4_is_master(dev)) ||
2934 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
2935 return -EPROTONOSUPPORT;
2936
2937 if ((vlan > 4095) || (qos > 7))
2938 return -EINVAL;
2939
2940 slave = mlx4_get_slave_indx(dev, vf);
2941 if (slave < 0)
2942 return -EINVAL;
2943
Matan Baraka91c7722014-09-10 16:41:53 +03002944 port = mlx4_slaves_closest_port(dev, slave, port);
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002945 vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002946
Ido Shamaycda373f2015-04-02 16:31:16 +03002947 if (!mlx4_valid_vf_state_change(dev, port, vf_admin, vlan, qos))
2948 return -EPERM;
2949
Rony Efraim3f7fb022013-04-25 05:22:28 +00002950 if ((0 == vlan) && (0 == qos))
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002951 vf_admin->default_vlan = MLX4_VGT;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002952 else
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002953 vf_admin->default_vlan = vlan;
2954 vf_admin->default_qos = qos;
2955
Ido Shamaycda373f2015-04-02 16:31:16 +03002956 /* If rate was configured prior to VST, we saved the configured rate
2957 * in vf_admin->rate and now, if priority supported we enforce the QoS
2958 */
2959 if (mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) &&
2960 vf_admin->tx_rate)
2961 vf_admin->qos_vport = slave;
2962
Rony Efraim0a6eac22013-06-27 19:05:22 +03002963 if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
2964 mlx4_info(dev,
2965 "updating vf %d port %d config will take effect on next VF restart\n",
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002966 vf, port);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002967 return 0;
2968}
2969EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
Rony Efraime6b6a232013-04-25 05:22:29 +00002970
Ido Shamaycda373f2015-04-02 16:31:16 +03002971int mlx4_set_vf_rate(struct mlx4_dev *dev, int port, int vf, int min_tx_rate,
2972 int max_tx_rate)
2973{
2974 int err;
2975 int slave;
2976 struct mlx4_vport_state *vf_admin;
2977 struct mlx4_priv *priv = mlx4_priv(dev);
2978
2979 if (!mlx4_is_master(dev) ||
2980 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
2981 return -EPROTONOSUPPORT;
2982
2983 if (min_tx_rate) {
2984 mlx4_info(dev, "Minimum BW share not supported\n");
2985 return -EPROTONOSUPPORT;
2986 }
2987
2988 slave = mlx4_get_slave_indx(dev, vf);
2989 if (slave < 0)
2990 return -EINVAL;
2991
2992 port = mlx4_slaves_closest_port(dev, slave, port);
2993 vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
2994
2995 err = mlx4_set_vport_qos(priv, slave, port, max_tx_rate);
2996 if (err) {
2997 mlx4_info(dev, "vf %d failed to set rate %d\n", vf,
2998 max_tx_rate);
2999 return err;
3000 }
3001
3002 vf_admin->tx_rate = max_tx_rate;
3003 /* if VF is not in supported mode (VST with supported prio),
3004 * we do not change vport configuration for its QPs, but save
3005 * the rate, so it will be enforced when it moves to supported
3006 * mode next time.
3007 */
3008 if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin)) {
3009 mlx4_info(dev,
3010 "rate set for VF %d when not in valid state\n", vf);
3011
3012 if (vf_admin->default_vlan != MLX4_VGT)
3013 mlx4_info(dev, "VST priority not supported by QoS\n");
3014 else
3015 mlx4_info(dev, "VF in VGT mode (needed VST)\n");
3016
3017 mlx4_info(dev,
3018 "rate %d take affect when VF moves to valid state\n",
3019 max_tx_rate);
3020 return 0;
3021 }
3022
3023 /* If user sets rate 0 assigning default vport for its QPs */
3024 vf_admin->qos_vport = max_tx_rate ? slave : MLX4_VPP_DEFAULT_VPORT;
3025
3026 if (priv->mfunc.master.slave_state[slave].active &&
3027 dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)
3028 mlx4_master_immediate_activate_vlan_qos(priv, slave, port);
3029
3030 return 0;
3031}
3032EXPORT_SYMBOL_GPL(mlx4_set_vf_rate);
3033
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +02003034 /* mlx4_get_slave_default_vlan -
3035 * return true if VST ( default vlan)
3036 * if VST, will return vlan & qos (if not NULL)
3037 */
3038bool mlx4_get_slave_default_vlan(struct mlx4_dev *dev, int port, int slave,
3039 u16 *vlan, u8 *qos)
3040{
3041 struct mlx4_vport_oper_state *vp_oper;
3042 struct mlx4_priv *priv;
3043
3044 priv = mlx4_priv(dev);
Matan Baraka91c7722014-09-10 16:41:53 +03003045 port = mlx4_slaves_closest_port(dev, slave, port);
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +02003046 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
3047
3048 if (MLX4_VGT != vp_oper->state.default_vlan) {
3049 if (vlan)
3050 *vlan = vp_oper->state.default_vlan;
3051 if (qos)
3052 *qos = vp_oper->state.default_qos;
3053 return true;
3054 }
3055 return false;
3056}
3057EXPORT_SYMBOL_GPL(mlx4_get_slave_default_vlan);
3058
Rony Efraime6b6a232013-04-25 05:22:29 +00003059int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
3060{
3061 struct mlx4_priv *priv = mlx4_priv(dev);
3062 struct mlx4_vport_state *s_info;
3063 int slave;
3064
3065 if ((!mlx4_is_master(dev)) ||
3066 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
3067 return -EPROTONOSUPPORT;
3068
3069 slave = mlx4_get_slave_indx(dev, vf);
3070 if (slave < 0)
3071 return -EINVAL;
3072
Matan Baraka91c7722014-09-10 16:41:53 +03003073 port = mlx4_slaves_closest_port(dev, slave, port);
Rony Efraime6b6a232013-04-25 05:22:29 +00003074 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
3075 s_info->spoofchk = setting;
3076
3077 return 0;
3078}
3079EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
Rony Efraim2cccb9e2013-04-25 05:22:30 +00003080
3081int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
3082{
3083 struct mlx4_priv *priv = mlx4_priv(dev);
3084 struct mlx4_vport_state *s_info;
3085 int slave;
3086
3087 if (!mlx4_is_master(dev))
3088 return -EPROTONOSUPPORT;
3089
3090 slave = mlx4_get_slave_indx(dev, vf);
3091 if (slave < 0)
3092 return -EINVAL;
3093
3094 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
3095 ivf->vf = vf;
3096
3097 /* need to convert it to a func */
3098 ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
3099 ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
3100 ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
3101 ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
3102 ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
3103 ivf->mac[5] = ((s_info->mac) & 0xff);
3104
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04003105 ivf->vlan = s_info->default_vlan;
3106 ivf->qos = s_info->default_qos;
Ido Shamaycda373f2015-04-02 16:31:16 +03003107
3108 if (mlx4_is_vf_vst_and_prio_qos(dev, port, s_info))
3109 ivf->max_tx_rate = s_info->tx_rate;
3110 else
3111 ivf->max_tx_rate = 0;
3112
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04003113 ivf->min_tx_rate = 0;
3114 ivf->spoofchk = s_info->spoofchk;
3115 ivf->linkstate = s_info->link_state;
Rony Efraim2cccb9e2013-04-25 05:22:30 +00003116
3117 return 0;
3118}
3119EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
Rony Efraim948e3062013-06-13 13:19:11 +03003120
3121int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
3122{
3123 struct mlx4_priv *priv = mlx4_priv(dev);
3124 struct mlx4_vport_state *s_info;
Rony Efraim948e3062013-06-13 13:19:11 +03003125 int slave;
3126 u8 link_stat_event;
3127
3128 slave = mlx4_get_slave_indx(dev, vf);
3129 if (slave < 0)
3130 return -EINVAL;
3131
Matan Baraka91c7722014-09-10 16:41:53 +03003132 port = mlx4_slaves_closest_port(dev, slave, port);
Rony Efraim948e3062013-06-13 13:19:11 +03003133 switch (link_state) {
3134 case IFLA_VF_LINK_STATE_AUTO:
3135 /* get current link state */
3136 if (!priv->sense.do_sense_port[port])
3137 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
3138 else
3139 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
3140 break;
3141
3142 case IFLA_VF_LINK_STATE_ENABLE:
3143 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
3144 break;
3145
3146 case IFLA_VF_LINK_STATE_DISABLE:
3147 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
3148 break;
3149
3150 default:
3151 mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
3152 link_state, slave, port);
3153 return -EINVAL;
3154 };
Rony Efraim948e3062013-06-13 13:19:11 +03003155 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
Rony Efraim948e3062013-06-13 13:19:11 +03003156 s_info->link_state = link_state;
Rony Efraim948e3062013-06-13 13:19:11 +03003157
3158 /* send event */
3159 mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
Rony Efraim0a6eac22013-06-27 19:05:22 +03003160
3161 if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
3162 mlx4_dbg(dev,
3163 "updating vf %d port %d no link state HW enforcment\n",
3164 vf, port);
Rony Efraim948e3062013-06-13 13:19:11 +03003165 return 0;
3166}
3167EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);
Jack Morgenstein97982f52014-05-29 16:31:02 +03003168
3169int mlx4_vf_smi_enabled(struct mlx4_dev *dev, int slave, int port)
3170{
Jack Morgenstein99ec41d2014-05-29 16:31:03 +03003171 struct mlx4_priv *priv = mlx4_priv(dev);
3172
3173 if (slave < 1 || slave >= dev->num_slaves ||
3174 port < 1 || port > MLX4_MAX_PORTS)
3175 return 0;
3176
3177 return priv->mfunc.master.vf_oper[slave].smi_enabled[port] ==
3178 MLX4_VF_SMI_ENABLED;
Jack Morgenstein97982f52014-05-29 16:31:02 +03003179}
3180EXPORT_SYMBOL_GPL(mlx4_vf_smi_enabled);
Jack Morgenstein65fed8a2014-05-29 16:31:04 +03003181
3182int mlx4_vf_get_enable_smi_admin(struct mlx4_dev *dev, int slave, int port)
3183{
3184 struct mlx4_priv *priv = mlx4_priv(dev);
3185
3186 if (slave == mlx4_master_func_num(dev))
3187 return 1;
3188
3189 if (slave < 1 || slave >= dev->num_slaves ||
3190 port < 1 || port > MLX4_MAX_PORTS)
3191 return 0;
3192
3193 return priv->mfunc.master.vf_admin[slave].enable_smi[port] ==
3194 MLX4_VF_SMI_ENABLED;
3195}
3196EXPORT_SYMBOL_GPL(mlx4_vf_get_enable_smi_admin);
3197
3198int mlx4_vf_set_enable_smi_admin(struct mlx4_dev *dev, int slave, int port,
3199 int enabled)
3200{
3201 struct mlx4_priv *priv = mlx4_priv(dev);
Or Gerlitzbe9b9ec2015-05-21 15:14:10 +03003202 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
3203 &priv->dev, slave);
3204 int min_port = find_first_bit(actv_ports.ports,
3205 priv->dev.caps.num_ports) + 1;
3206 int max_port = min_port - 1 +
3207 bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
Jack Morgenstein65fed8a2014-05-29 16:31:04 +03003208
3209 if (slave == mlx4_master_func_num(dev))
3210 return 0;
3211
3212 if (slave < 1 || slave >= dev->num_slaves ||
3213 port < 1 || port > MLX4_MAX_PORTS ||
3214 enabled < 0 || enabled > 1)
3215 return -EINVAL;
3216
Or Gerlitzbe9b9ec2015-05-21 15:14:10 +03003217 if (min_port == max_port && dev->caps.num_ports > 1) {
3218 mlx4_info(dev, "SMI access disallowed for single ported VFs\n");
3219 return -EPROTONOSUPPORT;
3220 }
3221
Jack Morgenstein65fed8a2014-05-29 16:31:04 +03003222 priv->mfunc.master.vf_admin[slave].enable_smi[port] = enabled;
3223 return 0;
3224}
3225EXPORT_SYMBOL_GPL(mlx4_vf_set_enable_smi_admin);