xref: /linux/drivers/net/ethernet/mellanox/mlx4/fw.c (revision f470f8d4e702593ee1d0852871ad80373bce707b)
15a2cc190SJeff Kirsher /*
25a2cc190SJeff Kirsher  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
35a2cc190SJeff Kirsher  * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
45a2cc190SJeff Kirsher  * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc.  All rights reserved.
55a2cc190SJeff Kirsher  *
65a2cc190SJeff Kirsher  * This software is available to you under a choice of one of two
75a2cc190SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
85a2cc190SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
95a2cc190SJeff Kirsher  * COPYING in the main directory of this source tree, or the
105a2cc190SJeff Kirsher  * OpenIB.org BSD license below:
115a2cc190SJeff Kirsher  *
125a2cc190SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
135a2cc190SJeff Kirsher  *     without modification, are permitted provided that the following
145a2cc190SJeff Kirsher  *     conditions are met:
155a2cc190SJeff Kirsher  *
165a2cc190SJeff Kirsher  *      - Redistributions of source code must retain the above
175a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
185a2cc190SJeff Kirsher  *        disclaimer.
195a2cc190SJeff Kirsher  *
205a2cc190SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
215a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
225a2cc190SJeff Kirsher  *        disclaimer in the documentation and/or other materials
235a2cc190SJeff Kirsher  *        provided with the distribution.
245a2cc190SJeff Kirsher  *
255a2cc190SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
265a2cc190SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
275a2cc190SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
285a2cc190SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
295a2cc190SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
305a2cc190SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
315a2cc190SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
325a2cc190SJeff Kirsher  * SOFTWARE.
335a2cc190SJeff Kirsher  */
345a2cc190SJeff Kirsher 
355a2cc190SJeff Kirsher #include <linux/mlx4/cmd.h>
365a2cc190SJeff Kirsher #include <linux/cache.h>
375a2cc190SJeff Kirsher 
385a2cc190SJeff Kirsher #include "fw.h"
395a2cc190SJeff Kirsher #include "icm.h"
405a2cc190SJeff Kirsher 
415a2cc190SJeff Kirsher enum {
425a2cc190SJeff Kirsher 	MLX4_COMMAND_INTERFACE_MIN_REV		= 2,
435a2cc190SJeff Kirsher 	MLX4_COMMAND_INTERFACE_MAX_REV		= 3,
445a2cc190SJeff Kirsher 	MLX4_COMMAND_INTERFACE_NEW_PORT_CMDS	= 3,
455a2cc190SJeff Kirsher };
465a2cc190SJeff Kirsher 
475a2cc190SJeff Kirsher extern void __buggy_use_of_MLX4_GET(void);
485a2cc190SJeff Kirsher extern void __buggy_use_of_MLX4_PUT(void);
495a2cc190SJeff Kirsher 
505a2cc190SJeff Kirsher static int enable_qos;
515a2cc190SJeff Kirsher module_param(enable_qos, bool, 0444);
525a2cc190SJeff Kirsher MODULE_PARM_DESC(enable_qos, "Enable Quality of Service support in the HCA (default: off)");
535a2cc190SJeff Kirsher 
545a2cc190SJeff Kirsher #define MLX4_GET(dest, source, offset)				      \
555a2cc190SJeff Kirsher 	do {							      \
565a2cc190SJeff Kirsher 		void *__p = (char *) (source) + (offset);	      \
575a2cc190SJeff Kirsher 		switch (sizeof (dest)) {			      \
585a2cc190SJeff Kirsher 		case 1: (dest) = *(u8 *) __p;	    break;	      \
595a2cc190SJeff Kirsher 		case 2: (dest) = be16_to_cpup(__p); break;	      \
605a2cc190SJeff Kirsher 		case 4: (dest) = be32_to_cpup(__p); break;	      \
615a2cc190SJeff Kirsher 		case 8: (dest) = be64_to_cpup(__p); break;	      \
625a2cc190SJeff Kirsher 		default: __buggy_use_of_MLX4_GET();		      \
635a2cc190SJeff Kirsher 		}						      \
645a2cc190SJeff Kirsher 	} while (0)
655a2cc190SJeff Kirsher 
665a2cc190SJeff Kirsher #define MLX4_PUT(dest, source, offset)				      \
675a2cc190SJeff Kirsher 	do {							      \
685a2cc190SJeff Kirsher 		void *__d = ((char *) (dest) + (offset));	      \
695a2cc190SJeff Kirsher 		switch (sizeof(source)) {			      \
705a2cc190SJeff Kirsher 		case 1: *(u8 *) __d = (source);		       break; \
715a2cc190SJeff Kirsher 		case 2:	*(__be16 *) __d = cpu_to_be16(source); break; \
725a2cc190SJeff Kirsher 		case 4:	*(__be32 *) __d = cpu_to_be32(source); break; \
735a2cc190SJeff Kirsher 		case 8:	*(__be64 *) __d = cpu_to_be64(source); break; \
745a2cc190SJeff Kirsher 		default: __buggy_use_of_MLX4_PUT();		      \
755a2cc190SJeff Kirsher 		}						      \
765a2cc190SJeff Kirsher 	} while (0)
775a2cc190SJeff Kirsher 
785a2cc190SJeff Kirsher static void dump_dev_cap_flags(struct mlx4_dev *dev, u64 flags)
795a2cc190SJeff Kirsher {
805a2cc190SJeff Kirsher 	static const char *fname[] = {
815a2cc190SJeff Kirsher 		[ 0] = "RC transport",
825a2cc190SJeff Kirsher 		[ 1] = "UC transport",
835a2cc190SJeff Kirsher 		[ 2] = "UD transport",
845a2cc190SJeff Kirsher 		[ 3] = "XRC transport",
855a2cc190SJeff Kirsher 		[ 4] = "reliable multicast",
865a2cc190SJeff Kirsher 		[ 5] = "FCoIB support",
875a2cc190SJeff Kirsher 		[ 6] = "SRQ support",
885a2cc190SJeff Kirsher 		[ 7] = "IPoIB checksum offload",
895a2cc190SJeff Kirsher 		[ 8] = "P_Key violation counter",
905a2cc190SJeff Kirsher 		[ 9] = "Q_Key violation counter",
915a2cc190SJeff Kirsher 		[10] = "VMM",
925a2cc190SJeff Kirsher 		[12] = "DPDP",
935a2cc190SJeff Kirsher 		[15] = "Big LSO headers",
945a2cc190SJeff Kirsher 		[16] = "MW support",
955a2cc190SJeff Kirsher 		[17] = "APM support",
965a2cc190SJeff Kirsher 		[18] = "Atomic ops support",
975a2cc190SJeff Kirsher 		[19] = "Raw multicast support",
985a2cc190SJeff Kirsher 		[20] = "Address vector port checking support",
995a2cc190SJeff Kirsher 		[21] = "UD multicast support",
1005a2cc190SJeff Kirsher 		[24] = "Demand paging support",
1015a2cc190SJeff Kirsher 		[25] = "Router support",
1025a2cc190SJeff Kirsher 		[30] = "IBoE support",
1035a2cc190SJeff Kirsher 		[32] = "Unicast loopback support",
104f3a9d1f2SYevgeny Petrilin 		[34] = "FCS header control",
1055a2cc190SJeff Kirsher 		[38] = "Wake On LAN support",
1065a2cc190SJeff Kirsher 		[40] = "UDP RSS support",
1075a2cc190SJeff Kirsher 		[41] = "Unicast VEP steering support",
1085a2cc190SJeff Kirsher 		[42] = "Multicast VEP steering support",
1095a2cc190SJeff Kirsher 		[48] = "Counters support",
1105a2cc190SJeff Kirsher 	};
1115a2cc190SJeff Kirsher 	int i;
1125a2cc190SJeff Kirsher 
1135a2cc190SJeff Kirsher 	mlx4_dbg(dev, "DEV_CAP flags:\n");
1145a2cc190SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(fname); ++i)
1155a2cc190SJeff Kirsher 		if (fname[i] && (flags & (1LL << i)))
1165a2cc190SJeff Kirsher 			mlx4_dbg(dev, "    %s\n", fname[i]);
1175a2cc190SJeff Kirsher }
1185a2cc190SJeff Kirsher 
1195a2cc190SJeff Kirsher int mlx4_MOD_STAT_CFG(struct mlx4_dev *dev, struct mlx4_mod_stat_cfg *cfg)
1205a2cc190SJeff Kirsher {
1215a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
1225a2cc190SJeff Kirsher 	u32 *inbox;
1235a2cc190SJeff Kirsher 	int err = 0;
1245a2cc190SJeff Kirsher 
1255a2cc190SJeff Kirsher #define MOD_STAT_CFG_IN_SIZE		0x100
1265a2cc190SJeff Kirsher 
1275a2cc190SJeff Kirsher #define MOD_STAT_CFG_PG_SZ_M_OFFSET	0x002
1285a2cc190SJeff Kirsher #define MOD_STAT_CFG_PG_SZ_OFFSET	0x003
1295a2cc190SJeff Kirsher 
1305a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
1315a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
1325a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
1335a2cc190SJeff Kirsher 	inbox = mailbox->buf;
1345a2cc190SJeff Kirsher 
1355a2cc190SJeff Kirsher 	memset(inbox, 0, MOD_STAT_CFG_IN_SIZE);
1365a2cc190SJeff Kirsher 
1375a2cc190SJeff Kirsher 	MLX4_PUT(inbox, cfg->log_pg_sz, MOD_STAT_CFG_PG_SZ_OFFSET);
1385a2cc190SJeff Kirsher 	MLX4_PUT(inbox, cfg->log_pg_sz_m, MOD_STAT_CFG_PG_SZ_M_OFFSET);
1395a2cc190SJeff Kirsher 
1405a2cc190SJeff Kirsher 	err = mlx4_cmd(dev, mailbox->dma, 0, 0, MLX4_CMD_MOD_STAT_CFG,
1415a2cc190SJeff Kirsher 			MLX4_CMD_TIME_CLASS_A);
1425a2cc190SJeff Kirsher 
1435a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
1445a2cc190SJeff Kirsher 	return err;
1455a2cc190SJeff Kirsher }
1465a2cc190SJeff Kirsher 
1475a2cc190SJeff Kirsher int mlx4_QUERY_DEV_CAP(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
1485a2cc190SJeff Kirsher {
1495a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
1505a2cc190SJeff Kirsher 	u32 *outbox;
1515a2cc190SJeff Kirsher 	u8 field;
1525a2cc190SJeff Kirsher 	u32 field32, flags, ext_flags;
1535a2cc190SJeff Kirsher 	u16 size;
1545a2cc190SJeff Kirsher 	u16 stat_rate;
1555a2cc190SJeff Kirsher 	int err;
1565a2cc190SJeff Kirsher 	int i;
1575a2cc190SJeff Kirsher 
1585a2cc190SJeff Kirsher #define QUERY_DEV_CAP_OUT_SIZE		       0x100
1595a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_SRQ_SZ_OFFSET		0x10
1605a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_QP_SZ_OFFSET		0x11
1615a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RSVD_QP_OFFSET		0x12
1625a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_QP_OFFSET		0x13
1635a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RSVD_SRQ_OFFSET		0x14
1645a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_SRQ_OFFSET		0x15
1655a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RSVD_EEC_OFFSET		0x16
1665a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_EEC_OFFSET		0x17
1675a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_CQ_SZ_OFFSET		0x19
1685a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RSVD_CQ_OFFSET		0x1a
1695a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_CQ_OFFSET		0x1b
1705a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_MPT_OFFSET		0x1d
1715a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RSVD_EQ_OFFSET		0x1e
1725a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_EQ_OFFSET		0x1f
1735a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RSVD_MTT_OFFSET		0x20
1745a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_MRW_SZ_OFFSET		0x21
1755a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RSVD_MRW_OFFSET		0x22
1765a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_MTT_SEG_OFFSET	0x23
1775a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_AV_OFFSET		0x27
1785a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_REQ_QP_OFFSET		0x29
1795a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_RES_QP_OFFSET		0x2b
1805a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_GSO_OFFSET		0x2d
1815a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_RDMA_OFFSET		0x2f
1825a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RSZ_SRQ_OFFSET		0x33
1835a2cc190SJeff Kirsher #define QUERY_DEV_CAP_ACK_DELAY_OFFSET		0x35
1845a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MTU_WIDTH_OFFSET		0x36
1855a2cc190SJeff Kirsher #define QUERY_DEV_CAP_VL_PORT_OFFSET		0x37
1865a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_MSG_SZ_OFFSET		0x38
1875a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_GID_OFFSET		0x3b
1885a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RATE_SUPPORT_OFFSET	0x3c
1895a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_PKEY_OFFSET		0x3f
1905a2cc190SJeff Kirsher #define QUERY_DEV_CAP_EXT_FLAGS_OFFSET		0x40
1915a2cc190SJeff Kirsher #define QUERY_DEV_CAP_FLAGS_OFFSET		0x44
1925a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RSVD_UAR_OFFSET		0x48
1935a2cc190SJeff Kirsher #define QUERY_DEV_CAP_UAR_SZ_OFFSET		0x49
1945a2cc190SJeff Kirsher #define QUERY_DEV_CAP_PAGE_SZ_OFFSET		0x4b
1955a2cc190SJeff Kirsher #define QUERY_DEV_CAP_BF_OFFSET			0x4c
1965a2cc190SJeff Kirsher #define QUERY_DEV_CAP_LOG_BF_REG_SZ_OFFSET	0x4d
1975a2cc190SJeff Kirsher #define QUERY_DEV_CAP_LOG_MAX_BF_REGS_PER_PAGE_OFFSET	0x4e
1985a2cc190SJeff Kirsher #define QUERY_DEV_CAP_LOG_MAX_BF_PAGES_OFFSET	0x4f
1995a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_SG_SQ_OFFSET		0x51
2005a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_DESC_SZ_SQ_OFFSET	0x52
2015a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_SG_RQ_OFFSET		0x55
2025a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_DESC_SZ_RQ_OFFSET	0x56
2035a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_QP_MCG_OFFSET		0x61
2045a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RSVD_MCG_OFFSET		0x62
2055a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_MCG_OFFSET		0x63
2065a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RSVD_PD_OFFSET		0x64
2075a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_PD_OFFSET		0x65
208*f470f8d4SLinus Torvalds #define QUERY_DEV_CAP_RSVD_XRC_OFFSET		0x66
209*f470f8d4SLinus Torvalds #define QUERY_DEV_CAP_MAX_XRC_OFFSET		0x67
2105a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_COUNTERS_OFFSET	0x68
2115a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RDMARC_ENTRY_SZ_OFFSET	0x80
2125a2cc190SJeff Kirsher #define QUERY_DEV_CAP_QPC_ENTRY_SZ_OFFSET	0x82
2135a2cc190SJeff Kirsher #define QUERY_DEV_CAP_AUX_ENTRY_SZ_OFFSET	0x84
2145a2cc190SJeff Kirsher #define QUERY_DEV_CAP_ALTC_ENTRY_SZ_OFFSET	0x86
2155a2cc190SJeff Kirsher #define QUERY_DEV_CAP_EQC_ENTRY_SZ_OFFSET	0x88
2165a2cc190SJeff Kirsher #define QUERY_DEV_CAP_CQC_ENTRY_SZ_OFFSET	0x8a
2175a2cc190SJeff Kirsher #define QUERY_DEV_CAP_SRQ_ENTRY_SZ_OFFSET	0x8c
2185a2cc190SJeff Kirsher #define QUERY_DEV_CAP_C_MPT_ENTRY_SZ_OFFSET	0x8e
2195a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MTT_ENTRY_SZ_OFFSET	0x90
2205a2cc190SJeff Kirsher #define QUERY_DEV_CAP_D_MPT_ENTRY_SZ_OFFSET	0x92
2215a2cc190SJeff Kirsher #define QUERY_DEV_CAP_BMME_FLAGS_OFFSET		0x94
2225a2cc190SJeff Kirsher #define QUERY_DEV_CAP_RSVD_LKEY_OFFSET		0x98
2235a2cc190SJeff Kirsher #define QUERY_DEV_CAP_MAX_ICM_SZ_OFFSET		0xa0
2245a2cc190SJeff Kirsher 
2255a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
2265a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
2275a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
2285a2cc190SJeff Kirsher 	outbox = mailbox->buf;
2295a2cc190SJeff Kirsher 
2305a2cc190SJeff Kirsher 	err = mlx4_cmd_box(dev, 0, mailbox->dma, 0, 0, MLX4_CMD_QUERY_DEV_CAP,
2315a2cc190SJeff Kirsher 			   MLX4_CMD_TIME_CLASS_A);
2325a2cc190SJeff Kirsher 	if (err)
2335a2cc190SJeff Kirsher 		goto out;
2345a2cc190SJeff Kirsher 
2355a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_QP_OFFSET);
2365a2cc190SJeff Kirsher 	dev_cap->reserved_qps = 1 << (field & 0xf);
2375a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_QP_OFFSET);
2385a2cc190SJeff Kirsher 	dev_cap->max_qps = 1 << (field & 0x1f);
2395a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_SRQ_OFFSET);
2405a2cc190SJeff Kirsher 	dev_cap->reserved_srqs = 1 << (field >> 4);
2415a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_SRQ_OFFSET);
2425a2cc190SJeff Kirsher 	dev_cap->max_srqs = 1 << (field & 0x1f);
2435a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_CQ_SZ_OFFSET);
2445a2cc190SJeff Kirsher 	dev_cap->max_cq_sz = 1 << field;
2455a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_CQ_OFFSET);
2465a2cc190SJeff Kirsher 	dev_cap->reserved_cqs = 1 << (field & 0xf);
2475a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_CQ_OFFSET);
2485a2cc190SJeff Kirsher 	dev_cap->max_cqs = 1 << (field & 0x1f);
2495a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_MPT_OFFSET);
2505a2cc190SJeff Kirsher 	dev_cap->max_mpts = 1 << (field & 0x3f);
2515a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_EQ_OFFSET);
2525a2cc190SJeff Kirsher 	dev_cap->reserved_eqs = field & 0xf;
2535a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_EQ_OFFSET);
2545a2cc190SJeff Kirsher 	dev_cap->max_eqs = 1 << (field & 0xf);
2555a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_MTT_OFFSET);
2565a2cc190SJeff Kirsher 	dev_cap->reserved_mtts = 1 << (field >> 4);
2575a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_MRW_SZ_OFFSET);
2585a2cc190SJeff Kirsher 	dev_cap->max_mrw_sz = 1 << field;
2595a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_MRW_OFFSET);
2605a2cc190SJeff Kirsher 	dev_cap->reserved_mrws = 1 << (field & 0xf);
2615a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_MTT_SEG_OFFSET);
2625a2cc190SJeff Kirsher 	dev_cap->max_mtt_seg = 1 << (field & 0x3f);
2635a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_REQ_QP_OFFSET);
2645a2cc190SJeff Kirsher 	dev_cap->max_requester_per_qp = 1 << (field & 0x3f);
2655a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_RES_QP_OFFSET);
2665a2cc190SJeff Kirsher 	dev_cap->max_responder_per_qp = 1 << (field & 0x3f);
2675a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_GSO_OFFSET);
2685a2cc190SJeff Kirsher 	field &= 0x1f;
2695a2cc190SJeff Kirsher 	if (!field)
2705a2cc190SJeff Kirsher 		dev_cap->max_gso_sz = 0;
2715a2cc190SJeff Kirsher 	else
2725a2cc190SJeff Kirsher 		dev_cap->max_gso_sz = 1 << field;
2735a2cc190SJeff Kirsher 
2745a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_RDMA_OFFSET);
2755a2cc190SJeff Kirsher 	dev_cap->max_rdma_global = 1 << (field & 0x3f);
2765a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_ACK_DELAY_OFFSET);
2775a2cc190SJeff Kirsher 	dev_cap->local_ca_ack_delay = field & 0x1f;
2785a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_VL_PORT_OFFSET);
2795a2cc190SJeff Kirsher 	dev_cap->num_ports = field & 0xf;
2805a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_MSG_SZ_OFFSET);
2815a2cc190SJeff Kirsher 	dev_cap->max_msg_sz = 1 << (field & 0x1f);
2825a2cc190SJeff Kirsher 	MLX4_GET(stat_rate, outbox, QUERY_DEV_CAP_RATE_SUPPORT_OFFSET);
2835a2cc190SJeff Kirsher 	dev_cap->stat_rate_support = stat_rate;
2845a2cc190SJeff Kirsher 	MLX4_GET(ext_flags, outbox, QUERY_DEV_CAP_EXT_FLAGS_OFFSET);
2855a2cc190SJeff Kirsher 	MLX4_GET(flags, outbox, QUERY_DEV_CAP_FLAGS_OFFSET);
2865a2cc190SJeff Kirsher 	dev_cap->flags = flags | (u64)ext_flags << 32;
2875a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_UAR_OFFSET);
2885a2cc190SJeff Kirsher 	dev_cap->reserved_uars = field >> 4;
2895a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_UAR_SZ_OFFSET);
2905a2cc190SJeff Kirsher 	dev_cap->uar_size = 1 << ((field & 0x3f) + 20);
2915a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_PAGE_SZ_OFFSET);
2925a2cc190SJeff Kirsher 	dev_cap->min_page_sz = 1 << field;
2935a2cc190SJeff Kirsher 
2945a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_BF_OFFSET);
2955a2cc190SJeff Kirsher 	if (field & 0x80) {
2965a2cc190SJeff Kirsher 		MLX4_GET(field, outbox, QUERY_DEV_CAP_LOG_BF_REG_SZ_OFFSET);
2975a2cc190SJeff Kirsher 		dev_cap->bf_reg_size = 1 << (field & 0x1f);
2985a2cc190SJeff Kirsher 		MLX4_GET(field, outbox, QUERY_DEV_CAP_LOG_MAX_BF_REGS_PER_PAGE_OFFSET);
2995a2cc190SJeff Kirsher 		if ((1 << (field & 0x3f)) > (PAGE_SIZE / dev_cap->bf_reg_size))
3005a2cc190SJeff Kirsher 			field = 3;
3015a2cc190SJeff Kirsher 		dev_cap->bf_regs_per_page = 1 << (field & 0x3f);
3025a2cc190SJeff Kirsher 		mlx4_dbg(dev, "BlueFlame available (reg size %d, regs/page %d)\n",
3035a2cc190SJeff Kirsher 			 dev_cap->bf_reg_size, dev_cap->bf_regs_per_page);
3045a2cc190SJeff Kirsher 	} else {
3055a2cc190SJeff Kirsher 		dev_cap->bf_reg_size = 0;
3065a2cc190SJeff Kirsher 		mlx4_dbg(dev, "BlueFlame not available\n");
3075a2cc190SJeff Kirsher 	}
3085a2cc190SJeff Kirsher 
3095a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_SG_SQ_OFFSET);
3105a2cc190SJeff Kirsher 	dev_cap->max_sq_sg = field;
3115a2cc190SJeff Kirsher 	MLX4_GET(size, outbox, QUERY_DEV_CAP_MAX_DESC_SZ_SQ_OFFSET);
3125a2cc190SJeff Kirsher 	dev_cap->max_sq_desc_sz = size;
3135a2cc190SJeff Kirsher 
3145a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_QP_MCG_OFFSET);
3155a2cc190SJeff Kirsher 	dev_cap->max_qp_per_mcg = 1 << field;
3165a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_MCG_OFFSET);
3175a2cc190SJeff Kirsher 	dev_cap->reserved_mgms = field & 0xf;
3185a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_MCG_OFFSET);
3195a2cc190SJeff Kirsher 	dev_cap->max_mcgs = 1 << field;
3205a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_PD_OFFSET);
3215a2cc190SJeff Kirsher 	dev_cap->reserved_pds = field >> 4;
3225a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_PD_OFFSET);
3235a2cc190SJeff Kirsher 	dev_cap->max_pds = 1 << (field & 0x3f);
324*f470f8d4SLinus Torvalds 	MLX4_GET(field, outbox, QUERY_DEV_CAP_RSVD_XRC_OFFSET);
325*f470f8d4SLinus Torvalds 	dev_cap->reserved_xrcds = field >> 4;
326*f470f8d4SLinus Torvalds 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_PD_OFFSET);
327*f470f8d4SLinus Torvalds 	dev_cap->max_xrcds = 1 << (field & 0x1f);
3285a2cc190SJeff Kirsher 
3295a2cc190SJeff Kirsher 	MLX4_GET(size, outbox, QUERY_DEV_CAP_RDMARC_ENTRY_SZ_OFFSET);
3305a2cc190SJeff Kirsher 	dev_cap->rdmarc_entry_sz = size;
3315a2cc190SJeff Kirsher 	MLX4_GET(size, outbox, QUERY_DEV_CAP_QPC_ENTRY_SZ_OFFSET);
3325a2cc190SJeff Kirsher 	dev_cap->qpc_entry_sz = size;
3335a2cc190SJeff Kirsher 	MLX4_GET(size, outbox, QUERY_DEV_CAP_AUX_ENTRY_SZ_OFFSET);
3345a2cc190SJeff Kirsher 	dev_cap->aux_entry_sz = size;
3355a2cc190SJeff Kirsher 	MLX4_GET(size, outbox, QUERY_DEV_CAP_ALTC_ENTRY_SZ_OFFSET);
3365a2cc190SJeff Kirsher 	dev_cap->altc_entry_sz = size;
3375a2cc190SJeff Kirsher 	MLX4_GET(size, outbox, QUERY_DEV_CAP_EQC_ENTRY_SZ_OFFSET);
3385a2cc190SJeff Kirsher 	dev_cap->eqc_entry_sz = size;
3395a2cc190SJeff Kirsher 	MLX4_GET(size, outbox, QUERY_DEV_CAP_CQC_ENTRY_SZ_OFFSET);
3405a2cc190SJeff Kirsher 	dev_cap->cqc_entry_sz = size;
3415a2cc190SJeff Kirsher 	MLX4_GET(size, outbox, QUERY_DEV_CAP_SRQ_ENTRY_SZ_OFFSET);
3425a2cc190SJeff Kirsher 	dev_cap->srq_entry_sz = size;
3435a2cc190SJeff Kirsher 	MLX4_GET(size, outbox, QUERY_DEV_CAP_C_MPT_ENTRY_SZ_OFFSET);
3445a2cc190SJeff Kirsher 	dev_cap->cmpt_entry_sz = size;
3455a2cc190SJeff Kirsher 	MLX4_GET(size, outbox, QUERY_DEV_CAP_MTT_ENTRY_SZ_OFFSET);
3465a2cc190SJeff Kirsher 	dev_cap->mtt_entry_sz = size;
3475a2cc190SJeff Kirsher 	MLX4_GET(size, outbox, QUERY_DEV_CAP_D_MPT_ENTRY_SZ_OFFSET);
3485a2cc190SJeff Kirsher 	dev_cap->dmpt_entry_sz = size;
3495a2cc190SJeff Kirsher 
3505a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_SRQ_SZ_OFFSET);
3515a2cc190SJeff Kirsher 	dev_cap->max_srq_sz = 1 << field;
3525a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_QP_SZ_OFFSET);
3535a2cc190SJeff Kirsher 	dev_cap->max_qp_sz = 1 << field;
3545a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_RSZ_SRQ_OFFSET);
3555a2cc190SJeff Kirsher 	dev_cap->resize_srq = field & 1;
3565a2cc190SJeff Kirsher 	MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_SG_RQ_OFFSET);
3575a2cc190SJeff Kirsher 	dev_cap->max_rq_sg = field;
3585a2cc190SJeff Kirsher 	MLX4_GET(size, outbox, QUERY_DEV_CAP_MAX_DESC_SZ_RQ_OFFSET);
3595a2cc190SJeff Kirsher 	dev_cap->max_rq_desc_sz = size;
3605a2cc190SJeff Kirsher 
3615a2cc190SJeff Kirsher 	MLX4_GET(dev_cap->bmme_flags, outbox,
3625a2cc190SJeff Kirsher 		 QUERY_DEV_CAP_BMME_FLAGS_OFFSET);
3635a2cc190SJeff Kirsher 	MLX4_GET(dev_cap->reserved_lkey, outbox,
3645a2cc190SJeff Kirsher 		 QUERY_DEV_CAP_RSVD_LKEY_OFFSET);
3655a2cc190SJeff Kirsher 	MLX4_GET(dev_cap->max_icm_sz, outbox,
3665a2cc190SJeff Kirsher 		 QUERY_DEV_CAP_MAX_ICM_SZ_OFFSET);
3675a2cc190SJeff Kirsher 	if (dev_cap->flags & MLX4_DEV_CAP_FLAG_COUNTERS)
3685a2cc190SJeff Kirsher 		MLX4_GET(dev_cap->max_counters, outbox,
3695a2cc190SJeff Kirsher 			 QUERY_DEV_CAP_MAX_COUNTERS_OFFSET);
3705a2cc190SJeff Kirsher 
3715a2cc190SJeff Kirsher 	if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
3725a2cc190SJeff Kirsher 		for (i = 1; i <= dev_cap->num_ports; ++i) {
3735a2cc190SJeff Kirsher 			MLX4_GET(field, outbox, QUERY_DEV_CAP_VL_PORT_OFFSET);
3745a2cc190SJeff Kirsher 			dev_cap->max_vl[i]	   = field >> 4;
3755a2cc190SJeff Kirsher 			MLX4_GET(field, outbox, QUERY_DEV_CAP_MTU_WIDTH_OFFSET);
3765a2cc190SJeff Kirsher 			dev_cap->ib_mtu[i]	   = field >> 4;
3775a2cc190SJeff Kirsher 			dev_cap->max_port_width[i] = field & 0xf;
3785a2cc190SJeff Kirsher 			MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_GID_OFFSET);
3795a2cc190SJeff Kirsher 			dev_cap->max_gids[i]	   = 1 << (field & 0xf);
3805a2cc190SJeff Kirsher 			MLX4_GET(field, outbox, QUERY_DEV_CAP_MAX_PKEY_OFFSET);
3815a2cc190SJeff Kirsher 			dev_cap->max_pkeys[i]	   = 1 << (field & 0xf);
3825a2cc190SJeff Kirsher 		}
3835a2cc190SJeff Kirsher 	} else {
3845a2cc190SJeff Kirsher #define QUERY_PORT_SUPPORTED_TYPE_OFFSET	0x00
3855a2cc190SJeff Kirsher #define QUERY_PORT_MTU_OFFSET			0x01
3865a2cc190SJeff Kirsher #define QUERY_PORT_ETH_MTU_OFFSET		0x02
3875a2cc190SJeff Kirsher #define QUERY_PORT_WIDTH_OFFSET			0x06
3885a2cc190SJeff Kirsher #define QUERY_PORT_MAX_GID_PKEY_OFFSET		0x07
3895a2cc190SJeff Kirsher #define QUERY_PORT_MAX_MACVLAN_OFFSET		0x0a
3905a2cc190SJeff Kirsher #define QUERY_PORT_MAX_VL_OFFSET		0x0b
3915a2cc190SJeff Kirsher #define QUERY_PORT_MAC_OFFSET			0x10
3925a2cc190SJeff Kirsher #define QUERY_PORT_TRANS_VENDOR_OFFSET		0x18
3935a2cc190SJeff Kirsher #define QUERY_PORT_WAVELENGTH_OFFSET		0x1c
3945a2cc190SJeff Kirsher #define QUERY_PORT_TRANS_CODE_OFFSET		0x20
3955a2cc190SJeff Kirsher 
3965a2cc190SJeff Kirsher 		for (i = 1; i <= dev_cap->num_ports; ++i) {
3975a2cc190SJeff Kirsher 			err = mlx4_cmd_box(dev, 0, mailbox->dma, i, 0, MLX4_CMD_QUERY_PORT,
3985a2cc190SJeff Kirsher 					   MLX4_CMD_TIME_CLASS_B);
3995a2cc190SJeff Kirsher 			if (err)
4005a2cc190SJeff Kirsher 				goto out;
4015a2cc190SJeff Kirsher 
4025a2cc190SJeff Kirsher 			MLX4_GET(field, outbox, QUERY_PORT_SUPPORTED_TYPE_OFFSET);
4035a2cc190SJeff Kirsher 			dev_cap->supported_port_types[i] = field & 3;
4045a2cc190SJeff Kirsher 			MLX4_GET(field, outbox, QUERY_PORT_MTU_OFFSET);
4055a2cc190SJeff Kirsher 			dev_cap->ib_mtu[i]	   = field & 0xf;
4065a2cc190SJeff Kirsher 			MLX4_GET(field, outbox, QUERY_PORT_WIDTH_OFFSET);
4075a2cc190SJeff Kirsher 			dev_cap->max_port_width[i] = field & 0xf;
4085a2cc190SJeff Kirsher 			MLX4_GET(field, outbox, QUERY_PORT_MAX_GID_PKEY_OFFSET);
4095a2cc190SJeff Kirsher 			dev_cap->max_gids[i]	   = 1 << (field >> 4);
4105a2cc190SJeff Kirsher 			dev_cap->max_pkeys[i]	   = 1 << (field & 0xf);
4115a2cc190SJeff Kirsher 			MLX4_GET(field, outbox, QUERY_PORT_MAX_VL_OFFSET);
4125a2cc190SJeff Kirsher 			dev_cap->max_vl[i]	   = field & 0xf;
4135a2cc190SJeff Kirsher 			MLX4_GET(field, outbox, QUERY_PORT_MAX_MACVLAN_OFFSET);
4145a2cc190SJeff Kirsher 			dev_cap->log_max_macs[i]  = field & 0xf;
4155a2cc190SJeff Kirsher 			dev_cap->log_max_vlans[i] = field >> 4;
4165a2cc190SJeff Kirsher 			MLX4_GET(dev_cap->eth_mtu[i], outbox, QUERY_PORT_ETH_MTU_OFFSET);
4175a2cc190SJeff Kirsher 			MLX4_GET(dev_cap->def_mac[i], outbox, QUERY_PORT_MAC_OFFSET);
4185a2cc190SJeff Kirsher 			MLX4_GET(field32, outbox, QUERY_PORT_TRANS_VENDOR_OFFSET);
4195a2cc190SJeff Kirsher 			dev_cap->trans_type[i] = field32 >> 24;
4205a2cc190SJeff Kirsher 			dev_cap->vendor_oui[i] = field32 & 0xffffff;
4215a2cc190SJeff Kirsher 			MLX4_GET(dev_cap->wavelength[i], outbox, QUERY_PORT_WAVELENGTH_OFFSET);
4225a2cc190SJeff Kirsher 			MLX4_GET(dev_cap->trans_code[i], outbox, QUERY_PORT_TRANS_CODE_OFFSET);
4235a2cc190SJeff Kirsher 		}
4245a2cc190SJeff Kirsher 	}
4255a2cc190SJeff Kirsher 
4265a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Base MM extensions: flags %08x, rsvd L_Key %08x\n",
4275a2cc190SJeff Kirsher 		 dev_cap->bmme_flags, dev_cap->reserved_lkey);
4285a2cc190SJeff Kirsher 
4295a2cc190SJeff Kirsher 	/*
4305a2cc190SJeff Kirsher 	 * Each UAR has 4 EQ doorbells; so if a UAR is reserved, then
4315a2cc190SJeff Kirsher 	 * we can't use any EQs whose doorbell falls on that page,
4325a2cc190SJeff Kirsher 	 * even if the EQ itself isn't reserved.
4335a2cc190SJeff Kirsher 	 */
4345a2cc190SJeff Kirsher 	dev_cap->reserved_eqs = max(dev_cap->reserved_uars * 4,
4355a2cc190SJeff Kirsher 				    dev_cap->reserved_eqs);
4365a2cc190SJeff Kirsher 
4375a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Max ICM size %lld MB\n",
4385a2cc190SJeff Kirsher 		 (unsigned long long) dev_cap->max_icm_sz >> 20);
4395a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Max QPs: %d, reserved QPs: %d, entry size: %d\n",
4405a2cc190SJeff Kirsher 		 dev_cap->max_qps, dev_cap->reserved_qps, dev_cap->qpc_entry_sz);
4415a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Max SRQs: %d, reserved SRQs: %d, entry size: %d\n",
4425a2cc190SJeff Kirsher 		 dev_cap->max_srqs, dev_cap->reserved_srqs, dev_cap->srq_entry_sz);
4435a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Max CQs: %d, reserved CQs: %d, entry size: %d\n",
4445a2cc190SJeff Kirsher 		 dev_cap->max_cqs, dev_cap->reserved_cqs, dev_cap->cqc_entry_sz);
4455a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Max EQs: %d, reserved EQs: %d, entry size: %d\n",
4465a2cc190SJeff Kirsher 		 dev_cap->max_eqs, dev_cap->reserved_eqs, dev_cap->eqc_entry_sz);
4475a2cc190SJeff Kirsher 	mlx4_dbg(dev, "reserved MPTs: %d, reserved MTTs: %d\n",
4485a2cc190SJeff Kirsher 		 dev_cap->reserved_mrws, dev_cap->reserved_mtts);
4495a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Max PDs: %d, reserved PDs: %d, reserved UARs: %d\n",
4505a2cc190SJeff Kirsher 		 dev_cap->max_pds, dev_cap->reserved_pds, dev_cap->reserved_uars);
4515a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Max QP/MCG: %d, reserved MGMs: %d\n",
4525a2cc190SJeff Kirsher 		 dev_cap->max_pds, dev_cap->reserved_mgms);
4535a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Max CQEs: %d, max WQEs: %d, max SRQ WQEs: %d\n",
4545a2cc190SJeff Kirsher 		 dev_cap->max_cq_sz, dev_cap->max_qp_sz, dev_cap->max_srq_sz);
4555a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Local CA ACK delay: %d, max MTU: %d, port width cap: %d\n",
4565a2cc190SJeff Kirsher 		 dev_cap->local_ca_ack_delay, 128 << dev_cap->ib_mtu[1],
4575a2cc190SJeff Kirsher 		 dev_cap->max_port_width[1]);
4585a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Max SQ desc size: %d, max SQ S/G: %d\n",
4595a2cc190SJeff Kirsher 		 dev_cap->max_sq_desc_sz, dev_cap->max_sq_sg);
4605a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Max RQ desc size: %d, max RQ S/G: %d\n",
4615a2cc190SJeff Kirsher 		 dev_cap->max_rq_desc_sz, dev_cap->max_rq_sg);
4625a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Max GSO size: %d\n", dev_cap->max_gso_sz);
4635a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Max counters: %d\n", dev_cap->max_counters);
4645a2cc190SJeff Kirsher 
4655a2cc190SJeff Kirsher 	dump_dev_cap_flags(dev, dev_cap->flags);
4665a2cc190SJeff Kirsher 
4675a2cc190SJeff Kirsher out:
4685a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
4695a2cc190SJeff Kirsher 	return err;
4705a2cc190SJeff Kirsher }
4715a2cc190SJeff Kirsher 
4725a2cc190SJeff Kirsher int mlx4_map_cmd(struct mlx4_dev *dev, u16 op, struct mlx4_icm *icm, u64 virt)
4735a2cc190SJeff Kirsher {
4745a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
4755a2cc190SJeff Kirsher 	struct mlx4_icm_iter iter;
4765a2cc190SJeff Kirsher 	__be64 *pages;
4775a2cc190SJeff Kirsher 	int lg;
4785a2cc190SJeff Kirsher 	int nent = 0;
4795a2cc190SJeff Kirsher 	int i;
4805a2cc190SJeff Kirsher 	int err = 0;
4815a2cc190SJeff Kirsher 	int ts = 0, tc = 0;
4825a2cc190SJeff Kirsher 
4835a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
4845a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
4855a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
4865a2cc190SJeff Kirsher 	memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
4875a2cc190SJeff Kirsher 	pages = mailbox->buf;
4885a2cc190SJeff Kirsher 
4895a2cc190SJeff Kirsher 	for (mlx4_icm_first(icm, &iter);
4905a2cc190SJeff Kirsher 	     !mlx4_icm_last(&iter);
4915a2cc190SJeff Kirsher 	     mlx4_icm_next(&iter)) {
4925a2cc190SJeff Kirsher 		/*
4935a2cc190SJeff Kirsher 		 * We have to pass pages that are aligned to their
4945a2cc190SJeff Kirsher 		 * size, so find the least significant 1 in the
4955a2cc190SJeff Kirsher 		 * address or size and use that as our log2 size.
4965a2cc190SJeff Kirsher 		 */
4975a2cc190SJeff Kirsher 		lg = ffs(mlx4_icm_addr(&iter) | mlx4_icm_size(&iter)) - 1;
4985a2cc190SJeff Kirsher 		if (lg < MLX4_ICM_PAGE_SHIFT) {
4995a2cc190SJeff Kirsher 			mlx4_warn(dev, "Got FW area not aligned to %d (%llx/%lx).\n",
5005a2cc190SJeff Kirsher 				   MLX4_ICM_PAGE_SIZE,
5015a2cc190SJeff Kirsher 				   (unsigned long long) mlx4_icm_addr(&iter),
5025a2cc190SJeff Kirsher 				   mlx4_icm_size(&iter));
5035a2cc190SJeff Kirsher 			err = -EINVAL;
5045a2cc190SJeff Kirsher 			goto out;
5055a2cc190SJeff Kirsher 		}
5065a2cc190SJeff Kirsher 
5075a2cc190SJeff Kirsher 		for (i = 0; i < mlx4_icm_size(&iter) >> lg; ++i) {
5085a2cc190SJeff Kirsher 			if (virt != -1) {
5095a2cc190SJeff Kirsher 				pages[nent * 2] = cpu_to_be64(virt);
5105a2cc190SJeff Kirsher 				virt += 1 << lg;
5115a2cc190SJeff Kirsher 			}
5125a2cc190SJeff Kirsher 
5135a2cc190SJeff Kirsher 			pages[nent * 2 + 1] =
5145a2cc190SJeff Kirsher 				cpu_to_be64((mlx4_icm_addr(&iter) + (i << lg)) |
5155a2cc190SJeff Kirsher 					    (lg - MLX4_ICM_PAGE_SHIFT));
5165a2cc190SJeff Kirsher 			ts += 1 << (lg - 10);
5175a2cc190SJeff Kirsher 			++tc;
5185a2cc190SJeff Kirsher 
5195a2cc190SJeff Kirsher 			if (++nent == MLX4_MAILBOX_SIZE / 16) {
5205a2cc190SJeff Kirsher 				err = mlx4_cmd(dev, mailbox->dma, nent, 0, op,
5215a2cc190SJeff Kirsher 						MLX4_CMD_TIME_CLASS_B);
5225a2cc190SJeff Kirsher 				if (err)
5235a2cc190SJeff Kirsher 					goto out;
5245a2cc190SJeff Kirsher 				nent = 0;
5255a2cc190SJeff Kirsher 			}
5265a2cc190SJeff Kirsher 		}
5275a2cc190SJeff Kirsher 	}
5285a2cc190SJeff Kirsher 
5295a2cc190SJeff Kirsher 	if (nent)
5305a2cc190SJeff Kirsher 		err = mlx4_cmd(dev, mailbox->dma, nent, 0, op, MLX4_CMD_TIME_CLASS_B);
5315a2cc190SJeff Kirsher 	if (err)
5325a2cc190SJeff Kirsher 		goto out;
5335a2cc190SJeff Kirsher 
5345a2cc190SJeff Kirsher 	switch (op) {
5355a2cc190SJeff Kirsher 	case MLX4_CMD_MAP_FA:
5365a2cc190SJeff Kirsher 		mlx4_dbg(dev, "Mapped %d chunks/%d KB for FW.\n", tc, ts);
5375a2cc190SJeff Kirsher 		break;
5385a2cc190SJeff Kirsher 	case MLX4_CMD_MAP_ICM_AUX:
5395a2cc190SJeff Kirsher 		mlx4_dbg(dev, "Mapped %d chunks/%d KB for ICM aux.\n", tc, ts);
5405a2cc190SJeff Kirsher 		break;
5415a2cc190SJeff Kirsher 	case MLX4_CMD_MAP_ICM:
5425a2cc190SJeff Kirsher 		mlx4_dbg(dev, "Mapped %d chunks/%d KB at %llx for ICM.\n",
5435a2cc190SJeff Kirsher 			  tc, ts, (unsigned long long) virt - (ts << 10));
5445a2cc190SJeff Kirsher 		break;
5455a2cc190SJeff Kirsher 	}
5465a2cc190SJeff Kirsher 
5475a2cc190SJeff Kirsher out:
5485a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
5495a2cc190SJeff Kirsher 	return err;
5505a2cc190SJeff Kirsher }
5515a2cc190SJeff Kirsher 
5525a2cc190SJeff Kirsher int mlx4_MAP_FA(struct mlx4_dev *dev, struct mlx4_icm *icm)
5535a2cc190SJeff Kirsher {
5545a2cc190SJeff Kirsher 	return mlx4_map_cmd(dev, MLX4_CMD_MAP_FA, icm, -1);
5555a2cc190SJeff Kirsher }
5565a2cc190SJeff Kirsher 
5575a2cc190SJeff Kirsher int mlx4_UNMAP_FA(struct mlx4_dev *dev)
5585a2cc190SJeff Kirsher {
5595a2cc190SJeff Kirsher 	return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_UNMAP_FA, MLX4_CMD_TIME_CLASS_B);
5605a2cc190SJeff Kirsher }
5615a2cc190SJeff Kirsher 
5625a2cc190SJeff Kirsher 
5635a2cc190SJeff Kirsher int mlx4_RUN_FW(struct mlx4_dev *dev)
5645a2cc190SJeff Kirsher {
5655a2cc190SJeff Kirsher 	return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_RUN_FW, MLX4_CMD_TIME_CLASS_A);
5665a2cc190SJeff Kirsher }
5675a2cc190SJeff Kirsher 
5685a2cc190SJeff Kirsher int mlx4_QUERY_FW(struct mlx4_dev *dev)
5695a2cc190SJeff Kirsher {
5705a2cc190SJeff Kirsher 	struct mlx4_fw  *fw  = &mlx4_priv(dev)->fw;
5715a2cc190SJeff Kirsher 	struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
5725a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
5735a2cc190SJeff Kirsher 	u32 *outbox;
5745a2cc190SJeff Kirsher 	int err = 0;
5755a2cc190SJeff Kirsher 	u64 fw_ver;
5765a2cc190SJeff Kirsher 	u16 cmd_if_rev;
5775a2cc190SJeff Kirsher 	u8 lg;
5785a2cc190SJeff Kirsher 
5795a2cc190SJeff Kirsher #define QUERY_FW_OUT_SIZE             0x100
5805a2cc190SJeff Kirsher #define QUERY_FW_VER_OFFSET            0x00
5815a2cc190SJeff Kirsher #define QUERY_FW_CMD_IF_REV_OFFSET     0x0a
5825a2cc190SJeff Kirsher #define QUERY_FW_MAX_CMD_OFFSET        0x0f
5835a2cc190SJeff Kirsher #define QUERY_FW_ERR_START_OFFSET      0x30
5845a2cc190SJeff Kirsher #define QUERY_FW_ERR_SIZE_OFFSET       0x38
5855a2cc190SJeff Kirsher #define QUERY_FW_ERR_BAR_OFFSET        0x3c
5865a2cc190SJeff Kirsher 
5875a2cc190SJeff Kirsher #define QUERY_FW_SIZE_OFFSET           0x00
5885a2cc190SJeff Kirsher #define QUERY_FW_CLR_INT_BASE_OFFSET   0x20
5895a2cc190SJeff Kirsher #define QUERY_FW_CLR_INT_BAR_OFFSET    0x28
5905a2cc190SJeff Kirsher 
5915a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
5925a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
5935a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
5945a2cc190SJeff Kirsher 	outbox = mailbox->buf;
5955a2cc190SJeff Kirsher 
5965a2cc190SJeff Kirsher 	err = mlx4_cmd_box(dev, 0, mailbox->dma, 0, 0, MLX4_CMD_QUERY_FW,
5975a2cc190SJeff Kirsher 			    MLX4_CMD_TIME_CLASS_A);
5985a2cc190SJeff Kirsher 	if (err)
5995a2cc190SJeff Kirsher 		goto out;
6005a2cc190SJeff Kirsher 
6015a2cc190SJeff Kirsher 	MLX4_GET(fw_ver, outbox, QUERY_FW_VER_OFFSET);
6025a2cc190SJeff Kirsher 	/*
6035a2cc190SJeff Kirsher 	 * FW subminor version is at more significant bits than minor
6045a2cc190SJeff Kirsher 	 * version, so swap here.
6055a2cc190SJeff Kirsher 	 */
6065a2cc190SJeff Kirsher 	dev->caps.fw_ver = (fw_ver & 0xffff00000000ull) |
6075a2cc190SJeff Kirsher 		((fw_ver & 0xffff0000ull) >> 16) |
6085a2cc190SJeff Kirsher 		((fw_ver & 0x0000ffffull) << 16);
6095a2cc190SJeff Kirsher 
6105a2cc190SJeff Kirsher 	MLX4_GET(cmd_if_rev, outbox, QUERY_FW_CMD_IF_REV_OFFSET);
6115a2cc190SJeff Kirsher 	if (cmd_if_rev < MLX4_COMMAND_INTERFACE_MIN_REV ||
6125a2cc190SJeff Kirsher 	    cmd_if_rev > MLX4_COMMAND_INTERFACE_MAX_REV) {
6135a2cc190SJeff Kirsher 		mlx4_err(dev, "Installed FW has unsupported "
6145a2cc190SJeff Kirsher 			 "command interface revision %d.\n",
6155a2cc190SJeff Kirsher 			 cmd_if_rev);
6165a2cc190SJeff Kirsher 		mlx4_err(dev, "(Installed FW version is %d.%d.%03d)\n",
6175a2cc190SJeff Kirsher 			 (int) (dev->caps.fw_ver >> 32),
6185a2cc190SJeff Kirsher 			 (int) (dev->caps.fw_ver >> 16) & 0xffff,
6195a2cc190SJeff Kirsher 			 (int) dev->caps.fw_ver & 0xffff);
6205a2cc190SJeff Kirsher 		mlx4_err(dev, "This driver version supports only revisions %d to %d.\n",
6215a2cc190SJeff Kirsher 			 MLX4_COMMAND_INTERFACE_MIN_REV, MLX4_COMMAND_INTERFACE_MAX_REV);
6225a2cc190SJeff Kirsher 		err = -ENODEV;
6235a2cc190SJeff Kirsher 		goto out;
6245a2cc190SJeff Kirsher 	}
6255a2cc190SJeff Kirsher 
6265a2cc190SJeff Kirsher 	if (cmd_if_rev < MLX4_COMMAND_INTERFACE_NEW_PORT_CMDS)
6275a2cc190SJeff Kirsher 		dev->flags |= MLX4_FLAG_OLD_PORT_CMDS;
6285a2cc190SJeff Kirsher 
6295a2cc190SJeff Kirsher 	MLX4_GET(lg, outbox, QUERY_FW_MAX_CMD_OFFSET);
6305a2cc190SJeff Kirsher 	cmd->max_cmds = 1 << lg;
6315a2cc190SJeff Kirsher 
6325a2cc190SJeff Kirsher 	mlx4_dbg(dev, "FW version %d.%d.%03d (cmd intf rev %d), max commands %d\n",
6335a2cc190SJeff Kirsher 		 (int) (dev->caps.fw_ver >> 32),
6345a2cc190SJeff Kirsher 		 (int) (dev->caps.fw_ver >> 16) & 0xffff,
6355a2cc190SJeff Kirsher 		 (int) dev->caps.fw_ver & 0xffff,
6365a2cc190SJeff Kirsher 		 cmd_if_rev, cmd->max_cmds);
6375a2cc190SJeff Kirsher 
6385a2cc190SJeff Kirsher 	MLX4_GET(fw->catas_offset, outbox, QUERY_FW_ERR_START_OFFSET);
6395a2cc190SJeff Kirsher 	MLX4_GET(fw->catas_size,   outbox, QUERY_FW_ERR_SIZE_OFFSET);
6405a2cc190SJeff Kirsher 	MLX4_GET(fw->catas_bar,    outbox, QUERY_FW_ERR_BAR_OFFSET);
6415a2cc190SJeff Kirsher 	fw->catas_bar = (fw->catas_bar >> 6) * 2;
6425a2cc190SJeff Kirsher 
6435a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Catastrophic error buffer at 0x%llx, size 0x%x, BAR %d\n",
6445a2cc190SJeff Kirsher 		 (unsigned long long) fw->catas_offset, fw->catas_size, fw->catas_bar);
6455a2cc190SJeff Kirsher 
6465a2cc190SJeff Kirsher 	MLX4_GET(fw->fw_pages,     outbox, QUERY_FW_SIZE_OFFSET);
6475a2cc190SJeff Kirsher 	MLX4_GET(fw->clr_int_base, outbox, QUERY_FW_CLR_INT_BASE_OFFSET);
6485a2cc190SJeff Kirsher 	MLX4_GET(fw->clr_int_bar,  outbox, QUERY_FW_CLR_INT_BAR_OFFSET);
6495a2cc190SJeff Kirsher 	fw->clr_int_bar = (fw->clr_int_bar >> 6) * 2;
6505a2cc190SJeff Kirsher 
6515a2cc190SJeff Kirsher 	mlx4_dbg(dev, "FW size %d KB\n", fw->fw_pages >> 2);
6525a2cc190SJeff Kirsher 
6535a2cc190SJeff Kirsher 	/*
6545a2cc190SJeff Kirsher 	 * Round up number of system pages needed in case
6555a2cc190SJeff Kirsher 	 * MLX4_ICM_PAGE_SIZE < PAGE_SIZE.
6565a2cc190SJeff Kirsher 	 */
6575a2cc190SJeff Kirsher 	fw->fw_pages =
6585a2cc190SJeff Kirsher 		ALIGN(fw->fw_pages, PAGE_SIZE / MLX4_ICM_PAGE_SIZE) >>
6595a2cc190SJeff Kirsher 		(PAGE_SHIFT - MLX4_ICM_PAGE_SHIFT);
6605a2cc190SJeff Kirsher 
6615a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Clear int @ %llx, BAR %d\n",
6625a2cc190SJeff Kirsher 		 (unsigned long long) fw->clr_int_base, fw->clr_int_bar);
6635a2cc190SJeff Kirsher 
6645a2cc190SJeff Kirsher out:
6655a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
6665a2cc190SJeff Kirsher 	return err;
6675a2cc190SJeff Kirsher }
6685a2cc190SJeff Kirsher 
6695a2cc190SJeff Kirsher static void get_board_id(void *vsd, char *board_id)
6705a2cc190SJeff Kirsher {
6715a2cc190SJeff Kirsher 	int i;
6725a2cc190SJeff Kirsher 
6735a2cc190SJeff Kirsher #define VSD_OFFSET_SIG1		0x00
6745a2cc190SJeff Kirsher #define VSD_OFFSET_SIG2		0xde
6755a2cc190SJeff Kirsher #define VSD_OFFSET_MLX_BOARD_ID	0xd0
6765a2cc190SJeff Kirsher #define VSD_OFFSET_TS_BOARD_ID	0x20
6775a2cc190SJeff Kirsher 
6785a2cc190SJeff Kirsher #define VSD_SIGNATURE_TOPSPIN	0x5ad
6795a2cc190SJeff Kirsher 
6805a2cc190SJeff Kirsher 	memset(board_id, 0, MLX4_BOARD_ID_LEN);
6815a2cc190SJeff Kirsher 
6825a2cc190SJeff Kirsher 	if (be16_to_cpup(vsd + VSD_OFFSET_SIG1) == VSD_SIGNATURE_TOPSPIN &&
6835a2cc190SJeff Kirsher 	    be16_to_cpup(vsd + VSD_OFFSET_SIG2) == VSD_SIGNATURE_TOPSPIN) {
6845a2cc190SJeff Kirsher 		strlcpy(board_id, vsd + VSD_OFFSET_TS_BOARD_ID, MLX4_BOARD_ID_LEN);
6855a2cc190SJeff Kirsher 	} else {
6865a2cc190SJeff Kirsher 		/*
6875a2cc190SJeff Kirsher 		 * The board ID is a string but the firmware byte
6885a2cc190SJeff Kirsher 		 * swaps each 4-byte word before passing it back to
6895a2cc190SJeff Kirsher 		 * us.  Therefore we need to swab it before printing.
6905a2cc190SJeff Kirsher 		 */
6915a2cc190SJeff Kirsher 		for (i = 0; i < 4; ++i)
6925a2cc190SJeff Kirsher 			((u32 *) board_id)[i] =
6935a2cc190SJeff Kirsher 				swab32(*(u32 *) (vsd + VSD_OFFSET_MLX_BOARD_ID + i * 4));
6945a2cc190SJeff Kirsher 	}
6955a2cc190SJeff Kirsher }
6965a2cc190SJeff Kirsher 
6975a2cc190SJeff Kirsher int mlx4_QUERY_ADAPTER(struct mlx4_dev *dev, struct mlx4_adapter *adapter)
6985a2cc190SJeff Kirsher {
6995a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
7005a2cc190SJeff Kirsher 	u32 *outbox;
7015a2cc190SJeff Kirsher 	int err;
7025a2cc190SJeff Kirsher 
7035a2cc190SJeff Kirsher #define QUERY_ADAPTER_OUT_SIZE             0x100
7045a2cc190SJeff Kirsher #define QUERY_ADAPTER_INTA_PIN_OFFSET      0x10
7055a2cc190SJeff Kirsher #define QUERY_ADAPTER_VSD_OFFSET           0x20
7065a2cc190SJeff Kirsher 
7075a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
7085a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
7095a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
7105a2cc190SJeff Kirsher 	outbox = mailbox->buf;
7115a2cc190SJeff Kirsher 
7125a2cc190SJeff Kirsher 	err = mlx4_cmd_box(dev, 0, mailbox->dma, 0, 0, MLX4_CMD_QUERY_ADAPTER,
7135a2cc190SJeff Kirsher 			   MLX4_CMD_TIME_CLASS_A);
7145a2cc190SJeff Kirsher 	if (err)
7155a2cc190SJeff Kirsher 		goto out;
7165a2cc190SJeff Kirsher 
7175a2cc190SJeff Kirsher 	MLX4_GET(adapter->inta_pin, outbox,    QUERY_ADAPTER_INTA_PIN_OFFSET);
7185a2cc190SJeff Kirsher 
7195a2cc190SJeff Kirsher 	get_board_id(outbox + QUERY_ADAPTER_VSD_OFFSET / 4,
7205a2cc190SJeff Kirsher 		     adapter->board_id);
7215a2cc190SJeff Kirsher 
7225a2cc190SJeff Kirsher out:
7235a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
7245a2cc190SJeff Kirsher 	return err;
7255a2cc190SJeff Kirsher }
7265a2cc190SJeff Kirsher 
7275a2cc190SJeff Kirsher int mlx4_INIT_HCA(struct mlx4_dev *dev, struct mlx4_init_hca_param *param)
7285a2cc190SJeff Kirsher {
7295a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
7305a2cc190SJeff Kirsher 	__be32 *inbox;
7315a2cc190SJeff Kirsher 	int err;
7325a2cc190SJeff Kirsher 
7335a2cc190SJeff Kirsher #define INIT_HCA_IN_SIZE		 0x200
7345a2cc190SJeff Kirsher #define INIT_HCA_VERSION_OFFSET		 0x000
7355a2cc190SJeff Kirsher #define	 INIT_HCA_VERSION		 2
7365a2cc190SJeff Kirsher #define INIT_HCA_CACHELINE_SZ_OFFSET	 0x0e
7375a2cc190SJeff Kirsher #define INIT_HCA_FLAGS_OFFSET		 0x014
7385a2cc190SJeff Kirsher #define INIT_HCA_QPC_OFFSET		 0x020
7395a2cc190SJeff Kirsher #define	 INIT_HCA_QPC_BASE_OFFSET	 (INIT_HCA_QPC_OFFSET + 0x10)
7405a2cc190SJeff Kirsher #define	 INIT_HCA_LOG_QP_OFFSET		 (INIT_HCA_QPC_OFFSET + 0x17)
7415a2cc190SJeff Kirsher #define	 INIT_HCA_SRQC_BASE_OFFSET	 (INIT_HCA_QPC_OFFSET + 0x28)
7425a2cc190SJeff Kirsher #define	 INIT_HCA_LOG_SRQ_OFFSET	 (INIT_HCA_QPC_OFFSET + 0x2f)
7435a2cc190SJeff Kirsher #define	 INIT_HCA_CQC_BASE_OFFSET	 (INIT_HCA_QPC_OFFSET + 0x30)
7445a2cc190SJeff Kirsher #define	 INIT_HCA_LOG_CQ_OFFSET		 (INIT_HCA_QPC_OFFSET + 0x37)
7455a2cc190SJeff Kirsher #define	 INIT_HCA_ALTC_BASE_OFFSET	 (INIT_HCA_QPC_OFFSET + 0x40)
7465a2cc190SJeff Kirsher #define	 INIT_HCA_AUXC_BASE_OFFSET	 (INIT_HCA_QPC_OFFSET + 0x50)
7475a2cc190SJeff Kirsher #define	 INIT_HCA_EQC_BASE_OFFSET	 (INIT_HCA_QPC_OFFSET + 0x60)
7485a2cc190SJeff Kirsher #define	 INIT_HCA_LOG_EQ_OFFSET		 (INIT_HCA_QPC_OFFSET + 0x67)
7495a2cc190SJeff Kirsher #define	 INIT_HCA_RDMARC_BASE_OFFSET	 (INIT_HCA_QPC_OFFSET + 0x70)
7505a2cc190SJeff Kirsher #define	 INIT_HCA_LOG_RD_OFFSET		 (INIT_HCA_QPC_OFFSET + 0x77)
7515a2cc190SJeff Kirsher #define INIT_HCA_MCAST_OFFSET		 0x0c0
7525a2cc190SJeff Kirsher #define	 INIT_HCA_MC_BASE_OFFSET	 (INIT_HCA_MCAST_OFFSET + 0x00)
7535a2cc190SJeff Kirsher #define	 INIT_HCA_LOG_MC_ENTRY_SZ_OFFSET (INIT_HCA_MCAST_OFFSET + 0x12)
7545a2cc190SJeff Kirsher #define	 INIT_HCA_LOG_MC_HASH_SZ_OFFSET	 (INIT_HCA_MCAST_OFFSET + 0x16)
7555a2cc190SJeff Kirsher #define  INIT_HCA_UC_STEERING_OFFSET	 (INIT_HCA_MCAST_OFFSET + 0x18)
7565a2cc190SJeff Kirsher #define	 INIT_HCA_LOG_MC_TABLE_SZ_OFFSET (INIT_HCA_MCAST_OFFSET + 0x1b)
7575a2cc190SJeff Kirsher #define INIT_HCA_TPT_OFFSET		 0x0f0
7585a2cc190SJeff Kirsher #define	 INIT_HCA_DMPT_BASE_OFFSET	 (INIT_HCA_TPT_OFFSET + 0x00)
7595a2cc190SJeff Kirsher #define	 INIT_HCA_LOG_MPT_SZ_OFFSET	 (INIT_HCA_TPT_OFFSET + 0x0b)
7605a2cc190SJeff Kirsher #define	 INIT_HCA_MTT_BASE_OFFSET	 (INIT_HCA_TPT_OFFSET + 0x10)
7615a2cc190SJeff Kirsher #define	 INIT_HCA_CMPT_BASE_OFFSET	 (INIT_HCA_TPT_OFFSET + 0x18)
7625a2cc190SJeff Kirsher #define INIT_HCA_UAR_OFFSET		 0x120
7635a2cc190SJeff Kirsher #define	 INIT_HCA_LOG_UAR_SZ_OFFSET	 (INIT_HCA_UAR_OFFSET + 0x0a)
7645a2cc190SJeff Kirsher #define  INIT_HCA_UAR_PAGE_SZ_OFFSET     (INIT_HCA_UAR_OFFSET + 0x0b)
7655a2cc190SJeff Kirsher 
7665a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
7675a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
7685a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
7695a2cc190SJeff Kirsher 	inbox = mailbox->buf;
7705a2cc190SJeff Kirsher 
7715a2cc190SJeff Kirsher 	memset(inbox, 0, INIT_HCA_IN_SIZE);
7725a2cc190SJeff Kirsher 
7735a2cc190SJeff Kirsher 	*((u8 *) mailbox->buf + INIT_HCA_VERSION_OFFSET) = INIT_HCA_VERSION;
7745a2cc190SJeff Kirsher 
7755a2cc190SJeff Kirsher 	*((u8 *) mailbox->buf + INIT_HCA_CACHELINE_SZ_OFFSET) =
7765a2cc190SJeff Kirsher 		(ilog2(cache_line_size()) - 4) << 5;
7775a2cc190SJeff Kirsher 
7785a2cc190SJeff Kirsher #if defined(__LITTLE_ENDIAN)
7795a2cc190SJeff Kirsher 	*(inbox + INIT_HCA_FLAGS_OFFSET / 4) &= ~cpu_to_be32(1 << 1);
7805a2cc190SJeff Kirsher #elif defined(__BIG_ENDIAN)
7815a2cc190SJeff Kirsher 	*(inbox + INIT_HCA_FLAGS_OFFSET / 4) |= cpu_to_be32(1 << 1);
7825a2cc190SJeff Kirsher #else
7835a2cc190SJeff Kirsher #error Host endianness not defined
7845a2cc190SJeff Kirsher #endif
7855a2cc190SJeff Kirsher 	/* Check port for UD address vector: */
7865a2cc190SJeff Kirsher 	*(inbox + INIT_HCA_FLAGS_OFFSET / 4) |= cpu_to_be32(1);
7875a2cc190SJeff Kirsher 
7885a2cc190SJeff Kirsher 	/* Enable IPoIB checksumming if we can: */
7895a2cc190SJeff Kirsher 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
7905a2cc190SJeff Kirsher 		*(inbox + INIT_HCA_FLAGS_OFFSET / 4) |= cpu_to_be32(1 << 3);
7915a2cc190SJeff Kirsher 
7925a2cc190SJeff Kirsher 	/* Enable QoS support if module parameter set */
7935a2cc190SJeff Kirsher 	if (enable_qos)
7945a2cc190SJeff Kirsher 		*(inbox + INIT_HCA_FLAGS_OFFSET / 4) |= cpu_to_be32(1 << 2);
7955a2cc190SJeff Kirsher 
7965a2cc190SJeff Kirsher 	/* enable counters */
7975a2cc190SJeff Kirsher 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS)
7985a2cc190SJeff Kirsher 		*(inbox + INIT_HCA_FLAGS_OFFSET / 4) |= cpu_to_be32(1 << 4);
7995a2cc190SJeff Kirsher 
8005a2cc190SJeff Kirsher 	/* QPC/EEC/CQC/EQC/RDMARC attributes */
8015a2cc190SJeff Kirsher 
8025a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->qpc_base,      INIT_HCA_QPC_BASE_OFFSET);
8035a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->log_num_qps,   INIT_HCA_LOG_QP_OFFSET);
8045a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->srqc_base,     INIT_HCA_SRQC_BASE_OFFSET);
8055a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->log_num_srqs,  INIT_HCA_LOG_SRQ_OFFSET);
8065a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->cqc_base,      INIT_HCA_CQC_BASE_OFFSET);
8075a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->log_num_cqs,   INIT_HCA_LOG_CQ_OFFSET);
8085a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->altc_base,     INIT_HCA_ALTC_BASE_OFFSET);
8095a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->auxc_base,     INIT_HCA_AUXC_BASE_OFFSET);
8105a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->eqc_base,      INIT_HCA_EQC_BASE_OFFSET);
8115a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->log_num_eqs,   INIT_HCA_LOG_EQ_OFFSET);
8125a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->rdmarc_base,   INIT_HCA_RDMARC_BASE_OFFSET);
8135a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->log_rd_per_qp, INIT_HCA_LOG_RD_OFFSET);
8145a2cc190SJeff Kirsher 
8155a2cc190SJeff Kirsher 	/* multicast attributes */
8165a2cc190SJeff Kirsher 
8175a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->mc_base,		INIT_HCA_MC_BASE_OFFSET);
8185a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->log_mc_entry_sz, INIT_HCA_LOG_MC_ENTRY_SZ_OFFSET);
8195a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->log_mc_hash_sz,  INIT_HCA_LOG_MC_HASH_SZ_OFFSET);
8205a2cc190SJeff Kirsher 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER)
8215a2cc190SJeff Kirsher 		MLX4_PUT(inbox, (u8) (1 << 3),	INIT_HCA_UC_STEERING_OFFSET);
8225a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->log_mc_table_sz, INIT_HCA_LOG_MC_TABLE_SZ_OFFSET);
8235a2cc190SJeff Kirsher 
8245a2cc190SJeff Kirsher 	/* TPT attributes */
8255a2cc190SJeff Kirsher 
8265a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->dmpt_base,  INIT_HCA_DMPT_BASE_OFFSET);
8275a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->log_mpt_sz, INIT_HCA_LOG_MPT_SZ_OFFSET);
8285a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->mtt_base,   INIT_HCA_MTT_BASE_OFFSET);
8295a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->cmpt_base,  INIT_HCA_CMPT_BASE_OFFSET);
8305a2cc190SJeff Kirsher 
8315a2cc190SJeff Kirsher 	/* UAR attributes */
8325a2cc190SJeff Kirsher 
8335a2cc190SJeff Kirsher 	MLX4_PUT(inbox, (u8) (PAGE_SHIFT - 12), INIT_HCA_UAR_PAGE_SZ_OFFSET);
8345a2cc190SJeff Kirsher 	MLX4_PUT(inbox, param->log_uar_sz,      INIT_HCA_LOG_UAR_SZ_OFFSET);
8355a2cc190SJeff Kirsher 
8365a2cc190SJeff Kirsher 	err = mlx4_cmd(dev, mailbox->dma, 0, 0, MLX4_CMD_INIT_HCA, 10000);
8375a2cc190SJeff Kirsher 
8385a2cc190SJeff Kirsher 	if (err)
8395a2cc190SJeff Kirsher 		mlx4_err(dev, "INIT_HCA returns %d\n", err);
8405a2cc190SJeff Kirsher 
8415a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
8425a2cc190SJeff Kirsher 	return err;
8435a2cc190SJeff Kirsher }
8445a2cc190SJeff Kirsher 
8455a2cc190SJeff Kirsher int mlx4_INIT_PORT(struct mlx4_dev *dev, int port)
8465a2cc190SJeff Kirsher {
8475a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
8485a2cc190SJeff Kirsher 	u32 *inbox;
8495a2cc190SJeff Kirsher 	int err;
8505a2cc190SJeff Kirsher 	u32 flags;
8515a2cc190SJeff Kirsher 	u16 field;
8525a2cc190SJeff Kirsher 
8535a2cc190SJeff Kirsher 	if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
8545a2cc190SJeff Kirsher #define INIT_PORT_IN_SIZE          256
8555a2cc190SJeff Kirsher #define INIT_PORT_FLAGS_OFFSET     0x00
8565a2cc190SJeff Kirsher #define INIT_PORT_FLAG_SIG         (1 << 18)
8575a2cc190SJeff Kirsher #define INIT_PORT_FLAG_NG          (1 << 17)
8585a2cc190SJeff Kirsher #define INIT_PORT_FLAG_G0          (1 << 16)
8595a2cc190SJeff Kirsher #define INIT_PORT_VL_SHIFT         4
8605a2cc190SJeff Kirsher #define INIT_PORT_PORT_WIDTH_SHIFT 8
8615a2cc190SJeff Kirsher #define INIT_PORT_MTU_OFFSET       0x04
8625a2cc190SJeff Kirsher #define INIT_PORT_MAX_GID_OFFSET   0x06
8635a2cc190SJeff Kirsher #define INIT_PORT_MAX_PKEY_OFFSET  0x0a
8645a2cc190SJeff Kirsher #define INIT_PORT_GUID0_OFFSET     0x10
8655a2cc190SJeff Kirsher #define INIT_PORT_NODE_GUID_OFFSET 0x18
8665a2cc190SJeff Kirsher #define INIT_PORT_SI_GUID_OFFSET   0x20
8675a2cc190SJeff Kirsher 
8685a2cc190SJeff Kirsher 		mailbox = mlx4_alloc_cmd_mailbox(dev);
8695a2cc190SJeff Kirsher 		if (IS_ERR(mailbox))
8705a2cc190SJeff Kirsher 			return PTR_ERR(mailbox);
8715a2cc190SJeff Kirsher 		inbox = mailbox->buf;
8725a2cc190SJeff Kirsher 
8735a2cc190SJeff Kirsher 		memset(inbox, 0, INIT_PORT_IN_SIZE);
8745a2cc190SJeff Kirsher 
8755a2cc190SJeff Kirsher 		flags = 0;
8765a2cc190SJeff Kirsher 		flags |= (dev->caps.vl_cap[port] & 0xf) << INIT_PORT_VL_SHIFT;
8775a2cc190SJeff Kirsher 		flags |= (dev->caps.port_width_cap[port] & 0xf) << INIT_PORT_PORT_WIDTH_SHIFT;
8785a2cc190SJeff Kirsher 		MLX4_PUT(inbox, flags,		  INIT_PORT_FLAGS_OFFSET);
8795a2cc190SJeff Kirsher 
8805a2cc190SJeff Kirsher 		field = 128 << dev->caps.ib_mtu_cap[port];
8815a2cc190SJeff Kirsher 		MLX4_PUT(inbox, field, INIT_PORT_MTU_OFFSET);
8825a2cc190SJeff Kirsher 		field = dev->caps.gid_table_len[port];
8835a2cc190SJeff Kirsher 		MLX4_PUT(inbox, field, INIT_PORT_MAX_GID_OFFSET);
8845a2cc190SJeff Kirsher 		field = dev->caps.pkey_table_len[port];
8855a2cc190SJeff Kirsher 		MLX4_PUT(inbox, field, INIT_PORT_MAX_PKEY_OFFSET);
8865a2cc190SJeff Kirsher 
8875a2cc190SJeff Kirsher 		err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_INIT_PORT,
8885a2cc190SJeff Kirsher 			       MLX4_CMD_TIME_CLASS_A);
8895a2cc190SJeff Kirsher 
8905a2cc190SJeff Kirsher 		mlx4_free_cmd_mailbox(dev, mailbox);
8915a2cc190SJeff Kirsher 	} else
8925a2cc190SJeff Kirsher 		err = mlx4_cmd(dev, 0, port, 0, MLX4_CMD_INIT_PORT,
8935a2cc190SJeff Kirsher 			       MLX4_CMD_TIME_CLASS_A);
8945a2cc190SJeff Kirsher 
8955a2cc190SJeff Kirsher 	return err;
8965a2cc190SJeff Kirsher }
8975a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_INIT_PORT);
8985a2cc190SJeff Kirsher 
8995a2cc190SJeff Kirsher int mlx4_CLOSE_PORT(struct mlx4_dev *dev, int port)
9005a2cc190SJeff Kirsher {
9015a2cc190SJeff Kirsher 	return mlx4_cmd(dev, 0, port, 0, MLX4_CMD_CLOSE_PORT, 1000);
9025a2cc190SJeff Kirsher }
9035a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_CLOSE_PORT);
9045a2cc190SJeff Kirsher 
9055a2cc190SJeff Kirsher int mlx4_CLOSE_HCA(struct mlx4_dev *dev, int panic)
9065a2cc190SJeff Kirsher {
9075a2cc190SJeff Kirsher 	return mlx4_cmd(dev, 0, 0, panic, MLX4_CMD_CLOSE_HCA, 1000);
9085a2cc190SJeff Kirsher }
9095a2cc190SJeff Kirsher 
9105a2cc190SJeff Kirsher int mlx4_SET_ICM_SIZE(struct mlx4_dev *dev, u64 icm_size, u64 *aux_pages)
9115a2cc190SJeff Kirsher {
9125a2cc190SJeff Kirsher 	int ret = mlx4_cmd_imm(dev, icm_size, aux_pages, 0, 0,
9135a2cc190SJeff Kirsher 			       MLX4_CMD_SET_ICM_SIZE,
9145a2cc190SJeff Kirsher 			       MLX4_CMD_TIME_CLASS_A);
9155a2cc190SJeff Kirsher 	if (ret)
9165a2cc190SJeff Kirsher 		return ret;
9175a2cc190SJeff Kirsher 
9185a2cc190SJeff Kirsher 	/*
9195a2cc190SJeff Kirsher 	 * Round up number of system pages needed in case
9205a2cc190SJeff Kirsher 	 * MLX4_ICM_PAGE_SIZE < PAGE_SIZE.
9215a2cc190SJeff Kirsher 	 */
9225a2cc190SJeff Kirsher 	*aux_pages = ALIGN(*aux_pages, PAGE_SIZE / MLX4_ICM_PAGE_SIZE) >>
9235a2cc190SJeff Kirsher 		(PAGE_SHIFT - MLX4_ICM_PAGE_SHIFT);
9245a2cc190SJeff Kirsher 
9255a2cc190SJeff Kirsher 	return 0;
9265a2cc190SJeff Kirsher }
9275a2cc190SJeff Kirsher 
9285a2cc190SJeff Kirsher int mlx4_NOP(struct mlx4_dev *dev)
9295a2cc190SJeff Kirsher {
9305a2cc190SJeff Kirsher 	/* Input modifier of 0x1f means "finish as soon as possible." */
9315a2cc190SJeff Kirsher 	return mlx4_cmd(dev, 0, 0x1f, 0, MLX4_CMD_NOP, 100);
9325a2cc190SJeff Kirsher }
9335a2cc190SJeff Kirsher 
9345a2cc190SJeff Kirsher #define MLX4_WOL_SETUP_MODE (5 << 28)
9355a2cc190SJeff Kirsher int mlx4_wol_read(struct mlx4_dev *dev, u64 *config, int port)
9365a2cc190SJeff Kirsher {
9375a2cc190SJeff Kirsher 	u32 in_mod = MLX4_WOL_SETUP_MODE | port << 8;
9385a2cc190SJeff Kirsher 
9395a2cc190SJeff Kirsher 	return mlx4_cmd_imm(dev, 0, config, in_mod, 0x3,
9405a2cc190SJeff Kirsher 			    MLX4_CMD_MOD_STAT_CFG, MLX4_CMD_TIME_CLASS_A);
9415a2cc190SJeff Kirsher }
9425a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_wol_read);
9435a2cc190SJeff Kirsher 
9445a2cc190SJeff Kirsher int mlx4_wol_write(struct mlx4_dev *dev, u64 config, int port)
9455a2cc190SJeff Kirsher {
9465a2cc190SJeff Kirsher 	u32 in_mod = MLX4_WOL_SETUP_MODE | port << 8;
9475a2cc190SJeff Kirsher 
9485a2cc190SJeff Kirsher 	return mlx4_cmd(dev, config, in_mod, 0x1, MLX4_CMD_MOD_STAT_CFG,
9495a2cc190SJeff Kirsher 					MLX4_CMD_TIME_CLASS_A);
9505a2cc190SJeff Kirsher }
9515a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_wol_write);
952