1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2010 Broadcom Corporation
4  */
5 
6 #ifndef BRCMFMAC_BUS_H
7 #define BRCMFMAC_BUS_H
8 
9 #include <linux/kernel.h>
10 #include <linux/firmware.h>
11 #include "debug.h"
12 
13 /* IDs of the 6 default common rings of msgbuf protocol */
14 #define BRCMF_H2D_MSGRING_CONTROL_SUBMIT	0
15 #define BRCMF_H2D_MSGRING_RXPOST_SUBMIT		1
16 #define BRCMF_H2D_MSGRING_FLOWRING_IDSTART	2
17 #define BRCMF_D2H_MSGRING_CONTROL_COMPLETE	2
18 #define BRCMF_D2H_MSGRING_TX_COMPLETE		3
19 #define BRCMF_D2H_MSGRING_RX_COMPLETE		4
20 
21 
22 #define BRCMF_NROF_H2D_COMMON_MSGRINGS		2
23 #define BRCMF_NROF_D2H_COMMON_MSGRINGS		3
24 #define BRCMF_NROF_COMMON_MSGRINGS	(BRCMF_NROF_H2D_COMMON_MSGRINGS + \
25 					 BRCMF_NROF_D2H_COMMON_MSGRINGS)
26 
27 /* The level of bus communication with the dongle */
28 enum brcmf_bus_state {
29 	BRCMF_BUS_DOWN,		/* Not ready for frame transfers */
30 	BRCMF_BUS_UP		/* Ready for frame transfers */
31 };
32 
33 /* The level of bus communication with the dongle */
34 enum brcmf_bus_protocol_type {
35 	BRCMF_PROTO_BCDC,
36 	BRCMF_PROTO_MSGBUF
37 };
38 
39 /* Firmware blobs that may be available */
40 enum brcmf_blob_type {
41 	BRCMF_BLOB_CLM,
42 };
43 
44 struct brcmf_mp_device;
45 
46 struct brcmf_bus_dcmd {
47 	char *name;
48 	char *param;
49 	int param_len;
50 	struct list_head list;
51 };
52 
53 /**
54  * struct brcmf_bus_ops - bus callback operations.
55  *
56  * @preinit: execute bus/device specific dongle init commands (optional).
57  * @init: prepare for communication with dongle.
58  * @stop: clear pending frames, disable data flow.
59  * @txdata: send a data frame to the dongle. When the data
60  *	has been transferred, the common driver must be
61  *	notified using brcmf_txcomplete(). The common
62  *	driver calls this function with interrupts
63  *	disabled.
64  * @txctl: transmit a control request message to dongle.
65  * @rxctl: receive a control response message from dongle.
66  * @gettxq: obtain a reference of bus transmit queue (optional).
67  * @wowl_config: specify if dongle is configured for wowl when going to suspend
68  * @get_ramsize: obtain size of device memory.
69  * @get_memdump: obtain device memory dump in provided buffer.
70  * @get_blob: obtain a firmware blob.
71  *
72  * This structure provides an abstract interface towards the
73  * bus specific driver. For control messages to common driver
74  * will assure there is only one active transaction. Unless
75  * indicated otherwise these callbacks are mandatory.
76  */
77 struct brcmf_bus_ops {
78 	int (*preinit)(struct device *dev);
79 	void (*stop)(struct device *dev);
80 	int (*txdata)(struct device *dev, struct sk_buff *skb);
81 	int (*txctl)(struct device *dev, unsigned char *msg, uint len);
82 	int (*rxctl)(struct device *dev, unsigned char *msg, uint len);
83 	struct pktq * (*gettxq)(struct device *dev);
84 	void (*wowl_config)(struct device *dev, bool enabled);
85 	size_t (*get_ramsize)(struct device *dev);
86 	int (*get_memdump)(struct device *dev, void *data, size_t len);
87 	int (*get_blob)(struct device *dev, const struct firmware **fw,
88 			enum brcmf_blob_type type);
89 	void (*debugfs_create)(struct device *dev);
90 	int (*reset)(struct device *dev);
91 };
92 
93 
94 /**
95  * struct brcmf_bus_msgbuf - bus ringbuf if in case of msgbuf.
96  *
97  * @commonrings: commonrings which are always there.
98  * @flowrings: commonrings which are dynamically created and destroyed for data.
99  * @rx_dataoffset: if set then all rx data has this offset.
100  * @max_rxbufpost: maximum number of buffers to post for rx.
101  * @max_flowrings: maximum number of tx flow rings supported.
102  * @max_submissionrings: maximum number of submission rings(h2d) supported.
103  * @max_completionrings: maximum number of completion rings(d2h) supported.
104  */
105 struct brcmf_bus_msgbuf {
106 	struct brcmf_commonring *commonrings[BRCMF_NROF_COMMON_MSGRINGS];
107 	struct brcmf_commonring **flowrings;
108 	u32 rx_dataoffset;
109 	u32 max_rxbufpost;
110 	u16 max_flowrings;
111 	u16 max_submissionrings;
112 	u16 max_completionrings;
113 };
114 
115 
116 /**
117  * struct brcmf_bus_stats - bus statistic counters.
118  *
119  * @pktcowed: packets cowed for extra headroom/unorphan.
120  * @pktcow_failed: packets dropped due to failed cow-ing.
121  */
122 struct brcmf_bus_stats {
123 	atomic_t pktcowed;
124 	atomic_t pktcow_failed;
125 };
126 
127 /**
128  * struct brcmf_bus - interface structure between common and bus layer
129  *
130  * @bus_priv: pointer to private bus device.
131  * @proto_type: protocol type, bcdc or msgbuf
132  * @dev: device pointer of bus device.
133  * @drvr: public driver information.
134  * @state: operational state of the bus interface.
135  * @stats: statistics shared between common and bus layer.
136  * @maxctl: maximum size for rxctl request message.
137  * @chip: device identifier of the dongle chip.
138  * @always_use_fws_queue: bus wants use queue also when fwsignal is inactive.
139  * @wowl_supported: is wowl supported by bus driver.
140  * @chiprev: revision of the dongle chip.
141  * @msgbuf: msgbuf protocol parameters provided by bus layer.
142  */
143 struct brcmf_bus {
144 	union {
145 		struct brcmf_sdio_dev *sdio;
146 		struct brcmf_usbdev *usb;
147 		struct brcmf_pciedev *pcie;
148 	} bus_priv;
149 	enum brcmf_bus_protocol_type proto_type;
150 	struct device *dev;
151 	struct brcmf_pub *drvr;
152 	enum brcmf_bus_state state;
153 	struct brcmf_bus_stats stats;
154 	uint maxctl;
155 	u32 chip;
156 	u32 chiprev;
157 	bool always_use_fws_queue;
158 	bool wowl_supported;
159 
160 	const struct brcmf_bus_ops *ops;
161 	struct brcmf_bus_msgbuf *msgbuf;
162 };
163 
164 /*
165  * callback wrappers
166  */
167 static inline int brcmf_bus_preinit(struct brcmf_bus *bus)
168 {
169 	if (!bus->ops->preinit)
170 		return 0;
171 	return bus->ops->preinit(bus->dev);
172 }
173 
174 static inline void brcmf_bus_stop(struct brcmf_bus *bus)
175 {
176 	bus->ops->stop(bus->dev);
177 }
178 
179 static inline int brcmf_bus_txdata(struct brcmf_bus *bus, struct sk_buff *skb)
180 {
181 	return bus->ops->txdata(bus->dev, skb);
182 }
183 
184 static inline
185 int brcmf_bus_txctl(struct brcmf_bus *bus, unsigned char *msg, uint len)
186 {
187 	return bus->ops->txctl(bus->dev, msg, len);
188 }
189 
190 static inline
191 int brcmf_bus_rxctl(struct brcmf_bus *bus, unsigned char *msg, uint len)
192 {
193 	return bus->ops->rxctl(bus->dev, msg, len);
194 }
195 
196 static inline
197 struct pktq *brcmf_bus_gettxq(struct brcmf_bus *bus)
198 {
199 	if (!bus->ops->gettxq)
200 		return ERR_PTR(-ENOENT);
201 
202 	return bus->ops->gettxq(bus->dev);
203 }
204 
205 static inline
206 void brcmf_bus_wowl_config(struct brcmf_bus *bus, bool enabled)
207 {
208 	if (bus->ops->wowl_config)
209 		bus->ops->wowl_config(bus->dev, enabled);
210 }
211 
212 static inline size_t brcmf_bus_get_ramsize(struct brcmf_bus *bus)
213 {
214 	if (!bus->ops->get_ramsize)
215 		return 0;
216 
217 	return bus->ops->get_ramsize(bus->dev);
218 }
219 
220 static inline
221 int brcmf_bus_get_memdump(struct brcmf_bus *bus, void *data, size_t len)
222 {
223 	if (!bus->ops->get_memdump)
224 		return -EOPNOTSUPP;
225 
226 	return bus->ops->get_memdump(bus->dev, data, len);
227 }
228 
229 static inline
230 int brcmf_bus_get_blob(struct brcmf_bus *bus, const struct firmware **fw,
231 		       enum brcmf_blob_type type)
232 {
233 	return bus->ops->get_blob(bus->dev, fw, type);
234 }
235 
236 static inline
237 void brcmf_bus_debugfs_create(struct brcmf_bus *bus)
238 {
239 	if (!bus->ops->debugfs_create)
240 		return;
241 
242 	return bus->ops->debugfs_create(bus->dev);
243 }
244 
245 static inline
246 int brcmf_bus_reset(struct brcmf_bus *bus)
247 {
248 	if (!bus->ops->reset)
249 		return -EOPNOTSUPP;
250 
251 	return bus->ops->reset(bus->dev);
252 }
253 
254 /*
255  * interface functions from common layer
256  */
257 
258 /* Receive frame for delivery to OS.  Callee disposes of rxp. */
259 void brcmf_rx_frame(struct device *dev, struct sk_buff *rxp, bool handle_event,
260 		    bool inirq);
261 /* Receive async event packet from firmware. Callee disposes of rxp. */
262 void brcmf_rx_event(struct device *dev, struct sk_buff *rxp);
263 
264 int brcmf_alloc(struct device *dev, struct brcmf_mp_device *settings);
265 /* Indication from bus module regarding presence/insertion of dongle. */
266 int brcmf_attach(struct device *dev);
267 /* Indication from bus module regarding removal/absence of dongle */
268 void brcmf_detach(struct device *dev);
269 void brcmf_free(struct device *dev);
270 /* Indication from bus module that dongle should be reset */
271 void brcmf_dev_reset(struct device *dev);
272 /* Request from bus module to initiate a coredump */
273 void brcmf_dev_coredump(struct device *dev);
274 /* Indication that firmware has halted or crashed */
275 void brcmf_fw_crashed(struct device *dev);
276 
277 /* Configure the "global" bus state used by upper layers */
278 void brcmf_bus_change_state(struct brcmf_bus *bus, enum brcmf_bus_state state);
279 
280 s32 brcmf_iovar_data_set(struct device *dev, char *name, void *data, u32 len);
281 void brcmf_bus_add_txhdrlen(struct device *dev, uint len);
282 
283 #ifdef CONFIG_BRCMFMAC_SDIO
284 void brcmf_sdio_exit(void);
285 int brcmf_sdio_register(void);
286 #else
287 static inline void brcmf_sdio_exit(void) { }
288 static inline int brcmf_sdio_register(void) { return 0; }
289 #endif
290 
291 #ifdef CONFIG_BRCMFMAC_USB
292 void brcmf_usb_exit(void);
293 int brcmf_usb_register(void);
294 #else
295 static inline void brcmf_usb_exit(void) { }
296 static inline int brcmf_usb_register(void) { return 0; }
297 #endif
298 
299 #ifdef CONFIG_BRCMFMAC_PCIE
300 void brcmf_pcie_exit(void);
301 int brcmf_pcie_register(void);
302 #else
303 static inline void brcmf_pcie_exit(void) { }
304 static inline int brcmf_pcie_register(void) { return 0; }
305 #endif
306 
307 #endif /* BRCMFMAC_BUS_H */
308