Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / pci / cxgb_adapter.h
blob1b1e4f89b830cef36979c31abd2bcf1576fdba04
1 /**************************************************************************
3 Copyright (c) 2007, Chelsio Inc.
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
12 2. Neither the name of the Chelsio Corporation nor the names of its
13 contributors may be used to endorse or promote products derived from
14 this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
29 $FreeBSD: src/sys/dev/cxgb/cxgb_adapter.h,v 1.20 2007/09/10 00:59:51 kmacy Exp $
31 ***************************************************************************/
35 #ifndef _CXGB_ADAPTER_H_
36 #define _CXGB_ADAPTER_H_
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #ifdef __FreeBSD__
41 #include <sys/sx.h>
42 #include <sys/rman.h>
43 #endif
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
48 #ifdef __FreeBSD__
49 #include <net/ethernet.h>
50 #endif
51 #include <net/if.h>
52 #ifdef __NetBSD__
53 #include <net/if_ether.h>
54 #endif
55 #include <net/if_media.h>
57 #include <machine/bus.h>
58 #ifdef __FreeBSD__
59 #include <machine/resource.h>
60 #include <sys/bus_dma.h>
61 #endif
62 #include <dev/pci/pcireg.h>
63 #include <dev/pci/pcivar.h>
65 #ifdef CONFIG_DEFINED
66 #include <cxgb_osdep.h>
67 #include <ulp/toecore/toedev.h>
68 #include <sys/mbufq.h>
69 #else
70 #ifdef __FreeBSD__
71 #include <dev/cxgb/cxgb_osdep.h>
72 #include <dev/cxgb/sys/mbufq.h>
73 #include <dev/cxgb/ulp/toecore/toedev.h>
74 #endif
75 #ifdef __NetBSD__
76 #include "cxgb_osdep.h"
77 #include "cxgb_mbuf.h"
78 #include "cxgb_toedev.h"
79 #endif
80 #endif
82 #ifdef __FreeBSD__
83 #define USE_SX
84 #endif
86 struct adapter;
87 struct sge_qset;
88 extern int cxgb_debug;
90 #ifdef DEBUG_LOCKING
91 #define MTX_INIT(lock, lockname, class, flags) \
92 do { \
93 printf("initializing %s at %s:%d\n", lockname, __FILE__, __LINE__); \
94 mtx_init((lock), lockname, class, flags); \
95 } while (0)
97 #define MTX_DESTROY(lock) \
98 do { \
99 printf("destroying %s at %s:%d\n", (lock)->lock_object.lo_name, __FILE__, __LINE__); \
100 mtx_destroy((lock)); \
101 } while (0)
103 #define SX_INIT(lock, lockname) \
104 do { \
105 printf("initializing %s at %s:%d\n", lockname, __FILE__, __LINE__); \
106 sx_init((lock), lockname); \
107 } while (0)
109 #define SX_DESTROY(lock) \
110 do { \
111 printf("destroying %s at %s:%d\n", (lock)->lock_object.lo_name, __FILE__, __LINE__); \
112 sx_destroy((lock)); \
113 } while (0)
114 #else
115 #define MTX_INIT mtx_init
116 #define MTX_DESTROY mtx_destroy
117 #define SX_INIT sx_init
118 #define SX_DESTROY sx_destroy
119 #endif
121 #ifdef __NetBSD__
122 struct port_device {
123 struct device original;
124 device_t dev;
125 struct adapter *parent;
126 int port_number;
128 #endif
130 struct port_info {
131 struct adapter *adapter;
132 struct ifnet *ifp;
133 #ifdef __NetBSD__
134 struct port_device *pd;
135 int port;
136 #endif
137 int if_flags;
138 const struct port_type_info *port_type;
139 struct cphy phy;
140 struct cmac mac;
141 struct link_config link_config;
142 struct ifmedia media;
143 #ifdef USE_SX
144 struct sx lock;
145 #else
146 struct mtx lock;
147 #endif
148 uint8_t port_id;
149 uint8_t tx_chan;
150 uint8_t txpkt_intf;
151 uint8_t nqsets;
152 uint8_t first_qset;
154 uint8_t hw_addr[ETHER_ADDR_LEN];
155 #ifdef __FreeBSD__
156 struct taskqueue *tq;
157 struct task start_task;
158 struct task timer_reclaim_task;
159 #endif
160 #ifdef __NetBSD__
161 struct cxgb_task start_task;
162 struct cxgb_task timer_reclaim_task;
163 #endif
164 struct cdev *port_cdev;
166 #define PORT_NAME_LEN 32
167 #define TASKQ_NAME_LEN 32
168 char lockbuf[PORT_NAME_LEN];
169 char taskqbuf[TASKQ_NAME_LEN];
172 enum { /* adapter flags */
173 FULL_INIT_DONE = (1 << 0),
174 USING_MSI = (1 << 1),
175 USING_MSIX = (1 << 2),
176 QUEUES_BOUND = (1 << 3),
177 FW_UPTODATE = (1 << 4),
178 TPS_UPTODATE = (1 << 5),
182 #define FL_Q_SIZE 4096
183 #define JUMBO_Q_SIZE 512
184 #define RSPQ_Q_SIZE 1024
185 #define TX_ETH_Q_SIZE 1024
190 * Types of Tx queues in each queue set. Order here matters, do not change.
191 * XXX TOE is not implemented yet, so the extra queues are just placeholders.
193 enum { TXQ_ETH, TXQ_OFLD, TXQ_CTRL };
196 /* careful, the following are set on priv_flags and must not collide with
197 * IFF_ flags!
199 enum {
200 LRO_ACTIVE = (1 << 8),
203 /* Max concurrent LRO sessions per queue set */
204 #define MAX_LRO_SES 8
206 struct t3_lro_session {
207 struct mbuf *head;
208 struct mbuf *tail;
209 uint32_t seq;
210 uint16_t ip_len;
211 uint16_t mss;
212 uint16_t vtag;
213 uint8_t npkts;
216 struct lro_state {
217 unsigned short enabled;
218 unsigned short active_idx;
219 unsigned int nactive;
220 struct t3_lro_session sess[MAX_LRO_SES];
223 #define RX_BUNDLE_SIZE 8
225 struct rsp_desc;
227 struct sge_rspq {
228 uint32_t credits;
229 uint32_t size;
230 uint32_t cidx;
231 uint32_t gen;
232 uint32_t polling;
233 uint32_t holdoff_tmr;
234 uint32_t next_holdoff;
235 uint32_t imm_data;
236 struct rsp_desc *desc;
237 uint32_t cntxt_id;
238 struct mtx lock;
239 struct mbuf *rx_head; /* offload packet receive queue head */
240 struct mbuf *rx_tail; /* offload packet receive queue tail */
242 uint32_t offload_pkts;
243 uint32_t offload_bundles;
244 uint32_t pure_rsps;
245 uint32_t unhandled_irqs;
247 bus_addr_t phys_addr;
248 bus_dma_tag_t desc_tag;
249 bus_dmamap_t desc_map;
251 struct t3_mbuf_hdr rspq_mh;
252 #define RSPQ_NAME_LEN 32
253 char lockbuf[RSPQ_NAME_LEN];
257 #ifndef DISABLE_MBUF_IOVEC
258 #define rspq_mbuf rspq_mh.mh_head
259 #endif
261 struct rx_desc;
262 struct rx_sw_desc;
264 struct sge_fl {
265 uint32_t buf_size;
266 uint32_t credits;
267 uint32_t size;
268 uint32_t cidx;
269 uint32_t pidx;
270 uint32_t gen;
271 struct rx_desc *desc;
272 struct rx_sw_desc *sdesc;
273 bus_addr_t phys_addr;
274 uint32_t cntxt_id;
275 uint64_t empty;
276 bus_dma_tag_t desc_tag;
277 bus_dmamap_t desc_map;
278 bus_dma_tag_t entry_tag;
279 #ifdef __FreeBSD__
280 uma_zone_t zone;
281 #endif
282 int type;
285 struct tx_desc;
286 struct tx_sw_desc;
288 #define TXQ_TRANSMITTING 0x1
290 struct sge_txq {
291 uint64_t flags;
292 uint32_t in_use;
293 uint32_t size;
294 uint32_t processed;
295 uint32_t cleaned;
296 uint32_t stop_thres;
297 uint32_t cidx;
298 uint32_t pidx;
299 uint32_t gen;
300 uint32_t unacked;
301 struct tx_desc *desc;
302 struct tx_sw_desc *sdesc;
303 uint32_t token;
304 bus_addr_t phys_addr;
305 #ifdef __FreeBSD__
306 struct task qresume_task;
307 struct task qreclaim_task;
308 #endif
309 #ifdef __NetBSD__
310 struct cxgb_task qresume_task;
311 struct cxgb_task qreclaim_task;
312 #endif
313 struct port_info *port;
314 uint32_t cntxt_id;
315 uint64_t stops;
316 uint64_t restarts;
317 bus_dma_tag_t desc_tag;
318 bus_dmamap_t desc_map;
319 bus_dma_tag_t entry_tag;
320 struct mbuf_head sendq;
321 struct mtx lock;
322 #define TXQ_NAME_LEN 32
323 char lockbuf[TXQ_NAME_LEN];
327 enum {
328 SGE_PSTAT_TSO, /* # of TSO requests */
329 SGE_PSTAT_RX_CSUM_GOOD, /* # of successful RX csum offloads */
330 SGE_PSTAT_TX_CSUM, /* # of TX checksum offloads */
331 SGE_PSTAT_VLANEX, /* # of VLAN tag extractions */
332 SGE_PSTAT_VLANINS, /* # of VLAN tag insertions */
333 SGE_PSTATS_LRO_QUEUED, /* # of LRO appended packets */
334 SGE_PSTATS_LRO_FLUSHED, /* # of LRO flushed packets */
335 SGE_PSTATS_LRO_X_STREAMS, /* # of exceeded LRO contexts */
338 #define SGE_PSTAT_MAX (SGE_PSTATS_LRO_X_STREAMS+1)
340 struct sge_qset {
341 struct sge_rspq rspq;
342 struct sge_fl fl[SGE_RXQ_PER_SET];
343 struct lro_state lro;
344 struct sge_txq txq[SGE_TXQ_PER_SET];
345 uint32_t txq_stopped; /* which Tx queues are stopped */
346 uint64_t port_stats[SGE_PSTAT_MAX];
347 struct port_info *port;
348 int idx; /* qset # */
351 struct sge {
352 struct sge_qset qs[SGE_QSETS];
353 struct mtx reg_lock;
356 struct filter_info;
358 struct adapter {
359 #ifdef __NetBSD__
360 struct device original;
361 #endif
362 device_t dev; // so we have a compatible pointer
363 int flags;
364 TAILQ_ENTRY(adapter) adapter_entry;
366 /* PCI register resources */
367 int regs_rid;
368 struct resource *regs_res;
369 bus_space_handle_t bh;
370 bus_space_tag_t bt;
371 bus_size_t mmio_len;
372 uint32_t link_width;
373 #ifdef __NetBSD__
374 struct pci_attach_args pa;
375 uint32_t bar0;
376 bus_space_handle_t bar0_handle;
377 pci_intr_handle_t intr_handle;
378 void *intr_cookie;
379 #endif
381 /* DMA resources */
382 bus_dma_tag_t parent_dmat;
383 bus_dma_tag_t rx_dmat;
384 bus_dma_tag_t rx_jumbo_dmat;
385 bus_dma_tag_t tx_dmat;
387 /* Interrupt resources */
388 #ifdef __FreeBSD__
389 struct resource *irq_res;
390 #endif
391 int irq_rid;
392 #ifdef __FreeBSD__
393 void *intr_tag;
394 #endif
396 uint32_t msix_regs_rid;
397 struct resource *msix_regs_res;
399 struct resource *msix_irq_res[SGE_QSETS];
400 int msix_irq_rid[SGE_QSETS];
401 void *msix_intr_tag[SGE_QSETS];
402 uint8_t rxpkt_map[8]; /* maps RX_PKT interface values to port ids */
403 uint8_t rrss_map[SGE_QSETS]; /* revers RSS map table */
405 struct filter_info *filters;
407 /* Tasks */
408 #ifdef __FreeBSD__
409 struct task ext_intr_task;
410 struct task slow_intr_task;
411 struct task tick_task;
412 struct task process_responses_task;
413 struct taskqueue *tq;
414 #endif
415 #ifdef __NetBSD__
416 struct cxgb_task ext_intr_task;
417 struct cxgb_task slow_intr_task;
418 struct cxgb_task tick_task;
419 #endif
420 struct callout cxgb_tick_ch;
421 struct callout sge_timer_ch;
423 /* Register lock for use by the hardware layer */
424 struct mtx mdio_lock;
425 struct mtx elmer_lock;
427 /* Bookkeeping for the hardware layer */
428 struct adapter_params params;
429 unsigned int slow_intr_mask;
430 unsigned long irq_stats[IRQ_NUM_STATS];
432 struct sge sge;
433 struct mc7 pmrx;
434 struct mc7 pmtx;
435 struct mc7 cm;
436 struct mc5 mc5;
438 struct port_info port[MAX_NPORTS];
439 device_t portdev[MAX_NPORTS];
440 struct toedev tdev;
441 char fw_version[64];
442 uint32_t open_device_map;
443 uint32_t registered_device_map;
444 #ifdef USE_SX
445 struct sx lock;
446 #else
447 struct mtx lock;
448 #endif
449 #ifdef __FreeBSD__
450 driver_intr_t *cxgb_intr;
451 #endif
452 #ifdef __NetBSD__
453 int (*cxgb_intr)(void *);
454 #endif
455 int msi_count;
457 #define ADAPTER_LOCK_NAME_LEN 32
458 char lockbuf[ADAPTER_LOCK_NAME_LEN];
459 char reglockbuf[ADAPTER_LOCK_NAME_LEN];
460 char mdiolockbuf[ADAPTER_LOCK_NAME_LEN];
461 char elmerlockbuf[ADAPTER_LOCK_NAME_LEN];
464 struct t3_rx_mode {
466 uint32_t idx;
467 struct port_info *port;
471 #define MDIO_LOCK(adapter) mtx_lock(&(adapter)->mdio_lock)
472 #define MDIO_UNLOCK(adapter) mtx_unlock(&(adapter)->mdio_lock)
473 #define ELMR_LOCK(adapter) mtx_lock(&(adapter)->elmer_lock)
474 #define ELMR_UNLOCK(adapter) mtx_unlock(&(adapter)->elmer_lock)
477 #ifdef USE_SX
478 #define PORT_LOCK(port) sx_xlock(&(port)->lock);
479 #define PORT_UNLOCK(port) sx_xunlock(&(port)->lock);
480 #define PORT_LOCK_INIT(port, name) SX_INIT(&(port)->lock, name)
481 #define PORT_LOCK_DEINIT(port) SX_DESTROY(&(port)->lock)
482 #define PORT_LOCK_ASSERT_OWNED(port) sx_assert(&(port)->lock, SA_LOCKED)
484 #define ADAPTER_LOCK(adap) sx_xlock(&(adap)->lock);
485 #define ADAPTER_UNLOCK(adap) sx_xunlock(&(adap)->lock);
486 #define ADAPTER_LOCK_INIT(adap, name) SX_INIT(&(adap)->lock, name)
487 #define ADAPTER_LOCK_DEINIT(adap) SX_DESTROY(&(adap)->lock)
488 #define ADAPTER_LOCK_ASSERT_NOTOWNED(adap) sx_assert(&(adap)->lock, SA_UNLOCKED)
489 #else
490 #define PORT_LOCK(port) mtx_lock(&(port)->lock);
491 #define PORT_UNLOCK(port) mtx_unlock(&(port)->lock);
492 #define PORT_LOCK_INIT(port, name) mtx_init(&(port)->lock, name, 0, MTX_DEF)
493 #define PORT_LOCK_DEINIT(port) mtx_destroy(&(port)->lock)
494 #define PORT_LOCK_ASSERT_OWNED(port) mtx_assert(&(port)->lock, MA_OWNED)
496 #define ADAPTER_LOCK(adap) mtx_lock(&(adap)->lock);
497 #define ADAPTER_UNLOCK(adap) mtx_unlock(&(adap)->lock);
498 #define ADAPTER_LOCK_INIT(adap, name) mtx_init(&(adap)->lock, name, 0, MTX_DEF)
499 #define ADAPTER_LOCK_DEINIT(adap) mtx_destroy(&(adap)->lock)
500 #define ADAPTER_LOCK_ASSERT_NOTOWNED(adap) mtx_assert(&(adap)->lock, MA_NOTOWNED)
501 #endif
504 static __inline uint32_t
505 t3_read_reg(adapter_t *adapter, uint32_t reg_addr)
507 return (bus_space_read_4(adapter->bt, adapter->bh, reg_addr));
510 static __inline void
511 t3_write_reg(adapter_t *adapter, uint32_t reg_addr, uint32_t val)
513 bus_space_write_4(adapter->bt, adapter->bh, reg_addr, val);
516 static __inline void
517 t3_os_pci_read_config_4(adapter_t *adapter, int reg, uint32_t *val)
519 #ifdef __FreeBSD__
520 *val = pci_read_config(adapter->dev, reg, 4);
521 #endif
522 #ifdef __NetBSD__
523 *val = pci_conf_read(adapter->pa.pa_pc, adapter->pa.pa_tag, reg);
524 #endif
527 static __inline void
528 t3_os_pci_write_config_4(adapter_t *adapter, int reg, uint32_t val)
530 #ifdef __FreeBSD__
531 pci_write_config(adapter->dev, reg, val, 4);
532 #endif
533 #ifdef __NetBSD__
534 pci_conf_write(adapter->pa.pa_pc, adapter->pa.pa_tag, reg, val);
535 #endif
538 static __inline void
539 t3_os_pci_read_config_2(adapter_t *adapter, int reg, uint16_t *val)
541 #ifdef __FreeBSD__
542 *val = pci_read_config(adapter->dev, reg, 2);
543 #endif
544 #ifdef __NetBSD__
545 uint32_t temp;
546 temp = pci_conf_read(adapter->pa.pa_pc, adapter->pa.pa_tag, reg&0xfc);
547 if (reg&0x2)
548 *val = (temp>>16)&0xffff;
549 else
550 *val = temp&0xffff;
551 #endif
554 static __inline void
555 t3_os_pci_write_config_2(adapter_t *adapter, int reg, uint16_t val)
557 #ifdef __FreeBSD__
558 pci_write_config(adapter->dev, reg, val, 2);
559 #endif
560 #ifdef __NetBSD__
561 uint32_t temp = pci_conf_read(adapter->pa.pa_pc, adapter->pa.pa_tag, reg&0xfc);
562 if (reg&0x2)
563 temp = (temp&0xffff)|(val<<16);
564 else
565 temp = (temp&0xffff0000)|val;
566 pci_conf_write(adapter->pa.pa_pc, adapter->pa.pa_tag, reg&0xfc, temp);
567 #endif
570 static __inline uint8_t *
571 t3_get_next_mcaddr(struct t3_rx_mode *rm)
573 uint8_t *macaddr = NULL;
575 if (rm->idx == 0)
576 macaddr = rm->port->hw_addr;
578 rm->idx++;
579 return (macaddr);
582 static __inline void
583 t3_init_rx_mode(struct t3_rx_mode *rm, struct port_info *port)
585 rm->idx = 0;
586 rm->port = port;
589 static __inline struct port_info *
590 adap2pinfo(struct adapter *adap, int idx)
592 return &adap->port[idx];
595 int t3_os_find_pci_capability(adapter_t *adapter, int cap);
596 int t3_os_pci_save_state(struct adapter *adapter);
597 int t3_os_pci_restore_state(struct adapter *adapter);
598 void t3_os_link_changed(adapter_t *adapter, int port_id, int link_status,
599 int speed, int duplex, int fc);
600 void t3_sge_err_intr_handler(adapter_t *adapter);
601 int t3_offload_tx(struct toedev *, struct mbuf *);
602 void t3_os_ext_intr_handler(adapter_t *adapter);
603 void t3_os_set_hw_addr(adapter_t *adapter, int port_idx, u8 hw_addr[]);
604 int t3_mgmt_tx(adapter_t *adap, struct mbuf *m);
607 int t3_sge_alloc(struct adapter *);
608 int t3_sge_free(struct adapter *);
609 int t3_sge_alloc_qset(adapter_t *, uint32_t, int, int, const struct qset_params *,
610 int, struct port_info *);
611 void t3_free_sge_resources(adapter_t *);
612 void t3_sge_start(adapter_t *);
613 void t3_sge_stop(adapter_t *);
614 #ifdef __FreeBSD__
615 void t3b_intr(void *data);
616 void t3_intr_msi(void *data);
617 void t3_intr_msix(void *data);
618 #endif
619 #ifdef __NetBSD__
620 int t3b_intr(void *data);
621 int t3_intr_msi(void *data);
622 int t3_intr_msix(void *data);
623 #endif
624 int t3_encap(struct port_info *, struct mbuf **, int *free);
626 int t3_sge_init_adapter(adapter_t *);
627 int t3_sge_init_port(struct port_info *);
628 void t3_sge_deinit_sw(adapter_t *);
630 void t3_rx_eth_lro(adapter_t *adap, struct sge_rspq *rq, struct mbuf *m,
631 int ethpad, uint32_t rss_hash, uint32_t rss_csum, int lro);
632 void t3_rx_eth(struct adapter *adap, struct sge_rspq *rq, struct mbuf *m, int ethpad);
633 void t3_lro_flush(adapter_t *adap, struct sge_qset *qs, struct lro_state *state);
635 void t3_add_sysctls(adapter_t *sc);
636 int t3_get_desc(const struct sge_qset *qs, unsigned int qnum, unsigned int idx,
637 unsigned char *data);
638 void t3_update_qset_coalesce(struct sge_qset *qs, const struct qset_params *p);
640 * XXX figure out how we can return this to being private to sge
642 #define desc_reclaimable(q) ((int)((q)->processed - (q)->cleaned - TX_MAX_DESC))
644 #define container_of(p, stype, field) ((stype *)(((uint8_t *)(p)) - offsetof(stype, field)))
646 static __inline struct sge_qset *
647 fl_to_qset(struct sge_fl *q, int qidx)
649 return container_of(q, struct sge_qset, fl[qidx]);
652 static __inline struct sge_qset *
653 rspq_to_qset(struct sge_rspq *q)
655 return container_of(q, struct sge_qset, rspq);
658 static __inline struct sge_qset *
659 txq_to_qset(struct sge_txq *q, int qidx)
661 return container_of(q, struct sge_qset, txq[qidx]);
664 static __inline struct adapter *
665 tdev2adap(struct toedev *d)
667 return container_of(d, struct adapter, tdev);
670 #undef container_of
672 #define OFFLOAD_DEVMAP_BIT 15
673 static inline int offload_running(adapter_t *adapter)
675 return isset(&adapter->open_device_map, OFFLOAD_DEVMAP_BIT);
679 #endif