blob: 153fb15769a8635d3fbfec1b15358689225ab574 [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 */
Jack Morgensteinfde913e2015-04-05 17:50:48 +0300728 if (op == MLX4_CMD_SET_PORT &&
729 (in_modifier == 1 || in_modifier == 2) &&
Ido Shamaya130b592015-04-02 16:31:19 +0300730 op_modifier == MLX4_SET_PORT_IB_OPCODE &&
731 context->fw_status == CMD_STAT_BAD_SIZE)
Jack Morgenstein1daa4302014-09-30 12:03:50 +0300732 mlx4_dbg(dev, "command 0x%x failed: fw status = 0x%x\n",
733 op, context->fw_status);
734 else
735 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
736 op, context->fw_status);
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200737 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
738 err = mlx4_internal_err_ret_value(dev, op, op_modifier);
739 else if (mlx4_closing_cmd_fatal_error(op, context->fw_status))
740 goto out_reset;
741
Roland Dreier225c7b12007-05-08 18:00:38 -0700742 goto out;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000743 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700744
745 if (out_is_imm)
746 *out_param = context->out_param;
747
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200748out_reset:
749 if (err)
750 err = mlx4_cmd_reset_flow(dev, op, op_modifier, err);
Roland Dreier225c7b12007-05-08 18:00:38 -0700751out:
752 spin_lock(&cmd->context_lock);
753 context->next = cmd->free_head;
754 cmd->free_head = context - cmd->context;
755 spin_unlock(&cmd->context_lock);
756
757 up(&cmd->event_sem);
758 return err;
759}
760
761int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
762 int out_is_imm, u32 in_modifier, u8 op_modifier,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000763 u16 op, unsigned long timeout, int native)
Roland Dreier225c7b12007-05-08 18:00:38 -0700764{
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200765 if (pci_channel_offline(dev->persist->pdev))
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200766 return mlx4_cmd_reset_flow(dev, op, op_modifier, -EIO);
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000767
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000768 if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200769 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
770 return mlx4_internal_err_ret_value(dev, op,
771 op_modifier);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000772 if (mlx4_priv(dev)->cmd.use_events)
773 return mlx4_cmd_wait(dev, in_param, out_param,
774 out_is_imm, in_modifier,
775 op_modifier, op, timeout);
776 else
777 return mlx4_cmd_poll(dev, in_param, out_param,
778 out_is_imm, in_modifier,
779 op_modifier, op, timeout);
780 }
781 return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
782 in_modifier, op_modifier, op, timeout);
Roland Dreier225c7b12007-05-08 18:00:38 -0700783}
784EXPORT_SYMBOL_GPL(__mlx4_cmd);
785
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000786
Yishai Hadas55ad3592015-01-25 16:59:42 +0200787int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000788{
789 return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
790 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
791}
792
793static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
794 int slave, u64 slave_addr,
795 int size, int is_read)
796{
797 u64 in_param;
798 u64 out_param;
799
800 if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
801 (slave & ~0x7f) | (size & 0xff)) {
Joe Perches1a91de22014-05-07 12:52:57 -0700802 mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx master_addr:0x%llx slave_id:%d size:%d\n",
803 slave_addr, master_addr, slave, size);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000804 return -EINVAL;
805 }
806
807 if (is_read) {
808 in_param = (u64) slave | slave_addr;
809 out_param = (u64) dev->caps.function | master_addr;
810 } else {
811 in_param = (u64) dev->caps.function | master_addr;
812 out_param = (u64) slave | slave_addr;
813 }
814
815 return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
816 MLX4_CMD_ACCESS_MEM,
817 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
818}
819
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000820static int query_pkey_block(struct mlx4_dev *dev, u8 port, u16 index, u16 *pkey,
821 struct mlx4_cmd_mailbox *inbox,
822 struct mlx4_cmd_mailbox *outbox)
823{
824 struct ib_smp *in_mad = (struct ib_smp *)(inbox->buf);
825 struct ib_smp *out_mad = (struct ib_smp *)(outbox->buf);
826 int err;
827 int i;
828
829 if (index & 0x1f)
830 return -EINVAL;
831
832 in_mad->attr_mod = cpu_to_be32(index / 32);
833
834 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
835 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
836 MLX4_CMD_NATIVE);
837 if (err)
838 return err;
839
840 for (i = 0; i < 32; ++i)
841 pkey[i] = be16_to_cpu(((__be16 *) out_mad->data)[i]);
842
843 return err;
844}
845
846static int get_full_pkey_table(struct mlx4_dev *dev, u8 port, u16 *table,
847 struct mlx4_cmd_mailbox *inbox,
848 struct mlx4_cmd_mailbox *outbox)
849{
850 int i;
851 int err;
852
853 for (i = 0; i < dev->caps.pkey_table_len[port]; i += 32) {
854 err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
855 if (err)
856 return err;
857 }
858
859 return 0;
860}
861#define PORT_CAPABILITY_LOCATION_IN_SMP 20
862#define PORT_STATE_OFFSET 32
863
864static enum ib_port_state vf_port_state(struct mlx4_dev *dev, int port, int vf)
865{
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000866 if (mlx4_get_slave_port_state(dev, vf, port) == SLAVE_PORT_UP)
867 return IB_PORT_ACTIVE;
868 else
869 return IB_PORT_DOWN;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000870}
871
872static int mlx4_MAD_IFC_wrapper(struct mlx4_dev *dev, int slave,
873 struct mlx4_vhcr *vhcr,
874 struct mlx4_cmd_mailbox *inbox,
875 struct mlx4_cmd_mailbox *outbox,
876 struct mlx4_cmd_info *cmd)
877{
878 struct ib_smp *smp = inbox->buf;
879 u32 index;
Or Gerlitz7c35ef42015-05-21 15:14:05 +0300880 u8 port, slave_port;
Jack Morgenstein97982f52014-05-29 16:31:02 +0300881 u8 opcode_modifier;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000882 u16 *table;
883 int err;
884 int vidx, pidx;
Jack Morgenstein97982f52014-05-29 16:31:02 +0300885 int network_view;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000886 struct mlx4_priv *priv = mlx4_priv(dev);
887 struct ib_smp *outsmp = outbox->buf;
888 __be16 *outtab = (__be16 *)(outsmp->data);
889 __be32 slave_cap_mask;
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000890 __be64 slave_node_guid;
Jack Morgenstein97982f52014-05-29 16:31:02 +0300891
Or Gerlitz7c35ef42015-05-21 15:14:05 +0300892 slave_port = vhcr->in_modifier;
893 port = mlx4_slave_convert_port(dev, slave, slave_port);
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000894
Jack Morgenstein97982f52014-05-29 16:31:02 +0300895 /* network-view bit is for driver use only, and should not be passed to FW */
896 opcode_modifier = vhcr->op_modifier & ~0x8; /* clear netw view bit */
897 network_view = !!(vhcr->op_modifier & 0x8);
898
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000899 if (smp->base_version == 1 &&
900 smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
901 smp->class_version == 1) {
Jack Morgenstein97982f52014-05-29 16:31:02 +0300902 /* host view is paravirtualized */
903 if (!network_view && smp->method == IB_MGMT_METHOD_GET) {
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000904 if (smp->attr_id == IB_SMP_ATTR_PKEY_TABLE) {
905 index = be32_to_cpu(smp->attr_mod);
906 if (port < 1 || port > dev->caps.num_ports)
907 return -EINVAL;
Matan Barak19ab5742015-01-27 15:58:07 +0200908 table = kcalloc((dev->caps.pkey_table_len[port] / 32) + 1,
909 sizeof(*table) * 32, GFP_KERNEL);
910
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000911 if (!table)
912 return -ENOMEM;
913 /* need to get the full pkey table because the paravirtualized
914 * pkeys may be scattered among several pkey blocks.
915 */
916 err = get_full_pkey_table(dev, port, table, inbox, outbox);
917 if (!err) {
918 for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
919 pidx = priv->virt2phys_pkey[slave][port - 1][vidx];
920 outtab[vidx % 32] = cpu_to_be16(table[pidx]);
921 }
922 }
923 kfree(table);
924 return err;
925 }
926 if (smp->attr_id == IB_SMP_ATTR_PORT_INFO) {
927 /*get the slave specific caps:*/
928 /*do the command */
Or Gerlitz7c35ef42015-05-21 15:14:05 +0300929 smp->attr_mod = cpu_to_be32(port);
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000930 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Or Gerlitz7c35ef42015-05-21 15:14:05 +0300931 port, opcode_modifier,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000932 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
933 /* modify the response for slaves */
934 if (!err && slave != mlx4_master_func_num(dev)) {
935 u8 *state = outsmp->data + PORT_STATE_OFFSET;
936
937 *state = (*state & 0xf0) | vf_port_state(dev, port, slave);
938 slave_cap_mask = priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
939 memcpy(outsmp->data + PORT_CAPABILITY_LOCATION_IN_SMP, &slave_cap_mask, 4);
940 }
941 return err;
942 }
943 if (smp->attr_id == IB_SMP_ATTR_GUID_INFO) {
Yishai Hadase9a7ff32015-02-02 15:07:23 +0200944 __be64 guid = mlx4_get_admin_guid(dev, slave,
945 port);
946
947 /* set the PF admin guid to the FW/HW burned
948 * GUID, if it wasn't yet set
949 */
950 if (slave == 0 && guid == 0) {
951 smp->attr_mod = 0;
952 err = mlx4_cmd_box(dev,
953 inbox->dma,
954 outbox->dma,
955 vhcr->in_modifier,
956 opcode_modifier,
957 vhcr->op,
958 MLX4_CMD_TIME_CLASS_C,
959 MLX4_CMD_NATIVE);
960 if (err)
961 return err;
962 mlx4_set_admin_guid(dev,
963 *(__be64 *)outsmp->
964 data, slave, port);
965 } else {
966 memcpy(outsmp->data, &guid, 8);
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000967 }
Yishai Hadase9a7ff32015-02-02 15:07:23 +0200968
969 /* clean all other gids */
970 memset(outsmp->data + 8, 0, 56);
971 return 0;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000972 }
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000973 if (smp->attr_id == IB_SMP_ATTR_NODE_INFO) {
974 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Or Gerlitz7c35ef42015-05-21 15:14:05 +0300975 port, opcode_modifier,
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000976 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
977 if (!err) {
978 slave_node_guid = mlx4_get_slave_node_guid(dev, slave);
979 memcpy(outsmp->data + 12, &slave_node_guid, 8);
980 }
981 return err;
982 }
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000983 }
984 }
Jack Morgenstein97982f52014-05-29 16:31:02 +0300985
986 /* Non-privileged VFs are only allowed "host" view LID-routed 'Get' MADs.
987 * These are the MADs used by ib verbs (such as ib_query_gids).
988 */
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000989 if (slave != mlx4_master_func_num(dev) &&
Jack Morgenstein97982f52014-05-29 16:31:02 +0300990 !mlx4_vf_smi_enabled(dev, slave, port)) {
991 if (!(smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
992 smp->method == IB_MGMT_METHOD_GET) || network_view) {
993 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",
994 slave, smp->method, smp->mgmt_class,
995 network_view ? "Network" : "Host",
996 be16_to_cpu(smp->attr_id));
997 return -EPERM;
998 }
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000999 }
Jack Morgenstein97982f52014-05-29 16:31:02 +03001000
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001001 return mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Jack Morgenstein97982f52014-05-29 16:31:02 +03001002 vhcr->in_modifier, opcode_modifier,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001003 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
1004}
1005
Or Gerlitzb7475792014-03-27 14:02:02 +02001006static int mlx4_CMD_EPERM_wrapper(struct mlx4_dev *dev, int slave,
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001007 struct mlx4_vhcr *vhcr,
1008 struct mlx4_cmd_mailbox *inbox,
1009 struct mlx4_cmd_mailbox *outbox,
1010 struct mlx4_cmd_info *cmd)
1011{
1012 return -EPERM;
1013}
1014
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001015int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
1016 struct mlx4_vhcr *vhcr,
1017 struct mlx4_cmd_mailbox *inbox,
1018 struct mlx4_cmd_mailbox *outbox,
1019 struct mlx4_cmd_info *cmd)
1020{
1021 u64 in_param;
1022 u64 out_param;
1023 int err;
1024
1025 in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
1026 out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
1027 if (cmd->encode_slave_id) {
1028 in_param &= 0xffffffffffffff00ll;
1029 in_param |= slave;
1030 }
1031
1032 err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
1033 vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
1034 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
1035
1036 if (cmd->out_is_imm)
1037 vhcr->out_param = out_param;
1038
1039 return err;
1040}
1041
1042static struct mlx4_cmd_info cmd_info[] = {
1043 {
1044 .opcode = MLX4_CMD_QUERY_FW,
1045 .has_inbox = false,
1046 .has_outbox = true,
1047 .out_is_imm = false,
1048 .encode_slave_id = false,
1049 .verify = NULL,
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +00001050 .wrapper = mlx4_QUERY_FW_wrapper
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001051 },
1052 {
1053 .opcode = MLX4_CMD_QUERY_HCA,
1054 .has_inbox = false,
1055 .has_outbox = true,
1056 .out_is_imm = false,
1057 .encode_slave_id = false,
1058 .verify = NULL,
1059 .wrapper = NULL
1060 },
1061 {
1062 .opcode = MLX4_CMD_QUERY_DEV_CAP,
1063 .has_inbox = false,
1064 .has_outbox = true,
1065 .out_is_imm = false,
1066 .encode_slave_id = false,
1067 .verify = NULL,
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +00001068 .wrapper = mlx4_QUERY_DEV_CAP_wrapper
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001069 },
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001070 {
1071 .opcode = MLX4_CMD_QUERY_FUNC_CAP,
1072 .has_inbox = false,
1073 .has_outbox = true,
1074 .out_is_imm = false,
1075 .encode_slave_id = false,
1076 .verify = NULL,
1077 .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
1078 },
1079 {
1080 .opcode = MLX4_CMD_QUERY_ADAPTER,
1081 .has_inbox = false,
1082 .has_outbox = true,
1083 .out_is_imm = false,
1084 .encode_slave_id = false,
1085 .verify = NULL,
1086 .wrapper = NULL
1087 },
1088 {
1089 .opcode = MLX4_CMD_INIT_PORT,
1090 .has_inbox = false,
1091 .has_outbox = false,
1092 .out_is_imm = false,
1093 .encode_slave_id = false,
1094 .verify = NULL,
1095 .wrapper = mlx4_INIT_PORT_wrapper
1096 },
1097 {
1098 .opcode = MLX4_CMD_CLOSE_PORT,
1099 .has_inbox = false,
1100 .has_outbox = false,
1101 .out_is_imm = false,
1102 .encode_slave_id = false,
1103 .verify = NULL,
1104 .wrapper = mlx4_CLOSE_PORT_wrapper
1105 },
1106 {
1107 .opcode = MLX4_CMD_QUERY_PORT,
1108 .has_inbox = false,
1109 .has_outbox = true,
1110 .out_is_imm = false,
1111 .encode_slave_id = false,
1112 .verify = NULL,
1113 .wrapper = mlx4_QUERY_PORT_wrapper
1114 },
1115 {
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001116 .opcode = MLX4_CMD_SET_PORT,
1117 .has_inbox = true,
1118 .has_outbox = false,
1119 .out_is_imm = false,
1120 .encode_slave_id = false,
1121 .verify = NULL,
1122 .wrapper = mlx4_SET_PORT_wrapper
1123 },
1124 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001125 .opcode = MLX4_CMD_MAP_EQ,
1126 .has_inbox = false,
1127 .has_outbox = false,
1128 .out_is_imm = false,
1129 .encode_slave_id = false,
1130 .verify = NULL,
1131 .wrapper = mlx4_MAP_EQ_wrapper
1132 },
1133 {
1134 .opcode = MLX4_CMD_SW2HW_EQ,
1135 .has_inbox = true,
1136 .has_outbox = false,
1137 .out_is_imm = false,
1138 .encode_slave_id = true,
1139 .verify = NULL,
1140 .wrapper = mlx4_SW2HW_EQ_wrapper
1141 },
1142 {
1143 .opcode = MLX4_CMD_HW_HEALTH_CHECK,
1144 .has_inbox = false,
1145 .has_outbox = false,
1146 .out_is_imm = false,
1147 .encode_slave_id = false,
1148 .verify = NULL,
1149 .wrapper = NULL
1150 },
1151 {
1152 .opcode = MLX4_CMD_NOP,
1153 .has_inbox = false,
1154 .has_outbox = false,
1155 .out_is_imm = false,
1156 .encode_slave_id = false,
1157 .verify = NULL,
1158 .wrapper = NULL
1159 },
1160 {
Or Gerlitzd18f1412014-03-27 14:02:03 +02001161 .opcode = MLX4_CMD_CONFIG_DEV,
1162 .has_inbox = false,
Matan Barakd475c952014-11-02 16:26:17 +02001163 .has_outbox = true,
Or Gerlitzd18f1412014-03-27 14:02:03 +02001164 .out_is_imm = false,
1165 .encode_slave_id = false,
1166 .verify = NULL,
Matan Barakd475c952014-11-02 16:26:17 +02001167 .wrapper = mlx4_CONFIG_DEV_wrapper
Or Gerlitzd18f1412014-03-27 14:02:03 +02001168 },
1169 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001170 .opcode = MLX4_CMD_ALLOC_RES,
1171 .has_inbox = false,
1172 .has_outbox = false,
1173 .out_is_imm = true,
1174 .encode_slave_id = false,
1175 .verify = NULL,
1176 .wrapper = mlx4_ALLOC_RES_wrapper
1177 },
1178 {
1179 .opcode = MLX4_CMD_FREE_RES,
1180 .has_inbox = false,
1181 .has_outbox = false,
1182 .out_is_imm = false,
1183 .encode_slave_id = false,
1184 .verify = NULL,
1185 .wrapper = mlx4_FREE_RES_wrapper
1186 },
1187 {
1188 .opcode = MLX4_CMD_SW2HW_MPT,
1189 .has_inbox = true,
1190 .has_outbox = false,
1191 .out_is_imm = false,
1192 .encode_slave_id = true,
1193 .verify = NULL,
1194 .wrapper = mlx4_SW2HW_MPT_wrapper
1195 },
1196 {
1197 .opcode = MLX4_CMD_QUERY_MPT,
1198 .has_inbox = false,
1199 .has_outbox = true,
1200 .out_is_imm = false,
1201 .encode_slave_id = false,
1202 .verify = NULL,
1203 .wrapper = mlx4_QUERY_MPT_wrapper
1204 },
1205 {
1206 .opcode = MLX4_CMD_HW2SW_MPT,
1207 .has_inbox = false,
1208 .has_outbox = false,
1209 .out_is_imm = false,
1210 .encode_slave_id = false,
1211 .verify = NULL,
1212 .wrapper = mlx4_HW2SW_MPT_wrapper
1213 },
1214 {
1215 .opcode = MLX4_CMD_READ_MTT,
1216 .has_inbox = false,
1217 .has_outbox = true,
1218 .out_is_imm = false,
1219 .encode_slave_id = false,
1220 .verify = NULL,
1221 .wrapper = NULL
1222 },
1223 {
1224 .opcode = MLX4_CMD_WRITE_MTT,
1225 .has_inbox = true,
1226 .has_outbox = false,
1227 .out_is_imm = false,
1228 .encode_slave_id = false,
1229 .verify = NULL,
1230 .wrapper = mlx4_WRITE_MTT_wrapper
1231 },
1232 {
1233 .opcode = MLX4_CMD_SYNC_TPT,
1234 .has_inbox = true,
1235 .has_outbox = false,
1236 .out_is_imm = false,
1237 .encode_slave_id = false,
1238 .verify = NULL,
1239 .wrapper = NULL
1240 },
1241 {
1242 .opcode = MLX4_CMD_HW2SW_EQ,
1243 .has_inbox = false,
Jack Morgenstein30a5da52015-01-27 15:58:03 +02001244 .has_outbox = false,
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001245 .out_is_imm = false,
1246 .encode_slave_id = true,
1247 .verify = NULL,
1248 .wrapper = mlx4_HW2SW_EQ_wrapper
1249 },
1250 {
1251 .opcode = MLX4_CMD_QUERY_EQ,
1252 .has_inbox = false,
1253 .has_outbox = true,
1254 .out_is_imm = false,
1255 .encode_slave_id = true,
1256 .verify = NULL,
1257 .wrapper = mlx4_QUERY_EQ_wrapper
1258 },
1259 {
1260 .opcode = MLX4_CMD_SW2HW_CQ,
1261 .has_inbox = true,
1262 .has_outbox = false,
1263 .out_is_imm = false,
1264 .encode_slave_id = true,
1265 .verify = NULL,
1266 .wrapper = mlx4_SW2HW_CQ_wrapper
1267 },
1268 {
1269 .opcode = MLX4_CMD_HW2SW_CQ,
1270 .has_inbox = false,
1271 .has_outbox = false,
1272 .out_is_imm = false,
1273 .encode_slave_id = false,
1274 .verify = NULL,
1275 .wrapper = mlx4_HW2SW_CQ_wrapper
1276 },
1277 {
1278 .opcode = MLX4_CMD_QUERY_CQ,
1279 .has_inbox = false,
1280 .has_outbox = true,
1281 .out_is_imm = false,
1282 .encode_slave_id = false,
1283 .verify = NULL,
1284 .wrapper = mlx4_QUERY_CQ_wrapper
1285 },
1286 {
1287 .opcode = MLX4_CMD_MODIFY_CQ,
1288 .has_inbox = true,
1289 .has_outbox = false,
1290 .out_is_imm = true,
1291 .encode_slave_id = false,
1292 .verify = NULL,
1293 .wrapper = mlx4_MODIFY_CQ_wrapper
1294 },
1295 {
1296 .opcode = MLX4_CMD_SW2HW_SRQ,
1297 .has_inbox = true,
1298 .has_outbox = false,
1299 .out_is_imm = false,
1300 .encode_slave_id = true,
1301 .verify = NULL,
1302 .wrapper = mlx4_SW2HW_SRQ_wrapper
1303 },
1304 {
1305 .opcode = MLX4_CMD_HW2SW_SRQ,
1306 .has_inbox = false,
1307 .has_outbox = false,
1308 .out_is_imm = false,
1309 .encode_slave_id = false,
1310 .verify = NULL,
1311 .wrapper = mlx4_HW2SW_SRQ_wrapper
1312 },
1313 {
1314 .opcode = MLX4_CMD_QUERY_SRQ,
1315 .has_inbox = false,
1316 .has_outbox = true,
1317 .out_is_imm = false,
1318 .encode_slave_id = false,
1319 .verify = NULL,
1320 .wrapper = mlx4_QUERY_SRQ_wrapper
1321 },
1322 {
1323 .opcode = MLX4_CMD_ARM_SRQ,
1324 .has_inbox = false,
1325 .has_outbox = false,
1326 .out_is_imm = false,
1327 .encode_slave_id = false,
1328 .verify = NULL,
1329 .wrapper = mlx4_ARM_SRQ_wrapper
1330 },
1331 {
1332 .opcode = MLX4_CMD_RST2INIT_QP,
1333 .has_inbox = true,
1334 .has_outbox = false,
1335 .out_is_imm = false,
1336 .encode_slave_id = true,
1337 .verify = NULL,
1338 .wrapper = mlx4_RST2INIT_QP_wrapper
1339 },
1340 {
1341 .opcode = MLX4_CMD_INIT2INIT_QP,
1342 .has_inbox = true,
1343 .has_outbox = false,
1344 .out_is_imm = false,
1345 .encode_slave_id = false,
1346 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001347 .wrapper = mlx4_INIT2INIT_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001348 },
1349 {
1350 .opcode = MLX4_CMD_INIT2RTR_QP,
1351 .has_inbox = true,
1352 .has_outbox = false,
1353 .out_is_imm = false,
1354 .encode_slave_id = false,
1355 .verify = NULL,
1356 .wrapper = mlx4_INIT2RTR_QP_wrapper
1357 },
1358 {
1359 .opcode = MLX4_CMD_RTR2RTS_QP,
1360 .has_inbox = true,
1361 .has_outbox = false,
1362 .out_is_imm = false,
1363 .encode_slave_id = false,
1364 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001365 .wrapper = mlx4_RTR2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001366 },
1367 {
1368 .opcode = MLX4_CMD_RTS2RTS_QP,
1369 .has_inbox = true,
1370 .has_outbox = false,
1371 .out_is_imm = false,
1372 .encode_slave_id = false,
1373 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001374 .wrapper = mlx4_RTS2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001375 },
1376 {
1377 .opcode = MLX4_CMD_SQERR2RTS_QP,
1378 .has_inbox = true,
1379 .has_outbox = false,
1380 .out_is_imm = false,
1381 .encode_slave_id = false,
1382 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001383 .wrapper = mlx4_SQERR2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001384 },
1385 {
1386 .opcode = MLX4_CMD_2ERR_QP,
1387 .has_inbox = false,
1388 .has_outbox = false,
1389 .out_is_imm = false,
1390 .encode_slave_id = false,
1391 .verify = NULL,
1392 .wrapper = mlx4_GEN_QP_wrapper
1393 },
1394 {
1395 .opcode = MLX4_CMD_RTS2SQD_QP,
1396 .has_inbox = false,
1397 .has_outbox = false,
1398 .out_is_imm = false,
1399 .encode_slave_id = false,
1400 .verify = NULL,
1401 .wrapper = mlx4_GEN_QP_wrapper
1402 },
1403 {
1404 .opcode = MLX4_CMD_SQD2SQD_QP,
1405 .has_inbox = true,
1406 .has_outbox = false,
1407 .out_is_imm = false,
1408 .encode_slave_id = false,
1409 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001410 .wrapper = mlx4_SQD2SQD_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001411 },
1412 {
1413 .opcode = MLX4_CMD_SQD2RTS_QP,
1414 .has_inbox = true,
1415 .has_outbox = false,
1416 .out_is_imm = false,
1417 .encode_slave_id = false,
1418 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001419 .wrapper = mlx4_SQD2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001420 },
1421 {
1422 .opcode = MLX4_CMD_2RST_QP,
1423 .has_inbox = false,
1424 .has_outbox = false,
1425 .out_is_imm = false,
1426 .encode_slave_id = false,
1427 .verify = NULL,
1428 .wrapper = mlx4_2RST_QP_wrapper
1429 },
1430 {
1431 .opcode = MLX4_CMD_QUERY_QP,
1432 .has_inbox = false,
1433 .has_outbox = true,
1434 .out_is_imm = false,
1435 .encode_slave_id = false,
1436 .verify = NULL,
1437 .wrapper = mlx4_GEN_QP_wrapper
1438 },
1439 {
1440 .opcode = MLX4_CMD_SUSPEND_QP,
1441 .has_inbox = false,
1442 .has_outbox = false,
1443 .out_is_imm = false,
1444 .encode_slave_id = false,
1445 .verify = NULL,
1446 .wrapper = mlx4_GEN_QP_wrapper
1447 },
1448 {
1449 .opcode = MLX4_CMD_UNSUSPEND_QP,
1450 .has_inbox = false,
1451 .has_outbox = false,
1452 .out_is_imm = false,
1453 .encode_slave_id = false,
1454 .verify = NULL,
1455 .wrapper = mlx4_GEN_QP_wrapper
1456 },
1457 {
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001458 .opcode = MLX4_CMD_UPDATE_QP,
Matan Barakce8d9e02014-05-15 15:29:27 +03001459 .has_inbox = true,
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001460 .has_outbox = false,
1461 .out_is_imm = false,
1462 .encode_slave_id = false,
1463 .verify = NULL,
Matan Barakce8d9e02014-05-15 15:29:27 +03001464 .wrapper = mlx4_UPDATE_QP_wrapper
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001465 },
1466 {
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001467 .opcode = MLX4_CMD_GET_OP_REQ,
1468 .has_inbox = false,
1469 .has_outbox = false,
1470 .out_is_imm = false,
1471 .encode_slave_id = false,
1472 .verify = NULL,
Or Gerlitzb7475792014-03-27 14:02:02 +02001473 .wrapper = mlx4_CMD_EPERM_wrapper,
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001474 },
1475 {
Ido Shamay7e95bb92015-04-02 16:31:11 +03001476 .opcode = MLX4_CMD_ALLOCATE_VPP,
1477 .has_inbox = false,
1478 .has_outbox = true,
1479 .out_is_imm = false,
1480 .encode_slave_id = false,
1481 .verify = NULL,
1482 .wrapper = mlx4_CMD_EPERM_wrapper,
1483 },
1484 {
Ido Shamay1c291462015-04-02 16:31:12 +03001485 .opcode = MLX4_CMD_SET_VPORT_QOS,
1486 .has_inbox = false,
1487 .has_outbox = true,
1488 .out_is_imm = false,
1489 .encode_slave_id = false,
1490 .verify = NULL,
1491 .wrapper = mlx4_CMD_EPERM_wrapper,
1492 },
1493 {
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001494 .opcode = MLX4_CMD_CONF_SPECIAL_QP,
1495 .has_inbox = false,
1496 .has_outbox = false,
1497 .out_is_imm = false,
1498 .encode_slave_id = false,
1499 .verify = NULL, /* XXX verify: only demux can do this */
1500 .wrapper = NULL
1501 },
1502 {
1503 .opcode = MLX4_CMD_MAD_IFC,
1504 .has_inbox = true,
1505 .has_outbox = true,
1506 .out_is_imm = false,
1507 .encode_slave_id = false,
1508 .verify = NULL,
1509 .wrapper = mlx4_MAD_IFC_wrapper
1510 },
1511 {
Jack Morgenstein114840c2014-06-01 11:53:50 +03001512 .opcode = MLX4_CMD_MAD_DEMUX,
1513 .has_inbox = false,
1514 .has_outbox = false,
1515 .out_is_imm = false,
1516 .encode_slave_id = false,
1517 .verify = NULL,
1518 .wrapper = mlx4_CMD_EPERM_wrapper
1519 },
1520 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001521 .opcode = MLX4_CMD_QUERY_IF_STAT,
1522 .has_inbox = false,
1523 .has_outbox = true,
1524 .out_is_imm = false,
1525 .encode_slave_id = false,
1526 .verify = NULL,
1527 .wrapper = mlx4_QUERY_IF_STAT_wrapper
1528 },
Saeed Mahameedadbc7ac2014-10-27 11:37:37 +02001529 {
1530 .opcode = MLX4_CMD_ACCESS_REG,
1531 .has_inbox = true,
1532 .has_outbox = true,
1533 .out_is_imm = false,
1534 .encode_slave_id = false,
1535 .verify = NULL,
Saeed Mahameed6e806692014-11-02 16:26:13 +02001536 .wrapper = mlx4_ACCESS_REG_wrapper,
Saeed Mahameedadbc7ac2014-10-27 11:37:37 +02001537 },
Shani Michaelid237baa2015-03-05 20:16:12 +02001538 {
1539 .opcode = MLX4_CMD_CONGESTION_CTRL_OPCODE,
1540 .has_inbox = false,
1541 .has_outbox = false,
1542 .out_is_imm = false,
1543 .encode_slave_id = false,
1544 .verify = NULL,
1545 .wrapper = mlx4_CMD_EPERM_wrapper,
1546 },
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001547 /* Native multicast commands are not available for guests */
1548 {
1549 .opcode = MLX4_CMD_QP_ATTACH,
1550 .has_inbox = true,
1551 .has_outbox = false,
1552 .out_is_imm = false,
1553 .encode_slave_id = false,
1554 .verify = NULL,
1555 .wrapper = mlx4_QP_ATTACH_wrapper
1556 },
1557 {
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +00001558 .opcode = MLX4_CMD_PROMISC,
1559 .has_inbox = false,
1560 .has_outbox = false,
1561 .out_is_imm = false,
1562 .encode_slave_id = false,
1563 .verify = NULL,
1564 .wrapper = mlx4_PROMISC_wrapper
1565 },
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001566 /* Ethernet specific commands */
1567 {
1568 .opcode = MLX4_CMD_SET_VLAN_FLTR,
1569 .has_inbox = true,
1570 .has_outbox = false,
1571 .out_is_imm = false,
1572 .encode_slave_id = false,
1573 .verify = NULL,
1574 .wrapper = mlx4_SET_VLAN_FLTR_wrapper
1575 },
1576 {
1577 .opcode = MLX4_CMD_SET_MCAST_FLTR,
1578 .has_inbox = false,
1579 .has_outbox = false,
1580 .out_is_imm = false,
1581 .encode_slave_id = false,
1582 .verify = NULL,
1583 .wrapper = mlx4_SET_MCAST_FLTR_wrapper
1584 },
1585 {
1586 .opcode = MLX4_CMD_DUMP_ETH_STATS,
1587 .has_inbox = false,
1588 .has_outbox = true,
1589 .out_is_imm = false,
1590 .encode_slave_id = false,
1591 .verify = NULL,
1592 .wrapper = mlx4_DUMP_ETH_STATS_wrapper
1593 },
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +00001594 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001595 .opcode = MLX4_CMD_INFORM_FLR_DONE,
1596 .has_inbox = false,
1597 .has_outbox = false,
1598 .out_is_imm = false,
1599 .encode_slave_id = false,
1600 .verify = NULL,
1601 .wrapper = NULL
1602 },
Hadar Hen Zion8fcfb4d2012-07-05 04:03:45 +00001603 /* flow steering commands */
1604 {
1605 .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
1606 .has_inbox = true,
1607 .has_outbox = false,
1608 .out_is_imm = true,
1609 .encode_slave_id = false,
1610 .verify = NULL,
1611 .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
1612 },
1613 {
1614 .opcode = MLX4_QP_FLOW_STEERING_DETACH,
1615 .has_inbox = false,
1616 .has_outbox = false,
1617 .out_is_imm = false,
1618 .encode_slave_id = false,
1619 .verify = NULL,
1620 .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
1621 },
Matan Barak4de65802013-11-07 15:25:14 +02001622 {
1623 .opcode = MLX4_FLOW_STEERING_IB_UC_QP_RANGE,
1624 .has_inbox = false,
1625 .has_outbox = false,
1626 .out_is_imm = false,
1627 .encode_slave_id = false,
1628 .verify = NULL,
Or Gerlitzb7475792014-03-27 14:02:02 +02001629 .wrapper = mlx4_CMD_EPERM_wrapper
Matan Barak4de65802013-11-07 15:25:14 +02001630 },
Moni Shoua59e14e32015-02-03 16:48:32 +02001631 {
1632 .opcode = MLX4_CMD_VIRT_PORT_MAP,
1633 .has_inbox = false,
1634 .has_outbox = false,
1635 .out_is_imm = false,
1636 .encode_slave_id = false,
1637 .verify = NULL,
1638 .wrapper = mlx4_CMD_EPERM_wrapper
1639 },
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001640};
1641
1642static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
1643 struct mlx4_vhcr_cmd *in_vhcr)
1644{
1645 struct mlx4_priv *priv = mlx4_priv(dev);
1646 struct mlx4_cmd_info *cmd = NULL;
1647 struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
1648 struct mlx4_vhcr *vhcr;
1649 struct mlx4_cmd_mailbox *inbox = NULL;
1650 struct mlx4_cmd_mailbox *outbox = NULL;
1651 u64 in_param;
1652 u64 out_param;
1653 int ret = 0;
1654 int i;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001655 int err = 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001656
1657 /* Create sw representation of Virtual HCR */
1658 vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
1659 if (!vhcr)
1660 return -ENOMEM;
1661
1662 /* DMA in the vHCR */
1663 if (!in_vhcr) {
1664 ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1665 priv->mfunc.master.slave_state[slave].vhcr_dma,
1666 ALIGN(sizeof(struct mlx4_vhcr_cmd),
1667 MLX4_ACCESS_MEM_ALIGN), 1);
1668 if (ret) {
Yishai Hadas0cd93022015-01-25 16:59:43 +02001669 if (!(dev->persist->state &
1670 MLX4_DEVICE_STATE_INTERNAL_ERROR))
1671 mlx4_err(dev, "%s: Failed reading vhcr ret: 0x%x\n",
1672 __func__, ret);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001673 kfree(vhcr);
1674 return ret;
1675 }
1676 }
1677
1678 /* Fill SW VHCR fields */
1679 vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
1680 vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
1681 vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
1682 vhcr->token = be16_to_cpu(vhcr_cmd->token);
1683 vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
1684 vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
1685 vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
1686
1687 /* Lookup command */
1688 for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
1689 if (vhcr->op == cmd_info[i].opcode) {
1690 cmd = &cmd_info[i];
1691 break;
1692 }
1693 }
1694 if (!cmd) {
1695 mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
1696 vhcr->op, slave);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001697 vhcr_cmd->status = CMD_STAT_BAD_PARAM;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001698 goto out_status;
1699 }
1700
1701 /* Read inbox */
1702 if (cmd->has_inbox) {
1703 vhcr->in_param &= INBOX_MASK;
1704 inbox = mlx4_alloc_cmd_mailbox(dev);
1705 if (IS_ERR(inbox)) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001706 vhcr_cmd->status = CMD_STAT_BAD_SIZE;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001707 inbox = NULL;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001708 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001709 }
1710
Yishai Hadas0cd93022015-01-25 16:59:43 +02001711 ret = mlx4_ACCESS_MEM(dev, inbox->dma, slave,
1712 vhcr->in_param,
1713 MLX4_MAILBOX_SIZE, 1);
1714 if (ret) {
1715 if (!(dev->persist->state &
1716 MLX4_DEVICE_STATE_INTERNAL_ERROR))
1717 mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
1718 __func__, cmd->opcode);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001719 vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
1720 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001721 }
1722 }
1723
1724 /* Apply permission and bound checks if applicable */
1725 if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
Joe Perches1a91de22014-05-07 12:52:57 -07001726 mlx4_warn(dev, "Command:0x%x from slave: %d failed protection checks for resource_id:%d\n",
1727 vhcr->op, slave, vhcr->in_modifier);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001728 vhcr_cmd->status = CMD_STAT_BAD_OP;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001729 goto out_status;
1730 }
1731
1732 /* Allocate outbox */
1733 if (cmd->has_outbox) {
1734 outbox = mlx4_alloc_cmd_mailbox(dev);
1735 if (IS_ERR(outbox)) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001736 vhcr_cmd->status = CMD_STAT_BAD_SIZE;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001737 outbox = NULL;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001738 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001739 }
1740 }
1741
1742 /* Execute the command! */
1743 if (cmd->wrapper) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001744 err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
1745 cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001746 if (cmd->out_is_imm)
1747 vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1748 } else {
1749 in_param = cmd->has_inbox ? (u64) inbox->dma :
1750 vhcr->in_param;
1751 out_param = cmd->has_outbox ? (u64) outbox->dma :
1752 vhcr->out_param;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001753 err = __mlx4_cmd(dev, in_param, &out_param,
1754 cmd->out_is_imm, vhcr->in_modifier,
1755 vhcr->op_modifier, vhcr->op,
1756 MLX4_CMD_TIME_CLASS_A,
1757 MLX4_CMD_NATIVE);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001758
1759 if (cmd->out_is_imm) {
1760 vhcr->out_param = out_param;
1761 vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1762 }
1763 }
1764
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001765 if (err) {
Yishai Hadas0cd93022015-01-25 16:59:43 +02001766 if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR))
1767 mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with error:%d, status %d\n",
1768 vhcr->op, slave, vhcr->errno, err);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001769 vhcr_cmd->status = mlx4_errno_to_status(err);
1770 goto out_status;
1771 }
1772
1773
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001774 /* Write outbox if command completed successfully */
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001775 if (cmd->has_outbox && !vhcr_cmd->status) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001776 ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
1777 vhcr->out_param,
1778 MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
1779 if (ret) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001780 /* If we failed to write back the outbox after the
1781 *command was successfully executed, we must fail this
1782 * slave, as it is now in undefined state */
Yishai Hadas0cd93022015-01-25 16:59:43 +02001783 if (!(dev->persist->state &
1784 MLX4_DEVICE_STATE_INTERNAL_ERROR))
1785 mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001786 goto out;
1787 }
1788 }
1789
1790out_status:
1791 /* DMA back vhcr result */
1792 if (!in_vhcr) {
1793 ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1794 priv->mfunc.master.slave_state[slave].vhcr_dma,
1795 ALIGN(sizeof(struct mlx4_vhcr),
1796 MLX4_ACCESS_MEM_ALIGN),
1797 MLX4_CMD_WRAPPED);
1798 if (ret)
1799 mlx4_err(dev, "%s:Failed writing vhcr result\n",
1800 __func__);
1801 else if (vhcr->e_bit &&
1802 mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
Joe Perches1a91de22014-05-07 12:52:57 -07001803 mlx4_warn(dev, "Failed to generate command completion eqe for slave %d\n",
1804 slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001805 }
1806
1807out:
1808 kfree(vhcr);
1809 mlx4_free_cmd_mailbox(dev, inbox);
1810 mlx4_free_cmd_mailbox(dev, outbox);
1811 return ret;
1812}
1813
Jingoo Hanf0946682013-08-05 18:04:51 +09001814static int mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv *priv,
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001815 int slave, int port)
1816{
1817 struct mlx4_vport_oper_state *vp_oper;
1818 struct mlx4_vport_state *vp_admin;
1819 struct mlx4_vf_immed_vlan_work *work;
Rony Efraim0a6eac22013-06-27 19:05:22 +03001820 struct mlx4_dev *dev = &(priv->dev);
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001821 int err;
1822 int admin_vlan_ix = NO_INDX;
1823
1824 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1825 vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
1826
1827 if (vp_oper->state.default_vlan == vp_admin->default_vlan &&
Rony Efraim0a6eac22013-06-27 19:05:22 +03001828 vp_oper->state.default_qos == vp_admin->default_qos &&
Ido Shamay08068cd2015-04-02 16:31:15 +03001829 vp_oper->state.link_state == vp_admin->link_state &&
1830 vp_oper->state.qos_vport == vp_admin->qos_vport)
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001831 return 0;
1832
Rony Efraim0a6eac22013-06-27 19:05:22 +03001833 if (!(priv->mfunc.master.slave_state[slave].active &&
Rony Efraimf0f829b2013-11-07 12:19:51 +02001834 dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)) {
Rony Efraim0a6eac22013-06-27 19:05:22 +03001835 /* even if the UPDATE_QP command isn't supported, we still want
1836 * to set this VF link according to the admin directive
1837 */
1838 vp_oper->state.link_state = vp_admin->link_state;
1839 return -1;
1840 }
1841
1842 mlx4_dbg(dev, "updating immediately admin params slave %d port %d\n",
1843 slave, port);
Joe Perches1a91de22014-05-07 12:52:57 -07001844 mlx4_dbg(dev, "vlan %d QoS %d link down %d\n",
1845 vp_admin->default_vlan, vp_admin->default_qos,
1846 vp_admin->link_state);
Rony Efraim0a6eac22013-06-27 19:05:22 +03001847
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001848 work = kzalloc(sizeof(*work), GFP_KERNEL);
1849 if (!work)
1850 return -ENOMEM;
1851
1852 if (vp_oper->state.default_vlan != vp_admin->default_vlan) {
Rony Efraimf0f829b2013-11-07 12:19:51 +02001853 if (MLX4_VGT != vp_admin->default_vlan) {
1854 err = __mlx4_register_vlan(&priv->dev, port,
1855 vp_admin->default_vlan,
1856 &admin_vlan_ix);
1857 if (err) {
1858 kfree(work);
Joe Perches1a91de22014-05-07 12:52:57 -07001859 mlx4_warn(&priv->dev,
Rony Efraimf0f829b2013-11-07 12:19:51 +02001860 "No vlan resources slave %d, port %d\n",
1861 slave, port);
1862 return err;
1863 }
1864 } else {
1865 admin_vlan_ix = NO_INDX;
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001866 }
1867 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_VLAN;
Joe Perches1a91de22014-05-07 12:52:57 -07001868 mlx4_dbg(&priv->dev,
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001869 "alloc vlan %d idx %d slave %d port %d\n",
1870 (int)(vp_admin->default_vlan),
1871 admin_vlan_ix, slave, port);
1872 }
1873
1874 /* save original vlan ix and vlan id */
1875 work->orig_vlan_id = vp_oper->state.default_vlan;
1876 work->orig_vlan_ix = vp_oper->vlan_idx;
1877
1878 /* handle new qos */
1879 if (vp_oper->state.default_qos != vp_admin->default_qos)
1880 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_QOS;
1881
1882 if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN)
1883 vp_oper->vlan_idx = admin_vlan_ix;
1884
1885 vp_oper->state.default_vlan = vp_admin->default_vlan;
1886 vp_oper->state.default_qos = vp_admin->default_qos;
Rony Efraim0a6eac22013-06-27 19:05:22 +03001887 vp_oper->state.link_state = vp_admin->link_state;
Ido Shamay08068cd2015-04-02 16:31:15 +03001888 vp_oper->state.qos_vport = vp_admin->qos_vport;
Rony Efraim0a6eac22013-06-27 19:05:22 +03001889
1890 if (vp_admin->link_state == IFLA_VF_LINK_STATE_DISABLE)
1891 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE;
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001892
1893 /* iterate over QPs owned by this slave, using UPDATE_QP */
1894 work->port = port;
1895 work->slave = slave;
1896 work->qos = vp_oper->state.default_qos;
Ido Shamay08068cd2015-04-02 16:31:15 +03001897 work->qos_vport = vp_oper->state.qos_vport;
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001898 work->vlan_id = vp_oper->state.default_vlan;
1899 work->vlan_ix = vp_oper->vlan_idx;
1900 work->priv = priv;
1901 INIT_WORK(&work->work, mlx4_vf_immed_vlan_work_handler);
1902 queue_work(priv->mfunc.master.comm_wq, &work->work);
1903
1904 return 0;
1905}
1906
Ido Shamay666672d2015-04-02 16:31:14 +03001907static void mlx4_set_default_port_qos(struct mlx4_dev *dev, int port)
1908{
1909 struct mlx4_qos_manager *port_qos_ctl;
1910 struct mlx4_priv *priv = mlx4_priv(dev);
1911
1912 port_qos_ctl = &priv->mfunc.master.qos_ctl[port];
1913 bitmap_zero(port_qos_ctl->priority_bm, MLX4_NUM_UP);
1914
1915 /* Enable only default prio at PF init routine */
1916 set_bit(MLX4_DEFAULT_QOS_PRIO, port_qos_ctl->priority_bm);
1917}
1918
1919static void mlx4_allocate_port_vpps(struct mlx4_dev *dev, int port)
1920{
1921 int i;
1922 int err;
1923 int num_vfs;
1924 u16 availible_vpp;
1925 u8 vpp_param[MLX4_NUM_UP];
1926 struct mlx4_qos_manager *port_qos;
1927 struct mlx4_priv *priv = mlx4_priv(dev);
1928
1929 err = mlx4_ALLOCATE_VPP_get(dev, port, &availible_vpp, vpp_param);
1930 if (err) {
1931 mlx4_info(dev, "Failed query availible VPPs\n");
1932 return;
1933 }
1934
1935 port_qos = &priv->mfunc.master.qos_ctl[port];
1936 num_vfs = (availible_vpp /
1937 bitmap_weight(port_qos->priority_bm, MLX4_NUM_UP));
1938
1939 for (i = 0; i < MLX4_NUM_UP; i++) {
1940 if (test_bit(i, port_qos->priority_bm))
1941 vpp_param[i] = num_vfs;
1942 }
1943
1944 err = mlx4_ALLOCATE_VPP_set(dev, port, vpp_param);
1945 if (err) {
1946 mlx4_info(dev, "Failed allocating VPPs\n");
1947 return;
1948 }
1949
1950 /* Query actual allocated VPP, just to make sure */
1951 err = mlx4_ALLOCATE_VPP_get(dev, port, &availible_vpp, vpp_param);
1952 if (err) {
1953 mlx4_info(dev, "Failed query availible VPPs\n");
1954 return;
1955 }
1956
1957 port_qos->num_of_qos_vfs = num_vfs;
1958 mlx4_dbg(dev, "Port %d Availible VPPs %d\n", port, availible_vpp);
1959
1960 for (i = 0; i < MLX4_NUM_UP; i++)
1961 mlx4_dbg(dev, "Port %d UP %d Allocated %d VPPs\n", port, i,
1962 vpp_param[i]);
1963}
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001964
Rony Efraim0eb62b92013-04-25 05:22:26 +00001965static int mlx4_master_activate_admin_state(struct mlx4_priv *priv, int slave)
1966{
Rony Efraim3f7fb022013-04-25 05:22:28 +00001967 int port, err;
1968 struct mlx4_vport_state *vp_admin;
1969 struct mlx4_vport_oper_state *vp_oper;
Matan Barak449fc482014-03-19 18:11:52 +02001970 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
1971 &priv->dev, slave);
1972 int min_port = find_first_bit(actv_ports.ports,
1973 priv->dev.caps.num_ports) + 1;
1974 int max_port = min_port - 1 +
1975 bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
Rony Efraim3f7fb022013-04-25 05:22:28 +00001976
Matan Barak449fc482014-03-19 18:11:52 +02001977 for (port = min_port; port <= max_port; port++) {
1978 if (!test_bit(port - 1, actv_ports.ports))
1979 continue;
Jack Morgenstein99ec41d2014-05-29 16:31:03 +03001980 priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
1981 priv->mfunc.master.vf_admin[slave].enable_smi[port];
Rony Efraim3f7fb022013-04-25 05:22:28 +00001982 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1983 vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
1984 vp_oper->state = *vp_admin;
1985 if (MLX4_VGT != vp_admin->default_vlan) {
1986 err = __mlx4_register_vlan(&priv->dev, port,
1987 vp_admin->default_vlan, &(vp_oper->vlan_idx));
1988 if (err) {
1989 vp_oper->vlan_idx = NO_INDX;
Joe Perches1a91de22014-05-07 12:52:57 -07001990 mlx4_warn(&priv->dev,
Masanari Iida1a84db52014-08-29 23:37:33 +09001991 "No vlan resources slave %d, port %d\n",
Rony Efraim3f7fb022013-04-25 05:22:28 +00001992 slave, port);
1993 return err;
1994 }
Joe Perches1a91de22014-05-07 12:52:57 -07001995 mlx4_dbg(&priv->dev, "alloc vlan %d idx %d slave %d port %d\n",
Rony Efraim3f7fb022013-04-25 05:22:28 +00001996 (int)(vp_oper->state.default_vlan),
1997 vp_oper->vlan_idx, slave, port);
1998 }
Rony Efraime6b6a232013-04-25 05:22:29 +00001999 if (vp_admin->spoofchk) {
2000 vp_oper->mac_idx = __mlx4_register_mac(&priv->dev,
2001 port,
2002 vp_admin->mac);
2003 if (0 > vp_oper->mac_idx) {
2004 err = vp_oper->mac_idx;
2005 vp_oper->mac_idx = NO_INDX;
Joe Perches1a91de22014-05-07 12:52:57 -07002006 mlx4_warn(&priv->dev,
Masanari Iida1a84db52014-08-29 23:37:33 +09002007 "No mac resources slave %d, port %d\n",
Rony Efraime6b6a232013-04-25 05:22:29 +00002008 slave, port);
2009 return err;
2010 }
Joe Perches1a91de22014-05-07 12:52:57 -07002011 mlx4_dbg(&priv->dev, "alloc mac %llx idx %d slave %d port %d\n",
Rony Efraime6b6a232013-04-25 05:22:29 +00002012 vp_oper->state.mac, vp_oper->mac_idx, slave, port);
2013 }
Rony Efraim0eb62b92013-04-25 05:22:26 +00002014 }
2015 return 0;
2016}
2017
Rony Efraim3f7fb022013-04-25 05:22:28 +00002018static void mlx4_master_deactivate_admin_state(struct mlx4_priv *priv, int slave)
2019{
2020 int port;
2021 struct mlx4_vport_oper_state *vp_oper;
Matan Barak449fc482014-03-19 18:11:52 +02002022 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
2023 &priv->dev, slave);
2024 int min_port = find_first_bit(actv_ports.ports,
2025 priv->dev.caps.num_ports) + 1;
2026 int max_port = min_port - 1 +
2027 bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002028
Matan Barak449fc482014-03-19 18:11:52 +02002029
2030 for (port = min_port; port <= max_port; port++) {
2031 if (!test_bit(port - 1, actv_ports.ports))
2032 continue;
Jack Morgenstein99ec41d2014-05-29 16:31:03 +03002033 priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
2034 MLX4_VF_SMI_DISABLED;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002035 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
2036 if (NO_INDX != vp_oper->vlan_idx) {
2037 __mlx4_unregister_vlan(&priv->dev,
Jack Morgenstein2009d002013-11-03 10:03:19 +02002038 port, vp_oper->state.default_vlan);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002039 vp_oper->vlan_idx = NO_INDX;
2040 }
Rony Efraime6b6a232013-04-25 05:22:29 +00002041 if (NO_INDX != vp_oper->mac_idx) {
Jack Morgensteinc32b7df2013-11-03 10:04:07 +02002042 __mlx4_unregister_mac(&priv->dev, port, vp_oper->state.mac);
Rony Efraime6b6a232013-04-25 05:22:29 +00002043 vp_oper->mac_idx = NO_INDX;
2044 }
Rony Efraim3f7fb022013-04-25 05:22:28 +00002045 }
2046 return;
2047}
2048
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002049static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
2050 u16 param, u8 toggle)
2051{
2052 struct mlx4_priv *priv = mlx4_priv(dev);
2053 struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
2054 u32 reply;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002055 u8 is_going_down = 0;
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002056 int i;
Jack Morgenstein311f8132012-11-27 16:24:30 +00002057 unsigned long flags;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002058
2059 slave_state[slave].comm_toggle ^= 1;
2060 reply = (u32) slave_state[slave].comm_toggle << 31;
2061 if (toggle != slave_state[slave].comm_toggle) {
Joe Perches1a91de22014-05-07 12:52:57 -07002062 mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER STATE COMPROMISED ***\n",
2063 toggle, slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002064 goto reset_slave;
2065 }
2066 if (cmd == MLX4_COMM_CMD_RESET) {
2067 mlx4_warn(dev, "Received reset from slave:%d\n", slave);
2068 slave_state[slave].active = false;
Jack Morgenstein2c957ff2013-11-03 10:03:21 +02002069 slave_state[slave].old_vlan_api = false;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002070 mlx4_master_deactivate_admin_state(priv, slave);
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002071 for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
2072 slave_state[slave].event_eq[i].eqn = -1;
2073 slave_state[slave].event_eq[i].token = 0;
2074 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002075 /*check if we are in the middle of FLR process,
2076 if so return "retry" status to the slave*/
Or Gerlitz162344e2012-05-15 10:34:57 +00002077 if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002078 goto inform_slave_state;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002079
Jack Morgensteinfc065732012-08-03 08:40:42 +00002080 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
2081
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002082 /* write the version in the event field */
2083 reply |= mlx4_comm_get_version();
2084
2085 goto reset_slave;
2086 }
2087 /*command from slave in the middle of FLR*/
2088 if (cmd != MLX4_COMM_CMD_RESET &&
2089 MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
Joe Perches1a91de22014-05-07 12:52:57 -07002090 mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) in the middle of FLR\n",
2091 slave, cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002092 return;
2093 }
2094
2095 switch (cmd) {
2096 case MLX4_COMM_CMD_VHCR0:
2097 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
2098 goto reset_slave;
2099 slave_state[slave].vhcr_dma = ((u64) param) << 48;
2100 priv->mfunc.master.slave_state[slave].cookie = 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002101 break;
2102 case MLX4_COMM_CMD_VHCR1:
2103 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
2104 goto reset_slave;
2105 slave_state[slave].vhcr_dma |= ((u64) param) << 32;
2106 break;
2107 case MLX4_COMM_CMD_VHCR2:
2108 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
2109 goto reset_slave;
2110 slave_state[slave].vhcr_dma |= ((u64) param) << 16;
2111 break;
2112 case MLX4_COMM_CMD_VHCR_EN:
2113 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
2114 goto reset_slave;
2115 slave_state[slave].vhcr_dma |= param;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002116 if (mlx4_master_activate_admin_state(priv, slave))
2117 goto reset_slave;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002118 slave_state[slave].active = true;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002119 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002120 break;
2121 case MLX4_COMM_CMD_VHCR_POST:
2122 if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
Yishai Hadas55ad3592015-01-25 16:59:42 +02002123 (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST)) {
2124 mlx4_warn(dev, "slave:%d is out of sync, cmd=0x%x, last command=0x%x, reset is needed\n",
2125 slave, cmd, slave_state[slave].last_cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002126 goto reset_slave;
Yishai Hadas55ad3592015-01-25 16:59:42 +02002127 }
Roland Dreierf3d4c892012-09-25 21:24:07 -07002128
2129 mutex_lock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002130 if (mlx4_master_process_vhcr(dev, slave, NULL)) {
Joe Perches1a91de22014-05-07 12:52:57 -07002131 mlx4_err(dev, "Failed processing vhcr for slave:%d, resetting slave\n",
2132 slave);
Roland Dreierf3d4c892012-09-25 21:24:07 -07002133 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002134 goto reset_slave;
2135 }
Roland Dreierf3d4c892012-09-25 21:24:07 -07002136 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002137 break;
2138 default:
2139 mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
2140 goto reset_slave;
2141 }
Jack Morgenstein311f8132012-11-27 16:24:30 +00002142 spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002143 if (!slave_state[slave].is_slave_going_down)
2144 slave_state[slave].last_cmd = cmd;
2145 else
2146 is_going_down = 1;
Jack Morgenstein311f8132012-11-27 16:24:30 +00002147 spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002148 if (is_going_down) {
Joe Perches1a91de22014-05-07 12:52:57 -07002149 mlx4_warn(dev, "Slave is going down aborting command(%d) executing from slave:%d\n",
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002150 cmd, slave);
2151 return;
2152 }
2153 __raw_writel((__force u32) cpu_to_be32(reply),
2154 &priv->mfunc.comm[slave].slave_read);
2155 mmiowb();
2156
2157 return;
2158
2159reset_slave:
Eli Cohenc82e9aa2011-12-13 04:15:24 +00002160 /* cleanup any slave resources */
Yishai Hadas55ad3592015-01-25 16:59:42 +02002161 if (dev->persist->interface_state & MLX4_INTERFACE_STATE_UP)
2162 mlx4_delete_all_resources_for_slave(dev, slave);
2163
2164 if (cmd != MLX4_COMM_CMD_RESET) {
2165 mlx4_warn(dev, "Turn on internal error to force reset, slave=%d, cmd=0x%x\n",
2166 slave, cmd);
2167 /* Turn on internal error letting slave reset itself immeditaly,
2168 * otherwise it might take till timeout on command is passed
2169 */
2170 reply |= ((u32)COMM_CHAN_EVENT_INTERNAL_ERR);
2171 }
2172
Jack Morgenstein311f8132012-11-27 16:24:30 +00002173 spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002174 if (!slave_state[slave].is_slave_going_down)
2175 slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
Jack Morgenstein311f8132012-11-27 16:24:30 +00002176 spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002177 /*with slave in the middle of flr, no need to clean resources again.*/
2178inform_slave_state:
2179 memset(&slave_state[slave].event_eq, 0,
2180 sizeof(struct mlx4_slave_event_eq_info));
2181 __raw_writel((__force u32) cpu_to_be32(reply),
2182 &priv->mfunc.comm[slave].slave_read);
2183 wmb();
2184}
2185
2186/* master command processing */
2187void mlx4_master_comm_channel(struct work_struct *work)
2188{
2189 struct mlx4_mfunc_master_ctx *master =
2190 container_of(work,
2191 struct mlx4_mfunc_master_ctx,
2192 comm_work);
2193 struct mlx4_mfunc *mfunc =
2194 container_of(master, struct mlx4_mfunc, master);
2195 struct mlx4_priv *priv =
2196 container_of(mfunc, struct mlx4_priv, mfunc);
2197 struct mlx4_dev *dev = &priv->dev;
2198 __be32 *bit_vec;
2199 u32 comm_cmd;
2200 u32 vec;
2201 int i, j, slave;
2202 int toggle;
2203 int served = 0;
2204 int reported = 0;
2205 u32 slt;
2206
2207 bit_vec = master->comm_arm_bit_vector;
2208 for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
2209 vec = be32_to_cpu(bit_vec[i]);
2210 for (j = 0; j < 32; j++) {
2211 if (!(vec & (1 << j)))
2212 continue;
2213 ++reported;
2214 slave = (i * 32) + j;
2215 comm_cmd = swab32(readl(
2216 &mfunc->comm[slave].slave_write));
2217 slt = swab32(readl(&mfunc->comm[slave].slave_read))
2218 >> 31;
2219 toggle = comm_cmd >> 31;
2220 if (toggle != slt) {
2221 if (master->slave_state[slave].comm_toggle
2222 != slt) {
Amir Vadaic20862c2014-05-22 15:55:40 +03002223 pr_info("slave %d out of sync. read toggle %d, state toggle %d. Resynching.\n",
2224 slave, slt,
2225 master->slave_state[slave].comm_toggle);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002226 master->slave_state[slave].comm_toggle =
2227 slt;
2228 }
2229 mlx4_master_do_cmd(dev, slave,
2230 comm_cmd >> 16 & 0xff,
2231 comm_cmd & 0xffff, toggle);
2232 ++served;
2233 }
2234 }
2235 }
2236
2237 if (reported && reported != served)
Joe Perches1a91de22014-05-07 12:52:57 -07002238 mlx4_warn(dev, "Got command event with bitmask from %d slaves but %d were served\n",
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002239 reported, served);
2240
2241 if (mlx4_ARM_COMM_CHANNEL(dev))
2242 mlx4_warn(dev, "Failed to arm comm channel events\n");
2243}
2244
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002245static int sync_toggles(struct mlx4_dev *dev)
2246{
2247 struct mlx4_priv *priv = mlx4_priv(dev);
Yishai Hadas55ad3592015-01-25 16:59:42 +02002248 u32 wr_toggle;
2249 u32 rd_toggle;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002250 unsigned long end;
2251
Yishai Hadas55ad3592015-01-25 16:59:42 +02002252 wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write));
2253 if (wr_toggle == 0xffffffff)
2254 end = jiffies + msecs_to_jiffies(30000);
2255 else
2256 end = jiffies + msecs_to_jiffies(5000);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002257
2258 while (time_before(jiffies, end)) {
Yishai Hadas55ad3592015-01-25 16:59:42 +02002259 rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read));
2260 if (wr_toggle == 0xffffffff || rd_toggle == 0xffffffff) {
2261 /* PCI might be offline */
2262 msleep(100);
2263 wr_toggle = swab32(readl(&priv->mfunc.comm->
2264 slave_write));
2265 continue;
2266 }
2267
2268 if (rd_toggle >> 31 == wr_toggle >> 31) {
2269 priv->cmd.comm_toggle = rd_toggle >> 31;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002270 return 0;
2271 }
2272
2273 cond_resched();
2274 }
2275
2276 /*
2277 * we could reach here if for example the previous VM using this
2278 * function misbehaved and left the channel with unsynced state. We
2279 * should fix this here and give this VM a chance to use a properly
2280 * synced channel
2281 */
2282 mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
2283 __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
2284 __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
2285 priv->cmd.comm_toggle = 0;
2286
2287 return 0;
2288}
2289
2290int mlx4_multi_func_init(struct mlx4_dev *dev)
2291{
2292 struct mlx4_priv *priv = mlx4_priv(dev);
2293 struct mlx4_slave_state *s_state;
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002294 int i, j, err, port;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002295
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002296 if (mlx4_is_master(dev))
2297 priv->mfunc.comm =
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002298 ioremap(pci_resource_start(dev->persist->pdev,
2299 priv->fw.comm_bar) +
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002300 priv->fw.comm_base, MLX4_COMM_PAGESIZE);
2301 else
2302 priv->mfunc.comm =
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002303 ioremap(pci_resource_start(dev->persist->pdev, 2) +
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002304 MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
2305 if (!priv->mfunc.comm) {
Joe Perches1a91de22014-05-07 12:52:57 -07002306 mlx4_err(dev, "Couldn't map communication vector\n");
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002307 goto err_vhcr;
2308 }
2309
2310 if (mlx4_is_master(dev)) {
Ido Shamay4abccb62015-04-02 16:31:09 +03002311 struct mlx4_vf_oper_state *vf_oper;
2312 struct mlx4_vf_admin_state *vf_admin;
2313
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002314 priv->mfunc.master.slave_state =
2315 kzalloc(dev->num_slaves *
2316 sizeof(struct mlx4_slave_state), GFP_KERNEL);
2317 if (!priv->mfunc.master.slave_state)
2318 goto err_comm;
2319
Rony Efraim0eb62b92013-04-25 05:22:26 +00002320 priv->mfunc.master.vf_admin =
2321 kzalloc(dev->num_slaves *
2322 sizeof(struct mlx4_vf_admin_state), GFP_KERNEL);
2323 if (!priv->mfunc.master.vf_admin)
2324 goto err_comm_admin;
2325
2326 priv->mfunc.master.vf_oper =
2327 kzalloc(dev->num_slaves *
2328 sizeof(struct mlx4_vf_oper_state), GFP_KERNEL);
2329 if (!priv->mfunc.master.vf_oper)
2330 goto err_comm_oper;
2331
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002332 for (i = 0; i < dev->num_slaves; ++i) {
Ido Shamay4abccb62015-04-02 16:31:09 +03002333 vf_admin = &priv->mfunc.master.vf_admin[i];
2334 vf_oper = &priv->mfunc.master.vf_oper[i];
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002335 s_state = &priv->mfunc.master.slave_state[i];
2336 s_state->last_cmd = MLX4_COMM_CMD_RESET;
Jack Morgensteinbffb0232015-03-24 15:18:39 +02002337 mutex_init(&priv->mfunc.master.gen_eqe_mutex[i]);
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002338 for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
2339 s_state->event_eq[j].eqn = -1;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002340 __raw_writel((__force u32) 0,
2341 &priv->mfunc.comm[i].slave_write);
2342 __raw_writel((__force u32) 0,
2343 &priv->mfunc.comm[i].slave_read);
2344 mmiowb();
2345 for (port = 1; port <= MLX4_MAX_PORTS; port++) {
Ido Shamay4abccb62015-04-02 16:31:09 +03002346 struct mlx4_vport_state *admin_vport;
2347 struct mlx4_vport_state *oper_vport;
2348
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002349 s_state->vlan_filter[port] =
2350 kzalloc(sizeof(struct mlx4_vlan_fltr),
2351 GFP_KERNEL);
2352 if (!s_state->vlan_filter[port]) {
2353 if (--port)
2354 kfree(s_state->vlan_filter[port]);
2355 goto err_slaves;
2356 }
Ido Shamay4abccb62015-04-02 16:31:09 +03002357
2358 admin_vport = &vf_admin->vport[port];
2359 oper_vport = &vf_oper->vport[port].state;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002360 INIT_LIST_HEAD(&s_state->mcast_filters[port]);
Ido Shamay4abccb62015-04-02 16:31:09 +03002361 admin_vport->default_vlan = MLX4_VGT;
2362 oper_vport->default_vlan = MLX4_VGT;
Ido Shamay08068cd2015-04-02 16:31:15 +03002363 admin_vport->qos_vport =
2364 MLX4_VPP_DEFAULT_VPORT;
2365 oper_vport->qos_vport = MLX4_VPP_DEFAULT_VPORT;
Ido Shamay4abccb62015-04-02 16:31:09 +03002366 vf_oper->vport[port].vlan_idx = NO_INDX;
2367 vf_oper->vport[port].mac_idx = NO_INDX;
Yishai Hadasfb517a42015-03-03 11:23:32 +02002368 mlx4_set_random_admin_guid(dev, i, port);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002369 }
2370 spin_lock_init(&s_state->lock);
2371 }
2372
Ido Shamay666672d2015-04-02 16:31:14 +03002373 if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP) {
2374 for (port = 1; port <= dev->caps.num_ports; port++) {
2375 if (mlx4_is_eth(dev, port)) {
2376 mlx4_set_default_port_qos(dev, port);
2377 mlx4_allocate_port_vpps(dev, port);
2378 }
2379 }
2380 }
2381
Or Gerlitz08ff3232012-10-21 14:59:24 +00002382 memset(&priv->mfunc.master.cmd_eqe, 0, dev->caps.eqe_size);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002383 priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
2384 INIT_WORK(&priv->mfunc.master.comm_work,
2385 mlx4_master_comm_channel);
2386 INIT_WORK(&priv->mfunc.master.slave_event_work,
2387 mlx4_gen_slave_eqe);
2388 INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
2389 mlx4_master_handle_slave_flr);
2390 spin_lock_init(&priv->mfunc.master.slave_state_lock);
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00002391 spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002392 priv->mfunc.master.comm_wq =
2393 create_singlethread_workqueue("mlx4_comm");
2394 if (!priv->mfunc.master.comm_wq)
2395 goto err_slaves;
2396
2397 if (mlx4_init_resource_tracker(dev))
2398 goto err_thread;
2399
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002400 } else {
2401 err = sync_toggles(dev);
2402 if (err) {
2403 mlx4_err(dev, "Couldn't sync toggles\n");
2404 goto err_comm;
2405 }
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002406 }
2407 return 0;
2408
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002409err_thread:
2410 flush_workqueue(priv->mfunc.master.comm_wq);
2411 destroy_workqueue(priv->mfunc.master.comm_wq);
2412err_slaves:
2413 while (--i) {
2414 for (port = 1; port <= MLX4_MAX_PORTS; port++)
2415 kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
2416 }
Rony Efraim0eb62b92013-04-25 05:22:26 +00002417 kfree(priv->mfunc.master.vf_oper);
2418err_comm_oper:
2419 kfree(priv->mfunc.master.vf_admin);
2420err_comm_admin:
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002421 kfree(priv->mfunc.master.slave_state);
2422err_comm:
2423 iounmap(priv->mfunc.comm);
2424err_vhcr:
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002425 dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
2426 priv->mfunc.vhcr,
2427 priv->mfunc.vhcr_dma);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002428 priv->mfunc.vhcr = NULL;
2429 return -ENOMEM;
2430}
2431
Roland Dreier225c7b12007-05-08 18:00:38 -07002432int mlx4_cmd_init(struct mlx4_dev *dev)
2433{
2434 struct mlx4_priv *priv = mlx4_priv(dev);
Matan Barakffc39f62014-11-13 14:45:29 +02002435 int flags = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002436
Matan Barakffc39f62014-11-13 14:45:29 +02002437 if (!priv->cmd.initialized) {
Matan Barakffc39f62014-11-13 14:45:29 +02002438 mutex_init(&priv->cmd.slave_cmd_mutex);
2439 sema_init(&priv->cmd.poll_sem, 1);
2440 priv->cmd.use_events = 0;
2441 priv->cmd.toggle = 1;
2442 priv->cmd.initialized = 1;
2443 flags |= MLX4_CMD_CLEANUP_STRUCT;
2444 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002445
Matan Barakffc39f62014-11-13 14:45:29 +02002446 if (!mlx4_is_slave(dev) && !priv->cmd.hcr) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002447 priv->cmd.hcr = ioremap(pci_resource_start(dev->persist->pdev,
2448 0) + MLX4_HCR_BASE, MLX4_HCR_SIZE);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002449 if (!priv->cmd.hcr) {
Joe Perches1a91de22014-05-07 12:52:57 -07002450 mlx4_err(dev, "Couldn't map command register\n");
Matan Barakffc39f62014-11-13 14:45:29 +02002451 goto err;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002452 }
Matan Barakffc39f62014-11-13 14:45:29 +02002453 flags |= MLX4_CMD_CLEANUP_HCR;
Roland Dreier225c7b12007-05-08 18:00:38 -07002454 }
2455
Matan Barakffc39f62014-11-13 14:45:29 +02002456 if (mlx4_is_mfunc(dev) && !priv->mfunc.vhcr) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002457 priv->mfunc.vhcr = dma_alloc_coherent(&dev->persist->pdev->dev,
2458 PAGE_SIZE,
Roland Dreierf3d4c892012-09-25 21:24:07 -07002459 &priv->mfunc.vhcr_dma,
2460 GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002461 if (!priv->mfunc.vhcr)
Matan Barakffc39f62014-11-13 14:45:29 +02002462 goto err;
2463
2464 flags |= MLX4_CMD_CLEANUP_VHCR;
Roland Dreierf3d4c892012-09-25 21:24:07 -07002465 }
2466
Matan Barakffc39f62014-11-13 14:45:29 +02002467 if (!priv->cmd.pool) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002468 priv->cmd.pool = pci_pool_create("mlx4_cmd",
2469 dev->persist->pdev,
Matan Barakffc39f62014-11-13 14:45:29 +02002470 MLX4_MAILBOX_SIZE,
2471 MLX4_MAILBOX_SIZE, 0);
2472 if (!priv->cmd.pool)
2473 goto err;
2474
2475 flags |= MLX4_CMD_CLEANUP_POOL;
2476 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002477
2478 return 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002479
Matan Barakffc39f62014-11-13 14:45:29 +02002480err:
2481 mlx4_cmd_cleanup(dev, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002482 return -ENOMEM;
Roland Dreier225c7b12007-05-08 18:00:38 -07002483}
2484
Yishai Hadas55ad3592015-01-25 16:59:42 +02002485void mlx4_report_internal_err_comm_event(struct mlx4_dev *dev)
2486{
2487 struct mlx4_priv *priv = mlx4_priv(dev);
2488 int slave;
2489 u32 slave_read;
2490
2491 /* Report an internal error event to all
2492 * communication channels.
2493 */
2494 for (slave = 0; slave < dev->num_slaves; slave++) {
2495 slave_read = swab32(readl(&priv->mfunc.comm[slave].slave_read));
2496 slave_read |= (u32)COMM_CHAN_EVENT_INTERNAL_ERR;
2497 __raw_writel((__force u32)cpu_to_be32(slave_read),
2498 &priv->mfunc.comm[slave].slave_read);
2499 /* Make sure that our comm channel write doesn't
2500 * get mixed in with writes from another CPU.
2501 */
2502 mmiowb();
2503 }
2504}
2505
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002506void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
2507{
2508 struct mlx4_priv *priv = mlx4_priv(dev);
2509 int i, port;
2510
2511 if (mlx4_is_master(dev)) {
2512 flush_workqueue(priv->mfunc.master.comm_wq);
2513 destroy_workqueue(priv->mfunc.master.comm_wq);
2514 for (i = 0; i < dev->num_slaves; i++) {
2515 for (port = 1; port <= MLX4_MAX_PORTS; port++)
2516 kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
2517 }
2518 kfree(priv->mfunc.master.slave_state);
Rony Efraim0eb62b92013-04-25 05:22:26 +00002519 kfree(priv->mfunc.master.vf_admin);
2520 kfree(priv->mfunc.master.vf_oper);
Yishai Hadas55ad3592015-01-25 16:59:42 +02002521 dev->num_slaves = 0;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002522 }
Eugenia Emantayevf08ad062012-02-06 06:26:17 +00002523
2524 iounmap(priv->mfunc.comm);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002525}
2526
Matan Barakffc39f62014-11-13 14:45:29 +02002527void mlx4_cmd_cleanup(struct mlx4_dev *dev, int cleanup_mask)
Roland Dreier225c7b12007-05-08 18:00:38 -07002528{
2529 struct mlx4_priv *priv = mlx4_priv(dev);
2530
Matan Barakffc39f62014-11-13 14:45:29 +02002531 if (priv->cmd.pool && (cleanup_mask & MLX4_CMD_CLEANUP_POOL)) {
2532 pci_pool_destroy(priv->cmd.pool);
2533 priv->cmd.pool = NULL;
2534 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002535
Matan Barakffc39f62014-11-13 14:45:29 +02002536 if (!mlx4_is_slave(dev) && priv->cmd.hcr &&
2537 (cleanup_mask & MLX4_CMD_CLEANUP_HCR)) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002538 iounmap(priv->cmd.hcr);
Matan Barakffc39f62014-11-13 14:45:29 +02002539 priv->cmd.hcr = NULL;
2540 }
2541 if (mlx4_is_mfunc(dev) && priv->mfunc.vhcr &&
2542 (cleanup_mask & MLX4_CMD_CLEANUP_VHCR)) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002543 dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
Roland Dreierf3d4c892012-09-25 21:24:07 -07002544 priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
Matan Barakffc39f62014-11-13 14:45:29 +02002545 priv->mfunc.vhcr = NULL;
2546 }
2547 if (priv->cmd.initialized && (cleanup_mask & MLX4_CMD_CLEANUP_STRUCT))
2548 priv->cmd.initialized = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002549}
2550
2551/*
2552 * Switch to using events to issue FW commands (can only be called
2553 * after event queue for command events has been initialized).
2554 */
2555int mlx4_cmd_use_events(struct mlx4_dev *dev)
2556{
2557 struct mlx4_priv *priv = mlx4_priv(dev);
2558 int i;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002559 int err = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002560
2561 priv->cmd.context = kmalloc(priv->cmd.max_cmds *
2562 sizeof (struct mlx4_cmd_context),
2563 GFP_KERNEL);
2564 if (!priv->cmd.context)
2565 return -ENOMEM;
2566
2567 for (i = 0; i < priv->cmd.max_cmds; ++i) {
2568 priv->cmd.context[i].token = i;
2569 priv->cmd.context[i].next = i + 1;
Yishai Hadasf5aef5a2015-01-25 16:59:39 +02002570 /* To support fatal error flow, initialize all
2571 * cmd contexts to allow simulating completions
2572 * with complete() at any time.
2573 */
2574 init_completion(&priv->cmd.context[i].done);
Roland Dreier225c7b12007-05-08 18:00:38 -07002575 }
2576
2577 priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
2578 priv->cmd.free_head = 0;
2579
2580 sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
2581 spin_lock_init(&priv->cmd.context_lock);
2582
2583 for (priv->cmd.token_mask = 1;
2584 priv->cmd.token_mask < priv->cmd.max_cmds;
2585 priv->cmd.token_mask <<= 1)
2586 ; /* nothing */
2587 --priv->cmd.token_mask;
2588
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002589 down(&priv->cmd.poll_sem);
Roland Dreier225c7b12007-05-08 18:00:38 -07002590 priv->cmd.use_events = 1;
2591
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002592 return err;
Roland Dreier225c7b12007-05-08 18:00:38 -07002593}
2594
2595/*
2596 * Switch back to polling (used when shutting down the device)
2597 */
2598void mlx4_cmd_use_polling(struct mlx4_dev *dev)
2599{
2600 struct mlx4_priv *priv = mlx4_priv(dev);
2601 int i;
2602
2603 priv->cmd.use_events = 0;
2604
2605 for (i = 0; i < priv->cmd.max_cmds; ++i)
2606 down(&priv->cmd.event_sem);
2607
2608 kfree(priv->cmd.context);
2609
2610 up(&priv->cmd.poll_sem);
2611}
2612
2613struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
2614{
2615 struct mlx4_cmd_mailbox *mailbox;
2616
2617 mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
2618 if (!mailbox)
2619 return ERR_PTR(-ENOMEM);
2620
2621 mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
2622 &mailbox->dma);
2623 if (!mailbox->buf) {
2624 kfree(mailbox);
2625 return ERR_PTR(-ENOMEM);
2626 }
2627
Jack Morgenstein571b8b92013-11-07 12:19:50 +02002628 memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
2629
Roland Dreier225c7b12007-05-08 18:00:38 -07002630 return mailbox;
2631}
2632EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
2633
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002634void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
2635 struct mlx4_cmd_mailbox *mailbox)
Roland Dreier225c7b12007-05-08 18:00:38 -07002636{
2637 if (!mailbox)
2638 return;
2639
2640 pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
2641 kfree(mailbox);
2642}
2643EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002644
2645u32 mlx4_comm_get_version(void)
2646{
2647 return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
2648}
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002649
2650static int mlx4_get_slave_indx(struct mlx4_dev *dev, int vf)
2651{
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002652 if ((vf < 0) || (vf >= dev->persist->num_vfs)) {
2653 mlx4_err(dev, "Bad vf number:%d (number of activated vf: %d)\n",
2654 vf, dev->persist->num_vfs);
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002655 return -EINVAL;
2656 }
2657
2658 return vf+1;
2659}
2660
Matan Barakf74462a2014-03-19 18:11:51 +02002661int mlx4_get_vf_indx(struct mlx4_dev *dev, int slave)
2662{
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002663 if (slave < 1 || slave > dev->persist->num_vfs) {
Matan Barakf74462a2014-03-19 18:11:51 +02002664 mlx4_err(dev,
2665 "Bad slave number:%d (number of activated slaves: %lu)\n",
2666 slave, dev->num_slaves);
2667 return -EINVAL;
2668 }
2669 return slave - 1;
2670}
2671
Yishai Hadasf5aef5a2015-01-25 16:59:39 +02002672void mlx4_cmd_wake_completions(struct mlx4_dev *dev)
2673{
2674 struct mlx4_priv *priv = mlx4_priv(dev);
2675 struct mlx4_cmd_context *context;
2676 int i;
2677
2678 spin_lock(&priv->cmd.context_lock);
2679 if (priv->cmd.context) {
2680 for (i = 0; i < priv->cmd.max_cmds; ++i) {
2681 context = &priv->cmd.context[i];
2682 context->fw_status = CMD_STAT_INTERNAL_ERR;
2683 context->result =
2684 mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
2685 complete(&context->done);
2686 }
2687 }
2688 spin_unlock(&priv->cmd.context_lock);
2689}
2690
Matan Barakf74462a2014-03-19 18:11:51 +02002691struct mlx4_active_ports mlx4_get_active_ports(struct mlx4_dev *dev, int slave)
2692{
2693 struct mlx4_active_ports actv_ports;
2694 int vf;
2695
2696 bitmap_zero(actv_ports.ports, MLX4_MAX_PORTS);
2697
2698 if (slave == 0) {
2699 bitmap_fill(actv_ports.ports, dev->caps.num_ports);
2700 return actv_ports;
2701 }
2702
2703 vf = mlx4_get_vf_indx(dev, slave);
2704 if (vf < 0)
2705 return actv_ports;
2706
2707 bitmap_set(actv_ports.ports, dev->dev_vfs[vf].min_port - 1,
2708 min((int)dev->dev_vfs[mlx4_get_vf_indx(dev, slave)].n_ports,
2709 dev->caps.num_ports));
2710
2711 return actv_ports;
2712}
2713EXPORT_SYMBOL_GPL(mlx4_get_active_ports);
2714
2715int mlx4_slave_convert_port(struct mlx4_dev *dev, int slave, int port)
2716{
2717 unsigned n;
2718 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2719 unsigned m = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2720
2721 if (port <= 0 || port > m)
2722 return -EINVAL;
2723
2724 n = find_first_bit(actv_ports.ports, dev->caps.num_ports);
2725 if (port <= n)
2726 port = n + 1;
2727
2728 return port;
2729}
2730EXPORT_SYMBOL_GPL(mlx4_slave_convert_port);
2731
2732int mlx4_phys_to_slave_port(struct mlx4_dev *dev, int slave, int port)
2733{
2734 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2735 if (test_bit(port - 1, actv_ports.ports))
2736 return port -
2737 find_first_bit(actv_ports.ports, dev->caps.num_ports);
2738
2739 return -1;
2740}
2741EXPORT_SYMBOL_GPL(mlx4_phys_to_slave_port);
2742
2743struct mlx4_slaves_pport mlx4_phys_to_slaves_pport(struct mlx4_dev *dev,
2744 int port)
2745{
2746 unsigned i;
2747 struct mlx4_slaves_pport slaves_pport;
2748
2749 bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
2750
2751 if (port <= 0 || port > dev->caps.num_ports)
2752 return slaves_pport;
2753
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002754 for (i = 0; i < dev->persist->num_vfs + 1; i++) {
Matan Barakf74462a2014-03-19 18:11:51 +02002755 struct mlx4_active_ports actv_ports =
2756 mlx4_get_active_ports(dev, i);
2757 if (test_bit(port - 1, actv_ports.ports))
2758 set_bit(i, slaves_pport.slaves);
2759 }
2760
2761 return slaves_pport;
2762}
2763EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport);
2764
2765struct mlx4_slaves_pport mlx4_phys_to_slaves_pport_actv(
2766 struct mlx4_dev *dev,
2767 const struct mlx4_active_ports *crit_ports)
2768{
2769 unsigned i;
2770 struct mlx4_slaves_pport slaves_pport;
2771
2772 bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
2773
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002774 for (i = 0; i < dev->persist->num_vfs + 1; i++) {
Matan Barakf74462a2014-03-19 18:11:51 +02002775 struct mlx4_active_ports actv_ports =
2776 mlx4_get_active_ports(dev, i);
2777 if (bitmap_equal(crit_ports->ports, actv_ports.ports,
2778 dev->caps.num_ports))
2779 set_bit(i, slaves_pport.slaves);
2780 }
2781
2782 return slaves_pport;
2783}
2784EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport_actv);
2785
Matan Baraka91c7722014-09-10 16:41:53 +03002786static int mlx4_slaves_closest_port(struct mlx4_dev *dev, int slave, int port)
2787{
2788 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2789 int min_port = find_first_bit(actv_ports.ports, dev->caps.num_ports)
2790 + 1;
2791 int max_port = min_port +
2792 bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2793
2794 if (port < min_port)
2795 port = min_port;
2796 else if (port >= max_port)
2797 port = max_port - 1;
2798
2799 return port;
2800}
2801
Ido Shamaycda373f2015-04-02 16:31:16 +03002802static int mlx4_set_vport_qos(struct mlx4_priv *priv, int slave, int port,
2803 int max_tx_rate)
2804{
2805 int i;
2806 int err;
2807 struct mlx4_qos_manager *port_qos;
2808 struct mlx4_dev *dev = &priv->dev;
2809 struct mlx4_vport_qos_param vpp_qos[MLX4_NUM_UP];
2810
2811 port_qos = &priv->mfunc.master.qos_ctl[port];
2812 memset(vpp_qos, 0, sizeof(struct mlx4_vport_qos_param) * MLX4_NUM_UP);
2813
2814 if (slave > port_qos->num_of_qos_vfs) {
2815 mlx4_info(dev, "No availible VPP resources for this VF\n");
2816 return -EINVAL;
2817 }
2818
2819 /* Query for default QoS values from Vport 0 is needed */
2820 err = mlx4_SET_VPORT_QOS_get(dev, port, 0, vpp_qos);
2821 if (err) {
2822 mlx4_info(dev, "Failed to query Vport 0 QoS values\n");
2823 return err;
2824 }
2825
2826 for (i = 0; i < MLX4_NUM_UP; i++) {
2827 if (test_bit(i, port_qos->priority_bm) && max_tx_rate) {
2828 vpp_qos[i].max_avg_bw = max_tx_rate;
2829 vpp_qos[i].enable = 1;
2830 } else {
2831 /* if user supplied tx_rate == 0, meaning no rate limit
2832 * configuration is required. so we are leaving the
2833 * value of max_avg_bw as queried from Vport 0.
2834 */
2835 vpp_qos[i].enable = 0;
2836 }
2837 }
2838
2839 err = mlx4_SET_VPORT_QOS_set(dev, port, slave, vpp_qos);
2840 if (err) {
2841 mlx4_info(dev, "Failed to set Vport %d QoS values\n", slave);
2842 return err;
2843 }
2844
2845 return 0;
2846}
2847
2848static bool mlx4_is_vf_vst_and_prio_qos(struct mlx4_dev *dev, int port,
2849 struct mlx4_vport_state *vf_admin)
2850{
2851 struct mlx4_qos_manager *info;
2852 struct mlx4_priv *priv = mlx4_priv(dev);
2853
2854 if (!mlx4_is_master(dev) ||
2855 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
2856 return false;
2857
2858 info = &priv->mfunc.master.qos_ctl[port];
2859
2860 if (vf_admin->default_vlan != MLX4_VGT &&
2861 test_bit(vf_admin->default_qos, info->priority_bm))
2862 return true;
2863
2864 return false;
2865}
2866
2867static bool mlx4_valid_vf_state_change(struct mlx4_dev *dev, int port,
2868 struct mlx4_vport_state *vf_admin,
2869 int vlan, int qos)
2870{
2871 struct mlx4_vport_state dummy_admin = {0};
2872
2873 if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) ||
2874 !vf_admin->tx_rate)
2875 return true;
2876
2877 dummy_admin.default_qos = qos;
2878 dummy_admin.default_vlan = vlan;
2879
2880 /* VF wants to move to other VST state which is valid with current
2881 * rate limit. Either differnt default vlan in VST or other
2882 * supported QoS priority. Otherwise we don't allow this change when
2883 * the TX rate is still configured.
2884 */
2885 if (mlx4_is_vf_vst_and_prio_qos(dev, port, &dummy_admin))
2886 return true;
2887
2888 mlx4_info(dev, "Cannot change VF state to %s while rate is set\n",
2889 (vlan == MLX4_VGT) ? "VGT" : "VST");
2890
2891 if (vlan != MLX4_VGT)
2892 mlx4_info(dev, "VST priority %d not supported for QoS\n", qos);
2893
2894 mlx4_info(dev, "Please set rate to 0 prior to this VF state change\n");
2895
2896 return false;
2897}
2898
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002899int mlx4_set_vf_mac(struct mlx4_dev *dev, int port, int vf, u64 mac)
2900{
2901 struct mlx4_priv *priv = mlx4_priv(dev);
2902 struct mlx4_vport_state *s_info;
2903 int slave;
2904
2905 if (!mlx4_is_master(dev))
2906 return -EPROTONOSUPPORT;
2907
2908 slave = mlx4_get_slave_indx(dev, vf);
2909 if (slave < 0)
2910 return -EINVAL;
2911
Matan Baraka91c7722014-09-10 16:41:53 +03002912 port = mlx4_slaves_closest_port(dev, slave, port);
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002913 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
2914 s_info->mac = mac;
2915 mlx4_info(dev, "default mac on vf %d port %d to %llX will take afect only after vf restart\n",
2916 vf, port, s_info->mac);
2917 return 0;
2918}
2919EXPORT_SYMBOL_GPL(mlx4_set_vf_mac);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002920
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002921
Rony Efraim3f7fb022013-04-25 05:22:28 +00002922int mlx4_set_vf_vlan(struct mlx4_dev *dev, int port, int vf, u16 vlan, u8 qos)
2923{
2924 struct mlx4_priv *priv = mlx4_priv(dev);
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002925 struct mlx4_vport_state *vf_admin;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002926 int slave;
2927
2928 if ((!mlx4_is_master(dev)) ||
2929 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
2930 return -EPROTONOSUPPORT;
2931
2932 if ((vlan > 4095) || (qos > 7))
2933 return -EINVAL;
2934
2935 slave = mlx4_get_slave_indx(dev, vf);
2936 if (slave < 0)
2937 return -EINVAL;
2938
Matan Baraka91c7722014-09-10 16:41:53 +03002939 port = mlx4_slaves_closest_port(dev, slave, port);
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002940 vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002941
Ido Shamaycda373f2015-04-02 16:31:16 +03002942 if (!mlx4_valid_vf_state_change(dev, port, vf_admin, vlan, qos))
2943 return -EPERM;
2944
Rony Efraim3f7fb022013-04-25 05:22:28 +00002945 if ((0 == vlan) && (0 == qos))
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002946 vf_admin->default_vlan = MLX4_VGT;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002947 else
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002948 vf_admin->default_vlan = vlan;
2949 vf_admin->default_qos = qos;
2950
Ido Shamaycda373f2015-04-02 16:31:16 +03002951 /* If rate was configured prior to VST, we saved the configured rate
2952 * in vf_admin->rate and now, if priority supported we enforce the QoS
2953 */
2954 if (mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) &&
2955 vf_admin->tx_rate)
2956 vf_admin->qos_vport = slave;
2957
Rony Efraim0a6eac22013-06-27 19:05:22 +03002958 if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
2959 mlx4_info(dev,
2960 "updating vf %d port %d config will take effect on next VF restart\n",
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002961 vf, port);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002962 return 0;
2963}
2964EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
Rony Efraime6b6a232013-04-25 05:22:29 +00002965
Ido Shamaycda373f2015-04-02 16:31:16 +03002966int mlx4_set_vf_rate(struct mlx4_dev *dev, int port, int vf, int min_tx_rate,
2967 int max_tx_rate)
2968{
2969 int err;
2970 int slave;
2971 struct mlx4_vport_state *vf_admin;
2972 struct mlx4_priv *priv = mlx4_priv(dev);
2973
2974 if (!mlx4_is_master(dev) ||
2975 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
2976 return -EPROTONOSUPPORT;
2977
2978 if (min_tx_rate) {
2979 mlx4_info(dev, "Minimum BW share not supported\n");
2980 return -EPROTONOSUPPORT;
2981 }
2982
2983 slave = mlx4_get_slave_indx(dev, vf);
2984 if (slave < 0)
2985 return -EINVAL;
2986
2987 port = mlx4_slaves_closest_port(dev, slave, port);
2988 vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
2989
2990 err = mlx4_set_vport_qos(priv, slave, port, max_tx_rate);
2991 if (err) {
2992 mlx4_info(dev, "vf %d failed to set rate %d\n", vf,
2993 max_tx_rate);
2994 return err;
2995 }
2996
2997 vf_admin->tx_rate = max_tx_rate;
2998 /* if VF is not in supported mode (VST with supported prio),
2999 * we do not change vport configuration for its QPs, but save
3000 * the rate, so it will be enforced when it moves to supported
3001 * mode next time.
3002 */
3003 if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin)) {
3004 mlx4_info(dev,
3005 "rate set for VF %d when not in valid state\n", vf);
3006
3007 if (vf_admin->default_vlan != MLX4_VGT)
3008 mlx4_info(dev, "VST priority not supported by QoS\n");
3009 else
3010 mlx4_info(dev, "VF in VGT mode (needed VST)\n");
3011
3012 mlx4_info(dev,
3013 "rate %d take affect when VF moves to valid state\n",
3014 max_tx_rate);
3015 return 0;
3016 }
3017
3018 /* If user sets rate 0 assigning default vport for its QPs */
3019 vf_admin->qos_vport = max_tx_rate ? slave : MLX4_VPP_DEFAULT_VPORT;
3020
3021 if (priv->mfunc.master.slave_state[slave].active &&
3022 dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)
3023 mlx4_master_immediate_activate_vlan_qos(priv, slave, port);
3024
3025 return 0;
3026}
3027EXPORT_SYMBOL_GPL(mlx4_set_vf_rate);
3028
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +02003029 /* mlx4_get_slave_default_vlan -
3030 * return true if VST ( default vlan)
3031 * if VST, will return vlan & qos (if not NULL)
3032 */
3033bool mlx4_get_slave_default_vlan(struct mlx4_dev *dev, int port, int slave,
3034 u16 *vlan, u8 *qos)
3035{
3036 struct mlx4_vport_oper_state *vp_oper;
3037 struct mlx4_priv *priv;
3038
3039 priv = mlx4_priv(dev);
Matan Baraka91c7722014-09-10 16:41:53 +03003040 port = mlx4_slaves_closest_port(dev, slave, port);
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +02003041 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
3042
3043 if (MLX4_VGT != vp_oper->state.default_vlan) {
3044 if (vlan)
3045 *vlan = vp_oper->state.default_vlan;
3046 if (qos)
3047 *qos = vp_oper->state.default_qos;
3048 return true;
3049 }
3050 return false;
3051}
3052EXPORT_SYMBOL_GPL(mlx4_get_slave_default_vlan);
3053
Rony Efraime6b6a232013-04-25 05:22:29 +00003054int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
3055{
3056 struct mlx4_priv *priv = mlx4_priv(dev);
3057 struct mlx4_vport_state *s_info;
3058 int slave;
3059
3060 if ((!mlx4_is_master(dev)) ||
3061 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
3062 return -EPROTONOSUPPORT;
3063
3064 slave = mlx4_get_slave_indx(dev, vf);
3065 if (slave < 0)
3066 return -EINVAL;
3067
Matan Baraka91c7722014-09-10 16:41:53 +03003068 port = mlx4_slaves_closest_port(dev, slave, port);
Rony Efraime6b6a232013-04-25 05:22:29 +00003069 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
3070 s_info->spoofchk = setting;
3071
3072 return 0;
3073}
3074EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
Rony Efraim2cccb9e2013-04-25 05:22:30 +00003075
3076int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
3077{
3078 struct mlx4_priv *priv = mlx4_priv(dev);
3079 struct mlx4_vport_state *s_info;
3080 int slave;
3081
3082 if (!mlx4_is_master(dev))
3083 return -EPROTONOSUPPORT;
3084
3085 slave = mlx4_get_slave_indx(dev, vf);
3086 if (slave < 0)
3087 return -EINVAL;
3088
3089 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
3090 ivf->vf = vf;
3091
3092 /* need to convert it to a func */
3093 ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
3094 ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
3095 ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
3096 ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
3097 ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
3098 ivf->mac[5] = ((s_info->mac) & 0xff);
3099
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04003100 ivf->vlan = s_info->default_vlan;
3101 ivf->qos = s_info->default_qos;
Ido Shamaycda373f2015-04-02 16:31:16 +03003102
3103 if (mlx4_is_vf_vst_and_prio_qos(dev, port, s_info))
3104 ivf->max_tx_rate = s_info->tx_rate;
3105 else
3106 ivf->max_tx_rate = 0;
3107
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04003108 ivf->min_tx_rate = 0;
3109 ivf->spoofchk = s_info->spoofchk;
3110 ivf->linkstate = s_info->link_state;
Rony Efraim2cccb9e2013-04-25 05:22:30 +00003111
3112 return 0;
3113}
3114EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
Rony Efraim948e3062013-06-13 13:19:11 +03003115
3116int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
3117{
3118 struct mlx4_priv *priv = mlx4_priv(dev);
3119 struct mlx4_vport_state *s_info;
Rony Efraim948e3062013-06-13 13:19:11 +03003120 int slave;
3121 u8 link_stat_event;
3122
3123 slave = mlx4_get_slave_indx(dev, vf);
3124 if (slave < 0)
3125 return -EINVAL;
3126
Matan Baraka91c7722014-09-10 16:41:53 +03003127 port = mlx4_slaves_closest_port(dev, slave, port);
Rony Efraim948e3062013-06-13 13:19:11 +03003128 switch (link_state) {
3129 case IFLA_VF_LINK_STATE_AUTO:
3130 /* get current link state */
3131 if (!priv->sense.do_sense_port[port])
3132 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
3133 else
3134 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
3135 break;
3136
3137 case IFLA_VF_LINK_STATE_ENABLE:
3138 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
3139 break;
3140
3141 case IFLA_VF_LINK_STATE_DISABLE:
3142 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
3143 break;
3144
3145 default:
3146 mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
3147 link_state, slave, port);
3148 return -EINVAL;
3149 };
Rony Efraim948e3062013-06-13 13:19:11 +03003150 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
Rony Efraim948e3062013-06-13 13:19:11 +03003151 s_info->link_state = link_state;
Rony Efraim948e3062013-06-13 13:19:11 +03003152
3153 /* send event */
3154 mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
Rony Efraim0a6eac22013-06-27 19:05:22 +03003155
3156 if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
3157 mlx4_dbg(dev,
3158 "updating vf %d port %d no link state HW enforcment\n",
3159 vf, port);
Rony Efraim948e3062013-06-13 13:19:11 +03003160 return 0;
3161}
3162EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);
Jack Morgenstein97982f52014-05-29 16:31:02 +03003163
3164int mlx4_vf_smi_enabled(struct mlx4_dev *dev, int slave, int port)
3165{
Jack Morgenstein99ec41d2014-05-29 16:31:03 +03003166 struct mlx4_priv *priv = mlx4_priv(dev);
3167
3168 if (slave < 1 || slave >= dev->num_slaves ||
3169 port < 1 || port > MLX4_MAX_PORTS)
3170 return 0;
3171
3172 return priv->mfunc.master.vf_oper[slave].smi_enabled[port] ==
3173 MLX4_VF_SMI_ENABLED;
Jack Morgenstein97982f52014-05-29 16:31:02 +03003174}
3175EXPORT_SYMBOL_GPL(mlx4_vf_smi_enabled);
Jack Morgenstein65fed8a2014-05-29 16:31:04 +03003176
3177int mlx4_vf_get_enable_smi_admin(struct mlx4_dev *dev, int slave, int port)
3178{
3179 struct mlx4_priv *priv = mlx4_priv(dev);
3180
3181 if (slave == mlx4_master_func_num(dev))
3182 return 1;
3183
3184 if (slave < 1 || slave >= dev->num_slaves ||
3185 port < 1 || port > MLX4_MAX_PORTS)
3186 return 0;
3187
3188 return priv->mfunc.master.vf_admin[slave].enable_smi[port] ==
3189 MLX4_VF_SMI_ENABLED;
3190}
3191EXPORT_SYMBOL_GPL(mlx4_vf_get_enable_smi_admin);
3192
3193int mlx4_vf_set_enable_smi_admin(struct mlx4_dev *dev, int slave, int port,
3194 int enabled)
3195{
3196 struct mlx4_priv *priv = mlx4_priv(dev);
3197
3198 if (slave == mlx4_master_func_num(dev))
3199 return 0;
3200
3201 if (slave < 1 || slave >= dev->num_slaves ||
3202 port < 1 || port > MLX4_MAX_PORTS ||
3203 enabled < 0 || enabled > 1)
3204 return -EINVAL;
3205
3206 priv->mfunc.master.vf_admin[slave].enable_smi[port] = enabled;
3207 return 0;
3208}
3209EXPORT_SYMBOL_GPL(mlx4_vf_set_enable_smi_admin);