xref: /openbmc/linux/drivers/infiniband/hw/mthca/mthca_cmd.c (revision d10ddbf6d7f6699c386d1f41bf542189de32b6be)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * This software is available to you under a choice of one of two
51da177e4SLinus Torvalds  * licenses.  You may choose to be licensed under the terms of the GNU
61da177e4SLinus Torvalds  * General Public License (GPL) Version 2, available from the file
71da177e4SLinus Torvalds  * COPYING in the main directory of this source tree, or the
81da177e4SLinus Torvalds  * OpenIB.org BSD license below:
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *     Redistribution and use in source and binary forms, with or
111da177e4SLinus Torvalds  *     without modification, are permitted provided that the following
121da177e4SLinus Torvalds  *     conditions are met:
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *      - Redistributions of source code must retain the above
151da177e4SLinus Torvalds  *        copyright notice, this list of conditions and the following
161da177e4SLinus Torvalds  *        disclaimer.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *      - Redistributions in binary form must reproduce the above
191da177e4SLinus Torvalds  *        copyright notice, this list of conditions and the following
201da177e4SLinus Torvalds  *        disclaimer in the documentation and/or other materials
211da177e4SLinus Torvalds  *        provided with the distribution.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
241da177e4SLinus Torvalds  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
251da177e4SLinus Torvalds  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
261da177e4SLinus Torvalds  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
271da177e4SLinus Torvalds  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
281da177e4SLinus Torvalds  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
291da177e4SLinus Torvalds  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
301da177e4SLinus Torvalds  * SOFTWARE.
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  * $Id: mthca_cmd.c 1349 2004-12-16 21:09:43Z roland $
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include <linux/sched.h>
361da177e4SLinus Torvalds #include <linux/pci.h>
371da177e4SLinus Torvalds #include <linux/errno.h>
381da177e4SLinus Torvalds #include <asm/io.h>
391da177e4SLinus Torvalds #include <ib_mad.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include "mthca_dev.h"
421da177e4SLinus Torvalds #include "mthca_config_reg.h"
431da177e4SLinus Torvalds #include "mthca_cmd.h"
441da177e4SLinus Torvalds #include "mthca_memfree.h"
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #define CMD_POLL_TOKEN 0xffff
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds enum {
491da177e4SLinus Torvalds 	HCR_IN_PARAM_OFFSET    = 0x00,
501da177e4SLinus Torvalds 	HCR_IN_MODIFIER_OFFSET = 0x08,
511da177e4SLinus Torvalds 	HCR_OUT_PARAM_OFFSET   = 0x0c,
521da177e4SLinus Torvalds 	HCR_TOKEN_OFFSET       = 0x14,
531da177e4SLinus Torvalds 	HCR_STATUS_OFFSET      = 0x18,
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	HCR_OPMOD_SHIFT        = 12,
561da177e4SLinus Torvalds 	HCA_E_BIT              = 22,
571da177e4SLinus Torvalds 	HCR_GO_BIT             = 23
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds enum {
611da177e4SLinus Torvalds 	/* initialization and general commands */
621da177e4SLinus Torvalds 	CMD_SYS_EN          = 0x1,
631da177e4SLinus Torvalds 	CMD_SYS_DIS         = 0x2,
641da177e4SLinus Torvalds 	CMD_MAP_FA          = 0xfff,
651da177e4SLinus Torvalds 	CMD_UNMAP_FA        = 0xffe,
661da177e4SLinus Torvalds 	CMD_RUN_FW          = 0xff6,
671da177e4SLinus Torvalds 	CMD_MOD_STAT_CFG    = 0x34,
681da177e4SLinus Torvalds 	CMD_QUERY_DEV_LIM   = 0x3,
691da177e4SLinus Torvalds 	CMD_QUERY_FW        = 0x4,
701da177e4SLinus Torvalds 	CMD_ENABLE_LAM      = 0xff8,
711da177e4SLinus Torvalds 	CMD_DISABLE_LAM     = 0xff7,
721da177e4SLinus Torvalds 	CMD_QUERY_DDR       = 0x5,
731da177e4SLinus Torvalds 	CMD_QUERY_ADAPTER   = 0x6,
741da177e4SLinus Torvalds 	CMD_INIT_HCA        = 0x7,
751da177e4SLinus Torvalds 	CMD_CLOSE_HCA       = 0x8,
761da177e4SLinus Torvalds 	CMD_INIT_IB         = 0x9,
771da177e4SLinus Torvalds 	CMD_CLOSE_IB        = 0xa,
781da177e4SLinus Torvalds 	CMD_QUERY_HCA       = 0xb,
791da177e4SLinus Torvalds 	CMD_SET_IB          = 0xc,
801da177e4SLinus Torvalds 	CMD_ACCESS_DDR      = 0x2e,
811da177e4SLinus Torvalds 	CMD_MAP_ICM         = 0xffa,
821da177e4SLinus Torvalds 	CMD_UNMAP_ICM       = 0xff9,
831da177e4SLinus Torvalds 	CMD_MAP_ICM_AUX     = 0xffc,
841da177e4SLinus Torvalds 	CMD_UNMAP_ICM_AUX   = 0xffb,
851da177e4SLinus Torvalds 	CMD_SET_ICM_SIZE    = 0xffd,
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	/* TPT commands */
881da177e4SLinus Torvalds 	CMD_SW2HW_MPT 	    = 0xd,
891da177e4SLinus Torvalds 	CMD_QUERY_MPT 	    = 0xe,
901da177e4SLinus Torvalds 	CMD_HW2SW_MPT 	    = 0xf,
911da177e4SLinus Torvalds 	CMD_READ_MTT        = 0x10,
921da177e4SLinus Torvalds 	CMD_WRITE_MTT       = 0x11,
931da177e4SLinus Torvalds 	CMD_SYNC_TPT        = 0x2f,
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	/* EQ commands */
961da177e4SLinus Torvalds 	CMD_MAP_EQ          = 0x12,
971da177e4SLinus Torvalds 	CMD_SW2HW_EQ 	    = 0x13,
981da177e4SLinus Torvalds 	CMD_HW2SW_EQ 	    = 0x14,
991da177e4SLinus Torvalds 	CMD_QUERY_EQ        = 0x15,
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	/* CQ commands */
1021da177e4SLinus Torvalds 	CMD_SW2HW_CQ 	    = 0x16,
1031da177e4SLinus Torvalds 	CMD_HW2SW_CQ 	    = 0x17,
1041da177e4SLinus Torvalds 	CMD_QUERY_CQ 	    = 0x18,
1051da177e4SLinus Torvalds 	CMD_RESIZE_CQ       = 0x2c,
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	/* SRQ commands */
1081da177e4SLinus Torvalds 	CMD_SW2HW_SRQ 	    = 0x35,
1091da177e4SLinus Torvalds 	CMD_HW2SW_SRQ 	    = 0x36,
1101da177e4SLinus Torvalds 	CMD_QUERY_SRQ       = 0x37,
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	/* QP/EE commands */
1131da177e4SLinus Torvalds 	CMD_RST2INIT_QPEE   = 0x19,
1141da177e4SLinus Torvalds 	CMD_INIT2RTR_QPEE   = 0x1a,
1151da177e4SLinus Torvalds 	CMD_RTR2RTS_QPEE    = 0x1b,
1161da177e4SLinus Torvalds 	CMD_RTS2RTS_QPEE    = 0x1c,
1171da177e4SLinus Torvalds 	CMD_SQERR2RTS_QPEE  = 0x1d,
1181da177e4SLinus Torvalds 	CMD_2ERR_QPEE       = 0x1e,
1191da177e4SLinus Torvalds 	CMD_RTS2SQD_QPEE    = 0x1f,
1201da177e4SLinus Torvalds 	CMD_SQD2SQD_QPEE    = 0x38,
1211da177e4SLinus Torvalds 	CMD_SQD2RTS_QPEE    = 0x20,
1221da177e4SLinus Torvalds 	CMD_ERR2RST_QPEE    = 0x21,
1231da177e4SLinus Torvalds 	CMD_QUERY_QPEE      = 0x22,
1241da177e4SLinus Torvalds 	CMD_INIT2INIT_QPEE  = 0x2d,
1251da177e4SLinus Torvalds 	CMD_SUSPEND_QPEE    = 0x32,
1261da177e4SLinus Torvalds 	CMD_UNSUSPEND_QPEE  = 0x33,
1271da177e4SLinus Torvalds 	/* special QPs and management commands */
1281da177e4SLinus Torvalds 	CMD_CONF_SPECIAL_QP = 0x23,
1291da177e4SLinus Torvalds 	CMD_MAD_IFC         = 0x24,
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	/* multicast commands */
1321da177e4SLinus Torvalds 	CMD_READ_MGM        = 0x25,
1331da177e4SLinus Torvalds 	CMD_WRITE_MGM       = 0x26,
1341da177e4SLinus Torvalds 	CMD_MGID_HASH       = 0x27,
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	/* miscellaneous commands */
1371da177e4SLinus Torvalds 	CMD_DIAG_RPRT       = 0x30,
1381da177e4SLinus Torvalds 	CMD_NOP             = 0x31,
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	/* debug commands */
1411da177e4SLinus Torvalds 	CMD_QUERY_DEBUG_MSG = 0x2a,
1421da177e4SLinus Torvalds 	CMD_SET_DEBUG_MSG   = 0x2b,
1431da177e4SLinus Torvalds };
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /*
1461da177e4SLinus Torvalds  * According to Mellanox code, FW may be starved and never complete
1471da177e4SLinus Torvalds  * commands.  So we can't use strict timeouts described in PRM -- we
1481da177e4SLinus Torvalds  * just arbitrarily select 60 seconds for now.
1491da177e4SLinus Torvalds  */
1501da177e4SLinus Torvalds #if 0
1511da177e4SLinus Torvalds /*
1521da177e4SLinus Torvalds  * Round up and add 1 to make sure we get the full wait time (since we
1531da177e4SLinus Torvalds  * will be starting in the middle of a jiffy)
1541da177e4SLinus Torvalds  */
1551da177e4SLinus Torvalds enum {
1561da177e4SLinus Torvalds 	CMD_TIME_CLASS_A = (HZ + 999) / 1000 + 1,
1571da177e4SLinus Torvalds 	CMD_TIME_CLASS_B = (HZ +  99) /  100 + 1,
1581da177e4SLinus Torvalds 	CMD_TIME_CLASS_C = (HZ +   9) /   10 + 1
1591da177e4SLinus Torvalds };
1601da177e4SLinus Torvalds #else
1611da177e4SLinus Torvalds enum {
1621da177e4SLinus Torvalds 	CMD_TIME_CLASS_A = 60 * HZ,
1631da177e4SLinus Torvalds 	CMD_TIME_CLASS_B = 60 * HZ,
1641da177e4SLinus Torvalds 	CMD_TIME_CLASS_C = 60 * HZ
1651da177e4SLinus Torvalds };
1661da177e4SLinus Torvalds #endif
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds enum {
1691da177e4SLinus Torvalds 	GO_BIT_TIMEOUT = HZ * 10
1701da177e4SLinus Torvalds };
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds struct mthca_cmd_context {
1731da177e4SLinus Torvalds 	struct completion done;
1741da177e4SLinus Torvalds 	struct timer_list timer;
1751da177e4SLinus Torvalds 	int               result;
1761da177e4SLinus Torvalds 	int               next;
1771da177e4SLinus Torvalds 	u64               out_param;
1781da177e4SLinus Torvalds 	u16               token;
1791da177e4SLinus Torvalds 	u8                status;
1801da177e4SLinus Torvalds };
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds static inline int go_bit(struct mthca_dev *dev)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	return readl(dev->hcr + HCR_STATUS_OFFSET) &
1851da177e4SLinus Torvalds 		swab32(1 << HCR_GO_BIT);
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds static int mthca_cmd_post(struct mthca_dev *dev,
1891da177e4SLinus Torvalds 			  u64 in_param,
1901da177e4SLinus Torvalds 			  u64 out_param,
1911da177e4SLinus Torvalds 			  u32 in_modifier,
1921da177e4SLinus Torvalds 			  u8 op_modifier,
1931da177e4SLinus Torvalds 			  u16 op,
1941da177e4SLinus Torvalds 			  u16 token,
1951da177e4SLinus Torvalds 			  int event)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	int err = 0;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	if (down_interruptible(&dev->cmd.hcr_sem))
2001da177e4SLinus Torvalds 		return -EINTR;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if (event) {
2031da177e4SLinus Torvalds 		unsigned long end = jiffies + GO_BIT_TIMEOUT;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 		while (go_bit(dev) && time_before(jiffies, end)) {
2061da177e4SLinus Torvalds 			set_current_state(TASK_RUNNING);
2071da177e4SLinus Torvalds 			schedule();
2081da177e4SLinus Torvalds 		}
2091da177e4SLinus Torvalds 	}
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	if (go_bit(dev)) {
2121da177e4SLinus Torvalds 		err = -EAGAIN;
2131da177e4SLinus Torvalds 		goto out;
2141da177e4SLinus Torvalds 	}
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	/*
2171da177e4SLinus Torvalds 	 * We use writel (instead of something like memcpy_toio)
2181da177e4SLinus Torvalds 	 * because writes of less than 32 bits to the HCR don't work
2191da177e4SLinus Torvalds 	 * (and some architectures such as ia64 implement memcpy_toio
2201da177e4SLinus Torvalds 	 * in terms of writeb).
2211da177e4SLinus Torvalds 	 */
2221da177e4SLinus Torvalds 	__raw_writel(cpu_to_be32(in_param >> 32),           dev->hcr + 0 * 4);
2231da177e4SLinus Torvalds 	__raw_writel(cpu_to_be32(in_param & 0xfffffffful),  dev->hcr + 1 * 4);
2241da177e4SLinus Torvalds 	__raw_writel(cpu_to_be32(in_modifier),              dev->hcr + 2 * 4);
2251da177e4SLinus Torvalds 	__raw_writel(cpu_to_be32(out_param >> 32),          dev->hcr + 3 * 4);
2261da177e4SLinus Torvalds 	__raw_writel(cpu_to_be32(out_param & 0xfffffffful), dev->hcr + 4 * 4);
2271da177e4SLinus Torvalds 	__raw_writel(cpu_to_be32(token << 16),              dev->hcr + 5 * 4);
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	/* __raw_writel may not order writes. */
2301da177e4SLinus Torvalds 	wmb();
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	__raw_writel(cpu_to_be32((1 << HCR_GO_BIT)                |
2331da177e4SLinus Torvalds 				 (event ? (1 << HCA_E_BIT) : 0)   |
2341da177e4SLinus Torvalds 				 (op_modifier << HCR_OPMOD_SHIFT) |
2351da177e4SLinus Torvalds 				 op),                       dev->hcr + 6 * 4);
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds out:
2381da177e4SLinus Torvalds 	up(&dev->cmd.hcr_sem);
2391da177e4SLinus Torvalds 	return err;
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds static int mthca_cmd_poll(struct mthca_dev *dev,
2431da177e4SLinus Torvalds 			  u64 in_param,
2441da177e4SLinus Torvalds 			  u64 *out_param,
2451da177e4SLinus Torvalds 			  int out_is_imm,
2461da177e4SLinus Torvalds 			  u32 in_modifier,
2471da177e4SLinus Torvalds 			  u8 op_modifier,
2481da177e4SLinus Torvalds 			  u16 op,
2491da177e4SLinus Torvalds 			  unsigned long timeout,
2501da177e4SLinus Torvalds 			  u8 *status)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	int err = 0;
2531da177e4SLinus Torvalds 	unsigned long end;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	if (down_interruptible(&dev->cmd.poll_sem))
2561da177e4SLinus Torvalds 		return -EINTR;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	err = mthca_cmd_post(dev, in_param,
2591da177e4SLinus Torvalds 			     out_param ? *out_param : 0,
2601da177e4SLinus Torvalds 			     in_modifier, op_modifier,
2611da177e4SLinus Torvalds 			     op, CMD_POLL_TOKEN, 0);
2621da177e4SLinus Torvalds 	if (err)
2631da177e4SLinus Torvalds 		goto out;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	end = timeout + jiffies;
2661da177e4SLinus Torvalds 	while (go_bit(dev) && time_before(jiffies, end)) {
2671da177e4SLinus Torvalds 		set_current_state(TASK_RUNNING);
2681da177e4SLinus Torvalds 		schedule();
2691da177e4SLinus Torvalds 	}
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	if (go_bit(dev)) {
2721da177e4SLinus Torvalds 		err = -EBUSY;
2731da177e4SLinus Torvalds 		goto out;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	if (out_is_imm) {
2771da177e4SLinus Torvalds 		memcpy_fromio(out_param, dev->hcr + HCR_OUT_PARAM_OFFSET, sizeof (u64));
2781da177e4SLinus Torvalds 		be64_to_cpus(out_param);
2791da177e4SLinus Torvalds 	}
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	*status = be32_to_cpu(__raw_readl(dev->hcr + HCR_STATUS_OFFSET)) >> 24;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds out:
2841da177e4SLinus Torvalds 	up(&dev->cmd.poll_sem);
2851da177e4SLinus Torvalds 	return err;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds void mthca_cmd_event(struct mthca_dev *dev,
2891da177e4SLinus Torvalds 		     u16 token,
2901da177e4SLinus Torvalds 		     u8  status,
2911da177e4SLinus Torvalds 		     u64 out_param)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	struct mthca_cmd_context *context =
2941da177e4SLinus Torvalds 		&dev->cmd.context[token & dev->cmd.token_mask];
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* previously timed out command completing at long last */
2971da177e4SLinus Torvalds 	if (token != context->token)
2981da177e4SLinus Torvalds 		return;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	context->result    = 0;
3011da177e4SLinus Torvalds 	context->status    = status;
3021da177e4SLinus Torvalds 	context->out_param = out_param;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	context->token += dev->cmd.token_mask + 1;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	complete(&context->done);
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds static void event_timeout(unsigned long context_ptr)
3101da177e4SLinus Torvalds {
3111da177e4SLinus Torvalds 	struct mthca_cmd_context *context =
3121da177e4SLinus Torvalds 		(struct mthca_cmd_context *) context_ptr;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	context->result = -EBUSY;
3151da177e4SLinus Torvalds 	complete(&context->done);
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds static int mthca_cmd_wait(struct mthca_dev *dev,
3191da177e4SLinus Torvalds 			  u64 in_param,
3201da177e4SLinus Torvalds 			  u64 *out_param,
3211da177e4SLinus Torvalds 			  int out_is_imm,
3221da177e4SLinus Torvalds 			  u32 in_modifier,
3231da177e4SLinus Torvalds 			  u8 op_modifier,
3241da177e4SLinus Torvalds 			  u16 op,
3251da177e4SLinus Torvalds 			  unsigned long timeout,
3261da177e4SLinus Torvalds 			  u8 *status)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	int err = 0;
3291da177e4SLinus Torvalds 	struct mthca_cmd_context *context;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	if (down_interruptible(&dev->cmd.event_sem))
3321da177e4SLinus Torvalds 		return -EINTR;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	spin_lock(&dev->cmd.context_lock);
3351da177e4SLinus Torvalds 	BUG_ON(dev->cmd.free_head < 0);
3361da177e4SLinus Torvalds 	context = &dev->cmd.context[dev->cmd.free_head];
3371da177e4SLinus Torvalds 	dev->cmd.free_head = context->next;
3381da177e4SLinus Torvalds 	spin_unlock(&dev->cmd.context_lock);
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	init_completion(&context->done);
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	err = mthca_cmd_post(dev, in_param,
3431da177e4SLinus Torvalds 			     out_param ? *out_param : 0,
3441da177e4SLinus Torvalds 			     in_modifier, op_modifier,
3451da177e4SLinus Torvalds 			     op, context->token, 1);
3461da177e4SLinus Torvalds 	if (err)
3471da177e4SLinus Torvalds 		goto out;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	context->timer.expires  = jiffies + timeout;
3501da177e4SLinus Torvalds 	add_timer(&context->timer);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	wait_for_completion(&context->done);
3531da177e4SLinus Torvalds 	del_timer_sync(&context->timer);
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	err = context->result;
3561da177e4SLinus Torvalds 	if (err)
3571da177e4SLinus Torvalds 		goto out;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	*status = context->status;
3601da177e4SLinus Torvalds 	if (*status)
3611da177e4SLinus Torvalds 		mthca_dbg(dev, "Command %02x completed with status %02x\n",
3621da177e4SLinus Torvalds 			  op, *status);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	if (out_is_imm)
3651da177e4SLinus Torvalds 		*out_param = context->out_param;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds out:
3681da177e4SLinus Torvalds 	spin_lock(&dev->cmd.context_lock);
3691da177e4SLinus Torvalds 	context->next = dev->cmd.free_head;
3701da177e4SLinus Torvalds 	dev->cmd.free_head = context - dev->cmd.context;
3711da177e4SLinus Torvalds 	spin_unlock(&dev->cmd.context_lock);
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	up(&dev->cmd.event_sem);
3741da177e4SLinus Torvalds 	return err;
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds /* Invoke a command with an output mailbox */
3781da177e4SLinus Torvalds static int mthca_cmd_box(struct mthca_dev *dev,
3791da177e4SLinus Torvalds 			 u64 in_param,
3801da177e4SLinus Torvalds 			 u64 out_param,
3811da177e4SLinus Torvalds 			 u32 in_modifier,
3821da177e4SLinus Torvalds 			 u8 op_modifier,
3831da177e4SLinus Torvalds 			 u16 op,
3841da177e4SLinus Torvalds 			 unsigned long timeout,
3851da177e4SLinus Torvalds 			 u8 *status)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	if (dev->cmd.use_events)
3881da177e4SLinus Torvalds 		return mthca_cmd_wait(dev, in_param, &out_param, 0,
3891da177e4SLinus Torvalds 				      in_modifier, op_modifier, op,
3901da177e4SLinus Torvalds 				      timeout, status);
3911da177e4SLinus Torvalds 	else
3921da177e4SLinus Torvalds 		return mthca_cmd_poll(dev, in_param, &out_param, 0,
3931da177e4SLinus Torvalds 				      in_modifier, op_modifier, op,
3941da177e4SLinus Torvalds 				      timeout, status);
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds /* Invoke a command with no output parameter */
3981da177e4SLinus Torvalds static int mthca_cmd(struct mthca_dev *dev,
3991da177e4SLinus Torvalds 		     u64 in_param,
4001da177e4SLinus Torvalds 		     u32 in_modifier,
4011da177e4SLinus Torvalds 		     u8 op_modifier,
4021da177e4SLinus Torvalds 		     u16 op,
4031da177e4SLinus Torvalds 		     unsigned long timeout,
4041da177e4SLinus Torvalds 		     u8 *status)
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	return mthca_cmd_box(dev, in_param, 0, in_modifier,
4071da177e4SLinus Torvalds 			     op_modifier, op, timeout, status);
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /*
4111da177e4SLinus Torvalds  * Invoke a command with an immediate output parameter (and copy the
4121da177e4SLinus Torvalds  * output into the caller's out_param pointer after the command
4131da177e4SLinus Torvalds  * executes).
4141da177e4SLinus Torvalds  */
4151da177e4SLinus Torvalds static int mthca_cmd_imm(struct mthca_dev *dev,
4161da177e4SLinus Torvalds 			 u64 in_param,
4171da177e4SLinus Torvalds 			 u64 *out_param,
4181da177e4SLinus Torvalds 			 u32 in_modifier,
4191da177e4SLinus Torvalds 			 u8 op_modifier,
4201da177e4SLinus Torvalds 			 u16 op,
4211da177e4SLinus Torvalds 			 unsigned long timeout,
4221da177e4SLinus Torvalds 			 u8 *status)
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	if (dev->cmd.use_events)
4251da177e4SLinus Torvalds 		return mthca_cmd_wait(dev, in_param, out_param, 1,
4261da177e4SLinus Torvalds 				      in_modifier, op_modifier, op,
4271da177e4SLinus Torvalds 				      timeout, status);
4281da177e4SLinus Torvalds 	else
4291da177e4SLinus Torvalds 		return mthca_cmd_poll(dev, in_param, out_param, 1,
4301da177e4SLinus Torvalds 				      in_modifier, op_modifier, op,
4311da177e4SLinus Torvalds 				      timeout, status);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds /*
4351da177e4SLinus Torvalds  * Switch to using events to issue FW commands (should be called after
4361da177e4SLinus Torvalds  * event queue to command events has been initialized).
4371da177e4SLinus Torvalds  */
4381da177e4SLinus Torvalds int mthca_cmd_use_events(struct mthca_dev *dev)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	int i;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	dev->cmd.context = kmalloc(dev->cmd.max_cmds *
4431da177e4SLinus Torvalds 				   sizeof (struct mthca_cmd_context),
4441da177e4SLinus Torvalds 				   GFP_KERNEL);
4451da177e4SLinus Torvalds 	if (!dev->cmd.context)
4461da177e4SLinus Torvalds 		return -ENOMEM;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	for (i = 0; i < dev->cmd.max_cmds; ++i) {
4491da177e4SLinus Torvalds 		dev->cmd.context[i].token = i;
4501da177e4SLinus Torvalds 		dev->cmd.context[i].next = i + 1;
4511da177e4SLinus Torvalds 		init_timer(&dev->cmd.context[i].timer);
4521da177e4SLinus Torvalds 		dev->cmd.context[i].timer.data     =
4531da177e4SLinus Torvalds 			(unsigned long) &dev->cmd.context[i];
4541da177e4SLinus Torvalds 		dev->cmd.context[i].timer.function = event_timeout;
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	dev->cmd.context[dev->cmd.max_cmds - 1].next = -1;
4581da177e4SLinus Torvalds 	dev->cmd.free_head = 0;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	sema_init(&dev->cmd.event_sem, dev->cmd.max_cmds);
4611da177e4SLinus Torvalds 	spin_lock_init(&dev->cmd.context_lock);
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	for (dev->cmd.token_mask = 1;
4641da177e4SLinus Torvalds 	     dev->cmd.token_mask < dev->cmd.max_cmds;
4651da177e4SLinus Torvalds 	     dev->cmd.token_mask <<= 1)
4661da177e4SLinus Torvalds 		; /* nothing */
4671da177e4SLinus Torvalds 	--dev->cmd.token_mask;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	dev->cmd.use_events = 1;
4701da177e4SLinus Torvalds 	down(&dev->cmd.poll_sem);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	return 0;
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds /*
4761da177e4SLinus Torvalds  * Switch back to polling (used when shutting down the device)
4771da177e4SLinus Torvalds  */
4781da177e4SLinus Torvalds void mthca_cmd_use_polling(struct mthca_dev *dev)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	int i;
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	dev->cmd.use_events = 0;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	for (i = 0; i < dev->cmd.max_cmds; ++i)
4851da177e4SLinus Torvalds 		down(&dev->cmd.event_sem);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	kfree(dev->cmd.context);
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	up(&dev->cmd.poll_sem);
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds int mthca_SYS_EN(struct mthca_dev *dev, u8 *status)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	u64 out;
4951da177e4SLinus Torvalds 	int ret;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	ret = mthca_cmd_imm(dev, 0, &out, 0, 0, CMD_SYS_EN, HZ, status);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	if (*status == MTHCA_CMD_STAT_DDR_MEM_ERR)
5001da177e4SLinus Torvalds 		mthca_warn(dev, "SYS_EN DDR error: syn=%x, sock=%d, "
5011da177e4SLinus Torvalds 			   "sladdr=%d, SPD source=%s\n",
5021da177e4SLinus Torvalds 			   (int) (out >> 6) & 0xf, (int) (out >> 4) & 3,
5031da177e4SLinus Torvalds 			   (int) (out >> 1) & 7, (int) out & 1 ? "NVMEM" : "DIMM");
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	return ret;
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds int mthca_SYS_DIS(struct mthca_dev *dev, u8 *status)
5091da177e4SLinus Torvalds {
5101da177e4SLinus Torvalds 	return mthca_cmd(dev, 0, 0, 0, CMD_SYS_DIS, HZ, status);
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds static int mthca_map_cmd(struct mthca_dev *dev, u16 op, struct mthca_icm *icm,
5141da177e4SLinus Torvalds 			 u64 virt, u8 *status)
5151da177e4SLinus Torvalds {
5161da177e4SLinus Torvalds 	u32 *inbox;
5171da177e4SLinus Torvalds 	dma_addr_t indma;
5181da177e4SLinus Torvalds 	struct mthca_icm_iter iter;
5191da177e4SLinus Torvalds 	int lg;
5201da177e4SLinus Torvalds 	int nent = 0;
5211da177e4SLinus Torvalds 	int i;
5221da177e4SLinus Torvalds 	int err = 0;
5231da177e4SLinus Torvalds 	int ts = 0, tc = 0;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	inbox = pci_alloc_consistent(dev->pdev, PAGE_SIZE, &indma);
5261da177e4SLinus Torvalds 	if (!inbox)
5271da177e4SLinus Torvalds 		return -ENOMEM;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	memset(inbox, 0, PAGE_SIZE);
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	for (mthca_icm_first(icm, &iter);
5321da177e4SLinus Torvalds 	     !mthca_icm_last(&iter);
5331da177e4SLinus Torvalds 	     mthca_icm_next(&iter)) {
5341da177e4SLinus Torvalds 		/*
5351da177e4SLinus Torvalds 		 * We have to pass pages that are aligned to their
5361da177e4SLinus Torvalds 		 * size, so find the least significant 1 in the
5371da177e4SLinus Torvalds 		 * address or size and use that as our log2 size.
5381da177e4SLinus Torvalds 		 */
5391da177e4SLinus Torvalds 		lg = ffs(mthca_icm_addr(&iter) | mthca_icm_size(&iter)) - 1;
5401da177e4SLinus Torvalds 		if (lg < 12) {
5411da177e4SLinus Torvalds 			mthca_warn(dev, "Got FW area not aligned to 4K (%llx/%lx).\n",
5421da177e4SLinus Torvalds 				   (unsigned long long) mthca_icm_addr(&iter),
5431da177e4SLinus Torvalds 				   mthca_icm_size(&iter));
5441da177e4SLinus Torvalds 			err = -EINVAL;
5451da177e4SLinus Torvalds 			goto out;
5461da177e4SLinus Torvalds 		}
5471da177e4SLinus Torvalds 		for (i = 0; i < mthca_icm_size(&iter) / (1 << lg); ++i, ++nent) {
5481da177e4SLinus Torvalds 			if (virt != -1) {
5491da177e4SLinus Torvalds 				*((__be64 *) (inbox + nent * 4)) =
5501da177e4SLinus Torvalds 					cpu_to_be64(virt);
5511da177e4SLinus Torvalds 				virt += 1 << lg;
5521da177e4SLinus Torvalds 			}
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 			*((__be64 *) (inbox + nent * 4 + 2)) =
5551da177e4SLinus Torvalds 				cpu_to_be64((mthca_icm_addr(&iter) +
5561da177e4SLinus Torvalds 					     (i << lg)) | (lg - 12));
5571da177e4SLinus Torvalds 			ts += 1 << (lg - 10);
5581da177e4SLinus Torvalds 			++tc;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 			if (nent == PAGE_SIZE / 16) {
5611da177e4SLinus Torvalds 				err = mthca_cmd(dev, indma, nent, 0, op,
5621da177e4SLinus Torvalds 						CMD_TIME_CLASS_B, status);
5631da177e4SLinus Torvalds 				if (err || *status)
5641da177e4SLinus Torvalds 					goto out;
5651da177e4SLinus Torvalds 				nent = 0;
5661da177e4SLinus Torvalds 			}
5671da177e4SLinus Torvalds 		}
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	if (nent)
5711da177e4SLinus Torvalds 		err = mthca_cmd(dev, indma, nent, 0, op,
5721da177e4SLinus Torvalds 				CMD_TIME_CLASS_B, status);
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	switch (op) {
5751da177e4SLinus Torvalds 	case CMD_MAP_FA:
5761da177e4SLinus Torvalds 		mthca_dbg(dev, "Mapped %d chunks/%d KB for FW.\n", tc, ts);
5771da177e4SLinus Torvalds 		break;
5781da177e4SLinus Torvalds 	case CMD_MAP_ICM_AUX:
5791da177e4SLinus Torvalds 		mthca_dbg(dev, "Mapped %d chunks/%d KB for ICM aux.\n", tc, ts);
5801da177e4SLinus Torvalds 		break;
5811da177e4SLinus Torvalds 	case CMD_MAP_ICM:
5821da177e4SLinus Torvalds 		mthca_dbg(dev, "Mapped %d chunks/%d KB at %llx for ICM.\n",
5831da177e4SLinus Torvalds 			  tc, ts, (unsigned long long) virt - (ts << 10));
5841da177e4SLinus Torvalds 		break;
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds out:
5881da177e4SLinus Torvalds 	pci_free_consistent(dev->pdev, PAGE_SIZE, inbox, indma);
5891da177e4SLinus Torvalds 	return err;
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds int mthca_MAP_FA(struct mthca_dev *dev, struct mthca_icm *icm, u8 *status)
5931da177e4SLinus Torvalds {
5941da177e4SLinus Torvalds 	return mthca_map_cmd(dev, CMD_MAP_FA, icm, -1, status);
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds int mthca_UNMAP_FA(struct mthca_dev *dev, u8 *status)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	return mthca_cmd(dev, 0, 0, 0, CMD_UNMAP_FA, CMD_TIME_CLASS_B, status);
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds int mthca_RUN_FW(struct mthca_dev *dev, u8 *status)
6031da177e4SLinus Torvalds {
6041da177e4SLinus Torvalds 	return mthca_cmd(dev, 0, 0, 0, CMD_RUN_FW, CMD_TIME_CLASS_A, status);
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds int mthca_QUERY_FW(struct mthca_dev *dev, u8 *status)
6081da177e4SLinus Torvalds {
6091da177e4SLinus Torvalds 	u32 *outbox;
6101da177e4SLinus Torvalds 	dma_addr_t outdma;
6111da177e4SLinus Torvalds 	int err = 0;
6121da177e4SLinus Torvalds 	u8 lg;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds #define QUERY_FW_OUT_SIZE             0x100
6151da177e4SLinus Torvalds #define QUERY_FW_VER_OFFSET            0x00
6161da177e4SLinus Torvalds #define QUERY_FW_MAX_CMD_OFFSET        0x0f
6171da177e4SLinus Torvalds #define QUERY_FW_ERR_START_OFFSET      0x30
6181da177e4SLinus Torvalds #define QUERY_FW_ERR_SIZE_OFFSET       0x38
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds #define QUERY_FW_START_OFFSET          0x20
6211da177e4SLinus Torvalds #define QUERY_FW_END_OFFSET            0x28
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds #define QUERY_FW_SIZE_OFFSET           0x00
6241da177e4SLinus Torvalds #define QUERY_FW_CLR_INT_BASE_OFFSET   0x20
6251da177e4SLinus Torvalds #define QUERY_FW_EQ_ARM_BASE_OFFSET    0x40
6261da177e4SLinus Torvalds #define QUERY_FW_EQ_SET_CI_BASE_OFFSET 0x48
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	outbox = pci_alloc_consistent(dev->pdev, QUERY_FW_OUT_SIZE, &outdma);
6291da177e4SLinus Torvalds 	if (!outbox) {
6301da177e4SLinus Torvalds 		return -ENOMEM;
6311da177e4SLinus Torvalds 	}
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	err = mthca_cmd_box(dev, 0, outdma, 0, 0, CMD_QUERY_FW,
6341da177e4SLinus Torvalds 			    CMD_TIME_CLASS_A, status);
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	if (err)
6371da177e4SLinus Torvalds 		goto out;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	MTHCA_GET(dev->fw_ver,   outbox, QUERY_FW_VER_OFFSET);
6401da177e4SLinus Torvalds 	/*
6411da177e4SLinus Torvalds 	 * FW subminor version is at more signifant bits than minor
6421da177e4SLinus Torvalds 	 * version, so swap here.
6431da177e4SLinus Torvalds 	 */
6441da177e4SLinus Torvalds 	dev->fw_ver = (dev->fw_ver & 0xffff00000000ull) |
6451da177e4SLinus Torvalds 		((dev->fw_ver & 0xffff0000ull) >> 16) |
6461da177e4SLinus Torvalds 		((dev->fw_ver & 0x0000ffffull) << 16);
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	MTHCA_GET(lg, outbox, QUERY_FW_MAX_CMD_OFFSET);
6491da177e4SLinus Torvalds 	dev->cmd.max_cmds = 1 << lg;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	mthca_dbg(dev, "FW version %012llx, max commands %d\n",
6521da177e4SLinus Torvalds 		  (unsigned long long) dev->fw_ver, dev->cmd.max_cmds);
6531da177e4SLinus Torvalds 
654*d10ddbf6SRoland Dreier 	if (mthca_is_memfree(dev)) {
6551da177e4SLinus Torvalds 		MTHCA_GET(dev->fw.arbel.fw_pages,       outbox, QUERY_FW_SIZE_OFFSET);
6561da177e4SLinus Torvalds 		MTHCA_GET(dev->fw.arbel.clr_int_base,   outbox, QUERY_FW_CLR_INT_BASE_OFFSET);
6571da177e4SLinus Torvalds 		MTHCA_GET(dev->fw.arbel.eq_arm_base,    outbox, QUERY_FW_EQ_ARM_BASE_OFFSET);
6581da177e4SLinus Torvalds 		MTHCA_GET(dev->fw.arbel.eq_set_ci_base, outbox, QUERY_FW_EQ_SET_CI_BASE_OFFSET);
6591da177e4SLinus Torvalds 		mthca_dbg(dev, "FW size %d KB\n", dev->fw.arbel.fw_pages << 2);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 		/*
6621da177e4SLinus Torvalds 		 * Arbel page size is always 4 KB; round up number of
6631da177e4SLinus Torvalds 		 * system pages needed.
6641da177e4SLinus Torvalds 		 */
6651da177e4SLinus Torvalds 		dev->fw.arbel.fw_pages =
6661da177e4SLinus Torvalds 			(dev->fw.arbel.fw_pages + (1 << (PAGE_SHIFT - 12)) - 1) >>
6671da177e4SLinus Torvalds 			(PAGE_SHIFT - 12);
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 		mthca_dbg(dev, "Clear int @ %llx, EQ arm @ %llx, EQ set CI @ %llx\n",
6701da177e4SLinus Torvalds 			  (unsigned long long) dev->fw.arbel.clr_int_base,
6711da177e4SLinus Torvalds 			  (unsigned long long) dev->fw.arbel.eq_arm_base,
6721da177e4SLinus Torvalds 			  (unsigned long long) dev->fw.arbel.eq_set_ci_base);
6731da177e4SLinus Torvalds 	} else {
6741da177e4SLinus Torvalds 		MTHCA_GET(dev->fw.tavor.fw_start, outbox, QUERY_FW_START_OFFSET);
6751da177e4SLinus Torvalds 		MTHCA_GET(dev->fw.tavor.fw_end,   outbox, QUERY_FW_END_OFFSET);
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 		mthca_dbg(dev, "FW size %d KB (start %llx, end %llx)\n",
6781da177e4SLinus Torvalds 			  (int) ((dev->fw.tavor.fw_end - dev->fw.tavor.fw_start) >> 10),
6791da177e4SLinus Torvalds 			  (unsigned long long) dev->fw.tavor.fw_start,
6801da177e4SLinus Torvalds 			  (unsigned long long) dev->fw.tavor.fw_end);
6811da177e4SLinus Torvalds 	}
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds out:
6841da177e4SLinus Torvalds 	pci_free_consistent(dev->pdev, QUERY_FW_OUT_SIZE, outbox, outdma);
6851da177e4SLinus Torvalds 	return err;
6861da177e4SLinus Torvalds }
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds int mthca_ENABLE_LAM(struct mthca_dev *dev, u8 *status)
6891da177e4SLinus Torvalds {
6901da177e4SLinus Torvalds 	u8 info;
6911da177e4SLinus Torvalds 	u32 *outbox;
6921da177e4SLinus Torvalds 	dma_addr_t outdma;
6931da177e4SLinus Torvalds 	int err = 0;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds #define ENABLE_LAM_OUT_SIZE         0x100
6961da177e4SLinus Torvalds #define ENABLE_LAM_START_OFFSET     0x00
6971da177e4SLinus Torvalds #define ENABLE_LAM_END_OFFSET       0x08
6981da177e4SLinus Torvalds #define ENABLE_LAM_INFO_OFFSET      0x13
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds #define ENABLE_LAM_INFO_HIDDEN_FLAG (1 << 4)
7011da177e4SLinus Torvalds #define ENABLE_LAM_INFO_ECC_MASK    0x3
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	outbox = pci_alloc_consistent(dev->pdev, ENABLE_LAM_OUT_SIZE, &outdma);
7041da177e4SLinus Torvalds 	if (!outbox)
7051da177e4SLinus Torvalds 		return -ENOMEM;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	err = mthca_cmd_box(dev, 0, outdma, 0, 0, CMD_ENABLE_LAM,
7081da177e4SLinus Torvalds 			    CMD_TIME_CLASS_C, status);
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	if (err)
7111da177e4SLinus Torvalds 		goto out;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	if (*status == MTHCA_CMD_STAT_LAM_NOT_PRE)
7141da177e4SLinus Torvalds 		goto out;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	MTHCA_GET(dev->ddr_start, outbox, ENABLE_LAM_START_OFFSET);
7171da177e4SLinus Torvalds 	MTHCA_GET(dev->ddr_end,   outbox, ENABLE_LAM_END_OFFSET);
7181da177e4SLinus Torvalds 	MTHCA_GET(info,           outbox, ENABLE_LAM_INFO_OFFSET);
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	if (!!(info & ENABLE_LAM_INFO_HIDDEN_FLAG) !=
7211da177e4SLinus Torvalds 	    !!(dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) {
7221da177e4SLinus Torvalds 		mthca_info(dev, "FW reports that HCA-attached memory "
7231da177e4SLinus Torvalds 			   "is %s hidden; does not match PCI config\n",
7241da177e4SLinus Torvalds 			   (info & ENABLE_LAM_INFO_HIDDEN_FLAG) ?
7251da177e4SLinus Torvalds 			   "" : "not");
7261da177e4SLinus Torvalds 	}
7271da177e4SLinus Torvalds 	if (info & ENABLE_LAM_INFO_HIDDEN_FLAG)
7281da177e4SLinus Torvalds 		mthca_dbg(dev, "HCA-attached memory is hidden.\n");
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	mthca_dbg(dev, "HCA memory size %d KB (start %llx, end %llx)\n",
7311da177e4SLinus Torvalds 		  (int) ((dev->ddr_end - dev->ddr_start) >> 10),
7321da177e4SLinus Torvalds 		  (unsigned long long) dev->ddr_start,
7331da177e4SLinus Torvalds 		  (unsigned long long) dev->ddr_end);
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds out:
7361da177e4SLinus Torvalds 	pci_free_consistent(dev->pdev, ENABLE_LAM_OUT_SIZE, outbox, outdma);
7371da177e4SLinus Torvalds 	return err;
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds int mthca_DISABLE_LAM(struct mthca_dev *dev, u8 *status)
7411da177e4SLinus Torvalds {
7421da177e4SLinus Torvalds 	return mthca_cmd(dev, 0, 0, 0, CMD_SYS_DIS, CMD_TIME_CLASS_C, status);
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds int mthca_QUERY_DDR(struct mthca_dev *dev, u8 *status)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	u8 info;
7481da177e4SLinus Torvalds 	u32 *outbox;
7491da177e4SLinus Torvalds 	dma_addr_t outdma;
7501da177e4SLinus Torvalds 	int err = 0;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds #define QUERY_DDR_OUT_SIZE         0x100
7531da177e4SLinus Torvalds #define QUERY_DDR_START_OFFSET     0x00
7541da177e4SLinus Torvalds #define QUERY_DDR_END_OFFSET       0x08
7551da177e4SLinus Torvalds #define QUERY_DDR_INFO_OFFSET      0x13
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds #define QUERY_DDR_INFO_HIDDEN_FLAG (1 << 4)
7581da177e4SLinus Torvalds #define QUERY_DDR_INFO_ECC_MASK    0x3
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	outbox = pci_alloc_consistent(dev->pdev, QUERY_DDR_OUT_SIZE, &outdma);
7611da177e4SLinus Torvalds 	if (!outbox)
7621da177e4SLinus Torvalds 		return -ENOMEM;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	err = mthca_cmd_box(dev, 0, outdma, 0, 0, CMD_QUERY_DDR,
7651da177e4SLinus Torvalds 			    CMD_TIME_CLASS_A, status);
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	if (err)
7681da177e4SLinus Torvalds 		goto out;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	MTHCA_GET(dev->ddr_start, outbox, QUERY_DDR_START_OFFSET);
7711da177e4SLinus Torvalds 	MTHCA_GET(dev->ddr_end,   outbox, QUERY_DDR_END_OFFSET);
7721da177e4SLinus Torvalds 	MTHCA_GET(info,           outbox, QUERY_DDR_INFO_OFFSET);
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	if (!!(info & QUERY_DDR_INFO_HIDDEN_FLAG) !=
7751da177e4SLinus Torvalds 	    !!(dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) {
7761da177e4SLinus Torvalds 		mthca_info(dev, "FW reports that HCA-attached memory "
7771da177e4SLinus Torvalds 			   "is %s hidden; does not match PCI config\n",
7781da177e4SLinus Torvalds 			   (info & QUERY_DDR_INFO_HIDDEN_FLAG) ?
7791da177e4SLinus Torvalds 			   "" : "not");
7801da177e4SLinus Torvalds 	}
7811da177e4SLinus Torvalds 	if (info & QUERY_DDR_INFO_HIDDEN_FLAG)
7821da177e4SLinus Torvalds 		mthca_dbg(dev, "HCA-attached memory is hidden.\n");
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	mthca_dbg(dev, "HCA memory size %d KB (start %llx, end %llx)\n",
7851da177e4SLinus Torvalds 		  (int) ((dev->ddr_end - dev->ddr_start) >> 10),
7861da177e4SLinus Torvalds 		  (unsigned long long) dev->ddr_start,
7871da177e4SLinus Torvalds 		  (unsigned long long) dev->ddr_end);
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds out:
7901da177e4SLinus Torvalds 	pci_free_consistent(dev->pdev, QUERY_DDR_OUT_SIZE, outbox, outdma);
7911da177e4SLinus Torvalds 	return err;
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds int mthca_QUERY_DEV_LIM(struct mthca_dev *dev,
7951da177e4SLinus Torvalds 			struct mthca_dev_lim *dev_lim, u8 *status)
7961da177e4SLinus Torvalds {
7971da177e4SLinus Torvalds 	u32 *outbox;
7981da177e4SLinus Torvalds 	dma_addr_t outdma;
7991da177e4SLinus Torvalds 	u8 field;
8001da177e4SLinus Torvalds 	u16 size;
8011da177e4SLinus Torvalds 	int err;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds #define QUERY_DEV_LIM_OUT_SIZE             0x100
8041da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_SRQ_SZ_OFFSET     0x10
8051da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_QP_SZ_OFFSET      0x11
8061da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_QP_OFFSET        0x12
8071da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_QP_OFFSET         0x13
8081da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_SRQ_OFFSET       0x14
8091da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_SRQ_OFFSET        0x15
8101da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_EEC_OFFSET       0x16
8111da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_EEC_OFFSET        0x17
8121da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_CQ_SZ_OFFSET      0x19
8131da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_CQ_OFFSET        0x1a
8141da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_CQ_OFFSET         0x1b
8151da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_MPT_OFFSET        0x1d
8161da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_EQ_OFFSET        0x1e
8171da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_EQ_OFFSET         0x1f
8181da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_MTT_OFFSET       0x20
8191da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_MRW_SZ_OFFSET     0x21
8201da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_MRW_OFFSET       0x22
8211da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_MTT_SEG_OFFSET    0x23
8221da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_AV_OFFSET         0x27
8231da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_REQ_QP_OFFSET     0x29
8241da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_RES_QP_OFFSET     0x2b
8251da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_RDMA_OFFSET       0x2f
8261da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSZ_SRQ_OFFSET        0x33
8271da177e4SLinus Torvalds #define QUERY_DEV_LIM_ACK_DELAY_OFFSET      0x35
8281da177e4SLinus Torvalds #define QUERY_DEV_LIM_MTU_WIDTH_OFFSET      0x36
8291da177e4SLinus Torvalds #define QUERY_DEV_LIM_VL_PORT_OFFSET        0x37
8301da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_GID_OFFSET        0x3b
8311da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_PKEY_OFFSET       0x3f
8321da177e4SLinus Torvalds #define QUERY_DEV_LIM_FLAGS_OFFSET          0x44
8331da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_UAR_OFFSET       0x48
8341da177e4SLinus Torvalds #define QUERY_DEV_LIM_UAR_SZ_OFFSET         0x49
8351da177e4SLinus Torvalds #define QUERY_DEV_LIM_PAGE_SZ_OFFSET        0x4b
8361da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_SG_OFFSET         0x51
8371da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_DESC_SZ_OFFSET    0x52
8381da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_SG_RQ_OFFSET      0x55
8391da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_DESC_SZ_RQ_OFFSET 0x56
8401da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_QP_MCG_OFFSET     0x61
8411da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_MCG_OFFSET       0x62
8421da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_MCG_OFFSET        0x63
8431da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_PD_OFFSET        0x64
8441da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_PD_OFFSET         0x65
8451da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_RDD_OFFSET       0x66
8461da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_RDD_OFFSET        0x67
8471da177e4SLinus Torvalds #define QUERY_DEV_LIM_EEC_ENTRY_SZ_OFFSET   0x80
8481da177e4SLinus Torvalds #define QUERY_DEV_LIM_QPC_ENTRY_SZ_OFFSET   0x82
8491da177e4SLinus Torvalds #define QUERY_DEV_LIM_EEEC_ENTRY_SZ_OFFSET  0x84
8501da177e4SLinus Torvalds #define QUERY_DEV_LIM_EQPC_ENTRY_SZ_OFFSET  0x86
8511da177e4SLinus Torvalds #define QUERY_DEV_LIM_EQC_ENTRY_SZ_OFFSET   0x88
8521da177e4SLinus Torvalds #define QUERY_DEV_LIM_CQC_ENTRY_SZ_OFFSET   0x8a
8531da177e4SLinus Torvalds #define QUERY_DEV_LIM_SRQ_ENTRY_SZ_OFFSET   0x8c
8541da177e4SLinus Torvalds #define QUERY_DEV_LIM_UAR_ENTRY_SZ_OFFSET   0x8e
8551da177e4SLinus Torvalds #define QUERY_DEV_LIM_MTT_ENTRY_SZ_OFFSET   0x90
8561da177e4SLinus Torvalds #define QUERY_DEV_LIM_MPT_ENTRY_SZ_OFFSET   0x92
8571da177e4SLinus Torvalds #define QUERY_DEV_LIM_PBL_SZ_OFFSET         0x96
8581da177e4SLinus Torvalds #define QUERY_DEV_LIM_BMME_FLAGS_OFFSET     0x97
8591da177e4SLinus Torvalds #define QUERY_DEV_LIM_RSVD_LKEY_OFFSET      0x98
8601da177e4SLinus Torvalds #define QUERY_DEV_LIM_LAMR_OFFSET           0x9f
8611da177e4SLinus Torvalds #define QUERY_DEV_LIM_MAX_ICM_SZ_OFFSET     0xa0
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	outbox = pci_alloc_consistent(dev->pdev, QUERY_DEV_LIM_OUT_SIZE, &outdma);
8641da177e4SLinus Torvalds 	if (!outbox)
8651da177e4SLinus Torvalds 		return -ENOMEM;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	err = mthca_cmd_box(dev, 0, outdma, 0, 0, CMD_QUERY_DEV_LIM,
8681da177e4SLinus Torvalds 			    CMD_TIME_CLASS_A, status);
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	if (err)
8711da177e4SLinus Torvalds 		goto out;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SRQ_SZ_OFFSET);
8741da177e4SLinus Torvalds 	dev_lim->max_srq_sz = 1 << field;
8751da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_SZ_OFFSET);
8761da177e4SLinus Torvalds 	dev_lim->max_qp_sz = 1 << field;
8771da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_QP_OFFSET);
8781da177e4SLinus Torvalds 	dev_lim->reserved_qps = 1 << (field & 0xf);
8791da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_OFFSET);
8801da177e4SLinus Torvalds 	dev_lim->max_qps = 1 << (field & 0x1f);
8811da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_SRQ_OFFSET);
8821da177e4SLinus Torvalds 	dev_lim->reserved_srqs = 1 << (field >> 4);
8831da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SRQ_OFFSET);
8841da177e4SLinus Torvalds 	dev_lim->max_srqs = 1 << (field & 0x1f);
8851da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_EEC_OFFSET);
8861da177e4SLinus Torvalds 	dev_lim->reserved_eecs = 1 << (field & 0xf);
8871da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_EEC_OFFSET);
8881da177e4SLinus Torvalds 	dev_lim->max_eecs = 1 << (field & 0x1f);
8891da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_CQ_SZ_OFFSET);
8901da177e4SLinus Torvalds 	dev_lim->max_cq_sz = 1 << field;
8911da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_CQ_OFFSET);
8921da177e4SLinus Torvalds 	dev_lim->reserved_cqs = 1 << (field & 0xf);
8931da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_CQ_OFFSET);
8941da177e4SLinus Torvalds 	dev_lim->max_cqs = 1 << (field & 0x1f);
8951da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MPT_OFFSET);
8961da177e4SLinus Torvalds 	dev_lim->max_mpts = 1 << (field & 0x3f);
8971da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_EQ_OFFSET);
8981da177e4SLinus Torvalds 	dev_lim->reserved_eqs = 1 << (field & 0xf);
8991da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_EQ_OFFSET);
9001da177e4SLinus Torvalds 	dev_lim->max_eqs = 1 << (field & 0x7);
9011da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_MTT_OFFSET);
9021da177e4SLinus Torvalds 	dev_lim->reserved_mtts = 1 << (field >> 4);
9031da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MRW_SZ_OFFSET);
9041da177e4SLinus Torvalds 	dev_lim->max_mrw_sz = 1 << field;
9051da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_MRW_OFFSET);
9061da177e4SLinus Torvalds 	dev_lim->reserved_mrws = 1 << (field & 0xf);
9071da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MTT_SEG_OFFSET);
9081da177e4SLinus Torvalds 	dev_lim->max_mtt_seg = 1 << (field & 0x3f);
9091da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_REQ_QP_OFFSET);
9101da177e4SLinus Torvalds 	dev_lim->max_requester_per_qp = 1 << (field & 0x3f);
9111da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_RES_QP_OFFSET);
9121da177e4SLinus Torvalds 	dev_lim->max_responder_per_qp = 1 << (field & 0x3f);
9131da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_RDMA_OFFSET);
9141da177e4SLinus Torvalds 	dev_lim->max_rdma_global = 1 << (field & 0x3f);
9151da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_ACK_DELAY_OFFSET);
9161da177e4SLinus Torvalds 	dev_lim->local_ca_ack_delay = field & 0x1f;
9171da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MTU_WIDTH_OFFSET);
9181da177e4SLinus Torvalds 	dev_lim->max_mtu        = field >> 4;
9191da177e4SLinus Torvalds 	dev_lim->max_port_width = field & 0xf;
9201da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_VL_PORT_OFFSET);
9211da177e4SLinus Torvalds 	dev_lim->max_vl    = field >> 4;
9221da177e4SLinus Torvalds 	dev_lim->num_ports = field & 0xf;
9231da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_GID_OFFSET);
9241da177e4SLinus Torvalds 	dev_lim->max_gids = 1 << (field & 0xf);
9251da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_PKEY_OFFSET);
9261da177e4SLinus Torvalds 	dev_lim->max_pkeys = 1 << (field & 0xf);
9271da177e4SLinus Torvalds 	MTHCA_GET(dev_lim->flags, outbox, QUERY_DEV_LIM_FLAGS_OFFSET);
9281da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_UAR_OFFSET);
9291da177e4SLinus Torvalds 	dev_lim->reserved_uars = field >> 4;
9301da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_UAR_SZ_OFFSET);
9311da177e4SLinus Torvalds 	dev_lim->uar_size = 1 << ((field & 0x3f) + 20);
9321da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_PAGE_SZ_OFFSET);
9331da177e4SLinus Torvalds 	dev_lim->min_page_sz = 1 << field;
9341da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SG_OFFSET);
9351da177e4SLinus Torvalds 	dev_lim->max_sg = field;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_MAX_DESC_SZ_OFFSET);
9381da177e4SLinus Torvalds 	dev_lim->max_desc_sz = size;
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_MCG_OFFSET);
9411da177e4SLinus Torvalds 	dev_lim->max_qp_per_mcg = 1 << field;
9421da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_MCG_OFFSET);
9431da177e4SLinus Torvalds 	dev_lim->reserved_mgms = field & 0xf;
9441da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MCG_OFFSET);
9451da177e4SLinus Torvalds 	dev_lim->max_mcgs = 1 << field;
9461da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_PD_OFFSET);
9471da177e4SLinus Torvalds 	dev_lim->reserved_pds = field >> 4;
9481da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_PD_OFFSET);
9491da177e4SLinus Torvalds 	dev_lim->max_pds = 1 << (field & 0x3f);
9501da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_RDD_OFFSET);
9511da177e4SLinus Torvalds 	dev_lim->reserved_rdds = field >> 4;
9521da177e4SLinus Torvalds 	MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_RDD_OFFSET);
9531da177e4SLinus Torvalds 	dev_lim->max_rdds = 1 << (field & 0x3f);
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_EEC_ENTRY_SZ_OFFSET);
9561da177e4SLinus Torvalds 	dev_lim->eec_entry_sz = size;
9571da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_QPC_ENTRY_SZ_OFFSET);
9581da177e4SLinus Torvalds 	dev_lim->qpc_entry_sz = size;
9591da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_EEEC_ENTRY_SZ_OFFSET);
9601da177e4SLinus Torvalds 	dev_lim->eeec_entry_sz = size;
9611da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_EQPC_ENTRY_SZ_OFFSET);
9621da177e4SLinus Torvalds 	dev_lim->eqpc_entry_sz = size;
9631da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_EQC_ENTRY_SZ_OFFSET);
9641da177e4SLinus Torvalds 	dev_lim->eqc_entry_sz = size;
9651da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_CQC_ENTRY_SZ_OFFSET);
9661da177e4SLinus Torvalds 	dev_lim->cqc_entry_sz = size;
9671da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_SRQ_ENTRY_SZ_OFFSET);
9681da177e4SLinus Torvalds 	dev_lim->srq_entry_sz = size;
9691da177e4SLinus Torvalds 	MTHCA_GET(size, outbox, QUERY_DEV_LIM_UAR_ENTRY_SZ_OFFSET);
9701da177e4SLinus Torvalds 	dev_lim->uar_scratch_entry_sz = size;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	mthca_dbg(dev, "Max QPs: %d, reserved QPs: %d, entry size: %d\n",
9731da177e4SLinus Torvalds 		  dev_lim->max_qps, dev_lim->reserved_qps, dev_lim->qpc_entry_sz);
9741da177e4SLinus Torvalds 	mthca_dbg(dev, "Max CQs: %d, reserved CQs: %d, entry size: %d\n",
9751da177e4SLinus Torvalds 		  dev_lim->max_cqs, dev_lim->reserved_cqs, dev_lim->cqc_entry_sz);
9761da177e4SLinus Torvalds 	mthca_dbg(dev, "Max EQs: %d, reserved EQs: %d, entry size: %d\n",
9771da177e4SLinus Torvalds 		  dev_lim->max_eqs, dev_lim->reserved_eqs, dev_lim->eqc_entry_sz);
9781da177e4SLinus Torvalds 	mthca_dbg(dev, "reserved MPTs: %d, reserved MTTs: %d\n",
9791da177e4SLinus Torvalds 		  dev_lim->reserved_mrws, dev_lim->reserved_mtts);
9801da177e4SLinus Torvalds 	mthca_dbg(dev, "Max PDs: %d, reserved PDs: %d, reserved UARs: %d\n",
9811da177e4SLinus Torvalds 		  dev_lim->max_pds, dev_lim->reserved_pds, dev_lim->reserved_uars);
9821da177e4SLinus Torvalds 	mthca_dbg(dev, "Max QP/MCG: %d, reserved MGMs: %d\n",
9831da177e4SLinus Torvalds 		  dev_lim->max_pds, dev_lim->reserved_mgms);
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	mthca_dbg(dev, "Flags: %08x\n", dev_lim->flags);
9861da177e4SLinus Torvalds 
987*d10ddbf6SRoland Dreier 	if (mthca_is_memfree(dev)) {
9881da177e4SLinus Torvalds 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSZ_SRQ_OFFSET);
9891da177e4SLinus Torvalds 		dev_lim->hca.arbel.resize_srq = field & 1;
9908cf2daf3SRoland Dreier 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SG_RQ_OFFSET);
9918cf2daf3SRoland Dreier 		dev_lim->max_sg = min_t(int, field, dev_lim->max_sg);
9921da177e4SLinus Torvalds 		MTHCA_GET(size, outbox, QUERY_DEV_LIM_MPT_ENTRY_SZ_OFFSET);
9931da177e4SLinus Torvalds 		dev_lim->mpt_entry_sz = size;
9941da177e4SLinus Torvalds 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_PBL_SZ_OFFSET);
9951da177e4SLinus Torvalds 		dev_lim->hca.arbel.max_pbl_sz = 1 << (field & 0x3f);
9961da177e4SLinus Torvalds 		MTHCA_GET(dev_lim->hca.arbel.bmme_flags, outbox,
9971da177e4SLinus Torvalds 			  QUERY_DEV_LIM_BMME_FLAGS_OFFSET);
9981da177e4SLinus Torvalds 		MTHCA_GET(dev_lim->hca.arbel.reserved_lkey, outbox,
9991da177e4SLinus Torvalds 			  QUERY_DEV_LIM_RSVD_LKEY_OFFSET);
10001da177e4SLinus Torvalds 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_LAMR_OFFSET);
10011da177e4SLinus Torvalds 		dev_lim->hca.arbel.lam_required = field & 1;
10021da177e4SLinus Torvalds 		MTHCA_GET(dev_lim->hca.arbel.max_icm_sz, outbox,
10031da177e4SLinus Torvalds 			  QUERY_DEV_LIM_MAX_ICM_SZ_OFFSET);
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 		if (dev_lim->hca.arbel.bmme_flags & 1)
10061da177e4SLinus Torvalds 			mthca_dbg(dev, "Base MM extensions: yes "
10071da177e4SLinus Torvalds 				  "(flags %d, max PBL %d, rsvd L_Key %08x)\n",
10081da177e4SLinus Torvalds 				  dev_lim->hca.arbel.bmme_flags,
10091da177e4SLinus Torvalds 				  dev_lim->hca.arbel.max_pbl_sz,
10101da177e4SLinus Torvalds 				  dev_lim->hca.arbel.reserved_lkey);
10111da177e4SLinus Torvalds 		else
10121da177e4SLinus Torvalds 			mthca_dbg(dev, "Base MM extensions: no\n");
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 		mthca_dbg(dev, "Max ICM size %lld MB\n",
10151da177e4SLinus Torvalds 			  (unsigned long long) dev_lim->hca.arbel.max_icm_sz >> 20);
10161da177e4SLinus Torvalds 	} else {
10171da177e4SLinus Torvalds 		MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_AV_OFFSET);
10181da177e4SLinus Torvalds 		dev_lim->hca.tavor.max_avs = 1 << (field & 0x3f);
10191da177e4SLinus Torvalds 		dev_lim->mpt_entry_sz = MTHCA_MPT_ENTRY_SIZE;
10201da177e4SLinus Torvalds 	}
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds out:
10231da177e4SLinus Torvalds 	pci_free_consistent(dev->pdev, QUERY_DEV_LIM_OUT_SIZE, outbox, outdma);
10241da177e4SLinus Torvalds 	return err;
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds int mthca_QUERY_ADAPTER(struct mthca_dev *dev,
10281da177e4SLinus Torvalds 			struct mthca_adapter *adapter, u8 *status)
10291da177e4SLinus Torvalds {
10301da177e4SLinus Torvalds 	u32 *outbox;
10311da177e4SLinus Torvalds 	dma_addr_t outdma;
10321da177e4SLinus Torvalds 	int err;
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds #define QUERY_ADAPTER_OUT_SIZE             0x100
10351da177e4SLinus Torvalds #define QUERY_ADAPTER_VENDOR_ID_OFFSET     0x00
10361da177e4SLinus Torvalds #define QUERY_ADAPTER_DEVICE_ID_OFFSET     0x04
10371da177e4SLinus Torvalds #define QUERY_ADAPTER_REVISION_ID_OFFSET   0x08
10381da177e4SLinus Torvalds #define QUERY_ADAPTER_INTA_PIN_OFFSET      0x10
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	outbox = pci_alloc_consistent(dev->pdev, QUERY_ADAPTER_OUT_SIZE, &outdma);
10411da177e4SLinus Torvalds 	if (!outbox)
10421da177e4SLinus Torvalds 		return -ENOMEM;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	err = mthca_cmd_box(dev, 0, outdma, 0, 0, CMD_QUERY_ADAPTER,
10451da177e4SLinus Torvalds 			    CMD_TIME_CLASS_A, status);
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	if (err)
10481da177e4SLinus Torvalds 		goto out;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	MTHCA_GET(adapter->vendor_id, outbox, QUERY_ADAPTER_VENDOR_ID_OFFSET);
10511da177e4SLinus Torvalds 	MTHCA_GET(adapter->device_id, outbox, QUERY_ADAPTER_DEVICE_ID_OFFSET);
10521da177e4SLinus Torvalds 	MTHCA_GET(adapter->revision_id, outbox, QUERY_ADAPTER_REVISION_ID_OFFSET);
10531da177e4SLinus Torvalds 	MTHCA_GET(adapter->inta_pin, outbox, QUERY_ADAPTER_INTA_PIN_OFFSET);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds out:
10561da177e4SLinus Torvalds 	pci_free_consistent(dev->pdev, QUERY_DEV_LIM_OUT_SIZE, outbox, outdma);
10571da177e4SLinus Torvalds 	return err;
10581da177e4SLinus Torvalds }
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds int mthca_INIT_HCA(struct mthca_dev *dev,
10611da177e4SLinus Torvalds 		   struct mthca_init_hca_param *param,
10621da177e4SLinus Torvalds 		   u8 *status)
10631da177e4SLinus Torvalds {
10641da177e4SLinus Torvalds 	u32 *inbox;
10651da177e4SLinus Torvalds 	dma_addr_t indma;
10661da177e4SLinus Torvalds 	int err;
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds #define INIT_HCA_IN_SIZE             	 0x200
10691da177e4SLinus Torvalds #define INIT_HCA_FLAGS_OFFSET        	 0x014
10701da177e4SLinus Torvalds #define INIT_HCA_QPC_OFFSET          	 0x020
10711da177e4SLinus Torvalds #define  INIT_HCA_QPC_BASE_OFFSET    	 (INIT_HCA_QPC_OFFSET + 0x10)
10721da177e4SLinus Torvalds #define  INIT_HCA_LOG_QP_OFFSET      	 (INIT_HCA_QPC_OFFSET + 0x17)
10731da177e4SLinus Torvalds #define  INIT_HCA_EEC_BASE_OFFSET    	 (INIT_HCA_QPC_OFFSET + 0x20)
10741da177e4SLinus Torvalds #define  INIT_HCA_LOG_EEC_OFFSET     	 (INIT_HCA_QPC_OFFSET + 0x27)
10751da177e4SLinus Torvalds #define  INIT_HCA_SRQC_BASE_OFFSET   	 (INIT_HCA_QPC_OFFSET + 0x28)
10761da177e4SLinus Torvalds #define  INIT_HCA_LOG_SRQ_OFFSET     	 (INIT_HCA_QPC_OFFSET + 0x2f)
10771da177e4SLinus Torvalds #define  INIT_HCA_CQC_BASE_OFFSET    	 (INIT_HCA_QPC_OFFSET + 0x30)
10781da177e4SLinus Torvalds #define  INIT_HCA_LOG_CQ_OFFSET      	 (INIT_HCA_QPC_OFFSET + 0x37)
10791da177e4SLinus Torvalds #define  INIT_HCA_EQPC_BASE_OFFSET   	 (INIT_HCA_QPC_OFFSET + 0x40)
10801da177e4SLinus Torvalds #define  INIT_HCA_EEEC_BASE_OFFSET   	 (INIT_HCA_QPC_OFFSET + 0x50)
10811da177e4SLinus Torvalds #define  INIT_HCA_EQC_BASE_OFFSET    	 (INIT_HCA_QPC_OFFSET + 0x60)
10821da177e4SLinus Torvalds #define  INIT_HCA_LOG_EQ_OFFSET      	 (INIT_HCA_QPC_OFFSET + 0x67)
10831da177e4SLinus Torvalds #define  INIT_HCA_RDB_BASE_OFFSET    	 (INIT_HCA_QPC_OFFSET + 0x70)
10841da177e4SLinus Torvalds #define INIT_HCA_UDAV_OFFSET         	 0x0b0
10851da177e4SLinus Torvalds #define  INIT_HCA_UDAV_LKEY_OFFSET   	 (INIT_HCA_UDAV_OFFSET + 0x0)
10861da177e4SLinus Torvalds #define  INIT_HCA_UDAV_PD_OFFSET     	 (INIT_HCA_UDAV_OFFSET + 0x4)
10871da177e4SLinus Torvalds #define INIT_HCA_MCAST_OFFSET        	 0x0c0
10881da177e4SLinus Torvalds #define  INIT_HCA_MC_BASE_OFFSET         (INIT_HCA_MCAST_OFFSET + 0x00)
10891da177e4SLinus Torvalds #define  INIT_HCA_LOG_MC_ENTRY_SZ_OFFSET (INIT_HCA_MCAST_OFFSET + 0x12)
10901da177e4SLinus Torvalds #define  INIT_HCA_MC_HASH_SZ_OFFSET      (INIT_HCA_MCAST_OFFSET + 0x16)
10911da177e4SLinus Torvalds #define  INIT_HCA_LOG_MC_TABLE_SZ_OFFSET (INIT_HCA_MCAST_OFFSET + 0x1b)
10921da177e4SLinus Torvalds #define INIT_HCA_TPT_OFFSET              0x0f0
10931da177e4SLinus Torvalds #define  INIT_HCA_MPT_BASE_OFFSET        (INIT_HCA_TPT_OFFSET + 0x00)
10941da177e4SLinus Torvalds #define  INIT_HCA_MTT_SEG_SZ_OFFSET      (INIT_HCA_TPT_OFFSET + 0x09)
10951da177e4SLinus Torvalds #define  INIT_HCA_LOG_MPT_SZ_OFFSET      (INIT_HCA_TPT_OFFSET + 0x0b)
10961da177e4SLinus Torvalds #define  INIT_HCA_MTT_BASE_OFFSET        (INIT_HCA_TPT_OFFSET + 0x10)
10971da177e4SLinus Torvalds #define INIT_HCA_UAR_OFFSET              0x120
10981da177e4SLinus Torvalds #define  INIT_HCA_UAR_BASE_OFFSET        (INIT_HCA_UAR_OFFSET + 0x00)
10991da177e4SLinus Torvalds #define  INIT_HCA_UARC_SZ_OFFSET         (INIT_HCA_UAR_OFFSET + 0x09)
11001da177e4SLinus Torvalds #define  INIT_HCA_LOG_UAR_SZ_OFFSET      (INIT_HCA_UAR_OFFSET + 0x0a)
11011da177e4SLinus Torvalds #define  INIT_HCA_UAR_PAGE_SZ_OFFSET     (INIT_HCA_UAR_OFFSET + 0x0b)
11021da177e4SLinus Torvalds #define  INIT_HCA_UAR_SCATCH_BASE_OFFSET (INIT_HCA_UAR_OFFSET + 0x10)
11031da177e4SLinus Torvalds #define  INIT_HCA_UAR_CTX_BASE_OFFSET    (INIT_HCA_UAR_OFFSET + 0x18)
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	inbox = pci_alloc_consistent(dev->pdev, INIT_HCA_IN_SIZE, &indma);
11061da177e4SLinus Torvalds 	if (!inbox)
11071da177e4SLinus Torvalds 		return -ENOMEM;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	memset(inbox, 0, INIT_HCA_IN_SIZE);
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds #if defined(__LITTLE_ENDIAN)
11121da177e4SLinus Torvalds 	*(inbox + INIT_HCA_FLAGS_OFFSET / 4) &= ~cpu_to_be32(1 << 1);
11131da177e4SLinus Torvalds #elif defined(__BIG_ENDIAN)
11141da177e4SLinus Torvalds 	*(inbox + INIT_HCA_FLAGS_OFFSET / 4) |= cpu_to_be32(1 << 1);
11151da177e4SLinus Torvalds #else
11161da177e4SLinus Torvalds #error Host endianness not defined
11171da177e4SLinus Torvalds #endif
11181da177e4SLinus Torvalds 	/* Check port for UD address vector: */
11191da177e4SLinus Torvalds 	*(inbox + INIT_HCA_FLAGS_OFFSET / 4) |= cpu_to_be32(1);
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	/* We leave wqe_quota, responder_exu, etc as 0 (default) */
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds 	/* QPC/EEC/CQC/EQC/RDB attributes */
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->qpc_base,     INIT_HCA_QPC_BASE_OFFSET);
11261da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_num_qps,  INIT_HCA_LOG_QP_OFFSET);
11271da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->eec_base,     INIT_HCA_EEC_BASE_OFFSET);
11281da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_num_eecs, INIT_HCA_LOG_EEC_OFFSET);
11291da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->srqc_base,    INIT_HCA_SRQC_BASE_OFFSET);
11301da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_num_srqs, INIT_HCA_LOG_SRQ_OFFSET);
11311da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->cqc_base,     INIT_HCA_CQC_BASE_OFFSET);
11321da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_num_cqs,  INIT_HCA_LOG_CQ_OFFSET);
11331da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->eqpc_base,    INIT_HCA_EQPC_BASE_OFFSET);
11341da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->eeec_base,    INIT_HCA_EEEC_BASE_OFFSET);
11351da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->eqc_base,     INIT_HCA_EQC_BASE_OFFSET);
11361da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_num_eqs,  INIT_HCA_LOG_EQ_OFFSET);
11371da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->rdb_base,     INIT_HCA_RDB_BASE_OFFSET);
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 	/* UD AV attributes */
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	/* multicast attributes */
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->mc_base,         INIT_HCA_MC_BASE_OFFSET);
11441da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_mc_entry_sz, INIT_HCA_LOG_MC_ENTRY_SZ_OFFSET);
11451da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->mc_hash_sz,      INIT_HCA_MC_HASH_SZ_OFFSET);
11461da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_mc_table_sz, INIT_HCA_LOG_MC_TABLE_SZ_OFFSET);
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	/* TPT attributes */
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->mpt_base,   INIT_HCA_MPT_BASE_OFFSET);
1151*d10ddbf6SRoland Dreier 	if (!mthca_is_memfree(dev))
11521da177e4SLinus Torvalds 		MTHCA_PUT(inbox, param->mtt_seg_sz, INIT_HCA_MTT_SEG_SZ_OFFSET);
11531da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->log_mpt_sz, INIT_HCA_LOG_MPT_SZ_OFFSET);
11541da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->mtt_base,   INIT_HCA_MTT_BASE_OFFSET);
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	/* UAR attributes */
11571da177e4SLinus Torvalds 	{
11581da177e4SLinus Torvalds 		u8 uar_page_sz = PAGE_SHIFT - 12;
11591da177e4SLinus Torvalds 		MTHCA_PUT(inbox, uar_page_sz, INIT_HCA_UAR_PAGE_SZ_OFFSET);
11601da177e4SLinus Torvalds 	}
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->uar_scratch_base, INIT_HCA_UAR_SCATCH_BASE_OFFSET);
11631da177e4SLinus Torvalds 
1164*d10ddbf6SRoland Dreier 	if (mthca_is_memfree(dev)) {
11651da177e4SLinus Torvalds 		MTHCA_PUT(inbox, param->log_uarc_sz, INIT_HCA_UARC_SZ_OFFSET);
11661da177e4SLinus Torvalds 		MTHCA_PUT(inbox, param->log_uar_sz,  INIT_HCA_LOG_UAR_SZ_OFFSET);
11671da177e4SLinus Torvalds 		MTHCA_PUT(inbox, param->uarc_base,   INIT_HCA_UAR_CTX_BASE_OFFSET);
11681da177e4SLinus Torvalds 	}
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	err = mthca_cmd(dev, indma, 0, 0, CMD_INIT_HCA,
11711da177e4SLinus Torvalds 			HZ, status);
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	pci_free_consistent(dev->pdev, INIT_HCA_IN_SIZE, inbox, indma);
11741da177e4SLinus Torvalds 	return err;
11751da177e4SLinus Torvalds }
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds int mthca_INIT_IB(struct mthca_dev *dev,
11781da177e4SLinus Torvalds 		  struct mthca_init_ib_param *param,
11791da177e4SLinus Torvalds 		  int port, u8 *status)
11801da177e4SLinus Torvalds {
11811da177e4SLinus Torvalds 	u32 *inbox;
11821da177e4SLinus Torvalds 	dma_addr_t indma;
11831da177e4SLinus Torvalds 	int err;
11841da177e4SLinus Torvalds 	u32 flags;
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds #define INIT_IB_IN_SIZE          56
11871da177e4SLinus Torvalds #define INIT_IB_FLAGS_OFFSET     0x00
11881da177e4SLinus Torvalds #define INIT_IB_FLAG_SIG         (1 << 18)
11891da177e4SLinus Torvalds #define INIT_IB_FLAG_NG          (1 << 17)
11901da177e4SLinus Torvalds #define INIT_IB_FLAG_G0          (1 << 16)
11911da177e4SLinus Torvalds #define INIT_IB_FLAG_1X          (1 << 8)
11921da177e4SLinus Torvalds #define INIT_IB_FLAG_4X          (1 << 9)
11931da177e4SLinus Torvalds #define INIT_IB_FLAG_12X         (1 << 11)
11941da177e4SLinus Torvalds #define INIT_IB_VL_SHIFT         4
11951da177e4SLinus Torvalds #define INIT_IB_MTU_SHIFT        12
11961da177e4SLinus Torvalds #define INIT_IB_MAX_GID_OFFSET   0x06
11971da177e4SLinus Torvalds #define INIT_IB_MAX_PKEY_OFFSET  0x0a
11981da177e4SLinus Torvalds #define INIT_IB_GUID0_OFFSET     0x10
11991da177e4SLinus Torvalds #define INIT_IB_NODE_GUID_OFFSET 0x18
12001da177e4SLinus Torvalds #define INIT_IB_SI_GUID_OFFSET   0x20
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	inbox = pci_alloc_consistent(dev->pdev, INIT_IB_IN_SIZE, &indma);
12031da177e4SLinus Torvalds 	if (!inbox)
12041da177e4SLinus Torvalds 		return -ENOMEM;
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	memset(inbox, 0, INIT_IB_IN_SIZE);
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	flags = 0;
12091da177e4SLinus Torvalds 	flags |= param->enable_1x     ? INIT_IB_FLAG_1X  : 0;
12101da177e4SLinus Torvalds 	flags |= param->enable_4x     ? INIT_IB_FLAG_4X  : 0;
12111da177e4SLinus Torvalds 	flags |= param->set_guid0     ? INIT_IB_FLAG_G0  : 0;
12121da177e4SLinus Torvalds 	flags |= param->set_node_guid ? INIT_IB_FLAG_NG  : 0;
12131da177e4SLinus Torvalds 	flags |= param->set_si_guid   ? INIT_IB_FLAG_SIG : 0;
12141da177e4SLinus Torvalds 	flags |= param->vl_cap << INIT_IB_VL_SHIFT;
12151da177e4SLinus Torvalds 	flags |= param->mtu_cap << INIT_IB_MTU_SHIFT;
12161da177e4SLinus Torvalds 	MTHCA_PUT(inbox, flags, INIT_IB_FLAGS_OFFSET);
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->gid_cap,   INIT_IB_MAX_GID_OFFSET);
12191da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->pkey_cap,  INIT_IB_MAX_PKEY_OFFSET);
12201da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->guid0,     INIT_IB_GUID0_OFFSET);
12211da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->node_guid, INIT_IB_NODE_GUID_OFFSET);
12221da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->si_guid,   INIT_IB_SI_GUID_OFFSET);
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	err = mthca_cmd(dev, indma, port, 0, CMD_INIT_IB,
12251da177e4SLinus Torvalds 			CMD_TIME_CLASS_A, status);
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	pci_free_consistent(dev->pdev, INIT_HCA_IN_SIZE, inbox, indma);
12281da177e4SLinus Torvalds 	return err;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds int mthca_CLOSE_IB(struct mthca_dev *dev, int port, u8 *status)
12321da177e4SLinus Torvalds {
12331da177e4SLinus Torvalds 	return mthca_cmd(dev, 0, port, 0, CMD_CLOSE_IB, HZ, status);
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds int mthca_CLOSE_HCA(struct mthca_dev *dev, int panic, u8 *status)
12371da177e4SLinus Torvalds {
12381da177e4SLinus Torvalds 	return mthca_cmd(dev, 0, 0, panic, CMD_CLOSE_HCA, HZ, status);
12391da177e4SLinus Torvalds }
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds int mthca_SET_IB(struct mthca_dev *dev, struct mthca_set_ib_param *param,
12421da177e4SLinus Torvalds 		 int port, u8 *status)
12431da177e4SLinus Torvalds {
12441da177e4SLinus Torvalds 	u32 *inbox;
12451da177e4SLinus Torvalds 	dma_addr_t indma;
12461da177e4SLinus Torvalds 	int err;
12471da177e4SLinus Torvalds 	u32 flags = 0;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds #define SET_IB_IN_SIZE         0x40
12501da177e4SLinus Torvalds #define SET_IB_FLAGS_OFFSET    0x00
12511da177e4SLinus Torvalds #define SET_IB_FLAG_SIG        (1 << 18)
12521da177e4SLinus Torvalds #define SET_IB_FLAG_RQK        (1 <<  0)
12531da177e4SLinus Torvalds #define SET_IB_CAP_MASK_OFFSET 0x04
12541da177e4SLinus Torvalds #define SET_IB_SI_GUID_OFFSET  0x08
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	inbox = pci_alloc_consistent(dev->pdev, SET_IB_IN_SIZE, &indma);
12571da177e4SLinus Torvalds 	if (!inbox)
12581da177e4SLinus Torvalds 		return -ENOMEM;
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 	memset(inbox, 0, SET_IB_IN_SIZE);
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 	flags |= param->set_si_guid     ? SET_IB_FLAG_SIG : 0;
12631da177e4SLinus Torvalds 	flags |= param->reset_qkey_viol ? SET_IB_FLAG_RQK : 0;
12641da177e4SLinus Torvalds 	MTHCA_PUT(inbox, flags, SET_IB_FLAGS_OFFSET);
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->cap_mask, SET_IB_CAP_MASK_OFFSET);
12671da177e4SLinus Torvalds 	MTHCA_PUT(inbox, param->si_guid,  SET_IB_SI_GUID_OFFSET);
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	err = mthca_cmd(dev, indma, port, 0, CMD_SET_IB,
12701da177e4SLinus Torvalds 			CMD_TIME_CLASS_B, status);
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	pci_free_consistent(dev->pdev, INIT_HCA_IN_SIZE, inbox, indma);
12731da177e4SLinus Torvalds 	return err;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds int mthca_MAP_ICM(struct mthca_dev *dev, struct mthca_icm *icm, u64 virt, u8 *status)
12771da177e4SLinus Torvalds {
12781da177e4SLinus Torvalds 	return mthca_map_cmd(dev, CMD_MAP_ICM, icm, virt, status);
12791da177e4SLinus Torvalds }
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds int mthca_MAP_ICM_page(struct mthca_dev *dev, u64 dma_addr, u64 virt, u8 *status)
12821da177e4SLinus Torvalds {
12831da177e4SLinus Torvalds 	u64 *inbox;
12841da177e4SLinus Torvalds 	dma_addr_t indma;
12851da177e4SLinus Torvalds 	int err;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	inbox = pci_alloc_consistent(dev->pdev, 16, &indma);
12881da177e4SLinus Torvalds 	if (!inbox)
12891da177e4SLinus Torvalds 		return -ENOMEM;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	inbox[0] = cpu_to_be64(virt);
12921da177e4SLinus Torvalds 	inbox[1] = cpu_to_be64(dma_addr);
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 	err = mthca_cmd(dev, indma, 1, 0, CMD_MAP_ICM, CMD_TIME_CLASS_B, status);
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	pci_free_consistent(dev->pdev, 16, inbox, indma);
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 	if (!err)
12996bd6228eSRoland Dreier 		mthca_dbg(dev, "Mapped page at %llx to %llx for ICM.\n",
13006bd6228eSRoland Dreier 			  (unsigned long long) dma_addr, (unsigned long long) virt);
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	return err;
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds int mthca_UNMAP_ICM(struct mthca_dev *dev, u64 virt, u32 page_count, u8 *status)
13061da177e4SLinus Torvalds {
13071da177e4SLinus Torvalds 	mthca_dbg(dev, "Unmapping %d pages at %llx from ICM.\n",
13081da177e4SLinus Torvalds 		  page_count, (unsigned long long) virt);
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	return mthca_cmd(dev, virt, page_count, 0, CMD_UNMAP_ICM, CMD_TIME_CLASS_B, status);
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds int mthca_MAP_ICM_AUX(struct mthca_dev *dev, struct mthca_icm *icm, u8 *status)
13141da177e4SLinus Torvalds {
13151da177e4SLinus Torvalds 	return mthca_map_cmd(dev, CMD_MAP_ICM_AUX, icm, -1, status);
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds int mthca_UNMAP_ICM_AUX(struct mthca_dev *dev, u8 *status)
13191da177e4SLinus Torvalds {
13201da177e4SLinus Torvalds 	return mthca_cmd(dev, 0, 0, 0, CMD_UNMAP_ICM_AUX, CMD_TIME_CLASS_B, status);
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds int mthca_SET_ICM_SIZE(struct mthca_dev *dev, u64 icm_size, u64 *aux_pages,
13241da177e4SLinus Torvalds 		       u8 *status)
13251da177e4SLinus Torvalds {
13261da177e4SLinus Torvalds 	int ret = mthca_cmd_imm(dev, icm_size, aux_pages, 0, 0, CMD_SET_ICM_SIZE,
13271da177e4SLinus Torvalds 				CMD_TIME_CLASS_A, status);
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	if (ret || status)
13301da177e4SLinus Torvalds 		return ret;
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	/*
13331da177e4SLinus Torvalds 	 * Arbel page size is always 4 KB; round up number of system
13341da177e4SLinus Torvalds 	 * pages needed.
13351da177e4SLinus Torvalds 	 */
13361da177e4SLinus Torvalds 	*aux_pages = (*aux_pages + (1 << (PAGE_SHIFT - 12)) - 1) >> (PAGE_SHIFT - 12);
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	return 0;
13391da177e4SLinus Torvalds }
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds int mthca_SW2HW_MPT(struct mthca_dev *dev, void *mpt_entry,
13421da177e4SLinus Torvalds 		    int mpt_index, u8 *status)
13431da177e4SLinus Torvalds {
13441da177e4SLinus Torvalds 	dma_addr_t indma;
13451da177e4SLinus Torvalds 	int err;
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	indma = pci_map_single(dev->pdev, mpt_entry,
13481da177e4SLinus Torvalds 			       MTHCA_MPT_ENTRY_SIZE,
13491da177e4SLinus Torvalds 			       PCI_DMA_TODEVICE);
13501da177e4SLinus Torvalds 	if (pci_dma_mapping_error(indma))
13511da177e4SLinus Torvalds 		return -ENOMEM;
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	err = mthca_cmd(dev, indma, mpt_index, 0, CMD_SW2HW_MPT,
13541da177e4SLinus Torvalds 			CMD_TIME_CLASS_B, status);
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	pci_unmap_single(dev->pdev, indma,
13571da177e4SLinus Torvalds 			 MTHCA_MPT_ENTRY_SIZE, PCI_DMA_TODEVICE);
13581da177e4SLinus Torvalds 	return err;
13591da177e4SLinus Torvalds }
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds int mthca_HW2SW_MPT(struct mthca_dev *dev, void *mpt_entry,
13621da177e4SLinus Torvalds 		    int mpt_index, u8 *status)
13631da177e4SLinus Torvalds {
13641da177e4SLinus Torvalds 	dma_addr_t outdma = 0;
13651da177e4SLinus Torvalds 	int err;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	if (mpt_entry) {
13681da177e4SLinus Torvalds 		outdma = pci_map_single(dev->pdev, mpt_entry,
13691da177e4SLinus Torvalds 					MTHCA_MPT_ENTRY_SIZE,
13701da177e4SLinus Torvalds 					PCI_DMA_FROMDEVICE);
13711da177e4SLinus Torvalds 		if (pci_dma_mapping_error(outdma))
13721da177e4SLinus Torvalds 			return -ENOMEM;
13731da177e4SLinus Torvalds 	}
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 	err = mthca_cmd_box(dev, 0, outdma, mpt_index, !mpt_entry,
13761da177e4SLinus Torvalds 			    CMD_HW2SW_MPT,
13771da177e4SLinus Torvalds 			    CMD_TIME_CLASS_B, status);
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	if (mpt_entry)
13801da177e4SLinus Torvalds 		pci_unmap_single(dev->pdev, outdma,
13811da177e4SLinus Torvalds 				 MTHCA_MPT_ENTRY_SIZE,
13821da177e4SLinus Torvalds 				 PCI_DMA_FROMDEVICE);
13831da177e4SLinus Torvalds 	return err;
13841da177e4SLinus Torvalds }
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds int mthca_WRITE_MTT(struct mthca_dev *dev, u64 *mtt_entry,
13871da177e4SLinus Torvalds 		    int num_mtt, u8 *status)
13881da177e4SLinus Torvalds {
13891da177e4SLinus Torvalds 	dma_addr_t indma;
13901da177e4SLinus Torvalds 	int err;
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	indma = pci_map_single(dev->pdev, mtt_entry,
13931da177e4SLinus Torvalds 			       (num_mtt + 2) * 8,
13941da177e4SLinus Torvalds 			       PCI_DMA_TODEVICE);
13951da177e4SLinus Torvalds 	if (pci_dma_mapping_error(indma))
13961da177e4SLinus Torvalds 		return -ENOMEM;
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 	err = mthca_cmd(dev, indma, num_mtt, 0, CMD_WRITE_MTT,
13991da177e4SLinus Torvalds 			CMD_TIME_CLASS_B, status);
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	pci_unmap_single(dev->pdev, indma,
14021da177e4SLinus Torvalds 			 (num_mtt + 2) * 8, PCI_DMA_TODEVICE);
14031da177e4SLinus Torvalds 	return err;
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
1406b8ca06f6SMichael S. Tsirkin int mthca_SYNC_TPT(struct mthca_dev *dev, u8 *status)
1407b8ca06f6SMichael S. Tsirkin {
1408b8ca06f6SMichael S. Tsirkin 	return mthca_cmd(dev, 0, 0, 0, CMD_SYNC_TPT, CMD_TIME_CLASS_B, status);
1409b8ca06f6SMichael S. Tsirkin }
1410b8ca06f6SMichael S. Tsirkin 
14111da177e4SLinus Torvalds int mthca_MAP_EQ(struct mthca_dev *dev, u64 event_mask, int unmap,
14121da177e4SLinus Torvalds 		 int eq_num, u8 *status)
14131da177e4SLinus Torvalds {
14141da177e4SLinus Torvalds 	mthca_dbg(dev, "%s mask %016llx for eqn %d\n",
14151da177e4SLinus Torvalds 		  unmap ? "Clearing" : "Setting",
14161da177e4SLinus Torvalds 		  (unsigned long long) event_mask, eq_num);
14171da177e4SLinus Torvalds 	return mthca_cmd(dev, event_mask, (unmap << 31) | eq_num,
14181da177e4SLinus Torvalds 			 0, CMD_MAP_EQ, CMD_TIME_CLASS_B, status);
14191da177e4SLinus Torvalds }
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds int mthca_SW2HW_EQ(struct mthca_dev *dev, void *eq_context,
14221da177e4SLinus Torvalds 		   int eq_num, u8 *status)
14231da177e4SLinus Torvalds {
14241da177e4SLinus Torvalds 	dma_addr_t indma;
14251da177e4SLinus Torvalds 	int err;
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	indma = pci_map_single(dev->pdev, eq_context,
14281da177e4SLinus Torvalds 			       MTHCA_EQ_CONTEXT_SIZE,
14291da177e4SLinus Torvalds 			       PCI_DMA_TODEVICE);
14301da177e4SLinus Torvalds 	if (pci_dma_mapping_error(indma))
14311da177e4SLinus Torvalds 		return -ENOMEM;
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	err = mthca_cmd(dev, indma, eq_num, 0, CMD_SW2HW_EQ,
14341da177e4SLinus Torvalds 			CMD_TIME_CLASS_A, status);
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 	pci_unmap_single(dev->pdev, indma,
14371da177e4SLinus Torvalds 			 MTHCA_EQ_CONTEXT_SIZE, PCI_DMA_TODEVICE);
14381da177e4SLinus Torvalds 	return err;
14391da177e4SLinus Torvalds }
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds int mthca_HW2SW_EQ(struct mthca_dev *dev, void *eq_context,
14421da177e4SLinus Torvalds 		   int eq_num, u8 *status)
14431da177e4SLinus Torvalds {
14441da177e4SLinus Torvalds 	dma_addr_t outdma = 0;
14451da177e4SLinus Torvalds 	int err;
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 	outdma = pci_map_single(dev->pdev, eq_context,
14481da177e4SLinus Torvalds 				MTHCA_EQ_CONTEXT_SIZE,
14491da177e4SLinus Torvalds 				PCI_DMA_FROMDEVICE);
14501da177e4SLinus Torvalds 	if (pci_dma_mapping_error(outdma))
14511da177e4SLinus Torvalds 		return -ENOMEM;
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 	err = mthca_cmd_box(dev, 0, outdma, eq_num, 0,
14541da177e4SLinus Torvalds 			    CMD_HW2SW_EQ,
14551da177e4SLinus Torvalds 			    CMD_TIME_CLASS_A, status);
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	pci_unmap_single(dev->pdev, outdma,
14581da177e4SLinus Torvalds 			 MTHCA_EQ_CONTEXT_SIZE,
14591da177e4SLinus Torvalds 			 PCI_DMA_FROMDEVICE);
14601da177e4SLinus Torvalds 	return err;
14611da177e4SLinus Torvalds }
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds int mthca_SW2HW_CQ(struct mthca_dev *dev, void *cq_context,
14641da177e4SLinus Torvalds 		   int cq_num, u8 *status)
14651da177e4SLinus Torvalds {
14661da177e4SLinus Torvalds 	dma_addr_t indma;
14671da177e4SLinus Torvalds 	int err;
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	indma = pci_map_single(dev->pdev, cq_context,
14701da177e4SLinus Torvalds 			       MTHCA_CQ_CONTEXT_SIZE,
14711da177e4SLinus Torvalds 			       PCI_DMA_TODEVICE);
14721da177e4SLinus Torvalds 	if (pci_dma_mapping_error(indma))
14731da177e4SLinus Torvalds 		return -ENOMEM;
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 	err = mthca_cmd(dev, indma, cq_num, 0, CMD_SW2HW_CQ,
14761da177e4SLinus Torvalds 			CMD_TIME_CLASS_A, status);
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	pci_unmap_single(dev->pdev, indma,
14791da177e4SLinus Torvalds 			 MTHCA_CQ_CONTEXT_SIZE, PCI_DMA_TODEVICE);
14801da177e4SLinus Torvalds 	return err;
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds int mthca_HW2SW_CQ(struct mthca_dev *dev, void *cq_context,
14841da177e4SLinus Torvalds 		   int cq_num, u8 *status)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	dma_addr_t outdma = 0;
14871da177e4SLinus Torvalds 	int err;
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 	outdma = pci_map_single(dev->pdev, cq_context,
14901da177e4SLinus Torvalds 				MTHCA_CQ_CONTEXT_SIZE,
14911da177e4SLinus Torvalds 				PCI_DMA_FROMDEVICE);
14921da177e4SLinus Torvalds 	if (pci_dma_mapping_error(outdma))
14931da177e4SLinus Torvalds 		return -ENOMEM;
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	err = mthca_cmd_box(dev, 0, outdma, cq_num, 0,
14961da177e4SLinus Torvalds 			    CMD_HW2SW_CQ,
14971da177e4SLinus Torvalds 			    CMD_TIME_CLASS_A, status);
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	pci_unmap_single(dev->pdev, outdma,
15001da177e4SLinus Torvalds 			 MTHCA_CQ_CONTEXT_SIZE,
15011da177e4SLinus Torvalds 			 PCI_DMA_FROMDEVICE);
15021da177e4SLinus Torvalds 	return err;
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds int mthca_MODIFY_QP(struct mthca_dev *dev, int trans, u32 num,
15061da177e4SLinus Torvalds 		    int is_ee, void *qp_context, u32 optmask,
15071da177e4SLinus Torvalds 		    u8 *status)
15081da177e4SLinus Torvalds {
15091da177e4SLinus Torvalds 	static const u16 op[] = {
15101da177e4SLinus Torvalds 		[MTHCA_TRANS_RST2INIT]  = CMD_RST2INIT_QPEE,
15111da177e4SLinus Torvalds 		[MTHCA_TRANS_INIT2INIT] = CMD_INIT2INIT_QPEE,
15121da177e4SLinus Torvalds 		[MTHCA_TRANS_INIT2RTR]  = CMD_INIT2RTR_QPEE,
15131da177e4SLinus Torvalds 		[MTHCA_TRANS_RTR2RTS]   = CMD_RTR2RTS_QPEE,
15141da177e4SLinus Torvalds 		[MTHCA_TRANS_RTS2RTS]   = CMD_RTS2RTS_QPEE,
15151da177e4SLinus Torvalds 		[MTHCA_TRANS_SQERR2RTS] = CMD_SQERR2RTS_QPEE,
15161da177e4SLinus Torvalds 		[MTHCA_TRANS_ANY2ERR]   = CMD_2ERR_QPEE,
15171da177e4SLinus Torvalds 		[MTHCA_TRANS_RTS2SQD]   = CMD_RTS2SQD_QPEE,
15181da177e4SLinus Torvalds 		[MTHCA_TRANS_SQD2SQD]   = CMD_SQD2SQD_QPEE,
15191da177e4SLinus Torvalds 		[MTHCA_TRANS_SQD2RTS]   = CMD_SQD2RTS_QPEE,
15201da177e4SLinus Torvalds 		[MTHCA_TRANS_ANY2RST]   = CMD_ERR2RST_QPEE
15211da177e4SLinus Torvalds 	};
15221da177e4SLinus Torvalds 	u8 op_mod = 0;
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 	dma_addr_t indma;
15251da177e4SLinus Torvalds 	int err;
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	if (trans < 0 || trans >= ARRAY_SIZE(op))
15281da177e4SLinus Torvalds 		return -EINVAL;
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds 	if (trans == MTHCA_TRANS_ANY2RST) {
15311da177e4SLinus Torvalds 		indma  = 0;
15321da177e4SLinus Torvalds 		op_mod = 3;	/* don't write outbox, any->reset */
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 		/* For debugging */
15351da177e4SLinus Torvalds 		qp_context = pci_alloc_consistent(dev->pdev, MTHCA_QP_CONTEXT_SIZE,
15361da177e4SLinus Torvalds 						  &indma);
15371da177e4SLinus Torvalds 		op_mod = 2;	/* write outbox, any->reset */
15381da177e4SLinus Torvalds 	} else {
15391da177e4SLinus Torvalds 		indma = pci_map_single(dev->pdev, qp_context,
15401da177e4SLinus Torvalds 				       MTHCA_QP_CONTEXT_SIZE,
15411da177e4SLinus Torvalds 				       PCI_DMA_TODEVICE);
15421da177e4SLinus Torvalds 		if (pci_dma_mapping_error(indma))
15431da177e4SLinus Torvalds 			return -ENOMEM;
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 		if (0) {
15461da177e4SLinus Torvalds 			int i;
15471da177e4SLinus Torvalds 			mthca_dbg(dev, "Dumping QP context:\n");
15481da177e4SLinus Torvalds 			printk("  opt param mask: %08x\n", be32_to_cpup(qp_context));
15491da177e4SLinus Torvalds 			for (i = 0; i < 0x100 / 4; ++i) {
15501da177e4SLinus Torvalds 				if (i % 8 == 0)
15511da177e4SLinus Torvalds 					printk("  [%02x] ", i * 4);
15521da177e4SLinus Torvalds 				printk(" %08x", be32_to_cpu(((u32 *) qp_context)[i + 2]));
15531da177e4SLinus Torvalds 				if ((i + 1) % 8 == 0)
15541da177e4SLinus Torvalds 					printk("\n");
15551da177e4SLinus Torvalds 			}
15561da177e4SLinus Torvalds 		}
15571da177e4SLinus Torvalds 	}
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 	if (trans == MTHCA_TRANS_ANY2RST) {
15601da177e4SLinus Torvalds 		err = mthca_cmd_box(dev, 0, indma, (!!is_ee << 24) | num,
15611da177e4SLinus Torvalds 				    op_mod, op[trans], CMD_TIME_CLASS_C, status);
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 		if (0) {
15641da177e4SLinus Torvalds 			int i;
15651da177e4SLinus Torvalds 			mthca_dbg(dev, "Dumping QP context:\n");
15661da177e4SLinus Torvalds 			printk(" %08x\n", be32_to_cpup(qp_context));
15671da177e4SLinus Torvalds 			for (i = 0; i < 0x100 / 4; ++i) {
15681da177e4SLinus Torvalds 				if (i % 8 == 0)
15691da177e4SLinus Torvalds 					printk("[%02x] ", i * 4);
15701da177e4SLinus Torvalds 				printk(" %08x", be32_to_cpu(((u32 *) qp_context)[i + 2]));
15711da177e4SLinus Torvalds 				if ((i + 1) % 8 == 0)
15721da177e4SLinus Torvalds 					printk("\n");
15731da177e4SLinus Torvalds 			}
15741da177e4SLinus Torvalds 		}
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	} else
15771da177e4SLinus Torvalds 		err = mthca_cmd(dev, indma, (!!is_ee << 24) | num,
15781da177e4SLinus Torvalds 				op_mod, op[trans], CMD_TIME_CLASS_C, status);
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	if (trans != MTHCA_TRANS_ANY2RST)
15811da177e4SLinus Torvalds 		pci_unmap_single(dev->pdev, indma,
15821da177e4SLinus Torvalds 				 MTHCA_QP_CONTEXT_SIZE, PCI_DMA_TODEVICE);
15831da177e4SLinus Torvalds 	else
15841da177e4SLinus Torvalds 		pci_free_consistent(dev->pdev, MTHCA_QP_CONTEXT_SIZE,
15851da177e4SLinus Torvalds 				    qp_context, indma);
15861da177e4SLinus Torvalds 	return err;
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds int mthca_QUERY_QP(struct mthca_dev *dev, u32 num, int is_ee,
15901da177e4SLinus Torvalds 		   void *qp_context, u8 *status)
15911da177e4SLinus Torvalds {
15921da177e4SLinus Torvalds 	dma_addr_t outdma = 0;
15931da177e4SLinus Torvalds 	int err;
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	outdma = pci_map_single(dev->pdev, qp_context,
15961da177e4SLinus Torvalds 				MTHCA_QP_CONTEXT_SIZE,
15971da177e4SLinus Torvalds 				PCI_DMA_FROMDEVICE);
15981da177e4SLinus Torvalds 	if (pci_dma_mapping_error(outdma))
15991da177e4SLinus Torvalds 		return -ENOMEM;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	err = mthca_cmd_box(dev, 0, outdma, (!!is_ee << 24) | num, 0,
16021da177e4SLinus Torvalds 			    CMD_QUERY_QPEE,
16031da177e4SLinus Torvalds 			    CMD_TIME_CLASS_A, status);
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	pci_unmap_single(dev->pdev, outdma,
16061da177e4SLinus Torvalds 			 MTHCA_QP_CONTEXT_SIZE,
16071da177e4SLinus Torvalds 			 PCI_DMA_FROMDEVICE);
16081da177e4SLinus Torvalds 	return err;
16091da177e4SLinus Torvalds }
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds int mthca_CONF_SPECIAL_QP(struct mthca_dev *dev, int type, u32 qpn,
16121da177e4SLinus Torvalds 			  u8 *status)
16131da177e4SLinus Torvalds {
16141da177e4SLinus Torvalds 	u8 op_mod;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	switch (type) {
16171da177e4SLinus Torvalds 	case IB_QPT_SMI:
16181da177e4SLinus Torvalds 		op_mod = 0;
16191da177e4SLinus Torvalds 		break;
16201da177e4SLinus Torvalds 	case IB_QPT_GSI:
16211da177e4SLinus Torvalds 		op_mod = 1;
16221da177e4SLinus Torvalds 		break;
16231da177e4SLinus Torvalds 	case IB_QPT_RAW_IPV6:
16241da177e4SLinus Torvalds 		op_mod = 2;
16251da177e4SLinus Torvalds 		break;
16261da177e4SLinus Torvalds 	case IB_QPT_RAW_ETY:
16271da177e4SLinus Torvalds 		op_mod = 3;
16281da177e4SLinus Torvalds 		break;
16291da177e4SLinus Torvalds 	default:
16301da177e4SLinus Torvalds 		return -EINVAL;
16311da177e4SLinus Torvalds 	}
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 	return mthca_cmd(dev, 0, qpn, op_mod, CMD_CONF_SPECIAL_QP,
16341da177e4SLinus Torvalds 			 CMD_TIME_CLASS_B, status);
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds int mthca_MAD_IFC(struct mthca_dev *dev, int ignore_mkey, int ignore_bkey,
16381da177e4SLinus Torvalds 		  int port, struct ib_wc* in_wc, struct ib_grh* in_grh,
16391da177e4SLinus Torvalds 		  void *in_mad, void *response_mad, u8 *status)
16401da177e4SLinus Torvalds {
16411da177e4SLinus Torvalds 	void *box;
16421da177e4SLinus Torvalds 	dma_addr_t dma;
16431da177e4SLinus Torvalds 	int err;
16441da177e4SLinus Torvalds 	u32 in_modifier = port;
16451da177e4SLinus Torvalds 	u8 op_modifier = 0;
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds #define MAD_IFC_BOX_SIZE      0x400
16481da177e4SLinus Torvalds #define MAD_IFC_MY_QPN_OFFSET 0x100
16491da177e4SLinus Torvalds #define MAD_IFC_RQPN_OFFSET   0x104
16501da177e4SLinus Torvalds #define MAD_IFC_SL_OFFSET     0x108
16511da177e4SLinus Torvalds #define MAD_IFC_G_PATH_OFFSET 0x109
16521da177e4SLinus Torvalds #define MAD_IFC_RLID_OFFSET   0x10a
16531da177e4SLinus Torvalds #define MAD_IFC_PKEY_OFFSET   0x10e
16541da177e4SLinus Torvalds #define MAD_IFC_GRH_OFFSET    0x140
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	box = pci_alloc_consistent(dev->pdev, MAD_IFC_BOX_SIZE, &dma);
16571da177e4SLinus Torvalds 	if (!box)
16581da177e4SLinus Torvalds 		return -ENOMEM;
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 	memcpy(box, in_mad, 256);
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds 	/*
16631da177e4SLinus Torvalds 	 * Key check traps can't be generated unless we have in_wc to
16641da177e4SLinus Torvalds 	 * tell us where to send the trap.
16651da177e4SLinus Torvalds 	 */
16661da177e4SLinus Torvalds 	if (ignore_mkey || !in_wc)
16671da177e4SLinus Torvalds 		op_modifier |= 0x1;
16681da177e4SLinus Torvalds 	if (ignore_bkey || !in_wc)
16691da177e4SLinus Torvalds 		op_modifier |= 0x2;
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	if (in_wc) {
16721da177e4SLinus Torvalds 		u8 val;
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 		memset(box + 256, 0, 256);
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 		MTHCA_PUT(box, in_wc->qp_num, 	  MAD_IFC_MY_QPN_OFFSET);
16771da177e4SLinus Torvalds 		MTHCA_PUT(box, in_wc->src_qp, 	  MAD_IFC_RQPN_OFFSET);
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 		val = in_wc->sl << 4;
16801da177e4SLinus Torvalds 		MTHCA_PUT(box, val,               MAD_IFC_SL_OFFSET);
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 		val = in_wc->dlid_path_bits |
16831da177e4SLinus Torvalds 			(in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
16841da177e4SLinus Torvalds 		MTHCA_PUT(box, val,               MAD_IFC_GRH_OFFSET);
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 		MTHCA_PUT(box, in_wc->slid,       MAD_IFC_RLID_OFFSET);
16871da177e4SLinus Torvalds 		MTHCA_PUT(box, in_wc->pkey_index, MAD_IFC_PKEY_OFFSET);
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 		if (in_grh)
16901da177e4SLinus Torvalds 			memcpy((u8 *) box + MAD_IFC_GRH_OFFSET, in_grh, 40);
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 		op_modifier |= 0x10;
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 		in_modifier |= in_wc->slid << 16;
16951da177e4SLinus Torvalds 	}
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	err = mthca_cmd_box(dev, dma, dma + 512, in_modifier, op_modifier,
16981da177e4SLinus Torvalds 			    CMD_MAD_IFC, CMD_TIME_CLASS_C, status);
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	if (!err && !*status)
17011da177e4SLinus Torvalds 		memcpy(response_mad, box + 512, 256);
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	pci_free_consistent(dev->pdev, MAD_IFC_BOX_SIZE, box, dma);
17041da177e4SLinus Torvalds 	return err;
17051da177e4SLinus Torvalds }
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds int mthca_READ_MGM(struct mthca_dev *dev, int index, void *mgm,
17081da177e4SLinus Torvalds 		   u8 *status)
17091da177e4SLinus Torvalds {
17101da177e4SLinus Torvalds 	dma_addr_t outdma = 0;
17111da177e4SLinus Torvalds 	int err;
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 	outdma = pci_map_single(dev->pdev, mgm,
17141da177e4SLinus Torvalds 				MTHCA_MGM_ENTRY_SIZE,
17151da177e4SLinus Torvalds 				PCI_DMA_FROMDEVICE);
17161da177e4SLinus Torvalds 	if (pci_dma_mapping_error(outdma))
17171da177e4SLinus Torvalds 		return -ENOMEM;
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 	err = mthca_cmd_box(dev, 0, outdma, index, 0,
17201da177e4SLinus Torvalds 			    CMD_READ_MGM,
17211da177e4SLinus Torvalds 			    CMD_TIME_CLASS_A, status);
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 	pci_unmap_single(dev->pdev, outdma,
17241da177e4SLinus Torvalds 			 MTHCA_MGM_ENTRY_SIZE,
17251da177e4SLinus Torvalds 			 PCI_DMA_FROMDEVICE);
17261da177e4SLinus Torvalds 	return err;
17271da177e4SLinus Torvalds }
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds int mthca_WRITE_MGM(struct mthca_dev *dev, int index, void *mgm,
17301da177e4SLinus Torvalds 		    u8 *status)
17311da177e4SLinus Torvalds {
17321da177e4SLinus Torvalds 	dma_addr_t indma;
17331da177e4SLinus Torvalds 	int err;
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 	indma = pci_map_single(dev->pdev, mgm,
17361da177e4SLinus Torvalds 			       MTHCA_MGM_ENTRY_SIZE,
17371da177e4SLinus Torvalds 			       PCI_DMA_TODEVICE);
17381da177e4SLinus Torvalds 	if (pci_dma_mapping_error(indma))
17391da177e4SLinus Torvalds 		return -ENOMEM;
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds 	err = mthca_cmd(dev, indma, index, 0, CMD_WRITE_MGM,
17421da177e4SLinus Torvalds 			CMD_TIME_CLASS_A, status);
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	pci_unmap_single(dev->pdev, indma,
17451da177e4SLinus Torvalds 			 MTHCA_MGM_ENTRY_SIZE, PCI_DMA_TODEVICE);
17461da177e4SLinus Torvalds 	return err;
17471da177e4SLinus Torvalds }
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds int mthca_MGID_HASH(struct mthca_dev *dev, void *gid, u16 *hash,
17501da177e4SLinus Torvalds 		    u8 *status)
17511da177e4SLinus Torvalds {
17521da177e4SLinus Torvalds 	dma_addr_t indma;
17531da177e4SLinus Torvalds 	u64 imm;
17541da177e4SLinus Torvalds 	int err;
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	indma = pci_map_single(dev->pdev, gid, 16, PCI_DMA_TODEVICE);
17571da177e4SLinus Torvalds 	if (pci_dma_mapping_error(indma))
17581da177e4SLinus Torvalds 		return -ENOMEM;
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 	err = mthca_cmd_imm(dev, indma, &imm, 0, 0, CMD_MGID_HASH,
17611da177e4SLinus Torvalds 			    CMD_TIME_CLASS_A, status);
17621da177e4SLinus Torvalds 	*hash = imm;
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 	pci_unmap_single(dev->pdev, indma, 16, PCI_DMA_TODEVICE);
17651da177e4SLinus Torvalds 	return err;
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds int mthca_NOP(struct mthca_dev *dev, u8 *status)
17691da177e4SLinus Torvalds {
17701da177e4SLinus Torvalds 	return mthca_cmd(dev, 0, 0x1f, 0, CMD_NOP, msecs_to_jiffies(100), status);
17711da177e4SLinus Torvalds }
1772