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