blob: a13d8a69b3bc712af6cfef774dd8b272e8ea6a56 [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>
Yevgeny Petriline8f081a2011-12-13 04:12:25 +000042#include <linux/semaphore.h>
Jack Morgenstein0a9a0182012-08-03 08:40:45 +000043#include <rdma/ib_smi.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070044
45#include <asm/io.h>
46
47#include "mlx4.h"
Yevgeny Petriline8f081a2011-12-13 04:12:25 +000048#include "fw.h"
Roland Dreier225c7b12007-05-08 18:00:38 -070049
50#define CMD_POLL_TOKEN 0xffff
Yevgeny Petriline8f081a2011-12-13 04:12:25 +000051#define INBOX_MASK 0xffffffffffffff00ULL
52
53#define CMD_CHAN_VER 1
54#define CMD_CHAN_IF_REV 1
Roland Dreier225c7b12007-05-08 18:00:38 -070055
56enum {
57 /* command completed successfully: */
58 CMD_STAT_OK = 0x00,
59 /* Internal error (such as a bus error) occurred while processing command: */
60 CMD_STAT_INTERNAL_ERR = 0x01,
61 /* Operation/command not supported or opcode modifier not supported: */
62 CMD_STAT_BAD_OP = 0x02,
63 /* Parameter not supported or parameter out of range: */
64 CMD_STAT_BAD_PARAM = 0x03,
65 /* System not enabled or bad system state: */
66 CMD_STAT_BAD_SYS_STATE = 0x04,
67 /* Attempt to access reserved or unallocaterd resource: */
68 CMD_STAT_BAD_RESOURCE = 0x05,
69 /* Requested resource is currently executing a command, or is otherwise busy: */
70 CMD_STAT_RESOURCE_BUSY = 0x06,
71 /* Required capability exceeds device limits: */
72 CMD_STAT_EXCEED_LIM = 0x08,
73 /* Resource is not in the appropriate state or ownership: */
74 CMD_STAT_BAD_RES_STATE = 0x09,
75 /* Index out of range: */
76 CMD_STAT_BAD_INDEX = 0x0a,
77 /* FW image corrupted: */
78 CMD_STAT_BAD_NVMEM = 0x0b,
Jack Morgenstein899698d2008-07-22 14:19:39 -070079 /* Error in ICM mapping (e.g. not enough auxiliary ICM pages to execute command): */
80 CMD_STAT_ICM_ERROR = 0x0c,
Roland Dreier225c7b12007-05-08 18:00:38 -070081 /* Attempt to modify a QP/EE which is not in the presumed state: */
82 CMD_STAT_BAD_QP_STATE = 0x10,
83 /* Bad segment parameters (Address/Size): */
84 CMD_STAT_BAD_SEG_PARAM = 0x20,
85 /* Memory Region has Memory Windows bound to: */
86 CMD_STAT_REG_BOUND = 0x21,
87 /* HCA local attached memory not present: */
88 CMD_STAT_LAM_NOT_PRE = 0x22,
89 /* Bad management packet (silently discarded): */
90 CMD_STAT_BAD_PKT = 0x30,
91 /* More outstanding CQEs in CQ than new CQ size: */
Yevgeny Petrilincc4ac2e2009-07-06 16:10:03 -070092 CMD_STAT_BAD_SIZE = 0x40,
93 /* Multi Function device support required: */
94 CMD_STAT_MULTI_FUNC_REQ = 0x50,
Roland Dreier225c7b12007-05-08 18:00:38 -070095};
96
97enum {
98 HCR_IN_PARAM_OFFSET = 0x00,
99 HCR_IN_MODIFIER_OFFSET = 0x08,
100 HCR_OUT_PARAM_OFFSET = 0x0c,
101 HCR_TOKEN_OFFSET = 0x14,
102 HCR_STATUS_OFFSET = 0x18,
103
104 HCR_OPMOD_SHIFT = 12,
105 HCR_T_BIT = 21,
106 HCR_E_BIT = 22,
107 HCR_GO_BIT = 23
108};
109
110enum {
Dotan Barak36ce10d2007-08-07 11:18:52 +0300111 GO_BIT_TIMEOUT_MSECS = 10000
Roland Dreier225c7b12007-05-08 18:00:38 -0700112};
113
114struct mlx4_cmd_context {
115 struct completion done;
116 int result;
117 int next;
118 u64 out_param;
119 u16 token;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000120 u8 fw_status;
Roland Dreier225c7b12007-05-08 18:00:38 -0700121};
122
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000123static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
124 struct mlx4_vhcr_cmd *in_vhcr);
125
Roland Dreierca281212008-04-16 21:01:04 -0700126static int mlx4_status_to_errno(u8 status)
127{
Roland Dreier225c7b12007-05-08 18:00:38 -0700128 static const int trans_table[] = {
129 [CMD_STAT_INTERNAL_ERR] = -EIO,
130 [CMD_STAT_BAD_OP] = -EPERM,
131 [CMD_STAT_BAD_PARAM] = -EINVAL,
132 [CMD_STAT_BAD_SYS_STATE] = -ENXIO,
133 [CMD_STAT_BAD_RESOURCE] = -EBADF,
134 [CMD_STAT_RESOURCE_BUSY] = -EBUSY,
135 [CMD_STAT_EXCEED_LIM] = -ENOMEM,
136 [CMD_STAT_BAD_RES_STATE] = -EBADF,
137 [CMD_STAT_BAD_INDEX] = -EBADF,
138 [CMD_STAT_BAD_NVMEM] = -EFAULT,
Jack Morgenstein899698d2008-07-22 14:19:39 -0700139 [CMD_STAT_ICM_ERROR] = -ENFILE,
Roland Dreier225c7b12007-05-08 18:00:38 -0700140 [CMD_STAT_BAD_QP_STATE] = -EINVAL,
141 [CMD_STAT_BAD_SEG_PARAM] = -EFAULT,
142 [CMD_STAT_REG_BOUND] = -EBUSY,
143 [CMD_STAT_LAM_NOT_PRE] = -EAGAIN,
144 [CMD_STAT_BAD_PKT] = -EINVAL,
145 [CMD_STAT_BAD_SIZE] = -ENOMEM,
Yevgeny Petrilincc4ac2e2009-07-06 16:10:03 -0700146 [CMD_STAT_MULTI_FUNC_REQ] = -EACCES,
Roland Dreier225c7b12007-05-08 18:00:38 -0700147 };
148
149 if (status >= ARRAY_SIZE(trans_table) ||
150 (status != CMD_STAT_OK && trans_table[status] == 0))
151 return -EIO;
152
153 return trans_table[status];
154}
155
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000156static u8 mlx4_errno_to_status(int errno)
157{
158 switch (errno) {
159 case -EPERM:
160 return CMD_STAT_BAD_OP;
161 case -EINVAL:
162 return CMD_STAT_BAD_PARAM;
163 case -ENXIO:
164 return CMD_STAT_BAD_SYS_STATE;
165 case -EBUSY:
166 return CMD_STAT_RESOURCE_BUSY;
167 case -ENOMEM:
168 return CMD_STAT_EXCEED_LIM;
169 case -ENFILE:
170 return CMD_STAT_ICM_ERROR;
171 default:
172 return CMD_STAT_INTERNAL_ERR;
173 }
174}
175
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000176static int comm_pending(struct mlx4_dev *dev)
177{
178 struct mlx4_priv *priv = mlx4_priv(dev);
179 u32 status = readl(&priv->mfunc.comm->slave_read);
180
181 return (swab32(status) >> 31) != priv->cmd.comm_toggle;
182}
183
184static void mlx4_comm_cmd_post(struct mlx4_dev *dev, u8 cmd, u16 param)
185{
186 struct mlx4_priv *priv = mlx4_priv(dev);
187 u32 val;
188
189 priv->cmd.comm_toggle ^= 1;
190 val = param | (cmd << 16) | (priv->cmd.comm_toggle << 31);
191 __raw_writel((__force u32) cpu_to_be32(val),
192 &priv->mfunc.comm->slave_write);
193 mmiowb();
194}
195
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000196static int mlx4_comm_cmd_poll(struct mlx4_dev *dev, u8 cmd, u16 param,
197 unsigned long timeout)
198{
199 struct mlx4_priv *priv = mlx4_priv(dev);
200 unsigned long end;
201 int err = 0;
202 int ret_from_pending = 0;
203
204 /* First, verify that the master reports correct status */
205 if (comm_pending(dev)) {
206 mlx4_warn(dev, "Communication channel is not idle."
207 "my toggle is %d (cmd:0x%x)\n",
208 priv->cmd.comm_toggle, cmd);
209 return -EAGAIN;
210 }
211
212 /* Write command */
213 down(&priv->cmd.poll_sem);
214 mlx4_comm_cmd_post(dev, cmd, param);
215
216 end = msecs_to_jiffies(timeout) + jiffies;
217 while (comm_pending(dev) && time_before(jiffies, end))
218 cond_resched();
219 ret_from_pending = comm_pending(dev);
220 if (ret_from_pending) {
221 /* check if the slave is trying to boot in the middle of
222 * FLR process. The only non-zero result in the RESET command
223 * is MLX4_DELAY_RESET_SLAVE*/
224 if ((MLX4_COMM_CMD_RESET == cmd)) {
225 mlx4_warn(dev, "Got slave FLRed from Communication"
226 " channel (ret:0x%x)\n", ret_from_pending);
227 err = MLX4_DELAY_RESET_SLAVE;
228 } else {
229 mlx4_warn(dev, "Communication channel timed out\n");
230 err = -ETIMEDOUT;
231 }
232 }
233
234 up(&priv->cmd.poll_sem);
235 return err;
236}
237
238static int mlx4_comm_cmd_wait(struct mlx4_dev *dev, u8 op,
239 u16 param, unsigned long timeout)
240{
241 struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
242 struct mlx4_cmd_context *context;
Eugenia Emantayev58a3de02012-03-18 04:32:08 +0000243 unsigned long end;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000244 int err = 0;
245
246 down(&cmd->event_sem);
247
248 spin_lock(&cmd->context_lock);
249 BUG_ON(cmd->free_head < 0);
250 context = &cmd->context[cmd->free_head];
251 context->token += cmd->token_mask + 1;
252 cmd->free_head = context->next;
253 spin_unlock(&cmd->context_lock);
254
255 init_completion(&context->done);
256
257 mlx4_comm_cmd_post(dev, op, param);
258
259 if (!wait_for_completion_timeout(&context->done,
260 msecs_to_jiffies(timeout))) {
261 err = -EBUSY;
262 goto out;
263 }
264
265 err = context->result;
266 if (err && context->fw_status != CMD_STAT_MULTI_FUNC_REQ) {
267 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
268 op, context->fw_status);
269 goto out;
270 }
271
272out:
Eugenia Emantayev58a3de02012-03-18 04:32:08 +0000273 /* wait for comm channel ready
274 * this is necessary for prevention the race
275 * when switching between event to polling mode
276 */
277 end = msecs_to_jiffies(timeout) + jiffies;
278 while (comm_pending(dev) && time_before(jiffies, end))
279 cond_resched();
280
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000281 spin_lock(&cmd->context_lock);
282 context->next = cmd->free_head;
283 cmd->free_head = context - cmd->context;
284 spin_unlock(&cmd->context_lock);
285
286 up(&cmd->event_sem);
287 return err;
288}
289
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000290int mlx4_comm_cmd(struct mlx4_dev *dev, u8 cmd, u16 param,
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000291 unsigned long timeout)
292{
293 if (mlx4_priv(dev)->cmd.use_events)
294 return mlx4_comm_cmd_wait(dev, cmd, param, timeout);
295 return mlx4_comm_cmd_poll(dev, cmd, param, timeout);
296}
297
Roland Dreier225c7b12007-05-08 18:00:38 -0700298static int cmd_pending(struct mlx4_dev *dev)
299{
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000300 u32 status;
301
302 if (pci_channel_offline(dev->pdev))
303 return -EIO;
304
305 status = readl(mlx4_priv(dev)->cmd.hcr + HCR_STATUS_OFFSET);
Roland Dreier225c7b12007-05-08 18:00:38 -0700306
307 return (status & swab32(1 << HCR_GO_BIT)) ||
308 (mlx4_priv(dev)->cmd.toggle ==
309 !!(status & swab32(1 << HCR_T_BIT)));
310}
311
312static int mlx4_cmd_post(struct mlx4_dev *dev, u64 in_param, u64 out_param,
313 u32 in_modifier, u8 op_modifier, u16 op, u16 token,
314 int event)
315{
316 struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
317 u32 __iomem *hcr = cmd->hcr;
318 int ret = -EAGAIN;
319 unsigned long end;
320
321 mutex_lock(&cmd->hcr_mutex);
322
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000323 if (pci_channel_offline(dev->pdev)) {
324 /*
325 * Device is going through error recovery
326 * and cannot accept commands.
327 */
328 ret = -EIO;
329 goto out;
330 }
331
Roland Dreier225c7b12007-05-08 18:00:38 -0700332 end = jiffies;
333 if (event)
Dotan Barak36ce10d2007-08-07 11:18:52 +0300334 end += msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS);
Roland Dreier225c7b12007-05-08 18:00:38 -0700335
336 while (cmd_pending(dev)) {
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000337 if (pci_channel_offline(dev->pdev)) {
338 /*
339 * Device is going through error recovery
340 * and cannot accept commands.
341 */
342 ret = -EIO;
343 goto out;
344 }
345
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000346 if (time_after_eq(jiffies, end)) {
347 mlx4_err(dev, "%s:cmd_pending failed\n", __func__);
Roland Dreier225c7b12007-05-08 18:00:38 -0700348 goto out;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000349 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700350 cond_resched();
351 }
352
353 /*
354 * We use writel (instead of something like memcpy_toio)
355 * because writes of less than 32 bits to the HCR don't work
356 * (and some architectures such as ia64 implement memcpy_toio
357 * in terms of writeb).
358 */
359 __raw_writel((__force u32) cpu_to_be32(in_param >> 32), hcr + 0);
360 __raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful), hcr + 1);
361 __raw_writel((__force u32) cpu_to_be32(in_modifier), hcr + 2);
362 __raw_writel((__force u32) cpu_to_be32(out_param >> 32), hcr + 3);
363 __raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), hcr + 4);
364 __raw_writel((__force u32) cpu_to_be32(token << 16), hcr + 5);
365
366 /* __raw_writel may not order writes. */
367 wmb();
368
369 __raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT) |
370 (cmd->toggle << HCR_T_BIT) |
371 (event ? (1 << HCR_E_BIT) : 0) |
372 (op_modifier << HCR_OPMOD_SHIFT) |
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000373 op), hcr + 6);
Roland Dreier2e61c642007-10-09 19:59:18 -0700374
375 /*
376 * Make sure that our HCR writes don't get mixed in with
377 * writes from another CPU starting a FW command.
378 */
379 mmiowb();
380
Roland Dreier225c7b12007-05-08 18:00:38 -0700381 cmd->toggle = cmd->toggle ^ 1;
382
383 ret = 0;
384
385out:
386 mutex_unlock(&cmd->hcr_mutex);
387 return ret;
388}
389
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000390static int mlx4_slave_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
391 int out_is_imm, u32 in_modifier, u8 op_modifier,
392 u16 op, unsigned long timeout)
393{
394 struct mlx4_priv *priv = mlx4_priv(dev);
395 struct mlx4_vhcr_cmd *vhcr = priv->mfunc.vhcr;
396 int ret;
397
398 down(&priv->cmd.slave_sem);
399 vhcr->in_param = cpu_to_be64(in_param);
400 vhcr->out_param = out_param ? cpu_to_be64(*out_param) : 0;
401 vhcr->in_modifier = cpu_to_be32(in_modifier);
402 vhcr->opcode = cpu_to_be16((((u16) op_modifier) << 12) | (op & 0xfff));
403 vhcr->token = cpu_to_be16(CMD_POLL_TOKEN);
404 vhcr->status = 0;
405 vhcr->flags = !!(priv->cmd.use_events) << 6;
406 if (mlx4_is_master(dev)) {
407 ret = mlx4_master_process_vhcr(dev, dev->caps.function, vhcr);
408 if (!ret) {
409 if (out_is_imm) {
410 if (out_param)
411 *out_param =
412 be64_to_cpu(vhcr->out_param);
413 else {
414 mlx4_err(dev, "response expected while"
415 "output mailbox is NULL for "
416 "command 0x%x\n", op);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000417 vhcr->status = CMD_STAT_BAD_PARAM;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000418 }
419 }
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000420 ret = mlx4_status_to_errno(vhcr->status);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000421 }
422 } else {
423 ret = mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_POST, 0,
424 MLX4_COMM_TIME + timeout);
425 if (!ret) {
426 if (out_is_imm) {
427 if (out_param)
428 *out_param =
429 be64_to_cpu(vhcr->out_param);
430 else {
431 mlx4_err(dev, "response expected while"
432 "output mailbox is NULL for "
433 "command 0x%x\n", op);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000434 vhcr->status = CMD_STAT_BAD_PARAM;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000435 }
436 }
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +0000437 ret = mlx4_status_to_errno(vhcr->status);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000438 } else
439 mlx4_err(dev, "failed execution of VHCR_POST command"
440 "opcode 0x%x\n", op);
441 }
442 up(&priv->cmd.slave_sem);
443 return ret;
444}
445
Roland Dreier225c7b12007-05-08 18:00:38 -0700446static int mlx4_cmd_poll(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
447 int out_is_imm, u32 in_modifier, u8 op_modifier,
448 u16 op, unsigned long timeout)
449{
450 struct mlx4_priv *priv = mlx4_priv(dev);
451 void __iomem *hcr = priv->cmd.hcr;
452 int err = 0;
453 unsigned long end;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000454 u32 stat;
Roland Dreier225c7b12007-05-08 18:00:38 -0700455
456 down(&priv->cmd.poll_sem);
457
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000458 if (pci_channel_offline(dev->pdev)) {
459 /*
460 * Device is going through error recovery
461 * and cannot accept commands.
462 */
463 err = -EIO;
464 goto out;
465 }
466
Roland Dreier225c7b12007-05-08 18:00:38 -0700467 err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
468 in_modifier, op_modifier, op, CMD_POLL_TOKEN, 0);
469 if (err)
470 goto out;
471
472 end = msecs_to_jiffies(timeout) + jiffies;
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000473 while (cmd_pending(dev) && time_before(jiffies, end)) {
474 if (pci_channel_offline(dev->pdev)) {
475 /*
476 * Device is going through error recovery
477 * and cannot accept commands.
478 */
479 err = -EIO;
480 goto out;
481 }
482
Roland Dreier225c7b12007-05-08 18:00:38 -0700483 cond_resched();
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000484 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700485
486 if (cmd_pending(dev)) {
487 err = -ETIMEDOUT;
488 goto out;
489 }
490
491 if (out_is_imm)
492 *out_param =
493 (u64) be32_to_cpu((__force __be32)
494 __raw_readl(hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
495 (u64) be32_to_cpu((__force __be32)
496 __raw_readl(hcr + HCR_OUT_PARAM_OFFSET + 4));
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000497 stat = be32_to_cpu((__force __be32)
498 __raw_readl(hcr + HCR_STATUS_OFFSET)) >> 24;
499 err = mlx4_status_to_errno(stat);
500 if (err)
501 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
502 op, stat);
Roland Dreier225c7b12007-05-08 18:00:38 -0700503
504out:
505 up(&priv->cmd.poll_sem);
506 return err;
507}
508
509void mlx4_cmd_event(struct mlx4_dev *dev, u16 token, u8 status, u64 out_param)
510{
511 struct mlx4_priv *priv = mlx4_priv(dev);
512 struct mlx4_cmd_context *context =
513 &priv->cmd.context[token & priv->cmd.token_mask];
514
515 /* previously timed out command completing at long last */
516 if (token != context->token)
517 return;
518
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000519 context->fw_status = status;
Roland Dreier225c7b12007-05-08 18:00:38 -0700520 context->result = mlx4_status_to_errno(status);
521 context->out_param = out_param;
522
Roland Dreier225c7b12007-05-08 18:00:38 -0700523 complete(&context->done);
524}
525
526static int mlx4_cmd_wait(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
527 int out_is_imm, u32 in_modifier, u8 op_modifier,
528 u16 op, unsigned long timeout)
529{
530 struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
531 struct mlx4_cmd_context *context;
532 int err = 0;
533
534 down(&cmd->event_sem);
535
536 spin_lock(&cmd->context_lock);
537 BUG_ON(cmd->free_head < 0);
538 context = &cmd->context[cmd->free_head];
Roland Dreier09815822007-07-20 21:19:43 -0700539 context->token += cmd->token_mask + 1;
Roland Dreier225c7b12007-05-08 18:00:38 -0700540 cmd->free_head = context->next;
541 spin_unlock(&cmd->context_lock);
542
543 init_completion(&context->done);
544
545 mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
546 in_modifier, op_modifier, op, context->token, 1);
547
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000548 if (!wait_for_completion_timeout(&context->done,
549 msecs_to_jiffies(timeout))) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700550 err = -EBUSY;
551 goto out;
552 }
553
554 err = context->result;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000555 if (err) {
556 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
557 op, context->fw_status);
Roland Dreier225c7b12007-05-08 18:00:38 -0700558 goto out;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000559 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700560
561 if (out_is_imm)
562 *out_param = context->out_param;
563
564out:
565 spin_lock(&cmd->context_lock);
566 context->next = cmd->free_head;
567 cmd->free_head = context - cmd->context;
568 spin_unlock(&cmd->context_lock);
569
570 up(&cmd->event_sem);
571 return err;
572}
573
574int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
575 int out_is_imm, u32 in_modifier, u8 op_modifier,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000576 u16 op, unsigned long timeout, int native)
Roland Dreier225c7b12007-05-08 18:00:38 -0700577{
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +0000578 if (pci_channel_offline(dev->pdev))
579 return -EIO;
580
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000581 if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
582 if (mlx4_priv(dev)->cmd.use_events)
583 return mlx4_cmd_wait(dev, in_param, out_param,
584 out_is_imm, in_modifier,
585 op_modifier, op, timeout);
586 else
587 return mlx4_cmd_poll(dev, in_param, out_param,
588 out_is_imm, in_modifier,
589 op_modifier, op, timeout);
590 }
591 return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
592 in_modifier, op_modifier, op, timeout);
Roland Dreier225c7b12007-05-08 18:00:38 -0700593}
594EXPORT_SYMBOL_GPL(__mlx4_cmd);
595
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000596
597static int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
598{
599 return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
600 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
601}
602
603static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
604 int slave, u64 slave_addr,
605 int size, int is_read)
606{
607 u64 in_param;
608 u64 out_param;
609
610 if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
611 (slave & ~0x7f) | (size & 0xff)) {
612 mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx "
613 "master_addr:0x%llx slave_id:%d size:%d\n",
614 slave_addr, master_addr, slave, size);
615 return -EINVAL;
616 }
617
618 if (is_read) {
619 in_param = (u64) slave | slave_addr;
620 out_param = (u64) dev->caps.function | master_addr;
621 } else {
622 in_param = (u64) dev->caps.function | master_addr;
623 out_param = (u64) slave | slave_addr;
624 }
625
626 return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
627 MLX4_CMD_ACCESS_MEM,
628 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
629}
630
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000631static int query_pkey_block(struct mlx4_dev *dev, u8 port, u16 index, u16 *pkey,
632 struct mlx4_cmd_mailbox *inbox,
633 struct mlx4_cmd_mailbox *outbox)
634{
635 struct ib_smp *in_mad = (struct ib_smp *)(inbox->buf);
636 struct ib_smp *out_mad = (struct ib_smp *)(outbox->buf);
637 int err;
638 int i;
639
640 if (index & 0x1f)
641 return -EINVAL;
642
643 in_mad->attr_mod = cpu_to_be32(index / 32);
644
645 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
646 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
647 MLX4_CMD_NATIVE);
648 if (err)
649 return err;
650
651 for (i = 0; i < 32; ++i)
652 pkey[i] = be16_to_cpu(((__be16 *) out_mad->data)[i]);
653
654 return err;
655}
656
657static int get_full_pkey_table(struct mlx4_dev *dev, u8 port, u16 *table,
658 struct mlx4_cmd_mailbox *inbox,
659 struct mlx4_cmd_mailbox *outbox)
660{
661 int i;
662 int err;
663
664 for (i = 0; i < dev->caps.pkey_table_len[port]; i += 32) {
665 err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
666 if (err)
667 return err;
668 }
669
670 return 0;
671}
672#define PORT_CAPABILITY_LOCATION_IN_SMP 20
673#define PORT_STATE_OFFSET 32
674
675static enum ib_port_state vf_port_state(struct mlx4_dev *dev, int port, int vf)
676{
677 /* will be modified when add alias_guid feature */
678 return IB_PORT_DOWN;
679}
680
681static int mlx4_MAD_IFC_wrapper(struct mlx4_dev *dev, int slave,
682 struct mlx4_vhcr *vhcr,
683 struct mlx4_cmd_mailbox *inbox,
684 struct mlx4_cmd_mailbox *outbox,
685 struct mlx4_cmd_info *cmd)
686{
687 struct ib_smp *smp = inbox->buf;
688 u32 index;
689 u8 port;
690 u16 *table;
691 int err;
692 int vidx, pidx;
693 struct mlx4_priv *priv = mlx4_priv(dev);
694 struct ib_smp *outsmp = outbox->buf;
695 __be16 *outtab = (__be16 *)(outsmp->data);
696 __be32 slave_cap_mask;
697 port = vhcr->in_modifier;
698
699 if (smp->base_version == 1 &&
700 smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
701 smp->class_version == 1) {
702 if (smp->method == IB_MGMT_METHOD_GET) {
703 if (smp->attr_id == IB_SMP_ATTR_PKEY_TABLE) {
704 index = be32_to_cpu(smp->attr_mod);
705 if (port < 1 || port > dev->caps.num_ports)
706 return -EINVAL;
707 table = kcalloc(dev->caps.pkey_table_len[port], sizeof *table, GFP_KERNEL);
708 if (!table)
709 return -ENOMEM;
710 /* need to get the full pkey table because the paravirtualized
711 * pkeys may be scattered among several pkey blocks.
712 */
713 err = get_full_pkey_table(dev, port, table, inbox, outbox);
714 if (!err) {
715 for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
716 pidx = priv->virt2phys_pkey[slave][port - 1][vidx];
717 outtab[vidx % 32] = cpu_to_be16(table[pidx]);
718 }
719 }
720 kfree(table);
721 return err;
722 }
723 if (smp->attr_id == IB_SMP_ATTR_PORT_INFO) {
724 /*get the slave specific caps:*/
725 /*do the command */
726 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
727 vhcr->in_modifier, vhcr->op_modifier,
728 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
729 /* modify the response for slaves */
730 if (!err && slave != mlx4_master_func_num(dev)) {
731 u8 *state = outsmp->data + PORT_STATE_OFFSET;
732
733 *state = (*state & 0xf0) | vf_port_state(dev, port, slave);
734 slave_cap_mask = priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
735 memcpy(outsmp->data + PORT_CAPABILITY_LOCATION_IN_SMP, &slave_cap_mask, 4);
736 }
737 return err;
738 }
739 if (smp->attr_id == IB_SMP_ATTR_GUID_INFO) {
740 /* compute slave's gid block */
741 smp->attr_mod = cpu_to_be32(slave / 8);
742 /* execute cmd */
743 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
744 vhcr->in_modifier, vhcr->op_modifier,
745 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
746 if (!err) {
747 /* if needed, move slave gid to index 0 */
748 if (slave % 8)
749 memcpy(outsmp->data,
750 outsmp->data + (slave % 8) * 8, 8);
751 /* delete all other gids */
752 memset(outsmp->data + 8, 0, 56);
753 }
754 return err;
755 }
756 }
757 }
758 if (slave != mlx4_master_func_num(dev) &&
759 ((smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) ||
760 (smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
761 smp->method == IB_MGMT_METHOD_SET))) {
762 mlx4_err(dev, "slave %d is trying to execute a Subnet MGMT MAD, "
763 "class 0x%x, method 0x%x for attr 0x%x. Rejecting\n",
764 slave, smp->method, smp->mgmt_class,
765 be16_to_cpu(smp->attr_id));
766 return -EPERM;
767 }
768 /*default:*/
769 return mlx4_cmd_box(dev, inbox->dma, outbox->dma,
770 vhcr->in_modifier, vhcr->op_modifier,
771 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
772}
773
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000774int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
775 struct mlx4_vhcr *vhcr,
776 struct mlx4_cmd_mailbox *inbox,
777 struct mlx4_cmd_mailbox *outbox,
778 struct mlx4_cmd_info *cmd)
779{
780 u64 in_param;
781 u64 out_param;
782 int err;
783
784 in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
785 out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
786 if (cmd->encode_slave_id) {
787 in_param &= 0xffffffffffffff00ll;
788 in_param |= slave;
789 }
790
791 err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
792 vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
793 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
794
795 if (cmd->out_is_imm)
796 vhcr->out_param = out_param;
797
798 return err;
799}
800
801static struct mlx4_cmd_info cmd_info[] = {
802 {
803 .opcode = MLX4_CMD_QUERY_FW,
804 .has_inbox = false,
805 .has_outbox = true,
806 .out_is_imm = false,
807 .encode_slave_id = false,
808 .verify = NULL,
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +0000809 .wrapper = mlx4_QUERY_FW_wrapper
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000810 },
811 {
812 .opcode = MLX4_CMD_QUERY_HCA,
813 .has_inbox = false,
814 .has_outbox = true,
815 .out_is_imm = false,
816 .encode_slave_id = false,
817 .verify = NULL,
818 .wrapper = NULL
819 },
820 {
821 .opcode = MLX4_CMD_QUERY_DEV_CAP,
822 .has_inbox = false,
823 .has_outbox = true,
824 .out_is_imm = false,
825 .encode_slave_id = false,
826 .verify = NULL,
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +0000827 .wrapper = mlx4_QUERY_DEV_CAP_wrapper
Yevgeny Petriline8f081a2011-12-13 04:12:25 +0000828 },
Eli Cohenc82e9aa2011-12-13 04:15:24 +0000829 {
830 .opcode = MLX4_CMD_QUERY_FUNC_CAP,
831 .has_inbox = false,
832 .has_outbox = true,
833 .out_is_imm = false,
834 .encode_slave_id = false,
835 .verify = NULL,
836 .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
837 },
838 {
839 .opcode = MLX4_CMD_QUERY_ADAPTER,
840 .has_inbox = false,
841 .has_outbox = true,
842 .out_is_imm = false,
843 .encode_slave_id = false,
844 .verify = NULL,
845 .wrapper = NULL
846 },
847 {
848 .opcode = MLX4_CMD_INIT_PORT,
849 .has_inbox = false,
850 .has_outbox = false,
851 .out_is_imm = false,
852 .encode_slave_id = false,
853 .verify = NULL,
854 .wrapper = mlx4_INIT_PORT_wrapper
855 },
856 {
857 .opcode = MLX4_CMD_CLOSE_PORT,
858 .has_inbox = false,
859 .has_outbox = false,
860 .out_is_imm = false,
861 .encode_slave_id = false,
862 .verify = NULL,
863 .wrapper = mlx4_CLOSE_PORT_wrapper
864 },
865 {
866 .opcode = MLX4_CMD_QUERY_PORT,
867 .has_inbox = false,
868 .has_outbox = true,
869 .out_is_imm = false,
870 .encode_slave_id = false,
871 .verify = NULL,
872 .wrapper = mlx4_QUERY_PORT_wrapper
873 },
874 {
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000875 .opcode = MLX4_CMD_SET_PORT,
876 .has_inbox = true,
877 .has_outbox = false,
878 .out_is_imm = false,
879 .encode_slave_id = false,
880 .verify = NULL,
881 .wrapper = mlx4_SET_PORT_wrapper
882 },
883 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +0000884 .opcode = MLX4_CMD_MAP_EQ,
885 .has_inbox = false,
886 .has_outbox = false,
887 .out_is_imm = false,
888 .encode_slave_id = false,
889 .verify = NULL,
890 .wrapper = mlx4_MAP_EQ_wrapper
891 },
892 {
893 .opcode = MLX4_CMD_SW2HW_EQ,
894 .has_inbox = true,
895 .has_outbox = false,
896 .out_is_imm = false,
897 .encode_slave_id = true,
898 .verify = NULL,
899 .wrapper = mlx4_SW2HW_EQ_wrapper
900 },
901 {
902 .opcode = MLX4_CMD_HW_HEALTH_CHECK,
903 .has_inbox = false,
904 .has_outbox = false,
905 .out_is_imm = false,
906 .encode_slave_id = false,
907 .verify = NULL,
908 .wrapper = NULL
909 },
910 {
911 .opcode = MLX4_CMD_NOP,
912 .has_inbox = false,
913 .has_outbox = false,
914 .out_is_imm = false,
915 .encode_slave_id = false,
916 .verify = NULL,
917 .wrapper = NULL
918 },
919 {
920 .opcode = MLX4_CMD_ALLOC_RES,
921 .has_inbox = false,
922 .has_outbox = false,
923 .out_is_imm = true,
924 .encode_slave_id = false,
925 .verify = NULL,
926 .wrapper = mlx4_ALLOC_RES_wrapper
927 },
928 {
929 .opcode = MLX4_CMD_FREE_RES,
930 .has_inbox = false,
931 .has_outbox = false,
932 .out_is_imm = false,
933 .encode_slave_id = false,
934 .verify = NULL,
935 .wrapper = mlx4_FREE_RES_wrapper
936 },
937 {
938 .opcode = MLX4_CMD_SW2HW_MPT,
939 .has_inbox = true,
940 .has_outbox = false,
941 .out_is_imm = false,
942 .encode_slave_id = true,
943 .verify = NULL,
944 .wrapper = mlx4_SW2HW_MPT_wrapper
945 },
946 {
947 .opcode = MLX4_CMD_QUERY_MPT,
948 .has_inbox = false,
949 .has_outbox = true,
950 .out_is_imm = false,
951 .encode_slave_id = false,
952 .verify = NULL,
953 .wrapper = mlx4_QUERY_MPT_wrapper
954 },
955 {
956 .opcode = MLX4_CMD_HW2SW_MPT,
957 .has_inbox = false,
958 .has_outbox = false,
959 .out_is_imm = false,
960 .encode_slave_id = false,
961 .verify = NULL,
962 .wrapper = mlx4_HW2SW_MPT_wrapper
963 },
964 {
965 .opcode = MLX4_CMD_READ_MTT,
966 .has_inbox = false,
967 .has_outbox = true,
968 .out_is_imm = false,
969 .encode_slave_id = false,
970 .verify = NULL,
971 .wrapper = NULL
972 },
973 {
974 .opcode = MLX4_CMD_WRITE_MTT,
975 .has_inbox = true,
976 .has_outbox = false,
977 .out_is_imm = false,
978 .encode_slave_id = false,
979 .verify = NULL,
980 .wrapper = mlx4_WRITE_MTT_wrapper
981 },
982 {
983 .opcode = MLX4_CMD_SYNC_TPT,
984 .has_inbox = true,
985 .has_outbox = false,
986 .out_is_imm = false,
987 .encode_slave_id = false,
988 .verify = NULL,
989 .wrapper = NULL
990 },
991 {
992 .opcode = MLX4_CMD_HW2SW_EQ,
993 .has_inbox = false,
994 .has_outbox = true,
995 .out_is_imm = false,
996 .encode_slave_id = true,
997 .verify = NULL,
998 .wrapper = mlx4_HW2SW_EQ_wrapper
999 },
1000 {
1001 .opcode = MLX4_CMD_QUERY_EQ,
1002 .has_inbox = false,
1003 .has_outbox = true,
1004 .out_is_imm = false,
1005 .encode_slave_id = true,
1006 .verify = NULL,
1007 .wrapper = mlx4_QUERY_EQ_wrapper
1008 },
1009 {
1010 .opcode = MLX4_CMD_SW2HW_CQ,
1011 .has_inbox = true,
1012 .has_outbox = false,
1013 .out_is_imm = false,
1014 .encode_slave_id = true,
1015 .verify = NULL,
1016 .wrapper = mlx4_SW2HW_CQ_wrapper
1017 },
1018 {
1019 .opcode = MLX4_CMD_HW2SW_CQ,
1020 .has_inbox = false,
1021 .has_outbox = false,
1022 .out_is_imm = false,
1023 .encode_slave_id = false,
1024 .verify = NULL,
1025 .wrapper = mlx4_HW2SW_CQ_wrapper
1026 },
1027 {
1028 .opcode = MLX4_CMD_QUERY_CQ,
1029 .has_inbox = false,
1030 .has_outbox = true,
1031 .out_is_imm = false,
1032 .encode_slave_id = false,
1033 .verify = NULL,
1034 .wrapper = mlx4_QUERY_CQ_wrapper
1035 },
1036 {
1037 .opcode = MLX4_CMD_MODIFY_CQ,
1038 .has_inbox = true,
1039 .has_outbox = false,
1040 .out_is_imm = true,
1041 .encode_slave_id = false,
1042 .verify = NULL,
1043 .wrapper = mlx4_MODIFY_CQ_wrapper
1044 },
1045 {
1046 .opcode = MLX4_CMD_SW2HW_SRQ,
1047 .has_inbox = true,
1048 .has_outbox = false,
1049 .out_is_imm = false,
1050 .encode_slave_id = true,
1051 .verify = NULL,
1052 .wrapper = mlx4_SW2HW_SRQ_wrapper
1053 },
1054 {
1055 .opcode = MLX4_CMD_HW2SW_SRQ,
1056 .has_inbox = false,
1057 .has_outbox = false,
1058 .out_is_imm = false,
1059 .encode_slave_id = false,
1060 .verify = NULL,
1061 .wrapper = mlx4_HW2SW_SRQ_wrapper
1062 },
1063 {
1064 .opcode = MLX4_CMD_QUERY_SRQ,
1065 .has_inbox = false,
1066 .has_outbox = true,
1067 .out_is_imm = false,
1068 .encode_slave_id = false,
1069 .verify = NULL,
1070 .wrapper = mlx4_QUERY_SRQ_wrapper
1071 },
1072 {
1073 .opcode = MLX4_CMD_ARM_SRQ,
1074 .has_inbox = false,
1075 .has_outbox = false,
1076 .out_is_imm = false,
1077 .encode_slave_id = false,
1078 .verify = NULL,
1079 .wrapper = mlx4_ARM_SRQ_wrapper
1080 },
1081 {
1082 .opcode = MLX4_CMD_RST2INIT_QP,
1083 .has_inbox = true,
1084 .has_outbox = false,
1085 .out_is_imm = false,
1086 .encode_slave_id = true,
1087 .verify = NULL,
1088 .wrapper = mlx4_RST2INIT_QP_wrapper
1089 },
1090 {
1091 .opcode = MLX4_CMD_INIT2INIT_QP,
1092 .has_inbox = true,
1093 .has_outbox = false,
1094 .out_is_imm = false,
1095 .encode_slave_id = false,
1096 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001097 .wrapper = mlx4_INIT2INIT_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001098 },
1099 {
1100 .opcode = MLX4_CMD_INIT2RTR_QP,
1101 .has_inbox = true,
1102 .has_outbox = false,
1103 .out_is_imm = false,
1104 .encode_slave_id = false,
1105 .verify = NULL,
1106 .wrapper = mlx4_INIT2RTR_QP_wrapper
1107 },
1108 {
1109 .opcode = MLX4_CMD_RTR2RTS_QP,
1110 .has_inbox = true,
1111 .has_outbox = false,
1112 .out_is_imm = false,
1113 .encode_slave_id = false,
1114 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001115 .wrapper = mlx4_RTR2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001116 },
1117 {
1118 .opcode = MLX4_CMD_RTS2RTS_QP,
1119 .has_inbox = true,
1120 .has_outbox = false,
1121 .out_is_imm = false,
1122 .encode_slave_id = false,
1123 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001124 .wrapper = mlx4_RTS2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001125 },
1126 {
1127 .opcode = MLX4_CMD_SQERR2RTS_QP,
1128 .has_inbox = true,
1129 .has_outbox = false,
1130 .out_is_imm = false,
1131 .encode_slave_id = false,
1132 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001133 .wrapper = mlx4_SQERR2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001134 },
1135 {
1136 .opcode = MLX4_CMD_2ERR_QP,
1137 .has_inbox = false,
1138 .has_outbox = false,
1139 .out_is_imm = false,
1140 .encode_slave_id = false,
1141 .verify = NULL,
1142 .wrapper = mlx4_GEN_QP_wrapper
1143 },
1144 {
1145 .opcode = MLX4_CMD_RTS2SQD_QP,
1146 .has_inbox = false,
1147 .has_outbox = false,
1148 .out_is_imm = false,
1149 .encode_slave_id = false,
1150 .verify = NULL,
1151 .wrapper = mlx4_GEN_QP_wrapper
1152 },
1153 {
1154 .opcode = MLX4_CMD_SQD2SQD_QP,
1155 .has_inbox = true,
1156 .has_outbox = false,
1157 .out_is_imm = false,
1158 .encode_slave_id = false,
1159 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001160 .wrapper = mlx4_SQD2SQD_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001161 },
1162 {
1163 .opcode = MLX4_CMD_SQD2RTS_QP,
1164 .has_inbox = true,
1165 .has_outbox = false,
1166 .out_is_imm = false,
1167 .encode_slave_id = false,
1168 .verify = NULL,
Jack Morgenstein54679e12012-08-03 08:40:43 +00001169 .wrapper = mlx4_SQD2RTS_QP_wrapper
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001170 },
1171 {
1172 .opcode = MLX4_CMD_2RST_QP,
1173 .has_inbox = false,
1174 .has_outbox = false,
1175 .out_is_imm = false,
1176 .encode_slave_id = false,
1177 .verify = NULL,
1178 .wrapper = mlx4_2RST_QP_wrapper
1179 },
1180 {
1181 .opcode = MLX4_CMD_QUERY_QP,
1182 .has_inbox = false,
1183 .has_outbox = true,
1184 .out_is_imm = false,
1185 .encode_slave_id = false,
1186 .verify = NULL,
1187 .wrapper = mlx4_GEN_QP_wrapper
1188 },
1189 {
1190 .opcode = MLX4_CMD_SUSPEND_QP,
1191 .has_inbox = false,
1192 .has_outbox = false,
1193 .out_is_imm = false,
1194 .encode_slave_id = false,
1195 .verify = NULL,
1196 .wrapper = mlx4_GEN_QP_wrapper
1197 },
1198 {
1199 .opcode = MLX4_CMD_UNSUSPEND_QP,
1200 .has_inbox = false,
1201 .has_outbox = false,
1202 .out_is_imm = false,
1203 .encode_slave_id = false,
1204 .verify = NULL,
1205 .wrapper = mlx4_GEN_QP_wrapper
1206 },
1207 {
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001208 .opcode = MLX4_CMD_CONF_SPECIAL_QP,
1209 .has_inbox = false,
1210 .has_outbox = false,
1211 .out_is_imm = false,
1212 .encode_slave_id = false,
1213 .verify = NULL, /* XXX verify: only demux can do this */
1214 .wrapper = NULL
1215 },
1216 {
1217 .opcode = MLX4_CMD_MAD_IFC,
1218 .has_inbox = true,
1219 .has_outbox = true,
1220 .out_is_imm = false,
1221 .encode_slave_id = false,
1222 .verify = NULL,
1223 .wrapper = mlx4_MAD_IFC_wrapper
1224 },
1225 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001226 .opcode = MLX4_CMD_QUERY_IF_STAT,
1227 .has_inbox = false,
1228 .has_outbox = true,
1229 .out_is_imm = false,
1230 .encode_slave_id = false,
1231 .verify = NULL,
1232 .wrapper = mlx4_QUERY_IF_STAT_wrapper
1233 },
1234 /* Native multicast commands are not available for guests */
1235 {
1236 .opcode = MLX4_CMD_QP_ATTACH,
1237 .has_inbox = true,
1238 .has_outbox = false,
1239 .out_is_imm = false,
1240 .encode_slave_id = false,
1241 .verify = NULL,
1242 .wrapper = mlx4_QP_ATTACH_wrapper
1243 },
1244 {
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +00001245 .opcode = MLX4_CMD_PROMISC,
1246 .has_inbox = false,
1247 .has_outbox = false,
1248 .out_is_imm = false,
1249 .encode_slave_id = false,
1250 .verify = NULL,
1251 .wrapper = mlx4_PROMISC_wrapper
1252 },
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001253 /* Ethernet specific commands */
1254 {
1255 .opcode = MLX4_CMD_SET_VLAN_FLTR,
1256 .has_inbox = true,
1257 .has_outbox = false,
1258 .out_is_imm = false,
1259 .encode_slave_id = false,
1260 .verify = NULL,
1261 .wrapper = mlx4_SET_VLAN_FLTR_wrapper
1262 },
1263 {
1264 .opcode = MLX4_CMD_SET_MCAST_FLTR,
1265 .has_inbox = false,
1266 .has_outbox = false,
1267 .out_is_imm = false,
1268 .encode_slave_id = false,
1269 .verify = NULL,
1270 .wrapper = mlx4_SET_MCAST_FLTR_wrapper
1271 },
1272 {
1273 .opcode = MLX4_CMD_DUMP_ETH_STATS,
1274 .has_inbox = false,
1275 .has_outbox = true,
1276 .out_is_imm = false,
1277 .encode_slave_id = false,
1278 .verify = NULL,
1279 .wrapper = mlx4_DUMP_ETH_STATS_wrapper
1280 },
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +00001281 {
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001282 .opcode = MLX4_CMD_INFORM_FLR_DONE,
1283 .has_inbox = false,
1284 .has_outbox = false,
1285 .out_is_imm = false,
1286 .encode_slave_id = false,
1287 .verify = NULL,
1288 .wrapper = NULL
1289 },
Hadar Hen Zion8fcfb4d2012-07-05 04:03:45 +00001290 /* flow steering commands */
1291 {
1292 .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
1293 .has_inbox = true,
1294 .has_outbox = false,
1295 .out_is_imm = true,
1296 .encode_slave_id = false,
1297 .verify = NULL,
1298 .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
1299 },
1300 {
1301 .opcode = MLX4_QP_FLOW_STEERING_DETACH,
1302 .has_inbox = false,
1303 .has_outbox = false,
1304 .out_is_imm = false,
1305 .encode_slave_id = false,
1306 .verify = NULL,
1307 .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
1308 },
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001309};
1310
1311static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
1312 struct mlx4_vhcr_cmd *in_vhcr)
1313{
1314 struct mlx4_priv *priv = mlx4_priv(dev);
1315 struct mlx4_cmd_info *cmd = NULL;
1316 struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
1317 struct mlx4_vhcr *vhcr;
1318 struct mlx4_cmd_mailbox *inbox = NULL;
1319 struct mlx4_cmd_mailbox *outbox = NULL;
1320 u64 in_param;
1321 u64 out_param;
1322 int ret = 0;
1323 int i;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001324 int err = 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001325
1326 /* Create sw representation of Virtual HCR */
1327 vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
1328 if (!vhcr)
1329 return -ENOMEM;
1330
1331 /* DMA in the vHCR */
1332 if (!in_vhcr) {
1333 ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1334 priv->mfunc.master.slave_state[slave].vhcr_dma,
1335 ALIGN(sizeof(struct mlx4_vhcr_cmd),
1336 MLX4_ACCESS_MEM_ALIGN), 1);
1337 if (ret) {
1338 mlx4_err(dev, "%s:Failed reading vhcr"
1339 "ret: 0x%x\n", __func__, ret);
1340 kfree(vhcr);
1341 return ret;
1342 }
1343 }
1344
1345 /* Fill SW VHCR fields */
1346 vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
1347 vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
1348 vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
1349 vhcr->token = be16_to_cpu(vhcr_cmd->token);
1350 vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
1351 vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
1352 vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
1353
1354 /* Lookup command */
1355 for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
1356 if (vhcr->op == cmd_info[i].opcode) {
1357 cmd = &cmd_info[i];
1358 break;
1359 }
1360 }
1361 if (!cmd) {
1362 mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
1363 vhcr->op, slave);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001364 vhcr_cmd->status = CMD_STAT_BAD_PARAM;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001365 goto out_status;
1366 }
1367
1368 /* Read inbox */
1369 if (cmd->has_inbox) {
1370 vhcr->in_param &= INBOX_MASK;
1371 inbox = mlx4_alloc_cmd_mailbox(dev);
1372 if (IS_ERR(inbox)) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001373 vhcr_cmd->status = CMD_STAT_BAD_SIZE;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001374 inbox = NULL;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001375 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001376 }
1377
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001378 if (mlx4_ACCESS_MEM(dev, inbox->dma, slave,
1379 vhcr->in_param,
1380 MLX4_MAILBOX_SIZE, 1)) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001381 mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
1382 __func__, cmd->opcode);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001383 vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
1384 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001385 }
1386 }
1387
1388 /* Apply permission and bound checks if applicable */
1389 if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
1390 mlx4_warn(dev, "Command:0x%x from slave: %d failed protection "
1391 "checks for resource_id:%d\n", vhcr->op, slave,
1392 vhcr->in_modifier);
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001393 vhcr_cmd->status = CMD_STAT_BAD_OP;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001394 goto out_status;
1395 }
1396
1397 /* Allocate outbox */
1398 if (cmd->has_outbox) {
1399 outbox = mlx4_alloc_cmd_mailbox(dev);
1400 if (IS_ERR(outbox)) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001401 vhcr_cmd->status = CMD_STAT_BAD_SIZE;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001402 outbox = NULL;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001403 goto out_status;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001404 }
1405 }
1406
1407 /* Execute the command! */
1408 if (cmd->wrapper) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001409 err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
1410 cmd);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001411 if (cmd->out_is_imm)
1412 vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1413 } else {
1414 in_param = cmd->has_inbox ? (u64) inbox->dma :
1415 vhcr->in_param;
1416 out_param = cmd->has_outbox ? (u64) outbox->dma :
1417 vhcr->out_param;
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001418 err = __mlx4_cmd(dev, in_param, &out_param,
1419 cmd->out_is_imm, vhcr->in_modifier,
1420 vhcr->op_modifier, vhcr->op,
1421 MLX4_CMD_TIME_CLASS_A,
1422 MLX4_CMD_NATIVE);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001423
1424 if (cmd->out_is_imm) {
1425 vhcr->out_param = out_param;
1426 vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1427 }
1428 }
1429
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001430 if (err) {
1431 mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with"
1432 " error:%d, status %d\n",
1433 vhcr->op, slave, vhcr->errno, err);
1434 vhcr_cmd->status = mlx4_errno_to_status(err);
1435 goto out_status;
1436 }
1437
1438
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001439 /* Write outbox if command completed successfully */
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001440 if (cmd->has_outbox && !vhcr_cmd->status) {
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001441 ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
1442 vhcr->out_param,
1443 MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
1444 if (ret) {
Yevgeny Petrilin72be84f2011-12-19 04:03:53 +00001445 /* If we failed to write back the outbox after the
1446 *command was successfully executed, we must fail this
1447 * slave, as it is now in undefined state */
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001448 mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
1449 goto out;
1450 }
1451 }
1452
1453out_status:
1454 /* DMA back vhcr result */
1455 if (!in_vhcr) {
1456 ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1457 priv->mfunc.master.slave_state[slave].vhcr_dma,
1458 ALIGN(sizeof(struct mlx4_vhcr),
1459 MLX4_ACCESS_MEM_ALIGN),
1460 MLX4_CMD_WRAPPED);
1461 if (ret)
1462 mlx4_err(dev, "%s:Failed writing vhcr result\n",
1463 __func__);
1464 else if (vhcr->e_bit &&
1465 mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
1466 mlx4_warn(dev, "Failed to generate command completion "
1467 "eqe for slave %d\n", slave);
1468 }
1469
1470out:
1471 kfree(vhcr);
1472 mlx4_free_cmd_mailbox(dev, inbox);
1473 mlx4_free_cmd_mailbox(dev, outbox);
1474 return ret;
1475}
1476
1477static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
1478 u16 param, u8 toggle)
1479{
1480 struct mlx4_priv *priv = mlx4_priv(dev);
1481 struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
1482 u32 reply;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001483 u8 is_going_down = 0;
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00001484 int i;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001485
1486 slave_state[slave].comm_toggle ^= 1;
1487 reply = (u32) slave_state[slave].comm_toggle << 31;
1488 if (toggle != slave_state[slave].comm_toggle) {
1489 mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER"
1490 "STATE COMPROMISIED ***\n", toggle, slave);
1491 goto reset_slave;
1492 }
1493 if (cmd == MLX4_COMM_CMD_RESET) {
1494 mlx4_warn(dev, "Received reset from slave:%d\n", slave);
1495 slave_state[slave].active = false;
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00001496 for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
1497 slave_state[slave].event_eq[i].eqn = -1;
1498 slave_state[slave].event_eq[i].token = 0;
1499 }
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001500 /*check if we are in the middle of FLR process,
1501 if so return "retry" status to the slave*/
Or Gerlitz162344e2012-05-15 10:34:57 +00001502 if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001503 goto inform_slave_state;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001504
Jack Morgensteinfc065732012-08-03 08:40:42 +00001505 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
1506
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001507 /* write the version in the event field */
1508 reply |= mlx4_comm_get_version();
1509
1510 goto reset_slave;
1511 }
1512 /*command from slave in the middle of FLR*/
1513 if (cmd != MLX4_COMM_CMD_RESET &&
1514 MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
1515 mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) "
1516 "in the middle of FLR\n", slave, cmd);
1517 return;
1518 }
1519
1520 switch (cmd) {
1521 case MLX4_COMM_CMD_VHCR0:
1522 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
1523 goto reset_slave;
1524 slave_state[slave].vhcr_dma = ((u64) param) << 48;
1525 priv->mfunc.master.slave_state[slave].cookie = 0;
1526 mutex_init(&priv->mfunc.master.gen_eqe_mutex[slave]);
1527 break;
1528 case MLX4_COMM_CMD_VHCR1:
1529 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
1530 goto reset_slave;
1531 slave_state[slave].vhcr_dma |= ((u64) param) << 32;
1532 break;
1533 case MLX4_COMM_CMD_VHCR2:
1534 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
1535 goto reset_slave;
1536 slave_state[slave].vhcr_dma |= ((u64) param) << 16;
1537 break;
1538 case MLX4_COMM_CMD_VHCR_EN:
1539 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
1540 goto reset_slave;
1541 slave_state[slave].vhcr_dma |= param;
1542 slave_state[slave].active = true;
Jack Morgensteinfc065732012-08-03 08:40:42 +00001543 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001544 break;
1545 case MLX4_COMM_CMD_VHCR_POST:
1546 if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
1547 (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST))
1548 goto reset_slave;
1549 down(&priv->cmd.slave_sem);
1550 if (mlx4_master_process_vhcr(dev, slave, NULL)) {
1551 mlx4_err(dev, "Failed processing vhcr for slave:%d,"
Masanari Iida8d9eb062012-02-03 03:27:38 +00001552 " resetting slave.\n", slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001553 up(&priv->cmd.slave_sem);
1554 goto reset_slave;
1555 }
1556 up(&priv->cmd.slave_sem);
1557 break;
1558 default:
1559 mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
1560 goto reset_slave;
1561 }
1562 spin_lock(&priv->mfunc.master.slave_state_lock);
1563 if (!slave_state[slave].is_slave_going_down)
1564 slave_state[slave].last_cmd = cmd;
1565 else
1566 is_going_down = 1;
1567 spin_unlock(&priv->mfunc.master.slave_state_lock);
1568 if (is_going_down) {
1569 mlx4_warn(dev, "Slave is going down aborting command(%d)"
1570 " executing from slave:%d\n",
1571 cmd, slave);
1572 return;
1573 }
1574 __raw_writel((__force u32) cpu_to_be32(reply),
1575 &priv->mfunc.comm[slave].slave_read);
1576 mmiowb();
1577
1578 return;
1579
1580reset_slave:
Eli Cohenc82e9aa2011-12-13 04:15:24 +00001581 /* cleanup any slave resources */
1582 mlx4_delete_all_resources_for_slave(dev, slave);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001583 spin_lock(&priv->mfunc.master.slave_state_lock);
1584 if (!slave_state[slave].is_slave_going_down)
1585 slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
1586 spin_unlock(&priv->mfunc.master.slave_state_lock);
1587 /*with slave in the middle of flr, no need to clean resources again.*/
1588inform_slave_state:
1589 memset(&slave_state[slave].event_eq, 0,
1590 sizeof(struct mlx4_slave_event_eq_info));
1591 __raw_writel((__force u32) cpu_to_be32(reply),
1592 &priv->mfunc.comm[slave].slave_read);
1593 wmb();
1594}
1595
1596/* master command processing */
1597void mlx4_master_comm_channel(struct work_struct *work)
1598{
1599 struct mlx4_mfunc_master_ctx *master =
1600 container_of(work,
1601 struct mlx4_mfunc_master_ctx,
1602 comm_work);
1603 struct mlx4_mfunc *mfunc =
1604 container_of(master, struct mlx4_mfunc, master);
1605 struct mlx4_priv *priv =
1606 container_of(mfunc, struct mlx4_priv, mfunc);
1607 struct mlx4_dev *dev = &priv->dev;
1608 __be32 *bit_vec;
1609 u32 comm_cmd;
1610 u32 vec;
1611 int i, j, slave;
1612 int toggle;
1613 int served = 0;
1614 int reported = 0;
1615 u32 slt;
1616
1617 bit_vec = master->comm_arm_bit_vector;
1618 for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
1619 vec = be32_to_cpu(bit_vec[i]);
1620 for (j = 0; j < 32; j++) {
1621 if (!(vec & (1 << j)))
1622 continue;
1623 ++reported;
1624 slave = (i * 32) + j;
1625 comm_cmd = swab32(readl(
1626 &mfunc->comm[slave].slave_write));
1627 slt = swab32(readl(&mfunc->comm[slave].slave_read))
1628 >> 31;
1629 toggle = comm_cmd >> 31;
1630 if (toggle != slt) {
1631 if (master->slave_state[slave].comm_toggle
1632 != slt) {
1633 printk(KERN_INFO "slave %d out of sync."
1634 " read toggle %d, state toggle %d. "
1635 "Resynching.\n", slave, slt,
1636 master->slave_state[slave].comm_toggle);
1637 master->slave_state[slave].comm_toggle =
1638 slt;
1639 }
1640 mlx4_master_do_cmd(dev, slave,
1641 comm_cmd >> 16 & 0xff,
1642 comm_cmd & 0xffff, toggle);
1643 ++served;
1644 }
1645 }
1646 }
1647
1648 if (reported && reported != served)
1649 mlx4_warn(dev, "Got command event with bitmask from %d slaves"
1650 " but %d were served\n",
1651 reported, served);
1652
1653 if (mlx4_ARM_COMM_CHANNEL(dev))
1654 mlx4_warn(dev, "Failed to arm comm channel events\n");
1655}
1656
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001657static int sync_toggles(struct mlx4_dev *dev)
1658{
1659 struct mlx4_priv *priv = mlx4_priv(dev);
1660 int wr_toggle;
1661 int rd_toggle;
1662 unsigned long end;
1663
1664 wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write)) >> 31;
1665 end = jiffies + msecs_to_jiffies(5000);
1666
1667 while (time_before(jiffies, end)) {
1668 rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read)) >> 31;
1669 if (rd_toggle == wr_toggle) {
1670 priv->cmd.comm_toggle = rd_toggle;
1671 return 0;
1672 }
1673
1674 cond_resched();
1675 }
1676
1677 /*
1678 * we could reach here if for example the previous VM using this
1679 * function misbehaved and left the channel with unsynced state. We
1680 * should fix this here and give this VM a chance to use a properly
1681 * synced channel
1682 */
1683 mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
1684 __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
1685 __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
1686 priv->cmd.comm_toggle = 0;
1687
1688 return 0;
1689}
1690
1691int mlx4_multi_func_init(struct mlx4_dev *dev)
1692{
1693 struct mlx4_priv *priv = mlx4_priv(dev);
1694 struct mlx4_slave_state *s_state;
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00001695 int i, j, err, port;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001696
1697 priv->mfunc.vhcr = dma_alloc_coherent(&(dev->pdev->dev), PAGE_SIZE,
1698 &priv->mfunc.vhcr_dma,
1699 GFP_KERNEL);
1700 if (!priv->mfunc.vhcr) {
1701 mlx4_err(dev, "Couldn't allocate vhcr.\n");
1702 return -ENOMEM;
1703 }
1704
1705 if (mlx4_is_master(dev))
1706 priv->mfunc.comm =
1707 ioremap(pci_resource_start(dev->pdev, priv->fw.comm_bar) +
1708 priv->fw.comm_base, MLX4_COMM_PAGESIZE);
1709 else
1710 priv->mfunc.comm =
1711 ioremap(pci_resource_start(dev->pdev, 2) +
1712 MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
1713 if (!priv->mfunc.comm) {
1714 mlx4_err(dev, "Couldn't map communication vector.\n");
1715 goto err_vhcr;
1716 }
1717
1718 if (mlx4_is_master(dev)) {
1719 priv->mfunc.master.slave_state =
1720 kzalloc(dev->num_slaves *
1721 sizeof(struct mlx4_slave_state), GFP_KERNEL);
1722 if (!priv->mfunc.master.slave_state)
1723 goto err_comm;
1724
1725 for (i = 0; i < dev->num_slaves; ++i) {
1726 s_state = &priv->mfunc.master.slave_state[i];
1727 s_state->last_cmd = MLX4_COMM_CMD_RESET;
Marcel Apfelbaum803143f2012-01-19 09:45:46 +00001728 for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
1729 s_state->event_eq[j].eqn = -1;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001730 __raw_writel((__force u32) 0,
1731 &priv->mfunc.comm[i].slave_write);
1732 __raw_writel((__force u32) 0,
1733 &priv->mfunc.comm[i].slave_read);
1734 mmiowb();
1735 for (port = 1; port <= MLX4_MAX_PORTS; port++) {
1736 s_state->vlan_filter[port] =
1737 kzalloc(sizeof(struct mlx4_vlan_fltr),
1738 GFP_KERNEL);
1739 if (!s_state->vlan_filter[port]) {
1740 if (--port)
1741 kfree(s_state->vlan_filter[port]);
1742 goto err_slaves;
1743 }
1744 INIT_LIST_HEAD(&s_state->mcast_filters[port]);
1745 }
1746 spin_lock_init(&s_state->lock);
1747 }
1748
1749 memset(&priv->mfunc.master.cmd_eqe, 0, sizeof(struct mlx4_eqe));
1750 priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
1751 INIT_WORK(&priv->mfunc.master.comm_work,
1752 mlx4_master_comm_channel);
1753 INIT_WORK(&priv->mfunc.master.slave_event_work,
1754 mlx4_gen_slave_eqe);
1755 INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
1756 mlx4_master_handle_slave_flr);
1757 spin_lock_init(&priv->mfunc.master.slave_state_lock);
1758 priv->mfunc.master.comm_wq =
1759 create_singlethread_workqueue("mlx4_comm");
1760 if (!priv->mfunc.master.comm_wq)
1761 goto err_slaves;
1762
1763 if (mlx4_init_resource_tracker(dev))
1764 goto err_thread;
1765
1766 sema_init(&priv->cmd.slave_sem, 1);
1767 err = mlx4_ARM_COMM_CHANNEL(dev);
1768 if (err) {
1769 mlx4_err(dev, " Failed to arm comm channel eq: %x\n",
1770 err);
1771 goto err_resource;
1772 }
1773
1774 } else {
1775 err = sync_toggles(dev);
1776 if (err) {
1777 mlx4_err(dev, "Couldn't sync toggles\n");
1778 goto err_comm;
1779 }
1780
1781 sema_init(&priv->cmd.slave_sem, 1);
1782 }
1783 return 0;
1784
1785err_resource:
Jack Morgensteinb8924952012-05-15 10:35:02 +00001786 mlx4_free_resource_tracker(dev, RES_TR_FREE_ALL);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001787err_thread:
1788 flush_workqueue(priv->mfunc.master.comm_wq);
1789 destroy_workqueue(priv->mfunc.master.comm_wq);
1790err_slaves:
1791 while (--i) {
1792 for (port = 1; port <= MLX4_MAX_PORTS; port++)
1793 kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
1794 }
1795 kfree(priv->mfunc.master.slave_state);
1796err_comm:
1797 iounmap(priv->mfunc.comm);
1798err_vhcr:
1799 dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
1800 priv->mfunc.vhcr,
1801 priv->mfunc.vhcr_dma);
1802 priv->mfunc.vhcr = NULL;
1803 return -ENOMEM;
1804}
1805
Roland Dreier225c7b12007-05-08 18:00:38 -07001806int mlx4_cmd_init(struct mlx4_dev *dev)
1807{
1808 struct mlx4_priv *priv = mlx4_priv(dev);
1809
1810 mutex_init(&priv->cmd.hcr_mutex);
1811 sema_init(&priv->cmd.poll_sem, 1);
1812 priv->cmd.use_events = 0;
1813 priv->cmd.toggle = 1;
1814
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001815 priv->cmd.hcr = NULL;
1816 priv->mfunc.vhcr = NULL;
1817
1818 if (!mlx4_is_slave(dev)) {
1819 priv->cmd.hcr = ioremap(pci_resource_start(dev->pdev, 0) +
1820 MLX4_HCR_BASE, MLX4_HCR_SIZE);
1821 if (!priv->cmd.hcr) {
1822 mlx4_err(dev, "Couldn't map command register.\n");
1823 return -ENOMEM;
1824 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001825 }
1826
1827 priv->cmd.pool = pci_pool_create("mlx4_cmd", dev->pdev,
1828 MLX4_MAILBOX_SIZE,
1829 MLX4_MAILBOX_SIZE, 0);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001830 if (!priv->cmd.pool)
1831 goto err_hcr;
Roland Dreier225c7b12007-05-08 18:00:38 -07001832
1833 return 0;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001834
1835err_hcr:
1836 if (!mlx4_is_slave(dev))
1837 iounmap(priv->cmd.hcr);
1838 return -ENOMEM;
Roland Dreier225c7b12007-05-08 18:00:38 -07001839}
1840
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001841void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
1842{
1843 struct mlx4_priv *priv = mlx4_priv(dev);
1844 int i, port;
1845
1846 if (mlx4_is_master(dev)) {
1847 flush_workqueue(priv->mfunc.master.comm_wq);
1848 destroy_workqueue(priv->mfunc.master.comm_wq);
1849 for (i = 0; i < dev->num_slaves; i++) {
1850 for (port = 1; port <= MLX4_MAX_PORTS; port++)
1851 kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
1852 }
1853 kfree(priv->mfunc.master.slave_state);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001854 }
Eugenia Emantayevf08ad062012-02-06 06:26:17 +00001855
1856 iounmap(priv->mfunc.comm);
1857 dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
1858 priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
1859 priv->mfunc.vhcr = NULL;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001860}
1861
Roland Dreier225c7b12007-05-08 18:00:38 -07001862void mlx4_cmd_cleanup(struct mlx4_dev *dev)
1863{
1864 struct mlx4_priv *priv = mlx4_priv(dev);
1865
1866 pci_pool_destroy(priv->cmd.pool);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001867
1868 if (!mlx4_is_slave(dev))
1869 iounmap(priv->cmd.hcr);
Roland Dreier225c7b12007-05-08 18:00:38 -07001870}
1871
1872/*
1873 * Switch to using events to issue FW commands (can only be called
1874 * after event queue for command events has been initialized).
1875 */
1876int mlx4_cmd_use_events(struct mlx4_dev *dev)
1877{
1878 struct mlx4_priv *priv = mlx4_priv(dev);
1879 int i;
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001880 int err = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07001881
1882 priv->cmd.context = kmalloc(priv->cmd.max_cmds *
1883 sizeof (struct mlx4_cmd_context),
1884 GFP_KERNEL);
1885 if (!priv->cmd.context)
1886 return -ENOMEM;
1887
1888 for (i = 0; i < priv->cmd.max_cmds; ++i) {
1889 priv->cmd.context[i].token = i;
1890 priv->cmd.context[i].next = i + 1;
1891 }
1892
1893 priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
1894 priv->cmd.free_head = 0;
1895
1896 sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
1897 spin_lock_init(&priv->cmd.context_lock);
1898
1899 for (priv->cmd.token_mask = 1;
1900 priv->cmd.token_mask < priv->cmd.max_cmds;
1901 priv->cmd.token_mask <<= 1)
1902 ; /* nothing */
1903 --priv->cmd.token_mask;
1904
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001905 down(&priv->cmd.poll_sem);
Roland Dreier225c7b12007-05-08 18:00:38 -07001906 priv->cmd.use_events = 1;
1907
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001908 return err;
Roland Dreier225c7b12007-05-08 18:00:38 -07001909}
1910
1911/*
1912 * Switch back to polling (used when shutting down the device)
1913 */
1914void mlx4_cmd_use_polling(struct mlx4_dev *dev)
1915{
1916 struct mlx4_priv *priv = mlx4_priv(dev);
1917 int i;
1918
1919 priv->cmd.use_events = 0;
1920
1921 for (i = 0; i < priv->cmd.max_cmds; ++i)
1922 down(&priv->cmd.event_sem);
1923
1924 kfree(priv->cmd.context);
1925
1926 up(&priv->cmd.poll_sem);
1927}
1928
1929struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
1930{
1931 struct mlx4_cmd_mailbox *mailbox;
1932
1933 mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
1934 if (!mailbox)
1935 return ERR_PTR(-ENOMEM);
1936
1937 mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
1938 &mailbox->dma);
1939 if (!mailbox->buf) {
1940 kfree(mailbox);
1941 return ERR_PTR(-ENOMEM);
1942 }
1943
1944 return mailbox;
1945}
1946EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
1947
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001948void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
1949 struct mlx4_cmd_mailbox *mailbox)
Roland Dreier225c7b12007-05-08 18:00:38 -07001950{
1951 if (!mailbox)
1952 return;
1953
1954 pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
1955 kfree(mailbox);
1956}
1957EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
Yevgeny Petriline8f081a2011-12-13 04:12:25 +00001958
1959u32 mlx4_comm_get_version(void)
1960{
1961 return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
1962}