xref: /openbmc/linux/drivers/infiniband/hw/mthca/mthca_cmd.c (revision 1188f7f111c61394ec56beb8e30322305a8220b6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
32a1d9b7fSRoland Dreier  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
44885bf64SRoland Dreier  * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This software is available to you under a choice of one of two
71da177e4SLinus Torvalds  * licenses.  You may choose to be licensed under the terms of the GNU
81da177e4SLinus Torvalds  * General Public License (GPL) Version 2, available from the file
91da177e4SLinus Torvalds  * COPYING in the main directory of this source tree, or the
101da177e4SLinus Torvalds  * OpenIB.org BSD license below:
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *     Redistribution and use in source and binary forms, with or
131da177e4SLinus Torvalds  *     without modification, are permitted provided that the following
141da177e4SLinus Torvalds  *     conditions are met:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *      - Redistributions of source code must retain the above
171da177e4SLinus Torvalds  *        copyright notice, this list of conditions and the following
181da177e4SLinus Torvalds  *        disclaimer.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *      - Redistributions in binary form must reproduce the above
211da177e4SLinus Torvalds  *        copyright notice, this list of conditions and the following
221da177e4SLinus Torvalds  *        disclaimer in the documentation and/or other materials
231da177e4SLinus Torvalds  *        provided with the distribution.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
261da177e4SLinus Torvalds  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
271da177e4SLinus Torvalds  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
281da177e4SLinus Torvalds  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
291da177e4SLinus Torvalds  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
301da177e4SLinus Torvalds  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
311da177e4SLinus Torvalds  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
321da177e4SLinus Torvalds  * SOFTWARE.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
352a214182SJames Lentini #include <linux/completion.h>
361da177e4SLinus Torvalds #include <linux/pci.h>
371da177e4SLinus Torvalds #include <linux/errno.h>
38e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
39e4dd23d7SPaul Gortmaker #include <linux/module.h>
405a0e3ad6STejun Heo #include <linux/slab.h>
411da177e4SLinus Torvalds #include <asm/io.h>
42a4d61e84SRoland Dreier #include <rdma/ib_mad.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include "mthca_dev.h"
451da177e4SLinus Torvalds #include "mthca_config_reg.h"
461da177e4SLinus Torvalds #include "mthca_cmd.h"
471da177e4SLinus Torvalds #include "mthca_memfree.h"
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #define CMD_POLL_TOKEN 0xffff
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds enum {
521da177e4SLinus Torvalds 	HCR_IN_PARAM_OFFSET    = 0x00,
531da177e4SLinus Torvalds 	HCR_IN_MODIFIER_OFFSET = 0x08,
541da177e4SLinus Torvalds 	HCR_OUT_PARAM_OFFSET   = 0x0c,
551da177e4SLinus Torvalds 	HCR_TOKEN_OFFSET       = 0x14,
561da177e4SLinus Torvalds 	HCR_STATUS_OFFSET      = 0x18,
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	HCR_OPMOD_SHIFT        = 12,
591da177e4SLinus Torvalds 	HCA_E_BIT              = 22,
601da177e4SLinus Torvalds 	HCR_GO_BIT             = 23
611da177e4SLinus Torvalds };
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds enum {
641da177e4SLinus Torvalds 	/* initialization and general commands */
651da177e4SLinus Torvalds 	CMD_SYS_EN          = 0x1,
661da177e4SLinus Torvalds 	CMD_SYS_DIS         = 0x2,
671da177e4SLinus Torvalds 	CMD_MAP_FA          = 0xfff,
681da177e4SLinus Torvalds 	CMD_UNMAP_FA        = 0xffe,
691da177e4SLinus Torvalds 	CMD_RUN_FW          = 0xff6,
701da177e4SLinus Torvalds 	CMD_MOD_STAT_CFG    = 0x34,
711da177e4SLinus Torvalds 	CMD_QUERY_DEV_LIM   = 0x3,
721da177e4SLinus Torvalds 	CMD_QUERY_FW        = 0x4,
731da177e4SLinus Torvalds 	CMD_ENABLE_LAM      = 0xff8,
741da177e4SLinus Torvalds 	CMD_DISABLE_LAM     = 0xff7,
751da177e4SLinus Torvalds 	CMD_QUERY_DDR       = 0x5,
761da177e4SLinus Torvalds 	CMD_QUERY_ADAPTER   = 0x6,
771da177e4SLinus Torvalds 	CMD_INIT_HCA        = 0x7,
781da177e4SLinus Torvalds 	CMD_CLOSE_HCA       = 0x8,
791da177e4SLinus Torvalds 	CMD_INIT_IB         = 0x9,
801da177e4SLinus Torvalds 	CMD_CLOSE_IB        = 0xa,
811da177e4SLinus Torvalds 	CMD_QUERY_HCA       = 0xb,
821da177e4SLinus Torvalds 	CMD_SET_IB          = 0xc,
831da177e4SLinus Torvalds 	CMD_ACCESS_DDR      = 0x2e,
841da177e4SLinus Torvalds 	CMD_MAP_ICM         = 0xffa,
851da177e4SLinus Torvalds 	CMD_UNMAP_ICM       = 0xff9,
861da177e4SLinus Torvalds 	CMD_MAP_ICM_AUX     = 0xffc,
871da177e4SLinus Torvalds 	CMD_UNMAP_ICM_AUX   = 0xffb,
881da177e4SLinus Torvalds 	CMD_SET_ICM_SIZE    = 0xffd,
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/* TPT commands */
911da177e4SLinus Torvalds 	CMD_SW2HW_MPT 	    = 0xd,
921da177e4SLinus Torvalds 	CMD_QUERY_MPT 	    = 0xe,
931da177e4SLinus Torvalds 	CMD_HW2SW_MPT 	    = 0xf,
941da177e4SLinus Torvalds 	CMD_READ_MTT        = 0x10,
951da177e4SLinus Torvalds 	CMD_WRITE_MTT       = 0x11,
961da177e4SLinus Torvalds 	CMD_SYNC_TPT        = 0x2f,
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	/* EQ commands */
991da177e4SLinus Torvalds 	CMD_MAP_EQ          = 0x12,
1001da177e4SLinus Torvalds 	CMD_SW2HW_EQ 	    = 0x13,
1011da177e4SLinus Torvalds 	CMD_HW2SW_EQ 	    = 0x14,
1021da177e4SLinus Torvalds 	CMD_QUERY_EQ        = 0x15,
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	/* CQ commands */
1051da177e4SLinus Torvalds 	CMD_SW2HW_CQ 	    = 0x16,
1061da177e4SLinus Torvalds 	CMD_HW2SW_CQ 	    = 0x17,
1071da177e4SLinus Torvalds 	CMD_QUERY_CQ 	    = 0x18,
1081da177e4SLinus Torvalds 	CMD_RESIZE_CQ       = 0x2c,
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	/* SRQ commands */
1111da177e4SLinus Torvalds 	CMD_SW2HW_SRQ 	    = 0x35,
1121da177e4SLinus Torvalds 	CMD_HW2SW_SRQ 	    = 0x36,
1131da177e4SLinus Torvalds 	CMD_QUERY_SRQ       = 0x37,
114ec34a922SRoland Dreier 	CMD_ARM_SRQ         = 0x40,
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	/* QP/EE commands */
1171da177e4SLinus Torvalds 	CMD_RST2INIT_QPEE   = 0x19,
1181da177e4SLinus Torvalds 	CMD_INIT2RTR_QPEE   = 0x1a,
1191da177e4SLinus Torvalds 	CMD_RTR2RTS_QPEE    = 0x1b,
1201da177e4SLinus Torvalds 	CMD_RTS2RTS_QPEE    = 0x1c,
1211da177e4SLinus Torvalds 	CMD_SQERR2RTS_QPEE  = 0x1d,
1221da177e4SLinus Torvalds 	CMD_2ERR_QPEE       = 0x1e,
1231da177e4SLinus Torvalds 	CMD_RTS2SQD_QPEE    = 0x1f,
1241da177e4SLinus Torvalds 	CMD_SQD2SQD_QPEE    = 0x38,
1251da177e4SLinus Torvalds 	CMD_SQD2RTS_QPEE    = 0x20,
1261da177e4SLinus Torvalds 	CMD_ERR2RST_QPEE    = 0x21,
1271da177e4SLinus Torvalds 	CMD_QUERY_QPEE      = 0x22,
1281da177e4SLinus Torvalds 	CMD_INIT2INIT_QPEE  = 0x2d,
1291da177e4SLinus Torvalds 	CMD_SUSPEND_QPEE    = 0x32,
1301da177e4SLinus Torvalds 	CMD_UNSUSPEND_QPEE  = 0x33,
1311da177e4SLinus Torvalds 	/* special QPs and management commands */
1321da177e4SLinus Torvalds 	CMD_CONF_SPECIAL_QP = 0x23,
1331da177e4SLinus Torvalds 	CMD_MAD_IFC         = 0x24,
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	/* multicast commands */
1361da177e4SLinus Torvalds 	CMD_READ_MGM        = 0x25,
1371da177e4SLinus Torvalds 	CMD_WRITE_MGM       = 0x26,
1381da177e4SLinus Torvalds 	CMD_MGID_HASH       = 0x27,
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	/* miscellaneous commands */
1411da177e4SLinus Torvalds 	CMD_DIAG_RPRT       = 0x30,
1421da177e4SLinus Torvalds 	CMD_NOP             = 0x31,
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	/* debug commands */
1451da177e4SLinus Torvalds 	CMD_QUERY_DEBUG_MSG = 0x2a,
1461da177e4SLinus Torvalds 	CMD_SET_DEBUG_MSG   = 0x2b,
1471da177e4SLinus Torvalds };
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /*
1501da177e4SLinus Torvalds  * According to Mellanox code, FW may be starved and never complete
1511da177e4SLinus Torvalds  * commands.  So we can't use strict timeouts described in PRM -- we
1521da177e4SLinus Torvalds  * just arbitrarily select 60 seconds for now.
1531da177e4SLinus Torvalds  */
1541da177e4SLinus Torvalds #if 0
1551da177e4SLinus Torvalds /*
1561da177e4SLinus Torvalds  * Round up and add 1 to make sure we get the full wait time (since we
1571da177e4SLinus Torvalds  * will be starting in the middle of a jiffy)
1581da177e4SLinus Torvalds  */
1591da177e4SLinus Torvalds enum {
1601da177e4SLinus Torvalds 	CMD_TIME_CLASS_A = (HZ + 999) / 1000 + 1,
1611da177e4SLinus Torvalds 	CMD_TIME_CLASS_B = (HZ +  99) /  100 + 1,
1628531f1f1SJack Morgenstein 	CMD_TIME_CLASS_C = (HZ +   9) /   10 + 1,
1638531f1f1SJack Morgenstein 	CMD_TIME_CLASS_D = 60 * HZ
1641da177e4SLinus Torvalds };
1651da177e4SLinus Torvalds #else
1661da177e4SLinus Torvalds enum {
1671da177e4SLinus Torvalds 	CMD_TIME_CLASS_A = 60 * HZ,
1681da177e4SLinus Torvalds 	CMD_TIME_CLASS_B = 60 * HZ,
1698531f1f1SJack Morgenstein 	CMD_TIME_CLASS_C = 60 * HZ,
1708531f1f1SJack Morgenstein 	CMD_TIME_CLASS_D = 60 * HZ
1711da177e4SLinus Torvalds };
1721da177e4SLinus Torvalds #endif
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds enum {
1751da177e4SLinus Torvalds 	GO_BIT_TIMEOUT = HZ * 10
1761da177e4SLinus Torvalds };
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds struct mthca_cmd_context {
1791da177e4SLinus Torvalds 	struct completion done;
1801da177e4SLinus Torvalds 	int               result;
1811da177e4SLinus Torvalds 	int               next;
1821da177e4SLinus Torvalds 	u64               out_param;
1831da177e4SLinus Torvalds 	u16               token;
1841da177e4SLinus Torvalds 	u8                status;
1851da177e4SLinus Torvalds };
1861da177e4SLinus Torvalds 
1871db76c14SRoland Dreier static int fw_cmd_doorbell = 0;
18814abdffcSEli Cohen module_param(fw_cmd_doorbell, int, 0644);
18914abdffcSEli Cohen MODULE_PARM_DESC(fw_cmd_doorbell, "post FW commands through doorbell page if nonzero "
19014abdffcSEli Cohen 		 "(and supported by FW)");
19114abdffcSEli Cohen 
go_bit(struct mthca_dev * dev)1921da177e4SLinus Torvalds static inline int go_bit(struct mthca_dev *dev)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	return readl(dev->hcr + HCR_STATUS_OFFSET) &
1951da177e4SLinus Torvalds 		swab32(1 << HCR_GO_BIT);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
mthca_cmd_post_dbell(struct mthca_dev * dev,u64 in_param,u64 out_param,u32 in_modifier,u8 op_modifier,u16 op,u16 token)19814abdffcSEli Cohen static void mthca_cmd_post_dbell(struct mthca_dev *dev,
19914abdffcSEli Cohen 				 u64 in_param,
20014abdffcSEli Cohen 				 u64 out_param,
20114abdffcSEli Cohen 				 u32 in_modifier,
20214abdffcSEli Cohen 				 u8 op_modifier,
20314abdffcSEli Cohen 				 u16 op,
20414abdffcSEli Cohen 				 u16 token)
20514abdffcSEli Cohen {
20614abdffcSEli Cohen 	void __iomem *ptr = dev->cmd.dbell_map;
20714abdffcSEli Cohen 	u16 *offs = dev->cmd.dbell_offsets;
20814abdffcSEli Cohen 
20914abdffcSEli Cohen 	__raw_writel((__force u32) cpu_to_be32(in_param >> 32),           ptr + offs[0]);
21014abdffcSEli Cohen 	wmb();
21114abdffcSEli Cohen 	__raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful),  ptr + offs[1]);
21214abdffcSEli Cohen 	wmb();
21314abdffcSEli Cohen 	__raw_writel((__force u32) cpu_to_be32(in_modifier),              ptr + offs[2]);
21414abdffcSEli Cohen 	wmb();
21514abdffcSEli Cohen 	__raw_writel((__force u32) cpu_to_be32(out_param >> 32),          ptr + offs[3]);
21614abdffcSEli Cohen 	wmb();
21714abdffcSEli Cohen 	__raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), ptr + offs[4]);
21814abdffcSEli Cohen 	wmb();
21914abdffcSEli Cohen 	__raw_writel((__force u32) cpu_to_be32(token << 16),              ptr + offs[5]);
22014abdffcSEli Cohen 	wmb();
22114abdffcSEli Cohen 	__raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT)                |
22214abdffcSEli Cohen 					       (1 << HCA_E_BIT)                 |
22314abdffcSEli Cohen 					       (op_modifier << HCR_OPMOD_SHIFT) |
22414abdffcSEli Cohen 						op),			  ptr + offs[6]);
22514abdffcSEli Cohen 	wmb();
22614abdffcSEli Cohen 	__raw_writel((__force u32) 0,                                     ptr + offs[7]);
22714abdffcSEli Cohen 	wmb();
22814abdffcSEli Cohen }
22914abdffcSEli Cohen 
mthca_cmd_post_hcr(struct mthca_dev * dev,u64 in_param,u64 out_param,u32 in_modifier,u8 op_modifier,u16 op,u16 token,int event)23014abdffcSEli Cohen static int mthca_cmd_post_hcr(struct mthca_dev *dev,
2311da177e4SLinus Torvalds 			      u64 in_param,
2321da177e4SLinus Torvalds 			      u64 out_param,
2331da177e4SLinus Torvalds 			      u32 in_modifier,
2341da177e4SLinus Torvalds 			      u8 op_modifier,
2351da177e4SLinus Torvalds 			      u16 op,
2361da177e4SLinus Torvalds 			      u16 token,
2371da177e4SLinus Torvalds 			      int event)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	if (event) {
2401da177e4SLinus Torvalds 		unsigned long end = jiffies + GO_BIT_TIMEOUT;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 		while (go_bit(dev) && time_before(jiffies, end)) {
2431da177e4SLinus Torvalds 			set_current_state(TASK_RUNNING);
2441da177e4SLinus Torvalds 			schedule();
2451da177e4SLinus Torvalds 		}
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds 
24814abdffcSEli Cohen 	if (go_bit(dev))
24914abdffcSEli Cohen 		return -EAGAIN;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	/*
2521da177e4SLinus Torvalds 	 * We use writel (instead of something like memcpy_toio)
2531da177e4SLinus Torvalds 	 * because writes of less than 32 bits to the HCR don't work
2541da177e4SLinus Torvalds 	 * (and some architectures such as ia64 implement memcpy_toio
2551da177e4SLinus Torvalds 	 * in terms of writeb).
2561da177e4SLinus Torvalds 	 */
25797f52eb4SSean Hefty 	__raw_writel((__force u32) cpu_to_be32(in_param >> 32),           dev->hcr + 0 * 4);
25897f52eb4SSean Hefty 	__raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful),  dev->hcr + 1 * 4);
25997f52eb4SSean Hefty 	__raw_writel((__force u32) cpu_to_be32(in_modifier),              dev->hcr + 2 * 4);
26097f52eb4SSean Hefty 	__raw_writel((__force u32) cpu_to_be32(out_param >> 32),          dev->hcr + 3 * 4);
26197f52eb4SSean Hefty 	__raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), dev->hcr + 4 * 4);
26297f52eb4SSean Hefty 	__raw_writel((__force u32) cpu_to_be32(token << 16),              dev->hcr + 5 * 4);
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	/* __raw_writel may not order writes. */
2651da177e4SLinus Torvalds 	wmb();
2661da177e4SLinus Torvalds 
26797f52eb4SSean Hefty 	__raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT)                |
2681da177e4SLinus Torvalds 					       (event ? (1 << HCA_E_BIT) : 0)   |
2691da177e4SLinus Torvalds 					       (op_modifier << HCR_OPMOD_SHIFT) |
2701da177e4SLinus Torvalds 					       op),                       dev->hcr + 6 * 4);
2711da177e4SLinus Torvalds 
27214abdffcSEli Cohen 	return 0;
27314abdffcSEli Cohen }
27414abdffcSEli Cohen 
mthca_cmd_post(struct mthca_dev * dev,u64 in_param,u64 out_param,u32 in_modifier,u8 op_modifier,u16 op,u16 token,int event)27514abdffcSEli Cohen static int mthca_cmd_post(struct mthca_dev *dev,
27614abdffcSEli Cohen 			  u64 in_param,
27714abdffcSEli Cohen 			  u64 out_param,
27814abdffcSEli Cohen 			  u32 in_modifier,
27914abdffcSEli Cohen 			  u8 op_modifier,
28014abdffcSEli Cohen 			  u16 op,
28114abdffcSEli Cohen 			  u16 token,
28214abdffcSEli Cohen 			  int event)
28314abdffcSEli Cohen {
28414abdffcSEli Cohen 	int err = 0;
28514abdffcSEli Cohen 
28614abdffcSEli Cohen 	mutex_lock(&dev->cmd.hcr_mutex);
28714abdffcSEli Cohen 
28814abdffcSEli Cohen 	if (event && dev->cmd.flags & MTHCA_CMD_POST_DOORBELLS && fw_cmd_doorbell)
28914abdffcSEli Cohen 		mthca_cmd_post_dbell(dev, in_param, out_param, in_modifier,
29014abdffcSEli Cohen 					   op_modifier, op, token);
29114abdffcSEli Cohen 	else
29214abdffcSEli Cohen 		err = mthca_cmd_post_hcr(dev, in_param, out_param, in_modifier,
29314abdffcSEli Cohen 					 op_modifier, op, token, event);
29414abdffcSEli Cohen 
295fd9cfdd1SRoland Dreier 	mutex_unlock(&dev->cmd.hcr_mutex);
2961da177e4SLinus Torvalds 	return err;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
299cdb73db0SGoldwyn Rodrigues 
mthca_status_to_errno(u8 status)300cdb73db0SGoldwyn Rodrigues static int mthca_status_to_errno(u8 status)
301cdb73db0SGoldwyn Rodrigues {
302cdb73db0SGoldwyn Rodrigues 	static const int trans_table[] = {
303cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_INTERNAL_ERR]   = -EIO,
304cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_BAD_OP]         = -EPERM,
305cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_BAD_PARAM]      = -EINVAL,
306cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_BAD_SYS_STATE]  = -ENXIO,
307cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_BAD_RESOURCE]   = -EBADF,
308cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_RESOURCE_BUSY]  = -EBUSY,
309cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_DDR_MEM_ERR]    = -ENOMEM,
310cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_EXCEED_LIM]     = -ENOMEM,
311cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_BAD_RES_STATE]  = -EBADF,
312cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_BAD_INDEX]      = -EBADF,
313cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_BAD_NVMEM]      = -EFAULT,
314cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_BAD_QPEE_STATE] = -EINVAL,
315cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_BAD_SEG_PARAM]  = -EFAULT,
316cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_REG_BOUND]      = -EBUSY,
317cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_LAM_NOT_PRE]    = -EAGAIN,
318cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_BAD_PKT]        = -EBADMSG,
319cdb73db0SGoldwyn Rodrigues 		[MTHCA_CMD_STAT_BAD_SIZE]       = -ENOMEM,
320cdb73db0SGoldwyn Rodrigues 	};
321cdb73db0SGoldwyn Rodrigues 
322cdb73db0SGoldwyn Rodrigues 	if (status >= ARRAY_SIZE(trans_table) ||
323cdb73db0SGoldwyn Rodrigues 			(status != MTHCA_CMD_STAT_OK
324cdb73db0SGoldwyn Rodrigues 			 && trans_table[status] == 0))
325cdb73db0SGoldwyn Rodrigues 		return -EINVAL;
326cdb73db0SGoldwyn Rodrigues 
327cdb73db0SGoldwyn Rodrigues 	return trans_table[status];
328cdb73db0SGoldwyn Rodrigues }
329cdb73db0SGoldwyn Rodrigues 
330cdb73db0SGoldwyn Rodrigues 
mthca_cmd_poll(struct mthca_dev * dev,u64 in_param,u64 * out_param,int out_is_imm,u32 in_modifier,u8 op_modifier,u16 op,unsigned long timeout)3311da177e4SLinus Torvalds static int mthca_cmd_poll(struct mthca_dev *dev,
3321da177e4SLinus Torvalds 			  u64 in_param,
3331da177e4SLinus Torvalds 			  u64 *out_param,
3341da177e4SLinus Torvalds 			  int out_is_imm,
3351da177e4SLinus Torvalds 			  u32 in_modifier,
3361da177e4SLinus Torvalds 			  u8 op_modifier,
3371da177e4SLinus Torvalds 			  u16 op,
338cdb73db0SGoldwyn Rodrigues 			  unsigned long timeout)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	int err = 0;
3411da177e4SLinus Torvalds 	unsigned long end;
342cdb73db0SGoldwyn Rodrigues 	u8 status;
3431da177e4SLinus Torvalds 
344e3aa31c5SMichael S. Tsirkin 	down(&dev->cmd.poll_sem);
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	err = mthca_cmd_post(dev, in_param,
3471da177e4SLinus Torvalds 			     out_param ? *out_param : 0,
3481da177e4SLinus Torvalds 			     in_modifier, op_modifier,
3491da177e4SLinus Torvalds 			     op, CMD_POLL_TOKEN, 0);
3501da177e4SLinus Torvalds 	if (err)
3511da177e4SLinus Torvalds 		goto out;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	end = timeout + jiffies;
3541da177e4SLinus Torvalds 	while (go_bit(dev) && time_before(jiffies, end)) {
3551da177e4SLinus Torvalds 		set_current_state(TASK_RUNNING);
3561da177e4SLinus Torvalds 		schedule();
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	if (go_bit(dev)) {
3601da177e4SLinus Torvalds 		err = -EBUSY;
3611da177e4SLinus Torvalds 		goto out;
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds 
364a43402afSLeon Romanovsky 	if (out_is_imm && out_param) {
36597f52eb4SSean Hefty 		*out_param =
36697f52eb4SSean Hefty 			(u64) be32_to_cpu((__force __be32)
36797f52eb4SSean Hefty 					  __raw_readl(dev->hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
36897f52eb4SSean Hefty 			(u64) be32_to_cpu((__force __be32)
36997f52eb4SSean Hefty 					  __raw_readl(dev->hcr + HCR_OUT_PARAM_OFFSET + 4));
370a43402afSLeon Romanovsky 	} else if (out_is_imm) {
371a43402afSLeon Romanovsky 		err = -EINVAL;
372a43402afSLeon Romanovsky 		goto out;
373a43402afSLeon Romanovsky 	}
3741da177e4SLinus Torvalds 
375cdb73db0SGoldwyn Rodrigues 	status = be32_to_cpu((__force __be32) __raw_readl(dev->hcr + HCR_STATUS_OFFSET)) >> 24;
376cdb73db0SGoldwyn Rodrigues 	if (status) {
377cdb73db0SGoldwyn Rodrigues 		mthca_dbg(dev, "Command %02x completed with status %02x\n",
378cdb73db0SGoldwyn Rodrigues 			  op, status);
379cdb73db0SGoldwyn Rodrigues 		err = mthca_status_to_errno(status);
380cdb73db0SGoldwyn Rodrigues 	}
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds out:
3831da177e4SLinus Torvalds 	up(&dev->cmd.poll_sem);
3841da177e4SLinus Torvalds 	return err;
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
mthca_cmd_event(struct mthca_dev * dev,u16 token,u8 status,u64 out_param)3871da177e4SLinus Torvalds void mthca_cmd_event(struct mthca_dev *dev,
3881da177e4SLinus Torvalds 		     u16 token,
3891da177e4SLinus Torvalds 		     u8  status,
3901da177e4SLinus Torvalds 		     u64 out_param)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 	struct mthca_cmd_context *context =
3931da177e4SLinus Torvalds 		&dev->cmd.context[token & dev->cmd.token_mask];
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	/* previously timed out command completing at long last */
3961da177e4SLinus Torvalds 	if (token != context->token)
3971da177e4SLinus Torvalds 		return;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	context->result    = 0;
4001da177e4SLinus Torvalds 	context->status    = status;
4011da177e4SLinus Torvalds 	context->out_param = out_param;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	complete(&context->done);
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
mthca_cmd_wait(struct mthca_dev * dev,u64 in_param,u64 * out_param,int out_is_imm,u32 in_modifier,u8 op_modifier,u16 op,unsigned long timeout)4061da177e4SLinus Torvalds static int mthca_cmd_wait(struct mthca_dev *dev,
4071da177e4SLinus Torvalds 			  u64 in_param,
4081da177e4SLinus Torvalds 			  u64 *out_param,
4091da177e4SLinus Torvalds 			  int out_is_imm,
4101da177e4SLinus Torvalds 			  u32 in_modifier,
4111da177e4SLinus Torvalds 			  u8 op_modifier,
4121da177e4SLinus Torvalds 			  u16 op,
413cdb73db0SGoldwyn Rodrigues 			  unsigned long timeout)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	int err = 0;
4161da177e4SLinus Torvalds 	struct mthca_cmd_context *context;
4171da177e4SLinus Torvalds 
418e3aa31c5SMichael S. Tsirkin 	down(&dev->cmd.event_sem);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	spin_lock(&dev->cmd.context_lock);
4211da177e4SLinus Torvalds 	BUG_ON(dev->cmd.free_head < 0);
4221da177e4SLinus Torvalds 	context = &dev->cmd.context[dev->cmd.free_head];
423c1f74958SMichael S. Tsirkin 	context->token += dev->cmd.token_mask + 1;
4241da177e4SLinus Torvalds 	dev->cmd.free_head = context->next;
4251da177e4SLinus Torvalds 	spin_unlock(&dev->cmd.context_lock);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	init_completion(&context->done);
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	err = mthca_cmd_post(dev, in_param,
4301da177e4SLinus Torvalds 			     out_param ? *out_param : 0,
4311da177e4SLinus Torvalds 			     in_modifier, op_modifier,
4321da177e4SLinus Torvalds 			     op, context->token, 1);
4331da177e4SLinus Torvalds 	if (err)
4341da177e4SLinus Torvalds 		goto out;
4351da177e4SLinus Torvalds 
436e9cd5941SRoland Dreier 	if (!wait_for_completion_timeout(&context->done, timeout)) {
437e9cd5941SRoland Dreier 		err = -EBUSY;
438e9cd5941SRoland Dreier 		goto out;
439e9cd5941SRoland Dreier 	}
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	err = context->result;
4421da177e4SLinus Torvalds 	if (err)
4431da177e4SLinus Torvalds 		goto out;
4441da177e4SLinus Torvalds 
445cdb73db0SGoldwyn Rodrigues 	if (context->status) {
4461da177e4SLinus Torvalds 		mthca_dbg(dev, "Command %02x completed with status %02x\n",
447cdb73db0SGoldwyn Rodrigues 			  op, context->status);
448cdb73db0SGoldwyn Rodrigues 		err = mthca_status_to_errno(context->status);
449cdb73db0SGoldwyn Rodrigues 	}
4501da177e4SLinus Torvalds 
451a43402afSLeon Romanovsky 	if (out_is_imm && out_param) {
4521da177e4SLinus Torvalds 		*out_param = context->out_param;
453a43402afSLeon Romanovsky 	} else if (out_is_imm) {
454a43402afSLeon Romanovsky 		err = -EINVAL;
455a43402afSLeon Romanovsky 		goto out;
456a43402afSLeon Romanovsky 	}
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds out:
4591da177e4SLinus Torvalds 	spin_lock(&dev->cmd.context_lock);
4601da177e4SLinus Torvalds 	context->next = dev->cmd.free_head;
4611da177e4SLinus Torvalds 	dev->cmd.free_head = context - dev->cmd.context;
4621da177e4SLinus Torvalds 	spin_unlock(&dev->cmd.context_lock);
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	up(&dev->cmd.event_sem);
4651da177e4SLinus Torvalds 	return err;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds /* Invoke a command with an output mailbox */
mthca_cmd_box(struct mthca_dev * dev,u64 in_param,u64 out_param,u32 in_modifier,u8 op_modifier,u16 op,unsigned long timeout)4691da177e4SLinus Torvalds static int mthca_cmd_box(struct mthca_dev *dev,
4701da177e4SLinus Torvalds 			 u64 in_param,
4711da177e4SLinus Torvalds 			 u64 out_param,
4721da177e4SLinus Torvalds 			 u32 in_modifier,
4731da177e4SLinus Torvalds 			 u8 op_modifier,
4741da177e4SLinus Torvalds 			 u16 op,
475cdb73db0SGoldwyn Rodrigues 			 unsigned long timeout)
4761da177e4SLinus Torvalds {
47714abdffcSEli Cohen 	if (dev->cmd.flags & MTHCA_CMD_USE_EVENTS)
4781da177e4SLinus Torvalds 		return mthca_cmd_wait(dev, in_param, &out_param, 0,
4791da177e4SLinus Torvalds 				      in_modifier, op_modifier, op,
480cdb73db0SGoldwyn Rodrigues 				      timeout);
4811da177e4SLinus Torvalds 	else
4821da177e4SLinus Torvalds 		return mthca_cmd_poll(dev, in_param, &out_param, 0,
4831da177e4SLinus Torvalds 				      in_modifier, op_modifier, op,
484cdb73db0SGoldwyn Rodrigues 				      timeout);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /* Invoke a command with no output parameter */
mthca_cmd(struct mthca_dev * dev,u64 in_param,u32 in_modifier,u8 op_modifier,u16 op,unsigned long timeout)4881da177e4SLinus Torvalds static int mthca_cmd(struct mthca_dev *dev,
4891da177e4SLinus Torvalds 		     u64 in_param,
4901da177e4SLinus Torvalds 		     u32 in_modifier,
4911da177e4SLinus Torvalds 		     u8 op_modifier,
4921da177e4SLinus Torvalds 		     u16 op,
493cdb73db0SGoldwyn Rodrigues 		     unsigned long timeout)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	return mthca_cmd_box(dev, in_param, 0, in_modifier,
496cdb73db0SGoldwyn Rodrigues 			     op_modifier, op, timeout);
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds /*
5001da177e4SLinus Torvalds  * Invoke a command with an immediate output parameter (and copy the
5011da177e4SLinus Torvalds  * output into the caller's out_param pointer after the command
5021da177e4SLinus Torvalds  * executes).
5031da177e4SLinus Torvalds  */
mthca_cmd_imm(struct mthca_dev * dev,u64 in_param,u64 * out_param,u32 in_modifier,u8 op_modifier,u16 op,unsigned long timeout)5041da177e4SLinus Torvalds static int mthca_cmd_imm(struct mthca_dev *dev,
5051da177e4SLinus Torvalds 			 u64 in_param,
5061da177e4SLinus Torvalds 			 u64 *out_param,
5071da177e4SLinus Torvalds 			 u32 in_modifier,
5081da177e4SLinus Torvalds 			 u8 op_modifier,
5091da177e4SLinus Torvalds 			 u16 op,
510cdb73db0SGoldwyn Rodrigues 			 unsigned long timeout)
5111da177e4SLinus Torvalds {
51214abdffcSEli Cohen 	if (dev->cmd.flags & MTHCA_CMD_USE_EVENTS)
5131da177e4SLinus Torvalds 		return mthca_cmd_wait(dev, in_param, out_param, 1,
5141da177e4SLinus Torvalds 				      in_modifier, op_modifier, op,
515cdb73db0SGoldwyn Rodrigues 				      timeout);
5161da177e4SLinus Torvalds 	else
5171da177e4SLinus Torvalds 		return mthca_cmd_poll(dev, in_param, out_param, 1,
5181da177e4SLinus Torvalds 				      in_modifier, op_modifier, op,
519cdb73db0SGoldwyn Rodrigues 				      timeout);
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
mthca_cmd_init(struct mthca_dev * dev)52280fd8238SRoland Dreier int mthca_cmd_init(struct mthca_dev *dev)
52380fd8238SRoland Dreier {
524fd9cfdd1SRoland Dreier 	mutex_init(&dev->cmd.hcr_mutex);
52580fd8238SRoland Dreier 	sema_init(&dev->cmd.poll_sem, 1);
52614abdffcSEli Cohen 	dev->cmd.flags = 0;
52780fd8238SRoland Dreier 
52880fd8238SRoland Dreier 	dev->hcr = ioremap(pci_resource_start(dev->pdev, 0) + MTHCA_HCR_BASE,
52980fd8238SRoland Dreier 			   MTHCA_HCR_SIZE);
53080fd8238SRoland Dreier 	if (!dev->hcr) {
53180fd8238SRoland Dreier 		mthca_err(dev, "Couldn't map command register.");
53280fd8238SRoland Dreier 		return -ENOMEM;
53380fd8238SRoland Dreier 	}
53480fd8238SRoland Dreier 
53511880a55SRomain Perier 	dev->cmd.pool = dma_pool_create("mthca_cmd", &dev->pdev->dev,
536ed878458SRoland Dreier 					MTHCA_MAILBOX_SIZE,
537ed878458SRoland Dreier 					MTHCA_MAILBOX_SIZE, 0);
538ed878458SRoland Dreier 	if (!dev->cmd.pool) {
539ed878458SRoland Dreier 		iounmap(dev->hcr);
540ed878458SRoland Dreier 		return -ENOMEM;
541ed878458SRoland Dreier 	}
542ed878458SRoland Dreier 
54380fd8238SRoland Dreier 	return 0;
54480fd8238SRoland Dreier }
54580fd8238SRoland Dreier 
mthca_cmd_cleanup(struct mthca_dev * dev)54680fd8238SRoland Dreier void mthca_cmd_cleanup(struct mthca_dev *dev)
54780fd8238SRoland Dreier {
54811880a55SRomain Perier 	dma_pool_destroy(dev->cmd.pool);
54980fd8238SRoland Dreier 	iounmap(dev->hcr);
55014abdffcSEli Cohen 	if (dev->cmd.flags & MTHCA_CMD_POST_DOORBELLS)
55114abdffcSEli Cohen 		iounmap(dev->cmd.dbell_map);
55280fd8238SRoland Dreier }
55380fd8238SRoland Dreier 
5541da177e4SLinus Torvalds /*
5551da177e4SLinus Torvalds  * Switch to using events to issue FW commands (should be called after
5561da177e4SLinus Torvalds  * event queue to command events has been initialized).
5571da177e4SLinus Torvalds  */
mthca_cmd_use_events(struct mthca_dev * dev)5581da177e4SLinus Torvalds int mthca_cmd_use_events(struct mthca_dev *dev)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	int i;
5611da177e4SLinus Torvalds 
5626da2ec56SKees Cook 	dev->cmd.context = kmalloc_array(dev->cmd.max_cmds,
5631da177e4SLinus Torvalds 					 sizeof(struct mthca_cmd_context),
5641da177e4SLinus Torvalds 					 GFP_KERNEL);
5651da177e4SLinus Torvalds 	if (!dev->cmd.context)
5661da177e4SLinus Torvalds 		return -ENOMEM;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	for (i = 0; i < dev->cmd.max_cmds; ++i) {
5691da177e4SLinus Torvalds 		dev->cmd.context[i].token = i;
5701da177e4SLinus Torvalds 		dev->cmd.context[i].next = i + 1;
5711da177e4SLinus Torvalds 	}
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	dev->cmd.context[dev->cmd.max_cmds - 1].next = -1;
5741da177e4SLinus Torvalds 	dev->cmd.free_head = 0;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	sema_init(&dev->cmd.event_sem, dev->cmd.max_cmds);
5771da177e4SLinus Torvalds 	spin_lock_init(&dev->cmd.context_lock);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	for (dev->cmd.token_mask = 1;
5801da177e4SLinus Torvalds 	     dev->cmd.token_mask < dev->cmd.max_cmds;
5811da177e4SLinus Torvalds 	     dev->cmd.token_mask <<= 1)
5821da177e4SLinus Torvalds 		; /* nothing */
5831da177e4SLinus Torvalds 	--dev->cmd.token_mask;
5841da177e4SLinus Torvalds 
58514abdffcSEli Cohen 	dev->cmd.flags |= MTHCA_CMD_USE_EVENTS;
58614abdffcSEli Cohen 
5871da177e4SLinus Torvalds 	down(&dev->cmd.poll_sem);
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	return 0;
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds /*
5931da177e4SLinus Torvalds  * Switch back to polling (used when shutting down the device)
5941da177e4SLinus Torvalds  */
mthca_cmd_use_polling(struct mthca_dev * dev)5951da177e4SLinus Torvalds void mthca_cmd_use_polling(struct mthca_dev *dev)
5961da177e4SLinus Torvalds {
5971da177e4SLinus Torvalds 	int i;
5981da177e4SLinus Torvalds 
59914abdffcSEli Cohen 	dev->cmd.flags &= ~MTHCA_CMD_USE_EVENTS;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	for (i = 0; i < dev->cmd.max_cmds; ++i)
6021da177e4SLinus Torvalds 		down(&dev->cmd.event_sem);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	kfree(dev->cmd.context);
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	up(&dev->cmd.poll_sem);
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
mthca_alloc_mailbox(struct mthca_dev * dev,gfp_t gfp_mask)609ed878458SRoland Dreier struct mthca_mailbox *mthca_alloc_mailbox(struct mthca_dev *dev,
61087b750dcSAl Viro 					  gfp_t gfp_mask)
611ed878458SRoland Dreier {
612ed878458SRoland Dreier 	struct mthca_mailbox *mailbox;
613ed878458SRoland Dreier 
614ed878458SRoland Dreier 	mailbox = kmalloc(sizeof *mailbox, gfp_mask);
615ed878458SRoland Dreier 	if (!mailbox)
616ed878458SRoland Dreier 		return ERR_PTR(-ENOMEM);
617ed878458SRoland Dreier 
61811880a55SRomain Perier 	mailbox->buf = dma_pool_alloc(dev->cmd.pool, gfp_mask, &mailbox->dma);
619ed878458SRoland Dreier 	if (!mailbox->buf) {
620ed878458SRoland Dreier 		kfree(mailbox);
621ed878458SRoland Dreier 		return ERR_PTR(-ENOMEM);
622ed878458SRoland Dreier 	}
623ed878458SRoland Dreier 
624ed878458SRoland Dreier 	return mailbox;
625ed878458SRoland Dreier }
626ed878458SRoland Dreier 
mthca_free_mailbox(struct mthca_dev * dev,struct mthca_mailbox * mailbox)627ed878458SRoland Dreier void mthca_free_mailbox(struct mthca_dev *dev, struct mthca_mailbox *mailbox)
628ed878458SRoland Dreier {
629ed878458SRoland Dreier 	if (!mailbox)
630ed878458SRoland Dreier 		return;
631ed878458SRoland Dreier 
63211880a55SRomain Perier 	dma_pool_free(dev->cmd.pool, mailbox->buf, mailbox->dma);
633ed878458SRoland Dreier 	kfree(mailbox);
634ed878458SRoland Dreier }
635ed878458SRoland Dreier 
mthca_SYS_EN(struct mthca_dev * dev)636cdb73db0SGoldwyn Rodrigues int mthca_SYS_EN(struct mthca_dev *dev)
6371da177e4SLinus Torvalds {
638*fff32018SLeon Romanovsky 	u64 out = 0;
6391da177e4SLinus Torvalds 	int ret;
6401da177e4SLinus Torvalds 
641cdb73db0SGoldwyn Rodrigues 	ret = mthca_cmd_imm(dev, 0, &out, 0, 0, CMD_SYS_EN, CMD_TIME_CLASS_D);
6421da177e4SLinus Torvalds 
643cdb73db0SGoldwyn Rodrigues 	if (ret == -ENOMEM)
6441da177e4SLinus Torvalds 		mthca_warn(dev, "SYS_EN DDR error: syn=%x, sock=%d, "
6451da177e4SLinus Torvalds 			   "sladdr=%d, SPD source=%s\n",
6461da177e4SLinus Torvalds 			   (int) (out >> 6) & 0xf, (int) (out >> 4) & 3,
6471da177e4SLinus Torvalds 			   (int) (out >> 1) & 7, (int) out & 1 ? "NVMEM" : "DIMM");
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	return ret;
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
mthca_SYS_DIS(struct mthca_dev * dev)652cdb73db0SGoldwyn Rodrigues int mthca_SYS_DIS(struct mthca_dev *dev)
6531da177e4SLinus Torvalds {
654cdb73db0SGoldwyn Rodrigues 	return mthca_cmd(dev, 0, 0, 0, CMD_SYS_DIS, CMD_TIME_CLASS_C);
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
mthca_map_cmd(struct mthca_dev * dev,u16 op,struct mthca_icm * icm,u64 virt)6571da177e4SLinus Torvalds static int mthca_map_cmd(struct mthca_dev *dev, u16 op, struct mthca_icm *icm,
658cdb73db0SGoldwyn Rodrigues 			 u64 virt)
6591da177e4SLinus Torvalds {
660ed878458SRoland Dreier 	struct mthca_mailbox *mailbox;
6611da177e4SLinus Torvalds 	struct mthca_icm_iter iter;
662ed878458SRoland Dreier 	__be64 *pages;
6631da177e4SLinus Torvalds 	int lg;
6641da177e4SLinus Torvalds 	int nent = 0;
6651da177e4SLinus Torvalds 	int i;
6661da177e4SLinus Torvalds 	int err = 0;
6671da177e4SLinus Torvalds 	int ts = 0, tc = 0;
6681da177e4SLinus Torvalds 
669ed878458SRoland Dreier 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
670ed878458SRoland Dreier 	if (IS_ERR(mailbox))
671ed878458SRoland Dreier 		return PTR_ERR(mailbox);
672ed878458SRoland Dreier 	memset(mailbox->buf, 0, MTHCA_MAILBOX_SIZE);
673ed878458SRoland Dreier 	pages = mailbox->buf;
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	for (mthca_icm_first(icm, &iter);
6761da177e4SLinus Torvalds 	     !mthca_icm_last(&iter);
6771da177e4SLinus Torvalds 	     mthca_icm_next(&iter)) {
6781da177e4SLinus Torvalds 		/*
6791da177e4SLinus Torvalds 		 * We have to pass pages that are aligned to their
6801da177e4SLinus Torvalds 		 * size, so find the least significant 1 in the
6811da177e4SLinus Torvalds 		 * address or size and use that as our log2 size.
6821da177e4SLinus Torvalds 		 */
6831da177e4SLinus Torvalds 		lg = ffs(mthca_icm_addr(&iter) | mthca_icm_size(&iter)) - 1;
6848d3ef29dSIshai Rabinovitz 		if (lg < MTHCA_ICM_PAGE_SHIFT) {
6858d3ef29dSIshai Rabinovitz 			mthca_warn(dev, "Got FW area not aligned to %d (%llx/%lx).\n",
6868d3ef29dSIshai Rabinovitz 				   MTHCA_ICM_PAGE_SIZE,
6871da177e4SLinus Torvalds 				   (unsigned long long) mthca_icm_addr(&iter),
6881da177e4SLinus Torvalds 				   mthca_icm_size(&iter));
6891da177e4SLinus Torvalds 			err = -EINVAL;
6901da177e4SLinus Torvalds 			goto out;
6911da177e4SLinus Torvalds 		}
69259f174faSIshai Rabinovitz 		for (i = 0; i < mthca_icm_size(&iter) >> lg; ++i) {
6931da177e4SLinus Torvalds 			if (virt != -1) {
694ed878458SRoland Dreier 				pages[nent * 2] = cpu_to_be64(virt);
69562324895SLeon Romanovsky 				virt += 1ULL << lg;
6961da177e4SLinus Torvalds 			}
6971da177e4SLinus Torvalds 
6988d3ef29dSIshai Rabinovitz 			pages[nent * 2 + 1] =
6998d3ef29dSIshai Rabinovitz 				cpu_to_be64((mthca_icm_addr(&iter) + (i << lg)) |
7008d3ef29dSIshai Rabinovitz 					    (lg - MTHCA_ICM_PAGE_SHIFT));
7011da177e4SLinus Torvalds 			ts += 1 << (lg - 10);
7021da177e4SLinus Torvalds 			++tc;
7031da177e4SLinus Torvalds 
70444dd823bSMichael S. Tsirkin 			if (++nent == MTHCA_MAILBOX_SIZE / 16) {
705ed878458SRoland Dreier 				err = mthca_cmd(dev, mailbox->dma, nent, 0, op,
706cdb73db0SGoldwyn Rodrigues 						CMD_TIME_CLASS_B);
707cdb73db0SGoldwyn Rodrigues 				if (err)
7081da177e4SLinus Torvalds 					goto out;
7091da177e4SLinus Torvalds 				nent = 0;
7101da177e4SLinus Torvalds 			}
7111da177e4SLinus Torvalds 		}
7121da177e4SLinus Torvalds 	}
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	if (nent)
715ed878458SRoland Dreier 		err = mthca_cmd(dev, mailbox->dma, nent, 0, op,
716cdb73db0SGoldwyn Rodrigues 				CMD_TIME_CLASS_B);
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	switch (op) {
7191da177e4SLinus Torvalds 	case CMD_MAP_FA:
7201da177e4SLinus Torvalds 		mthca_dbg(dev, "Mapped %d chunks/%d KB for FW.\n", tc, ts);
7211da177e4SLinus Torvalds 		break;
7221da177e4SLinus Torvalds 	case CMD_MAP_ICM_AUX:
7231da177e4SLinus Torvalds 		mthca_dbg(dev, "Mapped %d chunks/%d KB for ICM aux.\n", tc, ts);
7241da177e4SLinus Torvalds 		break;
7251da177e4SLinus Torvalds 	case CMD_MAP_ICM:
7261da177e4SLinus Torvalds 		mthca_dbg(dev, "Mapped %d chunks/%d KB at %llx for ICM.\n",
7271da177e4SLinus Torvalds 			  tc, ts, (unsigned long long) virt - (ts << 10));
7281da177e4SLinus Torvalds 		break;
7291da177e4SLinus Torvalds 	}
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds out:
732ed878458SRoland Dreier 	mthca_free_mailbox(dev, mailbox);
7331da177e4SLinus Torvalds 	return err;
7341da177e4SLinus Torvalds }
7351da177e4SLinus Torvalds 
mthca_MAP_FA(struct mthca_dev * dev,struct mthca_icm * icm)736cdb73db0SGoldwyn Rodrigues int mthca_MAP_FA(struct mthca_dev *dev, struct mthca_icm *icm)
7371da177e4SLinus Torvalds {
738cdb73db0SGoldwyn Rodrigues 	return mthca_map_cmd(dev, CMD_MAP_FA, icm, -1);
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
mthca_UNMAP_FA(struct mthca_dev * dev)741cdb73db0SGoldwyn Rodrigues int mthca_UNMAP_FA(struct mthca_dev *dev)
7421da177e4SLinus Torvalds {
743cdb73db0SGoldwyn Rodrigues 	return mthca_cmd(dev, 0, 0, 0, CMD_UNMAP_FA, CMD_TIME_CLASS_B);
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
mthca_RUN_FW(struct mthca_dev * dev)746cdb73db0SGoldwyn Rodrigues int mthca_RUN_FW(struct mthca_dev *dev)
7471da177e4SLinus Torvalds {
748cdb73db0SGoldwyn Rodrigues 	return mthca_cmd(dev, 0, 0, 0, CMD_RUN_FW, CMD_TIME_CLASS_A);
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
mthca_setup_cmd_doorbells(struct mthca_dev * dev,u64 base)75114abdffcSEli Cohen static void mthca_setup_cmd_doorbells(struct mthca_dev *dev, u64 base)
75214abdffcSEli Cohen {
753eb4a7cbfSJohn L. Burr 	phys_addr_t addr;
75414abdffcSEli Cohen 	u16 max_off = 0;
75514abdffcSEli Cohen 	int i;
75614abdffcSEli Cohen 
75714abdffcSEli Cohen 	for (i = 0; i < 8; ++i)
75814abdffcSEli Cohen 		max_off = max(max_off, dev->cmd.dbell_offsets[i]);
75914abdffcSEli Cohen 
76014abdffcSEli Cohen 	if ((base & PAGE_MASK) != ((base + max_off) & PAGE_MASK)) {
76114abdffcSEli Cohen 		mthca_warn(dev, "Firmware doorbell region at 0x%016llx, "
76214abdffcSEli Cohen 			   "length 0x%x crosses a page boundary\n",
76314abdffcSEli Cohen 			   (unsigned long long) base, max_off);
76414abdffcSEli Cohen 		return;
76514abdffcSEli Cohen 	}
76614abdffcSEli Cohen 
76714abdffcSEli Cohen 	addr = pci_resource_start(dev->pdev, 2) +
76814abdffcSEli Cohen 		((pci_resource_len(dev->pdev, 2) - 1) & base);
76914abdffcSEli Cohen 	dev->cmd.dbell_map = ioremap(addr, max_off + sizeof(u32));
77014abdffcSEli Cohen 	if (!dev->cmd.dbell_map)
77114abdffcSEli Cohen 		return;
77214abdffcSEli Cohen 
77314abdffcSEli Cohen 	dev->cmd.flags |= MTHCA_CMD_POST_DOORBELLS;
77414abdffcSEli Cohen 	mthca_dbg(dev, "Mapped doorbell page for posting FW commands\n");
77514abdffcSEli Cohen }
77614abdffcSEli Cohen 
mthca_QUERY_FW(struct mthca_dev * dev)777cdb73db0SGoldwyn Rodrigues int mthca_QUERY_FW(struct mthca_dev *dev)
7781da177e4SLinus Torvalds {
779ed878458SRoland Dreier 	struct mthca_mailbox *mailbox;
7801da177e4SLinus Torvalds 	u32 *outbox;
78114abdffcSEli Cohen 	u64 base;
78214abdffcSEli Cohen 	u32 tmp;
7831da177e4SLinus Torvalds 	int err = 0;
7841da177e4SLinus Torvalds 	u8 lg;
78514abdffcSEli Cohen 	int i;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds #define QUERY_FW_OUT_SIZE             0x100
7881da177e4SLinus Torvalds #define QUERY_FW_VER_OFFSET            0x00
7891da177e4SLinus Torvalds #define QUERY_FW_MAX_CMD_OFFSET        0x0f
7901da177e4SLinus Torvalds #define QUERY_FW_ERR_START_OFFSET      0x30
7911da177e4SLinus Torvalds #define QUERY_FW_ERR_SIZE_OFFSET       0x38
7921da177e4SLinus Torvalds 
79314abdffcSEli Cohen #define QUERY_FW_CMD_DB_EN_OFFSET      0x10
79414abdffcSEli Cohen #define QUERY_FW_CMD_DB_OFFSET         0x50
79514abdffcSEli Cohen #define QUERY_FW_CMD_DB_BASE           0x60
79614abdffcSEli Cohen 
7971da177e4SLinus Torvalds #define QUERY_FW_START_OFFSET          0x20
7981da177e4SLinus Torvalds #define QUERY_FW_END_OFFSET            0x28
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds #define QUERY_FW_SIZE_OFFSET           0x00
8011da177e4SLinus Torvalds #define QUERY_FW_CLR_INT_BASE_OFFSET   0x20
8021da177e4SLinus Torvalds #define QUERY_FW_EQ_ARM_BASE_OFFSET    0x40
8031da177e4SLinus Torvalds #define QUERY_FW_EQ_SET_CI_BASE_OFFSET 0x48
8041da177e4SLinus Torvalds 
805ed878458SRoland Dreier 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
806ed878458SRoland Dreier 	if (IS_ERR(mailbox))
807ed878458SRoland Dreier 		return PTR_ERR(mailbox);
808ed878458SRoland Dreier 	outbox = mailbox->buf;
8091da177e4SLinus Torvalds 
810ed878458SRoland Dreier 	err = mthca_cmd_box(dev, 0, mailbox->dma, 0, 0, CMD_QUERY_FW,
811cdb73db0SGoldwyn Rodrigues 			    CMD_TIME_CLASS_A);
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	if (err)
8141da177e4SLinus Torvalds 		goto out;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	MTHCA_GET(dev->fw_ver,   outbox, QUERY_FW_VER_OFFSET);
8171da177e4SLinus Torvalds 	/*
8183e1db334SRoland Dreier 	 * FW subminor version is at more significant bits than minor
8191da177e4SLinus Torvalds 	 * version, so swap here.
8201da177e4SLinus Torvalds 	 */
8211da177e4SLinus Torvalds 	dev->fw_ver = (dev->fw_ver & 0xffff00000000ull) |
8221da177e4SLinus Torvalds 		((dev->fw_ver & 0xffff0000ull) >> 16) |
8231da177e4SLinus Torvalds 		((dev->fw_ver & 0x0000ffffull) << 16);
8241da177e4SLinus Torvalds 
8258fdf679fSRoland Dreier 	MTHCA_GET(lg, outbox, QUERY_FW_MAX_CMD_OFFSET);
8268fdf679fSRoland Dreier 	dev->cmd.max_cmds = 1 << lg;
8278fdf679fSRoland Dreier 
82814abdffcSEli Cohen 	mthca_dbg(dev, "FW version %012llx, max commands %d\n",
82914abdffcSEli Cohen 		  (unsigned long long) dev->fw_ver, dev->cmd.max_cmds);
83014abdffcSEli Cohen 
8313d155f8cSRoland Dreier 	MTHCA_GET(dev->catas_err.addr, outbox, QUERY_FW_ERR_START_OFFSET);
8323d155f8cSRoland Dreier 	MTHCA_GET(dev->catas_err.size, outbox, QUERY_FW_ERR_SIZE_OFFSET);
8331da177e4SLinus Torvalds 
8343d155f8cSRoland Dreier 	mthca_dbg(dev, "Catastrophic error buffer at 0x%llx, size 0x%x\n",
8353d155f8cSRoland Dreier 		  (unsigned long long) dev->catas_err.addr, dev->catas_err.size);
8361da177e4SLinus Torvalds 
83714abdffcSEli Cohen 	MTHCA_GET(tmp, outbox, QUERY_FW_CMD_DB_EN_OFFSET);
83814abdffcSEli Cohen 	if (tmp & 0x1) {
83914abdffcSEli Cohen 		mthca_dbg(dev, "FW supports commands through doorbells\n");
84014abdffcSEli Cohen 
84114abdffcSEli Cohen 		MTHCA_GET(base, outbox, QUERY_FW_CMD_DB_BASE);
84214abdffcSEli Cohen 		for (i = 0; i < MTHCA_CMD_NUM_DBELL_DWORDS; ++i)
84314abdffcSEli Cohen 			MTHCA_GET(dev->cmd.dbell_offsets[i], outbox,
84414abdffcSEli Cohen 				  QUERY_FW_CMD_DB_OFFSET + (i << 1));
84514abdffcSEli Cohen 
84614abdffcSEli Cohen 		mthca_setup_cmd_doorbells(dev, base);
84714abdffcSEli Cohen 	}
84814abdffcSEli Cohen 
849d10ddbf6SRoland Dreier 	if (mthca_is_memfree(dev)) {
8501da177e4SLinus Torvalds 		MTHCA_GET(dev->fw.arbel.fw_pages,       outbox, QUERY_FW_SIZE_OFFSET);
8511da177e4SLinus Torvalds 		MTHCA_GET(dev->fw.arbel.clr_int_base,   outbox, QUERY_FW_CLR_INT_BASE_OFFSET);
8521da177e4SLinus Torvalds 		MTHCA_GET(dev->fw.arbel.eq_arm_base,    outbox, QUERY_FW_EQ_ARM_BASE_OFFSET);
8531da177e4SLinus Torvalds 		MTHCA_GET(dev->fw.arbel.eq_set_ci_base, outbox, QUERY_FW_EQ_SET_CI_BASE_OFFSET);
8541da177e4SLinus Torvalds 		mthca_dbg(dev, "FW size %d KB\n", dev->fw.arbel.fw_pages << 2);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 		/*
8578d3ef29dSIshai Rabinovitz 		 * Round up number of system pages needed in case
8588d3ef29dSIshai Rabinovitz 		 * MTHCA_ICM_PAGE_SIZE < PAGE_SIZE.
8591da177e4SLinus Torvalds 		 */
8601da177e4SLinus Torvalds 		dev->fw.arbel.fw_pages =
8618d3ef29dSIshai Rabinovitz 			ALIGN(dev->fw.arbel.fw_pages, PAGE_SIZE / MTHCA_ICM_PAGE_SIZE) >>
8628d3ef29dSIshai Rabinovitz 				(PAGE_SHIFT - MTHCA_ICM_PAGE_SHIFT);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 		mthca_dbg(dev, "Clear int @ %llx, EQ arm @ %llx, EQ set CI @ %llx\n",
8651da177e4SLinus Torvalds 			  (unsigned long long) dev->fw.arbel.clr_int_base,
8661da177e4SLinus Torvalds 			  (unsigned long long) dev->fw.arbel.eq_arm_base,
8671da177e4SLinus Torvalds 			  (unsigned long long) dev->fw.arbel.eq_set_ci_base);
8681da177e4SLinus Torvalds 	} else {
8691da177e4SLinus Torvalds 		MTHCA_GET(dev->fw.tavor.fw_start, outbox, QUERY_FW_START_OFFSET);
8701da177e4SLinus Torvalds 		MTHCA_GET(dev->fw.tavor.fw_end,   outbox, QUERY_FW_END_OFFSET);
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 		mthca_dbg(dev, "FW size %d KB (start %llx, end %llx)\n",
8731da177e4SLinus Torvalds 			  (int) ((dev->fw.tavor.fw_end - dev->fw.tavor.fw_start) >> 10),
8741da177e4SLinus Torvalds 			  (unsigned long long) dev->fw.tavor.fw_start,
8751da177e4SLinus Torvalds 			  (unsigned long long) dev->fw.tavor.fw_end);
8761da177e4SLinus Torvalds 	}
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds out:
879ed878458SRoland Dreier 	mthca_free_mailbox(dev, mailbox);
8801da177e4SLinus Torvalds 	return err;
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds 
mthca_ENABLE_LAM(struct mthca_dev * dev)883cdb73db0SGoldwyn Rodrigues int mthca_ENABLE_LAM(struct mthca_dev *dev)
8841da177e4SLinus Torvalds {
885ed878458SRoland Dreier 	struct mthca_mailbox *mailbox;
8861da177e4SLinus Torvalds 	u8 info;
8871da177e4SLinus Torvalds 	u32 *outbox;
8881da177e4SLinus Torvalds 	int err = 0;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds #define ENABLE_LAM_OUT_SIZE         0x100
8911da177e4SLinus Torvalds #define ENABLE_LAM_START_OFFSET     0x00
8921da177e4SLinus Torvalds #define ENABLE_LAM_END_OFFSET       0x08
8931da177e4SLinus Torvalds #define ENABLE_LAM_INFO_OFFSET      0x13
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds #define ENABLE_LAM_INFO_HIDDEN_FLAG (1 << 4)
8961da177e4SLinus Torvalds #define ENABLE_LAM_INFO_ECC_MASK    0x3
8971da177e4SLinus Torvalds 
898ed878458SRoland Dreier 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
899ed878458SRoland Dreier 	if (IS_ERR(mailbox))
900ed878458SRoland Dreier 		return PTR_ERR(mailbox);
901ed878458SRoland Dreier 	outbox = mailbox->buf;
9021da177e4SLinus Torvalds 
903ed878458SRoland Dreier 	err = mthca_cmd_box(dev, 0, mailbox->dma, 0, 0, CMD_ENABLE_LAM,
904cdb73db0SGoldwyn Rodrigues 			    CMD_TIME_CLASS_C);
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	if (err)
9071da177e4SLinus Torvalds 		goto out;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	MTHCA_GET(dev->ddr_start, outbox, ENABLE_LAM_START_OFFSET);
9101da177e4SLinus Torvalds 	MTHCA_GET(dev->ddr_end,   outbox, ENABLE_LAM_END_OFFSET);
9111da177e4SLinus Torvalds 	MTHCA_GET(info,           outbox, ENABLE_LAM_INFO_OFFSET);
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	if (!!(info & ENABLE_LAM_INFO_HIDDEN_FLAG) !=
9141da177e4SLinus Torvalds 	    !!(dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) {
9151da177e4SLinus Torvalds 		mthca_info(dev, "FW reports that HCA-attached memory "
9161da177e4SLinus Torvalds 			   "is %s hidden; does not match PCI config\n",
9171da177e4SLinus Torvalds 			   (info & ENABLE_LAM_INFO_HIDDEN_FLAG) ?
9181da177e4SLinus Torvalds 			   "" : "not");
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 	if (info & ENABLE_LAM_INFO_HIDDEN_FLAG)
9211da177e4SLinus Torvalds 		mthca_dbg(dev, "HCA-attached memory is hidden.\n");
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	mthca_dbg(dev, "HCA memory size %d KB (start %llx, end %llx)\n",
9241da177e4SLinus Torvalds 		  (int) ((dev->ddr_end - dev->ddr_start) >> 10),
9251da177e4SLinus Torvalds 		  (unsigned long long) dev->ddr_start,
9261da177e4SLinus Torvalds 		  (unsigned long long) dev->ddr_end);
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds out:
929ed878458SRoland Dreier 	mthca_free_mailbox(dev, mailbox);
9301da177e4SLinus Torvalds 	return err;
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
mthca_DISABLE_LAM(struct mthca_dev * dev)933cdb73db0SGoldwyn Rodrigues int mthca_DISABLE_LAM(struct mthca_dev *dev)
9341da177e4SLinus Torvalds {
935cdb73db0SGoldwyn Rodrigues 	return mthca_cmd(dev, 0, 0, 0, CMD_SYS_DIS, CMD_TIME_CLASS_C);
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
mthca_QUERY_DDR(struct mthca_dev * dev)938cdb73db0SGoldwyn Rodrigues int mthca_QUERY_DDR(struct mthca_dev *dev)
9391da177e4SLinus Torvalds {
940ed878458SRoland Dreier 	struct mthca_mailbox *mailbox;
9411da177e4SLinus Torvalds 	u8 info;
9421da177e4SLinus Torvalds 	u32 *outbox;
9431da177e4SLinus Torvalds 	int err = 0;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds #define QUERY_DDR_OUT_SIZE         0x100
9461da177e4SLinus Torvalds #define QUERY_DDR_START_OFFSET     0x00
9471da177e4SLinus Torvalds #define QUERY_DDR_END_OFFSET       0x08
9481da177e4SLinus Torvalds #define QUERY_DDR_INFO_OFFSET      0x13
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds #define QUERY_DDR_INFO_HIDDEN_FLAG (1 << 4)
9511da177e4SLinus Torvalds #define QUERY_DDR_INFO_ECC_MASK    0x3
9521da177e4SLinus Torvalds 
953ed878458SRoland Dreier 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
954ed878458SRoland Dreier 	if (IS_ERR(mailbox))
955ed878458SRoland Dreier 		return PTR_ERR(mailbox);
956ed878458SRoland Dreier 	outbox = mailbox->buf;
9571da177e4SLinus Torvalds 
958ed878458SRoland Dreier 	err = mthca_cmd_box(dev, 0, mailbox->dma, 0, 0, CMD_QUERY_DDR,
959cdb73db0SGoldwyn Rodrigues 			    CMD_TIME_CLASS_A);
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	if (err)
9621da177e4SLinus Torvalds 		goto out;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	MTHCA_GET(dev->ddr_start, outbox, QUERY_DDR_START_OFFSET);
9651da177e4SLinus Torvalds 	MTHCA_GET(dev->ddr_end,   outbox, QUERY_DDR_END_OFFSET);
9661da177e4SLinus Torvalds 	MTHCA_GET(info,           outbox, QUERY_DDR_INFO_OFFSET);
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	if (!!(info & QUERY_DDR_INFO_HIDDEN_FLAG) !=
9691da177e4SLinus Torvalds 	    !!(dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) {
9701da177e4SLinus Torvalds 		mthca_info(dev, "FW reports that HCA-attached memory "
9711da177e4SLinus Torvalds 			   "is %s hidden; does not match PCI config\n",
9721da177e4SLinus Torvalds 			   (info & QUERY_DDR_INFO_HIDDEN_FLAG) ?
9731da177e4SLinus Torvalds 			   "" : "not");
9741da177e4SLinus Torvalds 	}
9751da177e4SLinus Torvalds 	if (info & QUERY_DDR_INFO_HIDDEN_FLAG)
9761da177e4SLinus Torvalds 		mthca_dbg(dev, "HCA-attached memory is hidden.\n");
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	mthca_dbg(dev, "HCA memory size %d KB (start %llx, end %llx)\n",
9791da177e4SLinus Torvalds 		  (int) ((dev->ddr_end - dev->ddr_start) >> 10),
9801da177e4SLinus Torvalds 		  (unsigned long long) dev->ddr_start,
9811da177e4SLinus Torvalds 		  (unsigned long long) dev->ddr_end);
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds out:
984ed878458SRoland Dreier 	mthca_free_mailbox(dev, mailbox);
9851da177e4SLinus Torvalds 	return err;
9861da177e4SLinus Torvalds }
9871da177e4SLinus Torvalds 
mthca_QUERY_DEV_LIM(struct mthca_dev * dev,struct mthca_dev_lim * dev_lim)9881da177e4SLinus Torvalds int mthca_QUERY_DEV_LIM(struct mthca_dev *dev,
989cdb73db0SGoldwyn Rodrigues 			struct mthca_dev_lim *dev_lim)
9901da177e4SLinus Torvalds {
991ed878458SRoland Dreier 	struct mthca_mailbox *mailbox;
9921da177e4SLinus Torvalds 	u32 *outbox;
9931da177e4SLinus Torvalds 	u8 field;
9941da177e4SLinus Torvalds 	u16 size;
995bf6a9e31SJack Morgenstein 	u16 stat_rate;
9961da177e4SLinus Torvalds 	int err;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds #define QUERY_DEV_LIM_OUT_SIZE             0x100
9991da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_SRQ_SZ_OFFSET     0x10
10001da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_QP_SZ_OFFSET      0x11
10011da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_QP_OFFSET        0x12
10021da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_QP_OFFSET         0x13
10031da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_SRQ_OFFSET       0x14
10041da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_SRQ_OFFSET        0x15
10051da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_EEC_OFFSET       0x16
10061da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_EEC_OFFSET        0x17
10071da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_CQ_SZ_OFFSET      0x19
10081da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_CQ_OFFSET        0x1a
10091da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_CQ_OFFSET         0x1b
10101da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_MPT_OFFSET        0x1d
10111da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_EQ_OFFSET        0x1e
10121da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_EQ_OFFSET         0x1f
10131da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_MTT_OFFSET       0x20
10141da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_MRW_SZ_OFFSET     0x21
10151da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_MRW_OFFSET       0x22
10161da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_MTT_SEG_OFFSET    0x23
10171da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_AV_OFFSET         0x27
10181da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_REQ_QP_OFFSET     0x29
10191da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_RES_QP_OFFSET     0x2b
10201da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_RDMA_OFFSET       0x2f
10211da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSZ_SRQ_OFFSET        0x33
10221da177e4SLinus Torvalds #define QUERY_DEV_LIM_ACK_DELAY_OFFSET      0x35
10231da177e4SLinus Torvalds #define QUERY_DEV_LIM_MTU_WIDTH_OFFSET      0x36
10241da177e4SLinus Torvalds #define QUERY_DEV_LIM_VL_PORT_OFFSET        0x37
10251da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_GID_OFFSET        0x3b
1026bf6a9e31SJack Morgenstein #define QUERY_DEV_LIM_RATE_SUPPORT_OFFSET   0x3c
10271da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_PKEY_OFFSET       0x3f
10281da177e4SLinus Torvalds #define QUERY_DEV_LIM_FLAGS_OFFSET          0x44
10291da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_UAR_OFFSET       0x48
10301da177e4SLinus Torvalds #define QUERY_DEV_LIM_UAR_SZ_OFFSET         0x49
10311da177e4SLinus Torvalds #define QUERY_DEV_LIM_PAGE_SZ_OFFSET        0x4b
10321da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_SG_OFFSET         0x51
10331da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_DESC_SZ_OFFSET    0x52
10341da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_SG_RQ_OFFSET      0x55
10351da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_DESC_SZ_RQ_OFFSET 0x56
10361da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_QP_MCG_OFFSET     0x61
10371da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_MCG_OFFSET       0x62
10381da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_MCG_OFFSET        0x63
10391da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_PD_OFFSET        0x64
10401da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_PD_OFFSET         0x65
10411da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_RDD_OFFSET       0x66
10421da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_RDD_OFFSET        0x67
10431da177e4SLinus Torvalds #define QUERY_DEV_LIM_EEC_ENTRY_SZ_OFFSET   0x80
10441da177e4SLinus Torvalds #define QUERY_DEV_LIM_QPC_ENTRY_SZ_OFFSET   0x82
10451da177e4SLinus Torvalds #define QUERY_DEV_LIM_EEEC_ENTRY_SZ_OFFSET  0x84
10461da177e4SLinus Torvalds #define QUERY_DEV_LIM_EQPC_ENTRY_SZ_OFFSET  0x86
10471da177e4SLinus Torvalds #define QUERY_DEV_LIM_EQC_ENTRY_SZ_OFFSET   0x88
10481da177e4SLinus Torvalds #define QUERY_DEV_LIM_CQC_ENTRY_SZ_OFFSET   0x8a
10491da177e4SLinus Torvalds #define QUERY_DEV_LIM_SRQ_ENTRY_SZ_OFFSET   0x8c
10501da177e4SLinus Torvalds #define QUERY_DEV_LIM_UAR_ENTRY_SZ_OFFSET   0x8e
10511da177e4SLinus Torvalds #define QUERY_DEV_LIM_MTT_ENTRY_SZ_OFFSET   0x90
10521da177e4SLinus Torvalds #define QUERY_DEV_LIM_MPT_ENTRY_SZ_OFFSET   0x92
10531da177e4SLinus Torvalds #define QUERY_DEV_LIM_PBL_SZ_OFFSET         0x96
10541da177e4SLinus Torvalds #define QUERY_DEV_LIM_BMME_FLAGS_OFFSET     0x97
10551da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_LKEY_OFFSET      0x98
10561da177e4SLinus Torvalds #define QUERY_DEV_LIM_LAMR_OFFSET           0x9f
10571da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_ICM_SZ_OFFSET     0xa0
10581da177e4SLinus Torvalds 
1059ed878458SRoland Dreier 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1060ed878458SRoland Dreier 	if (IS_ERR(mailbox))
1061ed878458SRoland Dreier 		return PTR_ERR(mailbox);
1062ed878458SRoland Dreier 	outbox = mailbox->buf;
10631da177e4SLinus Torvalds 
1064ed878458SRoland Dreier 	err = mthca_cmd_box(dev, 0, mailbox->dma, 0, 0, CMD_QUERY_DEV_LIM,
1065cdb73db0SGoldwyn Rodrigues 			    CMD_TIME_CLASS_A);
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	if (err)
10681da177e4SLinus Torvalds 		goto out;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_QP_OFFSET);
10711da177e4SLinus Torvalds 	dev_lim->reserved_qps = 1 << (field & 0xf);
10721da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_OFFSET);
10731da177e4SLinus Torvalds 	dev_lim->max_qps = 1 << (field & 0x1f);
10741da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_SRQ_OFFSET);
10751da177e4SLinus Torvalds 	dev_lim->reserved_srqs = 1 << (field >> 4);
10761da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SRQ_OFFSET);
10771da177e4SLinus Torvalds 	dev_lim->max_srqs = 1 << (field & 0x1f);
10781da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_EEC_OFFSET);
10791da177e4SLinus Torvalds 	dev_lim->reserved_eecs = 1 << (field & 0xf);
10801da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_EEC_OFFSET);
10811da177e4SLinus Torvalds 	dev_lim->max_eecs = 1 << (field & 0x1f);
10821da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_CQ_SZ_OFFSET);
10831da177e4SLinus Torvalds 	dev_lim->max_cq_sz = 1 << field;
10841da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_CQ_OFFSET);
10851da177e4SLinus Torvalds 	dev_lim->reserved_cqs = 1 << (field & 0xf);
10861da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_CQ_OFFSET);
10871da177e4SLinus Torvalds 	dev_lim->max_cqs = 1 << (field & 0x1f);
10881da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MPT_OFFSET);
10891da177e4SLinus Torvalds 	dev_lim->max_mpts = 1 << (field & 0x3f);
10901da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_EQ_OFFSET);
10911da177e4SLinus Torvalds 	dev_lim->reserved_eqs = 1 << (field & 0xf);
10921da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_EQ_OFFSET);
10931da177e4SLinus Torvalds 	dev_lim->max_eqs = 1 << (field & 0x7);
10941da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_MTT_OFFSET);
1095c7d204e8SMichael S. Tsirkin 	if (mthca_is_memfree(dev))
1096c7d204e8SMichael S. Tsirkin 		dev_lim->reserved_mtts = ALIGN((1 << (field >> 4)) * sizeof(u64),
1097c1f67a88SEli Cohen 					       dev->limits.mtt_seg_size) / dev->limits.mtt_seg_size;
1098c7d204e8SMichael S. Tsirkin 	else
10991da177e4SLinus Torvalds 		dev_lim->reserved_mtts = 1 << (field >> 4);
11001da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MRW_SZ_OFFSET);
11011da177e4SLinus Torvalds 	dev_lim->max_mrw_sz = 1 << field;
11021da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_MRW_OFFSET);
11031da177e4SLinus Torvalds 	dev_lim->reserved_mrws = 1 << (field & 0xf);
11041da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MTT_SEG_OFFSET);
11051da177e4SLinus Torvalds 	dev_lim->max_mtt_seg = 1 << (field & 0x3f);
11061da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_REQ_QP_OFFSET);
11071da177e4SLinus Torvalds 	dev_lim->max_requester_per_qp = 1 << (field & 0x3f);
11081da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_RES_QP_OFFSET);
11091da177e4SLinus Torvalds 	dev_lim->max_responder_per_qp = 1 << (field & 0x3f);
11101da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_RDMA_OFFSET);
11111da177e4SLinus Torvalds 	dev_lim->max_rdma_global = 1 << (field & 0x3f);
11121da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_ACK_DELAY_OFFSET);
11131da177e4SLinus Torvalds 	dev_lim->local_ca_ack_delay = field & 0x1f;
11141da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MTU_WIDTH_OFFSET);
11151da177e4SLinus Torvalds 	dev_lim->max_mtu        = field >> 4;
11161da177e4SLinus Torvalds 	dev_lim->max_port_width = field & 0xf;
11171da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_VL_PORT_OFFSET);
11181da177e4SLinus Torvalds 	dev_lim->max_vl    = field >> 4;
11191da177e4SLinus Torvalds 	dev_lim->num_ports = field & 0xf;
11201da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_GID_OFFSET);
11211da177e4SLinus Torvalds 	dev_lim->max_gids = 1 << (field & 0xf);
1122bf6a9e31SJack Morgenstein 	MTHCA_GET(stat_rate, outbox, QUERY_DEV_LIM_RATE_SUPPORT_OFFSET);
1123bf6a9e31SJack Morgenstein 	dev_lim->stat_rate_support = stat_rate;
11241da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_PKEY_OFFSET);
11251da177e4SLinus Torvalds 	dev_lim->max_pkeys = 1 << (field & 0xf);
11261da177e4SLinus Torvalds 	MTHCA_GET(dev_lim->flags, outbox, QUERY_DEV_LIM_FLAGS_OFFSET);
11271da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_UAR_OFFSET);
11281da177e4SLinus Torvalds 	dev_lim->reserved_uars = field >> 4;
11291da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_UAR_SZ_OFFSET);
11301da177e4SLinus Torvalds 	dev_lim->uar_size = 1 << ((field & 0x3f) + 20);
11311da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_PAGE_SZ_OFFSET);
11321da177e4SLinus Torvalds 	dev_lim->min_page_sz = 1 << field;
11331da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SG_OFFSET);
11341da177e4SLinus Torvalds 	dev_lim->max_sg = field;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_MAX_DESC_SZ_OFFSET);
11371da177e4SLinus Torvalds 	dev_lim->max_desc_sz = size;
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_MCG_OFFSET);
11401da177e4SLinus Torvalds 	dev_lim->max_qp_per_mcg = 1 << field;
11411da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_MCG_OFFSET);
11421da177e4SLinus Torvalds 	dev_lim->reserved_mgms = field & 0xf;
11431da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MCG_OFFSET);
11441da177e4SLinus Torvalds 	dev_lim->max_mcgs = 1 << field;
11451da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_PD_OFFSET);
11461da177e4SLinus Torvalds 	dev_lim->reserved_pds = field >> 4;
11471da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_PD_OFFSET);
11481da177e4SLinus Torvalds 	dev_lim->max_pds = 1 << (field & 0x3f);
11491da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_RDD_OFFSET);
11501da177e4SLinus Torvalds 	dev_lim->reserved_rdds = field >> 4;
11511da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_RDD_OFFSET);
11521da177e4SLinus Torvalds 	dev_lim->max_rdds = 1 << (field & 0x3f);
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_EEC_ENTRY_SZ_OFFSET);
11551da177e4SLinus Torvalds 	dev_lim->eec_entry_sz = size;
11561da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_QPC_ENTRY_SZ_OFFSET);
11571da177e4SLinus Torvalds 	dev_lim->qpc_entry_sz = size;
11581da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_EEEC_ENTRY_SZ_OFFSET);
11591da177e4SLinus Torvalds 	dev_lim->eeec_entry_sz = size;
11601da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_EQPC_ENTRY_SZ_OFFSET);
11611da177e4SLinus Torvalds 	dev_lim->eqpc_entry_sz = size;
11621da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_EQC_ENTRY_SZ_OFFSET);
11631da177e4SLinus Torvalds 	dev_lim->eqc_entry_sz = size;
11641da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_CQC_ENTRY_SZ_OFFSET);
11651da177e4SLinus Torvalds 	dev_lim->cqc_entry_sz = size;
11661da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_SRQ_ENTRY_SZ_OFFSET);
11671da177e4SLinus Torvalds 	dev_lim->srq_entry_sz = size;
11681da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_UAR_ENTRY_SZ_OFFSET);
11691da177e4SLinus Torvalds 	dev_lim->uar_scratch_entry_sz = size;
11701da177e4SLinus Torvalds 
1171d10ddbf6SRoland Dreier 	if (mthca_is_memfree(dev)) {
1172a3c8ab4fSJack Morgenstein 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SRQ_SZ_OFFSET);
1173a3c8ab4fSJack Morgenstein 		dev_lim->max_srq_sz = 1 << field;
1174a3c8ab4fSJack Morgenstein 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_SZ_OFFSET);
1175a3c8ab4fSJack Morgenstein 		dev_lim->max_qp_sz = 1 << field;
11761da177e4SLinus Torvalds 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSZ_SRQ_OFFSET);
11771da177e4SLinus Torvalds 		dev_lim->hca.arbel.resize_srq = field & 1;
11788cf2daf3SRoland Dreier 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SG_RQ_OFFSET);
11798cf2daf3SRoland Dreier 		dev_lim->max_sg = min_t(int, field, dev_lim->max_sg);
118077369ed3SJack Morgenstein 		MTHCA_GET(size, outbox, QUERY_DEV_LIM_MAX_DESC_SZ_RQ_OFFSET);
118177369ed3SJack Morgenstein 		dev_lim->max_desc_sz = min_t(int, size, dev_lim->max_desc_sz);
11821da177e4SLinus Torvalds 		MTHCA_GET(size, outbox, QUERY_DEV_LIM_MPT_ENTRY_SZ_OFFSET);
11831da177e4SLinus Torvalds 		dev_lim->mpt_entry_sz = size;
11841da177e4SLinus Torvalds 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_PBL_SZ_OFFSET);
11851da177e4SLinus Torvalds 		dev_lim->hca.arbel.max_pbl_sz = 1 << (field & 0x3f);
11861da177e4SLinus Torvalds 		MTHCA_GET(dev_lim->hca.arbel.bmme_flags, outbox,
11871da177e4SLinus Torvalds 			  QUERY_DEV_LIM_BMME_FLAGS_OFFSET);
11881da177e4SLinus Torvalds 		MTHCA_GET(dev_lim->hca.arbel.reserved_lkey, outbox,
11891da177e4SLinus Torvalds 			  QUERY_DEV_LIM_RSVD_LKEY_OFFSET);
11901da177e4SLinus Torvalds 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_LAMR_OFFSET);
11911da177e4SLinus Torvalds 		dev_lim->hca.arbel.lam_required = field & 1;
11921da177e4SLinus Torvalds 		MTHCA_GET(dev_lim->hca.arbel.max_icm_sz, outbox,
11931da177e4SLinus Torvalds 			  QUERY_DEV_LIM_MAX_ICM_SZ_OFFSET);
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 		if (dev_lim->hca.arbel.bmme_flags & 1)
11961da177e4SLinus Torvalds 			mthca_dbg(dev, "Base MM extensions: yes "
11971da177e4SLinus Torvalds 				  "(flags %d, max PBL %d, rsvd L_Key %08x)\n",
11981da177e4SLinus Torvalds 				  dev_lim->hca.arbel.bmme_flags,
11991da177e4SLinus Torvalds 				  dev_lim->hca.arbel.max_pbl_sz,
12001da177e4SLinus Torvalds 				  dev_lim->hca.arbel.reserved_lkey);
12011da177e4SLinus Torvalds 		else
12021da177e4SLinus Torvalds 			mthca_dbg(dev, "Base MM extensions: no\n");
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 		mthca_dbg(dev, "Max ICM size %lld MB\n",
12051da177e4SLinus Torvalds 			  (unsigned long long) dev_lim->hca.arbel.max_icm_sz >> 20);
12061da177e4SLinus Torvalds 	} else {
1207a3c8ab4fSJack Morgenstein 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SRQ_SZ_OFFSET);
1208a3c8ab4fSJack Morgenstein 		dev_lim->max_srq_sz = (1 << field) - 1;
1209a3c8ab4fSJack Morgenstein 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_SZ_OFFSET);
1210a3c8ab4fSJack Morgenstein 		dev_lim->max_qp_sz = (1 << field) - 1;
12111da177e4SLinus Torvalds 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_AV_OFFSET);
12121da177e4SLinus Torvalds 		dev_lim->hca.tavor.max_avs = 1 << (field & 0x3f);
12131da177e4SLinus Torvalds 		dev_lim->mpt_entry_sz = MTHCA_MPT_ENTRY_SIZE;
12141da177e4SLinus Torvalds 	}
12151da177e4SLinus Torvalds 
1216f295c79bSRoland Dreier 	mthca_dbg(dev, "Max QPs: %d, reserved QPs: %d, entry size: %d\n",
1217f295c79bSRoland Dreier 		  dev_lim->max_qps, dev_lim->reserved_qps, dev_lim->qpc_entry_sz);
1218f295c79bSRoland Dreier 	mthca_dbg(dev, "Max SRQs: %d, reserved SRQs: %d, entry size: %d\n",
1219f295c79bSRoland Dreier 		  dev_lim->max_srqs, dev_lim->reserved_srqs, dev_lim->srq_entry_sz);
1220f295c79bSRoland Dreier 	mthca_dbg(dev, "Max CQs: %d, reserved CQs: %d, entry size: %d\n",
1221f295c79bSRoland Dreier 		  dev_lim->max_cqs, dev_lim->reserved_cqs, dev_lim->cqc_entry_sz);
1222f295c79bSRoland Dreier 	mthca_dbg(dev, "Max EQs: %d, reserved EQs: %d, entry size: %d\n",
1223f295c79bSRoland Dreier 		  dev_lim->max_eqs, dev_lim->reserved_eqs, dev_lim->eqc_entry_sz);
1224f295c79bSRoland Dreier 	mthca_dbg(dev, "reserved MPTs: %d, reserved MTTs: %d\n",
1225f295c79bSRoland Dreier 		  dev_lim->reserved_mrws, dev_lim->reserved_mtts);
1226f295c79bSRoland Dreier 	mthca_dbg(dev, "Max PDs: %d, reserved PDs: %d, reserved UARs: %d\n",
1227f295c79bSRoland Dreier 		  dev_lim->max_pds, dev_lim->reserved_pds, dev_lim->reserved_uars);
1228f295c79bSRoland Dreier 	mthca_dbg(dev, "Max QP/MCG: %d, reserved MGMs: %d\n",
1229f295c79bSRoland Dreier 		  dev_lim->max_pds, dev_lim->reserved_mgms);
1230f295c79bSRoland Dreier 	mthca_dbg(dev, "Max CQEs: %d, max WQEs: %d, max SRQ WQEs: %d\n",
1231f295c79bSRoland Dreier 		  dev_lim->max_cq_sz, dev_lim->max_qp_sz, dev_lim->max_srq_sz);
1232f295c79bSRoland Dreier 
1233f295c79bSRoland Dreier 	mthca_dbg(dev, "Flags: %08x\n", dev_lim->flags);
1234f295c79bSRoland Dreier 
12351da177e4SLinus Torvalds out:
1236ed878458SRoland Dreier 	mthca_free_mailbox(dev, mailbox);
12371da177e4SLinus Torvalds 	return err;
12381da177e4SLinus Torvalds }
12391da177e4SLinus Torvalds 
get_board_id(void * vsd,char * board_id)12402e8b981cSMichael S. Tsirkin static void get_board_id(void *vsd, char *board_id)
12412e8b981cSMichael S. Tsirkin {
12422e8b981cSMichael S. Tsirkin 	int i;
12432e8b981cSMichael S. Tsirkin 
12442e8b981cSMichael S. Tsirkin #define VSD_OFFSET_SIG1		0x00
12452e8b981cSMichael S. Tsirkin #define VSD_OFFSET_SIG2		0xde
12462e8b981cSMichael S. Tsirkin #define VSD_OFFSET_MLX_BOARD_ID	0xd0
12472e8b981cSMichael S. Tsirkin #define VSD_OFFSET_TS_BOARD_ID	0x20
12482e8b981cSMichael S. Tsirkin 
12492e8b981cSMichael S. Tsirkin #define VSD_SIGNATURE_TOPSPIN	0x5ad
12502e8b981cSMichael S. Tsirkin 
12512e8b981cSMichael S. Tsirkin 	memset(board_id, 0, MTHCA_BOARD_ID_LEN);
12522e8b981cSMichael S. Tsirkin 
12532e8b981cSMichael S. Tsirkin 	if (be16_to_cpup(vsd + VSD_OFFSET_SIG1) == VSD_SIGNATURE_TOPSPIN &&
12542e8b981cSMichael S. Tsirkin 	    be16_to_cpup(vsd + VSD_OFFSET_SIG2) == VSD_SIGNATURE_TOPSPIN) {
12552c34bb6dSWolfram Sang 		strscpy(board_id, vsd + VSD_OFFSET_TS_BOARD_ID, MTHCA_BOARD_ID_LEN);
12562e8b981cSMichael S. Tsirkin 	} else {
12572e8b981cSMichael S. Tsirkin 		/*
12582e8b981cSMichael S. Tsirkin 		 * The board ID is a string but the firmware byte
12592e8b981cSMichael S. Tsirkin 		 * swaps each 4-byte word before passing it back to
12602e8b981cSMichael S. Tsirkin 		 * us.  Therefore we need to swab it before printing.
12612e8b981cSMichael S. Tsirkin 		 */
12622e8b981cSMichael S. Tsirkin 		for (i = 0; i < 4; ++i)
12632e8b981cSMichael S. Tsirkin 			((u32 *) board_id)[i] =
12642e8b981cSMichael S. Tsirkin 				swab32(*(u32 *) (vsd + VSD_OFFSET_MLX_BOARD_ID + i * 4));
12652e8b981cSMichael S. Tsirkin 	}
12662e8b981cSMichael S. Tsirkin }
12672e8b981cSMichael S. Tsirkin 
mthca_QUERY_ADAPTER(struct mthca_dev * dev,struct mthca_adapter * adapter)12681da177e4SLinus Torvalds int mthca_QUERY_ADAPTER(struct mthca_dev *dev,
1269cdb73db0SGoldwyn Rodrigues 			struct mthca_adapter *adapter)
12701da177e4SLinus Torvalds {
1271ed878458SRoland Dreier 	struct mthca_mailbox *mailbox;
12721da177e4SLinus Torvalds 	u32 *outbox;
12731da177e4SLinus Torvalds 	int err;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds #define QUERY_ADAPTER_OUT_SIZE             0x100
12761da177e4SLinus Torvalds #define QUERY_ADAPTER_VENDOR_ID_OFFSET     0x00
12771da177e4SLinus Torvalds #define QUERY_ADAPTER_DEVICE_ID_OFFSET     0x04
12781da177e4SLinus Torvalds #define QUERY_ADAPTER_REVISION_ID_OFFSET   0x08
12791da177e4SLinus Torvalds #define QUERY_ADAPTER_INTA_PIN_OFFSET      0x10
12802e8b981cSMichael S. Tsirkin #define QUERY_ADAPTER_VSD_OFFSET           0x20
12811da177e4SLinus Torvalds 
1282ed878458SRoland Dreier 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1283ed878458SRoland Dreier 	if (IS_ERR(mailbox))
1284ed878458SRoland Dreier 		return PTR_ERR(mailbox);
1285ed878458SRoland Dreier 	outbox = mailbox->buf;
12861da177e4SLinus Torvalds 
1287ed878458SRoland Dreier 	err = mthca_cmd_box(dev, 0, mailbox->dma, 0, 0, CMD_QUERY_ADAPTER,
1288cdb73db0SGoldwyn Rodrigues 			    CMD_TIME_CLASS_A);
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	if (err)
12911da177e4SLinus Torvalds 		goto out;
12921da177e4SLinus Torvalds 
12936ccef1deSJack Morgenstein 	if (!mthca_is_memfree(dev)) {
12946ccef1deSJack Morgenstein 		MTHCA_GET(adapter->vendor_id, outbox,
12956ccef1deSJack Morgenstein 			  QUERY_ADAPTER_VENDOR_ID_OFFSET);
12966ccef1deSJack Morgenstein 		MTHCA_GET(adapter->device_id, outbox,
12976ccef1deSJack Morgenstein 			  QUERY_ADAPTER_DEVICE_ID_OFFSET);
12986ccef1deSJack Morgenstein 		MTHCA_GET(adapter->revision_id, outbox,
12996ccef1deSJack Morgenstein 			  QUERY_ADAPTER_REVISION_ID_OFFSET);
13006ccef1deSJack Morgenstein 	}
13011da177e4SLinus Torvalds 	MTHCA_GET(adapter->inta_pin, outbox,    QUERY_ADAPTER_INTA_PIN_OFFSET);
13021da177e4SLinus Torvalds 
13032e8b981cSMichael S. Tsirkin 	get_board_id(outbox + QUERY_ADAPTER_VSD_OFFSET / 4,
13042e8b981cSMichael S. Tsirkin 		     adapter->board_id);
13052e8b981cSMichael S. Tsirkin 
13061da177e4SLinus Torvalds out:
1307ed878458SRoland Dreier 	mthca_free_mailbox(dev, mailbox);
13081da177e4SLinus Torvalds 	return err;
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds 
mthca_INIT_HCA(struct mthca_dev * dev,struct mthca_init_hca_param * param)13111da177e4SLinus Torvalds int mthca_INIT_HCA(struct mthca_dev *dev,
1312cdb73db0SGoldwyn Rodrigues 		   struct mthca_init_hca_param *param)
13131da177e4SLinus Torvalds {
1314ed878458SRoland Dreier 	struct mthca_mailbox *mailbox;
131597f52eb4SSean Hefty 	__be32 *inbox;
13161da177e4SLinus Torvalds 	int err;
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds #define INIT_HCA_IN_SIZE             	 0x200
1319651eaac9SEli Cohen #define INIT_HCA_FLAGS1_OFFSET           0x00c
1320651eaac9SEli Cohen #define INIT_HCA_FLAGS2_OFFSET           0x014
13211da177e4SLinus Torvalds #define INIT_HCA_QPC_OFFSET          	 0x020
13221da177e4SLinus Torvalds #define  INIT_HCA_QPC_BASE_OFFSET    	 (INIT_HCA_QPC_OFFSET + 0x10)
13231da177e4SLinus Torvalds #define  INIT_HCA_LOG_QP_OFFSET      	 (INIT_HCA_QPC_OFFSET + 0x17)
13241da177e4SLinus Torvalds #define  INIT_HCA_EEC_BASE_OFFSET    	 (INIT_HCA_QPC_OFFSET + 0x20)
13251da177e4SLinus Torvalds #define  INIT_HCA_LOG_EEC_OFFSET     	 (INIT_HCA_QPC_OFFSET + 0x27)
13261da177e4SLinus Torvalds #define  INIT_HCA_SRQC_BASE_OFFSET   	 (INIT_HCA_QPC_OFFSET + 0x28)
13271da177e4SLinus Torvalds #define  INIT_HCA_LOG_SRQ_OFFSET     	 (INIT_HCA_QPC_OFFSET + 0x2f)
13281da177e4SLinus Torvalds #define  INIT_HCA_CQC_BASE_OFFSET    	 (INIT_HCA_QPC_OFFSET + 0x30)
13291da177e4SLinus Torvalds #define  INIT_HCA_LOG_CQ_OFFSET      	 (INIT_HCA_QPC_OFFSET + 0x37)
13301da177e4SLinus Torvalds #define  INIT_HCA_EQPC_BASE_OFFSET   	 (INIT_HCA_QPC_OFFSET + 0x40)
13311da177e4SLinus Torvalds #define  INIT_HCA_EEEC_BASE_OFFSET   	 (INIT_HCA_QPC_OFFSET + 0x50)
13321da177e4SLinus Torvalds #define  INIT_HCA_EQC_BASE_OFFSET    	 (INIT_HCA_QPC_OFFSET + 0x60)
13331da177e4SLinus Torvalds #define  INIT_HCA_LOG_EQ_OFFSET      	 (INIT_HCA_QPC_OFFSET + 0x67)
13341da177e4SLinus Torvalds #define  INIT_HCA_RDB_BASE_OFFSET    	 (INIT_HCA_QPC_OFFSET + 0x70)
13351da177e4SLinus Torvalds #define INIT_HCA_UDAV_OFFSET         	 0x0b0
13361da177e4SLinus Torvalds #define  INIT_HCA_UDAV_LKEY_OFFSET   	 (INIT_HCA_UDAV_OFFSET + 0x0)
13371da177e4SLinus Torvalds #define  INIT_HCA_UDAV_PD_OFFSET     	 (INIT_HCA_UDAV_OFFSET + 0x4)
13381da177e4SLinus Torvalds #define INIT_HCA_MCAST_OFFSET        	 0x0c0
13391da177e4SLinus Torvalds #define  INIT_HCA_MC_BASE_OFFSET         (INIT_HCA_MCAST_OFFSET + 0x00)
13401da177e4SLinus Torvalds #define  INIT_HCA_LOG_MC_ENTRY_SZ_OFFSET (INIT_HCA_MCAST_OFFSET + 0x12)
13411da177e4SLinus Torvalds #define  INIT_HCA_MC_HASH_SZ_OFFSET      (INIT_HCA_MCAST_OFFSET + 0x16)
13421da177e4SLinus Torvalds #define  INIT_HCA_LOG_MC_TABLE_SZ_OFFSET (INIT_HCA_MCAST_OFFSET + 0x1b)
13431da177e4SLinus Torvalds #define INIT_HCA_TPT_OFFSET              0x0f0
13441da177e4SLinus Torvalds #define  INIT_HCA_MPT_BASE_OFFSET        (INIT_HCA_TPT_OFFSET + 0x00)
13451da177e4SLinus Torvalds #define  INIT_HCA_MTT_SEG_SZ_OFFSET      (INIT_HCA_TPT_OFFSET + 0x09)
13461da177e4SLinus Torvalds #define  INIT_HCA_LOG_MPT_SZ_OFFSET      (INIT_HCA_TPT_OFFSET + 0x0b)
13471da177e4SLinus Torvalds #define  INIT_HCA_MTT_BASE_OFFSET        (INIT_HCA_TPT_OFFSET + 0x10)
13481da177e4SLinus Torvalds #define INIT_HCA_UAR_OFFSET              0x120
13491da177e4SLinus Torvalds #define  INIT_HCA_UAR_BASE_OFFSET        (INIT_HCA_UAR_OFFSET + 0x00)
13501da177e4SLinus Torvalds #define  INIT_HCA_UARC_SZ_OFFSET         (INIT_HCA_UAR_OFFSET + 0x09)
13511da177e4SLinus Torvalds #define  INIT_HCA_LOG_UAR_SZ_OFFSET      (INIT_HCA_UAR_OFFSET + 0x0a)
13521da177e4SLinus Torvalds #define  INIT_HCA_UAR_PAGE_SZ_OFFSET     (INIT_HCA_UAR_OFFSET + 0x0b)
13531da177e4SLinus Torvalds #define  INIT_HCA_UAR_SCATCH_BASE_OFFSET (INIT_HCA_UAR_OFFSET + 0x10)
13541da177e4SLinus Torvalds #define  INIT_HCA_UAR_CTX_BASE_OFFSET    (INIT_HCA_UAR_OFFSET + 0x18)
13551da177e4SLinus Torvalds 
1356ed878458SRoland Dreier 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1357ed878458SRoland Dreier 	if (IS_ERR(mailbox))
1358ed878458SRoland Dreier 		return PTR_ERR(mailbox);
1359ed878458SRoland Dreier 	inbox = mailbox->buf;
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	memset(inbox, 0, INIT_HCA_IN_SIZE);
13621da177e4SLinus Torvalds 
1363651eaac9SEli Cohen 	if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
1364651eaac9SEli Cohen 		MTHCA_PUT(inbox, 0x1, INIT_HCA_FLAGS1_OFFSET);
1365651eaac9SEli Cohen 
13661da177e4SLinus Torvalds #if defined(__LITTLE_ENDIAN)
1367651eaac9SEli Cohen 	*(inbox + INIT_HCA_FLAGS2_OFFSET / 4) &= ~cpu_to_be32(1 << 1);
13681da177e4SLinus Torvalds #elif defined(__BIG_ENDIAN)
1369651eaac9SEli Cohen 	*(inbox + INIT_HCA_FLAGS2_OFFSET / 4) |= cpu_to_be32(1 << 1);
13701da177e4SLinus Torvalds #else
13711da177e4SLinus Torvalds #error Host endianness not defined
13721da177e4SLinus Torvalds #endif
13731da177e4SLinus Torvalds 	/* Check port for UD address vector: */
1374651eaac9SEli Cohen 	*(inbox + INIT_HCA_FLAGS2_OFFSET / 4) |= cpu_to_be32(1);
13751da177e4SLinus Torvalds 
1376680b575fSEli Cohen 	/* Enable IPoIB checksumming if we can: */
1377680b575fSEli Cohen 	if (dev->device_cap_flags & IB_DEVICE_UD_IP_CSUM)
1378680b575fSEli Cohen 		*(inbox + INIT_HCA_FLAGS2_OFFSET / 4) |= cpu_to_be32(7 << 3);
1379680b575fSEli Cohen 
13801da177e4SLinus Torvalds 	/* We leave wqe_quota, responder_exu, etc as 0 (default) */
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	/* QPC/EEC/CQC/EQC/RDB attributes */
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->qpc_base,     INIT_HCA_QPC_BASE_OFFSET);
13851da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_num_qps,  INIT_HCA_LOG_QP_OFFSET);
13861da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->eec_base,     INIT_HCA_EEC_BASE_OFFSET);
13871da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_num_eecs, INIT_HCA_LOG_EEC_OFFSET);
13881da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->srqc_base,    INIT_HCA_SRQC_BASE_OFFSET);
13891da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_num_srqs, INIT_HCA_LOG_SRQ_OFFSET);
13901da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->cqc_base,     INIT_HCA_CQC_BASE_OFFSET);
13911da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_num_cqs,  INIT_HCA_LOG_CQ_OFFSET);
13921da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->eqpc_base,    INIT_HCA_EQPC_BASE_OFFSET);
13931da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->eeec_base,    INIT_HCA_EEEC_BASE_OFFSET);
13941da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->eqc_base,     INIT_HCA_EQC_BASE_OFFSET);
13951da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_num_eqs,  INIT_HCA_LOG_EQ_OFFSET);
13961da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->rdb_base,     INIT_HCA_RDB_BASE_OFFSET);
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 	/* UD AV attributes */
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	/* multicast attributes */
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->mc_base,         INIT_HCA_MC_BASE_OFFSET);
14031da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_mc_entry_sz, INIT_HCA_LOG_MC_ENTRY_SZ_OFFSET);
14041da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->mc_hash_sz,      INIT_HCA_MC_HASH_SZ_OFFSET);
14051da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_mc_table_sz, INIT_HCA_LOG_MC_TABLE_SZ_OFFSET);
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	/* TPT attributes */
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->mpt_base,   INIT_HCA_MPT_BASE_OFFSET);
1410d10ddbf6SRoland Dreier 	if (!mthca_is_memfree(dev))
14111da177e4SLinus Torvalds 		MTHCA_PUT(inbox, param->mtt_seg_sz, INIT_HCA_MTT_SEG_SZ_OFFSET);
14121da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_mpt_sz, INIT_HCA_LOG_MPT_SZ_OFFSET);
14131da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->mtt_base,   INIT_HCA_MTT_BASE_OFFSET);
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	/* UAR attributes */
14161da177e4SLinus Torvalds 	{
14171da177e4SLinus Torvalds 		u8 uar_page_sz = PAGE_SHIFT - 12;
14181da177e4SLinus Torvalds 		MTHCA_PUT(inbox, uar_page_sz, INIT_HCA_UAR_PAGE_SZ_OFFSET);
14191da177e4SLinus Torvalds 	}
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->uar_scratch_base, INIT_HCA_UAR_SCATCH_BASE_OFFSET);
14221da177e4SLinus Torvalds 
1423d10ddbf6SRoland Dreier 	if (mthca_is_memfree(dev)) {
14241da177e4SLinus Torvalds 		MTHCA_PUT(inbox, param->log_uarc_sz, INIT_HCA_UARC_SZ_OFFSET);
14251da177e4SLinus Torvalds 		MTHCA_PUT(inbox, param->log_uar_sz,  INIT_HCA_LOG_UAR_SZ_OFFSET);
14261da177e4SLinus Torvalds 		MTHCA_PUT(inbox, param->uarc_base,   INIT_HCA_UAR_CTX_BASE_OFFSET);
14271da177e4SLinus Torvalds 	}
14281da177e4SLinus Torvalds 
1429cdb73db0SGoldwyn Rodrigues 	err = mthca_cmd(dev, mailbox->dma, 0, 0,
1430cdb73db0SGoldwyn Rodrigues 			CMD_INIT_HCA, CMD_TIME_CLASS_D);
14311da177e4SLinus Torvalds 
1432ed878458SRoland Dreier 	mthca_free_mailbox(dev, mailbox);
14331da177e4SLinus Torvalds 	return err;
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
mthca_INIT_IB(struct mthca_dev * dev,struct mthca_init_ib_param * param,int port)14361da177e4SLinus Torvalds int mthca_INIT_IB(struct mthca_dev *dev,
14371da177e4SLinus Torvalds 		  struct mthca_init_ib_param *param,
1438cdb73db0SGoldwyn Rodrigues 		  int port)
14391da177e4SLinus Torvalds {
1440ed878458SRoland Dreier 	struct mthca_mailbox *mailbox;
14411da177e4SLinus Torvalds 	u32 *inbox;
14421da177e4SLinus Torvalds 	int err;
14431da177e4SLinus Torvalds 	u32 flags;
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds #define INIT_IB_IN_SIZE          56
14461da177e4SLinus Torvalds #define INIT_IB_FLAGS_OFFSET     0x00
14471da177e4SLinus Torvalds #define INIT_IB_FLAG_SIG         (1 << 18)
14481da177e4SLinus Torvalds #define INIT_IB_FLAG_NG          (1 << 17)
14491da177e4SLinus Torvalds #define INIT_IB_FLAG_G0          (1 << 16)
14501da177e4SLinus Torvalds #define INIT_IB_VL_SHIFT         4
1451da6561c2SRoland Dreier #define INIT_IB_PORT_WIDTH_SHIFT 8
14521da177e4SLinus Torvalds #define INIT_IB_MTU_SHIFT        12
14531da177e4SLinus Torvalds #define INIT_IB_MAX_GID_OFFSET   0x06
14541da177e4SLinus Torvalds #define INIT_IB_MAX_PKEY_OFFSET  0x0a
14551da177e4SLinus Torvalds #define INIT_IB_GUID0_OFFSET     0x10
14561da177e4SLinus Torvalds #define INIT_IB_NODE_GUID_OFFSET 0x18
14571da177e4SLinus Torvalds #define INIT_IB_SI_GUID_OFFSET   0x20
14581da177e4SLinus Torvalds 
1459ed878458SRoland Dreier 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1460ed878458SRoland Dreier 	if (IS_ERR(mailbox))
1461ed878458SRoland Dreier 		return PTR_ERR(mailbox);
1462ed878458SRoland Dreier 	inbox = mailbox->buf;
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 	memset(inbox, 0, INIT_IB_IN_SIZE);
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 	flags = 0;
14671da177e4SLinus Torvalds 	flags |= param->set_guid0     ? INIT_IB_FLAG_G0  : 0;
14681da177e4SLinus Torvalds 	flags |= param->set_node_guid ? INIT_IB_FLAG_NG  : 0;
14691da177e4SLinus Torvalds 	flags |= param->set_si_guid   ? INIT_IB_FLAG_SIG : 0;
14701da177e4SLinus Torvalds 	flags |= param->vl_cap << INIT_IB_VL_SHIFT;
1471da6561c2SRoland Dreier 	flags |= param->port_width << INIT_IB_PORT_WIDTH_SHIFT;
14721da177e4SLinus Torvalds 	flags |= param->mtu_cap << INIT_IB_MTU_SHIFT;
14731da177e4SLinus Torvalds 	MTHCA_PUT(inbox, flags, INIT_IB_FLAGS_OFFSET);
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->gid_cap,   INIT_IB_MAX_GID_OFFSET);
14761da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->pkey_cap,  INIT_IB_MAX_PKEY_OFFSET);
14771da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->guid0,     INIT_IB_GUID0_OFFSET);
14781da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->node_guid, INIT_IB_NODE_GUID_OFFSET);
14791da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->si_guid,   INIT_IB_SI_GUID_OFFSET);
14801da177e4SLinus Torvalds 
1481ed878458SRoland Dreier 	err = mthca_cmd(dev, mailbox->dma, port, 0, CMD_INIT_IB,
1482cdb73db0SGoldwyn Rodrigues 			CMD_TIME_CLASS_A);
14831da177e4SLinus Torvalds 
1484ed878458SRoland Dreier 	mthca_free_mailbox(dev, mailbox);
14851da177e4SLinus Torvalds 	return err;
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds 
mthca_CLOSE_IB(struct mthca_dev * dev,int port)1488cdb73db0SGoldwyn Rodrigues int mthca_CLOSE_IB(struct mthca_dev *dev, int port)
14891da177e4SLinus Torvalds {
1490cdb73db0SGoldwyn Rodrigues 	return mthca_cmd(dev, 0, port, 0, CMD_CLOSE_IB, CMD_TIME_CLASS_A);
14911da177e4SLinus Torvalds }
14921da177e4SLinus Torvalds 
mthca_CLOSE_HCA(struct mthca_dev * dev,int panic)1493cdb73db0SGoldwyn Rodrigues int mthca_CLOSE_HCA(struct mthca_dev *dev, int panic)
14941da177e4SLinus Torvalds {
1495cdb73db0SGoldwyn Rodrigues 	return mthca_cmd(dev, 0, 0, panic, CMD_CLOSE_HCA, CMD_TIME_CLASS_C);
14961da177e4SLinus Torvalds }
14971da177e4SLinus Torvalds 
mthca_SET_IB(struct mthca_dev * dev,struct mthca_set_ib_param * param,int port)14981da177e4SLinus Torvalds int mthca_SET_IB(struct mthca_dev *dev, struct mthca_set_ib_param *param,
1499cdb73db0SGoldwyn Rodrigues 		 int port)
15001da177e4SLinus Torvalds {
1501ed878458SRoland Dreier 	struct mthca_mailbox *mailbox;
15021da177e4SLinus Torvalds 	u32 *inbox;
15031da177e4SLinus Torvalds 	int err;
15041da177e4SLinus Torvalds 	u32 flags = 0;
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds #define SET_IB_IN_SIZE         0x40
15071da177e4SLinus Torvalds #define SET_IB_FLAGS_OFFSET    0x00
15081da177e4SLinus Torvalds #define SET_IB_FLAG_SIG        (1 << 18)
15091da177e4SLinus Torvalds #define SET_IB_FLAG_RQK        (1 <<  0)
15101da177e4SLinus Torvalds #define SET_IB_CAP_MASK_OFFSET 0x04
15111da177e4SLinus Torvalds #define SET_IB_SI_GUID_OFFSET  0x08
15121da177e4SLinus Torvalds 
1513ed878458SRoland Dreier 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1514ed878458SRoland Dreier 	if (IS_ERR(mailbox))
1515ed878458SRoland Dreier 		return PTR_ERR(mailbox);
1516ed878458SRoland Dreier 	inbox = mailbox->buf;
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds 	memset(inbox, 0, SET_IB_IN_SIZE);
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	flags |= param->set_si_guid     ? SET_IB_FLAG_SIG : 0;
15211da177e4SLinus Torvalds 	flags |= param->reset_qkey_viol ? SET_IB_FLAG_RQK : 0;
15221da177e4SLinus Torvalds 	MTHCA_PUT(inbox, flags, SET_IB_FLAGS_OFFSET);
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->cap_mask, SET_IB_CAP_MASK_OFFSET);
15251da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->si_guid,  SET_IB_SI_GUID_OFFSET);
15261da177e4SLinus Torvalds 
1527ed878458SRoland Dreier 	err = mthca_cmd(dev, mailbox->dma, port, 0, CMD_SET_IB,
1528cdb73db0SGoldwyn Rodrigues 			CMD_TIME_CLASS_B);
15291da177e4SLinus Torvalds 
1530ed878458SRoland Dreier 	mthca_free_mailbox(dev, mailbox);
15311da177e4SLinus Torvalds 	return err;
15321da177e4SLinus Torvalds }
15331da177e4SLinus Torvalds 
mthca_MAP_ICM(struct mthca_dev * dev,struct mthca_icm * icm,u64 virt)1534cdb73db0SGoldwyn Rodrigues int mthca_MAP_ICM(struct mthca_dev *dev, struct mthca_icm *icm, u64 virt)
15351da177e4SLinus Torvalds {
1536cdb73db0SGoldwyn Rodrigues 	return mthca_map_cmd(dev, CMD_MAP_ICM, icm, virt);
15371da177e4SLinus Torvalds }
15381da177e4SLinus Torvalds 
mthca_MAP_ICM_page(struct mthca_dev * dev,u64 dma_addr,u64 virt)1539cdb73db0SGoldwyn Rodrigues int mthca_MAP_ICM_page(struct mthca_dev *dev, u64 dma_addr, u64 virt)
15401da177e4SLinus Torvalds {
1541ed878458SRoland Dreier 	struct mthca_mailbox *mailbox;
154297f52eb4SSean Hefty 	__be64 *inbox;
15431da177e4SLinus Torvalds 	int err;
15441da177e4SLinus Torvalds 
1545ed878458SRoland Dreier 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1546ed878458SRoland Dreier 	if (IS_ERR(mailbox))
1547ed878458SRoland Dreier 		return PTR_ERR(mailbox);
1548ed878458SRoland Dreier 	inbox = mailbox->buf;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	inbox[0] = cpu_to_be64(virt);
15511da177e4SLinus Torvalds 	inbox[1] = cpu_to_be64(dma_addr);
15521da177e4SLinus Torvalds 
1553ed878458SRoland Dreier 	err = mthca_cmd(dev, mailbox->dma, 1, 0, CMD_MAP_ICM,
1554cdb73db0SGoldwyn Rodrigues 			CMD_TIME_CLASS_B);
15551da177e4SLinus Torvalds 
1556ed878458SRoland Dreier 	mthca_free_mailbox(dev, mailbox);
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	if (!err)
15596bd6228eSRoland Dreier 		mthca_dbg(dev, "Mapped page at %llx to %llx for ICM.\n",
15606bd6228eSRoland Dreier 			  (unsigned long long) dma_addr, (unsigned long long) virt);
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	return err;
15631da177e4SLinus Torvalds }
15641da177e4SLinus Torvalds 
mthca_UNMAP_ICM(struct mthca_dev * dev,u64 virt,u32 page_count)1565cdb73db0SGoldwyn Rodrigues int mthca_UNMAP_ICM(struct mthca_dev *dev, u64 virt, u32 page_count)
15661da177e4SLinus Torvalds {
15671da177e4SLinus Torvalds 	mthca_dbg(dev, "Unmapping %d pages at %llx from ICM.\n",
15681da177e4SLinus Torvalds 		  page_count, (unsigned long long) virt);
15691da177e4SLinus Torvalds 
1570cdb73db0SGoldwyn Rodrigues 	return mthca_cmd(dev, virt, page_count, 0,
1571cdb73db0SGoldwyn Rodrigues 			CMD_UNMAP_ICM, CMD_TIME_CLASS_B);
15721da177e4SLinus Torvalds }
15731da177e4SLinus Torvalds 
mthca_MAP_ICM_AUX(struct mthca_dev * dev,struct mthca_icm * icm)1574cdb73db0SGoldwyn Rodrigues int mthca_MAP_ICM_AUX(struct mthca_dev *dev, struct mthca_icm *icm)
15751da177e4SLinus Torvalds {
1576cdb73db0SGoldwyn Rodrigues 	return mthca_map_cmd(dev, CMD_MAP_ICM_AUX, icm, -1);
15771da177e4SLinus Torvalds }
15781da177e4SLinus Torvalds 
mthca_UNMAP_ICM_AUX(struct mthca_dev * dev)1579cdb73db0SGoldwyn Rodrigues int mthca_UNMAP_ICM_AUX(struct mthca_dev *dev)
15801da177e4SLinus Torvalds {
1581cdb73db0SGoldwyn Rodrigues 	return mthca_cmd(dev, 0, 0, 0, CMD_UNMAP_ICM_AUX, CMD_TIME_CLASS_B);
15821da177e4SLinus Torvalds }
15831da177e4SLinus Torvalds 
mthca_SET_ICM_SIZE(struct mthca_dev * dev,u64 icm_size,u64 * aux_pages)1584cdb73db0SGoldwyn Rodrigues int mthca_SET_ICM_SIZE(struct mthca_dev *dev, u64 icm_size, u64 *aux_pages)
15851da177e4SLinus Torvalds {
1586cdb73db0SGoldwyn Rodrigues 	int ret = mthca_cmd_imm(dev, icm_size, aux_pages, 0,
1587cdb73db0SGoldwyn Rodrigues 			0, CMD_SET_ICM_SIZE, CMD_TIME_CLASS_A);
15881da177e4SLinus Torvalds 
1589cdb73db0SGoldwyn Rodrigues 	if (ret)
15901da177e4SLinus Torvalds 		return ret;
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds 	/*
15938d3ef29dSIshai Rabinovitz 	 * Round up number of system pages needed in case
15948d3ef29dSIshai Rabinovitz 	 * MTHCA_ICM_PAGE_SIZE < PAGE_SIZE.
15951da177e4SLinus Torvalds 	 */
15968d3ef29dSIshai Rabinovitz 	*aux_pages = ALIGN(*aux_pages, PAGE_SIZE / MTHCA_ICM_PAGE_SIZE) >>
15978d3ef29dSIshai Rabinovitz 		(PAGE_SHIFT - MTHCA_ICM_PAGE_SHIFT);
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	return 0;
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds 
mthca_SW2HW_MPT(struct mthca_dev * dev,struct mthca_mailbox * mailbox,int mpt_index)1602ed878458SRoland Dreier int mthca_SW2HW_MPT(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1603cdb73db0SGoldwyn Rodrigues 		    int mpt_index)
16041da177e4SLinus Torvalds {
1605ed878458SRoland Dreier 	return mthca_cmd(dev, mailbox->dma, mpt_index, 0, CMD_SW2HW_MPT,
1606cdb73db0SGoldwyn Rodrigues 			 CMD_TIME_CLASS_B);
16071da177e4SLinus Torvalds }
16081da177e4SLinus Torvalds 
mthca_HW2SW_MPT(struct mthca_dev * dev,struct mthca_mailbox * mailbox,int mpt_index)1609ed878458SRoland Dreier int mthca_HW2SW_MPT(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1610cdb73db0SGoldwyn Rodrigues 		    int mpt_index)
16111da177e4SLinus Torvalds {
1612ed878458SRoland Dreier 	return mthca_cmd_box(dev, 0, mailbox ? mailbox->dma : 0, mpt_index,
1613ed878458SRoland Dreier 			     !mailbox, CMD_HW2SW_MPT,
1614cdb73db0SGoldwyn Rodrigues 			     CMD_TIME_CLASS_B);
16151da177e4SLinus Torvalds }
16161da177e4SLinus Torvalds 
mthca_WRITE_MTT(struct mthca_dev * dev,struct mthca_mailbox * mailbox,int num_mtt)1617ed878458SRoland Dreier int mthca_WRITE_MTT(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1618cdb73db0SGoldwyn Rodrigues 		    int num_mtt)
16191da177e4SLinus Torvalds {
1620ed878458SRoland Dreier 	return mthca_cmd(dev, mailbox->dma, num_mtt, 0, CMD_WRITE_MTT,
1621cdb73db0SGoldwyn Rodrigues 			 CMD_TIME_CLASS_B);
16221da177e4SLinus Torvalds }
16231da177e4SLinus Torvalds 
mthca_SYNC_TPT(struct mthca_dev * dev)1624cdb73db0SGoldwyn Rodrigues int mthca_SYNC_TPT(struct mthca_dev *dev)
1625b8ca06f6SMichael S. Tsirkin {
1626cdb73db0SGoldwyn Rodrigues 	return mthca_cmd(dev, 0, 0, 0, CMD_SYNC_TPT, CMD_TIME_CLASS_B);
1627b8ca06f6SMichael S. Tsirkin }
1628b8ca06f6SMichael S. Tsirkin 
mthca_MAP_EQ(struct mthca_dev * dev,u64 event_mask,int unmap,int eq_num)16291da177e4SLinus Torvalds int mthca_MAP_EQ(struct mthca_dev *dev, u64 event_mask, int unmap,
1630cdb73db0SGoldwyn Rodrigues 		 int eq_num)
16311da177e4SLinus Torvalds {
16321da177e4SLinus Torvalds 	mthca_dbg(dev, "%s mask %016llx for eqn %d\n",
16331da177e4SLinus Torvalds 		  unmap ? "Clearing" : "Setting",
16341da177e4SLinus Torvalds 		  (unsigned long long) event_mask, eq_num);
16351da177e4SLinus Torvalds 	return mthca_cmd(dev, event_mask, (unmap << 31) | eq_num,
1636cdb73db0SGoldwyn Rodrigues 			 0, CMD_MAP_EQ, CMD_TIME_CLASS_B);
16371da177e4SLinus Torvalds }
16381da177e4SLinus Torvalds 
mthca_SW2HW_EQ(struct mthca_dev * dev,struct mthca_mailbox * mailbox,int eq_num)1639ed878458SRoland Dreier int mthca_SW2HW_EQ(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1640cdb73db0SGoldwyn Rodrigues 		   int eq_num)
16411da177e4SLinus Torvalds {
1642ed878458SRoland Dreier 	return mthca_cmd(dev, mailbox->dma, eq_num, 0, CMD_SW2HW_EQ,
1643cdb73db0SGoldwyn Rodrigues 			 CMD_TIME_CLASS_A);
16441da177e4SLinus Torvalds }
16451da177e4SLinus Torvalds 
mthca_HW2SW_EQ(struct mthca_dev * dev,struct mthca_mailbox * mailbox,int eq_num)1646ed878458SRoland Dreier int mthca_HW2SW_EQ(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1647cdb73db0SGoldwyn Rodrigues 		   int eq_num)
16481da177e4SLinus Torvalds {
1649ed878458SRoland Dreier 	return mthca_cmd_box(dev, 0, mailbox->dma, eq_num, 0,
16501da177e4SLinus Torvalds 			     CMD_HW2SW_EQ,
1651cdb73db0SGoldwyn Rodrigues 			     CMD_TIME_CLASS_A);
16521da177e4SLinus Torvalds }
16531da177e4SLinus Torvalds 
mthca_SW2HW_CQ(struct mthca_dev * dev,struct mthca_mailbox * mailbox,int cq_num)1654ed878458SRoland Dreier int mthca_SW2HW_CQ(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1655cdb73db0SGoldwyn Rodrigues 		   int cq_num)
16561da177e4SLinus Torvalds {
1657ed878458SRoland Dreier 	return mthca_cmd(dev, mailbox->dma, cq_num, 0, CMD_SW2HW_CQ,
1658cdb73db0SGoldwyn Rodrigues 			CMD_TIME_CLASS_A);
16591da177e4SLinus Torvalds }
16601da177e4SLinus Torvalds 
mthca_HW2SW_CQ(struct mthca_dev * dev,struct mthca_mailbox * mailbox,int cq_num)1661ed878458SRoland Dreier int mthca_HW2SW_CQ(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1662cdb73db0SGoldwyn Rodrigues 		   int cq_num)
16631da177e4SLinus Torvalds {
1664ed878458SRoland Dreier 	return mthca_cmd_box(dev, 0, mailbox->dma, cq_num, 0,
16651da177e4SLinus Torvalds 			     CMD_HW2SW_CQ,
1666cdb73db0SGoldwyn Rodrigues 			     CMD_TIME_CLASS_A);
16671da177e4SLinus Torvalds }
16681da177e4SLinus Torvalds 
mthca_RESIZE_CQ(struct mthca_dev * dev,int cq_num,u32 lkey,u8 log_size)1669cdb73db0SGoldwyn Rodrigues int mthca_RESIZE_CQ(struct mthca_dev *dev, int cq_num, u32 lkey, u8 log_size)
16704885bf64SRoland Dreier {
16714885bf64SRoland Dreier 	struct mthca_mailbox *mailbox;
16724885bf64SRoland Dreier 	__be32 *inbox;
16734885bf64SRoland Dreier 	int err;
16744885bf64SRoland Dreier 
16754885bf64SRoland Dreier #define RESIZE_CQ_IN_SIZE		0x40
16764885bf64SRoland Dreier #define RESIZE_CQ_LOG_SIZE_OFFSET	0x0c
16774885bf64SRoland Dreier #define RESIZE_CQ_LKEY_OFFSET		0x1c
16784885bf64SRoland Dreier 
16794885bf64SRoland Dreier 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
16804885bf64SRoland Dreier 	if (IS_ERR(mailbox))
16814885bf64SRoland Dreier 		return PTR_ERR(mailbox);
16824885bf64SRoland Dreier 	inbox = mailbox->buf;
16834885bf64SRoland Dreier 
16844885bf64SRoland Dreier 	memset(inbox, 0, RESIZE_CQ_IN_SIZE);
16854885bf64SRoland Dreier 	/*
16864885bf64SRoland Dreier 	 * Leave start address fields zeroed out -- mthca assumes that
16874885bf64SRoland Dreier 	 * MRs for CQs always start at virtual address 0.
16884885bf64SRoland Dreier 	 */
16894885bf64SRoland Dreier 	MTHCA_PUT(inbox, log_size, RESIZE_CQ_LOG_SIZE_OFFSET);
16904885bf64SRoland Dreier 	MTHCA_PUT(inbox, lkey,     RESIZE_CQ_LKEY_OFFSET);
16914885bf64SRoland Dreier 
16924885bf64SRoland Dreier 	err = mthca_cmd(dev, mailbox->dma, cq_num, 1, CMD_RESIZE_CQ,
1693cdb73db0SGoldwyn Rodrigues 			CMD_TIME_CLASS_B);
16944885bf64SRoland Dreier 
16954885bf64SRoland Dreier 	mthca_free_mailbox(dev, mailbox);
16964885bf64SRoland Dreier 	return err;
16974885bf64SRoland Dreier }
16984885bf64SRoland Dreier 
mthca_SW2HW_SRQ(struct mthca_dev * dev,struct mthca_mailbox * mailbox,int srq_num)1699ec34a922SRoland Dreier int mthca_SW2HW_SRQ(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1700cdb73db0SGoldwyn Rodrigues 		    int srq_num)
1701ec34a922SRoland Dreier {
1702ec34a922SRoland Dreier 	return mthca_cmd(dev, mailbox->dma, srq_num, 0, CMD_SW2HW_SRQ,
1703cdb73db0SGoldwyn Rodrigues 			CMD_TIME_CLASS_A);
1704ec34a922SRoland Dreier }
1705ec34a922SRoland Dreier 
mthca_HW2SW_SRQ(struct mthca_dev * dev,struct mthca_mailbox * mailbox,int srq_num)1706ec34a922SRoland Dreier int mthca_HW2SW_SRQ(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1707cdb73db0SGoldwyn Rodrigues 		    int srq_num)
1708ec34a922SRoland Dreier {
1709ec34a922SRoland Dreier 	return mthca_cmd_box(dev, 0, mailbox->dma, srq_num, 0,
1710ec34a922SRoland Dreier 			     CMD_HW2SW_SRQ,
1711cdb73db0SGoldwyn Rodrigues 			     CMD_TIME_CLASS_A);
1712ec34a922SRoland Dreier }
1713ec34a922SRoland Dreier 
mthca_QUERY_SRQ(struct mthca_dev * dev,u32 num,struct mthca_mailbox * mailbox)17148ebe5077SEli Cohen int mthca_QUERY_SRQ(struct mthca_dev *dev, u32 num,
1715cdb73db0SGoldwyn Rodrigues 		    struct mthca_mailbox *mailbox)
17168ebe5077SEli Cohen {
17178ebe5077SEli Cohen 	return mthca_cmd_box(dev, 0, mailbox->dma, num, 0,
1718cdb73db0SGoldwyn Rodrigues 			     CMD_QUERY_SRQ, CMD_TIME_CLASS_A);
17198ebe5077SEli Cohen }
17208ebe5077SEli Cohen 
mthca_ARM_SRQ(struct mthca_dev * dev,int srq_num,int limit)1721cdb73db0SGoldwyn Rodrigues int mthca_ARM_SRQ(struct mthca_dev *dev, int srq_num, int limit)
1722ec34a922SRoland Dreier {
1723ec34a922SRoland Dreier 	return mthca_cmd(dev, limit, srq_num, 0, CMD_ARM_SRQ,
1724cdb73db0SGoldwyn Rodrigues 			 CMD_TIME_CLASS_B);
1725ec34a922SRoland Dreier }
1726ec34a922SRoland Dreier 
mthca_MODIFY_QP(struct mthca_dev * dev,enum ib_qp_state cur,enum ib_qp_state next,u32 num,int is_ee,struct mthca_mailbox * mailbox,u32 optmask)1727d844183dSRoland Dreier int mthca_MODIFY_QP(struct mthca_dev *dev, enum ib_qp_state cur,
1728d844183dSRoland Dreier 		    enum ib_qp_state next, u32 num, int is_ee,
1729cdb73db0SGoldwyn Rodrigues 		    struct mthca_mailbox *mailbox, u32 optmask)
17301da177e4SLinus Torvalds {
1731d844183dSRoland Dreier 	static const u16 op[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
1732d844183dSRoland Dreier 		[IB_QPS_RESET] = {
1733d844183dSRoland Dreier 			[IB_QPS_RESET]	= CMD_ERR2RST_QPEE,
1734d844183dSRoland Dreier 			[IB_QPS_ERR]	= CMD_2ERR_QPEE,
1735d844183dSRoland Dreier 			[IB_QPS_INIT]	= CMD_RST2INIT_QPEE,
1736d844183dSRoland Dreier 		},
1737d844183dSRoland Dreier 		[IB_QPS_INIT]  = {
1738d844183dSRoland Dreier 			[IB_QPS_RESET]	= CMD_ERR2RST_QPEE,
1739d844183dSRoland Dreier 			[IB_QPS_ERR]	= CMD_2ERR_QPEE,
1740d844183dSRoland Dreier 			[IB_QPS_INIT]	= CMD_INIT2INIT_QPEE,
1741d844183dSRoland Dreier 			[IB_QPS_RTR]	= CMD_INIT2RTR_QPEE,
1742d844183dSRoland Dreier 		},
1743d844183dSRoland Dreier 		[IB_QPS_RTR]   = {
1744d844183dSRoland Dreier 			[IB_QPS_RESET]	= CMD_ERR2RST_QPEE,
1745d844183dSRoland Dreier 			[IB_QPS_ERR]	= CMD_2ERR_QPEE,
1746d844183dSRoland Dreier 			[IB_QPS_RTS]	= CMD_RTR2RTS_QPEE,
1747d844183dSRoland Dreier 		},
1748d844183dSRoland Dreier 		[IB_QPS_RTS]   = {
1749d844183dSRoland Dreier 			[IB_QPS_RESET]	= CMD_ERR2RST_QPEE,
1750d844183dSRoland Dreier 			[IB_QPS_ERR]	= CMD_2ERR_QPEE,
1751d844183dSRoland Dreier 			[IB_QPS_RTS]	= CMD_RTS2RTS_QPEE,
1752d844183dSRoland Dreier 			[IB_QPS_SQD]	= CMD_RTS2SQD_QPEE,
1753d844183dSRoland Dreier 		},
1754d844183dSRoland Dreier 		[IB_QPS_SQD] = {
1755d844183dSRoland Dreier 			[IB_QPS_RESET]	= CMD_ERR2RST_QPEE,
1756d844183dSRoland Dreier 			[IB_QPS_ERR]	= CMD_2ERR_QPEE,
1757d844183dSRoland Dreier 			[IB_QPS_RTS]	= CMD_SQD2RTS_QPEE,
1758d844183dSRoland Dreier 			[IB_QPS_SQD]	= CMD_SQD2SQD_QPEE,
1759d844183dSRoland Dreier 		},
1760d844183dSRoland Dreier 		[IB_QPS_SQE] = {
1761d844183dSRoland Dreier 			[IB_QPS_RESET]	= CMD_ERR2RST_QPEE,
1762d844183dSRoland Dreier 			[IB_QPS_ERR]	= CMD_2ERR_QPEE,
1763d844183dSRoland Dreier 			[IB_QPS_RTS]	= CMD_SQERR2RTS_QPEE,
1764d844183dSRoland Dreier 		},
1765d844183dSRoland Dreier 		[IB_QPS_ERR] = {
1766d844183dSRoland Dreier 			[IB_QPS_RESET]	= CMD_ERR2RST_QPEE,
1767d844183dSRoland Dreier 			[IB_QPS_ERR]	= CMD_2ERR_QPEE,
1768d844183dSRoland Dreier 		}
17691da177e4SLinus Torvalds 	};
1770d844183dSRoland Dreier 
17711da177e4SLinus Torvalds 	u8 op_mod = 0;
1772ed878458SRoland Dreier 	int my_mailbox = 0;
17731da177e4SLinus Torvalds 	int err;
17741da177e4SLinus Torvalds 
1775d844183dSRoland Dreier 	if (op[cur][next] == CMD_ERR2RST_QPEE) {
17761da177e4SLinus Torvalds 		op_mod = 3;	/* don't write outbox, any->reset */
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 		/* For debugging */
1779ed878458SRoland Dreier 		if (!mailbox) {
1780ed878458SRoland Dreier 			mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1781ed878458SRoland Dreier 			if (!IS_ERR(mailbox)) {
1782ed878458SRoland Dreier 				my_mailbox = 1;
17831da177e4SLinus Torvalds 				op_mod     = 2;	/* write outbox, any->reset */
1784ed878458SRoland Dreier 			} else
1785ed878458SRoland Dreier 				mailbox = NULL;
1786ed878458SRoland Dreier 		}
17871da177e4SLinus Torvalds 
1788ed878458SRoland Dreier 		err = mthca_cmd_box(dev, 0, mailbox ? mailbox->dma : 0,
1789ed878458SRoland Dreier 				    (!!is_ee << 24) | num, op_mod,
1790cdb73db0SGoldwyn Rodrigues 				    op[cur][next], CMD_TIME_CLASS_C);
17911da177e4SLinus Torvalds 
1792ed878458SRoland Dreier 		if (0 && mailbox) {
17931da177e4SLinus Torvalds 			int i;
17941da177e4SLinus Torvalds 			mthca_dbg(dev, "Dumping QP context:\n");
1795ed878458SRoland Dreier 			printk(" %08x\n", be32_to_cpup(mailbox->buf));
17961da177e4SLinus Torvalds 			for (i = 0; i < 0x100 / 4; ++i) {
17971da177e4SLinus Torvalds 				if (i % 8 == 0)
17981da177e4SLinus Torvalds 					printk("[%02x] ", i * 4);
1799ed878458SRoland Dreier 				printk(" %08x",
180097f52eb4SSean Hefty 				       be32_to_cpu(((__be32 *) mailbox->buf)[i + 2]));
18011da177e4SLinus Torvalds 				if ((i + 1) % 8 == 0)
18021da177e4SLinus Torvalds 					printk("\n");
18031da177e4SLinus Torvalds 			}
18041da177e4SLinus Torvalds 		}
18051da177e4SLinus Torvalds 
1806ed878458SRoland Dreier 		if (my_mailbox)
1807ed878458SRoland Dreier 			mthca_free_mailbox(dev, mailbox);
1808d844183dSRoland Dreier 	} else {
1809d844183dSRoland Dreier 		if (0) {
1810d844183dSRoland Dreier 			int i;
1811d844183dSRoland Dreier 			mthca_dbg(dev, "Dumping QP context:\n");
1812d844183dSRoland Dreier 			printk("  opt param mask: %08x\n", be32_to_cpup(mailbox->buf));
1813d844183dSRoland Dreier 			for (i = 0; i < 0x100 / 4; ++i) {
1814d844183dSRoland Dreier 				if (i % 8 == 0)
1815d844183dSRoland Dreier 					printk("  [%02x] ", i * 4);
1816d844183dSRoland Dreier 				printk(" %08x",
1817d844183dSRoland Dreier 				       be32_to_cpu(((__be32 *) mailbox->buf)[i + 2]));
1818d844183dSRoland Dreier 				if ((i + 1) % 8 == 0)
1819d844183dSRoland Dreier 					printk("\n");
1820d844183dSRoland Dreier 			}
1821d844183dSRoland Dreier 		}
1822d844183dSRoland Dreier 
1823d844183dSRoland Dreier 		err = mthca_cmd(dev, mailbox->dma, optmask | (!!is_ee << 24) | num,
1824cdb73db0SGoldwyn Rodrigues 				op_mod, op[cur][next], CMD_TIME_CLASS_C);
1825d844183dSRoland Dreier 	}
1826ed878458SRoland Dreier 
18271da177e4SLinus Torvalds 	return err;
18281da177e4SLinus Torvalds }
18291da177e4SLinus Torvalds 
mthca_QUERY_QP(struct mthca_dev * dev,u32 num,int is_ee,struct mthca_mailbox * mailbox)18301da177e4SLinus Torvalds int mthca_QUERY_QP(struct mthca_dev *dev, u32 num, int is_ee,
1831cdb73db0SGoldwyn Rodrigues 		   struct mthca_mailbox *mailbox)
18321da177e4SLinus Torvalds {
1833ed878458SRoland Dreier 	return mthca_cmd_box(dev, 0, mailbox->dma, (!!is_ee << 24) | num, 0,
1834cdb73db0SGoldwyn Rodrigues 			     CMD_QUERY_QPEE, CMD_TIME_CLASS_A);
18351da177e4SLinus Torvalds }
18361da177e4SLinus Torvalds 
mthca_CONF_SPECIAL_QP(struct mthca_dev * dev,int type,u32 qpn)1837cdb73db0SGoldwyn Rodrigues int mthca_CONF_SPECIAL_QP(struct mthca_dev *dev, int type, u32 qpn)
18381da177e4SLinus Torvalds {
18391da177e4SLinus Torvalds 	u8 op_mod;
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	switch (type) {
18421da177e4SLinus Torvalds 	case IB_QPT_SMI:
18431da177e4SLinus Torvalds 		op_mod = 0;
18441da177e4SLinus Torvalds 		break;
18451da177e4SLinus Torvalds 	case IB_QPT_GSI:
18461da177e4SLinus Torvalds 		op_mod = 1;
18471da177e4SLinus Torvalds 		break;
18481da177e4SLinus Torvalds 	case IB_QPT_RAW_IPV6:
18491da177e4SLinus Torvalds 		op_mod = 2;
18501da177e4SLinus Torvalds 		break;
1851a2ebf07aSAleksey Senin 	case IB_QPT_RAW_ETHERTYPE:
18521da177e4SLinus Torvalds 		op_mod = 3;
18531da177e4SLinus Torvalds 		break;
18541da177e4SLinus Torvalds 	default:
18551da177e4SLinus Torvalds 		return -EINVAL;
18561da177e4SLinus Torvalds 	}
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	return mthca_cmd(dev, 0, qpn, op_mod, CMD_CONF_SPECIAL_QP,
1859cdb73db0SGoldwyn Rodrigues 			 CMD_TIME_CLASS_B);
18601da177e4SLinus Torvalds }
18611da177e4SLinus Torvalds 
mthca_MAD_IFC(struct mthca_dev * dev,int ignore_mkey,int ignore_bkey,int port,const struct ib_wc * in_wc,const struct ib_grh * in_grh,const void * in_mad,void * response_mad)18621da177e4SLinus Torvalds int mthca_MAD_IFC(struct mthca_dev *dev, int ignore_mkey, int ignore_bkey,
1863a97e2d86SIra Weiny 		  int port, const struct ib_wc *in_wc, const struct ib_grh *in_grh,
1864a97e2d86SIra Weiny 		  const void *in_mad, void *response_mad)
18651da177e4SLinus Torvalds {
1866ed878458SRoland Dreier 	struct mthca_mailbox *inmailbox, *outmailbox;
1867ed878458SRoland Dreier 	void *inbox;
18681da177e4SLinus Torvalds 	int err;
18691da177e4SLinus Torvalds 	u32 in_modifier = port;
18701da177e4SLinus Torvalds 	u8 op_modifier = 0;
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds #define MAD_IFC_BOX_SIZE      0x400
18731da177e4SLinus Torvalds #define MAD_IFC_MY_QPN_OFFSET 0x100
187468586b67SMichael S. Tsirkin #define MAD_IFC_RQPN_OFFSET   0x108
187568586b67SMichael S. Tsirkin #define MAD_IFC_SL_OFFSET     0x10c
187668586b67SMichael S. Tsirkin #define MAD_IFC_G_PATH_OFFSET 0x10d
187768586b67SMichael S. Tsirkin #define MAD_IFC_RLID_OFFSET   0x10e
187868586b67SMichael S. Tsirkin #define MAD_IFC_PKEY_OFFSET   0x112
18791da177e4SLinus Torvalds #define MAD_IFC_GRH_OFFSET    0x140
18801da177e4SLinus Torvalds 
1881ed878458SRoland Dreier 	inmailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1882ed878458SRoland Dreier 	if (IS_ERR(inmailbox))
1883ed878458SRoland Dreier 		return PTR_ERR(inmailbox);
1884ed878458SRoland Dreier 	inbox = inmailbox->buf;
18851da177e4SLinus Torvalds 
1886ed878458SRoland Dreier 	outmailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1887ed878458SRoland Dreier 	if (IS_ERR(outmailbox)) {
1888ed878458SRoland Dreier 		mthca_free_mailbox(dev, inmailbox);
1889ed878458SRoland Dreier 		return PTR_ERR(outmailbox);
1890ed878458SRoland Dreier 	}
1891ed878458SRoland Dreier 
1892ed878458SRoland Dreier 	memcpy(inbox, in_mad, 256);
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 	/*
18951da177e4SLinus Torvalds 	 * Key check traps can't be generated unless we have in_wc to
18961da177e4SLinus Torvalds 	 * tell us where to send the trap.
18971da177e4SLinus Torvalds 	 */
18981da177e4SLinus Torvalds 	if (ignore_mkey || !in_wc)
18991da177e4SLinus Torvalds 		op_modifier |= 0x1;
19001da177e4SLinus Torvalds 	if (ignore_bkey || !in_wc)
19011da177e4SLinus Torvalds 		op_modifier |= 0x2;
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 	if (in_wc) {
19041da177e4SLinus Torvalds 		u8 val;
19051da177e4SLinus Torvalds 
1906ed878458SRoland Dreier 		memset(inbox + 256, 0, 256);
19071da177e4SLinus Torvalds 
1908062dbb69SMichael S. Tsirkin 		MTHCA_PUT(inbox, in_wc->qp->qp_num, MAD_IFC_MY_QPN_OFFSET);
1909ed878458SRoland Dreier 		MTHCA_PUT(inbox, in_wc->src_qp,     MAD_IFC_RQPN_OFFSET);
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 		val = in_wc->sl << 4;
1912ed878458SRoland Dreier 		MTHCA_PUT(inbox, val,               MAD_IFC_SL_OFFSET);
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 		val = in_wc->dlid_path_bits |
19151da177e4SLinus Torvalds 			(in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
191668586b67SMichael S. Tsirkin 		MTHCA_PUT(inbox, val,               MAD_IFC_G_PATH_OFFSET);
19171da177e4SLinus Torvalds 
191862ede777SHiatt, Don 		MTHCA_PUT(inbox, ib_lid_cpu16(in_wc->slid), MAD_IFC_RLID_OFFSET);
1919ed878458SRoland Dreier 		MTHCA_PUT(inbox, in_wc->pkey_index, MAD_IFC_PKEY_OFFSET);
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 		if (in_grh)
1922ed878458SRoland Dreier 			memcpy(inbox + MAD_IFC_GRH_OFFSET, in_grh, 40);
19231da177e4SLinus Torvalds 
192468586b67SMichael S. Tsirkin 		op_modifier |= 0x4;
19251da177e4SLinus Torvalds 
192662ede777SHiatt, Don 		in_modifier |= ib_lid_cpu16(in_wc->slid) << 16;
19271da177e4SLinus Torvalds 	}
19281da177e4SLinus Torvalds 
1929ed878458SRoland Dreier 	err = mthca_cmd_box(dev, inmailbox->dma, outmailbox->dma,
1930ed878458SRoland Dreier 			    in_modifier, op_modifier,
1931cdb73db0SGoldwyn Rodrigues 			    CMD_MAD_IFC, CMD_TIME_CLASS_C);
19321da177e4SLinus Torvalds 
1933cdb73db0SGoldwyn Rodrigues 	if (!err)
1934ed878458SRoland Dreier 		memcpy(response_mad, outmailbox->buf, 256);
19351da177e4SLinus Torvalds 
1936ed878458SRoland Dreier 	mthca_free_mailbox(dev, inmailbox);
1937ed878458SRoland Dreier 	mthca_free_mailbox(dev, outmailbox);
19381da177e4SLinus Torvalds 	return err;
19391da177e4SLinus Torvalds }
19401da177e4SLinus Torvalds 
mthca_READ_MGM(struct mthca_dev * dev,int index,struct mthca_mailbox * mailbox)1941ed878458SRoland Dreier int mthca_READ_MGM(struct mthca_dev *dev, int index,
1942cdb73db0SGoldwyn Rodrigues 		   struct mthca_mailbox *mailbox)
19431da177e4SLinus Torvalds {
1944ed878458SRoland Dreier 	return mthca_cmd_box(dev, 0, mailbox->dma, index, 0,
1945cdb73db0SGoldwyn Rodrigues 			     CMD_READ_MGM, CMD_TIME_CLASS_A);
1946ed878458SRoland Dreier }
19471da177e4SLinus Torvalds 
mthca_WRITE_MGM(struct mthca_dev * dev,int index,struct mthca_mailbox * mailbox)1948ed878458SRoland Dreier int mthca_WRITE_MGM(struct mthca_dev *dev, int index,
1949cdb73db0SGoldwyn Rodrigues 		    struct mthca_mailbox *mailbox)
1950ed878458SRoland Dreier {
1951ed878458SRoland Dreier 	return mthca_cmd(dev, mailbox->dma, index, 0, CMD_WRITE_MGM,
1952cdb73db0SGoldwyn Rodrigues 			 CMD_TIME_CLASS_A);
19531da177e4SLinus Torvalds }
19541da177e4SLinus Torvalds 
mthca_MGID_HASH(struct mthca_dev * dev,struct mthca_mailbox * mailbox,u16 * hash)1955ed878458SRoland Dreier int mthca_MGID_HASH(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1956cdb73db0SGoldwyn Rodrigues 		    u16 *hash)
19571da177e4SLinus Torvalds {
1958*fff32018SLeon Romanovsky 	u64 imm = 0;
19591da177e4SLinus Torvalds 	int err;
19601da177e4SLinus Torvalds 
1961ed878458SRoland Dreier 	err = mthca_cmd_imm(dev, mailbox->dma, &imm, 0, 0, CMD_MGID_HASH,
1962cdb73db0SGoldwyn Rodrigues 			    CMD_TIME_CLASS_A);
19631da177e4SLinus Torvalds 
1964ed878458SRoland Dreier 	*hash = imm;
19651da177e4SLinus Torvalds 	return err;
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
mthca_NOP(struct mthca_dev * dev)1968cdb73db0SGoldwyn Rodrigues int mthca_NOP(struct mthca_dev *dev)
19691da177e4SLinus Torvalds {
1970cdb73db0SGoldwyn Rodrigues 	return mthca_cmd(dev, 0, 0x1f, 0, CMD_NOP, msecs_to_jiffies(100));
19711da177e4SLinus Torvalds }
1972