net/bnxt: check access denied for HWRM commands
[dpdk.git] / drivers / common / qat / qat_common.c
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, uint64_t buf_start,
11                 void *list_in, uint32_t data_len,
12                 const uint16_t max_segs)
13 {
14         int nr = 1;
15         struct qat_sgl *list = (struct qat_sgl *)list_in;
16         /* buf_start allows the first buffer to start at an address before or
17          * after the mbuf data start. It's used to either optimally align the
18          * dma to 64 or to start dma from an offset.
19          */
20         uint32_t buf_len;
21         uint32_t first_buf_len = rte_pktmbuf_data_len(buf) +
22                         (rte_pktmbuf_mtophys(buf) - buf_start);
23 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
24         uint8_t *virt_addr[max_segs];
25         virt_addr[0] = rte_pktmbuf_mtod(buf, uint8_t*) +
26                         (rte_pktmbuf_mtophys(buf) - buf_start);
27 #endif
28
29         list->buffers[0].addr = buf_start;
30         list->buffers[0].resrvd = 0;
31         list->buffers[0].len = first_buf_len;
32
33         if (data_len <= first_buf_len) {
34                 list->num_bufs = nr;
35                 list->buffers[0].len = data_len;
36                 goto sgl_end;
37         }
38
39         buf = buf->next;
40         buf_len = first_buf_len;
41         while (buf) {
42                 if (unlikely(nr == max_segs)) {
43                         QAT_DP_LOG(ERR, "Exceeded max segments in QAT SGL (%u)",
44                                         max_segs);
45                         return -EINVAL;
46                 }
47
48                 list->buffers[nr].len = rte_pktmbuf_data_len(buf);
49                 list->buffers[nr].resrvd = 0;
50                 list->buffers[nr].addr = rte_pktmbuf_mtophys(buf);
51 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
52                 virt_addr[nr] = rte_pktmbuf_mtod(buf, uint8_t*);
53 #endif
54                 buf_len += list->buffers[nr].len;
55                 buf = buf->next;
56
57                 if (buf_len >= data_len) {
58                         list->buffers[nr].len -=
59                                 buf_len - data_len;
60                         buf = NULL;
61                 }
62                 ++nr;
63         }
64         list->num_bufs = nr;
65
66 sgl_end:
67 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
68         {
69                 uint16_t i;
70                 QAT_DP_LOG(INFO, "SGL with %d buffers:", list->num_bufs);
71                 for (i = 0; i < list->num_bufs; i++) {
72                         QAT_DP_LOG(INFO,
73                                 "QAT SGL buf %d, len = %d, iova = 0x%012"PRIx64,
74                                 i, list->buffers[i].len,
75                                 list->buffers[i].addr);
76                         QAT_DP_HEXDUMP_LOG(DEBUG, "qat SGL",
77                                         virt_addr[i], list->buffers[i].len);
78                 }
79         }
80 #endif
81
82         return 0;
83 }
84
85 void qat_stats_get(struct qat_pci_device *dev,
86                 struct qat_common_stats *stats,
87                 enum qat_service_type service)
88 {
89         int i;
90         struct qat_qp **qp;
91
92         if (stats == NULL || dev == NULL || service >= QAT_SERVICE_INVALID) {
93                 QAT_LOG(ERR, "invalid param: stats %p, dev %p, service %d",
94                                 stats, dev, service);
95                 return;
96         }
97
98         qp = dev->qps_in_use[service];
99         for (i = 0; i < ADF_MAX_QPS_ON_ANY_SERVICE; i++) {
100                 if (qp[i] == NULL) {
101                         QAT_LOG(DEBUG, "Service %d Uninitialised qp %d",
102                                         service, i);
103                         continue;
104                 }
105
106                 stats->enqueued_count += qp[i]->stats.enqueued_count;
107                 stats->dequeued_count += qp[i]->stats.dequeued_count;
108                 stats->enqueue_err_count += qp[i]->stats.enqueue_err_count;
109                 stats->dequeue_err_count += qp[i]->stats.dequeue_err_count;
110         }
111 }
112
113 void qat_stats_reset(struct qat_pci_device *dev,
114                 enum qat_service_type service)
115 {
116         int i;
117         struct qat_qp **qp;
118
119         if (dev == NULL || service >= QAT_SERVICE_INVALID) {
120                 QAT_LOG(ERR, "invalid param: dev %p, service %d",
121                                 dev, service);
122                 return;
123         }
124
125         qp = dev->qps_in_use[service];
126         for (i = 0; i < ADF_MAX_QPS_ON_ANY_SERVICE; i++) {
127                 if (qp[i] == NULL) {
128                         QAT_LOG(DEBUG, "Service %d Uninitialised qp %d",
129                                         service, i);
130                         continue;
131                 }
132                 memset(&(qp[i]->stats), 0, sizeof(qp[i]->stats));
133         }
134
135         QAT_LOG(DEBUG, "QAT: %d stats cleared", service);
136 }