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