1/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
2
3#ifndef _FUNDEV_H
4#define _FUNDEV_H
5
6#include <linux/sbitmap.h>
7#include <linux/spinlock_types.h>
8#include <linux/workqueue.h>
9#include "fun_hci.h"
10
11struct pci_dev;
12struct fun_dev;
13struct fun_queue;
14struct fun_cmd_ctx;
15struct fun_queue_alloc_req;
16
17/* doorbell fields */
18enum {
19	FUN_DB_QIDX_S = 0,
20	FUN_DB_INTCOAL_ENTRIES_S = 16,
21	FUN_DB_INTCOAL_ENTRIES_M = 0x7f,
22	FUN_DB_INTCOAL_USEC_S = 23,
23	FUN_DB_INTCOAL_USEC_M = 0x7f,
24	FUN_DB_IRQ_S = 30,
25	FUN_DB_IRQ_F = 1 << FUN_DB_IRQ_S,
26	FUN_DB_IRQ_ARM_S = 31,
27	FUN_DB_IRQ_ARM_F = 1U << FUN_DB_IRQ_ARM_S
28};
29
30/* Callback for asynchronous admin commands.
31 * Invoked on reception of command response.
32 */
33typedef void (*fun_admin_callback_t)(struct fun_dev *fdev, void *rsp,
34				     void *cb_data);
35
36/* Callback for events/notifications received by an admin queue. */
37typedef void (*fun_admin_event_cb)(struct fun_dev *fdev, void *cqe);
38
39/* Callback for pending work handled by the service task. */
40typedef void (*fun_serv_cb)(struct fun_dev *fd);
41
42/* service task flags */
43enum {
44	FUN_SERV_DISABLED,    /* service task is disabled */
45	FUN_SERV_FIRST_AVAIL
46};
47
48/* Driver state associated with a PCI function. */
49struct fun_dev {
50	struct device *dev;
51
52	void __iomem *bar;            /* start of BAR0 mapping */
53	u32 __iomem *dbs;             /* start of doorbells in BAR0 mapping */
54
55	/* admin queue */
56	struct fun_queue *admin_q;
57	struct sbitmap_queue admin_sbq;
58	struct fun_cmd_ctx *cmd_ctx;
59	fun_admin_event_cb adminq_cb;
60	bool suppress_cmds;           /* if set don't write commands to SQ */
61
62	/* address increment between consecutive doorbells, in 4B units */
63	unsigned int db_stride;
64
65	/* SW versions of device registers */
66	u32 cc_reg;         /* CC register */
67	u64 cap_reg;        /* CAPability register */
68
69	unsigned int q_depth;    /* max queue depth supported by device */
70	unsigned int max_qid;    /* = #queues - 1, separately for SQs and CQs */
71	unsigned int kern_end_qid; /* last qid in the kernel range + 1 */
72
73	unsigned int fw_handle;
74
75	/* IRQ manager */
76	unsigned int num_irqs;
77	unsigned int irqs_avail;
78	spinlock_t irqmgr_lock;
79	unsigned long *irq_map;
80
81	/* The service task handles work that needs a process context */
82	struct work_struct service_task;
83	unsigned long service_flags;
84	fun_serv_cb serv_cb;
85};
86
87struct fun_dev_params {
88	u8  cqe_size_log2; /* admin q CQE size */
89	u8  sqe_size_log2; /* admin q SQE size */
90
91	/* admin q depths */
92	u16 cq_depth;
93	u16 sq_depth;
94	u16 rq_depth;
95
96	u16 min_msix; /* min vectors needed by requesting driver */
97
98	fun_admin_event_cb event_cb;
99	fun_serv_cb serv_cb;
100};
101
102/* Return the BAR address of a doorbell. */
103static inline u32 __iomem *fun_db_addr(const struct fun_dev *fdev,
104				       unsigned int db_index)
105{
106	return &fdev->dbs[db_index * fdev->db_stride];
107}
108
109/* Return the BAR address of an SQ doorbell. SQ and CQ DBs alternate,
110 * SQs have even DB indices.
111 */
112static inline u32 __iomem *fun_sq_db_addr(const struct fun_dev *fdev,
113					  unsigned int sqid)
114{
115	return fun_db_addr(fdev, sqid * 2);
116}
117
118static inline u32 __iomem *fun_cq_db_addr(const struct fun_dev *fdev,
119					  unsigned int cqid)
120{
121	return fun_db_addr(fdev, cqid * 2 + 1);
122}
123
124int fun_get_res_count(struct fun_dev *fdev, enum fun_admin_op res);
125int fun_res_destroy(struct fun_dev *fdev, enum fun_admin_op res,
126		    unsigned int flags, u32 id);
127int fun_bind(struct fun_dev *fdev, enum fun_admin_bind_type type0,
128	     unsigned int id0, enum fun_admin_bind_type type1,
129	     unsigned int id1);
130
131int fun_submit_admin_cmd(struct fun_dev *fdev, struct fun_admin_req_common *cmd,
132			 fun_admin_callback_t cb, void *cb_data, bool wait_ok);
133int fun_submit_admin_sync_cmd(struct fun_dev *fdev,
134			      struct fun_admin_req_common *cmd, void *rsp,
135			      size_t rspsize, unsigned int timeout);
136
137int fun_dev_enable(struct fun_dev *fdev, struct pci_dev *pdev,
138		   const struct fun_dev_params *areq, const char *name);
139void fun_dev_disable(struct fun_dev *fdev);
140
141int fun_reserve_irqs(struct fun_dev *fdev, unsigned int nirqs,
142		     u16 *irq_indices);
143void fun_release_irqs(struct fun_dev *fdev, unsigned int nirqs,
144		      u16 *irq_indices);
145
146void fun_serv_stop(struct fun_dev *fd);
147void fun_serv_restart(struct fun_dev *fd);
148void fun_serv_sched(struct fun_dev *fd);
149
150#endif /* _FUNDEV_H */
151