xref: /freebsd/sys/dev/cxgbe/tom/t4_tom.h (revision 10ff414c)
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 */
63 enum {
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_KTLS           = (1 << 11), /* send TLS records from KTLS */
76 	TPF_INITIALIZED    = (1 << 12), /* init_toepcb has been called */
77 	TPF_TLS_RECEIVE	   = (1 << 13), /* should receive TLS records */
78 	TPF_TLS_ESTABLISHED = (1 << 14), /* TLS handshake timer initialized */
79 	TPF_WAITING_FOR_FINAL = (1<< 15), /* waiting for wakeup on final CPL */
80 };
81 
82 enum {
83 	DDP_OK		= (1 << 0),	/* OK to turn on DDP */
84 	DDP_SC_REQ	= (1 << 1),	/* state change (on/off) requested */
85 	DDP_ON		= (1 << 2),	/* DDP is turned on */
86 	DDP_BUF0_ACTIVE	= (1 << 3),	/* buffer 0 in use (not invalidated) */
87 	DDP_BUF1_ACTIVE	= (1 << 4),	/* buffer 1 in use (not invalidated) */
88 	DDP_TASK_ACTIVE = (1 << 5),	/* requeue task is queued / running */
89 	DDP_DEAD	= (1 << 6),	/* toepcb is shutting down */
90 };
91 
92 struct ctl_sg_entry;
93 struct sockopt;
94 struct offload_settings;
95 
96 /*
97  * Connection parameters for an offloaded connection.  These are mostly (but not
98  * all) hardware TOE parameters.
99  */
100 struct conn_params {
101 	int8_t rx_coalesce;
102 	int8_t cong_algo;
103 	int8_t tc_idx;
104 	int8_t tstamp;
105 	int8_t sack;
106 	int8_t nagle;
107 	int8_t keepalive;
108 	int8_t wscale;
109 	int8_t ecn;
110 	int8_t mtu_idx;
111 	int8_t ulp_mode;
112 	int8_t tx_align;
113 	int16_t txq_idx;	/* ofld_txq = &sc->sge.ofld_txq[txq_idx] */
114 	int16_t rxq_idx;	/* ofld_rxq = &sc->sge.ofld_rxq[rxq_idx] */
115 	int16_t l2t_idx;
116 	uint16_t emss;
117 	uint16_t opt0_bufsize;
118 	u_int sndbuf;		/* controls TP tx pages */
119 };
120 
121 struct ofld_tx_sdesc {
122 	uint32_t plen;		/* payload length */
123 	uint8_t tx_credits;	/* firmware tx credits (unit is 16B) */
124 };
125 
126 struct ppod_region {
127 	u_int pr_start;
128 	u_int pr_len;
129 	u_int pr_page_shift[4];
130 	uint32_t pr_tag_mask;		/* hardware tagmask for this region. */
131 	uint32_t pr_invalid_bit;	/* OR with this to invalidate tag. */
132 	uint32_t pr_alias_mask;		/* AND with tag to get alias bits. */
133 	u_int pr_alias_shift;		/* shift this much for first alias bit. */
134 	vmem_t *pr_arena;
135 };
136 
137 struct ppod_reservation {
138 	struct ppod_region *prsv_pr;
139 	uint32_t prsv_tag;		/* Full tag: pgsz, alias, tag, color */
140 	u_int prsv_nppods;
141 };
142 
143 struct pageset {
144 	TAILQ_ENTRY(pageset) link;
145 	vm_page_t *pages;
146 	int npages;
147 	int flags;
148 	int offset;		/* offset in first page */
149 	int len;
150 	struct ppod_reservation prsv;
151 	struct vmspace *vm;
152 	vm_offset_t start;
153 	u_int vm_timestamp;
154 };
155 
156 TAILQ_HEAD(pagesetq, pageset);
157 
158 #define	PS_PPODS_WRITTEN	0x0001	/* Page pods written to the card. */
159 
160 struct ddp_buffer {
161 	struct pageset *ps;
162 
163 	struct kaiocb *job;
164 	int cancel_pending;
165 };
166 
167 struct ddp_pcb {
168 	u_int flags;
169 	struct ddp_buffer db[2];
170 	TAILQ_HEAD(, pageset) cached_pagesets;
171 	TAILQ_HEAD(, kaiocb) aiojobq;
172 	u_int waiting_count;
173 	u_int active_count;
174 	u_int cached_count;
175 	int active_id;	/* the currently active DDP buffer */
176 	struct task requeue_task;
177 	struct kaiocb *queueing;
178 	struct mtx lock;
179 };
180 
181 struct toepcb {
182 	struct tom_data *td;
183 	struct inpcb *inp;	/* backpointer to host stack's PCB */
184 	u_int flags;		/* miscellaneous flags */
185 	TAILQ_ENTRY(toepcb) link; /* toep_list */
186 	int refcount;
187 	struct vnet *vnet;
188 	struct vi_info *vi;	/* virtual interface */
189 	struct sge_ofld_txq *ofld_txq;
190 	struct sge_ofld_rxq *ofld_rxq;
191 	struct sge_wrq *ctrlq;
192 	struct l2t_entry *l2te;	/* L2 table entry used by this connection */
193 	struct clip_entry *ce;	/* CLIP table entry used by this tid */
194 	int tid;		/* Connection identifier */
195 
196 	/* tx credit handling */
197 	u_int tx_total;		/* total tx WR credits (in 16B units) */
198 	u_int tx_credits;	/* tx WR credits (in 16B units) available */
199 	u_int tx_nocompl;	/* tx WR credits since last compl request */
200 	u_int plen_nocompl;	/* payload since last compl request */
201 
202 	struct conn_params params;
203 
204 	void *ulpcb;
205 	void *ulpcb2;
206 	struct mbufq ulp_pduq;	/* PDUs waiting to be sent out. */
207 	struct mbufq ulp_pdu_reclaimq;
208 
209 	struct ddp_pcb ddp;
210 	struct tls_ofld_info tls;
211 
212 	TAILQ_HEAD(, kaiocb) aiotx_jobq;
213 	struct task aiotx_task;
214 	struct socket *aiotx_so;
215 
216 	/* Tx software descriptor */
217 	uint8_t txsd_total;
218 	uint8_t txsd_pidx;
219 	uint8_t txsd_cidx;
220 	uint8_t txsd_avail;
221 	struct ofld_tx_sdesc txsd[];
222 };
223 
224 static inline int
225 ulp_mode(struct toepcb *toep)
226 {
227 
228 	return (toep->params.ulp_mode);
229 }
230 
231 #define	DDP_LOCK(toep)		mtx_lock(&(toep)->ddp.lock)
232 #define	DDP_UNLOCK(toep)	mtx_unlock(&(toep)->ddp.lock)
233 #define	DDP_ASSERT_LOCKED(toep)	mtx_assert(&(toep)->ddp.lock, MA_OWNED)
234 
235 /*
236  * Compressed state for embryonic connections for a listener.
237  */
238 struct synq_entry {
239 	struct listen_ctx *lctx;	/* backpointer to listen ctx */
240 	struct mbuf *syn;
241 	int flags;			/* same as toepcb's tp_flags */
242 	volatile int ok_to_respond;
243 	volatile u_int refcnt;
244 	int tid;
245 	uint32_t iss;
246 	uint32_t irs;
247 	uint32_t ts;
248 	uint32_t rss_hash;
249 	__be16 tcp_opt; /* from cpl_pass_establish */
250 	struct toepcb *toep;
251 
252 	struct conn_params params;
253 };
254 
255 /* listen_ctx flags */
256 #define LCTX_RPL_PENDING 1	/* waiting for a CPL_PASS_OPEN_RPL */
257 
258 struct listen_ctx {
259 	LIST_ENTRY(listen_ctx) link;	/* listen hash linkage */
260 	volatile int refcount;
261 	int stid;
262 	struct stid_region stid_region;
263 	int flags;
264 	struct inpcb *inp;		/* listening socket's inp */
265 	struct vnet *vnet;
266 	struct sge_wrq *ctrlq;
267 	struct sge_ofld_rxq *ofld_rxq;
268 	struct clip_entry *ce;
269 };
270 
271 /* tcb_histent flags */
272 #define TE_RPL_PENDING	1
273 #define TE_ACTIVE	2
274 
275 /* bits in one 8b tcb_histent sample. */
276 #define TS_RTO			(1 << 0)
277 #define TS_DUPACKS		(1 << 1)
278 #define TS_FASTREXMT		(1 << 2)
279 #define TS_SND_BACKLOGGED	(1 << 3)
280 #define TS_CWND_LIMITED		(1 << 4)
281 #define TS_ECN_ECE		(1 << 5)
282 #define TS_ECN_CWR		(1 << 6)
283 #define TS_RESERVED		(1 << 7)	/* Unused. */
284 
285 struct tcb_histent {
286 	struct mtx te_lock;
287 	struct callout te_callout;
288 	uint64_t te_tcb[TCB_SIZE / sizeof(uint64_t)];
289 	struct adapter *te_adapter;
290 	u_int te_flags;
291 	u_int te_tid;
292 	uint8_t te_pidx;
293 	uint8_t te_sample[100];
294 };
295 
296 struct tom_data {
297 	struct toedev tod;
298 
299 	/* toepcb's associated with this TOE device */
300 	struct mtx toep_list_lock;
301 	TAILQ_HEAD(, toepcb) toep_list;
302 
303 	struct mtx lctx_hash_lock;
304 	LIST_HEAD(, listen_ctx) *listen_hash;
305 	u_long listen_mask;
306 	int lctx_count;		/* # of lctx in the hash table */
307 
308 	struct ppod_region pr;
309 
310 	struct rwlock tcb_history_lock __aligned(CACHE_LINE_SIZE);
311 	struct tcb_histent **tcb_history;
312 	int dupack_threshold;
313 
314 	/* WRs that will not be sent to the chip because L2 resolution failed */
315 	struct mtx unsent_wr_lock;
316 	STAILQ_HEAD(, wrqe) unsent_wr_list;
317 	struct task reclaim_wr_resources;
318 };
319 
320 static inline struct tom_data *
321 tod_td(struct toedev *tod)
322 {
323 
324 	return (__containerof(tod, struct tom_data, tod));
325 }
326 
327 static inline struct adapter *
328 td_adapter(struct tom_data *td)
329 {
330 
331 	return (td->tod.tod_softc);
332 }
333 
334 static inline void
335 set_mbuf_raw_wr(struct mbuf *m, bool raw)
336 {
337 
338 	M_ASSERTPKTHDR(m);
339 	m->m_pkthdr.PH_per.eight[6] = raw;
340 }
341 
342 static inline bool
343 mbuf_raw_wr(struct mbuf *m)
344 {
345 
346 	M_ASSERTPKTHDR(m);
347 	return (m->m_pkthdr.PH_per.eight[6]);
348 }
349 
350 static inline void
351 set_mbuf_ulp_submode(struct mbuf *m, uint8_t ulp_submode)
352 {
353 
354 	M_ASSERTPKTHDR(m);
355 	m->m_pkthdr.PH_per.eight[0] = ulp_submode;
356 }
357 
358 static inline uint8_t
359 mbuf_ulp_submode(struct mbuf *m)
360 {
361 
362 	M_ASSERTPKTHDR(m);
363 	return (m->m_pkthdr.PH_per.eight[0]);
364 }
365 
366 static inline void
367 set_mbuf_iscsi_iso(struct mbuf *m, bool iso)
368 {
369 
370 	M_ASSERTPKTHDR(m);
371 	m->m_pkthdr.PH_per.eight[1] = iso;
372 }
373 
374 static inline bool
375 mbuf_iscsi_iso(struct mbuf *m)
376 {
377 
378 	M_ASSERTPKTHDR(m);
379 	return (m->m_pkthdr.PH_per.eight[1]);
380 }
381 
382 /* Flags for iSCSI segmentation offload. */
383 #define	CXGBE_ISO_TYPE(flags)	((flags) & 0x3)
384 #define	CXGBE_ISO_F		0x4
385 
386 static inline void
387 set_mbuf_iscsi_iso_flags(struct mbuf *m, uint8_t flags)
388 {
389 
390 	M_ASSERTPKTHDR(m);
391 	m->m_pkthdr.PH_per.eight[2] = flags;
392 }
393 
394 static inline uint8_t
395 mbuf_iscsi_iso_flags(struct mbuf *m)
396 {
397 
398 	M_ASSERTPKTHDR(m);
399 	return (m->m_pkthdr.PH_per.eight[2]);
400 }
401 
402 static inline void
403 set_mbuf_iscsi_iso_mss(struct mbuf *m, uint16_t mss)
404 {
405 
406 	M_ASSERTPKTHDR(m);
407 	m->m_pkthdr.PH_per.sixteen[2] = mss;
408 }
409 
410 static inline uint16_t
411 mbuf_iscsi_iso_mss(struct mbuf *m)
412 {
413 
414 	M_ASSERTPKTHDR(m);
415 	return (m->m_pkthdr.PH_per.sixteen[2]);
416 }
417 
418 /* t4_tom.c */
419 struct toepcb *alloc_toepcb(struct vi_info *, int);
420 int init_toepcb(struct vi_info *, struct toepcb *);
421 struct toepcb *hold_toepcb(struct toepcb *);
422 void free_toepcb(struct toepcb *);
423 void offload_socket(struct socket *, struct toepcb *);
424 void restore_so_proto(struct socket *, bool);
425 void undo_offload_socket(struct socket *);
426 void final_cpl_received(struct toepcb *);
427 void insert_tid(struct adapter *, int, void *, int);
428 void *lookup_tid(struct adapter *, int);
429 void update_tid(struct adapter *, int, void *);
430 void remove_tid(struct adapter *, int, int);
431 u_long select_rcv_wnd(struct socket *);
432 int select_rcv_wscale(void);
433 void init_conn_params(struct vi_info *, struct offload_settings *,
434     struct in_conninfo *, struct socket *, const struct tcp_options *, int16_t,
435     struct conn_params *cp);
436 __be64 calc_options0(struct vi_info *, struct conn_params *);
437 __be32 calc_options2(struct vi_info *, struct conn_params *);
438 uint64_t select_ntuple(struct vi_info *, struct l2t_entry *);
439 int negative_advice(int);
440 int add_tid_to_history(struct adapter *, u_int);
441 
442 /* t4_connect.c */
443 void t4_init_connect_cpl_handlers(void);
444 void t4_uninit_connect_cpl_handlers(void);
445 int t4_connect(struct toedev *, struct socket *, struct nhop_object *,
446     struct sockaddr *);
447 void act_open_failure_cleanup(struct adapter *, u_int, u_int);
448 
449 /* t4_listen.c */
450 void t4_init_listen_cpl_handlers(void);
451 void t4_uninit_listen_cpl_handlers(void);
452 int t4_listen_start(struct toedev *, struct tcpcb *);
453 int t4_listen_stop(struct toedev *, struct tcpcb *);
454 void t4_syncache_added(struct toedev *, void *);
455 void t4_syncache_removed(struct toedev *, void *);
456 int t4_syncache_respond(struct toedev *, void *, struct mbuf *);
457 int do_abort_req_synqe(struct sge_iq *, const struct rss_header *,
458     struct mbuf *);
459 int do_abort_rpl_synqe(struct sge_iq *, const struct rss_header *,
460     struct mbuf *);
461 void t4_offload_socket(struct toedev *, void *, struct socket *);
462 void synack_failure_cleanup(struct adapter *, int);
463 
464 /* t4_cpl_io.c */
465 void aiotx_init_toep(struct toepcb *);
466 int t4_aio_queue_aiotx(struct socket *, struct kaiocb *);
467 void t4_init_cpl_io_handlers(void);
468 void t4_uninit_cpl_io_handlers(void);
469 void send_abort_rpl(struct adapter *, struct sge_ofld_txq *, int , int);
470 void send_flowc_wr(struct toepcb *, struct tcpcb *);
471 void send_reset(struct adapter *, struct toepcb *, uint32_t);
472 int send_rx_credits(struct adapter *, struct toepcb *, int);
473 void send_rx_modulate(struct adapter *, struct toepcb *);
474 void make_established(struct toepcb *, uint32_t, uint32_t, uint16_t);
475 int t4_close_conn(struct adapter *, struct toepcb *);
476 void t4_rcvd(struct toedev *, struct tcpcb *);
477 void t4_rcvd_locked(struct toedev *, struct tcpcb *);
478 int t4_tod_output(struct toedev *, struct tcpcb *);
479 int t4_send_fin(struct toedev *, struct tcpcb *);
480 int t4_send_rst(struct toedev *, struct tcpcb *);
481 void t4_set_tcb_field(struct adapter *, struct sge_wrq *, struct toepcb *,
482     uint16_t, uint64_t, uint64_t, int, int);
483 void t4_push_frames(struct adapter *, struct toepcb *, int);
484 void t4_push_pdus(struct adapter *, struct toepcb *, int);
485 
486 /* t4_ddp.c */
487 int t4_init_ppod_region(struct ppod_region *, struct t4_range *, u_int,
488     const char *);
489 void t4_free_ppod_region(struct ppod_region *);
490 int t4_alloc_page_pods_for_ps(struct ppod_region *, struct pageset *);
491 int t4_alloc_page_pods_for_buf(struct ppod_region *, vm_offset_t, int,
492     struct ppod_reservation *);
493 int t4_alloc_page_pods_for_sgl(struct ppod_region *, struct ctl_sg_entry *, int,
494     struct ppod_reservation *);
495 int t4_write_page_pods_for_ps(struct adapter *, struct sge_wrq *, int,
496     struct pageset *);
497 int t4_write_page_pods_for_buf(struct adapter *, struct toepcb *,
498     struct ppod_reservation *, vm_offset_t, int, struct mbufq *);
499 int t4_write_page_pods_for_sgl(struct adapter *, struct toepcb *,
500     struct ppod_reservation *, struct ctl_sg_entry *, int, int, struct mbufq *);
501 void t4_free_page_pods(struct ppod_reservation *);
502 int t4_soreceive_ddp(struct socket *, struct sockaddr **, struct uio *,
503     struct mbuf **, struct mbuf **, int *);
504 int t4_aio_queue_ddp(struct socket *, struct kaiocb *);
505 void t4_ddp_mod_load(void);
506 void t4_ddp_mod_unload(void);
507 void ddp_assert_empty(struct toepcb *);
508 void ddp_init_toep(struct toepcb *);
509 void ddp_uninit_toep(struct toepcb *);
510 void ddp_queue_toep(struct toepcb *);
511 void release_ddp_resources(struct toepcb *toep);
512 void handle_ddp_close(struct toepcb *, struct tcpcb *, uint32_t);
513 void handle_ddp_indicate(struct toepcb *);
514 void insert_ddp_data(struct toepcb *, uint32_t);
515 const struct offload_settings *lookup_offload_policy(struct adapter *, int,
516     struct mbuf *, uint16_t, struct inpcb *);
517 
518 /* t4_tls.c */
519 bool can_tls_offload(struct adapter *);
520 void do_rx_data_tls(const struct cpl_rx_data *, struct toepcb *, struct mbuf *);
521 void t4_push_ktls(struct adapter *, struct toepcb *, int);
522 void t4_tls_mod_load(void);
523 void t4_tls_mod_unload(void);
524 void tls_detach(struct toepcb *);
525 void tls_establish(struct toepcb *);
526 void tls_init_toep(struct toepcb *);
527 void tls_stop_handshake_timer(struct toepcb *);
528 int tls_tx_key(struct toepcb *);
529 void tls_uninit_toep(struct toepcb *);
530 int tls_alloc_ktls(struct toepcb *, struct ktls_session *, int);
531 
532 #endif
533