xref: /netbsd-src/sys/dev/pci/ixgbe/ixgbe_netbsd.c (revision 8ab7d3270e11fa3d3bc7e5a6e46ad926e3c35235)
1 /* $NetBSD: ixgbe_netbsd.c,v 1.17 2021/08/25 09:06:02 msaitoh Exp $ */
2 /*
3  * Copyright (c) 2011 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Coyote Point Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: ixgbe_netbsd.c,v 1.17 2021/08/25 09:06:02 msaitoh Exp $");
33 
34 #include <sys/param.h>
35 
36 #include <sys/atomic.h>
37 #include <sys/bus.h>
38 #include <sys/condvar.h>
39 #include <sys/cpu.h>
40 #include <sys/kmem.h>
41 #include <sys/mbuf.h>
42 #include <sys/mutex.h>
43 #include <sys/queue.h>
44 #include <sys/workqueue.h>
45 #include <dev/pci/pcivar.h>
46 
47 #include "ixgbe.h"
48 
49 void
ixgbe_dma_tag_destroy(ixgbe_dma_tag_t * dt)50 ixgbe_dma_tag_destroy(ixgbe_dma_tag_t *dt)
51 {
52 	kmem_free(dt, sizeof(*dt));
53 }
54 
55 int
ixgbe_dma_tag_create(bus_dma_tag_t dmat,bus_size_t alignment,bus_size_t boundary,bus_size_t maxsize,int nsegments,bus_size_t maxsegsize,int flags,ixgbe_dma_tag_t ** dtp)56 ixgbe_dma_tag_create(bus_dma_tag_t dmat, bus_size_t alignment,
57     bus_size_t boundary, bus_size_t maxsize, int nsegments,
58     bus_size_t maxsegsize, int flags, ixgbe_dma_tag_t **dtp)
59 {
60 	ixgbe_dma_tag_t *dt;
61 
62 	*dtp = NULL;
63 
64 	dt = kmem_zalloc(sizeof(*dt), KM_SLEEP);
65 	dt->dt_dmat = dmat;
66 	dt->dt_alignment = alignment;
67 	dt->dt_boundary = boundary;
68 	dt->dt_maxsize = maxsize;
69 	dt->dt_nsegments = nsegments;
70 	dt->dt_maxsegsize = maxsegsize;
71 	dt->dt_flags = flags;
72 	*dtp = dt;
73 
74 	return 0;
75 }
76 
77 void
ixgbe_dmamap_destroy(ixgbe_dma_tag_t * dt,bus_dmamap_t dmam)78 ixgbe_dmamap_destroy(ixgbe_dma_tag_t *dt, bus_dmamap_t dmam)
79 {
80 	bus_dmamap_destroy(dt->dt_dmat, dmam);
81 }
82 
83 void
ixgbe_dmamap_sync(ixgbe_dma_tag_t * dt,bus_dmamap_t dmam,int ops)84 ixgbe_dmamap_sync(ixgbe_dma_tag_t *dt, bus_dmamap_t dmam, int ops)
85 {
86 	bus_dmamap_sync(dt->dt_dmat, dmam, 0, dt->dt_maxsize, ops);
87 }
88 
89 void
ixgbe_dmamap_unload(ixgbe_dma_tag_t * dt,bus_dmamap_t dmam)90 ixgbe_dmamap_unload(ixgbe_dma_tag_t *dt, bus_dmamap_t dmam)
91 {
92 	bus_dmamap_unload(dt->dt_dmat, dmam);
93 }
94 
95 int
ixgbe_dmamap_create(ixgbe_dma_tag_t * dt,int flags,bus_dmamap_t * dmamp)96 ixgbe_dmamap_create(ixgbe_dma_tag_t *dt, int flags, bus_dmamap_t *dmamp)
97 {
98 	return bus_dmamap_create(dt->dt_dmat, dt->dt_maxsize, dt->dt_nsegments,
99 	    dt->dt_maxsegsize, dt->dt_boundary, flags, dmamp);
100 }
101 
102 
103 struct mbuf *
ixgbe_getcl(void)104 ixgbe_getcl(void)
105 {
106 	struct mbuf *m;
107 
108 	MGETHDR(m, M_DONTWAIT, MT_DATA);
109 
110 	if (m == NULL)
111 		return NULL;
112 
113 	MCLGET(m, M_DONTWAIT);
114 	if ((m->m_flags & M_EXT) == 0) {
115 		m_freem(m);
116 		return NULL;
117 	}
118 
119 	return m;
120 }
121 
122 void
ixgbe_pci_enable_busmaster(pci_chipset_tag_t pc,pcitag_t tag)123 ixgbe_pci_enable_busmaster(pci_chipset_tag_t pc, pcitag_t tag)
124 {
125 	pcireg_t	pci_cmd_word;
126 
127 	pci_cmd_word = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
128 	if (!(pci_cmd_word & PCI_COMMAND_MASTER_ENABLE)) {
129 		pci_cmd_word |= PCI_COMMAND_MASTER_ENABLE;
130 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, pci_cmd_word);
131 	}
132 }
133 
134 u_int
atomic_load_acq_uint(volatile u_int * p)135 atomic_load_acq_uint(volatile u_int *p)
136 {
137 	return atomic_load_acquire(p);
138 }
139 
140 void
ixgbe_delay(unsigned int us)141 ixgbe_delay(unsigned int us)
142 {
143 
144 	if (__predict_false(cold))
145 		delay(us);
146 	else if ((us / 1000) >= hztoms(1)) {
147 		/*
148 		 * Wait at least two clock ticks so we know the time has
149 		 * passed.
150 		 */
151 		kpause("ixgdly", false, mstohz(us / 1000) + 1, NULL);
152 	} else
153 		delay(us);
154 }
155