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>
50 #define CMD_POLL_TOKEN 0xffff
51 #define INBOX_MASK 0xffffffffffffff00ULL
53 #define CMD_CHAN_VER 1
54 #define CMD_CHAN_IF_REV 1
126 static int mlx4_status_to_errno(
u8 status)
128 static const int trans_table[] = {
150 (status !=
CMD_STAT_OK && trans_table[status] == 0))
153 return trans_table[
status];
156 static u8 mlx4_errno_to_status(
int errno)
181 return (
swab32(status) >> 31) != priv->
cmd.comm_toggle;
189 priv->
cmd.comm_toggle ^= 1;
190 val = param | (cmd << 16) | (priv->
cmd.comm_toggle << 31);
192 &priv->
mfunc.comm->slave_write);
196 static int mlx4_comm_cmd_poll(
struct mlx4_dev *dev,
u8 cmd,
u16 param,
197 unsigned long timeout)
202 int ret_from_pending = 0;
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);
214 mlx4_comm_cmd_post(dev, cmd, param);
217 while (comm_pending(dev) &&
time_before(jiffies, end))
219 ret_from_pending = comm_pending(dev);
220 if (ret_from_pending) {
225 mlx4_warn(dev,
"Got slave FLRed from Communication"
226 " channel (ret:0x%x)\n", ret_from_pending);
229 mlx4_warn(dev,
"Communication channel timed out\n");
234 up(&priv->
cmd.poll_sem);
238 static int mlx4_comm_cmd_wait(
struct mlx4_dev *dev,
u8 op,
239 u16 param,
unsigned long timeout)
255 init_completion(&context->
done);
257 mlx4_comm_cmd_post(dev, op, param);
267 mlx4_err(dev,
"command 0x%x failed: fw status = 0x%x\n",
278 while (comm_pending(dev) &&
time_before(jiffies, end))
291 unsigned long timeout)
294 return mlx4_comm_cmd_wait(dev, cmd, param, timeout);
295 return mlx4_comm_cmd_poll(dev, cmd, param, timeout);
298 static int cmd_pending(
struct mlx4_dev *dev)
302 if (pci_channel_offline(dev->
pdev))
323 if (pci_channel_offline(dev->
pdev)) {
336 while (cmd_pending(dev)) {
337 if (pci_channel_offline(dev->
pdev)) {
347 mlx4_err(dev,
"%s:cmd_pending failed\n", __func__);
390 static 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)
406 vhcr->
flags = !!(priv->
cmd.use_events) << 6;
408 if (mlx4_is_master(dev)) {
409 ret = mlx4_master_process_vhcr(dev, dev->
caps.function, vhcr);
416 mlx4_err(dev,
"response expected while"
417 "output mailbox is NULL for "
418 "command 0x%x\n", op);
422 ret = mlx4_status_to_errno(vhcr->
status);
433 mlx4_err(dev,
"response expected while"
434 "output mailbox is NULL for "
435 "command 0x%x\n", op);
439 ret = mlx4_status_to_errno(vhcr->
status);
441 mlx4_err(dev,
"failed execution of VHCR_POST command"
442 "opcode 0x%x\n", op);
449 static int mlx4_cmd_poll(
struct mlx4_dev *dev,
u64 in_param,
u64 *out_param,
450 int out_is_imm, u32 in_modifier,
u8 op_modifier,
451 u16 op,
unsigned long timeout)
461 if (pci_channel_offline(dev->
pdev)) {
470 err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
476 while (cmd_pending(dev) &&
time_before(jiffies, end)) {
477 if (pci_channel_offline(dev->
pdev)) {
489 if (cmd_pending(dev)) {
502 err = mlx4_status_to_errno(stat);
504 mlx4_err(dev,
"command 0x%x failed: fw status = 0x%x\n",
508 up(&priv->
cmd.poll_sem);
516 &priv->
cmd.context[token & priv->
cmd.token_mask];
519 if (token != context->
token)
523 context->
result = mlx4_status_to_errno(status);
529 static int mlx4_cmd_wait(
struct mlx4_dev *dev,
u64 in_param,
u64 *out_param,
530 int out_is_imm, u32 in_modifier,
u8 op_modifier,
531 u16 op,
unsigned long timeout)
546 init_completion(&context->
done);
548 mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
549 in_modifier, op_modifier, op, context->
token, 1);
559 mlx4_err(dev,
"command 0x%x failed: fw status = 0x%x\n",
578 int out_is_imm, u32 in_modifier,
u8 op_modifier,
579 u16 op,
unsigned long timeout,
int native)
581 if (pci_channel_offline(dev->
pdev))
584 if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
586 return mlx4_cmd_wait(dev, in_param, out_param,
587 out_is_imm, in_modifier,
588 op_modifier, op, timeout);
590 return mlx4_cmd_poll(dev, in_param, out_param,
591 out_is_imm, in_modifier,
592 op_modifier, op, timeout);
594 return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
595 in_modifier, op_modifier, op, timeout);
600 static int mlx4_ARM_COMM_CHANNEL(
struct mlx4_dev *dev)
606 static int mlx4_ACCESS_MEM(
struct mlx4_dev *dev,
u64 master_addr,
608 int size,
int is_read)
613 if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
614 (slave & ~0x7f) | (size & 0xff)) {
615 mlx4_err(dev,
"Bad access mem params - slave_addr:0x%llx "
616 "master_addr:0x%llx slave_id:%d size:%d\n",
617 slave_addr, master_addr, slave, size);
622 in_param = (
u64) slave | slave_addr;
623 out_param = (
u64) dev->
caps.function | master_addr;
625 in_param = (
u64) dev->
caps.function | master_addr;
629 return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
648 err = mlx4_cmd_box(dev, inbox->
dma, outbox->
dma, port, 3,
654 for (i = 0; i < 32; ++
i)
667 for (i = 0; i < dev->
caps.pkey_table_len[
port]; i += 32) {
668 err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
675 #define PORT_CAPABILITY_LOCATION_IN_SMP 20
676 #define PORT_STATE_OFFSET 32
686 static int mlx4_MAD_IFC_wrapper(
struct mlx4_dev *dev,
int slave,
701 __be32 slave_cap_mask;
713 table = kcalloc(dev->
caps.pkey_table_len[port],
sizeof *table,
GFP_KERNEL);
719 err = get_full_pkey_table(dev, port, table, inbox, outbox);
721 for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
732 err = mlx4_cmd_box(dev, inbox->
dma, outbox->
dma,
736 if (!err && slave != mlx4_master_func_num(dev)) {
739 *state = (*state & 0xf0) | vf_port_state(dev, port, slave);
740 slave_cap_mask = priv->
mfunc.master.slave_state[slave].ib_cap_mask[
port];
749 err = mlx4_cmd_box(dev, inbox->
dma, outbox->
dma,
756 outsmp->
data + (slave % 8) * 8, 8);
763 err = mlx4_cmd_box(dev, inbox->
dma, outbox->
dma,
768 memcpy(outsmp->
data + 12, &slave_node_guid, 8);
774 if (slave != mlx4_master_func_num(dev) &&
778 mlx4_err(dev,
"slave %d is trying to execute a Subnet MGMT MAD, "
779 "class 0x%x, method 0x%x for attr 0x%x. Rejecting\n",
785 return mlx4_cmd_box(dev, inbox->
dma, outbox->
dma,
803 in_param &= 0xffffffffffffff00ll;
823 .encode_slave_id =
false,
832 .encode_slave_id =
false,
841 .encode_slave_id =
false,
850 .encode_slave_id =
false,
859 .encode_slave_id =
false,
868 .encode_slave_id =
false,
877 .encode_slave_id =
false,
886 .encode_slave_id =
false,
895 .encode_slave_id =
false,
904 .encode_slave_id =
false,
913 .encode_slave_id =
true,
922 .encode_slave_id =
false,
931 .encode_slave_id =
false,
940 .encode_slave_id =
false,
949 .encode_slave_id =
false,
958 .encode_slave_id =
true,
967 .encode_slave_id =
false,
976 .encode_slave_id =
false,
985 .encode_slave_id =
false,
994 .encode_slave_id =
false,
1001 .has_outbox =
false,
1002 .out_is_imm =
false,
1003 .encode_slave_id =
false,
1011 .out_is_imm =
false,
1012 .encode_slave_id =
true,
1020 .out_is_imm =
false,
1021 .encode_slave_id =
true,
1028 .has_outbox =
false,
1029 .out_is_imm =
false,
1030 .encode_slave_id =
true,
1037 .has_outbox =
false,
1038 .out_is_imm =
false,
1039 .encode_slave_id =
false,
1047 .out_is_imm =
false,
1048 .encode_slave_id =
false,
1055 .has_outbox =
false,
1057 .encode_slave_id =
false,
1064 .has_outbox =
false,
1065 .out_is_imm =
false,
1066 .encode_slave_id =
true,
1073 .has_outbox =
false,
1074 .out_is_imm =
false,
1075 .encode_slave_id =
false,
1083 .out_is_imm =
false,
1084 .encode_slave_id =
false,
1091 .has_outbox =
false,
1092 .out_is_imm =
false,
1093 .encode_slave_id =
false,
1100 .has_outbox =
false,
1101 .out_is_imm =
false,
1102 .encode_slave_id =
true,
1109 .has_outbox =
false,
1110 .out_is_imm =
false,
1111 .encode_slave_id =
false,
1118 .has_outbox =
false,
1119 .out_is_imm =
false,
1120 .encode_slave_id =
false,
1127 .has_outbox =
false,
1128 .out_is_imm =
false,
1129 .encode_slave_id =
false,
1136 .has_outbox =
false,
1137 .out_is_imm =
false,
1138 .encode_slave_id =
false,
1145 .has_outbox =
false,
1146 .out_is_imm =
false,
1147 .encode_slave_id =
false,
1154 .has_outbox =
false,
1155 .out_is_imm =
false,
1156 .encode_slave_id =
false,
1163 .has_outbox =
false,
1164 .out_is_imm =
false,
1165 .encode_slave_id =
false,
1172 .has_outbox =
false,
1173 .out_is_imm =
false,
1174 .encode_slave_id =
false,
1181 .has_outbox =
false,
1182 .out_is_imm =
false,
1183 .encode_slave_id =
false,
1190 .has_outbox =
false,
1191 .out_is_imm =
false,
1192 .encode_slave_id =
false,
1200 .out_is_imm =
false,
1201 .encode_slave_id =
false,
1208 .has_outbox =
false,
1209 .out_is_imm =
false,
1210 .encode_slave_id =
false,
1217 .has_outbox =
false,
1218 .out_is_imm =
false,
1219 .encode_slave_id =
false,
1226 .has_outbox =
false,
1227 .out_is_imm =
false,
1228 .encode_slave_id =
false,
1236 .out_is_imm =
false,
1237 .encode_slave_id =
false,
1239 .wrapper = mlx4_MAD_IFC_wrapper
1245 .out_is_imm =
false,
1246 .encode_slave_id =
false,
1254 .has_outbox =
false,
1255 .out_is_imm =
false,
1256 .encode_slave_id =
false,
1263 .has_outbox =
false,
1264 .out_is_imm =
false,
1265 .encode_slave_id =
false,
1273 .has_outbox =
false,
1274 .out_is_imm =
false,
1275 .encode_slave_id =
false,
1282 .has_outbox =
false,
1283 .out_is_imm =
false,
1284 .encode_slave_id =
false,
1292 .out_is_imm =
false,
1293 .encode_slave_id =
false,
1300 .has_outbox =
false,
1301 .out_is_imm =
false,
1302 .encode_slave_id =
false,
1310 .has_outbox =
false,
1312 .encode_slave_id =
false,
1319 .has_outbox =
false,
1320 .out_is_imm =
false,
1321 .encode_slave_id =
false,
1327 static int mlx4_master_process_vhcr(
struct mlx4_dev *dev,
int slave,
1349 ret = mlx4_ACCESS_MEM(dev, priv->
mfunc.vhcr_dma, slave,
1350 priv->
mfunc.master.slave_state[slave].vhcr_dma,
1354 mlx4_err(dev,
"%s:Failed reading vhcr"
1355 "ret: 0x%x\n", __func__, ret);
1372 if (vhcr->
op == cmd_info[i].
opcode) {
1378 mlx4_err(dev,
"Unknown command:0x%x accepted from slave:%d\n",
1388 if (IS_ERR(inbox)) {
1394 if (mlx4_ACCESS_MEM(dev, inbox->
dma, slave,
1397 mlx4_err(dev,
"%s: Failed reading inbox (cmd:0x%x)\n",
1405 if (cmd->
verify && cmd->
verify(dev, slave, vhcr, inbox)) {
1406 mlx4_warn(dev,
"Command:0x%x from slave: %d failed protection "
1407 "checks for resource_id:%d\n", vhcr->
op, slave,
1416 if (IS_ERR(outbox)) {
1425 err = cmd->
wrapper(dev, slave, vhcr, inbox, outbox,
1447 mlx4_warn(dev,
"vhcr command:0x%x slave:%d failed with"
1448 " error:%d, status %d\n",
1449 vhcr->
op, slave, vhcr->
errno, err);
1450 vhcr_cmd->
status = mlx4_errno_to_status(err);
1457 ret = mlx4_ACCESS_MEM(dev, outbox->
dma, slave,
1464 mlx4_err(dev,
"%s:Failed writing outbox\n", __func__);
1472 ret = mlx4_ACCESS_MEM(dev, priv->
mfunc.vhcr_dma, slave,
1473 priv->
mfunc.master.slave_state[slave].vhcr_dma,
1478 mlx4_err(dev,
"%s:Failed writing vhcr result\n",
1480 else if (vhcr->
e_bit &&
1482 mlx4_warn(dev,
"Failed to generate command completion "
1483 "eqe for slave %d\n", slave);
1493 static void mlx4_master_do_cmd(
struct mlx4_dev *dev,
int slave,
u8 cmd,
1499 u8 is_going_down = 0;
1505 mlx4_warn(dev,
"Incorrect toggle %d from slave %d. *** MASTER"
1506 "STATE COMPROMISIED ***\n", toggle, slave);
1510 mlx4_warn(dev,
"Received reset from slave:%d\n", slave);
1511 slave_state[slave].
active =
false;
1513 slave_state[slave].
event_eq[
i].eqn = -1;
1514 slave_state[slave].
event_eq[
i].token = 0;
1519 goto inform_slave_state;
1531 mlx4_warn(dev,
"slave:%d is Trying to run cmd(0x%x) "
1532 "in the middle of FLR\n", slave, cmd);
1540 slave_state[slave].
vhcr_dma = ((
u64) param) << 48;
1541 priv->
mfunc.master.slave_state[slave].cookie = 0;
1547 slave_state[slave].
vhcr_dma |= ((
u64) param) << 32;
1552 slave_state[slave].
vhcr_dma |= ((
u64) param) << 16;
1558 slave_state[slave].
active =
true;
1567 if (mlx4_master_process_vhcr(dev, slave,
NULL)) {
1568 mlx4_err(dev,
"Failed processing vhcr for slave:%d,"
1569 " resetting slave.\n", slave);
1576 mlx4_warn(dev,
"Bad comm cmd:%d from slave:%d\n", cmd, slave);
1579 spin_lock(&priv->
mfunc.master.slave_state_lock);
1584 spin_unlock(&priv->
mfunc.master.slave_state_lock);
1585 if (is_going_down) {
1586 mlx4_warn(dev,
"Slave is going down aborting command(%d)"
1587 " executing from slave:%d\n",
1592 &priv->
mfunc.comm[slave].slave_read);
1600 spin_lock(&priv->
mfunc.master.slave_state_lock);
1601 if (!slave_state[slave].is_slave_going_down)
1603 spin_unlock(&priv->
mfunc.master.slave_state_lock);
1609 &priv->
mfunc.comm[slave].slave_read);
1637 for (j = 0; j < 32; j++) {
1638 if (!(vec & (1 << j)))
1641 slave = (i * 32) + j;
1643 &mfunc->
comm[slave].slave_write));
1646 toggle = comm_cmd >> 31;
1647 if (toggle != slt) {
1651 " read toggle %d, state toggle %d. "
1652 "Resynching.\n", slave, slt,
1657 mlx4_master_do_cmd(dev, slave,
1658 comm_cmd >> 16 & 0xff,
1659 comm_cmd & 0xffff, toggle);
1665 if (reported && reported != served)
1666 mlx4_warn(dev,
"Got command event with bitmask from %d slaves"
1667 " but %d were served\n",
1670 if (mlx4_ARM_COMM_CHANNEL(dev))
1671 mlx4_warn(dev,
"Failed to arm comm channel events\n");
1674 static int sync_toggles(
struct mlx4_dev *dev)
1686 if (rd_toggle == wr_toggle) {
1687 priv->
cmd.comm_toggle = rd_toggle;
1700 mlx4_warn(dev,
"recovering from previously mis-behaved VM\n");
1703 priv->
cmd.comm_toggle = 0;
1714 if (mlx4_is_master(dev))
1722 if (!priv->
mfunc.comm) {
1723 mlx4_err(dev,
"Couldn't map communication vector.\n");
1727 if (mlx4_is_master(dev)) {
1728 priv->
mfunc.master.slave_state =
1731 if (!priv->
mfunc.master.slave_state)
1735 s_state = &priv->
mfunc.master.slave_state[
i];
1740 &priv->
mfunc.comm[i].slave_write);
1742 &priv->
mfunc.comm[i].slave_read);
1768 priv->
mfunc.master.comm_wq =
1770 if (!priv->
mfunc.master.comm_wq)
1776 err = mlx4_ARM_COMM_CHANNEL(dev);
1778 mlx4_err(dev,
" Failed to arm comm channel eq: %x\n",
1784 err = sync_toggles(dev);
1786 mlx4_err(dev,
"Couldn't sync toggles\n");
1800 kfree(priv->
mfunc.master.slave_state[i].vlan_filter[port]);
1808 priv->
mfunc.vhcr_dma);
1819 sema_init(&priv->
cmd.poll_sem, 1);
1820 priv->
cmd.use_events = 0;
1821 priv->
cmd.toggle = 1;
1826 if (!mlx4_is_slave(dev)) {
1829 if (!priv->
cmd.hcr) {
1830 mlx4_err(dev,
"Couldn't map command register.\n");
1835 if (mlx4_is_mfunc(dev)) {
1837 &priv->
mfunc.vhcr_dma,
1839 if (!priv->
mfunc.vhcr) {
1840 mlx4_err(dev,
"Couldn't allocate VHCR.\n");
1845 priv->
cmd.pool = pci_pool_create(
"mlx4_cmd", dev->
pdev,
1848 if (!priv->
cmd.pool)
1854 if (mlx4_is_mfunc(dev))
1860 if (!mlx4_is_slave(dev))
1870 if (mlx4_is_master(dev)) {
1875 kfree(priv->
mfunc.master.slave_state[i].vlan_filter[port]);
1887 pci_pool_destroy(priv->
cmd.pool);
1889 if (!mlx4_is_slave(dev))
1891 if (mlx4_is_mfunc(dev))
1910 if (!priv->
cmd.context)
1913 for (i = 0; i < priv->
cmd.max_cmds; ++
i) {
1914 priv->
cmd.context[
i].token =
i;
1915 priv->
cmd.context[
i].next = i + 1;
1918 priv->
cmd.context[priv->
cmd.max_cmds - 1].next = -1;
1919 priv->
cmd.free_head = 0;
1921 sema_init(&priv->
cmd.event_sem, priv->
cmd.max_cmds);
1924 for (priv->
cmd.token_mask = 1;
1925 priv->
cmd.token_mask < priv->
cmd.max_cmds;
1926 priv->
cmd.token_mask <<= 1)
1928 --priv->
cmd.token_mask;
1931 priv->
cmd.use_events = 1;
1944 priv->
cmd.use_events = 0;
1946 for (i = 0; i < priv->
cmd.max_cmds; ++
i)
1951 up(&priv->
cmd.poll_sem);
1964 if (!mailbox->
buf) {