]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/cxgbe/adapter.h
MFC r278371:
[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         struct port_stats stats;
267
268         eventhandler_tag vlan_c;
269
270         struct callout tick;
271         struct sysctl_ctx_list ctx;     /* from ifconfig up to driver detach */
272
273         uint8_t hw_addr[ETHER_ADDR_LEN]; /* factory MAC address, won't change */
274 };
275
276 /* Where the cluster came from, how it has been carved up. */
277 struct cluster_layout {
278         int8_t zidx;
279         int8_t hwidx;
280         uint16_t region1;       /* mbufs laid out within this region */
281                                 /* region2 is the DMA region */
282         uint16_t region3;       /* cluster_metadata within this region */
283 };
284
285 struct cluster_metadata {
286         u_int refcount;
287 #ifdef INVARIANTS
288         struct fl_sdesc *sd;    /* For debug only.  Could easily be stale */
289 #endif
290 };
291
292 struct fl_sdesc {
293         caddr_t cl;
294         uint16_t nmbuf; /* # of driver originated mbufs with ref on cluster */
295         struct cluster_layout cll;
296 };
297
298 struct tx_desc {
299         __be64 flit[8];
300 };
301
302 struct tx_map {
303         struct mbuf *m;
304         bus_dmamap_t map;
305 };
306
307 /* DMA maps used for tx */
308 struct tx_maps {
309         struct tx_map *maps;
310         uint32_t map_total;     /* # of DMA maps */
311         uint32_t map_pidx;      /* next map to be used */
312         uint32_t map_cidx;      /* reclaimed up to this index */
313         uint32_t map_avail;     /* # of available maps */
314 };
315
316 struct tx_sdesc {
317         uint8_t desc_used;      /* # of hardware descriptors used by the WR */
318         uint8_t credits;        /* NIC txq: # of frames sent out in the WR */
319 };
320
321
322 #define IQ_PAD (IQ_ESIZE - sizeof(struct rsp_ctrl) - sizeof(struct rss_header))
323 struct iq_desc {
324         struct rss_header rss;
325         uint8_t cpl[IQ_PAD];
326         struct rsp_ctrl rsp;
327 };
328 #undef IQ_PAD
329 CTASSERT(sizeof(struct iq_desc) == IQ_ESIZE);
330
331 enum {
332         /* iq flags */
333         IQ_ALLOCATED    = (1 << 0),     /* firmware resources allocated */
334         IQ_HAS_FL       = (1 << 1),     /* iq associated with a freelist */
335         IQ_INTR         = (1 << 2),     /* iq takes direct interrupt */
336         IQ_LRO_ENABLED  = (1 << 3),     /* iq is an eth rxq with LRO enabled */
337
338         /* iq state */
339         IQS_DISABLED    = 0,
340         IQS_BUSY        = 1,
341         IQS_IDLE        = 2,
342 };
343
344 /*
345  * Ingress Queue: T4 is producer, driver is consumer.
346  */
347 struct sge_iq {
348         uint32_t flags;
349         volatile int state;
350         struct adapter *adapter;
351         struct iq_desc  *desc;  /* KVA of descriptor ring */
352         int8_t   intr_pktc_idx; /* packet count threshold index */
353         uint8_t  gen;           /* generation bit */
354         uint8_t  intr_params;   /* interrupt holdoff parameters */
355         uint8_t  intr_next;     /* XXX: holdoff for next interrupt */
356         uint16_t qsize;         /* size (# of entries) of the queue */
357         uint16_t sidx;          /* index of the entry with the status page */
358         uint16_t cidx;          /* consumer index */
359         uint16_t cntxt_id;      /* SGE context id for the iq */
360         uint16_t abs_id;        /* absolute SGE id for the iq */
361
362         STAILQ_ENTRY(sge_iq) link;
363
364         bus_dma_tag_t desc_tag;
365         bus_dmamap_t desc_map;
366         bus_addr_t ba;          /* bus address of descriptor ring */
367 };
368
369 enum {
370         EQ_CTRL         = 1,
371         EQ_ETH          = 2,
372 #ifdef TCP_OFFLOAD
373         EQ_OFLD         = 3,
374 #endif
375
376         /* eq flags */
377         EQ_TYPEMASK     = 7,            /* 3 lsbits hold the type */
378         EQ_ALLOCATED    = (1 << 3),     /* firmware resources allocated */
379         EQ_DOOMED       = (1 << 4),     /* about to be destroyed */
380         EQ_CRFLUSHED    = (1 << 5),     /* expecting an update from SGE */
381         EQ_STALLED      = (1 << 6),     /* out of hw descriptors or dmamaps */
382 };
383
384 /* Listed in order of preference.  Update t4_sysctls too if you change these */
385 enum {DOORBELL_UDB, DOORBELL_WCWR, DOORBELL_UDBWC, DOORBELL_KDB};
386
387 /*
388  * Egress Queue: driver is producer, T4 is consumer.
389  *
390  * Note: A free list is an egress queue (driver produces the buffers and T4
391  * consumes them) but it's special enough to have its own struct (see sge_fl).
392  */
393 struct sge_eq {
394         unsigned int flags;     /* MUST be first */
395         unsigned int cntxt_id;  /* SGE context id for the eq */
396         bus_dma_tag_t desc_tag;
397         bus_dmamap_t desc_map;
398         char lockname[16];
399         struct mtx eq_lock;
400
401         struct tx_desc *desc;   /* KVA of descriptor ring */
402         bus_addr_t ba;          /* bus address of descriptor ring */
403         struct sge_qstat *spg;  /* status page, for convenience */
404         uint16_t doorbells;
405         volatile uint32_t *udb; /* KVA of doorbell (lies within BAR2) */
406         u_int udb_qid;          /* relative qid within the doorbell page */
407         uint16_t cap;           /* max # of desc, for convenience */
408         uint16_t avail;         /* available descriptors, for convenience */
409         uint16_t qsize;         /* size (# of entries) of the queue */
410         uint16_t cidx;          /* consumer idx (desc idx) */
411         uint16_t pidx;          /* producer idx (desc idx) */
412         uint16_t pending;       /* # of descriptors used since last doorbell */
413         uint16_t iqid;          /* iq that gets egr_update for the eq */
414         uint8_t tx_chan;        /* tx channel used by the eq */
415         struct task tx_task;
416         struct callout tx_callout;
417
418         /* stats */
419
420         uint32_t egr_update;    /* # of SGE_EGR_UPDATE notifications for eq */
421         uint32_t unstalled;     /* recovered from stall */
422 };
423
424 struct sw_zone_info {
425         uma_zone_t zone;        /* zone that this cluster comes from */
426         int size;               /* size of cluster: 2K, 4K, 9K, 16K, etc. */
427         int type;               /* EXT_xxx type of the cluster */
428         int8_t head_hwidx;
429         int8_t tail_hwidx;
430 };
431
432 struct hw_buf_info {
433         int8_t zidx;            /* backpointer to zone; -ve means unused */
434         int8_t next;            /* next hwidx for this zone; -1 means no more */
435         int size;
436 };
437
438 enum {
439         FL_STARVING     = (1 << 0), /* on the adapter's list of starving fl's */
440         FL_DOOMED       = (1 << 1), /* about to be destroyed */
441         FL_BUF_PACKING  = (1 << 2), /* buffer packing enabled */
442         FL_BUF_RESUME   = (1 << 3), /* resume from the middle of the frame */
443 };
444
445 #define FL_RUNNING_LOW(fl) \
446     (IDXDIFF(fl->dbidx * 8, fl->cidx, fl->sidx * 8) <= fl->lowat)
447 #define FL_NOT_RUNNING_LOW(fl) \
448     (IDXDIFF(fl->dbidx * 8, fl->cidx, fl->sidx * 8) >= 2 * fl->lowat)
449
450 struct sge_fl {
451         struct mtx fl_lock;
452         __be64 *desc;           /* KVA of descriptor ring, ptr to addresses */
453         struct fl_sdesc *sdesc; /* KVA of software descriptor ring */
454         struct cluster_layout cll_def;  /* default refill zone, layout */
455         uint16_t lowat;         /* # of buffers <= this means fl needs help */
456         int flags;
457         uint16_t buf_boundary;
458
459         /* The 16b idx all deal with hw descriptors */
460         uint16_t dbidx;         /* hw pidx after last doorbell */
461         uint16_t sidx;          /* index of status page */
462         volatile uint16_t hw_cidx;
463
464         /* The 32b idx are all buffer idx, not hardware descriptor idx */
465         uint32_t cidx;          /* consumer index */
466         uint32_t pidx;          /* producer index */
467
468         uint32_t dbval;
469         u_int rx_offset;        /* offset in fl buf (when buffer packing) */
470         volatile uint32_t *udb;
471
472         uint64_t mbuf_allocated;/* # of mbuf allocated from zone_mbuf */
473         uint64_t mbuf_inlined;  /* # of mbuf created within clusters */
474         uint64_t cl_allocated;  /* # of clusters allocated */
475         uint64_t cl_recycled;   /* # of clusters recycled */
476         uint64_t cl_fast_recycled; /* # of clusters recycled (fast) */
477
478         /* These 3 are valid when FL_BUF_RESUME is set, stale otherwise. */
479         struct mbuf *m0;
480         struct mbuf **pnext;
481         u_int remaining;
482
483         uint16_t qsize;         /* # of hw descriptors (status page included) */
484         uint16_t cntxt_id;      /* SGE context id for the freelist */
485         TAILQ_ENTRY(sge_fl) link; /* All starving freelists */
486         bus_dma_tag_t desc_tag;
487         bus_dmamap_t desc_map;
488         char lockname[16];
489         bus_addr_t ba;          /* bus address of descriptor ring */
490         struct cluster_layout cll_alt;  /* alternate refill zone, layout */
491 };
492
493 /* txq: SGE egress queue + what's needed for Ethernet NIC */
494 struct sge_txq {
495         struct sge_eq eq;       /* MUST be first */
496
497         struct ifnet *ifp;      /* the interface this txq belongs to */
498         bus_dma_tag_t tx_tag;   /* tag for transmit buffers */
499         struct buf_ring *br;    /* tx buffer ring */
500         struct tx_sdesc *sdesc; /* KVA of software descriptor ring */
501         struct mbuf *m;         /* held up due to temporary resource shortage */
502
503         struct tx_maps txmaps;
504
505         /* stats for common events first */
506
507         uint64_t txcsum;        /* # of times hardware assisted with checksum */
508         uint64_t tso_wrs;       /* # of TSO work requests */
509         uint64_t vlan_insertion;/* # of times VLAN tag was inserted */
510         uint64_t imm_wrs;       /* # of work requests with immediate data */
511         uint64_t sgl_wrs;       /* # of work requests with direct SGL */
512         uint64_t txpkt_wrs;     /* # of txpkt work requests (not coalesced) */
513         uint64_t txpkts_wrs;    /* # of coalesced tx work requests */
514         uint64_t txpkts_pkts;   /* # of frames in coalesced tx work requests */
515
516         /* stats for not-that-common events */
517
518         uint32_t no_dmamap;     /* no DMA map to load the mbuf */
519         uint32_t no_desc;       /* out of hardware descriptors */
520 } __aligned(CACHE_LINE_SIZE);
521
522 /* rxq: SGE ingress queue + SGE free list + miscellaneous items */
523 struct sge_rxq {
524         struct sge_iq iq;       /* MUST be first */
525         struct sge_fl fl;       /* MUST follow iq */
526
527         struct ifnet *ifp;      /* the interface this rxq belongs to */
528 #if defined(INET) || defined(INET6)
529         struct lro_ctrl lro;    /* LRO state */
530 #endif
531
532         /* stats for common events first */
533
534         uint64_t rxcsum;        /* # of times hardware assisted with checksum */
535         uint64_t vlan_extraction;/* # of times VLAN tag was extracted */
536
537         /* stats for not-that-common events */
538
539 } __aligned(CACHE_LINE_SIZE);
540
541 static inline struct sge_rxq *
542 iq_to_rxq(struct sge_iq *iq)
543 {
544
545         return (__containerof(iq, struct sge_rxq, iq));
546 }
547
548
549 #ifdef TCP_OFFLOAD
550 /* ofld_rxq: SGE ingress queue + SGE free list + miscellaneous items */
551 struct sge_ofld_rxq {
552         struct sge_iq iq;       /* MUST be first */
553         struct sge_fl fl;       /* MUST follow iq */
554 } __aligned(CACHE_LINE_SIZE);
555
556 static inline struct sge_ofld_rxq *
557 iq_to_ofld_rxq(struct sge_iq *iq)
558 {
559
560         return (__containerof(iq, struct sge_ofld_rxq, iq));
561 }
562 #endif
563
564 struct wrqe {
565         STAILQ_ENTRY(wrqe) link;
566         struct sge_wrq *wrq;
567         int wr_len;
568         uint64_t wr[] __aligned(16);
569 };
570
571 /*
572  * wrq: SGE egress queue that is given prebuilt work requests.  Both the control
573  * and offload tx queues are of this type.
574  */
575 struct sge_wrq {
576         struct sge_eq eq;       /* MUST be first */
577
578         struct adapter *adapter;
579
580         /* List of WRs held up due to lack of tx descriptors */
581         STAILQ_HEAD(, wrqe) wr_list;
582
583         /* stats for common events first */
584
585         uint64_t tx_wrs;        /* # of tx work requests */
586
587         /* stats for not-that-common events */
588
589         uint32_t no_desc;       /* out of hardware descriptors */
590 } __aligned(CACHE_LINE_SIZE);
591
592
593 #ifdef DEV_NETMAP
594 struct sge_nm_rxq {
595         struct port_info *pi;
596
597         struct iq_desc *iq_desc;
598         uint16_t iq_abs_id;
599         uint16_t iq_cntxt_id;
600         uint16_t iq_cidx;
601         uint16_t iq_sidx;
602         uint8_t iq_gen;
603
604         __be64  *fl_desc;
605         uint16_t fl_cntxt_id;
606         uint32_t fl_cidx;
607         uint32_t fl_pidx;
608         uint32_t fl_sidx;
609         uint32_t fl_db_val;
610         u_int fl_hwidx:4;
611
612         u_int nid;              /* netmap ring # for this queue */
613
614         /* infrequently used items after this */
615
616         bus_dma_tag_t iq_desc_tag;
617         bus_dmamap_t iq_desc_map;
618         bus_addr_t iq_ba;
619         int intr_idx;
620
621         bus_dma_tag_t fl_desc_tag;
622         bus_dmamap_t fl_desc_map;
623         bus_addr_t fl_ba;
624 } __aligned(CACHE_LINE_SIZE);
625
626 struct sge_nm_txq {
627         struct tx_desc *desc;
628         uint16_t cidx;
629         uint16_t pidx;
630         uint16_t sidx;
631         uint16_t equiqidx;      /* EQUIQ last requested at this pidx */
632         uint16_t equeqidx;      /* EQUEQ last requested at this pidx */
633         uint16_t dbidx;         /* pidx of the most recent doorbell */
634         uint16_t doorbells;
635         volatile uint32_t *udb;
636         u_int udb_qid;
637         u_int cntxt_id;
638         __be32 cpl_ctrl0;       /* for convenience */
639         u_int nid;              /* netmap ring # for this queue */
640
641         /* infrequently used items after this */
642
643         bus_dma_tag_t desc_tag;
644         bus_dmamap_t desc_map;
645         bus_addr_t ba;
646         int iqidx;
647 } __aligned(CACHE_LINE_SIZE);
648 #endif
649
650 struct sge {
651         int timer_val[SGE_NTIMERS];
652         int counter_val[SGE_NCOUNTERS];
653         int fl_starve_threshold;
654         int fl_starve_threshold2;
655         int eq_s_qpp;
656         int iq_s_qpp;
657
658         int nrxq;       /* total # of Ethernet rx queues */
659         int ntxq;       /* total # of Ethernet tx tx queues */
660 #ifdef TCP_OFFLOAD
661         int nofldrxq;   /* total # of TOE rx queues */
662         int nofldtxq;   /* total # of TOE tx queues */
663 #endif
664 #ifdef DEV_NETMAP
665         int nnmrxq;     /* total # of netmap rx queues */
666         int nnmtxq;     /* total # of netmap tx queues */
667 #endif
668         int niq;        /* total # of ingress queues */
669         int neq;        /* total # of egress queues */
670
671         struct sge_iq fwq;      /* Firmware event queue */
672         struct sge_wrq mgmtq;   /* Management queue (control queue) */
673         struct sge_wrq *ctrlq;  /* Control queues */
674         struct sge_txq *txq;    /* NIC tx queues */
675         struct sge_rxq *rxq;    /* NIC rx queues */
676 #ifdef TCP_OFFLOAD
677         struct sge_wrq *ofld_txq;       /* TOE tx queues */
678         struct sge_ofld_rxq *ofld_rxq;  /* TOE rx queues */
679 #endif
680 #ifdef DEV_NETMAP
681         struct sge_nm_txq *nm_txq;      /* netmap tx queues */
682         struct sge_nm_rxq *nm_rxq;      /* netmap rx queues */
683 #endif
684
685         uint16_t iq_start;
686         int eq_start;
687         struct sge_iq **iqmap;  /* iq->cntxt_id to iq mapping */
688         struct sge_eq **eqmap;  /* eq->cntxt_id to eq mapping */
689
690         int pad_boundary;
691         int pack_boundary;
692         int8_t safe_hwidx1;     /* may not have room for metadata */
693         int8_t safe_hwidx2;     /* with room for metadata and maybe more */
694         struct sw_zone_info sw_zone_info[SW_ZONE_SIZES];
695         struct hw_buf_info hw_buf_info[SGE_FLBUF_SIZES];
696 };
697
698 struct rss_header;
699 typedef int (*cpl_handler_t)(struct sge_iq *, const struct rss_header *,
700     struct mbuf *);
701 typedef int (*an_handler_t)(struct sge_iq *, const struct rsp_ctrl *);
702 typedef int (*fw_msg_handler_t)(struct adapter *, const __be64 *);
703
704 struct adapter {
705         SLIST_ENTRY(adapter) link;
706         device_t dev;
707         struct cdev *cdev;
708
709         /* PCIe register resources */
710         int regs_rid;
711         struct resource *regs_res;
712         int msix_rid;
713         struct resource *msix_res;
714         bus_space_handle_t bh;
715         bus_space_tag_t bt;
716         bus_size_t mmio_len;
717         int udbs_rid;
718         struct resource *udbs_res;
719         volatile uint8_t *udbs_base;
720
721         unsigned int pf;
722         unsigned int mbox;
723
724         /* Interrupt information */
725         int intr_type;
726         int intr_count;
727         struct irq {
728                 struct resource *res;
729                 int rid;
730                 void *tag;
731         } *irq;
732
733         bus_dma_tag_t dmat;     /* Parent DMA tag */
734
735         struct sge sge;
736         int lro_timeout;
737
738         struct taskqueue *tq[NCHAN];    /* taskqueues that flush data out */
739         struct port_info *port[MAX_NPORTS];
740         uint8_t chan_map[NCHAN];
741
742 #ifdef TCP_OFFLOAD
743         void *tom_softc;        /* (struct tom_data *) */
744         struct tom_tunables tt;
745         void *iwarp_softc;      /* (struct c4iw_dev *) */
746         void *iscsi_softc;
747 #endif
748         struct l2t_data *l2t;   /* L2 table */
749         struct tid_info tids;
750
751         uint16_t doorbells;
752         int open_device_map;
753 #ifdef TCP_OFFLOAD
754         int offload_map;
755 #endif
756         int flags;
757
758         char ifp_lockname[16];
759         struct mtx ifp_lock;
760         struct ifnet *ifp;      /* tracer ifp */
761         struct ifmedia media;
762         int traceq;             /* iq used by all tracers, -1 if none */
763         int tracer_valid;       /* bitmap of valid tracers */
764         int tracer_enabled;     /* bitmap of enabled tracers */
765
766         char fw_version[32];
767         char cfg_file[32];
768         u_int cfcsum;
769         struct adapter_params params;
770         struct t4_virt_res vres;
771
772         uint16_t linkcaps;
773         uint16_t niccaps;
774         uint16_t toecaps;
775         uint16_t rdmacaps;
776         uint16_t iscsicaps;
777         uint16_t fcoecaps;
778
779         struct sysctl_ctx_list ctx; /* from adapter_full_init to full_uninit */
780
781         struct mtx sc_lock;
782         char lockname[16];
783
784         /* Starving free lists */
785         struct mtx sfl_lock;    /* same cache-line as sc_lock? but that's ok */
786         TAILQ_HEAD(, sge_fl) sfl;
787         struct callout sfl_callout;
788
789         an_handler_t an_handler __aligned(CACHE_LINE_SIZE);
790         fw_msg_handler_t fw_msg_handler[5];     /* NUM_FW6_TYPES */
791         cpl_handler_t cpl_handler[0xef];        /* NUM_CPL_CMDS */
792
793 #ifdef INVARIANTS
794         const char *last_op;
795         const void *last_op_thr;
796 #endif
797
798         int sc_do_rxcopy;
799 };
800
801 #define ADAPTER_LOCK(sc)                mtx_lock(&(sc)->sc_lock)
802 #define ADAPTER_UNLOCK(sc)              mtx_unlock(&(sc)->sc_lock)
803 #define ADAPTER_LOCK_ASSERT_OWNED(sc)   mtx_assert(&(sc)->sc_lock, MA_OWNED)
804 #define ADAPTER_LOCK_ASSERT_NOTOWNED(sc) mtx_assert(&(sc)->sc_lock, MA_NOTOWNED)
805
806 #define ASSERT_SYNCHRONIZED_OP(sc)      \
807     KASSERT(IS_BUSY(sc) && \
808         (mtx_owned(&(sc)->sc_lock) || sc->last_op_thr == curthread), \
809         ("%s: operation not synchronized.", __func__))
810
811 #define PORT_LOCK(pi)                   mtx_lock(&(pi)->pi_lock)
812 #define PORT_UNLOCK(pi)                 mtx_unlock(&(pi)->pi_lock)
813 #define PORT_LOCK_ASSERT_OWNED(pi)      mtx_assert(&(pi)->pi_lock, MA_OWNED)
814 #define PORT_LOCK_ASSERT_NOTOWNED(pi)   mtx_assert(&(pi)->pi_lock, MA_NOTOWNED)
815
816 #define FL_LOCK(fl)                     mtx_lock(&(fl)->fl_lock)
817 #define FL_TRYLOCK(fl)                  mtx_trylock(&(fl)->fl_lock)
818 #define FL_UNLOCK(fl)                   mtx_unlock(&(fl)->fl_lock)
819 #define FL_LOCK_ASSERT_OWNED(fl)        mtx_assert(&(fl)->fl_lock, MA_OWNED)
820 #define FL_LOCK_ASSERT_NOTOWNED(fl)     mtx_assert(&(fl)->fl_lock, MA_NOTOWNED)
821
822 #define RXQ_FL_LOCK(rxq)                FL_LOCK(&(rxq)->fl)
823 #define RXQ_FL_UNLOCK(rxq)              FL_UNLOCK(&(rxq)->fl)
824 #define RXQ_FL_LOCK_ASSERT_OWNED(rxq)   FL_LOCK_ASSERT_OWNED(&(rxq)->fl)
825 #define RXQ_FL_LOCK_ASSERT_NOTOWNED(rxq) FL_LOCK_ASSERT_NOTOWNED(&(rxq)->fl)
826
827 #define EQ_LOCK(eq)                     mtx_lock(&(eq)->eq_lock)
828 #define EQ_TRYLOCK(eq)                  mtx_trylock(&(eq)->eq_lock)
829 #define EQ_UNLOCK(eq)                   mtx_unlock(&(eq)->eq_lock)
830 #define EQ_LOCK_ASSERT_OWNED(eq)        mtx_assert(&(eq)->eq_lock, MA_OWNED)
831 #define EQ_LOCK_ASSERT_NOTOWNED(eq)     mtx_assert(&(eq)->eq_lock, MA_NOTOWNED)
832
833 #define TXQ_LOCK(txq)                   EQ_LOCK(&(txq)->eq)
834 #define TXQ_TRYLOCK(txq)                EQ_TRYLOCK(&(txq)->eq)
835 #define TXQ_UNLOCK(txq)                 EQ_UNLOCK(&(txq)->eq)
836 #define TXQ_LOCK_ASSERT_OWNED(txq)      EQ_LOCK_ASSERT_OWNED(&(txq)->eq)
837 #define TXQ_LOCK_ASSERT_NOTOWNED(txq)   EQ_LOCK_ASSERT_NOTOWNED(&(txq)->eq)
838
839 #define for_each_txq(pi, iter, q) \
840         for (q = &pi->adapter->sge.txq[pi->first_txq], iter = 0; \
841             iter < pi->ntxq; ++iter, ++q)
842 #define for_each_rxq(pi, iter, q) \
843         for (q = &pi->adapter->sge.rxq[pi->first_rxq], iter = 0; \
844             iter < pi->nrxq; ++iter, ++q)
845 #define for_each_ofld_txq(pi, iter, q) \
846         for (q = &pi->adapter->sge.ofld_txq[pi->first_ofld_txq], iter = 0; \
847             iter < pi->nofldtxq; ++iter, ++q)
848 #define for_each_ofld_rxq(pi, iter, q) \
849         for (q = &pi->adapter->sge.ofld_rxq[pi->first_ofld_rxq], iter = 0; \
850             iter < pi->nofldrxq; ++iter, ++q)
851 #define for_each_nm_txq(pi, iter, q) \
852         for (q = &pi->adapter->sge.nm_txq[pi->first_nm_txq], iter = 0; \
853             iter < pi->nnmtxq; ++iter, ++q)
854 #define for_each_nm_rxq(pi, iter, q) \
855         for (q = &pi->adapter->sge.nm_rxq[pi->first_nm_rxq], iter = 0; \
856             iter < pi->nnmrxq; ++iter, ++q)
857
858 #define IDXINCR(idx, incr, wrap) do { \
859         idx = wrap - idx > incr ? idx + incr : incr - (wrap - idx); \
860 } while (0)
861 #define IDXDIFF(head, tail, wrap) \
862         ((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
863
864 /* One for errors, one for firmware events */
865 #define T4_EXTRA_INTR 2
866
867 static inline uint32_t
868 t4_read_reg(struct adapter *sc, uint32_t reg)
869 {
870
871         return bus_space_read_4(sc->bt, sc->bh, reg);
872 }
873
874 static inline void
875 t4_write_reg(struct adapter *sc, uint32_t reg, uint32_t val)
876 {
877
878         bus_space_write_4(sc->bt, sc->bh, reg, val);
879 }
880
881 static inline uint64_t
882 t4_read_reg64(struct adapter *sc, uint32_t reg)
883 {
884
885         return t4_bus_space_read_8(sc->bt, sc->bh, reg);
886 }
887
888 static inline void
889 t4_write_reg64(struct adapter *sc, uint32_t reg, uint64_t val)
890 {
891
892         t4_bus_space_write_8(sc->bt, sc->bh, reg, val);
893 }
894
895 static inline void
896 t4_os_pci_read_cfg1(struct adapter *sc, int reg, uint8_t *val)
897 {
898
899         *val = pci_read_config(sc->dev, reg, 1);
900 }
901
902 static inline void
903 t4_os_pci_write_cfg1(struct adapter *sc, int reg, uint8_t val)
904 {
905
906         pci_write_config(sc->dev, reg, val, 1);
907 }
908
909 static inline void
910 t4_os_pci_read_cfg2(struct adapter *sc, int reg, uint16_t *val)
911 {
912
913         *val = pci_read_config(sc->dev, reg, 2);
914 }
915
916 static inline void
917 t4_os_pci_write_cfg2(struct adapter *sc, int reg, uint16_t val)
918 {
919
920         pci_write_config(sc->dev, reg, val, 2);
921 }
922
923 static inline void
924 t4_os_pci_read_cfg4(struct adapter *sc, int reg, uint32_t *val)
925 {
926
927         *val = pci_read_config(sc->dev, reg, 4);
928 }
929
930 static inline void
931 t4_os_pci_write_cfg4(struct adapter *sc, int reg, uint32_t val)
932 {
933
934         pci_write_config(sc->dev, reg, val, 4);
935 }
936
937 static inline struct port_info *
938 adap2pinfo(struct adapter *sc, int idx)
939 {
940
941         return (sc->port[idx]);
942 }
943
944 static inline void
945 t4_os_set_hw_addr(struct adapter *sc, int idx, uint8_t hw_addr[])
946 {
947
948         bcopy(hw_addr, sc->port[idx]->hw_addr, ETHER_ADDR_LEN);
949 }
950
951 static inline bool
952 is_10G_port(const struct port_info *pi)
953 {
954
955         return ((pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G) != 0);
956 }
957
958 static inline bool
959 is_40G_port(const struct port_info *pi)
960 {
961
962         return ((pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G) != 0);
963 }
964
965 static inline int
966 tx_resume_threshold(struct sge_eq *eq)
967 {
968
969         return (eq->qsize / 4);
970 }
971
972 /* t4_main.c */
973 void t4_tx_task(void *, int);
974 void t4_tx_callout(void *);
975 int t4_os_find_pci_capability(struct adapter *, int);
976 int t4_os_pci_save_state(struct adapter *);
977 int t4_os_pci_restore_state(struct adapter *);
978 void t4_os_portmod_changed(const struct adapter *, int);
979 void t4_os_link_changed(struct adapter *, int, int, int);
980 void t4_iterate(void (*)(struct adapter *, void *), void *);
981 int t4_register_cpl_handler(struct adapter *, int, cpl_handler_t);
982 int t4_register_an_handler(struct adapter *, an_handler_t);
983 int t4_register_fw_msg_handler(struct adapter *, int, fw_msg_handler_t);
984 int t4_filter_rpl(struct sge_iq *, const struct rss_header *, struct mbuf *);
985 int begin_synchronized_op(struct adapter *, struct port_info *, int, char *);
986 void end_synchronized_op(struct adapter *, int);
987 int update_mac_settings(struct ifnet *, int);
988 int adapter_full_init(struct adapter *);
989 int adapter_full_uninit(struct adapter *);
990 int port_full_init(struct port_info *);
991 int port_full_uninit(struct port_info *);
992
993 #ifdef DEV_NETMAP
994 /* t4_netmap.c */
995 int create_netmap_ifnet(struct port_info *);
996 int destroy_netmap_ifnet(struct port_info *);
997 void t4_nm_intr(void *);
998 #endif
999
1000 /* t4_sge.c */
1001 void t4_sge_modload(void);
1002 void t4_sge_modunload(void);
1003 uint64_t t4_sge_extfree_refs(void);
1004 void t4_init_sge_cpl_handlers(struct adapter *);
1005 void t4_tweak_chip_settings(struct adapter *);
1006 int t4_read_chip_settings(struct adapter *);
1007 int t4_create_dma_tag(struct adapter *);
1008 void t4_sge_sysctls(struct adapter *, struct sysctl_ctx_list *,
1009     struct sysctl_oid_list *);
1010 int t4_destroy_dma_tag(struct adapter *);
1011 int t4_setup_adapter_queues(struct adapter *);
1012 int t4_teardown_adapter_queues(struct adapter *);
1013 int t4_setup_port_queues(struct port_info *);
1014 int t4_teardown_port_queues(struct port_info *);
1015 int t4_alloc_tx_maps(struct tx_maps *, bus_dma_tag_t, int, int);
1016 void t4_free_tx_maps(struct tx_maps *, bus_dma_tag_t);
1017 void t4_intr_all(void *);
1018 void t4_intr(void *);
1019 void t4_intr_err(void *);
1020 void t4_intr_evt(void *);
1021 void t4_wrq_tx_locked(struct adapter *, struct sge_wrq *, struct wrqe *);
1022 int t4_eth_tx(struct ifnet *, struct sge_txq *, struct mbuf *);
1023 void t4_update_fl_bufsize(struct ifnet *);
1024 int can_resume_tx(struct sge_eq *);
1025
1026 /* t4_tracer.c */
1027 struct t4_tracer;
1028 void t4_tracer_modload(void);
1029 void t4_tracer_modunload(void);
1030 void t4_tracer_port_detach(struct adapter *);
1031 int t4_get_tracer(struct adapter *, struct t4_tracer *);
1032 int t4_set_tracer(struct adapter *, struct t4_tracer *);
1033 int t4_trace_pkt(struct sge_iq *, const struct rss_header *, struct mbuf *);
1034 int t5_trace_pkt(struct sge_iq *, const struct rss_header *, struct mbuf *);
1035
1036 static inline struct wrqe *
1037 alloc_wrqe(int wr_len, struct sge_wrq *wrq)
1038 {
1039         int len = offsetof(struct wrqe, wr) + wr_len;
1040         struct wrqe *wr;
1041
1042         wr = malloc(len, M_CXGBE, M_NOWAIT);
1043         if (__predict_false(wr == NULL))
1044                 return (NULL);
1045         wr->wr_len = wr_len;
1046         wr->wrq = wrq;
1047         return (wr);
1048 }
1049
1050 static inline void *
1051 wrtod(struct wrqe *wr)
1052 {
1053         return (&wr->wr[0]);
1054 }
1055
1056 static inline void
1057 free_wrqe(struct wrqe *wr)
1058 {
1059         free(wr, M_CXGBE);
1060 }
1061
1062 static inline void
1063 t4_wrq_tx(struct adapter *sc, struct wrqe *wr)
1064 {
1065         struct sge_wrq *wrq = wr->wrq;
1066
1067         TXQ_LOCK(wrq);
1068         t4_wrq_tx_locked(sc, wrq, wr);
1069         TXQ_UNLOCK(wrq);
1070 }
1071
1072 #endif