162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * This file is part of the Chelsio T6 Ethernet driver for Linux.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (c) 2017-2018 Chelsio Communications, Inc. All rights reserved.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This software is available to you under a choice of one of two
762306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
862306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
962306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
1062306a36Sopenharmony_ci * OpenIB.org BSD license below:
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1362306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1462306a36Sopenharmony_ci *     conditions are met:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1762306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1862306a36Sopenharmony_ci *        disclaimer.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2162306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2262306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2362306a36Sopenharmony_ci *        provided with the distribution.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2662306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2762306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2862306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2962306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3062306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3162306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3262306a36Sopenharmony_ci * SOFTWARE.
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#include "cxgb4.h"
3662306a36Sopenharmony_ci#include "t4_msg.h"
3762306a36Sopenharmony_ci#include "srq.h"
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_cistruct srq_data *t4_init_srq(int srq_size)
4062306a36Sopenharmony_ci{
4162306a36Sopenharmony_ci	struct srq_data *s;
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	s = kvzalloc(sizeof(*s), GFP_KERNEL);
4462306a36Sopenharmony_ci	if (!s)
4562306a36Sopenharmony_ci		return NULL;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	s->srq_size = srq_size;
4862306a36Sopenharmony_ci	init_completion(&s->comp);
4962306a36Sopenharmony_ci	mutex_init(&s->lock);
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	return s;
5262306a36Sopenharmony_ci}
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/* cxgb4_get_srq_entry: read the SRQ table entry
5562306a36Sopenharmony_ci * @dev: Pointer to the net_device
5662306a36Sopenharmony_ci * @idx: Index to the srq
5762306a36Sopenharmony_ci * @entryp: pointer to the srq entry
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci * Sends CPL_SRQ_TABLE_REQ message for the given index.
6062306a36Sopenharmony_ci * Contents will be returned in CPL_SRQ_TABLE_RPL message.
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * Returns zero if the read is successful, else a error
6362306a36Sopenharmony_ci * number will be returned. Caller should not use the srq
6462306a36Sopenharmony_ci * entry if the return value is non-zero.
6562306a36Sopenharmony_ci *
6662306a36Sopenharmony_ci *
6762306a36Sopenharmony_ci */
6862306a36Sopenharmony_ciint cxgb4_get_srq_entry(struct net_device *dev,
6962306a36Sopenharmony_ci			int srq_idx, struct srq_entry *entryp)
7062306a36Sopenharmony_ci{
7162306a36Sopenharmony_ci	struct cpl_srq_table_req *req;
7262306a36Sopenharmony_ci	struct adapter *adap;
7362306a36Sopenharmony_ci	struct sk_buff *skb;
7462306a36Sopenharmony_ci	struct srq_data *s;
7562306a36Sopenharmony_ci	int rc = -ENODEV;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	adap = netdev2adap(dev);
7862306a36Sopenharmony_ci	s = adap->srq;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	if (!(adap->flags & CXGB4_FULL_INIT_DONE) || !s)
8162306a36Sopenharmony_ci		goto out;
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	skb = alloc_skb(sizeof(*req), GFP_KERNEL);
8462306a36Sopenharmony_ci	if (!skb)
8562306a36Sopenharmony_ci		return -ENOMEM;
8662306a36Sopenharmony_ci	req = (struct cpl_srq_table_req *)
8762306a36Sopenharmony_ci		__skb_put_zero(skb, sizeof(*req));
8862306a36Sopenharmony_ci	INIT_TP_WR(req, 0);
8962306a36Sopenharmony_ci	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SRQ_TABLE_REQ,
9062306a36Sopenharmony_ci					      TID_TID_V(srq_idx) |
9162306a36Sopenharmony_ci				TID_QID_V(adap->sge.fw_evtq.abs_id)));
9262306a36Sopenharmony_ci	req->idx = srq_idx;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	mutex_lock(&s->lock);
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	s->entryp = entryp;
9762306a36Sopenharmony_ci	t4_mgmt_tx(adap, skb);
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	rc = wait_for_completion_timeout(&s->comp, SRQ_WAIT_TO);
10062306a36Sopenharmony_ci	if (rc)
10162306a36Sopenharmony_ci		rc = 0;
10262306a36Sopenharmony_ci	else /* !rc means we timed out */
10362306a36Sopenharmony_ci		rc = -ETIMEDOUT;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	WARN_ON_ONCE(entryp->idx != srq_idx);
10662306a36Sopenharmony_ci	mutex_unlock(&s->lock);
10762306a36Sopenharmony_ciout:
10862306a36Sopenharmony_ci	return rc;
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_ciEXPORT_SYMBOL(cxgb4_get_srq_entry);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_civoid do_srq_table_rpl(struct adapter *adap,
11362306a36Sopenharmony_ci		      const struct cpl_srq_table_rpl *rpl)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	unsigned int idx = TID_TID_G(GET_TID(rpl));
11662306a36Sopenharmony_ci	struct srq_data *s = adap->srq;
11762306a36Sopenharmony_ci	struct srq_entry *e;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	if (unlikely(rpl->status != CPL_CONTAINS_READ_RPL)) {
12062306a36Sopenharmony_ci		dev_err(adap->pdev_dev,
12162306a36Sopenharmony_ci			"Unexpected SRQ_TABLE_RPL status %u for entry %u\n",
12262306a36Sopenharmony_ci				rpl->status, idx);
12362306a36Sopenharmony_ci		goto out;
12462306a36Sopenharmony_ci	}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	/* Store the read entry */
12762306a36Sopenharmony_ci	e = s->entryp;
12862306a36Sopenharmony_ci	e->valid = 1;
12962306a36Sopenharmony_ci	e->idx = idx;
13062306a36Sopenharmony_ci	e->pdid = SRQT_PDID_G(be64_to_cpu(rpl->rsvd_pdid));
13162306a36Sopenharmony_ci	e->qlen = SRQT_QLEN_G(be32_to_cpu(rpl->qlen_qbase));
13262306a36Sopenharmony_ci	e->qbase = SRQT_QBASE_G(be32_to_cpu(rpl->qlen_qbase));
13362306a36Sopenharmony_ci	e->cur_msn = be16_to_cpu(rpl->cur_msn);
13462306a36Sopenharmony_ci	e->max_msn = be16_to_cpu(rpl->max_msn);
13562306a36Sopenharmony_ciout:
13662306a36Sopenharmony_ci	complete(&s->comp);
13762306a36Sopenharmony_ci}
138