1 /*
2  * Copyright (c) 2015 Quantenna Communications
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef QTNFMAC_BUS_H
18 #define QTNFMAC_BUS_H
19 
20 #include <linux/netdevice.h>
21 #include <linux/workqueue.h>
22 
23 #define QTNF_MAX_MAC		3
24 
25 enum qtnf_fw_state {
26 	QTNF_FW_STATE_RESET,
27 	QTNF_FW_STATE_FW_DNLD_DONE,
28 	QTNF_FW_STATE_BOOT_DONE,
29 	QTNF_FW_STATE_ACTIVE,
30 	QTNF_FW_STATE_DEAD,
31 };
32 
33 struct qtnf_bus;
34 
35 struct qtnf_bus_ops {
36 	/* mgmt methods */
37 	int (*preinit)(struct qtnf_bus *);
38 	void (*stop)(struct qtnf_bus *);
39 
40 	/* control path methods */
41 	int (*control_tx)(struct qtnf_bus *, struct sk_buff *);
42 
43 	/* data xfer methods */
44 	int (*data_tx)(struct qtnf_bus *, struct sk_buff *);
45 	void (*data_tx_timeout)(struct qtnf_bus *, struct net_device *);
46 	void (*data_rx_start)(struct qtnf_bus *);
47 	void (*data_rx_stop)(struct qtnf_bus *);
48 };
49 
50 struct qtnf_bus {
51 	struct device *dev;
52 	enum qtnf_fw_state fw_state;
53 	u32 chip;
54 	u32 chiprev;
55 	const struct qtnf_bus_ops *bus_ops;
56 	struct qtnf_wmac *mac[QTNF_MAX_MAC];
57 	struct qtnf_qlink_transport trans;
58 	struct qtnf_hw_info hw_info;
59 	char fwname[32];
60 	struct napi_struct mux_napi;
61 	struct net_device mux_dev;
62 	struct completion request_firmware_complete;
63 	struct workqueue_struct *workqueue;
64 	struct work_struct event_work;
65 	struct mutex bus_lock; /* lock during command/event processing */
66 	struct dentry *dbg_dir;
67 	/* bus private data */
68 	char bus_priv[0] __aligned(sizeof(void *));
69 };
70 
71 static inline void *get_bus_priv(struct qtnf_bus *bus)
72 {
73 	if (WARN(!bus, "qtnfmac: invalid bus pointer"))
74 		return NULL;
75 
76 	return &bus->bus_priv;
77 }
78 
79 /* callback wrappers */
80 
81 static inline int qtnf_bus_preinit(struct qtnf_bus *bus)
82 {
83 	if (!bus->bus_ops->preinit)
84 		return 0;
85 	return bus->bus_ops->preinit(bus);
86 }
87 
88 static inline void qtnf_bus_stop(struct qtnf_bus *bus)
89 {
90 	if (!bus->bus_ops->stop)
91 		return;
92 	bus->bus_ops->stop(bus);
93 }
94 
95 static inline int qtnf_bus_data_tx(struct qtnf_bus *bus, struct sk_buff *skb)
96 {
97 	return bus->bus_ops->data_tx(bus, skb);
98 }
99 
100 static inline void
101 qtnf_bus_data_tx_timeout(struct qtnf_bus *bus, struct net_device *ndev)
102 {
103 	return bus->bus_ops->data_tx_timeout(bus, ndev);
104 }
105 
106 static inline int qtnf_bus_control_tx(struct qtnf_bus *bus, struct sk_buff *skb)
107 {
108 	return bus->bus_ops->control_tx(bus, skb);
109 }
110 
111 static inline void qtnf_bus_data_rx_start(struct qtnf_bus *bus)
112 {
113 	return bus->bus_ops->data_rx_start(bus);
114 }
115 
116 static inline void qtnf_bus_data_rx_stop(struct qtnf_bus *bus)
117 {
118 	return bus->bus_ops->data_rx_stop(bus);
119 }
120 
121 static __always_inline void qtnf_bus_lock(struct qtnf_bus *bus)
122 {
123 	mutex_lock(&bus->bus_lock);
124 }
125 
126 static __always_inline void qtnf_bus_unlock(struct qtnf_bus *bus)
127 {
128 	mutex_unlock(&bus->bus_lock);
129 }
130 
131 /* interface functions from common layer */
132 
133 int qtnf_core_attach(struct qtnf_bus *bus);
134 void qtnf_core_detach(struct qtnf_bus *bus);
135 void qtnf_txflowblock(struct device *dev, bool state);
136 void qtnf_txcomplete(struct device *dev, struct sk_buff *txp, bool success);
137 
138 #endif /* QTNFMAC_BUS_H */
139