]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/cxgbe/adapter.h
Backport some parts of r272200.
[FreeBSD/stable/10.git] / sys / dev / cxgbe / adapter.h
1 /*-
2  * Copyright (c) 2011 Chelsio Communications, Inc.
3  * All rights reserved.
4  * Written by: Navdeep Parhar <np@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  *
29  */
30
31 #ifndef __T4_ADAPTER_H__
32 #define __T4_ADAPTER_H__
33
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/rman.h>
37 #include <sys/types.h>
38 #include <sys/malloc.h>
39 #include <dev/pci/pcivar.h>
40 #include <dev/pci/pcireg.h>
41 #include <machine/bus.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
44 #include <net/ethernet.h>
45 #include <net/if.h>
46 #include <net/if_media.h>
47 #include <netinet/in.h>
48 #include <netinet/tcp_lro.h>
49
50 #include "offload.h"
51 #include "common/t4_msg.h"
52 #include "firmware/t4fw_interface.h"
53
54 #define KTR_CXGBE       KTR_SPARE3
55 MALLOC_DECLARE(M_CXGBE);
56 #define CXGBE_UNIMPLEMENTED(s) \
57     panic("%s (%s, line %d) not implemented yet.", s, __FILE__, __LINE__)
58
59 #if defined(__i386__) || defined(__amd64__)
60 static __inline void
61 prefetch(void *x)
62 {
63         __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
64 }
65 #else
66 #define prefetch(x)
67 #endif
68
69 #ifndef SYSCTL_ADD_UQUAD
70 #define SYSCTL_ADD_UQUAD SYSCTL_ADD_QUAD
71 #define sysctl_handle_64 sysctl_handle_quad
72 #define CTLTYPE_U64 CTLTYPE_QUAD
73 #endif
74
75 #if (__FreeBSD_version >= 900030) || \
76     ((__FreeBSD_version >= 802507) && (__FreeBSD_version < 900000))
77 #define SBUF_DRAIN 1
78 #endif
79
80 #ifdef __amd64__
81 /* XXX: need systemwide bus_space_read_8/bus_space_write_8 */
82 static __inline uint64_t
83 t4_bus_space_read_8(bus_space_tag_t tag, bus_space_handle_t handle,
84     bus_size_t offset)
85 {
86         KASSERT(tag == X86_BUS_SPACE_MEM,
87             ("%s: can only handle mem space", __func__));
88
89         return (*(volatile uint64_t *)(handle + offset));
90 }
91
92 static __inline void
93 t4_bus_space_write_8(bus_space_tag_t tag, bus_space_handle_t bsh,
94     bus_size_t offset, uint64_t value)
95 {
96         KASSERT(tag == X86_BUS_SPACE_MEM,
97             ("%s: can only handle mem space", __func__));
98
99         *(volatile uint64_t *)(bsh + offset) = value;
100 }
101 #else
102 static __inline uint64_t
103 t4_bus_space_read_8(bus_space_tag_t tag, bus_space_handle_t handle,
104     bus_size_t offset)
105 {
106         return (uint64_t)bus_space_read_4(tag, handle, offset) +
107             ((uint64_t)bus_space_read_4(tag, handle, offset + 4) << 32);
108 }
109
110 static __inline void
111 t4_bus_space_write_8(bus_space_tag_t tag, bus_space_handle_t bsh,
112     bus_size_t offset, uint64_t value)
113 {
114         bus_space_write_4(tag, bsh, offset, value);
115         bus_space_write_4(tag, bsh, offset + 4, value >> 32);
116 }
117 #endif
118
119 struct adapter;
120 typedef struct adapter adapter_t;
121
122 enum {
123         /*
124          * All ingress queues use this entry size.  Note that the firmware event
125          * queue and any iq expecting CPL_RX_PKT in the descriptor needs this to
126          * be at least 64.
127          */
128         IQ_ESIZE = 64,
129
130         /* Default queue sizes for all kinds of ingress queues */
131         FW_IQ_QSIZE = 256,
132         RX_IQ_QSIZE = 1024,
133
134         /* All egress queues use this entry size */
135         EQ_ESIZE = 64,
136
137         /* Default queue sizes for all kinds of egress queues */
138         CTRL_EQ_QSIZE = 128,
139         TX_EQ_QSIZE = 1024,
140
141 #if MJUMPAGESIZE != MCLBYTES
142         SW_ZONE_SIZES = 4,      /* cluster, jumbop, jumbo9k, jumbo16k */
143 #else
144         SW_ZONE_SIZES = 3,      /* cluster, jumbo9k, jumbo16k */
145 #endif
146         CL_METADATA_SIZE = CACHE_LINE_SIZE,
147
148         SGE_MAX_WR_NDESC = SGE_MAX_WR_LEN / EQ_ESIZE, /* max WR size in desc */
149         TX_SGL_SEGS = 36,
150         TX_WR_FLITS = SGE_MAX_WR_LEN / 8
151 };
152
153 enum {
154         /* adapter intr_type */
155         INTR_INTX       = (1 << 0),
156         INTR_MSI        = (1 << 1),
157         INTR_MSIX       = (1 << 2)
158 };
159
160 enum {
161         XGMAC_MTU       = (1 << 0),
162         XGMAC_PROMISC   = (1 << 1),
163         XGMAC_ALLMULTI  = (1 << 2),
164         XGMAC_VLANEX    = (1 << 3),
165         XGMAC_UCADDR    = (1 << 4),
166         XGMAC_MCADDRS   = (1 << 5),
167
168         XGMAC_ALL       = 0xffff
169 };
170
171 enum {
172         /* flags understood by begin_synchronized_op */
173         HOLD_LOCK       = (1 << 0),
174         SLEEP_OK        = (1 << 1),
175         INTR_OK         = (1 << 2),
176
177         /* flags understood by end_synchronized_op */
178         LOCK_HELD       = HOLD_LOCK,
179 };
180
181 enum {
182         /* adapter flags */
183         FULL_INIT_DONE  = (1 << 0),
184         FW_OK           = (1 << 1),
185         /* INTR_DIRECT  = (1 << 2),     No longer used. */
186         MASTER_PF       = (1 << 3),
187         ADAP_SYSCTL_CTX = (1 << 4),
188         TOM_INIT_DONE   = (1 << 5),
189         BUF_PACKING_OK  = (1 << 6),
190
191         CXGBE_BUSY      = (1 << 9),
192
193         /* port flags */
194         DOOMED          = (1 << 0),
195         PORT_INIT_DONE  = (1 << 1),
196         PORT_SYSCTL_CTX = (1 << 2),
197         HAS_TRACEQ      = (1 << 3),
198         INTR_RXQ        = (1 << 4),     /* All NIC rxq's take interrupts */
199         INTR_OFLD_RXQ   = (1 << 5),     /* All TOE rxq's take interrupts */
200         INTR_NM_RXQ     = (1 << 6),     /* All netmap rxq's take interrupts */
201         INTR_ALL        = (INTR_RXQ | INTR_OFLD_RXQ | INTR_NM_RXQ),
202 };
203
204 #define IS_DOOMED(pi)   ((pi)->flags & DOOMED)
205 #define SET_DOOMED(pi)  do {(pi)->flags |= DOOMED;} while (0)
206 #define IS_BUSY(sc)     ((sc)->flags & CXGBE_BUSY)
207 #define SET_BUSY(sc)    do {(sc)->flags |= CXGBE_BUSY;} while (0)
208 #define CLR_BUSY(sc)    do {(sc)->flags &= ~CXGBE_BUSY;} while (0)
209
210 struct port_info {
211         device_t dev;
212         struct adapter *adapter;
213
214         struct ifnet *ifp;
215         struct ifmedia media;
216
217         struct mtx pi_lock;
218         char lockname[16];
219         unsigned long flags;
220         int if_flags;
221
222         uint16_t *rss;
223         uint16_t viid;
224         int16_t  xact_addr_filt;/* index of exact MAC address filter */
225         uint16_t rss_size;      /* size of VI's RSS table slice */
226         uint8_t  lport;         /* associated offload logical port */
227         int8_t   mdio_addr;
228         uint8_t  port_type;
229         uint8_t  mod_type;
230         uint8_t  port_id;
231         uint8_t  tx_chan;
232         uint8_t  rx_chan_map;   /* rx MPS channel bitmap */
233
234         /* These need to be int as they are used in sysctl */
235         int ntxq;       /* # of tx queues */
236         int first_txq;  /* index of first tx queue */
237         int rsrv_noflowq; /* Reserve queue 0 for non-flowid packets */
238         int nrxq;       /* # of rx queues */
239         int first_rxq;  /* index of first rx queue */
240 #ifdef TCP_OFFLOAD
241         int nofldtxq;           /* # of offload tx queues */
242         int first_ofld_txq;     /* index of first offload tx queue */
243         int nofldrxq;           /* # of offload rx queues */
244         int first_ofld_rxq;     /* index of first offload rx queue */
245 #endif
246 #ifdef DEV_NETMAP
247         int nnmtxq;             /* # of netmap tx queues */
248         int first_nm_txq;       /* index of first netmap tx queue */
249         int nnmrxq;             /* # of netmap rx queues */
250         int first_nm_rxq;       /* index of first netmap rx queue */
251
252         struct ifnet *nm_ifp;
253         struct ifmedia nm_media;
254         int nmif_flags;
255         uint16_t nm_viid;
256         int16_t nm_xact_addr_filt;
257         uint16_t nm_rss_size;   /* size of netmap VI's RSS table slice */
258 #endif
259         int tmr_idx;
260         int pktc_idx;
261         int qsize_rxq;
262         int qsize_txq;
263
264         int linkdnrc;
265         struct link_config link_cfg;
266
267         struct timeval last_refreshed;
268         struct port_stats stats;
269
270         eventhandler_tag vlan_c;
271
272         struct callout tick;
273         struct sysctl_ctx_list ctx;     /* from ifconfig up to driver detach */
274
275         uint8_t hw_addr[ETHER_ADDR_LEN]; /* factory MAC address, won't change */
276 };
277
278 /* Where the cluster came from, how it has been carved up. */
279 struct cluster_layout {
280         int8_t zidx;
281         int8_t hwidx;
282         uint16_t region1;       /* mbufs laid out within this region */
283                                 /* region2 is the DMA region */
284         uint16_t region3;       /* cluster_metadata within this region */
285 };
286
287 struct cluster_metadata {
288         u_int refcount;
289 #ifdef INVARIANTS
290         struct fl_sdesc *sd;    /* For debug only.  Could easily be stale */
291 #endif
292 };
293
294 struct fl_sdesc {
295         caddr_t cl;
296         uint16_t nmbuf; /* # of driver originated mbufs with ref on cluster */
297         struct cluster_layout cll;
298 };
299
300 struct tx_desc {
301         __be64 flit[8];
302 };
303
304 struct tx_map {
305         struct mbuf *m;
306         bus_dmamap_t map;
307 };
308
309 /* DMA maps used for tx */
310 struct tx_maps {
311         struct tx_map *maps;
312         uint32_t map_total;     /* # of DMA maps */
313         uint32_t map_pidx;      /* next map to be used */
314         uint32_t map_cidx;      /* reclaimed up to this index */
315         uint32_t map_avail;     /* # of available maps */
316 };
317
318 struct tx_sdesc {
319         uint8_t desc_used;      /* # of hardware descriptors used by the WR */
320         uint8_t credits;        /* NIC txq: # of frames sent out in the WR */
321 };
322
323
324 #define IQ_PAD (IQ_ESIZE - sizeof(struct rsp_ctrl) - sizeof(struct rss_header))
325 struct iq_desc {
326         struct rss_header rss;
327         uint8_t cpl[IQ_PAD];
328         struct rsp_ctrl rsp;
329 };
330 #undef IQ_PAD
331 CTASSERT(sizeof(struct iq_desc) == IQ_ESIZE);
332
333 enum {
334         /* iq flags */
335         IQ_ALLOCATED    = (1 << 0),     /* firmware resources allocated */
336         IQ_HAS_FL       = (1 << 1),     /* iq associated with a freelist */
337         IQ_INTR         = (1 << 2),     /* iq takes direct interrupt */
338         IQ_LRO_ENABLED  = (1 << 3),     /* iq is an eth rxq with LRO enabled */
339
340         /* iq state */
341         IQS_DISABLED    = 0,
342         IQS_BUSY        = 1,
343         IQS_IDLE        = 2,
344 };
345
346 /*
347  * Ingress Queue: T4 is producer, driver is consumer.
348  */
349 struct sge_iq {
350         uint32_t flags;
351         volatile int state;
352         struct adapter *adapter;
353         struct iq_desc  *desc;  /* KVA of descriptor ring */
354         int8_t   intr_pktc_idx; /* packet count threshold index */
355         uint8_t  gen;           /* generation bit */
356         uint8_t  intr_params;   /* interrupt holdoff parameters */
357         uint8_t  intr_next;     /* XXX: holdoff for next interrupt */
358         uint16_t qsize;         /* size (# of entries) of the queue */
359         uint16_t sidx;          /* index of the entry with the status page */
360         uint16_t cidx;          /* consumer index */
361         uint16_t cntxt_id;      /* SGE context id for the iq */
362         uint16_t abs_id;        /* absolute SGE id for the iq */
363
364         STAILQ_ENTRY(sge_iq) link;
365
366         bus_dma_tag_t desc_tag;
367         bus_dmamap_t desc_map;
368         bus_addr_t ba;          /* bus address of descriptor ring */
369 };
370
371 enum {
372         EQ_CTRL         = 1,
373         EQ_ETH          = 2,
374 #ifdef TCP_OFFLOAD
375         EQ_OFLD         = 3,
376 #endif
377
378         /* eq flags */
379         EQ_TYPEMASK     = 7,            /* 3 lsbits hold the type */
380         EQ_ALLOCATED    = (1 << 3),     /* firmware resources allocated */
381         EQ_DOOMED       = (1 << 4),     /* about to be destroyed */
382         EQ_CRFLUSHED    = (1 << 5),     /* expecting an update from SGE */
383         EQ_STALLED      = (1 << 6),     /* out of hw descriptors or dmamaps */
384 };
385
386 /* Listed in order of preference.  Update t4_sysctls too if you change these */
387 enum {DOORBELL_UDB, DOORBELL_WCWR, DOORBELL_UDBWC, DOORBELL_KDB};
388
389 /*
390  * Egress Queue: driver is producer, T4 is consumer.
391  *
392  * Note: A free list is an egress queue (driver produces the buffers and T4
393  * consumes them) but it's special enough to have its own struct (see sge_fl).
394  */
395 struct sge_eq {
396         unsigned int flags;     /* MUST be first */
397         unsigned int cntxt_id;  /* SGE context id for the eq */
398         bus_dma_tag_t desc_tag;
399         bus_dmamap_t desc_map;
400         char lockname[16];
401         struct mtx eq_lock;
402
403         struct tx_desc *desc;   /* KVA of descriptor ring */
404         bus_addr_t ba;          /* bus address of descriptor ring */
405         struct sge_qstat *spg;  /* status page, for convenience */
406         uint16_t doorbells;
407         volatile uint32_t *udb; /* KVA of doorbell (lies within BAR2) */
408         u_int udb_qid;          /* relative qid within the doorbell page */
409         uint16_t cap;           /* max # of desc, for convenience */
410         uint16_t avail;         /* available descriptors, for convenience */
411         uint16_t qsize;         /* size (# of entries) of the queue */
412         uint16_t cidx;          /* consumer idx (desc idx) */
413         uint16_t pidx;          /* producer idx (desc idx) */
414         uint16_t pending;       /* # of descriptors used since last doorbell */
415         uint16_t iqid;          /* iq that gets egr_update for the eq */
416         uint8_t tx_chan;        /* tx channel used by the eq */
417         struct task tx_task;
418         struct callout tx_callout;
419
420         /* stats */
421
422         uint32_t egr_update;    /* # of SGE_EGR_UPDATE notifications for eq */
423         uint32_t unstalled;     /* recovered from stall */
424 };
425
426 struct sw_zone_info {
427         uma_zone_t zone;        /* zone that this cluster comes from */
428         int size;               /* size of cluster: 2K, 4K, 9K, 16K, etc. */
429         int type;               /* EXT_xxx type of the cluster */
430         int8_t head_hwidx;
431         int8_t tail_hwidx;
432 };
433
434 struct hw_buf_info {
435         int8_t zidx;            /* backpointer to zone; -ve means unused */
436         int8_t next;            /* next hwidx for this zone; -1 means no more */
437         int size;
438 };
439
440 enum {
441         FL_STARVING     = (1 << 0), /* on the adapter's list of starving fl's */
442         FL_DOOMED       = (1 << 1), /* about to be destroyed */
443         FL_BUF_PACKING  = (1 << 2), /* buffer packing enabled */
444         FL_BUF_RESUME   = (1 << 3), /* resume from the middle of the frame */
445 };
446
447 #define FL_RUNNING_LOW(fl) \
448     (IDXDIFF(fl->dbidx * 8, fl->cidx, fl->sidx * 8) <= fl->lowat)
449 #define FL_NOT_RUNNING_LOW(fl) \
450     (IDXDIFF(fl->dbidx * 8, fl->cidx, fl->sidx * 8) >= 2 * fl->lowat)
451
452 struct sge_fl {
453         struct mtx fl_lock;
454         __be64 *desc;           /* KVA of descriptor ring, ptr to addresses */
455         struct fl_sdesc *sdesc; /* KVA of software descriptor ring */
456         struct cluster_layout cll_def;  /* default refill zone, layout */
457         uint16_t lowat;         /* # of buffers <= this means fl needs help */
458         int flags;
459         uint16_t buf_boundary;
460
461         /* The 16b idx all deal with hw descriptors */
462         uint16_t dbidx;         /* hw pidx after last doorbell */
463         uint16_t sidx;          /* index of status page */
464         volatile uint16_t hw_cidx;
465
466         /* The 32b idx are all buffer idx, not hardware descriptor idx */
467         uint32_t cidx;          /* consumer index */
468         uint32_t pidx;          /* producer index */
469
470         uint32_t dbval;
471         u_int rx_offset;        /* offset in fl buf (when buffer packing) */
472         volatile uint32_t *udb;
473
474         uint64_t mbuf_allocated;/* # of mbuf allocated from zone_mbuf */
475         uint64_t mbuf_inlined;  /* # of mbuf created within clusters */
476         uint64_t cl_allocated;  /* # of clusters allocated */
477         uint64_t cl_recycled;   /* # of clusters recycled */
478         uint64_t cl_fast_recycled; /* # of clusters recycled (fast) */
479
480         /* These 3 are valid when FL_BUF_RESUME is set, stale otherwise. */
481         struct mbuf *m0;
482         struct mbuf **pnext;
483         u_int remaining;
484
485         uint16_t qsize;         /* # of hw descriptors (status page included) */
486         uint16_t cntxt_id;      /* SGE context id for the freelist */
487         TAILQ_ENTRY(sge_fl) link; /* All starving freelists */
488         bus_dma_tag_t desc_tag;
489         bus_dmamap_t desc_map;
490         char lockname[16];
491         bus_addr_t ba;          /* bus address of descriptor ring */
492         struct cluster_layout cll_alt;  /* alternate refill zone, layout */
493 };
494
495 /* txq: SGE egress queue + what's needed for Ethernet NIC */
496 struct sge_txq {
497         struct sge_eq eq;       /* MUST be first */
498
499         struct ifnet *ifp;      /* the interface this txq belongs to */
500         bus_dma_tag_t tx_tag;   /* tag for transmit buffers */
501         struct buf_ring *br;    /* tx buffer ring */
502         struct tx_sdesc *sdesc; /* KVA of software descriptor ring */
503         struct mbuf *m;         /* held up due to temporary resource shortage */
504
505         struct tx_maps txmaps;
506
507         /* stats for common events first */
508
509         uint64_t txcsum;        /* # of times hardware assisted with checksum */
510         uint64_t tso_wrs;       /* # of TSO work requests */
511         uint64_t vlan_insertion;/* # of times VLAN tag was inserted */
512         uint64_t imm_wrs;       /* # of work requests with immediate data */
513         uint64_t sgl_wrs;       /* # of work requests with direct SGL */
514         uint64_t txpkt_wrs;     /* # of txpkt work requests (not coalesced) */
515         uint64_t txpkts_wrs;    /* # of coalesced tx work requests */
516         uint64_t txpkts_pkts;   /* # of frames in coalesced tx work requests */
517
518         /* stats for not-that-common events */
519
520         uint32_t no_dmamap;     /* no DMA map to load the mbuf */
521         uint32_t no_desc;       /* out of hardware descriptors */
522 } __aligned(CACHE_LINE_SIZE);
523
524 /* rxq: SGE ingress queue + SGE free list + miscellaneous items */
525 struct sge_rxq {
526         struct sge_iq iq;       /* MUST be first */
527         struct sge_fl fl;       /* MUST follow iq */
528
529         struct ifnet *ifp;      /* the interface this rxq belongs to */
530 #if defined(INET) || defined(INET6)
531         struct lro_ctrl lro;    /* LRO state */
532 #endif
533
534         /* stats for common events first */
535
536         uint64_t rxcsum;        /* # of times hardware assisted with checksum */
537         uint64_t vlan_extraction;/* # of times VLAN tag was extracted */
538
539         /* stats for not-that-common events */
540
541 } __aligned(CACHE_LINE_SIZE);
542
543 static inline struct sge_rxq *
544 iq_to_rxq(struct sge_iq *iq)
545 {
546
547         return (__containerof(iq, struct sge_rxq, iq));
548 }
549
550
551 #ifdef TCP_OFFLOAD
552 /* ofld_rxq: SGE ingress queue + SGE free list + miscellaneous items */
553 struct sge_ofld_rxq {
554         struct sge_iq iq;       /* MUST be first */
555         struct sge_fl fl;       /* MUST follow iq */
556 } __aligned(CACHE_LINE_SIZE);
557
558 static inline struct sge_ofld_rxq *
559 iq_to_ofld_rxq(struct sge_iq *iq)
560 {
561
562         return (__containerof(iq, struct sge_ofld_rxq, iq));
563 }
564 #endif
565
566 struct wrqe {
567         STAILQ_ENTRY(wrqe) link;
568         struct sge_wrq *wrq;
569         int wr_len;
570         uint64_t wr[] __aligned(16);
571 };
572
573 /*
574  * wrq: SGE egress queue that is given prebuilt work requests.  Both the control
575  * and offload tx queues are of this type.
576  */
577 struct sge_wrq {
578         struct sge_eq eq;       /* MUST be first */
579
580         struct adapter *adapter;
581
582         /* List of WRs held up due to lack of tx descriptors */
583         STAILQ_HEAD(, wrqe) wr_list;
584
585         /* stats for common events first */
586
587         uint64_t tx_wrs;        /* # of tx work requests */
588
589         /* stats for not-that-common events */
590
591         uint32_t no_desc;       /* out of hardware descriptors */
592 } __aligned(CACHE_LINE_SIZE);
593
594
595 #ifdef DEV_NETMAP
596 struct sge_nm_rxq {
597         struct port_info *pi;
598
599         struct iq_desc *iq_desc;
600         uint16_t iq_abs_id;
601         uint16_t iq_cntxt_id;
602         uint16_t iq_cidx;
603         uint16_t iq_sidx;
604         uint8_t iq_gen;
605
606         __be64  *fl_desc;
607         uint16_t fl_cntxt_id;
608         uint32_t fl_cidx;
609         uint32_t fl_pidx;
610         uint32_t fl_sidx;
611         uint32_t fl_db_val;
612         u_int fl_hwidx:4;
613
614         u_int nid;              /* netmap ring # for this queue */
615
616         /* infrequently used items after this */
617
618         bus_dma_tag_t iq_desc_tag;
619         bus_dmamap_t iq_desc_map;
620         bus_addr_t iq_ba;
621         int intr_idx;
622
623         bus_dma_tag_t fl_desc_tag;
624         bus_dmamap_t fl_desc_map;
625         bus_addr_t fl_ba;
626 } __aligned(CACHE_LINE_SIZE);
627
628 struct sge_nm_txq {
629         struct tx_desc *desc;
630         uint16_t cidx;
631         uint16_t pidx;
632         uint16_t sidx;
633         uint16_t equiqidx;      /* EQUIQ last requested at this pidx */
634         uint16_t equeqidx;      /* EQUEQ last requested at this pidx */
635         uint16_t dbidx;         /* pidx of the most recent doorbell */
636         uint16_t doorbells;
637         volatile uint32_t *udb;
638         u_int udb_qid;
639         u_int cntxt_id;
640         __be32 cpl_ctrl0;       /* for convenience */
641         u_int nid;              /* netmap ring # for this queue */
642
643         /* infrequently used items after this */
644
645         bus_dma_tag_t desc_tag;
646         bus_dmamap_t desc_map;
647         bus_addr_t ba;
648         int iqidx;
649 } __aligned(CACHE_LINE_SIZE);
650 #endif
651
652 struct sge {
653         int timer_val[SGE_NTIMERS];
654         int counter_val[SGE_NCOUNTERS];
655         int fl_starve_threshold;
656         int fl_starve_threshold2;
657         int eq_s_qpp;
658         int iq_s_qpp;
659
660         int nrxq;       /* total # of Ethernet rx queues */
661         int ntxq;       /* total # of Ethernet tx tx queues */
662 #ifdef TCP_OFFLOAD
663         int nofldrxq;   /* total # of TOE rx queues */
664         int nofldtxq;   /* total # of TOE tx queues */
665 #endif
666 #ifdef DEV_NETMAP
667         int nnmrxq;     /* total # of netmap rx queues */
668         int nnmtxq;     /* total # of netmap tx queues */
669 #endif
670         int niq;        /* total # of ingress queues */
671         int neq;        /* total # of egress queues */
672
673         struct sge_iq fwq;      /* Firmware event queue */
674         struct sge_wrq mgmtq;   /* Management queue (control queue) */
675         struct sge_wrq *ctrlq;  /* Control queues */
676         struct sge_txq *txq;    /* NIC tx queues */
677         struct sge_rxq *rxq;    /* NIC rx queues */
678 #ifdef TCP_OFFLOAD
679         struct sge_wrq *ofld_txq;       /* TOE tx queues */
680         struct sge_ofld_rxq *ofld_rxq;  /* TOE rx queues */
681 #endif
682 #ifdef DEV_NETMAP
683         struct sge_nm_txq *nm_txq;      /* netmap tx queues */
684         struct sge_nm_rxq *nm_rxq;      /* netmap rx queues */
685 #endif
686
687         uint16_t iq_start;
688         int eq_start;
689         struct sge_iq **iqmap;  /* iq->cntxt_id to iq mapping */
690         struct sge_eq **eqmap;  /* eq->cntxt_id to eq mapping */
691
692         int pad_boundary;
693         int pack_boundary;
694         int8_t safe_hwidx1;     /* may not have room for metadata */
695         int8_t safe_hwidx2;     /* with room for metadata and maybe more */
696         struct sw_zone_info sw_zone_info[SW_ZONE_SIZES];
697         struct hw_buf_info hw_buf_info[SGE_FLBUF_SIZES];
698 };
699
700 struct rss_header;
701 typedef int (*cpl_handler_t)(struct sge_iq *, const struct rss_header *,
702     struct mbuf *);
703 typedef int (*an_handler_t)(struct sge_iq *, const struct rsp_ctrl *);
704 typedef int (*fw_msg_handler_t)(struct adapter *, const __be64 *);
705
706 struct adapter {
707         SLIST_ENTRY(adapter) link;
708         device_t dev;
709         struct cdev *cdev;
710
711         /* PCIe register resources */
712         int regs_rid;
713         struct resource *regs_res;
714         int msix_rid;
715         struct resource *msix_res;
716         bus_space_handle_t bh;
717         bus_space_tag_t bt;
718         bus_size_t mmio_len;
719         int udbs_rid;
720         struct resource *udbs_res;
721         volatile uint8_t *udbs_base;
722
723         unsigned int pf;
724         unsigned int mbox;
725
726         /* Interrupt information */
727         int intr_type;
728         int intr_count;
729         struct irq {
730                 struct resource *res;
731                 int rid;
732                 void *tag;
733         } *irq;
734
735         bus_dma_tag_t dmat;     /* Parent DMA tag */
736
737         struct sge sge;
738         int lro_timeout;
739
740         struct taskqueue *tq[NCHAN];    /* taskqueues that flush data out */
741         struct port_info *port[MAX_NPORTS];
742         uint8_t chan_map[NCHAN];
743
744 #ifdef TCP_OFFLOAD
745         void *tom_softc;        /* (struct tom_data *) */
746         struct tom_tunables tt;
747         void *iwarp_softc;      /* (struct c4iw_dev *) */
748         void *iscsi_softc;
749 #endif
750         struct l2t_data *l2t;   /* L2 table */
751         struct tid_info tids;
752
753         uint16_t doorbells;
754         int open_device_map;
755 #ifdef TCP_OFFLOAD
756         int offload_map;
757 #endif
758         int flags;
759
760         char ifp_lockname[16];
761         struct mtx ifp_lock;
762         struct ifnet *ifp;      /* tracer ifp */
763         struct ifmedia media;
764         int traceq;             /* iq used by all tracers, -1 if none */
765         int tracer_valid;       /* bitmap of valid tracers */
766         int tracer_enabled;     /* bitmap of enabled tracers */
767
768         char fw_version[32];
769         char cfg_file[32];
770         u_int cfcsum;
771         struct adapter_params params;
772         struct t4_virt_res vres;
773
774         uint16_t linkcaps;
775         uint16_t niccaps;
776         uint16_t toecaps;
777         uint16_t rdmacaps;
778         uint16_t iscsicaps;
779         uint16_t fcoecaps;
780
781         struct sysctl_ctx_list ctx; /* from adapter_full_init to full_uninit */
782
783         struct mtx sc_lock;
784         char lockname[16];
785
786         /* Starving free lists */
787         struct mtx sfl_lock;    /* same cache-line as sc_lock? but that's ok */
788         TAILQ_HEAD(, sge_fl) sfl;
789         struct callout sfl_callout;
790
791         struct mtx regwin_lock; /* for indirect reads and memory windows */
792
793         an_handler_t an_handler __aligned(CACHE_LINE_SIZE);
794         fw_msg_handler_t fw_msg_handler[5];     /* NUM_FW6_TYPES */
795         cpl_handler_t cpl_handler[0xef];        /* NUM_CPL_CMDS */
796
797 #ifdef INVARIANTS
798         const char *last_op;
799         const void *last_op_thr;
800 #endif
801
802         int sc_do_rxcopy;
803 };
804
805 #define ADAPTER_LOCK(sc)                mtx_lock(&(sc)->sc_lock)
806 #define ADAPTER_UNLOCK(sc)              mtx_unlock(&(sc)->sc_lock)
807 #define ADAPTER_LOCK_ASSERT_OWNED(sc)   mtx_assert(&(sc)->sc_lock, MA_OWNED)
808 #define ADAPTER_LOCK_ASSERT_NOTOWNED(sc) mtx_assert(&(sc)->sc_lock, MA_NOTOWNED)
809
810 #define ASSERT_SYNCHRONIZED_OP(sc)      \
811     KASSERT(IS_BUSY(sc) && \
812         (mtx_owned(&(sc)->sc_lock) || sc->last_op_thr == curthread), \
813         ("%s: operation not synchronized.", __func__))
814
815 #define PORT_LOCK(pi)                   mtx_lock(&(pi)->pi_lock)
816 #define PORT_UNLOCK(pi)                 mtx_unlock(&(pi)->pi_lock)
817 #define PORT_LOCK_ASSERT_OWNED(pi)      mtx_assert(&(pi)->pi_lock, MA_OWNED)
818 #define PORT_LOCK_ASSERT_NOTOWNED(pi)   mtx_assert(&(pi)->pi_lock, MA_NOTOWNED)
819
820 #define FL_LOCK(fl)                     mtx_lock(&(fl)->fl_lock)
821 #define FL_TRYLOCK(fl)                  mtx_trylock(&(fl)->fl_lock)
822 #define FL_UNLOCK(fl)                   mtx_unlock(&(fl)->fl_lock)
823 #define FL_LOCK_ASSERT_OWNED(fl)        mtx_assert(&(fl)->fl_lock, MA_OWNED)
824 #define FL_LOCK_ASSERT_NOTOWNED(fl)     mtx_assert(&(fl)->fl_lock, MA_NOTOWNED)
825
826 #define RXQ_FL_LOCK(rxq)                FL_LOCK(&(rxq)->fl)
827 #define RXQ_FL_UNLOCK(rxq)              FL_UNLOCK(&(rxq)->fl)
828 #define RXQ_FL_LOCK_ASSERT_OWNED(rxq)   FL_LOCK_ASSERT_OWNED(&(rxq)->fl)
829 #define RXQ_FL_LOCK_ASSERT_NOTOWNED(rxq) FL_LOCK_ASSERT_NOTOWNED(&(rxq)->fl)
830
831 #define EQ_LOCK(eq)                     mtx_lock(&(eq)->eq_lock)
832 #define EQ_TRYLOCK(eq)                  mtx_trylock(&(eq)->eq_lock)
833 #define EQ_UNLOCK(eq)                   mtx_unlock(&(eq)->eq_lock)
834 #define EQ_LOCK_ASSERT_OWNED(eq)        mtx_assert(&(eq)->eq_lock, MA_OWNED)
835 #define EQ_LOCK_ASSERT_NOTOWNED(eq)     mtx_assert(&(eq)->eq_lock, MA_NOTOWNED)
836
837 #define TXQ_LOCK(txq)                   EQ_LOCK(&(txq)->eq)
838 #define TXQ_TRYLOCK(txq)                EQ_TRYLOCK(&(txq)->eq)
839 #define TXQ_UNLOCK(txq)                 EQ_UNLOCK(&(txq)->eq)
840 #define TXQ_LOCK_ASSERT_OWNED(txq)      EQ_LOCK_ASSERT_OWNED(&(txq)->eq)
841 #define TXQ_LOCK_ASSERT_NOTOWNED(txq)   EQ_LOCK_ASSERT_NOTOWNED(&(txq)->eq)
842
843 #define for_each_txq(pi, iter, q) \
844         for (q = &pi->adapter->sge.txq[pi->first_txq], iter = 0; \
845             iter < pi->ntxq; ++iter, ++q)
846 #define for_each_rxq(pi, iter, q) \
847         for (q = &pi->adapter->sge.rxq[pi->first_rxq], iter = 0; \
848             iter < pi->nrxq; ++iter, ++q)
849 #define for_each_ofld_txq(pi, iter, q) \
850         for (q = &pi->adapter->sge.ofld_txq[pi->first_ofld_txq], iter = 0; \
851             iter < pi->nofldtxq; ++iter, ++q)
852 #define for_each_ofld_rxq(pi, iter, q) \
853         for (q = &pi->adapter->sge.ofld_rxq[pi->first_ofld_rxq], iter = 0; \
854             iter < pi->nofldrxq; ++iter, ++q)
855 #define for_each_nm_txq(pi, iter, q) \
856         for (q = &pi->adapter->sge.nm_txq[pi->first_nm_txq], iter = 0; \
857             iter < pi->nnmtxq; ++iter, ++q)
858 #define for_each_nm_rxq(pi, iter, q) \
859         for (q = &pi->adapter->sge.nm_rxq[pi->first_nm_rxq], iter = 0; \
860             iter < pi->nnmrxq; ++iter, ++q)
861
862 #define IDXINCR(idx, incr, wrap) do { \
863         idx = wrap - idx > incr ? idx + incr : incr - (wrap - idx); \
864 } while (0)
865 #define IDXDIFF(head, tail, wrap) \
866         ((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
867
868 /* One for errors, one for firmware events */
869 #define T4_EXTRA_INTR 2
870
871 static inline uint32_t
872 t4_read_reg(struct adapter *sc, uint32_t reg)
873 {
874
875         return bus_space_read_4(sc->bt, sc->bh, reg);
876 }
877
878 static inline void
879 t4_write_reg(struct adapter *sc, uint32_t reg, uint32_t val)
880 {
881
882         bus_space_write_4(sc->bt, sc->bh, reg, val);
883 }
884
885 static inline uint64_t
886 t4_read_reg64(struct adapter *sc, uint32_t reg)
887 {
888
889         return t4_bus_space_read_8(sc->bt, sc->bh, reg);
890 }
891
892 static inline void
893 t4_write_reg64(struct adapter *sc, uint32_t reg, uint64_t val)
894 {
895
896         t4_bus_space_write_8(sc->bt, sc->bh, reg, val);
897 }
898
899 static inline void
900 t4_os_pci_read_cfg1(struct adapter *sc, int reg, uint8_t *val)
901 {
902
903         *val = pci_read_config(sc->dev, reg, 1);
904 }
905
906 static inline void
907 t4_os_pci_write_cfg1(struct adapter *sc, int reg, uint8_t val)
908 {
909
910         pci_write_config(sc->dev, reg, val, 1);
911 }
912
913 static inline void
914 t4_os_pci_read_cfg2(struct adapter *sc, int reg, uint16_t *val)
915 {
916
917         *val = pci_read_config(sc->dev, reg, 2);
918 }
919
920 static inline void
921 t4_os_pci_write_cfg2(struct adapter *sc, int reg, uint16_t val)
922 {
923
924         pci_write_config(sc->dev, reg, val, 2);
925 }
926
927 static inline void
928 t4_os_pci_read_cfg4(struct adapter *sc, int reg, uint32_t *val)
929 {
930
931         *val = pci_read_config(sc->dev, reg, 4);
932 }
933
934 static inline void
935 t4_os_pci_write_cfg4(struct adapter *sc, int reg, uint32_t val)
936 {
937
938         pci_write_config(sc->dev, reg, val, 4);
939 }
940
941 static inline struct port_info *
942 adap2pinfo(struct adapter *sc, int idx)
943 {
944
945         return (sc->port[idx]);
946 }
947
948 static inline void
949 t4_os_set_hw_addr(struct adapter *sc, int idx, uint8_t hw_addr[])
950 {
951
952         bcopy(hw_addr, sc->port[idx]->hw_addr, ETHER_ADDR_LEN);
953 }
954
955 static inline bool
956 is_10G_port(const struct port_info *pi)
957 {
958
959         return ((pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G) != 0);
960 }
961
962 static inline bool
963 is_40G_port(const struct port_info *pi)
964 {
965
966         return ((pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G) != 0);
967 }
968
969 static inline int
970 tx_resume_threshold(struct sge_eq *eq)
971 {
972
973         return (eq->qsize / 4);
974 }
975
976 /* t4_main.c */
977 void t4_tx_task(void *, int);
978 void t4_tx_callout(void *);
979 int t4_os_find_pci_capability(struct adapter *, int);
980 int t4_os_pci_save_state(struct adapter *);
981 int t4_os_pci_restore_state(struct adapter *);
982 void t4_os_portmod_changed(const struct adapter *, int);
983 void t4_os_link_changed(struct adapter *, int, int, int);
984 void t4_iterate(void (*)(struct adapter *, void *), void *);
985 int t4_register_cpl_handler(struct adapter *, int, cpl_handler_t);
986 int t4_register_an_handler(struct adapter *, an_handler_t);
987 int t4_register_fw_msg_handler(struct adapter *, int, fw_msg_handler_t);
988 int t4_filter_rpl(struct sge_iq *, const struct rss_header *, struct mbuf *);
989 int begin_synchronized_op(struct adapter *, struct port_info *, int, char *);
990 void end_synchronized_op(struct adapter *, int);
991 int update_mac_settings(struct ifnet *, int);
992 int adapter_full_init(struct adapter *);
993 int adapter_full_uninit(struct adapter *);
994 int port_full_init(struct port_info *);
995 int port_full_uninit(struct port_info *);
996
997 #ifdef DEV_NETMAP
998 /* t4_netmap.c */
999 int create_netmap_ifnet(struct port_info *);
1000 int destroy_netmap_ifnet(struct port_info *);
1001 void t4_nm_intr(void *);
1002 #endif
1003
1004 /* t4_sge.c */
1005 void t4_sge_modload(void);
1006 void t4_sge_modunload(void);
1007 uint64_t t4_sge_extfree_refs(void);
1008 void t4_init_sge_cpl_handlers(struct adapter *);
1009 void t4_tweak_chip_settings(struct adapter *);
1010 int t4_read_chip_settings(struct adapter *);
1011 int t4_create_dma_tag(struct adapter *);
1012 void t4_sge_sysctls(struct adapter *, struct sysctl_ctx_list *,
1013     struct sysctl_oid_list *);
1014 int t4_destroy_dma_tag(struct adapter *);
1015 int t4_setup_adapter_queues(struct adapter *);
1016 int t4_teardown_adapter_queues(struct adapter *);
1017 int t4_setup_port_queues(struct port_info *);
1018 int t4_teardown_port_queues(struct port_info *);
1019 int t4_alloc_tx_maps(struct tx_maps *, bus_dma_tag_t, int, int);
1020 void t4_free_tx_maps(struct tx_maps *, bus_dma_tag_t);
1021 void t4_intr_all(void *);
1022 void t4_intr(void *);
1023 void t4_intr_err(void *);
1024 void t4_intr_evt(void *);
1025 void t4_wrq_tx_locked(struct adapter *, struct sge_wrq *, struct wrqe *);
1026 int t4_eth_tx(struct ifnet *, struct sge_txq *, struct mbuf *);
1027 void t4_update_fl_bufsize(struct ifnet *);
1028 int can_resume_tx(struct sge_eq *);
1029 int tnl_cong(struct port_info *);
1030
1031 /* t4_tracer.c */
1032 struct t4_tracer;
1033 void t4_tracer_modload(void);
1034 void t4_tracer_modunload(void);
1035 void t4_tracer_port_detach(struct adapter *);
1036 int t4_get_tracer(struct adapter *, struct t4_tracer *);
1037 int t4_set_tracer(struct adapter *, struct t4_tracer *);
1038 int t4_trace_pkt(struct sge_iq *, const struct rss_header *, struct mbuf *);
1039 int t5_trace_pkt(struct sge_iq *, const struct rss_header *, struct mbuf *);
1040
1041 static inline struct wrqe *
1042 alloc_wrqe(int wr_len, struct sge_wrq *wrq)
1043 {
1044         int len = offsetof(struct wrqe, wr) + wr_len;
1045         struct wrqe *wr;
1046
1047         wr = malloc(len, M_CXGBE, M_NOWAIT);
1048         if (__predict_false(wr == NULL))
1049                 return (NULL);
1050         wr->wr_len = wr_len;
1051         wr->wrq = wrq;
1052         return (wr);
1053 }
1054
1055 static inline void *
1056 wrtod(struct wrqe *wr)
1057 {
1058         return (&wr->wr[0]);
1059 }
1060
1061 static inline void
1062 free_wrqe(struct wrqe *wr)
1063 {
1064         free(wr, M_CXGBE);
1065 }
1066
1067 static inline void
1068 t4_wrq_tx(struct adapter *sc, struct wrqe *wr)
1069 {
1070         struct sge_wrq *wrq = wr->wrq;
1071
1072         TXQ_LOCK(wrq);
1073         t4_wrq_tx_locked(sc, wrq, wr);
1074         TXQ_UNLOCK(wrq);
1075 }
1076
1077 #endif