Bug Summary

File:dev/cardbus/cardbus.c
Warning:line 132, column 2
Value stored to 'cdstatus' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name cardbus.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -ffreestanding -mcmodel=kernel -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -target-feature -sse2 -target-feature -sse -target-feature -3dnow -target-feature -mmx -target-feature +save-args -disable-red-zone -no-implicit-float -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -nostdsysteminc -nobuiltininc -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/sys -I /usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -I /usr/src/sys/arch -I /usr/src/sys/dev/pci/drm/include -I /usr/src/sys/dev/pci/drm/include/uapi -I /usr/src/sys/dev/pci/drm/amd/include/asic_reg -I /usr/src/sys/dev/pci/drm/amd/include -I /usr/src/sys/dev/pci/drm/amd/amdgpu -I /usr/src/sys/dev/pci/drm/amd/display -I /usr/src/sys/dev/pci/drm/amd/display/include -I /usr/src/sys/dev/pci/drm/amd/display/dc -I /usr/src/sys/dev/pci/drm/amd/display/amdgpu_dm -I /usr/src/sys/dev/pci/drm/amd/pm/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu11 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu12 -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/hwmgr -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/smumgr -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc/hw -I /usr/src/sys/dev/pci/drm/amd/display/dc/clk_mgr -I /usr/src/sys/dev/pci/drm/amd/display/modules/inc -I /usr/src/sys/dev/pci/drm/amd/display/modules/hdcp -I /usr/src/sys/dev/pci/drm/amd/display/dmub/inc -I /usr/src/sys/dev/pci/drm/i915 -D DDB -D DIAGNOSTIC -D KTRACE -D ACCOUNTING -D KMEMSTATS -D PTRACE -D POOL_DEBUG -D CRYPTO -D SYSVMSG -D SYSVSEM -D SYSVSHM -D UVM_SWAP_ENCRYPT -D FFS -D FFS2 -D FFS_SOFTUPDATES -D UFS_DIRHASH -D QUOTA -D EXT2FS -D MFS -D NFSCLIENT -D NFSSERVER -D CD9660 -D UDF -D MSDOSFS -D FIFO -D FUSE -D SOCKET_SPLICE -D TCP_ECN -D TCP_SIGNATURE -D INET6 -D IPSEC -D PPP_BSDCOMP -D PPP_DEFLATE -D PIPEX -D MROUTING -D MPLS -D BOOT_CONFIG -D USER_PCICONF -D APERTURE -D MTRR -D NTFS -D HIBERNATE -D PCIVERBOSE -D USBVERBOSE -D WSDISPLAY_COMPAT_USL -D WSDISPLAY_COMPAT_RAWKBD -D WSDISPLAY_DEFAULTSCREENS=6 -D X86EMU -D ONEWIREVERBOSE -D MULTIPROCESSOR -D MAXUSERS=80 -D _KERNEL -D CONFIG_DRM_AMD_DC_DCN3_0 -O2 -Wno-pointer-sign -Wno-address-of-packed-member -Wno-constant-conversion -Wno-unused-but-set-variable -Wno-gnu-folding-constant -fdebug-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /usr/obj/sys/arch/amd64/compile/GENERIC.MP/scan-build/2022-01-12-131800-47421-1 -x c /usr/src/sys/dev/cardbus/cardbus.c
1/* $OpenBSD: cardbus.c,v 1.52 2019/11/30 18:19:51 cheloha Exp $ */
2/* $NetBSD: cardbus.c,v 1.24 2000/04/02 19:11:37 mycroft Exp $ */
3
4/*
5 * Copyright (c) 1997, 1998, 1999 and 2000
6 * HAYAKAWA Koichi. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/device.h>
34#include <sys/malloc.h>
35#include <sys/kernel.h>
36
37#include <machine/bus.h>
38
39#include <dev/cardbus/cardbusvar.h>
40#include <dev/pci/pcidevs.h>
41
42#include <dev/cardbus/cardbus_exrom.h>
43
44#include <dev/pci/pcivar.h> /* XXX */
45#include <dev/pci/pcireg.h> /* XXX */
46
47#include <dev/pcmcia/pcmciareg.h>
48
49#ifdef CARDBUS_DEBUG
50#define STATIC
51#define DPRINTF(a) printf a
52#else
53#ifdef DDB1
54#define STATIC
55#else
56#define STATIC static
57#endif
58#define DPRINTF(a)
59#endif
60
61STATIC void cardbusattach(struct device *, struct device *, void *);
62/* STATIC int cardbusprint(void *, const char *); */
63
64STATIC int cardbusmatch(struct device *, void *, void *);
65STATIC int cardbussubmatch(struct device *, void *, void *);
66STATIC int cardbusprint(void *, const char *);
67
68typedef void (*tuple_decode_func)(u_int8_t *, int, void *);
69
70STATIC int decode_tuples(u_int8_t *, int, tuple_decode_func, void *);
71STATIC void parse_tuple(u_int8_t *, int, void *);
72#ifdef CARDBUS_DEBUG
73static void print_tuple(u_int8_t *, int, void *);
74#endif
75
76STATIC int cardbus_read_tuples(struct cardbus_attach_args *,
77 pcireg_t, u_int8_t *, size_t);
78
79STATIC void enable_function(struct cardbus_softc *, int, int);
80STATIC void disable_function(struct cardbus_softc *, int);
81
82
83struct cfattach cardbus_ca = {
84 sizeof(struct cardbus_softc), cardbusmatch, cardbusattach
85};
86
87struct cfdriver cardbus_cd = {
88 NULL((void *)0), "cardbus", DV_DULL
89};
90
91STATIC int
92cardbusmatch(struct device *parent, void *match, void *aux)
93{
94 struct cfdata *cf = match;
95 struct cbslot_attach_args *cba = aux;
96
97 if (strcmp(cba->cba_busname, cf->cf_driver->cd_name)) {
98 DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
99 cba->cba_busname, cf->cf_driver->cd_name));
100 return (0);
101 }
102
103 return (1);
104}
105
106STATIC void
107cardbusattach(struct device *parent, struct device *self, void *aux)
108{
109 struct cardbus_softc *sc = (void *)self;
110 struct cbslot_attach_args *cba = aux;
111 int cdstatus;
112
113 sc->sc_bus = cba->cba_bus;
114 sc->sc_device = 0;
115 sc->sc_intrline = cba->cba_intrline;
116 sc->sc_cacheline = cba->cba_cacheline;
117 sc->sc_lattimer = cba->cba_lattimer;
118
119 printf(": bus %d device %d", sc->sc_bus, sc->sc_device);
120 printf(" cacheline 0x%x, lattimer 0x%x\n",
121 sc->sc_cacheline,sc->sc_lattimer);
122
123 sc->sc_iot = cba->cba_iot; /* CardBus I/O space tag */
124 sc->sc_memt = cba->cba_memt; /* CardBus MEM space tag */
125 sc->sc_dmat = cba->cba_dmat; /* DMA tag */
126 sc->sc_cc = cba->cba_cc;
127 sc->sc_pc = cba->cba_pc;
128 sc->sc_cf = cba->cba_cf;
129 sc->sc_rbus_iot = cba->cba_rbus_iot;
130 sc->sc_rbus_memt = cba->cba_rbus_memt;
131
132 cdstatus = 0;
Value stored to 'cdstatus' is never read
133}
134
135STATIC int
136cardbus_read_tuples(struct cardbus_attach_args *ca, pcireg_t cis_ptr,
137 u_int8_t *tuples, size_t len)
138{
139 struct cardbus_softc *sc = ca->ca_ct->ct_sc;
140 pci_chipset_tag_t pc = ca->ca_pc;
141 pcitag_t tag = ca->ca_tag;
142 pcireg_t command;
143 int found = 0;
144
145 int i, j;
146 int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK0x07;
147 bus_space_tag_t bar_tag;
148 bus_space_handle_t bar_memh;
149 bus_size_t bar_size;
150 bus_addr_t bar_addr;
151
152 int reg;
153
154 memset(tuples, 0, len)__builtin_memset((tuples), (0), (len));
155
156 cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK0x0ffffff8;
157
158 switch (cardbus_space) {
159 case CARDBUS_CIS_ASI_TUPLE0x00:
160 DPRINTF(("%s: reading CIS data from configuration space\n",
161 sc->sc_dev.dv_xname));
162 for (i = cis_ptr, j = 0; i < 0xff; i += 4) {
163 u_int32_t e = pci_conf_read(pc, tag, i);
164 tuples[j] = 0xff & e;
165 e >>= 8;
166 tuples[j + 1] = 0xff & e;
167 e >>= 8;
168 tuples[j + 2] = 0xff & e;
169 e >>= 8;
170 tuples[j + 3] = 0xff & e;
171 j += 4;
172 }
173 found++;
174 break;
175
176 case CARDBUS_CIS_ASI_BAR00x01:
177 case CARDBUS_CIS_ASI_BAR10x02:
178 case CARDBUS_CIS_ASI_BAR20x03:
179 case CARDBUS_CIS_ASI_BAR30x04:
180 case CARDBUS_CIS_ASI_BAR40x05:
181 case CARDBUS_CIS_ASI_BAR50x06:
182 case CARDBUS_CIS_ASI_ROM0x07:
183 if (cardbus_space == CARDBUS_CIS_ASI_ROM0x07) {
184 reg = CARDBUS_ROM_REG0x30;
185 DPRINTF(("%s: reading CIS data from ROM\n",
186 sc->sc_dev.dv_xname));
187 } else {
188 reg = CARDBUS_BASE0_REG0x10 + (cardbus_space - 1) * 4;
189 DPRINTF(("%s: reading CIS data from BAR%d\n",
190 sc->sc_dev.dv_xname, cardbus_space - 1));
191 }
192
193 /* XXX zero register so mapreg_map doesn't get confused by old
194 contents */
195 pci_conf_write(pc, tag, reg, 0);
196 if (Cardbus_mapreg_map(ca->ca_ct, reg,cardbus_mapreg_map((ca->ca_ct)->ct_sc, (ca->ca_ct->
ct_func), (reg), (0x00000000 | 0x00000000), (0), (&bar_tag
), (&bar_memh), (&bar_addr), (&bar_size))
197 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,cardbus_mapreg_map((ca->ca_ct)->ct_sc, (ca->ca_ct->
ct_func), (reg), (0x00000000 | 0x00000000), (0), (&bar_tag
), (&bar_memh), (&bar_addr), (&bar_size))
198 &bar_tag, &bar_memh, &bar_addr, &bar_size)cardbus_mapreg_map((ca->ca_ct)->ct_sc, (ca->ca_ct->
ct_func), (reg), (0x00000000 | 0x00000000), (0), (&bar_tag
), (&bar_memh), (&bar_addr), (&bar_size))
) {
199 printf("%s: can't map memory\n",
200 sc->sc_dev.dv_xname);
201 return (1);
202 }
203
204 if (cardbus_space == CARDBUS_CIS_ASI_ROM0x07) {
205 pcireg_t exrom;
206 int save;
207 struct cardbus_rom_image_head rom_image;
208 struct cardbus_rom_image *p;
209
210 save = splhigh()splraise(0xd);
211 /* enable rom address decoder */
212 exrom = pci_conf_read(pc, tag, reg);
213 pci_conf_write(pc, tag, reg, exrom | 1);
214
215 command = pci_conf_read(pc, tag,
216 PCI_COMMAND_STATUS_REG0x04);
217 pci_conf_write(pc, tag,
218 PCI_COMMAND_STATUS_REG0x04,
219 command | PCI_COMMAND_MEM_ENABLE0x00000002);
220
221 if (cardbus_read_exrom(ca->ca_memt, bar_memh,
222 &rom_image))
223 goto out;
224
225 for (p = SIMPLEQ_FIRST(&rom_image)((&rom_image)->sqh_first); p;
226 p = SIMPLEQ_NEXT(p, next)((p)->next.sqe_next)) {
227 if (p->rom_image ==
228 CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)(((cis_ptr) >> 28) & 0xf)) {
229 bus_space_read_region_1(p->romt,((p->romt)->read_region_1((p->romh), ((0x0ffffff8 &
(cis_ptr))), (tuples), ((((p->image_size)<(len))?(p->
image_size):(len)))))
230 p->romh, CARDBUS_CIS_ADDR(cis_ptr),((p->romt)->read_region_1((p->romh), ((0x0ffffff8 &
(cis_ptr))), (tuples), ((((p->image_size)<(len))?(p->
image_size):(len)))))
231 tuples, MIN(p->image_size, len))((p->romt)->read_region_1((p->romh), ((0x0ffffff8 &
(cis_ptr))), (tuples), ((((p->image_size)<(len))?(p->
image_size):(len)))))
;
232 found++;
233 break;
234 }
235 }
236
237 out:
238 while ((p = SIMPLEQ_FIRST(&rom_image)((&rom_image)->sqh_first)) != NULL((void *)0)) {
239 SIMPLEQ_REMOVE_HEAD(&rom_image, next)do { if (((&rom_image)->sqh_first = (&rom_image)->
sqh_first->next.sqe_next) == ((void *)0)) (&rom_image)
->sqh_last = &(&rom_image)->sqh_first; } while (
0)
;
240 free(p, M_DEVBUF2, sizeof(*p));
241 }
242 exrom = pci_conf_read(pc, tag, reg);
243 pci_conf_write(pc, tag, reg, exrom & ~1);
244 splx(save)spllower(save);
245 } else {
246 command = pci_conf_read(pc, tag,
247 PCI_COMMAND_STATUS_REG0x04);
248 pci_conf_write(pc, tag,
249 PCI_COMMAND_STATUS_REG0x04,
250 command | PCI_COMMAND_MEM_ENABLE0x00000002);
251 /* XXX byte order? */
252 bus_space_read_region_1(ca->ca_memt, bar_memh,((ca->ca_memt)->read_region_1((bar_memh), (cis_ptr), (tuples
), (256)))
253 cis_ptr, tuples, 256)((ca->ca_memt)->read_region_1((bar_memh), (cis_ptr), (tuples
), (256)))
;
254 found++;
255 }
256 command = pci_conf_read(pc, tag,
257 PCI_COMMAND_STATUS_REG0x04);
258 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG0x04,
259 command & ~PCI_COMMAND_MEM_ENABLE0x00000002);
260 pci_conf_write(pc, tag, reg, 0);
261
262 Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh,cardbus_mapreg_unmap((ca->ca_ct)->ct_sc, (ca->ca_ct->
ct_func), (reg), (bar_tag), (bar_memh), (bar_size))
263 bar_size)cardbus_mapreg_unmap((ca->ca_ct)->ct_sc, (ca->ca_ct->
ct_func), (reg), (bar_tag), (bar_memh), (bar_size))
;
264 break;
265
266#ifdef DIAGNOSTIC1
267 default:
268 panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname,
269 cardbus_space);
270#endif
271 }
272 return (!found);
273}
274
275STATIC void
276parse_tuple(u_int8_t *tuple, int len, void *data)
277{
278 struct cardbus_cis_info *cis = data;
279 int bar_index;
280 int i;
281 char *p;
282
283 switch (tuple[0]) {
284 case PCMCIA_CISTPL_MANFID0x20:
285 if (tuple[1] < 4) {
286 DPRINTF(("%s: wrong length manufacturer id (%d)\n",
287 __func__, tuple[1]));
288 break;
289 }
290 cis->manufacturer = tuple[2] | (tuple[3] << 8);
291 cis->product = tuple[4] | (tuple[5] << 8);
292 break;
293 case PCMCIA_CISTPL_VERS_10x15:
294 bcopy(tuple + 2, cis->cis1_info_buf, tuple[1]);
295 i = 0;
296 p = cis->cis1_info_buf + 2;
297 while (i <
298 sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
299 if (p >= cis->cis1_info_buf + tuple[1] || *p == '\xff')
300 break;
301 cis->cis1_info[i++] = p;
302 while (*p != '\0' && *p != '\xff')
303 p++;
304 if (*p == '\0')
305 p++;
306 }
307 break;
308 case PCMCIA_CISTPL_BAR0x07:
309 if (tuple[1] != 6) {
310 DPRINTF(("%s: BAR with short length (%d)\n",
311 __func__, tuple[1]));
312 break;
313 }
314 bar_index = tuple[2] & 7;
315 if (bar_index == 0) {
316 DPRINTF(("%s: invalid ASI in BAR tuple\n",
317 __func__));
318 break;
319 }
320 bar_index--;
321 cis->bar[bar_index].flags = tuple[2];
322 cis->bar[bar_index].size = (tuple[4] << 0) |
323 (tuple[5] << 8) | (tuple[6] << 16) | (tuple[7] << 24);
324 break;
325 case PCMCIA_CISTPL_FUNCID0x21:
326 cis->funcid = tuple[2];
327 break;
328
329 case PCMCIA_CISTPL_FUNCE0x22:
330 switch (cis->funcid) {
331 case PCMCIA_FUNCTION_SERIAL2:
332 if (tuple[1] >= 2 &&
333 tuple[2] == 0
334 /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */) {
335 cis->funce.serial.uart_type = tuple[3] & 0x1f;
336 cis->funce.serial.uart_present = 1;
337 }
338 break;
339 case PCMCIA_FUNCTION_NETWORK6:
340 if (tuple[1] >= 8 && tuple[2] ==
341 PCMCIA_TPLFE_TYPE_LAN_NID0x04) {
342 if (tuple[3] >
343 sizeof(cis->funce.network.netid)) {
344 DPRINTF(("%s: unknown network id type"
345 " (len = %d)\n", __func__,
346 tuple[3]));
347 } else {
348 cis->funce.network.netid_present = 1;
349 bcopy(tuple + 4,
350 cis->funce.network.netid, tuple[3]);
351 }
352 }
353 }
354 break;
355 }
356}
357
358/*
359 * int cardbus_attach_card(struct cardbus_softc *sc)
360 *
361 * This function attaches the card on the slot: turns on power,
362 * reads and analyses tuple, sets configuration index.
363 *
364 * This function returns the number of recognised device functions.
365 * If no functions are recognised, return 0.
366 */
367int
368cardbus_attach_card(struct cardbus_softc *sc)
369{
370 cardbus_chipset_tag_t cc;
371 cardbus_function_tag_t cf;
372 int cdstatus;
373 pcitag_t tag;
374 pcireg_t id, class, cis_ptr;
375 pcireg_t bhlc;
376 u_int8_t *tuple;
377 int function, nfunction;
378 struct device *csc;
379 int no_work_funcs = 0;
380 cardbus_devfunc_t ct;
381 pci_chipset_tag_t pc = sc->sc_pc;
382 int i;
383
384 cc = sc->sc_cc;
385 cf = sc->sc_cf;
386
387 DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
388
389 /* inspect initial voltage */
390 if (0 == (cdstatus = (cf->cardbus_ctrl)(cc, CARDBUS_CD7))) {
391 DPRINTF(("cardbusattach: no CardBus card on cb%d\n",
392 sc->sc_dev.dv_unit));
393 return (0);
394 }
395
396 /* XXX use fake function 8 to keep power on during whole configuration */
397 enable_function(sc, cdstatus, 8);
398
399 function = 0;
400
401 tag = pci_make_tag(pc, sc->sc_bus, sc->sc_device, function);
402
403 /* Wait until power comes up. Maximum 500 ms. */
404 for (i = 0; i < 5; ++i) {
405 id = pci_conf_read(pc, tag, PCI_ID_REG0x00);
406 if (id != 0xffffffff && id != 0)
407 break;
408 if (cold) { /* before kernel thread invoked */
409 delay(100*1000)(*delay_func)(100*1000);
410 } else { /* thread context */
411 if (tsleep_nsec(sc, PCATCH0x100, "cardbus",
412 MSEC_TO_NSEC(100)) != EWOULDBLOCK35) {
413 break;
414 }
415 }
416 }
417 if (i == 5)
418 return (0);
419
420 bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG0x0c);
421 DPRINTF(("%s bhlc 0x%08x -> ", sc->sc_dev.dv_xname, bhlc));
422 nfunction = PCI_HDRTYPE_MULTIFN(bhlc)(((((bhlc) >> 16) & 0xff) & 0x80) != 0) ? 8 : 1;
423
424 tuple = malloc(2048, M_TEMP127, M_NOWAIT0x0002);
425 if (tuple == NULL((void *)0))
426 panic("no room for cardbus tuples");
427
428 for (function = 0; function < nfunction; function++) {
429 struct cardbus_attach_args ca;
430
431 tag = pci_make_tag(pc, sc->sc_bus, sc->sc_device,
432 function);
433
434 id = pci_conf_read(pc, tag, PCI_ID_REG0x00);
435 class = pci_conf_read(pc, tag, PCI_CLASS_REG0x08);
436 cis_ptr = pci_conf_read(pc, tag, CARDBUS_CIS_REG0x28);
437
438 /* Invalid vendor ID value? */
439 if (PCI_VENDOR(id)(((id) >> 0) & 0xffff) == PCI_VENDOR_INVALID0xffff)
440 continue;
441
442 DPRINTF(("cardbus_attach_card: Vendor 0x%x, Product 0x%x, "
443 "CIS 0x%x\n", PCI_VENDOR(id), PCI_PRODUCT(id),
444 cis_ptr));
445
446 enable_function(sc, cdstatus, function);
447
448 /* clean up every BAR */
449 pci_conf_write(pc, tag, CARDBUS_BASE0_REG0x10, 0);
450 pci_conf_write(pc, tag, CARDBUS_BASE1_REG0x14, 0);
451 pci_conf_write(pc, tag, CARDBUS_BASE2_REG0x18, 0);
452 pci_conf_write(pc, tag, CARDBUS_BASE3_REG0x1C, 0);
453 pci_conf_write(pc, tag, CARDBUS_BASE4_REG0x20, 0);
454 pci_conf_write(pc, tag, CARDBUS_BASE5_REG0x24, 0);
455 pci_conf_write(pc, tag, CARDBUS_ROM_REG0x30, 0);
456
457 /* set initial latency and cacheline size */
458 bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG0x0c);
459 DPRINTF(("%s func%d bhlc 0x%08x -> ", sc->sc_dev.dv_xname,
460 function, bhlc));
461 bhlc &= ~((PCI_LATTIMER_MASK0xff << PCI_LATTIMER_SHIFT8) |
462 (PCI_CACHELINE_MASK0xff << PCI_CACHELINE_SHIFT0));
463 bhlc |= ((sc->sc_cacheline & PCI_CACHELINE_MASK0xff) <<
464 PCI_CACHELINE_SHIFT0);
465 bhlc |= ((sc->sc_lattimer & PCI_LATTIMER_MASK0xff) <<
466 PCI_LATTIMER_SHIFT8);
467
468 pci_conf_write(pc, tag, PCI_BHLC_REG0x0c, bhlc);
469 bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG0x0c);
470 DPRINTF(("0x%08x\n", bhlc));
471
472 if (PCI_LATTIMER(bhlc)(((bhlc) >> 8) & 0xff) < 0x10) {
473 bhlc &= ~(PCI_LATTIMER_MASK0xff <<
474 PCI_LATTIMER_SHIFT8);
475 bhlc |= (0x10 << PCI_LATTIMER_SHIFT8);
476 pci_conf_write(pc, tag, PCI_BHLC_REG0x0c,
477 bhlc);
478 }
479
480 /*
481 * We need to allocate the ct here, since we might
482 * need it when reading the CIS
483 */
484 if ((ct =
485 (cardbus_devfunc_t)malloc(sizeof(struct cardbus_devfunc),
486 M_DEVBUF2, M_NOWAIT0x0002)) == NULL((void *)0))
487 panic("no room for cardbus_tag");
488
489 ct->ct_cc = sc->sc_cc;
490 ct->ct_cf = sc->sc_cf;
491 ct->ct_bus = sc->sc_bus;
492 ct->ct_dev = sc->sc_device;
493 ct->ct_func = function;
494 ct->ct_sc = sc;
495 sc->sc_funcs[function] = ct;
496
497 memset(&ca, 0, sizeof(ca))__builtin_memset((&ca), (0), (sizeof(ca)));
498
499 ca.ca_unit = sc->sc_dev.dv_unit;
500 ca.ca_ct = ct;
501
502 ca.ca_iot = sc->sc_iot;
503 ca.ca_memt = sc->sc_memt;
504 ca.ca_dmat = sc->sc_dmat;
505 ca.ca_rbus_iot = sc->sc_rbus_iot;
506 ca.ca_rbus_memt = sc->sc_rbus_memt;
507 ca.ca_tag = tag;
508 ca.ca_bus = sc->sc_bus;
509 ca.ca_device = sc->sc_device;
510 ca.ca_function = function;
511 ca.ca_id = id;
512 ca.ca_class = class;
513 ca.ca_pc = sc->sc_pc;
514
515 ca.ca_intrline = sc->sc_intrline;
516
517 if (cis_ptr != 0) {
518 if (cardbus_read_tuples(&ca, cis_ptr, tuple, 2048)) {
519 printf("cardbus_attach_card: failed to "
520 "read CIS\n");
521 } else {
522#ifdef CARDBUS_DEBUG
523 decode_tuples(tuple, 2048, print_tuple, NULL((void *)0));
524#endif
525 decode_tuples(tuple, 2048, parse_tuple,
526 &ca.ca_cis);
527 }
528 }
529
530 if ((csc = config_found_sm((void *)sc, &ca, cardbusprint,
531 cardbussubmatch)) == NULL((void *)0)) {
532 /* do not match */
533 disable_function(sc, function);
534 sc->sc_funcs[function] = NULL((void *)0);
535 free(ct, M_DEVBUF2, sizeof(struct cardbus_devfunc));
536 } else {
537 /* found */
538 ct->ct_device = csc;
539 ++no_work_funcs;
540 }
541 }
542 /*
543 * XXX power down pseudo function 8 (this will power down the card
544 * if no functions were attached).
545 */
546 disable_function(sc, 8);
547 free(tuple, M_TEMP127, 2048);
548
549 return (no_work_funcs);
550}
551
552STATIC int
553cardbussubmatch(struct device *parent, void *match, void *aux)
554{
555 struct cfdata *cf = match;
556 struct cardbus_attach_args *ca = aux;
557
558 if (cf->cardbuscf_devcf_loc[0] != CARDBUS_UNK_DEV-1 &&
559 cf->cardbuscf_devcf_loc[0] != ca->ca_unit) {
560 return (0);
561 }
562 if (cf->cardbuscf_functioncf_loc[1] != CARDBUS_UNK_FUNCTION-1 &&
563 cf->cardbuscf_functioncf_loc[1] != ca->ca_function) {
564 return (0);
565 }
566
567 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
568}
569
570STATIC int
571cardbusprint(void *aux, const char *pnp)
572{
573 struct cardbus_attach_args *ca = aux;
574 char devinfo[256];
575
576 if (pnp) {
577 pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo,
578 sizeof(devinfo));
579 printf("%s at %s", devinfo, pnp);
580 }
581 printf(" dev %d function %d", ca->ca_device, ca->ca_function);
582 if (!pnp) {
583 pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo,
584 sizeof(devinfo));
585 printf(" %s", devinfo);
586 }
587
588 return (UNCONF1);
589}
590
591/*
592 * void cardbus_detach_card(struct cardbus_softc *sc)
593 *
594 * This function detaches the card on the slot: detach device data
595 * structure and turns off the power.
596 *
597 * This function must not be called under interrupt context.
598 */
599void
600cardbus_detach_card(struct cardbus_softc *sc)
601{
602 struct cardbus_devfunc *ct;
603 int f;
604
605 for (f = 0; f < 8; f++) {
606 ct = sc->sc_funcs[f];
607 if (ct == NULL((void *)0))
608 continue;
609
610 DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname,
611 ct->ct_device->dv_xname));
612
613 if (config_detach(ct->ct_device, 0) != 0) {
614 printf("%s: cannot detach dev %s, function %d\n",
615 sc->sc_dev.dv_xname, ct->ct_device->dv_xname,
616 ct->ct_func);
617 } else {
618 sc->sc_poweron_func &= ~(1 << ct->ct_func);
619 sc->sc_funcs[ct->ct_func] = NULL((void *)0);
620 free(ct, M_DEVBUF2, sizeof(struct cardbus_devfunc));
621 }
622 }
623
624 sc->sc_poweron_func = 0;
625 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V0x0004 | CARDBUS_VPP_0V0x0040);
626}
627
628/*
629 * void *cardbus_intr_establish(cc, cf, irq, level, func, arg, name)
630 * Interrupt handler of pccard.
631 * args:
632 * cardbus_chipset_tag_t *cc
633 * int irq:
634 */
635void *
636cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
637 cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg,
638 const char *name)
639{
640 DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
641
642 return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg, name);
643}
644
645/*
646 * void cardbus_intr_disestablish(cc, cf, handler)
647 * Interrupt handler of pccard.
648 * args:
649 * cardbus_chipset_tag_t *cc
650 */
651void
652cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
653 void *handler)
654{
655 DPRINTF(("- pccard_intr_disestablish\n"));
656
657 (*cf->cardbus_intr_disestablish)(cc, handler);
658}
659
660/* XXX this should be merged with cardbus_function_{enable,disable},
661 but we don't have a ct when these functions are called */
662
663STATIC void
664enable_function(struct cardbus_softc *sc, int cdstatus, int function)
665{
666 if (sc->sc_poweron_func == 0) {
667 /* switch to 3V and/or wait for power to stabilize */
668 if (cdstatus & CARDBUS_3V_CARD0x02) {
669 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V0x0001);
670 } else {
671 /* No cards other than 3.3V cards. */
672 return;
673 }
674 (sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET4);
675 }
676 sc->sc_poweron_func |= (1 << function);
677}
678
679STATIC void
680disable_function(struct cardbus_softc *sc, int function)
681{
682 sc->sc_poweron_func &= ~(1 << function);
683 if (sc->sc_poweron_func == 0) {
684 /* power-off because no functions are enabled */
685 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V0x0004);
686 }
687}
688
689/*
690 * int cardbus_function_enable(struct cardbus_softc *sc, int func)
691 *
692 * This function enables a function on a card. When no power is
693 * applied on the card, power will be applied on it.
694 */
695int
696cardbus_function_enable(struct cardbus_softc *sc, int func)
697{
698 pci_chipset_tag_t pc = sc->sc_pc;
699 pcireg_t command;
700 pcitag_t tag;
701
702 DPRINTF(("entering cardbus_function_enable... "));
703
704 /* entering critical area */
705
706 /* XXX: sc_vold should be used */
707 enable_function(sc, CARDBUS_3V_CARD0x02, func);
708
709 /* exiting critical area */
710
711 tag = pci_make_tag(pc, sc->sc_bus, sc->sc_device, func);
712
713 command = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG0x04);
714 command |= (PCI_COMMAND_MEM_ENABLE0x00000002 | PCI_COMMAND_IO_ENABLE0x00000001 |
715 PCI_COMMAND_MASTER_ENABLE0x00000004); /* XXX: good guess needed */
716
717 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG0x04, command);
718
719 DPRINTF(("%x\n", sc->sc_poweron_func));
720
721 return (0);
722}
723
724/*
725 * int cardbus_function_disable(struct cardbus_softc *, int func)
726 *
727 * This function disable a function on a card. When no functions are
728 * enabled, it turns off the power.
729 */
730int
731cardbus_function_disable(struct cardbus_softc *sc, int func)
732{
733 DPRINTF(("entering cardbus_function_disable... "));
734
735 disable_function(sc, func);
736
737 return (0);
738}
739
740int
741cardbus_matchbyid(struct cardbus_attach_args *ca,
742 const struct pci_matchid *ids, int nent)
743{
744 const struct pci_matchid *pm;
745 int i;
746
747 for (i = 0, pm = ids; i < nent; i++, pm++)
748 if (PCI_VENDOR(ca->ca_id)(((ca->ca_id) >> 0) & 0xffff) == pm->pm_vid &&
749 PCI_PRODUCT(ca->ca_id)(((ca->ca_id) >> 16) & 0xffff) == pm->pm_pid)
750 return (1);
751 return (0);
752}
753
754/*
755 * below this line, there are some functions for decoding tuples.
756 * They should go out from this file.
757 */
758
759STATIC u_int8_t *
760decode_tuple(u_int8_t *, u_int8_t *, tuple_decode_func, void *);
761
762STATIC int
763decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
764{
765 u_int8_t *tp = tuple;
766
767 if (PCMCIA_CISTPL_LINKTARGET0x13 != *tuple) {
768 DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
769 return (0);
770 }
771
772 while ((tp = decode_tuple(tp, tuple + buflen, func, data)) != NULL((void *)0))
773 ;
774
775 return (1);
776}
777
778STATIC u_int8_t *
779decode_tuple(u_int8_t *tuple, u_int8_t *end, tuple_decode_func func,
780 void *data)
781{
782 u_int8_t type;
783 u_int8_t len;
784
785 type = tuple[0];
786 switch (type) {
787 case PCMCIA_CISTPL_NULL0x00:
788 case PCMCIA_CISTPL_END0xFF:
789 len = 1;
790 break;
791 default:
792 if (tuple + 2 > end)
793 return (NULL((void *)0));
794 len = tuple[1] + 2;
795 break;
796 }
797
798 if (tuple + len > end)
799 return (NULL((void *)0));
800
801 (*func)(tuple, len, data);
802
803 if (PCMCIA_CISTPL_END0xFF == type || tuple + len == end)
804 return (NULL((void *)0));
805
806 return (tuple + len);
807}
808
809#ifdef CARDBUS_DEBUG
810static char *tuple_name(int type);
811
812static char *
813tuple_name(int type)
814{
815 static char *tuple_name_s [] = {
816 "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
817 "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
818 "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
819 "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
820 "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
821 "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A", /* 14-17 */
822 "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY", /* 18-1B */
823 "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", /* 1C-1E */
824 "DEVICE_GEO_A", "MANFID", "FUNCID", "FUNCE", "SWIL", /* 1F-23 */
825 "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
826 "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
827 "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
828 "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
829 "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
830 "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
831 "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
832 "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER", /* 40-43 */
833 "DATE", "BATTERY", "ORG", "FORMAT_A" /* 44-47 */
834 };
835
836 if (type > 0 && type < nitems(tuple_name_s)(sizeof((tuple_name_s)) / sizeof((tuple_name_s)[0])))
837 return (tuple_name_s[type]);
838 else if (0xff == type)
839 return ("END");
840 else
841 return ("Reserved");
842}
843
844static void
845print_tuple(u_int8_t *tuple, int len, void *data)
846{
847 int i;
848
849 printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
850
851 for (i = 0; i < len; ++i) {
852 if (i % 16 == 0)
853 printf(" 0x%02x:", i);
854 printf(" %x",tuple[i]);
855 if (i % 16 == 15)
856 printf("\n");
857 }
858 if (i % 16 != 0)
859 printf("\n");
860}
861#endif