1 /*
2  * This file is part of the Chelsio T4 Ethernet driver for Linux.
3  *
4  * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #ifndef __CXGB4_ULD_H
36 #define __CXGB4_ULD_H
37 
38 #include <linux/cache.h>
39 #include <linux/spinlock.h>
40 #include <linux/skbuff.h>
41 #include <linux/inetdevice.h>
42 #include <linux/atomic.h>
43 #include <net/tls.h>
44 #include "cxgb4.h"
45 
46 #define MAX_ULD_QSETS 16
47 #define MAX_ULD_NPORTS 4
48 
49 /* CPL message priority levels */
50 enum {
51 	CPL_PRIORITY_DATA     = 0,  /* data messages */
52 	CPL_PRIORITY_SETUP    = 1,  /* connection setup messages */
53 	CPL_PRIORITY_TEARDOWN = 0,  /* connection teardown messages */
54 	CPL_PRIORITY_LISTEN   = 1,  /* listen start/stop messages */
55 	CPL_PRIORITY_ACK      = 1,  /* RX ACK messages */
56 	CPL_PRIORITY_CONTROL  = 1   /* control messages */
57 };
58 
59 #define INIT_TP_WR(w, tid) do { \
60 	(w)->wr.wr_hi = htonl(FW_WR_OP_V(FW_TP_WR) | \
61 			      FW_WR_IMMDLEN_V(sizeof(*w) - sizeof(w->wr))); \
62 	(w)->wr.wr_mid = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*w), 16)) | \
63 			       FW_WR_FLOWID_V(tid)); \
64 	(w)->wr.wr_lo = cpu_to_be64(0); \
65 } while (0)
66 
67 #define INIT_TP_WR_CPL(w, cpl, tid) do { \
68 	INIT_TP_WR(w, tid); \
69 	OPCODE_TID(w) = htonl(MK_OPCODE_TID(cpl, tid)); \
70 } while (0)
71 
72 #define INIT_ULPTX_WR(w, wrlen, atomic, tid) do { \
73 	(w)->wr.wr_hi = htonl(FW_WR_OP_V(FW_ULPTX_WR) | \
74 			      FW_WR_ATOMIC_V(atomic)); \
75 	(w)->wr.wr_mid = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(wrlen, 16)) | \
76 			       FW_WR_FLOWID_V(tid)); \
77 	(w)->wr.wr_lo = cpu_to_be64(0); \
78 } while (0)
79 
80 /* Special asynchronous notification message */
81 #define CXGB4_MSG_AN ((void *)1)
82 #define TX_ULD(uld)(((uld) != CXGB4_ULD_CRYPTO) ? CXGB4_TX_OFLD :\
83 		      CXGB4_TX_CRYPTO)
84 
85 struct serv_entry {
86 	void *data;
87 };
88 
89 union aopen_entry {
90 	void *data;
91 	union aopen_entry *next;
92 };
93 
94 struct eotid_entry {
95 	void *data;
96 };
97 
98 /*
99  * Holds the size, base address, free list start, etc of the TID, server TID,
100  * and active-open TID tables.  The tables themselves are allocated dynamically.
101  */
102 struct tid_info {
103 	void **tid_tab;
104 	unsigned int tid_base;
105 	unsigned int ntids;
106 
107 	struct serv_entry *stid_tab;
108 	unsigned long *stid_bmap;
109 	unsigned int nstids;
110 	unsigned int stid_base;
111 
112 	unsigned int nhash;
113 	unsigned int hash_base;
114 
115 	union aopen_entry *atid_tab;
116 	unsigned int natids;
117 	unsigned int atid_base;
118 
119 	struct filter_entry *hpftid_tab;
120 	unsigned long *hpftid_bmap;
121 	unsigned int nhpftids;
122 	unsigned int hpftid_base;
123 
124 	struct filter_entry *ftid_tab;
125 	unsigned long *ftid_bmap;
126 	unsigned int nftids;
127 	unsigned int ftid_base;
128 	unsigned int aftid_base;
129 	unsigned int aftid_end;
130 	/* Server filter region */
131 	unsigned int sftid_base;
132 	unsigned int nsftids;
133 
134 	spinlock_t atid_lock ____cacheline_aligned_in_smp;
135 	union aopen_entry *afree;
136 	unsigned int atids_in_use;
137 
138 	spinlock_t stid_lock;
139 	unsigned int stids_in_use;
140 	unsigned int v6_stids_in_use;
141 	unsigned int sftids_in_use;
142 
143 	/* ETHOFLD range */
144 	struct eotid_entry *eotid_tab;
145 	unsigned long *eotid_bmap;
146 	unsigned int eotid_base;
147 	unsigned int neotids;
148 
149 	/* TIDs in the TCAM */
150 	atomic_t tids_in_use;
151 	/* TIDs in the HASH */
152 	atomic_t hash_tids_in_use;
153 	atomic_t conns_in_use;
154 	/* ETHOFLD TIDs used for rate limiting */
155 	atomic_t eotids_in_use;
156 
157 	/* lock for setting/clearing filter bitmap */
158 	spinlock_t ftid_lock;
159 
160 	unsigned int tc_hash_tids_max_prio;
161 };
162 
163 static inline void *lookup_tid(const struct tid_info *t, unsigned int tid)
164 {
165 	tid -= t->tid_base;
166 	return tid < t->ntids ? t->tid_tab[tid] : NULL;
167 }
168 
169 static inline bool tid_out_of_range(const struct tid_info *t, unsigned int tid)
170 {
171 	return ((tid - t->tid_base) >= t->ntids);
172 }
173 
174 static inline void *lookup_atid(const struct tid_info *t, unsigned int atid)
175 {
176 	return atid < t->natids ? t->atid_tab[atid].data : NULL;
177 }
178 
179 static inline void *lookup_stid(const struct tid_info *t, unsigned int stid)
180 {
181 	/* Is it a server filter TID? */
182 	if (t->nsftids && (stid >= t->sftid_base)) {
183 		stid -= t->sftid_base;
184 		stid += t->nstids;
185 	} else {
186 		stid -= t->stid_base;
187 	}
188 
189 	return stid < (t->nstids + t->nsftids) ? t->stid_tab[stid].data : NULL;
190 }
191 
192 static inline void cxgb4_insert_tid(struct tid_info *t, void *data,
193 				    unsigned int tid, unsigned short family)
194 {
195 	t->tid_tab[tid - t->tid_base] = data;
196 	if (t->hash_base && (tid >= t->hash_base)) {
197 		if (family == AF_INET6)
198 			atomic_add(2, &t->hash_tids_in_use);
199 		else
200 			atomic_inc(&t->hash_tids_in_use);
201 	} else {
202 		if (family == AF_INET6)
203 			atomic_add(2, &t->tids_in_use);
204 		else
205 			atomic_inc(&t->tids_in_use);
206 	}
207 	atomic_inc(&t->conns_in_use);
208 }
209 
210 static inline struct eotid_entry *cxgb4_lookup_eotid(struct tid_info *t,
211 						     u32 eotid)
212 {
213 	return eotid < t->neotids ? &t->eotid_tab[eotid] : NULL;
214 }
215 
216 static inline int cxgb4_get_free_eotid(struct tid_info *t)
217 {
218 	int eotid;
219 
220 	eotid = find_first_zero_bit(t->eotid_bmap, t->neotids);
221 	if (eotid >= t->neotids)
222 		eotid = -1;
223 
224 	return eotid;
225 }
226 
227 static inline void cxgb4_alloc_eotid(struct tid_info *t, u32 eotid, void *data)
228 {
229 	set_bit(eotid, t->eotid_bmap);
230 	t->eotid_tab[eotid].data = data;
231 	atomic_inc(&t->eotids_in_use);
232 }
233 
234 static inline void cxgb4_free_eotid(struct tid_info *t, u32 eotid)
235 {
236 	clear_bit(eotid, t->eotid_bmap);
237 	t->eotid_tab[eotid].data = NULL;
238 	atomic_dec(&t->eotids_in_use);
239 }
240 
241 int cxgb4_alloc_atid(struct tid_info *t, void *data);
242 int cxgb4_alloc_stid(struct tid_info *t, int family, void *data);
243 int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data);
244 void cxgb4_free_atid(struct tid_info *t, unsigned int atid);
245 void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family);
246 void cxgb4_remove_tid(struct tid_info *t, unsigned int qid, unsigned int tid,
247 		      unsigned short family);
248 struct in6_addr;
249 
250 int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
251 			__be32 sip, __be16 sport, __be16 vlan,
252 			unsigned int queue);
253 int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
254 			 const struct in6_addr *sip, __be16 sport,
255 			 unsigned int queue);
256 int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
257 			unsigned int queue, bool ipv6);
258 int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
259 			       __be32 sip, __be16 sport, __be16 vlan,
260 			       unsigned int queue,
261 			       unsigned char port, unsigned char mask);
262 int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
263 			       unsigned int queue, bool ipv6);
264 
265 /* Filter operation context to allow callers of cxgb4_set_filter() and
266  * cxgb4_del_filter() to wait for an asynchronous completion.
267  */
268 struct filter_ctx {
269 	struct completion completion;	/* completion rendezvous */
270 	void *closure;			/* caller's opaque information */
271 	int result;			/* result of operation */
272 	u32 tid;			/* to store tid */
273 };
274 
275 struct chcr_ktls {
276 	refcount_t ktls_refcount;
277 };
278 
279 struct ch_filter_specification;
280 
281 int cxgb4_get_free_ftid(struct net_device *dev, u8 family, bool hash_en,
282 			u32 tc_prio);
283 int __cxgb4_set_filter(struct net_device *dev, int filter_id,
284 		       struct ch_filter_specification *fs,
285 		       struct filter_ctx *ctx);
286 int __cxgb4_del_filter(struct net_device *dev, int filter_id,
287 		       struct ch_filter_specification *fs,
288 		       struct filter_ctx *ctx);
289 int cxgb4_set_filter(struct net_device *dev, int filter_id,
290 		     struct ch_filter_specification *fs);
291 int cxgb4_del_filter(struct net_device *dev, int filter_id,
292 		     struct ch_filter_specification *fs);
293 int cxgb4_get_filter_counters(struct net_device *dev, unsigned int fidx,
294 			      u64 *hitcnt, u64 *bytecnt, bool hash);
295 
296 static inline void set_wr_txq(struct sk_buff *skb, int prio, int queue)
297 {
298 	skb_set_queue_mapping(skb, (queue << 1) | prio);
299 }
300 
301 enum cxgb4_uld {
302 	CXGB4_ULD_INIT,
303 	CXGB4_ULD_RDMA,
304 	CXGB4_ULD_ISCSI,
305 	CXGB4_ULD_ISCSIT,
306 	CXGB4_ULD_CRYPTO,
307 	CXGB4_ULD_IPSEC,
308 	CXGB4_ULD_TLS,
309 	CXGB4_ULD_KTLS,
310 	CXGB4_ULD_MAX
311 };
312 
313 enum cxgb4_tx_uld {
314 	CXGB4_TX_OFLD,
315 	CXGB4_TX_CRYPTO,
316 	CXGB4_TX_MAX
317 };
318 
319 enum cxgb4_txq_type {
320 	CXGB4_TXQ_ETH,
321 	CXGB4_TXQ_ULD,
322 	CXGB4_TXQ_CTRL,
323 	CXGB4_TXQ_MAX
324 };
325 
326 enum cxgb4_state {
327 	CXGB4_STATE_UP,
328 	CXGB4_STATE_START_RECOVERY,
329 	CXGB4_STATE_DOWN,
330 	CXGB4_STATE_DETACH,
331 	CXGB4_STATE_FATAL_ERROR
332 };
333 
334 enum cxgb4_control {
335 	CXGB4_CONTROL_DB_FULL,
336 	CXGB4_CONTROL_DB_EMPTY,
337 	CXGB4_CONTROL_DB_DROP,
338 };
339 
340 struct adapter;
341 struct pci_dev;
342 struct l2t_data;
343 struct net_device;
344 struct pkt_gl;
345 struct tp_tcp_stats;
346 struct t4_lro_mgr;
347 
348 struct cxgb4_range {
349 	unsigned int start;
350 	unsigned int size;
351 };
352 
353 struct cxgb4_virt_res {                      /* virtualized HW resources */
354 	struct cxgb4_range ddp;
355 	struct cxgb4_range iscsi;
356 	struct cxgb4_range stag;
357 	struct cxgb4_range rq;
358 	struct cxgb4_range srq;
359 	struct cxgb4_range pbl;
360 	struct cxgb4_range qp;
361 	struct cxgb4_range cq;
362 	struct cxgb4_range ocq;
363 	struct cxgb4_range key;
364 	unsigned int ncrypto_fc;
365 	struct cxgb4_range ppod_edram;
366 };
367 
368 #if IS_ENABLED(CONFIG_CHELSIO_TLS_DEVICE)
369 struct ch_ktls_port_stats_debug {
370 	atomic64_t ktls_tx_connection_open;
371 	atomic64_t ktls_tx_connection_fail;
372 	atomic64_t ktls_tx_connection_close;
373 	atomic64_t ktls_tx_encrypted_packets;
374 	atomic64_t ktls_tx_encrypted_bytes;
375 	atomic64_t ktls_tx_ctx;
376 	atomic64_t ktls_tx_ooo;
377 	atomic64_t ktls_tx_skip_no_sync_data;
378 	atomic64_t ktls_tx_drop_no_sync_data;
379 	atomic64_t ktls_tx_drop_bypass_req;
380 };
381 
382 struct ch_ktls_stats_debug {
383 	struct ch_ktls_port_stats_debug ktls_port[MAX_ULD_NPORTS];
384 	atomic64_t ktls_tx_send_records;
385 	atomic64_t ktls_tx_end_pkts;
386 	atomic64_t ktls_tx_start_pkts;
387 	atomic64_t ktls_tx_middle_pkts;
388 	atomic64_t ktls_tx_retransmit_pkts;
389 	atomic64_t ktls_tx_complete_pkts;
390 	atomic64_t ktls_tx_trimmed_pkts;
391 };
392 #endif
393 
394 struct chcr_stats_debug {
395 	atomic_t cipher_rqst;
396 	atomic_t digest_rqst;
397 	atomic_t aead_rqst;
398 	atomic_t complete;
399 	atomic_t error;
400 	atomic_t fallback;
401 	atomic_t tls_pdu_tx;
402 	atomic_t tls_pdu_rx;
403 	atomic_t tls_key;
404 };
405 
406 #if IS_ENABLED(CONFIG_CHELSIO_IPSEC_INLINE)
407 struct ch_ipsec_stats_debug {
408 	atomic_t ipsec_cnt;
409 };
410 #endif
411 
412 #define OCQ_WIN_OFFSET(pdev, vres) \
413 	(pci_resource_len((pdev), 2) - roundup_pow_of_two((vres)->ocq.size))
414 
415 /*
416  * Block of information the LLD provides to ULDs attaching to a device.
417  */
418 struct cxgb4_lld_info {
419 	struct pci_dev *pdev;                /* associated PCI device */
420 	struct l2t_data *l2t;                /* L2 table */
421 	struct tid_info *tids;               /* TID table */
422 	struct net_device **ports;           /* device ports */
423 	const struct cxgb4_virt_res *vr;     /* assorted HW resources */
424 	const unsigned short *mtus;          /* MTU table */
425 	const unsigned short *rxq_ids;       /* the ULD's Rx queue ids */
426 	const unsigned short *ciq_ids;       /* the ULD's concentrator IQ ids */
427 	unsigned short nrxq;                 /* # of Rx queues */
428 	unsigned short ntxq;                 /* # of Tx queues */
429 	unsigned short nciq;		     /* # of concentrator IQ */
430 	unsigned char nchan:4;               /* # of channels */
431 	unsigned char nports:4;              /* # of ports */
432 	unsigned char wr_cred;               /* WR 16-byte credits */
433 	unsigned char adapter_type;          /* type of adapter */
434 	unsigned char fw_api_ver;            /* FW API version */
435 	unsigned char crypto;                /* crypto support */
436 	unsigned int fw_vers;                /* FW version */
437 	unsigned int iscsi_iolen;            /* iSCSI max I/O length */
438 	unsigned int cclk_ps;                /* Core clock period in psec */
439 	unsigned short udb_density;          /* # of user DB/page */
440 	unsigned short ucq_density;          /* # of user CQs/page */
441 	unsigned int sge_host_page_size;     /* SGE host page size */
442 	unsigned short filt_mode;            /* filter optional components */
443 	unsigned short tx_modq[NCHAN];       /* maps each tx channel to a */
444 					     /* scheduler queue */
445 	void __iomem *gts_reg;               /* address of GTS register */
446 	void __iomem *db_reg;                /* address of kernel doorbell */
447 	int dbfifo_int_thresh;		     /* doorbell fifo int threshold */
448 	unsigned int sge_ingpadboundary;     /* SGE ingress padding boundary */
449 	unsigned int sge_egrstatuspagesize;  /* SGE egress status page size */
450 	unsigned int sge_pktshift;           /* Padding between CPL and */
451 					     /*	packet data */
452 	unsigned int pf;		     /* Physical Function we're using */
453 	bool enable_fw_ofld_conn;            /* Enable connection through fw */
454 					     /* WR */
455 	unsigned int max_ordird_qp;          /* Max ORD/IRD depth per RDMA QP */
456 	unsigned int max_ird_adapter;        /* Max IRD memory per adapter */
457 	bool ulptx_memwrite_dsgl;            /* use of T5 DSGL allowed */
458 	unsigned int iscsi_tagmask;	     /* iscsi ddp tag mask */
459 	unsigned int iscsi_pgsz_order;	     /* iscsi ddp page size orders */
460 	unsigned int iscsi_llimit;	     /* chip's iscsi region llimit */
461 	unsigned int ulp_crypto;             /* crypto lookaside support */
462 	void **iscsi_ppm;		     /* iscsi page pod manager */
463 	int nodeid;			     /* device numa node id */
464 	bool fr_nsmr_tpte_wr_support;	     /* FW supports FR_NSMR_TPTE_WR */
465 	bool write_w_imm_support;         /* FW supports WRITE_WITH_IMMEDIATE */
466 	bool write_cmpl_support;             /* FW supports WRITE_CMPL WR */
467 };
468 
469 struct cxgb4_uld_info {
470 	char name[IFNAMSIZ];
471 	void *handle;
472 	unsigned int nrxq;
473 	unsigned int rxq_size;
474 	unsigned int ntxq;
475 	bool ciq;
476 	bool lro;
477 	void *(*add)(const struct cxgb4_lld_info *p);
478 	int (*rx_handler)(void *handle, const __be64 *rsp,
479 			  const struct pkt_gl *gl);
480 	int (*state_change)(void *handle, enum cxgb4_state new_state);
481 	int (*control)(void *handle, enum cxgb4_control control, ...);
482 	int (*lro_rx_handler)(void *handle, const __be64 *rsp,
483 			      const struct pkt_gl *gl,
484 			      struct t4_lro_mgr *lro_mgr,
485 			      struct napi_struct *napi);
486 	void (*lro_flush)(struct t4_lro_mgr *);
487 	int (*tx_handler)(struct sk_buff *skb, struct net_device *dev);
488 #if IS_ENABLED(CONFIG_CHELSIO_TLS_DEVICE)
489 	const struct tlsdev_ops *tlsdev_ops;
490 #endif
491 #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
492 	const struct xfrmdev_ops *xfrmdev_ops;
493 #endif
494 };
495 
496 void cxgb4_uld_enable(struct adapter *adap);
497 void cxgb4_register_uld(enum cxgb4_uld type, const struct cxgb4_uld_info *p);
498 int cxgb4_unregister_uld(enum cxgb4_uld type);
499 int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb);
500 int cxgb4_immdata_send(struct net_device *dev, unsigned int idx,
501 		       const void *src, unsigned int len);
502 int cxgb4_crypto_send(struct net_device *dev, struct sk_buff *skb);
503 unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo);
504 unsigned int cxgb4_port_chan(const struct net_device *dev);
505 unsigned int cxgb4_port_e2cchan(const struct net_device *dev);
506 unsigned int cxgb4_port_viid(const struct net_device *dev);
507 unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid);
508 unsigned int cxgb4_port_idx(const struct net_device *dev);
509 unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
510 			    unsigned int *idx);
511 unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
512 				    unsigned short header_size,
513 				    unsigned short data_size_max,
514 				    unsigned short data_size_align,
515 				    unsigned int *mtu_idxp);
516 void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
517 			 struct tp_tcp_stats *v6);
518 void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
519 		      const unsigned int *pgsz_order);
520 struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
521 				   unsigned int skb_len, unsigned int pull_len);
522 int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx, u16 size);
523 int cxgb4_flush_eq_cache(struct net_device *dev);
524 int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte);
525 u64 cxgb4_read_sge_timestamp(struct net_device *dev);
526 
527 enum cxgb4_bar2_qtype { CXGB4_BAR2_QTYPE_EGRESS, CXGB4_BAR2_QTYPE_INGRESS };
528 int cxgb4_bar2_sge_qregs(struct net_device *dev,
529 			 unsigned int qid,
530 			 enum cxgb4_bar2_qtype qtype,
531 			 int user,
532 			 u64 *pbar2_qoffset,
533 			 unsigned int *pbar2_qid);
534 
535 #endif  /* !__CXGB4_ULD_H */
536