xref: /dpdk/drivers/common/qat/qat_common.c (revision 68a03efeed657e6e05f281479b33b51102797e15)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4 
5 #include "qat_common.h"
6 #include "qat_device.h"
7 #include "qat_logs.h"
8 
9 int
10 qat_sgl_fill_array(struct rte_mbuf *buf, int64_t offset,
11 		void *list_in, uint32_t data_len,
12 		const uint16_t max_segs)
13 {
14 	int res = -EINVAL;
15 	uint32_t buf_len, nr;
16 	struct qat_sgl *list = (struct qat_sgl *)list_in;
17 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
18 	uint8_t *virt_addr[max_segs];
19 #endif
20 
21 	for (nr = buf_len = 0; buf &&  nr < max_segs; buf = buf->next)  {
22 		if (offset >= rte_pktmbuf_data_len(buf)) {
23 			offset -= rte_pktmbuf_data_len(buf);
24 			continue;
25 		}
26 
27 		list->buffers[nr].len = rte_pktmbuf_data_len(buf) - offset;
28 		list->buffers[nr].resrvd = 0;
29 		list->buffers[nr].addr = rte_pktmbuf_iova_offset(buf, offset);
30 
31 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
32 		virt_addr[nr] = rte_pktmbuf_mtod_offset(buf, uint8_t*, offset);
33 #endif
34 		offset = 0;
35 		buf_len += list->buffers[nr].len;
36 
37 		if (buf_len >= data_len) {
38 			list->buffers[nr].len -= buf_len - data_len;
39 			res = 0;
40 			break;
41 		}
42 		++nr;
43 	}
44 
45 	if (unlikely(res != 0)) {
46 		if (nr == max_segs) {
47 			QAT_DP_LOG(ERR, "Exceeded max segments in QAT SGL (%u)",
48 				   max_segs);
49 		} else {
50 			QAT_DP_LOG(ERR, "Mbuf chain is too short");
51 		}
52 	} else {
53 
54 		list->num_bufs = ++nr;
55 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
56 		QAT_DP_LOG(INFO, "SGL with %d buffers:", list->num_bufs);
57 		for (nr = 0; nr < list->num_bufs; nr++) {
58 			QAT_DP_LOG(INFO,
59 				"QAT SGL buf %d, len = %d, iova = 0x%012"PRIx64,
60 				 nr, list->buffers[nr].len,
61 				 list->buffers[nr].addr);
62 			QAT_DP_HEXDUMP_LOG(DEBUG, "qat SGL",
63 					   virt_addr[nr],
64 					   list->buffers[nr].len);
65 		}
66 #endif
67 	}
68 
69 	return res;
70 }
71 
72 void qat_stats_get(struct qat_pci_device *dev,
73 		struct qat_common_stats *stats,
74 		enum qat_service_type service)
75 {
76 	int i;
77 	struct qat_qp **qp;
78 
79 	if (stats == NULL || dev == NULL || service >= QAT_SERVICE_INVALID) {
80 		QAT_LOG(ERR, "invalid param: stats %p, dev %p, service %d",
81 				stats, dev, service);
82 		return;
83 	}
84 
85 	qp = dev->qps_in_use[service];
86 	for (i = 0; i < ADF_MAX_QPS_ON_ANY_SERVICE; i++) {
87 		if (qp[i] == NULL) {
88 			QAT_LOG(DEBUG, "Service %d Uninitialised qp %d",
89 					service, i);
90 			continue;
91 		}
92 
93 		stats->enqueued_count += qp[i]->stats.enqueued_count;
94 		stats->dequeued_count += qp[i]->stats.dequeued_count;
95 		stats->enqueue_err_count += qp[i]->stats.enqueue_err_count;
96 		stats->dequeue_err_count += qp[i]->stats.dequeue_err_count;
97 		stats->threshold_hit_count += qp[i]->stats.threshold_hit_count;
98 		QAT_LOG(DEBUG, "Threshold was used for qp %d %"PRIu64" times",
99 				i, stats->threshold_hit_count);
100 	}
101 }
102 
103 void qat_stats_reset(struct qat_pci_device *dev,
104 		enum qat_service_type service)
105 {
106 	int i;
107 	struct qat_qp **qp;
108 
109 	if (dev == NULL || service >= QAT_SERVICE_INVALID) {
110 		QAT_LOG(ERR, "invalid param: dev %p, service %d",
111 				dev, service);
112 		return;
113 	}
114 
115 	qp = dev->qps_in_use[service];
116 	for (i = 0; i < ADF_MAX_QPS_ON_ANY_SERVICE; i++) {
117 		if (qp[i] == NULL) {
118 			QAT_LOG(DEBUG, "Service %d Uninitialised qp %d",
119 					service, i);
120 			continue;
121 		}
122 		memset(&(qp[i]->stats), 0, sizeof(qp[i]->stats));
123 	}
124 
125 	QAT_LOG(DEBUG, "QAT: %d stats cleared", service);
126 }
127