blob: 06993ea9e6ba57a8f71af3d5470186129edb91bc [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);
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200717 err = -EIO;
718 goto out_reset;
Roland Dreier225c7b12007-05-08 18:00:38 -0700719 }
720
721 err = context->result;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000722 if (err) {
Jack Morgenstein1daa4302014-09-30 12:03:50 +0300723 /* Since we do not want to have this error message always
724 * displayed at driver start when there are ConnectX2 HCAs
725 * on the host, we deprecate the error message for this
726 * specific command/input_mod/opcode_mod/fw-status to be debug.
727 */
728 if (op == MLX4_CMD_SET_PORT && in_modifier == 1 &&
Ido Shamaya130b592015-04-02 16:31:19 +0300729 op_modifier == MLX4_SET_PORT_IB_OPCODE &&
730 context->fw_status == CMD_STAT_BAD_SIZE)
Jack Morgenstein1daa4302014-09-30 12:03:50 +0300731 mlx4_dbg(dev, "command 0x%x failed: fw status = 0x%x\n",
732 op, context->fw_status);
733 else
734 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
735 op, context->fw_status);
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200736 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
737 err = mlx4_internal_err_ret_value(dev, op, op_modifier);
738 else if (mlx4_closing_cmd_fatal_error(op, context->fw_status))
739 goto out_reset;
740
Roland Dreier225c7b12007-05-08 18:00:38 -0700741 goto out;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000742 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700743
744 if (out_is_imm)
745 *out_param = context->out_param;
746
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200747out_reset:
748 if (err)
749 err = mlx4_cmd_reset_flow(dev, op, op_modifier, err);
Roland Dreier225c7b12007-05-08 18:00:38 -0700750out:
751 spin_lock(&cmd->context_lock);
752 context->next = cmd->free_head;
753 cmd->free_head = context - cmd->context;
754 spin_unlock(&cmd->context_lock);
755
756 up(&cmd->event_sem);
757 return err;
758}
759
760int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
761 int out_is_imm, u32 in_modifier, u8 op_modifier,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000762 u16 op, unsigned long timeout, int native)
Roland Dreier225c7b12007-05-08 18:00:38 -0700763{
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200764 if (pci_channel_offline(dev->persist->pdev))
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200765 return mlx4_cmd_reset_flow(dev, op, op_modifier, -EIO);
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000766
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000767 if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200768 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
769 return mlx4_internal_err_ret_value(dev, op,
770 op_modifier);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000771 if (mlx4_priv(dev)->cmd.use_events)
772 return mlx4_cmd_wait(dev, in_param, out_param,
773 out_is_imm, in_modifier,
774 op_modifier, op, timeout);
775 else
776 return mlx4_cmd_poll(dev, in_param, out_param,
777 out_is_imm, in_modifier,
778 op_modifier, op, timeout);
779 }
780 return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
781 in_modifier, op_modifier, op, timeout);
Roland Dreier225c7b12007-05-08 18:00:38 -0700782}
783EXPORT_SYMBOL_GPL(__mlx4_cmd);
784
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000785
Yishai Hadas55ad3592015-01-25 16:59:42 +0200786int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000787{
788 return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
789 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
790}
791
792static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
793 int slave, u64 slave_addr,
794 int size, int is_read)
795{
796 u64 in_param;
797 u64 out_param;
798
799 if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
800 (slave & ~0x7f) | (size & 0xff)) {
Joe Perches1a91de22014-05-07 12:52:57 -0700801 mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx master_addr:0x%llx slave_id:%d size:%d\n",
802 slave_addr, master_addr, slave, size);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000803 return -EINVAL;
804 }
805
806 if (is_read) {
807 in_param = (u64) slave | slave_addr;
808 out_param = (u64) dev->caps.function | master_addr;
809 } else {
810 in_param = (u64) dev->caps.function | master_addr;
811 out_param = (u64) slave | slave_addr;
812 }
813
814 return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
815 MLX4_CMD_ACCESS_MEM,
816 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
817}
818
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000819static int query_pkey_block(struct mlx4_dev *dev, u8 port, u16 index, u16 *pkey,
820 struct mlx4_cmd_mailbox *inbox,
821 struct mlx4_cmd_mailbox *outbox)
822{
823 struct ib_smp *in_mad = (struct ib_smp *)(inbox->buf);
824 struct ib_smp *out_mad = (struct ib_smp *)(outbox->buf);
825 int err;
826 int i;
827
828 if (index & 0x1f)
829 return -EINVAL;
830
831 in_mad->attr_mod = cpu_to_be32(index / 32);
832
833 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
834 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
835 MLX4_CMD_NATIVE);
836 if (err)
837 return err;
838
839 for (i = 0; i < 32; ++i)
840 pkey[i] = be16_to_cpu(((__be16 *) out_mad->data)[i]);
841
842 return err;
843}
844
845static int get_full_pkey_table(struct mlx4_dev *dev, u8 port, u16 *table,
846 struct mlx4_cmd_mailbox *inbox,
847 struct mlx4_cmd_mailbox *outbox)
848{
849 int i;
850 int err;
851
852 for (i = 0; i < dev->caps.pkey_table_len[port]; i += 32) {
853 err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
854 if (err)
855 return err;
856 }
857
858 return 0;
859}
860#define PORT_CAPABILITY_LOCATION_IN_SMP 20
861#define PORT_STATE_OFFSET 32
862
863static enum ib_port_state vf_port_state(struct mlx4_dev *dev, int port, int vf)
864{
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000865 if (mlx4_get_slave_port_state(dev, vf, port) == SLAVE_PORT_UP)
866 return IB_PORT_ACTIVE;
867 else
868 return IB_PORT_DOWN;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000869}
870
871static int mlx4_MAD_IFC_wrapper(struct mlx4_dev *dev, int slave,
872 struct mlx4_vhcr *vhcr,
873 struct mlx4_cmd_mailbox *inbox,
874 struct mlx4_cmd_mailbox *outbox,
875 struct mlx4_cmd_info *cmd)
876{
877 struct ib_smp *smp = inbox->buf;
878 u32 index;
879 u8 port;
Jack Morgenstein97982f52014-05-29 16:31:02 +0300880 u8 opcode_modifier;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000881 u16 *table;
882 int err;
883 int vidx, pidx;
Jack Morgenstein97982f52014-05-29 16:31:02 +0300884 int network_view;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000885 struct mlx4_priv *priv = mlx4_priv(dev);
886 struct ib_smp *outsmp = outbox->buf;
887 __be16 *outtab = (__be16 *)(outsmp->data);
888 __be32 slave_cap_mask;
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000889 __be64 slave_node_guid;
Jack Morgenstein97982f52014-05-29 16:31:02 +0300890
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000891 port = vhcr->in_modifier;
892
Jack Morgenstein97982f52014-05-29 16:31:02 +0300893 /* network-view bit is for driver use only, and should not be passed to FW */
894 opcode_modifier = vhcr->op_modifier & ~0x8; /* clear netw view bit */
895 network_view = !!(vhcr->op_modifier & 0x8);
896
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000897 if (smp->base_version == 1 &&
898 smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
899 smp->class_version == 1) {
Jack Morgenstein97982f52014-05-29 16:31:02 +0300900 /* host view is paravirtualized */
901 if (!network_view && smp->method == IB_MGMT_METHOD_GET) {
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000902 if (smp->attr_id == IB_SMP_ATTR_PKEY_TABLE) {
903 index = be32_to_cpu(smp->attr_mod);
904 if (port < 1 || port > dev->caps.num_ports)
905 return -EINVAL;
Matan Barak19ab5742015-01-27 15:58:07 +0200906 table = kcalloc((dev->caps.pkey_table_len[port] / 32) + 1,
907 sizeof(*table) * 32, GFP_KERNEL);
908
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000909 if (!table)
910 return -ENOMEM;
911 /* need to get the full pkey table because the paravirtualized
912 * pkeys may be scattered among several pkey blocks.
913 */
914 err = get_full_pkey_table(dev, port, table, inbox, outbox);
915 if (!err) {
916 for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
917 pidx = priv->virt2phys_pkey[slave][port - 1][vidx];
918 outtab[vidx % 32] = cpu_to_be16(table[pidx]);
919 }
920 }
921 kfree(table);
922 return err;
923 }
924 if (smp->attr_id == IB_SMP_ATTR_PORT_INFO) {
925 /*get the slave specific caps:*/
926 /*do the command */
927 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Jack Morgenstein97982f52014-05-29 16:31:02 +0300928 vhcr->in_modifier, opcode_modifier,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000929 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
930 /* modify the response for slaves */
931 if (!err && slave != mlx4_master_func_num(dev)) {
932 u8 *state = outsmp->data + PORT_STATE_OFFSET;
933
934 *state = (*state & 0xf0) | vf_port_state(dev, port, slave);
935 slave_cap_mask = priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
936 memcpy(outsmp->data + PORT_CAPABILITY_LOCATION_IN_SMP, &slave_cap_mask, 4);
937 }
938 return err;
939 }
940 if (smp->attr_id == IB_SMP_ATTR_GUID_INFO) {
941 /* compute slave's gid block */
942 smp->attr_mod = cpu_to_be32(slave / 8);
943 /* execute cmd */
944 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Jack Morgenstein97982f52014-05-29 16:31:02 +0300945 vhcr->in_modifier, opcode_modifier,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000946 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
947 if (!err) {
948 /* if needed, move slave gid to index 0 */
949 if (slave % 8)
950 memcpy(outsmp->data,
951 outsmp->data + (slave % 8) * 8, 8);
952 /* delete all other gids */
953 memset(outsmp->data + 8, 0, 56);
954 }
955 return err;
956 }
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000957 if (smp->attr_id == IB_SMP_ATTR_NODE_INFO) {
958 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Jack Morgenstein97982f52014-05-29 16:31:02 +0300959 vhcr->in_modifier, opcode_modifier,
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000960 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
961 if (!err) {
962 slave_node_guid = mlx4_get_slave_node_guid(dev, slave);
963 memcpy(outsmp->data + 12, &slave_node_guid, 8);
964 }
965 return err;
966 }
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000967 }
968 }
Jack Morgenstein97982f52014-05-29 16:31:02 +0300969
970 /* Non-privileged VFs are only allowed "host" view LID-routed 'Get' MADs.
971 * These are the MADs used by ib verbs (such as ib_query_gids).
972 */
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000973 if (slave != mlx4_master_func_num(dev) &&
Jack Morgenstein97982f52014-05-29 16:31:02 +0300974 !mlx4_vf_smi_enabled(dev, slave, port)) {
975 if (!(smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
976 smp->method == IB_MGMT_METHOD_GET) || network_view) {
977 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",
978 slave, smp->method, smp->mgmt_class,
979 network_view ? "Network" : "Host",
980 be16_to_cpu(smp->attr_id));
981 return -EPERM;
982 }
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000983 }
Jack Morgenstein97982f52014-05-29 16:31:02 +0300984
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000985 return mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Jack Morgenstein97982f52014-05-29 16:31:02 +0300986 vhcr->in_modifier, opcode_modifier,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000987 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
988}
989
Or Gerlitzb7475792014-03-27 14:02:02 +0200990static int mlx4_CMD_EPERM_wrapper(struct mlx4_dev *dev, int slave,
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +0300991 struct mlx4_vhcr *vhcr,
992 struct mlx4_cmd_mailbox *inbox,
993 struct mlx4_cmd_mailbox *outbox,
994 struct mlx4_cmd_info *cmd)
995{
996 return -EPERM;
997}
998
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000999int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
1000 struct mlx4_vhcr *vhcr,
1001 struct mlx4_cmd_mailbox *inbox,
1002 struct mlx4_cmd_mailbox *outbox,
1003 struct mlx4_cmd_info *cmd)
1004{
1005 u64 in_param;
1006 u64 out_param;
1007 int err;
1008
1009 in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
1010 out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
1011 if (cmd->encode_slave_id) {
1012 in_param &= 0xffffffffffffff00ll;
1013 in_param |= slave;
1014 }
1015
1016 err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
1017 vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
1018 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
1019
1020 if (cmd->out_is_imm)
1021 vhcr->out_param = out_param;
1022
1023 return err;
1024}
1025
1026static struct mlx4_cmd_info cmd_info[] = {
1027 {
1028 .opcode = MLX4_CMD_QUERY_FW,
1029 .has_inbox = false,
1030 .has_outbox = true,
1031 .out_is_imm = false,
1032 .encode_slave_id = false,
1033 .verify = NULL,
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +00001034 .wrapper = mlx4_QUERY_FW_wrapper
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001035 },
1036 {
1037 .opcode = MLX4_CMD_QUERY_HCA,
1038 .has_inbox = false,
1039 .has_outbox = true,
1040 .out_is_imm = false,
1041 .encode_slave_id = false,
1042 .verify = NULL,
1043 .wrapper = NULL
1044 },
1045 {
1046 .opcode = MLX4_CMD_QUERY_DEV_CAP,
1047 .has_inbox = false,
1048 .has_outbox = true,
1049 .out_is_imm = false,
1050 .encode_slave_id = false,
1051 .verify = NULL,
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +00001052 .wrapper = mlx4_QUERY_DEV_CAP_wrapper
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001053 },
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001054 {
1055 .opcode = MLX4_CMD_QUERY_FUNC_CAP,
1056 .has_inbox = false,
1057 .has_outbox = true,
1058 .out_is_imm = false,
1059 .encode_slave_id = false,
1060 .verify = NULL,
1061 .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
1062 },
1063 {
1064 .opcode = MLX4_CMD_QUERY_ADAPTER,
1065 .has_inbox = false,
1066 .has_outbox = true,
1067 .out_is_imm = false,
1068 .encode_slave_id = false,
1069 .verify = NULL,
1070 .wrapper = NULL
1071 },
1072 {
1073 .opcode = MLX4_CMD_INIT_PORT,
1074 .has_inbox = false,
1075 .has_outbox = false,
1076 .out_is_imm = false,
1077 .encode_slave_id = false,
1078 .verify = NULL,
1079 .wrapper = mlx4_INIT_PORT_wrapper
1080 },
1081 {
1082 .opcode = MLX4_CMD_CLOSE_PORT,
1083 .has_inbox = false,
1084 .has_outbox = false,
1085 .out_is_imm = false,
1086 .encode_slave_id = false,
1087 .verify = NULL,
1088 .wrapper = mlx4_CLOSE_PORT_wrapper
1089 },
1090 {
1091 .opcode = MLX4_CMD_QUERY_PORT,
1092 .has_inbox = false,
1093 .has_outbox = true,
1094 .out_is_imm = false,
1095 .encode_slave_id = false,
1096 .verify = NULL,
1097 .wrapper = mlx4_QUERY_PORT_wrapper
1098 },
1099 {
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001100 .opcode = MLX4_CMD_SET_PORT,
1101 .has_inbox = true,
1102 .has_outbox = false,
1103 .out_is_imm = false,
1104 .encode_slave_id = false,
1105 .verify = NULL,
1106 .wrapper = mlx4_SET_PORT_wrapper
1107 },
1108 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001109 .opcode = MLX4_CMD_MAP_EQ,
1110 .has_inbox = false,
1111 .has_outbox = false,
1112 .out_is_imm = false,
1113 .encode_slave_id = false,
1114 .verify = NULL,
1115 .wrapper = mlx4_MAP_EQ_wrapper
1116 },
1117 {
1118 .opcode = MLX4_CMD_SW2HW_EQ,
1119 .has_inbox = true,
1120 .has_outbox = false,
1121 .out_is_imm = false,
1122 .encode_slave_id = true,
1123 .verify = NULL,
1124 .wrapper = mlx4_SW2HW_EQ_wrapper
1125 },
1126 {
1127 .opcode = MLX4_CMD_HW_HEALTH_CHECK,
1128 .has_inbox = false,
1129 .has_outbox = false,
1130 .out_is_imm = false,
1131 .encode_slave_id = false,
1132 .verify = NULL,
1133 .wrapper = NULL
1134 },
1135 {
1136 .opcode = MLX4_CMD_NOP,
1137 .has_inbox = false,
1138 .has_outbox = false,
1139 .out_is_imm = false,
1140 .encode_slave_id = false,
1141 .verify = NULL,
1142 .wrapper = NULL
1143 },
1144 {
Or Gerlitzd18f1412014-03-27 14:02:03 +02001145 .opcode = MLX4_CMD_CONFIG_DEV,
1146 .has_inbox = false,
Matan Barakd475c952014-11-02 16:26:17 +02001147 .has_outbox = true,
Or Gerlitzd18f1412014-03-27 14:02:03 +02001148 .out_is_imm = false,
1149 .encode_slave_id = false,
1150 .verify = NULL,
Matan Barakd475c952014-11-02 16:26:17 +02001151 .wrapper = mlx4_CONFIG_DEV_wrapper
Or Gerlitzd18f1412014-03-27 14:02:03 +02001152 },
1153 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001154 .opcode = MLX4_CMD_ALLOC_RES,
1155 .has_inbox = false,
1156 .has_outbox = false,
1157 .out_is_imm = true,
1158 .encode_slave_id = false,
1159 .verify = NULL,
1160 .wrapper = mlx4_ALLOC_RES_wrapper
1161 },
1162 {
1163 .opcode = MLX4_CMD_FREE_RES,
1164 .has_inbox = false,
1165 .has_outbox = false,
1166 .out_is_imm = false,
1167 .encode_slave_id = false,
1168 .verify = NULL,
1169 .wrapper = mlx4_FREE_RES_wrapper
1170 },
1171 {
1172 .opcode = MLX4_CMD_SW2HW_MPT,
1173 .has_inbox = true,
1174 .has_outbox = false,
1175 .out_is_imm = false,
1176 .encode_slave_id = true,
1177 .verify = NULL,
1178 .wrapper = mlx4_SW2HW_MPT_wrapper
1179 },
1180 {
1181 .opcode = MLX4_CMD_QUERY_MPT,
1182 .has_inbox = false,
1183 .has_outbox = true,
1184 .out_is_imm = false,
1185 .encode_slave_id = false,
1186 .verify = NULL,
1187 .wrapper = mlx4_QUERY_MPT_wrapper
1188 },
1189 {
1190 .opcode = MLX4_CMD_HW2SW_MPT,
1191 .has_inbox = false,
1192 .has_outbox = false,
1193 .out_is_imm = false,
1194 .encode_slave_id = false,
1195 .verify = NULL,
1196 .wrapper = mlx4_HW2SW_MPT_wrapper
1197 },
1198 {
1199 .opcode = MLX4_CMD_READ_MTT,
1200 .has_inbox = false,
1201 .has_outbox = true,
1202 .out_is_imm = false,
1203 .encode_slave_id = false,
1204 .verify = NULL,
1205 .wrapper = NULL
1206 },
1207 {
1208 .opcode = MLX4_CMD_WRITE_MTT,
1209 .has_inbox = true,
1210 .has_outbox = false,
1211 .out_is_imm = false,
1212 .encode_slave_id = false,
1213 .verify = NULL,
1214 .wrapper = mlx4_WRITE_MTT_wrapper
1215 },
1216 {
1217 .opcode = MLX4_CMD_SYNC_TPT,
1218 .has_inbox = true,
1219 .has_outbox = false,
1220 .out_is_imm = false,
1221 .encode_slave_id = false,
1222 .verify = NULL,
1223 .wrapper = NULL
1224 },
1225 {
1226 .opcode = MLX4_CMD_HW2SW_EQ,
1227 .has_inbox = false,
Jack Morgenstein30a5da52015-01-27 15:58:03 +02001228 .has_outbox = false,
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001229 .out_is_imm = false,
1230 .encode_slave_id = true,
1231 .verify = NULL,
1232 .wrapper = mlx4_HW2SW_EQ_wrapper
1233 },
1234 {
1235 .opcode = MLX4_CMD_QUERY_EQ,
1236 .has_inbox = false,
1237 .has_outbox = true,
1238 .out_is_imm = false,
1239 .encode_slave_id = true,
1240 .verify = NULL,
1241 .wrapper = mlx4_QUERY_EQ_wrapper
1242 },
1243 {
1244 .opcode = MLX4_CMD_SW2HW_CQ,
1245 .has_inbox = true,
1246 .has_outbox = false,
1247 .out_is_imm = false,
1248 .encode_slave_id = true,
1249 .verify = NULL,
1250 .wrapper = mlx4_SW2HW_CQ_wrapper
1251 },
1252 {
1253 .opcode = MLX4_CMD_HW2SW_CQ,
1254 .has_inbox = false,
1255 .has_outbox = false,
1256 .out_is_imm = false,
1257 .encode_slave_id = false,
1258 .verify = NULL,
1259 .wrapper = mlx4_HW2SW_CQ_wrapper
1260 },
1261 {
1262 .opcode = MLX4_CMD_QUERY_CQ,
1263 .has_inbox = false,
1264 .has_outbox = true,
1265 .out_is_imm = false,
1266 .encode_slave_id = false,
1267 .verify = NULL,
1268 .wrapper = mlx4_QUERY_CQ_wrapper
1269 },
1270 {
1271 .opcode = MLX4_CMD_MODIFY_CQ,
1272 .has_inbox = true,
1273 .has_outbox = false,
1274 .out_is_imm = true,
1275 .encode_slave_id = false,
1276 .verify = NULL,
1277 .wrapper = mlx4_MODIFY_CQ_wrapper
1278 },
1279 {
1280 .opcode = MLX4_CMD_SW2HW_SRQ,
1281 .has_inbox = true,
1282 .has_outbox = false,
1283 .out_is_imm = false,
1284 .encode_slave_id = true,
1285 .verify = NULL,
1286 .wrapper = mlx4_SW2HW_SRQ_wrapper
1287 },
1288 {
1289 .opcode = MLX4_CMD_HW2SW_SRQ,
1290 .has_inbox = false,
1291 .has_outbox = false,
1292 .out_is_imm = false,
1293 .encode_slave_id = false,
1294 .verify = NULL,
1295 .wrapper = mlx4_HW2SW_SRQ_wrapper
1296 },
1297 {
1298 .opcode = MLX4_CMD_QUERY_SRQ,
1299 .has_inbox = false,
1300 .has_outbox = true,
1301 .out_is_imm = false,
1302 .encode_slave_id = false,
1303 .verify = NULL,
1304 .wrapper = mlx4_QUERY_SRQ_wrapper
1305 },
1306 {
1307 .opcode = MLX4_CMD_ARM_SRQ,
1308 .has_inbox = false,
1309 .has_outbox = false,
1310 .out_is_imm = false,
1311 .encode_slave_id = false,
1312 .verify = NULL,
1313 .wrapper = mlx4_ARM_SRQ_wrapper
1314 },
1315 {
1316 .opcode = MLX4_CMD_RST2INIT_QP,
1317 .has_inbox = true,
1318 .has_outbox = false,
1319 .out_is_imm = false,
1320 .encode_slave_id = true,
1321 .verify = NULL,
1322 .wrapper = mlx4_RST2INIT_QP_wrapper
1323 },
1324 {
1325 .opcode = MLX4_CMD_INIT2INIT_QP,
1326 .has_inbox = true,
1327 .has_outbox = false,
1328 .out_is_imm = false,
1329 .encode_slave_id = false,
1330 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001331 .wrapper = mlx4_INIT2INIT_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001332 },
1333 {
1334 .opcode = MLX4_CMD_INIT2RTR_QP,
1335 .has_inbox = true,
1336 .has_outbox = false,
1337 .out_is_imm = false,
1338 .encode_slave_id = false,
1339 .verify = NULL,
1340 .wrapper = mlx4_INIT2RTR_QP_wrapper
1341 },
1342 {
1343 .opcode = MLX4_CMD_RTR2RTS_QP,
1344 .has_inbox = true,
1345 .has_outbox = false,
1346 .out_is_imm = false,
1347 .encode_slave_id = false,
1348 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001349 .wrapper = mlx4_RTR2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001350 },
1351 {
1352 .opcode = MLX4_CMD_RTS2RTS_QP,
1353 .has_inbox = true,
1354 .has_outbox = false,
1355 .out_is_imm = false,
1356 .encode_slave_id = false,
1357 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001358 .wrapper = mlx4_RTS2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001359 },
1360 {
1361 .opcode = MLX4_CMD_SQERR2RTS_QP,
1362 .has_inbox = true,
1363 .has_outbox = false,
1364 .out_is_imm = false,
1365 .encode_slave_id = false,
1366 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001367 .wrapper = mlx4_SQERR2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001368 },
1369 {
1370 .opcode = MLX4_CMD_2ERR_QP,
1371 .has_inbox = false,
1372 .has_outbox = false,
1373 .out_is_imm = false,
1374 .encode_slave_id = false,
1375 .verify = NULL,
1376 .wrapper = mlx4_GEN_QP_wrapper
1377 },
1378 {
1379 .opcode = MLX4_CMD_RTS2SQD_QP,
1380 .has_inbox = false,
1381 .has_outbox = false,
1382 .out_is_imm = false,
1383 .encode_slave_id = false,
1384 .verify = NULL,
1385 .wrapper = mlx4_GEN_QP_wrapper
1386 },
1387 {
1388 .opcode = MLX4_CMD_SQD2SQD_QP,
1389 .has_inbox = true,
1390 .has_outbox = false,
1391 .out_is_imm = false,
1392 .encode_slave_id = false,
1393 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001394 .wrapper = mlx4_SQD2SQD_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001395 },
1396 {
1397 .opcode = MLX4_CMD_SQD2RTS_QP,
1398 .has_inbox = true,
1399 .has_outbox = false,
1400 .out_is_imm = false,
1401 .encode_slave_id = false,
1402 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001403 .wrapper = mlx4_SQD2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001404 },
1405 {
1406 .opcode = MLX4_CMD_2RST_QP,
1407 .has_inbox = false,
1408 .has_outbox = false,
1409 .out_is_imm = false,
1410 .encode_slave_id = false,
1411 .verify = NULL,
1412 .wrapper = mlx4_2RST_QP_wrapper
1413 },
1414 {
1415 .opcode = MLX4_CMD_QUERY_QP,
1416 .has_inbox = false,
1417 .has_outbox = true,
1418 .out_is_imm = false,
1419 .encode_slave_id = false,
1420 .verify = NULL,
1421 .wrapper = mlx4_GEN_QP_wrapper
1422 },
1423 {
1424 .opcode = MLX4_CMD_SUSPEND_QP,
1425 .has_inbox = false,
1426 .has_outbox = false,
1427 .out_is_imm = false,
1428 .encode_slave_id = false,
1429 .verify = NULL,
1430 .wrapper = mlx4_GEN_QP_wrapper
1431 },
1432 {
1433 .opcode = MLX4_CMD_UNSUSPEND_QP,
1434 .has_inbox = false,
1435 .has_outbox = false,
1436 .out_is_imm = false,
1437 .encode_slave_id = false,
1438 .verify = NULL,
1439 .wrapper = mlx4_GEN_QP_wrapper
1440 },
1441 {
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001442 .opcode = MLX4_CMD_UPDATE_QP,
Matan Barakce8d9e02014-05-15 15:29:27 +03001443 .has_inbox = true,
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001444 .has_outbox = false,
1445 .out_is_imm = false,
1446 .encode_slave_id = false,
1447 .verify = NULL,
Matan Barakce8d9e02014-05-15 15:29:27 +03001448 .wrapper = mlx4_UPDATE_QP_wrapper
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001449 },
1450 {
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001451 .opcode = MLX4_CMD_GET_OP_REQ,
1452 .has_inbox = false,
1453 .has_outbox = false,
1454 .out_is_imm = false,
1455 .encode_slave_id = false,
1456 .verify = NULL,
Or Gerlitzb7475792014-03-27 14:02:02 +02001457 .wrapper = mlx4_CMD_EPERM_wrapper,
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001458 },
1459 {
Ido Shamay7e95bb92015-04-02 16:31:11 +03001460 .opcode = MLX4_CMD_ALLOCATE_VPP,
1461 .has_inbox = false,
1462 .has_outbox = true,
1463 .out_is_imm = false,
1464 .encode_slave_id = false,
1465 .verify = NULL,
1466 .wrapper = mlx4_CMD_EPERM_wrapper,
1467 },
1468 {
Ido Shamay1c291462015-04-02 16:31:12 +03001469 .opcode = MLX4_CMD_SET_VPORT_QOS,
1470 .has_inbox = false,
1471 .has_outbox = true,
1472 .out_is_imm = false,
1473 .encode_slave_id = false,
1474 .verify = NULL,
1475 .wrapper = mlx4_CMD_EPERM_wrapper,
1476 },
1477 {
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001478 .opcode = MLX4_CMD_CONF_SPECIAL_QP,
1479 .has_inbox = false,
1480 .has_outbox = false,
1481 .out_is_imm = false,
1482 .encode_slave_id = false,
1483 .verify = NULL, /* XXX verify: only demux can do this */
1484 .wrapper = NULL
1485 },
1486 {
1487 .opcode = MLX4_CMD_MAD_IFC,
1488 .has_inbox = true,
1489 .has_outbox = true,
1490 .out_is_imm = false,
1491 .encode_slave_id = false,
1492 .verify = NULL,
1493 .wrapper = mlx4_MAD_IFC_wrapper
1494 },
1495 {
Jack Morgenstein114840c2014-06-01 11:53:50 +03001496 .opcode = MLX4_CMD_MAD_DEMUX,
1497 .has_inbox = false,
1498 .has_outbox = false,
1499 .out_is_imm = false,
1500 .encode_slave_id = false,
1501 .verify = NULL,
1502 .wrapper = mlx4_CMD_EPERM_wrapper
1503 },
1504 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001505 .opcode = MLX4_CMD_QUERY_IF_STAT,
1506 .has_inbox = false,
1507 .has_outbox = true,
1508 .out_is_imm = false,
1509 .encode_slave_id = false,
1510 .verify = NULL,
1511 .wrapper = mlx4_QUERY_IF_STAT_wrapper
1512 },
Saeed Mahameedadbc7ac2014-10-27 11:37:37 +02001513 {
1514 .opcode = MLX4_CMD_ACCESS_REG,
1515 .has_inbox = true,
1516 .has_outbox = true,
1517 .out_is_imm = false,
1518 .encode_slave_id = false,
1519 .verify = NULL,
Saeed Mahameed6e806692014-11-02 16:26:13 +02001520 .wrapper = mlx4_ACCESS_REG_wrapper,
Saeed Mahameedadbc7ac2014-10-27 11:37:37 +02001521 },
Shani Michaelid237baa2015-03-05 20:16:12 +02001522 {
1523 .opcode = MLX4_CMD_CONGESTION_CTRL_OPCODE,
1524 .has_inbox = false,
1525 .has_outbox = false,
1526 .out_is_imm = false,
1527 .encode_slave_id = false,
1528 .verify = NULL,
1529 .wrapper = mlx4_CMD_EPERM_wrapper,
1530 },
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001531 /* Native multicast commands are not available for guests */
1532 {
1533 .opcode = MLX4_CMD_QP_ATTACH,
1534 .has_inbox = true,
1535 .has_outbox = false,
1536 .out_is_imm = false,
1537 .encode_slave_id = false,
1538 .verify = NULL,
1539 .wrapper = mlx4_QP_ATTACH_wrapper
1540 },
1541 {
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +00001542 .opcode = MLX4_CMD_PROMISC,
1543 .has_inbox = false,
1544 .has_outbox = false,
1545 .out_is_imm = false,
1546 .encode_slave_id = false,
1547 .verify = NULL,
1548 .wrapper = mlx4_PROMISC_wrapper
1549 },
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001550 /* Ethernet specific commands */
1551 {
1552 .opcode = MLX4_CMD_SET_VLAN_FLTR,
1553 .has_inbox = true,
1554 .has_outbox = false,
1555 .out_is_imm = false,
1556 .encode_slave_id = false,
1557 .verify = NULL,
1558 .wrapper = mlx4_SET_VLAN_FLTR_wrapper
1559 },
1560 {
1561 .opcode = MLX4_CMD_SET_MCAST_FLTR,
1562 .has_inbox = false,
1563 .has_outbox = false,
1564 .out_is_imm = false,
1565 .encode_slave_id = false,
1566 .verify = NULL,
1567 .wrapper = mlx4_SET_MCAST_FLTR_wrapper
1568 },
1569 {
1570 .opcode = MLX4_CMD_DUMP_ETH_STATS,
1571 .has_inbox = false,
1572 .has_outbox = true,
1573 .out_is_imm = false,
1574 .encode_slave_id = false,
1575 .verify = NULL,
1576 .wrapper = mlx4_DUMP_ETH_STATS_wrapper
1577 },
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +00001578 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001579 .opcode = MLX4_CMD_INFORM_FLR_DONE,
1580 .has_inbox = false,
1581 .has_outbox = false,
1582 .out_is_imm = false,
1583 .encode_slave_id = false,
1584 .verify = NULL,
1585 .wrapper = NULL
1586 },
Hadar Hen Zion8fcfb4d2012-07-05 04:03:45 +00001587 /* flow steering commands */
1588 {
1589 .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
1590 .has_inbox = true,
1591 .has_outbox = false,
1592 .out_is_imm = true,
1593 .encode_slave_id = false,
1594 .verify = NULL,
1595 .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
1596 },
1597 {
1598 .opcode = MLX4_QP_FLOW_STEERING_DETACH,
1599 .has_inbox = false,
1600 .has_outbox = false,
1601 .out_is_imm = false,
1602 .encode_slave_id = false,
1603 .verify = NULL,
1604 .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
1605 },
Matan Barak4de65802013-11-07 15:25:14 +02001606 {
1607 .opcode = MLX4_FLOW_STEERING_IB_UC_QP_RANGE,
1608 .has_inbox = false,
1609 .has_outbox = false,
1610 .out_is_imm = false,
1611 .encode_slave_id = false,
1612 .verify = NULL,
Or Gerlitzb7475792014-03-27 14:02:02 +02001613 .wrapper = mlx4_CMD_EPERM_wrapper
Matan Barak4de65802013-11-07 15:25:14 +02001614 },
Moni Shoua59e14e32015-02-03 16:48:32 +02001615 {
1616 .opcode = MLX4_CMD_VIRT_PORT_MAP,
1617 .has_inbox = false,
1618 .has_outbox = false,
1619 .out_is_imm = false,
1620 .encode_slave_id = false,
1621 .verify = NULL,
1622 .wrapper = mlx4_CMD_EPERM_wrapper
1623 },
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001624};
1625
1626static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
1627 struct mlx4_vhcr_cmd *in_vhcr)
1628{
1629 struct mlx4_priv *priv = mlx4_priv(dev);
1630 struct mlx4_cmd_info *cmd = NULL;
1631 struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
1632 struct mlx4_vhcr *vhcr;
1633 struct mlx4_cmd_mailbox *inbox = NULL;
1634 struct mlx4_cmd_mailbox *outbox = NULL;
1635 u64 in_param;
1636 u64 out_param;
1637 int ret = 0;
1638 int i;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001639 int err = 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001640
1641 /* Create sw representation of Virtual HCR */
1642 vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
1643 if (!vhcr)
1644 return -ENOMEM;
1645
1646 /* DMA in the vHCR */
1647 if (!in_vhcr) {
1648 ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1649 priv->mfunc.master.slave_state[slave].vhcr_dma,
1650 ALIGN(sizeof(struct mlx4_vhcr_cmd),
1651 MLX4_ACCESS_MEM_ALIGN), 1);
1652 if (ret) {
Yishai Hadas0cd93022015-01-25 16:59:43 +02001653 if (!(dev->persist->state &
1654 MLX4_DEVICE_STATE_INTERNAL_ERROR))
1655 mlx4_err(dev, "%s: Failed reading vhcr ret: 0x%x\n",
1656 __func__, ret);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001657 kfree(vhcr);
1658 return ret;
1659 }
1660 }
1661
1662 /* Fill SW VHCR fields */
1663 vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
1664 vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
1665 vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
1666 vhcr->token = be16_to_cpu(vhcr_cmd->token);
1667 vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
1668 vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
1669 vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
1670
1671 /* Lookup command */
1672 for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
1673 if (vhcr->op == cmd_info[i].opcode) {
1674 cmd = &cmd_info[i];
1675 break;
1676 }
1677 }
1678 if (!cmd) {
1679 mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
1680 vhcr->op, slave);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001681 vhcr_cmd->status = CMD_STAT_BAD_PARAM;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001682 goto out_status;
1683 }
1684
1685 /* Read inbox */
1686 if (cmd->has_inbox) {
1687 vhcr->in_param &= INBOX_MASK;
1688 inbox = mlx4_alloc_cmd_mailbox(dev);
1689 if (IS_ERR(inbox)) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001690 vhcr_cmd->status = CMD_STAT_BAD_SIZE;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001691 inbox = NULL;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001692 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001693 }
1694
Yishai Hadas0cd93022015-01-25 16:59:43 +02001695 ret = mlx4_ACCESS_MEM(dev, inbox->dma, slave,
1696 vhcr->in_param,
1697 MLX4_MAILBOX_SIZE, 1);
1698 if (ret) {
1699 if (!(dev->persist->state &
1700 MLX4_DEVICE_STATE_INTERNAL_ERROR))
1701 mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
1702 __func__, cmd->opcode);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001703 vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
1704 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001705 }
1706 }
1707
1708 /* Apply permission and bound checks if applicable */
1709 if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
Joe Perches1a91de22014-05-07 12:52:57 -07001710 mlx4_warn(dev, "Command:0x%x from slave: %d failed protection checks for resource_id:%d\n",
1711 vhcr->op, slave, vhcr->in_modifier);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001712 vhcr_cmd->status = CMD_STAT_BAD_OP;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001713 goto out_status;
1714 }
1715
1716 /* Allocate outbox */
1717 if (cmd->has_outbox) {
1718 outbox = mlx4_alloc_cmd_mailbox(dev);
1719 if (IS_ERR(outbox)) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001720 vhcr_cmd->status = CMD_STAT_BAD_SIZE;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001721 outbox = NULL;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001722 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001723 }
1724 }
1725
1726 /* Execute the command! */
1727 if (cmd->wrapper) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001728 err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
1729 cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001730 if (cmd->out_is_imm)
1731 vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1732 } else {
1733 in_param = cmd->has_inbox ? (u64) inbox->dma :
1734 vhcr->in_param;
1735 out_param = cmd->has_outbox ? (u64) outbox->dma :
1736 vhcr->out_param;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001737 err = __mlx4_cmd(dev, in_param, &out_param,
1738 cmd->out_is_imm, vhcr->in_modifier,
1739 vhcr->op_modifier, vhcr->op,
1740 MLX4_CMD_TIME_CLASS_A,
1741 MLX4_CMD_NATIVE);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001742
1743 if (cmd->out_is_imm) {
1744 vhcr->out_param = out_param;
1745 vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1746 }
1747 }
1748
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001749 if (err) {
Yishai Hadas0cd93022015-01-25 16:59:43 +02001750 if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR))
1751 mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with error:%d, status %d\n",
1752 vhcr->op, slave, vhcr->errno, err);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001753 vhcr_cmd->status = mlx4_errno_to_status(err);
1754 goto out_status;
1755 }
1756
1757
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001758 /* Write outbox if command completed successfully */
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001759 if (cmd->has_outbox && !vhcr_cmd->status) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001760 ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
1761 vhcr->out_param,
1762 MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
1763 if (ret) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001764 /* If we failed to write back the outbox after the
1765 *command was successfully executed, we must fail this
1766 * slave, as it is now in undefined state */
Yishai Hadas0cd93022015-01-25 16:59:43 +02001767 if (!(dev->persist->state &
1768 MLX4_DEVICE_STATE_INTERNAL_ERROR))
1769 mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001770 goto out;
1771 }
1772 }
1773
1774out_status:
1775 /* DMA back vhcr result */
1776 if (!in_vhcr) {
1777 ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1778 priv->mfunc.master.slave_state[slave].vhcr_dma,
1779 ALIGN(sizeof(struct mlx4_vhcr),
1780 MLX4_ACCESS_MEM_ALIGN),
1781 MLX4_CMD_WRAPPED);
1782 if (ret)
1783 mlx4_err(dev, "%s:Failed writing vhcr result\n",
1784 __func__);
1785 else if (vhcr->e_bit &&
1786 mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
Joe Perches1a91de22014-05-07 12:52:57 -07001787 mlx4_warn(dev, "Failed to generate command completion eqe for slave %d\n",
1788 slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001789 }
1790
1791out:
1792 kfree(vhcr);
1793 mlx4_free_cmd_mailbox(dev, inbox);
1794 mlx4_free_cmd_mailbox(dev, outbox);
1795 return ret;
1796}
1797
Jingoo Hanf0946682013-08-05 18:04:51 +09001798static int mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv *priv,
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001799 int slave, int port)
1800{
1801 struct mlx4_vport_oper_state *vp_oper;
1802 struct mlx4_vport_state *vp_admin;
1803 struct mlx4_vf_immed_vlan_work *work;
Rony Efraim0a6eac22013-06-27 19:05:22 +03001804 struct mlx4_dev *dev = &(priv->dev);
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001805 int err;
1806 int admin_vlan_ix = NO_INDX;
1807
1808 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1809 vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
1810
1811 if (vp_oper->state.default_vlan == vp_admin->default_vlan &&
Rony Efraim0a6eac22013-06-27 19:05:22 +03001812 vp_oper->state.default_qos == vp_admin->default_qos &&
Ido Shamay08068cd2015-04-02 16:31:15 +03001813 vp_oper->state.link_state == vp_admin->link_state &&
1814 vp_oper->state.qos_vport == vp_admin->qos_vport)
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001815 return 0;
1816
Rony Efraim0a6eac22013-06-27 19:05:22 +03001817 if (!(priv->mfunc.master.slave_state[slave].active &&
Rony Efraimf0f829b2013-11-07 12:19:51 +02001818 dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)) {
Rony Efraim0a6eac22013-06-27 19:05:22 +03001819 /* even if the UPDATE_QP command isn't supported, we still want
1820 * to set this VF link according to the admin directive
1821 */
1822 vp_oper->state.link_state = vp_admin->link_state;
1823 return -1;
1824 }
1825
1826 mlx4_dbg(dev, "updating immediately admin params slave %d port %d\n",
1827 slave, port);
Joe Perches1a91de22014-05-07 12:52:57 -07001828 mlx4_dbg(dev, "vlan %d QoS %d link down %d\n",
1829 vp_admin->default_vlan, vp_admin->default_qos,
1830 vp_admin->link_state);
Rony Efraim0a6eac22013-06-27 19:05:22 +03001831
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001832 work = kzalloc(sizeof(*work), GFP_KERNEL);
1833 if (!work)
1834 return -ENOMEM;
1835
1836 if (vp_oper->state.default_vlan != vp_admin->default_vlan) {
Rony Efraimf0f829b2013-11-07 12:19:51 +02001837 if (MLX4_VGT != vp_admin->default_vlan) {
1838 err = __mlx4_register_vlan(&priv->dev, port,
1839 vp_admin->default_vlan,
1840 &admin_vlan_ix);
1841 if (err) {
1842 kfree(work);
Joe Perches1a91de22014-05-07 12:52:57 -07001843 mlx4_warn(&priv->dev,
Rony Efraimf0f829b2013-11-07 12:19:51 +02001844 "No vlan resources slave %d, port %d\n",
1845 slave, port);
1846 return err;
1847 }
1848 } else {
1849 admin_vlan_ix = NO_INDX;
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001850 }
1851 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_VLAN;
Joe Perches1a91de22014-05-07 12:52:57 -07001852 mlx4_dbg(&priv->dev,
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001853 "alloc vlan %d idx %d slave %d port %d\n",
1854 (int)(vp_admin->default_vlan),
1855 admin_vlan_ix, slave, port);
1856 }
1857
1858 /* save original vlan ix and vlan id */
1859 work->orig_vlan_id = vp_oper->state.default_vlan;
1860 work->orig_vlan_ix = vp_oper->vlan_idx;
1861
1862 /* handle new qos */
1863 if (vp_oper->state.default_qos != vp_admin->default_qos)
1864 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_QOS;
1865
1866 if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN)
1867 vp_oper->vlan_idx = admin_vlan_ix;
1868
1869 vp_oper->state.default_vlan = vp_admin->default_vlan;
1870 vp_oper->state.default_qos = vp_admin->default_qos;
Rony Efraim0a6eac22013-06-27 19:05:22 +03001871 vp_oper->state.link_state = vp_admin->link_state;
Ido Shamay08068cd2015-04-02 16:31:15 +03001872 vp_oper->state.qos_vport = vp_admin->qos_vport;
Rony Efraim0a6eac22013-06-27 19:05:22 +03001873
1874 if (vp_admin->link_state == IFLA_VF_LINK_STATE_DISABLE)
1875 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE;
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001876
1877 /* iterate over QPs owned by this slave, using UPDATE_QP */
1878 work->port = port;
1879 work->slave = slave;
1880 work->qos = vp_oper->state.default_qos;
Ido Shamay08068cd2015-04-02 16:31:15 +03001881 work->qos_vport = vp_oper->state.qos_vport;
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001882 work->vlan_id = vp_oper->state.default_vlan;
1883 work->vlan_ix = vp_oper->vlan_idx;
1884 work->priv = priv;
1885 INIT_WORK(&work->work, mlx4_vf_immed_vlan_work_handler);
1886 queue_work(priv->mfunc.master.comm_wq, &work->work);
1887
1888 return 0;
1889}
1890
Ido Shamay666672d2015-04-02 16:31:14 +03001891static void mlx4_set_default_port_qos(struct mlx4_dev *dev, int port)
1892{
1893 struct mlx4_qos_manager *port_qos_ctl;
1894 struct mlx4_priv *priv = mlx4_priv(dev);
1895
1896 port_qos_ctl = &priv->mfunc.master.qos_ctl[port];
1897 bitmap_zero(port_qos_ctl->priority_bm, MLX4_NUM_UP);
1898
1899 /* Enable only default prio at PF init routine */
1900 set_bit(MLX4_DEFAULT_QOS_PRIO, port_qos_ctl->priority_bm);
1901}
1902
1903static void mlx4_allocate_port_vpps(struct mlx4_dev *dev, int port)
1904{
1905 int i;
1906 int err;
1907 int num_vfs;
1908 u16 availible_vpp;
1909 u8 vpp_param[MLX4_NUM_UP];
1910 struct mlx4_qos_manager *port_qos;
1911 struct mlx4_priv *priv = mlx4_priv(dev);
1912
1913 err = mlx4_ALLOCATE_VPP_get(dev, port, &availible_vpp, vpp_param);
1914 if (err) {
1915 mlx4_info(dev, "Failed query availible VPPs\n");
1916 return;
1917 }
1918
1919 port_qos = &priv->mfunc.master.qos_ctl[port];
1920 num_vfs = (availible_vpp /
1921 bitmap_weight(port_qos->priority_bm, MLX4_NUM_UP));
1922
1923 for (i = 0; i < MLX4_NUM_UP; i++) {
1924 if (test_bit(i, port_qos->priority_bm))
1925 vpp_param[i] = num_vfs;
1926 }
1927
1928 err = mlx4_ALLOCATE_VPP_set(dev, port, vpp_param);
1929 if (err) {
1930 mlx4_info(dev, "Failed allocating VPPs\n");
1931 return;
1932 }
1933
1934 /* Query actual allocated VPP, just to make sure */
1935 err = mlx4_ALLOCATE_VPP_get(dev, port, &availible_vpp, vpp_param);
1936 if (err) {
1937 mlx4_info(dev, "Failed query availible VPPs\n");
1938 return;
1939 }
1940
1941 port_qos->num_of_qos_vfs = num_vfs;
1942 mlx4_dbg(dev, "Port %d Availible VPPs %d\n", port, availible_vpp);
1943
1944 for (i = 0; i < MLX4_NUM_UP; i++)
1945 mlx4_dbg(dev, "Port %d UP %d Allocated %d VPPs\n", port, i,
1946 vpp_param[i]);
1947}
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001948
Rony Efraim0eb62b92013-04-25 05:22:26 +00001949static int mlx4_master_activate_admin_state(struct mlx4_priv *priv, int slave)
1950{
Rony Efraim3f7fb022013-04-25 05:22:28 +00001951 int port, err;
1952 struct mlx4_vport_state *vp_admin;
1953 struct mlx4_vport_oper_state *vp_oper;
Matan Barak449fc482014-03-19 18:11:52 +02001954 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
1955 &priv->dev, slave);
1956 int min_port = find_first_bit(actv_ports.ports,
1957 priv->dev.caps.num_ports) + 1;
1958 int max_port = min_port - 1 +
1959 bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
Rony Efraim3f7fb022013-04-25 05:22:28 +00001960
Matan Barak449fc482014-03-19 18:11:52 +02001961 for (port = min_port; port <= max_port; port++) {
1962 if (!test_bit(port - 1, actv_ports.ports))
1963 continue;
Jack Morgenstein99ec41d2014-05-29 16:31:03 +03001964 priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
1965 priv->mfunc.master.vf_admin[slave].enable_smi[port];
Rony Efraim3f7fb022013-04-25 05:22:28 +00001966 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1967 vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
1968 vp_oper->state = *vp_admin;
1969 if (MLX4_VGT != vp_admin->default_vlan) {
1970 err = __mlx4_register_vlan(&priv->dev, port,
1971 vp_admin->default_vlan, &(vp_oper->vlan_idx));
1972 if (err) {
1973 vp_oper->vlan_idx = NO_INDX;
Joe Perches1a91de22014-05-07 12:52:57 -07001974 mlx4_warn(&priv->dev,
Masanari Iida1a84db52014-08-29 23:37:33 +09001975 "No vlan resources slave %d, port %d\n",
Rony Efraim3f7fb022013-04-25 05:22:28 +00001976 slave, port);
1977 return err;
1978 }
Joe Perches1a91de22014-05-07 12:52:57 -07001979 mlx4_dbg(&priv->dev, "alloc vlan %d idx %d slave %d port %d\n",
Rony Efraim3f7fb022013-04-25 05:22:28 +00001980 (int)(vp_oper->state.default_vlan),
1981 vp_oper->vlan_idx, slave, port);
1982 }
Rony Efraime6b6a232013-04-25 05:22:29 +00001983 if (vp_admin->spoofchk) {
1984 vp_oper->mac_idx = __mlx4_register_mac(&priv->dev,
1985 port,
1986 vp_admin->mac);
1987 if (0 > vp_oper->mac_idx) {
1988 err = vp_oper->mac_idx;
1989 vp_oper->mac_idx = NO_INDX;
Joe Perches1a91de22014-05-07 12:52:57 -07001990 mlx4_warn(&priv->dev,
Masanari Iida1a84db52014-08-29 23:37:33 +09001991 "No mac resources slave %d, port %d\n",
Rony Efraime6b6a232013-04-25 05:22:29 +00001992 slave, port);
1993 return err;
1994 }
Joe Perches1a91de22014-05-07 12:52:57 -07001995 mlx4_dbg(&priv->dev, "alloc mac %llx idx %d slave %d port %d\n",
Rony Efraime6b6a232013-04-25 05:22:29 +00001996 vp_oper->state.mac, vp_oper->mac_idx, slave, port);
1997 }
Rony Efraim0eb62b92013-04-25 05:22:26 +00001998 }
1999 return 0;
2000}
2001
Rony Efraim3f7fb022013-04-25 05:22:28 +00002002static void mlx4_master_deactivate_admin_state(struct mlx4_priv *priv, int slave)
2003{
2004 int port;
2005 struct mlx4_vport_oper_state *vp_oper;
Matan Barak449fc482014-03-19 18:11:52 +02002006 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
2007 &priv->dev, slave);
2008 int min_port = find_first_bit(actv_ports.ports,
2009 priv->dev.caps.num_ports) + 1;
2010 int max_port = min_port - 1 +
2011 bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002012
Matan Barak449fc482014-03-19 18:11:52 +02002013
2014 for (port = min_port; port <= max_port; port++) {
2015 if (!test_bit(port - 1, actv_ports.ports))
2016 continue;
Jack Morgenstein99ec41d2014-05-29 16:31:03 +03002017 priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
2018 MLX4_VF_SMI_DISABLED;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002019 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
2020 if (NO_INDX != vp_oper->vlan_idx) {
2021 __mlx4_unregister_vlan(&priv->dev,
Jack Morgenstein2009d002013-11-03 10:03:19 +02002022 port, vp_oper->state.default_vlan);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002023 vp_oper->vlan_idx = NO_INDX;
2024 }
Rony Efraime6b6a232013-04-25 05:22:29 +00002025 if (NO_INDX != vp_oper->mac_idx) {
Jack Morgensteinc32b7df2013-11-03 10:04:07 +02002026 __mlx4_unregister_mac(&priv->dev, port, vp_oper->state.mac);
Rony Efraime6b6a232013-04-25 05:22:29 +00002027 vp_oper->mac_idx = NO_INDX;
2028 }
Rony Efraim3f7fb022013-04-25 05:22:28 +00002029 }
2030 return;
2031}
2032
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002033static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
2034 u16 param, u8 toggle)
2035{
2036 struct mlx4_priv *priv = mlx4_priv(dev);
2037 struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
2038 u32 reply;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002039 u8 is_going_down = 0;
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002040 int i;
Jack Morgenstein311f8132012-11-27 16:24:30 +00002041 unsigned long flags;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002042
2043 slave_state[slave].comm_toggle ^= 1;
2044 reply = (u32) slave_state[slave].comm_toggle << 31;
2045 if (toggle != slave_state[slave].comm_toggle) {
Joe Perches1a91de22014-05-07 12:52:57 -07002046 mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER STATE COMPROMISED ***\n",
2047 toggle, slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002048 goto reset_slave;
2049 }
2050 if (cmd == MLX4_COMM_CMD_RESET) {
2051 mlx4_warn(dev, "Received reset from slave:%d\n", slave);
2052 slave_state[slave].active = false;
Jack Morgenstein2c957ff2013-11-03 10:03:21 +02002053 slave_state[slave].old_vlan_api = false;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002054 mlx4_master_deactivate_admin_state(priv, slave);
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002055 for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
2056 slave_state[slave].event_eq[i].eqn = -1;
2057 slave_state[slave].event_eq[i].token = 0;
2058 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002059 /*check if we are in the middle of FLR process,
2060 if so return "retry" status to the slave*/
Or Gerlitz162344e2012-05-15 10:34:57 +00002061 if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002062 goto inform_slave_state;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002063
Jack Morgensteinfc065732012-08-03 08:40:42 +00002064 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
2065
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002066 /* write the version in the event field */
2067 reply |= mlx4_comm_get_version();
2068
2069 goto reset_slave;
2070 }
2071 /*command from slave in the middle of FLR*/
2072 if (cmd != MLX4_COMM_CMD_RESET &&
2073 MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
Joe Perches1a91de22014-05-07 12:52:57 -07002074 mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) in the middle of FLR\n",
2075 slave, cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002076 return;
2077 }
2078
2079 switch (cmd) {
2080 case MLX4_COMM_CMD_VHCR0:
2081 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
2082 goto reset_slave;
2083 slave_state[slave].vhcr_dma = ((u64) param) << 48;
2084 priv->mfunc.master.slave_state[slave].cookie = 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002085 break;
2086 case MLX4_COMM_CMD_VHCR1:
2087 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
2088 goto reset_slave;
2089 slave_state[slave].vhcr_dma |= ((u64) param) << 32;
2090 break;
2091 case MLX4_COMM_CMD_VHCR2:
2092 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
2093 goto reset_slave;
2094 slave_state[slave].vhcr_dma |= ((u64) param) << 16;
2095 break;
2096 case MLX4_COMM_CMD_VHCR_EN:
2097 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
2098 goto reset_slave;
2099 slave_state[slave].vhcr_dma |= param;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002100 if (mlx4_master_activate_admin_state(priv, slave))
2101 goto reset_slave;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002102 slave_state[slave].active = true;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002103 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002104 break;
2105 case MLX4_COMM_CMD_VHCR_POST:
2106 if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
Yishai Hadas55ad3592015-01-25 16:59:42 +02002107 (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST)) {
2108 mlx4_warn(dev, "slave:%d is out of sync, cmd=0x%x, last command=0x%x, reset is needed\n",
2109 slave, cmd, slave_state[slave].last_cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002110 goto reset_slave;
Yishai Hadas55ad3592015-01-25 16:59:42 +02002111 }
Roland Dreierf3d4c892012-09-25 21:24:07 -07002112
2113 mutex_lock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002114 if (mlx4_master_process_vhcr(dev, slave, NULL)) {
Joe Perches1a91de22014-05-07 12:52:57 -07002115 mlx4_err(dev, "Failed processing vhcr for slave:%d, resetting slave\n",
2116 slave);
Roland Dreierf3d4c892012-09-25 21:24:07 -07002117 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002118 goto reset_slave;
2119 }
Roland Dreierf3d4c892012-09-25 21:24:07 -07002120 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002121 break;
2122 default:
2123 mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
2124 goto reset_slave;
2125 }
Jack Morgenstein311f8132012-11-27 16:24:30 +00002126 spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002127 if (!slave_state[slave].is_slave_going_down)
2128 slave_state[slave].last_cmd = cmd;
2129 else
2130 is_going_down = 1;
Jack Morgenstein311f8132012-11-27 16:24:30 +00002131 spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002132 if (is_going_down) {
Joe Perches1a91de22014-05-07 12:52:57 -07002133 mlx4_warn(dev, "Slave is going down aborting command(%d) executing from slave:%d\n",
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002134 cmd, slave);
2135 return;
2136 }
2137 __raw_writel((__force u32) cpu_to_be32(reply),
2138 &priv->mfunc.comm[slave].slave_read);
2139 mmiowb();
2140
2141 return;
2142
2143reset_slave:
Eli Cohenc82e9aa2011-12-13 04:15:24 +00002144 /* cleanup any slave resources */
Yishai Hadas55ad3592015-01-25 16:59:42 +02002145 if (dev->persist->interface_state & MLX4_INTERFACE_STATE_UP)
2146 mlx4_delete_all_resources_for_slave(dev, slave);
2147
2148 if (cmd != MLX4_COMM_CMD_RESET) {
2149 mlx4_warn(dev, "Turn on internal error to force reset, slave=%d, cmd=0x%x\n",
2150 slave, cmd);
2151 /* Turn on internal error letting slave reset itself immeditaly,
2152 * otherwise it might take till timeout on command is passed
2153 */
2154 reply |= ((u32)COMM_CHAN_EVENT_INTERNAL_ERR);
2155 }
2156
Jack Morgenstein311f8132012-11-27 16:24:30 +00002157 spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002158 if (!slave_state[slave].is_slave_going_down)
2159 slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
Jack Morgenstein311f8132012-11-27 16:24:30 +00002160 spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002161 /*with slave in the middle of flr, no need to clean resources again.*/
2162inform_slave_state:
2163 memset(&slave_state[slave].event_eq, 0,
2164 sizeof(struct mlx4_slave_event_eq_info));
2165 __raw_writel((__force u32) cpu_to_be32(reply),
2166 &priv->mfunc.comm[slave].slave_read);
2167 wmb();
2168}
2169
2170/* master command processing */
2171void mlx4_master_comm_channel(struct work_struct *work)
2172{
2173 struct mlx4_mfunc_master_ctx *master =
2174 container_of(work,
2175 struct mlx4_mfunc_master_ctx,
2176 comm_work);
2177 struct mlx4_mfunc *mfunc =
2178 container_of(master, struct mlx4_mfunc, master);
2179 struct mlx4_priv *priv =
2180 container_of(mfunc, struct mlx4_priv, mfunc);
2181 struct mlx4_dev *dev = &priv->dev;
2182 __be32 *bit_vec;
2183 u32 comm_cmd;
2184 u32 vec;
2185 int i, j, slave;
2186 int toggle;
2187 int served = 0;
2188 int reported = 0;
2189 u32 slt;
2190
2191 bit_vec = master->comm_arm_bit_vector;
2192 for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
2193 vec = be32_to_cpu(bit_vec[i]);
2194 for (j = 0; j < 32; j++) {
2195 if (!(vec & (1 << j)))
2196 continue;
2197 ++reported;
2198 slave = (i * 32) + j;
2199 comm_cmd = swab32(readl(
2200 &mfunc->comm[slave].slave_write));
2201 slt = swab32(readl(&mfunc->comm[slave].slave_read))
2202 >> 31;
2203 toggle = comm_cmd >> 31;
2204 if (toggle != slt) {
2205 if (master->slave_state[slave].comm_toggle
2206 != slt) {
Amir Vadaic20862c2014-05-22 15:55:40 +03002207 pr_info("slave %d out of sync. read toggle %d, state toggle %d. Resynching.\n",
2208 slave, slt,
2209 master->slave_state[slave].comm_toggle);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002210 master->slave_state[slave].comm_toggle =
2211 slt;
2212 }
2213 mlx4_master_do_cmd(dev, slave,
2214 comm_cmd >> 16 & 0xff,
2215 comm_cmd & 0xffff, toggle);
2216 ++served;
2217 }
2218 }
2219 }
2220
2221 if (reported && reported != served)
Joe Perches1a91de22014-05-07 12:52:57 -07002222 mlx4_warn(dev, "Got command event with bitmask from %d slaves but %d were served\n",
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002223 reported, served);
2224
2225 if (mlx4_ARM_COMM_CHANNEL(dev))
2226 mlx4_warn(dev, "Failed to arm comm channel events\n");
2227}
2228
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002229static int sync_toggles(struct mlx4_dev *dev)
2230{
2231 struct mlx4_priv *priv = mlx4_priv(dev);
Yishai Hadas55ad3592015-01-25 16:59:42 +02002232 u32 wr_toggle;
2233 u32 rd_toggle;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002234 unsigned long end;
2235
Yishai Hadas55ad3592015-01-25 16:59:42 +02002236 wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write));
2237 if (wr_toggle == 0xffffffff)
2238 end = jiffies + msecs_to_jiffies(30000);
2239 else
2240 end = jiffies + msecs_to_jiffies(5000);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002241
2242 while (time_before(jiffies, end)) {
Yishai Hadas55ad3592015-01-25 16:59:42 +02002243 rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read));
2244 if (wr_toggle == 0xffffffff || rd_toggle == 0xffffffff) {
2245 /* PCI might be offline */
2246 msleep(100);
2247 wr_toggle = swab32(readl(&priv->mfunc.comm->
2248 slave_write));
2249 continue;
2250 }
2251
2252 if (rd_toggle >> 31 == wr_toggle >> 31) {
2253 priv->cmd.comm_toggle = rd_toggle >> 31;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002254 return 0;
2255 }
2256
2257 cond_resched();
2258 }
2259
2260 /*
2261 * we could reach here if for example the previous VM using this
2262 * function misbehaved and left the channel with unsynced state. We
2263 * should fix this here and give this VM a chance to use a properly
2264 * synced channel
2265 */
2266 mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
2267 __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
2268 __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
2269 priv->cmd.comm_toggle = 0;
2270
2271 return 0;
2272}
2273
2274int mlx4_multi_func_init(struct mlx4_dev *dev)
2275{
2276 struct mlx4_priv *priv = mlx4_priv(dev);
2277 struct mlx4_slave_state *s_state;
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002278 int i, j, err, port;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002279
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002280 if (mlx4_is_master(dev))
2281 priv->mfunc.comm =
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002282 ioremap(pci_resource_start(dev->persist->pdev,
2283 priv->fw.comm_bar) +
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002284 priv->fw.comm_base, MLX4_COMM_PAGESIZE);
2285 else
2286 priv->mfunc.comm =
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002287 ioremap(pci_resource_start(dev->persist->pdev, 2) +
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002288 MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
2289 if (!priv->mfunc.comm) {
Joe Perches1a91de22014-05-07 12:52:57 -07002290 mlx4_err(dev, "Couldn't map communication vector\n");
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002291 goto err_vhcr;
2292 }
2293
2294 if (mlx4_is_master(dev)) {
Ido Shamay4abccb62015-04-02 16:31:09 +03002295 struct mlx4_vf_oper_state *vf_oper;
2296 struct mlx4_vf_admin_state *vf_admin;
2297
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002298 priv->mfunc.master.slave_state =
2299 kzalloc(dev->num_slaves *
2300 sizeof(struct mlx4_slave_state), GFP_KERNEL);
2301 if (!priv->mfunc.master.slave_state)
2302 goto err_comm;
2303
Rony Efraim0eb62b92013-04-25 05:22:26 +00002304 priv->mfunc.master.vf_admin =
2305 kzalloc(dev->num_slaves *
2306 sizeof(struct mlx4_vf_admin_state), GFP_KERNEL);
2307 if (!priv->mfunc.master.vf_admin)
2308 goto err_comm_admin;
2309
2310 priv->mfunc.master.vf_oper =
2311 kzalloc(dev->num_slaves *
2312 sizeof(struct mlx4_vf_oper_state), GFP_KERNEL);
2313 if (!priv->mfunc.master.vf_oper)
2314 goto err_comm_oper;
2315
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002316 for (i = 0; i < dev->num_slaves; ++i) {
Ido Shamay4abccb62015-04-02 16:31:09 +03002317 vf_admin = &priv->mfunc.master.vf_admin[i];
2318 vf_oper = &priv->mfunc.master.vf_oper[i];
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002319 s_state = &priv->mfunc.master.slave_state[i];
2320 s_state->last_cmd = MLX4_COMM_CMD_RESET;
Jack Morgensteinbffb0232015-03-24 15:18:39 +02002321 mutex_init(&priv->mfunc.master.gen_eqe_mutex[i]);
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002322 for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
2323 s_state->event_eq[j].eqn = -1;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002324 __raw_writel((__force u32) 0,
2325 &priv->mfunc.comm[i].slave_write);
2326 __raw_writel((__force u32) 0,
2327 &priv->mfunc.comm[i].slave_read);
2328 mmiowb();
2329 for (port = 1; port <= MLX4_MAX_PORTS; port++) {
Ido Shamay4abccb62015-04-02 16:31:09 +03002330 struct mlx4_vport_state *admin_vport;
2331 struct mlx4_vport_state *oper_vport;
2332
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002333 s_state->vlan_filter[port] =
2334 kzalloc(sizeof(struct mlx4_vlan_fltr),
2335 GFP_KERNEL);
2336 if (!s_state->vlan_filter[port]) {
2337 if (--port)
2338 kfree(s_state->vlan_filter[port]);
2339 goto err_slaves;
2340 }
Ido Shamay4abccb62015-04-02 16:31:09 +03002341
2342 admin_vport = &vf_admin->vport[port];
2343 oper_vport = &vf_oper->vport[port].state;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002344 INIT_LIST_HEAD(&s_state->mcast_filters[port]);
Ido Shamay4abccb62015-04-02 16:31:09 +03002345 admin_vport->default_vlan = MLX4_VGT;
2346 oper_vport->default_vlan = MLX4_VGT;
Ido Shamay08068cd2015-04-02 16:31:15 +03002347 admin_vport->qos_vport =
2348 MLX4_VPP_DEFAULT_VPORT;
2349 oper_vport->qos_vport = MLX4_VPP_DEFAULT_VPORT;
Ido Shamay4abccb62015-04-02 16:31:09 +03002350 vf_oper->vport[port].vlan_idx = NO_INDX;
2351 vf_oper->vport[port].mac_idx = NO_INDX;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002352 }
2353 spin_lock_init(&s_state->lock);
2354 }
2355
Ido Shamay666672d2015-04-02 16:31:14 +03002356 if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP) {
2357 for (port = 1; port <= dev->caps.num_ports; port++) {
2358 if (mlx4_is_eth(dev, port)) {
2359 mlx4_set_default_port_qos(dev, port);
2360 mlx4_allocate_port_vpps(dev, port);
2361 }
2362 }
2363 }
2364
Or Gerlitz08ff3232012-10-21 14:59:24 +00002365 memset(&priv->mfunc.master.cmd_eqe, 0, dev->caps.eqe_size);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002366 priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
2367 INIT_WORK(&priv->mfunc.master.comm_work,
2368 mlx4_master_comm_channel);
2369 INIT_WORK(&priv->mfunc.master.slave_event_work,
2370 mlx4_gen_slave_eqe);
2371 INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
2372 mlx4_master_handle_slave_flr);
2373 spin_lock_init(&priv->mfunc.master.slave_state_lock);
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00002374 spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002375 priv->mfunc.master.comm_wq =
2376 create_singlethread_workqueue("mlx4_comm");
2377 if (!priv->mfunc.master.comm_wq)
2378 goto err_slaves;
2379
2380 if (mlx4_init_resource_tracker(dev))
2381 goto err_thread;
2382
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002383 } else {
2384 err = sync_toggles(dev);
2385 if (err) {
2386 mlx4_err(dev, "Couldn't sync toggles\n");
2387 goto err_comm;
2388 }
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002389 }
2390 return 0;
2391
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002392err_thread:
2393 flush_workqueue(priv->mfunc.master.comm_wq);
2394 destroy_workqueue(priv->mfunc.master.comm_wq);
2395err_slaves:
2396 while (--i) {
2397 for (port = 1; port <= MLX4_MAX_PORTS; port++)
2398 kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
2399 }
Rony Efraim0eb62b92013-04-25 05:22:26 +00002400 kfree(priv->mfunc.master.vf_oper);
2401err_comm_oper:
2402 kfree(priv->mfunc.master.vf_admin);
2403err_comm_admin:
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002404 kfree(priv->mfunc.master.slave_state);
2405err_comm:
2406 iounmap(priv->mfunc.comm);
2407err_vhcr:
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002408 dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
2409 priv->mfunc.vhcr,
2410 priv->mfunc.vhcr_dma);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002411 priv->mfunc.vhcr = NULL;
2412 return -ENOMEM;
2413}
2414
Roland Dreier225c7b12007-05-08 18:00:38 -07002415int mlx4_cmd_init(struct mlx4_dev *dev)
2416{
2417 struct mlx4_priv *priv = mlx4_priv(dev);
Matan Barakffc39f62014-11-13 14:45:29 +02002418 int flags = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002419
Matan Barakffc39f62014-11-13 14:45:29 +02002420 if (!priv->cmd.initialized) {
Matan Barakffc39f62014-11-13 14:45:29 +02002421 mutex_init(&priv->cmd.slave_cmd_mutex);
2422 sema_init(&priv->cmd.poll_sem, 1);
2423 priv->cmd.use_events = 0;
2424 priv->cmd.toggle = 1;
2425 priv->cmd.initialized = 1;
2426 flags |= MLX4_CMD_CLEANUP_STRUCT;
2427 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002428
Matan Barakffc39f62014-11-13 14:45:29 +02002429 if (!mlx4_is_slave(dev) && !priv->cmd.hcr) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002430 priv->cmd.hcr = ioremap(pci_resource_start(dev->persist->pdev,
2431 0) + MLX4_HCR_BASE, MLX4_HCR_SIZE);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002432 if (!priv->cmd.hcr) {
Joe Perches1a91de22014-05-07 12:52:57 -07002433 mlx4_err(dev, "Couldn't map command register\n");
Matan Barakffc39f62014-11-13 14:45:29 +02002434 goto err;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002435 }
Matan Barakffc39f62014-11-13 14:45:29 +02002436 flags |= MLX4_CMD_CLEANUP_HCR;
Roland Dreier225c7b12007-05-08 18:00:38 -07002437 }
2438
Matan Barakffc39f62014-11-13 14:45:29 +02002439 if (mlx4_is_mfunc(dev) && !priv->mfunc.vhcr) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002440 priv->mfunc.vhcr = dma_alloc_coherent(&dev->persist->pdev->dev,
2441 PAGE_SIZE,
Roland Dreierf3d4c892012-09-25 21:24:07 -07002442 &priv->mfunc.vhcr_dma,
2443 GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002444 if (!priv->mfunc.vhcr)
Matan Barakffc39f62014-11-13 14:45:29 +02002445 goto err;
2446
2447 flags |= MLX4_CMD_CLEANUP_VHCR;
Roland Dreierf3d4c892012-09-25 21:24:07 -07002448 }
2449
Matan Barakffc39f62014-11-13 14:45:29 +02002450 if (!priv->cmd.pool) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002451 priv->cmd.pool = pci_pool_create("mlx4_cmd",
2452 dev->persist->pdev,
Matan Barakffc39f62014-11-13 14:45:29 +02002453 MLX4_MAILBOX_SIZE,
2454 MLX4_MAILBOX_SIZE, 0);
2455 if (!priv->cmd.pool)
2456 goto err;
2457
2458 flags |= MLX4_CMD_CLEANUP_POOL;
2459 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002460
2461 return 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002462
Matan Barakffc39f62014-11-13 14:45:29 +02002463err:
2464 mlx4_cmd_cleanup(dev, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002465 return -ENOMEM;
Roland Dreier225c7b12007-05-08 18:00:38 -07002466}
2467
Yishai Hadas55ad3592015-01-25 16:59:42 +02002468void mlx4_report_internal_err_comm_event(struct mlx4_dev *dev)
2469{
2470 struct mlx4_priv *priv = mlx4_priv(dev);
2471 int slave;
2472 u32 slave_read;
2473
2474 /* Report an internal error event to all
2475 * communication channels.
2476 */
2477 for (slave = 0; slave < dev->num_slaves; slave++) {
2478 slave_read = swab32(readl(&priv->mfunc.comm[slave].slave_read));
2479 slave_read |= (u32)COMM_CHAN_EVENT_INTERNAL_ERR;
2480 __raw_writel((__force u32)cpu_to_be32(slave_read),
2481 &priv->mfunc.comm[slave].slave_read);
2482 /* Make sure that our comm channel write doesn't
2483 * get mixed in with writes from another CPU.
2484 */
2485 mmiowb();
2486 }
2487}
2488
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002489void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
2490{
2491 struct mlx4_priv *priv = mlx4_priv(dev);
2492 int i, port;
2493
2494 if (mlx4_is_master(dev)) {
2495 flush_workqueue(priv->mfunc.master.comm_wq);
2496 destroy_workqueue(priv->mfunc.master.comm_wq);
2497 for (i = 0; i < dev->num_slaves; i++) {
2498 for (port = 1; port <= MLX4_MAX_PORTS; port++)
2499 kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
2500 }
2501 kfree(priv->mfunc.master.slave_state);
Rony Efraim0eb62b92013-04-25 05:22:26 +00002502 kfree(priv->mfunc.master.vf_admin);
2503 kfree(priv->mfunc.master.vf_oper);
Yishai Hadas55ad3592015-01-25 16:59:42 +02002504 dev->num_slaves = 0;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002505 }
Eugenia Emantayevf08ad062012-02-06 06:26:17 +00002506
2507 iounmap(priv->mfunc.comm);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002508}
2509
Matan Barakffc39f62014-11-13 14:45:29 +02002510void mlx4_cmd_cleanup(struct mlx4_dev *dev, int cleanup_mask)
Roland Dreier225c7b12007-05-08 18:00:38 -07002511{
2512 struct mlx4_priv *priv = mlx4_priv(dev);
2513
Matan Barakffc39f62014-11-13 14:45:29 +02002514 if (priv->cmd.pool && (cleanup_mask & MLX4_CMD_CLEANUP_POOL)) {
2515 pci_pool_destroy(priv->cmd.pool);
2516 priv->cmd.pool = NULL;
2517 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002518
Matan Barakffc39f62014-11-13 14:45:29 +02002519 if (!mlx4_is_slave(dev) && priv->cmd.hcr &&
2520 (cleanup_mask & MLX4_CMD_CLEANUP_HCR)) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002521 iounmap(priv->cmd.hcr);
Matan Barakffc39f62014-11-13 14:45:29 +02002522 priv->cmd.hcr = NULL;
2523 }
2524 if (mlx4_is_mfunc(dev) && priv->mfunc.vhcr &&
2525 (cleanup_mask & MLX4_CMD_CLEANUP_VHCR)) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002526 dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
Roland Dreierf3d4c892012-09-25 21:24:07 -07002527 priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
Matan Barakffc39f62014-11-13 14:45:29 +02002528 priv->mfunc.vhcr = NULL;
2529 }
2530 if (priv->cmd.initialized && (cleanup_mask & MLX4_CMD_CLEANUP_STRUCT))
2531 priv->cmd.initialized = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002532}
2533
2534/*
2535 * Switch to using events to issue FW commands (can only be called
2536 * after event queue for command events has been initialized).
2537 */
2538int mlx4_cmd_use_events(struct mlx4_dev *dev)
2539{
2540 struct mlx4_priv *priv = mlx4_priv(dev);
2541 int i;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002542 int err = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002543
2544 priv->cmd.context = kmalloc(priv->cmd.max_cmds *
2545 sizeof (struct mlx4_cmd_context),
2546 GFP_KERNEL);
2547 if (!priv->cmd.context)
2548 return -ENOMEM;
2549
2550 for (i = 0; i < priv->cmd.max_cmds; ++i) {
2551 priv->cmd.context[i].token = i;
2552 priv->cmd.context[i].next = i + 1;
Yishai Hadasf5aef5a2015-01-25 16:59:39 +02002553 /* To support fatal error flow, initialize all
2554 * cmd contexts to allow simulating completions
2555 * with complete() at any time.
2556 */
2557 init_completion(&priv->cmd.context[i].done);
Roland Dreier225c7b12007-05-08 18:00:38 -07002558 }
2559
2560 priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
2561 priv->cmd.free_head = 0;
2562
2563 sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
2564 spin_lock_init(&priv->cmd.context_lock);
2565
2566 for (priv->cmd.token_mask = 1;
2567 priv->cmd.token_mask < priv->cmd.max_cmds;
2568 priv->cmd.token_mask <<= 1)
2569 ; /* nothing */
2570 --priv->cmd.token_mask;
2571
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002572 down(&priv->cmd.poll_sem);
Roland Dreier225c7b12007-05-08 18:00:38 -07002573 priv->cmd.use_events = 1;
2574
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002575 return err;
Roland Dreier225c7b12007-05-08 18:00:38 -07002576}
2577
2578/*
2579 * Switch back to polling (used when shutting down the device)
2580 */
2581void mlx4_cmd_use_polling(struct mlx4_dev *dev)
2582{
2583 struct mlx4_priv *priv = mlx4_priv(dev);
2584 int i;
2585
2586 priv->cmd.use_events = 0;
2587
2588 for (i = 0; i < priv->cmd.max_cmds; ++i)
2589 down(&priv->cmd.event_sem);
2590
2591 kfree(priv->cmd.context);
2592
2593 up(&priv->cmd.poll_sem);
2594}
2595
2596struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
2597{
2598 struct mlx4_cmd_mailbox *mailbox;
2599
2600 mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
2601 if (!mailbox)
2602 return ERR_PTR(-ENOMEM);
2603
2604 mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
2605 &mailbox->dma);
2606 if (!mailbox->buf) {
2607 kfree(mailbox);
2608 return ERR_PTR(-ENOMEM);
2609 }
2610
Jack Morgenstein571b8b92013-11-07 12:19:50 +02002611 memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
2612
Roland Dreier225c7b12007-05-08 18:00:38 -07002613 return mailbox;
2614}
2615EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
2616
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002617void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
2618 struct mlx4_cmd_mailbox *mailbox)
Roland Dreier225c7b12007-05-08 18:00:38 -07002619{
2620 if (!mailbox)
2621 return;
2622
2623 pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
2624 kfree(mailbox);
2625}
2626EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002627
2628u32 mlx4_comm_get_version(void)
2629{
2630 return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
2631}
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002632
2633static int mlx4_get_slave_indx(struct mlx4_dev *dev, int vf)
2634{
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002635 if ((vf < 0) || (vf >= dev->persist->num_vfs)) {
2636 mlx4_err(dev, "Bad vf number:%d (number of activated vf: %d)\n",
2637 vf, dev->persist->num_vfs);
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002638 return -EINVAL;
2639 }
2640
2641 return vf+1;
2642}
2643
Matan Barakf74462a2014-03-19 18:11:51 +02002644int mlx4_get_vf_indx(struct mlx4_dev *dev, int slave)
2645{
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002646 if (slave < 1 || slave > dev->persist->num_vfs) {
Matan Barakf74462a2014-03-19 18:11:51 +02002647 mlx4_err(dev,
2648 "Bad slave number:%d (number of activated slaves: %lu)\n",
2649 slave, dev->num_slaves);
2650 return -EINVAL;
2651 }
2652 return slave - 1;
2653}
2654
Yishai Hadasf5aef5a2015-01-25 16:59:39 +02002655void mlx4_cmd_wake_completions(struct mlx4_dev *dev)
2656{
2657 struct mlx4_priv *priv = mlx4_priv(dev);
2658 struct mlx4_cmd_context *context;
2659 int i;
2660
2661 spin_lock(&priv->cmd.context_lock);
2662 if (priv->cmd.context) {
2663 for (i = 0; i < priv->cmd.max_cmds; ++i) {
2664 context = &priv->cmd.context[i];
2665 context->fw_status = CMD_STAT_INTERNAL_ERR;
2666 context->result =
2667 mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
2668 complete(&context->done);
2669 }
2670 }
2671 spin_unlock(&priv->cmd.context_lock);
2672}
2673
Matan Barakf74462a2014-03-19 18:11:51 +02002674struct mlx4_active_ports mlx4_get_active_ports(struct mlx4_dev *dev, int slave)
2675{
2676 struct mlx4_active_ports actv_ports;
2677 int vf;
2678
2679 bitmap_zero(actv_ports.ports, MLX4_MAX_PORTS);
2680
2681 if (slave == 0) {
2682 bitmap_fill(actv_ports.ports, dev->caps.num_ports);
2683 return actv_ports;
2684 }
2685
2686 vf = mlx4_get_vf_indx(dev, slave);
2687 if (vf < 0)
2688 return actv_ports;
2689
2690 bitmap_set(actv_ports.ports, dev->dev_vfs[vf].min_port - 1,
2691 min((int)dev->dev_vfs[mlx4_get_vf_indx(dev, slave)].n_ports,
2692 dev->caps.num_ports));
2693
2694 return actv_ports;
2695}
2696EXPORT_SYMBOL_GPL(mlx4_get_active_ports);
2697
2698int mlx4_slave_convert_port(struct mlx4_dev *dev, int slave, int port)
2699{
2700 unsigned n;
2701 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2702 unsigned m = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2703
2704 if (port <= 0 || port > m)
2705 return -EINVAL;
2706
2707 n = find_first_bit(actv_ports.ports, dev->caps.num_ports);
2708 if (port <= n)
2709 port = n + 1;
2710
2711 return port;
2712}
2713EXPORT_SYMBOL_GPL(mlx4_slave_convert_port);
2714
2715int mlx4_phys_to_slave_port(struct mlx4_dev *dev, int slave, int port)
2716{
2717 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2718 if (test_bit(port - 1, actv_ports.ports))
2719 return port -
2720 find_first_bit(actv_ports.ports, dev->caps.num_ports);
2721
2722 return -1;
2723}
2724EXPORT_SYMBOL_GPL(mlx4_phys_to_slave_port);
2725
2726struct mlx4_slaves_pport mlx4_phys_to_slaves_pport(struct mlx4_dev *dev,
2727 int port)
2728{
2729 unsigned i;
2730 struct mlx4_slaves_pport slaves_pport;
2731
2732 bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
2733
2734 if (port <= 0 || port > dev->caps.num_ports)
2735 return slaves_pport;
2736
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002737 for (i = 0; i < dev->persist->num_vfs + 1; i++) {
Matan Barakf74462a2014-03-19 18:11:51 +02002738 struct mlx4_active_ports actv_ports =
2739 mlx4_get_active_ports(dev, i);
2740 if (test_bit(port - 1, actv_ports.ports))
2741 set_bit(i, slaves_pport.slaves);
2742 }
2743
2744 return slaves_pport;
2745}
2746EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport);
2747
2748struct mlx4_slaves_pport mlx4_phys_to_slaves_pport_actv(
2749 struct mlx4_dev *dev,
2750 const struct mlx4_active_ports *crit_ports)
2751{
2752 unsigned i;
2753 struct mlx4_slaves_pport slaves_pport;
2754
2755 bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
2756
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002757 for (i = 0; i < dev->persist->num_vfs + 1; i++) {
Matan Barakf74462a2014-03-19 18:11:51 +02002758 struct mlx4_active_ports actv_ports =
2759 mlx4_get_active_ports(dev, i);
2760 if (bitmap_equal(crit_ports->ports, actv_ports.ports,
2761 dev->caps.num_ports))
2762 set_bit(i, slaves_pport.slaves);
2763 }
2764
2765 return slaves_pport;
2766}
2767EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport_actv);
2768
Matan Baraka91c7722014-09-10 16:41:53 +03002769static int mlx4_slaves_closest_port(struct mlx4_dev *dev, int slave, int port)
2770{
2771 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2772 int min_port = find_first_bit(actv_ports.ports, dev->caps.num_ports)
2773 + 1;
2774 int max_port = min_port +
2775 bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2776
2777 if (port < min_port)
2778 port = min_port;
2779 else if (port >= max_port)
2780 port = max_port - 1;
2781
2782 return port;
2783}
2784
Ido Shamaycda373f2015-04-02 16:31:16 +03002785static int mlx4_set_vport_qos(struct mlx4_priv *priv, int slave, int port,
2786 int max_tx_rate)
2787{
2788 int i;
2789 int err;
2790 struct mlx4_qos_manager *port_qos;
2791 struct mlx4_dev *dev = &priv->dev;
2792 struct mlx4_vport_qos_param vpp_qos[MLX4_NUM_UP];
2793
2794 port_qos = &priv->mfunc.master.qos_ctl[port];
2795 memset(vpp_qos, 0, sizeof(struct mlx4_vport_qos_param) * MLX4_NUM_UP);
2796
2797 if (slave > port_qos->num_of_qos_vfs) {
2798 mlx4_info(dev, "No availible VPP resources for this VF\n");
2799 return -EINVAL;
2800 }
2801
2802 /* Query for default QoS values from Vport 0 is needed */
2803 err = mlx4_SET_VPORT_QOS_get(dev, port, 0, vpp_qos);
2804 if (err) {
2805 mlx4_info(dev, "Failed to query Vport 0 QoS values\n");
2806 return err;
2807 }
2808
2809 for (i = 0; i < MLX4_NUM_UP; i++) {
2810 if (test_bit(i, port_qos->priority_bm) && max_tx_rate) {
2811 vpp_qos[i].max_avg_bw = max_tx_rate;
2812 vpp_qos[i].enable = 1;
2813 } else {
2814 /* if user supplied tx_rate == 0, meaning no rate limit
2815 * configuration is required. so we are leaving the
2816 * value of max_avg_bw as queried from Vport 0.
2817 */
2818 vpp_qos[i].enable = 0;
2819 }
2820 }
2821
2822 err = mlx4_SET_VPORT_QOS_set(dev, port, slave, vpp_qos);
2823 if (err) {
2824 mlx4_info(dev, "Failed to set Vport %d QoS values\n", slave);
2825 return err;
2826 }
2827
2828 return 0;
2829}
2830
2831static bool mlx4_is_vf_vst_and_prio_qos(struct mlx4_dev *dev, int port,
2832 struct mlx4_vport_state *vf_admin)
2833{
2834 struct mlx4_qos_manager *info;
2835 struct mlx4_priv *priv = mlx4_priv(dev);
2836
2837 if (!mlx4_is_master(dev) ||
2838 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
2839 return false;
2840
2841 info = &priv->mfunc.master.qos_ctl[port];
2842
2843 if (vf_admin->default_vlan != MLX4_VGT &&
2844 test_bit(vf_admin->default_qos, info->priority_bm))
2845 return true;
2846
2847 return false;
2848}
2849
2850static bool mlx4_valid_vf_state_change(struct mlx4_dev *dev, int port,
2851 struct mlx4_vport_state *vf_admin,
2852 int vlan, int qos)
2853{
2854 struct mlx4_vport_state dummy_admin = {0};
2855
2856 if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) ||
2857 !vf_admin->tx_rate)
2858 return true;
2859
2860 dummy_admin.default_qos = qos;
2861 dummy_admin.default_vlan = vlan;
2862
2863 /* VF wants to move to other VST state which is valid with current
2864 * rate limit. Either differnt default vlan in VST or other
2865 * supported QoS priority. Otherwise we don't allow this change when
2866 * the TX rate is still configured.
2867 */
2868 if (mlx4_is_vf_vst_and_prio_qos(dev, port, &dummy_admin))
2869 return true;
2870
2871 mlx4_info(dev, "Cannot change VF state to %s while rate is set\n",
2872 (vlan == MLX4_VGT) ? "VGT" : "VST");
2873
2874 if (vlan != MLX4_VGT)
2875 mlx4_info(dev, "VST priority %d not supported for QoS\n", qos);
2876
2877 mlx4_info(dev, "Please set rate to 0 prior to this VF state change\n");
2878
2879 return false;
2880}
2881
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002882int mlx4_set_vf_mac(struct mlx4_dev *dev, int port, int vf, u64 mac)
2883{
2884 struct mlx4_priv *priv = mlx4_priv(dev);
2885 struct mlx4_vport_state *s_info;
2886 int slave;
2887
2888 if (!mlx4_is_master(dev))
2889 return -EPROTONOSUPPORT;
2890
2891 slave = mlx4_get_slave_indx(dev, vf);
2892 if (slave < 0)
2893 return -EINVAL;
2894
Matan Baraka91c7722014-09-10 16:41:53 +03002895 port = mlx4_slaves_closest_port(dev, slave, port);
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002896 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
2897 s_info->mac = mac;
2898 mlx4_info(dev, "default mac on vf %d port %d to %llX will take afect only after vf restart\n",
2899 vf, port, s_info->mac);
2900 return 0;
2901}
2902EXPORT_SYMBOL_GPL(mlx4_set_vf_mac);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002903
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002904
Rony Efraim3f7fb022013-04-25 05:22:28 +00002905int mlx4_set_vf_vlan(struct mlx4_dev *dev, int port, int vf, u16 vlan, u8 qos)
2906{
2907 struct mlx4_priv *priv = mlx4_priv(dev);
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002908 struct mlx4_vport_state *vf_admin;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002909 int slave;
2910
2911 if ((!mlx4_is_master(dev)) ||
2912 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
2913 return -EPROTONOSUPPORT;
2914
2915 if ((vlan > 4095) || (qos > 7))
2916 return -EINVAL;
2917
2918 slave = mlx4_get_slave_indx(dev, vf);
2919 if (slave < 0)
2920 return -EINVAL;
2921
Matan Baraka91c7722014-09-10 16:41:53 +03002922 port = mlx4_slaves_closest_port(dev, slave, port);
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002923 vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002924
Ido Shamaycda373f2015-04-02 16:31:16 +03002925 if (!mlx4_valid_vf_state_change(dev, port, vf_admin, vlan, qos))
2926 return -EPERM;
2927
Rony Efraim3f7fb022013-04-25 05:22:28 +00002928 if ((0 == vlan) && (0 == qos))
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002929 vf_admin->default_vlan = MLX4_VGT;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002930 else
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002931 vf_admin->default_vlan = vlan;
2932 vf_admin->default_qos = qos;
2933
Ido Shamaycda373f2015-04-02 16:31:16 +03002934 /* If rate was configured prior to VST, we saved the configured rate
2935 * in vf_admin->rate and now, if priority supported we enforce the QoS
2936 */
2937 if (mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) &&
2938 vf_admin->tx_rate)
2939 vf_admin->qos_vport = slave;
2940
Rony Efraim0a6eac22013-06-27 19:05:22 +03002941 if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
2942 mlx4_info(dev,
2943 "updating vf %d port %d config will take effect on next VF restart\n",
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002944 vf, port);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002945 return 0;
2946}
2947EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
Rony Efraime6b6a232013-04-25 05:22:29 +00002948
Ido Shamaycda373f2015-04-02 16:31:16 +03002949int mlx4_set_vf_rate(struct mlx4_dev *dev, int port, int vf, int min_tx_rate,
2950 int max_tx_rate)
2951{
2952 int err;
2953 int slave;
2954 struct mlx4_vport_state *vf_admin;
2955 struct mlx4_priv *priv = mlx4_priv(dev);
2956
2957 if (!mlx4_is_master(dev) ||
2958 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
2959 return -EPROTONOSUPPORT;
2960
2961 if (min_tx_rate) {
2962 mlx4_info(dev, "Minimum BW share not supported\n");
2963 return -EPROTONOSUPPORT;
2964 }
2965
2966 slave = mlx4_get_slave_indx(dev, vf);
2967 if (slave < 0)
2968 return -EINVAL;
2969
2970 port = mlx4_slaves_closest_port(dev, slave, port);
2971 vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
2972
2973 err = mlx4_set_vport_qos(priv, slave, port, max_tx_rate);
2974 if (err) {
2975 mlx4_info(dev, "vf %d failed to set rate %d\n", vf,
2976 max_tx_rate);
2977 return err;
2978 }
2979
2980 vf_admin->tx_rate = max_tx_rate;
2981 /* if VF is not in supported mode (VST with supported prio),
2982 * we do not change vport configuration for its QPs, but save
2983 * the rate, so it will be enforced when it moves to supported
2984 * mode next time.
2985 */
2986 if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin)) {
2987 mlx4_info(dev,
2988 "rate set for VF %d when not in valid state\n", vf);
2989
2990 if (vf_admin->default_vlan != MLX4_VGT)
2991 mlx4_info(dev, "VST priority not supported by QoS\n");
2992 else
2993 mlx4_info(dev, "VF in VGT mode (needed VST)\n");
2994
2995 mlx4_info(dev,
2996 "rate %d take affect when VF moves to valid state\n",
2997 max_tx_rate);
2998 return 0;
2999 }
3000
3001 /* If user sets rate 0 assigning default vport for its QPs */
3002 vf_admin->qos_vport = max_tx_rate ? slave : MLX4_VPP_DEFAULT_VPORT;
3003
3004 if (priv->mfunc.master.slave_state[slave].active &&
3005 dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)
3006 mlx4_master_immediate_activate_vlan_qos(priv, slave, port);
3007
3008 return 0;
3009}
3010EXPORT_SYMBOL_GPL(mlx4_set_vf_rate);
3011
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +02003012 /* mlx4_get_slave_default_vlan -
3013 * return true if VST ( default vlan)
3014 * if VST, will return vlan & qos (if not NULL)
3015 */
3016bool mlx4_get_slave_default_vlan(struct mlx4_dev *dev, int port, int slave,
3017 u16 *vlan, u8 *qos)
3018{
3019 struct mlx4_vport_oper_state *vp_oper;
3020 struct mlx4_priv *priv;
3021
3022 priv = mlx4_priv(dev);
Matan Baraka91c7722014-09-10 16:41:53 +03003023 port = mlx4_slaves_closest_port(dev, slave, port);
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +02003024 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
3025
3026 if (MLX4_VGT != vp_oper->state.default_vlan) {
3027 if (vlan)
3028 *vlan = vp_oper->state.default_vlan;
3029 if (qos)
3030 *qos = vp_oper->state.default_qos;
3031 return true;
3032 }
3033 return false;
3034}
3035EXPORT_SYMBOL_GPL(mlx4_get_slave_default_vlan);
3036
Rony Efraime6b6a232013-04-25 05:22:29 +00003037int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
3038{
3039 struct mlx4_priv *priv = mlx4_priv(dev);
3040 struct mlx4_vport_state *s_info;
3041 int slave;
3042
3043 if ((!mlx4_is_master(dev)) ||
3044 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
3045 return -EPROTONOSUPPORT;
3046
3047 slave = mlx4_get_slave_indx(dev, vf);
3048 if (slave < 0)
3049 return -EINVAL;
3050
Matan Baraka91c7722014-09-10 16:41:53 +03003051 port = mlx4_slaves_closest_port(dev, slave, port);
Rony Efraime6b6a232013-04-25 05:22:29 +00003052 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
3053 s_info->spoofchk = setting;
3054
3055 return 0;
3056}
3057EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
Rony Efraim2cccb9e2013-04-25 05:22:30 +00003058
3059int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
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 return -EPROTONOSUPPORT;
3067
3068 slave = mlx4_get_slave_indx(dev, vf);
3069 if (slave < 0)
3070 return -EINVAL;
3071
3072 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
3073 ivf->vf = vf;
3074
3075 /* need to convert it to a func */
3076 ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
3077 ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
3078 ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
3079 ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
3080 ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
3081 ivf->mac[5] = ((s_info->mac) & 0xff);
3082
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04003083 ivf->vlan = s_info->default_vlan;
3084 ivf->qos = s_info->default_qos;
Ido Shamaycda373f2015-04-02 16:31:16 +03003085
3086 if (mlx4_is_vf_vst_and_prio_qos(dev, port, s_info))
3087 ivf->max_tx_rate = s_info->tx_rate;
3088 else
3089 ivf->max_tx_rate = 0;
3090
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04003091 ivf->min_tx_rate = 0;
3092 ivf->spoofchk = s_info->spoofchk;
3093 ivf->linkstate = s_info->link_state;
Rony Efraim2cccb9e2013-04-25 05:22:30 +00003094
3095 return 0;
3096}
3097EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
Rony Efraim948e3062013-06-13 13:19:11 +03003098
3099int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
3100{
3101 struct mlx4_priv *priv = mlx4_priv(dev);
3102 struct mlx4_vport_state *s_info;
Rony Efraim948e3062013-06-13 13:19:11 +03003103 int slave;
3104 u8 link_stat_event;
3105
3106 slave = mlx4_get_slave_indx(dev, vf);
3107 if (slave < 0)
3108 return -EINVAL;
3109
Matan Baraka91c7722014-09-10 16:41:53 +03003110 port = mlx4_slaves_closest_port(dev, slave, port);
Rony Efraim948e3062013-06-13 13:19:11 +03003111 switch (link_state) {
3112 case IFLA_VF_LINK_STATE_AUTO:
3113 /* get current link state */
3114 if (!priv->sense.do_sense_port[port])
3115 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
3116 else
3117 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
3118 break;
3119
3120 case IFLA_VF_LINK_STATE_ENABLE:
3121 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
3122 break;
3123
3124 case IFLA_VF_LINK_STATE_DISABLE:
3125 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
3126 break;
3127
3128 default:
3129 mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
3130 link_state, slave, port);
3131 return -EINVAL;
3132 };
Rony Efraim948e3062013-06-13 13:19:11 +03003133 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
Rony Efraim948e3062013-06-13 13:19:11 +03003134 s_info->link_state = link_state;
Rony Efraim948e3062013-06-13 13:19:11 +03003135
3136 /* send event */
3137 mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
Rony Efraim0a6eac22013-06-27 19:05:22 +03003138
3139 if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
3140 mlx4_dbg(dev,
3141 "updating vf %d port %d no link state HW enforcment\n",
3142 vf, port);
Rony Efraim948e3062013-06-13 13:19:11 +03003143 return 0;
3144}
3145EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);
Jack Morgenstein97982f52014-05-29 16:31:02 +03003146
3147int mlx4_vf_smi_enabled(struct mlx4_dev *dev, int slave, int port)
3148{
Jack Morgenstein99ec41d2014-05-29 16:31:03 +03003149 struct mlx4_priv *priv = mlx4_priv(dev);
3150
3151 if (slave < 1 || slave >= dev->num_slaves ||
3152 port < 1 || port > MLX4_MAX_PORTS)
3153 return 0;
3154
3155 return priv->mfunc.master.vf_oper[slave].smi_enabled[port] ==
3156 MLX4_VF_SMI_ENABLED;
Jack Morgenstein97982f52014-05-29 16:31:02 +03003157}
3158EXPORT_SYMBOL_GPL(mlx4_vf_smi_enabled);
Jack Morgenstein65fed8a2014-05-29 16:31:04 +03003159
3160int mlx4_vf_get_enable_smi_admin(struct mlx4_dev *dev, int slave, int port)
3161{
3162 struct mlx4_priv *priv = mlx4_priv(dev);
3163
3164 if (slave == mlx4_master_func_num(dev))
3165 return 1;
3166
3167 if (slave < 1 || slave >= dev->num_slaves ||
3168 port < 1 || port > MLX4_MAX_PORTS)
3169 return 0;
3170
3171 return priv->mfunc.master.vf_admin[slave].enable_smi[port] ==
3172 MLX4_VF_SMI_ENABLED;
3173}
3174EXPORT_SYMBOL_GPL(mlx4_vf_get_enable_smi_admin);
3175
3176int mlx4_vf_set_enable_smi_admin(struct mlx4_dev *dev, int slave, int port,
3177 int enabled)
3178{
3179 struct mlx4_priv *priv = mlx4_priv(dev);
3180
3181 if (slave == mlx4_master_func_num(dev))
3182 return 0;
3183
3184 if (slave < 1 || slave >= dev->num_slaves ||
3185 port < 1 || port > MLX4_MAX_PORTS ||
3186 enabled < 0 || enabled > 1)
3187 return -EINVAL;
3188
3189 priv->mfunc.master.vf_admin[slave].enable_smi[port] = enabled;
3190 return 0;
3191}
3192EXPORT_SYMBOL_GPL(mlx4_vf_set_enable_smi_admin);