18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This program is free software; you may redistribute it and/or modify
68c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License as published by
78c2ecf20Sopenharmony_ci * the Free Software Foundation; version 2 of the License.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
108c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
118c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
128c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
138c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
148c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
158c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
168c2ecf20Sopenharmony_ci * SOFTWARE.
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci#ifndef _VNIC_CQ_H_
198c2ecf20Sopenharmony_ci#define _VNIC_CQ_H_
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "cq_desc.h"
228c2ecf20Sopenharmony_ci#include "vnic_dev.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/*
258c2ecf20Sopenharmony_ci * These defines avoid symbol clash between fnic and enic (Cisco 10G Eth
268c2ecf20Sopenharmony_ci * Driver) when both are built with CONFIG options =y
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci#define vnic_cq_service fnic_cq_service
298c2ecf20Sopenharmony_ci#define vnic_cq_free fnic_cq_free
308c2ecf20Sopenharmony_ci#define vnic_cq_alloc fnic_cq_alloc
318c2ecf20Sopenharmony_ci#define vnic_cq_init fnic_cq_init
328c2ecf20Sopenharmony_ci#define vnic_cq_clean fnic_cq_clean
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* Completion queue control */
358c2ecf20Sopenharmony_cistruct vnic_cq_ctrl {
368c2ecf20Sopenharmony_ci	u64 ring_base;			/* 0x00 */
378c2ecf20Sopenharmony_ci	u32 ring_size;			/* 0x08 */
388c2ecf20Sopenharmony_ci	u32 pad0;
398c2ecf20Sopenharmony_ci	u32 flow_control_enable;	/* 0x10 */
408c2ecf20Sopenharmony_ci	u32 pad1;
418c2ecf20Sopenharmony_ci	u32 color_enable;		/* 0x18 */
428c2ecf20Sopenharmony_ci	u32 pad2;
438c2ecf20Sopenharmony_ci	u32 cq_head;			/* 0x20 */
448c2ecf20Sopenharmony_ci	u32 pad3;
458c2ecf20Sopenharmony_ci	u32 cq_tail;			/* 0x28 */
468c2ecf20Sopenharmony_ci	u32 pad4;
478c2ecf20Sopenharmony_ci	u32 cq_tail_color;		/* 0x30 */
488c2ecf20Sopenharmony_ci	u32 pad5;
498c2ecf20Sopenharmony_ci	u32 interrupt_enable;		/* 0x38 */
508c2ecf20Sopenharmony_ci	u32 pad6;
518c2ecf20Sopenharmony_ci	u32 cq_entry_enable;		/* 0x40 */
528c2ecf20Sopenharmony_ci	u32 pad7;
538c2ecf20Sopenharmony_ci	u32 cq_message_enable;		/* 0x48 */
548c2ecf20Sopenharmony_ci	u32 pad8;
558c2ecf20Sopenharmony_ci	u32 interrupt_offset;		/* 0x50 */
568c2ecf20Sopenharmony_ci	u32 pad9;
578c2ecf20Sopenharmony_ci	u64 cq_message_addr;		/* 0x58 */
588c2ecf20Sopenharmony_ci	u32 pad10;
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistruct vnic_cq {
628c2ecf20Sopenharmony_ci	unsigned int index;
638c2ecf20Sopenharmony_ci	struct vnic_dev *vdev;
648c2ecf20Sopenharmony_ci	struct vnic_cq_ctrl __iomem *ctrl;	/* memory-mapped */
658c2ecf20Sopenharmony_ci	struct vnic_dev_ring ring;
668c2ecf20Sopenharmony_ci	unsigned int to_clean;
678c2ecf20Sopenharmony_ci	unsigned int last_color;
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic inline unsigned int vnic_cq_service(struct vnic_cq *cq,
718c2ecf20Sopenharmony_ci	unsigned int work_to_do,
728c2ecf20Sopenharmony_ci	int (*q_service)(struct vnic_dev *vdev, struct cq_desc *cq_desc,
738c2ecf20Sopenharmony_ci	u8 type, u16 q_number, u16 completed_index, void *opaque),
748c2ecf20Sopenharmony_ci	void *opaque)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct cq_desc *cq_desc;
778c2ecf20Sopenharmony_ci	unsigned int work_done = 0;
788c2ecf20Sopenharmony_ci	u16 q_number, completed_index;
798c2ecf20Sopenharmony_ci	u8 type, color;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	cq_desc = (struct cq_desc *)((u8 *)cq->ring.descs +
828c2ecf20Sopenharmony_ci		cq->ring.desc_size * cq->to_clean);
838c2ecf20Sopenharmony_ci	cq_desc_dec(cq_desc, &type, &color,
848c2ecf20Sopenharmony_ci		&q_number, &completed_index);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	while (color != cq->last_color) {
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci		if ((*q_service)(cq->vdev, cq_desc, type,
898c2ecf20Sopenharmony_ci			q_number, completed_index, opaque))
908c2ecf20Sopenharmony_ci			break;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci		cq->to_clean++;
938c2ecf20Sopenharmony_ci		if (cq->to_clean == cq->ring.desc_count) {
948c2ecf20Sopenharmony_ci			cq->to_clean = 0;
958c2ecf20Sopenharmony_ci			cq->last_color = cq->last_color ? 0 : 1;
968c2ecf20Sopenharmony_ci		}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci		cq_desc = (struct cq_desc *)((u8 *)cq->ring.descs +
998c2ecf20Sopenharmony_ci			cq->ring.desc_size * cq->to_clean);
1008c2ecf20Sopenharmony_ci		cq_desc_dec(cq_desc, &type, &color,
1018c2ecf20Sopenharmony_ci			&q_number, &completed_index);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci		work_done++;
1048c2ecf20Sopenharmony_ci		if (work_done >= work_to_do)
1058c2ecf20Sopenharmony_ci			break;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return work_done;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_civoid vnic_cq_free(struct vnic_cq *cq);
1128c2ecf20Sopenharmony_ciint vnic_cq_alloc(struct vnic_dev *vdev, struct vnic_cq *cq, unsigned int index,
1138c2ecf20Sopenharmony_ci	unsigned int desc_count, unsigned int desc_size);
1148c2ecf20Sopenharmony_civoid vnic_cq_init(struct vnic_cq *cq, unsigned int flow_control_enable,
1158c2ecf20Sopenharmony_ci	unsigned int color_enable, unsigned int cq_head, unsigned int cq_tail,
1168c2ecf20Sopenharmony_ci	unsigned int cq_tail_color, unsigned int interrupt_enable,
1178c2ecf20Sopenharmony_ci	unsigned int cq_entry_enable, unsigned int message_enable,
1188c2ecf20Sopenharmony_ci	unsigned int interrupt_offset, u64 message_addr);
1198c2ecf20Sopenharmony_civoid vnic_cq_clean(struct vnic_cq *cq);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#endif /* _VNIC_CQ_H_ */
122