if_bwfm_pci.c revision 1.8 1 1.8 jdolecek /* $NetBSD: if_bwfm_pci.c,v 1.8 2020/05/30 13:41:58 jdolecek Exp $ */
2 1.1 maya /* $OpenBSD: if_bwfm_pci.c,v 1.18 2018/02/08 05:00:38 patrick Exp $ */
3 1.1 maya /*
4 1.1 maya * Copyright (c) 2010-2016 Broadcom Corporation
5 1.1 maya * Copyright (c) 2017 Patrick Wildt <patrick (at) blueri.se>
6 1.1 maya *
7 1.1 maya * Permission to use, copy, modify, and/or distribute this software for any
8 1.1 maya * purpose with or without fee is hereby granted, provided that the above
9 1.1 maya * copyright notice and this permission notice appear in all copies.
10 1.1 maya *
11 1.1 maya * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 maya * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 maya * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 maya * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 maya * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 maya * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 maya * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 maya */
19 1.1 maya
20 1.1 maya #include <sys/param.h>
21 1.1 maya #include <sys/systm.h>
22 1.1 maya #include <sys/buf.h>
23 1.1 maya #include <sys/kernel.h>
24 1.1 maya #include <sys/kmem.h>
25 1.1 maya #include <sys/device.h>
26 1.1 maya #include <sys/pool.h>
27 1.1 maya #include <sys/workqueue.h>
28 1.1 maya #include <sys/socket.h>
29 1.1 maya
30 1.1 maya #include <net/bpf.h>
31 1.1 maya #include <net/if.h>
32 1.1 maya #include <net/if_dl.h>
33 1.1 maya #include <net/if_ether.h>
34 1.1 maya #include <net/if_media.h>
35 1.1 maya
36 1.1 maya #include <netinet/in.h>
37 1.1 maya
38 1.1 maya #include <net80211/ieee80211_var.h>
39 1.1 maya
40 1.1 maya #include <dev/pci/pcireg.h>
41 1.1 maya #include <dev/pci/pcivar.h>
42 1.1 maya #include <dev/pci/pcidevs.h>
43 1.1 maya
44 1.1 maya #include <dev/ic/bwfmvar.h>
45 1.1 maya #include <dev/ic/bwfmreg.h>
46 1.1 maya #include <dev/pci/if_bwfm_pci.h>
47 1.1 maya
48 1.1 maya #define BWFM_DMA_D2H_SCRATCH_BUF_LEN 8
49 1.1 maya #define BWFM_DMA_D2H_RINGUPD_BUF_LEN 1024
50 1.1 maya #define BWFM_DMA_H2D_IOCTL_BUF_LEN ETHER_MAX_LEN
51 1.1 maya
52 1.1 maya #define BWFM_NUM_TX_MSGRINGS 2
53 1.1 maya #define BWFM_NUM_RX_MSGRINGS 3
54 1.1 maya
55 1.1 maya #define BWFM_NUM_TX_PKTIDS 2048
56 1.1 maya #define BWFM_NUM_RX_PKTIDS 1024
57 1.1 maya
58 1.1 maya #define BWFM_NUM_TX_DESCS 1
59 1.1 maya #define BWFM_NUM_RX_DESCS 1
60 1.1 maya
61 1.1 maya #ifdef BWFM_DEBUG
62 1.1 maya #define DPRINTF(x) do { if (bwfm_debug > 0) printf x; } while (0)
63 1.1 maya #define DPRINTFN(n, x) do { if (bwfm_debug >= (n)) printf x; } while (0)
64 1.1 maya static int bwfm_debug = 2;
65 1.1 maya #else
66 1.1 maya #define DPRINTF(x) do { ; } while (0)
67 1.1 maya #define DPRINTFN(n, x) do { ; } while (0)
68 1.1 maya #endif
69 1.1 maya
70 1.1 maya #define DEVNAME(sc) device_xname((sc)->sc_sc.sc_dev)
71 1.1 maya #define letoh16 htole16
72 1.1 maya #define letoh32 htole32
73 1.1 maya #define nitems(x) __arraycount(x)
74 1.1 maya
75 1.1 maya enum ring_status {
76 1.1 maya RING_CLOSED,
77 1.1 maya RING_CLOSING,
78 1.1 maya RING_OPEN,
79 1.1 maya RING_OPENING,
80 1.1 maya };
81 1.1 maya
82 1.1 maya struct bwfm_pci_msgring {
83 1.1 maya uint32_t w_idx_addr;
84 1.1 maya uint32_t r_idx_addr;
85 1.1 maya uint32_t w_ptr;
86 1.1 maya uint32_t r_ptr;
87 1.1 maya int nitem;
88 1.1 maya int itemsz;
89 1.1 maya enum ring_status status;
90 1.1 maya struct bwfm_pci_dmamem *ring;
91 1.1 maya struct mbuf *m;
92 1.1 maya
93 1.1 maya int fifo;
94 1.1 maya uint8_t mac[ETHER_ADDR_LEN];
95 1.1 maya };
96 1.1 maya
97 1.1 maya struct bwfm_pci_buf {
98 1.1 maya bus_dmamap_t bb_map;
99 1.1 maya struct mbuf *bb_m;
100 1.1 maya };
101 1.1 maya
102 1.1 maya struct bwfm_pci_pkts {
103 1.1 maya struct bwfm_pci_buf *pkts;
104 1.1 maya uint32_t npkt;
105 1.1 maya int last;
106 1.1 maya };
107 1.1 maya
108 1.1 maya struct if_rxring {
109 1.1 maya u_int rxr_total;
110 1.1 maya u_int rxr_inuse;
111 1.1 maya };
112 1.1 maya
113 1.1 maya struct bwfm_cmd_flowring_create {
114 1.1 maya struct work wq_cookie;
115 1.1 maya struct bwfm_pci_softc *sc;
116 1.1 maya struct mbuf *m;
117 1.1 maya int flowid;
118 1.1 maya int prio;
119 1.1 maya };
120 1.1 maya
121 1.1 maya struct bwfm_pci_softc {
122 1.1 maya struct bwfm_softc sc_sc;
123 1.1 maya pci_chipset_tag_t sc_pc;
124 1.1 maya pcitag_t sc_tag;
125 1.1 maya pcireg_t sc_id;
126 1.1 maya void *sc_ih;
127 1.1 maya pci_intr_handle_t *sc_pihp;
128 1.1 maya
129 1.1 maya bus_space_tag_t sc_reg_iot;
130 1.1 maya bus_space_handle_t sc_reg_ioh;
131 1.1 maya bus_size_t sc_reg_ios;
132 1.1 maya
133 1.1 maya bus_space_tag_t sc_tcm_iot;
134 1.1 maya bus_space_handle_t sc_tcm_ioh;
135 1.1 maya bus_size_t sc_tcm_ios;
136 1.1 maya
137 1.1 maya bus_dma_tag_t sc_dmat;
138 1.1 maya
139 1.1 maya uint32_t sc_shared_address;
140 1.1 maya uint32_t sc_shared_flags;
141 1.1 maya uint8_t sc_shared_version;
142 1.1 maya
143 1.1 maya uint8_t sc_dma_idx_sz;
144 1.1 maya struct bwfm_pci_dmamem *sc_dma_idx_buf;
145 1.1 maya size_t sc_dma_idx_bufsz;
146 1.1 maya
147 1.1 maya uint16_t sc_max_rxbufpost;
148 1.1 maya uint32_t sc_rx_dataoffset;
149 1.1 maya uint32_t sc_htod_mb_data_addr;
150 1.1 maya uint32_t sc_dtoh_mb_data_addr;
151 1.1 maya uint32_t sc_ring_info_addr;
152 1.1 maya
153 1.1 maya uint32_t sc_console_base_addr;
154 1.1 maya uint32_t sc_console_buf_addr;
155 1.1 maya uint32_t sc_console_buf_size;
156 1.1 maya uint32_t sc_console_readidx;
157 1.1 maya
158 1.1 maya struct pool sc_flowring_pool;
159 1.1 maya struct workqueue *flowring_wq;
160 1.1 maya
161 1.1 maya uint16_t sc_max_flowrings;
162 1.1 maya uint16_t sc_max_submissionrings;
163 1.1 maya uint16_t sc_max_completionrings;
164 1.1 maya
165 1.1 maya struct bwfm_pci_msgring sc_ctrl_submit;
166 1.1 maya struct bwfm_pci_msgring sc_rxpost_submit;
167 1.1 maya struct bwfm_pci_msgring sc_ctrl_complete;
168 1.1 maya struct bwfm_pci_msgring sc_tx_complete;
169 1.1 maya struct bwfm_pci_msgring sc_rx_complete;
170 1.1 maya struct bwfm_pci_msgring *sc_flowrings;
171 1.1 maya
172 1.1 maya struct bwfm_pci_dmamem *sc_scratch_buf;
173 1.1 maya struct bwfm_pci_dmamem *sc_ringupd_buf;
174 1.1 maya
175 1.1 maya struct bwfm_pci_dmamem *sc_ioctl_buf;
176 1.1 maya int sc_ioctl_reqid;
177 1.1 maya uint32_t sc_ioctl_resp_pktid;
178 1.1 maya uint32_t sc_ioctl_resp_ret_len;
179 1.1 maya uint32_t sc_ioctl_resp_status;
180 1.1 maya int sc_ioctl_poll;
181 1.1 maya
182 1.1 maya struct if_rxring sc_ioctl_ring;
183 1.1 maya struct if_rxring sc_event_ring;
184 1.1 maya struct if_rxring sc_rxbuf_ring;
185 1.1 maya
186 1.1 maya struct bwfm_pci_pkts sc_rx_pkts;
187 1.1 maya struct bwfm_pci_pkts sc_tx_pkts;
188 1.1 maya int sc_tx_pkts_full;
189 1.1 maya };
190 1.1 maya
191 1.1 maya struct bwfm_pci_dmamem {
192 1.1 maya bus_dmamap_t bdm_map;
193 1.1 maya bus_dma_segment_t bdm_seg;
194 1.1 maya size_t bdm_size;
195 1.1 maya char * bdm_kva;
196 1.1 maya };
197 1.1 maya
198 1.1 maya #define BWFM_PCI_DMA_MAP(_bdm) ((_bdm)->bdm_map)
199 1.1 maya #define BWFM_PCI_DMA_LEN(_bdm) ((_bdm)->bdm_size)
200 1.5 martin #define BWFM_PCI_DMA_DVA(_bdm) (uint64_t)((_bdm)->bdm_map->dm_segs[0].ds_addr)
201 1.1 maya #define BWFM_PCI_DMA_KVA(_bdm) ((_bdm)->bdm_kva)
202 1.1 maya
203 1.1 maya static u_int if_rxr_get(struct if_rxring *rxr, unsigned int max);
204 1.1 maya static void if_rxr_put(struct if_rxring *rxr, unsigned int n);
205 1.1 maya static void if_rxr_init(struct if_rxring *rxr, unsigned int lwm, unsigned int hwm);
206 1.1 maya
207 1.1 maya int bwfm_pci_match(device_t parent, cfdata_t match, void *aux);
208 1.1 maya void bwfm_pci_attachhook(device_t);
209 1.1 maya void bwfm_pci_attach(device_t, device_t, void *);
210 1.1 maya int bwfm_pci_detach(device_t, int);
211 1.1 maya
212 1.1 maya int bwfm_pci_intr(void *);
213 1.1 maya void bwfm_pci_intr_enable(struct bwfm_pci_softc *);
214 1.1 maya void bwfm_pci_intr_disable(struct bwfm_pci_softc *);
215 1.1 maya int bwfm_pci_load_microcode(struct bwfm_pci_softc *, const u_char *,
216 1.1 maya size_t);
217 1.1 maya void bwfm_pci_select_core(struct bwfm_pci_softc *, int );
218 1.1 maya
219 1.1 maya struct bwfm_pci_dmamem *
220 1.1 maya bwfm_pci_dmamem_alloc(struct bwfm_pci_softc *, bus_size_t,
221 1.1 maya bus_size_t);
222 1.1 maya void bwfm_pci_dmamem_free(struct bwfm_pci_softc *, struct bwfm_pci_dmamem *);
223 1.1 maya int bwfm_pci_pktid_avail(struct bwfm_pci_softc *,
224 1.1 maya struct bwfm_pci_pkts *);
225 1.1 maya int bwfm_pci_pktid_new(struct bwfm_pci_softc *,
226 1.2 riastrad struct bwfm_pci_pkts *, struct mbuf **,
227 1.1 maya uint32_t *, paddr_t *);
228 1.1 maya struct mbuf * bwfm_pci_pktid_free(struct bwfm_pci_softc *,
229 1.1 maya struct bwfm_pci_pkts *, uint32_t);
230 1.1 maya void bwfm_pci_fill_rx_ioctl_ring(struct bwfm_pci_softc *,
231 1.1 maya struct if_rxring *, uint32_t);
232 1.1 maya void bwfm_pci_fill_rx_buf_ring(struct bwfm_pci_softc *);
233 1.1 maya void bwfm_pci_fill_rx_rings(struct bwfm_pci_softc *);
234 1.1 maya int bwfm_pci_setup_ring(struct bwfm_pci_softc *, struct bwfm_pci_msgring *,
235 1.1 maya int, size_t, uint32_t, uint32_t, int, uint32_t, uint32_t *);
236 1.1 maya int bwfm_pci_setup_flowring(struct bwfm_pci_softc *, struct bwfm_pci_msgring *,
237 1.1 maya int, size_t);
238 1.1 maya
239 1.1 maya void bwfm_pci_ring_bell(struct bwfm_pci_softc *,
240 1.1 maya struct bwfm_pci_msgring *);
241 1.1 maya void bwfm_pci_ring_update_rptr(struct bwfm_pci_softc *,
242 1.1 maya struct bwfm_pci_msgring *);
243 1.1 maya void bwfm_pci_ring_update_wptr(struct bwfm_pci_softc *,
244 1.1 maya struct bwfm_pci_msgring *);
245 1.1 maya void bwfm_pci_ring_write_rptr(struct bwfm_pci_softc *,
246 1.1 maya struct bwfm_pci_msgring *);
247 1.1 maya void bwfm_pci_ring_write_wptr(struct bwfm_pci_softc *,
248 1.1 maya struct bwfm_pci_msgring *);
249 1.1 maya void * bwfm_pci_ring_write_reserve(struct bwfm_pci_softc *,
250 1.1 maya struct bwfm_pci_msgring *);
251 1.1 maya void * bwfm_pci_ring_write_reserve_multi(struct bwfm_pci_softc *,
252 1.1 maya struct bwfm_pci_msgring *, int, int *);
253 1.1 maya void * bwfm_pci_ring_read_avail(struct bwfm_pci_softc *,
254 1.1 maya struct bwfm_pci_msgring *, int *);
255 1.1 maya void bwfm_pci_ring_read_commit(struct bwfm_pci_softc *,
256 1.1 maya struct bwfm_pci_msgring *, int);
257 1.1 maya void bwfm_pci_ring_write_commit(struct bwfm_pci_softc *,
258 1.1 maya struct bwfm_pci_msgring *);
259 1.1 maya void bwfm_pci_ring_write_cancel(struct bwfm_pci_softc *,
260 1.1 maya struct bwfm_pci_msgring *, int);
261 1.1 maya
262 1.1 maya void bwfm_pci_ring_rx(struct bwfm_pci_softc *,
263 1.1 maya struct bwfm_pci_msgring *);
264 1.1 maya void bwfm_pci_msg_rx(struct bwfm_pci_softc *, void *);
265 1.1 maya
266 1.1 maya uint32_t bwfm_pci_buscore_read(struct bwfm_softc *, uint32_t);
267 1.1 maya void bwfm_pci_buscore_write(struct bwfm_softc *, uint32_t,
268 1.1 maya uint32_t);
269 1.1 maya int bwfm_pci_buscore_prepare(struct bwfm_softc *);
270 1.1 maya int bwfm_pci_buscore_reset(struct bwfm_softc *);
271 1.1 maya void bwfm_pci_buscore_activate(struct bwfm_softc *, const uint32_t);
272 1.1 maya
273 1.1 maya int bwfm_pci_flowring_lookup(struct bwfm_pci_softc *,
274 1.1 maya struct mbuf *);
275 1.1 maya void bwfm_pci_flowring_create(struct bwfm_pci_softc *,
276 1.1 maya struct mbuf *);
277 1.1 maya void bwfm_pci_flowring_create_cb(struct work *, void *);
278 1.1 maya void bwfm_pci_flowring_delete(struct bwfm_pci_softc *, int);
279 1.1 maya
280 1.1 maya void bwfm_pci_stop(struct bwfm_softc *);
281 1.1 maya int bwfm_pci_txcheck(struct bwfm_softc *);
282 1.2 riastrad int bwfm_pci_txdata(struct bwfm_softc *, struct mbuf **);
283 1.1 maya
284 1.1 maya #ifdef BWFM_DEBUG
285 1.1 maya void bwfm_pci_debug_console(struct bwfm_pci_softc *);
286 1.1 maya #endif
287 1.1 maya
288 1.1 maya int bwfm_pci_msgbuf_query_dcmd(struct bwfm_softc *, int,
289 1.1 maya int, char *, size_t *);
290 1.1 maya int bwfm_pci_msgbuf_set_dcmd(struct bwfm_softc *, int,
291 1.1 maya int, char *, size_t);
292 1.1 maya
293 1.8 jdolecek static const struct bwfm_buscore_ops bwfm_pci_buscore_ops = {
294 1.1 maya .bc_read = bwfm_pci_buscore_read,
295 1.1 maya .bc_write = bwfm_pci_buscore_write,
296 1.1 maya .bc_prepare = bwfm_pci_buscore_prepare,
297 1.1 maya .bc_reset = bwfm_pci_buscore_reset,
298 1.1 maya .bc_setup = NULL,
299 1.1 maya .bc_activate = bwfm_pci_buscore_activate,
300 1.1 maya };
301 1.1 maya
302 1.8 jdolecek static const struct bwfm_bus_ops bwfm_pci_bus_ops = {
303 1.1 maya .bs_init = NULL,
304 1.1 maya .bs_stop = bwfm_pci_stop,
305 1.1 maya .bs_txcheck = bwfm_pci_txcheck,
306 1.1 maya .bs_txdata = bwfm_pci_txdata,
307 1.1 maya .bs_txctl = NULL,
308 1.1 maya .bs_rxctl = NULL,
309 1.1 maya };
310 1.1 maya
311 1.8 jdolecek static const struct bwfm_proto_ops bwfm_pci_msgbuf_ops = {
312 1.1 maya .proto_query_dcmd = bwfm_pci_msgbuf_query_dcmd,
313 1.1 maya .proto_set_dcmd = bwfm_pci_msgbuf_set_dcmd,
314 1.1 maya };
315 1.1 maya
316 1.1 maya
317 1.1 maya CFATTACH_DECL_NEW(bwfm_pci, sizeof(struct bwfm_pci_softc),
318 1.1 maya bwfm_pci_match, bwfm_pci_attach, bwfm_pci_detach, NULL);
319 1.1 maya
320 1.7 thorpej static const struct bwfm_firmware_selector bwfm_pci_fwtab[] = {
321 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_43602_CHIP_ID,
322 1.7 thorpej BWFM_FWSEL_ALLREVS, "brcmfmac43602-pcie"),
323 1.7 thorpej
324 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_43465_CHIP_ID,
325 1.7 thorpej BWFM_FWSEL_REV_GE(4), "brcmfmac4366c-pcie"),
326 1.7 thorpej
327 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_4350_CHIP_ID,
328 1.7 thorpej BWFM_FWSEL_REV_LE(7), "brcmfmac4350c2-pcie"),
329 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_4350_CHIP_ID,
330 1.7 thorpej BWFM_FWSEL_REV_GE(8), "brcmfmac4350-pcie"),
331 1.7 thorpej
332 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_43525_CHIP_ID,
333 1.7 thorpej BWFM_FWSEL_REV_GE(4), "brcmfmac4365c-pcie"),
334 1.7 thorpej
335 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_4356_CHIP_ID,
336 1.7 thorpej BWFM_FWSEL_ALLREVS, "brcmfmac4356-pcie"),
337 1.7 thorpej
338 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_43567_CHIP_ID,
339 1.7 thorpej BWFM_FWSEL_ALLREVS, "brcmfmac43570-pcie"),
340 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_43569_CHIP_ID,
341 1.7 thorpej BWFM_FWSEL_ALLREVS, "brcmfmac43570-pcie"),
342 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_43570_CHIP_ID,
343 1.7 thorpej BWFM_FWSEL_ALLREVS, "brcmfmac43570-pcie"),
344 1.7 thorpej
345 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_4358_CHIP_ID,
346 1.7 thorpej BWFM_FWSEL_ALLREVS, "brcmfmac4358-pcie"),
347 1.7 thorpej
348 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_4359_CHIP_ID,
349 1.7 thorpej BWFM_FWSEL_ALLREVS, "brcmfmac4359-pcie"),
350 1.7 thorpej
351 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_4365_CHIP_ID,
352 1.7 thorpej BWFM_FWSEL_REV_LE(3), "brcmfmac4365b-pcie"),
353 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_4365_CHIP_ID,
354 1.7 thorpej BWFM_FWSEL_REV_GE(4), "brcmfmac4365c-pcie"),
355 1.7 thorpej
356 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_4366_CHIP_ID,
357 1.7 thorpej BWFM_FWSEL_REV_LE(3), "brcmfmac4366b-pcie"),
358 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_4366_CHIP_ID,
359 1.7 thorpej BWFM_FWSEL_REV_GE(4), "brcmfmac4366c-pcie"),
360 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_43664_CHIP_ID,
361 1.7 thorpej BWFM_FWSEL_REV_GE(4), "brcmfmac4366c-pcie"),
362 1.7 thorpej
363 1.7 thorpej BWFM_FW_ENTRY(BRCM_CC_4371_CHIP_ID,
364 1.7 thorpej BWFM_FWSEL_ALLREVS, "brcmfmac4371-pcie"),
365 1.7 thorpej
366 1.7 thorpej BWFM_FW_ENTRY_END
367 1.7 thorpej };
368 1.7 thorpej
369 1.1 maya static const struct bwfm_pci_matchid {
370 1.1 maya pci_vendor_id_t bwfm_vendor;
371 1.1 maya pci_product_id_t bwfm_product;
372 1.1 maya } bwfm_pci_devices[] = {
373 1.1 maya { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM43602 },
374 1.1 maya { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4350 },
375 1.1 maya };
376 1.1 maya
377 1.1 maya static struct mbuf *
378 1.1 maya MCLGETI(struct bwfm_pci_softc *sc __unused, int how,
379 1.1 maya struct ifnet *ifp __unused, u_int size)
380 1.1 maya {
381 1.1 maya struct mbuf *m;
382 1.1 maya
383 1.1 maya MGETHDR(m, how, MT_DATA);
384 1.1 maya if (m == NULL)
385 1.1 maya return NULL;
386 1.1 maya
387 1.1 maya MEXTMALLOC(m, size, how);
388 1.1 maya if ((m->m_flags & M_EXT) == 0) {
389 1.1 maya m_freem(m);
390 1.1 maya return NULL;
391 1.1 maya }
392 1.1 maya return m;
393 1.1 maya }
394 1.1 maya
395 1.1 maya int
396 1.1 maya bwfm_pci_match(device_t parent, cfdata_t match, void *aux)
397 1.1 maya {
398 1.1 maya struct pci_attach_args *pa = aux;
399 1.1 maya
400 1.1 maya if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_BROADCOM)
401 1.1 maya return 0;
402 1.1 maya
403 1.1 maya for (size_t i = 0; i < __arraycount(bwfm_pci_devices); i++)
404 1.1 maya if (PCI_PRODUCT(pa->pa_id) == bwfm_pci_devices[i].bwfm_product)
405 1.1 maya return 1;
406 1.1 maya
407 1.1 maya return 0;
408 1.1 maya }
409 1.1 maya
410 1.1 maya void
411 1.1 maya bwfm_pci_attach(device_t parent, device_t self, void *aux)
412 1.1 maya {
413 1.1 maya struct bwfm_pci_softc *sc = device_private(self);
414 1.1 maya struct pci_attach_args *pa = (struct pci_attach_args *)aux;
415 1.1 maya const char *intrstr;
416 1.1 maya char intrbuf[PCI_INTRSTR_LEN];
417 1.1 maya
418 1.1 maya sc->sc_sc.sc_dev = self;
419 1.1 maya
420 1.1 maya if (pci_mapreg_map(pa, PCI_MAPREG_START + 0x00,
421 1.1 maya PCI_MAPREG_MEM_TYPE_64BIT, 0, &sc->sc_reg_iot, &sc->sc_reg_ioh,
422 1.1 maya NULL, &sc->sc_reg_ios)) {
423 1.1 maya printf(": can't map bar0\n");
424 1.1 maya return;
425 1.1 maya }
426 1.1 maya
427 1.1 maya if (pci_mapreg_map(pa, PCI_MAPREG_START + 0x08,
428 1.1 maya PCI_MAPREG_MEM_TYPE_64BIT, 0, &sc->sc_tcm_iot, &sc->sc_tcm_ioh,
429 1.1 maya NULL, &sc->sc_tcm_ios)) {
430 1.1 maya printf(": can't map bar1\n");
431 1.1 maya goto bar0;
432 1.1 maya }
433 1.1 maya
434 1.1 maya sc->sc_pc = pa->pa_pc;
435 1.1 maya sc->sc_tag = pa->pa_tag;
436 1.1 maya sc->sc_id = pa->pa_id;
437 1.1 maya
438 1.1 maya if (pci_dma64_available(pa))
439 1.1 maya sc->sc_dmat = pa->pa_dmat64;
440 1.1 maya else
441 1.1 maya sc->sc_dmat = pa->pa_dmat;
442 1.1 maya
443 1.1 maya /* Map and establish the interrupt. */
444 1.1 maya if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0) != 0) {
445 1.1 maya printf(": couldn't map interrupt\n");
446 1.1 maya goto bar1;
447 1.1 maya }
448 1.1 maya intrstr = pci_intr_string(pa->pa_pc, sc->sc_pihp[0], intrbuf, sizeof(intrbuf));
449 1.1 maya
450 1.6 jdolecek sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, sc->sc_pihp[0], IPL_NET,
451 1.6 jdolecek bwfm_pci_intr, sc, device_xname(self));
452 1.1 maya if (sc->sc_ih == NULL) {
453 1.1 maya printf(": couldn't establish interrupt");
454 1.1 maya if (intrstr != NULL)
455 1.1 maya printf(" at %s", intrstr);
456 1.1 maya printf("\n");
457 1.1 maya goto bar1;
458 1.1 maya }
459 1.1 maya printf(": %s\n", intrstr);
460 1.1 maya
461 1.1 maya config_mountroot(self, bwfm_pci_attachhook);
462 1.1 maya return;
463 1.1 maya
464 1.1 maya bar1:
465 1.1 maya bus_space_unmap(sc->sc_tcm_iot, sc->sc_tcm_ioh, sc->sc_tcm_ios);
466 1.1 maya bar0:
467 1.1 maya bus_space_unmap(sc->sc_reg_iot, sc->sc_reg_ioh, sc->sc_reg_ios);
468 1.1 maya }
469 1.1 maya
470 1.1 maya void
471 1.1 maya bwfm_pci_attachhook(device_t self)
472 1.1 maya {
473 1.1 maya struct bwfm_pci_softc *sc = device_private(self);
474 1.1 maya struct bwfm_softc *bwfm = (void *)sc;
475 1.1 maya struct bwfm_pci_ringinfo ringinfo;
476 1.7 thorpej struct bwfm_firmware_context fwctx;
477 1.7 thorpej uint8_t *ucode;
478 1.7 thorpej size_t ucsize;
479 1.1 maya uint32_t d2h_w_idx_ptr, d2h_r_idx_ptr;
480 1.1 maya uint32_t h2d_w_idx_ptr, h2d_r_idx_ptr;
481 1.1 maya uint32_t idx_offset, reg;
482 1.1 maya int i;
483 1.1 maya
484 1.1 maya sc->sc_sc.sc_buscore_ops = &bwfm_pci_buscore_ops;
485 1.1 maya if (bwfm_chip_attach(&sc->sc_sc) != 0) {
486 1.7 thorpej aprint_error_dev(bwfm->sc_dev, "cannot attach chip\n");
487 1.1 maya return;
488 1.1 maya }
489 1.1 maya
490 1.1 maya bwfm_pci_select_core(sc, BWFM_AGENT_CORE_PCIE2);
491 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
492 1.1 maya BWFM_PCI_PCIE2REG_CONFIGADDR, 0x4e0);
493 1.1 maya reg = bus_space_read_4(sc->sc_reg_iot, sc->sc_reg_ioh,
494 1.1 maya BWFM_PCI_PCIE2REG_CONFIGDATA);
495 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
496 1.1 maya BWFM_PCI_PCIE2REG_CONFIGDATA, reg);
497 1.1 maya
498 1.7 thorpej bwfm_firmware_context_init(&fwctx,
499 1.7 thorpej bwfm->sc_chip.ch_chip, bwfm->sc_chip.ch_chiprev, NULL,
500 1.7 thorpej BWFM_FWREQ(BWFM_FILETYPE_UCODE));
501 1.7 thorpej
502 1.7 thorpej if (!bwfm_firmware_open(bwfm, bwfm_pci_fwtab, &fwctx)) {
503 1.7 thorpej /* Error message already displayed. */
504 1.7 thorpej goto err;
505 1.1 maya }
506 1.1 maya
507 1.7 thorpej ucode = bwfm_firmware_data(&fwctx, BWFM_FILETYPE_UCODE, &ucsize);
508 1.7 thorpej KASSERT(ucode != NULL);
509 1.1 maya
510 1.1 maya /* Retrieve RAM size from firmware. */
511 1.7 thorpej if (ucsize >= BWFM_RAMSIZE + 8) {
512 1.1 maya uint32_t *ramsize = (uint32_t *)&ucode[BWFM_RAMSIZE];
513 1.1 maya if (letoh32(ramsize[0]) == BWFM_RAMSIZE_MAGIC)
514 1.1 maya bwfm->sc_chip.ch_ramsize = letoh32(ramsize[1]);
515 1.1 maya }
516 1.1 maya
517 1.7 thorpej if (bwfm_pci_load_microcode(sc, ucode, ucsize) != 0) {
518 1.7 thorpej aprint_error_dev(bwfm->sc_dev, "could not load microcode\n");
519 1.7 thorpej goto err;
520 1.1 maya }
521 1.1 maya
522 1.7 thorpej bwfm_firmware_close(&fwctx);
523 1.1 maya
524 1.1 maya sc->sc_shared_flags = bus_space_read_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
525 1.1 maya sc->sc_shared_address + BWFM_SHARED_INFO);
526 1.1 maya sc->sc_shared_version = sc->sc_shared_flags;
527 1.1 maya if (sc->sc_shared_version > BWFM_SHARED_INFO_MAX_VERSION ||
528 1.1 maya sc->sc_shared_version < BWFM_SHARED_INFO_MIN_VERSION) {
529 1.7 thorpej aprint_error_dev(bwfm->sc_dev,
530 1.7 thorpej "PCIe version %d unsupported\n", sc->sc_shared_version);
531 1.1 maya return;
532 1.1 maya }
533 1.1 maya
534 1.1 maya if (sc->sc_shared_flags & BWFM_SHARED_INFO_DMA_INDEX) {
535 1.1 maya if (sc->sc_shared_flags & BWFM_SHARED_INFO_DMA_2B_IDX)
536 1.1 maya sc->sc_dma_idx_sz = sizeof(uint16_t);
537 1.1 maya else
538 1.1 maya sc->sc_dma_idx_sz = sizeof(uint32_t);
539 1.1 maya }
540 1.1 maya
541 1.1 maya /* Maximum RX data buffers in the ring. */
542 1.1 maya sc->sc_max_rxbufpost = bus_space_read_2(sc->sc_tcm_iot, sc->sc_tcm_ioh,
543 1.1 maya sc->sc_shared_address + BWFM_SHARED_MAX_RXBUFPOST);
544 1.1 maya if (sc->sc_max_rxbufpost == 0)
545 1.1 maya sc->sc_max_rxbufpost = BWFM_SHARED_MAX_RXBUFPOST_DEFAULT;
546 1.1 maya
547 1.1 maya /* Alternative offset of data in a packet */
548 1.1 maya sc->sc_rx_dataoffset = bus_space_read_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
549 1.1 maya sc->sc_shared_address + BWFM_SHARED_RX_DATAOFFSET);
550 1.1 maya
551 1.1 maya /* For Power Management */
552 1.1 maya sc->sc_htod_mb_data_addr = bus_space_read_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
553 1.1 maya sc->sc_shared_address + BWFM_SHARED_HTOD_MB_DATA_ADDR);
554 1.1 maya sc->sc_dtoh_mb_data_addr = bus_space_read_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
555 1.1 maya sc->sc_shared_address + BWFM_SHARED_DTOH_MB_DATA_ADDR);
556 1.1 maya
557 1.1 maya /* Ring information */
558 1.1 maya sc->sc_ring_info_addr = bus_space_read_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
559 1.1 maya sc->sc_shared_address + BWFM_SHARED_RING_INFO_ADDR);
560 1.1 maya
561 1.1 maya /* Firmware's "dmesg" */
562 1.1 maya sc->sc_console_base_addr = bus_space_read_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
563 1.1 maya sc->sc_shared_address + BWFM_SHARED_CONSOLE_ADDR);
564 1.1 maya sc->sc_console_buf_addr = bus_space_read_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
565 1.1 maya sc->sc_console_base_addr + BWFM_CONSOLE_BUFADDR);
566 1.1 maya sc->sc_console_buf_size = bus_space_read_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
567 1.1 maya sc->sc_console_base_addr + BWFM_CONSOLE_BUFSIZE);
568 1.1 maya
569 1.1 maya /* Read ring information. */
570 1.1 maya bus_space_read_region_1(sc->sc_tcm_iot, sc->sc_tcm_ioh,
571 1.1 maya sc->sc_ring_info_addr, (void *)&ringinfo, sizeof(ringinfo));
572 1.1 maya
573 1.1 maya if (sc->sc_shared_version >= 6) {
574 1.1 maya sc->sc_max_submissionrings = le16toh(ringinfo.max_submissionrings);
575 1.1 maya sc->sc_max_flowrings = le16toh(ringinfo.max_flowrings);
576 1.1 maya sc->sc_max_completionrings = le16toh(ringinfo.max_completionrings);
577 1.1 maya } else {
578 1.1 maya sc->sc_max_submissionrings = le16toh(ringinfo.max_flowrings);
579 1.1 maya sc->sc_max_flowrings = sc->sc_max_submissionrings -
580 1.1 maya BWFM_NUM_TX_MSGRINGS;
581 1.1 maya sc->sc_max_completionrings = BWFM_NUM_RX_MSGRINGS;
582 1.1 maya }
583 1.1 maya
584 1.1 maya if (sc->sc_dma_idx_sz == 0) {
585 1.1 maya d2h_w_idx_ptr = letoh32(ringinfo.d2h_w_idx_ptr);
586 1.1 maya d2h_r_idx_ptr = letoh32(ringinfo.d2h_r_idx_ptr);
587 1.1 maya h2d_w_idx_ptr = letoh32(ringinfo.h2d_w_idx_ptr);
588 1.1 maya h2d_r_idx_ptr = letoh32(ringinfo.h2d_r_idx_ptr);
589 1.1 maya idx_offset = sizeof(uint32_t);
590 1.1 maya } else {
591 1.1 maya uint64_t address;
592 1.1 maya
593 1.1 maya /* Each TX/RX Ring has a Read and Write Ptr */
594 1.1 maya sc->sc_dma_idx_bufsz = (sc->sc_max_submissionrings +
595 1.1 maya sc->sc_max_completionrings) * sc->sc_dma_idx_sz * 2;
596 1.1 maya sc->sc_dma_idx_buf = bwfm_pci_dmamem_alloc(sc,
597 1.1 maya sc->sc_dma_idx_bufsz, 8);
598 1.1 maya if (sc->sc_dma_idx_buf == NULL) {
599 1.1 maya /* XXX: Fallback to TCM? */
600 1.7 thorpej aprint_error_dev(bwfm->sc_dev,
601 1.7 thorpej "cannot allocate idx buf\n");
602 1.1 maya return;
603 1.1 maya }
604 1.1 maya
605 1.1 maya idx_offset = sc->sc_dma_idx_sz;
606 1.1 maya h2d_w_idx_ptr = 0;
607 1.1 maya address = BWFM_PCI_DMA_DVA(sc->sc_dma_idx_buf);
608 1.1 maya ringinfo.h2d_w_idx_hostaddr_low =
609 1.1 maya htole32(address & 0xffffffff);
610 1.1 maya ringinfo.h2d_w_idx_hostaddr_high =
611 1.1 maya htole32(address >> 32);
612 1.1 maya
613 1.1 maya h2d_r_idx_ptr = h2d_w_idx_ptr +
614 1.1 maya sc->sc_max_submissionrings * idx_offset;
615 1.1 maya address += sc->sc_max_submissionrings * idx_offset;
616 1.1 maya ringinfo.h2d_r_idx_hostaddr_low =
617 1.1 maya htole32(address & 0xffffffff);
618 1.1 maya ringinfo.h2d_r_idx_hostaddr_high =
619 1.1 maya htole32(address >> 32);
620 1.1 maya
621 1.1 maya d2h_w_idx_ptr = h2d_r_idx_ptr +
622 1.1 maya sc->sc_max_submissionrings * idx_offset;
623 1.1 maya address += sc->sc_max_submissionrings * idx_offset;
624 1.1 maya ringinfo.d2h_w_idx_hostaddr_low =
625 1.1 maya htole32(address & 0xffffffff);
626 1.1 maya ringinfo.d2h_w_idx_hostaddr_high =
627 1.1 maya htole32(address >> 32);
628 1.1 maya
629 1.1 maya d2h_r_idx_ptr = d2h_w_idx_ptr +
630 1.1 maya sc->sc_max_completionrings * idx_offset;
631 1.1 maya address += sc->sc_max_completionrings * idx_offset;
632 1.1 maya ringinfo.d2h_r_idx_hostaddr_low =
633 1.1 maya htole32(address & 0xffffffff);
634 1.1 maya ringinfo.d2h_r_idx_hostaddr_high =
635 1.1 maya htole32(address >> 32);
636 1.1 maya
637 1.1 maya bus_space_write_region_1(sc->sc_tcm_iot, sc->sc_tcm_ioh,
638 1.1 maya sc->sc_ring_info_addr, (void *)&ringinfo, sizeof(ringinfo));
639 1.1 maya }
640 1.1 maya
641 1.1 maya uint32_t ring_mem_ptr = letoh32(ringinfo.ringmem);
642 1.1 maya /* TX ctrl ring: Send ctrl buffers, send IOCTLs */
643 1.1 maya if (bwfm_pci_setup_ring(sc, &sc->sc_ctrl_submit, 64, 40,
644 1.1 maya h2d_w_idx_ptr, h2d_r_idx_ptr, 0, idx_offset,
645 1.1 maya &ring_mem_ptr))
646 1.1 maya goto cleanup;
647 1.1 maya /* TX rxpost ring: Send clean data mbufs for RX */
648 1.1 maya if (bwfm_pci_setup_ring(sc, &sc->sc_rxpost_submit, 512, 32,
649 1.1 maya h2d_w_idx_ptr, h2d_r_idx_ptr, 1, idx_offset,
650 1.1 maya &ring_mem_ptr))
651 1.1 maya goto cleanup;
652 1.1 maya /* RX completion rings: recv our filled buffers back */
653 1.1 maya if (bwfm_pci_setup_ring(sc, &sc->sc_ctrl_complete, 64, 24,
654 1.1 maya d2h_w_idx_ptr, d2h_r_idx_ptr, 0, idx_offset,
655 1.1 maya &ring_mem_ptr))
656 1.1 maya goto cleanup;
657 1.1 maya if (bwfm_pci_setup_ring(sc, &sc->sc_tx_complete, 1024, 16,
658 1.1 maya d2h_w_idx_ptr, d2h_r_idx_ptr, 1, idx_offset,
659 1.1 maya &ring_mem_ptr))
660 1.1 maya goto cleanup;
661 1.1 maya if (bwfm_pci_setup_ring(sc, &sc->sc_rx_complete, 512, 32,
662 1.1 maya d2h_w_idx_ptr, d2h_r_idx_ptr, 2, idx_offset,
663 1.1 maya &ring_mem_ptr))
664 1.1 maya goto cleanup;
665 1.1 maya
666 1.1 maya /* Dynamic TX rings for actual data */
667 1.1 maya sc->sc_flowrings = kmem_zalloc(sc->sc_max_flowrings *
668 1.1 maya sizeof(struct bwfm_pci_msgring), KM_SLEEP);
669 1.1 maya for (i = 0; i < sc->sc_max_flowrings; i++) {
670 1.1 maya struct bwfm_pci_msgring *ring = &sc->sc_flowrings[i];
671 1.1 maya ring->w_idx_addr = h2d_w_idx_ptr + (i + 2) * idx_offset;
672 1.1 maya ring->r_idx_addr = h2d_r_idx_ptr + (i + 2) * idx_offset;
673 1.1 maya }
674 1.1 maya
675 1.1 maya pool_init(&sc->sc_flowring_pool, sizeof(struct bwfm_cmd_flowring_create),
676 1.1 maya 0, 0, 0, "bwfmpl", NULL, IPL_NET);
677 1.1 maya
678 1.1 maya /* Scratch and ring update buffers for firmware */
679 1.1 maya if ((sc->sc_scratch_buf = bwfm_pci_dmamem_alloc(sc,
680 1.1 maya BWFM_DMA_D2H_SCRATCH_BUF_LEN, 8)) == NULL)
681 1.1 maya goto cleanup;
682 1.1 maya bus_space_write_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
683 1.1 maya sc->sc_shared_address + BWFM_SHARED_DMA_SCRATCH_ADDR_LOW,
684 1.1 maya BWFM_PCI_DMA_DVA(sc->sc_scratch_buf) & 0xffffffff);
685 1.1 maya bus_space_write_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
686 1.1 maya sc->sc_shared_address + BWFM_SHARED_DMA_SCRATCH_ADDR_HIGH,
687 1.1 maya BWFM_PCI_DMA_DVA(sc->sc_scratch_buf) >> 32);
688 1.1 maya bus_space_write_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
689 1.1 maya sc->sc_shared_address + BWFM_SHARED_DMA_SCRATCH_LEN,
690 1.1 maya BWFM_DMA_D2H_SCRATCH_BUF_LEN);
691 1.1 maya
692 1.1 maya if ((sc->sc_ringupd_buf = bwfm_pci_dmamem_alloc(sc,
693 1.1 maya BWFM_DMA_D2H_RINGUPD_BUF_LEN, 8)) == NULL)
694 1.1 maya goto cleanup;
695 1.1 maya bus_space_write_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
696 1.1 maya sc->sc_shared_address + BWFM_SHARED_DMA_RINGUPD_ADDR_LOW,
697 1.1 maya BWFM_PCI_DMA_DVA(sc->sc_ringupd_buf) & 0xffffffff);
698 1.1 maya bus_space_write_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
699 1.1 maya sc->sc_shared_address + BWFM_SHARED_DMA_RINGUPD_ADDR_HIGH,
700 1.1 maya BWFM_PCI_DMA_DVA(sc->sc_ringupd_buf) >> 32);
701 1.1 maya bus_space_write_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
702 1.1 maya sc->sc_shared_address + BWFM_SHARED_DMA_RINGUPD_LEN,
703 1.1 maya BWFM_DMA_D2H_RINGUPD_BUF_LEN);
704 1.1 maya
705 1.1 maya if ((sc->sc_ioctl_buf = bwfm_pci_dmamem_alloc(sc,
706 1.1 maya BWFM_DMA_H2D_IOCTL_BUF_LEN, 8)) == NULL)
707 1.1 maya goto cleanup;
708 1.1 maya
709 1.1 maya if (workqueue_create(&sc->flowring_wq, "bwfmflow",
710 1.1 maya bwfm_pci_flowring_create_cb, sc, PRI_SOFTNET, IPL_NET, 0))
711 1.1 maya goto cleanup;
712 1.1 maya
713 1.1 maya bwfm_pci_select_core(sc, BWFM_AGENT_CORE_PCIE2);
714 1.1 maya bwfm_pci_intr_enable(sc);
715 1.1 maya
716 1.1 maya /* Maps RX mbufs to a packet id and back. */
717 1.1 maya sc->sc_rx_pkts.npkt = BWFM_NUM_RX_PKTIDS;
718 1.1 maya sc->sc_rx_pkts.pkts = kmem_zalloc(BWFM_NUM_RX_PKTIDS *
719 1.1 maya sizeof(struct bwfm_pci_buf), KM_SLEEP);
720 1.1 maya for (i = 0; i < BWFM_NUM_RX_PKTIDS; i++)
721 1.1 maya bus_dmamap_create(sc->sc_dmat, MSGBUF_MAX_PKT_SIZE,
722 1.1 maya BWFM_NUM_RX_DESCS, MSGBUF_MAX_PKT_SIZE, 0, BUS_DMA_WAITOK,
723 1.1 maya &sc->sc_rx_pkts.pkts[i].bb_map);
724 1.1 maya
725 1.1 maya /* Maps TX mbufs to a packet id and back. */
726 1.1 maya sc->sc_tx_pkts.npkt = BWFM_NUM_TX_PKTIDS;
727 1.1 maya sc->sc_tx_pkts.pkts = kmem_zalloc(BWFM_NUM_TX_PKTIDS
728 1.1 maya * sizeof(struct bwfm_pci_buf), KM_SLEEP);
729 1.1 maya for (i = 0; i < BWFM_NUM_TX_PKTIDS; i++)
730 1.1 maya bus_dmamap_create(sc->sc_dmat, MSGBUF_MAX_PKT_SIZE,
731 1.1 maya BWFM_NUM_TX_DESCS, MSGBUF_MAX_PKT_SIZE, 0, BUS_DMA_WAITOK,
732 1.1 maya &sc->sc_tx_pkts.pkts[i].bb_map);
733 1.1 maya
734 1.1 maya /*
735 1.1 maya * For whatever reason, could also be a bug somewhere in this
736 1.1 maya * driver, the firmware needs a bunch of RX buffers otherwise
737 1.1 maya * it won't send any RX complete messages. 64 buffers don't
738 1.1 maya * suffice, but 128 buffers are enough.
739 1.1 maya */
740 1.1 maya if_rxr_init(&sc->sc_rxbuf_ring, 128, sc->sc_max_rxbufpost);
741 1.1 maya if_rxr_init(&sc->sc_ioctl_ring, 8, 8);
742 1.1 maya if_rxr_init(&sc->sc_event_ring, 8, 8);
743 1.1 maya bwfm_pci_fill_rx_rings(sc);
744 1.1 maya
745 1.1 maya
746 1.1 maya #ifdef BWFM_DEBUG
747 1.1 maya sc->sc_console_readidx = 0;
748 1.1 maya bwfm_pci_debug_console(sc);
749 1.1 maya #endif
750 1.1 maya
751 1.1 maya sc->sc_ioctl_poll = 1;
752 1.1 maya sc->sc_sc.sc_bus_ops = &bwfm_pci_bus_ops;
753 1.1 maya sc->sc_sc.sc_proto_ops = &bwfm_pci_msgbuf_ops;
754 1.1 maya bwfm_attach(&sc->sc_sc);
755 1.1 maya sc->sc_ioctl_poll = 0;
756 1.1 maya return;
757 1.1 maya
758 1.1 maya cleanup:
759 1.1 maya if (sc->flowring_wq != NULL)
760 1.1 maya workqueue_destroy(sc->flowring_wq);
761 1.1 maya if (sc->sc_ih != NULL) {
762 1.1 maya pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
763 1.1 maya pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
764 1.1 maya }
765 1.1 maya if (sc->sc_ioctl_buf)
766 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_ioctl_buf);
767 1.1 maya if (sc->sc_ringupd_buf)
768 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_ringupd_buf);
769 1.1 maya if (sc->sc_scratch_buf)
770 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_scratch_buf);
771 1.1 maya if (sc->sc_rx_complete.ring)
772 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_rx_complete.ring);
773 1.1 maya if (sc->sc_tx_complete.ring)
774 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_tx_complete.ring);
775 1.1 maya if (sc->sc_ctrl_complete.ring)
776 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_ctrl_complete.ring);
777 1.1 maya if (sc->sc_rxpost_submit.ring)
778 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_rxpost_submit.ring);
779 1.1 maya if (sc->sc_ctrl_submit.ring)
780 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_ctrl_submit.ring);
781 1.1 maya if (sc->sc_dma_idx_buf)
782 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_dma_idx_buf);
783 1.7 thorpej
784 1.7 thorpej err:
785 1.7 thorpej bwfm_firmware_close(&fwctx);
786 1.1 maya }
787 1.1 maya
788 1.1 maya int
789 1.1 maya bwfm_pci_load_microcode(struct bwfm_pci_softc *sc, const u_char *ucode, size_t size)
790 1.1 maya {
791 1.1 maya struct bwfm_softc *bwfm = (void *)sc;
792 1.1 maya struct bwfm_core *core;
793 1.1 maya uint32_t shared;
794 1.1 maya int i;
795 1.1 maya
796 1.1 maya if (bwfm->sc_chip.ch_chip == BRCM_CC_43602_CHIP_ID) {
797 1.1 maya bwfm_pci_select_core(sc, BWFM_AGENT_CORE_ARM_CR4);
798 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
799 1.1 maya BWFM_PCI_ARMCR4REG_BANKIDX, 5);
800 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
801 1.1 maya BWFM_PCI_ARMCR4REG_BANKPDA, 0);
802 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
803 1.1 maya BWFM_PCI_ARMCR4REG_BANKIDX, 7);
804 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
805 1.1 maya BWFM_PCI_ARMCR4REG_BANKPDA, 0);
806 1.1 maya }
807 1.1 maya
808 1.1 maya for (i = 0; i < size; i++)
809 1.1 maya bus_space_write_1(sc->sc_tcm_iot, sc->sc_tcm_ioh,
810 1.1 maya bwfm->sc_chip.ch_rambase + i, ucode[i]);
811 1.1 maya
812 1.1 maya /* Firmware replaces this with a pointer once up. */
813 1.1 maya bus_space_write_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
814 1.1 maya bwfm->sc_chip.ch_rambase + bwfm->sc_chip.ch_ramsize - 4, 0);
815 1.1 maya
816 1.1 maya /* TODO: restore NVRAM */
817 1.1 maya
818 1.1 maya /* Load reset vector from firmware and kickstart core. */
819 1.1 maya if (bwfm->sc_chip.ch_chip == BRCM_CC_43602_CHIP_ID) {
820 1.1 maya core = bwfm_chip_get_core(bwfm, BWFM_AGENT_INTERNAL_MEM);
821 1.1 maya bwfm->sc_chip.ch_core_reset(bwfm, core, 0, 0, 0);
822 1.1 maya }
823 1.1 maya bwfm_chip_set_active(bwfm, *(const uint32_t *)ucode);
824 1.1 maya
825 1.1 maya for (i = 0; i < 40; i++) {
826 1.1 maya delay(50 * 1000);
827 1.1 maya shared = bus_space_read_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
828 1.1 maya bwfm->sc_chip.ch_rambase + bwfm->sc_chip.ch_ramsize - 4);
829 1.1 maya if (shared)
830 1.1 maya break;
831 1.1 maya }
832 1.1 maya if (!shared) {
833 1.1 maya printf("%s: firmware did not come up\n", DEVNAME(sc));
834 1.1 maya return 1;
835 1.1 maya }
836 1.1 maya
837 1.1 maya sc->sc_shared_address = shared;
838 1.1 maya return 0;
839 1.1 maya }
840 1.1 maya
841 1.1 maya int
842 1.1 maya bwfm_pci_detach(device_t self, int flags)
843 1.1 maya {
844 1.1 maya struct bwfm_pci_softc *sc = device_private(self);
845 1.1 maya
846 1.1 maya bwfm_detach(&sc->sc_sc, flags);
847 1.1 maya
848 1.1 maya /* FIXME: free RX buffers */
849 1.1 maya /* FIXME: free TX buffers */
850 1.1 maya /* FIXME: free more memory */
851 1.1 maya
852 1.1 maya kmem_free(sc->sc_flowrings, sc->sc_max_flowrings
853 1.1 maya * sizeof(struct bwfm_pci_msgring));
854 1.1 maya pool_destroy(&sc->sc_flowring_pool);
855 1.1 maya
856 1.1 maya workqueue_destroy(sc->flowring_wq);
857 1.1 maya pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
858 1.1 maya pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
859 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_ioctl_buf);
860 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_ringupd_buf);
861 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_scratch_buf);
862 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_rx_complete.ring);
863 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_tx_complete.ring);
864 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_ctrl_complete.ring);
865 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_rxpost_submit.ring);
866 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_ctrl_submit.ring);
867 1.1 maya bwfm_pci_dmamem_free(sc, sc->sc_dma_idx_buf);
868 1.1 maya return 0;
869 1.1 maya }
870 1.1 maya
871 1.1 maya /* DMA code */
872 1.1 maya struct bwfm_pci_dmamem *
873 1.1 maya bwfm_pci_dmamem_alloc(struct bwfm_pci_softc *sc, bus_size_t size, bus_size_t align)
874 1.1 maya {
875 1.1 maya struct bwfm_pci_dmamem *bdm;
876 1.1 maya int nsegs;
877 1.1 maya
878 1.1 maya bdm = kmem_zalloc(sizeof(*bdm), KM_SLEEP);
879 1.1 maya bdm->bdm_size = size;
880 1.1 maya
881 1.1 maya if (bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
882 1.1 maya BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &bdm->bdm_map) != 0)
883 1.1 maya goto bdmfree;
884 1.1 maya
885 1.1 maya if (bus_dmamem_alloc(sc->sc_dmat, size, align, 0, &bdm->bdm_seg, 1,
886 1.1 maya &nsegs, BUS_DMA_WAITOK) != 0)
887 1.1 maya goto destroy;
888 1.1 maya
889 1.1 maya if (bus_dmamem_map(sc->sc_dmat, &bdm->bdm_seg, nsegs, size,
890 1.1 maya (void **) &bdm->bdm_kva, BUS_DMA_WAITOK | BUS_DMA_COHERENT) != 0)
891 1.1 maya goto free;
892 1.1 maya
893 1.1 maya if (bus_dmamap_load(sc->sc_dmat, bdm->bdm_map, bdm->bdm_kva, size,
894 1.1 maya NULL, BUS_DMA_WAITOK) != 0)
895 1.1 maya goto unmap;
896 1.1 maya
897 1.1 maya bzero(bdm->bdm_kva, size);
898 1.1 maya
899 1.1 maya return (bdm);
900 1.1 maya
901 1.1 maya unmap:
902 1.1 maya bus_dmamem_unmap(sc->sc_dmat, bdm->bdm_kva, size);
903 1.1 maya free:
904 1.1 maya bus_dmamem_free(sc->sc_dmat, &bdm->bdm_seg, 1);
905 1.1 maya destroy:
906 1.1 maya bus_dmamap_destroy(sc->sc_dmat, bdm->bdm_map);
907 1.1 maya bdmfree:
908 1.1 maya kmem_free(bdm, sizeof(*bdm));
909 1.1 maya
910 1.1 maya return (NULL);
911 1.1 maya }
912 1.1 maya
913 1.1 maya void
914 1.1 maya bwfm_pci_dmamem_free(struct bwfm_pci_softc *sc, struct bwfm_pci_dmamem *bdm)
915 1.1 maya {
916 1.1 maya bus_dmamem_unmap(sc->sc_dmat, bdm->bdm_kva, bdm->bdm_size);
917 1.1 maya bus_dmamem_free(sc->sc_dmat, &bdm->bdm_seg, 1);
918 1.1 maya bus_dmamap_destroy(sc->sc_dmat, bdm->bdm_map);
919 1.1 maya kmem_free(bdm, sizeof(*bdm));
920 1.1 maya }
921 1.1 maya
922 1.1 maya /*
923 1.1 maya * We need a simple mapping from a packet ID to mbufs, because when
924 1.1 maya * a transfer completed, we only know the ID so we have to look up
925 1.1 maya * the memory for the ID. This simply looks for an empty slot.
926 1.1 maya */
927 1.1 maya int
928 1.1 maya bwfm_pci_pktid_avail(struct bwfm_pci_softc *sc, struct bwfm_pci_pkts *pkts)
929 1.1 maya {
930 1.1 maya int i, idx;
931 1.1 maya
932 1.1 maya idx = pkts->last + 1;
933 1.1 maya for (i = 0; i < pkts->npkt; i++) {
934 1.1 maya if (idx == pkts->npkt)
935 1.1 maya idx = 0;
936 1.1 maya if (pkts->pkts[idx].bb_m == NULL)
937 1.1 maya return 0;
938 1.1 maya idx++;
939 1.1 maya }
940 1.1 maya return ENOBUFS;
941 1.1 maya }
942 1.1 maya
943 1.1 maya int
944 1.1 maya bwfm_pci_pktid_new(struct bwfm_pci_softc *sc, struct bwfm_pci_pkts *pkts,
945 1.2 riastrad struct mbuf **mp, uint32_t *pktid, paddr_t *paddr)
946 1.1 maya {
947 1.1 maya int i, idx;
948 1.1 maya
949 1.1 maya idx = pkts->last + 1;
950 1.1 maya for (i = 0; i < pkts->npkt; i++) {
951 1.1 maya if (idx == pkts->npkt)
952 1.1 maya idx = 0;
953 1.1 maya if (pkts->pkts[idx].bb_m == NULL) {
954 1.1 maya if (bus_dmamap_load_mbuf(sc->sc_dmat,
955 1.2 riastrad pkts->pkts[idx].bb_map, *mp, BUS_DMA_NOWAIT) != 0) {
956 1.2 riastrad /*
957 1.2 riastrad * Didn't fit. Maybe it has too many
958 1.2 riastrad * segments. If it has only one
959 1.2 riastrad * segment, fail; otherwise try to
960 1.2 riastrad * compact it into a single mbuf
961 1.2 riastrad * segment.
962 1.2 riastrad */
963 1.2 riastrad if ((*mp)->m_next == NULL)
964 1.2 riastrad return ENOBUFS;
965 1.2 riastrad struct mbuf *m0 = MCLGETI(NULL, M_DONTWAIT,
966 1.2 riastrad NULL, MSGBUF_MAX_PKT_SIZE);
967 1.2 riastrad if (m0 == NULL)
968 1.2 riastrad return ENOBUFS;
969 1.2 riastrad m_copydata(*mp, 0, (*mp)->m_pkthdr.len,
970 1.2 riastrad mtod(m0, void *));
971 1.2 riastrad m0->m_pkthdr.len = m0->m_len =
972 1.2 riastrad (*mp)->m_pkthdr.len;
973 1.2 riastrad m_freem(*mp);
974 1.2 riastrad *mp = m0;
975 1.1 maya if (bus_dmamap_load_mbuf(sc->sc_dmat,
976 1.2 riastrad pkts->pkts[idx].bb_map, *mp, BUS_DMA_NOWAIT) != 0)
977 1.1 maya return EFBIG;
978 1.1 maya }
979 1.4 maya bus_dmamap_sync(sc->sc_dmat, pkts->pkts[idx].bb_map,
980 1.4 maya 0, pkts->pkts[idx].bb_map->dm_mapsize,
981 1.4 maya BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
982 1.1 maya pkts->last = idx;
983 1.2 riastrad pkts->pkts[idx].bb_m = *mp;
984 1.1 maya *pktid = idx;
985 1.1 maya *paddr = pkts->pkts[idx].bb_map->dm_segs[0].ds_addr;
986 1.1 maya return 0;
987 1.1 maya }
988 1.1 maya idx++;
989 1.1 maya }
990 1.1 maya return ENOBUFS;
991 1.1 maya }
992 1.1 maya
993 1.1 maya struct mbuf *
994 1.1 maya bwfm_pci_pktid_free(struct bwfm_pci_softc *sc, struct bwfm_pci_pkts *pkts,
995 1.1 maya uint32_t pktid)
996 1.1 maya {
997 1.1 maya struct mbuf *m;
998 1.1 maya
999 1.1 maya if (pktid >= pkts->npkt || pkts->pkts[pktid].bb_m == NULL)
1000 1.1 maya return NULL;
1001 1.4 maya bus_dmamap_sync(sc->sc_dmat, pkts->pkts[pktid].bb_map, 0,
1002 1.4 maya pkts->pkts[pktid].bb_map->dm_mapsize,
1003 1.4 maya BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1004 1.1 maya bus_dmamap_unload(sc->sc_dmat, pkts->pkts[pktid].bb_map);
1005 1.1 maya m = pkts->pkts[pktid].bb_m;
1006 1.1 maya pkts->pkts[pktid].bb_m = NULL;
1007 1.1 maya return m;
1008 1.1 maya }
1009 1.1 maya
1010 1.1 maya void
1011 1.1 maya bwfm_pci_fill_rx_rings(struct bwfm_pci_softc *sc)
1012 1.1 maya {
1013 1.1 maya bwfm_pci_fill_rx_buf_ring(sc);
1014 1.1 maya bwfm_pci_fill_rx_ioctl_ring(sc, &sc->sc_ioctl_ring,
1015 1.1 maya MSGBUF_TYPE_IOCTLRESP_BUF_POST);
1016 1.1 maya bwfm_pci_fill_rx_ioctl_ring(sc, &sc->sc_event_ring,
1017 1.1 maya MSGBUF_TYPE_EVENT_BUF_POST);
1018 1.1 maya }
1019 1.1 maya
1020 1.1 maya void
1021 1.1 maya bwfm_pci_fill_rx_ioctl_ring(struct bwfm_pci_softc *sc, struct if_rxring *rxring,
1022 1.1 maya uint32_t msgtype)
1023 1.1 maya {
1024 1.1 maya struct msgbuf_rx_ioctl_resp_or_event *req;
1025 1.1 maya struct mbuf *m;
1026 1.1 maya uint32_t pktid;
1027 1.1 maya paddr_t paddr;
1028 1.1 maya int s, slots;
1029 1.5 martin uint64_t devaddr;
1030 1.1 maya
1031 1.1 maya s = splnet();
1032 1.1 maya for (slots = if_rxr_get(rxring, 8); slots > 0; slots--) {
1033 1.1 maya if (bwfm_pci_pktid_avail(sc, &sc->sc_rx_pkts))
1034 1.1 maya break;
1035 1.1 maya req = bwfm_pci_ring_write_reserve(sc, &sc->sc_ctrl_submit);
1036 1.1 maya if (req == NULL)
1037 1.1 maya break;
1038 1.1 maya m = MCLGETI(NULL, M_DONTWAIT, NULL, MSGBUF_MAX_PKT_SIZE);
1039 1.1 maya if (m == NULL) {
1040 1.1 maya bwfm_pci_ring_write_cancel(sc, &sc->sc_ctrl_submit, 1);
1041 1.1 maya break;
1042 1.1 maya }
1043 1.1 maya m->m_len = m->m_pkthdr.len = MSGBUF_MAX_PKT_SIZE;
1044 1.2 riastrad if (bwfm_pci_pktid_new(sc, &sc->sc_rx_pkts, &m, &pktid, &paddr)) {
1045 1.1 maya bwfm_pci_ring_write_cancel(sc, &sc->sc_ctrl_submit, 1);
1046 1.1 maya m_freem(m);
1047 1.1 maya break;
1048 1.1 maya }
1049 1.5 martin devaddr = paddr;
1050 1.1 maya memset(req, 0, sizeof(*req));
1051 1.1 maya req->msg.msgtype = msgtype;
1052 1.1 maya req->msg.request_id = htole32(pktid);
1053 1.1 maya req->host_buf_len = htole16(MSGBUF_MAX_PKT_SIZE);
1054 1.5 martin req->host_buf_addr.high_addr = htole32(devaddr >> 32);
1055 1.5 martin req->host_buf_addr.low_addr = htole32(devaddr & 0xffffffff);
1056 1.1 maya bwfm_pci_ring_write_commit(sc, &sc->sc_ctrl_submit);
1057 1.1 maya }
1058 1.1 maya if_rxr_put(rxring, slots);
1059 1.1 maya splx(s);
1060 1.1 maya }
1061 1.1 maya
1062 1.1 maya void
1063 1.1 maya bwfm_pci_fill_rx_buf_ring(struct bwfm_pci_softc *sc)
1064 1.1 maya {
1065 1.1 maya struct msgbuf_rx_bufpost *req;
1066 1.1 maya struct mbuf *m;
1067 1.1 maya uint32_t pktid;
1068 1.1 maya paddr_t paddr;
1069 1.1 maya int s, slots;
1070 1.5 martin uint64_t devaddr;
1071 1.1 maya
1072 1.1 maya s = splnet();
1073 1.1 maya for (slots = if_rxr_get(&sc->sc_rxbuf_ring, sc->sc_max_rxbufpost);
1074 1.1 maya slots > 0; slots--) {
1075 1.1 maya if (bwfm_pci_pktid_avail(sc, &sc->sc_rx_pkts))
1076 1.1 maya break;
1077 1.1 maya req = bwfm_pci_ring_write_reserve(sc, &sc->sc_rxpost_submit);
1078 1.1 maya if (req == NULL)
1079 1.1 maya break;
1080 1.1 maya m = MCLGETI(NULL, M_DONTWAIT, NULL, MSGBUF_MAX_PKT_SIZE);
1081 1.1 maya if (m == NULL) {
1082 1.1 maya bwfm_pci_ring_write_cancel(sc, &sc->sc_rxpost_submit, 1);
1083 1.1 maya break;
1084 1.1 maya }
1085 1.1 maya m->m_len = m->m_pkthdr.len = MSGBUF_MAX_PKT_SIZE;
1086 1.2 riastrad if (bwfm_pci_pktid_new(sc, &sc->sc_rx_pkts, &m, &pktid, &paddr)) {
1087 1.1 maya bwfm_pci_ring_write_cancel(sc, &sc->sc_rxpost_submit, 1);
1088 1.1 maya m_freem(m);
1089 1.1 maya break;
1090 1.1 maya }
1091 1.5 martin devaddr = paddr;
1092 1.1 maya memset(req, 0, sizeof(*req));
1093 1.1 maya req->msg.msgtype = MSGBUF_TYPE_RXBUF_POST;
1094 1.1 maya req->msg.request_id = htole32(pktid);
1095 1.1 maya req->data_buf_len = htole16(MSGBUF_MAX_PKT_SIZE);
1096 1.5 martin req->data_buf_addr.high_addr = htole32(devaddr >> 32);
1097 1.5 martin req->data_buf_addr.low_addr = htole32(devaddr & 0xffffffff);
1098 1.1 maya bwfm_pci_ring_write_commit(sc, &sc->sc_rxpost_submit);
1099 1.1 maya }
1100 1.1 maya if_rxr_put(&sc->sc_rxbuf_ring, slots);
1101 1.1 maya splx(s);
1102 1.1 maya }
1103 1.1 maya
1104 1.1 maya int
1105 1.1 maya bwfm_pci_setup_ring(struct bwfm_pci_softc *sc, struct bwfm_pci_msgring *ring,
1106 1.1 maya int nitem, size_t itemsz, uint32_t w_idx, uint32_t r_idx,
1107 1.1 maya int idx, uint32_t idx_off, uint32_t *ring_mem)
1108 1.1 maya {
1109 1.1 maya ring->w_idx_addr = w_idx + idx * idx_off;
1110 1.1 maya ring->r_idx_addr = r_idx + idx * idx_off;
1111 1.1 maya ring->nitem = nitem;
1112 1.1 maya ring->itemsz = itemsz;
1113 1.1 maya bwfm_pci_ring_write_rptr(sc, ring);
1114 1.1 maya bwfm_pci_ring_write_wptr(sc, ring);
1115 1.1 maya
1116 1.1 maya ring->ring = bwfm_pci_dmamem_alloc(sc, nitem * itemsz, 8);
1117 1.1 maya if (ring->ring == NULL)
1118 1.1 maya return ENOMEM;
1119 1.1 maya bus_space_write_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
1120 1.1 maya *ring_mem + BWFM_RING_MEM_BASE_ADDR_LOW,
1121 1.1 maya BWFM_PCI_DMA_DVA(ring->ring) & 0xffffffff);
1122 1.1 maya bus_space_write_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
1123 1.1 maya *ring_mem + BWFM_RING_MEM_BASE_ADDR_HIGH,
1124 1.1 maya BWFM_PCI_DMA_DVA(ring->ring) >> 32);
1125 1.1 maya bus_space_write_2(sc->sc_tcm_iot, sc->sc_tcm_ioh,
1126 1.1 maya *ring_mem + BWFM_RING_MAX_ITEM, nitem);
1127 1.1 maya bus_space_write_2(sc->sc_tcm_iot, sc->sc_tcm_ioh,
1128 1.1 maya *ring_mem + BWFM_RING_LEN_ITEMS, itemsz);
1129 1.1 maya *ring_mem = *ring_mem + BWFM_RING_MEM_SZ;
1130 1.1 maya return 0;
1131 1.1 maya }
1132 1.1 maya
1133 1.1 maya int
1134 1.1 maya bwfm_pci_setup_flowring(struct bwfm_pci_softc *sc, struct bwfm_pci_msgring *ring,
1135 1.1 maya int nitem, size_t itemsz)
1136 1.1 maya {
1137 1.1 maya ring->w_ptr = 0;
1138 1.1 maya ring->r_ptr = 0;
1139 1.1 maya ring->nitem = nitem;
1140 1.1 maya ring->itemsz = itemsz;
1141 1.1 maya bwfm_pci_ring_write_rptr(sc, ring);
1142 1.1 maya bwfm_pci_ring_write_wptr(sc, ring);
1143 1.1 maya
1144 1.1 maya ring->ring = bwfm_pci_dmamem_alloc(sc, nitem * itemsz, 8);
1145 1.1 maya if (ring->ring == NULL)
1146 1.1 maya return ENOMEM;
1147 1.1 maya return 0;
1148 1.1 maya }
1149 1.1 maya
1150 1.1 maya /* Ring helpers */
1151 1.1 maya void
1152 1.1 maya bwfm_pci_ring_bell(struct bwfm_pci_softc *sc,
1153 1.1 maya struct bwfm_pci_msgring *ring)
1154 1.1 maya {
1155 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
1156 1.1 maya BWFM_PCI_PCIE2REG_H2D_MAILBOX, 1);
1157 1.1 maya }
1158 1.1 maya
1159 1.1 maya void
1160 1.1 maya bwfm_pci_ring_update_rptr(struct bwfm_pci_softc *sc,
1161 1.1 maya struct bwfm_pci_msgring *ring)
1162 1.1 maya {
1163 1.1 maya if (sc->sc_dma_idx_sz == 0) {
1164 1.1 maya ring->r_ptr = bus_space_read_2(sc->sc_tcm_iot,
1165 1.1 maya sc->sc_tcm_ioh, ring->r_idx_addr);
1166 1.1 maya } else {
1167 1.4 maya bus_dmamap_sync(sc->sc_dmat,
1168 1.4 maya BWFM_PCI_DMA_MAP(sc->sc_dma_idx_buf), ring->r_idx_addr,
1169 1.4 maya sizeof(uint16_t), BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1170 1.1 maya ring->r_ptr = *(uint16_t *)(BWFM_PCI_DMA_KVA(sc->sc_dma_idx_buf)
1171 1.1 maya + ring->r_idx_addr);
1172 1.1 maya }
1173 1.1 maya }
1174 1.1 maya
1175 1.1 maya static u_int
1176 1.1 maya if_rxr_get(struct if_rxring *rxr, unsigned int max)
1177 1.1 maya {
1178 1.1 maya u_int taken = MIN(max, (rxr->rxr_total - rxr->rxr_inuse));
1179 1.1 maya
1180 1.1 maya KASSERTMSG(rxr->rxr_inuse + taken <= rxr->rxr_total,
1181 1.1 maya "rxr->rxr_inuse: %d\n"
1182 1.1 maya "taken: %d\n"
1183 1.1 maya "rxr->rxr_total: %d\n",
1184 1.1 maya rxr->rxr_inuse, taken, rxr->rxr_total);
1185 1.1 maya rxr->rxr_inuse += taken;
1186 1.1 maya
1187 1.1 maya return taken;
1188 1.1 maya }
1189 1.1 maya
1190 1.1 maya static void
1191 1.1 maya if_rxr_put(struct if_rxring *rxr, unsigned int n)
1192 1.1 maya {
1193 1.1 maya KASSERTMSG(rxr->rxr_inuse >= n,
1194 1.1 maya "rxr->rxr_inuse: %d\n"
1195 1.1 maya "n: %d\n"
1196 1.1 maya "rxr->rxr_total: %d\n",
1197 1.1 maya rxr->rxr_inuse, n, rxr->rxr_total);
1198 1.1 maya
1199 1.1 maya rxr->rxr_inuse -= n;
1200 1.1 maya }
1201 1.1 maya
1202 1.1 maya static void
1203 1.1 maya if_rxr_init(struct if_rxring *rxr, unsigned int lwm __unused, unsigned int hwm)
1204 1.1 maya {
1205 1.1 maya (void) lwm;
1206 1.1 maya
1207 1.1 maya rxr->rxr_total = hwm;
1208 1.1 maya rxr->rxr_inuse = 0;
1209 1.1 maya }
1210 1.1 maya
1211 1.1 maya void
1212 1.1 maya bwfm_pci_ring_update_wptr(struct bwfm_pci_softc *sc,
1213 1.1 maya struct bwfm_pci_msgring *ring)
1214 1.1 maya {
1215 1.1 maya if (sc->sc_dma_idx_sz == 0) {
1216 1.1 maya ring->w_ptr = bus_space_read_2(sc->sc_tcm_iot,
1217 1.1 maya sc->sc_tcm_ioh, ring->w_idx_addr);
1218 1.1 maya } else {
1219 1.1 maya ring->w_ptr = *(uint16_t *)(BWFM_PCI_DMA_KVA(sc->sc_dma_idx_buf)
1220 1.1 maya + ring->w_idx_addr);
1221 1.4 maya bus_dmamap_sync(sc->sc_dmat,
1222 1.4 maya BWFM_PCI_DMA_MAP(sc->sc_dma_idx_buf), ring->w_idx_addr,
1223 1.4 maya sizeof(uint16_t), BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1224 1.1 maya }
1225 1.1 maya }
1226 1.1 maya
1227 1.1 maya void
1228 1.1 maya bwfm_pci_ring_write_rptr(struct bwfm_pci_softc *sc,
1229 1.1 maya struct bwfm_pci_msgring *ring)
1230 1.1 maya {
1231 1.1 maya if (sc->sc_dma_idx_sz == 0) {
1232 1.1 maya bus_space_write_2(sc->sc_tcm_iot, sc->sc_tcm_ioh,
1233 1.1 maya ring->r_idx_addr, ring->r_ptr);
1234 1.1 maya } else {
1235 1.1 maya *(uint16_t *)(BWFM_PCI_DMA_KVA(sc->sc_dma_idx_buf)
1236 1.1 maya + ring->r_idx_addr) = ring->r_ptr;
1237 1.4 maya bus_dmamap_sync(sc->sc_dmat,
1238 1.4 maya BWFM_PCI_DMA_MAP(sc->sc_dma_idx_buf), ring->r_idx_addr,
1239 1.4 maya sizeof(uint16_t), BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1240 1.1 maya }
1241 1.1 maya }
1242 1.1 maya
1243 1.1 maya void
1244 1.1 maya bwfm_pci_ring_write_wptr(struct bwfm_pci_softc *sc,
1245 1.1 maya struct bwfm_pci_msgring *ring)
1246 1.1 maya {
1247 1.1 maya if (sc->sc_dma_idx_sz == 0) {
1248 1.1 maya bus_space_write_2(sc->sc_tcm_iot, sc->sc_tcm_ioh,
1249 1.1 maya ring->w_idx_addr, ring->w_ptr);
1250 1.1 maya } else {
1251 1.1 maya *(uint16_t *)(BWFM_PCI_DMA_KVA(sc->sc_dma_idx_buf)
1252 1.1 maya + ring->w_idx_addr) = ring->w_ptr;
1253 1.4 maya bus_dmamap_sync(sc->sc_dmat,
1254 1.4 maya BWFM_PCI_DMA_MAP(sc->sc_dma_idx_buf), ring->w_idx_addr,
1255 1.4 maya sizeof(uint16_t), BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1256 1.1 maya }
1257 1.1 maya }
1258 1.1 maya
1259 1.1 maya /*
1260 1.1 maya * Retrieve a free descriptor to put new stuff in, but don't commit
1261 1.1 maya * to it yet so we can rollback later if any error occurs.
1262 1.1 maya */
1263 1.1 maya void *
1264 1.1 maya bwfm_pci_ring_write_reserve(struct bwfm_pci_softc *sc,
1265 1.1 maya struct bwfm_pci_msgring *ring)
1266 1.1 maya {
1267 1.1 maya int available;
1268 1.1 maya char *ret;
1269 1.1 maya
1270 1.1 maya bwfm_pci_ring_update_rptr(sc, ring);
1271 1.1 maya
1272 1.1 maya if (ring->r_ptr > ring->w_ptr)
1273 1.1 maya available = ring->r_ptr - ring->w_ptr;
1274 1.1 maya else
1275 1.1 maya available = ring->r_ptr + (ring->nitem - ring->w_ptr);
1276 1.1 maya
1277 1.1 maya if (available < 1)
1278 1.1 maya return NULL;
1279 1.1 maya
1280 1.1 maya ret = BWFM_PCI_DMA_KVA(ring->ring) + (ring->w_ptr * ring->itemsz);
1281 1.1 maya ring->w_ptr += 1;
1282 1.1 maya if (ring->w_ptr == ring->nitem)
1283 1.1 maya ring->w_ptr = 0;
1284 1.1 maya return ret;
1285 1.1 maya }
1286 1.1 maya
1287 1.1 maya void *
1288 1.1 maya bwfm_pci_ring_write_reserve_multi(struct bwfm_pci_softc *sc,
1289 1.1 maya struct bwfm_pci_msgring *ring, int count, int *avail)
1290 1.1 maya {
1291 1.1 maya int available;
1292 1.1 maya char *ret;
1293 1.1 maya
1294 1.1 maya bwfm_pci_ring_update_rptr(sc, ring);
1295 1.1 maya
1296 1.1 maya if (ring->r_ptr > ring->w_ptr)
1297 1.1 maya available = ring->r_ptr - ring->w_ptr;
1298 1.1 maya else
1299 1.1 maya available = ring->r_ptr + (ring->nitem - ring->w_ptr);
1300 1.1 maya
1301 1.1 maya if (available < 1)
1302 1.1 maya return NULL;
1303 1.1 maya
1304 1.1 maya ret = BWFM_PCI_DMA_KVA(ring->ring) + (ring->w_ptr * ring->itemsz);
1305 1.3 riastrad *avail = uimin(count, available - 1);
1306 1.1 maya if (*avail + ring->w_ptr > ring->nitem)
1307 1.1 maya *avail = ring->nitem - ring->w_ptr;
1308 1.1 maya ring->w_ptr += *avail;
1309 1.1 maya if (ring->w_ptr == ring->nitem)
1310 1.1 maya ring->w_ptr = 0;
1311 1.1 maya return ret;
1312 1.1 maya }
1313 1.1 maya
1314 1.1 maya /*
1315 1.1 maya * Read number of descriptors available (submitted by the firmware)
1316 1.1 maya * and retrieve pointer to first descriptor.
1317 1.1 maya */
1318 1.1 maya void *
1319 1.1 maya bwfm_pci_ring_read_avail(struct bwfm_pci_softc *sc,
1320 1.1 maya struct bwfm_pci_msgring *ring, int *avail)
1321 1.1 maya {
1322 1.1 maya bwfm_pci_ring_update_wptr(sc, ring);
1323 1.1 maya
1324 1.1 maya if (ring->w_ptr >= ring->r_ptr)
1325 1.1 maya *avail = ring->w_ptr - ring->r_ptr;
1326 1.1 maya else
1327 1.1 maya *avail = ring->nitem - ring->r_ptr;
1328 1.1 maya
1329 1.1 maya if (*avail == 0)
1330 1.1 maya return NULL;
1331 1.4 maya bus_dmamap_sync(sc->sc_dmat, BWFM_PCI_DMA_MAP(ring->ring),
1332 1.4 maya ring->r_ptr * ring->itemsz, *avail * ring->itemsz,
1333 1.4 maya BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1334 1.1 maya return BWFM_PCI_DMA_KVA(ring->ring) + (ring->r_ptr * ring->itemsz);
1335 1.1 maya }
1336 1.1 maya
1337 1.1 maya /*
1338 1.1 maya * Let firmware know we read N descriptors.
1339 1.1 maya */
1340 1.1 maya void
1341 1.1 maya bwfm_pci_ring_read_commit(struct bwfm_pci_softc *sc,
1342 1.1 maya struct bwfm_pci_msgring *ring, int nitem)
1343 1.1 maya {
1344 1.1 maya ring->r_ptr += nitem;
1345 1.1 maya if (ring->r_ptr == ring->nitem)
1346 1.1 maya ring->r_ptr = 0;
1347 1.1 maya bwfm_pci_ring_write_rptr(sc, ring);
1348 1.1 maya }
1349 1.1 maya
1350 1.1 maya /*
1351 1.1 maya * Let firmware know that we submitted some descriptors.
1352 1.1 maya */
1353 1.1 maya void
1354 1.1 maya bwfm_pci_ring_write_commit(struct bwfm_pci_softc *sc,
1355 1.1 maya struct bwfm_pci_msgring *ring)
1356 1.1 maya {
1357 1.4 maya bus_dmamap_sync(sc->sc_dmat, BWFM_PCI_DMA_MAP(ring->ring),
1358 1.4 maya 0, BWFM_PCI_DMA_LEN(ring->ring), BUS_DMASYNC_PREREAD |
1359 1.4 maya BUS_DMASYNC_PREWRITE);
1360 1.1 maya bwfm_pci_ring_write_wptr(sc, ring);
1361 1.1 maya bwfm_pci_ring_bell(sc, ring);
1362 1.1 maya }
1363 1.1 maya
1364 1.1 maya /*
1365 1.1 maya * Rollback N descriptors in case we don't actually want
1366 1.1 maya * to commit to it.
1367 1.1 maya */
1368 1.1 maya void
1369 1.1 maya bwfm_pci_ring_write_cancel(struct bwfm_pci_softc *sc,
1370 1.1 maya struct bwfm_pci_msgring *ring, int nitem)
1371 1.1 maya {
1372 1.1 maya if (ring->w_ptr == 0)
1373 1.1 maya ring->w_ptr = ring->nitem - nitem;
1374 1.1 maya else
1375 1.1 maya ring->w_ptr -= nitem;
1376 1.1 maya }
1377 1.1 maya
1378 1.1 maya /*
1379 1.1 maya * Foreach written descriptor on the ring, pass the descriptor to
1380 1.1 maya * a message handler and let the firmware know we handled it.
1381 1.1 maya */
1382 1.1 maya void
1383 1.1 maya bwfm_pci_ring_rx(struct bwfm_pci_softc *sc, struct bwfm_pci_msgring *ring)
1384 1.1 maya {
1385 1.1 maya char *buf;
1386 1.1 maya int avail, processed;
1387 1.1 maya
1388 1.1 maya again:
1389 1.1 maya buf = bwfm_pci_ring_read_avail(sc, ring, &avail);
1390 1.1 maya if (buf == NULL)
1391 1.1 maya return;
1392 1.1 maya
1393 1.1 maya processed = 0;
1394 1.1 maya while (avail) {
1395 1.1 maya bwfm_pci_msg_rx(sc, buf + sc->sc_rx_dataoffset);
1396 1.1 maya buf += ring->itemsz;
1397 1.1 maya processed++;
1398 1.1 maya if (processed == 48) {
1399 1.1 maya bwfm_pci_ring_read_commit(sc, ring, processed);
1400 1.1 maya processed = 0;
1401 1.1 maya }
1402 1.1 maya avail--;
1403 1.1 maya }
1404 1.1 maya if (processed)
1405 1.1 maya bwfm_pci_ring_read_commit(sc, ring, processed);
1406 1.1 maya if (ring->r_ptr == 0)
1407 1.1 maya goto again;
1408 1.1 maya }
1409 1.1 maya
1410 1.1 maya void
1411 1.1 maya bwfm_pci_msg_rx(struct bwfm_pci_softc *sc, void *buf)
1412 1.1 maya {
1413 1.1 maya struct ifnet *ifp = sc->sc_sc.sc_ic.ic_ifp;
1414 1.1 maya struct msgbuf_ioctl_resp_hdr *resp;
1415 1.1 maya struct msgbuf_tx_status *tx;
1416 1.1 maya struct msgbuf_rx_complete *rx;
1417 1.1 maya struct msgbuf_rx_event *event;
1418 1.1 maya struct msgbuf_common_hdr *msg;
1419 1.1 maya struct msgbuf_flowring_create_resp *fcr;
1420 1.1 maya struct msgbuf_flowring_delete_resp *fdr;
1421 1.1 maya struct bwfm_pci_msgring *ring;
1422 1.1 maya struct mbuf *m;
1423 1.1 maya int flowid;
1424 1.1 maya
1425 1.1 maya msg = (struct msgbuf_common_hdr *)buf;
1426 1.1 maya switch (msg->msgtype)
1427 1.1 maya {
1428 1.1 maya case MSGBUF_TYPE_FLOW_RING_CREATE_CMPLT:
1429 1.1 maya fcr = (struct msgbuf_flowring_create_resp *)buf;
1430 1.1 maya flowid = letoh16(fcr->compl_hdr.flow_ring_id);
1431 1.1 maya if (flowid < 2)
1432 1.1 maya break;
1433 1.1 maya flowid -= 2;
1434 1.1 maya if (flowid >= sc->sc_max_flowrings)
1435 1.1 maya break;
1436 1.1 maya ring = &sc->sc_flowrings[flowid];
1437 1.1 maya if (ring->status != RING_OPENING)
1438 1.1 maya break;
1439 1.1 maya if (fcr->compl_hdr.status) {
1440 1.1 maya printf("%s: failed to open flowring %d\n",
1441 1.1 maya DEVNAME(sc), flowid);
1442 1.1 maya ring->status = RING_CLOSED;
1443 1.1 maya if (ring->m) {
1444 1.1 maya m_freem(ring->m);
1445 1.1 maya ring->m = NULL;
1446 1.1 maya }
1447 1.1 maya ifp->if_flags &= ~IFF_OACTIVE;
1448 1.1 maya ifp->if_start(ifp);
1449 1.1 maya break;
1450 1.1 maya }
1451 1.1 maya ring->status = RING_OPEN;
1452 1.1 maya if (ring->m != NULL) {
1453 1.1 maya m = ring->m;
1454 1.1 maya ring->m = NULL;
1455 1.2 riastrad if (bwfm_pci_txdata(&sc->sc_sc, &m))
1456 1.1 maya m_freem(ring->m);
1457 1.1 maya }
1458 1.1 maya ifp->if_flags &= ~IFF_OACTIVE;
1459 1.1 maya ifp->if_start(ifp);
1460 1.1 maya break;
1461 1.1 maya case MSGBUF_TYPE_FLOW_RING_DELETE_CMPLT:
1462 1.1 maya fdr = (struct msgbuf_flowring_delete_resp *)buf;
1463 1.1 maya flowid = letoh16(fdr->compl_hdr.flow_ring_id);
1464 1.1 maya if (flowid < 2)
1465 1.1 maya break;
1466 1.1 maya flowid -= 2;
1467 1.1 maya if (flowid >= sc->sc_max_flowrings)
1468 1.1 maya break;
1469 1.1 maya ring = &sc->sc_flowrings[flowid];
1470 1.1 maya if (ring->status != RING_CLOSING)
1471 1.1 maya break;
1472 1.1 maya if (fdr->compl_hdr.status) {
1473 1.1 maya printf("%s: failed to delete flowring %d\n",
1474 1.1 maya DEVNAME(sc), flowid);
1475 1.1 maya break;
1476 1.1 maya }
1477 1.1 maya bwfm_pci_dmamem_free(sc, ring->ring);
1478 1.1 maya ring->status = RING_CLOSED;
1479 1.1 maya break;
1480 1.1 maya case MSGBUF_TYPE_IOCTLPTR_REQ_ACK:
1481 1.1 maya break;
1482 1.1 maya case MSGBUF_TYPE_IOCTL_CMPLT:
1483 1.1 maya resp = (struct msgbuf_ioctl_resp_hdr *)buf;
1484 1.1 maya sc->sc_ioctl_resp_pktid = letoh32(resp->msg.request_id);
1485 1.1 maya sc->sc_ioctl_resp_ret_len = letoh16(resp->resp_len);
1486 1.1 maya sc->sc_ioctl_resp_status = letoh16(resp->compl_hdr.status);
1487 1.1 maya if_rxr_put(&sc->sc_ioctl_ring, 1);
1488 1.1 maya bwfm_pci_fill_rx_rings(sc);
1489 1.1 maya wakeup(&sc->sc_ioctl_buf);
1490 1.1 maya break;
1491 1.1 maya case MSGBUF_TYPE_WL_EVENT:
1492 1.1 maya event = (struct msgbuf_rx_event *)buf;
1493 1.1 maya m = bwfm_pci_pktid_free(sc, &sc->sc_rx_pkts,
1494 1.1 maya letoh32(event->msg.request_id));
1495 1.1 maya if (m == NULL)
1496 1.1 maya break;
1497 1.1 maya m_adj(m, sc->sc_rx_dataoffset);
1498 1.1 maya m->m_len = m->m_pkthdr.len = letoh16(event->event_data_len);
1499 1.1 maya bwfm_rx(&sc->sc_sc, m);
1500 1.1 maya if_rxr_put(&sc->sc_event_ring, 1);
1501 1.1 maya bwfm_pci_fill_rx_rings(sc);
1502 1.1 maya break;
1503 1.1 maya case MSGBUF_TYPE_TX_STATUS:
1504 1.1 maya tx = (struct msgbuf_tx_status *)buf;
1505 1.1 maya m = bwfm_pci_pktid_free(sc, &sc->sc_tx_pkts,
1506 1.1 maya letoh32(tx->msg.request_id));
1507 1.1 maya if (m == NULL)
1508 1.1 maya break;
1509 1.1 maya m_freem(m);
1510 1.1 maya if (sc->sc_tx_pkts_full) {
1511 1.1 maya sc->sc_tx_pkts_full = 0;
1512 1.1 maya ifp->if_flags &= ~IFF_OACTIVE;
1513 1.1 maya ifp->if_start(ifp);
1514 1.1 maya }
1515 1.1 maya break;
1516 1.1 maya case MSGBUF_TYPE_RX_CMPLT:
1517 1.1 maya rx = (struct msgbuf_rx_complete *)buf;
1518 1.1 maya m = bwfm_pci_pktid_free(sc, &sc->sc_rx_pkts,
1519 1.1 maya letoh32(rx->msg.request_id));
1520 1.1 maya if (m == NULL)
1521 1.1 maya break;
1522 1.1 maya if (letoh16(rx->data_offset))
1523 1.1 maya m_adj(m, letoh16(rx->data_offset));
1524 1.1 maya else if (sc->sc_rx_dataoffset)
1525 1.1 maya m_adj(m, sc->sc_rx_dataoffset);
1526 1.1 maya m->m_len = m->m_pkthdr.len = letoh16(rx->data_len);
1527 1.1 maya bwfm_rx(&sc->sc_sc, m);
1528 1.1 maya if_rxr_put(&sc->sc_rxbuf_ring, 1);
1529 1.1 maya bwfm_pci_fill_rx_rings(sc);
1530 1.1 maya break;
1531 1.1 maya default:
1532 1.1 maya printf("%s: msgtype 0x%08x\n", __func__, msg->msgtype);
1533 1.1 maya break;
1534 1.1 maya }
1535 1.1 maya }
1536 1.1 maya
1537 1.1 maya /* Bus core helpers */
1538 1.1 maya void
1539 1.1 maya bwfm_pci_select_core(struct bwfm_pci_softc *sc, int id)
1540 1.1 maya {
1541 1.1 maya struct bwfm_softc *bwfm = (void *)sc;
1542 1.1 maya struct bwfm_core *core;
1543 1.1 maya
1544 1.1 maya core = bwfm_chip_get_core(bwfm, id);
1545 1.1 maya if (core == NULL) {
1546 1.1 maya printf("%s: could not find core to select", DEVNAME(sc));
1547 1.1 maya return;
1548 1.1 maya }
1549 1.1 maya
1550 1.1 maya pci_conf_write(sc->sc_pc, sc->sc_tag,
1551 1.1 maya BWFM_PCI_BAR0_WINDOW, core->co_base);
1552 1.1 maya if (pci_conf_read(sc->sc_pc, sc->sc_tag,
1553 1.1 maya BWFM_PCI_BAR0_WINDOW) != core->co_base)
1554 1.1 maya pci_conf_write(sc->sc_pc, sc->sc_tag,
1555 1.1 maya BWFM_PCI_BAR0_WINDOW, core->co_base);
1556 1.1 maya }
1557 1.1 maya
1558 1.1 maya uint32_t
1559 1.1 maya bwfm_pci_buscore_read(struct bwfm_softc *bwfm, uint32_t reg)
1560 1.1 maya {
1561 1.1 maya struct bwfm_pci_softc *sc = (void *)bwfm;
1562 1.1 maya uint32_t page, offset;
1563 1.1 maya
1564 1.1 maya page = reg & ~(BWFM_PCI_BAR0_REG_SIZE - 1);
1565 1.1 maya offset = reg & (BWFM_PCI_BAR0_REG_SIZE - 1);
1566 1.1 maya pci_conf_write(sc->sc_pc, sc->sc_tag, BWFM_PCI_BAR0_WINDOW, page);
1567 1.1 maya return bus_space_read_4(sc->sc_reg_iot, sc->sc_reg_ioh, offset);
1568 1.1 maya }
1569 1.1 maya
1570 1.1 maya void
1571 1.1 maya bwfm_pci_buscore_write(struct bwfm_softc *bwfm, uint32_t reg, uint32_t val)
1572 1.1 maya {
1573 1.1 maya struct bwfm_pci_softc *sc = (void *)bwfm;
1574 1.1 maya uint32_t page, offset;
1575 1.1 maya
1576 1.1 maya page = reg & ~(BWFM_PCI_BAR0_REG_SIZE - 1);
1577 1.1 maya offset = reg & (BWFM_PCI_BAR0_REG_SIZE - 1);
1578 1.1 maya pci_conf_write(sc->sc_pc, sc->sc_tag, BWFM_PCI_BAR0_WINDOW, page);
1579 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh, offset, val);
1580 1.1 maya }
1581 1.1 maya
1582 1.1 maya int
1583 1.1 maya bwfm_pci_buscore_prepare(struct bwfm_softc *bwfm)
1584 1.1 maya {
1585 1.1 maya return 0;
1586 1.1 maya }
1587 1.1 maya
1588 1.1 maya int
1589 1.1 maya bwfm_pci_buscore_reset(struct bwfm_softc *bwfm)
1590 1.1 maya {
1591 1.1 maya struct bwfm_pci_softc *sc = (void *)bwfm;
1592 1.1 maya struct bwfm_core *core;
1593 1.1 maya uint32_t reg;
1594 1.1 maya int i;
1595 1.1 maya
1596 1.1 maya bwfm_pci_select_core(sc, BWFM_AGENT_CORE_PCIE2);
1597 1.1 maya reg = pci_conf_read(sc->sc_pc, sc->sc_tag,
1598 1.1 maya BWFM_PCI_CFGREG_LINK_STATUS_CTRL);
1599 1.1 maya pci_conf_write(sc->sc_pc, sc->sc_tag, BWFM_PCI_CFGREG_LINK_STATUS_CTRL,
1600 1.1 maya reg & ~BWFM_PCI_CFGREG_LINK_STATUS_CTRL_ASPM_ENAB);
1601 1.1 maya
1602 1.1 maya bwfm_pci_select_core(sc, BWFM_AGENT_CORE_CHIPCOMMON);
1603 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
1604 1.1 maya BWFM_CHIP_REG_WATCHDOG, 4);
1605 1.1 maya delay(100 * 1000);
1606 1.1 maya
1607 1.1 maya bwfm_pci_select_core(sc, BWFM_AGENT_CORE_PCIE2);
1608 1.1 maya pci_conf_write(sc->sc_pc, sc->sc_tag,
1609 1.1 maya BWFM_PCI_CFGREG_LINK_STATUS_CTRL, reg);
1610 1.1 maya
1611 1.1 maya core = bwfm_chip_get_core(bwfm, BWFM_AGENT_CORE_PCIE2);
1612 1.1 maya if (core->co_rev <= 13) {
1613 1.1 maya uint16_t cfg_offset[] = {
1614 1.1 maya BWFM_PCI_CFGREG_STATUS_CMD,
1615 1.1 maya BWFM_PCI_CFGREG_PM_CSR,
1616 1.1 maya BWFM_PCI_CFGREG_MSI_CAP,
1617 1.1 maya BWFM_PCI_CFGREG_MSI_ADDR_L,
1618 1.1 maya BWFM_PCI_CFGREG_MSI_ADDR_H,
1619 1.1 maya BWFM_PCI_CFGREG_MSI_DATA,
1620 1.1 maya BWFM_PCI_CFGREG_LINK_STATUS_CTRL2,
1621 1.1 maya BWFM_PCI_CFGREG_RBAR_CTRL,
1622 1.1 maya BWFM_PCI_CFGREG_PML1_SUB_CTRL1,
1623 1.1 maya BWFM_PCI_CFGREG_REG_BAR2_CONFIG,
1624 1.1 maya BWFM_PCI_CFGREG_REG_BAR3_CONFIG,
1625 1.1 maya };
1626 1.1 maya
1627 1.1 maya for (i = 0; i < nitems(cfg_offset); i++) {
1628 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
1629 1.1 maya BWFM_PCI_PCIE2REG_CONFIGADDR, cfg_offset[i]);
1630 1.1 maya reg = bus_space_read_4(sc->sc_reg_iot, sc->sc_reg_ioh,
1631 1.1 maya BWFM_PCI_PCIE2REG_CONFIGDATA);
1632 1.1 maya DPRINTFN(3, ("%s: config offset 0x%04x, value 0x%04x\n",
1633 1.1 maya DEVNAME(sc), cfg_offset[i], reg));
1634 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
1635 1.1 maya BWFM_PCI_PCIE2REG_CONFIGDATA, reg);
1636 1.1 maya }
1637 1.1 maya }
1638 1.1 maya
1639 1.1 maya reg = bus_space_read_4(sc->sc_reg_iot, sc->sc_reg_ioh,
1640 1.1 maya BWFM_PCI_PCIE2REG_MAILBOXINT);
1641 1.1 maya if (reg != 0xffffffff)
1642 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
1643 1.1 maya BWFM_PCI_PCIE2REG_MAILBOXINT, reg);
1644 1.1 maya
1645 1.1 maya return 0;
1646 1.1 maya }
1647 1.1 maya
1648 1.1 maya void
1649 1.1 maya bwfm_pci_buscore_activate(struct bwfm_softc *bwfm, const uint32_t rstvec)
1650 1.1 maya {
1651 1.1 maya struct bwfm_pci_softc *sc = (void *)bwfm;
1652 1.1 maya bus_space_write_4(sc->sc_tcm_iot, sc->sc_tcm_ioh, 0, rstvec);
1653 1.1 maya }
1654 1.1 maya
1655 1.1 maya static int bwfm_pci_prio2fifo[8] = {
1656 1.1 maya 1, /* best effort */
1657 1.1 maya 0, /* IPTOS_PREC_IMMEDIATE */
1658 1.1 maya 0, /* IPTOS_PREC_PRIORITY */
1659 1.1 maya 1, /* IPTOS_PREC_FLASH */
1660 1.1 maya 2, /* IPTOS_PREC_FLASHOVERRIDE */
1661 1.1 maya 2, /* IPTOS_PREC_CRITIC_ECP */
1662 1.1 maya 3, /* IPTOS_PREC_INTERNETCONTROL */
1663 1.1 maya 3, /* IPTOS_PREC_NETCONTROL */
1664 1.1 maya };
1665 1.1 maya
1666 1.1 maya int
1667 1.1 maya bwfm_pci_flowring_lookup(struct bwfm_pci_softc *sc, struct mbuf *m)
1668 1.1 maya {
1669 1.1 maya struct ieee80211com *ic = &sc->sc_sc.sc_ic;
1670 1.1 maya uint8_t *da = mtod(m, uint8_t *);
1671 1.1 maya struct ether_header *eh;
1672 1.1 maya int flowid, prio, fifo;
1673 1.1 maya int i, found, ac;
1674 1.1 maya
1675 1.1 maya /* No QoS for EAPOL frames. */
1676 1.1 maya eh = mtod(m, struct ether_header *);
1677 1.1 maya ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
1678 1.1 maya M_WME_GETAC(m) : WME_AC_BE;
1679 1.1 maya
1680 1.1 maya prio = ac;
1681 1.1 maya fifo = bwfm_pci_prio2fifo[prio];
1682 1.1 maya
1683 1.1 maya switch (ic->ic_opmode)
1684 1.1 maya {
1685 1.1 maya case IEEE80211_M_STA:
1686 1.1 maya flowid = fifo;
1687 1.1 maya break;
1688 1.1 maya #ifndef IEEE80211_STA_ONLY
1689 1.1 maya case IEEE80211_M_HOSTAP:
1690 1.1 maya if (ETHER_IS_MULTICAST(da))
1691 1.1 maya da = __UNCONST(etherbroadcastaddr);
1692 1.1 maya flowid = da[5] * 2 + fifo;
1693 1.1 maya break;
1694 1.1 maya #endif
1695 1.1 maya default:
1696 1.1 maya printf("%s: state not supported\n", DEVNAME(sc));
1697 1.1 maya return ENOBUFS;
1698 1.1 maya }
1699 1.1 maya
1700 1.1 maya found = 0;
1701 1.1 maya flowid = flowid % sc->sc_max_flowrings;
1702 1.1 maya for (i = 0; i < sc->sc_max_flowrings; i++) {
1703 1.1 maya if (ic->ic_opmode == IEEE80211_M_STA &&
1704 1.1 maya sc->sc_flowrings[flowid].status >= RING_OPEN &&
1705 1.1 maya sc->sc_flowrings[flowid].fifo == fifo) {
1706 1.1 maya found = 1;
1707 1.1 maya break;
1708 1.1 maya }
1709 1.1 maya #ifndef IEEE80211_STA_ONLY
1710 1.1 maya if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1711 1.1 maya sc->sc_flowrings[flowid].status >= RING_OPEN &&
1712 1.1 maya sc->sc_flowrings[flowid].fifo == fifo &&
1713 1.1 maya !memcmp(sc->sc_flowrings[flowid].mac, da, ETHER_ADDR_LEN)) {
1714 1.1 maya found = 1;
1715 1.1 maya break;
1716 1.1 maya }
1717 1.1 maya #endif
1718 1.1 maya flowid = (flowid + 1) % sc->sc_max_flowrings;
1719 1.1 maya }
1720 1.1 maya
1721 1.1 maya if (found)
1722 1.1 maya return flowid;
1723 1.1 maya
1724 1.1 maya return -1;
1725 1.1 maya }
1726 1.1 maya
1727 1.1 maya void
1728 1.1 maya bwfm_pci_flowring_create(struct bwfm_pci_softc *sc, struct mbuf *m)
1729 1.1 maya {
1730 1.1 maya struct ieee80211com *ic = &sc->sc_sc.sc_ic;
1731 1.1 maya struct bwfm_cmd_flowring_create * cmd;
1732 1.1 maya uint8_t *da = mtod(m, uint8_t *);
1733 1.1 maya struct ether_header *eh;
1734 1.1 maya struct bwfm_pci_msgring *ring;
1735 1.1 maya int flowid, prio, fifo;
1736 1.1 maya int i, found, ac;
1737 1.1 maya
1738 1.1 maya cmd = pool_get(&sc->sc_flowring_pool, PR_NOWAIT);
1739 1.1 maya if (__predict_false(cmd == NULL))
1740 1.1 maya return;
1741 1.1 maya
1742 1.1 maya /* No QoS for EAPOL frames. */
1743 1.1 maya eh = mtod(m, struct ether_header *);
1744 1.1 maya ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
1745 1.1 maya M_WME_GETAC(m) : WME_AC_BE;
1746 1.1 maya
1747 1.1 maya prio = ac;
1748 1.1 maya fifo = bwfm_pci_prio2fifo[prio];
1749 1.1 maya
1750 1.1 maya switch (ic->ic_opmode)
1751 1.1 maya {
1752 1.1 maya case IEEE80211_M_STA:
1753 1.1 maya flowid = fifo;
1754 1.1 maya break;
1755 1.1 maya #ifndef IEEE80211_STA_ONLY
1756 1.1 maya case IEEE80211_M_HOSTAP:
1757 1.1 maya if (ETHER_IS_MULTICAST(da))
1758 1.1 maya da = __UNCONST(etherbroadcastaddr);
1759 1.1 maya flowid = da[5] * 2 + fifo;
1760 1.1 maya break;
1761 1.1 maya #endif
1762 1.1 maya default:
1763 1.1 maya printf("%s: state not supported\n", DEVNAME(sc));
1764 1.1 maya return;
1765 1.1 maya }
1766 1.1 maya
1767 1.1 maya found = 0;
1768 1.1 maya flowid = flowid % sc->sc_max_flowrings;
1769 1.1 maya for (i = 0; i < sc->sc_max_flowrings; i++) {
1770 1.1 maya ring = &sc->sc_flowrings[flowid];
1771 1.1 maya if (ring->status == RING_CLOSED) {
1772 1.1 maya ring->status = RING_OPENING;
1773 1.1 maya found = 1;
1774 1.1 maya break;
1775 1.1 maya }
1776 1.1 maya flowid = (flowid + 1) % sc->sc_max_flowrings;
1777 1.1 maya }
1778 1.1 maya
1779 1.1 maya /*
1780 1.1 maya * We cannot recover from that so far. Only a stop/init
1781 1.1 maya * cycle can revive this if it ever happens at all.
1782 1.1 maya */
1783 1.1 maya if (!found) {
1784 1.1 maya printf("%s: no flowring available\n", DEVNAME(sc));
1785 1.1 maya return;
1786 1.1 maya }
1787 1.1 maya
1788 1.1 maya cmd->sc = sc;
1789 1.1 maya cmd->m = m;
1790 1.1 maya cmd->prio = prio;
1791 1.1 maya cmd->flowid = flowid;
1792 1.1 maya workqueue_enqueue(sc->flowring_wq, &cmd->wq_cookie, NULL);
1793 1.1 maya }
1794 1.1 maya
1795 1.1 maya void
1796 1.1 maya bwfm_pci_flowring_create_cb(struct work *wk, void *arg) //(struct bwfm_softc *bwfm, void *arg)
1797 1.1 maya {
1798 1.1 maya struct bwfm_cmd_flowring_create *cmd = container_of(wk, struct bwfm_cmd_flowring_create, wq_cookie);
1799 1.1 maya struct bwfm_pci_softc *sc = cmd->sc; // (void *)bwfm;
1800 1.1 maya struct ieee80211com *ic = &sc->sc_sc.sc_ic;
1801 1.1 maya struct msgbuf_tx_flowring_create_req *req;
1802 1.1 maya struct bwfm_pci_msgring *ring;
1803 1.1 maya uint8_t *da, *sa;
1804 1.1 maya
1805 1.1 maya da = mtod(cmd->m, char *) + 0 * ETHER_ADDR_LEN;
1806 1.1 maya sa = mtod(cmd->m, char *) + 1 * ETHER_ADDR_LEN;
1807 1.1 maya
1808 1.1 maya ring = &sc->sc_flowrings[cmd->flowid];
1809 1.1 maya if (ring->status != RING_OPENING) {
1810 1.1 maya printf("%s: flowring not opening\n", DEVNAME(sc));
1811 1.1 maya return;
1812 1.1 maya }
1813 1.1 maya
1814 1.1 maya if (bwfm_pci_setup_flowring(sc, ring, 512, 48)) {
1815 1.1 maya printf("%s: cannot setup flowring\n", DEVNAME(sc));
1816 1.1 maya return;
1817 1.1 maya }
1818 1.1 maya
1819 1.1 maya req = bwfm_pci_ring_write_reserve(sc, &sc->sc_ctrl_submit);
1820 1.1 maya if (req == NULL) {
1821 1.1 maya printf("%s: cannot reserve for flowring\n", DEVNAME(sc));
1822 1.1 maya return;
1823 1.1 maya }
1824 1.1 maya
1825 1.1 maya ring->status = RING_OPENING;
1826 1.1 maya ring->fifo = bwfm_pci_prio2fifo[cmd->prio];
1827 1.1 maya ring->m = cmd->m;
1828 1.1 maya memcpy(ring->mac, da, ETHER_ADDR_LEN);
1829 1.1 maya #ifndef IEEE80211_STA_ONLY
1830 1.1 maya if (ic->ic_opmode == IEEE80211_M_HOSTAP && ETHER_IS_MULTICAST(da))
1831 1.1 maya memcpy(ring->mac, etherbroadcastaddr, ETHER_ADDR_LEN);
1832 1.1 maya #endif
1833 1.1 maya
1834 1.1 maya req->msg.msgtype = MSGBUF_TYPE_FLOW_RING_CREATE;
1835 1.1 maya req->msg.ifidx = 0;
1836 1.1 maya req->msg.request_id = 0;
1837 1.1 maya req->tid = bwfm_pci_prio2fifo[cmd->prio];
1838 1.1 maya req->flow_ring_id = letoh16(cmd->flowid + 2);
1839 1.1 maya memcpy(req->da, da, ETHER_ADDR_LEN);
1840 1.1 maya memcpy(req->sa, sa, ETHER_ADDR_LEN);
1841 1.1 maya req->flow_ring_addr.high_addr =
1842 1.1 maya letoh32(BWFM_PCI_DMA_DVA(ring->ring) >> 32);
1843 1.1 maya req->flow_ring_addr.low_addr =
1844 1.1 maya letoh32(BWFM_PCI_DMA_DVA(ring->ring) & 0xffffffff);
1845 1.1 maya req->max_items = letoh16(512);
1846 1.1 maya req->len_item = letoh16(48);
1847 1.1 maya
1848 1.1 maya bwfm_pci_ring_write_commit(sc, &sc->sc_ctrl_submit);
1849 1.1 maya pool_put(&sc->sc_flowring_pool, cmd);
1850 1.1 maya }
1851 1.1 maya
1852 1.1 maya void
1853 1.1 maya bwfm_pci_flowring_delete(struct bwfm_pci_softc *sc, int flowid)
1854 1.1 maya {
1855 1.1 maya struct msgbuf_tx_flowring_delete_req *req;
1856 1.1 maya struct bwfm_pci_msgring *ring;
1857 1.1 maya
1858 1.1 maya ring = &sc->sc_flowrings[flowid];
1859 1.1 maya if (ring->status != RING_OPEN) {
1860 1.1 maya printf("%s: flowring not open\n", DEVNAME(sc));
1861 1.1 maya return;
1862 1.1 maya }
1863 1.1 maya
1864 1.1 maya req = bwfm_pci_ring_write_reserve(sc, &sc->sc_ctrl_submit);
1865 1.1 maya if (req == NULL) {
1866 1.1 maya printf("%s: cannot reserve for flowring\n", DEVNAME(sc));
1867 1.1 maya return;
1868 1.1 maya }
1869 1.1 maya
1870 1.1 maya ring->status = RING_CLOSING;
1871 1.1 maya
1872 1.1 maya req->msg.msgtype = MSGBUF_TYPE_FLOW_RING_DELETE;
1873 1.1 maya req->msg.ifidx = 0;
1874 1.1 maya req->msg.request_id = 0;
1875 1.1 maya req->flow_ring_id = letoh16(flowid + 2);
1876 1.1 maya req->reason = 0;
1877 1.1 maya
1878 1.1 maya bwfm_pci_ring_write_commit(sc, &sc->sc_ctrl_submit);
1879 1.1 maya }
1880 1.1 maya
1881 1.1 maya void
1882 1.1 maya bwfm_pci_stop(struct bwfm_softc *bwfm)
1883 1.1 maya {
1884 1.1 maya struct bwfm_pci_softc *sc = (void *)bwfm;
1885 1.1 maya struct bwfm_pci_msgring *ring;
1886 1.1 maya int i;
1887 1.1 maya
1888 1.1 maya for (i = 0; i < sc->sc_max_flowrings; i++) {
1889 1.1 maya ring = &sc->sc_flowrings[i];
1890 1.1 maya if (ring->status == RING_OPEN)
1891 1.1 maya bwfm_pci_flowring_delete(sc, i);
1892 1.1 maya }
1893 1.1 maya }
1894 1.1 maya
1895 1.1 maya int
1896 1.1 maya bwfm_pci_txcheck(struct bwfm_softc *bwfm)
1897 1.1 maya {
1898 1.1 maya struct bwfm_pci_softc *sc = (void *)bwfm;
1899 1.1 maya struct bwfm_pci_msgring *ring;
1900 1.1 maya int i;
1901 1.1 maya
1902 1.1 maya /* If we are transitioning, we cannot send. */
1903 1.1 maya for (i = 0; i < sc->sc_max_flowrings; i++) {
1904 1.1 maya ring = &sc->sc_flowrings[i];
1905 1.1 maya if (ring->status == RING_OPENING)
1906 1.1 maya return ENOBUFS;
1907 1.1 maya }
1908 1.1 maya
1909 1.1 maya if (bwfm_pci_pktid_avail(sc, &sc->sc_tx_pkts)) {
1910 1.1 maya sc->sc_tx_pkts_full = 1;
1911 1.1 maya return ENOBUFS;
1912 1.1 maya }
1913 1.1 maya
1914 1.1 maya return 0;
1915 1.1 maya }
1916 1.1 maya
1917 1.1 maya int
1918 1.2 riastrad bwfm_pci_txdata(struct bwfm_softc *bwfm, struct mbuf **mp)
1919 1.1 maya {
1920 1.1 maya struct bwfm_pci_softc *sc = (void *)bwfm;
1921 1.1 maya struct bwfm_pci_msgring *ring;
1922 1.1 maya struct msgbuf_tx_msghdr *tx;
1923 1.1 maya uint32_t pktid;
1924 1.1 maya paddr_t paddr;
1925 1.5 martin uint64_t devaddr;
1926 1.1 maya struct ether_header *eh;
1927 1.1 maya int flowid, ret, ac;
1928 1.1 maya
1929 1.2 riastrad flowid = bwfm_pci_flowring_lookup(sc, *mp);
1930 1.1 maya if (flowid < 0) {
1931 1.1 maya /*
1932 1.1 maya * We cannot send the packet right now as there is
1933 1.1 maya * no flowring yet. The flowring will be created
1934 1.1 maya * asynchronously. While the ring is transitioning
1935 1.1 maya * the TX check will tell the upper layers that we
1936 1.1 maya * cannot send packets right now. When the flowring
1937 1.1 maya * is created the queue will be restarted and this
1938 1.1 maya * mbuf will be transmitted.
1939 1.1 maya */
1940 1.2 riastrad bwfm_pci_flowring_create(sc, *mp);
1941 1.1 maya return 0;
1942 1.1 maya }
1943 1.1 maya
1944 1.1 maya ring = &sc->sc_flowrings[flowid];
1945 1.1 maya if (ring->status == RING_OPENING ||
1946 1.1 maya ring->status == RING_CLOSING) {
1947 1.1 maya printf("%s: tried to use a flow that was "
1948 1.1 maya "transitioning in status %d\n",
1949 1.1 maya DEVNAME(sc), ring->status);
1950 1.1 maya return ENOBUFS;
1951 1.1 maya }
1952 1.1 maya
1953 1.1 maya tx = bwfm_pci_ring_write_reserve(sc, ring);
1954 1.1 maya if (tx == NULL)
1955 1.1 maya return ENOBUFS;
1956 1.1 maya
1957 1.1 maya /* No QoS for EAPOL frames. */
1958 1.2 riastrad eh = mtod(*mp, struct ether_header *);
1959 1.1 maya ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
1960 1.2 riastrad M_WME_GETAC(*mp) : WME_AC_BE;
1961 1.1 maya
1962 1.1 maya memset(tx, 0, sizeof(*tx));
1963 1.1 maya tx->msg.msgtype = MSGBUF_TYPE_TX_POST;
1964 1.1 maya tx->msg.ifidx = 0;
1965 1.1 maya tx->flags = BWFM_MSGBUF_PKT_FLAGS_FRAME_802_3;
1966 1.1 maya tx->flags |= ac << BWFM_MSGBUF_PKT_FLAGS_PRIO_SHIFT;
1967 1.1 maya tx->seg_cnt = 1;
1968 1.2 riastrad memcpy(tx->txhdr, mtod(*mp, char *), ETHER_HDR_LEN);
1969 1.1 maya
1970 1.2 riastrad ret = bwfm_pci_pktid_new(sc, &sc->sc_tx_pkts, mp, &pktid, &paddr);
1971 1.1 maya if (ret) {
1972 1.1 maya if (ret == ENOBUFS) {
1973 1.1 maya printf("%s: no pktid available for TX\n",
1974 1.1 maya DEVNAME(sc));
1975 1.1 maya sc->sc_tx_pkts_full = 1;
1976 1.1 maya }
1977 1.1 maya bwfm_pci_ring_write_cancel(sc, ring, 1);
1978 1.1 maya return ret;
1979 1.1 maya }
1980 1.5 martin devaddr = paddr + ETHER_HDR_LEN;
1981 1.1 maya
1982 1.1 maya tx->msg.request_id = htole32(pktid);
1983 1.2 riastrad tx->data_len = htole16((*mp)->m_len - ETHER_HDR_LEN);
1984 1.5 martin tx->data_buf_addr.high_addr = htole32(devaddr >> 32);
1985 1.5 martin tx->data_buf_addr.low_addr = htole32(devaddr & 0xffffffff);
1986 1.1 maya
1987 1.1 maya bwfm_pci_ring_write_commit(sc, ring);
1988 1.1 maya return 0;
1989 1.1 maya }
1990 1.1 maya
1991 1.1 maya #ifdef BWFM_DEBUG
1992 1.1 maya void
1993 1.1 maya bwfm_pci_debug_console(struct bwfm_pci_softc *sc)
1994 1.1 maya {
1995 1.1 maya uint32_t newidx = bus_space_read_4(sc->sc_tcm_iot, sc->sc_tcm_ioh,
1996 1.1 maya sc->sc_console_base_addr + BWFM_CONSOLE_WRITEIDX);
1997 1.1 maya
1998 1.1 maya if (newidx != sc->sc_console_readidx)
1999 1.1 maya DPRINTFN(3, ("BWFM CONSOLE: "));
2000 1.1 maya while (newidx != sc->sc_console_readidx) {
2001 1.1 maya uint8_t ch = bus_space_read_1(sc->sc_tcm_iot, sc->sc_tcm_ioh,
2002 1.1 maya sc->sc_console_buf_addr + sc->sc_console_readidx);
2003 1.1 maya sc->sc_console_readidx++;
2004 1.1 maya if (sc->sc_console_readidx == sc->sc_console_buf_size)
2005 1.1 maya sc->sc_console_readidx = 0;
2006 1.1 maya if (ch == '\r')
2007 1.1 maya continue;
2008 1.1 maya DPRINTFN(3, ("%c", ch));
2009 1.1 maya }
2010 1.1 maya }
2011 1.1 maya #endif
2012 1.1 maya
2013 1.1 maya int
2014 1.1 maya bwfm_pci_intr(void *v)
2015 1.1 maya {
2016 1.1 maya struct bwfm_pci_softc *sc = (void *)v;
2017 1.1 maya uint32_t status;
2018 1.1 maya
2019 1.1 maya if ((status = bus_space_read_4(sc->sc_reg_iot, sc->sc_reg_ioh,
2020 1.1 maya BWFM_PCI_PCIE2REG_MAILBOXINT)) == 0)
2021 1.1 maya return 0;
2022 1.1 maya
2023 1.1 maya bwfm_pci_intr_disable(sc);
2024 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
2025 1.1 maya BWFM_PCI_PCIE2REG_MAILBOXINT, status);
2026 1.1 maya
2027 1.1 maya if (status & (BWFM_PCI_PCIE2REG_MAILBOXMASK_INT_FN0_0 |
2028 1.1 maya BWFM_PCI_PCIE2REG_MAILBOXMASK_INT_FN0_1))
2029 1.1 maya printf("%s: handle MB data\n", __func__);
2030 1.1 maya
2031 1.1 maya if (status & BWFM_PCI_PCIE2REG_MAILBOXMASK_INT_D2H_DB) {
2032 1.1 maya bwfm_pci_ring_rx(sc, &sc->sc_rx_complete);
2033 1.1 maya bwfm_pci_ring_rx(sc, &sc->sc_tx_complete);
2034 1.1 maya bwfm_pci_ring_rx(sc, &sc->sc_ctrl_complete);
2035 1.1 maya }
2036 1.1 maya
2037 1.1 maya #ifdef BWFM_DEBUG
2038 1.1 maya bwfm_pci_debug_console(sc);
2039 1.1 maya #endif
2040 1.1 maya
2041 1.1 maya bwfm_pci_intr_enable(sc);
2042 1.1 maya return 1;
2043 1.1 maya }
2044 1.1 maya
2045 1.1 maya void
2046 1.1 maya bwfm_pci_intr_enable(struct bwfm_pci_softc *sc)
2047 1.1 maya {
2048 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
2049 1.1 maya BWFM_PCI_PCIE2REG_MAILBOXMASK,
2050 1.1 maya BWFM_PCI_PCIE2REG_MAILBOXMASK_INT_FN0_0 |
2051 1.1 maya BWFM_PCI_PCIE2REG_MAILBOXMASK_INT_FN0_1 |
2052 1.1 maya BWFM_PCI_PCIE2REG_MAILBOXMASK_INT_D2H_DB);
2053 1.1 maya }
2054 1.1 maya
2055 1.1 maya void
2056 1.1 maya bwfm_pci_intr_disable(struct bwfm_pci_softc *sc)
2057 1.1 maya {
2058 1.1 maya bus_space_write_4(sc->sc_reg_iot, sc->sc_reg_ioh,
2059 1.1 maya BWFM_PCI_PCIE2REG_MAILBOXMASK, 0);
2060 1.1 maya }
2061 1.1 maya
2062 1.1 maya /* Msgbuf protocol implementation */
2063 1.1 maya int
2064 1.1 maya bwfm_pci_msgbuf_query_dcmd(struct bwfm_softc *bwfm, int ifidx,
2065 1.1 maya int cmd, char *buf, size_t *len)
2066 1.1 maya {
2067 1.1 maya struct bwfm_pci_softc *sc = (void *)bwfm;
2068 1.1 maya struct msgbuf_ioctl_req_hdr *req;
2069 1.1 maya struct mbuf *m;
2070 1.1 maya size_t buflen;
2071 1.1 maya int s;
2072 1.1 maya
2073 1.1 maya s = splnet();
2074 1.1 maya sc->sc_ioctl_resp_pktid = -1;
2075 1.1 maya req = bwfm_pci_ring_write_reserve(sc, &sc->sc_ctrl_submit);
2076 1.1 maya if (req == NULL) {
2077 1.1 maya printf("%s: cannot reserve for write\n", DEVNAME(sc));
2078 1.1 maya splx(s);
2079 1.1 maya return 1;
2080 1.1 maya }
2081 1.1 maya req->msg.msgtype = MSGBUF_TYPE_IOCTLPTR_REQ;
2082 1.1 maya req->msg.ifidx = 0;
2083 1.1 maya req->msg.flags = 0;
2084 1.1 maya req->msg.request_id = htole32(MSGBUF_IOCTL_REQ_PKTID);
2085 1.1 maya req->cmd = htole32(cmd);
2086 1.1 maya req->output_buf_len = htole16(*len);
2087 1.1 maya req->trans_id = htole16(sc->sc_ioctl_reqid++);
2088 1.1 maya
2089 1.3 riastrad buflen = uimin(*len, BWFM_DMA_H2D_IOCTL_BUF_LEN);
2090 1.1 maya req->input_buf_len = htole16(buflen);
2091 1.1 maya req->req_buf_addr.high_addr =
2092 1.1 maya htole32((uint64_t)BWFM_PCI_DMA_DVA(sc->sc_ioctl_buf) >> 32);
2093 1.1 maya req->req_buf_addr.low_addr =
2094 1.1 maya htole32((uint64_t)BWFM_PCI_DMA_DVA(sc->sc_ioctl_buf) & 0xffffffff);
2095 1.1 maya if (buf)
2096 1.1 maya memcpy(BWFM_PCI_DMA_KVA(sc->sc_ioctl_buf), buf, buflen);
2097 1.1 maya else
2098 1.1 maya memset(BWFM_PCI_DMA_KVA(sc->sc_ioctl_buf), 0, buflen);
2099 1.1 maya
2100 1.1 maya bwfm_pci_ring_write_commit(sc, &sc->sc_ctrl_submit);
2101 1.1 maya splx(s);
2102 1.1 maya
2103 1.1 maya if (tsleep(&sc->sc_ioctl_buf, PCATCH, "bwfm", hz)) {
2104 1.1 maya printf("%s: timeout waiting for ioctl response\n",
2105 1.1 maya DEVNAME(sc));
2106 1.1 maya return 1;
2107 1.1 maya }
2108 1.1 maya
2109 1.1 maya m = bwfm_pci_pktid_free(sc, &sc->sc_rx_pkts, sc->sc_ioctl_resp_pktid);
2110 1.1 maya if (m == NULL)
2111 1.1 maya return 1;
2112 1.1 maya
2113 1.3 riastrad *len = uimin(buflen, sc->sc_ioctl_resp_ret_len);
2114 1.1 maya if (buf)
2115 1.1 maya memcpy(buf, mtod(m, char *), *len);
2116 1.1 maya m_freem(m);
2117 1.1 maya splx(s);
2118 1.1 maya
2119 1.1 maya return 0;
2120 1.1 maya }
2121 1.1 maya
2122 1.1 maya int
2123 1.1 maya bwfm_pci_msgbuf_set_dcmd(struct bwfm_softc *bwfm, int ifidx,
2124 1.1 maya int cmd, char *buf, size_t len)
2125 1.1 maya {
2126 1.1 maya return bwfm_pci_msgbuf_query_dcmd(bwfm, ifidx, cmd, buf, &len);
2127 1.1 maya }
2128