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 #include "trans.h"
24 #include "core.h"
25 
26 #define QTNF_MAX_MAC		3
27 
28 enum qtnf_fw_state {
29 	QTNF_FW_STATE_RESET,
30 	QTNF_FW_STATE_FW_DNLD_DONE,
31 	QTNF_FW_STATE_BOOT_DONE,
32 	QTNF_FW_STATE_ACTIVE,
33 	QTNF_FW_STATE_DETACHED,
34 	QTNF_FW_STATE_EP_DEAD,
35 };
36 
37 struct qtnf_bus;
38 
39 struct qtnf_bus_ops {
40 	/* mgmt methods */
41 	int (*preinit)(struct qtnf_bus *);
42 	void (*stop)(struct qtnf_bus *);
43 
44 	/* control path methods */
45 	int (*control_tx)(struct qtnf_bus *, struct sk_buff *);
46 
47 	/* data xfer methods */
48 	int (*data_tx)(struct qtnf_bus *, struct sk_buff *);
49 	void (*data_tx_timeout)(struct qtnf_bus *, struct net_device *);
50 	void (*data_rx_start)(struct qtnf_bus *);
51 	void (*data_rx_stop)(struct qtnf_bus *);
52 };
53 
54 struct qtnf_bus {
55 	struct device *dev;
56 	enum qtnf_fw_state fw_state;
57 	u32 chip;
58 	u32 chiprev;
59 	const struct qtnf_bus_ops *bus_ops;
60 	struct qtnf_wmac *mac[QTNF_MAX_MAC];
61 	struct qtnf_qlink_transport trans;
62 	struct qtnf_hw_info hw_info;
63 	struct napi_struct mux_napi;
64 	struct net_device mux_dev;
65 	struct workqueue_struct *workqueue;
66 	struct work_struct fw_work;
67 	struct work_struct event_work;
68 	struct mutex bus_lock; /* lock during command/event processing */
69 	struct dentry *dbg_dir;
70 	/* bus private data */
71 	char bus_priv[0] __aligned(sizeof(void *));
72 };
73 
74 static inline void *get_bus_priv(struct qtnf_bus *bus)
75 {
76 	if (WARN(!bus, "qtnfmac: invalid bus pointer"))
77 		return NULL;
78 
79 	return &bus->bus_priv;
80 }
81 
82 /* callback wrappers */
83 
84 static inline int qtnf_bus_preinit(struct qtnf_bus *bus)
85 {
86 	if (!bus->bus_ops->preinit)
87 		return 0;
88 	return bus->bus_ops->preinit(bus);
89 }
90 
91 static inline void qtnf_bus_stop(struct qtnf_bus *bus)
92 {
93 	if (!bus->bus_ops->stop)
94 		return;
95 	bus->bus_ops->stop(bus);
96 }
97 
98 static inline int qtnf_bus_data_tx(struct qtnf_bus *bus, struct sk_buff *skb)
99 {
100 	return bus->bus_ops->data_tx(bus, skb);
101 }
102 
103 static inline void
104 qtnf_bus_data_tx_timeout(struct qtnf_bus *bus, struct net_device *ndev)
105 {
106 	return bus->bus_ops->data_tx_timeout(bus, ndev);
107 }
108 
109 static inline int qtnf_bus_control_tx(struct qtnf_bus *bus, struct sk_buff *skb)
110 {
111 	return bus->bus_ops->control_tx(bus, skb);
112 }
113 
114 static inline void qtnf_bus_data_rx_start(struct qtnf_bus *bus)
115 {
116 	return bus->bus_ops->data_rx_start(bus);
117 }
118 
119 static inline void qtnf_bus_data_rx_stop(struct qtnf_bus *bus)
120 {
121 	return bus->bus_ops->data_rx_stop(bus);
122 }
123 
124 static __always_inline void qtnf_bus_lock(struct qtnf_bus *bus)
125 {
126 	mutex_lock(&bus->bus_lock);
127 }
128 
129 static __always_inline void qtnf_bus_unlock(struct qtnf_bus *bus)
130 {
131 	mutex_unlock(&bus->bus_lock);
132 }
133 
134 /* interface functions from common layer */
135 
136 int qtnf_core_attach(struct qtnf_bus *bus);
137 void qtnf_core_detach(struct qtnf_bus *bus);
138 void qtnf_txflowblock(struct device *dev, bool state);
139 void qtnf_txcomplete(struct device *dev, struct sk_buff *txp, bool success);
140 
141 #endif /* QTNFMAC_BUS_H */
142