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