1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2010 Broadcom Corporation
9 #include <linux/kernel.h>
10 #include <linux/firmware.h>
11 #include <linux/device.h>
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
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)
28 /* The interval to poll console */
29 #define BRCMF_CONSOLE 10
31 /* The maximum console interval value (5 mins) */
32 #define MAX_CONSOLE_INTERVAL (5 * 60)
40 BRCMF_FWVENDOR_INVALID
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 */
49 /* The level of bus communication with the dongle */
50 enum brcmf_bus_protocol_type {
55 /* Firmware blobs that may be available */
56 enum brcmf_blob_type {
61 struct brcmf_mp_device;
63 struct brcmf_bus_dcmd {
67 struct list_head list;
71 * struct brcmf_bus_ops - bus callback operations.
73 * @preinit: execute bus/device specific dongle init commands (optional).
74 * @init: prepare for communication with dongle.
75 * @stop: clear pending frames, disable data flow.
76 * @txdata: send a data frame to the dongle. When the data
77 * has been transferred, the common driver must be
78 * notified using brcmf_txcomplete(). The common
79 * driver calls this function with interrupts
81 * @txctl: transmit a control request message to dongle.
82 * @rxctl: receive a control response message from dongle.
83 * @gettxq: obtain a reference of bus transmit queue (optional).
84 * @wowl_config: specify if dongle is configured for wowl when going to suspend
85 * @get_ramsize: obtain size of device memory.
86 * @get_memdump: obtain device memory dump in provided buffer.
87 * @get_blob: obtain a firmware blob.
88 * @remove: initiate unbind of the device.
90 * This structure provides an abstract interface towards the
91 * bus specific driver. For control messages to common driver
92 * will assure there is only one active transaction. Unless
93 * indicated otherwise these callbacks are mandatory.
95 struct brcmf_bus_ops {
96 int (*preinit)(struct device *dev);
97 void (*stop)(struct device *dev);
98 int (*txdata)(struct device *dev, struct sk_buff *skb);
99 int (*txctl)(struct device *dev, unsigned char *msg, uint len);
100 int (*rxctl)(struct device *dev, unsigned char *msg, uint len);
101 struct pktq * (*gettxq)(struct device *dev);
102 void (*wowl_config)(struct device *dev, bool enabled);
103 size_t (*get_ramsize)(struct device *dev);
104 int (*get_memdump)(struct device *dev, void *data, size_t len);
105 int (*get_blob)(struct device *dev, const struct firmware **fw,
106 enum brcmf_blob_type type);
107 void (*debugfs_create)(struct device *dev);
108 int (*reset)(struct device *dev);
109 void (*remove)(struct device *dev);
114 * struct brcmf_bus_msgbuf - bus ringbuf if in case of msgbuf.
116 * @commonrings: commonrings which are always there.
117 * @flowrings: commonrings which are dynamically created and destroyed for data.
118 * @rx_dataoffset: if set then all rx data has this offset.
119 * @max_rxbufpost: maximum number of buffers to post for rx.
120 * @max_flowrings: maximum number of tx flow rings supported.
121 * @max_submissionrings: maximum number of submission rings(h2d) supported.
122 * @max_completionrings: maximum number of completion rings(d2h) supported.
124 struct brcmf_bus_msgbuf {
125 struct brcmf_commonring *commonrings[BRCMF_NROF_COMMON_MSGRINGS];
126 struct brcmf_commonring **flowrings;
130 u16 max_submissionrings;
131 u16 max_completionrings;
136 * struct brcmf_bus_stats - bus statistic counters.
138 * @pktcowed: packets cowed for extra headroom/unorphan.
139 * @pktcow_failed: packets dropped due to failed cow-ing.
141 struct brcmf_bus_stats {
143 atomic_t pktcow_failed;
147 * struct brcmf_bus - interface structure between common and bus layer
149 * @bus_priv: pointer to private bus device.
150 * @proto_type: protocol type, bcdc or msgbuf
151 * @dev: device pointer of bus device.
152 * @drvr: public driver information.
153 * @state: operational state of the bus interface.
154 * @stats: statistics shared between common and bus layer.
155 * @maxctl: maximum size for rxctl request message.
156 * @chip: device identifier of the dongle chip.
157 * @chiprev: revision of the dongle chip.
158 * @fwvid: firmware vendor-support identifier of the device.
159 * @always_use_fws_queue: bus wants use queue also when fwsignal is inactive.
160 * @wowl_supported: is wowl supported by bus driver.
161 * @ops: callbacks for this bus instance.
162 * @msgbuf: msgbuf protocol parameters provided by bus layer.
163 * @list: member used to add this bus instance to linked list.
167 struct brcmf_sdio_dev *sdio;
168 struct brcmf_usbdev *usb;
169 struct brcmf_pciedev *pcie;
171 enum brcmf_bus_protocol_type proto_type;
173 struct brcmf_pub *drvr;
174 enum brcmf_bus_state state;
175 struct brcmf_bus_stats stats;
179 enum brcmf_fwvendor fwvid;
180 bool always_use_fws_queue;
183 const struct brcmf_bus_ops *ops;
184 struct brcmf_bus_msgbuf *msgbuf;
186 struct list_head list;
192 static inline int brcmf_bus_preinit(struct brcmf_bus *bus)
194 if (!bus->ops->preinit)
196 return bus->ops->preinit(bus->dev);
199 static inline void brcmf_bus_stop(struct brcmf_bus *bus)
201 bus->ops->stop(bus->dev);
204 static inline int brcmf_bus_txdata(struct brcmf_bus *bus, struct sk_buff *skb)
206 return bus->ops->txdata(bus->dev, skb);
210 int brcmf_bus_txctl(struct brcmf_bus *bus, unsigned char *msg, uint len)
212 return bus->ops->txctl(bus->dev, msg, len);
216 int brcmf_bus_rxctl(struct brcmf_bus *bus, unsigned char *msg, uint len)
218 return bus->ops->rxctl(bus->dev, msg, len);
222 struct pktq *brcmf_bus_gettxq(struct brcmf_bus *bus)
224 if (!bus->ops->gettxq)
225 return ERR_PTR(-ENOENT);
227 return bus->ops->gettxq(bus->dev);
231 void brcmf_bus_wowl_config(struct brcmf_bus *bus, bool enabled)
233 if (bus->ops->wowl_config)
234 bus->ops->wowl_config(bus->dev, enabled);
237 static inline size_t brcmf_bus_get_ramsize(struct brcmf_bus *bus)
239 if (!bus->ops->get_ramsize)
242 return bus->ops->get_ramsize(bus->dev);
246 int brcmf_bus_get_memdump(struct brcmf_bus *bus, void *data, size_t len)
248 if (!bus->ops->get_memdump)
251 return bus->ops->get_memdump(bus->dev, data, len);
255 int brcmf_bus_get_blob(struct brcmf_bus *bus, const struct firmware **fw,
256 enum brcmf_blob_type type)
258 return bus->ops->get_blob(bus->dev, fw, type);
262 void brcmf_bus_debugfs_create(struct brcmf_bus *bus)
264 if (!bus->ops->debugfs_create)
267 return bus->ops->debugfs_create(bus->dev);
271 int brcmf_bus_reset(struct brcmf_bus *bus)
273 if (!bus->ops->reset)
276 return bus->ops->reset(bus->dev);
279 static inline void brcmf_bus_remove(struct brcmf_bus *bus)
281 if (!bus->ops->remove) {
282 device_release_driver(bus->dev);
286 bus->ops->remove(bus->dev);
290 * interface functions from common layer
293 /* Receive frame for delivery to OS. Callee disposes of rxp. */
294 void brcmf_rx_frame(struct device *dev, struct sk_buff *rxp, bool handle_event,
296 /* Receive async event packet from firmware. Callee disposes of rxp. */
297 void brcmf_rx_event(struct device *dev, struct sk_buff *rxp);
299 int brcmf_alloc(struct device *dev, struct brcmf_mp_device *settings);
300 /* Indication from bus module regarding presence/insertion of dongle. */
301 int brcmf_attach(struct device *dev);
302 /* Indication from bus module regarding removal/absence of dongle */
303 void brcmf_detach(struct device *dev);
304 void brcmf_free(struct device *dev);
305 /* Indication from bus module that dongle should be reset */
306 void brcmf_dev_reset(struct device *dev);
307 /* Request from bus module to initiate a coredump */
308 void brcmf_dev_coredump(struct device *dev);
309 /* Indication that firmware has halted or crashed */
310 void brcmf_fw_crashed(struct device *dev);
312 /* Configure the "global" bus state used by upper layers */
313 void brcmf_bus_change_state(struct brcmf_bus *bus, enum brcmf_bus_state state);
315 s32 brcmf_iovar_data_set(struct device *dev, char *name, void *data, u32 len);
316 void brcmf_bus_add_txhdrlen(struct device *dev, uint len);
318 #ifdef CONFIG_BRCMFMAC_SDIO
319 void brcmf_sdio_exit(void);
320 int brcmf_sdio_register(void);
322 static inline void brcmf_sdio_exit(void) { }
323 static inline int brcmf_sdio_register(void) { return 0; }
326 #ifdef CONFIG_BRCMFMAC_USB
327 void brcmf_usb_exit(void);
328 int brcmf_usb_register(void);
330 static inline void brcmf_usb_exit(void) { }
331 static inline int brcmf_usb_register(void) { return 0; }
334 #ifdef CONFIG_BRCMFMAC_PCIE
335 void brcmf_pcie_exit(void);
336 int brcmf_pcie_register(void);
338 static inline void brcmf_pcie_exit(void) { }
339 static inline int brcmf_pcie_register(void) { return 0; }
342 #endif /* BRCMFMAC_BUS_H */