xref: /illumos-gate/usr/src/uts/common/io/cxgbe/t4nex/t4_sge.c (revision 31f89476218163eaf6cee254a52c8d4935354693)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source. A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * This file is part of the Chelsio T4 support code.
14  *
15  * Copyright (C) 2010-2013 Chelsio Communications.  All rights reserved.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the LICENSE file included in this
20  * release for licensing terms and conditions.
21  */
22 
23 /*
24  * Copyright 2025 Oxide Computer Company
25  */
26 
27 #include <sys/ddi.h>
28 #include <sys/sunddi.h>
29 #include <sys/sunndi.h>
30 #include <sys/atomic.h>
31 #include <sys/dlpi.h>
32 #include <sys/pattr.h>
33 #include <sys/strsubr.h>
34 #include <sys/stream.h>
35 #include <sys/strsun.h>
36 #include <inet/ip.h>
37 #include <inet/tcp.h>
38 
39 #include "common/common.h"
40 #include "common/t4_msg.h"
41 #include "common/t4_regs.h"
42 #include "common/t4_regs_values.h"
43 
44 /* TODO: Tune. */
45 int rx_buf_size = 8192;
46 int tx_copy_threshold = 256;
47 uint16_t rx_copy_threshold = 256;
48 
49 /* Used to track coalesced tx work request */
50 struct txpkts {
51 	mblk_t *tail;		/* head is in the software descriptor */
52 	uint64_t *flitp;	/* ptr to flit where next pkt should start */
53 	uint8_t npkt;		/* # of packets in this work request */
54 	uint8_t nflits;		/* # of flits used by this work request */
55 	uint16_t plen;		/* total payload (sum of all packets) */
56 };
57 
58 /* All information needed to tx a frame */
59 struct txinfo {
60 	uint32_t len;		/* Total length of frame */
61 	uint32_t flags;		/* Checksum and LSO flags */
62 	uint32_t mss;		/* MSS for LSO */
63 	uint8_t nsegs;		/* # of segments in the SGL, 0 means imm. tx */
64 	uint8_t nflits;		/* # of flits needed for the SGL */
65 	uint8_t hdls_used;	/* # of DMA handles used */
66 	uint32_t txb_used;	/* txb_space used */
67 	mac_ether_offload_info_t meoi;	/* pkt hdr info for offloads */
68 	struct ulptx_sgl sgl __attribute__((aligned(8)));
69 	struct ulptx_sge_pair reserved[TX_SGL_SEGS / 2];
70 };
71 
72 struct rxbuf {
73 	kmem_cache_t *cache;		/* the kmem_cache this rxb came from */
74 	ddi_dma_handle_t dhdl;
75 	ddi_acc_handle_t ahdl;
76 	caddr_t va;			/* KVA of buffer */
77 	uint64_t ba;			/* bus address of buffer */
78 	frtn_t freefunc;
79 	uint_t buf_size;
80 	volatile uint_t ref_cnt;
81 };
82 
83 static const uint16_t t4_iq_esize_bytes[] = {
84 	[T4_IQ_ESIZE_16B] = 16,
85 	[T4_IQ_ESIZE_32B] = 32,
86 	[T4_IQ_ESIZE_64B] = 64,
87 	[T4_IQ_ESIZE_128B] = 128,
88 };
89 
90 typedef struct t4_iq_params {
91 	t4_iq_type_t	tip_iq_type;
92 	uint8_t		tip_tmr_idx;
93 	int8_t		tip_pktc_idx;
94 	uint16_t	tip_qsize;
95 	t4_iq_esize_t	tip_esize;
96 	uint16_t	tip_fl_qsize;
97 	int		tip_cong_chan;
98 	t4_sge_iq_t	*tip_intr_evtq;
99 	uint_t		tip_intr_idx;
100 } t4_iq_params_t;
101 
102 static int t4_alloc_eq_base(struct port_info *, t4_sge_eq_t *);
103 static void t4_free_iq(struct port_info *, t4_sge_iq_t *);
104 static int t4_alloc_rxq(struct port_info *, struct sge_rxq *, uint_t);
105 static void t4_free_rxq(struct port_info *, struct sge_rxq *);
106 static void t4_free_eq(struct port_info *, t4_sge_eq_t *);
107 static void t4_alloc_eq_post(struct port_info *, t4_sge_eq_t *);
108 static int t4_alloc_txq(struct port_info *, struct sge_txq *, int);
109 static void t4_free_txq(struct port_info *, struct sge_txq *);
110 static int alloc_dma_memory(struct adapter *sc, size_t len, int flags,
111     ddi_device_acc_attr_t *acc_attr, ddi_dma_attr_t *dma_attr,
112     ddi_dma_handle_t *dma_hdl, ddi_acc_handle_t *acc_hdl, uint64_t *pba,
113     caddr_t *pva);
114 static int free_dma_memory(ddi_dma_handle_t *dhdl, ddi_acc_handle_t *ahdl);
115 static int alloc_desc_ring(struct adapter *sc, size_t len, int rw,
116     ddi_dma_handle_t *dma_hdl, ddi_acc_handle_t *acc_hdl, uint64_t *pba,
117     caddr_t *pva);
118 static int free_desc_ring(ddi_dma_handle_t *dhdl, ddi_acc_handle_t *ahdl);
119 static int alloc_tx_copybuffer(struct adapter *sc, size_t len,
120     ddi_dma_handle_t *dma_hdl, ddi_acc_handle_t *acc_hdl, uint64_t *pba,
121     caddr_t *pva);
122 static inline bool t4_get_new_rsp(const t4_sge_iq_t *, struct rsp_ctrl *);
123 static inline void t4_iq_next_entry(t4_sge_iq_t *iq);
124 static t4_iq_result_t t4_process_event_iq(t4_sge_iq_t *event_iq);
125 static bool t4_fl_refill(struct sge_fl *, uint_t);
126 static void t4_sfl_enqueue(struct adapter *, struct sge_fl *);
127 static void t4_sfl_process(void *);
128 static void t4_fl_free_bufs(struct sge_fl *fl);
129 static mblk_t *t4_fl_get_payload(struct sge_fl *, uint32_t, bool);
130 static int get_frame_txinfo(struct sge_txq *txq, mblk_t **fp,
131     struct txinfo *txinfo, int sgl_only);
132 static inline int fits_in_txb(struct sge_txq *txq, int len, int *waste);
133 static inline int copy_into_txb(struct sge_txq *txq, mblk_t *m, int len,
134     struct txinfo *txinfo);
135 static inline void add_seg(struct txinfo *txinfo, uint64_t ba, uint32_t len);
136 static inline int add_mblk(struct sge_txq *txq, struct txinfo *txinfo,
137     mblk_t *m, int len);
138 static void free_txinfo_resources(struct sge_txq *txq, struct txinfo *txinfo);
139 static int add_to_txpkts(struct sge_txq *txq, struct txpkts *txpkts, mblk_t *m,
140     struct txinfo *txinfo);
141 static void write_txpkts_wr(struct sge_txq *txq, struct txpkts *txpkts);
142 static int write_txpkt_wr(struct port_info *pi, struct sge_txq *txq, mblk_t *m,
143     struct txinfo *txinfo);
144 static void t4_write_flush_wr(struct sge_txq *);
145 static inline void write_ulp_cpl_sgl(struct port_info *pi, struct sge_txq *txq,
146     struct txpkts *txpkts, struct txinfo *txinfo);
147 static inline void copy_to_txd(t4_sge_eq_t *eq, caddr_t from, caddr_t *to,
148     size_t len);
149 static void t4_tx_ring_db(struct sge_txq *);
150 static uint16_t t4_tx_reclaim_credits(struct sge_txq *, uint16_t, mblk_t **);
151 static void t4_fl_ring_db(struct sge_fl *fl);
152 static kstat_t *setup_port_config_kstats(struct port_info *pi);
153 static kstat_t *setup_port_info_kstats(struct port_info *pi);
154 static kstat_t *setup_rxq_kstats(struct port_info *pi, struct sge_rxq *rxq,
155     uint_t idx);
156 static int update_rxq_kstats(kstat_t *ksp, int rw);
157 static int update_port_info_kstats(kstat_t *ksp, int rw);
158 static kstat_t *setup_txq_kstats(struct port_info *pi, struct sge_txq *txq,
159     int idx);
160 static int update_txq_kstats(kstat_t *ksp, int rw);
161 static void t4_sge_egr_update(t4_sge_iq_t *, const struct rss_header *);
162 static int t4_handle_cpl_msg(t4_sge_iq_t *, const struct rss_header *,
163     mblk_t *);
164 static int t4_handle_fw_msg(t4_sge_iq_t *, const struct rss_header *);
165 
166 static kmem_cache_t *rxbuf_cache_create(struct rxbuf_cache_params *);
167 static struct rxbuf *rxbuf_alloc(kmem_cache_t *, int);
168 static void rxbuf_free(struct rxbuf *);
169 static int rxbuf_ctor(void *, void *, int);
170 static void rxbuf_dtor(void *, void *);
171 
172 static inline void *
t4_rss_payload(const struct rss_header * rss)173 t4_rss_payload(const struct rss_header *rss)
174 {
175 	return ((void *)(&rss[1]));
176 }
177 
178 static inline t4_sge_iq_t **
t4_iqmap_slot(struct adapter * sc,uint_t cntxt_id)179 t4_iqmap_slot(struct adapter *sc, uint_t cntxt_id)
180 {
181 	const uint_t idx = cntxt_id - sc->sge.iqmap_start;
182 	VERIFY3U(idx, <, sc->sge.iqmap_sz);
183 	return (&sc->sge.iqmap[idx]);
184 }
185 
186 static inline t4_sge_eq_t **
t4_eqmap_slot(struct adapter * sc,uint_t cntxt_id)187 t4_eqmap_slot(struct adapter *sc, uint_t cntxt_id)
188 {
189 	const uint_t idx = cntxt_id - sc->sge.eqmap_start;
190 	VERIFY3U(idx, <, sc->sge.eqmap_sz);
191 	return (&sc->sge.eqmap[idx]);
192 }
193 
194 /*
195  * Get the address of the EQ host credit at the provided index.
196  */
197 static inline void *
t4_eq_credit(t4_sge_eq_t * eq,uint16_t idx)198 t4_eq_credit(t4_sge_eq_t *eq, uint16_t idx)
199 {
200 	ASSERT3U(idx, <, eq->tse_qsize_spg);
201 	uint8_t *credits = eq->tse_ring;
202 	return (&credits[idx * EQ_HC_SIZE]);
203 }
204 
205 static inline struct sge_rxq *
t4_iq_to_rxq(t4_sge_iq_t * iq)206 t4_iq_to_rxq(t4_sge_iq_t *iq)
207 {
208 	if (iq->tsi_iqtype == TIQT_ETH_RX) {
209 		return (__containerof(iq, struct sge_rxq, iq));
210 	} else {
211 		return (NULL);
212 	}
213 }
214 
215 static inline t4_sge_iq_t *
t4_fl_to_iq(struct sge_fl * fl)216 t4_fl_to_iq(struct sge_fl *fl)
217 {
218 	/*
219 	 * Currently, RXQs are the only consumer of sge_fl, and are thus the
220 	 * only case we need to worry about.
221 	 */
222 	struct sge_rxq *rxq = __containerof(fl, struct sge_rxq, fl);
223 	ASSERT(rxq->iq.tsi_iqtype == TIQT_ETH_RX);
224 
225 	return (&rxq->iq);
226 }
227 
228 void
t4_sge_init(struct adapter * sc)229 t4_sge_init(struct adapter *sc)
230 {
231 	struct driver_properties *p = &sc->props;
232 	ddi_dma_attr_t *dma_attr;
233 	ddi_device_acc_attr_t *acc_attr;
234 	uint32_t sge_control;
235 
236 	/*
237 	 * Device access and DMA attributes for descriptor rings
238 	 */
239 	acc_attr = &sc->sge.acc_attr_desc;
240 	acc_attr->devacc_attr_version = DDI_DEVICE_ATTR_V0;
241 	acc_attr->devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
242 	acc_attr->devacc_attr_dataorder = DDI_STRICTORDER_ACC;
243 
244 	dma_attr = &sc->sge.dma_attr_desc;
245 	dma_attr->dma_attr_version = DMA_ATTR_V0;
246 	dma_attr->dma_attr_addr_lo = 0;
247 	dma_attr->dma_attr_addr_hi = UINT64_MAX;
248 	dma_attr->dma_attr_count_max = UINT64_MAX;
249 	dma_attr->dma_attr_align = 512;
250 	dma_attr->dma_attr_burstsizes = 0xfff;
251 	dma_attr->dma_attr_minxfer = 1;
252 	dma_attr->dma_attr_maxxfer = UINT64_MAX;
253 	dma_attr->dma_attr_seg = UINT64_MAX;
254 	dma_attr->dma_attr_sgllen = 1;
255 	dma_attr->dma_attr_granular = 1;
256 	dma_attr->dma_attr_flags = 0;
257 
258 	/*
259 	 * Device access and DMA attributes for tx buffers
260 	 */
261 	acc_attr = &sc->sge.acc_attr_tx;
262 	acc_attr->devacc_attr_version = DDI_DEVICE_ATTR_V0;
263 	acc_attr->devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
264 
265 	dma_attr = &sc->sge.dma_attr_tx;
266 	dma_attr->dma_attr_version = DMA_ATTR_V0;
267 	dma_attr->dma_attr_addr_lo = 0;
268 	dma_attr->dma_attr_addr_hi = UINT64_MAX;
269 	dma_attr->dma_attr_count_max = UINT64_MAX;
270 	dma_attr->dma_attr_align = 1;
271 	dma_attr->dma_attr_burstsizes = 0xfff;
272 	dma_attr->dma_attr_minxfer = 1;
273 	dma_attr->dma_attr_maxxfer = UINT64_MAX;
274 	dma_attr->dma_attr_seg = UINT64_MAX;
275 	dma_attr->dma_attr_sgllen = TX_SGL_SEGS;
276 	dma_attr->dma_attr_granular = 1;
277 	dma_attr->dma_attr_flags = 0;
278 
279 	/*
280 	 * Ingress Padding Boundary and Egress Status Page Size are set up by
281 	 * t4_fixup_host_params().
282 	 */
283 	sge_control = t4_read_reg(sc, A_SGE_CONTROL);
284 	sc->sge.pktshift = G_PKTSHIFT(sge_control);
285 	sc->sge.eq_spg_len = (sge_control & F_EGRSTATUSPAGESIZE) ? 2 : 1;
286 
287 	/* t4_nex uses FLM packed mode */
288 	const int fl_align = t4_fl_pkt_align(sc, true);
289 	VERIFY3S(fl_align, >=, 0);
290 	/*
291 	 * Minimum alignment for freelist buffer sizes is stated as 16, but in
292 	 * order to keep bits [3:0] clear for identifying the buffer size
293 	 * register, we use a minimum of 32.
294 	 *
295 	 * See A_SGE_FL_BUFFER_SIZE0 setting below.
296 	 */
297 	sc->sge.fl_align = MAX(fl_align, 32);
298 
299 	/*
300 	 * Device access and DMA attributes for RX buffers
301 	 */
302 	sc->sge.rxb_params.dip = sc->dip;
303 	sc->sge.rxb_params.buf_size = P2ROUNDUP(rx_buf_size, fl_align);
304 
305 	acc_attr = &sc->sge.rxb_params.acc_attr_rx;
306 	acc_attr->devacc_attr_version = DDI_DEVICE_ATTR_V0;
307 	acc_attr->devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
308 
309 	dma_attr = &sc->sge.rxb_params.dma_attr_rx;
310 	dma_attr->dma_attr_version = DMA_ATTR_V0;
311 	dma_attr->dma_attr_addr_lo = 0;
312 	dma_attr->dma_attr_addr_hi = UINT64_MAX;
313 	dma_attr->dma_attr_count_max = UINT64_MAX;
314 	dma_attr->dma_attr_align = sc->sge.fl_align;
315 	dma_attr->dma_attr_burstsizes = 0xfff;
316 	dma_attr->dma_attr_minxfer = 1;
317 	dma_attr->dma_attr_maxxfer = UINT64_MAX;
318 	dma_attr->dma_attr_seg = UINT64_MAX;
319 	dma_attr->dma_attr_sgllen = 1;
320 	dma_attr->dma_attr_granular = 1;
321 	dma_attr->dma_attr_flags = 0;
322 
323 	sc->sge.rxbuf_cache = rxbuf_cache_create(&sc->sge.rxb_params);
324 
325 	/*
326 	 * A FL with <= fl_starve_threshold buffers is starving and a periodic
327 	 * timer will attempt to refill it.  This needs to be larger than the
328 	 * SGE's Egress Congestion Threshold.  If it isn't, then we can get
329 	 * stuck waiting for new packets while the SGE is waiting for us to
330 	 * give it more Free List entries.  (Note that the SGE's Egress
331 	 * Congestion Threshold is in units of 2 Free List pointers.) For T4,
332 	 * there was only a single field to control this.  For T5 there's the
333 	 * original field which now only applies to Unpacked Mode Free List
334 	 * buffers and a new field which only applies to Packed Mode Free List
335 	 * buffers.
336 	 */
337 
338 	const uint32_t sge_conm_ctrl = t4_read_reg(sc, A_SGE_CONM_CTRL);
339 	uint_t egress_threshold;
340 	switch (CHELSIO_CHIP_VERSION(sc->params.chip)) {
341 	case CHELSIO_T4:
342 		egress_threshold = G_EGRTHRESHOLD(sge_conm_ctrl);
343 		break;
344 	case CHELSIO_T5:
345 		egress_threshold = G_EGRTHRESHOLDPACKING(sge_conm_ctrl);
346 		break;
347 	case CHELSIO_T6:
348 	default:
349 		egress_threshold = G_T6_EGRTHRESHOLDPACKING(sge_conm_ctrl);
350 	}
351 	sc->sge.fl_starve_threshold = 2 * egress_threshold + 1;
352 
353 	/*
354 	 * Set the size of buffers submitted through freelists.
355 	 *
356 	 * Strictly speaking, this is setting one of sixteen possible buffer
357 	 * sizes, with bits [3:0] of freelist entries designating the size
358 	 * register (0-15) which contains its corresponding size.
359 	 *
360 	 * Our driver does not currently make use of multiple sizes.  Submitted
361 	 * buffers are at least 16-byte aligned, thus bits [3:0] are 0,
362 	 * selecting this size register.
363 	 */
364 	t4_write_reg(sc, A_SGE_FL_BUFFER_SIZE0, sc->sge.rxb_params.buf_size);
365 
366 	t4_write_reg(sc, A_SGE_INGRESS_RX_THRESHOLD,
367 	    V_THRESHOLD_0(p->holdoff_pktcnt[0]) |
368 	    V_THRESHOLD_1(p->holdoff_pktcnt[1]) |
369 	    V_THRESHOLD_2(p->holdoff_pktcnt[2]) |
370 	    V_THRESHOLD_3(p->holdoff_pktcnt[3]));
371 
372 	t4_write_reg(sc, A_SGE_TIMER_VALUE_0_AND_1,
373 	    V_TIMERVALUE0(us_to_core_ticks(sc, p->holdoff_timer_us[0])) |
374 	    V_TIMERVALUE1(us_to_core_ticks(sc, p->holdoff_timer_us[1])));
375 	t4_write_reg(sc, A_SGE_TIMER_VALUE_2_AND_3,
376 	    V_TIMERVALUE2(us_to_core_ticks(sc, p->holdoff_timer_us[2])) |
377 	    V_TIMERVALUE3(us_to_core_ticks(sc, p->holdoff_timer_us[3])));
378 	t4_write_reg(sc, A_SGE_TIMER_VALUE_4_AND_5,
379 	    V_TIMERVALUE4(us_to_core_ticks(sc, p->holdoff_timer_us[4])) |
380 	    V_TIMERVALUE5(us_to_core_ticks(sc, p->holdoff_timer_us[5])));
381 }
382 
383 static uint_t
t4_queue_to_intrq(struct adapter * sc,uint_t q_idx)384 t4_queue_to_intrq(struct adapter *sc, uint_t q_idx)
385 {
386 	return (q_idx % sc->intr_queue_cfg.intr_per_port);
387 }
388 
389 /*
390  * Assign an interrupt event queue to the Rx queue specified by q_idx. If
391  * we are in TIP_PER_PORT mode, this is done by multiplexing the Rx queues
392  * across the port's interrupt queues. Otherwise, all events are directed
393  * to the adapter-wide firmware queue.
394  */
395 static void
t4_rxq_intr_assign(struct port_info * pi,uint_t rxq_idx,struct t4_iq_params * iqp)396 t4_rxq_intr_assign(struct port_info *pi, uint_t rxq_idx,
397     struct t4_iq_params *iqp)
398 {
399 	struct adapter *sc = pi->adapter;
400 	const struct t4_intrs_queues *iqc = &sc->intr_queue_cfg;
401 
402 	switch (iqc->intr_plan) {
403 	case TIP_PER_PORT: {
404 		uint_t intr_iq_idx = t4_queue_to_intrq(sc, rxq_idx);
405 		iqp->tip_intr_evtq = &pi->intr_iqs[intr_iq_idx];
406 		iqp->tip_intr_idx = INTR_FORWARDED;
407 		break;
408 	}
409 	case TIP_SINGLE:
410 	case TIP_ERR_QUEUES:
411 	default:
412 		/* Forward all RXQ interrupts to FWQ */
413 		iqp->tip_intr_evtq = &sc->sge.fwq;
414 		iqp->tip_intr_idx = INTR_FORWARDED;
415 		break;
416 	}
417 }
418 
419 void
t4_port_kstats_init(struct port_info * pi)420 t4_port_kstats_init(struct port_info *pi)
421 {
422 	ASSERT(pi->ksp_config == NULL);
423 	ASSERT(pi->ksp_info == NULL);
424 
425 	pi->ksp_config = setup_port_config_kstats(pi);
426 	pi->ksp_info = setup_port_info_kstats(pi);
427 }
428 
429 void
t4_port_kstats_fini(struct port_info * pi)430 t4_port_kstats_fini(struct port_info *pi)
431 {
432 	if (pi->ksp_config != NULL) {
433 		kstat_delete(pi->ksp_config);
434 		pi->ksp_config = NULL;
435 	}
436 	if (pi->ksp_info != NULL) {
437 		kstat_delete(pi->ksp_info);
438 		pi->ksp_info = NULL;
439 	}
440 }
441 
442 int
t4_port_queues_init(struct port_info * pi)443 t4_port_queues_init(struct port_info *pi)
444 {
445 	int rc = 0;
446 	uint_t q_idx;
447 	struct adapter *sc = pi->adapter;
448 
449 	struct sge_rxq *rxq;
450 	for_each_rxq(pi, q_idx, rxq) {
451 		if ((rc = t4_alloc_rxq(pi, rxq, q_idx)) != 0) {
452 			goto cleanup;
453 		}
454 	}
455 
456 	struct sge_txq *txq;
457 	for_each_txq(pi, q_idx, txq) {
458 		txq->eq.tse_flags = 0;
459 		txq->eq.tse_tx_chan = pi->tx_chan;
460 		txq->eq.tse_qsize = sc->props.qsize_txq;
461 
462 		if (sc->intr_queue_cfg.intr_plan == TIP_PER_PORT) {
463 			/*
464 			 * If we have per port interrupts, then multiplex
465 			 * TX completion events across them.
466 			 */
467 			uint_t intr_iq_idx = t4_queue_to_intrq(sc, q_idx);
468 			txq->eq.tse_iqid =
469 			    pi->intr_iqs[intr_iq_idx].tsi_cntxt_id;
470 		} else {
471 			/*
472 			 * Otherwise, handle all TX completion events in
473 			 * the firmware queue.
474 			 */
475 			txq->eq.tse_iqid = sc->sge.fwq.tsi_cntxt_id;
476 		}
477 
478 		if ((rc = t4_alloc_txq(pi, txq, q_idx)) != 0) {
479 			goto cleanup;
480 		}
481 	}
482 
483 	return (0);
484 
485 cleanup:
486 	t4_port_queues_fini(pi);
487 	return (rc);
488 }
489 
490 void
t4_port_queues_fini(struct port_info * pi)491 t4_port_queues_fini(struct port_info *pi)
492 {
493 	uint_t i;
494 
495 	struct sge_txq *txq;
496 	for_each_txq(pi, i, txq) {
497 		t4_free_txq(pi, txq);
498 	}
499 
500 	struct sge_rxq *rxq;
501 	for_each_rxq(pi, i, rxq) {
502 		t4_free_rxq(pi, rxq);
503 	}
504 }
505 
506 void
t4_port_queues_enable(struct port_info * pi)507 t4_port_queues_enable(struct port_info *pi)
508 {
509 	ASSERT(pi->flags & TPF_INIT_DONE);
510 
511 	uint_t i;
512 	struct adapter *sc = pi->adapter;
513 	struct sge_rxq *rxq;
514 
515 	mutex_enter(&sc->sfl_lock);
516 	for_each_rxq(pi, i, rxq) {
517 		t4_sge_iq_t *iq = &rxq->iq;
518 
519 		IQ_LOCK(iq);
520 		VERIFY0(iq->tsi_flags & IQ_ENABLED);
521 		iq->tsi_flags |= IQ_ENABLED;
522 
523 		/*
524 		 * Freelists which were marked "doomed" by a previous
525 		 * t4_port_queues_disable() call should clear that status.
526 		 */
527 		rxq->fl.sfl_flags &= ~SFL_DOOMED;
528 
529 		t4_iq_gts_update(iq, iq->tsi_gts_rearm, 0);
530 		IQ_UNLOCK(iq);
531 	}
532 	mutex_exit(&sc->sfl_lock);
533 
534 	struct sge_txq *txq;
535 	for_each_txq(pi, i, txq) {
536 		t4_sge_eq_t *eq = &txq->eq;
537 
538 		EQ_LOCK(eq);
539 		eq->tse_flags |= EQ_ENABLED;
540 		EQ_UNLOCK(eq);
541 	}
542 }
543 
544 void
t4_port_queues_disable(struct port_info * pi)545 t4_port_queues_disable(struct port_info *pi)
546 {
547 	uint_t i;
548 	struct adapter *sc = pi->adapter;
549 	struct sge_rxq *rxq;
550 
551 	ASSERT(pi->flags & TPF_INIT_DONE);
552 
553 	for_each_rxq(pi, i, rxq) {
554 		t4_sge_iq_t *iq = &rxq->iq;
555 
556 		IQ_LOCK(iq);
557 		iq->tsi_flags &= ~IQ_ENABLED;
558 		IQ_UNLOCK(iq);
559 	}
560 
561 	mutex_enter(&sc->sfl_lock);
562 	for_each_rxq(pi, i, rxq) {
563 		rxq->fl.sfl_flags |= SFL_DOOMED;
564 	}
565 	mutex_exit(&sc->sfl_lock);
566 	/* TODO: need to wait for all fl's to be removed from sc->sfl */
567 
568 	struct sge_txq *txq;
569 	for_each_txq(pi, i, txq) {
570 		t4_sge_eq_t *eq = &txq->eq;
571 
572 		EQ_LOCK(eq);
573 		eq->tse_flags &= ~EQ_ENABLED;
574 		EQ_UNLOCK(eq);
575 	}
576 	/*
577 	 * TODO: issue flush WR to EQs and wait for EGR update to ensure that
578 	 * all processing has completed.
579 	 */
580 }
581 
582 /*
583  * We are counting on the values of t4_gts_config_t matching the register
584  * definitions from the shared code.
585  */
586 CTASSERT(TGC_SE_INTR_ARM == F_QINTR_CNT_EN);
587 CTASSERT(TGC_TIMER0 == V_QINTR_TIMER_IDX(X_TIMERREG_COUNTER0));
588 CTASSERT(TGC_TIMER5 == V_QINTR_TIMER_IDX(X_TIMERREG_COUNTER5));
589 CTASSERT(TGC_START_COUNTER == V_QINTR_TIMER_IDX(X_TIMERREG_RESTART_COUNTER));
590 
591 void
t4_iq_update_intr_cfg(t4_sge_iq_t * iq,uint8_t tmr_idx,int8_t pktc_idx)592 t4_iq_update_intr_cfg(t4_sge_iq_t *iq, uint8_t tmr_idx, int8_t pktc_idx)
593 {
594 	ASSERT((pktc_idx >= 0 && pktc_idx < SGE_NCOUNTERS) || pktc_idx == -1);
595 	IQ_LOCK_ASSERT_OWNED(iq);
596 	/*
597 	 * Strictly speaking, the IQ could be programmed with a TimerReg value
598 	 * of 6 (TICK_START_COUNTER), which is outside the range of SGE_NTIMERS.
599 	 *
600 	 * Since we do not currently offer an interface to configure such
601 	 * behavior, we assert its absence here for now.
602 	 */
603 	ASSERT3U(tmr_idx, <, SGE_NTIMERS);
604 
605 	iq->tsi_gts_rearm = V_QINTR_TIMER_IDX(tmr_idx) |
606 	    ((pktc_idx != -1) ? TGC_SE_INTR_ARM : 0);
607 
608 	/* Update IQ for new packet count threshold, but only if enabled */
609 	if (pktc_idx != iq->tsi_intr_pktc_idx && pktc_idx >= 0) {
610 		const uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
611 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
612 		    V_FW_PARAMS_PARAM_YZ(iq->tsi_cntxt_id);
613 		const uint32_t val = pktc_idx;
614 
615 		struct adapter *sc = iq->tsi_adapter;
616 		int rc =
617 		    -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
618 		if (rc != 0) {
619 			/* report error but carry on */
620 			cxgb_printf(sc->dip, CE_WARN,
621 			    "failed to set intr pktcnt index for IQ %d: %d",
622 			    iq->tsi_cntxt_id, rc);
623 		}
624 	}
625 	iq->tsi_intr_pktc_idx = pktc_idx;
626 }
627 
628 void
t4_eq_update_dbq_timer(t4_sge_eq_t * eq,struct port_info * pi)629 t4_eq_update_dbq_timer(t4_sge_eq_t *eq, struct port_info *pi)
630 {
631 	struct adapter *sc = pi->adapter;
632 
633 	const uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
634 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_EQ_TIMERIX) |
635 	    V_FW_PARAMS_PARAM_YZ(eq->tse_cntxt_id);
636 	const uint32_t val = pi->dbq_timer_idx;
637 
638 	int rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
639 	if (rc != 0) {
640 		/* report error but carry on */
641 		cxgb_printf(sc->dip, CE_WARN,
642 		    "failed to set DBQ timer index for EQ %d: %d",
643 		    eq->tse_cntxt_id, rc);
644 	}
645 }
646 
647 /*
648  * Update (via GTS) the interrupt/timer config and CIDX value for a specified
649  * ingress queue.
650  */
651 void
t4_iq_gts_update(t4_sge_iq_t * iq,t4_gts_config_t cfg,uint16_t cidx_incr)652 t4_iq_gts_update(t4_sge_iq_t *iq, t4_gts_config_t cfg, uint16_t cidx_incr)
653 {
654 	const uint32_t value =
655 	    V_INGRESSQID((uint32_t)iq->tsi_cntxt_id) |
656 	    V_CIDXINC((uint32_t)cidx_incr) |
657 	    V_SEINTARM((uint32_t)cfg);
658 	t4_write_reg(iq->tsi_adapter, MYPF_REG(A_SGE_PF_GTS), value);
659 }
660 
661 /*
662  * Update (via GTS) the CIDX value for a specified ingress queue.
663  *
664  * This _only_ increments CIDX and does not alter any other timer related state
665  * associated with the IQ.
666  */
667 static void
t4_iq_gts_incr(t4_sge_iq_t * iq,uint16_t cidx_incr)668 t4_iq_gts_incr(t4_sge_iq_t *iq, uint16_t cidx_incr)
669 {
670 	if (cidx_incr == 0) {
671 		return;
672 	}
673 
674 	const uint32_t value =
675 	    V_INGRESSQID((uint32_t)iq->tsi_cntxt_id) |
676 	    V_CIDXINC((uint32_t)cidx_incr) |
677 	    V_SEINTARM((uint32_t)V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX));
678 	t4_write_reg(iq->tsi_adapter, MYPF_REG(A_SGE_PF_GTS), value);
679 }
680 
681 uint_t
t4_intr_all(caddr_t arg1,caddr_t arg2)682 t4_intr_all(caddr_t arg1, caddr_t arg2)
683 {
684 	struct adapter *sc = (struct adapter *)arg1;
685 
686 	/* handle any device errors */
687 	t4_write_reg(sc, MYPF_REG(A_PCIE_PF_CLI), 0);
688 	(void) t4_slow_intr_handler(sc);
689 
690 	/* process fwq */
691 	(void) t4_process_event_iq(&sc->sge.fwq);
692 
693 	return (DDI_INTR_CLAIMED);
694 }
695 
696 uint_t
t4_intr_err(caddr_t arg1,caddr_t arg2)697 t4_intr_err(caddr_t arg1, caddr_t arg2)
698 {
699 	struct adapter *sc = (struct adapter *)arg1;
700 
701 	/* handle any device errors */
702 	t4_write_reg(sc, MYPF_REG(A_PCIE_PF_CLI), 0);
703 	(void) t4_slow_intr_handler(sc);
704 
705 	return (DDI_INTR_CLAIMED);
706 }
707 
708 uint_t
t4_intr_fwq(caddr_t arg1,caddr_t arg2)709 t4_intr_fwq(caddr_t arg1, caddr_t arg2)
710 {
711 	struct adapter *sc = (struct adapter *)arg1;
712 
713 	(void) t4_process_event_iq(&sc->sge.fwq);
714 
715 	return (DDI_INTR_CLAIMED);
716 }
717 
718 uint_t
t4_intr_port_queue(caddr_t arg1,caddr_t arg2)719 t4_intr_port_queue(caddr_t arg1, caddr_t arg2)
720 {
721 	t4_sge_iq_t *iq = (t4_sge_iq_t *)arg1;
722 
723 	(void) t4_process_event_iq(iq);
724 
725 	return (DDI_INTR_CLAIMED);
726 }
727 
728 static bool
t4_fl_periodic_refill(struct sge_fl * fl)729 t4_fl_periodic_refill(struct sge_fl *fl)
730 {
731 	FL_LOCK(fl);
732 	const bool starved = t4_fl_refill(fl, fl->bufs_cap / 8);
733 	FL_UNLOCK(fl);
734 
735 	return (starved);
736 }
737 
738 /*
739  * Convenience struct for tracking entry types while servicing an IQ.
740  * Used to communicate said counts through the t4-process-* probes.
741  */
742 struct sge_iq_totals {
743 	uint_t sit_desc;
744 	uint_t sit_flbuf;
745 	uint_t sit_cpl;
746 	uint_t sit_intr;
747 	uint_t sit_rx_bytes;
748 };
749 
750 /*
751  * Process entries on an event Ingress Queue. This type of queue receives
752  * firmware events, Tx EGR messages, and Rx forwarded interrupts only. It is
753  * used by the firmware queue and the individual port queues.
754  */
755 static t4_iq_result_t
t4_process_event_iq(t4_sge_iq_t * event_iq)756 t4_process_event_iq(t4_sge_iq_t *event_iq)
757 {
758 	int rc = TIR_SUCCESS;
759 	struct adapter *sc = event_iq->tsi_adapter;
760 
761 	const uint_t desc_limit = event_iq->tsi_qsize / 8;
762 	struct sge_iq_totals totals = { 0 };
763 	uint_t cidx_incr = 0;
764 	struct rsp_ctrl ctrl;
765 	list_t iql_fwd;
766 
767 	ASSERT3S(event_iq->tsi_iqtype, ==, TIQT_EVENT);
768 	ASSERT3P(event_iq->tsi_intr_evtq, ==, NULL);
769 
770 	IQ_LOCK(event_iq);
771 	if ((event_iq->tsi_flags & IQ_ENABLED) == 0) {
772 		IQ_UNLOCK(event_iq);
773 		return (TIR_DISABLED);
774 	}
775 
776 	list_create(&iql_fwd, sizeof (t4_sge_iq_t),
777 	    offsetof(t4_sge_iq_t, tsi_intr_fwd_node));
778 
779 	while (t4_get_new_rsp(event_iq, &ctrl)) {
780 		const uint8_t rsp_type = G_RSPD_TYPE(ctrl.u.type_gen);
781 		const bool overflowed = (ctrl.u.type_gen & F_RSPD_QOVFL) != 0;
782 
783 		if (overflowed) {
784 			event_iq->tsi_stats.sis_overflow++;
785 		}
786 
787 		const struct rss_header *rss =
788 		    (const struct rss_header *)event_iq->tsi_cdesc;
789 
790 		DTRACE_PROBE3(t4__event__iq__entry, t4_sge_iq_t *, event_iq,
791 		    struct rsp_ctrl *, &ctrl, struct rss_header *, rss);
792 		ASSERT((rsp_type & (X_RSPD_TYPE_CPL | X_RSPD_TYPE_INTR)) != 0);
793 
794 		switch (rsp_type) {
795 		case X_RSPD_TYPE_CPL:
796 			totals.sit_cpl++;
797 			(void) t4_handle_cpl_msg(event_iq, rss, NULL);
798 			break;
799 
800 		case X_RSPD_TYPE_INTR:
801 			totals.sit_intr++;
802 			const uint32_t tgt_qid = BE_32(ctrl.pldbuflen_qid);
803 
804 			t4_sge_iq_t *tgt_iq = *t4_iqmap_slot(sc, tgt_qid);
805 			/*
806 			 * Make sure the forwarded interrupt was sent to the
807 			 * expected event queue.
808 			 */
809 			ASSERT3P(tgt_iq->tsi_intr_evtq, ==, event_iq);
810 
811 			if (!list_link_active(&tgt_iq->tsi_intr_fwd_node)) {
812 				list_insert_tail(&iql_fwd, tgt_iq);
813 			}
814 			break;
815 
816 		default:
817 			cxgb_printf(sc->dip, CE_WARN, "unexpected IQ entry "
818 			    "type %d on IQ %u of type %d", rsp_type,
819 			    event_iq->tsi_cntxt_id, event_iq->tsi_iqtype);
820 			break;
821 		}
822 
823 		t4_iq_next_entry(event_iq);
824 		cidx_incr++;
825 		totals.sit_desc++;
826 		event_iq->tsi_stats.sis_processed++;
827 
828 		if (cidx_incr == desc_limit) {
829 			rc = TIR_BUDGET_MAX;
830 			break;
831 		}
832 	}
833 
834 	/*
835 	 * At this point we may have collected a number of interrupt forwarding
836 	 * entries for Rx IQs, indicating that they have outstanding data ready
837 	 * for consumption. We process those now while still in interrupt
838 	 * context. We remain holding the event IQ's mutex while doing this
839 	 * work. No additional interrupts should be generated for this event IQ
840 	 * until after we have finished processing and re-armed the interrupt
841 	 * via t4_iq_gts_update().
842 	 *
843 	 * There is a finite budget for processing each rx queue, and not all
844 	 * data is guaranteed to be processed as part of this interrupt. Each rx
845 	 * queue should re-arm its interrupt to trigger a fresh interrupt later
846 	 * if polling mode has not been enabled.
847 	 */
848 	t4_sge_iq_t *rx_iq = NULL;
849 	while ((rx_iq = list_remove_head(&iql_fwd)) != NULL) {
850 		(void) t4_process_rx_iq(rx_iq, rx_iq->tsi_qsize / 8, NULL);
851 	}
852 
853 	/*
854 	 * Send an update to the device about the event queue's new cidx and
855 	 * re-arm its interrupt.
856 	 */
857 	ASSERT3U(cidx_incr, >, 0);
858 	t4_iq_gts_update(event_iq, event_iq->tsi_gts_rearm, cidx_incr);
859 	IQ_UNLOCK(event_iq);
860 
861 	DTRACE_PROBE3(t4__event__iq__processed, t4_sge_iq_t *, event_iq,
862 	    struct sge_iq_totals *, &totals, t4_iq_result_t, rc);
863 	return (rc);
864 }
865 
866 /*
867  * Process entries on an Rx Ingress Queue. When called from interrupt context
868  * 'desc_budget' should be non-zero and 'tpr' should be NULL. When called from
869  * polling context 'desc_budget' should be zero and 'tpr' should be non-NULL.
870  */
871 t4_iq_result_t
t4_process_rx_iq(t4_sge_iq_t * rx_iq,uint_t desc_budget,struct t4_poll_req * tpr)872 t4_process_rx_iq(t4_sge_iq_t *rx_iq, uint_t desc_budget,
873     struct t4_poll_req *tpr)
874 {
875 	struct adapter *sc = rx_iq->tsi_adapter;
876 	struct sge_fl *fl = rx_iq->tsi_fl;
877 	struct sge_rxq *rxq = t4_iq_to_rxq(rx_iq);
878 	const uint_t byte_limit = (tpr != NULL) ? tpr->tpr_byte_budget : 0;
879 	mblk_t *mp_head = NULL, **mp_tail = &mp_head;
880 	struct sge_iq_totals totals = { 0 };
881 	uint_t cidx_incr = 0;
882 	struct rsp_ctrl ctrl;
883 	t4_iq_result_t rc = TIR_SUCCESS;
884 
885 	ASSERT3S(rx_iq->tsi_iqtype, ==, TIQT_ETH_RX);
886 	ASSERT3P(rx_iq->tsi_intr_evtq, !=, NULL);
887 	ASSERT3P(rxq, !=, NULL);
888 	/* Rx queues require an FL. */
889 	ASSERT3P(fl, !=, NULL);
890 	/*
891 	 * The desc_budget is used only when processing in interrupt context.
892 	 * The tpr is used only when proessing in polling context.
893 	 */
894 	ASSERT(desc_budget == 0 || tpr == NULL);
895 
896 	IQ_LOCK(rx_iq);
897 	const bool is_polling = (rx_iq->tsi_flags & IQ_POLLING) != 0;
898 	if ((rx_iq->tsi_flags & IQ_ENABLED) == 0) {
899 		IQ_UNLOCK(rx_iq);
900 		return (TIR_DISABLED);
901 	} else if (is_polling && tpr == NULL) {
902 		/*
903 		 * Skip IQ processing driven from interrupt when port is
904 		 * configured for polling.
905 		 */
906 		IQ_UNLOCK(rx_iq);
907 		return (TIR_POLLING);
908 	}
909 
910 	while (t4_get_new_rsp(rx_iq, &ctrl)) {
911 		const uint8_t rsp_type = G_RSPD_TYPE(ctrl.u.type_gen);
912 		const bool overflowed = (ctrl.u.type_gen & F_RSPD_QOVFL) != 0;
913 
914 		if (overflowed) {
915 			rx_iq->tsi_stats.sis_overflow++;
916 		}
917 
918 		const struct rss_header *rss =
919 		    (const struct rss_header *)rx_iq->tsi_cdesc;
920 
921 		DTRACE_PROBE3(t4__rx__iq__entry, t4_sge_iq_t *, rx_iq,
922 		    struct rsp_ctrl *, &ctrl, struct rss_header *, rss);
923 
924 		switch (rsp_type) {
925 		case X_RSPD_TYPE_FLBUF: {
926 			const uint32_t dlen_nb = BE_32(ctrl.pldbuflen_qid);
927 			const struct cpl_rx_pkt *cpl = t4_rss_payload(rss);
928 
929 			if (rss->opcode == CPL_RX_PKT) {
930 				const uint16_t pkt_len = BE_16(cpl->len);
931 				const uint_t new_total =
932 				    totals.sit_rx_bytes + pkt_len;
933 
934 				if (byte_limit != 0 && new_total > byte_limit) {
935 					rc = TIR_BUDGET_MAX;
936 					goto bail;
937 				}
938 			}
939 
940 			const bool newbuf = (dlen_nb & F_RSPD_NEWBUF) != 0;
941 			const uint32_t data_len = G_RSPD_LEN(dlen_nb);
942 			mblk_t *mp = t4_fl_get_payload(fl, data_len, newbuf);
943 			if (mp == NULL) {
944 				/* Rearm IQ with longer-than-default timer */
945 				t4_iq_gts_update(rx_iq, TGC_TIMER5, cidx_incr);
946 				cidx_incr = 0;
947 				rc = TIR_ALLOC_FAIL;
948 				goto bail;
949 			}
950 
951 			/*
952 			 * Add this entry to the totals once we are past the
953 			 * possible bail-outs above.
954 			 */
955 			totals.sit_flbuf++;
956 
957 			if (rss->opcode == CPL_RX_PKT) {
958 				mp->b_rptr += sc->sge.pktshift;
959 
960 				uint16_t err_vec;
961 				if (sc->params.tp.rx_pkt_encap) {
962 					/* Enabled only in T6 config file */
963 					err_vec = G_T6_COMPR_RXERR_VEC(
964 					    ntohs(cpl->err_vec));
965 				} else {
966 					err_vec = ntohs(cpl->err_vec);
967 				}
968 
969 				const bool csum_ok = cpl->csum_calc && !err_vec;
970 
971 				if (csum_ok && !cpl->ip_frag) {
972 					mac_hcksum_set(mp, 0, 0, 0, 0xffff,
973 					    HCK_FULLCKSUM_OK | HCK_FULLCKSUM |
974 					    HCK_IPV4_HDRCKSUM_OK);
975 					rxq->stats.rxcsum++;
976 				}
977 
978 				const uint16_t pkt_len = BE_16(cpl->len);
979 				rxq->stats.rxpkts++;
980 				rxq->stats.rxbytes += pkt_len;
981 				totals.sit_rx_bytes += pkt_len;
982 
983 				*mp_tail = mp;
984 				mp_tail = &mp->b_next;
985 			} else {
986 				(void) t4_handle_cpl_msg(rx_iq, rss, mp);
987 			}
988 			break;
989 		}
990 
991 		default:
992 			cxgb_printf(sc->dip, CE_WARN, "unexpected IQ entry "
993 			    "type %d on IQ %u of type %d", rsp_type,
994 			    rx_iq->tsi_cntxt_id, rx_iq->tsi_iqtype);
995 #ifdef DEBUG
996 			panic("unexpected IQ entry on rx queue");
997 #endif
998 			break;
999 		}
1000 
1001 		t4_iq_next_entry(rx_iq);
1002 		cidx_incr++;
1003 		totals.sit_desc++;
1004 		rx_iq->tsi_stats.sis_processed++;
1005 
1006 		/*
1007 		 * The desc_budget value is non-zero only when processing in
1008 		 * interrupt context. In this case we honor the desc_limit. In
1009 		 * polling mode we are passed a byte-based budget and disregard
1010 		 * the the desc_limit.
1011 		 */
1012 		if (desc_budget != 0 && cidx_incr == desc_budget) {
1013 			rc = TIR_BUDGET_MAX;
1014 			goto bail;
1015 		}
1016 	}
1017 
1018 bail:
1019 	if (tpr != NULL) {
1020 		/*
1021 		 * Do not re-arm interrupts while this IQ is being polled.
1022 		 * Just update the CIDX as necessary.
1023 		 */
1024 		if (cidx_incr != 0) {
1025 			t4_iq_gts_incr(rx_iq, cidx_incr);
1026 		}
1027 	} else {
1028 		/*
1029 		 * Just being extra sure that any future code changes keep this
1030 		 * code path to interrupt processing only.
1031 		 */
1032 		ASSERT3U(desc_budget, >, 0);
1033 		ASSERT3P(tpr, ==, NULL);
1034 
1035 		/*
1036 		 * Make sure to re-arm the interrupt for this rx queue.
1037 		 * Remember, the actual interrupt is delivered to the event
1038 		 * queue (rq_iq->tsi_intr_evtq), but the generation of the
1039 		 * forwarded interrupt event requires arming the interrupt on
1040 		 * this rx queue.
1041 		 */
1042 		t4_iq_gts_update(rx_iq, rx_iq->tsi_gts_rearm, cidx_incr);
1043 	}
1044 
1045 	/*
1046 	 * Take a snapshot of the ring generation number prior to dropping the
1047 	 * IQ/RXQ lock, in case we need it to pass packets into the mac RX path.
1048 	 */
1049 	const uint64_t ring_gen_num = rxq->ring_gen_num;
1050 	IQ_UNLOCK(rx_iq);
1051 
1052 	/*
1053 	 * First we deliver the packets up to mac to give the client a chance to
1054 	 * consume these mblks before the driver attempts to refill them.
1055 	 */
1056 	if (mp_head != NULL) {
1057 		if (tpr != NULL) {
1058 			tpr->tpr_mp = mp_head;
1059 		} else {
1060 			mac_rx_ring(rxq->port->mh, rxq->ring_handle, mp_head,
1061 			    ring_gen_num);
1062 		}
1063 	}
1064 
1065 	/*
1066 	 * Next we refill some FL buffers. If the FL is "starving", we enqueue
1067 	 * it on the starving list for further refilling on a background
1068 	 * thread.
1069 	 */
1070 	if (fl != NULL && t4_fl_periodic_refill(fl)) {
1071 		t4_sfl_enqueue(sc, fl);
1072 	}
1073 	DTRACE_PROBE3(t4__rx__iq__processed, t4_sge_iq_t *, rx_iq,
1074 	    struct sge_iq_totals *, &totals, t4_iq_result_t, rc);
1075 	return (rc);
1076 }
1077 
1078 /* Per-packet header in a coalesced tx WR, before the SGL starts (in flits) */
1079 #define	TXPKTS_PKT_HDR_FLITS ((\
1080 	sizeof (struct ulp_txpkt) + \
1081 	sizeof (struct ulptx_idata) + \
1082 	sizeof (struct cpl_tx_pkt_core)) / FLIT_NUM_BYTES)
1083 
1084 /* Header of a coalesced tx WR, before SGL of first packet (in flits) */
1085 #define	TXPKTS_WR_HDR_FLITS (\
1086 	sizeof (struct fw_eth_tx_pkts_wr) / FLIT_NUM_BYTES + \
1087 	TXPKTS_PKT_HDR_FLITS)
1088 
1089 /* Header of a tx WR, before SGL of first packet (in flits) */
1090 #define	TXPKT_WR_HDR_FLITS ((\
1091 	sizeof (struct fw_eth_tx_pkt_wr) + \
1092 	sizeof (struct cpl_tx_pkt_core)) / FLIT_NUM_BYTES)
1093 
1094 /* Header of a tx LSO WR, before SGL of first packet (in flits) */
1095 #define	TXPKT_LSO_WR_HDR_FLITS ((\
1096 	sizeof (struct fw_eth_tx_pkt_wr) + \
1097 	sizeof (struct cpl_tx_pkt_lso_core) + \
1098 	sizeof (struct cpl_tx_pkt_core)) / FLIT_NUM_BYTES)
1099 
1100 mblk_t *
t4_eth_tx(void * arg,mblk_t * frame)1101 t4_eth_tx(void *arg, mblk_t *frame)
1102 {
1103 	struct sge_txq *txq = arg;
1104 	struct port_info *pi = txq->port;
1105 	t4_sge_eq_t *eq = &txq->eq;
1106 	mblk_t *next_frame = NULL;
1107 	int coalescing = 0;
1108 	struct txpkts txpkts = {};
1109 	struct txinfo txinfo = {};
1110 
1111 	txpkts.npkt = 0; /* indicates there's nothing in txpkts */
1112 
1113 	TXQ_LOCK(txq);
1114 	if ((eq->tse_flags & EQ_ENABLED) == 0) {
1115 		/* Apply flow control until EQ is enabled. */
1116 		TXQ_UNLOCK(txq);
1117 		return (frame);
1118 	}
1119 
1120 	/* We always strive to send the maximum size WR. */
1121 	if (eq->tse_avail < TX_WR_MAX_CREDITS) {
1122 		(void) t4_tx_reclaim_credits(txq, TX_WR_MAX_CREDITS, NULL);
1123 	}
1124 	for (; frame != NULL; frame = next_frame) {
1125 		int rc = 0;
1126 
1127 		if (eq->tse_avail < TX_WR_MAX_CREDITS)
1128 			break;
1129 
1130 		next_frame = frame->b_next;
1131 		frame->b_next = NULL;
1132 
1133 		if (next_frame != NULL)
1134 			coalescing = 1;
1135 
1136 		rc = get_frame_txinfo(txq, &frame, &txinfo, coalescing);
1137 		if (rc != 0) {
1138 			if (rc == ENOMEM) {
1139 				/* Short of resources, suspend tx */
1140 				frame->b_next = next_frame;
1141 
1142 				/*
1143 				 * Since we are out of memory for this packet,
1144 				 * rather than TX descriptors, enqueue an
1145 				 * flush work request.  This will ensure that a
1146 				 * completion notification is delivered for this
1147 				 * EQ which will trigger a call to update the
1148 				 * state in mac to continue transmissions.
1149 				 */
1150 				t4_write_flush_wr(txq);
1151 				break;
1152 			}
1153 
1154 			/*
1155 			 * Unrecoverable error for this frame, throw it away and
1156 			 * move on to the next.
1157 			 */
1158 			freemsg(frame);
1159 			continue;
1160 		}
1161 
1162 		if (coalescing != 0 &&
1163 		    add_to_txpkts(txq, &txpkts, frame, &txinfo) == 0) {
1164 			/* Successfully absorbed into txpkts */
1165 			write_ulp_cpl_sgl(pi, txq, &txpkts, &txinfo);
1166 			goto doorbell;
1167 		}
1168 
1169 		/*
1170 		 * We weren't coalescing to begin with, or current frame could
1171 		 * not be coalesced (add_to_txpkts flushes txpkts if a frame
1172 		 * given to it can't be coalesced).  Either way there should be
1173 		 * nothing in txpkts.
1174 		 */
1175 		ASSERT(txpkts.npkt == 0);
1176 
1177 		/* We're sending out individual frames now */
1178 		coalescing = 0;
1179 
1180 		if (eq->tse_avail < TX_WR_MAX_CREDITS) {
1181 			(void) t4_tx_reclaim_credits(txq, TX_WR_MAX_CREDITS,
1182 			    NULL);
1183 		}
1184 
1185 		rc = write_txpkt_wr(pi, txq, frame, &txinfo);
1186 		if (rc != 0) {
1187 
1188 			/* Short of hardware descriptors, suspend tx */
1189 
1190 			/*
1191 			 * This is an unlikely but expensive failure.  We've
1192 			 * done all the hard work (DMA bindings etc.) and now we
1193 			 * can't send out the frame.  What's worse, we have to
1194 			 * spend even more time freeing up everything in txinfo.
1195 			 */
1196 			txq->stats.qfull++;
1197 			free_txinfo_resources(txq, &txinfo);
1198 
1199 			frame->b_next = next_frame;
1200 			break;
1201 		}
1202 
1203 doorbell:
1204 		/* Fewer and fewer doorbells as the queue fills up */
1205 		if (eq->tse_pending >=
1206 		    (1 << (fls(eq->tse_qsize - eq->tse_avail) / 2))) {
1207 			txq->stats.txbytes += txinfo.len;
1208 			txq->stats.txpkts++;
1209 			t4_tx_ring_db(txq);
1210 		}
1211 		(void) t4_tx_reclaim_credits(txq, 32, NULL);
1212 	}
1213 
1214 	if (txpkts.npkt > 0) {
1215 		write_txpkts_wr(txq, &txpkts);
1216 	}
1217 
1218 	if (eq->tse_pending != 0) {
1219 		t4_tx_ring_db(txq);
1220 	}
1221 
1222 	if (frame != NULL) {
1223 		eq->tse_flags |= EQ_CORKED;
1224 	}
1225 
1226 	(void) t4_tx_reclaim_credits(txq, eq->tse_qsize, NULL);
1227 	TXQ_UNLOCK(txq);
1228 
1229 	return (frame);
1230 }
1231 
1232 static int
t4_alloc_iq(struct port_info * pi,const t4_iq_params_t * tip,t4_sge_iq_t * iq,struct sge_fl * fl)1233 t4_alloc_iq(struct port_info *pi, const t4_iq_params_t *tip, t4_sge_iq_t *iq,
1234     struct sge_fl *fl)
1235 {
1236 	struct adapter *sc = pi->adapter;
1237 	int rc;
1238 
1239 	ASSERT(tip->tip_tmr_idx >= 0 && tip->tip_tmr_idx < SGE_NTIMERS);
1240 	ASSERT(tip->tip_pktc_idx < SGE_NCOUNTERS);
1241 	ASSERT(tip->tip_cong_chan == -1 || tip->tip_cong_chan > 0);
1242 
1243 	const bool intr_fwd = (tip->tip_intr_evtq != NULL);
1244 	const uint_t intr_idx =
1245 	    intr_fwd ? tip->tip_intr_evtq->tsi_cntxt_id : tip->tip_intr_idx;
1246 
1247 	ASSERT(intr_fwd || intr_idx < sc->intr_queue_cfg.intr_count);
1248 
1249 	mutex_init(&iq->tsi_lock, NULL, MUTEX_DRIVER,
1250 	    DDI_INTR_PRI(DDI_INTR_PRI(sc->intr_pri)));
1251 	iq->tsi_flags = 0;
1252 	iq->tsi_iqtype = tip->tip_iq_type;
1253 	iq->tsi_adapter = sc;
1254 	iq->tsi_gts_rearm = V_QINTR_TIMER_IDX(tip->tip_tmr_idx);
1255 	iq->tsi_intr_pktc_idx = -1;
1256 	if (tip->tip_pktc_idx >= 0) {
1257 		iq->tsi_gts_rearm |= TGC_SE_INTR_ARM;
1258 		iq->tsi_intr_pktc_idx = tip->tip_pktc_idx;
1259 	}
1260 
1261 	/*
1262 	 * The tsi_qsize holds the number of total entries in the queue, but the
1263 	 * device requires that this number be a multiple of 16. See the
1264 	 * documentation for FW_IQ_CMD in the Firmware Interface Book.
1265 	 */
1266 	iq->tsi_qsize = P2ROUNDUP(tip->tip_qsize, 16);
1267 	/*
1268 	 * The last entry is always reserved for the status page, even if status
1269 	 * page updates are not being utilized.
1270 	 */
1271 	iq->tsi_cap = iq->tsi_qsize - 1;
1272 	iq->tsi_esize = tip->tip_esize;
1273 	iq->tsi_esize_bytes = t4_iq_esize_bytes[iq->tsi_esize];
1274 	iq->tsi_intr_evtq = intr_fwd ? tip->tip_intr_evtq : NULL;
1275 	iq->tsi_intr_idx = intr_fwd ? INTR_FORWARDED : intr_idx;
1276 
1277 	const size_t len = iq->tsi_qsize * iq->tsi_esize_bytes;
1278 	rc = alloc_desc_ring(sc, len, DDI_DMA_READ, &iq->tsi_desc_dhdl,
1279 	    &iq->tsi_desc_ahdl, &iq->tsi_desc_ba, (caddr_t *)&iq->tsi_desc);
1280 	if (rc != 0) {
1281 		mutex_destroy(&iq->tsi_lock);
1282 		return (rc);
1283 	}
1284 	iq->tsi_flags |= IQ_ALLOC_HOST;
1285 
1286 	/*
1287 	 * If the coalescing counter is not enabled for this IQ, use the 0
1288 	 * index, rather than populating it with the invalid -1 value.
1289 	 *
1290 	 * The selected index does not matter when the counter is not enabled
1291 	 * through the GTS flags.
1292 	 */
1293 	const uint_t pktc_idx = (iq->tsi_intr_pktc_idx < 0) ? 0 :
1294 	    iq->tsi_intr_pktc_idx;
1295 	const bool is_fwq = (iq == &sc->sge.fwq);
1296 
1297 	struct fw_iq_cmd iq_cmd;
1298 	bzero(&iq_cmd, sizeof (iq_cmd));
1299 
1300 	iq_cmd.op_to_vfn = BE_32(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST |
1301 	    F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_IQ_CMD_PFN(sc->pf) |
1302 	    V_FW_IQ_CMD_VFN(0));
1303 
1304 	iq_cmd.alloc_to_len16 = BE_32(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART |
1305 	    FW_LEN16(struct fw_iq_cmd));
1306 
1307 	iq_cmd.type_to_iqandstindex = BE_32(
1308 	    /* Special handling for firmware event queue */
1309 	    (is_fwq ? F_FW_IQ_CMD_IQASYNCH : 0) |
1310 	    (intr_fwd ? F_FW_IQ_CMD_IQANDST : 0) |
1311 	    V_FW_IQ_CMD_IQANDSTINDEX(intr_idx) |
1312 	    V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
1313 	    V_FW_IQ_CMD_VIID(pi->viid) |
1314 	    V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_INTERRUPT));
1315 
1316 	iq_cmd.iqdroprss_to_iqesize = BE_16(V_FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
1317 	    F_FW_IQ_CMD_IQGTSMODE | V_FW_IQ_CMD_IQINTCNTTHRESH(pktc_idx) |
1318 	    V_FW_IQ_CMD_IQESIZE(iq->tsi_esize));
1319 
1320 	iq_cmd.iqsize = BE_16(iq->tsi_qsize);
1321 	iq_cmd.iqaddr = BE_64(iq->tsi_desc_ba);
1322 	iq_cmd.iqns_to_fl0congen = tip->tip_cong_chan == -1 ? 0 :
1323 	    BE_32(F_FW_IQ_CMD_IQFLINTCONGEN);
1324 
1325 	/*
1326 	 * This setting currently only pertains to T4/T5 parts with 2 ports, and
1327 	 * its only effect is to correct a bug in setting the IQPCIECH related
1328 	 * to offload queues (Chelsio bug#34516). Therefore, setting it is
1329 	 * irrelevant for our driver. However, we set it anyways in case a
1330 	 * future part or fimrware revision decides to use this information for
1331 	 * other purposes relevant the behavior of our driver.
1332 	 */
1333 	iq_cmd.iqns_to_fl0congen |= BE_32(V_FW_IQ_CMD_IQTYPE(FW_IQ_IQTYPE_NIC));
1334 
1335 	if (fl != NULL) {
1336 		t4_sge_eq_t *eq = &fl->eq;
1337 
1338 		iq->tsi_fl = fl;
1339 		bzero(&fl->stats, sizeof (fl->stats));
1340 
1341 		fl->bufs_cap = tip->tip_fl_qsize;
1342 		eq->tse_flags = 0;
1343 		eq->tse_qsize = EQ_FLITS_TO_HC(fl->bufs_cap);
1344 
1345 		if ((rc = t4_alloc_eq_base(pi, eq)) != 0) {
1346 			t4_free_iq(pi, iq);
1347 			return (rc);
1348 		}
1349 
1350 		fl->bufs_lowat = P2ROUNDUP(sc->sge.fl_starve_threshold, 8);
1351 
1352 		/*
1353 		 * In T6, for egress queue type FL there is internal overhead
1354 		 * of 16B for header going into FLM module.  Hence the maximum
1355 		 * allowed burst size is 448 bytes.  For T4/T5, the hardware
1356 		 * doesn't coalesce fetch requests if more than 64 bytes of
1357 		 * Free List pointers are provided, so we use a 128-byte Fetch
1358 		 * Burst Minimum there (T6 implements coalescing so we can use
1359 		 * the smaller 64-byte value there).
1360 		 */
1361 		const uint_t fbmin = t4_cver_ge(sc, CHELSIO_T6) ?
1362 		    X_FETCHBURSTMIN_64B_T6: X_FETCHBURSTMIN_128B;
1363 		const uint_t fbmax = t4_cver_ge(sc, CHELSIO_T6) ?
1364 		    X_FETCHBURSTMAX_256B : X_FETCHBURSTMAX_512B;
1365 		const uint32_t fl_cong = (tip->tip_cong_chan == -1) ? 0 :
1366 		    (V_FW_IQ_CMD_FL0CNGCHMAP(tip->tip_cong_chan) |
1367 		    F_FW_IQ_CMD_FL0CONGCIF |
1368 		    F_FW_IQ_CMD_FL0CONGEN);
1369 
1370 		iq_cmd.iqns_to_fl0congen |= BE_32(
1371 		    V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) |
1372 		    F_FW_IQ_CMD_FL0PACKEN |
1373 		    F_FW_IQ_CMD_FL0PADEN |
1374 		    fl_cong);
1375 		/*
1376 		 * We do not set cidx flushing because we choose to have no cidx
1377 		 * updates for an FL. Instead we track FL usage implicitly by
1378 		 * the incoming CPL messages on the Rx IQ.
1379 		 */
1380 		iq_cmd.fl0dcaen_to_fl0cidxfthresh |= BE_16(
1381 		    V_FW_IQ_CMD_FL0FBMIN(fbmin) |
1382 		    V_FW_IQ_CMD_FL0FBMAX(fbmax));
1383 		iq_cmd.fl0size |= BE_16(eq->tse_qsize_spg);
1384 		iq_cmd.fl0addr |= BE_64(eq->tse_ring_ba);
1385 	}
1386 	if (!intr_fwd) {
1387 		iq->tsi_flags |= IQ_INTR;
1388 	}
1389 
1390 	rc = -t4_wr_mbox(sc, sc->mbox, &iq_cmd, sizeof (iq_cmd), &iq_cmd);
1391 	if (rc != 0) {
1392 		cxgb_printf(sc->dip, CE_WARN,
1393 		    "failed to create ingress queue: %d", rc);
1394 		t4_free_iq(pi, iq);
1395 		return (rc);
1396 	}
1397 	iq->tsi_cntxt_id = BE_16(iq_cmd.iqid);
1398 	iq->tsi_abs_id = BE_16(iq_cmd.physiqid);
1399 	iq->tsi_flags |= IQ_ALLOC_DEV;
1400 
1401 	iq->tsi_cdesc = iq->tsi_desc;
1402 	iq->tsi_cidx = 0;
1403 	iq->tsi_gen = F_RSPD_GEN;
1404 	iq->tsi_adapter = sc;
1405 
1406 	*t4_iqmap_slot(sc, iq->tsi_cntxt_id) = iq;
1407 
1408 	if (fl != NULL) {
1409 		t4_sge_eq_t *eq = &fl->eq;
1410 
1411 		eq->tse_cntxt_id = BE_16(iq_cmd.fl0id);
1412 
1413 		CTASSERT(offsetof(struct sge_fl, eq) == 0);
1414 		*t4_eqmap_slot(sc, eq->tse_cntxt_id) = (t4_sge_eq_t *)fl;
1415 		eq->tse_flags |= EQ_ALLOC_DEV;
1416 		eq->tse_pidx = eq->tse_cidx = 0;
1417 		t4_alloc_eq_post(pi, eq);
1418 		fl->copy_threshold = rx_copy_threshold;
1419 
1420 		/* Allocate space for one software descriptor per buffer. */
1421 		const size_t sdesc_sz = fl->bufs_cap * sizeof (struct fl_sdesc);
1422 		fl->sdesc = kmem_zalloc(sdesc_sz, KM_SLEEP);
1423 		eq->tse_flags |= EQ_ALLOC_DESC;
1424 
1425 		FL_LOCK(fl);
1426 		(void) t4_fl_refill(fl, fl->bufs_lowat);
1427 		FL_UNLOCK(fl);
1428 	}
1429 
1430 	if (t4_cver_ge(sc, CHELSIO_T5) && tip->tip_cong_chan != -1) {
1431 		const uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
1432 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
1433 		    V_FW_PARAMS_PARAM_YZ(iq->tsi_cntxt_id);
1434 
1435 		const uint_t congmap_log = sc->params.arch.cng_ch_bits_log;
1436 		uint32_t val =
1437 		    V_CONMCTXT_CNGTPMODE(X_CONMCTXT_CNGTPMODE_CHANNEL);
1438 		for (uint_t i = 0; i < 4; i++) {
1439 			if (tip->tip_cong_chan & (1 << i)) {
1440 				val |= (1 << (i << congmap_log));
1441 			}
1442 		}
1443 
1444 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
1445 		if (rc != 0) {
1446 			/* report error but carry on */
1447 			cxgb_printf(sc->dip, CE_WARN,
1448 			    "failed to set congestion manager context for "
1449 			    "ingress queue %d: %d", iq->tsi_cntxt_id, rc);
1450 		}
1451 	}
1452 
1453 	/* Enable event (and firmware) queues IQs immediately */
1454 	if (iq->tsi_iqtype == TIQT_EVENT) {
1455 		iq->tsi_flags |= IQ_ENABLED;
1456 		t4_iq_gts_update(iq, iq->tsi_gts_rearm, 0);
1457 	}
1458 
1459 	return (0);
1460 }
1461 
1462 static void
t4_free_iq(struct port_info * pi,t4_sge_iq_t * iq)1463 t4_free_iq(struct port_info *pi, t4_sge_iq_t *iq)
1464 {
1465 	struct adapter *sc = iq->tsi_adapter;
1466 	struct sge_fl *fl = iq->tsi_fl;
1467 	t4_sge_eq_t *eq = fl != NULL ? &fl->eq : NULL;
1468 
1469 	/*
1470 	 * The onus is placed on the caller to ensure that no further activity
1471 	 * will occur on this IQ.
1472 	 */
1473 	iq->tsi_flags &= ~IQ_ENABLED;
1474 
1475 	if (iq->tsi_flags & IQ_ALLOC_DEV) {
1476 		/*
1477 		 * Device-side resources of freelists are allocated in concert
1478 		 * with the device-side resources of their associated IQ.
1479 		 */
1480 		ASSERT(fl == NULL || (eq->tse_flags & EQ_ALLOC_DEV));
1481 
1482 		const uint16_t eq_cntxid = fl ? eq->tse_cntxt_id : 0xffff;
1483 		int rc = -t4_iq_free(sc, sc->mbox, sc->pf, 0,
1484 		    FW_IQ_TYPE_FL_INT_CAP, iq->tsi_cntxt_id, eq_cntxid, 0xffff);
1485 		if (rc != 0) {
1486 			cxgb_printf(sc->dip, CE_WARN,
1487 			    "failed to free IQ/FL (%x/%x): %d",
1488 			    iq->tsi_cntxt_id, eq_cntxid, rc);
1489 			/* attempt to complete the rest of clean-up */
1490 		}
1491 		iq->tsi_flags &= ~IQ_ALLOC_DEV;
1492 		if (fl != NULL) {
1493 			eq->tse_flags &= ~EQ_ALLOC_DEV;
1494 		}
1495 	}
1496 	if (iq->tsi_flags & IQ_ALLOC_HOST) {
1497 		(void) free_desc_ring(&iq->tsi_desc_dhdl, &iq->tsi_desc_ahdl);
1498 		iq->tsi_desc = NULL;
1499 		iq->tsi_cdesc = NULL;
1500 		iq->tsi_desc_ba = 0;
1501 		mutex_destroy(&iq->tsi_lock);
1502 		iq->tsi_flags &= ~IQ_ALLOC_HOST;
1503 	}
1504 	iq->tsi_flags &= ~IQ_INTR;
1505 	ASSERT0(iq->tsi_flags);
1506 
1507 	iq->tsi_intr_idx = 0;
1508 	iq->tsi_intr_evtq = NULL;
1509 	iq->tsi_iqtype = TIQT_UNINIT;
1510 
1511 	if (fl != NULL) {
1512 		if (eq->tse_flags & EQ_ALLOC_DESC) {
1513 			FL_LOCK(fl);
1514 			t4_fl_free_bufs(fl);
1515 			FL_UNLOCK(fl);
1516 
1517 			kmem_free(fl->sdesc, fl->bufs_cap *
1518 			    sizeof (struct fl_sdesc));
1519 			fl->sdesc = NULL;
1520 
1521 			eq->tse_flags &= ~EQ_ALLOC_DESC;
1522 		}
1523 		t4_free_eq(pi, eq);
1524 		iq->tsi_fl = NULL;
1525 
1526 		ASSERT0(eq->tse_flags);
1527 	}
1528 }
1529 
1530 int
t4_alloc_evt_iqs(struct adapter * sc)1531 t4_alloc_evt_iqs(struct adapter *sc)
1532 {
1533 	const t4_intr_plan_t plan = sc->intr_queue_cfg.intr_plan;
1534 
1535 	const t4_iq_params_t fwq_iqp = {
1536 		.tip_iq_type	= TIQT_EVENT,
1537 		.tip_tmr_idx	= sc->sge.fwq_tmr_idx,
1538 		.tip_pktc_idx	= sc->sge.fwq_pktc_idx,
1539 		.tip_qsize	= FW_IQ_QSIZE,
1540 		.tip_esize	= FW_IQ_ESIZE,
1541 		.tip_cong_chan	= -1,
1542 		.tip_intr_evtq	= NULL,
1543 		/*
1544 		 * The device error-handling interrupt always occupies the 0th
1545 		 * slot, which the firmware queue will share if no additional
1546 		 * interrupts are available.  Otherwise it uses the next slot
1547 		 * after that.
1548 		 */
1549 		.tip_intr_idx	= (plan == TIP_SINGLE) ? 0 : 1,
1550 	};
1551 	const int rc = t4_alloc_iq(sc->port[0], &fwq_iqp, &sc->sge.fwq, NULL);
1552 	if (rc != 0) {
1553 		cxgb_printf(sc->dip, CE_WARN,
1554 		    "failed to create firmware event queue: %d.", rc);
1555 		return (rc);
1556 	}
1557 
1558 	if (plan == TIP_PER_PORT) {
1559 		const uint_t ipp = sc->intr_queue_cfg.intr_per_port;
1560 		const uint_t port_count = sc->params.nports;
1561 
1562 		for (uint_t i = 0; i < port_count; i++) {
1563 			struct port_info *port = sc->port[i];
1564 
1565 			for (uint_t j = 0; j < ipp; j++) {
1566 				const t4_iq_params_t iqp = {
1567 					.tip_iq_type	= TIQT_EVENT,
1568 					.tip_tmr_idx	= sc->sge.fwq_tmr_idx,
1569 					.tip_pktc_idx	= sc->sge.fwq_pktc_idx,
1570 					.tip_qsize	= FW_IQ_QSIZE,
1571 					.tip_esize	= FW_IQ_ESIZE,
1572 					.tip_cong_chan	= -1,
1573 					.tip_intr_evtq	= NULL,
1574 					.tip_intr_idx	= 2 + (i * ipp) + j,
1575 				};
1576 
1577 				const int rc = t4_alloc_iq(port, &iqp,
1578 				    &port->intr_iqs[j], NULL);
1579 				if (rc != 0) {
1580 					cxgb_printf(sc->dip, CE_WARN,
1581 					    "failed to create interrupt event "
1582 					    "queue %u for port %u: %d.", j, i,
1583 					    rc);
1584 					t4_free_evt_iqs(sc);
1585 					return (rc);
1586 				}
1587 			}
1588 		}
1589 	}
1590 
1591 	return (0);
1592 }
1593 
1594 void
t4_free_evt_iqs(struct adapter * sc)1595 t4_free_evt_iqs(struct adapter *sc)
1596 {
1597 	const uint_t port_count = sc->params.nports;
1598 
1599 	for (uint_t i = 0; i < port_count; i++) {
1600 		struct port_info *port = sc->port[i];
1601 
1602 		for (uint_t j = 0; j < sc->intr_queue_cfg.intr_per_port; j++) {
1603 			t4_free_iq(port, &port->intr_iqs[j]);
1604 		}
1605 	}
1606 
1607 	t4_free_iq(sc->port[0], &sc->sge.fwq);
1608 }
1609 
1610 static int
t4_alloc_rxq(struct port_info * pi,struct sge_rxq * rxq,uint_t q_idx)1611 t4_alloc_rxq(struct port_info *pi, struct sge_rxq *rxq, uint_t q_idx)
1612 {
1613 	struct adapter *sc = pi->adapter;
1614 
1615 	rxq->port = pi;
1616 
1617 	t4_iq_params_t iqp = {
1618 		.tip_iq_type	= TIQT_ETH_RX,
1619 		.tip_tmr_idx	= pi->tmr_idx,
1620 		.tip_pktc_idx	= pi->pktc_idx,
1621 		.tip_qsize	= sc->props.qsize_rxq,
1622 		.tip_esize	= RX_IQ_ESIZE,
1623 		.tip_fl_qsize	= sc->props.qsize_rxq,
1624 		.tip_cong_chan	= t4_get_tp_ch_map(sc, pi->tx_chan),
1625 	};
1626 	t4_rxq_intr_assign(pi, q_idx, &iqp);
1627 	const int rc = t4_alloc_iq(pi, &iqp, &rxq->iq, &rxq->fl);
1628 	if (rc != 0) {
1629 		return (rc);
1630 	}
1631 
1632 	rxq->ksp = setup_rxq_kstats(pi, rxq, q_idx);
1633 	return (0);
1634 }
1635 
1636 static void
t4_free_rxq(struct port_info * pi,struct sge_rxq * rxq)1637 t4_free_rxq(struct port_info *pi, struct sge_rxq *rxq)
1638 {
1639 	if (rxq->ksp != NULL) {
1640 		kstat_delete(rxq->ksp);
1641 		rxq->ksp = NULL;
1642 	}
1643 
1644 	t4_free_iq(pi, &rxq->iq);
1645 }
1646 
1647 static int
t4_alloc_eq_base(struct port_info * pi,t4_sge_eq_t * eq)1648 t4_alloc_eq_base(struct port_info *pi, t4_sge_eq_t *eq)
1649 {
1650 	struct adapter *sc = pi->adapter;
1651 	ASSERT0(eq->tse_flags);
1652 	mutex_init(&eq->tse_lock, NULL, MUTEX_DRIVER,
1653 	    DDI_INTR_PRI(sc->intr_pri));
1654 
1655 	/*
1656 	 * Make sure to account for the status page which sits at the end of the
1657 	 * hardware ring and may consume one or two credits.
1658 	 */
1659 	ASSERT3U(eq->tse_qsize, <=, T4_MAX_EQ_SIZE);
1660 	eq->tse_qsize_spg = eq->tse_qsize + sc->sge.eq_spg_len;
1661 
1662 	/*
1663 	 * We are allocating the "hardware" ring to hold the host credits, make
1664 	 * sure to use tse_qsize_spg to include the status page credits.
1665 	 */
1666 	const size_t len = eq->tse_qsize_spg * EQ_HC_SIZE;
1667 	int rc = alloc_desc_ring(sc, len, DDI_DMA_WRITE, &eq->tse_ring_dhdl,
1668 	    &eq->tse_ring_ahdl, &eq->tse_ring_ba, (caddr_t *)&eq->tse_ring);
1669 	if (rc != 0) {
1670 		mutex_destroy(&eq->tse_lock);
1671 		return (rc);
1672 	}
1673 	eq->tse_flags |= EQ_ALLOC_HOST;
1674 
1675 	/*
1676 	 * We always use one credit less than the technical capacity to avoid
1677 	 * the situation where pidx == cidx which would indicate to the hardware
1678 	 * that the queue is empty.
1679 	 */
1680 	eq->tse_avail = eq->tse_qsize - 1;
1681 	eq->tse_pending = 0;
1682 	eq->tse_pidx = 0;
1683 	eq->tse_cidx = 0;
1684 	eq->tse_spg = t4_eq_credit(eq, eq->tse_qsize);
1685 
1686 	return (0);
1687 }
1688 
1689 #define	UDB_DBS	(DOORBELL_UDB | DOORBELL_UDBWC | DOORBELL_WCWR)
1690 
1691 static void
t4_alloc_eq_post(struct port_info * pi,t4_sge_eq_t * eq)1692 t4_alloc_eq_post(struct port_info *pi, t4_sge_eq_t *eq)
1693 {
1694 	struct adapter *sc = pi->adapter;
1695 	const boolean_t udb = (sc->doorbells & UDB_DBS) != 0;
1696 	ASSERT(eq->tse_flags & EQ_ALLOC_DEV);
1697 
1698 	eq->tse_doorbells = sc->doorbells;
1699 	if (udb) {
1700 		uint64_t udb_offset;
1701 		uint_t udb_qid;
1702 
1703 		const int rc = t4_bar2_sge_qregs(sc, eq->tse_cntxt_id,
1704 		    T4_BAR2_QTYPE_EGRESS, 0, &udb_offset, &udb_qid);
1705 		if (rc == 0) {
1706 			eq->tse_udb = sc->bar2_ptr + udb_offset;
1707 			eq->tse_udb_qid = udb_qid;
1708 		} else {
1709 			eq->tse_doorbells &= ~UDB_DBS;
1710 			eq->tse_udb = NULL;
1711 			eq->tse_udb_qid = 0;
1712 		}
1713 	}
1714 }
1715 
1716 static int
t4_eq_alloc_eth(struct port_info * pi,t4_sge_eq_t * eq)1717 t4_eq_alloc_eth(struct port_info *pi, t4_sge_eq_t *eq)
1718 {
1719 	struct adapter *sc = pi->adapter;
1720 	int rc;
1721 
1722 	if ((rc = t4_alloc_eq_base(pi, eq)) != 0) {
1723 		return (rc);
1724 	}
1725 
1726 	struct fw_eq_eth_cmd c = {
1727 		.op_to_vfn = BE_32(
1728 		    V_FW_CMD_OP(FW_EQ_ETH_CMD) |
1729 		    F_FW_CMD_REQUEST | F_FW_CMD_WRITE | F_FW_CMD_EXEC |
1730 		    V_FW_EQ_ETH_CMD_PFN(sc->pf) |
1731 		    V_FW_EQ_ETH_CMD_VFN(0)),
1732 		.alloc_to_len16 = BE_32(
1733 		    F_FW_EQ_ETH_CMD_ALLOC |
1734 		    F_FW_EQ_ETH_CMD_EQSTART |
1735 		    FW_LEN16(struct fw_eq_eth_cmd)),
1736 		.autoequiqe_to_viid = BE_32(
1737 		    F_FW_EQ_ETH_CMD_AUTOEQUIQE |
1738 		    F_FW_EQ_ETH_CMD_AUTOEQUEQE |
1739 		    V_FW_EQ_ETH_CMD_VIID(pi->viid)),
1740 		.fetchszm_to_iqid = BE_32(
1741 		    V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_BOTH) |
1742 		    V_FW_EQ_ETH_CMD_PCIECHN(eq->tse_tx_chan) |
1743 		    F_FW_EQ_ETH_CMD_FETCHRO |
1744 		    V_FW_EQ_ETH_CMD_IQID(eq->tse_iqid)),
1745 		.dcaen_to_eqsize = BE_32(
1746 		    V_FW_EQ_ETH_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
1747 		    V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
1748 		    V_FW_EQ_ETH_CMD_CIDXFTHRESH(X_CIDXFLUSHTHRESH_32) |
1749 		    V_FW_EQ_ETH_CMD_EQSIZE(eq->tse_qsize_spg)),
1750 		.eqaddr = BE_64(eq->tse_ring_ba),
1751 	};
1752 
1753 	/*
1754 	 * The T4 is configured to send a notification for every 32 consumed
1755 	 * host credits (X_CIDXFLUSHTHRESH_32). During times of periodic Tx
1756 	 * traffic that threshold may not be reached with regularity, leaving
1757 	 * outstanding credits that cannot be reclaimed until more traffic is
1758 	 * sent. This can result in a situation where the device driver is
1759 	 * unable to shutdown and detach. To alleviate this problem two methods
1760 	 * may be employed:
1761 	 *
1762 	 * 1. The DBQ timer can be configured to arm and deliver a notification
1763 	 *    after the EQ has gone idle for a period of time. This is available
1764 	 *    on T6 and later adapters.
1765 	 *
1766 	 * 2. The CIDXFlushThresholdOverride flag (also documented under
1767 	 *    FCThreshOverride flag in the T6 Programmers Guide) will send a
1768 	 *    notification whenever a consumed credit causes CDIX==PIDX, even if
1769 	 *    the CIDXFlushThreshold has not been reached.
1770 	 *
1771 	 * The DBQ timer is preferred, as it results in less notifications when
1772 	 * the EQ is kept busy with frequent single-credit transmissions.
1773 	 */
1774 	if (sc->flags & TAF_DBQ_TIMER) {
1775 		/* Configure the DBQ timer when it is available */
1776 		c.timeren_timerix = BE_32(
1777 		    F_FW_EQ_ETH_CMD_TIMEREN |
1778 		    V_FW_EQ_ETH_CMD_TIMERIX(pi->dbq_timer_idx));
1779 	} else {
1780 		/* Otherwise fall back to CIDXFlushThresholdOverride */
1781 		c.dcaen_to_eqsize |= BE_32(F_FW_EQ_ETH_CMD_CIDXFTHRESHO);
1782 	}
1783 
1784 	rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof (c), &c);
1785 	if (rc != 0) {
1786 		cxgb_printf(pi->dip, CE_WARN,
1787 		    "failed to create Ethernet egress queue: %d", rc);
1788 		return (rc);
1789 	}
1790 	eq->tse_cntxt_id = G_FW_EQ_ETH_CMD_EQID(BE_32(c.eqid_pkd));
1791 	*t4_eqmap_slot(sc, eq->tse_cntxt_id) = eq;
1792 	eq->tse_flags |= EQ_ALLOC_DEV;
1793 
1794 	t4_alloc_eq_post(pi, eq);
1795 
1796 	return (0);
1797 }
1798 
1799 static void
t4_free_eq(struct port_info * pi,t4_sge_eq_t * eq)1800 t4_free_eq(struct port_info *pi, t4_sge_eq_t *eq)
1801 {
1802 	struct adapter *sc = pi->adapter;
1803 
1804 	if (eq->tse_flags & EQ_ALLOC_DEV) {
1805 		int rc = -t4_eth_eq_free(sc, sc->mbox, sc->pf, 0,
1806 		    eq->tse_cntxt_id);
1807 		if (rc != 0) {
1808 			cxgb_printf(sc->dip, CE_WARN,
1809 			    "failed to free egress queue: %d", rc);
1810 			/*
1811 			 * Continue on with freeing operation, even though the
1812 			 * device resource will be effectively leaked.
1813 			 */
1814 		}
1815 		eq->tse_flags &= ~EQ_ALLOC_DEV;
1816 	}
1817 
1818 	if (eq->tse_flags & EQ_ALLOC_HOST) {
1819 		(void) free_desc_ring(&eq->tse_ring_dhdl, &eq->tse_ring_ahdl);
1820 		eq->tse_ring = NULL;
1821 		eq->tse_ring_ba = 0;
1822 		eq->tse_spg = NULL;
1823 		mutex_destroy(&eq->tse_lock);
1824 		eq->tse_flags &= ~EQ_ALLOC_HOST;
1825 	}
1826 
1827 	bzero(eq, sizeof (*eq));
1828 }
1829 
1830 static int
t4_alloc_txq(struct port_info * pi,struct sge_txq * txq,int idx)1831 t4_alloc_txq(struct port_info *pi, struct sge_txq *txq, int idx)
1832 {
1833 	struct adapter *sc = pi->adapter;
1834 	t4_sge_eq_t *eq = &txq->eq;
1835 	int rc;
1836 
1837 	if ((rc = t4_eq_alloc_eth(pi, eq)) != 0) {
1838 		return (rc);
1839 	}
1840 
1841 	txq->port = pi;
1842 	txq->sdesc = kmem_zalloc(sizeof (struct tx_sdesc) * eq->tse_qsize,
1843 	    KM_SLEEP);
1844 	txq->copy_threshold = tx_copy_threshold;
1845 	txq->txb_size = eq->tse_qsize * txq->copy_threshold;
1846 	rc = alloc_tx_copybuffer(sc, txq->txb_size, &txq->txb_dhdl,
1847 	    &txq->txb_ahdl, &txq->txb_ba, &txq->txb_va);
1848 	if (rc != 0) {
1849 		txq->txb_size = 0;
1850 		txq->txb_avail = 0;
1851 		return (ENOMEM);
1852 	} else {
1853 		txq->txb_avail = txq->txb_size;
1854 		eq->tse_flags |= EQ_ALLOC_DESC;
1855 	}
1856 
1857 	/*
1858 	 * TODO: is this too low?  Worst case would need around 4 times qsize
1859 	 * (all tx descriptors filled to the brim with SGLs, with each entry in
1860 	 * the SGL coming from a distinct DMA handle).  Increase tx_dhdl_total
1861 	 * if you see too many dma_hdl_failed.
1862 	 */
1863 	txq->tx_dhdl_total = eq->tse_qsize * 2;
1864 	txq->tx_dhdl = kmem_zalloc(sizeof (ddi_dma_handle_t) *
1865 	    txq->tx_dhdl_total, KM_SLEEP);
1866 	for (uint_t i = 0; i < txq->tx_dhdl_total; i++) {
1867 		rc = ddi_dma_alloc_handle(sc->dip, &sc->sge.dma_attr_tx,
1868 		    DDI_DMA_SLEEP, 0, &txq->tx_dhdl[i]);
1869 		if (rc != DDI_SUCCESS) {
1870 			cxgb_printf(sc->dip, CE_WARN,
1871 			    "%s: failed to allocate DMA handle (%d)",
1872 			    __func__, rc);
1873 			return (rc == DDI_DMA_NORESOURCES ? ENOMEM : EINVAL);
1874 		}
1875 		txq->tx_dhdl_avail++;
1876 	}
1877 
1878 	txq->ksp = setup_txq_kstats(pi, txq, idx);
1879 
1880 	return (0);
1881 }
1882 
1883 static void
t4_free_txq(struct port_info * pi,struct sge_txq * txq)1884 t4_free_txq(struct port_info *pi, struct sge_txq *txq)
1885 {
1886 	t4_sge_eq_t *eq = &txq->eq;
1887 
1888 	if (txq->ksp != NULL) {
1889 		kstat_delete(txq->ksp);
1890 		txq->ksp = NULL;
1891 	}
1892 
1893 	if (txq->txb_va != NULL) {
1894 		(void) free_desc_ring(&txq->txb_dhdl, &txq->txb_ahdl);
1895 		txq->txb_va = NULL;
1896 	}
1897 
1898 	if (txq->sdesc != NULL) {
1899 		ddi_dma_handle_t hdl;
1900 
1901 		TXQ_LOCK(txq);
1902 		while (eq->tse_cidx != eq->tse_pidx) {
1903 			struct tx_sdesc *sd = &txq->sdesc[eq->tse_cidx];
1904 
1905 			for (uint_t i = sd->hdls_used; i != 0; i--) {
1906 				hdl = txq->tx_dhdl[txq->tx_dhdl_cidx];
1907 				(void) ddi_dma_unbind_handle(hdl);
1908 				if (++txq->tx_dhdl_cidx == txq->tx_dhdl_total)
1909 					txq->tx_dhdl_cidx = 0;
1910 			}
1911 
1912 			ASSERT(sd->mp_head);
1913 			freemsgchain(sd->mp_head);
1914 			sd->mp_head = sd->mp_tail = NULL;
1915 
1916 			eq->tse_cidx += sd->credits_used;
1917 			if (eq->tse_cidx >= eq->tse_qsize)
1918 				eq->tse_cidx -= eq->tse_qsize;
1919 
1920 			txq->txb_avail += sd->txb_used;
1921 		}
1922 		ASSERT(txq->tx_dhdl_cidx == txq->tx_dhdl_pidx);
1923 		ASSERT(txq->txb_avail == txq->txb_size);
1924 		TXQ_UNLOCK(txq);
1925 
1926 		kmem_free(txq->sdesc, sizeof (struct tx_sdesc) * eq->tse_qsize);
1927 		txq->sdesc = NULL;
1928 		eq->tse_flags &= ~EQ_ALLOC_DESC;
1929 	}
1930 
1931 	if (txq->tx_dhdl != NULL) {
1932 		for (uint_t i = 0; i < txq->tx_dhdl_total; i++) {
1933 			if (txq->tx_dhdl[i] != NULL)
1934 				ddi_dma_free_handle(&txq->tx_dhdl[i]);
1935 		}
1936 		kmem_free(txq->tx_dhdl,
1937 		    sizeof (ddi_dma_handle_t) * txq->tx_dhdl_total);
1938 		txq->tx_dhdl = NULL;
1939 	}
1940 
1941 	t4_free_eq(pi, &txq->eq);
1942 
1943 	bzero(txq, sizeof (*txq));
1944 }
1945 
1946 /*
1947  * Allocates a block of contiguous memory for DMA.  Can be used to allocate
1948  * memory for descriptor rings or for tx/rx copy buffers.
1949  *
1950  * Caller does not have to clean up anything if this function fails, it cleans
1951  * up after itself.
1952  *
1953  * Caller provides the following:
1954  * len		length of the block of memory to allocate.
1955  * flags	DDI_DMA_* flags to use (CONSISTENT/STREAMING, READ/WRITE/RDWR)
1956  * acc_attr	device access attributes for the allocation.
1957  * dma_attr	DMA attributes for the allocation
1958  *
1959  * If the function is successful it fills up this information:
1960  * dma_hdl	DMA handle for the allocated memory
1961  * acc_hdl	access handle for the allocated memory
1962  * ba		bus address of the allocated memory
1963  * va		KVA of the allocated memory.
1964  */
1965 static int
alloc_dma_memory(struct adapter * sc,size_t len,int flags,ddi_device_acc_attr_t * acc_attr,ddi_dma_attr_t * dma_attr,ddi_dma_handle_t * dma_hdl,ddi_acc_handle_t * acc_hdl,uint64_t * pba,caddr_t * pva)1966 alloc_dma_memory(struct adapter *sc, size_t len, int flags,
1967     ddi_device_acc_attr_t *acc_attr, ddi_dma_attr_t *dma_attr,
1968     ddi_dma_handle_t *dma_hdl, ddi_acc_handle_t *acc_hdl,
1969     uint64_t *pba, caddr_t *pva)
1970 {
1971 	int rc;
1972 	ddi_dma_handle_t dhdl;
1973 	ddi_acc_handle_t ahdl;
1974 	ddi_dma_cookie_t cookie;
1975 	uint_t ccount;
1976 	caddr_t va;
1977 	size_t real_len;
1978 
1979 	*pva = NULL;
1980 
1981 	/*
1982 	 * DMA handle.
1983 	 */
1984 	rc = ddi_dma_alloc_handle(sc->dip, dma_attr, DDI_DMA_SLEEP, 0, &dhdl);
1985 	if (rc != DDI_SUCCESS) {
1986 		return (rc == DDI_DMA_NORESOURCES ? ENOMEM : EINVAL);
1987 	}
1988 
1989 	/*
1990 	 * Memory suitable for DMA.
1991 	 */
1992 	rc = ddi_dma_mem_alloc(dhdl, len, acc_attr,
1993 	    flags & DDI_DMA_CONSISTENT ? DDI_DMA_CONSISTENT : DDI_DMA_STREAMING,
1994 	    DDI_DMA_SLEEP, 0, &va, &real_len, &ahdl);
1995 	if (rc != DDI_SUCCESS) {
1996 		ddi_dma_free_handle(&dhdl);
1997 		return (ENOMEM);
1998 	}
1999 
2000 	/*
2001 	 * DMA bindings.
2002 	 */
2003 	rc = ddi_dma_addr_bind_handle(dhdl, NULL, va, real_len, flags, NULL,
2004 	    NULL, &cookie, &ccount);
2005 	if (rc != DDI_DMA_MAPPED) {
2006 		ddi_dma_mem_free(&ahdl);
2007 		ddi_dma_free_handle(&dhdl);
2008 		return (ENOMEM);
2009 	}
2010 	if (ccount != 1) {
2011 		/* unusable DMA mapping */
2012 		(void) free_desc_ring(&dhdl, &ahdl);
2013 		return (ENOMEM);
2014 	}
2015 
2016 	bzero(va, real_len);
2017 	*dma_hdl = dhdl;
2018 	*acc_hdl = ahdl;
2019 	*pba = cookie.dmac_laddress;
2020 	*pva = va;
2021 
2022 	return (0);
2023 }
2024 
2025 static int
free_dma_memory(ddi_dma_handle_t * dhdl,ddi_acc_handle_t * ahdl)2026 free_dma_memory(ddi_dma_handle_t *dhdl, ddi_acc_handle_t *ahdl)
2027 {
2028 	(void) ddi_dma_unbind_handle(*dhdl);
2029 	ddi_dma_mem_free(ahdl);
2030 	ddi_dma_free_handle(dhdl);
2031 
2032 	return (0);
2033 }
2034 
2035 static int
alloc_desc_ring(struct adapter * sc,size_t len,int rw,ddi_dma_handle_t * dma_hdl,ddi_acc_handle_t * acc_hdl,uint64_t * pba,caddr_t * pva)2036 alloc_desc_ring(struct adapter *sc, size_t len, int rw,
2037     ddi_dma_handle_t *dma_hdl, ddi_acc_handle_t *acc_hdl,
2038     uint64_t *pba, caddr_t *pva)
2039 {
2040 	ddi_device_acc_attr_t *acc_attr = &sc->sge.acc_attr_desc;
2041 	ddi_dma_attr_t *dma_attr = &sc->sge.dma_attr_desc;
2042 
2043 	return (alloc_dma_memory(sc, len, DDI_DMA_CONSISTENT | rw, acc_attr,
2044 	    dma_attr, dma_hdl, acc_hdl, pba, pva));
2045 }
2046 
2047 static int
free_desc_ring(ddi_dma_handle_t * dhdl,ddi_acc_handle_t * ahdl)2048 free_desc_ring(ddi_dma_handle_t *dhdl, ddi_acc_handle_t *ahdl)
2049 {
2050 	return (free_dma_memory(dhdl, ahdl));
2051 }
2052 
2053 static int
alloc_tx_copybuffer(struct adapter * sc,size_t len,ddi_dma_handle_t * dma_hdl,ddi_acc_handle_t * acc_hdl,uint64_t * pba,caddr_t * pva)2054 alloc_tx_copybuffer(struct adapter *sc, size_t len,
2055     ddi_dma_handle_t *dma_hdl, ddi_acc_handle_t *acc_hdl,
2056     uint64_t *pba, caddr_t *pva)
2057 {
2058 	ddi_device_acc_attr_t *acc_attr = &sc->sge.acc_attr_tx;
2059 	ddi_dma_attr_t *dma_attr = &sc->sge.dma_attr_desc; /* NOT dma_attr_tx */
2060 
2061 	return (alloc_dma_memory(sc, len, DDI_DMA_STREAMING | DDI_DMA_WRITE,
2062 	    acc_attr, dma_attr, dma_hdl, acc_hdl, pba, pva));
2063 }
2064 
2065 /*
2066  * Fetch next valid (if any) response from adapter in IQ.  Returns `true` if
2067  * rsp_ctrl data read into `ctrl` has generation bit state matching IQ
2068  * expectation for a new entry.
2069  *
2070  * This does not advance cidx, which is left to a subsequent call to
2071  * t4_iq_next_entry().
2072  */
2073 static inline bool
t4_get_new_rsp(const t4_sge_iq_t * iq,struct rsp_ctrl * ctrl)2074 t4_get_new_rsp(const t4_sge_iq_t *iq, struct rsp_ctrl *ctrl)
2075 {
2076 	(void) ddi_dma_sync(iq->tsi_desc_dhdl, 0, 0, DDI_DMA_SYNC_FORKERNEL);
2077 
2078 	*ctrl = *(struct rsp_ctrl *)
2079 	    ((caddr_t)iq->tsi_cdesc + (iq->tsi_esize_bytes -
2080 	    sizeof (struct rsp_ctrl)));
2081 	return ((ctrl->u.type_gen & F_RSPD_GEN) == iq->tsi_gen);
2082 }
2083 
2084 /*
2085  * Advance IQ consumer index, wrapping (and toggling generation bit) when the
2086  * end of the ring is reached.
2087  */
2088 static inline void
t4_iq_next_entry(t4_sge_iq_t * iq)2089 t4_iq_next_entry(t4_sge_iq_t *iq)
2090 {
2091 	iq->tsi_cdesc = (void *) ((caddr_t)iq->tsi_cdesc + iq->tsi_esize_bytes);
2092 	if (++iq->tsi_cidx == iq->tsi_cap) {
2093 		iq->tsi_cidx = 0;
2094 		iq->tsi_gen ^= F_RSPD_GEN;
2095 		iq->tsi_cdesc = iq->tsi_desc;
2096 	}
2097 }
2098 
2099 static inline bool
t4_fl_running_low(const struct sge_fl * fl)2100 t4_fl_running_low(const struct sge_fl *fl)
2101 {
2102 	return (fl->bufs_avail <= fl->bufs_lowat);
2103 }
2104 
2105 static inline bool
t4_fl_not_running_low(const struct sge_fl * fl)2106 t4_fl_not_running_low(const struct sge_fl *fl)
2107 {
2108 	return (fl->bufs_avail >= (2 * fl->bufs_lowat));
2109 }
2110 
2111 static inline uint_t
t4_fl_advance_cidx(struct sge_fl * fl)2112 t4_fl_advance_cidx(struct sge_fl *fl)
2113 {
2114 	t4_sge_eq_t *eq = &fl->eq;
2115 
2116 	FL_LOCK_ASSERT_OWNED(fl);
2117 	ASSERT3U(fl->cidx_sdesc, <, FL_BUF_PTR_PER_HC);
2118 	ASSERT3U(eq->tse_cidx, <, eq->tse_qsize);
2119 
2120 	fl->cidx_sdesc++;
2121 	if (fl->cidx_sdesc == FL_BUF_PTR_PER_HC) {
2122 		fl->cidx_sdesc = 0;
2123 		eq->tse_cidx++;
2124 		if (eq->tse_cidx == eq->tse_qsize) {
2125 			eq->tse_cidx = 0;
2126 		}
2127 		return (1);
2128 	}
2129 	return (0);
2130 }
2131 
2132 static inline struct fl_sdesc *
t4_fl_sdesc(struct sge_fl * fl,uint_t eq_idx,uint_t sdesc_idx)2133 t4_fl_sdesc(struct sge_fl *fl, uint_t eq_idx, uint_t sdesc_idx)
2134 {
2135 	ASSERT(sdesc_idx < FL_BUF_PTR_PER_HC);
2136 	const uint_t idx = (eq_idx * FL_BUF_PTR_PER_HC) + sdesc_idx;
2137 
2138 	return (&fl->sdesc[idx]);
2139 }
2140 
2141 /*
2142  * Fill up the freelist by upto nbufs and maybe ring its doorbell.
2143  *
2144  * Returns non-zero to indicate that it should be added to the list of starving
2145  * freelists.
2146  */
2147 static bool
t4_fl_refill(struct sge_fl * fl,uint_t nbufs)2148 t4_fl_refill(struct sge_fl *fl, uint_t nbufs)
2149 {
2150 	struct adapter *sc = t4_fl_to_iq(fl)->tsi_adapter;
2151 	t4_sge_eq_t *eq = &fl->eq;
2152 
2153 	FL_LOCK_ASSERT_OWNED(fl);
2154 
2155 	/*
2156 	 * We refill up to nbufs, but maybe less if there are not that many
2157 	 * outstanding.
2158 	 */
2159 	nbufs = MIN(nbufs, fl->bufs_cap - fl->bufs_avail);
2160 	while (nbufs != 0 && eq->tse_avail != 0) {
2161 		struct fl_desc *fld = t4_eq_credit(eq, eq->tse_pidx);
2162 		struct fl_sdesc *sd = t4_fl_sdesc(fl, eq->tse_pidx,
2163 		    fl->pidx_sdesc);
2164 
2165 		if (sd->rxb != NULL) {
2166 			if (sd->rxb->ref_cnt == 1) {
2167 				/*
2168 				 * Buffer is available for recycling.  Two ways
2169 				 * this can happen:
2170 				 *
2171 				 * a) All the packets DMA'd into it last time
2172 				 *    around were within the rx_copy_threshold
2173 				 *    and no part of the buffer was ever passed
2174 				 *    up (ref_cnt never went over 1).
2175 				 *
2176 				 * b) Packets DMA'd into the buffer were passed
2177 				 *    up but have all been freed by the upper
2178 				 *    layers by now (ref_cnt went over 1 but is
2179 				 *    now back to 1).
2180 				 *
2181 				 * Either way the bus address in the descriptor
2182 				 * ring is already valid.
2183 				 */
2184 				ASSERT3U(fld->dptr[fl->pidx_sdesc], ==,
2185 				    BE_64(sd->rxb->ba));
2186 				fl->stats.rxb_recycle++;
2187 			} else {
2188 				/*
2189 				 * Buffer still in use and we need a
2190 				 * replacement. But first release our reference
2191 				 * on the existing buffer.
2192 				 */
2193 				rxbuf_free(sd->rxb);
2194 				sd->rxb = NULL;
2195 			}
2196 		}
2197 
2198 		if (sd->rxb == NULL) {
2199 			sd->rxb = rxbuf_alloc(sc->sge.rxbuf_cache, KM_NOSLEEP);
2200 			if (sd->rxb == NULL) {
2201 				fl->stats.rxb_alloc_fail++;
2202 				break;
2203 			}
2204 			fl->stats.rxb_alloc++;
2205 		}
2206 		fld->dptr[fl->pidx_sdesc] = BE_64(sd->rxb->ba);
2207 
2208 		nbufs--;
2209 		fl->bufs_avail++;
2210 		fl->pidx_sdesc++;
2211 		if (fl->pidx_sdesc == FL_BUF_PTR_PER_HC) {
2212 			/*
2213 			 * The host credit is filled. It is now ready to be
2214 			 * posted to the device.
2215 			 */
2216 			fl->pidx_sdesc = 0;
2217 			eq->tse_pending++;
2218 			eq->tse_avail--;
2219 			eq->tse_pidx++;
2220 			if (eq->tse_pidx == eq->tse_qsize) {
2221 				eq->tse_pidx = 0;
2222 			}
2223 		}
2224 	}
2225 
2226 	if (eq->tse_pending != 0) {
2227 		t4_fl_ring_db(fl);
2228 	}
2229 
2230 	return (t4_fl_running_low(fl));
2231 }
2232 
2233 static clock_t t4_sfl_period_us = 100000;
2234 
2235 static void
t4_sfl_reschedule(struct adapter * sc)2236 t4_sfl_reschedule(struct adapter *sc)
2237 {
2238 	ASSERT(MUTEX_HELD(&sc->sfl_lock));
2239 	ASSERT(!list_is_empty(&sc->sfl_list));
2240 
2241 	sc->sfl_timer = timeout(t4_sfl_process, sc,
2242 	    drv_usectohz(t4_sfl_period_us));
2243 }
2244 
2245 /*
2246  * Attempt to refill all starving freelists.
2247  */
2248 static void
t4_sfl_process(void * arg)2249 t4_sfl_process(void *arg)
2250 {
2251 	struct adapter *sc = arg;
2252 
2253 	mutex_enter(&sc->sfl_lock);
2254 	struct sge_fl *fl = list_head(&sc->sfl_list);
2255 	while (fl != NULL) {
2256 		struct sge_fl *next = list_next(&sc->sfl_list, fl);
2257 
2258 		FL_LOCK(fl);
2259 		(void) t4_fl_refill(fl, 64);
2260 		if (t4_fl_not_running_low(fl) || fl->sfl_flags & SFL_DOOMED) {
2261 			list_remove(&sc->sfl_list, fl);
2262 			fl->sfl_flags &= ~SFL_STARVING;
2263 		}
2264 		FL_UNLOCK(fl);
2265 		fl = next;
2266 	}
2267 
2268 	if (!list_is_empty(&sc->sfl_list)) {
2269 		t4_sfl_reschedule(sc);
2270 	}
2271 	mutex_exit(&sc->sfl_lock);
2272 }
2273 
2274 static void
t4_sfl_enqueue(struct adapter * sc,struct sge_fl * fl)2275 t4_sfl_enqueue(struct adapter *sc, struct sge_fl *fl)
2276 {
2277 	mutex_enter(&sc->sfl_lock);
2278 	FL_LOCK(fl);
2279 	if ((fl->sfl_flags & (SFL_DOOMED | SFL_STARVING)) == 0) {
2280 		const bool was_empty = list_is_empty(&sc->sfl_list);
2281 
2282 		fl->sfl_flags |= SFL_STARVING;
2283 		list_insert_tail(&sc->sfl_list, fl);
2284 		if (was_empty) {
2285 			t4_sfl_reschedule(sc);
2286 		}
2287 	}
2288 	FL_UNLOCK(fl);
2289 	mutex_exit(&sc->sfl_lock);
2290 }
2291 
2292 static void
t4_fl_free_bufs(struct sge_fl * fl)2293 t4_fl_free_bufs(struct sge_fl *fl)
2294 {
2295 	t4_sge_eq_t *eq = &fl->eq;
2296 
2297 	EQ_LOCK_ASSERT_OWNED(eq);
2298 
2299 	for (uint_t i = 0; i < eq->tse_qsize * FL_BUF_PTR_PER_HC; i++) {
2300 		struct fl_sdesc *sd = &fl->sdesc[i];
2301 
2302 		if (sd->rxb != NULL) {
2303 			rxbuf_free(sd->rxb);
2304 			sd->rxb = NULL;
2305 		}
2306 	}
2307 }
2308 
2309 /*
2310  * Attempt to create an mblk representing the payload stored at the current
2311  * offset (fl->offset) in the current FL buffer (fl->cidx_sdesc). If the length
2312  * of the payload is less than fl->copy_threshold, then allocable a new
2313  * mblk/dblk to hold the contents and copy it over. Otherwise, attempt to
2314  * desballoc() the payload. If there is a failure to allocate, then restore the
2315  * eq->tse_cidx and fl->offset to their original value that they had upon
2316  * entering this function.
2317  */
2318 static mblk_t *
t4_fl_get_payload(struct sge_fl * fl,uint32_t len,bool newbuf)2319 t4_fl_get_payload(struct sge_fl *fl, uint32_t len, bool newbuf)
2320 {
2321 	struct adapter *sc = t4_fl_to_iq(fl)->tsi_adapter;
2322 	t4_sge_eq_t *eq = &fl->eq;
2323 	mblk_t *mp = NULL;
2324 	mblk_t *head = NULL, **tailp = &head;
2325 	uint_t bufs_consumed = 0;
2326 
2327 	FL_LOCK(fl);
2328 	/*
2329 	 * The SGE won't pack a new frame into the current buffer if the entire
2330 	 * payload doesn't fit in the remaining space.  Move on to the next buf
2331 	 * in that case.
2332 	 */
2333 	const uint16_t rcidx = eq->tse_cidx;
2334 	const uint_t rcidx_sdesc = fl->cidx_sdesc;
2335 	const uint32_t roffset = fl->offset;
2336 	uint_t credits_avail = 0;
2337 
2338 	if (fl->offset > 0 && newbuf) {
2339 		/*
2340 		 * The device has moved onto the next buffer. Reset our offset
2341 		 * into the current buffer and advanced the driver's cidx, which
2342 		 * may have freed up an EQ host credit to be refilled by the
2343 		 * driver.
2344 		 */
2345 		fl->offset = 0;
2346 		credits_avail += t4_fl_advance_cidx(fl);
2347 		bufs_consumed++;
2348 	}
2349 
2350 	const bool do_copy = (len <= fl->copy_threshold);
2351 	if (do_copy) {
2352 		mp = allocb(len, 0);
2353 		if (mp == NULL) {
2354 			fl->stats.copy_fail++;
2355 			DTRACE_PROBE1(t4__fl_alloc_fail, struct sge_fl *, fl);
2356 			goto restore;
2357 		}
2358 		*tailp = mp;
2359 		tailp = &mp->b_cont;
2360 	}
2361 
2362 	uint_t offset = fl->offset;
2363 	while (len != 0) {
2364 		struct rxbuf *rxb =
2365 		    t4_fl_sdesc(fl, eq->tse_cidx, fl->cidx_sdesc)->rxb;
2366 		const uint_t copy_len = MIN(len, rxb->buf_size - offset);
2367 
2368 		(void) ddi_dma_sync(rxb->dhdl, 0, 0, DDI_DMA_SYNC_FORKERNEL);
2369 
2370 		if (do_copy) {
2371 			bcopy(rxb->va + offset, mp->b_wptr, copy_len);
2372 			fl->stats.copy++;
2373 		} else {
2374 			mp = desballoc((unsigned char *)rxb->va + offset,
2375 			    copy_len, 0, &rxb->freefunc);
2376 			if (mp == NULL) {
2377 				fl->stats.wrap_fail++;
2378 				DTRACE_PROBE1(t4__fl_alloc_fail,
2379 				    struct sge_fl *, fl);
2380 				goto restore;
2381 			}
2382 			atomic_inc_uint(&rxb->ref_cnt);
2383 			*tailp = mp;
2384 			tailp = &mp->b_cont;
2385 			fl->stats.wrap++;
2386 		}
2387 		mp->b_wptr += copy_len;
2388 		len -= copy_len;
2389 		offset += roundup(copy_len, sc->sge.fl_align);
2390 
2391 		ASSERT3U(offset, <=, rxb->buf_size);
2392 		if (offset == rxb->buf_size) {
2393 			offset = 0;
2394 			credits_avail += t4_fl_advance_cidx(fl);
2395 			bufs_consumed++;
2396 		}
2397 	}
2398 	fl->offset = offset;
2399 	ASSERT3U(credits_avail, <=, eq->tse_qsize);
2400 	eq->tse_avail += credits_avail;
2401 	/* We can't consume more than are available. */
2402 	ASSERT3U(bufs_consumed, <=, fl->bufs_avail);
2403 	fl->bufs_avail -= bufs_consumed;
2404 
2405 	FL_UNLOCK(fl);
2406 
2407 	ASSERT(head != NULL);
2408 	return (head);
2409 
2410 restore:
2411 	eq->tse_cidx = rcidx;
2412 	fl->cidx_sdesc = rcidx_sdesc;
2413 	fl->offset = roffset;
2414 	FL_UNLOCK(fl);
2415 	freemsgchain(head);
2416 
2417 	return (NULL);
2418 }
2419 
2420 /*
2421  * We'll do immediate data tx for non-LSO, but only when not coalescing.  We're
2422  * willing to use upto 2 hardware descriptors which means a maximum of 96 bytes
2423  * of immediate data.
2424  */
2425 #define	IMM_LEN ( \
2426 	2 * EQ_HC_SIZE \
2427 	- sizeof (struct fw_eth_tx_pkt_wr) \
2428 	- sizeof (struct cpl_tx_pkt_core))
2429 
2430 /*
2431  * Returns non-zero on failure, no need to cleanup anything in that case.
2432  *
2433  * Note 1: We always try to pull up the mblk if required and return E2BIG only
2434  * if this fails.
2435  *
2436  * Note 2: We'll also pullup incoming mblk if HW_LSO is set and the first mblk
2437  * does not have the TCP header in it.
2438  */
2439 static int
get_frame_txinfo(struct sge_txq * txq,mblk_t ** fp,struct txinfo * txinfo,int sgl_only)2440 get_frame_txinfo(struct sge_txq *txq, mblk_t **fp, struct txinfo *txinfo,
2441     int sgl_only)
2442 {
2443 	uint32_t flags = 0, len, n;
2444 	mblk_t *m = *fp;
2445 	int rc;
2446 
2447 	TXQ_LOCK_ASSERT_OWNED(txq);	/* will manipulate txb and dma_hdls */
2448 
2449 	mac_hcksum_get(m, NULL, NULL, NULL, NULL, &flags);
2450 	txinfo->flags = (flags & HCK_TX_FLAGS);
2451 
2452 	mac_lso_get(m, &txinfo->mss, &flags);
2453 	txinfo->flags |= (flags & HW_LSO_FLAGS);
2454 
2455 	if (flags & HW_LSO)
2456 		sgl_only = 1;	/* Do not allow immediate data with LSO */
2457 
2458 	/*
2459 	 * If checksum or segmentation offloads are requested, gather
2460 	 * information about the sizes and types of headers in the packet.
2461 	 */
2462 	if (txinfo->flags != 0) {
2463 		mac_ether_offload_info(m, &txinfo->meoi);
2464 	} else {
2465 		bzero(&txinfo->meoi, sizeof (txinfo->meoi));
2466 	}
2467 
2468 start:
2469 	txinfo->nsegs = 0;
2470 	txinfo->hdls_used = 0;
2471 	txinfo->txb_used = 0;
2472 	txinfo->len = 0;
2473 
2474 	/* total length and a rough estimate of # of segments */
2475 	n = 0;
2476 	for (; m; m = m->b_cont) {
2477 		len = MBLKL(m);
2478 		n += (len / PAGE_SIZE) + 1;
2479 		txinfo->len += len;
2480 	}
2481 	m = *fp;
2482 
2483 	if (n >= TX_SGL_SEGS || ((flags & HW_LSO) && MBLKL(m) < 50)) {
2484 		txq->stats.pullup_early++;
2485 		m = msgpullup(*fp, -1);
2486 		if (m == NULL) {
2487 			txq->stats.pullup_failed++;
2488 			return (E2BIG);	/* (*fp) left as it was */
2489 		}
2490 		freemsg(*fp);
2491 		*fp = m;
2492 		mac_hcksum_set(m, 0, 0, 0, 0, txinfo->flags);
2493 	}
2494 
2495 	if (txinfo->len <= IMM_LEN && !sgl_only)
2496 		return (0);	/* nsegs = 0 tells caller to use imm. tx */
2497 
2498 	if (txinfo->len <= txq->copy_threshold &&
2499 	    copy_into_txb(txq, m, txinfo->len, txinfo) == 0) {
2500 		goto done;
2501 	}
2502 
2503 	for (; m; m = m->b_cont) {
2504 
2505 		len = MBLKL(m);
2506 
2507 		/*
2508 		 * Use tx copy buffer if this mblk is small enough and there is
2509 		 * room, otherwise add DMA bindings for this mblk to the SGL.
2510 		 */
2511 		if (len > txq->copy_threshold ||
2512 		    (rc = copy_into_txb(txq, m, len, txinfo)) != 0) {
2513 			rc = add_mblk(txq, txinfo, m, len);
2514 		}
2515 
2516 		if (rc == E2BIG ||
2517 		    (txinfo->nsegs == TX_SGL_SEGS && m->b_cont)) {
2518 
2519 			txq->stats.pullup_late++;
2520 			m = msgpullup(*fp, -1);
2521 			if (m != NULL) {
2522 				free_txinfo_resources(txq, txinfo);
2523 				freemsg(*fp);
2524 				*fp = m;
2525 				mac_hcksum_set(m, 0, 0, 0, 0, txinfo->flags);
2526 				goto start;
2527 			}
2528 
2529 			txq->stats.pullup_failed++;
2530 			rc = E2BIG;
2531 		}
2532 
2533 		if (rc != 0) {
2534 			free_txinfo_resources(txq, txinfo);
2535 			return (rc);
2536 		}
2537 	}
2538 
2539 done:
2540 	ASSERT(txinfo->nsegs > 0 && txinfo->nsegs <= TX_SGL_SEGS);
2541 
2542 	/*
2543 	 * Store the # of flits required to hold this frame's SGL in nflits.  An
2544 	 * SGL has a (ULPTX header + len0, addr0) tuple optionally followed by
2545 	 * multiple (len0 + len1, addr0, addr1) tuples.  If addr1 is not used
2546 	 * then len1 must be set to 0.
2547 	 */
2548 	n = txinfo->nsegs - 1;
2549 	txinfo->nflits = (3 * n) / 2 + (n & 1) + 2;
2550 	if (n & 1)
2551 		txinfo->sgl.sge[n / 2].len[1] = cpu_to_be32(0);
2552 
2553 	txinfo->sgl.cmd_nsge = cpu_to_be32(V_ULPTX_CMD((u32)ULP_TX_SC_DSGL) |
2554 	    V_ULPTX_NSGE(txinfo->nsegs));
2555 
2556 	return (0);
2557 }
2558 
2559 static inline int
fits_in_txb(struct sge_txq * txq,int len,int * waste)2560 fits_in_txb(struct sge_txq *txq, int len, int *waste)
2561 {
2562 	if (txq->txb_avail < len)
2563 		return (0);
2564 
2565 	if (txq->txb_next + len <= txq->txb_size) {
2566 		*waste = 0;
2567 		return (1);
2568 	}
2569 
2570 	*waste = txq->txb_size - txq->txb_next;
2571 
2572 	return (txq->txb_avail - *waste < len ? 0 : 1);
2573 }
2574 
2575 #define	TXB_CHUNK	64
2576 
2577 /*
2578  * Copies the specified # of bytes into txq's tx copy buffer and updates txinfo
2579  * and txq to indicate resources used.  Caller has to make sure that those many
2580  * bytes are available in the mblk chain (b_cont linked).
2581  */
2582 static inline int
copy_into_txb(struct sge_txq * txq,mblk_t * m,int len,struct txinfo * txinfo)2583 copy_into_txb(struct sge_txq *txq, mblk_t *m, int len, struct txinfo *txinfo)
2584 {
2585 	int waste, n;
2586 
2587 	TXQ_LOCK_ASSERT_OWNED(txq);	/* will manipulate txb */
2588 
2589 	if (!fits_in_txb(txq, len, &waste)) {
2590 		txq->stats.txb_full++;
2591 		return (ENOMEM);
2592 	}
2593 
2594 	if (waste != 0) {
2595 		ASSERT((waste & (TXB_CHUNK - 1)) == 0);
2596 		txinfo->txb_used += waste;
2597 		txq->txb_avail -= waste;
2598 		txq->txb_next = 0;
2599 	}
2600 
2601 	for (n = 0; n < len; m = m->b_cont) {
2602 		bcopy(m->b_rptr, txq->txb_va + txq->txb_next + n, MBLKL(m));
2603 		n += MBLKL(m);
2604 	}
2605 
2606 	add_seg(txinfo, txq->txb_ba + txq->txb_next, len);
2607 
2608 	n = roundup(len, TXB_CHUNK);
2609 	txinfo->txb_used += n;
2610 	txq->txb_avail -= n;
2611 	txq->txb_next += n;
2612 	ASSERT(txq->txb_next <= txq->txb_size);
2613 	if (txq->txb_next == txq->txb_size)
2614 		txq->txb_next = 0;
2615 
2616 	return (0);
2617 }
2618 
2619 static inline void
add_seg(struct txinfo * txinfo,uint64_t ba,uint32_t len)2620 add_seg(struct txinfo *txinfo, uint64_t ba, uint32_t len)
2621 {
2622 	ASSERT(txinfo->nsegs < TX_SGL_SEGS);	/* must have room */
2623 
2624 	if (txinfo->nsegs != 0) {
2625 		int idx = txinfo->nsegs - 1;
2626 		txinfo->sgl.sge[idx / 2].len[idx & 1] = cpu_to_be32(len);
2627 		txinfo->sgl.sge[idx / 2].addr[idx & 1] = cpu_to_be64(ba);
2628 	} else {
2629 		txinfo->sgl.len0 = cpu_to_be32(len);
2630 		txinfo->sgl.addr0 = cpu_to_be64(ba);
2631 	}
2632 	txinfo->nsegs++;
2633 }
2634 
2635 /*
2636  * This function cleans up any partially allocated resources when it fails so
2637  * there's nothing for the caller to clean up in that case.
2638  *
2639  * EIO indicates permanent failure.  Caller should drop the frame containing
2640  * this mblk and continue.
2641  *
2642  * E2BIG indicates that the SGL length for this mblk exceeds the hardware
2643  * limit.  Caller should pull up the frame before trying to send it out.
2644  * (This error means our pullup_early heuristic did not work for this frame)
2645  *
2646  * ENOMEM indicates a temporary shortage of resources (DMA handles, other DMA
2647  * resources, etc.).  Caller should suspend the tx queue and wait for reclaim to
2648  * free up resources.
2649  */
2650 static inline int
add_mblk(struct sge_txq * txq,struct txinfo * txinfo,mblk_t * m,int len)2651 add_mblk(struct sge_txq *txq, struct txinfo *txinfo, mblk_t *m, int len)
2652 {
2653 	ddi_dma_handle_t dhdl;
2654 	ddi_dma_cookie_t cookie;
2655 	uint_t ccount = 0;
2656 	int rc;
2657 
2658 	TXQ_LOCK_ASSERT_OWNED(txq);	/* will manipulate dhdls */
2659 
2660 	if (txq->tx_dhdl_avail == 0) {
2661 		txq->stats.dma_hdl_failed++;
2662 		return (ENOMEM);
2663 	}
2664 
2665 	dhdl = txq->tx_dhdl[txq->tx_dhdl_pidx];
2666 	rc = ddi_dma_addr_bind_handle(dhdl, NULL, (caddr_t)m->b_rptr, len,
2667 	    DDI_DMA_WRITE | DDI_DMA_STREAMING, DDI_DMA_DONTWAIT, NULL, &cookie,
2668 	    &ccount);
2669 	if (rc != DDI_DMA_MAPPED) {
2670 		txq->stats.dma_map_failed++;
2671 
2672 		ASSERT(rc != DDI_DMA_INUSE && rc != DDI_DMA_PARTIAL_MAP);
2673 
2674 		return (rc == DDI_DMA_NORESOURCES ? ENOMEM : EIO);
2675 	}
2676 
2677 	if (ccount + txinfo->nsegs > TX_SGL_SEGS) {
2678 		(void) ddi_dma_unbind_handle(dhdl);
2679 		return (E2BIG);
2680 	}
2681 
2682 	add_seg(txinfo, cookie.dmac_laddress, cookie.dmac_size);
2683 	while (--ccount) {
2684 		ddi_dma_nextcookie(dhdl, &cookie);
2685 		add_seg(txinfo, cookie.dmac_laddress, cookie.dmac_size);
2686 	}
2687 
2688 	if (++txq->tx_dhdl_pidx == txq->tx_dhdl_total)
2689 		txq->tx_dhdl_pidx = 0;
2690 	txq->tx_dhdl_avail--;
2691 	txinfo->hdls_used++;
2692 
2693 	return (0);
2694 }
2695 
2696 /*
2697  * Releases all the txq resources used up in the specified txinfo.
2698  */
2699 static void
free_txinfo_resources(struct sge_txq * txq,struct txinfo * txinfo)2700 free_txinfo_resources(struct sge_txq *txq, struct txinfo *txinfo)
2701 {
2702 	int n;
2703 
2704 	TXQ_LOCK_ASSERT_OWNED(txq);	/* dhdls, txb */
2705 
2706 	n = txinfo->txb_used;
2707 	if (n > 0) {
2708 		txq->txb_avail += n;
2709 		if (n <= txq->txb_next)
2710 			txq->txb_next -= n;
2711 		else {
2712 			n -= txq->txb_next;
2713 			txq->txb_next = txq->txb_size - n;
2714 		}
2715 	}
2716 
2717 	for (n = txinfo->hdls_used; n > 0; n--) {
2718 		if (txq->tx_dhdl_pidx > 0)
2719 			txq->tx_dhdl_pidx--;
2720 		else
2721 			txq->tx_dhdl_pidx = txq->tx_dhdl_total - 1;
2722 		txq->tx_dhdl_avail++;
2723 		(void) ddi_dma_unbind_handle(txq->tx_dhdl[txq->tx_dhdl_pidx]);
2724 	}
2725 }
2726 
2727 /*
2728  * Returns 0 to indicate that m has been accepted into a coalesced tx work
2729  * request.  It has either been folded into txpkts or txpkts was flushed and m
2730  * has started a new coalesced work request (as the first frame in a fresh
2731  * txpkts).
2732  *
2733  * Returns non-zero to indicate a failure - caller is responsible for
2734  * transmitting m, if there was anything in txpkts it has been flushed.
2735  */
2736 static int
add_to_txpkts(struct sge_txq * txq,struct txpkts * txpkts,mblk_t * m,struct txinfo * txinfo)2737 add_to_txpkts(struct sge_txq *txq, struct txpkts *txpkts, mblk_t *m,
2738     struct txinfo *txinfo)
2739 {
2740 	t4_sge_eq_t *eq = &txq->eq;
2741 	int can_coalesce;
2742 	struct tx_sdesc *txsd;
2743 	uint8_t flits;
2744 
2745 	TXQ_LOCK_ASSERT_OWNED(txq);
2746 	ASSERT(m->b_next == NULL);
2747 
2748 	if (txpkts->npkt > 0) {
2749 		flits = TXPKTS_PKT_HDR_FLITS + txinfo->nflits;
2750 		can_coalesce = (txinfo->flags & HW_LSO) == 0 &&
2751 		    txpkts->nflits + flits <= TX_WR_MAX_FLITS &&
2752 		    txpkts->nflits + flits <= EQ_HC_TO_FLITS(eq->tse_avail) &&
2753 		    txpkts->plen + txinfo->len < 65536;
2754 
2755 		if (can_coalesce != 0) {
2756 			txpkts->tail->b_next = m;
2757 			txpkts->tail = m;
2758 			txpkts->npkt++;
2759 			txpkts->nflits += flits;
2760 			txpkts->plen += txinfo->len;
2761 
2762 			txsd = &txq->sdesc[eq->tse_pidx];
2763 			txsd->txb_used += txinfo->txb_used;
2764 			txsd->hdls_used += txinfo->hdls_used;
2765 
2766 			/*
2767 			 * The txpkts chaining above has already placed `m` at
2768 			 * the end with b_next.  Keep the txsd notion of this
2769 			 * new tail up to date.
2770 			 */
2771 			ASSERT3P(txsd->mp_tail->b_next, ==, m);
2772 			txsd->mp_tail = m;
2773 
2774 			return (0);
2775 		}
2776 
2777 		/*
2778 		 * Couldn't coalesce m into txpkts.  The first order of business
2779 		 * is to send txpkts on its way.  Then we'll revisit m.
2780 		 */
2781 		write_txpkts_wr(txq, txpkts);
2782 	}
2783 
2784 	/*
2785 	 * Check if we can start a new coalesced tx work request with m as
2786 	 * the first packet in it.
2787 	 */
2788 
2789 	ASSERT(txpkts->npkt == 0);
2790 	ASSERT(txinfo->len < 65536);
2791 
2792 	flits = TXPKTS_WR_HDR_FLITS + txinfo->nflits;
2793 
2794 	/*
2795 	 * We can coalesce if this is non-LSO and the number of flits required
2796 	 * is both less than or equal to the maximum flits allowed for a single
2797 	 * WR and less than or equal to the number of flits currently available.
2798 	 */
2799 	can_coalesce = (txinfo->flags & HW_LSO) == 0 &&
2800 	    flits <= EQ_HC_TO_FLITS(eq->tse_avail) && flits <= TX_WR_MAX_FLITS;
2801 
2802 	if (can_coalesce == 0)
2803 		return (EINVAL);
2804 
2805 	/*
2806 	 * Start a fresh coalesced tx WR with m as the first frame in it.
2807 	 */
2808 	t4_eq_host_credit_t *hc = t4_eq_credit(eq, eq->tse_pidx);
2809 	txpkts->tail = m;
2810 	txpkts->npkt = 1;
2811 	txpkts->nflits = flits;
2812 	txpkts->flitp = &hc->flit[2];
2813 	txpkts->plen = txinfo->len;
2814 
2815 	txsd = &txq->sdesc[eq->tse_pidx];
2816 	txsd->mp_head = txsd->mp_tail = m;
2817 	txsd->txb_used = txinfo->txb_used;
2818 	txsd->hdls_used = txinfo->hdls_used;
2819 
2820 	return (0);
2821 }
2822 
2823 static inline void
t4_tx_incr_pending(struct sge_txq * txq,uint16_t ncredits)2824 t4_tx_incr_pending(struct sge_txq *txq, uint16_t ncredits)
2825 {
2826 	t4_sge_eq_t *eq = &txq->eq;
2827 
2828 	TXQ_LOCK_ASSERT_OWNED(txq);
2829 	ASSERT3U(ncredits, !=, 0);
2830 	ASSERT3U(eq->tse_avail, >=, ncredits);
2831 
2832 	eq->tse_pending += ncredits;
2833 	eq->tse_avail -= ncredits;
2834 	eq->tse_pidx += ncredits;
2835 	if (eq->tse_pidx >= eq->tse_qsize) {
2836 		eq->tse_pidx -= eq->tse_qsize;
2837 	}
2838 
2839 	ASSERT3U(eq->tse_pidx, <, eq->tse_qsize);
2840 	ASSERT3U(eq->tse_pending, <=, eq->tse_qsize - 1);
2841 }
2842 
2843 /*
2844  * Note that write_txpkts_wr() can never run out of host credits (but
2845  * write_txpkt_wr() can). add_to_txpkts() ensures that a frame is accepted for
2846  * coalescing only if sufficient host credits are available.
2847  */
2848 static void
write_txpkts_wr(struct sge_txq * txq,struct txpkts * txpkts)2849 write_txpkts_wr(struct sge_txq *txq, struct txpkts *txpkts)
2850 {
2851 	t4_sge_eq_t *eq = &txq->eq;
2852 
2853 	TXQ_LOCK_ASSERT_OWNED(txq);	/* pidx, avail */
2854 
2855 	struct fw_eth_tx_pkts_wr *wr = t4_eq_credit(eq, eq->tse_pidx);
2856 	const uint16_t ncredits = EQ_FLITS_TO_HC(txpkts->nflits);
2857 	ASSERT3U(ncredits, <=, eq->tse_avail);
2858 
2859 	/* The immdlen value does not matter for this WR. */
2860 	wr->op_pkd = BE_32(V_FW_WR_OP(FW_ETH_TX_PKTS_WR) | V_FW_WR_IMMDLEN(0));
2861 
2862 	/*
2863 	 * If all remaining credits are consumed by this WR, then request an EQ
2864 	 * status update to both the EQ status page and the associated ingress
2865 	 * queue entry.
2866 	 *
2867 	 * See §29.10 FW_ETH_TX_PKTS_WR of the T4 Firmware Interface
2868 	 * Specification.
2869 	 */
2870 	const uint32_t update_bits = (eq->tse_avail == ncredits) ?
2871 	    (F_FW_WR_EQUEQ | F_FW_WR_EQUIQ) : 0;
2872 	wr->equiq_to_len16 = BE_32(V_FW_WR_LEN16(howmany(txpkts->nflits, 2)) |
2873 	    update_bits);
2874 	wr->r3 = 0;
2875 	wr->plen = BE_16(txpkts->plen);
2876 	wr->npkt = txpkts->npkt;
2877 	wr->type = 0;
2878 
2879 	/* Everything else already written */
2880 	struct tx_sdesc *txsd = &txq->sdesc[eq->tse_pidx];
2881 	txsd->credits_used = ncredits;
2882 
2883 	txq->stats.txb_used += txsd->txb_used / TXB_CHUNK;
2884 	txq->stats.hdl_used += txsd->hdls_used;
2885 
2886 	t4_tx_incr_pending(txq, ncredits);
2887 
2888 	txq->stats.txpkts_pkts += txpkts->npkt;
2889 	txq->stats.txpkts_wrs++;
2890 	txpkts->npkt = 0;	/* emptied */
2891 }
2892 
2893 typedef enum {
2894 	COS_SUCCESS,	/* ctrl flit contains proper bits for csum offload */
2895 	COS_IGNORE,	/* no csum offload requested */
2896 	COS_FAIL,	/* csum offload requested, but pkt data missing */
2897 } csum_offload_status_t;
2898 /*
2899  * Build a ctrl1 flit for checksum offload in CPL_TX_PKT_XT command
2900  */
2901 static csum_offload_status_t
csum_to_ctrl(const struct txinfo * txinfo,uint32_t chip_version,uint64_t * ctrlp)2902 csum_to_ctrl(const struct txinfo *txinfo, uint32_t chip_version,
2903     uint64_t *ctrlp)
2904 {
2905 	const mac_ether_offload_info_t *meoi = &txinfo->meoi;
2906 	const uint32_t tx_flags = txinfo->flags;
2907 	const boolean_t needs_l3_csum = ((tx_flags & HW_LSO) != 0 || (tx_flags &
2908 	    HCK_IPV4_HDRCKSUM) != 0) && meoi->meoi_l3proto == ETHERTYPE_IP;
2909 	const boolean_t needs_l4_csum = (tx_flags & HW_LSO) != 0 ||
2910 	    (tx_flags & (HCK_FULLCKSUM | HCK_PARTIALCKSUM)) != 0;
2911 
2912 	/*
2913 	 * Default to disabling any checksumming both for cases where it is not
2914 	 * requested, but also if we cannot appropriately interrogate the
2915 	 * required information from the packet.
2916 	 */
2917 	uint64_t ctrl = F_TXPKT_L4CSUM_DIS | F_TXPKT_IPCSUM_DIS;
2918 	if (!needs_l3_csum && !needs_l4_csum) {
2919 		*ctrlp = ctrl;
2920 		return (COS_IGNORE);
2921 	}
2922 
2923 	if (needs_l3_csum) {
2924 		/* Only IPv4 checksums are supported (for L3) */
2925 		if ((meoi->meoi_flags & MEOI_L3INFO_SET) == 0) {
2926 			*ctrlp = ctrl;
2927 			return (COS_FAIL);
2928 		}
2929 		ctrl &= ~F_TXPKT_IPCSUM_DIS;
2930 	}
2931 
2932 	if (needs_l4_csum) {
2933 		/*
2934 		 * We need at least all of the L3 header to make decisions about
2935 		 * the contained L4 protocol.  If not all of the L4 information
2936 		 * is present, we will leave it to the NIC to checksum all it is
2937 		 * able to.
2938 		 */
2939 		if ((meoi->meoi_flags & MEOI_L3INFO_SET) == 0) {
2940 			*ctrlp = ctrl;
2941 			return (COS_FAIL);
2942 		}
2943 
2944 		/*
2945 		 * Since we are parsing the packet anyways, make the checksum
2946 		 * decision based on the L4 protocol, rather than using the
2947 		 * Generic TCP/UDP checksum using start & end offsets in the
2948 		 * packet (like requested with PARTIALCKSUM).
2949 		 */
2950 		int csum_type = -1;
2951 		if (meoi->meoi_l3proto == ETHERTYPE_IP &&
2952 		    meoi->meoi_l4proto == IPPROTO_TCP) {
2953 			csum_type = TX_CSUM_TCPIP;
2954 		} else if (meoi->meoi_l3proto == ETHERTYPE_IPV6 &&
2955 		    meoi->meoi_l4proto == IPPROTO_TCP) {
2956 			csum_type = TX_CSUM_TCPIP6;
2957 		} else if (meoi->meoi_l3proto == ETHERTYPE_IP &&
2958 		    meoi->meoi_l4proto == IPPROTO_UDP) {
2959 			csum_type = TX_CSUM_UDPIP;
2960 		} else if (meoi->meoi_l3proto == ETHERTYPE_IPV6 &&
2961 		    meoi->meoi_l4proto == IPPROTO_UDP) {
2962 			csum_type = TX_CSUM_UDPIP6;
2963 		} else {
2964 			*ctrlp = ctrl;
2965 			return (COS_FAIL);
2966 		}
2967 
2968 		ASSERT(csum_type != -1);
2969 		ctrl &= ~F_TXPKT_L4CSUM_DIS;
2970 		ctrl |= V_TXPKT_CSUM_TYPE(csum_type);
2971 	}
2972 
2973 	if ((ctrl & F_TXPKT_IPCSUM_DIS) == 0 &&
2974 	    (ctrl & F_TXPKT_L4CSUM_DIS) != 0) {
2975 		/*
2976 		 * If only the IPv4 checksum is requested, we need to set an
2977 		 * appropriate type in the command for it.
2978 		 */
2979 		ctrl |= V_TXPKT_CSUM_TYPE(TX_CSUM_IP);
2980 	}
2981 
2982 	ASSERT(ctrl != (F_TXPKT_L4CSUM_DIS | F_TXPKT_IPCSUM_DIS));
2983 
2984 	/*
2985 	 * Fill in the requisite L2/L3 header length data.
2986 	 *
2987 	 * The Ethernet header length is recorded as 'size - 14 bytes'
2988 	 */
2989 	const uint8_t eth_len = meoi->meoi_l2hlen - 14;
2990 	if (chip_version >= CHELSIO_T6) {
2991 		ctrl |= V_T6_TXPKT_ETHHDR_LEN(eth_len);
2992 	} else {
2993 		ctrl |= V_TXPKT_ETHHDR_LEN(eth_len);
2994 	}
2995 	ctrl |= V_TXPKT_IPHDR_LEN(meoi->meoi_l3hlen);
2996 
2997 	*ctrlp = ctrl;
2998 	return (COS_SUCCESS);
2999 }
3000 
3001 static int
write_txpkt_wr(struct port_info * pi,struct sge_txq * txq,mblk_t * m,struct txinfo * txinfo)3002 write_txpkt_wr(struct port_info *pi, struct sge_txq *txq, mblk_t *m,
3003     struct txinfo *txinfo)
3004 {
3005 	t4_sge_eq_t *eq = &txq->eq;
3006 	struct cpl_tx_pkt_core *cpl;
3007 	uint32_t ctrl;	/* used in many unrelated places */
3008 	uint64_t ctrl1;
3009 	uint16_t nflits = 0;
3010 	struct tx_sdesc *txsd;
3011 	caddr_t dst;
3012 	const mac_ether_offload_info_t *meoi = &txinfo->meoi;
3013 
3014 	TXQ_LOCK_ASSERT_OWNED(txq);	/* pidx, avail */
3015 
3016 	/*
3017 	 * Do we have enough flits to send this frame out?
3018 	 */
3019 	ctrl = sizeof (struct cpl_tx_pkt_core);
3020 	if (txinfo->flags & HW_LSO) {
3021 		nflits = TXPKT_LSO_WR_HDR_FLITS;
3022 		ctrl += sizeof (struct cpl_tx_pkt_lso_core);
3023 	} else {
3024 		nflits = TXPKT_WR_HDR_FLITS;
3025 	}
3026 	if (txinfo->nsegs > 0)
3027 		nflits += txinfo->nflits;
3028 	else {
3029 		nflits += howmany(txinfo->len, FLIT_NUM_BYTES);
3030 		ctrl += txinfo->len;
3031 	}
3032 
3033 	ASSERT3U(nflits, >, 0);
3034 
3035 	const uint16_t ncredits = EQ_FLITS_TO_HC(nflits);
3036 	if (ncredits > eq->tse_avail)
3037 		return (ENOMEM);
3038 
3039 	/* Firmware work request header */
3040 	struct fw_eth_tx_pkt_wr *wr = t4_eq_credit(eq, eq->tse_pidx);
3041 	wr->op_immdlen = cpu_to_be32(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
3042 	    V_FW_WR_IMMDLEN(ctrl));
3043 	ctrl = V_FW_WR_LEN16(howmany(nflits, 2));
3044 
3045 	/*
3046 	 * If all remaining credits are consumed by this WR, then request an EQ
3047 	 * status update to both the EQ status page and the associated ingress
3048 	 * queue entry.
3049 	 */
3050 	if (ncredits == eq->tse_avail)
3051 		ctrl |= F_FW_WR_EQUEQ | F_FW_WR_EQUIQ;
3052 
3053 	wr->equiq_to_len16 = cpu_to_be32(ctrl);
3054 	wr->r3 = 0;
3055 
3056 	if (txinfo->flags & HW_LSO &&
3057 	    (meoi->meoi_flags & MEOI_L4INFO_SET) != 0 &&
3058 	    meoi->meoi_l4proto == IPPROTO_TCP) {
3059 		struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
3060 
3061 		ctrl = V_LSO_OPCODE((u32)CPL_TX_PKT_LSO) | F_LSO_FIRST_SLICE |
3062 		    F_LSO_LAST_SLICE;
3063 
3064 		if (meoi->meoi_l2hlen > sizeof (struct ether_header)) {
3065 			/*
3066 			 * This presently assumes a standard VLAN header,
3067 			 * without support for Q-in-Q.
3068 			 */
3069 			ctrl |= V_LSO_ETHHDR_LEN(1);
3070 		}
3071 
3072 		switch (meoi->meoi_l3proto) {
3073 		case ETHERTYPE_IPV6:
3074 			ctrl |= F_LSO_IPV6;
3075 			/* FALLTHROUGH */
3076 		case ETHERTYPE_IP:
3077 			ctrl |= V_LSO_IPHDR_LEN(meoi->meoi_l3hlen / 4);
3078 			break;
3079 		default:
3080 			break;
3081 		}
3082 
3083 		ctrl |= V_LSO_TCPHDR_LEN(meoi->meoi_l4hlen / 4);
3084 
3085 		lso->lso_ctrl = cpu_to_be32(ctrl);
3086 		lso->ipid_ofst = cpu_to_be16(0);
3087 		lso->mss = cpu_to_be16(txinfo->mss);
3088 		lso->seqno_offset = cpu_to_be32(0);
3089 		if (t4_cver_eq(pi->adapter, CHELSIO_T4))
3090 			lso->len = cpu_to_be32(txinfo->len);
3091 		else
3092 			lso->len = cpu_to_be32(V_LSO_T5_XFER_SIZE(txinfo->len));
3093 
3094 		cpl = (void *)(lso + 1);
3095 
3096 		txq->stats.tso_wrs++;
3097 	} else {
3098 		cpl = (void *)(wr + 1);
3099 	}
3100 
3101 	/* Checksum offload */
3102 	switch (csum_to_ctrl(txinfo,
3103 	    CHELSIO_CHIP_VERSION(pi->adapter->params.chip), &ctrl1)) {
3104 	case COS_SUCCESS:
3105 		txq->stats.txcsum++;
3106 		break;
3107 	case COS_FAIL:
3108 		/*
3109 		 * Packet will be going out with checksums which are probably
3110 		 * wrong but there is little we can do now.
3111 		 */
3112 		txq->stats.csum_failed++;
3113 		break;
3114 	default:
3115 		break;
3116 	}
3117 
3118 	/* CPL header */
3119 	cpl->ctrl0 = cpu_to_be32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
3120 	    V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(pi->adapter->pf));
3121 	cpl->pack = 0;
3122 	cpl->len = cpu_to_be16(txinfo->len);
3123 	cpl->ctrl1 = cpu_to_be64(ctrl1);
3124 
3125 	/* Software descriptor */
3126 	txsd = &txq->sdesc[eq->tse_pidx];
3127 	txsd->mp_head = txsd->mp_tail = m;
3128 	txsd->txb_used = txinfo->txb_used;
3129 	txsd->hdls_used = txinfo->hdls_used;
3130 	txsd->credits_used = ncredits;
3131 
3132 	txq->stats.txb_used += txinfo->txb_used / TXB_CHUNK;
3133 	txq->stats.hdl_used += txinfo->hdls_used;
3134 
3135 	t4_tx_incr_pending(txq, ncredits);
3136 
3137 	/* SGL */
3138 	dst = (void *)(cpl + 1);
3139 	if (txinfo->nsegs > 0) {
3140 		txq->stats.sgl_wrs++;
3141 		copy_to_txd(eq, (void *)&txinfo->sgl, &dst, txinfo->nflits * 8);
3142 
3143 		/* Need to zero-pad to a 16 byte boundary if not on one */
3144 		if ((uintptr_t)dst & 0xf)
3145 			*(uint64_t *)dst = 0;
3146 
3147 	} else {
3148 		txq->stats.imm_wrs++;
3149 #ifdef DEBUG
3150 		ctrl = txinfo->len;
3151 #endif
3152 		for (; m; m = m->b_cont) {
3153 			copy_to_txd(eq, (void *)m->b_rptr, &dst, MBLKL(m));
3154 #ifdef DEBUG
3155 			ctrl -= MBLKL(m);
3156 #endif
3157 		}
3158 		ASSERT(ctrl == 0);
3159 	}
3160 
3161 	txq->stats.txpkt_wrs++;
3162 	return (0);
3163 }
3164 
3165 static void
t4_write_flush_wr(struct sge_txq * txq)3166 t4_write_flush_wr(struct sge_txq *txq)
3167 {
3168 	t4_sge_eq_t *eq = &txq->eq;
3169 
3170 	EQ_LOCK_ASSERT_OWNED(eq);
3171 	ASSERT3U(eq->tse_avail, >, 0);
3172 
3173 	const struct fw_eq_flush_wr wr = {
3174 		.opcode = FW_EQ_FLUSH_WR,
3175 		.equiq_to_len16 = BE_32(
3176 		    V_FW_WR_LEN16(sizeof (struct fw_eq_flush_wr) / 16) |
3177 		    F_FW_WR_EQUEQ | F_FW_WR_EQUIQ),
3178 	};
3179 	*(struct fw_eq_flush_wr *)t4_eq_credit(eq, eq->tse_pidx) = wr;
3180 
3181 	const struct tx_sdesc txsd = {
3182 		.mp_head = NULL,
3183 		.mp_tail = NULL,
3184 		.txb_used = 0,
3185 		.hdls_used = 0,
3186 		.credits_used = 1,
3187 	};
3188 	txq->sdesc[eq->tse_pidx] = txsd;
3189 
3190 	t4_tx_incr_pending(txq, 1);
3191 }
3192 
3193 /*
3194  * Increment the flit pointer by the given number of bytes.
3195  */
3196 static inline void *
t4_incr_flit(void * flitp,size_t num_bytes)3197 t4_incr_flit(void *flitp, size_t num_bytes)
3198 {
3199 	/* A flit should always start on an 8-byte boundary. */
3200 	ASSERT0(((uintptr_t)flitp + num_bytes) & 0x7);
3201 	return ((void *)((caddr_t)(flitp) + (num_bytes)));
3202 }
3203 
3204 static inline void
write_ulp_cpl_sgl(struct port_info * pi,struct sge_txq * txq,struct txpkts * txpkts,struct txinfo * txinfo)3205 write_ulp_cpl_sgl(struct port_info *pi, struct sge_txq *txq,
3206     struct txpkts *txpkts, struct txinfo *txinfo)
3207 {
3208 	struct ulp_txpkt *ulpmc;
3209 	struct ulptx_idata *ulpsc;
3210 	struct cpl_tx_pkt_core *cpl;
3211 	void *flitp = txpkts->flitp;
3212 	uint64_t ctrl;
3213 	caddr_t dst;
3214 	const uintptr_t end = (uintptr_t)txq->eq.tse_spg;
3215 
3216 	ASSERT3U(txpkts->npkt, >, 0);
3217 
3218 	/* Checksum offload */
3219 	switch (csum_to_ctrl(txinfo,
3220 	    CHELSIO_CHIP_VERSION(pi->adapter->params.chip), &ctrl)) {
3221 	case COS_SUCCESS:
3222 		txq->stats.txcsum++;
3223 		break;
3224 	case COS_FAIL:
3225 		/*
3226 		 * Packet will be going out with checksums which are probably
3227 		 * wrong but there is little we can do now.
3228 		 */
3229 		txq->stats.csum_failed++;
3230 		break;
3231 	default:
3232 		break;
3233 	}
3234 
3235 	/*
3236 	 * The previous packet's SGL must have ended at a 16 byte boundary (this
3237 	 * is required by the firmware/hardware).  It follows that flitp cannot
3238 	 * wrap around between the ULPTX master command and ULPTX subcommand (8
3239 	 * bytes each), and that it can not wrap around in the middle of the
3240 	 * cpl_tx_pkt_core either.
3241 	 */
3242 	ASSERT0((uintptr_t)flitp & 0xf);
3243 	ASSERT3U((uintptr_t)flitp + sizeof (*ulpmc), <=, end);
3244 
3245 	/* ULP master command */
3246 	ulpmc = flitp;
3247 	ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0));
3248 	ulpmc->len = htonl(howmany(sizeof (*ulpmc) + sizeof (*ulpsc) +
3249 	    sizeof (*cpl) + FLITS_TO_BYTES(txinfo->nflits), 16));
3250 
3251 	flitp = t4_incr_flit(flitp, sizeof (*ulpmc));
3252 
3253 	/* We cannot wrap-around between the ULPTX master and subcommand. */
3254 	ASSERT3U((uintptr_t)flitp, <, end);
3255 	ASSERT3U((uintptr_t)flitp + sizeof (*ulpsc), <=, end);
3256 
3257 	/* ULP subcommand */
3258 	ulpsc = flitp;
3259 	ulpsc->cmd_more = cpu_to_be32(V_ULPTX_CMD((u32)ULP_TX_SC_IMM) |
3260 	    F_ULP_TX_SC_MORE);
3261 	ulpsc->len = cpu_to_be32(sizeof (struct cpl_tx_pkt_core));
3262 
3263 	flitp = t4_incr_flit(flitp, sizeof (*ulpsc));
3264 
3265 	/* If we have reached the end, go back to the start of the ring. */
3266 	if ((uintptr_t)flitp == end)
3267 		flitp = txq->eq.tse_ring;
3268 
3269 	/* CPL_TX_PKT_XT */
3270 	cpl = flitp;
3271 	cpl->ctrl0 = cpu_to_be32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
3272 	    V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(pi->adapter->pf));
3273 	cpl->pack = 0;
3274 	cpl->len = cpu_to_be16(txinfo->len);
3275 	cpl->ctrl1 = cpu_to_be64(ctrl);
3276 
3277 	flitp = t4_incr_flit(flitp, sizeof (*cpl));
3278 
3279 	/* The CPL cannot wrap-around the end. */
3280 	ASSERT3U((uintptr_t)flitp, <=, end);
3281 
3282 	if ((uintptr_t)flitp == end)
3283 		flitp = txq->eq.tse_ring;
3284 
3285 	/* SGL for this frame */
3286 	dst = (caddr_t)flitp;
3287 	copy_to_txd(&txq->eq, (void *)&txinfo->sgl, &dst,
3288 	    FLITS_TO_BYTES(txinfo->nflits));
3289 	flitp = (void *)dst;
3290 
3291 	/* Zero pad and advance to a 16 byte boundary if not already at one. */
3292 	if (((uintptr_t)flitp & 0xf) != 0) {
3293 		/* A flit should always be on an 8 byte boundary. */
3294 		ASSERT(((uintptr_t)flitp & 0x7) == 0);
3295 
3296 		*(uint64_t *)flitp = 0;
3297 		flitp = t4_incr_flit(flitp, FLIT_NUM_BYTES);
3298 		txpkts->nflits++;
3299 	}
3300 
3301 	ASSERT0((uintptr_t)flitp & 0xf);
3302 
3303 	/*
3304 	 * The SGL can wrap-around, but lets make sure we stayed within the
3305 	 * ring.
3306 	 */
3307 	ASSERT3U((uintptr_t)flitp, <=, end);
3308 
3309 	if ((uintptr_t)flitp == end)
3310 		flitp = txq->eq.tse_ring;
3311 
3312 	txpkts->flitp = flitp;
3313 }
3314 
3315 static inline void
copy_to_txd(t4_sge_eq_t * eq,caddr_t from,caddr_t * to,size_t len)3316 copy_to_txd(t4_sge_eq_t *eq, caddr_t from, caddr_t *to, size_t len)
3317 {
3318 	/*
3319 	 * Technically the maximum WR size is lower, but this assert is just to
3320 	 * make sure nothing funky is going on with len. We subtract one from
3321 	 * the qsize because you can never totally fill the queue.
3322 	 */
3323 	ASSERT3U(len, <=, FLITS_TO_BYTES(EQ_HC_TO_FLITS(eq->tse_qsize - 1)));
3324 
3325 	if ((uintptr_t)(*to) + len <= (uintptr_t)eq->tse_spg) {
3326 		bcopy(from, *to, len);
3327 		(*to) += len;
3328 	} else {
3329 		/*
3330 		 * The number of bytes left before the end of the ring (which is
3331 		 * the status page).
3332 		 */
3333 		size_t portion = (uintptr_t)eq->tse_spg - (uintptr_t)(*to);
3334 
3335 		ASSERT3U(portion, <, len);
3336 		bcopy(from, *to, portion);
3337 		from += portion;
3338 		portion = len - portion;	/* remaining */
3339 		bcopy(from, eq->tse_ring, portion);
3340 		(*to) = (caddr_t)eq->tse_ring + portion;
3341 	}
3342 }
3343 
3344 static void
t4_tx_ring_db(struct sge_txq * txq)3345 t4_tx_ring_db(struct sge_txq *txq)
3346 {
3347 	t4_sge_eq_t *eq = &txq->eq;
3348 	struct adapter *sc = txq->port->adapter;
3349 	int val, db_mode;
3350 	t4_doorbells_t db = eq->tse_doorbells;
3351 
3352 	EQ_LOCK_ASSERT_OWNED(eq);
3353 
3354 	/*
3355 	 * A Write-Combining Work Request implicitly uses a single credit and
3356 	 * only a single credit. If we have produced more than one credit, then
3357 	 * fallback to the Write-Combining UDB, then plain UDB, and finally KDB.
3358 	 */
3359 	if (eq->tse_pending > 1)
3360 		db &= ~DOORBELL_WCWR;
3361 
3362 	(void) ddi_dma_sync(eq->tse_ring_dhdl, 0, 0, DDI_DMA_SYNC_FORDEV);
3363 
3364 	membar_producer();
3365 
3366 	val = V_PIDX(eq->tse_pending);
3367 
3368 	db_mode = (1 << (ffs(db) - 1));
3369 	switch (db_mode) {
3370 		case DOORBELL_WCWR: {
3371 			/*
3372 			 * Queues whose 128B doorbell segment fits in
3373 			 * the page do not use relative qid
3374 			 * (udb_qid is always 0).  Only queues with
3375 			 * doorbell segments can do WCWR.
3376 			 */
3377 			ASSERT(eq->tse_udb_qid == 0 && eq->tse_pending == 1);
3378 
3379 			const uint16_t credit_idx = eq->tse_pidx != 0 ?
3380 			    eq->tse_pidx - 1 : eq->tse_qsize - 1;
3381 			uint64_t *src = t4_eq_credit(eq, credit_idx);
3382 			volatile uint64_t *dst =
3383 			    (uint64_t *)(eq->tse_udb + UDBS_WR_OFFSET);
3384 
3385 			/*
3386 			 * Copy the 8 flits of the host credit to the UDB WCWR
3387 			 * space (the second 64 bytes of the 128 byte segment).
3388 			 */
3389 			const uint_t flit_count =
3390 			    sizeof (t4_eq_host_credit_t) / sizeof (uint64_t);
3391 			for (uint_t i = 0; i < flit_count; i++) {
3392 				/*
3393 				 * Perform the copy directly through the BAR
3394 				 * mapping, rather than using ddi_put64().
3395 				 *
3396 				 * The latter was found to impose a significant
3397 				 * performance burden when called in this loop.
3398 				 */
3399 				dst[i] = src[i];
3400 			}
3401 
3402 			membar_producer();
3403 			break;
3404 		}
3405 
3406 		case DOORBELL_UDB:
3407 		case DOORBELL_UDBWC:
3408 			ddi_put32(sc->bar2_hdl,
3409 			    (uint32_t *)(eq->tse_udb + UDBS_DB_OFFSET),
3410 			    LE_32(V_QID(eq->tse_udb_qid) | val));
3411 			membar_producer();
3412 			break;
3413 
3414 		case DOORBELL_KDB:
3415 			t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
3416 			    V_QID(eq->tse_cntxt_id) | val);
3417 			break;
3418 	}
3419 
3420 	eq->tse_pending = 0;
3421 }
3422 
3423 /*
3424  * Attempt to reclaim consumed host credits from the given Tx EQ. The number of
3425  * credits to reclaim is specified by 'howmany', but that value is clamped down
3426  * to the number of credits available for reclaim if it is too large. The mblks
3427  * associated with the reclaimed credits are freed inline unless a non-NULL
3428  * 'defer_freemp' is provided; in that case an mblk chain is provided to the
3429  * caller who is now responsible for freeing.
3430  *
3431  * Returns the number of reclaimed host credits.
3432  *
3433  * When debugging/analyzing this code it is important to remember that host
3434  * credits != mblks.
3435  */
3436 static uint16_t
t4_tx_reclaim_credits(struct sge_txq * txq,uint16_t howmany,mblk_t ** defer_freemp)3437 t4_tx_reclaim_credits(struct sge_txq *txq, uint16_t howmany,
3438     mblk_t **defer_freemp)
3439 {
3440 	t4_sge_eq_t *eq = &txq->eq;
3441 
3442 	EQ_LOCK_ASSERT_OWNED(eq);
3443 
3444 	const uint16_t cur_cidx = BE_16(eq->tse_spg->cidx);
3445 	const uint16_t reclaim_avail = (cur_cidx >= eq->tse_cidx) ?
3446 	    (cur_cidx - eq->tse_cidx) :
3447 	    (cur_cidx + eq->tse_qsize - eq->tse_cidx);
3448 
3449 	if (reclaim_avail == 0) {
3450 		return (0);
3451 	}
3452 
3453 	uint_t txb_freed = 0, hdl_freed = 0;
3454 	uint16_t reclaimed = 0;
3455 
3456 	do {
3457 		struct tx_sdesc *txsd = &txq->sdesc[eq->tse_cidx];
3458 		const uint16_t ncredits = txsd->credits_used;
3459 
3460 		/* Firmware doesn't return "partial" credits. */
3461 		ASSERT3U(reclaimed + ncredits, <=, reclaim_avail);
3462 
3463 		if (txsd->mp_head != NULL) {
3464 			/*
3465 			 * Even when packet content fits entirely in immediate
3466 			 * buffer, the mblk is kept around until the
3467 			 * transmission completes.
3468 			 */
3469 			if (defer_freemp != NULL) {
3470 				/*
3471 				 * Append the mblk chain from this descriptor
3472 				 * onto the end of the defer list.
3473 				 *
3474 				 * In the case that this is the first mblk we
3475 				 * have processed, the below assignment will
3476 				 * communicate the head of the chain to the
3477 				 * caller.
3478 				 */
3479 				*defer_freemp = txsd->mp_head;
3480 				defer_freemp = &txsd->mp_tail->b_next;
3481 			} else {
3482 				freemsgchain(txsd->mp_head);
3483 			}
3484 			txsd->mp_head = txsd->mp_tail = NULL;
3485 		} else {
3486 			/*
3487 			 * If mblk is NULL, this has to be the software
3488 			 * descriptor for a credit flush work request.
3489 			 */
3490 			ASSERT0(txsd->txb_used);
3491 			ASSERT0(txsd->hdls_used);
3492 			ASSERT3U(ncredits, ==, 1);
3493 		}
3494 
3495 		txb_freed += txsd->txb_used;
3496 		hdl_freed += txsd->hdls_used;
3497 		reclaimed += ncredits;
3498 
3499 		eq->tse_cidx += ncredits;
3500 		if (eq->tse_cidx >= eq->tse_qsize) {
3501 			eq->tse_cidx -= eq->tse_qsize;
3502 		}
3503 	} while (reclaimed < reclaim_avail && reclaimed < howmany);
3504 
3505 	eq->tse_avail += reclaimed;
3506 	txq->txb_avail += txb_freed;
3507 	txq->tx_dhdl_avail += hdl_freed;
3508 
3509 	ASSERT3U(eq->tse_avail, <, eq->tse_qsize);
3510 	ASSERT3U(txq->tx_dhdl_avail, <=, txq->tx_dhdl_total);
3511 
3512 	for (; hdl_freed; hdl_freed--) {
3513 		(void) ddi_dma_unbind_handle(txq->tx_dhdl[txq->tx_dhdl_cidx]);
3514 		if (++txq->tx_dhdl_cidx == txq->tx_dhdl_total)
3515 			txq->tx_dhdl_cidx = 0;
3516 	}
3517 
3518 	return (reclaimed);
3519 }
3520 
3521 static int
t4_handle_cpl_msg(t4_sge_iq_t * iq,const struct rss_header * rss,mblk_t * mp)3522 t4_handle_cpl_msg(t4_sge_iq_t *iq, const struct rss_header *rss, mblk_t *mp)
3523 {
3524 	const uint8_t opcode = rss->opcode;
3525 
3526 	DTRACE_PROBE4(t4__cpl_msg, t4_sge_iq_t *, iq, uint8_t, opcode,
3527 	    const struct rss_header *, rss, mblk_t *, mp);
3528 
3529 	switch (opcode) {
3530 	case CPL_FW4_MSG:
3531 	case CPL_FW6_MSG:
3532 		ASSERT3P(mp, ==, NULL);
3533 		return (t4_handle_fw_msg(iq, rss));
3534 	case CPL_SGE_EGR_UPDATE:
3535 		ASSERT3P(mp, ==, NULL);
3536 		t4_sge_egr_update(iq, rss);
3537 		return (0);
3538 	case CPL_RX_PKT:
3539 		/*
3540 		 * Packet RX is expected to be handled in t4_process_rx_iq().
3541 		 * CPL messages of such a type should not make it here.
3542 		 */
3543 		cxgb_printf(iq->tsi_adapter->dip, CE_WARN,
3544 		    "unexpected unhandled CPL_RX_PKT msg");
3545 		freemsg(mp);
3546 		return (0);
3547 	default:
3548 		cxgb_printf(iq->tsi_adapter->dip, CE_WARN,
3549 		    "unhandled CPL opcode 0x%02x", opcode);
3550 		if (mp != NULL) {
3551 			freemsg(mp);
3552 		}
3553 		return (0);
3554 	}
3555 }
3556 
3557 static int
t4_handle_fw_msg(t4_sge_iq_t * iq,const struct rss_header * rss)3558 t4_handle_fw_msg(t4_sge_iq_t *iq, const struct rss_header *rss)
3559 {
3560 	const struct cpl_fw6_msg *cpl = (const void *)(rss + 1);
3561 	const uint8_t msg_type = cpl->type;
3562 	const struct rss_header *rss2;
3563 	struct adapter *sc = iq->tsi_adapter;
3564 
3565 	DTRACE_PROBE3(t4__fw_msg, t4_sge_iq_t *, iq, uint8_t, msg_type,
3566 	    const struct rss_header *, rss);
3567 
3568 	switch (msg_type) {
3569 	case FW_TYPE_RSSCPL:	/* also synonym for FW6_TYPE_RSSCPL */
3570 		rss2 = (const struct rss_header *)&cpl->data[0];
3571 		return (t4_handle_cpl_msg(iq, rss2, NULL));
3572 	case FW6_TYPE_CMD_RPL:
3573 		return (t4_handle_fw_rpl(sc, &cpl->data[0]));
3574 	default:
3575 		cxgb_printf(sc->dip, CE_WARN,
3576 		    "unhandled fw_msg type 0x%02x", msg_type);
3577 		return (0);
3578 	}
3579 }
3580 
3581 static void
t4_fl_ring_db(struct sge_fl * fl)3582 t4_fl_ring_db(struct sge_fl *fl)
3583 {
3584 	struct adapter *sc = t4_fl_to_iq(fl)->tsi_adapter;
3585 	t4_sge_eq_t *eq = &fl->eq;
3586 
3587 	EQ_LOCK_ASSERT_OWNED(eq);
3588 
3589 	(void) ddi_dma_sync(eq->tse_ring_dhdl, 0, 0, DDI_DMA_SYNC_FORDEV);
3590 
3591 	membar_producer();
3592 
3593 	t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
3594 	    sc->params.arch.sge_fl_db |
3595 	    V_QID(eq->tse_cntxt_id) |
3596 	    V_PIDX(eq->tse_pending));
3597 
3598 	eq->tse_pending = 0;
3599 }
3600 
3601 static void
t4_sge_egr_update(t4_sge_iq_t * iq,const struct rss_header * rss)3602 t4_sge_egr_update(t4_sge_iq_t *iq, const struct rss_header *rss)
3603 {
3604 	struct adapter *sc = iq->tsi_adapter;
3605 	const struct cpl_sge_egr_update *cpl = t4_rss_payload(rss);
3606 	const uint_t qid = G_EGR_QID(BE_32(cpl->opcode_qid));
3607 	struct sge_txq *txq = (struct sge_txq *)(*t4_eqmap_slot(sc, qid));
3608 	t4_sge_eq_t *eq = &txq->eq;
3609 
3610 	/*
3611 	 * Get a "live" snapshot of the flags and PIDX state from the TXQ.
3612 	 *
3613 	 * This is done without the protection of the TXQ/EQ lock, since the
3614 	 * gathered information is used to avoid contending on that lock for the
3615 	 * reclaim.
3616 	 */
3617 	membar_consumer();
3618 	const uint16_t live_pidx = BE_16(eq->tse_pidx);
3619 	const t4_eq_flags_t live_flags = eq->tse_flags;
3620 
3621 	if ((live_flags & EQ_CORKED) == 0 &&
3622 	    (cpl->pidx != cpl->cidx || live_pidx != cpl->cidx)) {
3623 		/*
3624 		 * A reclaim of the ring can be skipped if:
3625 		 *
3626 		 * 1. The EQ is not in the "corked" state, where it was unable
3627 		 *    allocate descriptors (or memory) while attempting to place
3628 		 *    a packet in the TXQ.
3629 		 *
3630 		 * 2. There are outstanding transmit descriptors in the EQ which
3631 		 *    will trigger a subsequent SGE_EGR_UPDATE notification.
3632 		 *
3633 		 * When those conditions are met, it is safe to skip performing
3634 		 * a reclaim here, reducing the chance that we contend with
3635 		 * other transmission activity against the TXQ.
3636 		 */
3637 		DTRACE_PROBE2(t4__elide__reclaim,
3638 		    struct sge_txq *, txq, struct cpl_sge_egr_update *, cpl);
3639 		return;
3640 	}
3641 
3642 	mblk_t *freemp = NULL;
3643 	bool do_mac_update = false;
3644 
3645 	TXQ_LOCK(txq);
3646 	(void) t4_tx_reclaim_credits(txq, eq->tse_qsize, &freemp);
3647 	if (eq->tse_flags & EQ_CORKED && eq->tse_avail != 0) {
3648 		do_mac_update = true;
3649 		eq->tse_flags &= ~EQ_CORKED;
3650 	}
3651 	TXQ_UNLOCK(txq);
3652 
3653 	freemsgchain(freemp);
3654 	if (do_mac_update) {
3655 		t4_mac_tx_update(txq->port, txq);
3656 	}
3657 }
3658 
3659 #define	KS_UINIT(x)	kstat_named_init(&kstatp->x, #x, KSTAT_DATA_ULONG)
3660 #define	KS_CINIT(x)	kstat_named_init(&kstatp->x, #x, KSTAT_DATA_CHAR)
3661 #define	KS_U_SET(x, y)	kstatp->x.value.ul = (y)
3662 #define	KS_U_FROM(x, y)	kstatp->x.value.ul = (y)->stats.x
3663 #define	KS_C_SET(x, ...)	\
3664 			(void) snprintf(kstatp->x.value.c, 16,  __VA_ARGS__)
3665 
3666 /*
3667  * cxgbe:X:config
3668  */
3669 struct cxgbe_port_config_kstats {
3670 	kstat_named_t idx;
3671 	kstat_named_t rxq_count;
3672 	kstat_named_t txq_count;
3673 	kstat_named_t rxq_start;
3674 	kstat_named_t txq_start;
3675 	kstat_named_t controller;
3676 	kstat_named_t factory_mac_address;
3677 };
3678 
3679 /*
3680  * cxgbe:X:info
3681  */
3682 struct cxgbe_port_info_kstats {
3683 	kstat_named_t transceiver;
3684 	kstat_named_t rx_ovflow0;
3685 	kstat_named_t rx_ovflow1;
3686 	kstat_named_t rx_ovflow2;
3687 	kstat_named_t rx_ovflow3;
3688 	kstat_named_t rx_trunc0;
3689 	kstat_named_t rx_trunc1;
3690 	kstat_named_t rx_trunc2;
3691 	kstat_named_t rx_trunc3;
3692 	kstat_named_t tx_pause;
3693 	kstat_named_t rx_pause;
3694 };
3695 
3696 static kstat_t *
setup_port_config_kstats(struct port_info * pi)3697 setup_port_config_kstats(struct port_info *pi)
3698 {
3699 	kstat_t *ksp;
3700 	struct cxgbe_port_config_kstats *kstatp;
3701 	int ndata;
3702 	dev_info_t *pdip = ddi_get_parent(pi->dip);
3703 	uint8_t *ma = &pi->hw_addr[0];
3704 
3705 	ndata = sizeof (struct cxgbe_port_config_kstats) /
3706 	    sizeof (kstat_named_t);
3707 
3708 	ksp = kstat_create(T4_PORT_NAME, ddi_get_instance(pi->dip), "config",
3709 	    "net", KSTAT_TYPE_NAMED, ndata, 0);
3710 	if (ksp == NULL) {
3711 		cxgb_printf(pi->dip, CE_WARN, "failed to initialize kstats.");
3712 		return (NULL);
3713 	}
3714 
3715 	kstatp = (struct cxgbe_port_config_kstats *)ksp->ks_data;
3716 
3717 	KS_UINIT(idx);
3718 	KS_UINIT(rxq_count);
3719 	KS_UINIT(txq_count);
3720 	KS_UINIT(rxq_start);
3721 	KS_UINIT(txq_start);
3722 	KS_CINIT(controller);
3723 	KS_CINIT(factory_mac_address);
3724 
3725 	KS_U_SET(idx, pi->port_id);
3726 	KS_U_SET(rxq_count, pi->rxq_count);
3727 	KS_U_SET(txq_count, pi->txq_count);
3728 	KS_U_SET(rxq_start, pi->rxq_start);
3729 	KS_U_SET(txq_start, pi->txq_start);
3730 	KS_C_SET(controller, "%s%d", ddi_driver_name(pdip),
3731 	    ddi_get_instance(pdip));
3732 	KS_C_SET(factory_mac_address, "%02X%02X%02X%02X%02X%02X",
3733 	    ma[0], ma[1], ma[2], ma[3], ma[4], ma[5]);
3734 
3735 	/* Do NOT set ksp->ks_update.  These kstats do not change. */
3736 
3737 	/* Install the kstat */
3738 	ksp->ks_private = (void *)pi;
3739 	kstat_install(ksp);
3740 
3741 	return (ksp);
3742 }
3743 
3744 static kstat_t *
setup_port_info_kstats(struct port_info * pi)3745 setup_port_info_kstats(struct port_info *pi)
3746 {
3747 	kstat_t *ksp;
3748 	struct cxgbe_port_info_kstats *kstatp;
3749 	int ndata;
3750 
3751 	ndata = sizeof (struct cxgbe_port_info_kstats) / sizeof (kstat_named_t);
3752 
3753 	ksp = kstat_create(T4_PORT_NAME, ddi_get_instance(pi->dip), "info",
3754 	    "net", KSTAT_TYPE_NAMED, ndata, 0);
3755 	if (ksp == NULL) {
3756 		cxgb_printf(pi->dip, CE_WARN, "failed to initialize kstats.");
3757 		return (NULL);
3758 	}
3759 
3760 	kstatp = (struct cxgbe_port_info_kstats *)ksp->ks_data;
3761 
3762 	KS_CINIT(transceiver);
3763 	KS_UINIT(rx_ovflow0);
3764 	KS_UINIT(rx_ovflow1);
3765 	KS_UINIT(rx_ovflow2);
3766 	KS_UINIT(rx_ovflow3);
3767 	KS_UINIT(rx_trunc0);
3768 	KS_UINIT(rx_trunc1);
3769 	KS_UINIT(rx_trunc2);
3770 	KS_UINIT(rx_trunc3);
3771 	KS_UINIT(tx_pause);
3772 	KS_UINIT(rx_pause);
3773 
3774 	/* Install the kstat */
3775 	ksp->ks_update = update_port_info_kstats;
3776 	ksp->ks_private = (void *)pi;
3777 	kstat_install(ksp);
3778 
3779 	return (ksp);
3780 }
3781 
3782 static int
update_port_info_kstats(kstat_t * ksp,int rw)3783 update_port_info_kstats(kstat_t *ksp, int rw)
3784 {
3785 	struct cxgbe_port_info_kstats *kstatp =
3786 	    (struct cxgbe_port_info_kstats *)ksp->ks_data;
3787 	struct port_info *pi = ksp->ks_private;
3788 	static const char *mod_str[] = { NULL, "LR", "SR", "ER", "TWINAX",
3789 	    "active TWINAX", "LRM" };
3790 	uint32_t bgmap;
3791 
3792 	if (rw == KSTAT_WRITE)
3793 		return (0);
3794 
3795 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
3796 		KS_C_SET(transceiver, "unplugged");
3797 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
3798 		KS_C_SET(transceiver, "unknown");
3799 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
3800 		KS_C_SET(transceiver, "unsupported");
3801 	else if (pi->mod_type > 0 && pi->mod_type < ARRAY_SIZE(mod_str))
3802 		KS_C_SET(transceiver, "%s", mod_str[pi->mod_type]);
3803 	else
3804 		KS_C_SET(transceiver, "type %d", pi->mod_type);
3805 
3806 #define	GET_STAT(name) t4_read_reg64(pi->adapter, \
3807 	    PORT_REG(pi->port_id, A_MPS_PORT_STAT_##name##_L))
3808 #define	GET_STAT_COM(name) t4_read_reg64(pi->adapter, \
3809 	    A_MPS_STAT_##name##_L)
3810 
3811 	bgmap = G_NUMPORTS(t4_read_reg(pi->adapter, A_MPS_CMN_CTL));
3812 	if (bgmap == 0)
3813 		bgmap = (pi->port_id == 0) ? 0xf : 0;
3814 	else if (bgmap == 1)
3815 		bgmap = (pi->port_id < 2) ? (3 << (2 * pi->port_id)) : 0;
3816 	else
3817 		bgmap = 1;
3818 
3819 	KS_U_SET(rx_ovflow0, (bgmap & 1) ?
3820 	    GET_STAT_COM(RX_BG_0_MAC_DROP_FRAME) : 0);
3821 	KS_U_SET(rx_ovflow1, (bgmap & 2) ?
3822 	    GET_STAT_COM(RX_BG_1_MAC_DROP_FRAME) : 0);
3823 	KS_U_SET(rx_ovflow2, (bgmap & 4) ?
3824 	    GET_STAT_COM(RX_BG_2_MAC_DROP_FRAME) : 0);
3825 	KS_U_SET(rx_ovflow3, (bgmap & 8) ?
3826 	    GET_STAT_COM(RX_BG_3_MAC_DROP_FRAME) : 0);
3827 	KS_U_SET(rx_trunc0,  (bgmap & 1) ?
3828 	    GET_STAT_COM(RX_BG_0_MAC_TRUNC_FRAME) : 0);
3829 	KS_U_SET(rx_trunc1,  (bgmap & 2) ?
3830 	    GET_STAT_COM(RX_BG_1_MAC_TRUNC_FRAME) : 0);
3831 	KS_U_SET(rx_trunc2,  (bgmap & 4) ?
3832 	    GET_STAT_COM(RX_BG_2_MAC_TRUNC_FRAME) : 0);
3833 	KS_U_SET(rx_trunc3,  (bgmap & 8) ?
3834 	    GET_STAT_COM(RX_BG_3_MAC_TRUNC_FRAME) : 0);
3835 
3836 	KS_U_SET(tx_pause, GET_STAT(TX_PORT_PAUSE));
3837 	KS_U_SET(rx_pause, GET_STAT(RX_PORT_PAUSE));
3838 
3839 	return (0);
3840 
3841 }
3842 
3843 /*
3844  * cxgbe:X:rxqY
3845  */
3846 struct rxq_kstats {
3847 	kstat_named_t rxcsum;
3848 	kstat_named_t rxpkts;
3849 	kstat_named_t rxbytes;
3850 };
3851 
3852 static kstat_t *
setup_rxq_kstats(struct port_info * pi,struct sge_rxq * rxq,uint_t q_idx)3853 setup_rxq_kstats(struct port_info *pi, struct sge_rxq *rxq, uint_t q_idx)
3854 {
3855 	struct kstat *ksp;
3856 	struct rxq_kstats *kstatp;
3857 	int ndata;
3858 	char str[16];
3859 
3860 	ndata = sizeof (struct rxq_kstats) / sizeof (kstat_named_t);
3861 	(void) snprintf(str, sizeof (str), "rxq%u", q_idx);
3862 
3863 	ksp = kstat_create(T4_PORT_NAME, ddi_get_instance(pi->dip), str, "rxq",
3864 	    KSTAT_TYPE_NAMED, ndata, 0);
3865 	if (ksp == NULL) {
3866 		cxgb_printf(pi->dip, CE_WARN,
3867 		    "%s: failed to initialize rxq kstats for queue %u.",
3868 		    __func__, q_idx);
3869 		return (NULL);
3870 	}
3871 
3872 	kstatp = (struct rxq_kstats *)ksp->ks_data;
3873 
3874 	KS_UINIT(rxcsum);
3875 	KS_UINIT(rxpkts);
3876 	KS_UINIT(rxbytes);
3877 
3878 	ksp->ks_update = update_rxq_kstats;
3879 	ksp->ks_private = (void *)rxq;
3880 	kstat_install(ksp);
3881 
3882 	return (ksp);
3883 }
3884 
3885 static int
update_rxq_kstats(kstat_t * ksp,int rw)3886 update_rxq_kstats(kstat_t *ksp, int rw)
3887 {
3888 	struct rxq_kstats *kstatp = (struct rxq_kstats *)ksp->ks_data;
3889 	struct sge_rxq *rxq = ksp->ks_private;
3890 
3891 	if (rw == KSTAT_WRITE)
3892 		return (0);
3893 
3894 	KS_U_FROM(rxcsum, rxq);
3895 	KS_U_FROM(rxpkts, rxq);
3896 	KS_U_FROM(rxbytes, rxq);
3897 
3898 	return (0);
3899 }
3900 
3901 /*
3902  * cxgbe:X:txqY
3903  */
3904 struct txq_kstats {
3905 	kstat_named_t txcsum;
3906 	kstat_named_t tso_wrs;
3907 	kstat_named_t imm_wrs;
3908 	kstat_named_t sgl_wrs;
3909 	kstat_named_t txpkt_wrs;
3910 	kstat_named_t txpkts_wrs;
3911 	kstat_named_t txpkts_pkts;
3912 	kstat_named_t txb_used;
3913 	kstat_named_t hdl_used;
3914 	kstat_named_t txb_full;
3915 	kstat_named_t dma_hdl_failed;
3916 	kstat_named_t dma_map_failed;
3917 	kstat_named_t qfull;
3918 	kstat_named_t pullup_early;
3919 	kstat_named_t pullup_late;
3920 	kstat_named_t pullup_failed;
3921 	kstat_named_t csum_failed;
3922 };
3923 
3924 static kstat_t *
setup_txq_kstats(struct port_info * pi,struct sge_txq * txq,int idx)3925 setup_txq_kstats(struct port_info *pi, struct sge_txq *txq, int idx)
3926 {
3927 	struct kstat *ksp;
3928 	struct txq_kstats *kstatp;
3929 	int ndata;
3930 	char str[16];
3931 
3932 	ndata = sizeof (struct txq_kstats) / sizeof (kstat_named_t);
3933 	(void) snprintf(str, sizeof (str), "txq%u", idx);
3934 
3935 	ksp = kstat_create(T4_PORT_NAME, ddi_get_instance(pi->dip), str, "txq",
3936 	    KSTAT_TYPE_NAMED, ndata, 0);
3937 	if (ksp == NULL) {
3938 		cxgb_printf(pi->dip, CE_WARN,
3939 		    "%s: failed to initialize txq kstats for queue %d.",
3940 		    __func__, idx);
3941 		return (NULL);
3942 	}
3943 
3944 	kstatp = (struct txq_kstats *)ksp->ks_data;
3945 
3946 	KS_UINIT(txcsum);
3947 	KS_UINIT(tso_wrs);
3948 	KS_UINIT(imm_wrs);
3949 	KS_UINIT(sgl_wrs);
3950 	KS_UINIT(txpkt_wrs);
3951 	KS_UINIT(txpkts_wrs);
3952 	KS_UINIT(txpkts_pkts);
3953 	KS_UINIT(txb_used);
3954 	KS_UINIT(hdl_used);
3955 	KS_UINIT(txb_full);
3956 	KS_UINIT(dma_hdl_failed);
3957 	KS_UINIT(dma_map_failed);
3958 	KS_UINIT(qfull);
3959 	KS_UINIT(pullup_early);
3960 	KS_UINIT(pullup_late);
3961 	KS_UINIT(pullup_failed);
3962 	KS_UINIT(csum_failed);
3963 
3964 	ksp->ks_update = update_txq_kstats;
3965 	ksp->ks_private = (void *)txq;
3966 	kstat_install(ksp);
3967 
3968 	return (ksp);
3969 }
3970 
3971 static int
update_txq_kstats(kstat_t * ksp,int rw)3972 update_txq_kstats(kstat_t *ksp, int rw)
3973 {
3974 	struct txq_kstats *kstatp = (struct txq_kstats *)ksp->ks_data;
3975 	struct sge_txq *txq = ksp->ks_private;
3976 
3977 	if (rw == KSTAT_WRITE)
3978 		return (0);
3979 
3980 	KS_U_FROM(txcsum, txq);
3981 	KS_U_FROM(tso_wrs, txq);
3982 	KS_U_FROM(imm_wrs, txq);
3983 	KS_U_FROM(sgl_wrs, txq);
3984 	KS_U_FROM(txpkt_wrs, txq);
3985 	KS_U_FROM(txpkts_wrs, txq);
3986 	KS_U_FROM(txpkts_pkts, txq);
3987 	KS_U_FROM(txb_used, txq);
3988 	KS_U_FROM(hdl_used, txq);
3989 	KS_U_FROM(txb_full, txq);
3990 	KS_U_FROM(dma_hdl_failed, txq);
3991 	KS_U_FROM(dma_map_failed, txq);
3992 	KS_U_FROM(qfull, txq);
3993 	KS_U_FROM(pullup_early, txq);
3994 	KS_U_FROM(pullup_late, txq);
3995 	KS_U_FROM(pullup_failed, txq);
3996 	KS_U_FROM(csum_failed, txq);
3997 
3998 	return (0);
3999 }
4000 
4001 static int rxbuf_ctor(void *, void *, int);
4002 static void rxbuf_dtor(void *, void *);
4003 
4004 static kmem_cache_t *
rxbuf_cache_create(struct rxbuf_cache_params * p)4005 rxbuf_cache_create(struct rxbuf_cache_params *p)
4006 {
4007 	char name[32];
4008 
4009 	(void) snprintf(name, sizeof (name), "%s%d_rxbuf_cache",
4010 	    ddi_driver_name(p->dip), ddi_get_instance(p->dip));
4011 
4012 	return kmem_cache_create(name, sizeof (struct rxbuf), _CACHE_LINE_SIZE,
4013 	    rxbuf_ctor, rxbuf_dtor, NULL, p, NULL, 0);
4014 }
4015 
4016 static struct rxbuf *
rxbuf_alloc(kmem_cache_t * cache,int kmflags)4017 rxbuf_alloc(kmem_cache_t *cache, int kmflags)
4018 {
4019 	struct rxbuf *rxb;
4020 
4021 	rxb = kmem_cache_alloc(cache, kmflags);
4022 	if (rxb != NULL) {
4023 		rxb->ref_cnt = 1;
4024 		rxb->cache = cache;
4025 	}
4026 
4027 	return (rxb);
4028 }
4029 
4030 /*
4031  * This is normally called via the rxb's freefunc, when an mblk referencing the
4032  * rxb is freed.
4033  */
4034 static void
rxbuf_free(struct rxbuf * rxb)4035 rxbuf_free(struct rxbuf *rxb)
4036 {
4037 	if (atomic_dec_uint_nv(&rxb->ref_cnt) == 0)
4038 		kmem_cache_free(rxb->cache, rxb);
4039 }
4040 
4041 static int
rxbuf_ctor(void * arg1,void * arg2,int kmflag)4042 rxbuf_ctor(void *arg1, void *arg2, int kmflag)
4043 {
4044 	struct rxbuf *rxb = arg1;
4045 	struct rxbuf_cache_params *p = arg2;
4046 	size_t real_len;
4047 	ddi_dma_cookie_t cookie;
4048 	uint_t ccount = 0;
4049 	int (*callback)(caddr_t);
4050 	int rc = ENOMEM;
4051 
4052 	if ((kmflag & KM_NOSLEEP) != 0)
4053 		callback = DDI_DMA_DONTWAIT;
4054 	else
4055 		callback = DDI_DMA_SLEEP;
4056 
4057 	rc = ddi_dma_alloc_handle(p->dip, &p->dma_attr_rx, callback, 0,
4058 	    &rxb->dhdl);
4059 	if (rc != DDI_SUCCESS)
4060 		return (rc == DDI_DMA_BADATTR ? EINVAL : ENOMEM);
4061 
4062 	rc = ddi_dma_mem_alloc(rxb->dhdl, p->buf_size, &p->acc_attr_rx,
4063 	    DDI_DMA_STREAMING, callback, 0, &rxb->va, &real_len, &rxb->ahdl);
4064 	if (rc != DDI_SUCCESS) {
4065 		rc = ENOMEM;
4066 		goto fail1;
4067 	}
4068 
4069 	rc = ddi_dma_addr_bind_handle(rxb->dhdl, NULL, rxb->va, p->buf_size,
4070 	    DDI_DMA_READ | DDI_DMA_STREAMING, NULL, NULL, &cookie, &ccount);
4071 	if (rc != DDI_DMA_MAPPED) {
4072 		if (rc == DDI_DMA_INUSE)
4073 			rc = EBUSY;
4074 		else if (rc == DDI_DMA_TOOBIG)
4075 			rc = E2BIG;
4076 		else
4077 			rc = ENOMEM;
4078 		goto fail2;
4079 	}
4080 
4081 	if (ccount != 1) {
4082 		rc = E2BIG;
4083 		goto fail3;
4084 	}
4085 
4086 	rxb->ref_cnt = 0;
4087 	rxb->buf_size = p->buf_size;
4088 	rxb->freefunc.free_arg = (caddr_t)rxb;
4089 	rxb->freefunc.free_func = rxbuf_free;
4090 	rxb->ba = cookie.dmac_laddress;
4091 
4092 	return (0);
4093 
4094 fail3:	(void) ddi_dma_unbind_handle(rxb->dhdl);
4095 fail2:	ddi_dma_mem_free(&rxb->ahdl);
4096 fail1:	ddi_dma_free_handle(&rxb->dhdl);
4097 	return (rc);
4098 }
4099 
4100 static void
rxbuf_dtor(void * arg1,void * arg2)4101 rxbuf_dtor(void *arg1, void *arg2)
4102 {
4103 	struct rxbuf *rxb = arg1;
4104 
4105 	(void) ddi_dma_unbind_handle(rxb->dhdl);
4106 	ddi_dma_mem_free(&rxb->ahdl);
4107 	ddi_dma_free_handle(&rxb->dhdl);
4108 }
4109