2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc. All rights reserved.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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.
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
35 #include <linux/sched.h>
36 #include <linux/slab.h>
37 #include <linux/export.h>
38 #include <linux/pci.h>
39 #include <linux/errno.h>
41 #include <linux/mlx4/cmd.h>
42 #include <linux/mlx4/device.h>
43 #include <linux/semaphore.h>
44 #include <rdma/ib_smi.h>
51 #define CMD_POLL_TOKEN 0xffff
52 #define INBOX_MASK 0xffffffffffffff00ULL
54 #define CMD_CHAN_VER 1
55 #define CMD_CHAN_IF_REV 1
58 /* command completed successfully: */
60 /* Internal error (such as a bus error) occurred while processing command: */
61 CMD_STAT_INTERNAL_ERR = 0x01,
62 /* Operation/command not supported or opcode modifier not supported: */
63 CMD_STAT_BAD_OP = 0x02,
64 /* Parameter not supported or parameter out of range: */
65 CMD_STAT_BAD_PARAM = 0x03,
66 /* System not enabled or bad system state: */
67 CMD_STAT_BAD_SYS_STATE = 0x04,
68 /* Attempt to access reserved or unallocaterd resource: */
69 CMD_STAT_BAD_RESOURCE = 0x05,
70 /* Requested resource is currently executing a command, or is otherwise busy: */
71 CMD_STAT_RESOURCE_BUSY = 0x06,
72 /* Required capability exceeds device limits: */
73 CMD_STAT_EXCEED_LIM = 0x08,
74 /* Resource is not in the appropriate state or ownership: */
75 CMD_STAT_BAD_RES_STATE = 0x09,
76 /* Index out of range: */
77 CMD_STAT_BAD_INDEX = 0x0a,
78 /* FW image corrupted: */
79 CMD_STAT_BAD_NVMEM = 0x0b,
80 /* Error in ICM mapping (e.g. not enough auxiliary ICM pages to execute command): */
81 CMD_STAT_ICM_ERROR = 0x0c,
82 /* Attempt to modify a QP/EE which is not in the presumed state: */
83 CMD_STAT_BAD_QP_STATE = 0x10,
84 /* Bad segment parameters (Address/Size): */
85 CMD_STAT_BAD_SEG_PARAM = 0x20,
86 /* Memory Region has Memory Windows bound to: */
87 CMD_STAT_REG_BOUND = 0x21,
88 /* HCA local attached memory not present: */
89 CMD_STAT_LAM_NOT_PRE = 0x22,
90 /* Bad management packet (silently discarded): */
91 CMD_STAT_BAD_PKT = 0x30,
92 /* More outstanding CQEs in CQ than new CQ size: */
93 CMD_STAT_BAD_SIZE = 0x40,
94 /* Multi Function device support required: */
95 CMD_STAT_MULTI_FUNC_REQ = 0x50,
99 HCR_IN_PARAM_OFFSET = 0x00,
100 HCR_IN_MODIFIER_OFFSET = 0x08,
101 HCR_OUT_PARAM_OFFSET = 0x0c,
102 HCR_TOKEN_OFFSET = 0x14,
103 HCR_STATUS_OFFSET = 0x18,
105 HCR_OPMOD_SHIFT = 12,
112 GO_BIT_TIMEOUT_MSECS = 10000
115 enum mlx4_vlan_transition {
116 MLX4_VLAN_TRANSITION_VST_VST = 0,
117 MLX4_VLAN_TRANSITION_VST_VGT = 1,
118 MLX4_VLAN_TRANSITION_VGT_VST = 2,
119 MLX4_VLAN_TRANSITION_VGT_VGT = 3,
123 struct mlx4_cmd_context {
124 struct completion done;
132 static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
133 struct mlx4_vhcr_cmd *in_vhcr);
135 static int mlx4_status_to_errno(u8 status)
137 static const int trans_table[] = {
138 [CMD_STAT_INTERNAL_ERR] = -EIO,
139 [CMD_STAT_BAD_OP] = -EPERM,
140 [CMD_STAT_BAD_PARAM] = -EINVAL,
141 [CMD_STAT_BAD_SYS_STATE] = -ENXIO,
142 [CMD_STAT_BAD_RESOURCE] = -EBADF,
143 [CMD_STAT_RESOURCE_BUSY] = -EBUSY,
144 [CMD_STAT_EXCEED_LIM] = -ENOMEM,
145 [CMD_STAT_BAD_RES_STATE] = -EBADF,
146 [CMD_STAT_BAD_INDEX] = -EBADF,
147 [CMD_STAT_BAD_NVMEM] = -EFAULT,
148 [CMD_STAT_ICM_ERROR] = -ENFILE,
149 [CMD_STAT_BAD_QP_STATE] = -EINVAL,
150 [CMD_STAT_BAD_SEG_PARAM] = -EFAULT,
151 [CMD_STAT_REG_BOUND] = -EBUSY,
152 [CMD_STAT_LAM_NOT_PRE] = -EAGAIN,
153 [CMD_STAT_BAD_PKT] = -EINVAL,
154 [CMD_STAT_BAD_SIZE] = -ENOMEM,
155 [CMD_STAT_MULTI_FUNC_REQ] = -EACCES,
158 if (status >= ARRAY_SIZE(trans_table) ||
159 (status != CMD_STAT_OK && trans_table[status] == 0))
162 return trans_table[status];
165 static u8 mlx4_errno_to_status(int errno)
169 return CMD_STAT_BAD_OP;
171 return CMD_STAT_BAD_PARAM;
173 return CMD_STAT_BAD_SYS_STATE;
175 return CMD_STAT_RESOURCE_BUSY;
177 return CMD_STAT_EXCEED_LIM;
179 return CMD_STAT_ICM_ERROR;
181 return CMD_STAT_INTERNAL_ERR;
185 static int comm_pending(struct mlx4_dev *dev)
187 struct mlx4_priv *priv = mlx4_priv(dev);
188 u32 status = readl(&priv->mfunc.comm->slave_read);
190 return (swab32(status) >> 31) != priv->cmd.comm_toggle;
193 static void mlx4_comm_cmd_post(struct mlx4_dev *dev, u8 cmd, u16 param)
195 struct mlx4_priv *priv = mlx4_priv(dev);
198 priv->cmd.comm_toggle ^= 1;
199 val = param | (cmd << 16) | (priv->cmd.comm_toggle << 31);
200 __raw_writel((__force u32) cpu_to_be32(val),
201 &priv->mfunc.comm->slave_write);
205 static int mlx4_comm_cmd_poll(struct mlx4_dev *dev, u8 cmd, u16 param,
206 unsigned long timeout)
208 struct mlx4_priv *priv = mlx4_priv(dev);
211 int ret_from_pending = 0;
213 /* First, verify that the master reports correct status */
214 if (comm_pending(dev)) {
215 mlx4_warn(dev, "Communication channel is not idle - my toggle is %d (cmd:0x%x)\n",
216 priv->cmd.comm_toggle, cmd);
221 down(&priv->cmd.poll_sem);
222 mlx4_comm_cmd_post(dev, cmd, param);
224 end = msecs_to_jiffies(timeout) + jiffies;
225 while (comm_pending(dev) && time_before(jiffies, end))
227 ret_from_pending = comm_pending(dev);
228 if (ret_from_pending) {
229 /* check if the slave is trying to boot in the middle of
230 * FLR process. The only non-zero result in the RESET command
231 * is MLX4_DELAY_RESET_SLAVE*/
232 if ((MLX4_COMM_CMD_RESET == cmd)) {
233 err = MLX4_DELAY_RESET_SLAVE;
235 mlx4_warn(dev, "Communication channel timed out\n");
240 up(&priv->cmd.poll_sem);
244 static int mlx4_comm_cmd_wait(struct mlx4_dev *dev, u8 op,
245 u16 param, unsigned long timeout)
247 struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
248 struct mlx4_cmd_context *context;
252 down(&cmd->event_sem);
254 spin_lock(&cmd->context_lock);
255 BUG_ON(cmd->free_head < 0);
256 context = &cmd->context[cmd->free_head];
257 context->token += cmd->token_mask + 1;
258 cmd->free_head = context->next;
259 spin_unlock(&cmd->context_lock);
261 init_completion(&context->done);
263 mlx4_comm_cmd_post(dev, op, param);
265 if (!wait_for_completion_timeout(&context->done,
266 msecs_to_jiffies(timeout))) {
267 mlx4_warn(dev, "communication channel command 0x%x timed out\n",
273 err = context->result;
274 if (err && context->fw_status != CMD_STAT_MULTI_FUNC_REQ) {
275 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
276 op, context->fw_status);
281 /* wait for comm channel ready
282 * this is necessary for prevention the race
283 * when switching between event to polling mode
285 end = msecs_to_jiffies(timeout) + jiffies;
286 while (comm_pending(dev) && time_before(jiffies, end))
289 spin_lock(&cmd->context_lock);
290 context->next = cmd->free_head;
291 cmd->free_head = context - cmd->context;
292 spin_unlock(&cmd->context_lock);
298 int mlx4_comm_cmd(struct mlx4_dev *dev, u8 cmd, u16 param,
299 unsigned long timeout)
301 if (mlx4_priv(dev)->cmd.use_events)
302 return mlx4_comm_cmd_wait(dev, cmd, param, timeout);
303 return mlx4_comm_cmd_poll(dev, cmd, param, timeout);
306 static int cmd_pending(struct mlx4_dev *dev)
310 if (pci_channel_offline(dev->pdev))
313 status = readl(mlx4_priv(dev)->cmd.hcr + HCR_STATUS_OFFSET);
315 return (status & swab32(1 << HCR_GO_BIT)) ||
316 (mlx4_priv(dev)->cmd.toggle ==
317 !!(status & swab32(1 << HCR_T_BIT)));
320 static int mlx4_cmd_post(struct mlx4_dev *dev, u64 in_param, u64 out_param,
321 u32 in_modifier, u8 op_modifier, u16 op, u16 token,
324 struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
325 u32 __iomem *hcr = cmd->hcr;
329 mutex_lock(&cmd->hcr_mutex);
331 if (pci_channel_offline(dev->pdev)) {
333 * Device is going through error recovery
334 * and cannot accept commands.
342 end += msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS);
344 while (cmd_pending(dev)) {
345 if (pci_channel_offline(dev->pdev)) {
347 * Device is going through error recovery
348 * and cannot accept commands.
354 if (time_after_eq(jiffies, end)) {
355 mlx4_err(dev, "%s:cmd_pending failed\n", __func__);
362 * We use writel (instead of something like memcpy_toio)
363 * because writes of less than 32 bits to the HCR don't work
364 * (and some architectures such as ia64 implement memcpy_toio
365 * in terms of writeb).
367 __raw_writel((__force u32) cpu_to_be32(in_param >> 32), hcr + 0);
368 __raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful), hcr + 1);
369 __raw_writel((__force u32) cpu_to_be32(in_modifier), hcr + 2);
370 __raw_writel((__force u32) cpu_to_be32(out_param >> 32), hcr + 3);
371 __raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), hcr + 4);
372 __raw_writel((__force u32) cpu_to_be32(token << 16), hcr + 5);
374 /* __raw_writel may not order writes. */
377 __raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT) |
378 (cmd->toggle << HCR_T_BIT) |
379 (event ? (1 << HCR_E_BIT) : 0) |
380 (op_modifier << HCR_OPMOD_SHIFT) |
384 * Make sure that our HCR writes don't get mixed in with
385 * writes from another CPU starting a FW command.
389 cmd->toggle = cmd->toggle ^ 1;
394 mutex_unlock(&cmd->hcr_mutex);
398 static int mlx4_slave_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
399 int out_is_imm, u32 in_modifier, u8 op_modifier,
400 u16 op, unsigned long timeout)
402 struct mlx4_priv *priv = mlx4_priv(dev);
403 struct mlx4_vhcr_cmd *vhcr = priv->mfunc.vhcr;
406 mutex_lock(&priv->cmd.slave_cmd_mutex);
408 vhcr->in_param = cpu_to_be64(in_param);
409 vhcr->out_param = out_param ? cpu_to_be64(*out_param) : 0;
410 vhcr->in_modifier = cpu_to_be32(in_modifier);
411 vhcr->opcode = cpu_to_be16((((u16) op_modifier) << 12) | (op & 0xfff));
412 vhcr->token = cpu_to_be16(CMD_POLL_TOKEN);
414 vhcr->flags = !!(priv->cmd.use_events) << 6;
416 if (mlx4_is_master(dev)) {
417 ret = mlx4_master_process_vhcr(dev, dev->caps.function, vhcr);
422 be64_to_cpu(vhcr->out_param);
424 mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
426 vhcr->status = CMD_STAT_BAD_PARAM;
429 ret = mlx4_status_to_errno(vhcr->status);
432 ret = mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_POST, 0,
433 MLX4_COMM_TIME + timeout);
438 be64_to_cpu(vhcr->out_param);
440 mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
442 vhcr->status = CMD_STAT_BAD_PARAM;
445 ret = mlx4_status_to_errno(vhcr->status);
447 mlx4_err(dev, "failed execution of VHCR_POST command opcode 0x%x\n",
451 mutex_unlock(&priv->cmd.slave_cmd_mutex);
455 static int mlx4_cmd_poll(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
456 int out_is_imm, u32 in_modifier, u8 op_modifier,
457 u16 op, unsigned long timeout)
459 struct mlx4_priv *priv = mlx4_priv(dev);
460 void __iomem *hcr = priv->cmd.hcr;
465 down(&priv->cmd.poll_sem);
467 if (pci_channel_offline(dev->pdev)) {
469 * Device is going through error recovery
470 * and cannot accept commands.
476 if (out_is_imm && !out_param) {
477 mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
483 err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
484 in_modifier, op_modifier, op, CMD_POLL_TOKEN, 0);
488 end = msecs_to_jiffies(timeout) + jiffies;
489 while (cmd_pending(dev) && time_before(jiffies, end)) {
490 if (pci_channel_offline(dev->pdev)) {
492 * Device is going through error recovery
493 * and cannot accept commands.
502 if (cmd_pending(dev)) {
503 mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
511 (u64) be32_to_cpu((__force __be32)
512 __raw_readl(hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
513 (u64) be32_to_cpu((__force __be32)
514 __raw_readl(hcr + HCR_OUT_PARAM_OFFSET + 4));
515 stat = be32_to_cpu((__force __be32)
516 __raw_readl(hcr + HCR_STATUS_OFFSET)) >> 24;
517 err = mlx4_status_to_errno(stat);
519 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
523 up(&priv->cmd.poll_sem);
527 void mlx4_cmd_event(struct mlx4_dev *dev, u16 token, u8 status, u64 out_param)
529 struct mlx4_priv *priv = mlx4_priv(dev);
530 struct mlx4_cmd_context *context =
531 &priv->cmd.context[token & priv->cmd.token_mask];
533 /* previously timed out command completing at long last */
534 if (token != context->token)
537 context->fw_status = status;
538 context->result = mlx4_status_to_errno(status);
539 context->out_param = out_param;
541 complete(&context->done);
544 static int mlx4_cmd_wait(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
545 int out_is_imm, u32 in_modifier, u8 op_modifier,
546 u16 op, unsigned long timeout)
548 struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
549 struct mlx4_cmd_context *context;
552 down(&cmd->event_sem);
554 spin_lock(&cmd->context_lock);
555 BUG_ON(cmd->free_head < 0);
556 context = &cmd->context[cmd->free_head];
557 context->token += cmd->token_mask + 1;
558 cmd->free_head = context->next;
559 spin_unlock(&cmd->context_lock);
561 if (out_is_imm && !out_param) {
562 mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
568 init_completion(&context->done);
570 mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
571 in_modifier, op_modifier, op, context->token, 1);
573 if (!wait_for_completion_timeout(&context->done,
574 msecs_to_jiffies(timeout))) {
575 mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
581 err = context->result;
583 /* Since we do not want to have this error message always
584 * displayed at driver start when there are ConnectX2 HCAs
585 * on the host, we deprecate the error message for this
586 * specific command/input_mod/opcode_mod/fw-status to be debug.
588 if (op == MLX4_CMD_SET_PORT && in_modifier == 1 &&
589 op_modifier == 0 && context->fw_status == CMD_STAT_BAD_SIZE)
590 mlx4_dbg(dev, "command 0x%x failed: fw status = 0x%x\n",
591 op, context->fw_status);
593 mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
594 op, context->fw_status);
599 *out_param = context->out_param;
602 spin_lock(&cmd->context_lock);
603 context->next = cmd->free_head;
604 cmd->free_head = context - cmd->context;
605 spin_unlock(&cmd->context_lock);
611 int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
612 int out_is_imm, u32 in_modifier, u8 op_modifier,
613 u16 op, unsigned long timeout, int native)
615 if (pci_channel_offline(dev->pdev))
618 if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
619 if (mlx4_priv(dev)->cmd.use_events)
620 return mlx4_cmd_wait(dev, in_param, out_param,
621 out_is_imm, in_modifier,
622 op_modifier, op, timeout);
624 return mlx4_cmd_poll(dev, in_param, out_param,
625 out_is_imm, in_modifier,
626 op_modifier, op, timeout);
628 return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
629 in_modifier, op_modifier, op, timeout);
631 EXPORT_SYMBOL_GPL(__mlx4_cmd);
634 static int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
636 return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
637 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
640 static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
641 int slave, u64 slave_addr,
642 int size, int is_read)
647 if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
648 (slave & ~0x7f) | (size & 0xff)) {
649 mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx master_addr:0x%llx slave_id:%d size:%d\n",
650 slave_addr, master_addr, slave, size);
655 in_param = (u64) slave | slave_addr;
656 out_param = (u64) dev->caps.function | master_addr;
658 in_param = (u64) dev->caps.function | master_addr;
659 out_param = (u64) slave | slave_addr;
662 return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
664 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
667 static int query_pkey_block(struct mlx4_dev *dev, u8 port, u16 index, u16 *pkey,
668 struct mlx4_cmd_mailbox *inbox,
669 struct mlx4_cmd_mailbox *outbox)
671 struct ib_smp *in_mad = (struct ib_smp *)(inbox->buf);
672 struct ib_smp *out_mad = (struct ib_smp *)(outbox->buf);
679 in_mad->attr_mod = cpu_to_be32(index / 32);
681 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
682 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
687 for (i = 0; i < 32; ++i)
688 pkey[i] = be16_to_cpu(((__be16 *) out_mad->data)[i]);
693 static int get_full_pkey_table(struct mlx4_dev *dev, u8 port, u16 *table,
694 struct mlx4_cmd_mailbox *inbox,
695 struct mlx4_cmd_mailbox *outbox)
700 for (i = 0; i < dev->caps.pkey_table_len[port]; i += 32) {
701 err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
708 #define PORT_CAPABILITY_LOCATION_IN_SMP 20
709 #define PORT_STATE_OFFSET 32
711 static enum ib_port_state vf_port_state(struct mlx4_dev *dev, int port, int vf)
713 if (mlx4_get_slave_port_state(dev, vf, port) == SLAVE_PORT_UP)
714 return IB_PORT_ACTIVE;
719 static int mlx4_MAD_IFC_wrapper(struct mlx4_dev *dev, int slave,
720 struct mlx4_vhcr *vhcr,
721 struct mlx4_cmd_mailbox *inbox,
722 struct mlx4_cmd_mailbox *outbox,
723 struct mlx4_cmd_info *cmd)
725 struct ib_smp *smp = inbox->buf;
733 struct mlx4_priv *priv = mlx4_priv(dev);
734 struct ib_smp *outsmp = outbox->buf;
735 __be16 *outtab = (__be16 *)(outsmp->data);
736 __be32 slave_cap_mask;
737 __be64 slave_node_guid;
739 port = vhcr->in_modifier;
741 /* network-view bit is for driver use only, and should not be passed to FW */
742 opcode_modifier = vhcr->op_modifier & ~0x8; /* clear netw view bit */
743 network_view = !!(vhcr->op_modifier & 0x8);
745 if (smp->base_version == 1 &&
746 smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
747 smp->class_version == 1) {
748 /* host view is paravirtualized */
749 if (!network_view && smp->method == IB_MGMT_METHOD_GET) {
750 if (smp->attr_id == IB_SMP_ATTR_PKEY_TABLE) {
751 index = be32_to_cpu(smp->attr_mod);
752 if (port < 1 || port > dev->caps.num_ports)
754 table = kcalloc(dev->caps.pkey_table_len[port], sizeof *table, GFP_KERNEL);
757 /* need to get the full pkey table because the paravirtualized
758 * pkeys may be scattered among several pkey blocks.
760 err = get_full_pkey_table(dev, port, table, inbox, outbox);
762 for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
763 pidx = priv->virt2phys_pkey[slave][port - 1][vidx];
764 outtab[vidx % 32] = cpu_to_be16(table[pidx]);
770 if (smp->attr_id == IB_SMP_ATTR_PORT_INFO) {
771 /*get the slave specific caps:*/
773 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
774 vhcr->in_modifier, opcode_modifier,
775 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
776 /* modify the response for slaves */
777 if (!err && slave != mlx4_master_func_num(dev)) {
778 u8 *state = outsmp->data + PORT_STATE_OFFSET;
780 *state = (*state & 0xf0) | vf_port_state(dev, port, slave);
781 slave_cap_mask = priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
782 memcpy(outsmp->data + PORT_CAPABILITY_LOCATION_IN_SMP, &slave_cap_mask, 4);
786 if (smp->attr_id == IB_SMP_ATTR_GUID_INFO) {
787 /* compute slave's gid block */
788 smp->attr_mod = cpu_to_be32(slave / 8);
790 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
791 vhcr->in_modifier, opcode_modifier,
792 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
794 /* if needed, move slave gid to index 0 */
797 outsmp->data + (slave % 8) * 8, 8);
798 /* delete all other gids */
799 memset(outsmp->data + 8, 0, 56);
803 if (smp->attr_id == IB_SMP_ATTR_NODE_INFO) {
804 err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
805 vhcr->in_modifier, opcode_modifier,
806 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
808 slave_node_guid = mlx4_get_slave_node_guid(dev, slave);
809 memcpy(outsmp->data + 12, &slave_node_guid, 8);
816 /* Non-privileged VFs are only allowed "host" view LID-routed 'Get' MADs.
817 * These are the MADs used by ib verbs (such as ib_query_gids).
819 if (slave != mlx4_master_func_num(dev) &&
820 !mlx4_vf_smi_enabled(dev, slave, port)) {
821 if (!(smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
822 smp->method == IB_MGMT_METHOD_GET) || network_view) {
823 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",
824 slave, smp->method, smp->mgmt_class,
825 network_view ? "Network" : "Host",
826 be16_to_cpu(smp->attr_id));
831 return mlx4_cmd_box(dev, inbox->dma, outbox->dma,
832 vhcr->in_modifier, opcode_modifier,
833 vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
836 static int mlx4_CMD_EPERM_wrapper(struct mlx4_dev *dev, int slave,
837 struct mlx4_vhcr *vhcr,
838 struct mlx4_cmd_mailbox *inbox,
839 struct mlx4_cmd_mailbox *outbox,
840 struct mlx4_cmd_info *cmd)
845 int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
846 struct mlx4_vhcr *vhcr,
847 struct mlx4_cmd_mailbox *inbox,
848 struct mlx4_cmd_mailbox *outbox,
849 struct mlx4_cmd_info *cmd)
855 in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
856 out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
857 if (cmd->encode_slave_id) {
858 in_param &= 0xffffffffffffff00ll;
862 err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
863 vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
864 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
867 vhcr->out_param = out_param;
872 static struct mlx4_cmd_info cmd_info[] = {
874 .opcode = MLX4_CMD_QUERY_FW,
878 .encode_slave_id = false,
880 .wrapper = mlx4_QUERY_FW_wrapper
883 .opcode = MLX4_CMD_QUERY_HCA,
887 .encode_slave_id = false,
892 .opcode = MLX4_CMD_QUERY_DEV_CAP,
896 .encode_slave_id = false,
898 .wrapper = mlx4_QUERY_DEV_CAP_wrapper
901 .opcode = MLX4_CMD_QUERY_FUNC_CAP,
905 .encode_slave_id = false,
907 .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
910 .opcode = MLX4_CMD_QUERY_ADAPTER,
914 .encode_slave_id = false,
919 .opcode = MLX4_CMD_INIT_PORT,
923 .encode_slave_id = false,
925 .wrapper = mlx4_INIT_PORT_wrapper
928 .opcode = MLX4_CMD_CLOSE_PORT,
932 .encode_slave_id = false,
934 .wrapper = mlx4_CLOSE_PORT_wrapper
937 .opcode = MLX4_CMD_QUERY_PORT,
941 .encode_slave_id = false,
943 .wrapper = mlx4_QUERY_PORT_wrapper
946 .opcode = MLX4_CMD_SET_PORT,
950 .encode_slave_id = false,
952 .wrapper = mlx4_SET_PORT_wrapper
955 .opcode = MLX4_CMD_MAP_EQ,
959 .encode_slave_id = false,
961 .wrapper = mlx4_MAP_EQ_wrapper
964 .opcode = MLX4_CMD_SW2HW_EQ,
968 .encode_slave_id = true,
970 .wrapper = mlx4_SW2HW_EQ_wrapper
973 .opcode = MLX4_CMD_HW_HEALTH_CHECK,
977 .encode_slave_id = false,
982 .opcode = MLX4_CMD_NOP,
986 .encode_slave_id = false,
991 .opcode = MLX4_CMD_CONFIG_DEV,
995 .encode_slave_id = false,
997 .wrapper = mlx4_CMD_EPERM_wrapper
1000 .opcode = MLX4_CMD_ALLOC_RES,
1002 .has_outbox = false,
1004 .encode_slave_id = false,
1006 .wrapper = mlx4_ALLOC_RES_wrapper
1009 .opcode = MLX4_CMD_FREE_RES,
1011 .has_outbox = false,
1012 .out_is_imm = false,
1013 .encode_slave_id = false,
1015 .wrapper = mlx4_FREE_RES_wrapper
1018 .opcode = MLX4_CMD_SW2HW_MPT,
1020 .has_outbox = false,
1021 .out_is_imm = false,
1022 .encode_slave_id = true,
1024 .wrapper = mlx4_SW2HW_MPT_wrapper
1027 .opcode = MLX4_CMD_QUERY_MPT,
1030 .out_is_imm = false,
1031 .encode_slave_id = false,
1033 .wrapper = mlx4_QUERY_MPT_wrapper
1036 .opcode = MLX4_CMD_HW2SW_MPT,
1038 .has_outbox = false,
1039 .out_is_imm = false,
1040 .encode_slave_id = false,
1042 .wrapper = mlx4_HW2SW_MPT_wrapper
1045 .opcode = MLX4_CMD_READ_MTT,
1048 .out_is_imm = false,
1049 .encode_slave_id = false,
1054 .opcode = MLX4_CMD_WRITE_MTT,
1056 .has_outbox = false,
1057 .out_is_imm = false,
1058 .encode_slave_id = false,
1060 .wrapper = mlx4_WRITE_MTT_wrapper
1063 .opcode = MLX4_CMD_SYNC_TPT,
1065 .has_outbox = false,
1066 .out_is_imm = false,
1067 .encode_slave_id = false,
1072 .opcode = MLX4_CMD_HW2SW_EQ,
1075 .out_is_imm = false,
1076 .encode_slave_id = true,
1078 .wrapper = mlx4_HW2SW_EQ_wrapper
1081 .opcode = MLX4_CMD_QUERY_EQ,
1084 .out_is_imm = false,
1085 .encode_slave_id = true,
1087 .wrapper = mlx4_QUERY_EQ_wrapper
1090 .opcode = MLX4_CMD_SW2HW_CQ,
1092 .has_outbox = false,
1093 .out_is_imm = false,
1094 .encode_slave_id = true,
1096 .wrapper = mlx4_SW2HW_CQ_wrapper
1099 .opcode = MLX4_CMD_HW2SW_CQ,
1101 .has_outbox = false,
1102 .out_is_imm = false,
1103 .encode_slave_id = false,
1105 .wrapper = mlx4_HW2SW_CQ_wrapper
1108 .opcode = MLX4_CMD_QUERY_CQ,
1111 .out_is_imm = false,
1112 .encode_slave_id = false,
1114 .wrapper = mlx4_QUERY_CQ_wrapper
1117 .opcode = MLX4_CMD_MODIFY_CQ,
1119 .has_outbox = false,
1121 .encode_slave_id = false,
1123 .wrapper = mlx4_MODIFY_CQ_wrapper
1126 .opcode = MLX4_CMD_SW2HW_SRQ,
1128 .has_outbox = false,
1129 .out_is_imm = false,
1130 .encode_slave_id = true,
1132 .wrapper = mlx4_SW2HW_SRQ_wrapper
1135 .opcode = MLX4_CMD_HW2SW_SRQ,
1137 .has_outbox = false,
1138 .out_is_imm = false,
1139 .encode_slave_id = false,
1141 .wrapper = mlx4_HW2SW_SRQ_wrapper
1144 .opcode = MLX4_CMD_QUERY_SRQ,
1147 .out_is_imm = false,
1148 .encode_slave_id = false,
1150 .wrapper = mlx4_QUERY_SRQ_wrapper
1153 .opcode = MLX4_CMD_ARM_SRQ,
1155 .has_outbox = false,
1156 .out_is_imm = false,
1157 .encode_slave_id = false,
1159 .wrapper = mlx4_ARM_SRQ_wrapper
1162 .opcode = MLX4_CMD_RST2INIT_QP,
1164 .has_outbox = false,
1165 .out_is_imm = false,
1166 .encode_slave_id = true,
1168 .wrapper = mlx4_RST2INIT_QP_wrapper
1171 .opcode = MLX4_CMD_INIT2INIT_QP,
1173 .has_outbox = false,
1174 .out_is_imm = false,
1175 .encode_slave_id = false,
1177 .wrapper = mlx4_INIT2INIT_QP_wrapper
1180 .opcode = MLX4_CMD_INIT2RTR_QP,
1182 .has_outbox = false,
1183 .out_is_imm = false,
1184 .encode_slave_id = false,
1186 .wrapper = mlx4_INIT2RTR_QP_wrapper
1189 .opcode = MLX4_CMD_RTR2RTS_QP,
1191 .has_outbox = false,
1192 .out_is_imm = false,
1193 .encode_slave_id = false,
1195 .wrapper = mlx4_RTR2RTS_QP_wrapper
1198 .opcode = MLX4_CMD_RTS2RTS_QP,
1200 .has_outbox = false,
1201 .out_is_imm = false,
1202 .encode_slave_id = false,
1204 .wrapper = mlx4_RTS2RTS_QP_wrapper
1207 .opcode = MLX4_CMD_SQERR2RTS_QP,
1209 .has_outbox = false,
1210 .out_is_imm = false,
1211 .encode_slave_id = false,
1213 .wrapper = mlx4_SQERR2RTS_QP_wrapper
1216 .opcode = MLX4_CMD_2ERR_QP,
1218 .has_outbox = false,
1219 .out_is_imm = false,
1220 .encode_slave_id = false,
1222 .wrapper = mlx4_GEN_QP_wrapper
1225 .opcode = MLX4_CMD_RTS2SQD_QP,
1227 .has_outbox = false,
1228 .out_is_imm = false,
1229 .encode_slave_id = false,
1231 .wrapper = mlx4_GEN_QP_wrapper
1234 .opcode = MLX4_CMD_SQD2SQD_QP,
1236 .has_outbox = false,
1237 .out_is_imm = false,
1238 .encode_slave_id = false,
1240 .wrapper = mlx4_SQD2SQD_QP_wrapper
1243 .opcode = MLX4_CMD_SQD2RTS_QP,
1245 .has_outbox = false,
1246 .out_is_imm = false,
1247 .encode_slave_id = false,
1249 .wrapper = mlx4_SQD2RTS_QP_wrapper
1252 .opcode = MLX4_CMD_2RST_QP,
1254 .has_outbox = false,
1255 .out_is_imm = false,
1256 .encode_slave_id = false,
1258 .wrapper = mlx4_2RST_QP_wrapper
1261 .opcode = MLX4_CMD_QUERY_QP,
1264 .out_is_imm = false,
1265 .encode_slave_id = false,
1267 .wrapper = mlx4_GEN_QP_wrapper
1270 .opcode = MLX4_CMD_SUSPEND_QP,
1272 .has_outbox = false,
1273 .out_is_imm = false,
1274 .encode_slave_id = false,
1276 .wrapper = mlx4_GEN_QP_wrapper
1279 .opcode = MLX4_CMD_UNSUSPEND_QP,
1281 .has_outbox = false,
1282 .out_is_imm = false,
1283 .encode_slave_id = false,
1285 .wrapper = mlx4_GEN_QP_wrapper
1288 .opcode = MLX4_CMD_UPDATE_QP,
1290 .has_outbox = false,
1291 .out_is_imm = false,
1292 .encode_slave_id = false,
1294 .wrapper = mlx4_UPDATE_QP_wrapper
1297 .opcode = MLX4_CMD_GET_OP_REQ,
1299 .has_outbox = false,
1300 .out_is_imm = false,
1301 .encode_slave_id = false,
1303 .wrapper = mlx4_CMD_EPERM_wrapper,
1306 .opcode = MLX4_CMD_CONF_SPECIAL_QP,
1308 .has_outbox = false,
1309 .out_is_imm = false,
1310 .encode_slave_id = false,
1311 .verify = NULL, /* XXX verify: only demux can do this */
1315 .opcode = MLX4_CMD_MAD_IFC,
1318 .out_is_imm = false,
1319 .encode_slave_id = false,
1321 .wrapper = mlx4_MAD_IFC_wrapper
1324 .opcode = MLX4_CMD_MAD_DEMUX,
1326 .has_outbox = false,
1327 .out_is_imm = false,
1328 .encode_slave_id = false,
1330 .wrapper = mlx4_CMD_EPERM_wrapper
1333 .opcode = MLX4_CMD_QUERY_IF_STAT,
1336 .out_is_imm = false,
1337 .encode_slave_id = false,
1339 .wrapper = mlx4_QUERY_IF_STAT_wrapper
1341 /* Native multicast commands are not available for guests */
1343 .opcode = MLX4_CMD_QP_ATTACH,
1345 .has_outbox = false,
1346 .out_is_imm = false,
1347 .encode_slave_id = false,
1349 .wrapper = mlx4_QP_ATTACH_wrapper
1352 .opcode = MLX4_CMD_PROMISC,
1354 .has_outbox = false,
1355 .out_is_imm = false,
1356 .encode_slave_id = false,
1358 .wrapper = mlx4_PROMISC_wrapper
1360 /* Ethernet specific commands */
1362 .opcode = MLX4_CMD_SET_VLAN_FLTR,
1364 .has_outbox = false,
1365 .out_is_imm = false,
1366 .encode_slave_id = false,
1368 .wrapper = mlx4_SET_VLAN_FLTR_wrapper
1371 .opcode = MLX4_CMD_SET_MCAST_FLTR,
1373 .has_outbox = false,
1374 .out_is_imm = false,
1375 .encode_slave_id = false,
1377 .wrapper = mlx4_SET_MCAST_FLTR_wrapper
1380 .opcode = MLX4_CMD_DUMP_ETH_STATS,
1383 .out_is_imm = false,
1384 .encode_slave_id = false,
1386 .wrapper = mlx4_DUMP_ETH_STATS_wrapper
1389 .opcode = MLX4_CMD_INFORM_FLR_DONE,
1391 .has_outbox = false,
1392 .out_is_imm = false,
1393 .encode_slave_id = false,
1397 /* flow steering commands */
1399 .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
1401 .has_outbox = false,
1403 .encode_slave_id = false,
1405 .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
1408 .opcode = MLX4_QP_FLOW_STEERING_DETACH,
1410 .has_outbox = false,
1411 .out_is_imm = false,
1412 .encode_slave_id = false,
1414 .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
1417 .opcode = MLX4_FLOW_STEERING_IB_UC_QP_RANGE,
1419 .has_outbox = false,
1420 .out_is_imm = false,
1421 .encode_slave_id = false,
1423 .wrapper = mlx4_CMD_EPERM_wrapper
1427 static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
1428 struct mlx4_vhcr_cmd *in_vhcr)
1430 struct mlx4_priv *priv = mlx4_priv(dev);
1431 struct mlx4_cmd_info *cmd = NULL;
1432 struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
1433 struct mlx4_vhcr *vhcr;
1434 struct mlx4_cmd_mailbox *inbox = NULL;
1435 struct mlx4_cmd_mailbox *outbox = NULL;
1442 /* Create sw representation of Virtual HCR */
1443 vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
1447 /* DMA in the vHCR */
1449 ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1450 priv->mfunc.master.slave_state[slave].vhcr_dma,
1451 ALIGN(sizeof(struct mlx4_vhcr_cmd),
1452 MLX4_ACCESS_MEM_ALIGN), 1);
1454 mlx4_err(dev, "%s: Failed reading vhcr ret: 0x%x\n",
1461 /* Fill SW VHCR fields */
1462 vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
1463 vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
1464 vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
1465 vhcr->token = be16_to_cpu(vhcr_cmd->token);
1466 vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
1467 vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
1468 vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
1470 /* Lookup command */
1471 for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
1472 if (vhcr->op == cmd_info[i].opcode) {
1478 mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
1480 vhcr_cmd->status = CMD_STAT_BAD_PARAM;
1485 if (cmd->has_inbox) {
1486 vhcr->in_param &= INBOX_MASK;
1487 inbox = mlx4_alloc_cmd_mailbox(dev);
1488 if (IS_ERR(inbox)) {
1489 vhcr_cmd->status = CMD_STAT_BAD_SIZE;
1494 if (mlx4_ACCESS_MEM(dev, inbox->dma, slave,
1496 MLX4_MAILBOX_SIZE, 1)) {
1497 mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
1498 __func__, cmd->opcode);
1499 vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
1504 /* Apply permission and bound checks if applicable */
1505 if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
1506 mlx4_warn(dev, "Command:0x%x from slave: %d failed protection checks for resource_id:%d\n",
1507 vhcr->op, slave, vhcr->in_modifier);
1508 vhcr_cmd->status = CMD_STAT_BAD_OP;
1512 /* Allocate outbox */
1513 if (cmd->has_outbox) {
1514 outbox = mlx4_alloc_cmd_mailbox(dev);
1515 if (IS_ERR(outbox)) {
1516 vhcr_cmd->status = CMD_STAT_BAD_SIZE;
1522 /* Execute the command! */
1524 err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
1526 if (cmd->out_is_imm)
1527 vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1529 in_param = cmd->has_inbox ? (u64) inbox->dma :
1531 out_param = cmd->has_outbox ? (u64) outbox->dma :
1533 err = __mlx4_cmd(dev, in_param, &out_param,
1534 cmd->out_is_imm, vhcr->in_modifier,
1535 vhcr->op_modifier, vhcr->op,
1536 MLX4_CMD_TIME_CLASS_A,
1539 if (cmd->out_is_imm) {
1540 vhcr->out_param = out_param;
1541 vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1546 mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with error:%d, status %d\n",
1547 vhcr->op, slave, vhcr->errno, err);
1548 vhcr_cmd->status = mlx4_errno_to_status(err);
1553 /* Write outbox if command completed successfully */
1554 if (cmd->has_outbox && !vhcr_cmd->status) {
1555 ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
1557 MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
1559 /* If we failed to write back the outbox after the
1560 *command was successfully executed, we must fail this
1561 * slave, as it is now in undefined state */
1562 mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
1568 /* DMA back vhcr result */
1570 ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1571 priv->mfunc.master.slave_state[slave].vhcr_dma,
1572 ALIGN(sizeof(struct mlx4_vhcr),
1573 MLX4_ACCESS_MEM_ALIGN),
1576 mlx4_err(dev, "%s:Failed writing vhcr result\n",
1578 else if (vhcr->e_bit &&
1579 mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
1580 mlx4_warn(dev, "Failed to generate command completion eqe for slave %d\n",
1586 mlx4_free_cmd_mailbox(dev, inbox);
1587 mlx4_free_cmd_mailbox(dev, outbox);
1591 static int mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv *priv,
1592 int slave, int port)
1594 struct mlx4_vport_oper_state *vp_oper;
1595 struct mlx4_vport_state *vp_admin;
1596 struct mlx4_vf_immed_vlan_work *work;
1597 struct mlx4_dev *dev = &(priv->dev);
1599 int admin_vlan_ix = NO_INDX;
1601 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1602 vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
1604 if (vp_oper->state.default_vlan == vp_admin->default_vlan &&
1605 vp_oper->state.default_qos == vp_admin->default_qos &&
1606 vp_oper->state.link_state == vp_admin->link_state)
1609 if (!(priv->mfunc.master.slave_state[slave].active &&
1610 dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)) {
1611 /* even if the UPDATE_QP command isn't supported, we still want
1612 * to set this VF link according to the admin directive
1614 vp_oper->state.link_state = vp_admin->link_state;
1618 mlx4_dbg(dev, "updating immediately admin params slave %d port %d\n",
1620 mlx4_dbg(dev, "vlan %d QoS %d link down %d\n",
1621 vp_admin->default_vlan, vp_admin->default_qos,
1622 vp_admin->link_state);
1624 work = kzalloc(sizeof(*work), GFP_KERNEL);
1628 if (vp_oper->state.default_vlan != vp_admin->default_vlan) {
1629 if (MLX4_VGT != vp_admin->default_vlan) {
1630 err = __mlx4_register_vlan(&priv->dev, port,
1631 vp_admin->default_vlan,
1635 mlx4_warn(&priv->dev,
1636 "No vlan resources slave %d, port %d\n",
1641 admin_vlan_ix = NO_INDX;
1643 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_VLAN;
1644 mlx4_dbg(&priv->dev,
1645 "alloc vlan %d idx %d slave %d port %d\n",
1646 (int)(vp_admin->default_vlan),
1647 admin_vlan_ix, slave, port);
1650 /* save original vlan ix and vlan id */
1651 work->orig_vlan_id = vp_oper->state.default_vlan;
1652 work->orig_vlan_ix = vp_oper->vlan_idx;
1654 /* handle new qos */
1655 if (vp_oper->state.default_qos != vp_admin->default_qos)
1656 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_QOS;
1658 if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN)
1659 vp_oper->vlan_idx = admin_vlan_ix;
1661 vp_oper->state.default_vlan = vp_admin->default_vlan;
1662 vp_oper->state.default_qos = vp_admin->default_qos;
1663 vp_oper->state.link_state = vp_admin->link_state;
1665 if (vp_admin->link_state == IFLA_VF_LINK_STATE_DISABLE)
1666 work->flags |= MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE;
1668 /* iterate over QPs owned by this slave, using UPDATE_QP */
1670 work->slave = slave;
1671 work->qos = vp_oper->state.default_qos;
1672 work->vlan_id = vp_oper->state.default_vlan;
1673 work->vlan_ix = vp_oper->vlan_idx;
1675 INIT_WORK(&work->work, mlx4_vf_immed_vlan_work_handler);
1676 queue_work(priv->mfunc.master.comm_wq, &work->work);
1682 static int mlx4_master_activate_admin_state(struct mlx4_priv *priv, int slave)
1685 struct mlx4_vport_state *vp_admin;
1686 struct mlx4_vport_oper_state *vp_oper;
1687 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
1689 int min_port = find_first_bit(actv_ports.ports,
1690 priv->dev.caps.num_ports) + 1;
1691 int max_port = min_port - 1 +
1692 bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
1694 for (port = min_port; port <= max_port; port++) {
1695 if (!test_bit(port - 1, actv_ports.ports))
1697 priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
1698 priv->mfunc.master.vf_admin[slave].enable_smi[port];
1699 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1700 vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
1701 vp_oper->state = *vp_admin;
1702 if (MLX4_VGT != vp_admin->default_vlan) {
1703 err = __mlx4_register_vlan(&priv->dev, port,
1704 vp_admin->default_vlan, &(vp_oper->vlan_idx));
1706 vp_oper->vlan_idx = NO_INDX;
1707 mlx4_warn(&priv->dev,
1708 "No vlan resorces slave %d, port %d\n",
1712 mlx4_dbg(&priv->dev, "alloc vlan %d idx %d slave %d port %d\n",
1713 (int)(vp_oper->state.default_vlan),
1714 vp_oper->vlan_idx, slave, port);
1716 if (vp_admin->spoofchk) {
1717 vp_oper->mac_idx = __mlx4_register_mac(&priv->dev,
1720 if (0 > vp_oper->mac_idx) {
1721 err = vp_oper->mac_idx;
1722 vp_oper->mac_idx = NO_INDX;
1723 mlx4_warn(&priv->dev,
1724 "No mac resorces slave %d, port %d\n",
1728 mlx4_dbg(&priv->dev, "alloc mac %llx idx %d slave %d port %d\n",
1729 vp_oper->state.mac, vp_oper->mac_idx, slave, port);
1735 static void mlx4_master_deactivate_admin_state(struct mlx4_priv *priv, int slave)
1738 struct mlx4_vport_oper_state *vp_oper;
1739 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
1741 int min_port = find_first_bit(actv_ports.ports,
1742 priv->dev.caps.num_ports) + 1;
1743 int max_port = min_port - 1 +
1744 bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
1747 for (port = min_port; port <= max_port; port++) {
1748 if (!test_bit(port - 1, actv_ports.ports))
1750 priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
1751 MLX4_VF_SMI_DISABLED;
1752 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1753 if (NO_INDX != vp_oper->vlan_idx) {
1754 __mlx4_unregister_vlan(&priv->dev,
1755 port, vp_oper->state.default_vlan);
1756 vp_oper->vlan_idx = NO_INDX;
1758 if (NO_INDX != vp_oper->mac_idx) {
1759 __mlx4_unregister_mac(&priv->dev, port, vp_oper->state.mac);
1760 vp_oper->mac_idx = NO_INDX;
1766 static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
1767 u16 param, u8 toggle)
1769 struct mlx4_priv *priv = mlx4_priv(dev);
1770 struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
1772 u8 is_going_down = 0;
1774 unsigned long flags;
1776 slave_state[slave].comm_toggle ^= 1;
1777 reply = (u32) slave_state[slave].comm_toggle << 31;
1778 if (toggle != slave_state[slave].comm_toggle) {
1779 mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER STATE COMPROMISED ***\n",
1783 if (cmd == MLX4_COMM_CMD_RESET) {
1784 mlx4_warn(dev, "Received reset from slave:%d\n", slave);
1785 slave_state[slave].active = false;
1786 slave_state[slave].old_vlan_api = false;
1787 mlx4_master_deactivate_admin_state(priv, slave);
1788 for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
1789 slave_state[slave].event_eq[i].eqn = -1;
1790 slave_state[slave].event_eq[i].token = 0;
1792 /*check if we are in the middle of FLR process,
1793 if so return "retry" status to the slave*/
1794 if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
1795 goto inform_slave_state;
1797 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
1799 /* write the version in the event field */
1800 reply |= mlx4_comm_get_version();
1804 /*command from slave in the middle of FLR*/
1805 if (cmd != MLX4_COMM_CMD_RESET &&
1806 MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
1807 mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) in the middle of FLR\n",
1813 case MLX4_COMM_CMD_VHCR0:
1814 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
1816 slave_state[slave].vhcr_dma = ((u64) param) << 48;
1817 priv->mfunc.master.slave_state[slave].cookie = 0;
1818 mutex_init(&priv->mfunc.master.gen_eqe_mutex[slave]);
1820 case MLX4_COMM_CMD_VHCR1:
1821 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
1823 slave_state[slave].vhcr_dma |= ((u64) param) << 32;
1825 case MLX4_COMM_CMD_VHCR2:
1826 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
1828 slave_state[slave].vhcr_dma |= ((u64) param) << 16;
1830 case MLX4_COMM_CMD_VHCR_EN:
1831 if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
1833 slave_state[slave].vhcr_dma |= param;
1834 if (mlx4_master_activate_admin_state(priv, slave))
1836 slave_state[slave].active = true;
1837 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
1839 case MLX4_COMM_CMD_VHCR_POST:
1840 if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
1841 (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST))
1844 mutex_lock(&priv->cmd.slave_cmd_mutex);
1845 if (mlx4_master_process_vhcr(dev, slave, NULL)) {
1846 mlx4_err(dev, "Failed processing vhcr for slave:%d, resetting slave\n",
1848 mutex_unlock(&priv->cmd.slave_cmd_mutex);
1851 mutex_unlock(&priv->cmd.slave_cmd_mutex);
1854 mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
1857 spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
1858 if (!slave_state[slave].is_slave_going_down)
1859 slave_state[slave].last_cmd = cmd;
1862 spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
1863 if (is_going_down) {
1864 mlx4_warn(dev, "Slave is going down aborting command(%d) executing from slave:%d\n",
1868 __raw_writel((__force u32) cpu_to_be32(reply),
1869 &priv->mfunc.comm[slave].slave_read);
1875 /* cleanup any slave resources */
1876 mlx4_delete_all_resources_for_slave(dev, slave);
1877 spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
1878 if (!slave_state[slave].is_slave_going_down)
1879 slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
1880 spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
1881 /*with slave in the middle of flr, no need to clean resources again.*/
1883 memset(&slave_state[slave].event_eq, 0,
1884 sizeof(struct mlx4_slave_event_eq_info));
1885 __raw_writel((__force u32) cpu_to_be32(reply),
1886 &priv->mfunc.comm[slave].slave_read);
1890 /* master command processing */
1891 void mlx4_master_comm_channel(struct work_struct *work)
1893 struct mlx4_mfunc_master_ctx *master =
1895 struct mlx4_mfunc_master_ctx,
1897 struct mlx4_mfunc *mfunc =
1898 container_of(master, struct mlx4_mfunc, master);
1899 struct mlx4_priv *priv =
1900 container_of(mfunc, struct mlx4_priv, mfunc);
1901 struct mlx4_dev *dev = &priv->dev;
1911 bit_vec = master->comm_arm_bit_vector;
1912 for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
1913 vec = be32_to_cpu(bit_vec[i]);
1914 for (j = 0; j < 32; j++) {
1915 if (!(vec & (1 << j)))
1918 slave = (i * 32) + j;
1919 comm_cmd = swab32(readl(
1920 &mfunc->comm[slave].slave_write));
1921 slt = swab32(readl(&mfunc->comm[slave].slave_read))
1923 toggle = comm_cmd >> 31;
1924 if (toggle != slt) {
1925 if (master->slave_state[slave].comm_toggle
1927 pr_info("slave %d out of sync. read toggle %d, state toggle %d. Resynching.\n",
1929 master->slave_state[slave].comm_toggle);
1930 master->slave_state[slave].comm_toggle =
1933 mlx4_master_do_cmd(dev, slave,
1934 comm_cmd >> 16 & 0xff,
1935 comm_cmd & 0xffff, toggle);
1941 if (reported && reported != served)
1942 mlx4_warn(dev, "Got command event with bitmask from %d slaves but %d were served\n",
1945 if (mlx4_ARM_COMM_CHANNEL(dev))
1946 mlx4_warn(dev, "Failed to arm comm channel events\n");
1949 static int sync_toggles(struct mlx4_dev *dev)
1951 struct mlx4_priv *priv = mlx4_priv(dev);
1956 wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write)) >> 31;
1957 end = jiffies + msecs_to_jiffies(5000);
1959 while (time_before(jiffies, end)) {
1960 rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read)) >> 31;
1961 if (rd_toggle == wr_toggle) {
1962 priv->cmd.comm_toggle = rd_toggle;
1970 * we could reach here if for example the previous VM using this
1971 * function misbehaved and left the channel with unsynced state. We
1972 * should fix this here and give this VM a chance to use a properly
1975 mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
1976 __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
1977 __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
1978 priv->cmd.comm_toggle = 0;
1983 int mlx4_multi_func_init(struct mlx4_dev *dev)
1985 struct mlx4_priv *priv = mlx4_priv(dev);
1986 struct mlx4_slave_state *s_state;
1987 int i, j, err, port;
1989 if (mlx4_is_master(dev))
1991 ioremap(pci_resource_start(dev->pdev, priv->fw.comm_bar) +
1992 priv->fw.comm_base, MLX4_COMM_PAGESIZE);
1995 ioremap(pci_resource_start(dev->pdev, 2) +
1996 MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
1997 if (!priv->mfunc.comm) {
1998 mlx4_err(dev, "Couldn't map communication vector\n");
2002 if (mlx4_is_master(dev)) {
2003 priv->mfunc.master.slave_state =
2004 kzalloc(dev->num_slaves *
2005 sizeof(struct mlx4_slave_state), GFP_KERNEL);
2006 if (!priv->mfunc.master.slave_state)
2009 priv->mfunc.master.vf_admin =
2010 kzalloc(dev->num_slaves *
2011 sizeof(struct mlx4_vf_admin_state), GFP_KERNEL);
2012 if (!priv->mfunc.master.vf_admin)
2013 goto err_comm_admin;
2015 priv->mfunc.master.vf_oper =
2016 kzalloc(dev->num_slaves *
2017 sizeof(struct mlx4_vf_oper_state), GFP_KERNEL);
2018 if (!priv->mfunc.master.vf_oper)
2021 for (i = 0; i < dev->num_slaves; ++i) {
2022 s_state = &priv->mfunc.master.slave_state[i];
2023 s_state->last_cmd = MLX4_COMM_CMD_RESET;
2024 for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
2025 s_state->event_eq[j].eqn = -1;
2026 __raw_writel((__force u32) 0,
2027 &priv->mfunc.comm[i].slave_write);
2028 __raw_writel((__force u32) 0,
2029 &priv->mfunc.comm[i].slave_read);
2031 for (port = 1; port <= MLX4_MAX_PORTS; port++) {
2032 s_state->vlan_filter[port] =
2033 kzalloc(sizeof(struct mlx4_vlan_fltr),
2035 if (!s_state->vlan_filter[port]) {
2037 kfree(s_state->vlan_filter[port]);
2040 INIT_LIST_HEAD(&s_state->mcast_filters[port]);
2041 priv->mfunc.master.vf_admin[i].vport[port].default_vlan = MLX4_VGT;
2042 priv->mfunc.master.vf_oper[i].vport[port].state.default_vlan = MLX4_VGT;
2043 priv->mfunc.master.vf_oper[i].vport[port].vlan_idx = NO_INDX;
2044 priv->mfunc.master.vf_oper[i].vport[port].mac_idx = NO_INDX;
2046 spin_lock_init(&s_state->lock);
2049 memset(&priv->mfunc.master.cmd_eqe, 0, dev->caps.eqe_size);
2050 priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
2051 INIT_WORK(&priv->mfunc.master.comm_work,
2052 mlx4_master_comm_channel);
2053 INIT_WORK(&priv->mfunc.master.slave_event_work,
2054 mlx4_gen_slave_eqe);
2055 INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
2056 mlx4_master_handle_slave_flr);
2057 spin_lock_init(&priv->mfunc.master.slave_state_lock);
2058 spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
2059 priv->mfunc.master.comm_wq =
2060 create_singlethread_workqueue("mlx4_comm");
2061 if (!priv->mfunc.master.comm_wq)
2064 if (mlx4_init_resource_tracker(dev))
2067 err = mlx4_ARM_COMM_CHANNEL(dev);
2069 mlx4_err(dev, " Failed to arm comm channel eq: %x\n",
2075 err = sync_toggles(dev);
2077 mlx4_err(dev, "Couldn't sync toggles\n");
2084 mlx4_free_resource_tracker(dev, RES_TR_FREE_ALL);
2086 flush_workqueue(priv->mfunc.master.comm_wq);
2087 destroy_workqueue(priv->mfunc.master.comm_wq);
2090 for (port = 1; port <= MLX4_MAX_PORTS; port++)
2091 kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
2093 kfree(priv->mfunc.master.vf_oper);
2095 kfree(priv->mfunc.master.vf_admin);
2097 kfree(priv->mfunc.master.slave_state);
2099 iounmap(priv->mfunc.comm);
2101 dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
2103 priv->mfunc.vhcr_dma);
2104 priv->mfunc.vhcr = NULL;
2108 int mlx4_cmd_init(struct mlx4_dev *dev)
2110 struct mlx4_priv *priv = mlx4_priv(dev);
2112 mutex_init(&priv->cmd.hcr_mutex);
2113 mutex_init(&priv->cmd.slave_cmd_mutex);
2114 sema_init(&priv->cmd.poll_sem, 1);
2115 priv->cmd.use_events = 0;
2116 priv->cmd.toggle = 1;
2118 priv->cmd.hcr = NULL;
2119 priv->mfunc.vhcr = NULL;
2121 if (!mlx4_is_slave(dev)) {
2122 priv->cmd.hcr = ioremap(pci_resource_start(dev->pdev, 0) +
2123 MLX4_HCR_BASE, MLX4_HCR_SIZE);
2124 if (!priv->cmd.hcr) {
2125 mlx4_err(dev, "Couldn't map command register\n");
2130 if (mlx4_is_mfunc(dev)) {
2131 priv->mfunc.vhcr = dma_alloc_coherent(&(dev->pdev->dev), PAGE_SIZE,
2132 &priv->mfunc.vhcr_dma,
2134 if (!priv->mfunc.vhcr)
2138 priv->cmd.pool = pci_pool_create("mlx4_cmd", dev->pdev,
2140 MLX4_MAILBOX_SIZE, 0);
2141 if (!priv->cmd.pool)
2147 if (mlx4_is_mfunc(dev))
2148 dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
2149 priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
2150 priv->mfunc.vhcr = NULL;
2153 if (!mlx4_is_slave(dev))
2154 iounmap(priv->cmd.hcr);
2158 void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
2160 struct mlx4_priv *priv = mlx4_priv(dev);
2163 if (mlx4_is_master(dev)) {
2164 flush_workqueue(priv->mfunc.master.comm_wq);
2165 destroy_workqueue(priv->mfunc.master.comm_wq);
2166 for (i = 0; i < dev->num_slaves; i++) {
2167 for (port = 1; port <= MLX4_MAX_PORTS; port++)
2168 kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
2170 kfree(priv->mfunc.master.slave_state);
2171 kfree(priv->mfunc.master.vf_admin);
2172 kfree(priv->mfunc.master.vf_oper);
2175 iounmap(priv->mfunc.comm);
2178 void mlx4_cmd_cleanup(struct mlx4_dev *dev)
2180 struct mlx4_priv *priv = mlx4_priv(dev);
2182 pci_pool_destroy(priv->cmd.pool);
2184 if (!mlx4_is_slave(dev))
2185 iounmap(priv->cmd.hcr);
2186 if (mlx4_is_mfunc(dev))
2187 dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
2188 priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
2189 priv->mfunc.vhcr = NULL;
2193 * Switch to using events to issue FW commands (can only be called
2194 * after event queue for command events has been initialized).
2196 int mlx4_cmd_use_events(struct mlx4_dev *dev)
2198 struct mlx4_priv *priv = mlx4_priv(dev);
2202 priv->cmd.context = kmalloc(priv->cmd.max_cmds *
2203 sizeof (struct mlx4_cmd_context),
2205 if (!priv->cmd.context)
2208 for (i = 0; i < priv->cmd.max_cmds; ++i) {
2209 priv->cmd.context[i].token = i;
2210 priv->cmd.context[i].next = i + 1;
2213 priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
2214 priv->cmd.free_head = 0;
2216 sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
2217 spin_lock_init(&priv->cmd.context_lock);
2219 for (priv->cmd.token_mask = 1;
2220 priv->cmd.token_mask < priv->cmd.max_cmds;
2221 priv->cmd.token_mask <<= 1)
2223 --priv->cmd.token_mask;
2225 down(&priv->cmd.poll_sem);
2226 priv->cmd.use_events = 1;
2232 * Switch back to polling (used when shutting down the device)
2234 void mlx4_cmd_use_polling(struct mlx4_dev *dev)
2236 struct mlx4_priv *priv = mlx4_priv(dev);
2239 priv->cmd.use_events = 0;
2241 for (i = 0; i < priv->cmd.max_cmds; ++i)
2242 down(&priv->cmd.event_sem);
2244 kfree(priv->cmd.context);
2246 up(&priv->cmd.poll_sem);
2249 struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
2251 struct mlx4_cmd_mailbox *mailbox;
2253 mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
2255 return ERR_PTR(-ENOMEM);
2257 mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
2259 if (!mailbox->buf) {
2261 return ERR_PTR(-ENOMEM);
2264 memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
2268 EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
2270 void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
2271 struct mlx4_cmd_mailbox *mailbox)
2276 pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
2279 EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
2281 u32 mlx4_comm_get_version(void)
2283 return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
2286 static int mlx4_get_slave_indx(struct mlx4_dev *dev, int vf)
2288 if ((vf < 0) || (vf >= dev->num_vfs)) {
2289 mlx4_err(dev, "Bad vf number:%d (number of activated vf: %d)\n", vf, dev->num_vfs);
2296 int mlx4_get_vf_indx(struct mlx4_dev *dev, int slave)
2298 if (slave < 1 || slave > dev->num_vfs) {
2300 "Bad slave number:%d (number of activated slaves: %lu)\n",
2301 slave, dev->num_slaves);
2307 struct mlx4_active_ports mlx4_get_active_ports(struct mlx4_dev *dev, int slave)
2309 struct mlx4_active_ports actv_ports;
2312 bitmap_zero(actv_ports.ports, MLX4_MAX_PORTS);
2315 bitmap_fill(actv_ports.ports, dev->caps.num_ports);
2319 vf = mlx4_get_vf_indx(dev, slave);
2323 bitmap_set(actv_ports.ports, dev->dev_vfs[vf].min_port - 1,
2324 min((int)dev->dev_vfs[mlx4_get_vf_indx(dev, slave)].n_ports,
2325 dev->caps.num_ports));
2329 EXPORT_SYMBOL_GPL(mlx4_get_active_ports);
2331 int mlx4_slave_convert_port(struct mlx4_dev *dev, int slave, int port)
2334 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2335 unsigned m = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2337 if (port <= 0 || port > m)
2340 n = find_first_bit(actv_ports.ports, dev->caps.num_ports);
2346 EXPORT_SYMBOL_GPL(mlx4_slave_convert_port);
2348 int mlx4_phys_to_slave_port(struct mlx4_dev *dev, int slave, int port)
2350 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2351 if (test_bit(port - 1, actv_ports.ports))
2353 find_first_bit(actv_ports.ports, dev->caps.num_ports);
2357 EXPORT_SYMBOL_GPL(mlx4_phys_to_slave_port);
2359 struct mlx4_slaves_pport mlx4_phys_to_slaves_pport(struct mlx4_dev *dev,
2363 struct mlx4_slaves_pport slaves_pport;
2365 bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
2367 if (port <= 0 || port > dev->caps.num_ports)
2368 return slaves_pport;
2370 for (i = 0; i < dev->num_vfs + 1; i++) {
2371 struct mlx4_active_ports actv_ports =
2372 mlx4_get_active_ports(dev, i);
2373 if (test_bit(port - 1, actv_ports.ports))
2374 set_bit(i, slaves_pport.slaves);
2377 return slaves_pport;
2379 EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport);
2381 struct mlx4_slaves_pport mlx4_phys_to_slaves_pport_actv(
2382 struct mlx4_dev *dev,
2383 const struct mlx4_active_ports *crit_ports)
2386 struct mlx4_slaves_pport slaves_pport;
2388 bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
2390 for (i = 0; i < dev->num_vfs + 1; i++) {
2391 struct mlx4_active_ports actv_ports =
2392 mlx4_get_active_ports(dev, i);
2393 if (bitmap_equal(crit_ports->ports, actv_ports.ports,
2394 dev->caps.num_ports))
2395 set_bit(i, slaves_pport.slaves);
2398 return slaves_pport;
2400 EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport_actv);
2402 static int mlx4_slaves_closest_port(struct mlx4_dev *dev, int slave, int port)
2404 struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2405 int min_port = find_first_bit(actv_ports.ports, dev->caps.num_ports)
2407 int max_port = min_port +
2408 bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2410 if (port < min_port)
2412 else if (port >= max_port)
2413 port = max_port - 1;
2418 int mlx4_set_vf_mac(struct mlx4_dev *dev, int port, int vf, u64 mac)
2420 struct mlx4_priv *priv = mlx4_priv(dev);
2421 struct mlx4_vport_state *s_info;
2424 if (!mlx4_is_master(dev))
2425 return -EPROTONOSUPPORT;
2427 slave = mlx4_get_slave_indx(dev, vf);
2431 port = mlx4_slaves_closest_port(dev, slave, port);
2432 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
2434 mlx4_info(dev, "default mac on vf %d port %d to %llX will take afect only after vf restart\n",
2435 vf, port, s_info->mac);
2438 EXPORT_SYMBOL_GPL(mlx4_set_vf_mac);
2441 int mlx4_set_vf_vlan(struct mlx4_dev *dev, int port, int vf, u16 vlan, u8 qos)
2443 struct mlx4_priv *priv = mlx4_priv(dev);
2444 struct mlx4_vport_state *vf_admin;
2447 if ((!mlx4_is_master(dev)) ||
2448 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
2449 return -EPROTONOSUPPORT;
2451 if ((vlan > 4095) || (qos > 7))
2454 slave = mlx4_get_slave_indx(dev, vf);
2458 port = mlx4_slaves_closest_port(dev, slave, port);
2459 vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
2461 if ((0 == vlan) && (0 == qos))
2462 vf_admin->default_vlan = MLX4_VGT;
2464 vf_admin->default_vlan = vlan;
2465 vf_admin->default_qos = qos;
2467 if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
2469 "updating vf %d port %d config will take effect on next VF restart\n",
2473 EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
2475 /* mlx4_get_slave_default_vlan -
2476 * return true if VST ( default vlan)
2477 * if VST, will return vlan & qos (if not NULL)
2479 bool mlx4_get_slave_default_vlan(struct mlx4_dev *dev, int port, int slave,
2482 struct mlx4_vport_oper_state *vp_oper;
2483 struct mlx4_priv *priv;
2485 priv = mlx4_priv(dev);
2486 port = mlx4_slaves_closest_port(dev, slave, port);
2487 vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
2489 if (MLX4_VGT != vp_oper->state.default_vlan) {
2491 *vlan = vp_oper->state.default_vlan;
2493 *qos = vp_oper->state.default_qos;
2498 EXPORT_SYMBOL_GPL(mlx4_get_slave_default_vlan);
2500 int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
2502 struct mlx4_priv *priv = mlx4_priv(dev);
2503 struct mlx4_vport_state *s_info;
2506 if ((!mlx4_is_master(dev)) ||
2507 !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
2508 return -EPROTONOSUPPORT;
2510 slave = mlx4_get_slave_indx(dev, vf);
2514 port = mlx4_slaves_closest_port(dev, slave, port);
2515 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
2516 s_info->spoofchk = setting;
2520 EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
2522 int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
2524 struct mlx4_priv *priv = mlx4_priv(dev);
2525 struct mlx4_vport_state *s_info;
2528 if (!mlx4_is_master(dev))
2529 return -EPROTONOSUPPORT;
2531 slave = mlx4_get_slave_indx(dev, vf);
2535 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
2538 /* need to convert it to a func */
2539 ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
2540 ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
2541 ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
2542 ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
2543 ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
2544 ivf->mac[5] = ((s_info->mac) & 0xff);
2546 ivf->vlan = s_info->default_vlan;
2547 ivf->qos = s_info->default_qos;
2548 ivf->max_tx_rate = s_info->tx_rate;
2549 ivf->min_tx_rate = 0;
2550 ivf->spoofchk = s_info->spoofchk;
2551 ivf->linkstate = s_info->link_state;
2555 EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
2557 int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
2559 struct mlx4_priv *priv = mlx4_priv(dev);
2560 struct mlx4_vport_state *s_info;
2564 slave = mlx4_get_slave_indx(dev, vf);
2568 port = mlx4_slaves_closest_port(dev, slave, port);
2569 switch (link_state) {
2570 case IFLA_VF_LINK_STATE_AUTO:
2571 /* get current link state */
2572 if (!priv->sense.do_sense_port[port])
2573 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
2575 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
2578 case IFLA_VF_LINK_STATE_ENABLE:
2579 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
2582 case IFLA_VF_LINK_STATE_DISABLE:
2583 link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
2587 mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
2588 link_state, slave, port);
2591 s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
2592 s_info->link_state = link_state;
2595 mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
2597 if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
2599 "updating vf %d port %d no link state HW enforcment\n",
2603 EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);
2605 int mlx4_vf_smi_enabled(struct mlx4_dev *dev, int slave, int port)
2607 struct mlx4_priv *priv = mlx4_priv(dev);
2609 if (slave < 1 || slave >= dev->num_slaves ||
2610 port < 1 || port > MLX4_MAX_PORTS)
2613 return priv->mfunc.master.vf_oper[slave].smi_enabled[port] ==
2614 MLX4_VF_SMI_ENABLED;
2616 EXPORT_SYMBOL_GPL(mlx4_vf_smi_enabled);
2618 int mlx4_vf_get_enable_smi_admin(struct mlx4_dev *dev, int slave, int port)
2620 struct mlx4_priv *priv = mlx4_priv(dev);
2622 if (slave == mlx4_master_func_num(dev))
2625 if (slave < 1 || slave >= dev->num_slaves ||
2626 port < 1 || port > MLX4_MAX_PORTS)
2629 return priv->mfunc.master.vf_admin[slave].enable_smi[port] ==
2630 MLX4_VF_SMI_ENABLED;
2632 EXPORT_SYMBOL_GPL(mlx4_vf_get_enable_smi_admin);
2634 int mlx4_vf_set_enable_smi_admin(struct mlx4_dev *dev, int slave, int port,
2637 struct mlx4_priv *priv = mlx4_priv(dev);
2639 if (slave == mlx4_master_func_num(dev))
2642 if (slave < 1 || slave >= dev->num_slaves ||
2643 port < 1 || port > MLX4_MAX_PORTS ||
2644 enabled < 0 || enabled > 1)
2647 priv->mfunc.master.vf_admin[slave].enable_smi[port] = enabled;
2650 EXPORT_SYMBOL_GPL(mlx4_vf_set_enable_smi_admin);