18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright(c) 2016 - 2018 Intel Corporation.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
58c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
108c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
118c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
148c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
158c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
168c2ecf20Sopenharmony_ci * General Public License for more details.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * BSD LICENSE
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
218c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
228c2ecf20Sopenharmony_ci * are met:
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci *  - Redistributions of source code must retain the above copyright
258c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
268c2ecf20Sopenharmony_ci *  - Redistributions in binary form must reproduce the above copyright
278c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in
288c2ecf20Sopenharmony_ci *    the documentation and/or other materials provided with the
298c2ecf20Sopenharmony_ci *    distribution.
308c2ecf20Sopenharmony_ci *  - Neither the name of Intel Corporation nor the names of its
318c2ecf20Sopenharmony_ci *    contributors may be used to endorse or promote products derived
328c2ecf20Sopenharmony_ci *    from this software without specific prior written permission.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
358c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
368c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
378c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
388c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
398c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
408c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
418c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
428c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
438c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
448c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#include "hfi.h"
498c2ecf20Sopenharmony_ci#include "verbs_txreq.h"
508c2ecf20Sopenharmony_ci#include "qp.h"
518c2ecf20Sopenharmony_ci#include "trace.h"
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define TXREQ_LEN 24
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_civoid hfi1_put_txreq(struct verbs_txreq *tx)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	struct hfi1_ibdev *dev;
588c2ecf20Sopenharmony_ci	struct rvt_qp *qp;
598c2ecf20Sopenharmony_ci	unsigned long flags;
608c2ecf20Sopenharmony_ci	unsigned int seq;
618c2ecf20Sopenharmony_ci	struct hfi1_qp_priv *priv;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	qp = tx->qp;
648c2ecf20Sopenharmony_ci	dev = to_idev(qp->ibqp.device);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	if (tx->mr)
678c2ecf20Sopenharmony_ci		rvt_put_mr(tx->mr);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	sdma_txclean(dd_from_dev(dev), &tx->txreq);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/* Free verbs_txreq and return to slab cache */
728c2ecf20Sopenharmony_ci	kmem_cache_free(dev->verbs_txreq_cache, tx);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	do {
758c2ecf20Sopenharmony_ci		seq = read_seqbegin(&dev->txwait_lock);
768c2ecf20Sopenharmony_ci		if (!list_empty(&dev->txwait)) {
778c2ecf20Sopenharmony_ci			struct iowait *wait;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci			write_seqlock_irqsave(&dev->txwait_lock, flags);
808c2ecf20Sopenharmony_ci			wait = list_first_entry(&dev->txwait, struct iowait,
818c2ecf20Sopenharmony_ci						list);
828c2ecf20Sopenharmony_ci			qp = iowait_to_qp(wait);
838c2ecf20Sopenharmony_ci			priv = qp->priv;
848c2ecf20Sopenharmony_ci			list_del_init(&priv->s_iowait.list);
858c2ecf20Sopenharmony_ci			/* refcount held until actual wake up */
868c2ecf20Sopenharmony_ci			write_sequnlock_irqrestore(&dev->txwait_lock, flags);
878c2ecf20Sopenharmony_ci			hfi1_qp_wakeup(qp, RVT_S_WAIT_TX);
888c2ecf20Sopenharmony_ci			break;
898c2ecf20Sopenharmony_ci		}
908c2ecf20Sopenharmony_ci	} while (read_seqretry(&dev->txwait_lock, seq));
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistruct verbs_txreq *__get_txreq(struct hfi1_ibdev *dev,
948c2ecf20Sopenharmony_ci				struct rvt_qp *qp)
958c2ecf20Sopenharmony_ci	__must_hold(&qp->s_lock)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	struct verbs_txreq *tx = NULL;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	write_seqlock(&dev->txwait_lock);
1008c2ecf20Sopenharmony_ci	if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
1018c2ecf20Sopenharmony_ci		struct hfi1_qp_priv *priv;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci		tx = kmem_cache_alloc(dev->verbs_txreq_cache, VERBS_TXREQ_GFP);
1048c2ecf20Sopenharmony_ci		if (tx)
1058c2ecf20Sopenharmony_ci			goto out;
1068c2ecf20Sopenharmony_ci		priv = qp->priv;
1078c2ecf20Sopenharmony_ci		if (list_empty(&priv->s_iowait.list)) {
1088c2ecf20Sopenharmony_ci			dev->n_txwait++;
1098c2ecf20Sopenharmony_ci			qp->s_flags |= RVT_S_WAIT_TX;
1108c2ecf20Sopenharmony_ci			list_add_tail(&priv->s_iowait.list, &dev->txwait);
1118c2ecf20Sopenharmony_ci			priv->s_iowait.lock = &dev->txwait_lock;
1128c2ecf20Sopenharmony_ci			trace_hfi1_qpsleep(qp, RVT_S_WAIT_TX);
1138c2ecf20Sopenharmony_ci			rvt_get_qp(qp);
1148c2ecf20Sopenharmony_ci		}
1158c2ecf20Sopenharmony_ci		qp->s_flags &= ~RVT_S_BUSY;
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ciout:
1188c2ecf20Sopenharmony_ci	write_sequnlock(&dev->txwait_lock);
1198c2ecf20Sopenharmony_ci	return tx;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciint verbs_txreq_init(struct hfi1_ibdev *dev)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	char buf[TXREQ_LEN];
1258c2ecf20Sopenharmony_ci	struct hfi1_devdata *dd = dd_from_dev(dev);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	snprintf(buf, sizeof(buf), "hfi1_%u_vtxreq_cache", dd->unit);
1288c2ecf20Sopenharmony_ci	dev->verbs_txreq_cache = kmem_cache_create(buf,
1298c2ecf20Sopenharmony_ci						   sizeof(struct verbs_txreq),
1308c2ecf20Sopenharmony_ci						   0, SLAB_HWCACHE_ALIGN,
1318c2ecf20Sopenharmony_ci						   NULL);
1328c2ecf20Sopenharmony_ci	if (!dev->verbs_txreq_cache)
1338c2ecf20Sopenharmony_ci		return -ENOMEM;
1348c2ecf20Sopenharmony_ci	return 0;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_civoid verbs_txreq_exit(struct hfi1_ibdev *dev)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	kmem_cache_destroy(dev->verbs_txreq_cache);
1408c2ecf20Sopenharmony_ci	dev->verbs_txreq_cache = NULL;
1418c2ecf20Sopenharmony_ci}
142