blob: af95231ba3e2b27c6b92e1b582cb393ac6f8708f [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"
Roland Dreier225c7b12007-05-08 18:00:38 -070051
52#define CMD_POLL_TOKEN 0xffff
Yevgeny Petriline8f081a2011-12-13 04:12:25 +000053#define INBOX_MASK 0xffffffffffffff00ULL
54
55#define CMD_CHAN_VER 1
56#define CMD_CHAN_IF_REV 1
Roland Dreier225c7b12007-05-08 18:00:38 -070057
58enum {
59 /* command completed successfully: */
60 CMD_STAT_OK = 0x00,
61 /* Internal error (such as a bus error) occurred while processing command: */
62 CMD_STAT_INTERNAL_ERR = 0x01,
63 /* Operation/command not supported or opcode modifier not supported: */
64 CMD_STAT_BAD_OP = 0x02,
65 /* Parameter not supported or parameter out of range: */
66 CMD_STAT_BAD_PARAM = 0x03,
67 /* System not enabled or bad system state: */
68 CMD_STAT_BAD_SYS_STATE = 0x04,
69 /* Attempt to access reserved or unallocaterd resource: */
70 CMD_STAT_BAD_RESOURCE = 0x05,
71 /* Requested resource is currently executing a command, or is otherwise busy: */
72 CMD_STAT_RESOURCE_BUSY = 0x06,
73 /* Required capability exceeds device limits: */
74 CMD_STAT_EXCEED_LIM = 0x08,
75 /* Resource is not in the appropriate state or ownership: */
76 CMD_STAT_BAD_RES_STATE = 0x09,
77 /* Index out of range: */
78 CMD_STAT_BAD_INDEX = 0x0a,
79 /* FW image corrupted: */
80 CMD_STAT_BAD_NVMEM = 0x0b,
Jack Morgenstein899698d2008-07-22 14:19:39 -070081 /* Error in ICM mapping (e.g. not enough auxiliary ICM pages to execute command): */
82 CMD_STAT_ICM_ERROR = 0x0c,
Roland Dreier225c7b12007-05-08 18:00:38 -070083 /* Attempt to modify a QP/EE which is not in the presumed state: */
84 CMD_STAT_BAD_QP_STATE = 0x10,
85 /* Bad segment parameters (Address/Size): */
86 CMD_STAT_BAD_SEG_PARAM = 0x20,
87 /* Memory Region has Memory Windows bound to: */
88 CMD_STAT_REG_BOUND = 0x21,
89 /* HCA local attached memory not present: */
90 CMD_STAT_LAM_NOT_PRE = 0x22,
91 /* Bad management packet (silently discarded): */
92 CMD_STAT_BAD_PKT = 0x30,
93 /* More outstanding CQEs in CQ than new CQ size: */
Yevgeny Petrilincc4ac2e2009-07-06 16:10:03 -070094 CMD_STAT_BAD_SIZE = 0x40,
95 /* Multi Function device support required: */
96 CMD_STAT_MULTI_FUNC_REQ = 0x50,
Roland Dreier225c7b12007-05-08 18:00:38 -070097};
98
99enum {
100 HCR_IN_PARAM_OFFSET = 0x00,
101 HCR_IN_MODIFIER_OFFSET = 0x08,
102 HCR_OUT_PARAM_OFFSET = 0x0c,
103 HCR_TOKEN_OFFSET = 0x14,
104 HCR_STATUS_OFFSET = 0x18,
105
106 HCR_OPMOD_SHIFT = 12,
107 HCR_T_BIT = 21,
108 HCR_E_BIT = 22,
109 HCR_GO_BIT = 23
110};
111
112enum {
Dotan Barak36ce10d2007-08-07 11:18:52 +0300113 GO_BIT_TIMEOUT_MSECS = 10000
Roland Dreier225c7b12007-05-08 18:00:38 -0700114};
115
Jack Morgensteinb01978c2013-06-27 19:05:21 +0300116enum mlx4_vlan_transition {
117 MLX4_VLAN_TRANSITION_VST_VST = 0,
118 MLX4_VLAN_TRANSITION_VST_VGT = 1,
119 MLX4_VLAN_TRANSITION_VGT_VST = 2,
120 MLX4_VLAN_TRANSITION_VGT_VGT = 3,
121};
122
123
Roland Dreier225c7b12007-05-08 18:00:38 -0700124struct mlx4_cmd_context {
125 struct completion done;
126 int result;
127 int next;
128 u64 out_param;
129 u16 token;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000130 u8 fw_status;
Roland Dreier225c7b12007-05-08 18:00:38 -0700131};
132
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000133static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
134 struct mlx4_vhcr_cmd *in_vhcr);
135
Roland Dreierca281212008-04-16 21:01:04 -0700136static int mlx4_status_to_errno(u8 status)
137{
Roland Dreier225c7b12007-05-08 18:00:38 -0700138 static const int trans_table[] = {
139 [CMD_STAT_INTERNAL_ERR] = -EIO,
140 [CMD_STAT_BAD_OP] = -EPERM,
141 [CMD_STAT_BAD_PARAM] = -EINVAL,
142 [CMD_STAT_BAD_SYS_STATE] = -ENXIO,
143 [CMD_STAT_BAD_RESOURCE] = -EBADF,
144 [CMD_STAT_RESOURCE_BUSY] = -EBUSY,
145 [CMD_STAT_EXCEED_LIM] = -ENOMEM,
146 [CMD_STAT_BAD_RES_STATE] = -EBADF,
147 [CMD_STAT_BAD_INDEX] = -EBADF,
148 [CMD_STAT_BAD_NVMEM] = -EFAULT,
Jack Morgenstein899698d2008-07-22 14:19:39 -0700149 [CMD_STAT_ICM_ERROR] = -ENFILE,
Roland Dreier225c7b12007-05-08 18:00:38 -0700150 [CMD_STAT_BAD_QP_STATE] = -EINVAL,
151 [CMD_STAT_BAD_SEG_PARAM] = -EFAULT,
152 [CMD_STAT_REG_BOUND] = -EBUSY,
153 [CMD_STAT_LAM_NOT_PRE] = -EAGAIN,
154 [CMD_STAT_BAD_PKT] = -EINVAL,
155 [CMD_STAT_BAD_SIZE] = -ENOMEM,
Yevgeny Petrilincc4ac2e2009-07-06 16:10:03 -0700156 [CMD_STAT_MULTI_FUNC_REQ] = -EACCES,
Roland Dreier225c7b12007-05-08 18:00:38 -0700157 };
158
159 if (status >= ARRAY_SIZE(trans_table) ||
160 (status != CMD_STAT_OK && trans_table[status] == 0))
161 return -EIO;
162
163 return trans_table[status];
164}
165
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000166static u8 mlx4_errno_to_status(int errno)
167{
168 switch (errno) {
169 case -EPERM:
170 return CMD_STAT_BAD_OP;
171 case -EINVAL:
172 return CMD_STAT_BAD_PARAM;
173 case -ENXIO:
174 return CMD_STAT_BAD_SYS_STATE;
175 case -EBUSY:
176 return CMD_STAT_RESOURCE_BUSY;
177 case -ENOMEM:
178 return CMD_STAT_EXCEED_LIM;
179 case -ENFILE:
180 return CMD_STAT_ICM_ERROR;
181 default:
182 return CMD_STAT_INTERNAL_ERR;
183 }
184}
185
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200186static int mlx4_internal_err_ret_value(struct mlx4_dev *dev, u16 op,
187 u8 op_modifier)
188{
189 switch (op) {
190 case MLX4_CMD_UNMAP_ICM:
191 case MLX4_CMD_UNMAP_ICM_AUX:
192 case MLX4_CMD_UNMAP_FA:
193 case MLX4_CMD_2RST_QP:
194 case MLX4_CMD_HW2SW_EQ:
195 case MLX4_CMD_HW2SW_CQ:
196 case MLX4_CMD_HW2SW_SRQ:
197 case MLX4_CMD_HW2SW_MPT:
198 case MLX4_CMD_CLOSE_HCA:
199 case MLX4_QP_FLOW_STEERING_DETACH:
200 case MLX4_CMD_FREE_RES:
201 case MLX4_CMD_CLOSE_PORT:
202 return CMD_STAT_OK;
203
204 case MLX4_CMD_QP_ATTACH:
205 /* On Detach case return success */
206 if (op_modifier == 0)
207 return CMD_STAT_OK;
208 return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
209
210 default:
211 return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
212 }
213}
214
215static int mlx4_closing_cmd_fatal_error(u16 op, u8 fw_status)
216{
217 /* Any error during the closing commands below is considered fatal */
218 if (op == MLX4_CMD_CLOSE_HCA ||
219 op == MLX4_CMD_HW2SW_EQ ||
220 op == MLX4_CMD_HW2SW_CQ ||
221 op == MLX4_CMD_2RST_QP ||
222 op == MLX4_CMD_HW2SW_SRQ ||
223 op == MLX4_CMD_SYNC_TPT ||
224 op == MLX4_CMD_UNMAP_ICM ||
225 op == MLX4_CMD_UNMAP_ICM_AUX ||
226 op == MLX4_CMD_UNMAP_FA)
227 return 1;
228 /* Error on MLX4_CMD_HW2SW_MPT is fatal except when fw status equals
229 * CMD_STAT_REG_BOUND.
230 * This status indicates that memory region has memory windows bound to it
231 * which may result from invalid user space usage and is not fatal.
232 */
233 if (op == MLX4_CMD_HW2SW_MPT && fw_status != CMD_STAT_REG_BOUND)
234 return 1;
235 return 0;
236}
237
238static int mlx4_cmd_reset_flow(struct mlx4_dev *dev, u16 op, u8 op_modifier,
239 int err)
240{
241 /* Only if reset flow is really active return code is based on
242 * command, otherwise current error code is returned.
243 */
244 if (mlx4_internal_err_reset) {
245 mlx4_enter_error_state(dev->persist);
246 err = mlx4_internal_err_ret_value(dev, op, op_modifier);
247 }
248
249 return err;
250}
251
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000252static int comm_pending(struct mlx4_dev *dev)
253{
254 struct mlx4_priv *priv = mlx4_priv(dev);
255 u32 status = readl(&priv->mfunc.comm->slave_read);
256
257 return (swab32(status) >> 31) != priv->cmd.comm_toggle;
258}
259
Yishai Hadas0cd93022015-01-25 16:59:43 +0200260static int mlx4_comm_cmd_post(struct mlx4_dev *dev, u8 cmd, u16 param)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000261{
262 struct mlx4_priv *priv = mlx4_priv(dev);
263 u32 val;
264
Yishai Hadas0cd93022015-01-25 16:59:43 +0200265 /* To avoid writing to unknown addresses after the device state was
266 * changed to internal error and the function was rest,
267 * check the INTERNAL_ERROR flag which is updated under
268 * device_state_mutex lock.
269 */
270 mutex_lock(&dev->persist->device_state_mutex);
271
272 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
273 mutex_unlock(&dev->persist->device_state_mutex);
274 return -EIO;
275 }
276
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000277 priv->cmd.comm_toggle ^= 1;
278 val = param | (cmd << 16) | (priv->cmd.comm_toggle << 31);
279 __raw_writel((__force u32) cpu_to_be32(val),
280 &priv->mfunc.comm->slave_write);
281 mmiowb();
Yishai Hadas0cd93022015-01-25 16:59:43 +0200282 mutex_unlock(&dev->persist->device_state_mutex);
283 return 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000284}
285
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000286static int mlx4_comm_cmd_poll(struct mlx4_dev *dev, u8 cmd, u16 param,
287 unsigned long timeout)
288{
289 struct mlx4_priv *priv = mlx4_priv(dev);
290 unsigned long end;
291 int err = 0;
292 int ret_from_pending = 0;
293
294 /* First, verify that the master reports correct status */
295 if (comm_pending(dev)) {
Joe Perches1a91de22014-05-07 12:52:57 -0700296 mlx4_warn(dev, "Communication channel is not idle - my toggle is %d (cmd:0x%x)\n",
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000297 priv->cmd.comm_toggle, cmd);
298 return -EAGAIN;
299 }
300
301 /* Write command */
302 down(&priv->cmd.poll_sem);
Yishai Hadas0cd93022015-01-25 16:59:43 +0200303 if (mlx4_comm_cmd_post(dev, cmd, param)) {
304 /* Only in case the device state is INTERNAL_ERROR,
305 * mlx4_comm_cmd_post returns with an error
306 */
307 err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
308 goto out;
309 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000310
311 end = msecs_to_jiffies(timeout) + jiffies;
312 while (comm_pending(dev) && time_before(jiffies, end))
313 cond_resched();
314 ret_from_pending = comm_pending(dev);
315 if (ret_from_pending) {
316 /* check if the slave is trying to boot in the middle of
317 * FLR process. The only non-zero result in the RESET command
318 * is MLX4_DELAY_RESET_SLAVE*/
319 if ((MLX4_COMM_CMD_RESET == cmd)) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000320 err = MLX4_DELAY_RESET_SLAVE;
Yishai Hadas0cd93022015-01-25 16:59:43 +0200321 goto out;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000322 } else {
Yishai Hadas0cd93022015-01-25 16:59:43 +0200323 mlx4_warn(dev, "Communication channel command 0x%x timed out\n",
324 cmd);
325 err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000326 }
327 }
328
Yishai Hadas0cd93022015-01-25 16:59:43 +0200329 if (err)
330 mlx4_enter_error_state(dev->persist);
331out:
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000332 up(&priv->cmd.poll_sem);
333 return err;
334}
335
Yishai Hadas0cd93022015-01-25 16:59:43 +0200336static int mlx4_comm_cmd_wait(struct mlx4_dev *dev, u8 vhcr_cmd,
337 u16 param, u16 op, unsigned long timeout)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000338{
339 struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
340 struct mlx4_cmd_context *context;
Eugenia Emantayev58a3de02012-03-18 04:32:08 +0000341 unsigned long end;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000342 int err = 0;
343
344 down(&cmd->event_sem);
345
346 spin_lock(&cmd->context_lock);
347 BUG_ON(cmd->free_head < 0);
348 context = &cmd->context[cmd->free_head];
349 context->token += cmd->token_mask + 1;
350 cmd->free_head = context->next;
351 spin_unlock(&cmd->context_lock);
352
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200353 reinit_completion(&context->done);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000354
Yishai Hadas0cd93022015-01-25 16:59:43 +0200355 if (mlx4_comm_cmd_post(dev, vhcr_cmd, param)) {
356 /* Only in case the device state is INTERNAL_ERROR,
357 * mlx4_comm_cmd_post returns with an error
358 */
359 err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
360 goto out;
361 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000362
363 if (!wait_for_completion_timeout(&context->done,
364 msecs_to_jiffies(timeout))) {
Yishai Hadas0cd93022015-01-25 16:59:43 +0200365 mlx4_warn(dev, "communication channel command 0x%x (op=0x%x) timed out\n",
366 vhcr_cmd, op);
367 goto out_reset;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000368 }
369
370 err = context->result;
371 if (err && context->fw_status != CMD_STAT_MULTI_FUNC_REQ) {
372 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
Yishai Hadas0cd93022015-01-25 16:59:43 +0200373 vhcr_cmd, context->fw_status);
374 if (mlx4_closing_cmd_fatal_error(op, context->fw_status))
375 goto out_reset;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000376 }
377
Eugenia Emantayev58a3de02012-03-18 04:32:08 +0000378 /* wait for comm channel ready
379 * this is necessary for prevention the race
380 * when switching between event to polling mode
Yishai Hadas0cd93022015-01-25 16:59:43 +0200381 * Skipping this section in case the device is in FATAL_ERROR state,
382 * In this state, no commands are sent via the comm channel until
383 * the device has returned from reset.
Eugenia Emantayev58a3de02012-03-18 04:32:08 +0000384 */
Yishai Hadas0cd93022015-01-25 16:59:43 +0200385 if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)) {
386 end = msecs_to_jiffies(timeout) + jiffies;
387 while (comm_pending(dev) && time_before(jiffies, end))
388 cond_resched();
389 }
390 goto out;
Eugenia Emantayev58a3de02012-03-18 04:32:08 +0000391
Yishai Hadas0cd93022015-01-25 16:59:43 +0200392out_reset:
393 err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
394 mlx4_enter_error_state(dev->persist);
395out:
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000396 spin_lock(&cmd->context_lock);
397 context->next = cmd->free_head;
398 cmd->free_head = context - cmd->context;
399 spin_unlock(&cmd->context_lock);
400
401 up(&cmd->event_sem);
402 return err;
403}
404
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000405int mlx4_comm_cmd(struct mlx4_dev *dev, u8 cmd, u16 param,
Yishai Hadas0cd93022015-01-25 16:59:43 +0200406 u16 op, unsigned long timeout)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000407{
Yishai Hadas0cd93022015-01-25 16:59:43 +0200408 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
409 return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
410
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000411 if (mlx4_priv(dev)->cmd.use_events)
Yishai Hadas0cd93022015-01-25 16:59:43 +0200412 return mlx4_comm_cmd_wait(dev, cmd, param, op, timeout);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000413 return mlx4_comm_cmd_poll(dev, cmd, param, timeout);
414}
415
Roland Dreier225c7b12007-05-08 18:00:38 -0700416static int cmd_pending(struct mlx4_dev *dev)
417{
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000418 u32 status;
419
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200420 if (pci_channel_offline(dev->persist->pdev))
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000421 return -EIO;
422
423 status = readl(mlx4_priv(dev)->cmd.hcr + HCR_STATUS_OFFSET);
Roland Dreier225c7b12007-05-08 18:00:38 -0700424
425 return (status & swab32(1 << HCR_GO_BIT)) ||
426 (mlx4_priv(dev)->cmd.toggle ==
427 !!(status & swab32(1 << HCR_T_BIT)));
428}
429
430static int mlx4_cmd_post(struct mlx4_dev *dev, u64 in_param, u64 out_param,
431 u32 in_modifier, u8 op_modifier, u16 op, u16 token,
432 int event)
433{
434 struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
435 u32 __iomem *hcr = cmd->hcr;
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200436 int ret = -EIO;
Roland Dreier225c7b12007-05-08 18:00:38 -0700437 unsigned long end;
438
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200439 mutex_lock(&dev->persist->device_state_mutex);
440 /* To avoid writing to unknown addresses after the device state was
441 * changed to internal error and the chip was reset,
442 * check the INTERNAL_ERROR flag which is updated under
443 * device_state_mutex lock.
444 */
445 if (pci_channel_offline(dev->persist->pdev) ||
446 (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)) {
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000447 /*
448 * Device is going through error recovery
449 * and cannot accept commands.
450 */
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000451 goto out;
452 }
453
Roland Dreier225c7b12007-05-08 18:00:38 -0700454 end = jiffies;
455 if (event)
Dotan Barak36ce10d2007-08-07 11:18:52 +0300456 end += msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS);
Roland Dreier225c7b12007-05-08 18:00:38 -0700457
458 while (cmd_pending(dev)) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200459 if (pci_channel_offline(dev->persist->pdev)) {
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000460 /*
461 * Device is going through error recovery
462 * and cannot accept commands.
463 */
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000464 goto out;
465 }
466
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000467 if (time_after_eq(jiffies, end)) {
468 mlx4_err(dev, "%s:cmd_pending failed\n", __func__);
Roland Dreier225c7b12007-05-08 18:00:38 -0700469 goto out;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000470 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700471 cond_resched();
472 }
473
474 /*
475 * We use writel (instead of something like memcpy_toio)
476 * because writes of less than 32 bits to the HCR don't work
477 * (and some architectures such as ia64 implement memcpy_toio
478 * in terms of writeb).
479 */
480 __raw_writel((__force u32) cpu_to_be32(in_param >> 32), hcr + 0);
481 __raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful), hcr + 1);
482 __raw_writel((__force u32) cpu_to_be32(in_modifier), hcr + 2);
483 __raw_writel((__force u32) cpu_to_be32(out_param >> 32), hcr + 3);
484 __raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), hcr + 4);
485 __raw_writel((__force u32) cpu_to_be32(token << 16), hcr + 5);
486
487 /* __raw_writel may not order writes. */
488 wmb();
489
490 __raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT) |
491 (cmd->toggle << HCR_T_BIT) |
492 (event ? (1 << HCR_E_BIT) : 0) |
493 (op_modifier << HCR_OPMOD_SHIFT) |
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000494 op), hcr + 6);
Roland Dreier2e61c642007-10-09 19:59:18 -0700495
496 /*
497 * Make sure that our HCR writes don't get mixed in with
498 * writes from another CPU starting a FW command.
499 */
500 mmiowb();
501
Roland Dreier225c7b12007-05-08 18:00:38 -0700502 cmd->toggle = cmd->toggle ^ 1;
503
504 ret = 0;
505
506out:
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200507 if (ret)
508 mlx4_warn(dev, "Could not post command 0x%x: ret=%d, in_param=0x%llx, in_mod=0x%x, op_mod=0x%x\n",
509 op, ret, in_param, in_modifier, op_modifier);
510 mutex_unlock(&dev->persist->device_state_mutex);
511
Roland Dreier225c7b12007-05-08 18:00:38 -0700512 return ret;
513}
514
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000515static int mlx4_slave_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
516 int out_is_imm, u32 in_modifier, u8 op_modifier,
517 u16 op, unsigned long timeout)
518{
519 struct mlx4_priv *priv = mlx4_priv(dev);
520 struct mlx4_vhcr_cmd *vhcr = priv->mfunc.vhcr;
521 int ret;
522
Roland Dreierf3d4c892012-09-25 21:24:07 -0700523 mutex_lock(&priv->cmd.slave_cmd_mutex);
524
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000525 vhcr->in_param = cpu_to_be64(in_param);
526 vhcr->out_param = out_param ? cpu_to_be64(*out_param) : 0;
527 vhcr->in_modifier = cpu_to_be32(in_modifier);
528 vhcr->opcode = cpu_to_be16((((u16) op_modifier) << 12) | (op & 0xfff));
529 vhcr->token = cpu_to_be16(CMD_POLL_TOKEN);
530 vhcr->status = 0;
531 vhcr->flags = !!(priv->cmd.use_events) << 6;
Roland Dreierf3d4c892012-09-25 21:24:07 -0700532
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000533 if (mlx4_is_master(dev)) {
534 ret = mlx4_master_process_vhcr(dev, dev->caps.function, vhcr);
535 if (!ret) {
536 if (out_is_imm) {
537 if (out_param)
538 *out_param =
539 be64_to_cpu(vhcr->out_param);
540 else {
Joe Perches1a91de22014-05-07 12:52:57 -0700541 mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
542 op);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000543 vhcr->status = CMD_STAT_BAD_PARAM;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000544 }
545 }
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000546 ret = mlx4_status_to_errno(vhcr->status);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000547 }
Yishai Hadas0cd93022015-01-25 16:59:43 +0200548 if (ret &&
549 dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
550 ret = mlx4_internal_err_ret_value(dev, op, op_modifier);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000551 } else {
Yishai Hadas0cd93022015-01-25 16:59:43 +0200552 ret = mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_POST, 0, op,
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000553 MLX4_COMM_TIME + timeout);
554 if (!ret) {
555 if (out_is_imm) {
556 if (out_param)
557 *out_param =
558 be64_to_cpu(vhcr->out_param);
559 else {
Joe Perches1a91de22014-05-07 12:52:57 -0700560 mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
561 op);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000562 vhcr->status = CMD_STAT_BAD_PARAM;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000563 }
564 }
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000565 ret = mlx4_status_to_errno(vhcr->status);
Yishai Hadas0cd93022015-01-25 16:59:43 +0200566 } else {
567 if (dev->persist->state &
568 MLX4_DEVICE_STATE_INTERNAL_ERROR)
569 ret = mlx4_internal_err_ret_value(dev, op,
570 op_modifier);
571 else
572 mlx4_err(dev, "failed execution of VHCR_POST command opcode 0x%x\n", op);
573 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000574 }
Roland Dreierf3d4c892012-09-25 21:24:07 -0700575
576 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000577 return ret;
578}
579
Roland Dreier225c7b12007-05-08 18:00:38 -0700580static int mlx4_cmd_poll(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
581 int out_is_imm, u32 in_modifier, u8 op_modifier,
582 u16 op, unsigned long timeout)
583{
584 struct mlx4_priv *priv = mlx4_priv(dev);
585 void __iomem *hcr = priv->cmd.hcr;
586 int err = 0;
587 unsigned long end;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000588 u32 stat;
Roland Dreier225c7b12007-05-08 18:00:38 -0700589
590 down(&priv->cmd.poll_sem);
591
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200592 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000593 /*
594 * Device is going through error recovery
595 * and cannot accept commands.
596 */
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200597 err = mlx4_internal_err_ret_value(dev, op, op_modifier);
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000598 goto out;
599 }
600
Eyal Perryc05a1162014-05-14 12:15:13 +0300601 if (out_is_imm && !out_param) {
602 mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
603 op);
604 err = -EINVAL;
605 goto out;
606 }
607
Roland Dreier225c7b12007-05-08 18:00:38 -0700608 err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
609 in_modifier, op_modifier, op, CMD_POLL_TOKEN, 0);
610 if (err)
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200611 goto out_reset;
Roland Dreier225c7b12007-05-08 18:00:38 -0700612
613 end = msecs_to_jiffies(timeout) + jiffies;
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000614 while (cmd_pending(dev) && time_before(jiffies, end)) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200615 if (pci_channel_offline(dev->persist->pdev)) {
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000616 /*
617 * Device is going through error recovery
618 * and cannot accept commands.
619 */
620 err = -EIO;
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200621 goto out_reset;
622 }
623
624 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
625 err = mlx4_internal_err_ret_value(dev, op, op_modifier);
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000626 goto out;
627 }
628
Roland Dreier225c7b12007-05-08 18:00:38 -0700629 cond_resched();
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000630 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700631
632 if (cmd_pending(dev)) {
Dotan Barak674925e2013-06-25 12:09:37 +0300633 mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
634 op);
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200635 err = -EIO;
636 goto out_reset;
Roland Dreier225c7b12007-05-08 18:00:38 -0700637 }
638
639 if (out_is_imm)
640 *out_param =
641 (u64) be32_to_cpu((__force __be32)
642 __raw_readl(hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
643 (u64) be32_to_cpu((__force __be32)
644 __raw_readl(hcr + HCR_OUT_PARAM_OFFSET + 4));
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000645 stat = be32_to_cpu((__force __be32)
646 __raw_readl(hcr + HCR_STATUS_OFFSET)) >> 24;
647 err = mlx4_status_to_errno(stat);
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200648 if (err) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000649 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
650 op, stat);
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200651 if (mlx4_closing_cmd_fatal_error(op, stat))
652 goto out_reset;
653 goto out;
654 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700655
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200656out_reset:
657 if (err)
658 err = mlx4_cmd_reset_flow(dev, op, op_modifier, err);
Roland Dreier225c7b12007-05-08 18:00:38 -0700659out:
660 up(&priv->cmd.poll_sem);
661 return err;
662}
663
664void mlx4_cmd_event(struct mlx4_dev *dev, u16 token, u8 status, u64 out_param)
665{
666 struct mlx4_priv *priv = mlx4_priv(dev);
667 struct mlx4_cmd_context *context =
668 &priv->cmd.context[token & priv->cmd.token_mask];
669
670 /* previously timed out command completing at long last */
671 if (token != context->token)
672 return;
673
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000674 context->fw_status = status;
Roland Dreier225c7b12007-05-08 18:00:38 -0700675 context->result = mlx4_status_to_errno(status);
676 context->out_param = out_param;
677
Roland Dreier225c7b12007-05-08 18:00:38 -0700678 complete(&context->done);
679}
680
681static int mlx4_cmd_wait(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
682 int out_is_imm, u32 in_modifier, u8 op_modifier,
683 u16 op, unsigned long timeout)
684{
685 struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
686 struct mlx4_cmd_context *context;
687 int err = 0;
688
689 down(&cmd->event_sem);
690
691 spin_lock(&cmd->context_lock);
692 BUG_ON(cmd->free_head < 0);
693 context = &cmd->context[cmd->free_head];
Roland Dreier09815822007-07-20 21:19:43 -0700694 context->token += cmd->token_mask + 1;
Roland Dreier225c7b12007-05-08 18:00:38 -0700695 cmd->free_head = context->next;
696 spin_unlock(&cmd->context_lock);
697
Eyal Perryc05a1162014-05-14 12:15:13 +0300698 if (out_is_imm && !out_param) {
699 mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
700 op);
701 err = -EINVAL;
702 goto out;
703 }
704
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200705 reinit_completion(&context->done);
Roland Dreier225c7b12007-05-08 18:00:38 -0700706
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200707 err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
708 in_modifier, op_modifier, op, context->token, 1);
709 if (err)
710 goto out_reset;
Roland Dreier225c7b12007-05-08 18:00:38 -0700711
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000712 if (!wait_for_completion_timeout(&context->done,
713 msecs_to_jiffies(timeout))) {
Dotan Barak674925e2013-06-25 12:09:37 +0300714 mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
715 op);
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200716 err = -EIO;
717 goto out_reset;
Roland Dreier225c7b12007-05-08 18:00:38 -0700718 }
719
720 err = context->result;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000721 if (err) {
Jack Morgenstein1daa4302014-09-30 12:03:50 +0300722 /* Since we do not want to have this error message always
723 * displayed at driver start when there are ConnectX2 HCAs
724 * on the host, we deprecate the error message for this
725 * specific command/input_mod/opcode_mod/fw-status to be debug.
726 */
727 if (op == MLX4_CMD_SET_PORT && in_modifier == 1 &&
728 op_modifier == 0 && context->fw_status == CMD_STAT_BAD_SIZE)
729 mlx4_dbg(dev, "command 0x%x failed: fw status = 0x%x\n",
730 op, context->fw_status);
731 else
732 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
733 op, context->fw_status);
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200734 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
735 err = mlx4_internal_err_ret_value(dev, op, op_modifier);
736 else if (mlx4_closing_cmd_fatal_error(op, context->fw_status))
737 goto out_reset;
738
Roland Dreier225c7b12007-05-08 18:00:38 -0700739 goto out;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000740 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700741
742 if (out_is_imm)
743 *out_param = context->out_param;
744
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200745out_reset:
746 if (err)
747 err = mlx4_cmd_reset_flow(dev, op, op_modifier, err);
Roland Dreier225c7b12007-05-08 18:00:38 -0700748out:
749 spin_lock(&cmd->context_lock);
750 context->next = cmd->free_head;
751 cmd->free_head = context - cmd->context;
752 spin_unlock(&cmd->context_lock);
753
754 up(&cmd->event_sem);
755 return err;
756}
757
758int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
759 int out_is_imm, u32 in_modifier, u8 op_modifier,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000760 u16 op, unsigned long timeout, int native)
Roland Dreier225c7b12007-05-08 18:00:38 -0700761{
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200762 if (pci_channel_offline(dev->persist->pdev))
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200763 return mlx4_cmd_reset_flow(dev, op, op_modifier, -EIO);
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000764
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000765 if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
Yishai Hadasf5aef5a2015-01-25 16:59:39 +0200766 if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
767 return mlx4_internal_err_ret_value(dev, op,
768 op_modifier);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000769 if (mlx4_priv(dev)->cmd.use_events)
770 return mlx4_cmd_wait(dev, in_param, out_param,
771 out_is_imm, in_modifier,
772 op_modifier, op, timeout);
773 else
774 return mlx4_cmd_poll(dev, in_param, out_param,
775 out_is_imm, in_modifier,
776 op_modifier, op, timeout);
777 }
778 return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
779 in_modifier, op_modifier, op, timeout);
Roland Dreier225c7b12007-05-08 18:00:38 -0700780}
781EXPORT_SYMBOL_GPL(__mlx4_cmd);
782
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000783
Yishai Hadas55ad3592015-01-25 16:59:42 +0200784int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000785{
786 return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
787 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
788}
789
790static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
791 int slave, u64 slave_addr,
792 int size, int is_read)
793{
794 u64 in_param;
795 u64 out_param;
796
797 if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
798 (slave & ~0x7f) | (size & 0xff)) {
Joe Perches1a91de22014-05-07 12:52:57 -0700799 mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx master_addr:0x%llx slave_id:%d size:%d\n",
800 slave_addr, master_addr, slave, size);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000801 return -EINVAL;
802 }
803
804 if (is_read) {
805 in_param = (u64) slave | slave_addr;
806 out_param = (u64) dev->caps.function | master_addr;
807 } else {
808 in_param = (u64) dev->caps.function | master_addr;
809 out_param = (u64) slave | slave_addr;
810 }
811
812 return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
813 MLX4_CMD_ACCESS_MEM,
814 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
815}
816
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000817static int query_pkey_block(struct mlx4_dev *dev, u8 port, u16 index, u16 *pkey,
818 struct mlx4_cmd_mailbox *inbox,
819 struct mlx4_cmd_mailbox *outbox)
820{
821 struct ib_smp *in_mad = (struct ib_smp *)(inbox->buf);
822 struct ib_smp *out_mad = (struct ib_smp *)(outbox->buf);
823 int err;
824 int i;
825
826 if (index & 0x1f)
827 return -EINVAL;
828
829 in_mad->attr_mod = cpu_to_be32(index / 32);
830
831 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
832 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
833 MLX4_CMD_NATIVE);
834 if (err)
835 return err;
836
837 for (i = 0; i < 32; ++i)
838 pkey[i] = be16_to_cpu(((__be16 *) out_mad->data)[i]);
839
840 return err;
841}
842
843static int get_full_pkey_table(struct mlx4_dev *dev, u8 port, u16 *table,
844 struct mlx4_cmd_mailbox *inbox,
845 struct mlx4_cmd_mailbox *outbox)
846{
847 int i;
848 int err;
849
850 for (i = 0; i < dev->caps.pkey_table_len[port]; i += 32) {
851 err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
852 if (err)
853 return err;
854 }
855
856 return 0;
857}
858#define PORT_CAPABILITY_LOCATION_IN_SMP 20
859#define PORT_STATE_OFFSET 32
860
861static enum ib_port_state vf_port_state(struct mlx4_dev *dev, int port, int vf)
862{
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000863 if (mlx4_get_slave_port_state(dev, vf, port) == SLAVE_PORT_UP)
864 return IB_PORT_ACTIVE;
865 else
866 return IB_PORT_DOWN;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000867}
868
869static int mlx4_MAD_IFC_wrapper(struct mlx4_dev *dev, int slave,
870 struct mlx4_vhcr *vhcr,
871 struct mlx4_cmd_mailbox *inbox,
872 struct mlx4_cmd_mailbox *outbox,
873 struct mlx4_cmd_info *cmd)
874{
875 struct ib_smp *smp = inbox->buf;
876 u32 index;
877 u8 port;
Jack Morgenstein97982f52014-05-29 16:31:02 +0300878 u8 opcode_modifier;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000879 u16 *table;
880 int err;
881 int vidx, pidx;
Jack Morgenstein97982f52014-05-29 16:31:02 +0300882 int network_view;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000883 struct mlx4_priv *priv = mlx4_priv(dev);
884 struct ib_smp *outsmp = outbox->buf;
885 __be16 *outtab = (__be16 *)(outsmp->data);
886 __be32 slave_cap_mask;
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000887 __be64 slave_node_guid;
Jack Morgenstein97982f52014-05-29 16:31:02 +0300888
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000889 port = vhcr->in_modifier;
890
Jack Morgenstein97982f52014-05-29 16:31:02 +0300891 /* network-view bit is for driver use only, and should not be passed to FW */
892 opcode_modifier = vhcr->op_modifier & ~0x8; /* clear netw view bit */
893 network_view = !!(vhcr->op_modifier & 0x8);
894
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000895 if (smp->base_version == 1 &&
896 smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
897 smp->class_version == 1) {
Jack Morgenstein97982f52014-05-29 16:31:02 +0300898 /* host view is paravirtualized */
899 if (!network_view && smp->method == IB_MGMT_METHOD_GET) {
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000900 if (smp->attr_id == IB_SMP_ATTR_PKEY_TABLE) {
901 index = be32_to_cpu(smp->attr_mod);
902 if (port < 1 || port > dev->caps.num_ports)
903 return -EINVAL;
Matan Barak19ab5742015-01-27 15:58:07 +0200904 table = kcalloc((dev->caps.pkey_table_len[port] / 32) + 1,
905 sizeof(*table) * 32, GFP_KERNEL);
906
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000907 if (!table)
908 return -ENOMEM;
909 /* need to get the full pkey table because the paravirtualized
910 * pkeys may be scattered among several pkey blocks.
911 */
912 err = get_full_pkey_table(dev, port, table, inbox, outbox);
913 if (!err) {
914 for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
915 pidx = priv->virt2phys_pkey[slave][port - 1][vidx];
916 outtab[vidx % 32] = cpu_to_be16(table[pidx]);
917 }
918 }
919 kfree(table);
920 return err;
921 }
922 if (smp->attr_id == IB_SMP_ATTR_PORT_INFO) {
923 /*get the slave specific caps:*/
924 /*do the command */
925 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Jack Morgenstein97982f52014-05-29 16:31:02 +0300926 vhcr->in_modifier, opcode_modifier,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000927 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
928 /* modify the response for slaves */
929 if (!err && slave != mlx4_master_func_num(dev)) {
930 u8 *state = outsmp->data + PORT_STATE_OFFSET;
931
932 *state = (*state & 0xf0) | vf_port_state(dev, port, slave);
933 slave_cap_mask = priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
934 memcpy(outsmp->data + PORT_CAPABILITY_LOCATION_IN_SMP, &slave_cap_mask, 4);
935 }
936 return err;
937 }
938 if (smp->attr_id == IB_SMP_ATTR_GUID_INFO) {
939 /* compute slave's gid block */
940 smp->attr_mod = cpu_to_be32(slave / 8);
941 /* execute cmd */
942 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Jack Morgenstein97982f52014-05-29 16:31:02 +0300943 vhcr->in_modifier, opcode_modifier,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000944 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
945 if (!err) {
946 /* if needed, move slave gid to index 0 */
947 if (slave % 8)
948 memcpy(outsmp->data,
949 outsmp->data + (slave % 8) * 8, 8);
950 /* delete all other gids */
951 memset(outsmp->data + 8, 0, 56);
952 }
953 return err;
954 }
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000955 if (smp->attr_id == IB_SMP_ATTR_NODE_INFO) {
956 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Jack Morgenstein97982f52014-05-29 16:31:02 +0300957 vhcr->in_modifier, opcode_modifier,
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000958 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
959 if (!err) {
960 slave_node_guid = mlx4_get_slave_node_guid(dev, slave);
961 memcpy(outsmp->data + 12, &slave_node_guid, 8);
962 }
963 return err;
964 }
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000965 }
966 }
Jack Morgenstein97982f52014-05-29 16:31:02 +0300967
968 /* Non-privileged VFs are only allowed "host" view LID-routed 'Get' MADs.
969 * These are the MADs used by ib verbs (such as ib_query_gids).
970 */
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000971 if (slave != mlx4_master_func_num(dev) &&
Jack Morgenstein97982f52014-05-29 16:31:02 +0300972 !mlx4_vf_smi_enabled(dev, slave, port)) {
973 if (!(smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
974 smp->method == IB_MGMT_METHOD_GET) || network_view) {
975 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",
976 slave, smp->method, smp->mgmt_class,
977 network_view ? "Network" : "Host",
978 be16_to_cpu(smp->attr_id));
979 return -EPERM;
980 }
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000981 }
Jack Morgenstein97982f52014-05-29 16:31:02 +0300982
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000983 return mlx4_cmd_box(dev, inbox->dma, outbox->dma,
Jack Morgenstein97982f52014-05-29 16:31:02 +0300984 vhcr->in_modifier, opcode_modifier,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000985 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
986}
987
Or Gerlitzb7475792014-03-27 14:02:02 +0200988static int mlx4_CMD_EPERM_wrapper(struct mlx4_dev *dev, int slave,
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +0300989 struct mlx4_vhcr *vhcr,
990 struct mlx4_cmd_mailbox *inbox,
991 struct mlx4_cmd_mailbox *outbox,
992 struct mlx4_cmd_info *cmd)
993{
994 return -EPERM;
995}
996
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000997int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
998 struct mlx4_vhcr *vhcr,
999 struct mlx4_cmd_mailbox *inbox,
1000 struct mlx4_cmd_mailbox *outbox,
1001 struct mlx4_cmd_info *cmd)
1002{
1003 u64 in_param;
1004 u64 out_param;
1005 int err;
1006
1007 in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
1008 out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
1009 if (cmd->encode_slave_id) {
1010 in_param &= 0xffffffffffffff00ll;
1011 in_param |= slave;
1012 }
1013
1014 err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
1015 vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
1016 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
1017
1018 if (cmd->out_is_imm)
1019 vhcr->out_param = out_param;
1020
1021 return err;
1022}
1023
1024static struct mlx4_cmd_info cmd_info[] = {
1025 {
1026 .opcode = MLX4_CMD_QUERY_FW,
1027 .has_inbox = false,
1028 .has_outbox = true,
1029 .out_is_imm = false,
1030 .encode_slave_id = false,
1031 .verify = NULL,
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +00001032 .wrapper = mlx4_QUERY_FW_wrapper
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001033 },
1034 {
1035 .opcode = MLX4_CMD_QUERY_HCA,
1036 .has_inbox = false,
1037 .has_outbox = true,
1038 .out_is_imm = false,
1039 .encode_slave_id = false,
1040 .verify = NULL,
1041 .wrapper = NULL
1042 },
1043 {
1044 .opcode = MLX4_CMD_QUERY_DEV_CAP,
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_DEV_CAP_wrapper
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001051 },
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001052 {
1053 .opcode = MLX4_CMD_QUERY_FUNC_CAP,
1054 .has_inbox = false,
1055 .has_outbox = true,
1056 .out_is_imm = false,
1057 .encode_slave_id = false,
1058 .verify = NULL,
1059 .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
1060 },
1061 {
1062 .opcode = MLX4_CMD_QUERY_ADAPTER,
1063 .has_inbox = false,
1064 .has_outbox = true,
1065 .out_is_imm = false,
1066 .encode_slave_id = false,
1067 .verify = NULL,
1068 .wrapper = NULL
1069 },
1070 {
1071 .opcode = MLX4_CMD_INIT_PORT,
1072 .has_inbox = false,
1073 .has_outbox = false,
1074 .out_is_imm = false,
1075 .encode_slave_id = false,
1076 .verify = NULL,
1077 .wrapper = mlx4_INIT_PORT_wrapper
1078 },
1079 {
1080 .opcode = MLX4_CMD_CLOSE_PORT,
1081 .has_inbox = false,
1082 .has_outbox = false,
1083 .out_is_imm = false,
1084 .encode_slave_id = false,
1085 .verify = NULL,
1086 .wrapper = mlx4_CLOSE_PORT_wrapper
1087 },
1088 {
1089 .opcode = MLX4_CMD_QUERY_PORT,
1090 .has_inbox = false,
1091 .has_outbox = true,
1092 .out_is_imm = false,
1093 .encode_slave_id = false,
1094 .verify = NULL,
1095 .wrapper = mlx4_QUERY_PORT_wrapper
1096 },
1097 {
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001098 .opcode = MLX4_CMD_SET_PORT,
1099 .has_inbox = true,
1100 .has_outbox = false,
1101 .out_is_imm = false,
1102 .encode_slave_id = false,
1103 .verify = NULL,
1104 .wrapper = mlx4_SET_PORT_wrapper
1105 },
1106 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001107 .opcode = MLX4_CMD_MAP_EQ,
1108 .has_inbox = false,
1109 .has_outbox = false,
1110 .out_is_imm = false,
1111 .encode_slave_id = false,
1112 .verify = NULL,
1113 .wrapper = mlx4_MAP_EQ_wrapper
1114 },
1115 {
1116 .opcode = MLX4_CMD_SW2HW_EQ,
1117 .has_inbox = true,
1118 .has_outbox = false,
1119 .out_is_imm = false,
1120 .encode_slave_id = true,
1121 .verify = NULL,
1122 .wrapper = mlx4_SW2HW_EQ_wrapper
1123 },
1124 {
1125 .opcode = MLX4_CMD_HW_HEALTH_CHECK,
1126 .has_inbox = false,
1127 .has_outbox = false,
1128 .out_is_imm = false,
1129 .encode_slave_id = false,
1130 .verify = NULL,
1131 .wrapper = NULL
1132 },
1133 {
1134 .opcode = MLX4_CMD_NOP,
1135 .has_inbox = false,
1136 .has_outbox = false,
1137 .out_is_imm = false,
1138 .encode_slave_id = false,
1139 .verify = NULL,
1140 .wrapper = NULL
1141 },
1142 {
Or Gerlitzd18f1412014-03-27 14:02:03 +02001143 .opcode = MLX4_CMD_CONFIG_DEV,
1144 .has_inbox = false,
Matan Barakd475c952014-11-02 16:26:17 +02001145 .has_outbox = true,
Or Gerlitzd18f1412014-03-27 14:02:03 +02001146 .out_is_imm = false,
1147 .encode_slave_id = false,
1148 .verify = NULL,
Matan Barakd475c952014-11-02 16:26:17 +02001149 .wrapper = mlx4_CONFIG_DEV_wrapper
Or Gerlitzd18f1412014-03-27 14:02:03 +02001150 },
1151 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001152 .opcode = MLX4_CMD_ALLOC_RES,
1153 .has_inbox = false,
1154 .has_outbox = false,
1155 .out_is_imm = true,
1156 .encode_slave_id = false,
1157 .verify = NULL,
1158 .wrapper = mlx4_ALLOC_RES_wrapper
1159 },
1160 {
1161 .opcode = MLX4_CMD_FREE_RES,
1162 .has_inbox = false,
1163 .has_outbox = false,
1164 .out_is_imm = false,
1165 .encode_slave_id = false,
1166 .verify = NULL,
1167 .wrapper = mlx4_FREE_RES_wrapper
1168 },
1169 {
1170 .opcode = MLX4_CMD_SW2HW_MPT,
1171 .has_inbox = true,
1172 .has_outbox = false,
1173 .out_is_imm = false,
1174 .encode_slave_id = true,
1175 .verify = NULL,
1176 .wrapper = mlx4_SW2HW_MPT_wrapper
1177 },
1178 {
1179 .opcode = MLX4_CMD_QUERY_MPT,
1180 .has_inbox = false,
1181 .has_outbox = true,
1182 .out_is_imm = false,
1183 .encode_slave_id = false,
1184 .verify = NULL,
1185 .wrapper = mlx4_QUERY_MPT_wrapper
1186 },
1187 {
1188 .opcode = MLX4_CMD_HW2SW_MPT,
1189 .has_inbox = false,
1190 .has_outbox = false,
1191 .out_is_imm = false,
1192 .encode_slave_id = false,
1193 .verify = NULL,
1194 .wrapper = mlx4_HW2SW_MPT_wrapper
1195 },
1196 {
1197 .opcode = MLX4_CMD_READ_MTT,
1198 .has_inbox = false,
1199 .has_outbox = true,
1200 .out_is_imm = false,
1201 .encode_slave_id = false,
1202 .verify = NULL,
1203 .wrapper = NULL
1204 },
1205 {
1206 .opcode = MLX4_CMD_WRITE_MTT,
1207 .has_inbox = true,
1208 .has_outbox = false,
1209 .out_is_imm = false,
1210 .encode_slave_id = false,
1211 .verify = NULL,
1212 .wrapper = mlx4_WRITE_MTT_wrapper
1213 },
1214 {
1215 .opcode = MLX4_CMD_SYNC_TPT,
1216 .has_inbox = true,
1217 .has_outbox = false,
1218 .out_is_imm = false,
1219 .encode_slave_id = false,
1220 .verify = NULL,
1221 .wrapper = NULL
1222 },
1223 {
1224 .opcode = MLX4_CMD_HW2SW_EQ,
1225 .has_inbox = false,
Jack Morgenstein30a5da52015-01-27 15:58:03 +02001226 .has_outbox = false,
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001227 .out_is_imm = false,
1228 .encode_slave_id = true,
1229 .verify = NULL,
1230 .wrapper = mlx4_HW2SW_EQ_wrapper
1231 },
1232 {
1233 .opcode = MLX4_CMD_QUERY_EQ,
1234 .has_inbox = false,
1235 .has_outbox = true,
1236 .out_is_imm = false,
1237 .encode_slave_id = true,
1238 .verify = NULL,
1239 .wrapper = mlx4_QUERY_EQ_wrapper
1240 },
1241 {
1242 .opcode = MLX4_CMD_SW2HW_CQ,
1243 .has_inbox = true,
1244 .has_outbox = false,
1245 .out_is_imm = false,
1246 .encode_slave_id = true,
1247 .verify = NULL,
1248 .wrapper = mlx4_SW2HW_CQ_wrapper
1249 },
1250 {
1251 .opcode = MLX4_CMD_HW2SW_CQ,
1252 .has_inbox = false,
1253 .has_outbox = false,
1254 .out_is_imm = false,
1255 .encode_slave_id = false,
1256 .verify = NULL,
1257 .wrapper = mlx4_HW2SW_CQ_wrapper
1258 },
1259 {
1260 .opcode = MLX4_CMD_QUERY_CQ,
1261 .has_inbox = false,
1262 .has_outbox = true,
1263 .out_is_imm = false,
1264 .encode_slave_id = false,
1265 .verify = NULL,
1266 .wrapper = mlx4_QUERY_CQ_wrapper
1267 },
1268 {
1269 .opcode = MLX4_CMD_MODIFY_CQ,
1270 .has_inbox = true,
1271 .has_outbox = false,
1272 .out_is_imm = true,
1273 .encode_slave_id = false,
1274 .verify = NULL,
1275 .wrapper = mlx4_MODIFY_CQ_wrapper
1276 },
1277 {
1278 .opcode = MLX4_CMD_SW2HW_SRQ,
1279 .has_inbox = true,
1280 .has_outbox = false,
1281 .out_is_imm = false,
1282 .encode_slave_id = true,
1283 .verify = NULL,
1284 .wrapper = mlx4_SW2HW_SRQ_wrapper
1285 },
1286 {
1287 .opcode = MLX4_CMD_HW2SW_SRQ,
1288 .has_inbox = false,
1289 .has_outbox = false,
1290 .out_is_imm = false,
1291 .encode_slave_id = false,
1292 .verify = NULL,
1293 .wrapper = mlx4_HW2SW_SRQ_wrapper
1294 },
1295 {
1296 .opcode = MLX4_CMD_QUERY_SRQ,
1297 .has_inbox = false,
1298 .has_outbox = true,
1299 .out_is_imm = false,
1300 .encode_slave_id = false,
1301 .verify = NULL,
1302 .wrapper = mlx4_QUERY_SRQ_wrapper
1303 },
1304 {
1305 .opcode = MLX4_CMD_ARM_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_ARM_SRQ_wrapper
1312 },
1313 {
1314 .opcode = MLX4_CMD_RST2INIT_QP,
1315 .has_inbox = true,
1316 .has_outbox = false,
1317 .out_is_imm = false,
1318 .encode_slave_id = true,
1319 .verify = NULL,
1320 .wrapper = mlx4_RST2INIT_QP_wrapper
1321 },
1322 {
1323 .opcode = MLX4_CMD_INIT2INIT_QP,
1324 .has_inbox = true,
1325 .has_outbox = false,
1326 .out_is_imm = false,
1327 .encode_slave_id = false,
1328 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001329 .wrapper = mlx4_INIT2INIT_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001330 },
1331 {
1332 .opcode = MLX4_CMD_INIT2RTR_QP,
1333 .has_inbox = true,
1334 .has_outbox = false,
1335 .out_is_imm = false,
1336 .encode_slave_id = false,
1337 .verify = NULL,
1338 .wrapper = mlx4_INIT2RTR_QP_wrapper
1339 },
1340 {
1341 .opcode = MLX4_CMD_RTR2RTS_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_RTR2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001348 },
1349 {
1350 .opcode = MLX4_CMD_RTS2RTS_QP,
1351 .has_inbox = true,
1352 .has_outbox = false,
1353 .out_is_imm = false,
1354 .encode_slave_id = false,
1355 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001356 .wrapper = mlx4_RTS2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001357 },
1358 {
1359 .opcode = MLX4_CMD_SQERR2RTS_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_SQERR2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001366 },
1367 {
1368 .opcode = MLX4_CMD_2ERR_QP,
1369 .has_inbox = false,
1370 .has_outbox = false,
1371 .out_is_imm = false,
1372 .encode_slave_id = false,
1373 .verify = NULL,
1374 .wrapper = mlx4_GEN_QP_wrapper
1375 },
1376 {
1377 .opcode = MLX4_CMD_RTS2SQD_QP,
1378 .has_inbox = false,
1379 .has_outbox = false,
1380 .out_is_imm = false,
1381 .encode_slave_id = false,
1382 .verify = NULL,
1383 .wrapper = mlx4_GEN_QP_wrapper
1384 },
1385 {
1386 .opcode = MLX4_CMD_SQD2SQD_QP,
1387 .has_inbox = true,
1388 .has_outbox = false,
1389 .out_is_imm = false,
1390 .encode_slave_id = false,
1391 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001392 .wrapper = mlx4_SQD2SQD_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001393 },
1394 {
1395 .opcode = MLX4_CMD_SQD2RTS_QP,
1396 .has_inbox = true,
1397 .has_outbox = false,
1398 .out_is_imm = false,
1399 .encode_slave_id = false,
1400 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001401 .wrapper = mlx4_SQD2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001402 },
1403 {
1404 .opcode = MLX4_CMD_2RST_QP,
1405 .has_inbox = false,
1406 .has_outbox = false,
1407 .out_is_imm = false,
1408 .encode_slave_id = false,
1409 .verify = NULL,
1410 .wrapper = mlx4_2RST_QP_wrapper
1411 },
1412 {
1413 .opcode = MLX4_CMD_QUERY_QP,
1414 .has_inbox = false,
1415 .has_outbox = true,
1416 .out_is_imm = false,
1417 .encode_slave_id = false,
1418 .verify = NULL,
1419 .wrapper = mlx4_GEN_QP_wrapper
1420 },
1421 {
1422 .opcode = MLX4_CMD_SUSPEND_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_GEN_QP_wrapper
1429 },
1430 {
1431 .opcode = MLX4_CMD_UNSUSPEND_QP,
1432 .has_inbox = false,
1433 .has_outbox = false,
1434 .out_is_imm = false,
1435 .encode_slave_id = false,
1436 .verify = NULL,
1437 .wrapper = mlx4_GEN_QP_wrapper
1438 },
1439 {
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001440 .opcode = MLX4_CMD_UPDATE_QP,
Matan Barakce8d9e02014-05-15 15:29:27 +03001441 .has_inbox = true,
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001442 .has_outbox = false,
1443 .out_is_imm = false,
1444 .encode_slave_id = false,
1445 .verify = NULL,
Matan Barakce8d9e02014-05-15 15:29:27 +03001446 .wrapper = mlx4_UPDATE_QP_wrapper
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001447 },
1448 {
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001449 .opcode = MLX4_CMD_GET_OP_REQ,
1450 .has_inbox = false,
1451 .has_outbox = false,
1452 .out_is_imm = false,
1453 .encode_slave_id = false,
1454 .verify = NULL,
Or Gerlitzb7475792014-03-27 14:02:02 +02001455 .wrapper = mlx4_CMD_EPERM_wrapper,
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001456 },
1457 {
Ido Shamay7e95bb92015-04-02 16:31:11 +03001458 .opcode = MLX4_CMD_ALLOCATE_VPP,
1459 .has_inbox = false,
1460 .has_outbox = true,
1461 .out_is_imm = false,
1462 .encode_slave_id = false,
1463 .verify = NULL,
1464 .wrapper = mlx4_CMD_EPERM_wrapper,
1465 },
1466 {
Ido Shamay1c291462015-04-02 16:31:12 +03001467 .opcode = MLX4_CMD_SET_VPORT_QOS,
1468 .has_inbox = false,
1469 .has_outbox = true,
1470 .out_is_imm = false,
1471 .encode_slave_id = false,
1472 .verify = NULL,
1473 .wrapper = mlx4_CMD_EPERM_wrapper,
1474 },
1475 {
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001476 .opcode = MLX4_CMD_CONF_SPECIAL_QP,
1477 .has_inbox = false,
1478 .has_outbox = false,
1479 .out_is_imm = false,
1480 .encode_slave_id = false,
1481 .verify = NULL, /* XXX verify: only demux can do this */
1482 .wrapper = NULL
1483 },
1484 {
1485 .opcode = MLX4_CMD_MAD_IFC,
1486 .has_inbox = true,
1487 .has_outbox = true,
1488 .out_is_imm = false,
1489 .encode_slave_id = false,
1490 .verify = NULL,
1491 .wrapper = mlx4_MAD_IFC_wrapper
1492 },
1493 {
Jack Morgenstein114840c2014-06-01 11:53:50 +03001494 .opcode = MLX4_CMD_MAD_DEMUX,
1495 .has_inbox = false,
1496 .has_outbox = false,
1497 .out_is_imm = false,
1498 .encode_slave_id = false,
1499 .verify = NULL,
1500 .wrapper = mlx4_CMD_EPERM_wrapper
1501 },
1502 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001503 .opcode = MLX4_CMD_QUERY_IF_STAT,
1504 .has_inbox = false,
1505 .has_outbox = true,
1506 .out_is_imm = false,
1507 .encode_slave_id = false,
1508 .verify = NULL,
1509 .wrapper = mlx4_QUERY_IF_STAT_wrapper
1510 },
Saeed Mahameedadbc7ac2014-10-27 11:37:37 +02001511 {
1512 .opcode = MLX4_CMD_ACCESS_REG,
1513 .has_inbox = true,
1514 .has_outbox = true,
1515 .out_is_imm = false,
1516 .encode_slave_id = false,
1517 .verify = NULL,
Saeed Mahameed6e806692014-11-02 16:26:13 +02001518 .wrapper = mlx4_ACCESS_REG_wrapper,
Saeed Mahameedadbc7ac2014-10-27 11:37:37 +02001519 },
Shani Michaelid237baa2015-03-05 20:16:12 +02001520 {
1521 .opcode = MLX4_CMD_CONGESTION_CTRL_OPCODE,
1522 .has_inbox = false,
1523 .has_outbox = false,
1524 .out_is_imm = false,
1525 .encode_slave_id = false,
1526 .verify = NULL,
1527 .wrapper = mlx4_CMD_EPERM_wrapper,
1528 },
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001529 /* Native multicast commands are not available for guests */
1530 {
1531 .opcode = MLX4_CMD_QP_ATTACH,
1532 .has_inbox = true,
1533 .has_outbox = false,
1534 .out_is_imm = false,
1535 .encode_slave_id = false,
1536 .verify = NULL,
1537 .wrapper = mlx4_QP_ATTACH_wrapper
1538 },
1539 {
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +00001540 .opcode = MLX4_CMD_PROMISC,
1541 .has_inbox = false,
1542 .has_outbox = false,
1543 .out_is_imm = false,
1544 .encode_slave_id = false,
1545 .verify = NULL,
1546 .wrapper = mlx4_PROMISC_wrapper
1547 },
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001548 /* Ethernet specific commands */
1549 {
1550 .opcode = MLX4_CMD_SET_VLAN_FLTR,
1551 .has_inbox = true,
1552 .has_outbox = false,
1553 .out_is_imm = false,
1554 .encode_slave_id = false,
1555 .verify = NULL,
1556 .wrapper = mlx4_SET_VLAN_FLTR_wrapper
1557 },
1558 {
1559 .opcode = MLX4_CMD_SET_MCAST_FLTR,
1560 .has_inbox = false,
1561 .has_outbox = false,
1562 .out_is_imm = false,
1563 .encode_slave_id = false,
1564 .verify = NULL,
1565 .wrapper = mlx4_SET_MCAST_FLTR_wrapper
1566 },
1567 {
1568 .opcode = MLX4_CMD_DUMP_ETH_STATS,
1569 .has_inbox = false,
1570 .has_outbox = true,
1571 .out_is_imm = false,
1572 .encode_slave_id = false,
1573 .verify = NULL,
1574 .wrapper = mlx4_DUMP_ETH_STATS_wrapper
1575 },
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +00001576 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001577 .opcode = MLX4_CMD_INFORM_FLR_DONE,
1578 .has_inbox = false,
1579 .has_outbox = false,
1580 .out_is_imm = false,
1581 .encode_slave_id = false,
1582 .verify = NULL,
1583 .wrapper = NULL
1584 },
Hadar Hen Zion8fcfb4d2012-07-05 04:03:45 +00001585 /* flow steering commands */
1586 {
1587 .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
1588 .has_inbox = true,
1589 .has_outbox = false,
1590 .out_is_imm = true,
1591 .encode_slave_id = false,
1592 .verify = NULL,
1593 .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
1594 },
1595 {
1596 .opcode = MLX4_QP_FLOW_STEERING_DETACH,
1597 .has_inbox = false,
1598 .has_outbox = false,
1599 .out_is_imm = false,
1600 .encode_slave_id = false,
1601 .verify = NULL,
1602 .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
1603 },
Matan Barak4de65802013-11-07 15:25:14 +02001604 {
1605 .opcode = MLX4_FLOW_STEERING_IB_UC_QP_RANGE,
1606 .has_inbox = false,
1607 .has_outbox = false,
1608 .out_is_imm = false,
1609 .encode_slave_id = false,
1610 .verify = NULL,
Or Gerlitzb7475792014-03-27 14:02:02 +02001611 .wrapper = mlx4_CMD_EPERM_wrapper
Matan Barak4de65802013-11-07 15:25:14 +02001612 },
Moni Shoua59e14e32015-02-03 16:48:32 +02001613 {
1614 .opcode = MLX4_CMD_VIRT_PORT_MAP,
1615 .has_inbox = false,
1616 .has_outbox = false,
1617 .out_is_imm = false,
1618 .encode_slave_id = false,
1619 .verify = NULL,
1620 .wrapper = mlx4_CMD_EPERM_wrapper
1621 },
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001622};
1623
1624static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
1625 struct mlx4_vhcr_cmd *in_vhcr)
1626{
1627 struct mlx4_priv *priv = mlx4_priv(dev);
1628 struct mlx4_cmd_info *cmd = NULL;
1629 struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
1630 struct mlx4_vhcr *vhcr;
1631 struct mlx4_cmd_mailbox *inbox = NULL;
1632 struct mlx4_cmd_mailbox *outbox = NULL;
1633 u64 in_param;
1634 u64 out_param;
1635 int ret = 0;
1636 int i;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001637 int err = 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001638
1639 /* Create sw representation of Virtual HCR */
1640 vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
1641 if (!vhcr)
1642 return -ENOMEM;
1643
1644 /* DMA in the vHCR */
1645 if (!in_vhcr) {
1646 ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1647 priv->mfunc.master.slave_state[slave].vhcr_dma,
1648 ALIGN(sizeof(struct mlx4_vhcr_cmd),
1649 MLX4_ACCESS_MEM_ALIGN), 1);
1650 if (ret) {
Yishai Hadas0cd93022015-01-25 16:59:43 +02001651 if (!(dev->persist->state &
1652 MLX4_DEVICE_STATE_INTERNAL_ERROR))
1653 mlx4_err(dev, "%s: Failed reading vhcr ret: 0x%x\n",
1654 __func__, ret);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001655 kfree(vhcr);
1656 return ret;
1657 }
1658 }
1659
1660 /* Fill SW VHCR fields */
1661 vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
1662 vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
1663 vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
1664 vhcr->token = be16_to_cpu(vhcr_cmd->token);
1665 vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
1666 vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
1667 vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
1668
1669 /* Lookup command */
1670 for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
1671 if (vhcr->op == cmd_info[i].opcode) {
1672 cmd = &cmd_info[i];
1673 break;
1674 }
1675 }
1676 if (!cmd) {
1677 mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
1678 vhcr->op, slave);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001679 vhcr_cmd->status = CMD_STAT_BAD_PARAM;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001680 goto out_status;
1681 }
1682
1683 /* Read inbox */
1684 if (cmd->has_inbox) {
1685 vhcr->in_param &= INBOX_MASK;
1686 inbox = mlx4_alloc_cmd_mailbox(dev);
1687 if (IS_ERR(inbox)) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001688 vhcr_cmd->status = CMD_STAT_BAD_SIZE;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001689 inbox = NULL;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001690 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001691 }
1692
Yishai Hadas0cd93022015-01-25 16:59:43 +02001693 ret = mlx4_ACCESS_MEM(dev, inbox->dma, slave,
1694 vhcr->in_param,
1695 MLX4_MAILBOX_SIZE, 1);
1696 if (ret) {
1697 if (!(dev->persist->state &
1698 MLX4_DEVICE_STATE_INTERNAL_ERROR))
1699 mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
1700 __func__, cmd->opcode);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001701 vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
1702 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001703 }
1704 }
1705
1706 /* Apply permission and bound checks if applicable */
1707 if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
Joe Perches1a91de22014-05-07 12:52:57 -07001708 mlx4_warn(dev, "Command:0x%x from slave: %d failed protection checks for resource_id:%d\n",
1709 vhcr->op, slave, vhcr->in_modifier);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001710 vhcr_cmd->status = CMD_STAT_BAD_OP;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001711 goto out_status;
1712 }
1713
1714 /* Allocate outbox */
1715 if (cmd->has_outbox) {
1716 outbox = mlx4_alloc_cmd_mailbox(dev);
1717 if (IS_ERR(outbox)) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001718 vhcr_cmd->status = CMD_STAT_BAD_SIZE;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001719 outbox = NULL;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001720 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001721 }
1722 }
1723
1724 /* Execute the command! */
1725 if (cmd->wrapper) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001726 err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
1727 cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001728 if (cmd->out_is_imm)
1729 vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1730 } else {
1731 in_param = cmd->has_inbox ? (u64) inbox->dma :
1732 vhcr->in_param;
1733 out_param = cmd->has_outbox ? (u64) outbox->dma :
1734 vhcr->out_param;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001735 err = __mlx4_cmd(dev, in_param, &out_param,
1736 cmd->out_is_imm, vhcr->in_modifier,
1737 vhcr->op_modifier, vhcr->op,
1738 MLX4_CMD_TIME_CLASS_A,
1739 MLX4_CMD_NATIVE);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001740
1741 if (cmd->out_is_imm) {
1742 vhcr->out_param = out_param;
1743 vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1744 }
1745 }
1746
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001747 if (err) {
Yishai Hadas0cd93022015-01-25 16:59:43 +02001748 if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR))
1749 mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with error:%d, status %d\n",
1750 vhcr->op, slave, vhcr->errno, err);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001751 vhcr_cmd->status = mlx4_errno_to_status(err);
1752 goto out_status;
1753 }
1754
1755
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001756 /* Write outbox if command completed successfully */
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001757 if (cmd->has_outbox && !vhcr_cmd->status) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001758 ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
1759 vhcr->out_param,
1760 MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
1761 if (ret) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001762 /* If we failed to write back the outbox after the
1763 *command was successfully executed, we must fail this
1764 * slave, as it is now in undefined state */
Yishai Hadas0cd93022015-01-25 16:59:43 +02001765 if (!(dev->persist->state &
1766 MLX4_DEVICE_STATE_INTERNAL_ERROR))
1767 mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001768 goto out;
1769 }
1770 }
1771
1772out_status:
1773 /* DMA back vhcr result */
1774 if (!in_vhcr) {
1775 ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1776 priv->mfunc.master.slave_state[slave].vhcr_dma,
1777 ALIGN(sizeof(struct mlx4_vhcr),
1778 MLX4_ACCESS_MEM_ALIGN),
1779 MLX4_CMD_WRAPPED);
1780 if (ret)
1781 mlx4_err(dev, "%s:Failed writing vhcr result\n",
1782 __func__);
1783 else if (vhcr->e_bit &&
1784 mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
Joe Perches1a91de22014-05-07 12:52:57 -07001785 mlx4_warn(dev, "Failed to generate command completion eqe for slave %d\n",
1786 slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001787 }
1788
1789out:
1790 kfree(vhcr);
1791 mlx4_free_cmd_mailbox(dev, inbox);
1792 mlx4_free_cmd_mailbox(dev, outbox);
1793 return ret;
1794}
1795
Jingoo Hanf0946682013-08-05 18:04:51 +09001796static int mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv *priv,
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001797 int slave, int port)
1798{
1799 struct mlx4_vport_oper_state *vp_oper;
1800 struct mlx4_vport_state *vp_admin;
1801 struct mlx4_vf_immed_vlan_work *work;
Rony Efraim0a6eac22013-06-27 19:05:22 +03001802 struct mlx4_dev *dev = &(priv->dev);
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001803 int err;
1804 int admin_vlan_ix = NO_INDX;
1805
1806 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1807 vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
1808
1809 if (vp_oper->state.default_vlan == vp_admin->default_vlan &&
Rony Efraim0a6eac22013-06-27 19:05:22 +03001810 vp_oper->state.default_qos == vp_admin->default_qos &&
1811 vp_oper->state.link_state == vp_admin->link_state)
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001812 return 0;
1813
Rony Efraim0a6eac22013-06-27 19:05:22 +03001814 if (!(priv->mfunc.master.slave_state[slave].active &&
Rony Efraimf0f829b2013-11-07 12:19:51 +02001815 dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)) {
Rony Efraim0a6eac22013-06-27 19:05:22 +03001816 /* even if the UPDATE_QP command isn't supported, we still want
1817 * to set this VF link according to the admin directive
1818 */
1819 vp_oper->state.link_state = vp_admin->link_state;
1820 return -1;
1821 }
1822
1823 mlx4_dbg(dev, "updating immediately admin params slave %d port %d\n",
1824 slave, port);
Joe Perches1a91de22014-05-07 12:52:57 -07001825 mlx4_dbg(dev, "vlan %d QoS %d link down %d\n",
1826 vp_admin->default_vlan, vp_admin->default_qos,
1827 vp_admin->link_state);
Rony Efraim0a6eac22013-06-27 19:05:22 +03001828
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001829 work = kzalloc(sizeof(*work), GFP_KERNEL);
1830 if (!work)
1831 return -ENOMEM;
1832
1833 if (vp_oper->state.default_vlan != vp_admin->default_vlan) {
Rony Efraimf0f829b2013-11-07 12:19:51 +02001834 if (MLX4_VGT != vp_admin->default_vlan) {
1835 err = __mlx4_register_vlan(&priv->dev, port,
1836 vp_admin->default_vlan,
1837 &admin_vlan_ix);
1838 if (err) {
1839 kfree(work);
Joe Perches1a91de22014-05-07 12:52:57 -07001840 mlx4_warn(&priv->dev,
Rony Efraimf0f829b2013-11-07 12:19:51 +02001841 "No vlan resources slave %d, port %d\n",
1842 slave, port);
1843 return err;
1844 }
1845 } else {
1846 admin_vlan_ix = NO_INDX;
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001847 }
1848 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_VLAN;
Joe Perches1a91de22014-05-07 12:52:57 -07001849 mlx4_dbg(&priv->dev,
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001850 "alloc vlan %d idx %d slave %d port %d\n",
1851 (int)(vp_admin->default_vlan),
1852 admin_vlan_ix, slave, port);
1853 }
1854
1855 /* save original vlan ix and vlan id */
1856 work->orig_vlan_id = vp_oper->state.default_vlan;
1857 work->orig_vlan_ix = vp_oper->vlan_idx;
1858
1859 /* handle new qos */
1860 if (vp_oper->state.default_qos != vp_admin->default_qos)
1861 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_QOS;
1862
1863 if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN)
1864 vp_oper->vlan_idx = admin_vlan_ix;
1865
1866 vp_oper->state.default_vlan = vp_admin->default_vlan;
1867 vp_oper->state.default_qos = vp_admin->default_qos;
Rony Efraim0a6eac22013-06-27 19:05:22 +03001868 vp_oper->state.link_state = vp_admin->link_state;
1869
1870 if (vp_admin->link_state == IFLA_VF_LINK_STATE_DISABLE)
1871 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE;
Jack Morgensteinb01978c2013-06-27 19:05:21 +03001872
1873 /* iterate over QPs owned by this slave, using UPDATE_QP */
1874 work->port = port;
1875 work->slave = slave;
1876 work->qos = vp_oper->state.default_qos;
1877 work->vlan_id = vp_oper->state.default_vlan;
1878 work->vlan_ix = vp_oper->vlan_idx;
1879 work->priv = priv;
1880 INIT_WORK(&work->work, mlx4_vf_immed_vlan_work_handler);
1881 queue_work(priv->mfunc.master.comm_wq, &work->work);
1882
1883 return 0;
1884}
1885
1886
Rony Efraim0eb62b92013-04-25 05:22:26 +00001887static int mlx4_master_activate_admin_state(struct mlx4_priv *priv, int slave)
1888{
Rony Efraim3f7fb022013-04-25 05:22:28 +00001889 int port, err;
1890 struct mlx4_vport_state *vp_admin;
1891 struct mlx4_vport_oper_state *vp_oper;
Matan Barak449fc482014-03-19 18:11:52 +02001892 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
1893 &priv->dev, slave);
1894 int min_port = find_first_bit(actv_ports.ports,
1895 priv->dev.caps.num_ports) + 1;
1896 int max_port = min_port - 1 +
1897 bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
Rony Efraim3f7fb022013-04-25 05:22:28 +00001898
Matan Barak449fc482014-03-19 18:11:52 +02001899 for (port = min_port; port <= max_port; port++) {
1900 if (!test_bit(port - 1, actv_ports.ports))
1901 continue;
Jack Morgenstein99ec41d2014-05-29 16:31:03 +03001902 priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
1903 priv->mfunc.master.vf_admin[slave].enable_smi[port];
Rony Efraim3f7fb022013-04-25 05:22:28 +00001904 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1905 vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
1906 vp_oper->state = *vp_admin;
1907 if (MLX4_VGT != vp_admin->default_vlan) {
1908 err = __mlx4_register_vlan(&priv->dev, port,
1909 vp_admin->default_vlan, &(vp_oper->vlan_idx));
1910 if (err) {
1911 vp_oper->vlan_idx = NO_INDX;
Joe Perches1a91de22014-05-07 12:52:57 -07001912 mlx4_warn(&priv->dev,
Masanari Iida1a84db52014-08-29 23:37:33 +09001913 "No vlan resources slave %d, port %d\n",
Rony Efraim3f7fb022013-04-25 05:22:28 +00001914 slave, port);
1915 return err;
1916 }
Joe Perches1a91de22014-05-07 12:52:57 -07001917 mlx4_dbg(&priv->dev, "alloc vlan %d idx %d slave %d port %d\n",
Rony Efraim3f7fb022013-04-25 05:22:28 +00001918 (int)(vp_oper->state.default_vlan),
1919 vp_oper->vlan_idx, slave, port);
1920 }
Rony Efraime6b6a232013-04-25 05:22:29 +00001921 if (vp_admin->spoofchk) {
1922 vp_oper->mac_idx = __mlx4_register_mac(&priv->dev,
1923 port,
1924 vp_admin->mac);
1925 if (0 > vp_oper->mac_idx) {
1926 err = vp_oper->mac_idx;
1927 vp_oper->mac_idx = NO_INDX;
Joe Perches1a91de22014-05-07 12:52:57 -07001928 mlx4_warn(&priv->dev,
Masanari Iida1a84db52014-08-29 23:37:33 +09001929 "No mac resources slave %d, port %d\n",
Rony Efraime6b6a232013-04-25 05:22:29 +00001930 slave, port);
1931 return err;
1932 }
Joe Perches1a91de22014-05-07 12:52:57 -07001933 mlx4_dbg(&priv->dev, "alloc mac %llx idx %d slave %d port %d\n",
Rony Efraime6b6a232013-04-25 05:22:29 +00001934 vp_oper->state.mac, vp_oper->mac_idx, slave, port);
1935 }
Rony Efraim0eb62b92013-04-25 05:22:26 +00001936 }
1937 return 0;
1938}
1939
Rony Efraim3f7fb022013-04-25 05:22:28 +00001940static void mlx4_master_deactivate_admin_state(struct mlx4_priv *priv, int slave)
1941{
1942 int port;
1943 struct mlx4_vport_oper_state *vp_oper;
Matan Barak449fc482014-03-19 18:11:52 +02001944 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
1945 &priv->dev, slave);
1946 int min_port = find_first_bit(actv_ports.ports,
1947 priv->dev.caps.num_ports) + 1;
1948 int max_port = min_port - 1 +
1949 bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
Rony Efraim3f7fb022013-04-25 05:22:28 +00001950
Matan Barak449fc482014-03-19 18:11:52 +02001951
1952 for (port = min_port; port <= max_port; port++) {
1953 if (!test_bit(port - 1, actv_ports.ports))
1954 continue;
Jack Morgenstein99ec41d2014-05-29 16:31:03 +03001955 priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
1956 MLX4_VF_SMI_DISABLED;
Rony Efraim3f7fb022013-04-25 05:22:28 +00001957 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1958 if (NO_INDX != vp_oper->vlan_idx) {
1959 __mlx4_unregister_vlan(&priv->dev,
Jack Morgenstein2009d002013-11-03 10:03:19 +02001960 port, vp_oper->state.default_vlan);
Rony Efraim3f7fb022013-04-25 05:22:28 +00001961 vp_oper->vlan_idx = NO_INDX;
1962 }
Rony Efraime6b6a232013-04-25 05:22:29 +00001963 if (NO_INDX != vp_oper->mac_idx) {
Jack Morgensteinc32b7df2013-11-03 10:04:07 +02001964 __mlx4_unregister_mac(&priv->dev, port, vp_oper->state.mac);
Rony Efraime6b6a232013-04-25 05:22:29 +00001965 vp_oper->mac_idx = NO_INDX;
1966 }
Rony Efraim3f7fb022013-04-25 05:22:28 +00001967 }
1968 return;
1969}
1970
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001971static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
1972 u16 param, u8 toggle)
1973{
1974 struct mlx4_priv *priv = mlx4_priv(dev);
1975 struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
1976 u32 reply;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001977 u8 is_going_down = 0;
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00001978 int i;
Jack Morgenstein311f8132012-11-27 16:24:30 +00001979 unsigned long flags;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001980
1981 slave_state[slave].comm_toggle ^= 1;
1982 reply = (u32) slave_state[slave].comm_toggle << 31;
1983 if (toggle != slave_state[slave].comm_toggle) {
Joe Perches1a91de22014-05-07 12:52:57 -07001984 mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER STATE COMPROMISED ***\n",
1985 toggle, slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001986 goto reset_slave;
1987 }
1988 if (cmd == MLX4_COMM_CMD_RESET) {
1989 mlx4_warn(dev, "Received reset from slave:%d\n", slave);
1990 slave_state[slave].active = false;
Jack Morgenstein2c957ff2013-11-03 10:03:21 +02001991 slave_state[slave].old_vlan_api = false;
Rony Efraim3f7fb022013-04-25 05:22:28 +00001992 mlx4_master_deactivate_admin_state(priv, slave);
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00001993 for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
1994 slave_state[slave].event_eq[i].eqn = -1;
1995 slave_state[slave].event_eq[i].token = 0;
1996 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001997 /*check if we are in the middle of FLR process,
1998 if so return "retry" status to the slave*/
Or Gerlitz162344e2012-05-15 10:34:57 +00001999 if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002000 goto inform_slave_state;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002001
Jack Morgensteinfc065732012-08-03 08:40:42 +00002002 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
2003
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002004 /* write the version in the event field */
2005 reply |= mlx4_comm_get_version();
2006
2007 goto reset_slave;
2008 }
2009 /*command from slave in the middle of FLR*/
2010 if (cmd != MLX4_COMM_CMD_RESET &&
2011 MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
Joe Perches1a91de22014-05-07 12:52:57 -07002012 mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) in the middle of FLR\n",
2013 slave, cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002014 return;
2015 }
2016
2017 switch (cmd) {
2018 case MLX4_COMM_CMD_VHCR0:
2019 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
2020 goto reset_slave;
2021 slave_state[slave].vhcr_dma = ((u64) param) << 48;
2022 priv->mfunc.master.slave_state[slave].cookie = 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002023 break;
2024 case MLX4_COMM_CMD_VHCR1:
2025 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
2026 goto reset_slave;
2027 slave_state[slave].vhcr_dma |= ((u64) param) << 32;
2028 break;
2029 case MLX4_COMM_CMD_VHCR2:
2030 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
2031 goto reset_slave;
2032 slave_state[slave].vhcr_dma |= ((u64) param) << 16;
2033 break;
2034 case MLX4_COMM_CMD_VHCR_EN:
2035 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
2036 goto reset_slave;
2037 slave_state[slave].vhcr_dma |= param;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002038 if (mlx4_master_activate_admin_state(priv, slave))
2039 goto reset_slave;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002040 slave_state[slave].active = true;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002041 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002042 break;
2043 case MLX4_COMM_CMD_VHCR_POST:
2044 if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
Yishai Hadas55ad3592015-01-25 16:59:42 +02002045 (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST)) {
2046 mlx4_warn(dev, "slave:%d is out of sync, cmd=0x%x, last command=0x%x, reset is needed\n",
2047 slave, cmd, slave_state[slave].last_cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002048 goto reset_slave;
Yishai Hadas55ad3592015-01-25 16:59:42 +02002049 }
Roland Dreierf3d4c892012-09-25 21:24:07 -07002050
2051 mutex_lock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002052 if (mlx4_master_process_vhcr(dev, slave, NULL)) {
Joe Perches1a91de22014-05-07 12:52:57 -07002053 mlx4_err(dev, "Failed processing vhcr for slave:%d, resetting slave\n",
2054 slave);
Roland Dreierf3d4c892012-09-25 21:24:07 -07002055 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002056 goto reset_slave;
2057 }
Roland Dreierf3d4c892012-09-25 21:24:07 -07002058 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002059 break;
2060 default:
2061 mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
2062 goto reset_slave;
2063 }
Jack Morgenstein311f8132012-11-27 16:24:30 +00002064 spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002065 if (!slave_state[slave].is_slave_going_down)
2066 slave_state[slave].last_cmd = cmd;
2067 else
2068 is_going_down = 1;
Jack Morgenstein311f8132012-11-27 16:24:30 +00002069 spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002070 if (is_going_down) {
Joe Perches1a91de22014-05-07 12:52:57 -07002071 mlx4_warn(dev, "Slave is going down aborting command(%d) executing from slave:%d\n",
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002072 cmd, slave);
2073 return;
2074 }
2075 __raw_writel((__force u32) cpu_to_be32(reply),
2076 &priv->mfunc.comm[slave].slave_read);
2077 mmiowb();
2078
2079 return;
2080
2081reset_slave:
Eli Cohenc82e9aa2011-12-13 04:15:24 +00002082 /* cleanup any slave resources */
Yishai Hadas55ad3592015-01-25 16:59:42 +02002083 if (dev->persist->interface_state & MLX4_INTERFACE_STATE_UP)
2084 mlx4_delete_all_resources_for_slave(dev, slave);
2085
2086 if (cmd != MLX4_COMM_CMD_RESET) {
2087 mlx4_warn(dev, "Turn on internal error to force reset, slave=%d, cmd=0x%x\n",
2088 slave, cmd);
2089 /* Turn on internal error letting slave reset itself immeditaly,
2090 * otherwise it might take till timeout on command is passed
2091 */
2092 reply |= ((u32)COMM_CHAN_EVENT_INTERNAL_ERR);
2093 }
2094
Jack Morgenstein311f8132012-11-27 16:24:30 +00002095 spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002096 if (!slave_state[slave].is_slave_going_down)
2097 slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
Jack Morgenstein311f8132012-11-27 16:24:30 +00002098 spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002099 /*with slave in the middle of flr, no need to clean resources again.*/
2100inform_slave_state:
2101 memset(&slave_state[slave].event_eq, 0,
2102 sizeof(struct mlx4_slave_event_eq_info));
2103 __raw_writel((__force u32) cpu_to_be32(reply),
2104 &priv->mfunc.comm[slave].slave_read);
2105 wmb();
2106}
2107
2108/* master command processing */
2109void mlx4_master_comm_channel(struct work_struct *work)
2110{
2111 struct mlx4_mfunc_master_ctx *master =
2112 container_of(work,
2113 struct mlx4_mfunc_master_ctx,
2114 comm_work);
2115 struct mlx4_mfunc *mfunc =
2116 container_of(master, struct mlx4_mfunc, master);
2117 struct mlx4_priv *priv =
2118 container_of(mfunc, struct mlx4_priv, mfunc);
2119 struct mlx4_dev *dev = &priv->dev;
2120 __be32 *bit_vec;
2121 u32 comm_cmd;
2122 u32 vec;
2123 int i, j, slave;
2124 int toggle;
2125 int served = 0;
2126 int reported = 0;
2127 u32 slt;
2128
2129 bit_vec = master->comm_arm_bit_vector;
2130 for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
2131 vec = be32_to_cpu(bit_vec[i]);
2132 for (j = 0; j < 32; j++) {
2133 if (!(vec & (1 << j)))
2134 continue;
2135 ++reported;
2136 slave = (i * 32) + j;
2137 comm_cmd = swab32(readl(
2138 &mfunc->comm[slave].slave_write));
2139 slt = swab32(readl(&mfunc->comm[slave].slave_read))
2140 >> 31;
2141 toggle = comm_cmd >> 31;
2142 if (toggle != slt) {
2143 if (master->slave_state[slave].comm_toggle
2144 != slt) {
Amir Vadaic20862c2014-05-22 15:55:40 +03002145 pr_info("slave %d out of sync. read toggle %d, state toggle %d. Resynching.\n",
2146 slave, slt,
2147 master->slave_state[slave].comm_toggle);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002148 master->slave_state[slave].comm_toggle =
2149 slt;
2150 }
2151 mlx4_master_do_cmd(dev, slave,
2152 comm_cmd >> 16 & 0xff,
2153 comm_cmd & 0xffff, toggle);
2154 ++served;
2155 }
2156 }
2157 }
2158
2159 if (reported && reported != served)
Joe Perches1a91de22014-05-07 12:52:57 -07002160 mlx4_warn(dev, "Got command event with bitmask from %d slaves but %d were served\n",
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002161 reported, served);
2162
2163 if (mlx4_ARM_COMM_CHANNEL(dev))
2164 mlx4_warn(dev, "Failed to arm comm channel events\n");
2165}
2166
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002167static int sync_toggles(struct mlx4_dev *dev)
2168{
2169 struct mlx4_priv *priv = mlx4_priv(dev);
Yishai Hadas55ad3592015-01-25 16:59:42 +02002170 u32 wr_toggle;
2171 u32 rd_toggle;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002172 unsigned long end;
2173
Yishai Hadas55ad3592015-01-25 16:59:42 +02002174 wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write));
2175 if (wr_toggle == 0xffffffff)
2176 end = jiffies + msecs_to_jiffies(30000);
2177 else
2178 end = jiffies + msecs_to_jiffies(5000);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002179
2180 while (time_before(jiffies, end)) {
Yishai Hadas55ad3592015-01-25 16:59:42 +02002181 rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read));
2182 if (wr_toggle == 0xffffffff || rd_toggle == 0xffffffff) {
2183 /* PCI might be offline */
2184 msleep(100);
2185 wr_toggle = swab32(readl(&priv->mfunc.comm->
2186 slave_write));
2187 continue;
2188 }
2189
2190 if (rd_toggle >> 31 == wr_toggle >> 31) {
2191 priv->cmd.comm_toggle = rd_toggle >> 31;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002192 return 0;
2193 }
2194
2195 cond_resched();
2196 }
2197
2198 /*
2199 * we could reach here if for example the previous VM using this
2200 * function misbehaved and left the channel with unsynced state. We
2201 * should fix this here and give this VM a chance to use a properly
2202 * synced channel
2203 */
2204 mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
2205 __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
2206 __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
2207 priv->cmd.comm_toggle = 0;
2208
2209 return 0;
2210}
2211
2212int mlx4_multi_func_init(struct mlx4_dev *dev)
2213{
2214 struct mlx4_priv *priv = mlx4_priv(dev);
2215 struct mlx4_slave_state *s_state;
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002216 int i, j, err, port;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002217
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002218 if (mlx4_is_master(dev))
2219 priv->mfunc.comm =
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002220 ioremap(pci_resource_start(dev->persist->pdev,
2221 priv->fw.comm_bar) +
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002222 priv->fw.comm_base, MLX4_COMM_PAGESIZE);
2223 else
2224 priv->mfunc.comm =
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002225 ioremap(pci_resource_start(dev->persist->pdev, 2) +
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002226 MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
2227 if (!priv->mfunc.comm) {
Joe Perches1a91de22014-05-07 12:52:57 -07002228 mlx4_err(dev, "Couldn't map communication vector\n");
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002229 goto err_vhcr;
2230 }
2231
2232 if (mlx4_is_master(dev)) {
Ido Shamay4abccb62015-04-02 16:31:09 +03002233 struct mlx4_vf_oper_state *vf_oper;
2234 struct mlx4_vf_admin_state *vf_admin;
2235
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002236 priv->mfunc.master.slave_state =
2237 kzalloc(dev->num_slaves *
2238 sizeof(struct mlx4_slave_state), GFP_KERNEL);
2239 if (!priv->mfunc.master.slave_state)
2240 goto err_comm;
2241
Rony Efraim0eb62b92013-04-25 05:22:26 +00002242 priv->mfunc.master.vf_admin =
2243 kzalloc(dev->num_slaves *
2244 sizeof(struct mlx4_vf_admin_state), GFP_KERNEL);
2245 if (!priv->mfunc.master.vf_admin)
2246 goto err_comm_admin;
2247
2248 priv->mfunc.master.vf_oper =
2249 kzalloc(dev->num_slaves *
2250 sizeof(struct mlx4_vf_oper_state), GFP_KERNEL);
2251 if (!priv->mfunc.master.vf_oper)
2252 goto err_comm_oper;
2253
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002254 for (i = 0; i < dev->num_slaves; ++i) {
Ido Shamay4abccb62015-04-02 16:31:09 +03002255 vf_admin = &priv->mfunc.master.vf_admin[i];
2256 vf_oper = &priv->mfunc.master.vf_oper[i];
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002257 s_state = &priv->mfunc.master.slave_state[i];
2258 s_state->last_cmd = MLX4_COMM_CMD_RESET;
Jack Morgensteinbffb0232015-03-24 15:18:39 +02002259 mutex_init(&priv->mfunc.master.gen_eqe_mutex[i]);
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00002260 for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
2261 s_state->event_eq[j].eqn = -1;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002262 __raw_writel((__force u32) 0,
2263 &priv->mfunc.comm[i].slave_write);
2264 __raw_writel((__force u32) 0,
2265 &priv->mfunc.comm[i].slave_read);
2266 mmiowb();
2267 for (port = 1; port <= MLX4_MAX_PORTS; port++) {
Ido Shamay4abccb62015-04-02 16:31:09 +03002268 struct mlx4_vport_state *admin_vport;
2269 struct mlx4_vport_state *oper_vport;
2270
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002271 s_state->vlan_filter[port] =
2272 kzalloc(sizeof(struct mlx4_vlan_fltr),
2273 GFP_KERNEL);
2274 if (!s_state->vlan_filter[port]) {
2275 if (--port)
2276 kfree(s_state->vlan_filter[port]);
2277 goto err_slaves;
2278 }
Ido Shamay4abccb62015-04-02 16:31:09 +03002279
2280 admin_vport = &vf_admin->vport[port];
2281 oper_vport = &vf_oper->vport[port].state;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002282 INIT_LIST_HEAD(&s_state->mcast_filters[port]);
Ido Shamay4abccb62015-04-02 16:31:09 +03002283 admin_vport->default_vlan = MLX4_VGT;
2284 oper_vport->default_vlan = MLX4_VGT;
2285 vf_oper->vport[port].vlan_idx = NO_INDX;
2286 vf_oper->vport[port].mac_idx = NO_INDX;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002287 }
2288 spin_lock_init(&s_state->lock);
2289 }
2290
Or Gerlitz08ff3232012-10-21 14:59:24 +00002291 memset(&priv->mfunc.master.cmd_eqe, 0, dev->caps.eqe_size);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002292 priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
2293 INIT_WORK(&priv->mfunc.master.comm_work,
2294 mlx4_master_comm_channel);
2295 INIT_WORK(&priv->mfunc.master.slave_event_work,
2296 mlx4_gen_slave_eqe);
2297 INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
2298 mlx4_master_handle_slave_flr);
2299 spin_lock_init(&priv->mfunc.master.slave_state_lock);
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00002300 spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002301 priv->mfunc.master.comm_wq =
2302 create_singlethread_workqueue("mlx4_comm");
2303 if (!priv->mfunc.master.comm_wq)
2304 goto err_slaves;
2305
2306 if (mlx4_init_resource_tracker(dev))
2307 goto err_thread;
2308
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002309 } else {
2310 err = sync_toggles(dev);
2311 if (err) {
2312 mlx4_err(dev, "Couldn't sync toggles\n");
2313 goto err_comm;
2314 }
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002315 }
2316 return 0;
2317
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002318err_thread:
2319 flush_workqueue(priv->mfunc.master.comm_wq);
2320 destroy_workqueue(priv->mfunc.master.comm_wq);
2321err_slaves:
2322 while (--i) {
2323 for (port = 1; port <= MLX4_MAX_PORTS; port++)
2324 kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
2325 }
Rony Efraim0eb62b92013-04-25 05:22:26 +00002326 kfree(priv->mfunc.master.vf_oper);
2327err_comm_oper:
2328 kfree(priv->mfunc.master.vf_admin);
2329err_comm_admin:
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002330 kfree(priv->mfunc.master.slave_state);
2331err_comm:
2332 iounmap(priv->mfunc.comm);
2333err_vhcr:
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002334 dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
2335 priv->mfunc.vhcr,
2336 priv->mfunc.vhcr_dma);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002337 priv->mfunc.vhcr = NULL;
2338 return -ENOMEM;
2339}
2340
Roland Dreier225c7b12007-05-08 18:00:38 -07002341int mlx4_cmd_init(struct mlx4_dev *dev)
2342{
2343 struct mlx4_priv *priv = mlx4_priv(dev);
Matan Barakffc39f62014-11-13 14:45:29 +02002344 int flags = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002345
Matan Barakffc39f62014-11-13 14:45:29 +02002346 if (!priv->cmd.initialized) {
Matan Barakffc39f62014-11-13 14:45:29 +02002347 mutex_init(&priv->cmd.slave_cmd_mutex);
2348 sema_init(&priv->cmd.poll_sem, 1);
2349 priv->cmd.use_events = 0;
2350 priv->cmd.toggle = 1;
2351 priv->cmd.initialized = 1;
2352 flags |= MLX4_CMD_CLEANUP_STRUCT;
2353 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002354
Matan Barakffc39f62014-11-13 14:45:29 +02002355 if (!mlx4_is_slave(dev) && !priv->cmd.hcr) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002356 priv->cmd.hcr = ioremap(pci_resource_start(dev->persist->pdev,
2357 0) + MLX4_HCR_BASE, MLX4_HCR_SIZE);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002358 if (!priv->cmd.hcr) {
Joe Perches1a91de22014-05-07 12:52:57 -07002359 mlx4_err(dev, "Couldn't map command register\n");
Matan Barakffc39f62014-11-13 14:45:29 +02002360 goto err;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002361 }
Matan Barakffc39f62014-11-13 14:45:29 +02002362 flags |= MLX4_CMD_CLEANUP_HCR;
Roland Dreier225c7b12007-05-08 18:00:38 -07002363 }
2364
Matan Barakffc39f62014-11-13 14:45:29 +02002365 if (mlx4_is_mfunc(dev) && !priv->mfunc.vhcr) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002366 priv->mfunc.vhcr = dma_alloc_coherent(&dev->persist->pdev->dev,
2367 PAGE_SIZE,
Roland Dreierf3d4c892012-09-25 21:24:07 -07002368 &priv->mfunc.vhcr_dma,
2369 GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002370 if (!priv->mfunc.vhcr)
Matan Barakffc39f62014-11-13 14:45:29 +02002371 goto err;
2372
2373 flags |= MLX4_CMD_CLEANUP_VHCR;
Roland Dreierf3d4c892012-09-25 21:24:07 -07002374 }
2375
Matan Barakffc39f62014-11-13 14:45:29 +02002376 if (!priv->cmd.pool) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002377 priv->cmd.pool = pci_pool_create("mlx4_cmd",
2378 dev->persist->pdev,
Matan Barakffc39f62014-11-13 14:45:29 +02002379 MLX4_MAILBOX_SIZE,
2380 MLX4_MAILBOX_SIZE, 0);
2381 if (!priv->cmd.pool)
2382 goto err;
2383
2384 flags |= MLX4_CMD_CLEANUP_POOL;
2385 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002386
2387 return 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002388
Matan Barakffc39f62014-11-13 14:45:29 +02002389err:
2390 mlx4_cmd_cleanup(dev, flags);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002391 return -ENOMEM;
Roland Dreier225c7b12007-05-08 18:00:38 -07002392}
2393
Yishai Hadas55ad3592015-01-25 16:59:42 +02002394void mlx4_report_internal_err_comm_event(struct mlx4_dev *dev)
2395{
2396 struct mlx4_priv *priv = mlx4_priv(dev);
2397 int slave;
2398 u32 slave_read;
2399
2400 /* Report an internal error event to all
2401 * communication channels.
2402 */
2403 for (slave = 0; slave < dev->num_slaves; slave++) {
2404 slave_read = swab32(readl(&priv->mfunc.comm[slave].slave_read));
2405 slave_read |= (u32)COMM_CHAN_EVENT_INTERNAL_ERR;
2406 __raw_writel((__force u32)cpu_to_be32(slave_read),
2407 &priv->mfunc.comm[slave].slave_read);
2408 /* Make sure that our comm channel write doesn't
2409 * get mixed in with writes from another CPU.
2410 */
2411 mmiowb();
2412 }
2413}
2414
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002415void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
2416{
2417 struct mlx4_priv *priv = mlx4_priv(dev);
2418 int i, port;
2419
2420 if (mlx4_is_master(dev)) {
2421 flush_workqueue(priv->mfunc.master.comm_wq);
2422 destroy_workqueue(priv->mfunc.master.comm_wq);
2423 for (i = 0; i < dev->num_slaves; i++) {
2424 for (port = 1; port <= MLX4_MAX_PORTS; port++)
2425 kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
2426 }
2427 kfree(priv->mfunc.master.slave_state);
Rony Efraim0eb62b92013-04-25 05:22:26 +00002428 kfree(priv->mfunc.master.vf_admin);
2429 kfree(priv->mfunc.master.vf_oper);
Yishai Hadas55ad3592015-01-25 16:59:42 +02002430 dev->num_slaves = 0;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002431 }
Eugenia Emantayevf08ad062012-02-06 06:26:17 +00002432
2433 iounmap(priv->mfunc.comm);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002434}
2435
Matan Barakffc39f62014-11-13 14:45:29 +02002436void mlx4_cmd_cleanup(struct mlx4_dev *dev, int cleanup_mask)
Roland Dreier225c7b12007-05-08 18:00:38 -07002437{
2438 struct mlx4_priv *priv = mlx4_priv(dev);
2439
Matan Barakffc39f62014-11-13 14:45:29 +02002440 if (priv->cmd.pool && (cleanup_mask & MLX4_CMD_CLEANUP_POOL)) {
2441 pci_pool_destroy(priv->cmd.pool);
2442 priv->cmd.pool = NULL;
2443 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002444
Matan Barakffc39f62014-11-13 14:45:29 +02002445 if (!mlx4_is_slave(dev) && priv->cmd.hcr &&
2446 (cleanup_mask & MLX4_CMD_CLEANUP_HCR)) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002447 iounmap(priv->cmd.hcr);
Matan Barakffc39f62014-11-13 14:45:29 +02002448 priv->cmd.hcr = NULL;
2449 }
2450 if (mlx4_is_mfunc(dev) && priv->mfunc.vhcr &&
2451 (cleanup_mask & MLX4_CMD_CLEANUP_VHCR)) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002452 dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
Roland Dreierf3d4c892012-09-25 21:24:07 -07002453 priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
Matan Barakffc39f62014-11-13 14:45:29 +02002454 priv->mfunc.vhcr = NULL;
2455 }
2456 if (priv->cmd.initialized && (cleanup_mask & MLX4_CMD_CLEANUP_STRUCT))
2457 priv->cmd.initialized = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002458}
2459
2460/*
2461 * Switch to using events to issue FW commands (can only be called
2462 * after event queue for command events has been initialized).
2463 */
2464int mlx4_cmd_use_events(struct mlx4_dev *dev)
2465{
2466 struct mlx4_priv *priv = mlx4_priv(dev);
2467 int i;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002468 int err = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002469
2470 priv->cmd.context = kmalloc(priv->cmd.max_cmds *
2471 sizeof (struct mlx4_cmd_context),
2472 GFP_KERNEL);
2473 if (!priv->cmd.context)
2474 return -ENOMEM;
2475
2476 for (i = 0; i < priv->cmd.max_cmds; ++i) {
2477 priv->cmd.context[i].token = i;
2478 priv->cmd.context[i].next = i + 1;
Yishai Hadasf5aef5a2015-01-25 16:59:39 +02002479 /* To support fatal error flow, initialize all
2480 * cmd contexts to allow simulating completions
2481 * with complete() at any time.
2482 */
2483 init_completion(&priv->cmd.context[i].done);
Roland Dreier225c7b12007-05-08 18:00:38 -07002484 }
2485
2486 priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
2487 priv->cmd.free_head = 0;
2488
2489 sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
2490 spin_lock_init(&priv->cmd.context_lock);
2491
2492 for (priv->cmd.token_mask = 1;
2493 priv->cmd.token_mask < priv->cmd.max_cmds;
2494 priv->cmd.token_mask <<= 1)
2495 ; /* nothing */
2496 --priv->cmd.token_mask;
2497
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002498 down(&priv->cmd.poll_sem);
Roland Dreier225c7b12007-05-08 18:00:38 -07002499 priv->cmd.use_events = 1;
2500
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002501 return err;
Roland Dreier225c7b12007-05-08 18:00:38 -07002502}
2503
2504/*
2505 * Switch back to polling (used when shutting down the device)
2506 */
2507void mlx4_cmd_use_polling(struct mlx4_dev *dev)
2508{
2509 struct mlx4_priv *priv = mlx4_priv(dev);
2510 int i;
2511
2512 priv->cmd.use_events = 0;
2513
2514 for (i = 0; i < priv->cmd.max_cmds; ++i)
2515 down(&priv->cmd.event_sem);
2516
2517 kfree(priv->cmd.context);
2518
2519 up(&priv->cmd.poll_sem);
2520}
2521
2522struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
2523{
2524 struct mlx4_cmd_mailbox *mailbox;
2525
2526 mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
2527 if (!mailbox)
2528 return ERR_PTR(-ENOMEM);
2529
2530 mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
2531 &mailbox->dma);
2532 if (!mailbox->buf) {
2533 kfree(mailbox);
2534 return ERR_PTR(-ENOMEM);
2535 }
2536
Jack Morgenstein571b8b92013-11-07 12:19:50 +02002537 memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
2538
Roland Dreier225c7b12007-05-08 18:00:38 -07002539 return mailbox;
2540}
2541EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
2542
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002543void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
2544 struct mlx4_cmd_mailbox *mailbox)
Roland Dreier225c7b12007-05-08 18:00:38 -07002545{
2546 if (!mailbox)
2547 return;
2548
2549 pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
2550 kfree(mailbox);
2551}
2552EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00002553
2554u32 mlx4_comm_get_version(void)
2555{
2556 return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
2557}
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002558
2559static int mlx4_get_slave_indx(struct mlx4_dev *dev, int vf)
2560{
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002561 if ((vf < 0) || (vf >= dev->persist->num_vfs)) {
2562 mlx4_err(dev, "Bad vf number:%d (number of activated vf: %d)\n",
2563 vf, dev->persist->num_vfs);
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002564 return -EINVAL;
2565 }
2566
2567 return vf+1;
2568}
2569
Matan Barakf74462a2014-03-19 18:11:51 +02002570int mlx4_get_vf_indx(struct mlx4_dev *dev, int slave)
2571{
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002572 if (slave < 1 || slave > dev->persist->num_vfs) {
Matan Barakf74462a2014-03-19 18:11:51 +02002573 mlx4_err(dev,
2574 "Bad slave number:%d (number of activated slaves: %lu)\n",
2575 slave, dev->num_slaves);
2576 return -EINVAL;
2577 }
2578 return slave - 1;
2579}
2580
Yishai Hadasf5aef5a2015-01-25 16:59:39 +02002581void mlx4_cmd_wake_completions(struct mlx4_dev *dev)
2582{
2583 struct mlx4_priv *priv = mlx4_priv(dev);
2584 struct mlx4_cmd_context *context;
2585 int i;
2586
2587 spin_lock(&priv->cmd.context_lock);
2588 if (priv->cmd.context) {
2589 for (i = 0; i < priv->cmd.max_cmds; ++i) {
2590 context = &priv->cmd.context[i];
2591 context->fw_status = CMD_STAT_INTERNAL_ERR;
2592 context->result =
2593 mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
2594 complete(&context->done);
2595 }
2596 }
2597 spin_unlock(&priv->cmd.context_lock);
2598}
2599
Matan Barakf74462a2014-03-19 18:11:51 +02002600struct mlx4_active_ports mlx4_get_active_ports(struct mlx4_dev *dev, int slave)
2601{
2602 struct mlx4_active_ports actv_ports;
2603 int vf;
2604
2605 bitmap_zero(actv_ports.ports, MLX4_MAX_PORTS);
2606
2607 if (slave == 0) {
2608 bitmap_fill(actv_ports.ports, dev->caps.num_ports);
2609 return actv_ports;
2610 }
2611
2612 vf = mlx4_get_vf_indx(dev, slave);
2613 if (vf < 0)
2614 return actv_ports;
2615
2616 bitmap_set(actv_ports.ports, dev->dev_vfs[vf].min_port - 1,
2617 min((int)dev->dev_vfs[mlx4_get_vf_indx(dev, slave)].n_ports,
2618 dev->caps.num_ports));
2619
2620 return actv_ports;
2621}
2622EXPORT_SYMBOL_GPL(mlx4_get_active_ports);
2623
2624int mlx4_slave_convert_port(struct mlx4_dev *dev, int slave, int port)
2625{
2626 unsigned n;
2627 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2628 unsigned m = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2629
2630 if (port <= 0 || port > m)
2631 return -EINVAL;
2632
2633 n = find_first_bit(actv_ports.ports, dev->caps.num_ports);
2634 if (port <= n)
2635 port = n + 1;
2636
2637 return port;
2638}
2639EXPORT_SYMBOL_GPL(mlx4_slave_convert_port);
2640
2641int mlx4_phys_to_slave_port(struct mlx4_dev *dev, int slave, int port)
2642{
2643 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2644 if (test_bit(port - 1, actv_ports.ports))
2645 return port -
2646 find_first_bit(actv_ports.ports, dev->caps.num_ports);
2647
2648 return -1;
2649}
2650EXPORT_SYMBOL_GPL(mlx4_phys_to_slave_port);
2651
2652struct mlx4_slaves_pport mlx4_phys_to_slaves_pport(struct mlx4_dev *dev,
2653 int port)
2654{
2655 unsigned i;
2656 struct mlx4_slaves_pport slaves_pport;
2657
2658 bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
2659
2660 if (port <= 0 || port > dev->caps.num_ports)
2661 return slaves_pport;
2662
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002663 for (i = 0; i < dev->persist->num_vfs + 1; i++) {
Matan Barakf74462a2014-03-19 18:11:51 +02002664 struct mlx4_active_ports actv_ports =
2665 mlx4_get_active_ports(dev, i);
2666 if (test_bit(port - 1, actv_ports.ports))
2667 set_bit(i, slaves_pport.slaves);
2668 }
2669
2670 return slaves_pport;
2671}
2672EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport);
2673
2674struct mlx4_slaves_pport mlx4_phys_to_slaves_pport_actv(
2675 struct mlx4_dev *dev,
2676 const struct mlx4_active_ports *crit_ports)
2677{
2678 unsigned i;
2679 struct mlx4_slaves_pport slaves_pport;
2680
2681 bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
2682
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002683 for (i = 0; i < dev->persist->num_vfs + 1; i++) {
Matan Barakf74462a2014-03-19 18:11:51 +02002684 struct mlx4_active_ports actv_ports =
2685 mlx4_get_active_ports(dev, i);
2686 if (bitmap_equal(crit_ports->ports, actv_ports.ports,
2687 dev->caps.num_ports))
2688 set_bit(i, slaves_pport.slaves);
2689 }
2690
2691 return slaves_pport;
2692}
2693EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport_actv);
2694
Matan Baraka91c7722014-09-10 16:41:53 +03002695static int mlx4_slaves_closest_port(struct mlx4_dev *dev, int slave, int port)
2696{
2697 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2698 int min_port = find_first_bit(actv_ports.ports, dev->caps.num_ports)
2699 + 1;
2700 int max_port = min_port +
2701 bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2702
2703 if (port < min_port)
2704 port = min_port;
2705 else if (port >= max_port)
2706 port = max_port - 1;
2707
2708 return port;
2709}
2710
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002711int mlx4_set_vf_mac(struct mlx4_dev *dev, int port, int vf, u64 mac)
2712{
2713 struct mlx4_priv *priv = mlx4_priv(dev);
2714 struct mlx4_vport_state *s_info;
2715 int slave;
2716
2717 if (!mlx4_is_master(dev))
2718 return -EPROTONOSUPPORT;
2719
2720 slave = mlx4_get_slave_indx(dev, vf);
2721 if (slave < 0)
2722 return -EINVAL;
2723
Matan Baraka91c7722014-09-10 16:41:53 +03002724 port = mlx4_slaves_closest_port(dev, slave, port);
Rony Efraim8f7ba3c2013-04-25 05:22:27 +00002725 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
2726 s_info->mac = mac;
2727 mlx4_info(dev, "default mac on vf %d port %d to %llX will take afect only after vf restart\n",
2728 vf, port, s_info->mac);
2729 return 0;
2730}
2731EXPORT_SYMBOL_GPL(mlx4_set_vf_mac);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002732
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002733
Rony Efraim3f7fb022013-04-25 05:22:28 +00002734int mlx4_set_vf_vlan(struct mlx4_dev *dev, int port, int vf, u16 vlan, u8 qos)
2735{
2736 struct mlx4_priv *priv = mlx4_priv(dev);
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002737 struct mlx4_vport_state *vf_admin;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002738 int slave;
2739
2740 if ((!mlx4_is_master(dev)) ||
2741 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
2742 return -EPROTONOSUPPORT;
2743
2744 if ((vlan > 4095) || (qos > 7))
2745 return -EINVAL;
2746
2747 slave = mlx4_get_slave_indx(dev, vf);
2748 if (slave < 0)
2749 return -EINVAL;
2750
Matan Baraka91c7722014-09-10 16:41:53 +03002751 port = mlx4_slaves_closest_port(dev, slave, port);
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002752 vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002753
Rony Efraim3f7fb022013-04-25 05:22:28 +00002754 if ((0 == vlan) && (0 == qos))
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002755 vf_admin->default_vlan = MLX4_VGT;
Rony Efraim3f7fb022013-04-25 05:22:28 +00002756 else
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002757 vf_admin->default_vlan = vlan;
2758 vf_admin->default_qos = qos;
2759
Rony Efraim0a6eac22013-06-27 19:05:22 +03002760 if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
2761 mlx4_info(dev,
2762 "updating vf %d port %d config will take effect on next VF restart\n",
Jack Morgensteinb01978c2013-06-27 19:05:21 +03002763 vf, port);
Rony Efraim3f7fb022013-04-25 05:22:28 +00002764 return 0;
2765}
2766EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
Rony Efraime6b6a232013-04-25 05:22:29 +00002767
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +02002768 /* mlx4_get_slave_default_vlan -
2769 * return true if VST ( default vlan)
2770 * if VST, will return vlan & qos (if not NULL)
2771 */
2772bool mlx4_get_slave_default_vlan(struct mlx4_dev *dev, int port, int slave,
2773 u16 *vlan, u8 *qos)
2774{
2775 struct mlx4_vport_oper_state *vp_oper;
2776 struct mlx4_priv *priv;
2777
2778 priv = mlx4_priv(dev);
Matan Baraka91c7722014-09-10 16:41:53 +03002779 port = mlx4_slaves_closest_port(dev, slave, port);
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +02002780 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
2781
2782 if (MLX4_VGT != vp_oper->state.default_vlan) {
2783 if (vlan)
2784 *vlan = vp_oper->state.default_vlan;
2785 if (qos)
2786 *qos = vp_oper->state.default_qos;
2787 return true;
2788 }
2789 return false;
2790}
2791EXPORT_SYMBOL_GPL(mlx4_get_slave_default_vlan);
2792
Rony Efraime6b6a232013-04-25 05:22:29 +00002793int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
2794{
2795 struct mlx4_priv *priv = mlx4_priv(dev);
2796 struct mlx4_vport_state *s_info;
2797 int slave;
2798
2799 if ((!mlx4_is_master(dev)) ||
2800 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
2801 return -EPROTONOSUPPORT;
2802
2803 slave = mlx4_get_slave_indx(dev, vf);
2804 if (slave < 0)
2805 return -EINVAL;
2806
Matan Baraka91c7722014-09-10 16:41:53 +03002807 port = mlx4_slaves_closest_port(dev, slave, port);
Rony Efraime6b6a232013-04-25 05:22:29 +00002808 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
2809 s_info->spoofchk = setting;
2810
2811 return 0;
2812}
2813EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
Rony Efraim2cccb9e2013-04-25 05:22:30 +00002814
2815int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
2816{
2817 struct mlx4_priv *priv = mlx4_priv(dev);
2818 struct mlx4_vport_state *s_info;
2819 int slave;
2820
2821 if (!mlx4_is_master(dev))
2822 return -EPROTONOSUPPORT;
2823
2824 slave = mlx4_get_slave_indx(dev, vf);
2825 if (slave < 0)
2826 return -EINVAL;
2827
2828 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
2829 ivf->vf = vf;
2830
2831 /* need to convert it to a func */
2832 ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
2833 ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
2834 ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
2835 ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
2836 ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
2837 ivf->mac[5] = ((s_info->mac) & 0xff);
2838
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002839 ivf->vlan = s_info->default_vlan;
2840 ivf->qos = s_info->default_qos;
2841 ivf->max_tx_rate = s_info->tx_rate;
2842 ivf->min_tx_rate = 0;
2843 ivf->spoofchk = s_info->spoofchk;
2844 ivf->linkstate = s_info->link_state;
Rony Efraim2cccb9e2013-04-25 05:22:30 +00002845
2846 return 0;
2847}
2848EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
Rony Efraim948e3062013-06-13 13:19:11 +03002849
2850int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
2851{
2852 struct mlx4_priv *priv = mlx4_priv(dev);
2853 struct mlx4_vport_state *s_info;
Rony Efraim948e3062013-06-13 13:19:11 +03002854 int slave;
2855 u8 link_stat_event;
2856
2857 slave = mlx4_get_slave_indx(dev, vf);
2858 if (slave < 0)
2859 return -EINVAL;
2860
Matan Baraka91c7722014-09-10 16:41:53 +03002861 port = mlx4_slaves_closest_port(dev, slave, port);
Rony Efraim948e3062013-06-13 13:19:11 +03002862 switch (link_state) {
2863 case IFLA_VF_LINK_STATE_AUTO:
2864 /* get current link state */
2865 if (!priv->sense.do_sense_port[port])
2866 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
2867 else
2868 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
2869 break;
2870
2871 case IFLA_VF_LINK_STATE_ENABLE:
2872 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
2873 break;
2874
2875 case IFLA_VF_LINK_STATE_DISABLE:
2876 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
2877 break;
2878
2879 default:
2880 mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
2881 link_state, slave, port);
2882 return -EINVAL;
2883 };
Rony Efraim948e3062013-06-13 13:19:11 +03002884 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
Rony Efraim948e3062013-06-13 13:19:11 +03002885 s_info->link_state = link_state;
Rony Efraim948e3062013-06-13 13:19:11 +03002886
2887 /* send event */
2888 mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
Rony Efraim0a6eac22013-06-27 19:05:22 +03002889
2890 if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
2891 mlx4_dbg(dev,
2892 "updating vf %d port %d no link state HW enforcment\n",
2893 vf, port);
Rony Efraim948e3062013-06-13 13:19:11 +03002894 return 0;
2895}
2896EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);
Jack Morgenstein97982f52014-05-29 16:31:02 +03002897
2898int mlx4_vf_smi_enabled(struct mlx4_dev *dev, int slave, int port)
2899{
Jack Morgenstein99ec41d2014-05-29 16:31:03 +03002900 struct mlx4_priv *priv = mlx4_priv(dev);
2901
2902 if (slave < 1 || slave >= dev->num_slaves ||
2903 port < 1 || port > MLX4_MAX_PORTS)
2904 return 0;
2905
2906 return priv->mfunc.master.vf_oper[slave].smi_enabled[port] ==
2907 MLX4_VF_SMI_ENABLED;
Jack Morgenstein97982f52014-05-29 16:31:02 +03002908}
2909EXPORT_SYMBOL_GPL(mlx4_vf_smi_enabled);
Jack Morgenstein65fed8a2014-05-29 16:31:04 +03002910
2911int mlx4_vf_get_enable_smi_admin(struct mlx4_dev *dev, int slave, int port)
2912{
2913 struct mlx4_priv *priv = mlx4_priv(dev);
2914
2915 if (slave == mlx4_master_func_num(dev))
2916 return 1;
2917
2918 if (slave < 1 || slave >= dev->num_slaves ||
2919 port < 1 || port > MLX4_MAX_PORTS)
2920 return 0;
2921
2922 return priv->mfunc.master.vf_admin[slave].enable_smi[port] ==
2923 MLX4_VF_SMI_ENABLED;
2924}
2925EXPORT_SYMBOL_GPL(mlx4_vf_get_enable_smi_admin);
2926
2927int mlx4_vf_set_enable_smi_admin(struct mlx4_dev *dev, int slave, int port,
2928 int enabled)
2929{
2930 struct mlx4_priv *priv = mlx4_priv(dev);
2931
2932 if (slave == mlx4_master_func_num(dev))
2933 return 0;
2934
2935 if (slave < 1 || slave >= dev->num_slaves ||
2936 port < 1 || port > MLX4_MAX_PORTS ||
2937 enabled < 0 || enabled > 1)
2938 return -EINVAL;
2939
2940 priv->mfunc.master.vf_admin[slave].enable_smi[port] = enabled;
2941 return 0;
2942}
2943EXPORT_SYMBOL_GPL(mlx4_vf_set_enable_smi_admin);