Sun Mar 8 11:03:19 2020 UTC ()
Pull up following revision(s) (requested by chs in ticket #765):

	sys/dev/acpi/acpi_pci_link.c: revision 1.25

apply FreeBSD revs r214848 and r214849:

    r214849 | jkim | 2010-11-05 13:24:26 -0700 (Fri, 05 Nov 2010) | 2 lines
    Add a forgotten change from the previous commit.
    r214848 | jkim | 2010-11-05 12:50:09 -0700 (Fri, 05 Nov 2010) | 13 lines

    Fix a use-after-free bug for extended IRQ resource[1].  When _PRS buffer is
    copied as a template for _SRS, a string pointer for descriptor name is also
    copied and it becomes stale as soon as it gets de-allocated[2].  Now _CRS is
    used as a template for _SRS as ACPI specification suggests if it is usable.

    The template from _PRS is still utilized but only when _CRS is not available
    or broken.  To avoid use-after-free the problem in this case, however, only
    mandatory fields are copied, optional data is removed, and structure length
    is adjusted accordingly.

    Reported by:    hps[1]
    Analyzed by:    avg[2]
    Tested by:      hps

This also fixes reading past the end of a structure as detected by KASAN.


(martin)
diff -r1.22 -r1.22.26.1 src/sys/dev/acpi/acpi_pci_link.c

cvs diff -r1.22 -r1.22.26.1 src/sys/dev/acpi/acpi_pci_link.c (switch to unified diff)

--- src/sys/dev/acpi/acpi_pci_link.c 2014/09/14 19:54:05 1.22
+++ src/sys/dev/acpi/acpi_pci_link.c 2020/03/08 11:03:19 1.22.26.1
@@ -1,1275 +1,1253 @@ @@ -1,1275 +1,1253 @@
1/* $NetBSD: acpi_pci_link.c,v 1.22 2014/09/14 19:54:05 mrg Exp $ */ 1/* $NetBSD: acpi_pci_link.c,v 1.22.26.1 2020/03/08 11:03:19 martin Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2002 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4 * Copyright (c) 2002 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: acpi_pci_link.c,v 1.22 2014/09/14 19:54:05 mrg Exp $"); 30__KERNEL_RCSID(0, "$NetBSD: acpi_pci_link.c,v 1.22.26.1 2020/03/08 11:03:19 martin Exp $");
31 31
32#include <sys/param.h> 32#include <sys/param.h>
33#include <sys/malloc.h> 33#include <sys/malloc.h>
34#include <sys/queue.h> 34#include <sys/queue.h>
35#include <sys/reboot.h> 35#include <sys/reboot.h>
36#include <sys/systm.h> 36#include <sys/systm.h>
37 37
38#include <dev/acpi/acpireg.h> 38#include <dev/acpi/acpireg.h>
39#include <dev/acpi/acpivar.h> 39#include <dev/acpi/acpivar.h>
40 40
41#include <dev/pci/pcireg.h> 41#include <dev/pci/pcireg.h>
42 42
43#include "opt_acpi.h" 43#include "opt_acpi.h"
44 44
45 45
46#define _COMPONENT ACPI_BUS_COMPONENT 46#define _COMPONENT ACPI_BUS_COMPONENT
47ACPI_MODULE_NAME ("acpi_pci_link") 47ACPI_MODULE_NAME ("acpi_pci_link")
48 48
49MALLOC_DECLARE(M_ACPI); 49MALLOC_DECLARE(M_ACPI);
50 50
51#define NUM_ISA_INTERRUPTS 16 51#define NUM_ISA_INTERRUPTS 16
52#define NUM_ACPI_INTERRUPTS 256 52#define NUM_ACPI_INTERRUPTS 256
53 53
54#define PCI_INVALID_IRQ 255 54#define PCI_INVALID_IRQ 255
55#define PCI_INTERRUPT_VALID(x) ((x) != PCI_INVALID_IRQ && (x) != 0) 55#define PCI_INTERRUPT_VALID(x) ((x) != PCI_INVALID_IRQ && (x) != 0)
56 56
57#define ACPI_SERIAL_BEGIN(x) 57#define ACPI_SERIAL_BEGIN(x)
58#define ACPI_SERIAL_END(x) 58#define ACPI_SERIAL_END(x)
59 59
60/* 60/*
61 * An ACPI PCI link device may contain multiple links. Each link has its 61 * An ACPI PCI link device may contain multiple links. Each link has its
62 * own ACPI resource. _PRT entries specify which link is being used via 62 * own ACPI resource. _PRT entries specify which link is being used via
63 * the Source Index. 63 * the Source Index.
64 * 64 *
65 * XXX: A note about Source Indices and DPFs: Currently we assume that 65 * XXX: A note about Source Indices and DPFs: Currently we assume that
66 * the DPF start and end tags are not counted towards the index that 66 * the DPF start and end tags are not counted towards the index that
67 * Source Index corresponds to. Also, we assume that when DPFs are in use 67 * Source Index corresponds to. Also, we assume that when DPFs are in use
68 * they various sets overlap in terms of Indices. Here's an example 68 * they various sets overlap in terms of Indices. Here's an example
69 * resource list indicating these assumptions: 69 * resource list indicating these assumptions:
70 * 70 *
71 * Resource Index 71 * Resource Index
72 * -------- ----- 72 * -------- -----
73 * I/O Port 0 73 * I/O Port 0
74 * Start DPF - 74 * Start DPF -
75 * IRQ 1 75 * IRQ 1
76 * MemIO 2 76 * MemIO 2
77 * Start DPF - 77 * Start DPF -
78 * IRQ 1 78 * IRQ 1
79 * MemIO 2 79 * MemIO 2
80 * End DPF - 80 * End DPF -
81 * DMA Channel 3 81 * DMA Channel 3
82 * 82 *
83 * The XXX is because I'm not sure if this is a valid assumption to make. 83 * The XXX is because I'm not sure if this is a valid assumption to make.
84 */ 84 */
85 85
86/* States during DPF processing. */ 86/* States during DPF processing. */
87#define DPF_OUTSIDE 0 87#define DPF_OUTSIDE 0
88#define DPF_FIRST 1 88#define DPF_FIRST 1
89#define DPF_IGNORE 2 89#define DPF_IGNORE 2
90 90
91struct link; 91struct link;
92 92
93struct acpi_pci_link_softc { 93struct acpi_pci_link_softc {
94 int pl_num_links; 94 int pl_num_links;
95 int pl_crs_bad; 95 int pl_crs_bad;
96 struct link *pl_links; 96 struct link *pl_links;
97 char pl_name[32]; 97 char pl_name[32];
98 ACPI_HANDLE pl_handle; 98 ACPI_HANDLE pl_handle;
99 TAILQ_ENTRY(acpi_pci_link_softc) pl_list; 99 TAILQ_ENTRY(acpi_pci_link_softc) pl_list;
100}; 100};
101 101
102static TAILQ_HEAD(, acpi_pci_link_softc) acpi_pci_linkdevs = 102static TAILQ_HEAD(, acpi_pci_link_softc) acpi_pci_linkdevs =
103 TAILQ_HEAD_INITIALIZER(acpi_pci_linkdevs); 103 TAILQ_HEAD_INITIALIZER(acpi_pci_linkdevs);
104 104
105 105
106struct link { 106struct link {
107 struct acpi_pci_link_softc *l_sc; 107 struct acpi_pci_link_softc *l_sc;
108 uint8_t l_bios_irq; 108 uint8_t l_bios_irq;
109 uint8_t l_irq; 109 uint8_t l_irq;
110 uint8_t l_trig; 110 uint8_t l_trig;
111 uint8_t l_pol; 111 uint8_t l_pol;
112 uint8_t l_initial_irq; 112 uint8_t l_initial_irq;
113 int l_res_index; 113 int l_res_index;
114 int l_num_irqs; 114 int l_num_irqs;
115 int *l_irqs; 115 int *l_irqs;
116 int l_references; 116 int l_references;
117 int l_dev_count; 117 int l_dev_count;
118 pcitag_t *l_devices; 118 pcitag_t *l_devices;
119 int l_routed:1; 119 int l_routed:1;
120 int l_isa_irq:1; 120 int l_isa_irq:1;
121 ACPI_RESOURCE l_prs_template; 121 ACPI_RESOURCE l_prs_template;
122}; 122};
123 123
124struct link_count_request { 124struct link_count_request {
125 int in_dpf; 125 int in_dpf;
126 int count; 126 int count;
127}; 127};
128 128
129struct link_res_request { 129struct link_res_request {
130 struct acpi_pci_link_softc *sc; 130 struct acpi_pci_link_softc *sc;
131 int in_dpf; 131 int in_dpf;
132 int res_index; 132 int res_index;
133 int link_index; 133 int link_index;
134}; 134};
135 135
136static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS]; 136static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS];
137static int pci_link_bios_isa_irqs; 137static int pci_link_bios_isa_irqs;
138 138
139static ACPI_STATUS acpi_count_irq_resources(ACPI_RESOURCE *, void *); 139static ACPI_STATUS acpi_count_irq_resources(ACPI_RESOURCE *, void *);
140static ACPI_STATUS link_add_crs(ACPI_RESOURCE *, void *); 140static ACPI_STATUS link_add_crs(ACPI_RESOURCE *, void *);
141static ACPI_STATUS link_add_prs(ACPI_RESOURCE *, void *); 141static ACPI_STATUS link_add_prs(ACPI_RESOURCE *, void *);
142static int link_valid_irq(struct link *, int); 142static int link_valid_irq(struct link *, int);
143static void acpi_pci_link_dump(struct acpi_pci_link_softc *); 143static void acpi_pci_link_dump(struct acpi_pci_link_softc *);
144static int acpi_pci_link_attach(struct acpi_pci_link_softc *); 144static int acpi_pci_link_attach(struct acpi_pci_link_softc *);
145static uint8_t acpi_pci_link_search_irq(struct acpi_pci_link_softc *, int, int, 145static uint8_t acpi_pci_link_search_irq(struct acpi_pci_link_softc *, int, int,
146 int); 146 int);
147static struct link *acpi_pci_link_lookup(struct acpi_pci_link_softc *, int); 147static struct link *acpi_pci_link_lookup(struct acpi_pci_link_softc *, int);
148static ACPI_STATUS acpi_pci_link_srs(struct acpi_pci_link_softc *, 148static ACPI_STATUS acpi_pci_link_srs(struct acpi_pci_link_softc *,
149 ACPI_BUFFER *); 149 ACPI_BUFFER *);
150static ACPI_STATUS acpi_AppendBufferResource(ACPI_BUFFER *, ACPI_RESOURCE *); 150static ACPI_STATUS acpi_AppendBufferResource(ACPI_BUFFER *, ACPI_RESOURCE *);
151 151
152static ACPI_STATUS 152static ACPI_STATUS
153acpi_count_irq_resources(ACPI_RESOURCE *res, void *context) 153acpi_count_irq_resources(ACPI_RESOURCE *res, void *context)
154{ 154{
155 struct link_count_request *req; 155 struct link_count_request *req;
156 156
157 req = (struct link_count_request *)context; 157 req = (struct link_count_request *)context;
158 switch (res->Type) { 158 switch (res->Type) {
159 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 159 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
160 switch (req->in_dpf) { 160 switch (req->in_dpf) {
161 case DPF_OUTSIDE: 161 case DPF_OUTSIDE:
162 /* We've started the first DPF. */ 162 /* We've started the first DPF. */
163 req->in_dpf = DPF_FIRST; 163 req->in_dpf = DPF_FIRST;
164 break; 164 break;
165 case DPF_FIRST: 165 case DPF_FIRST:
166 /* We've started the second DPF. */ 166 /* We've started the second DPF. */
167 req->in_dpf = DPF_IGNORE; 167 req->in_dpf = DPF_IGNORE;
168 break; 168 break;
169 } 169 }
170 break; 170 break;
171 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 171 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
172 /* We are finished with DPF parsing. */ 172 /* We are finished with DPF parsing. */
173 KASSERT(req->in_dpf != DPF_OUTSIDE); 173 KASSERT(req->in_dpf != DPF_OUTSIDE);
174 req->in_dpf = DPF_OUTSIDE; 174 req->in_dpf = DPF_OUTSIDE;
175 break; 175 break;
176 case ACPI_RESOURCE_TYPE_IRQ: 176 case ACPI_RESOURCE_TYPE_IRQ:
177 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 177 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
178 /* 178 /*
179 * Don't count resources if we are in a DPF set that we are 179 * Don't count resources if we are in a DPF set that we are
180 * ignoring. 180 * ignoring.
181 */ 181 */
182 if (req->in_dpf != DPF_IGNORE) 182 if (req->in_dpf != DPF_IGNORE)
183 req->count++; 183 req->count++;
184 } 184 }
185 return (AE_OK); 185 return (AE_OK);
186} 186}
187 187
188static ACPI_STATUS 188static ACPI_STATUS
189link_add_crs(ACPI_RESOURCE *res, void *context) 189link_add_crs(ACPI_RESOURCE *res, void *context)
190{ 190{
191 struct link_res_request *req; 191 struct link_res_request *req;
192 struct link *link; 192 struct link *link;
193 193
194 req = (struct link_res_request *)context; 194 req = (struct link_res_request *)context;
195 switch (res->Type) { 195 switch (res->Type) {
196 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 196 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
197 switch (req->in_dpf) { 197 switch (req->in_dpf) {
198 case DPF_OUTSIDE: 198 case DPF_OUTSIDE:
199 /* We've started the first DPF. */ 199 /* We've started the first DPF. */
200 req->in_dpf = DPF_FIRST; 200 req->in_dpf = DPF_FIRST;
201 break; 201 break;
202 case DPF_FIRST: 202 case DPF_FIRST:
203 /* We've started the second DPF. */ 203 /* We've started the second DPF. */
204 panic( 204 panic(
205 "%s: Multiple dependent functions within a current resource", 205 "%s: Multiple dependent functions within a current resource",
206 __func__); 206 __func__);
207 break; 207 break;
208 } 208 }
209 break; 209 break;
210 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 210 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
211 /* We are finished with DPF parsing. */ 211 /* We are finished with DPF parsing. */
212 KASSERT(req->in_dpf != DPF_OUTSIDE); 212 KASSERT(req->in_dpf != DPF_OUTSIDE);
213 req->in_dpf = DPF_OUTSIDE; 213 req->in_dpf = DPF_OUTSIDE;
214 break; 214 break;
215 case ACPI_RESOURCE_TYPE_IRQ: 215 case ACPI_RESOURCE_TYPE_IRQ:
216 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 216 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
217 KASSERT(req->link_index < req->sc->pl_num_links); 217 KASSERT(req->link_index < req->sc->pl_num_links);
218 link = &req->sc->pl_links[req->link_index]; 218 link = &req->sc->pl_links[req->link_index];
219 link->l_res_index = req->res_index; 219 link->l_res_index = req->res_index;
220 req->link_index++; 220 req->link_index++;
221 req->res_index++; 221 req->res_index++;
222 222
223 /* 223 /*
224 * Only use the current value if there's one IRQ. Some 224 * Only use the current value if there's one IRQ. Some
225 * systems return multiple IRQs (which is nonsense for _CRS) 225 * systems return multiple IRQs (which is nonsense for _CRS)
226 * when the link hasn't been programmed. 226 * when the link hasn't been programmed.
227 */ 227 */
228 if (res->Type == ACPI_RESOURCE_TYPE_IRQ) { 228 if (res->Type == ACPI_RESOURCE_TYPE_IRQ) {
229 if (res->Data.Irq.InterruptCount == 1) { 229 if (res->Data.Irq.InterruptCount == 1) {
230 link->l_irq = res->Data.Irq.Interrupts[0]; 230 link->l_irq = res->Data.Irq.Interrupts[0];
231 link->l_trig = res->Data.Irq.Triggering; 231 link->l_trig = res->Data.Irq.Triggering;
232 link->l_pol = res->Data.Irq.Polarity; 232 link->l_pol = res->Data.Irq.Polarity;
233 } 233 }
234 } else if (res->Data.ExtendedIrq.InterruptCount == 1) { 234 } else if (res->Data.ExtendedIrq.InterruptCount == 1) {
235 link->l_irq = res->Data.ExtendedIrq.Interrupts[0]; 235 link->l_irq = res->Data.ExtendedIrq.Interrupts[0];
236 link->l_trig = res->Data.ExtendedIrq.Triggering; 236 link->l_trig = res->Data.ExtendedIrq.Triggering;
237 link->l_pol = res->Data.ExtendedIrq.Polarity; 237 link->l_pol = res->Data.ExtendedIrq.Polarity;
238 } 238 }
239 239
240 /* 240 /*
241 * An IRQ of zero means that the link isn't routed. 241 * An IRQ of zero means that the link isn't routed.
242 */ 242 */
243 if (link->l_irq == 0) 243 if (link->l_irq == 0)
244 link->l_irq = PCI_INVALID_IRQ; 244 link->l_irq = PCI_INVALID_IRQ;
245 break; 245 break;
246 default: 246 default:
247 req->res_index++; 247 req->res_index++;
248 } 248 }
249 return (AE_OK); 249 return (AE_OK);
250} 250}
251 251
252/* 252/*
253 * Populate the set of possible IRQs for each device. 253 * Populate the set of possible IRQs for each device.
254 */ 254 */
255static ACPI_STATUS 255static ACPI_STATUS
256link_add_prs(ACPI_RESOURCE *res, void *context) 256link_add_prs(ACPI_RESOURCE *res, void *context)
257{ 257{
 258 ACPI_RESOURCE *tmp;
258 struct link_res_request *req; 259 struct link_res_request *req;
259 struct link *link; 260 struct link *link;
260 uint8_t *irqs = NULL; 261 uint8_t *irqs = NULL;
261 uint32_t *ext_irqs = NULL; 262 uint32_t *ext_irqs = NULL;
262 int i, is_ext_irq = 1; 263 int i, is_ext_irq = 1;
263 264
264 req = (struct link_res_request *)context; 265 req = (struct link_res_request *)context;
265 switch (res->Type) { 266 switch (res->Type) {
266 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 267 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
267 switch (req->in_dpf) { 268 switch (req->in_dpf) {
268 case DPF_OUTSIDE: 269 case DPF_OUTSIDE:
269 /* We've started the first DPF. */ 270 /* We've started the first DPF. */
270 req->in_dpf = DPF_FIRST; 271 req->in_dpf = DPF_FIRST;
271 break; 272 break;
272 case DPF_FIRST: 273 case DPF_FIRST:
273 /* We've started the second DPF. */ 274 /* We've started the second DPF. */
274 req->in_dpf = DPF_IGNORE; 275 req->in_dpf = DPF_IGNORE;
275 break; 276 break;
276 } 277 }
277 break; 278 break;
278 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 279 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
279 /* We are finished with DPF parsing. */ 280 /* We are finished with DPF parsing. */
280 KASSERT(req->in_dpf != DPF_OUTSIDE); 281 KASSERT(req->in_dpf != DPF_OUTSIDE);
281 req->in_dpf = DPF_OUTSIDE; 282 req->in_dpf = DPF_OUTSIDE;
282 break; 283 break;
283 case ACPI_RESOURCE_TYPE_IRQ: 284 case ACPI_RESOURCE_TYPE_IRQ:
284 is_ext_irq = 0; 285 is_ext_irq = 0;
285 /* fall through */ 286 /* fall through */
286 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 287 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
287 /* 288 /*
288 * Don't parse resources if we are in a DPF set that we are 289 * Don't parse resources if we are in a DPF set that we are
289 * ignoring. 290 * ignoring.
290 */ 291 */
291 if (req->in_dpf == DPF_IGNORE) 292 if (req->in_dpf == DPF_IGNORE)
292 break; 293 break;
293 294
294 KASSERT(req->link_index < req->sc->pl_num_links); 295 KASSERT(req->link_index < req->sc->pl_num_links);
295 link = &req->sc->pl_links[req->link_index]; 296 link = &req->sc->pl_links[req->link_index];
296 if (link->l_res_index == -1) { 297 if (link->l_res_index == -1) {
297 KASSERT(req->sc->pl_crs_bad); 298 KASSERT(req->sc->pl_crs_bad);
298 link->l_res_index = req->res_index; 299 link->l_res_index = req->res_index;
299 } 300 }
300 req->link_index++; 301 req->link_index++;
301 req->res_index++; 302 req->res_index++;
302 303
303 /* 304 /*
304 * Stash a copy of the resource for later use when 305 * Stash a copy of the resource for later use when doing
305 * doing _SRS. 306 * _SRS.
306 * 
307 * Note that in theory res->Length may exceed the size 
308 * of ACPI_RESOURCE, due to variable length lists in 
309 * subtypes. However, all uses of l_prs_template only 
310 * rely on lists lengths of zero or one, for which 
311 * sizeof(ACPI_RESOURCE) is sufficient space anyway. 
312 * We cannot read longer than Length bytes, in case we 
313 * read off the end of mapped memory. So we read 
314 * whichever length is shortest, Length or 
315 * sizeof(ACPI_RESOURCE). 
316 */ 307 */
317 KASSERT(res->Length >= ACPI_RS_SIZE_MIN); 308 tmp = &link->l_prs_template;
 309 if (is_ext_irq) {
 310 memcpy(tmp, res, ACPI_RS_SIZE(tmp->Data.ExtendedIrq));
318 311
319 memset(&link->l_prs_template, 0, sizeof(link->l_prs_template)); 312 /*
320 memcpy(&link->l_prs_template, res, 313 * XXX acpi_AppendBufferResource() cannot handle
321 MIN(res->Length, sizeof(link->l_prs_template))); 314 * optional data.
 315 */
 316 memset(&tmp->Data.ExtendedIrq.ResourceSource, 0,
 317 sizeof(tmp->Data.ExtendedIrq.ResourceSource));
 318 tmp->Length = ACPI_RS_SIZE(tmp->Data.ExtendedIrq);
322 319
323 if (is_ext_irq) { 
324 link->l_num_irqs = 320 link->l_num_irqs =
325 res->Data.ExtendedIrq.InterruptCount; 321 res->Data.ExtendedIrq.InterruptCount;
326 link->l_trig = res->Data.ExtendedIrq.Triggering; 322 link->l_trig = res->Data.ExtendedIrq.Triggering;
327 link->l_pol = res->Data.ExtendedIrq.Polarity; 323 link->l_pol = res->Data.ExtendedIrq.Polarity;
328 ext_irqs = res->Data.ExtendedIrq.Interrupts; 324 ext_irqs = res->Data.ExtendedIrq.Interrupts;
329 } else { 325 } else {
 326 memcpy(tmp, res, ACPI_RS_SIZE(tmp->Data.Irq));
330 link->l_num_irqs = res->Data.Irq.InterruptCount; 327 link->l_num_irqs = res->Data.Irq.InterruptCount;
331 link->l_trig = res->Data.Irq.Triggering; 328 link->l_trig = res->Data.Irq.Triggering;
332 link->l_pol = res->Data.Irq.Polarity; 329 link->l_pol = res->Data.Irq.Polarity;
333 irqs = res->Data.Irq.Interrupts; 330 irqs = res->Data.Irq.Interrupts;
334 } 331 }
335 if (link->l_num_irqs == 0) 332 if (link->l_num_irqs == 0)
336 break; 333 break;
337 334
338 /* 335 /*
339 * Save a list of the valid IRQs. Also, if all of the 336 * Save a list of the valid IRQs. Also, if all of the
340 * valid IRQs are ISA IRQs, then mark this link as 337 * valid IRQs are ISA IRQs, then mark this link as
341 * routed via an ISA interrupt. 338 * routed via an ISA interrupt.
342 */ 339 */
343 link->l_isa_irq = TRUE; 340 link->l_isa_irq = TRUE;
344 link->l_irqs = malloc(sizeof(int) * link->l_num_irqs, 341 link->l_irqs = malloc(sizeof(int) * link->l_num_irqs,
345 M_ACPI, M_WAITOK | M_ZERO); 342 M_ACPI, M_WAITOK | M_ZERO);
346 for (i = 0; i < link->l_num_irqs; i++) { 343 for (i = 0; i < link->l_num_irqs; i++) {
347 if (is_ext_irq) { 344 if (is_ext_irq) {
348 link->l_irqs[i] = ext_irqs[i]; 345 link->l_irqs[i] = ext_irqs[i];
349 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS) 346 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS)
350 link->l_isa_irq = FALSE; 347 link->l_isa_irq = FALSE;
351 } else { 348 } else {
352 link->l_irqs[i] = irqs[i]; 349 link->l_irqs[i] = irqs[i];
353 if (irqs[i] >= NUM_ISA_INTERRUPTS) 350 if (irqs[i] >= NUM_ISA_INTERRUPTS)
354 link->l_isa_irq = FALSE; 351 link->l_isa_irq = FALSE;
355 } 352 }
356 } 353 }
357 break; 354 break;
358 default: 355 default:
359 if (req->in_dpf == DPF_IGNORE) 356 if (req->in_dpf == DPF_IGNORE)
360 break; 357 break;
361 if (req->sc->pl_crs_bad) 358 if (req->sc->pl_crs_bad)
362 aprint_normal("%s: Warning: possible resource %d " 359 aprint_normal("%s: Warning: possible resource %d "
363 "will be lost during _SRS\n", req->sc->pl_name, 360 "will be lost during _SRS\n", req->sc->pl_name,
364 req->res_index); 361 req->res_index);
365 req->res_index++; 362 req->res_index++;
366 } 363 }
367 return (AE_OK); 364 return (AE_OK);
368} 365}
369 366
370static int 367static int
371link_valid_irq(struct link *link, int irq) 368link_valid_irq(struct link *link, int irq)
372{ 369{
373 int i; 370 int i;
374 371
375 /* Invalid interrupts are never valid. */ 372 /* Invalid interrupts are never valid. */
376 if (!PCI_INTERRUPT_VALID(irq)) 373 if (!PCI_INTERRUPT_VALID(irq))
377 return (FALSE); 374 return (FALSE);
378 375
379 /* Any interrupt in the list of possible interrupts is valid. */ 376 /* Any interrupt in the list of possible interrupts is valid. */
380 for (i = 0; i < link->l_num_irqs; i++) 377 for (i = 0; i < link->l_num_irqs; i++)
381 if (link->l_irqs[i] == irq) 378 if (link->l_irqs[i] == irq)
382 return (TRUE); 379 return (TRUE);
383 380
384 /* 381 /*
385 * For links routed via an ISA interrupt, if the SCI is routed via 382 * For links routed via an ISA interrupt, if the SCI is routed via
386 * an ISA interrupt, the SCI is always treated as a valid IRQ. 383 * an ISA interrupt, the SCI is always treated as a valid IRQ.
387 */ 384 */
388 if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq && 385 if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq &&
389 irq < NUM_ISA_INTERRUPTS) 386 irq < NUM_ISA_INTERRUPTS)
390 return (TRUE); 387 return (TRUE);
391 388
392 /* If the interrupt wasn't found in the list it is not valid. */ 389 /* If the interrupt wasn't found in the list it is not valid. */
393 return (FALSE); 390 return (FALSE);
394} 391}
395 392
396void 393void
397acpi_pci_link_state(void) 394acpi_pci_link_state(void)
398{ 395{
399 struct acpi_pci_link_softc *sc; 396 struct acpi_pci_link_softc *sc;
400 397
401 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) { 398 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) {
402 acpi_pci_link_dump(sc); 399 acpi_pci_link_dump(sc);
403 } 400 }
404} 401}
405 402
406static void 403static void
407acpi_pci_link_dump(struct acpi_pci_link_softc *sc) 404acpi_pci_link_dump(struct acpi_pci_link_softc *sc)
408{ 405{
409 struct link *link; 406 struct link *link;
410 int i, j; 407 int i, j;
411 408
412 printf("Link Device %s:\n", sc->pl_name); 409 printf("Link Device %s:\n", sc->pl_name);
413 printf("Index IRQ Rtd Ref IRQs\n"); 410 printf("Index IRQ Rtd Ref IRQs\n");
414 for (i = 0; i < sc->pl_num_links; i++) { 411 for (i = 0; i < sc->pl_num_links; i++) {
415 link = &sc->pl_links[i]; 412 link = &sc->pl_links[i];
416 printf("%5d %3d %c %3d ", i, link->l_irq, 413 printf("%5d %3d %c %3d ", i, link->l_irq,
417 link->l_routed ? 'Y' : 'N', link->l_references); 414 link->l_routed ? 'Y' : 'N', link->l_references);
418 if (link->l_num_irqs == 0) 415 if (link->l_num_irqs == 0)
419 printf(" none"); 416 printf(" none");
420 else for (j = 0; j < link->l_num_irqs; j++) 417 else for (j = 0; j < link->l_num_irqs; j++)
421 printf(" %d", link->l_irqs[j]); 418 printf(" %d", link->l_irqs[j]);
422 printf(" polarity %u trigger %u\n", link->l_pol, link->l_trig); 419 printf(" polarity %u trigger %u\n", link->l_pol, link->l_trig);
423 } 420 }
424 printf("\n"); 421 printf("\n");
425} 422}
426 423
427static int 424static int
428acpi_pci_link_attach(struct acpi_pci_link_softc *sc) 425acpi_pci_link_attach(struct acpi_pci_link_softc *sc)
429{ 426{
430 struct link_count_request creq; 427 struct link_count_request creq;
431 struct link_res_request rreq; 428 struct link_res_request rreq;
432 ACPI_STATUS status; 429 ACPI_STATUS status;
433 int i; 430 int i;
434 431
435 ACPI_SERIAL_BEGIN(pci_link); 432 ACPI_SERIAL_BEGIN(pci_link);
436 433
437 /* 434 /*
438 * Count the number of current resources so we know how big of 435 * Count the number of current resources so we know how big of
439 * a link array to allocate. On some systems, _CRS is broken, 436 * a link array to allocate. On some systems, _CRS is broken,
440 * so for those systems try to derive the count from _PRS instead. 437 * so for those systems try to derive the count from _PRS instead.
441 */ 438 */
442 creq.in_dpf = DPF_OUTSIDE; 439 creq.in_dpf = DPF_OUTSIDE;
443 creq.count = 0; 440 creq.count = 0;
444 status = AcpiWalkResources(sc->pl_handle, "_CRS", 441 status = AcpiWalkResources(sc->pl_handle, "_CRS",
445 acpi_count_irq_resources, &creq); 442 acpi_count_irq_resources, &creq);
446 sc->pl_crs_bad = ACPI_FAILURE(status); 443 sc->pl_crs_bad = ACPI_FAILURE(status);
447 if (sc->pl_crs_bad) { 444 if (sc->pl_crs_bad) {
448 creq.in_dpf = DPF_OUTSIDE; 445 creq.in_dpf = DPF_OUTSIDE;
449 creq.count = 0; 446 creq.count = 0;
450 status = AcpiWalkResources(sc->pl_handle, "_PRS", 447 status = AcpiWalkResources(sc->pl_handle, "_PRS",
451 acpi_count_irq_resources, &creq); 448 acpi_count_irq_resources, &creq);
452 if (ACPI_FAILURE(status)) { 449 if (ACPI_FAILURE(status)) {
453 aprint_error("%s: Unable to parse _CRS or _PRS: %s\n", 450 aprint_error("%s: Unable to parse _CRS or _PRS: %s\n",
454 sc->pl_name, AcpiFormatException(status)); 451 sc->pl_name, AcpiFormatException(status));
455 ACPI_SERIAL_END(pci_link); 452 ACPI_SERIAL_END(pci_link);
456 return (ENXIO); 453 return (ENXIO);
457 } 454 }
458 } 455 }
459 sc->pl_num_links = creq.count; 456 sc->pl_num_links = creq.count;
460 if (creq.count == 0) { 457 if (creq.count == 0) {
461 ACPI_SERIAL_END(pci_link); 458 ACPI_SERIAL_END(pci_link);
462 return (0); 459 return (0);
463 } 460 }
464 sc->pl_links = malloc(sizeof(struct link) * sc->pl_num_links, 461 sc->pl_links = malloc(sizeof(struct link) * sc->pl_num_links,
465 M_ACPI, M_WAITOK | M_ZERO); 462 M_ACPI, M_WAITOK | M_ZERO);
466 463
467 /* Initialize the child links. */ 464 /* Initialize the child links. */
468 for (i = 0; i < sc->pl_num_links; i++) { 465 for (i = 0; i < sc->pl_num_links; i++) {
469 sc->pl_links[i].l_irq = PCI_INVALID_IRQ; 466 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
470 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ; 467 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ;
471 sc->pl_links[i].l_sc = sc; 468 sc->pl_links[i].l_sc = sc;
472 sc->pl_links[i].l_isa_irq = FALSE; 469 sc->pl_links[i].l_isa_irq = FALSE;
473 sc->pl_links[i].l_res_index = -1; 470 sc->pl_links[i].l_res_index = -1;
474 sc->pl_links[i].l_dev_count = 0; 471 sc->pl_links[i].l_dev_count = 0;
475 sc->pl_links[i].l_devices = NULL; 472 sc->pl_links[i].l_devices = NULL;
476 } 473 }
477 474
478 /* Try to read the current settings from _CRS if it is valid. */ 475 /* Try to read the current settings from _CRS if it is valid. */
479 if (!sc->pl_crs_bad) { 476 if (!sc->pl_crs_bad) {
480 rreq.in_dpf = DPF_OUTSIDE; 477 rreq.in_dpf = DPF_OUTSIDE;
481 rreq.link_index = 0; 478 rreq.link_index = 0;
482 rreq.res_index = 0; 479 rreq.res_index = 0;
483 rreq.sc = sc; 480 rreq.sc = sc;
484 status = AcpiWalkResources(sc->pl_handle, "_CRS", 481 status = AcpiWalkResources(sc->pl_handle, "_CRS",
485 link_add_crs, &rreq); 482 link_add_crs, &rreq);
486 if (ACPI_FAILURE(status)) { 483 if (ACPI_FAILURE(status)) {
487 aprint_error("%s: Unable to parse _CRS: %s\n", 484 aprint_error("%s: Unable to parse _CRS: %s\n",
488 sc->pl_name, AcpiFormatException(status)); 485 sc->pl_name, AcpiFormatException(status));
489 goto fail; 486 goto fail;
490 } 487 }
491 } 488 }
492 489
493 /* 490 /*
494 * Try to read the possible settings from _PRS. Note that if the 491 * Try to read the possible settings from _PRS. Note that if the
495 * _CRS is toast, we depend on having a working _PRS. However, if 492 * _CRS is toast, we depend on having a working _PRS. However, if
496 * _CRS works, then it is ok for _PRS to be missing. 493 * _CRS works, then it is ok for _PRS to be missing.
497 */ 494 */
498 rreq.in_dpf = DPF_OUTSIDE; 495 rreq.in_dpf = DPF_OUTSIDE;
499 rreq.link_index = 0; 496 rreq.link_index = 0;
500 rreq.res_index = 0; 497 rreq.res_index = 0;
501 rreq.sc = sc; 498 rreq.sc = sc;
502 status = AcpiWalkResources(sc->pl_handle, "_PRS", 499 status = AcpiWalkResources(sc->pl_handle, "_PRS",
503 link_add_prs, &rreq); 500 link_add_prs, &rreq);
504 if (ACPI_FAILURE(status) && 501 if (ACPI_FAILURE(status) &&
505 (status != AE_NOT_FOUND || sc->pl_crs_bad)) { 502 (status != AE_NOT_FOUND || sc->pl_crs_bad)) {
506 aprint_error("%s: Unable to parse _PRS: %s\n", 503 aprint_error("%s: Unable to parse _PRS: %s\n",
507 sc->pl_name, AcpiFormatException(status)); 504 sc->pl_name, AcpiFormatException(status));
508 goto fail; 505 goto fail;
509 } 506 }
510 if (boothowto & AB_VERBOSE) { 507 if (boothowto & AB_VERBOSE) {
511 aprint_normal("%s: Links after initial probe:\n", sc->pl_name); 508 aprint_normal("%s: Links after initial probe:\n", sc->pl_name);
512 acpi_pci_link_dump(sc); 509 acpi_pci_link_dump(sc);
513 } 510 }
514 511
515 /* Verify initial IRQs if we have _PRS. */ 512 /* Verify initial IRQs if we have _PRS. */
516 if (status != AE_NOT_FOUND) 513 if (status != AE_NOT_FOUND)
517 for (i = 0; i < sc->pl_num_links; i++) 514 for (i = 0; i < sc->pl_num_links; i++)
518 if (!link_valid_irq(&sc->pl_links[i], 515 if (!link_valid_irq(&sc->pl_links[i],
519 sc->pl_links[i].l_irq)) 516 sc->pl_links[i].l_irq))
520 sc->pl_links[i].l_irq = PCI_INVALID_IRQ; 517 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
521 if (boothowto & AB_VERBOSE) { 518 if (boothowto & AB_VERBOSE) {
522 printf("%s: Links after initial validation:\n", sc->pl_name); 519 printf("%s: Links after initial validation:\n", sc->pl_name);
523 acpi_pci_link_dump(sc); 520 acpi_pci_link_dump(sc);
524 } 521 }
525 522
526 /* Save initial IRQs. */ 523 /* Save initial IRQs. */
527 for (i = 0; i < sc->pl_num_links; i++) 524 for (i = 0; i < sc->pl_num_links; i++)
528 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq; 525 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq;
529 526
530 /* 527 /*
531 * Try to disable this link. If successful, set the current IRQ to 528 * Try to disable this link. If successful, set the current IRQ to
532 * zero and flags to indicate this link is not routed. If we can't 529 * zero and flags to indicate this link is not routed. If we can't
533 * run _DIS (i.e., the method doesn't exist), assume the initial 530 * run _DIS (i.e., the method doesn't exist), assume the initial
534 * IRQ was routed by the BIOS. 531 * IRQ was routed by the BIOS.
535 */ 532 */
536#ifndef ACPI__DIS_IS_BROKEN 533#ifndef ACPI__DIS_IS_BROKEN
537 if (ACPI_SUCCESS(AcpiEvaluateObject(sc->pl_handle, "_DIS", NULL, 534 if (ACPI_SUCCESS(AcpiEvaluateObject(sc->pl_handle, "_DIS", NULL,
538 NULL))) 535 NULL)))
539 for (i = 0; i < sc->pl_num_links; i++) 536 for (i = 0; i < sc->pl_num_links; i++)
540 sc->pl_links[i].l_irq = PCI_INVALID_IRQ; 537 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
541 else 538 else
542#endif 539#endif
543 for (i = 0; i < sc->pl_num_links; i++) 540 for (i = 0; i < sc->pl_num_links; i++)
544 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq)) 541 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
545 sc->pl_links[i].l_routed = TRUE; 542 sc->pl_links[i].l_routed = TRUE;
546 if (boothowto & AB_VERBOSE) { 543 if (boothowto & AB_VERBOSE) {
547 printf("%s: Links after disable:\n", sc->pl_name); 544 printf("%s: Links after disable:\n", sc->pl_name);
548 acpi_pci_link_dump(sc); 545 acpi_pci_link_dump(sc);
549 } 546 }
550 ACPI_SERIAL_END(pci_link); 547 ACPI_SERIAL_END(pci_link);
551 return (0); 548 return (0);
552fail: 549fail:
553 ACPI_SERIAL_END(pci_link); 550 ACPI_SERIAL_END(pci_link);
554 for (i = 0; i < sc->pl_num_links; i++) { 551 for (i = 0; i < sc->pl_num_links; i++) {
555 if (sc->pl_links[i].l_irqs != NULL) 552 if (sc->pl_links[i].l_irqs != NULL)
556 free(sc->pl_links[i].l_irqs, M_ACPI); 553 free(sc->pl_links[i].l_irqs, M_ACPI);
557 if (sc->pl_links[i].l_devices != NULL) 554 if (sc->pl_links[i].l_devices != NULL)
558 free(sc->pl_links[i].l_devices, M_ACPI); 555 free(sc->pl_links[i].l_devices, M_ACPI);
559 } 556 }
560 free(sc->pl_links, M_ACPI); 557 free(sc->pl_links, M_ACPI);
561 return (ENXIO); 558 return (ENXIO);
562} 559}
563 560
564static void 561static void
565acpi_pci_link_add_functions(struct acpi_pci_link_softc *sc, struct link *link, 562acpi_pci_link_add_functions(struct acpi_pci_link_softc *sc, struct link *link,
566 int bus, int device, int pin) 563 int bus, int device, int pin)
567{ 564{
568 uint32_t value; 565 uint32_t value;
569 uint8_t func, maxfunc, ipin; 566 uint8_t func, maxfunc, ipin;
570 pcitag_t tag; 567 pcitag_t tag;
571 568
572 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, 0); 569 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, 0);
573 /* See if we have a valid device at function 0. */ 570 /* See if we have a valid device at function 0. */
574 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_BHLC_REG); 571 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_BHLC_REG);
575 if (PCI_HDRTYPE_TYPE(value) > PCI_HDRTYPE_PCB) 572 if (PCI_HDRTYPE_TYPE(value) > PCI_HDRTYPE_PCB)
576 return; 573 return;
577 if (PCI_HDRTYPE_MULTIFN(value)) 574 if (PCI_HDRTYPE_MULTIFN(value))
578 maxfunc = 7; 575 maxfunc = 7;
579 else 576 else
580 maxfunc = 0; 577 maxfunc = 0;
581 578
582 /* Scan all possible functions at this device. */ 579 /* Scan all possible functions at this device. */
583 for (func = 0; func <= maxfunc; func++) { 580 for (func = 0; func <= maxfunc; func++) {
584 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, func); 581 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, func);
585 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_ID_REG); 582 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_ID_REG);
586 if (PCI_VENDOR(value) == 0xffff) 583 if (PCI_VENDOR(value) == 0xffff)
587 continue; 584 continue;
588 value = pci_conf_read(acpi_softc->sc_pc, tag, 585 value = pci_conf_read(acpi_softc->sc_pc, tag,
589 PCI_INTERRUPT_REG); 586 PCI_INTERRUPT_REG);
590 ipin = PCI_INTERRUPT_PIN(value); 587 ipin = PCI_INTERRUPT_PIN(value);
591 /* 588 /*
592 * See if it uses the pin in question. Note that the passed 589 * See if it uses the pin in question. Note that the passed
593 * in pin uses 0 for A, .. 3 for D whereas the intpin 590 * in pin uses 0 for A, .. 3 for D whereas the intpin
594 * register uses 0 for no interrupt, 1 for A, .. 4 for D. 591 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
595 */ 592 */
596 if (ipin != pin + 1) 593 if (ipin != pin + 1)
597 continue; 594 continue;
598 595
599 link->l_devices = realloc(link->l_devices, 596 link->l_devices = realloc(link->l_devices,
600 sizeof(pcitag_t) * (link->l_dev_count + 1), 597 sizeof(pcitag_t) * (link->l_dev_count + 1),
601 M_ACPI, M_WAITOK); 598 M_ACPI, M_WAITOK);
602 link->l_devices[link->l_dev_count] = tag; 599 link->l_devices[link->l_dev_count] = tag;
603 ++link->l_dev_count; 600 ++link->l_dev_count;
604 } 601 }
605} 602}
606 603
607static uint8_t 604static uint8_t
608acpi_pci_link_search_irq(struct acpi_pci_link_softc *sc, int bus, int device, 605acpi_pci_link_search_irq(struct acpi_pci_link_softc *sc, int bus, int device,
609 int pin) 606 int pin)
610{ 607{
611 uint32_t value; 608 uint32_t value;
612 uint8_t func, maxfunc, ipin, iline; 609 uint8_t func, maxfunc, ipin, iline;
613 pcitag_t tag; 610 pcitag_t tag;
614 611
615 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, 0); 612 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, 0);
616 /* See if we have a valid device at function 0. */ 613 /* See if we have a valid device at function 0. */
617 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_BHLC_REG); 614 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_BHLC_REG);
618 if (PCI_HDRTYPE_TYPE(value) > PCI_HDRTYPE_PCB) 615 if (PCI_HDRTYPE_TYPE(value) > PCI_HDRTYPE_PCB)
619 return (PCI_INVALID_IRQ); 616 return (PCI_INVALID_IRQ);
620 if (PCI_HDRTYPE_MULTIFN(value)) 617 if (PCI_HDRTYPE_MULTIFN(value))
621 maxfunc = 7; 618 maxfunc = 7;
622 else 619 else
623 maxfunc = 0; 620 maxfunc = 0;
624 621
625 /* Scan all possible functions at this device. */ 622 /* Scan all possible functions at this device. */
626 for (func = 0; func <= maxfunc; func++) { 623 for (func = 0; func <= maxfunc; func++) {
627 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, func); 624 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, func);
628 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_ID_REG); 625 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_ID_REG);
629 if (PCI_VENDOR(value) == 0xffff) 626 if (PCI_VENDOR(value) == 0xffff)
630 continue; 627 continue;
631 value = pci_conf_read(acpi_softc->sc_pc, tag, 628 value = pci_conf_read(acpi_softc->sc_pc, tag,
632 PCI_INTERRUPT_REG); 629 PCI_INTERRUPT_REG);
633 ipin = PCI_INTERRUPT_PIN(value); 630 ipin = PCI_INTERRUPT_PIN(value);
634 iline = PCI_INTERRUPT_LINE(value); 631 iline = PCI_INTERRUPT_LINE(value);
635 632
636 /* 633 /*
637 * See if it uses the pin in question. Note that the passed 634 * See if it uses the pin in question. Note that the passed
638 * in pin uses 0 for A, .. 3 for D whereas the intpin 635 * in pin uses 0 for A, .. 3 for D whereas the intpin
639 * register uses 0 for no interrupt, 1 for A, .. 4 for D. 636 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
640 */ 637 */
641 if (ipin != pin + 1) 638 if (ipin != pin + 1)
642 continue; 639 continue;
643 aprint_verbose( 640 aprint_verbose(
644 "%s: ACPI: Found matching pin for %d.%d.INT%c" 641 "%s: ACPI: Found matching pin for %d.%d.INT%c"
645 " at func %d: %d\n", 642 " at func %d: %d\n",
646 sc->pl_name, bus, device, pin + 'A', func, iline); 643 sc->pl_name, bus, device, pin + 'A', func, iline);
647 if (PCI_INTERRUPT_VALID(iline)) 644 if (PCI_INTERRUPT_VALID(iline))
648 return (iline); 645 return (iline);
649 } 646 }
650 return (PCI_INVALID_IRQ); 647 return (PCI_INVALID_IRQ);
651} 648}
652 649
653/* 650/*
654 * Find the link structure that corresponds to the resource index passed in 651 * Find the link structure that corresponds to the resource index passed in
655 * via 'source_index'. 652 * via 'source_index'.
656 */ 653 */
657static struct link * 654static struct link *
658acpi_pci_link_lookup(struct acpi_pci_link_softc *sc, int source_index) 655acpi_pci_link_lookup(struct acpi_pci_link_softc *sc, int source_index)
659{ 656{
660 int i; 657 int i;
661 658
662 for (i = 0; i < sc->pl_num_links; i++) 659 for (i = 0; i < sc->pl_num_links; i++)
663 if (sc->pl_links[i].l_res_index == source_index) 660 if (sc->pl_links[i].l_res_index == source_index)
664 return (&sc->pl_links[i]); 661 return (&sc->pl_links[i]);
665 return (NULL); 662 return (NULL);
666} 663}
667 664
668void 665void
669acpi_pci_link_add_reference(void *v, int index, int bus, int slot, int pin) 666acpi_pci_link_add_reference(void *v, int index, int bus, int slot, int pin)
670{ 667{
671 struct acpi_pci_link_softc *sc = v; 668 struct acpi_pci_link_softc *sc = v;
672 struct link *link; 669 struct link *link;
673 uint8_t bios_irq; 670 uint8_t bios_irq;
674 671
675 /* Bump the reference count. */ 672 /* Bump the reference count. */
676 ACPI_SERIAL_BEGIN(pci_link); 673 ACPI_SERIAL_BEGIN(pci_link);
677 link = acpi_pci_link_lookup(sc, index); 674 link = acpi_pci_link_lookup(sc, index);
678 if (link == NULL) { 675 if (link == NULL) {
679 printf("%s: apparently invalid index %d\n", sc->pl_name, index); 676 printf("%s: apparently invalid index %d\n", sc->pl_name, index);
680 ACPI_SERIAL_END(pci_link); 677 ACPI_SERIAL_END(pci_link);
681 return; 678 return;
682 } 679 }
683 link->l_references++; 680 link->l_references++;
684 acpi_pci_link_add_functions(sc, link, bus, slot, pin); 681 acpi_pci_link_add_functions(sc, link, bus, slot, pin);
685 if (link->l_routed) 682 if (link->l_routed)
686 pci_link_interrupt_weights[link->l_irq]++; 683 pci_link_interrupt_weights[link->l_irq]++;
687 684
688 /* 685 /*
689 * The BIOS only routes interrupts via ISA IRQs using the ATPICs 686 * The BIOS only routes interrupts via ISA IRQs using the ATPICs
690 * (8259As). Thus, if this link is routed via an ISA IRQ, go 687 * (8259As). Thus, if this link is routed via an ISA IRQ, go
691 * look to see if the BIOS routed an IRQ for this link at the 688 * look to see if the BIOS routed an IRQ for this link at the
692 * indicated (bus, slot, pin). If so, we prefer that IRQ for 689 * indicated (bus, slot, pin). If so, we prefer that IRQ for
693 * this link and add that IRQ to our list of known-good IRQs. 690 * this link and add that IRQ to our list of known-good IRQs.
694 * This provides a good work-around for link devices whose _CRS 691 * This provides a good work-around for link devices whose _CRS
695 * method is either broken or bogus. We only use the value 692 * method is either broken or bogus. We only use the value
696 * returned by _CRS if we can't find a valid IRQ via this method 693 * returned by _CRS if we can't find a valid IRQ via this method
697 * in fact. 694 * in fact.
698 * 695 *
699 * If this link is not routed via an ISA IRQ (because we are using 696 * If this link is not routed via an ISA IRQ (because we are using
700 * APIC for example), then don't bother looking up the BIOS IRQ 697 * APIC for example), then don't bother looking up the BIOS IRQ
701 * as if we find one it won't be valid anyway. 698 * as if we find one it won't be valid anyway.
702 */ 699 */
703 if (!link->l_isa_irq) { 700 if (!link->l_isa_irq) {
704 ACPI_SERIAL_END(pci_link); 701 ACPI_SERIAL_END(pci_link);
705 return; 702 return;
706 } 703 }
707 704
708 /* Try to find a BIOS IRQ setting from any matching devices. */ 705 /* Try to find a BIOS IRQ setting from any matching devices. */
709 bios_irq = acpi_pci_link_search_irq(sc, bus, slot, pin); 706 bios_irq = acpi_pci_link_search_irq(sc, bus, slot, pin);
710 if (!PCI_INTERRUPT_VALID(bios_irq)) { 707 if (!PCI_INTERRUPT_VALID(bios_irq)) {
711 ACPI_SERIAL_END(pci_link); 708 ACPI_SERIAL_END(pci_link);
712 return; 709 return;
713 } 710 }
714 711
715 /* Validate the BIOS IRQ. */ 712 /* Validate the BIOS IRQ. */
716 if (!link_valid_irq(link, bios_irq)) { 713 if (!link_valid_irq(link, bios_irq)) {
717 printf("%s: BIOS IRQ %u for %d.%d.INT%c is invalid\n", 714 printf("%s: BIOS IRQ %u for %d.%d.INT%c is invalid\n",
718 sc->pl_name, bios_irq, (int)bus, slot, pin + 'A'); 715 sc->pl_name, bios_irq, (int)bus, slot, pin + 'A');
719 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) { 716 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
720 link->l_bios_irq = bios_irq; 717 link->l_bios_irq = bios_irq;
721 if (bios_irq < NUM_ISA_INTERRUPTS) 718 if (bios_irq < NUM_ISA_INTERRUPTS)
722 pci_link_bios_isa_irqs |= (1 << bios_irq); 719 pci_link_bios_isa_irqs |= (1 << bios_irq);
723 if (bios_irq != link->l_initial_irq && 720 if (bios_irq != link->l_initial_irq &&
724 PCI_INTERRUPT_VALID(link->l_initial_irq)) 721 PCI_INTERRUPT_VALID(link->l_initial_irq))
725 printf( 722 printf(
726 "%s: BIOS IRQ %u does not match initial IRQ %u\n", 723 "%s: BIOS IRQ %u does not match initial IRQ %u\n",
727 sc->pl_name, bios_irq, link->l_initial_irq); 724 sc->pl_name, bios_irq, link->l_initial_irq);
728 } else if (bios_irq != link->l_bios_irq) 725 } else if (bios_irq != link->l_bios_irq)
729 printf( 726 printf(
730 "%s: BIOS IRQ %u for %d.%d.INT%c does not match " 727 "%s: BIOS IRQ %u for %d.%d.INT%c does not match "
731 "previous BIOS IRQ %u\n", 728 "previous BIOS IRQ %u\n",
732 sc->pl_name, bios_irq, (int)bus, slot, pin + 'A', 729 sc->pl_name, bios_irq, (int)bus, slot, pin + 'A',
733 link->l_bios_irq); 730 link->l_bios_irq);
734 ACPI_SERIAL_END(pci_link); 731 ACPI_SERIAL_END(pci_link);
735} 732}
736 733
737static ACPI_STATUS 734static ACPI_STATUS
738acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf) 735acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
739{ 736{
740 ACPI_RESOURCE *resource, *end, newres, *resptr; 737 ACPI_RESOURCE *end, *res;
741 ACPI_BUFFER crsbuf; 
742 ACPI_STATUS status; 738 ACPI_STATUS status;
743 struct link *link; 739 struct link *link;
744 int i, in_dpf; 740 int i, in_dpf;
745 741
746 /* Fetch the _CRS. */ 742 /* Fetch the _CRS. */
747 crsbuf.Pointer = NULL; 743 srsbuf->Pointer = NULL;
748 crsbuf.Length = ACPI_ALLOCATE_LOCAL_BUFFER; 744 srsbuf->Length = ACPI_ALLOCATE_BUFFER;
749 status = AcpiGetCurrentResources(sc->pl_handle, &crsbuf); 745 status = AcpiGetCurrentResources(sc->pl_handle, srsbuf);
750 if (ACPI_SUCCESS(status) && crsbuf.Pointer == NULL) 746 if (ACPI_SUCCESS(status) && srsbuf->Pointer == NULL)
751 status = AE_NO_MEMORY; 747 status = AE_NO_MEMORY;
752 if (ACPI_FAILURE(status)) { 748 if (ACPI_FAILURE(status)) {
753 aprint_verbose("%s: Unable to fetch current resources: %s\n", 749 aprint_verbose("%s: Unable to fetch current resources: %s\n",
754 sc->pl_name, AcpiFormatException(status)); 750 sc->pl_name, AcpiFormatException(status));
755 return (status); 751 return (status);
756 } 752 }
757 753
758 /* Fill in IRQ resources via link structures. */ 754 /* Fill in IRQ resources via link structures. */
759 srsbuf->Pointer = NULL; 
760 link = sc->pl_links; 755 link = sc->pl_links;
761 i = 0; 756 i = 0;
762 in_dpf = DPF_OUTSIDE; 757 in_dpf = DPF_OUTSIDE;
763 resource = (ACPI_RESOURCE *)crsbuf.Pointer; 758 res = (ACPI_RESOURCE *)srsbuf->Pointer;
764 end = (ACPI_RESOURCE *)((char *)crsbuf.Pointer + crsbuf.Length); 759 end = (ACPI_RESOURCE *)((char *)srsbuf->Pointer + srsbuf->Length);
765 for (;;) { 760 for (;;) {
766 switch (resource->Type) { 761 switch (res->Type) {
767 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 762 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
768 switch (in_dpf) { 763 switch (in_dpf) {
769 case DPF_OUTSIDE: 764 case DPF_OUTSIDE:
770 /* We've started the first DPF. */ 765 /* We've started the first DPF. */
771 in_dpf = DPF_FIRST; 766 in_dpf = DPF_FIRST;
772 break; 767 break;
773 case DPF_FIRST: 768 case DPF_FIRST:
774 /* We've started the second DPF. */ 769 /* We've started the second DPF. */
775 panic( 770 panic(
776 "%s: Multiple dependent functions within a current resource", 771 "%s: Multiple dependent functions within a current resource",
777 __func__); 772 __func__);
778 break; 773 break;
779 } 774 }
780 resptr = NULL; 
781 break; 775 break;
782 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 776 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
783 /* We are finished with DPF parsing. */ 777 /* We are finished with DPF parsing. */
784 KASSERT(in_dpf != DPF_OUTSIDE); 778 KASSERT(in_dpf != DPF_OUTSIDE);
785 in_dpf = DPF_OUTSIDE; 779 in_dpf = DPF_OUTSIDE;
786 resptr = NULL; 
787 break; 780 break;
788 case ACPI_RESOURCE_TYPE_IRQ: 781 case ACPI_RESOURCE_TYPE_IRQ:
789 newres = link->l_prs_template; 782 res->Data.Irq.InterruptCount = 1;
790 resptr = &newres; 
791 resptr->Data.Irq.InterruptCount = 1; 
792 if (PCI_INTERRUPT_VALID(link->l_irq)) { 783 if (PCI_INTERRUPT_VALID(link->l_irq)) {
793 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS); 784 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS);
794 resptr->Data.Irq.Interrupts[0] = link->l_irq; 785 res->Data.Irq.Interrupts[0] = link->l_irq;
795 resptr->Data.Irq.Triggering = link->l_trig; 786 res->Data.Irq.Triggering = link->l_trig;
796 resptr->Data.Irq.Polarity = link->l_pol; 787 res->Data.Irq.Polarity = link->l_pol;
797 } else 788 } else
798 resptr->Data.Irq.Interrupts[0] = 0; 789 res->Data.Irq.Interrupts[0] = 0;
799 link++; 790 link++;
800 i++; 791 i++;
801 break; 792 break;
802 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 793 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
803 newres = link->l_prs_template; 794 res->Data.ExtendedIrq.InterruptCount = 1;
804 resptr = &newres; 
805 resptr->Data.ExtendedIrq.InterruptCount = 1; 
806 if (PCI_INTERRUPT_VALID(link->l_irq)) { 795 if (PCI_INTERRUPT_VALID(link->l_irq)) {
807 resptr->Data.ExtendedIrq.Interrupts[0] = 796 res->Data.ExtendedIrq.Interrupts[0] =
808 link->l_irq; 797 link->l_irq;
809 resptr->Data.ExtendedIrq.Triggering = 798 res->Data.ExtendedIrq.Triggering =
810 link->l_trig; 799 link->l_trig;
811 resptr->Data.ExtendedIrq.Polarity = link->l_pol; 800 res->Data.ExtendedIrq.Polarity = link->l_pol;
812 } else 801 } else
813 resptr->Data.ExtendedIrq.Interrupts[0] = 0; 802 res->Data.ExtendedIrq.Interrupts[0] = 0;
814 link++; 803 link++;
815 i++; 804 i++;
816 break; 805 break;
817 default: 
818 resptr = resource; 
819 } 
820 if (resptr != NULL) { 
821 status = acpi_AppendBufferResource(srsbuf, resptr); 
822 if (ACPI_FAILURE(status)) { 
823 printf("%s: Unable to build resources: %s\n", 
824 sc->pl_name, AcpiFormatException(status)); 
825 if (srsbuf->Pointer != NULL) 
826 ACPI_FREE(srsbuf->Pointer); 
827 ACPI_FREE(crsbuf.Pointer); 
828 return (status); 
829 } 
830 } 806 }
831 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG) 807 if (res->Type == ACPI_RESOURCE_TYPE_END_TAG)
832 break; 808 break;
833 resource = ACPI_NEXT_RESOURCE(resource); 809 res = ACPI_NEXT_RESOURCE(res);
834 if (resource >= end) 810 if (res >= end)
835 break; 811 break;
836 } 812 }
837 ACPI_FREE(crsbuf.Pointer); 
838 return (AE_OK); 813 return (AE_OK);
839} 814}
840 815
841static ACPI_STATUS 816static ACPI_STATUS
842acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc, 817acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
843 ACPI_BUFFER *srsbuf) 818 ACPI_BUFFER *srsbuf)
844{ 819{
845 ACPI_RESOURCE newres; 820 ACPI_RESOURCE newres;
846 ACPI_STATUS status; 821 ACPI_STATUS status;
847 struct link *link; 822 struct link *link;
848 int i; 823 int i;
849 824
850 /* Start off with an empty buffer. */ 825 /* Start off with an empty buffer. */
851 srsbuf->Pointer = NULL; 826 srsbuf->Pointer = NULL;
852 link = sc->pl_links; 827 link = sc->pl_links;
853 for (i = 0; i < sc->pl_num_links; i++) { 828 for (i = 0; i < sc->pl_num_links; i++) {
854 829
855 /* Add a new IRQ resource from each link. */ 830 /* Add a new IRQ resource from each link. */
856 link = &sc->pl_links[i]; 831 link = &sc->pl_links[i];
857 newres = link->l_prs_template; 832 if (link->l_prs_template.Type == ACPI_RESOURCE_TYPE_IRQ) {
858 if (newres.Type == ACPI_RESOURCE_TYPE_IRQ) { 
859 833
860 /* Build an IRQ resource. */ 834 /* Build an IRQ resource. */
 835 bcopy(&link->l_prs_template, &newres,
 836 ACPI_RS_SIZE(newres.Data.Irq));
861 newres.Data.Irq.InterruptCount = 1; 837 newres.Data.Irq.InterruptCount = 1;
862 if (PCI_INTERRUPT_VALID(link->l_irq)) { 838 if (PCI_INTERRUPT_VALID(link->l_irq)) {
863 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS); 839 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS);
864 newres.Data.Irq.Interrupts[0] = link->l_irq; 840 newres.Data.Irq.Interrupts[0] = link->l_irq;
865 newres.Data.Irq.Triggering = link->l_trig; 841 newres.Data.Irq.Triggering = link->l_trig;
866 newres.Data.Irq.Polarity = link->l_pol; 842 newres.Data.Irq.Polarity = link->l_pol;
867 } else 843 } else
868 newres.Data.Irq.Interrupts[0] = 0; 844 newres.Data.Irq.Interrupts[0] = 0;
869 } else { 845 } else {
870 846
871 /* Build an ExtIRQ resuorce. */ 847 /* Build an ExtIRQ resuorce. */
 848 bcopy(&link->l_prs_template, &newres,
 849 ACPI_RS_SIZE(newres.Data.ExtendedIrq));
872 newres.Data.ExtendedIrq.InterruptCount = 1; 850 newres.Data.ExtendedIrq.InterruptCount = 1;
873 if (PCI_INTERRUPT_VALID(link->l_irq)) { 851 if (PCI_INTERRUPT_VALID(link->l_irq)) {
874 newres.Data.ExtendedIrq.Interrupts[0] = 852 newres.Data.ExtendedIrq.Interrupts[0] =
875 link->l_irq; 853 link->l_irq;
876 newres.Data.ExtendedIrq.Triggering = 854 newres.Data.ExtendedIrq.Triggering =
877 link->l_trig; 855 link->l_trig;
878 newres.Data.ExtendedIrq.Polarity = 856 newres.Data.ExtendedIrq.Polarity =
879 link->l_pol; 857 link->l_pol;
880 } else { 858 } else {
881 newres.Data.ExtendedIrq.Interrupts[0] = 0; 859 newres.Data.ExtendedIrq.Interrupts[0] = 0;
882 } 860 }
883 } 861 }
884 862
885 /* Add the new resource to the end of the _SRS buffer. */ 863 /* Add the new resource to the end of the _SRS buffer. */
886 status = acpi_AppendBufferResource(srsbuf, &newres); 864 status = acpi_AppendBufferResource(srsbuf, &newres);
887 if (ACPI_FAILURE(status)) { 865 if (ACPI_FAILURE(status)) {
888 printf("%s: Unable to build resources: %s\n", 866 printf("%s: Unable to build resources: %s\n",
889 sc->pl_name, AcpiFormatException(status)); 867 sc->pl_name, AcpiFormatException(status));
890 if (srsbuf->Pointer != NULL) 868 if (srsbuf->Pointer != NULL)
891 ACPI_FREE(srsbuf->Pointer); 869 ACPI_FREE(srsbuf->Pointer);
892 return (status); 870 return (status);
893 } 871 }
894 } 872 }
895 return (AE_OK); 873 return (AE_OK);
896} 874}
897 875
898static ACPI_STATUS 876static ACPI_STATUS
899acpi_pci_link_srs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf) 877acpi_pci_link_srs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
900{ 878{
901 ACPI_STATUS status; 879 ACPI_STATUS status;
902 880
903 if (sc->pl_crs_bad) 881 if (sc->pl_crs_bad)
904 status = acpi_pci_link_srs_from_links(sc, srsbuf); 882 status = acpi_pci_link_srs_from_links(sc, srsbuf);
905 else 883 else
906 status = acpi_pci_link_srs_from_crs(sc, srsbuf); 884 status = acpi_pci_link_srs_from_crs(sc, srsbuf);
907 885
908 if (ACPI_FAILURE(status)) 886 if (ACPI_FAILURE(status))
909 printf("%s: Unable to find link srs : %s\n", 887 printf("%s: Unable to find link srs : %s\n",
910 sc->pl_name, AcpiFormatException(status)); 888 sc->pl_name, AcpiFormatException(status));
911 889
912 /* Write out new resources via _SRS. */ 890 /* Write out new resources via _SRS. */
913 return AcpiSetCurrentResources(sc->pl_handle, srsbuf); 891 return AcpiSetCurrentResources(sc->pl_handle, srsbuf);
914} 892}
915 893
916static ACPI_STATUS 894static ACPI_STATUS
917acpi_pci_link_route_irqs(struct acpi_pci_link_softc *sc, int *irq, int *pol, 895acpi_pci_link_route_irqs(struct acpi_pci_link_softc *sc, int *irq, int *pol,
918 int *trig) 896 int *trig)
919{ 897{
920 ACPI_RESOURCE *resource, *end; 898 ACPI_RESOURCE *resource, *end;
921 ACPI_BUFFER srsbuf; 899 ACPI_BUFFER srsbuf;
922 ACPI_STATUS status; 900 ACPI_STATUS status;
923 struct link *link; 901 struct link *link;
924 int i, is_ext = 0; 902 int i, is_ext = 0;
925 903
926 status = acpi_pci_link_srs(sc, &srsbuf); 904 status = acpi_pci_link_srs(sc, &srsbuf);
927 if (ACPI_FAILURE(status)) { 905 if (ACPI_FAILURE(status)) {
928 printf("%s: _SRS failed: %s\n", 906 printf("%s: _SRS failed: %s\n",
929 sc->pl_name, AcpiFormatException(status)); 907 sc->pl_name, AcpiFormatException(status));
930 return (status); 908 return (status);
931 } 909 }
932 /* 910 /*
933 * Perform acpi_config_intr() on each IRQ resource if it was just 911 * Perform acpi_config_intr() on each IRQ resource if it was just
934 * routed for the first time. 912 * routed for the first time.
935 */ 913 */
936 link = sc->pl_links; 914 link = sc->pl_links;
937 i = 0; 915 i = 0;
938 resource = (ACPI_RESOURCE *)srsbuf.Pointer; 916 resource = (ACPI_RESOURCE *)srsbuf.Pointer;
939 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length); 917 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
940 for (;;) { 918 for (;;) {
941 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG) 919 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
942 break; 920 break;
943 switch (resource->Type) { 921 switch (resource->Type) {
944 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 922 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
945 is_ext = 1; 923 is_ext = 1;
946 /* FALLTHROUGH */ 924 /* FALLTHROUGH */
947 case ACPI_RESOURCE_TYPE_IRQ: 925 case ACPI_RESOURCE_TYPE_IRQ:
948 /* 926 /*
949 * Only configure the interrupt and update the 927 * Only configure the interrupt and update the
950 * weights if this link has a valid IRQ and was 928 * weights if this link has a valid IRQ and was
951 * previously unrouted. 929 * previously unrouted.
952 */ 930 */
953 if (!link->l_routed && 931 if (!link->l_routed &&
954 PCI_INTERRUPT_VALID(link->l_irq)) { 932 PCI_INTERRUPT_VALID(link->l_irq)) {
955 *trig = is_ext ? 933 *trig = is_ext ?
956 resource->Data.ExtendedIrq.Triggering : 934 resource->Data.ExtendedIrq.Triggering :
957 resource->Data.Irq.Triggering; 935 resource->Data.Irq.Triggering;
958 *pol = is_ext ? 936 *pol = is_ext ?
959 resource->Data.ExtendedIrq.Polarity : 937 resource->Data.ExtendedIrq.Polarity :
960 resource->Data.Irq.Polarity; 938 resource->Data.Irq.Polarity;
961 *irq = is_ext ? 939 *irq = is_ext ?
962 resource->Data.ExtendedIrq.Interrupts[0] : 940 resource->Data.ExtendedIrq.Interrupts[0] :
963 resource->Data.Irq.Interrupts[0]; 941 resource->Data.Irq.Interrupts[0];
964 link->l_routed = TRUE; 942 link->l_routed = TRUE;
965 pci_link_interrupt_weights[link->l_irq] += 943 pci_link_interrupt_weights[link->l_irq] +=
966 link->l_references; 944 link->l_references;
967 } 945 }
968 link++; 946 link++;
969 i++; 947 i++;
970 break; 948 break;
971 } 949 }
972 resource = ACPI_NEXT_RESOURCE(resource); 950 resource = ACPI_NEXT_RESOURCE(resource);
973 if (resource >= end) 951 if (resource >= end)
974 break; 952 break;
975 } 953 }
976 ACPI_FREE(srsbuf.Pointer); 954 ACPI_FREE(srsbuf.Pointer);
977 return (AE_OK); 955 return (AE_OK);
978} 956}
979 957
980/* 958/*
981 * Pick an IRQ to use for this unrouted link. 959 * Pick an IRQ to use for this unrouted link.
982 */ 960 */
983static uint8_t 961static uint8_t
984acpi_pci_link_choose_irq(struct acpi_pci_link_softc *sc, struct link *link) 962acpi_pci_link_choose_irq(struct acpi_pci_link_softc *sc, struct link *link)
985{ 963{
986 u_int8_t best_irq, pos_irq; 964 u_int8_t best_irq, pos_irq;
987 int best_weight, pos_weight, i; 965 int best_weight, pos_weight, i;
988 966
989 KASSERT(!link->l_routed); 967 KASSERT(!link->l_routed);
990 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq)); 968 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq));
991 969
992 /* 970 /*
993 * If we have a valid BIOS IRQ, use that. We trust what the BIOS 971 * If we have a valid BIOS IRQ, use that. We trust what the BIOS
994 * says it routed over what _CRS says the link thinks is routed. 972 * says it routed over what _CRS says the link thinks is routed.
995 */ 973 */
996 if (PCI_INTERRUPT_VALID(link->l_bios_irq)) 974 if (PCI_INTERRUPT_VALID(link->l_bios_irq))
997 return (link->l_bios_irq); 975 return (link->l_bios_irq);
998 976
999 /* 977 /*
1000 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS, 978 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
1001 * then use that. 979 * then use that.
1002 */ 980 */
1003 if (PCI_INTERRUPT_VALID(link->l_initial_irq)) 981 if (PCI_INTERRUPT_VALID(link->l_initial_irq))
1004 return (link->l_initial_irq); 982 return (link->l_initial_irq);
1005 983
1006 /* 984 /*
1007 * Ok, we have no useful hints, so we have to pick from the 985 * Ok, we have no useful hints, so we have to pick from the
1008 * possible IRQs. For ISA IRQs we only use interrupts that 986 * possible IRQs. For ISA IRQs we only use interrupts that
1009 * have already been used by the BIOS. 987 * have already been used by the BIOS.
1010 */ 988 */
1011 best_irq = PCI_INVALID_IRQ; 989 best_irq = PCI_INVALID_IRQ;
1012 best_weight = INT_MAX; 990 best_weight = INT_MAX;
1013 for (i = 0; i < link->l_num_irqs; i++) { 991 for (i = 0; i < link->l_num_irqs; i++) {
1014 pos_irq = link->l_irqs[i]; 992 pos_irq = link->l_irqs[i];
1015 if (pos_irq < NUM_ISA_INTERRUPTS && 993 if (pos_irq < NUM_ISA_INTERRUPTS &&
1016 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0) 994 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0)
1017 continue; 995 continue;
1018 pos_weight = pci_link_interrupt_weights[pos_irq]; 996 pos_weight = pci_link_interrupt_weights[pos_irq];
1019 if (pos_weight < best_weight) { 997 if (pos_weight < best_weight) {
1020 best_weight = pos_weight; 998 best_weight = pos_weight;
1021 best_irq = pos_irq; 999 best_irq = pos_irq;
1022 } 1000 }
1023 } 1001 }
1024 1002
1025 /* 1003 /*
1026 * If this is an ISA IRQ, try using the SCI if it is also an ISA 1004 * If this is an ISA IRQ, try using the SCI if it is also an ISA
1027 * interrupt as a fallback. 1005 * interrupt as a fallback.
1028 */ 1006 */
1029 if (link->l_isa_irq && !PCI_INTERRUPT_VALID(best_irq)) { 1007 if (link->l_isa_irq && !PCI_INTERRUPT_VALID(best_irq)) {
1030 pos_irq = AcpiGbl_FADT.SciInterrupt; 1008 pos_irq = AcpiGbl_FADT.SciInterrupt;
1031 pos_weight = pci_link_interrupt_weights[pos_irq]; 1009 pos_weight = pci_link_interrupt_weights[pos_irq];
1032 if (pos_weight < best_weight) { 1010 if (pos_weight < best_weight) {
1033 best_weight = pos_weight; 1011 best_weight = pos_weight;
1034 best_irq = pos_irq; 1012 best_irq = pos_irq;
1035 } 1013 }
1036 } 1014 }
1037 1015
1038 if (PCI_INTERRUPT_VALID(best_irq)) { 1016 if (PCI_INTERRUPT_VALID(best_irq)) {
1039 aprint_verbose("%s: Picked IRQ %u with weight %d\n", 1017 aprint_verbose("%s: Picked IRQ %u with weight %d\n",
1040 sc->pl_name, best_irq, best_weight); 1018 sc->pl_name, best_irq, best_weight);
1041 } else 1019 } else
1042 printf("%s: Unable to choose an IRQ\n", sc->pl_name); 1020 printf("%s: Unable to choose an IRQ\n", sc->pl_name);
1043 return (best_irq); 1021 return (best_irq);
1044} 1022}
1045 1023
1046int 1024int
1047acpi_pci_link_route_interrupt(void *v, int index, int *irq, int *pol, int *trig) 1025acpi_pci_link_route_interrupt(void *v, int index, int *irq, int *pol, int *trig)
1048{ 1026{
1049 struct acpi_pci_link_softc *sc = v; 1027 struct acpi_pci_link_softc *sc = v;
1050 struct link *link; 1028 struct link *link;
1051 int i; 1029 int i;
1052 pcireg_t reg; 1030 pcireg_t reg;
1053 1031
1054 ACPI_SERIAL_BEGIN(pci_link); 1032 ACPI_SERIAL_BEGIN(pci_link);
1055 link = acpi_pci_link_lookup(sc, index); 1033 link = acpi_pci_link_lookup(sc, index);
1056 if (link == NULL) 1034 if (link == NULL)
1057 panic("%s: apparently invalid index %d", __func__, index); 1035 panic("%s: apparently invalid index %d", __func__, index);
1058 1036
1059 /* 1037 /*
1060 * If this link device is already routed to an interrupt, just return 1038 * If this link device is already routed to an interrupt, just return
1061 * the interrupt it is routed to. 1039 * the interrupt it is routed to.
1062 */ 1040 */
1063 if (link->l_routed) { 1041 if (link->l_routed) {
1064 KASSERT(PCI_INTERRUPT_VALID(link->l_irq)); 1042 KASSERT(PCI_INTERRUPT_VALID(link->l_irq));
1065 ACPI_SERIAL_END(pci_link); 1043 ACPI_SERIAL_END(pci_link);
1066 *irq = link->l_irq; 1044 *irq = link->l_irq;
1067 *pol = link->l_pol; 1045 *pol = link->l_pol;
1068 *trig = link->l_trig; 1046 *trig = link->l_trig;
1069 return (link->l_irq); 1047 return (link->l_irq);
1070 } 1048 }
1071 1049
1072 /* Choose an IRQ if we need one. */ 1050 /* Choose an IRQ if we need one. */
1073 if (PCI_INTERRUPT_VALID(link->l_irq)) { 1051 if (PCI_INTERRUPT_VALID(link->l_irq)) {
1074 *irq = link->l_irq; 1052 *irq = link->l_irq;
1075 *pol = link->l_pol; 1053 *pol = link->l_pol;
1076 *trig = link->l_trig; 1054 *trig = link->l_trig;
1077 goto done; 1055 goto done;
1078 } 1056 }
1079 1057
1080 link->l_irq = acpi_pci_link_choose_irq(sc, link); 1058 link->l_irq = acpi_pci_link_choose_irq(sc, link);
1081 1059
1082 /* 1060 /*
1083 * Try to route the interrupt we picked. If it fails, then 1061 * Try to route the interrupt we picked. If it fails, then
1084 * assume the interrupt is not routed. 1062 * assume the interrupt is not routed.
1085 */ 1063 */
1086 if (!PCI_INTERRUPT_VALID(link->l_irq)) 1064 if (!PCI_INTERRUPT_VALID(link->l_irq))
1087 goto done; 1065 goto done;
1088 1066
1089 acpi_pci_link_route_irqs(sc, irq, pol, trig); 1067 acpi_pci_link_route_irqs(sc, irq, pol, trig);
1090 if (!link->l_routed) { 1068 if (!link->l_routed) {
1091 link->l_irq = PCI_INVALID_IRQ; 1069 link->l_irq = PCI_INVALID_IRQ;
1092 goto done; 1070 goto done;
1093 } 1071 }
1094 1072
1095 link->l_pol = *pol; 1073 link->l_pol = *pol;
1096 link->l_trig = *trig; 1074 link->l_trig = *trig;
1097 for (i = 0; i < link->l_dev_count; ++i) { 1075 for (i = 0; i < link->l_dev_count; ++i) {
1098 reg = pci_conf_read(acpi_softc->sc_pc, link->l_devices[i], 1076 reg = pci_conf_read(acpi_softc->sc_pc, link->l_devices[i],
1099 PCI_INTERRUPT_REG); 1077 PCI_INTERRUPT_REG);
1100 reg &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT); 1078 reg &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
1101 reg |= link->l_irq << PCI_INTERRUPT_LINE_SHIFT; 1079 reg |= link->l_irq << PCI_INTERRUPT_LINE_SHIFT;
1102 pci_conf_write(acpi_softc->sc_pc, link->l_devices[i], 1080 pci_conf_write(acpi_softc->sc_pc, link->l_devices[i],
1103 PCI_INTERRUPT_REG, reg); 1081 PCI_INTERRUPT_REG, reg);
1104 } 1082 }
1105 1083
1106done: 1084done:
1107 ACPI_SERIAL_END(pci_link); 1085 ACPI_SERIAL_END(pci_link);
1108 1086
1109 return (link->l_irq); 1087 return (link->l_irq);
1110} 1088}
1111 1089
1112/* 1090/*
1113 * This is gross, but we abuse the identify routine to perform one-time 1091 * This is gross, but we abuse the identify routine to perform one-time
1114 * SYSINIT() style initialization for the driver. 1092 * SYSINIT() style initialization for the driver.
1115 */ 1093 */
1116static void 1094static void
1117acpi_pci_link_init(struct acpi_pci_link_softc *sc) 1095acpi_pci_link_init(struct acpi_pci_link_softc *sc)
1118{ 1096{
1119 ACPI_BUFFER buf; 1097 ACPI_BUFFER buf;
1120 1098
1121 /* 1099 /*
1122 * If the SCI is an ISA IRQ, add it to the bitmask of known good 1100 * If the SCI is an ISA IRQ, add it to the bitmask of known good
1123 * ISA IRQs. 1101 * ISA IRQs.
1124 * 1102 *
1125 * XXX: If we are using the APIC, the SCI might have been 1103 * XXX: If we are using the APIC, the SCI might have been
1126 * rerouted to an APIC pin in which case this is invalid. However, 1104 * rerouted to an APIC pin in which case this is invalid. However,
1127 * if we are using the APIC, we also shouldn't be having any PCI 1105 * if we are using the APIC, we also shouldn't be having any PCI
1128 * interrupts routed via ISA IRQs, so this is probably ok. 1106 * interrupts routed via ISA IRQs, so this is probably ok.
1129 */ 1107 */
1130 if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS) 1108 if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS)
1131 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt); 1109 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt);
1132 1110
1133 buf.Length = sizeof (sc->pl_name); 1111 buf.Length = sizeof (sc->pl_name);
1134 buf.Pointer = sc->pl_name; 1112 buf.Pointer = sc->pl_name;
1135 1113
1136 if (ACPI_FAILURE(AcpiGetName(sc->pl_handle, ACPI_SINGLE_NAME, &buf))) 1114 if (ACPI_FAILURE(AcpiGetName(sc->pl_handle, ACPI_SINGLE_NAME, &buf)))
1137 snprintf(sc->pl_name, sizeof (sc->pl_name), "%s", 1115 snprintf(sc->pl_name, sizeof (sc->pl_name), "%s",
1138 "ACPI link device"); 1116 "ACPI link device");
1139 1117
1140 acpi_pci_link_attach(sc); 1118 acpi_pci_link_attach(sc);
1141} 1119}
1142 1120
1143void * 1121void *
1144acpi_pci_link_devbyhandle(ACPI_HANDLE handle) 1122acpi_pci_link_devbyhandle(ACPI_HANDLE handle)
1145{ 1123{
1146 struct acpi_pci_link_softc *sc; 1124 struct acpi_pci_link_softc *sc;
1147 1125
1148 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) { 1126 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) {
1149 if (sc->pl_handle == handle) 1127 if (sc->pl_handle == handle)
1150 return sc; 1128 return sc;
1151 } 1129 }
1152 1130
1153 sc = malloc(sizeof (*sc), M_ACPI, M_NOWAIT | M_ZERO); 1131 sc = malloc(sizeof (*sc), M_ACPI, M_NOWAIT | M_ZERO);
1154 if (sc == NULL) 1132 if (sc == NULL)
1155 return NULL; 1133 return NULL;
1156 1134
1157 sc->pl_handle = handle; 1135 sc->pl_handle = handle;
1158 1136
1159 acpi_pci_link_init(sc); 1137 acpi_pci_link_init(sc);
1160 1138
1161 TAILQ_INSERT_TAIL(&acpi_pci_linkdevs, sc, pl_list); 1139 TAILQ_INSERT_TAIL(&acpi_pci_linkdevs, sc, pl_list);
1162 1140
1163 return (void *)sc; 1141 return (void *)sc;
1164} 1142}
1165 1143
1166void 1144void
1167acpi_pci_link_resume(void) 1145acpi_pci_link_resume(void)
1168{ 1146{
1169 struct acpi_pci_link_softc *sc; 1147 struct acpi_pci_link_softc *sc;
1170 ACPI_BUFFER srsbuf; 1148 ACPI_BUFFER srsbuf;
1171 1149
1172 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) { 1150 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) {
1173 ACPI_SERIAL_BEGIN(pci_link); 1151 ACPI_SERIAL_BEGIN(pci_link);
1174 if (ACPI_SUCCESS(acpi_pci_link_srs(sc, &srsbuf))) 1152 if (ACPI_SUCCESS(acpi_pci_link_srs(sc, &srsbuf)))
1175 ACPI_FREE(srsbuf.Pointer); 1153 ACPI_FREE(srsbuf.Pointer);
1176 ACPI_SERIAL_END(pci_link); 1154 ACPI_SERIAL_END(pci_link);
1177 } 1155 }
1178} 1156}
1179 1157
1180ACPI_HANDLE 1158ACPI_HANDLE
1181acpi_pci_link_handle(void *v) 1159acpi_pci_link_handle(void *v)
1182{ 1160{
1183 struct acpi_pci_link_softc *sc = v; 1161 struct acpi_pci_link_softc *sc = v;
1184 1162
1185 return sc->pl_handle; 1163 return sc->pl_handle;
1186} 1164}
1187 1165
1188char * 1166char *
1189acpi_pci_link_name(void *v) 1167acpi_pci_link_name(void *v)
1190{ 1168{
1191 struct acpi_pci_link_softc *sc = v; 1169 struct acpi_pci_link_softc *sc = v;
1192 1170
1193 return sc->pl_name; 1171 return sc->pl_name;
1194} 1172}
1195 1173
1196 1174
1197/* 1175/*
1198 * Append an ACPI_RESOURCE to an ACPI_BUFFER. 1176 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
1199 * 1177 *
1200 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 1178 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
1201 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 1179 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
1202 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 1180 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
1203 * resources. 1181 * resources.
1204 */ 1182 */
1205#define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 1183#define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
1206 1184
1207static ACPI_STATUS 1185static ACPI_STATUS
1208acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 1186acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
1209{ 1187{
1210 ACPI_RESOURCE *rp; 1188 ACPI_RESOURCE *rp;
1211 void *newp; 1189 void *newp;
1212 1190
1213 /* Initialise the buffer if necessary. */ 1191 /* Initialise the buffer if necessary. */
1214 if (buf->Pointer == NULL) { 1192 if (buf->Pointer == NULL) {
1215 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 1193 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
1216 if ((buf->Pointer = ACPI_ALLOCATE(buf->Length)) == NULL) 1194 if ((buf->Pointer = ACPI_ALLOCATE(buf->Length)) == NULL)
1217 return (AE_NO_MEMORY); 1195 return (AE_NO_MEMORY);
1218 rp = (ACPI_RESOURCE *)buf->Pointer; 1196 rp = (ACPI_RESOURCE *)buf->Pointer;
1219 rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 1197 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
1220 rp->Length = 0; 1198 rp->Length = 0;
1221 } 1199 }
1222 1200
1223 if (res == NULL) 1201 if (res == NULL)
1224 return (AE_OK); 1202 return (AE_OK);
1225 1203
1226 /* 1204 /*
1227 * Scan the current buffer looking for the terminator. 1205 * Scan the current buffer looking for the terminator.
1228 * This will either find the terminator or hit the end 1206 * This will either find the terminator or hit the end
1229 * of the buffer and return an error. 1207 * of the buffer and return an error.
1230 */ 1208 */
1231 rp = (ACPI_RESOURCE *)buf->Pointer; 1209 rp = (ACPI_RESOURCE *)buf->Pointer;
1232 for (;;) { 1210 for (;;) {
1233 /* Range check, don't go outside the buffer */ 1211 /* Range check, don't go outside the buffer */
1234 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + 1212 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer +
1235 buf->Length)) 1213 buf->Length))
1236 return (AE_BAD_PARAMETER); 1214 return (AE_BAD_PARAMETER);
1237 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 1215 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
1238 break; 1216 break;
1239 rp = ACPI_NEXT_RESOURCE(rp); 1217 rp = ACPI_NEXT_RESOURCE(rp);
1240 } 1218 }
1241 1219
1242 /* 1220 /*
1243 * Check the size of the buffer and expand if required. 1221 * Check the size of the buffer and expand if required.
1244 * 1222 *
1245 * Required size is: 1223 * Required size is:
1246 * size of existing resources before terminator +  1224 * size of existing resources before terminator +
1247 * size of new resource and header + 1225 * size of new resource and header +
1248 * size of terminator. 1226 * size of terminator.
1249 * 1227 *
1250 * Note that this loop should really only run once, unless 1228 * Note that this loop should really only run once, unless
1251 * for some reason we are stuffing a *really* huge resource. 1229 * for some reason we are stuffing a *really* huge resource.
1252 */ 1230 */
1253 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +  1231 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
1254 res->Length + ACPI_RS_SIZE_NO_DATA + 1232 res->Length + ACPI_RS_SIZE_NO_DATA +
1255 ACPI_RS_SIZE_MIN) >= buf->Length) { 1233 ACPI_RS_SIZE_MIN) >= buf->Length) {
1256 if ((newp = ACPI_ALLOCATE(buf->Length * 2)) == NULL) 1234 if ((newp = ACPI_ALLOCATE(buf->Length * 2)) == NULL)
1257 return (AE_NO_MEMORY); 1235 return (AE_NO_MEMORY);
1258 memcpy(newp, buf->Pointer, buf->Length); 1236 memcpy(newp, buf->Pointer, buf->Length);
1259 rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1237 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1260 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 1238 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
1261 ACPI_FREE(buf->Pointer); 1239 ACPI_FREE(buf->Pointer);
1262 buf->Pointer = newp; 1240 buf->Pointer = newp;
1263 buf->Length += buf->Length; 1241 buf->Length += buf->Length;
1264 } 1242 }
1265 1243
1266 /* Insert the new resource. */ 1244 /* Insert the new resource. */
1267 memcpy(rp, res, res->Length); 1245 memcpy(rp, res, res->Length);
1268 1246
1269 /* And add the terminator. */ 1247 /* And add the terminator. */
1270 rp = ACPI_NEXT_RESOURCE(rp); 1248 rp = ACPI_NEXT_RESOURCE(rp);
1271 rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 1249 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
1272 rp->Length = 0; 1250 rp->Length = 0;
1273 1251
1274 return (AE_OK); 1252 return (AE_OK);
1275} 1253}