1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012, 2015 Chelsio Communications, Inc.
5 * All rights reserved.
6 * Written by: Navdeep Parhar <np@FreeBSD.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD$
30 *
31 */
32
33#ifndef __T4_TOM_H__
34#define __T4_TOM_H__
35#include <sys/vmem.h>
36#include "common/t4_hw.h"
37#include "common/t4_msg.h"
38#include "tom/t4_tls.h"
39
40#define LISTEN_HASH_SIZE 32
41
42/*
43 * Min receive window.  We want it to be large enough to accommodate receive
44 * coalescing, handle jumbo frames, and not trigger sender SWS avoidance.
45 */
46#define MIN_RCV_WND (24 * 1024U)
47
48/*
49 * Max receive window supported by HW in bytes.  Only a small part of it can
50 * be set through option0, the rest needs to be set through RX_DATA_ACK.
51 */
52#define MAX_RCV_WND ((1U << 27) - 1)
53
54#define	DDP_RSVD_WIN (16 * 1024U)
55#define	SB_DDP_INDICATE	SB_IN_TOE	/* soreceive must respond to indicate */
56
57#define USE_DDP_RX_FLOW_CONTROL
58
59#define PPOD_SZ(n)	((n) * sizeof(struct pagepod))
60#define PPOD_SIZE	(PPOD_SZ(1))
61
62/* TOE PCB flags */
63enum {
64	TPF_ATTACHED	   = (1 << 0),	/* a tcpcb refers to this toepcb */
65	TPF_FLOWC_WR_SENT  = (1 << 1),	/* firmware flow context WR sent */
66	TPF_TX_DATA_SENT   = (1 << 2),	/* some data sent */
67	TPF_TX_SUSPENDED   = (1 << 3),	/* tx suspended for lack of resources */
68	TPF_SEND_FIN	   = (1 << 4),	/* send FIN after all pending data */
69	TPF_FIN_SENT	   = (1 << 5),	/* FIN has been sent */
70	TPF_ABORT_SHUTDOWN = (1 << 6),	/* connection abort is in progress */
71	TPF_CPL_PENDING    = (1 << 7),	/* haven't received the last CPL */
72	TPF_SYNQE	   = (1 << 8),	/* synq_entry, not really a toepcb */
73	TPF_SYNQE_EXPANDED = (1 << 9),	/* toepcb ready, tid context updated */
74	TPF_FORCE_CREDITS  = (1 << 10), /* always send credits */
75	TPF_INITIALIZED    = (1 << 12), /* init_toepcb has been called */
76	TPF_TLS_RECEIVE	   = (1 << 13), /* should receive TLS records */
77};
78
79enum {
80	DDP_OK		= (1 << 0),	/* OK to turn on DDP */
81	DDP_SC_REQ	= (1 << 1),	/* state change (on/off) requested */
82	DDP_ON		= (1 << 2),	/* DDP is turned on */
83	DDP_BUF0_ACTIVE	= (1 << 3),	/* buffer 0 in use (not invalidated) */
84	DDP_BUF1_ACTIVE	= (1 << 4),	/* buffer 1 in use (not invalidated) */
85	DDP_TASK_ACTIVE = (1 << 5),	/* requeue task is queued / running */
86	DDP_DEAD	= (1 << 6),	/* toepcb is shutting down */
87};
88
89struct sockopt;
90struct offload_settings;
91
92/*
93 * Connection parameters for an offloaded connection.  These are mostly (but not
94 * all) hardware TOE parameters.
95 */
96struct conn_params {
97	int8_t rx_coalesce;
98	int8_t cong_algo;
99	int8_t tc_idx;
100	int8_t tstamp;
101	int8_t sack;
102	int8_t nagle;
103	int8_t keepalive;
104	int8_t wscale;
105	int8_t ecn;
106	int8_t mtu_idx;
107	int8_t ulp_mode;
108	int8_t tx_align;
109	int16_t txq_idx;	/* ofld_txq = &sc->sge.ofld_txq[txq_idx] */
110	int16_t rxq_idx;	/* ofld_rxq = &sc->sge.ofld_rxq[rxq_idx] */
111	int16_t l2t_idx;
112	uint16_t emss;
113	uint16_t opt0_bufsize;
114	u_int sndbuf;		/* controls TP tx pages */
115};
116
117struct ofld_tx_sdesc {
118	uint32_t plen;		/* payload length */
119	uint8_t tx_credits;	/* firmware tx credits (unit is 16B) */
120	void *iv_buffer;	/* optional buffer holding IVs for TLS */
121};
122
123struct ppod_region {
124	u_int pr_start;
125	u_int pr_len;
126	u_int pr_page_shift[4];
127	uint32_t pr_tag_mask;		/* hardware tagmask for this region. */
128	uint32_t pr_invalid_bit;	/* OR with this to invalidate tag. */
129	uint32_t pr_alias_mask;		/* AND with tag to get alias bits. */
130	u_int pr_alias_shift;		/* shift this much for first alias bit. */
131	vmem_t *pr_arena;
132};
133
134struct ppod_reservation {
135	struct ppod_region *prsv_pr;
136	uint32_t prsv_tag;		/* Full tag: pgsz, alias, tag, color */
137	u_int prsv_nppods;
138};
139
140struct pageset {
141	TAILQ_ENTRY(pageset) link;
142	vm_page_t *pages;
143	int npages;
144	int flags;
145	int offset;		/* offset in first page */
146	int len;
147	struct ppod_reservation prsv;
148	struct vmspace *vm;
149	vm_offset_t start;
150	u_int vm_timestamp;
151};
152
153TAILQ_HEAD(pagesetq, pageset);
154
155#define	PS_WIRED		0x0001	/* Pages wired rather than held. */
156#define	PS_PPODS_WRITTEN	0x0002	/* Page pods written to the card. */
157
158#define	EXT_FLAG_AIOTX		EXT_FLAG_VENDOR1
159
160#define	IS_AIOTX_MBUF(m)						\
161	((m)->m_flags & M_EXT && (m)->m_ext.ext_flags & EXT_FLAG_AIOTX)
162
163struct ddp_buffer {
164	struct pageset *ps;
165
166	struct kaiocb *job;
167	int cancel_pending;
168};
169
170struct ddp_pcb {
171	u_int flags;
172	struct ddp_buffer db[2];
173	TAILQ_HEAD(, pageset) cached_pagesets;
174	TAILQ_HEAD(, kaiocb) aiojobq;
175	u_int waiting_count;
176	u_int active_count;
177	u_int cached_count;
178	int active_id;	/* the currently active DDP buffer */
179	struct task requeue_task;
180	struct kaiocb *queueing;
181	struct mtx lock;
182};
183
184struct aiotx_buffer {
185	struct pageset ps;
186	struct kaiocb *job;
187	int refcount;
188};
189
190struct toepcb {
191	struct tom_data *td;
192	struct inpcb *inp;	/* backpointer to host stack's PCB */
193	u_int flags;		/* miscellaneous flags */
194	TAILQ_ENTRY(toepcb) link; /* toep_list */
195	int refcount;
196	struct vnet *vnet;
197	struct vi_info *vi;	/* virtual interface */
198	struct sge_wrq *ofld_txq;
199	struct sge_ofld_rxq *ofld_rxq;
200	struct sge_wrq *ctrlq;
201	struct l2t_entry *l2te;	/* L2 table entry used by this connection */
202	struct clip_entry *ce;	/* CLIP table entry used by this tid */
203	int tid;		/* Connection identifier */
204
205	/* tx credit handling */
206	u_int tx_total;		/* total tx WR credits (in 16B units) */
207	u_int tx_credits;	/* tx WR credits (in 16B units) available */
208	u_int tx_nocompl;	/* tx WR credits since last compl request */
209	u_int plen_nocompl;	/* payload since last compl request */
210
211	struct conn_params params;
212
213	void *ulpcb;
214	void *ulpcb2;
215	struct mbufq ulp_pduq;	/* PDUs waiting to be sent out. */
216	struct mbufq ulp_pdu_reclaimq;
217
218	struct ddp_pcb ddp;
219	struct tls_ofld_info tls;
220
221	TAILQ_HEAD(, kaiocb) aiotx_jobq;
222	struct task aiotx_task;
223	struct socket *aiotx_so;
224
225	/* Tx software descriptor */
226	uint8_t txsd_total;
227	uint8_t txsd_pidx;
228	uint8_t txsd_cidx;
229	uint8_t txsd_avail;
230	struct ofld_tx_sdesc txsd[];
231};
232
233static inline int
234ulp_mode(struct toepcb *toep)
235{
236
237	return (toep->params.ulp_mode);
238}
239
240#define	DDP_LOCK(toep)		mtx_lock(&(toep)->ddp.lock)
241#define	DDP_UNLOCK(toep)	mtx_unlock(&(toep)->ddp.lock)
242#define	DDP_ASSERT_LOCKED(toep)	mtx_assert(&(toep)->ddp.lock, MA_OWNED)
243
244/*
245 * Compressed state for embryonic connections for a listener.
246 */
247struct synq_entry {
248	struct listen_ctx *lctx;	/* backpointer to listen ctx */
249	struct mbuf *syn;
250	int flags;			/* same as toepcb's tp_flags */
251	volatile int ok_to_respond;
252	volatile u_int refcnt;
253	int tid;
254	uint32_t iss;
255	uint32_t irs;
256	uint32_t ts;
257	__be16 tcp_opt; /* from cpl_pass_establish */
258	struct toepcb *toep;
259
260	struct conn_params params;
261};
262
263/* listen_ctx flags */
264#define LCTX_RPL_PENDING 1	/* waiting for a CPL_PASS_OPEN_RPL */
265
266struct listen_ctx {
267	LIST_ENTRY(listen_ctx) link;	/* listen hash linkage */
268	volatile int refcount;
269	int stid;
270	struct stid_region stid_region;
271	int flags;
272	struct inpcb *inp;		/* listening socket's inp */
273	struct vnet *vnet;
274	struct sge_wrq *ctrlq;
275	struct sge_ofld_rxq *ofld_rxq;
276	struct clip_entry *ce;
277};
278
279/* tcb_histent flags */
280#define TE_RPL_PENDING	1
281#define TE_ACTIVE	2
282
283/* bits in one 8b tcb_histent sample. */
284#define TS_RTO			(1 << 0)
285#define TS_DUPACKS		(1 << 1)
286#define TS_FASTREXMT		(1 << 2)
287#define TS_SND_BACKLOGGED	(1 << 3)
288#define TS_CWND_LIMITED		(1 << 4)
289#define TS_ECN_ECE		(1 << 5)
290#define TS_ECN_CWR		(1 << 6)
291#define TS_RESERVED		(1 << 7)	/* Unused. */
292
293struct tcb_histent {
294	struct mtx te_lock;
295	struct callout te_callout;
296	uint64_t te_tcb[TCB_SIZE / sizeof(uint64_t)];
297	struct adapter *te_adapter;
298	u_int te_flags;
299	u_int te_tid;
300	uint8_t te_pidx;
301	uint8_t te_sample[100];
302};
303
304struct tom_data {
305	struct toedev tod;
306
307	/* toepcb's associated with this TOE device */
308	struct mtx toep_list_lock;
309	TAILQ_HEAD(, toepcb) toep_list;
310
311	struct mtx lctx_hash_lock;
312	LIST_HEAD(, listen_ctx) *listen_hash;
313	u_long listen_mask;
314	int lctx_count;		/* # of lctx in the hash table */
315
316	struct ppod_region pr;
317
318	struct rwlock tcb_history_lock __aligned(CACHE_LINE_SIZE);
319	struct tcb_histent **tcb_history;
320	int dupack_threshold;
321
322	/* WRs that will not be sent to the chip because L2 resolution failed */
323	struct mtx unsent_wr_lock;
324	STAILQ_HEAD(, wrqe) unsent_wr_list;
325	struct task reclaim_wr_resources;
326};
327
328static inline struct tom_data *
329tod_td(struct toedev *tod)
330{
331
332	return (__containerof(tod, struct tom_data, tod));
333}
334
335static inline struct adapter *
336td_adapter(struct tom_data *td)
337{
338
339	return (td->tod.tod_softc);
340}
341
342static inline void
343set_mbuf_ulp_submode(struct mbuf *m, uint8_t ulp_submode)
344{
345
346	M_ASSERTPKTHDR(m);
347	m->m_pkthdr.PH_per.eight[0] = ulp_submode;
348}
349
350static inline uint8_t
351mbuf_ulp_submode(struct mbuf *m)
352{
353
354	M_ASSERTPKTHDR(m);
355	return (m->m_pkthdr.PH_per.eight[0]);
356}
357
358/* t4_tom.c */
359struct toepcb *alloc_toepcb(struct vi_info *, int);
360int init_toepcb(struct vi_info *, struct toepcb *);
361struct toepcb *hold_toepcb(struct toepcb *);
362void free_toepcb(struct toepcb *);
363void offload_socket(struct socket *, struct toepcb *);
364void undo_offload_socket(struct socket *);
365void final_cpl_received(struct toepcb *);
366void insert_tid(struct adapter *, int, void *, int);
367void *lookup_tid(struct adapter *, int);
368void update_tid(struct adapter *, int, void *);
369void remove_tid(struct adapter *, int, int);
370u_long select_rcv_wnd(struct socket *);
371int select_rcv_wscale(void);
372void init_conn_params(struct vi_info *, struct offload_settings *,
373    struct in_conninfo *, struct socket *, const struct tcp_options *, int16_t,
374    struct conn_params *cp);
375__be64 calc_options0(struct vi_info *, struct conn_params *);
376__be32 calc_options2(struct vi_info *, struct conn_params *);
377uint64_t select_ntuple(struct vi_info *, struct l2t_entry *);
378int negative_advice(int);
379int add_tid_to_history(struct adapter *, u_int);
380
381/* t4_connect.c */
382void t4_init_connect_cpl_handlers(void);
383void t4_uninit_connect_cpl_handlers(void);
384int t4_connect(struct toedev *, struct socket *, struct rtentry *,
385    struct sockaddr *);
386void act_open_failure_cleanup(struct adapter *, u_int, u_int);
387
388/* t4_listen.c */
389void t4_init_listen_cpl_handlers(void);
390void t4_uninit_listen_cpl_handlers(void);
391int t4_listen_start(struct toedev *, struct tcpcb *);
392int t4_listen_stop(struct toedev *, struct tcpcb *);
393void t4_syncache_added(struct toedev *, void *);
394void t4_syncache_removed(struct toedev *, void *);
395int t4_syncache_respond(struct toedev *, void *, struct mbuf *);
396int do_abort_req_synqe(struct sge_iq *, const struct rss_header *,
397    struct mbuf *);
398int do_abort_rpl_synqe(struct sge_iq *, const struct rss_header *,
399    struct mbuf *);
400void t4_offload_socket(struct toedev *, void *, struct socket *);
401void synack_failure_cleanup(struct adapter *, int);
402
403/* t4_cpl_io.c */
404void aiotx_init_toep(struct toepcb *);
405int t4_aio_queue_aiotx(struct socket *, struct kaiocb *);
406void t4_init_cpl_io_handlers(void);
407void t4_uninit_cpl_io_handlers(void);
408void send_abort_rpl(struct adapter *, struct sge_wrq *, int , int);
409void send_flowc_wr(struct toepcb *, struct tcpcb *);
410void send_reset(struct adapter *, struct toepcb *, uint32_t);
411int send_rx_credits(struct adapter *, struct toepcb *, int);
412void send_rx_modulate(struct adapter *, struct toepcb *);
413void make_established(struct toepcb *, uint32_t, uint32_t, uint16_t);
414int t4_close_conn(struct adapter *, struct toepcb *);
415void t4_rcvd(struct toedev *, struct tcpcb *);
416void t4_rcvd_locked(struct toedev *, struct tcpcb *);
417int t4_tod_output(struct toedev *, struct tcpcb *);
418int t4_send_fin(struct toedev *, struct tcpcb *);
419int t4_send_rst(struct toedev *, struct tcpcb *);
420void t4_set_tcb_field(struct adapter *, struct sge_wrq *, struct toepcb *,
421    uint16_t, uint64_t, uint64_t, int, int);
422void t4_push_frames(struct adapter *, struct toepcb *, int);
423void t4_push_pdus(struct adapter *, struct toepcb *, int);
424
425/* t4_ddp.c */
426int t4_init_ppod_region(struct ppod_region *, struct t4_range *, u_int,
427    const char *);
428void t4_free_ppod_region(struct ppod_region *);
429int t4_alloc_page_pods_for_ps(struct ppod_region *, struct pageset *);
430int t4_alloc_page_pods_for_buf(struct ppod_region *, vm_offset_t, int,
431    struct ppod_reservation *);
432int t4_write_page_pods_for_ps(struct adapter *, struct sge_wrq *, int,
433    struct pageset *);
434int t4_write_page_pods_for_buf(struct adapter *, struct sge_wrq *, int tid,
435    struct ppod_reservation *, vm_offset_t, int);
436void t4_free_page_pods(struct ppod_reservation *);
437int t4_soreceive_ddp(struct socket *, struct sockaddr **, struct uio *,
438    struct mbuf **, struct mbuf **, int *);
439int t4_aio_queue_ddp(struct socket *, struct kaiocb *);
440void t4_ddp_mod_load(void);
441void t4_ddp_mod_unload(void);
442void ddp_assert_empty(struct toepcb *);
443void ddp_init_toep(struct toepcb *);
444void ddp_uninit_toep(struct toepcb *);
445void ddp_queue_toep(struct toepcb *);
446void release_ddp_resources(struct toepcb *toep);
447void handle_ddp_close(struct toepcb *, struct tcpcb *, uint32_t);
448void handle_ddp_indicate(struct toepcb *);
449void insert_ddp_data(struct toepcb *, uint32_t);
450const struct offload_settings *lookup_offload_policy(struct adapter *, int,
451    struct mbuf *, uint16_t, struct inpcb *);
452
453/* t4_tls.c */
454bool can_tls_offload(struct adapter *);
455void do_rx_data_tls(const struct cpl_rx_data *, struct toepcb *, struct mbuf *);
456int t4_ctloutput_tls(struct socket *, struct sockopt *);
457void t4_push_tls_records(struct adapter *, struct toepcb *, int);
458void t4_tls_mod_load(void);
459void t4_tls_mod_unload(void);
460void tls_establish(struct toepcb *);
461void tls_init_toep(struct toepcb *);
462int tls_rx_key(struct toepcb *);
463void tls_stop_handshake_timer(struct toepcb *);
464int tls_tx_key(struct toepcb *);
465void tls_uninit_toep(struct toepcb *);
466
467#endif
468