162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2012-2016 VMware, Inc.  All rights reserved.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or
562306a36Sopenharmony_ci * modify it under the terms of EITHER the GNU General Public License
662306a36Sopenharmony_ci * version 2 as published by the Free Software Foundation or the BSD
762306a36Sopenharmony_ci * 2-Clause License. This program is distributed in the hope that it
862306a36Sopenharmony_ci * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
962306a36Sopenharmony_ci * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
1062306a36Sopenharmony_ci * See the GNU General Public License version 2 for more details at
1162306a36Sopenharmony_ci * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * You should have received a copy of the GNU General Public License
1462306a36Sopenharmony_ci * along with this program available in the file COPYING in the main
1562306a36Sopenharmony_ci * directory of this source tree.
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * The BSD 2-Clause License
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
2062306a36Sopenharmony_ci *     without modification, are permitted provided that the following
2162306a36Sopenharmony_ci *     conditions are met:
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
2462306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2562306a36Sopenharmony_ci *        disclaimer.
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2862306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2962306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
3062306a36Sopenharmony_ci *        provided with the distribution.
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3362306a36Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3462306a36Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
3562306a36Sopenharmony_ci * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
3662306a36Sopenharmony_ci * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
3762306a36Sopenharmony_ci * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3862306a36Sopenharmony_ci * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3962306a36Sopenharmony_ci * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4062306a36Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4162306a36Sopenharmony_ci * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4262306a36Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4362306a36Sopenharmony_ci * OF THE POSSIBILITY OF SUCH DAMAGE.
4462306a36Sopenharmony_ci */
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#include <linux/list.h>
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci#include "pvrdma.h"
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci#define PVRDMA_CMD_TIMEOUT	10000 /* ms */
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_cistatic inline int pvrdma_cmd_recv(struct pvrdma_dev *dev,
5362306a36Sopenharmony_ci				  union pvrdma_cmd_resp *resp,
5462306a36Sopenharmony_ci				  unsigned resp_code)
5562306a36Sopenharmony_ci{
5662306a36Sopenharmony_ci	int err;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	dev_dbg(&dev->pdev->dev, "receive response from device\n");
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	err = wait_for_completion_interruptible_timeout(&dev->cmd_done,
6162306a36Sopenharmony_ci			msecs_to_jiffies(PVRDMA_CMD_TIMEOUT));
6262306a36Sopenharmony_ci	if (err == 0 || err == -ERESTARTSYS) {
6362306a36Sopenharmony_ci		dev_warn(&dev->pdev->dev,
6462306a36Sopenharmony_ci			 "completion timeout or interrupted\n");
6562306a36Sopenharmony_ci		return -ETIMEDOUT;
6662306a36Sopenharmony_ci	}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	spin_lock(&dev->cmd_lock);
6962306a36Sopenharmony_ci	memcpy(resp, dev->resp_slot, sizeof(*resp));
7062306a36Sopenharmony_ci	spin_unlock(&dev->cmd_lock);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	if (resp->hdr.ack != resp_code) {
7362306a36Sopenharmony_ci		dev_warn(&dev->pdev->dev,
7462306a36Sopenharmony_ci			 "unknown response %#x expected %#x\n",
7562306a36Sopenharmony_ci			 resp->hdr.ack, resp_code);
7662306a36Sopenharmony_ci		return -EFAULT;
7762306a36Sopenharmony_ci	}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	return 0;
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ciint
8362306a36Sopenharmony_cipvrdma_cmd_post(struct pvrdma_dev *dev, union pvrdma_cmd_req *req,
8462306a36Sopenharmony_ci		union pvrdma_cmd_resp *resp, unsigned resp_code)
8562306a36Sopenharmony_ci{
8662306a36Sopenharmony_ci	int err;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	dev_dbg(&dev->pdev->dev, "post request to device\n");
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	/* Serializiation */
9162306a36Sopenharmony_ci	down(&dev->cmd_sema);
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	BUILD_BUG_ON(sizeof(union pvrdma_cmd_req) !=
9462306a36Sopenharmony_ci		     sizeof(struct pvrdma_cmd_modify_qp));
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	spin_lock(&dev->cmd_lock);
9762306a36Sopenharmony_ci	memcpy(dev->cmd_slot, req, sizeof(*req));
9862306a36Sopenharmony_ci	spin_unlock(&dev->cmd_lock);
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	init_completion(&dev->cmd_done);
10162306a36Sopenharmony_ci	pvrdma_write_reg(dev, PVRDMA_REG_REQUEST, 0);
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	/* Make sure the request is written before reading status. */
10462306a36Sopenharmony_ci	mb();
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	err = pvrdma_read_reg(dev, PVRDMA_REG_ERR);
10762306a36Sopenharmony_ci	if (err == 0) {
10862306a36Sopenharmony_ci		if (resp != NULL)
10962306a36Sopenharmony_ci			err = pvrdma_cmd_recv(dev, resp, resp_code);
11062306a36Sopenharmony_ci	} else {
11162306a36Sopenharmony_ci		dev_warn(&dev->pdev->dev,
11262306a36Sopenharmony_ci			 "failed to write request error reg: %d\n", err);
11362306a36Sopenharmony_ci		err = -EFAULT;
11462306a36Sopenharmony_ci	}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	up(&dev->cmd_sema);
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci	return err;
11962306a36Sopenharmony_ci}
120