162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
462306a36Sopenharmony_ci * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci#ifndef _VNIC_WQ_COPY_H_
762306a36Sopenharmony_ci#define _VNIC_WQ_COPY_H_
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/pci.h>
1062306a36Sopenharmony_ci#include "vnic_wq.h"
1162306a36Sopenharmony_ci#include "fcpio.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#define	VNIC_WQ_COPY_MAX 1
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_cistruct vnic_wq_copy {
1662306a36Sopenharmony_ci	unsigned int index;
1762306a36Sopenharmony_ci	struct vnic_dev *vdev;
1862306a36Sopenharmony_ci	struct vnic_wq_ctrl __iomem *ctrl;	/* memory-mapped */
1962306a36Sopenharmony_ci	struct vnic_dev_ring ring;
2062306a36Sopenharmony_ci	unsigned to_use_index;
2162306a36Sopenharmony_ci	unsigned to_clean_index;
2262306a36Sopenharmony_ci};
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistatic inline unsigned int vnic_wq_copy_desc_avail(struct vnic_wq_copy *wq)
2562306a36Sopenharmony_ci{
2662306a36Sopenharmony_ci	return wq->ring.desc_avail;
2762306a36Sopenharmony_ci}
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cistatic inline unsigned int vnic_wq_copy_desc_in_use(struct vnic_wq_copy *wq)
3062306a36Sopenharmony_ci{
3162306a36Sopenharmony_ci	return wq->ring.desc_count - 1 - wq->ring.desc_avail;
3262306a36Sopenharmony_ci}
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cistatic inline void *vnic_wq_copy_next_desc(struct vnic_wq_copy *wq)
3562306a36Sopenharmony_ci{
3662306a36Sopenharmony_ci	struct fcpio_host_req *desc = wq->ring.descs;
3762306a36Sopenharmony_ci	return &desc[wq->to_use_index];
3862306a36Sopenharmony_ci}
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistatic inline void vnic_wq_copy_post(struct vnic_wq_copy *wq)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	((wq->to_use_index + 1) == wq->ring.desc_count) ?
4462306a36Sopenharmony_ci		(wq->to_use_index = 0) : (wq->to_use_index++);
4562306a36Sopenharmony_ci	wq->ring.desc_avail--;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	/* Adding write memory barrier prevents compiler and/or CPU
4862306a36Sopenharmony_ci	 * reordering, thus avoiding descriptor posting before
4962306a36Sopenharmony_ci	 * descriptor is initialized. Otherwise, hardware can read
5062306a36Sopenharmony_ci	 * stale descriptor fields.
5162306a36Sopenharmony_ci	 */
5262306a36Sopenharmony_ci	wmb();
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	iowrite32(wq->to_use_index, &wq->ctrl->posted_index);
5562306a36Sopenharmony_ci}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistatic inline void vnic_wq_copy_desc_process(struct vnic_wq_copy *wq, u16 index)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	unsigned int cnt;
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	if (wq->to_clean_index <= index)
6262306a36Sopenharmony_ci		cnt = (index - wq->to_clean_index) + 1;
6362306a36Sopenharmony_ci	else
6462306a36Sopenharmony_ci		cnt = wq->ring.desc_count - wq->to_clean_index + index + 1;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	wq->to_clean_index = ((index + 1) % wq->ring.desc_count);
6762306a36Sopenharmony_ci	wq->ring.desc_avail += cnt;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistatic inline void vnic_wq_copy_service(struct vnic_wq_copy *wq,
7262306a36Sopenharmony_ci	u16 completed_index,
7362306a36Sopenharmony_ci	void (*q_service)(struct vnic_wq_copy *wq,
7462306a36Sopenharmony_ci	struct fcpio_host_req *wq_desc))
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	struct fcpio_host_req *wq_desc = wq->ring.descs;
7762306a36Sopenharmony_ci	unsigned int curr_index;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	while (1) {
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci		if (q_service)
8262306a36Sopenharmony_ci			(*q_service)(wq, &wq_desc[wq->to_clean_index]);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci		wq->ring.desc_avail++;
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci		curr_index = wq->to_clean_index;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci		/* increment the to-clean index so that we start
8962306a36Sopenharmony_ci		 * with an unprocessed index next time we enter the loop
9062306a36Sopenharmony_ci		 */
9162306a36Sopenharmony_ci		((wq->to_clean_index + 1) == wq->ring.desc_count) ?
9262306a36Sopenharmony_ci			(wq->to_clean_index = 0) : (wq->to_clean_index++);
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci		if (curr_index == completed_index)
9562306a36Sopenharmony_ci			break;
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci		/* we have cleaned all the entries */
9862306a36Sopenharmony_ci		if ((completed_index == (u16)-1) &&
9962306a36Sopenharmony_ci		    (wq->to_clean_index == wq->to_use_index))
10062306a36Sopenharmony_ci			break;
10162306a36Sopenharmony_ci	}
10262306a36Sopenharmony_ci}
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_civoid vnic_wq_copy_enable(struct vnic_wq_copy *wq);
10562306a36Sopenharmony_ciint vnic_wq_copy_disable(struct vnic_wq_copy *wq);
10662306a36Sopenharmony_civoid vnic_wq_copy_free(struct vnic_wq_copy *wq);
10762306a36Sopenharmony_ciint vnic_wq_copy_alloc(struct vnic_dev *vdev, struct vnic_wq_copy *wq,
10862306a36Sopenharmony_ci	unsigned int index, unsigned int desc_count, unsigned int desc_size);
10962306a36Sopenharmony_civoid vnic_wq_copy_init(struct vnic_wq_copy *wq, unsigned int cq_index,
11062306a36Sopenharmony_ci	unsigned int error_interrupt_enable,
11162306a36Sopenharmony_ci	unsigned int error_interrupt_offset);
11262306a36Sopenharmony_civoid vnic_wq_copy_clean(struct vnic_wq_copy *wq,
11362306a36Sopenharmony_ci	void (*q_clean)(struct vnic_wq_copy *wq,
11462306a36Sopenharmony_ci	struct fcpio_host_req *wq_desc));
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci#endif /* _VNIC_WQ_COPY_H_ */
117