1 /* Copyright 2008 - 2016 Freescale Semiconductor, Inc.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5  *     * Redistributions of source code must retain the above copyright
6  *	 notice, this list of conditions and the following disclaimer.
7  *     * Redistributions in binary form must reproduce the above copyright
8  *	 notice, this list of conditions and the following disclaimer in the
9  *	 documentation and/or other materials provided with the distribution.
10  *     * Neither the name of Freescale Semiconductor nor the
11  *	 names of its contributors may be used to endorse or promote products
12  *	 derived from this software without specific prior written permission.
13  *
14  * ALTERNATIVELY, this software may be distributed under the terms of the
15  * GNU General Public License ("GPL") as published by the Free Software
16  * Foundation, either version 2 of that License or (at your option) any
17  * later version.
18  *
19  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "qman_test.h"
32 
33 #define CGR_ID		27
34 #define POOL_ID		2
35 #define FQ_FLAGS	QMAN_FQ_FLAG_DYNAMIC_FQID
36 #define NUM_ENQUEUES	10
37 #define NUM_PARTIAL	4
38 #define PORTAL_SDQCR	(QM_SDQCR_SOURCE_CHANNELS | \
39 			QM_SDQCR_TYPE_PRIO_QOS | \
40 			QM_SDQCR_TOKEN_SET(0x98) | \
41 			QM_SDQCR_CHANNELS_DEDICATED | \
42 			QM_SDQCR_CHANNELS_POOL(POOL_ID))
43 #define PORTAL_OPAQUE	((void *)0xf00dbeef)
44 #define VDQCR_FLAGS	(QMAN_VOLATILE_FLAG_WAIT | QMAN_VOLATILE_FLAG_FINISH)
45 
46 static enum qman_cb_dqrr_result cb_dqrr(struct qman_portal *,
47 					struct qman_fq *,
48 					const struct qm_dqrr_entry *);
49 static void cb_ern(struct qman_portal *, struct qman_fq *,
50 		   const union qm_mr_entry *);
51 static void cb_fqs(struct qman_portal *, struct qman_fq *,
52 		   const union qm_mr_entry *);
53 
54 static struct qm_fd fd, fd_dq;
55 static struct qman_fq fq_base = {
56 	.cb.dqrr = cb_dqrr,
57 	.cb.ern = cb_ern,
58 	.cb.fqs = cb_fqs
59 };
60 static DECLARE_WAIT_QUEUE_HEAD(waitqueue);
61 static int retire_complete, sdqcr_complete;
62 
63 /* Helpers for initialising and "incrementing" a frame descriptor */
64 static void fd_init(struct qm_fd *fd)
65 {
66 	qm_fd_addr_set64(fd, 0xabdeadbeefLLU);
67 	qm_fd_set_contig_big(fd, 0x0000ffff);
68 	fd->cmd = cpu_to_be32(0xfeedf00d);
69 }
70 
71 static void fd_inc(struct qm_fd *fd)
72 {
73 	u64 t = qm_fd_addr_get64(fd);
74 	int z = t >> 40;
75 	unsigned int len, off;
76 	enum qm_fd_format fmt;
77 
78 	t <<= 1;
79 	if (z)
80 		t |= 1;
81 	qm_fd_addr_set64(fd, t);
82 
83 	fmt = qm_fd_get_format(fd);
84 	off = qm_fd_get_offset(fd);
85 	len = qm_fd_get_length(fd);
86 	len--;
87 	qm_fd_set_param(fd, fmt, off, len);
88 
89 	fd->cmd = cpu_to_be32(be32_to_cpu(fd->cmd) + 1);
90 }
91 
92 /* The only part of the 'fd' we can't memcmp() is the ppid */
93 static bool fd_neq(const struct qm_fd *a, const struct qm_fd *b)
94 {
95 	bool neq = qm_fd_addr_get64(a) != qm_fd_addr_get64(b);
96 
97 	neq |= qm_fd_get_format(a) != qm_fd_get_format(b);
98 	neq |= a->cfg != b->cfg;
99 	neq |= a->cmd != b->cmd;
100 
101 	return neq;
102 }
103 
104 /* test */
105 static int do_enqueues(struct qman_fq *fq)
106 {
107 	unsigned int loop;
108 	int err = 0;
109 
110 	for (loop = 0; loop < NUM_ENQUEUES; loop++) {
111 		if (qman_enqueue(fq, &fd)) {
112 			pr_crit("qman_enqueue() failed\n");
113 			err = -EIO;
114 		}
115 		fd_inc(&fd);
116 	}
117 
118 	return err;
119 }
120 
121 int qman_test_api(void)
122 {
123 	unsigned int flags, frmcnt;
124 	int err;
125 	struct qman_fq *fq = &fq_base;
126 
127 	pr_info("%s(): Starting\n", __func__);
128 	fd_init(&fd);
129 	fd_init(&fd_dq);
130 
131 	/* Initialise (parked) FQ */
132 	err = qman_create_fq(0, FQ_FLAGS, fq);
133 	if (err) {
134 		pr_crit("qman_create_fq() failed\n");
135 		goto failed;
136 	}
137 	err = qman_init_fq(fq, QMAN_INITFQ_FLAG_LOCAL, NULL);
138 	if (err) {
139 		pr_crit("qman_init_fq() failed\n");
140 		goto failed;
141 	}
142 	/* Do enqueues + VDQCR, twice. (Parked FQ) */
143 	err = do_enqueues(fq);
144 	if (err)
145 		goto failed;
146 	pr_info("VDQCR (till-empty);\n");
147 	frmcnt = QM_VDQCR_NUMFRAMES_TILLEMPTY;
148 	err = qman_volatile_dequeue(fq, VDQCR_FLAGS, frmcnt);
149 	if (err) {
150 		pr_crit("qman_volatile_dequeue() failed\n");
151 		goto failed;
152 	}
153 	err = do_enqueues(fq);
154 	if (err)
155 		goto failed;
156 	pr_info("VDQCR (%d of %d);\n", NUM_PARTIAL, NUM_ENQUEUES);
157 	frmcnt = QM_VDQCR_NUMFRAMES_SET(NUM_PARTIAL);
158 	err = qman_volatile_dequeue(fq, VDQCR_FLAGS, frmcnt);
159 	if (err) {
160 		pr_crit("qman_volatile_dequeue() failed\n");
161 		goto failed;
162 	}
163 	pr_info("VDQCR (%d of %d);\n", NUM_ENQUEUES - NUM_PARTIAL,
164 		NUM_ENQUEUES);
165 	frmcnt = QM_VDQCR_NUMFRAMES_SET(NUM_ENQUEUES - NUM_PARTIAL);
166 	err = qman_volatile_dequeue(fq, VDQCR_FLAGS, frmcnt);
167 	if (err) {
168 		pr_err("qman_volatile_dequeue() failed\n");
169 		goto failed;
170 	}
171 
172 	err = do_enqueues(fq);
173 	if (err)
174 		goto failed;
175 	pr_info("scheduled dequeue (till-empty)\n");
176 	err = qman_schedule_fq(fq);
177 	if (err) {
178 		pr_crit("qman_schedule_fq() failed\n");
179 		goto failed;
180 	}
181 	wait_event(waitqueue, sdqcr_complete);
182 
183 	/* Retire and OOS the FQ */
184 	err = qman_retire_fq(fq, &flags);
185 	if (err < 0) {
186 		pr_crit("qman_retire_fq() failed\n");
187 		goto failed;
188 	}
189 	wait_event(waitqueue, retire_complete);
190 	if (flags & QMAN_FQ_STATE_BLOCKOOS) {
191 		err = -EIO;
192 		pr_crit("leaking frames\n");
193 		goto failed;
194 	}
195 	err = qman_oos_fq(fq);
196 	if (err) {
197 		pr_crit("qman_oos_fq() failed\n");
198 		goto failed;
199 	}
200 	qman_destroy_fq(fq);
201 	pr_info("%s(): Finished\n", __func__);
202 	return 0;
203 
204 failed:
205 	WARN_ON(1);
206 	return err;
207 }
208 
209 static enum qman_cb_dqrr_result cb_dqrr(struct qman_portal *p,
210 					struct qman_fq *fq,
211 					const struct qm_dqrr_entry *dq)
212 {
213 	if (WARN_ON(fd_neq(&fd_dq, &dq->fd))) {
214 		pr_err("BADNESS: dequeued frame doesn't match;\n");
215 		return qman_cb_dqrr_consume;
216 	}
217 	fd_inc(&fd_dq);
218 	if (!(dq->stat & QM_DQRR_STAT_UNSCHEDULED) && !fd_neq(&fd_dq, &fd)) {
219 		sdqcr_complete = 1;
220 		wake_up(&waitqueue);
221 	}
222 	return qman_cb_dqrr_consume;
223 }
224 
225 static void cb_ern(struct qman_portal *p, struct qman_fq *fq,
226 		   const union qm_mr_entry *msg)
227 {
228 	pr_crit("cb_ern() unimplemented");
229 	WARN_ON(1);
230 }
231 
232 static void cb_fqs(struct qman_portal *p, struct qman_fq *fq,
233 		   const union qm_mr_entry *msg)
234 {
235 	u8 verb = (msg->verb & QM_MR_VERB_TYPE_MASK);
236 
237 	if ((verb != QM_MR_VERB_FQRN) && (verb != QM_MR_VERB_FQRNI)) {
238 		pr_crit("unexpected FQS message");
239 		WARN_ON(1);
240 		return;
241 	}
242 	pr_info("Retirement message received\n");
243 	retire_complete = 1;
244 	wake_up(&waitqueue);
245 }
246