xref: /openbmc/linux/drivers/accel/qaic/qaic_data.c (revision 1f0d40d8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 /* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved. */
4 /* Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved. */
5 
6 #include <linux/bitfield.h>
7 #include <linux/bits.h>
8 #include <linux/completion.h>
9 #include <linux/delay.h>
10 #include <linux/dma-buf.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/interrupt.h>
13 #include <linux/kref.h>
14 #include <linux/list.h>
15 #include <linux/math64.h>
16 #include <linux/mm.h>
17 #include <linux/moduleparam.h>
18 #include <linux/scatterlist.h>
19 #include <linux/spinlock.h>
20 #include <linux/srcu.h>
21 #include <linux/types.h>
22 #include <linux/uaccess.h>
23 #include <linux/wait.h>
24 #include <drm/drm_file.h>
25 #include <drm/drm_gem.h>
26 #include <drm/drm_print.h>
27 #include <uapi/drm/qaic_accel.h>
28 
29 #include "qaic.h"
30 
31 #define SEM_VAL_MASK	GENMASK_ULL(11, 0)
32 #define SEM_INDEX_MASK	GENMASK_ULL(4, 0)
33 #define BULK_XFER	BIT(3)
34 #define GEN_COMPLETION	BIT(4)
35 #define INBOUND_XFER	1
36 #define OUTBOUND_XFER	2
37 #define REQHP_OFF	0x0 /* we read this */
38 #define REQTP_OFF	0x4 /* we write this */
39 #define RSPHP_OFF	0x8 /* we write this */
40 #define RSPTP_OFF	0xc /* we read this */
41 
42 #define ENCODE_SEM(val, index, sync, cmd, flags)			\
43 		({							\
44 			FIELD_PREP(GENMASK(11, 0), (val)) |		\
45 			FIELD_PREP(GENMASK(20, 16), (index)) |		\
46 			FIELD_PREP(BIT(22), (sync)) |			\
47 			FIELD_PREP(GENMASK(26, 24), (cmd)) |		\
48 			FIELD_PREP(GENMASK(30, 29), (flags)) |		\
49 			FIELD_PREP(BIT(31), (cmd) ? 1 : 0);		\
50 		})
51 #define NUM_EVENTS	128
52 #define NUM_DELAYS	10
53 
54 static unsigned int wait_exec_default_timeout_ms = 5000; /* 5 sec default */
55 module_param(wait_exec_default_timeout_ms, uint, 0600);
56 MODULE_PARM_DESC(wait_exec_default_timeout_ms, "Default timeout for DRM_IOCTL_QAIC_WAIT_BO");
57 
58 static unsigned int datapath_poll_interval_us = 100; /* 100 usec default */
59 module_param(datapath_poll_interval_us, uint, 0600);
60 MODULE_PARM_DESC(datapath_poll_interval_us,
61 		 "Amount of time to sleep between activity when datapath polling is enabled");
62 
63 struct dbc_req {
64 	/*
65 	 * A request ID is assigned to each memory handle going in DMA queue.
66 	 * As a single memory handle can enqueue multiple elements in DMA queue
67 	 * all of them will have the same request ID.
68 	 */
69 	__le16	req_id;
70 	/* Future use */
71 	__u8	seq_id;
72 	/*
73 	 * Special encoded variable
74 	 * 7	0 - Do not force to generate MSI after DMA is completed
75 	 *	1 - Force to generate MSI after DMA is completed
76 	 * 6:5	Reserved
77 	 * 4	1 - Generate completion element in the response queue
78 	 *	0 - No Completion Code
79 	 * 3	0 - DMA request is a Link list transfer
80 	 *	1 - DMA request is a Bulk transfer
81 	 * 2	Reserved
82 	 * 1:0	00 - No DMA transfer involved
83 	 *	01 - DMA transfer is part of inbound transfer
84 	 *	10 - DMA transfer has outbound transfer
85 	 *	11 - NA
86 	 */
87 	__u8	cmd;
88 	__le32	resv;
89 	/* Source address for the transfer */
90 	__le64	src_addr;
91 	/* Destination address for the transfer */
92 	__le64	dest_addr;
93 	/* Length of transfer request */
94 	__le32	len;
95 	__le32	resv2;
96 	/* Doorbell address */
97 	__le64	db_addr;
98 	/*
99 	 * Special encoded variable
100 	 * 7	1 - Doorbell(db) write
101 	 *	0 - No doorbell write
102 	 * 6:2	Reserved
103 	 * 1:0	00 - 32 bit access, db address must be aligned to 32bit-boundary
104 	 *	01 - 16 bit access, db address must be aligned to 16bit-boundary
105 	 *	10 - 8 bit access, db address must be aligned to 8bit-boundary
106 	 *	11 - Reserved
107 	 */
108 	__u8	db_len;
109 	__u8	resv3;
110 	__le16	resv4;
111 	/* 32 bit data written to doorbell address */
112 	__le32	db_data;
113 	/*
114 	 * Special encoded variable
115 	 * All the fields of sem_cmdX are passed from user and all are ORed
116 	 * together to form sem_cmd.
117 	 * 0:11		Semaphore value
118 	 * 15:12	Reserved
119 	 * 20:16	Semaphore index
120 	 * 21		Reserved
121 	 * 22		Semaphore Sync
122 	 * 23		Reserved
123 	 * 26:24	Semaphore command
124 	 * 28:27	Reserved
125 	 * 29		Semaphore DMA out bound sync fence
126 	 * 30		Semaphore DMA in bound sync fence
127 	 * 31		Enable semaphore command
128 	 */
129 	__le32	sem_cmd0;
130 	__le32	sem_cmd1;
131 	__le32	sem_cmd2;
132 	__le32	sem_cmd3;
133 } __packed;
134 
135 struct dbc_rsp {
136 	/* Request ID of the memory handle whose DMA transaction is completed */
137 	__le16	req_id;
138 	/* Status of the DMA transaction. 0 : Success otherwise failure */
139 	__le16	status;
140 } __packed;
141 
142 inline int get_dbc_req_elem_size(void)
143 {
144 	return sizeof(struct dbc_req);
145 }
146 
147 inline int get_dbc_rsp_elem_size(void)
148 {
149 	return sizeof(struct dbc_rsp);
150 }
151 
152 static void free_slice(struct kref *kref)
153 {
154 	struct bo_slice *slice = container_of(kref, struct bo_slice, ref_count);
155 
156 	list_del(&slice->slice);
157 	drm_gem_object_put(&slice->bo->base);
158 	sg_free_table(slice->sgt);
159 	kfree(slice->sgt);
160 	kfree(slice->reqs);
161 	kfree(slice);
162 }
163 
164 static int clone_range_of_sgt_for_slice(struct qaic_device *qdev, struct sg_table **sgt_out,
165 					struct sg_table *sgt_in, u64 size, u64 offset)
166 {
167 	int total_len, len, nents, offf = 0, offl = 0;
168 	struct scatterlist *sg, *sgn, *sgf, *sgl;
169 	struct sg_table *sgt;
170 	int ret, j;
171 
172 	/* find out number of relevant nents needed for this mem */
173 	total_len = 0;
174 	sgf = NULL;
175 	sgl = NULL;
176 	nents = 0;
177 
178 	size = size ? size : PAGE_SIZE;
179 	for (sg = sgt_in->sgl; sg; sg = sg_next(sg)) {
180 		len = sg_dma_len(sg);
181 
182 		if (!len)
183 			continue;
184 		if (offset >= total_len && offset < total_len + len) {
185 			sgf = sg;
186 			offf = offset - total_len;
187 		}
188 		if (sgf)
189 			nents++;
190 		if (offset + size >= total_len &&
191 		    offset + size <= total_len + len) {
192 			sgl = sg;
193 			offl = offset + size - total_len;
194 			break;
195 		}
196 		total_len += len;
197 	}
198 
199 	if (!sgf || !sgl) {
200 		ret = -EINVAL;
201 		goto out;
202 	}
203 
204 	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
205 	if (!sgt) {
206 		ret = -ENOMEM;
207 		goto out;
208 	}
209 
210 	ret = sg_alloc_table(sgt, nents, GFP_KERNEL);
211 	if (ret)
212 		goto free_sgt;
213 
214 	/* copy relevant sg node and fix page and length */
215 	sgn = sgf;
216 	for_each_sgtable_sg(sgt, sg, j) {
217 		memcpy(sg, sgn, sizeof(*sg));
218 		if (sgn == sgf) {
219 			sg_dma_address(sg) += offf;
220 			sg_dma_len(sg) -= offf;
221 			sg_set_page(sg, sg_page(sgn), sg_dma_len(sg), offf);
222 		} else {
223 			offf = 0;
224 		}
225 		if (sgn == sgl) {
226 			sg_dma_len(sg) = offl - offf;
227 			sg_set_page(sg, sg_page(sgn), offl - offf, offf);
228 			sg_mark_end(sg);
229 			break;
230 		}
231 		sgn = sg_next(sgn);
232 	}
233 
234 	*sgt_out = sgt;
235 	return ret;
236 
237 free_sgt:
238 	kfree(sgt);
239 out:
240 	*sgt_out = NULL;
241 	return ret;
242 }
243 
244 static int encode_reqs(struct qaic_device *qdev, struct bo_slice *slice,
245 		       struct qaic_attach_slice_entry *req)
246 {
247 	__le64 db_addr = cpu_to_le64(req->db_addr);
248 	__le32 db_data = cpu_to_le32(req->db_data);
249 	struct scatterlist *sg;
250 	__u8 cmd = BULK_XFER;
251 	int presync_sem;
252 	u64 dev_addr;
253 	__u8 db_len;
254 	int i;
255 
256 	if (!slice->no_xfer)
257 		cmd |= (slice->dir == DMA_TO_DEVICE ? INBOUND_XFER : OUTBOUND_XFER);
258 
259 	if (req->db_len && !IS_ALIGNED(req->db_addr, req->db_len / 8))
260 		return -EINVAL;
261 
262 	presync_sem = req->sem0.presync + req->sem1.presync + req->sem2.presync + req->sem3.presync;
263 	if (presync_sem > 1)
264 		return -EINVAL;
265 
266 	presync_sem = req->sem0.presync << 0 | req->sem1.presync << 1 |
267 		      req->sem2.presync << 2 | req->sem3.presync << 3;
268 
269 	switch (req->db_len) {
270 	case 32:
271 		db_len = BIT(7);
272 		break;
273 	case 16:
274 		db_len = BIT(7) | 1;
275 		break;
276 	case 8:
277 		db_len = BIT(7) | 2;
278 		break;
279 	case 0:
280 		db_len = 0; /* doorbell is not active for this command */
281 		break;
282 	default:
283 		return -EINVAL; /* should never hit this */
284 	}
285 
286 	/*
287 	 * When we end up splitting up a single request (ie a buf slice) into
288 	 * multiple DMA requests, we have to manage the sync data carefully.
289 	 * There can only be one presync sem. That needs to be on every xfer
290 	 * so that the DMA engine doesn't transfer data before the receiver is
291 	 * ready. We only do the doorbell and postsync sems after the xfer.
292 	 * To guarantee previous xfers for the request are complete, we use a
293 	 * fence.
294 	 */
295 	dev_addr = req->dev_addr;
296 	for_each_sgtable_sg(slice->sgt, sg, i) {
297 		slice->reqs[i].cmd = cmd;
298 		slice->reqs[i].src_addr = cpu_to_le64(slice->dir == DMA_TO_DEVICE ?
299 						      sg_dma_address(sg) : dev_addr);
300 		slice->reqs[i].dest_addr = cpu_to_le64(slice->dir == DMA_TO_DEVICE ?
301 						       dev_addr : sg_dma_address(sg));
302 		/*
303 		 * sg_dma_len(sg) returns size of a DMA segment, maximum DMA
304 		 * segment size is set to UINT_MAX by qaic and hence return
305 		 * values of sg_dma_len(sg) can never exceed u32 range. So,
306 		 * by down sizing we are not corrupting the value.
307 		 */
308 		slice->reqs[i].len = cpu_to_le32((u32)sg_dma_len(sg));
309 		switch (presync_sem) {
310 		case BIT(0):
311 			slice->reqs[i].sem_cmd0 = cpu_to_le32(ENCODE_SEM(req->sem0.val,
312 									 req->sem0.index,
313 									 req->sem0.presync,
314 									 req->sem0.cmd,
315 									 req->sem0.flags));
316 			break;
317 		case BIT(1):
318 			slice->reqs[i].sem_cmd1 = cpu_to_le32(ENCODE_SEM(req->sem1.val,
319 									 req->sem1.index,
320 									 req->sem1.presync,
321 									 req->sem1.cmd,
322 									 req->sem1.flags));
323 			break;
324 		case BIT(2):
325 			slice->reqs[i].sem_cmd2 = cpu_to_le32(ENCODE_SEM(req->sem2.val,
326 									 req->sem2.index,
327 									 req->sem2.presync,
328 									 req->sem2.cmd,
329 									 req->sem2.flags));
330 			break;
331 		case BIT(3):
332 			slice->reqs[i].sem_cmd3 = cpu_to_le32(ENCODE_SEM(req->sem3.val,
333 									 req->sem3.index,
334 									 req->sem3.presync,
335 									 req->sem3.cmd,
336 									 req->sem3.flags));
337 			break;
338 		}
339 		dev_addr += sg_dma_len(sg);
340 	}
341 	/* add post transfer stuff to last segment */
342 	i--;
343 	slice->reqs[i].cmd |= GEN_COMPLETION;
344 	slice->reqs[i].db_addr = db_addr;
345 	slice->reqs[i].db_len = db_len;
346 	slice->reqs[i].db_data = db_data;
347 	/*
348 	 * Add a fence if we have more than one request going to the hardware
349 	 * representing the entirety of the user request, and the user request
350 	 * has no presync condition.
351 	 * Fences are expensive, so we try to avoid them. We rely on the
352 	 * hardware behavior to avoid needing one when there is a presync
353 	 * condition. When a presync exists, all requests for that same
354 	 * presync will be queued into a fifo. Thus, since we queue the
355 	 * post xfer activity only on the last request we queue, the hardware
356 	 * will ensure that the last queued request is processed last, thus
357 	 * making sure the post xfer activity happens at the right time without
358 	 * a fence.
359 	 */
360 	if (i && !presync_sem)
361 		req->sem0.flags |= (slice->dir == DMA_TO_DEVICE ?
362 				    QAIC_SEM_INSYNCFENCE : QAIC_SEM_OUTSYNCFENCE);
363 	slice->reqs[i].sem_cmd0 = cpu_to_le32(ENCODE_SEM(req->sem0.val, req->sem0.index,
364 							 req->sem0.presync, req->sem0.cmd,
365 							 req->sem0.flags));
366 	slice->reqs[i].sem_cmd1 = cpu_to_le32(ENCODE_SEM(req->sem1.val, req->sem1.index,
367 							 req->sem1.presync, req->sem1.cmd,
368 							 req->sem1.flags));
369 	slice->reqs[i].sem_cmd2 = cpu_to_le32(ENCODE_SEM(req->sem2.val, req->sem2.index,
370 							 req->sem2.presync, req->sem2.cmd,
371 							 req->sem2.flags));
372 	slice->reqs[i].sem_cmd3 = cpu_to_le32(ENCODE_SEM(req->sem3.val, req->sem3.index,
373 							 req->sem3.presync, req->sem3.cmd,
374 							 req->sem3.flags));
375 
376 	return 0;
377 }
378 
379 static int qaic_map_one_slice(struct qaic_device *qdev, struct qaic_bo *bo,
380 			      struct qaic_attach_slice_entry *slice_ent)
381 {
382 	struct sg_table *sgt = NULL;
383 	struct bo_slice *slice;
384 	int ret;
385 
386 	ret = clone_range_of_sgt_for_slice(qdev, &sgt, bo->sgt, slice_ent->size, slice_ent->offset);
387 	if (ret)
388 		goto out;
389 
390 	slice = kmalloc(sizeof(*slice), GFP_KERNEL);
391 	if (!slice) {
392 		ret = -ENOMEM;
393 		goto free_sgt;
394 	}
395 
396 	slice->reqs = kcalloc(sgt->nents, sizeof(*slice->reqs), GFP_KERNEL);
397 	if (!slice->reqs) {
398 		ret = -ENOMEM;
399 		goto free_slice;
400 	}
401 
402 	slice->no_xfer = !slice_ent->size;
403 	slice->sgt = sgt;
404 	slice->nents = sgt->nents;
405 	slice->dir = bo->dir;
406 	slice->bo = bo;
407 	slice->size = slice_ent->size;
408 	slice->offset = slice_ent->offset;
409 
410 	ret = encode_reqs(qdev, slice, slice_ent);
411 	if (ret)
412 		goto free_req;
413 
414 	bo->total_slice_nents += sgt->nents;
415 	kref_init(&slice->ref_count);
416 	drm_gem_object_get(&bo->base);
417 	list_add_tail(&slice->slice, &bo->slices);
418 
419 	return 0;
420 
421 free_req:
422 	kfree(slice->reqs);
423 free_slice:
424 	kfree(slice);
425 free_sgt:
426 	sg_free_table(sgt);
427 	kfree(sgt);
428 out:
429 	return ret;
430 }
431 
432 static int create_sgt(struct qaic_device *qdev, struct sg_table **sgt_out, u64 size)
433 {
434 	struct scatterlist *sg;
435 	struct sg_table *sgt;
436 	struct page **pages;
437 	int *pages_order;
438 	int buf_extra;
439 	int max_order;
440 	int nr_pages;
441 	int ret = 0;
442 	int i, j, k;
443 	int order;
444 
445 	if (size) {
446 		nr_pages = DIV_ROUND_UP(size, PAGE_SIZE);
447 		/*
448 		 * calculate how much extra we are going to allocate, to remove
449 		 * later
450 		 */
451 		buf_extra = (PAGE_SIZE - size % PAGE_SIZE) % PAGE_SIZE;
452 		max_order = min(MAX_ORDER - 1, get_order(size));
453 	} else {
454 		/* allocate a single page for book keeping */
455 		nr_pages = 1;
456 		buf_extra = 0;
457 		max_order = 0;
458 	}
459 
460 	pages = kvmalloc_array(nr_pages, sizeof(*pages) + sizeof(*pages_order), GFP_KERNEL);
461 	if (!pages) {
462 		ret = -ENOMEM;
463 		goto out;
464 	}
465 	pages_order = (void *)pages + sizeof(*pages) * nr_pages;
466 
467 	/*
468 	 * Allocate requested memory using alloc_pages. It is possible to allocate
469 	 * the requested memory in multiple chunks by calling alloc_pages
470 	 * multiple times. Use SG table to handle multiple allocated pages.
471 	 */
472 	i = 0;
473 	while (nr_pages > 0) {
474 		order = min(get_order(nr_pages * PAGE_SIZE), max_order);
475 		while (1) {
476 			pages[i] = alloc_pages(GFP_KERNEL | GFP_HIGHUSER |
477 					       __GFP_NOWARN | __GFP_ZERO |
478 					       (order ? __GFP_NORETRY : __GFP_RETRY_MAYFAIL),
479 					       order);
480 			if (pages[i])
481 				break;
482 			if (!order--) {
483 				ret = -ENOMEM;
484 				goto free_partial_alloc;
485 			}
486 		}
487 
488 		max_order = order;
489 		pages_order[i] = order;
490 
491 		nr_pages -= 1 << order;
492 		if (nr_pages <= 0)
493 			/* account for over allocation */
494 			buf_extra += abs(nr_pages) * PAGE_SIZE;
495 		i++;
496 	}
497 
498 	sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
499 	if (!sgt) {
500 		ret = -ENOMEM;
501 		goto free_partial_alloc;
502 	}
503 
504 	if (sg_alloc_table(sgt, i, GFP_KERNEL)) {
505 		ret = -ENOMEM;
506 		goto free_sgt;
507 	}
508 
509 	/* Populate the SG table with the allocated memory pages */
510 	sg = sgt->sgl;
511 	for (k = 0; k < i; k++, sg = sg_next(sg)) {
512 		/* Last entry requires special handling */
513 		if (k < i - 1) {
514 			sg_set_page(sg, pages[k], PAGE_SIZE << pages_order[k], 0);
515 		} else {
516 			sg_set_page(sg, pages[k], (PAGE_SIZE << pages_order[k]) - buf_extra, 0);
517 			sg_mark_end(sg);
518 		}
519 	}
520 
521 	kvfree(pages);
522 	*sgt_out = sgt;
523 	return ret;
524 
525 free_sgt:
526 	kfree(sgt);
527 free_partial_alloc:
528 	for (j = 0; j < i; j++)
529 		__free_pages(pages[j], pages_order[j]);
530 	kvfree(pages);
531 out:
532 	*sgt_out = NULL;
533 	return ret;
534 }
535 
536 static bool invalid_sem(struct qaic_sem *sem)
537 {
538 	if (sem->val & ~SEM_VAL_MASK || sem->index & ~SEM_INDEX_MASK ||
539 	    !(sem->presync == 0 || sem->presync == 1) || sem->pad ||
540 	    sem->flags & ~(QAIC_SEM_INSYNCFENCE | QAIC_SEM_OUTSYNCFENCE) ||
541 	    sem->cmd > QAIC_SEM_WAIT_GT_0)
542 		return true;
543 	return false;
544 }
545 
546 static int qaic_validate_req(struct qaic_device *qdev, struct qaic_attach_slice_entry *slice_ent,
547 			     u32 count, u64 total_size)
548 {
549 	int i;
550 
551 	for (i = 0; i < count; i++) {
552 		if (!(slice_ent[i].db_len == 32 || slice_ent[i].db_len == 16 ||
553 		      slice_ent[i].db_len == 8 || slice_ent[i].db_len == 0) ||
554 		      invalid_sem(&slice_ent[i].sem0) || invalid_sem(&slice_ent[i].sem1) ||
555 		      invalid_sem(&slice_ent[i].sem2) || invalid_sem(&slice_ent[i].sem3))
556 			return -EINVAL;
557 
558 		if (slice_ent[i].offset + slice_ent[i].size > total_size)
559 			return -EINVAL;
560 	}
561 
562 	return 0;
563 }
564 
565 static void qaic_free_sgt(struct sg_table *sgt)
566 {
567 	struct scatterlist *sg;
568 
569 	for (sg = sgt->sgl; sg; sg = sg_next(sg))
570 		if (sg_page(sg))
571 			__free_pages(sg_page(sg), get_order(sg->length));
572 	sg_free_table(sgt);
573 	kfree(sgt);
574 }
575 
576 static void qaic_gem_print_info(struct drm_printer *p, unsigned int indent,
577 				const struct drm_gem_object *obj)
578 {
579 	struct qaic_bo *bo = to_qaic_bo(obj);
580 
581 	drm_printf_indent(p, indent, "user requested size=%llu\n", bo->size);
582 }
583 
584 static const struct vm_operations_struct drm_vm_ops = {
585 	.open = drm_gem_vm_open,
586 	.close = drm_gem_vm_close,
587 };
588 
589 static int qaic_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
590 {
591 	struct qaic_bo *bo = to_qaic_bo(obj);
592 	unsigned long offset = 0;
593 	struct scatterlist *sg;
594 	int ret;
595 
596 	if (obj->import_attach)
597 		return -EINVAL;
598 
599 	for (sg = bo->sgt->sgl; sg; sg = sg_next(sg)) {
600 		if (sg_page(sg)) {
601 			ret = remap_pfn_range(vma, vma->vm_start + offset, page_to_pfn(sg_page(sg)),
602 					      sg->length, vma->vm_page_prot);
603 			if (ret)
604 				goto out;
605 			offset += sg->length;
606 		}
607 	}
608 
609 out:
610 	return ret;
611 }
612 
613 static void qaic_free_object(struct drm_gem_object *obj)
614 {
615 	struct qaic_bo *bo = to_qaic_bo(obj);
616 
617 	if (obj->import_attach) {
618 		/* DMABUF/PRIME Path */
619 		dma_buf_detach(obj->import_attach->dmabuf, obj->import_attach);
620 		dma_buf_put(obj->import_attach->dmabuf);
621 	} else {
622 		/* Private buffer allocation path */
623 		qaic_free_sgt(bo->sgt);
624 	}
625 
626 	drm_gem_object_release(obj);
627 	kfree(bo);
628 }
629 
630 static const struct drm_gem_object_funcs qaic_gem_funcs = {
631 	.free = qaic_free_object,
632 	.print_info = qaic_gem_print_info,
633 	.mmap = qaic_gem_object_mmap,
634 	.vm_ops = &drm_vm_ops,
635 };
636 
637 static struct qaic_bo *qaic_alloc_init_bo(void)
638 {
639 	struct qaic_bo *bo;
640 
641 	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
642 	if (!bo)
643 		return ERR_PTR(-ENOMEM);
644 
645 	INIT_LIST_HEAD(&bo->slices);
646 	init_completion(&bo->xfer_done);
647 	complete_all(&bo->xfer_done);
648 
649 	return bo;
650 }
651 
652 int qaic_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
653 {
654 	struct qaic_create_bo *args = data;
655 	int usr_rcu_id, qdev_rcu_id;
656 	struct drm_gem_object *obj;
657 	struct qaic_device *qdev;
658 	struct qaic_user *usr;
659 	struct qaic_bo *bo;
660 	size_t size;
661 	int ret;
662 
663 	if (args->pad)
664 		return -EINVAL;
665 
666 	usr = file_priv->driver_priv;
667 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
668 	if (!usr->qddev) {
669 		ret = -ENODEV;
670 		goto unlock_usr_srcu;
671 	}
672 
673 	qdev = usr->qddev->qdev;
674 	qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
675 	if (qdev->in_reset) {
676 		ret = -ENODEV;
677 		goto unlock_dev_srcu;
678 	}
679 
680 	size = PAGE_ALIGN(args->size);
681 	if (size == 0) {
682 		ret = -EINVAL;
683 		goto unlock_dev_srcu;
684 	}
685 
686 	bo = qaic_alloc_init_bo();
687 	if (IS_ERR(bo)) {
688 		ret = PTR_ERR(bo);
689 		goto unlock_dev_srcu;
690 	}
691 	obj = &bo->base;
692 
693 	drm_gem_private_object_init(dev, obj, size);
694 
695 	obj->funcs = &qaic_gem_funcs;
696 	ret = create_sgt(qdev, &bo->sgt, size);
697 	if (ret)
698 		goto free_bo;
699 
700 	bo->size = args->size;
701 
702 	ret = drm_gem_handle_create(file_priv, obj, &args->handle);
703 	if (ret)
704 		goto free_sgt;
705 
706 	bo->handle = args->handle;
707 	drm_gem_object_put(obj);
708 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
709 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
710 
711 	return 0;
712 
713 free_sgt:
714 	qaic_free_sgt(bo->sgt);
715 free_bo:
716 	kfree(bo);
717 unlock_dev_srcu:
718 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
719 unlock_usr_srcu:
720 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
721 	return ret;
722 }
723 
724 int qaic_mmap_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
725 {
726 	struct qaic_mmap_bo *args = data;
727 	int usr_rcu_id, qdev_rcu_id;
728 	struct drm_gem_object *obj;
729 	struct qaic_device *qdev;
730 	struct qaic_user *usr;
731 	int ret;
732 
733 	usr = file_priv->driver_priv;
734 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
735 	if (!usr->qddev) {
736 		ret = -ENODEV;
737 		goto unlock_usr_srcu;
738 	}
739 
740 	qdev = usr->qddev->qdev;
741 	qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
742 	if (qdev->in_reset) {
743 		ret = -ENODEV;
744 		goto unlock_dev_srcu;
745 	}
746 
747 	obj = drm_gem_object_lookup(file_priv, args->handle);
748 	if (!obj) {
749 		ret = -ENOENT;
750 		goto unlock_dev_srcu;
751 	}
752 
753 	ret = drm_gem_create_mmap_offset(obj);
754 	if (ret == 0)
755 		args->offset = drm_vma_node_offset_addr(&obj->vma_node);
756 
757 	drm_gem_object_put(obj);
758 
759 unlock_dev_srcu:
760 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
761 unlock_usr_srcu:
762 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
763 	return ret;
764 }
765 
766 struct drm_gem_object *qaic_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
767 {
768 	struct dma_buf_attachment *attach;
769 	struct drm_gem_object *obj;
770 	struct qaic_bo *bo;
771 	size_t size;
772 	int ret;
773 
774 	bo = qaic_alloc_init_bo();
775 	if (IS_ERR(bo)) {
776 		ret = PTR_ERR(bo);
777 		goto out;
778 	}
779 
780 	obj = &bo->base;
781 	get_dma_buf(dma_buf);
782 
783 	attach = dma_buf_attach(dma_buf, dev->dev);
784 	if (IS_ERR(attach)) {
785 		ret = PTR_ERR(attach);
786 		goto attach_fail;
787 	}
788 
789 	size = PAGE_ALIGN(attach->dmabuf->size);
790 	if (size == 0) {
791 		ret = -EINVAL;
792 		goto size_align_fail;
793 	}
794 
795 	drm_gem_private_object_init(dev, obj, size);
796 	/*
797 	 * skipping dma_buf_map_attachment() as we do not know the direction
798 	 * just yet. Once the direction is known in the subsequent IOCTL to
799 	 * attach slicing, we can do it then.
800 	 */
801 
802 	obj->funcs = &qaic_gem_funcs;
803 	obj->import_attach = attach;
804 	obj->resv = dma_buf->resv;
805 
806 	return obj;
807 
808 size_align_fail:
809 	dma_buf_detach(dma_buf, attach);
810 attach_fail:
811 	dma_buf_put(dma_buf);
812 	kfree(bo);
813 out:
814 	return ERR_PTR(ret);
815 }
816 
817 static int qaic_prepare_import_bo(struct qaic_bo *bo, struct qaic_attach_slice_hdr *hdr)
818 {
819 	struct drm_gem_object *obj = &bo->base;
820 	struct sg_table *sgt;
821 	int ret;
822 
823 	if (obj->import_attach->dmabuf->size < hdr->size)
824 		return -EINVAL;
825 
826 	sgt = dma_buf_map_attachment(obj->import_attach, hdr->dir);
827 	if (IS_ERR(sgt)) {
828 		ret = PTR_ERR(sgt);
829 		return ret;
830 	}
831 
832 	bo->sgt = sgt;
833 	bo->size = hdr->size;
834 
835 	return 0;
836 }
837 
838 static int qaic_prepare_export_bo(struct qaic_device *qdev, struct qaic_bo *bo,
839 				  struct qaic_attach_slice_hdr *hdr)
840 {
841 	int ret;
842 
843 	if (bo->size != hdr->size)
844 		return -EINVAL;
845 
846 	ret = dma_map_sgtable(&qdev->pdev->dev, bo->sgt, hdr->dir, 0);
847 	if (ret)
848 		return -EFAULT;
849 
850 	return 0;
851 }
852 
853 static int qaic_prepare_bo(struct qaic_device *qdev, struct qaic_bo *bo,
854 			   struct qaic_attach_slice_hdr *hdr)
855 {
856 	int ret;
857 
858 	if (bo->base.import_attach)
859 		ret = qaic_prepare_import_bo(bo, hdr);
860 	else
861 		ret = qaic_prepare_export_bo(qdev, bo, hdr);
862 
863 	if (ret == 0)
864 		bo->dir = hdr->dir;
865 
866 	return ret;
867 }
868 
869 static void qaic_unprepare_import_bo(struct qaic_bo *bo)
870 {
871 	dma_buf_unmap_attachment(bo->base.import_attach, bo->sgt, bo->dir);
872 	bo->sgt = NULL;
873 	bo->size = 0;
874 }
875 
876 static void qaic_unprepare_export_bo(struct qaic_device *qdev, struct qaic_bo *bo)
877 {
878 	dma_unmap_sgtable(&qdev->pdev->dev, bo->sgt, bo->dir, 0);
879 }
880 
881 static void qaic_unprepare_bo(struct qaic_device *qdev, struct qaic_bo *bo)
882 {
883 	if (bo->base.import_attach)
884 		qaic_unprepare_import_bo(bo);
885 	else
886 		qaic_unprepare_export_bo(qdev, bo);
887 
888 	bo->dir = 0;
889 }
890 
891 static void qaic_free_slices_bo(struct qaic_bo *bo)
892 {
893 	struct bo_slice *slice, *temp;
894 
895 	list_for_each_entry_safe(slice, temp, &bo->slices, slice)
896 		kref_put(&slice->ref_count, free_slice);
897 }
898 
899 static int qaic_attach_slicing_bo(struct qaic_device *qdev, struct qaic_bo *bo,
900 				  struct qaic_attach_slice_hdr *hdr,
901 				  struct qaic_attach_slice_entry *slice_ent)
902 {
903 	int ret, i;
904 
905 	for (i = 0; i < hdr->count; i++) {
906 		ret = qaic_map_one_slice(qdev, bo, &slice_ent[i]);
907 		if (ret) {
908 			qaic_free_slices_bo(bo);
909 			return ret;
910 		}
911 	}
912 
913 	if (bo->total_slice_nents > qdev->dbc[hdr->dbc_id].nelem) {
914 		qaic_free_slices_bo(bo);
915 		return -ENOSPC;
916 	}
917 
918 	bo->sliced = true;
919 	bo->nr_slice = hdr->count;
920 	list_add_tail(&bo->bo_list, &qdev->dbc[hdr->dbc_id].bo_lists);
921 
922 	return 0;
923 }
924 
925 int qaic_attach_slice_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
926 {
927 	struct qaic_attach_slice_entry *slice_ent;
928 	struct qaic_attach_slice *args = data;
929 	struct dma_bridge_chan	*dbc;
930 	int usr_rcu_id, qdev_rcu_id;
931 	struct drm_gem_object *obj;
932 	struct qaic_device *qdev;
933 	unsigned long arg_size;
934 	struct qaic_user *usr;
935 	u8 __user *user_data;
936 	struct qaic_bo *bo;
937 	int ret;
938 
939 	usr = file_priv->driver_priv;
940 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
941 	if (!usr->qddev) {
942 		ret = -ENODEV;
943 		goto unlock_usr_srcu;
944 	}
945 
946 	qdev = usr->qddev->qdev;
947 	qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
948 	if (qdev->in_reset) {
949 		ret = -ENODEV;
950 		goto unlock_dev_srcu;
951 	}
952 
953 	if (args->hdr.count == 0) {
954 		ret = -EINVAL;
955 		goto unlock_dev_srcu;
956 	}
957 
958 	arg_size = args->hdr.count * sizeof(*slice_ent);
959 	if (arg_size / args->hdr.count != sizeof(*slice_ent)) {
960 		ret = -EINVAL;
961 		goto unlock_dev_srcu;
962 	}
963 
964 	if (args->hdr.dbc_id >= qdev->num_dbc) {
965 		ret = -EINVAL;
966 		goto unlock_dev_srcu;
967 	}
968 
969 	if (args->hdr.size == 0) {
970 		ret = -EINVAL;
971 		goto unlock_dev_srcu;
972 	}
973 
974 	if (!(args->hdr.dir == DMA_TO_DEVICE  || args->hdr.dir == DMA_FROM_DEVICE)) {
975 		ret = -EINVAL;
976 		goto unlock_dev_srcu;
977 	}
978 
979 	dbc = &qdev->dbc[args->hdr.dbc_id];
980 	if (dbc->usr != usr) {
981 		ret = -EINVAL;
982 		goto unlock_dev_srcu;
983 	}
984 
985 	if (args->data == 0) {
986 		ret = -EINVAL;
987 		goto unlock_dev_srcu;
988 	}
989 
990 	user_data = u64_to_user_ptr(args->data);
991 
992 	slice_ent = kzalloc(arg_size, GFP_KERNEL);
993 	if (!slice_ent) {
994 		ret = -EINVAL;
995 		goto unlock_dev_srcu;
996 	}
997 
998 	ret = copy_from_user(slice_ent, user_data, arg_size);
999 	if (ret) {
1000 		ret = -EFAULT;
1001 		goto free_slice_ent;
1002 	}
1003 
1004 	ret = qaic_validate_req(qdev, slice_ent, args->hdr.count, args->hdr.size);
1005 	if (ret)
1006 		goto free_slice_ent;
1007 
1008 	obj = drm_gem_object_lookup(file_priv, args->hdr.handle);
1009 	if (!obj) {
1010 		ret = -ENOENT;
1011 		goto free_slice_ent;
1012 	}
1013 
1014 	bo = to_qaic_bo(obj);
1015 
1016 	ret = qaic_prepare_bo(qdev, bo, &args->hdr);
1017 	if (ret)
1018 		goto put_bo;
1019 
1020 	ret = qaic_attach_slicing_bo(qdev, bo, &args->hdr, slice_ent);
1021 	if (ret)
1022 		goto unprepare_bo;
1023 
1024 	if (args->hdr.dir == DMA_TO_DEVICE)
1025 		dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, args->hdr.dir);
1026 
1027 	bo->dbc = dbc;
1028 	drm_gem_object_put(obj);
1029 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1030 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1031 
1032 	return 0;
1033 
1034 unprepare_bo:
1035 	qaic_unprepare_bo(qdev, bo);
1036 put_bo:
1037 	drm_gem_object_put(obj);
1038 free_slice_ent:
1039 	kfree(slice_ent);
1040 unlock_dev_srcu:
1041 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1042 unlock_usr_srcu:
1043 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1044 	return ret;
1045 }
1046 
1047 static inline int copy_exec_reqs(struct qaic_device *qdev, struct bo_slice *slice, u32 dbc_id,
1048 				 u32 head, u32 *ptail)
1049 {
1050 	struct dma_bridge_chan *dbc = &qdev->dbc[dbc_id];
1051 	struct dbc_req *reqs = slice->reqs;
1052 	u32 tail = *ptail;
1053 	u32 avail;
1054 
1055 	avail = head - tail;
1056 	if (head <= tail)
1057 		avail += dbc->nelem;
1058 
1059 	--avail;
1060 
1061 	if (avail < slice->nents)
1062 		return -EAGAIN;
1063 
1064 	if (tail + slice->nents > dbc->nelem) {
1065 		avail = dbc->nelem - tail;
1066 		avail = min_t(u32, avail, slice->nents);
1067 		memcpy(dbc->req_q_base + tail * get_dbc_req_elem_size(), reqs,
1068 		       sizeof(*reqs) * avail);
1069 		reqs += avail;
1070 		avail = slice->nents - avail;
1071 		if (avail)
1072 			memcpy(dbc->req_q_base, reqs, sizeof(*reqs) * avail);
1073 	} else {
1074 		memcpy(dbc->req_q_base + tail * get_dbc_req_elem_size(), reqs,
1075 		       sizeof(*reqs) * slice->nents);
1076 	}
1077 
1078 	*ptail = (tail + slice->nents) % dbc->nelem;
1079 
1080 	return 0;
1081 }
1082 
1083 /*
1084  * Based on the value of resize we may only need to transmit first_n
1085  * entries and the last entry, with last_bytes to send from the last entry.
1086  * Note that first_n could be 0.
1087  */
1088 static inline int copy_partial_exec_reqs(struct qaic_device *qdev, struct bo_slice *slice,
1089 					 u64 resize, u32 dbc_id, u32 head, u32 *ptail)
1090 {
1091 	struct dma_bridge_chan *dbc = &qdev->dbc[dbc_id];
1092 	struct dbc_req *reqs = slice->reqs;
1093 	struct dbc_req *last_req;
1094 	u32 tail = *ptail;
1095 	u64 total_bytes;
1096 	u64 last_bytes;
1097 	u32 first_n;
1098 	u32 avail;
1099 	int ret;
1100 	int i;
1101 
1102 	avail = head - tail;
1103 	if (head <= tail)
1104 		avail += dbc->nelem;
1105 
1106 	--avail;
1107 
1108 	total_bytes = 0;
1109 	for (i = 0; i < slice->nents; i++) {
1110 		total_bytes += le32_to_cpu(reqs[i].len);
1111 		if (total_bytes >= resize)
1112 			break;
1113 	}
1114 
1115 	if (total_bytes < resize) {
1116 		/* User space should have used the full buffer path. */
1117 		ret = -EINVAL;
1118 		return ret;
1119 	}
1120 
1121 	first_n = i;
1122 	last_bytes = i ? resize + le32_to_cpu(reqs[i].len) - total_bytes : resize;
1123 
1124 	if (avail < (first_n + 1))
1125 		return -EAGAIN;
1126 
1127 	if (first_n) {
1128 		if (tail + first_n > dbc->nelem) {
1129 			avail = dbc->nelem - tail;
1130 			avail = min_t(u32, avail, first_n);
1131 			memcpy(dbc->req_q_base + tail * get_dbc_req_elem_size(), reqs,
1132 			       sizeof(*reqs) * avail);
1133 			last_req = reqs + avail;
1134 			avail = first_n - avail;
1135 			if (avail)
1136 				memcpy(dbc->req_q_base, last_req, sizeof(*reqs) * avail);
1137 		} else {
1138 			memcpy(dbc->req_q_base + tail * get_dbc_req_elem_size(), reqs,
1139 			       sizeof(*reqs) * first_n);
1140 		}
1141 	}
1142 
1143 	/* Copy over the last entry. Here we need to adjust len to the left over
1144 	 * size, and set src and dst to the entry it is copied to.
1145 	 */
1146 	last_req = dbc->req_q_base + (tail + first_n) % dbc->nelem * get_dbc_req_elem_size();
1147 	memcpy(last_req, reqs + slice->nents - 1, sizeof(*reqs));
1148 
1149 	/*
1150 	 * last_bytes holds size of a DMA segment, maximum DMA segment size is
1151 	 * set to UINT_MAX by qaic and hence last_bytes can never exceed u32
1152 	 * range. So, by down sizing we are not corrupting the value.
1153 	 */
1154 	last_req->len = cpu_to_le32((u32)last_bytes);
1155 	last_req->src_addr = reqs[first_n].src_addr;
1156 	last_req->dest_addr = reqs[first_n].dest_addr;
1157 
1158 	*ptail = (tail + first_n + 1) % dbc->nelem;
1159 
1160 	return 0;
1161 }
1162 
1163 static int send_bo_list_to_device(struct qaic_device *qdev, struct drm_file *file_priv,
1164 				  struct qaic_execute_entry *exec, unsigned int count,
1165 				  bool is_partial, struct dma_bridge_chan *dbc, u32 head,
1166 				  u32 *tail)
1167 {
1168 	struct qaic_partial_execute_entry *pexec = (struct qaic_partial_execute_entry *)exec;
1169 	struct drm_gem_object *obj;
1170 	struct bo_slice *slice;
1171 	unsigned long flags;
1172 	struct qaic_bo *bo;
1173 	bool queued;
1174 	int i, j;
1175 	int ret;
1176 
1177 	for (i = 0; i < count; i++) {
1178 		/*
1179 		 * ref count will be decremented when the transfer of this
1180 		 * buffer is complete. It is inside dbc_irq_threaded_fn().
1181 		 */
1182 		obj = drm_gem_object_lookup(file_priv,
1183 					    is_partial ? pexec[i].handle : exec[i].handle);
1184 		if (!obj) {
1185 			ret = -ENOENT;
1186 			goto failed_to_send_bo;
1187 		}
1188 
1189 		bo = to_qaic_bo(obj);
1190 
1191 		if (!bo->sliced) {
1192 			ret = -EINVAL;
1193 			goto failed_to_send_bo;
1194 		}
1195 
1196 		if (is_partial && pexec[i].resize > bo->size) {
1197 			ret = -EINVAL;
1198 			goto failed_to_send_bo;
1199 		}
1200 
1201 		spin_lock_irqsave(&dbc->xfer_lock, flags);
1202 		queued = bo->queued;
1203 		bo->queued = true;
1204 		if (queued) {
1205 			spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1206 			ret = -EINVAL;
1207 			goto failed_to_send_bo;
1208 		}
1209 
1210 		bo->req_id = dbc->next_req_id++;
1211 
1212 		list_for_each_entry(slice, &bo->slices, slice) {
1213 			/*
1214 			 * If this slice does not fall under the given
1215 			 * resize then skip this slice and continue the loop
1216 			 */
1217 			if (is_partial && pexec[i].resize && pexec[i].resize <= slice->offset)
1218 				continue;
1219 
1220 			for (j = 0; j < slice->nents; j++)
1221 				slice->reqs[j].req_id = cpu_to_le16(bo->req_id);
1222 
1223 			/*
1224 			 * If it is a partial execute ioctl call then check if
1225 			 * resize has cut this slice short then do a partial copy
1226 			 * else do complete copy
1227 			 */
1228 			if (is_partial && pexec[i].resize &&
1229 			    pexec[i].resize < slice->offset + slice->size)
1230 				ret = copy_partial_exec_reqs(qdev, slice,
1231 							     pexec[i].resize - slice->offset,
1232 							     dbc->id, head, tail);
1233 			else
1234 				ret = copy_exec_reqs(qdev, slice, dbc->id, head, tail);
1235 			if (ret) {
1236 				bo->queued = false;
1237 				spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1238 				goto failed_to_send_bo;
1239 			}
1240 		}
1241 		reinit_completion(&bo->xfer_done);
1242 		list_add_tail(&bo->xfer_list, &dbc->xfer_list);
1243 		spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1244 		dma_sync_sgtable_for_device(&qdev->pdev->dev, bo->sgt, bo->dir);
1245 	}
1246 
1247 	return 0;
1248 
1249 failed_to_send_bo:
1250 	if (likely(obj))
1251 		drm_gem_object_put(obj);
1252 	for (j = 0; j < i; j++) {
1253 		spin_lock_irqsave(&dbc->xfer_lock, flags);
1254 		bo = list_last_entry(&dbc->xfer_list, struct qaic_bo, xfer_list);
1255 		obj = &bo->base;
1256 		bo->queued = false;
1257 		list_del(&bo->xfer_list);
1258 		spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1259 		dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, bo->dir);
1260 		drm_gem_object_put(obj);
1261 	}
1262 	return ret;
1263 }
1264 
1265 static void update_profiling_data(struct drm_file *file_priv,
1266 				  struct qaic_execute_entry *exec, unsigned int count,
1267 				  bool is_partial, u64 received_ts, u64 submit_ts, u32 queue_level)
1268 {
1269 	struct qaic_partial_execute_entry *pexec = (struct qaic_partial_execute_entry *)exec;
1270 	struct drm_gem_object *obj;
1271 	struct qaic_bo *bo;
1272 	int i;
1273 
1274 	for (i = 0; i < count; i++) {
1275 		/*
1276 		 * Since we already committed the BO to hardware, the only way
1277 		 * this should fail is a pending signal. We can't cancel the
1278 		 * submit to hardware, so we have to just skip the profiling
1279 		 * data. In case the signal is not fatal to the process, we
1280 		 * return success so that the user doesn't try to resubmit.
1281 		 */
1282 		obj = drm_gem_object_lookup(file_priv,
1283 					    is_partial ? pexec[i].handle : exec[i].handle);
1284 		if (!obj)
1285 			break;
1286 		bo = to_qaic_bo(obj);
1287 		bo->perf_stats.req_received_ts = received_ts;
1288 		bo->perf_stats.req_submit_ts = submit_ts;
1289 		bo->perf_stats.queue_level_before = queue_level;
1290 		queue_level += bo->total_slice_nents;
1291 		drm_gem_object_put(obj);
1292 	}
1293 }
1294 
1295 static int __qaic_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv,
1296 				   bool is_partial)
1297 {
1298 	struct qaic_partial_execute_entry *pexec;
1299 	struct qaic_execute *args = data;
1300 	struct qaic_execute_entry *exec;
1301 	struct dma_bridge_chan *dbc;
1302 	int usr_rcu_id, qdev_rcu_id;
1303 	struct qaic_device *qdev;
1304 	struct qaic_user *usr;
1305 	u8 __user *user_data;
1306 	unsigned long n;
1307 	u64 received_ts;
1308 	u32 queue_level;
1309 	u64 submit_ts;
1310 	int rcu_id;
1311 	u32 head;
1312 	u32 tail;
1313 	u64 size;
1314 	int ret;
1315 
1316 	received_ts = ktime_get_ns();
1317 
1318 	size = is_partial ? sizeof(*pexec) : sizeof(*exec);
1319 
1320 	n = (unsigned long)size * args->hdr.count;
1321 	if (args->hdr.count == 0 || n / args->hdr.count != size)
1322 		return -EINVAL;
1323 
1324 	user_data = u64_to_user_ptr(args->data);
1325 
1326 	exec = kcalloc(args->hdr.count, size, GFP_KERNEL);
1327 	pexec = (struct qaic_partial_execute_entry *)exec;
1328 	if (!exec)
1329 		return -ENOMEM;
1330 
1331 	if (copy_from_user(exec, user_data, n)) {
1332 		ret = -EFAULT;
1333 		goto free_exec;
1334 	}
1335 
1336 	usr = file_priv->driver_priv;
1337 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
1338 	if (!usr->qddev) {
1339 		ret = -ENODEV;
1340 		goto unlock_usr_srcu;
1341 	}
1342 
1343 	qdev = usr->qddev->qdev;
1344 	qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
1345 	if (qdev->in_reset) {
1346 		ret = -ENODEV;
1347 		goto unlock_dev_srcu;
1348 	}
1349 
1350 	if (args->hdr.dbc_id >= qdev->num_dbc) {
1351 		ret = -EINVAL;
1352 		goto unlock_dev_srcu;
1353 	}
1354 
1355 	dbc = &qdev->dbc[args->hdr.dbc_id];
1356 
1357 	rcu_id = srcu_read_lock(&dbc->ch_lock);
1358 	if (!dbc->usr || dbc->usr->handle != usr->handle) {
1359 		ret = -EPERM;
1360 		goto release_ch_rcu;
1361 	}
1362 
1363 	head = readl(dbc->dbc_base + REQHP_OFF);
1364 	tail = readl(dbc->dbc_base + REQTP_OFF);
1365 
1366 	if (head == U32_MAX || tail == U32_MAX) {
1367 		/* PCI link error */
1368 		ret = -ENODEV;
1369 		goto release_ch_rcu;
1370 	}
1371 
1372 	queue_level = head <= tail ? tail - head : dbc->nelem - (head - tail);
1373 
1374 	ret = send_bo_list_to_device(qdev, file_priv, exec, args->hdr.count, is_partial, dbc,
1375 				     head, &tail);
1376 	if (ret)
1377 		goto release_ch_rcu;
1378 
1379 	/* Finalize commit to hardware */
1380 	submit_ts = ktime_get_ns();
1381 	writel(tail, dbc->dbc_base + REQTP_OFF);
1382 
1383 	update_profiling_data(file_priv, exec, args->hdr.count, is_partial, received_ts,
1384 			      submit_ts, queue_level);
1385 
1386 	if (datapath_polling)
1387 		schedule_work(&dbc->poll_work);
1388 
1389 release_ch_rcu:
1390 	srcu_read_unlock(&dbc->ch_lock, rcu_id);
1391 unlock_dev_srcu:
1392 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1393 unlock_usr_srcu:
1394 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1395 free_exec:
1396 	kfree(exec);
1397 	return ret;
1398 }
1399 
1400 int qaic_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1401 {
1402 	return __qaic_execute_bo_ioctl(dev, data, file_priv, false);
1403 }
1404 
1405 int qaic_partial_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1406 {
1407 	return __qaic_execute_bo_ioctl(dev, data, file_priv, true);
1408 }
1409 
1410 /*
1411  * Our interrupt handling is a bit more complicated than a simple ideal, but
1412  * sadly necessary.
1413  *
1414  * Each dbc has a completion queue. Entries in the queue correspond to DMA
1415  * requests which the device has processed. The hardware already has a built
1416  * in irq mitigation. When the device puts an entry into the queue, it will
1417  * only trigger an interrupt if the queue was empty. Therefore, when adding
1418  * the Nth event to a non-empty queue, the hardware doesn't trigger an
1419  * interrupt. This means the host doesn't get additional interrupts signaling
1420  * the same thing - the queue has something to process.
1421  * This behavior can be overridden in the DMA request.
1422  * This means that when the host receives an interrupt, it is required to
1423  * drain the queue.
1424  *
1425  * This behavior is what NAPI attempts to accomplish, although we can't use
1426  * NAPI as we don't have a netdev. We use threaded irqs instead.
1427  *
1428  * However, there is a situation where the host drains the queue fast enough
1429  * that every event causes an interrupt. Typically this is not a problem as
1430  * the rate of events would be low. However, that is not the case with
1431  * lprnet for example. On an Intel Xeon D-2191 where we run 8 instances of
1432  * lprnet, the host receives roughly 80k interrupts per second from the device
1433  * (per /proc/interrupts). While NAPI documentation indicates the host should
1434  * just chug along, sadly that behavior causes instability in some hosts.
1435  *
1436  * Therefore, we implement an interrupt disable scheme similar to NAPI. The
1437  * key difference is that we will delay after draining the queue for a small
1438  * time to allow additional events to come in via polling. Using the above
1439  * lprnet workload, this reduces the number of interrupts processed from
1440  * ~80k/sec to about 64 in 5 minutes and appears to solve the system
1441  * instability.
1442  */
1443 irqreturn_t dbc_irq_handler(int irq, void *data)
1444 {
1445 	struct dma_bridge_chan *dbc = data;
1446 	int rcu_id;
1447 	u32 head;
1448 	u32 tail;
1449 
1450 	rcu_id = srcu_read_lock(&dbc->ch_lock);
1451 
1452 	if (!dbc->usr) {
1453 		srcu_read_unlock(&dbc->ch_lock, rcu_id);
1454 		return IRQ_HANDLED;
1455 	}
1456 
1457 	head = readl(dbc->dbc_base + RSPHP_OFF);
1458 	if (head == U32_MAX) { /* PCI link error */
1459 		srcu_read_unlock(&dbc->ch_lock, rcu_id);
1460 		return IRQ_NONE;
1461 	}
1462 
1463 	tail = readl(dbc->dbc_base + RSPTP_OFF);
1464 	if (tail == U32_MAX) { /* PCI link error */
1465 		srcu_read_unlock(&dbc->ch_lock, rcu_id);
1466 		return IRQ_NONE;
1467 	}
1468 
1469 	if (head == tail) { /* queue empty */
1470 		srcu_read_unlock(&dbc->ch_lock, rcu_id);
1471 		return IRQ_NONE;
1472 	}
1473 
1474 	disable_irq_nosync(irq);
1475 	srcu_read_unlock(&dbc->ch_lock, rcu_id);
1476 	return IRQ_WAKE_THREAD;
1477 }
1478 
1479 void irq_polling_work(struct work_struct *work)
1480 {
1481 	struct dma_bridge_chan *dbc = container_of(work, struct dma_bridge_chan,  poll_work);
1482 	unsigned long flags;
1483 	int rcu_id;
1484 	u32 head;
1485 	u32 tail;
1486 
1487 	rcu_id = srcu_read_lock(&dbc->ch_lock);
1488 
1489 	while (1) {
1490 		if (dbc->qdev->in_reset) {
1491 			srcu_read_unlock(&dbc->ch_lock, rcu_id);
1492 			return;
1493 		}
1494 		if (!dbc->usr) {
1495 			srcu_read_unlock(&dbc->ch_lock, rcu_id);
1496 			return;
1497 		}
1498 		spin_lock_irqsave(&dbc->xfer_lock, flags);
1499 		if (list_empty(&dbc->xfer_list)) {
1500 			spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1501 			srcu_read_unlock(&dbc->ch_lock, rcu_id);
1502 			return;
1503 		}
1504 		spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1505 
1506 		head = readl(dbc->dbc_base + RSPHP_OFF);
1507 		if (head == U32_MAX) { /* PCI link error */
1508 			srcu_read_unlock(&dbc->ch_lock, rcu_id);
1509 			return;
1510 		}
1511 
1512 		tail = readl(dbc->dbc_base + RSPTP_OFF);
1513 		if (tail == U32_MAX) { /* PCI link error */
1514 			srcu_read_unlock(&dbc->ch_lock, rcu_id);
1515 			return;
1516 		}
1517 
1518 		if (head != tail) {
1519 			irq_wake_thread(dbc->irq, dbc);
1520 			srcu_read_unlock(&dbc->ch_lock, rcu_id);
1521 			return;
1522 		}
1523 
1524 		cond_resched();
1525 		usleep_range(datapath_poll_interval_us, 2 * datapath_poll_interval_us);
1526 	}
1527 }
1528 
1529 irqreturn_t dbc_irq_threaded_fn(int irq, void *data)
1530 {
1531 	struct dma_bridge_chan *dbc = data;
1532 	int event_count = NUM_EVENTS;
1533 	int delay_count = NUM_DELAYS;
1534 	struct qaic_device *qdev;
1535 	struct qaic_bo *bo, *i;
1536 	struct dbc_rsp *rsp;
1537 	unsigned long flags;
1538 	int rcu_id;
1539 	u16 status;
1540 	u16 req_id;
1541 	u32 head;
1542 	u32 tail;
1543 
1544 	rcu_id = srcu_read_lock(&dbc->ch_lock);
1545 
1546 	head = readl(dbc->dbc_base + RSPHP_OFF);
1547 	if (head == U32_MAX) /* PCI link error */
1548 		goto error_out;
1549 
1550 	qdev = dbc->qdev;
1551 read_fifo:
1552 
1553 	if (!event_count) {
1554 		event_count = NUM_EVENTS;
1555 		cond_resched();
1556 	}
1557 
1558 	/*
1559 	 * if this channel isn't assigned or gets unassigned during processing
1560 	 * we have nothing further to do
1561 	 */
1562 	if (!dbc->usr)
1563 		goto error_out;
1564 
1565 	tail = readl(dbc->dbc_base + RSPTP_OFF);
1566 	if (tail == U32_MAX) /* PCI link error */
1567 		goto error_out;
1568 
1569 	if (head == tail) { /* queue empty */
1570 		if (delay_count) {
1571 			--delay_count;
1572 			usleep_range(100, 200);
1573 			goto read_fifo; /* check for a new event */
1574 		}
1575 		goto normal_out;
1576 	}
1577 
1578 	delay_count = NUM_DELAYS;
1579 	while (head != tail) {
1580 		if (!event_count)
1581 			break;
1582 		--event_count;
1583 		rsp = dbc->rsp_q_base + head * sizeof(*rsp);
1584 		req_id = le16_to_cpu(rsp->req_id);
1585 		status = le16_to_cpu(rsp->status);
1586 		if (status)
1587 			pci_dbg(qdev->pdev, "req_id %d failed with status %d\n", req_id, status);
1588 		spin_lock_irqsave(&dbc->xfer_lock, flags);
1589 		/*
1590 		 * A BO can receive multiple interrupts, since a BO can be
1591 		 * divided into multiple slices and a buffer receives as many
1592 		 * interrupts as slices. So until it receives interrupts for
1593 		 * all the slices we cannot mark that buffer complete.
1594 		 */
1595 		list_for_each_entry_safe(bo, i, &dbc->xfer_list, xfer_list) {
1596 			if (bo->req_id == req_id)
1597 				bo->nr_slice_xfer_done++;
1598 			else
1599 				continue;
1600 
1601 			if (bo->nr_slice_xfer_done < bo->nr_slice)
1602 				break;
1603 
1604 			/*
1605 			 * At this point we have received all the interrupts for
1606 			 * BO, which means BO execution is complete.
1607 			 */
1608 			dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, bo->dir);
1609 			bo->nr_slice_xfer_done = 0;
1610 			bo->queued = false;
1611 			list_del(&bo->xfer_list);
1612 			bo->perf_stats.req_processed_ts = ktime_get_ns();
1613 			complete_all(&bo->xfer_done);
1614 			drm_gem_object_put(&bo->base);
1615 			break;
1616 		}
1617 		spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1618 		head = (head + 1) % dbc->nelem;
1619 	}
1620 
1621 	/*
1622 	 * Update the head pointer of response queue and let the device know
1623 	 * that we have consumed elements from the queue.
1624 	 */
1625 	writel(head, dbc->dbc_base + RSPHP_OFF);
1626 
1627 	/* elements might have been put in the queue while we were processing */
1628 	goto read_fifo;
1629 
1630 normal_out:
1631 	if (likely(!datapath_polling))
1632 		enable_irq(irq);
1633 	else
1634 		schedule_work(&dbc->poll_work);
1635 	/* checking the fifo and enabling irqs is a race, missed event check */
1636 	tail = readl(dbc->dbc_base + RSPTP_OFF);
1637 	if (tail != U32_MAX && head != tail) {
1638 		if (likely(!datapath_polling))
1639 			disable_irq_nosync(irq);
1640 		goto read_fifo;
1641 	}
1642 	srcu_read_unlock(&dbc->ch_lock, rcu_id);
1643 	return IRQ_HANDLED;
1644 
1645 error_out:
1646 	srcu_read_unlock(&dbc->ch_lock, rcu_id);
1647 	if (likely(!datapath_polling))
1648 		enable_irq(irq);
1649 	else
1650 		schedule_work(&dbc->poll_work);
1651 
1652 	return IRQ_HANDLED;
1653 }
1654 
1655 int qaic_wait_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1656 {
1657 	struct qaic_wait *args = data;
1658 	int usr_rcu_id, qdev_rcu_id;
1659 	struct dma_bridge_chan *dbc;
1660 	struct drm_gem_object *obj;
1661 	struct qaic_device *qdev;
1662 	unsigned long timeout;
1663 	struct qaic_user *usr;
1664 	struct qaic_bo *bo;
1665 	int rcu_id;
1666 	int ret;
1667 
1668 	usr = file_priv->driver_priv;
1669 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
1670 	if (!usr->qddev) {
1671 		ret = -ENODEV;
1672 		goto unlock_usr_srcu;
1673 	}
1674 
1675 	qdev = usr->qddev->qdev;
1676 	qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
1677 	if (qdev->in_reset) {
1678 		ret = -ENODEV;
1679 		goto unlock_dev_srcu;
1680 	}
1681 
1682 	if (args->pad != 0) {
1683 		ret = -EINVAL;
1684 		goto unlock_dev_srcu;
1685 	}
1686 
1687 	if (args->dbc_id >= qdev->num_dbc) {
1688 		ret = -EINVAL;
1689 		goto unlock_dev_srcu;
1690 	}
1691 
1692 	dbc = &qdev->dbc[args->dbc_id];
1693 
1694 	rcu_id = srcu_read_lock(&dbc->ch_lock);
1695 	if (dbc->usr != usr) {
1696 		ret = -EPERM;
1697 		goto unlock_ch_srcu;
1698 	}
1699 
1700 	obj = drm_gem_object_lookup(file_priv, args->handle);
1701 	if (!obj) {
1702 		ret = -ENOENT;
1703 		goto unlock_ch_srcu;
1704 	}
1705 
1706 	bo = to_qaic_bo(obj);
1707 	timeout = args->timeout ? args->timeout : wait_exec_default_timeout_ms;
1708 	timeout = msecs_to_jiffies(timeout);
1709 	ret = wait_for_completion_interruptible_timeout(&bo->xfer_done, timeout);
1710 	if (!ret) {
1711 		ret = -ETIMEDOUT;
1712 		goto put_obj;
1713 	}
1714 	if (ret > 0)
1715 		ret = 0;
1716 
1717 	if (!dbc->usr)
1718 		ret = -EPERM;
1719 
1720 put_obj:
1721 	drm_gem_object_put(obj);
1722 unlock_ch_srcu:
1723 	srcu_read_unlock(&dbc->ch_lock, rcu_id);
1724 unlock_dev_srcu:
1725 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1726 unlock_usr_srcu:
1727 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1728 	return ret;
1729 }
1730 
1731 int qaic_perf_stats_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
1732 {
1733 	struct qaic_perf_stats_entry *ent = NULL;
1734 	struct qaic_perf_stats *args = data;
1735 	int usr_rcu_id, qdev_rcu_id;
1736 	struct drm_gem_object *obj;
1737 	struct qaic_device *qdev;
1738 	struct qaic_user *usr;
1739 	struct qaic_bo *bo;
1740 	int ret, i;
1741 
1742 	usr = file_priv->driver_priv;
1743 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
1744 	if (!usr->qddev) {
1745 		ret = -ENODEV;
1746 		goto unlock_usr_srcu;
1747 	}
1748 
1749 	qdev = usr->qddev->qdev;
1750 	qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
1751 	if (qdev->in_reset) {
1752 		ret = -ENODEV;
1753 		goto unlock_dev_srcu;
1754 	}
1755 
1756 	if (args->hdr.dbc_id >= qdev->num_dbc) {
1757 		ret = -EINVAL;
1758 		goto unlock_dev_srcu;
1759 	}
1760 
1761 	ent = kcalloc(args->hdr.count, sizeof(*ent), GFP_KERNEL);
1762 	if (!ent) {
1763 		ret = -EINVAL;
1764 		goto unlock_dev_srcu;
1765 	}
1766 
1767 	ret = copy_from_user(ent, u64_to_user_ptr(args->data), args->hdr.count * sizeof(*ent));
1768 	if (ret) {
1769 		ret = -EFAULT;
1770 		goto free_ent;
1771 	}
1772 
1773 	for (i = 0; i < args->hdr.count; i++) {
1774 		obj = drm_gem_object_lookup(file_priv, ent[i].handle);
1775 		if (!obj) {
1776 			ret = -ENOENT;
1777 			goto free_ent;
1778 		}
1779 		bo = to_qaic_bo(obj);
1780 		/*
1781 		 * perf stats ioctl is called before wait ioctl is complete then
1782 		 * the latency information is invalid.
1783 		 */
1784 		if (bo->perf_stats.req_processed_ts < bo->perf_stats.req_submit_ts) {
1785 			ent[i].device_latency_us = 0;
1786 		} else {
1787 			ent[i].device_latency_us = div_u64((bo->perf_stats.req_processed_ts -
1788 							    bo->perf_stats.req_submit_ts), 1000);
1789 		}
1790 		ent[i].submit_latency_us = div_u64((bo->perf_stats.req_submit_ts -
1791 						    bo->perf_stats.req_received_ts), 1000);
1792 		ent[i].queue_level_before = bo->perf_stats.queue_level_before;
1793 		ent[i].num_queue_element = bo->total_slice_nents;
1794 		drm_gem_object_put(obj);
1795 	}
1796 
1797 	if (copy_to_user(u64_to_user_ptr(args->data), ent, args->hdr.count * sizeof(*ent)))
1798 		ret = -EFAULT;
1799 
1800 free_ent:
1801 	kfree(ent);
1802 unlock_dev_srcu:
1803 	srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
1804 unlock_usr_srcu:
1805 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
1806 	return ret;
1807 }
1808 
1809 static void empty_xfer_list(struct qaic_device *qdev, struct dma_bridge_chan *dbc)
1810 {
1811 	unsigned long flags;
1812 	struct qaic_bo *bo;
1813 
1814 	spin_lock_irqsave(&dbc->xfer_lock, flags);
1815 	while (!list_empty(&dbc->xfer_list)) {
1816 		bo = list_first_entry(&dbc->xfer_list, typeof(*bo), xfer_list);
1817 		bo->queued = false;
1818 		list_del(&bo->xfer_list);
1819 		spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1820 		dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, bo->dir);
1821 		complete_all(&bo->xfer_done);
1822 		drm_gem_object_put(&bo->base);
1823 		spin_lock_irqsave(&dbc->xfer_lock, flags);
1824 	}
1825 	spin_unlock_irqrestore(&dbc->xfer_lock, flags);
1826 }
1827 
1828 int disable_dbc(struct qaic_device *qdev, u32 dbc_id, struct qaic_user *usr)
1829 {
1830 	if (!qdev->dbc[dbc_id].usr || qdev->dbc[dbc_id].usr->handle != usr->handle)
1831 		return -EPERM;
1832 
1833 	qdev->dbc[dbc_id].usr = NULL;
1834 	synchronize_srcu(&qdev->dbc[dbc_id].ch_lock);
1835 	return 0;
1836 }
1837 
1838 /**
1839  * enable_dbc - Enable the DBC. DBCs are disabled by removing the context of
1840  * user. Add user context back to DBC to enable it. This function trusts the
1841  * DBC ID passed and expects the DBC to be disabled.
1842  * @qdev: Qranium device handle
1843  * @dbc_id: ID of the DBC
1844  * @usr: User context
1845  */
1846 void enable_dbc(struct qaic_device *qdev, u32 dbc_id, struct qaic_user *usr)
1847 {
1848 	qdev->dbc[dbc_id].usr = usr;
1849 }
1850 
1851 void wakeup_dbc(struct qaic_device *qdev, u32 dbc_id)
1852 {
1853 	struct dma_bridge_chan *dbc = &qdev->dbc[dbc_id];
1854 
1855 	dbc->usr = NULL;
1856 	empty_xfer_list(qdev, dbc);
1857 	synchronize_srcu(&dbc->ch_lock);
1858 }
1859 
1860 void release_dbc(struct qaic_device *qdev, u32 dbc_id)
1861 {
1862 	struct bo_slice *slice, *slice_temp;
1863 	struct qaic_bo *bo, *bo_temp;
1864 	struct dma_bridge_chan *dbc;
1865 
1866 	dbc = &qdev->dbc[dbc_id];
1867 	if (!dbc->in_use)
1868 		return;
1869 
1870 	wakeup_dbc(qdev, dbc_id);
1871 
1872 	dma_free_coherent(&qdev->pdev->dev, dbc->total_size, dbc->req_q_base, dbc->dma_addr);
1873 	dbc->total_size = 0;
1874 	dbc->req_q_base = NULL;
1875 	dbc->dma_addr = 0;
1876 	dbc->nelem = 0;
1877 	dbc->usr = NULL;
1878 
1879 	list_for_each_entry_safe(bo, bo_temp, &dbc->bo_lists, bo_list) {
1880 		list_for_each_entry_safe(slice, slice_temp, &bo->slices, slice)
1881 			kref_put(&slice->ref_count, free_slice);
1882 		bo->sliced = false;
1883 		INIT_LIST_HEAD(&bo->slices);
1884 		bo->total_slice_nents = 0;
1885 		bo->dir = 0;
1886 		bo->dbc = NULL;
1887 		bo->nr_slice = 0;
1888 		bo->nr_slice_xfer_done = 0;
1889 		bo->queued = false;
1890 		bo->req_id = 0;
1891 		init_completion(&bo->xfer_done);
1892 		complete_all(&bo->xfer_done);
1893 		list_del(&bo->bo_list);
1894 		bo->perf_stats.req_received_ts = 0;
1895 		bo->perf_stats.req_submit_ts = 0;
1896 		bo->perf_stats.req_processed_ts = 0;
1897 		bo->perf_stats.queue_level_before = 0;
1898 	}
1899 
1900 	dbc->in_use = false;
1901 	wake_up(&dbc->dbc_release);
1902 }
1903