]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/pccard/pccard.c
Upgrade Unbound to 1.6.1. More to follow.
[FreeBSD/FreeBSD.git] / sys / dev / pccard / pccard.c
1 /*      $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $     */
2
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 1997 Marc Horowitz.  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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Marc Horowitz.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <sys/types.h>
45
46 #include <sys/bus.h>
47 #include <machine/bus.h>
48 #include <sys/rman.h>
49 #include <machine/resource.h>
50
51 #include <net/ethernet.h>
52
53 #include <dev/pccard/pccardreg.h>
54 #include <dev/pccard/pccardvar.h>
55 #include <dev/pccard/pccardvarp.h>
56 #include <dev/pccard/pccard_cis.h>
57
58 #include "power_if.h"
59 #include "card_if.h"
60
61 #define PCCARDDEBUG
62
63 /* sysctl vars */
64 static SYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters");
65
66 int     pccard_debug = 0;
67 SYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RWTUN,
68     &pccard_debug, 0,
69   "pccard debug");
70
71 int     pccard_cis_debug = 0;
72 SYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RWTUN,
73     &pccard_cis_debug, 0, "pccard CIS debug");
74
75 #ifdef PCCARDDEBUG
76 #define DPRINTF(arg) if (pccard_debug) printf arg
77 #define DEVPRINTF(arg) if (pccard_debug) device_printf arg
78 #define PRVERBOSE(arg) printf arg
79 #define DEVPRVERBOSE(arg) device_printf arg
80 #else
81 #define DPRINTF(arg)
82 #define DEVPRINTF(arg)
83 #define PRVERBOSE(arg) if (bootverbose) printf arg
84 #define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg
85 #endif
86
87 static int      pccard_ccr_read(struct pccard_function *pf, int ccr);
88 static void     pccard_ccr_write(struct pccard_function *pf, int ccr, int val);
89 static int      pccard_attach_card(device_t dev);
90 static int      pccard_detach_card(device_t dev);
91 static void     pccard_function_init(struct pccard_function *pf, int entry);
92 static void     pccard_function_free(struct pccard_function *pf);
93 static int      pccard_function_enable(struct pccard_function *pf);
94 static void     pccard_function_disable(struct pccard_function *pf);
95 static int      pccard_probe(device_t dev);
96 static int      pccard_attach(device_t dev);
97 static int      pccard_detach(device_t dev);
98 static void     pccard_print_resources(struct resource_list *rl,
99                     const char *name, int type, int count, const char *format);
100 static int      pccard_print_child(device_t dev, device_t child);
101 static int      pccard_set_resource(device_t dev, device_t child, int type,
102                     int rid, rman_res_t start, rman_res_t count);
103 static int      pccard_get_resource(device_t dev, device_t child, int type,
104                     int rid, rman_res_t *startp, rman_res_t *countp);
105 static void     pccard_delete_resource(device_t dev, device_t child, int type,
106                     int rid);
107 static int      pccard_set_res_flags(device_t dev, device_t child, int type,
108                     int rid, u_long flags);
109 static int      pccard_set_memory_offset(device_t dev, device_t child, int rid,
110                     uint32_t offset, uint32_t *deltap);
111 static int      pccard_probe_and_attach_child(device_t dev, device_t child,
112                     struct pccard_function *pf);
113 static void     pccard_probe_nomatch(device_t cbdev, device_t child);
114 static int      pccard_read_ivar(device_t bus, device_t child, int which,
115                     uintptr_t *result);
116 static void     pccard_driver_added(device_t dev, driver_t *driver);
117 static struct resource *pccard_alloc_resource(device_t dev,
118                     device_t child, int type, int *rid, rman_res_t start,
119                     rman_res_t end, rman_res_t count, u_int flags);
120 static int      pccard_release_resource(device_t dev, device_t child, int type,
121                     int rid, struct resource *r);
122 static void     pccard_child_detached(device_t parent, device_t dev);
123 static int      pccard_filter(void *arg);
124 static void     pccard_intr(void *arg);
125 static int      pccard_setup_intr(device_t dev, device_t child,
126                     struct resource *irq, int flags, driver_filter_t *filt, 
127                     driver_intr_t *intr, void *arg, void **cookiep);
128 static int      pccard_teardown_intr(device_t dev, device_t child,
129                     struct resource *r, void *cookie);
130
131 static const struct pccard_product *
132 pccard_do_product_lookup(device_t bus, device_t dev,
133                          const struct pccard_product *tab, size_t ent_size,
134                          pccard_product_match_fn matchfn);
135
136
137 static int
138 pccard_ccr_read(struct pccard_function *pf, int ccr)
139 {
140         return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
141             pf->pf_ccr_offset + ccr));
142 }
143
144 static void
145 pccard_ccr_write(struct pccard_function *pf, int ccr, int val)
146 {
147         if ((pf->ccr_mask) & (1 << (ccr / 2))) {
148                 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
149                     pf->pf_ccr_offset + ccr, val);
150         }
151 }
152
153 static int
154 pccard_set_default_descr(device_t dev)
155 {
156         const char *vendorstr, *prodstr;
157         uint32_t vendor, prod;
158         char *str;
159
160         if (pccard_get_vendor_str(dev, &vendorstr))
161                 return (0);
162         if (pccard_get_product_str(dev, &prodstr))
163                 return (0);
164         if (vendorstr != NULL && prodstr != NULL) {
165                 str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF,
166                     M_WAITOK);
167                 sprintf(str, "%s %s", vendorstr, prodstr);
168                 device_set_desc_copy(dev, str);
169                 free(str, M_DEVBUF);
170         } else {
171                 if (pccard_get_vendor(dev, &vendor))
172                         return (0);
173                 if (pccard_get_product(dev, &prod))
174                         return (0);
175                 str = malloc(100, M_DEVBUF, M_WAITOK);
176                 snprintf(str, 100, "vendor=%#x product=%#x", vendor, prod);
177                 device_set_desc_copy(dev, str);
178                 free(str, M_DEVBUF);
179         }
180         return (0);
181 }
182
183 static int
184 pccard_attach_card(device_t dev)
185 {
186         struct pccard_softc *sc = PCCARD_SOFTC(dev);
187         struct pccard_function *pf;
188         struct pccard_ivar *ivar;
189         device_t child;
190         int i;
191
192         if (!STAILQ_EMPTY(&sc->card.pf_head)) {
193                 if (bootverbose || pccard_debug)
194                         device_printf(dev, "Card already inserted.\n");
195         }
196
197         DEVPRINTF((dev, "chip_socket_enable\n"));
198         POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
199
200         DEVPRINTF((dev, "read_cis\n"));
201         pccard_read_cis(sc);
202
203         DEVPRINTF((dev, "check_cis_quirks\n"));
204         pccard_check_cis_quirks(dev);
205
206         /*
207          * bail now if the card has no functions, or if there was an error in
208          * the cis.
209          */
210
211         if (sc->card.error) {
212                 device_printf(dev, "CARD ERROR!\n");
213                 return (1);
214         }
215         if (STAILQ_EMPTY(&sc->card.pf_head)) {
216                 device_printf(dev, "Card has no functions!\n");
217                 return (1);
218         }
219
220         if (bootverbose || pccard_debug)
221                 pccard_print_cis(dev);
222
223         DEVPRINTF((dev, "functions scanning\n"));
224         i = -1;
225         STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
226                 i++;
227                 if (STAILQ_EMPTY(&pf->cfe_head)) {
228                         device_printf(dev,
229                             "Function %d has no config entries.!\n", i);
230                         continue;
231                 }
232                 pf->sc = sc;
233                 pf->cfe = NULL;
234                 pf->dev = NULL;
235         }
236         DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1,
237             pccard_mfc(sc)));
238
239         mtx_lock(&Giant);
240         STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
241                 if (STAILQ_EMPTY(&pf->cfe_head))
242                         continue;
243                 ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF,
244                     M_WAITOK | M_ZERO);
245                 resource_list_init(&ivar->resources);
246                 child = device_add_child(dev, NULL, -1);
247                 device_set_ivars(child, ivar);
248                 ivar->pf = pf;
249                 pf->dev = child;
250                 pccard_probe_and_attach_child(dev, child, pf);
251         }
252         mtx_unlock(&Giant);
253         return (0);
254 }
255
256 static int
257 pccard_probe_and_attach_child(device_t dev, device_t child,
258     struct pccard_function *pf)
259 {
260         struct pccard_softc *sc = PCCARD_SOFTC(dev);
261         int error;
262
263         /*
264          * In NetBSD, the drivers are responsible for activating each
265          * function of a card and selecting the config to use.  In
266          * FreeBSD, all that's done automatically in the typical lazy
267          * way we do device resoruce allocation (except we pick the
268          * cfe up front).  This is the biggest depature from the
269          * inherited NetBSD model, apart from the FreeBSD resource code.
270          *
271          * This seems to work well in practice for most cards.
272          * However, there are two cases that are problematic.  If a
273          * driver wishes to pick and chose which config entry to use,
274          * then this method falls down.  These are usually older
275          * cards.  In addition, there are some cards that have
276          * multiple hardware units on the cards, but presents only one
277          * CIS chain.  These cards are combination cards, but only one
278          * of these units can be on at a time.
279          *
280          * To overcome this limitation, while preserving the basic
281          * model, the probe routine can select a cfe and try to
282          * activate it.  If that succeeds, then we'll keep track of
283          * and let that information persist until we attach the card.
284          * Probe routines that do this MUST return 0, and cannot
285          * participate in the bidding process for a device.  This
286          * seems harsh until you realize that if a probe routine knows
287          * enough to override the cfe we pick, then chances are very
288          * very good that it is the only driver that could hope to
289          * cope with the card.  Bidding is for generic drivers, and
290          * while some of them may also match, none of them will do
291          * configuration override.
292          */
293         error = device_probe(child);
294         if (error != 0)
295                 goto out;
296         pccard_function_init(pf, -1);
297         if (sc->sc_enabled_count == 0)
298                 POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
299         if (pccard_function_enable(pf) == 0 &&
300             pccard_set_default_descr(child) == 0 &&
301             device_attach(child) == 0) {
302                 DEVPRINTF((sc->dev, "function %d CCR at %d offset %#x "
303                     "mask %#x: %#x %#x %#x %#x, %#x %#x %#x %#x, %#x\n",
304                     pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
305                     pf->ccr_mask, pccard_ccr_read(pf, 0x00),
306                     pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
307                     pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
308                     pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
309                     pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
310                 return (0);
311         }
312         error = ENXIO;
313 out:;
314         /*
315          * Probe may fail AND also try to select a cfe, if so, free
316          * it.  This is how we do cfe override.  Or the attach fails.
317          * Either way, we have to clean up.
318          */
319         if (pf->cfe != NULL)
320                 pccard_function_disable(pf);
321         pf->cfe = NULL;
322         pccard_function_free(pf);
323         return error;
324 }
325
326 static int
327 pccard_detach_card(device_t dev)
328 {
329         struct pccard_softc *sc = PCCARD_SOFTC(dev);
330         struct pccard_function *pf;
331         struct pccard_config_entry *cfe;
332         struct pccard_ivar *devi;
333         int state;
334
335         /*
336          * We are running on either the PCCARD socket's event thread
337          * or in user context detaching a device by user request.
338          */
339         STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
340                 if (pf->dev == NULL)
341                         continue;
342                 state = device_get_state(pf->dev);
343                 if (state == DS_ATTACHED || state == DS_BUSY)
344                         device_detach(pf->dev);
345                 if (pf->cfe != NULL)
346                         pccard_function_disable(pf);
347                 pccard_function_free(pf);
348                 devi = PCCARD_IVAR(pf->dev);
349                 device_delete_child(dev, pf->dev);
350                 free(devi, M_DEVBUF);
351         }
352         if (sc->sc_enabled_count == 0)
353                 POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
354
355         while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
356                 while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
357                         STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
358                         free(cfe, M_DEVBUF);
359                 }
360                 STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
361                 free(pf, M_DEVBUF);
362         }
363         STAILQ_INIT(&sc->card.pf_head);
364         return (0);
365 }
366
367 static const struct pccard_product *
368 pccard_do_product_lookup(device_t bus, device_t dev,
369     const struct pccard_product *tab, size_t ent_size,
370     pccard_product_match_fn matchfn)
371 {
372         const struct pccard_product *ent;
373         int matches;
374         uint32_t vendor;
375         uint32_t prod;
376         const char *vendorstr;
377         const char *prodstr;
378         const char *cis3str;
379         const char *cis4str;
380
381 #ifdef DIAGNOSTIC
382         if (sizeof *ent > ent_size)
383                 panic("pccard_product_lookup: bogus ent_size %jd",
384                     (intmax_t) ent_size);
385 #endif
386         if (pccard_get_vendor(dev, &vendor))
387                 return (NULL);
388         if (pccard_get_product(dev, &prod))
389                 return (NULL);
390         if (pccard_get_vendor_str(dev, &vendorstr))
391                 return (NULL);
392         if (pccard_get_product_str(dev, &prodstr))
393                 return (NULL);
394         if (pccard_get_cis3_str(dev, &cis3str))
395                 return (NULL);
396         if (pccard_get_cis4_str(dev, &cis4str))
397                 return (NULL);
398         for (ent = tab; ent->pp_vendor != 0; ent =
399             (const struct pccard_product *) ((const char *) ent + ent_size)) {
400                 matches = 1;
401                 if (ent->pp_vendor == PCCARD_VENDOR_ANY &&
402                     ent->pp_product == PCCARD_PRODUCT_ANY &&
403                     ent->pp_cis[0] == NULL &&
404                     ent->pp_cis[1] == NULL) {
405                         if (ent->pp_name)
406                                 device_printf(dev,
407                                     "Total wildcard entry ignored for %s\n",
408                                     ent->pp_name);
409                         continue;
410                 }
411                 if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
412                     vendor != ent->pp_vendor)
413                         matches = 0;
414                 if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
415                     prod != ent->pp_product)
416                         matches = 0;
417                 if (matches && ent->pp_cis[0] &&
418                     (vendorstr == NULL ||
419                     strcmp(ent->pp_cis[0], vendorstr) != 0))
420                         matches = 0;
421                 if (matches && ent->pp_cis[1] &&
422                     (prodstr == NULL ||
423                     strcmp(ent->pp_cis[1], prodstr) != 0))
424                         matches = 0;
425                 if (matches && ent->pp_cis[2] &&
426                     (cis3str == NULL ||
427                     strcmp(ent->pp_cis[2], cis3str) != 0))
428                         matches = 0;
429                 if (matches && ent->pp_cis[3] &&
430                     (cis4str == NULL ||
431                     strcmp(ent->pp_cis[3], cis4str) != 0))
432                         matches = 0;
433                 if (matchfn != NULL)
434                         matches = (*matchfn)(dev, ent, matches);
435                 if (matches)
436                         return (ent);
437         }
438         return (NULL);
439 }
440
441 /**
442  * @brief pccard_select_cfe
443  *
444  * Select a cfe entry to use.  Should be called from the pccard's probe
445  * routine after it knows for sure that it wants this card.
446  *
447  * XXX I think we need to make this symbol be static, ala the kobj stuff
448  * we do for everything else.  This is a quick hack.
449  */
450 int
451 pccard_select_cfe(device_t dev, int entry)
452 {
453         struct pccard_ivar *devi = PCCARD_IVAR(dev);
454         struct pccard_function *pf = devi->pf;
455         
456         pccard_function_init(pf, entry);
457         return (pf->cfe ? 0 : ENOMEM);
458 }
459
460 /*
461  * Initialize a PCCARD function.  May be called as long as the function is
462  * disabled.
463  *
464  * Note: pccard_function_init should not keep resources allocated.  It should
465  * only set them up ala isa pnp, set the values in the rl lists, and return.
466  * Any resource held after pccard_function_init is called is a bug.  However,
467  * the bus routines to get the resources also assume that pccard_function_init
468  * does this, so they need to be fixed too.
469  */
470 static void
471 pccard_function_init(struct pccard_function *pf, int entry)
472 {
473         struct pccard_config_entry *cfe;
474         struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
475         struct resource_list *rl = &devi->resources;
476         struct resource_list_entry *rle;
477         struct resource *r = NULL;
478         struct pccard_ce_iospace *ios;
479         struct pccard_ce_memspace *mems;
480         device_t bus;
481         rman_res_t start, end, len;
482         int i, rid, spaces;
483
484         if (pf->pf_flags & PFF_ENABLED) {
485                 printf("pccard_function_init: function is enabled");
486                 return;
487         }
488
489         /*
490          * Driver probe routine requested a specific entry already
491          * that succeeded.
492          */
493         if (pf->cfe != NULL)
494                 return;
495
496         /*
497          * walk the list of configuration entries until we find one that
498          * we can allocate all the resources to.
499          */
500         bus = device_get_parent(pf->dev);
501         STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
502                 if (cfe->iftype != PCCARD_IFTYPE_IO)
503                         continue;
504                 if (entry != -1 && cfe->number != entry)
505                         continue;
506                 spaces = 0;
507                 for (i = 0; i < cfe->num_iospace; i++) {
508                         ios = cfe->iospace + i;
509                         start = ios->start;
510                         if (start)
511                                 end = start + ios->length - 1;
512                         else
513                                 end = ~0;
514                         DEVPRINTF((bus, "I/O rid %d start %#jx end %#jx\n",
515                             i, start, end));
516                         rid = i;
517                         len = ios->length;
518                         r = bus_alloc_resource(bus, SYS_RES_IOPORT, &rid,
519                             start, end, len, rman_make_alignment_flags(len));
520                         if (r == NULL) {
521                                 DEVPRINTF((bus, "I/O rid %d failed\n", i));
522                                 goto not_this_one;
523                         }
524                         rle = resource_list_add(rl, SYS_RES_IOPORT,
525                             rid, rman_get_start(r), rman_get_end(r), len);
526                         if (rle == NULL)
527                                 panic("Cannot add resource rid %d IOPORT", rid);
528                         rle->res = r;
529                         spaces++;
530                 }
531                 for (i = 0; i < cfe->num_memspace; i++) {
532                         mems = cfe->memspace + i;
533                         start = mems->cardaddr + mems->hostaddr;
534                         if (start)
535                                 end = start + mems->length - 1;
536                         else
537                                 end = ~0;
538                         DEVPRINTF((bus, "Memory rid %d start %#jx end %#jx\ncardaddr %#jx hostaddr %#jx length %#jx\n",
539                             i, start, end, mems->cardaddr, mems->hostaddr,
540                             mems->length));
541                         rid = i;
542                         len = mems->length;
543                         r = bus_alloc_resource(bus, SYS_RES_MEMORY, &rid,
544                             start, end, len, rman_make_alignment_flags(len));
545                         if (r == NULL) {
546                                 DEVPRINTF((bus, "Memory rid %d failed\n", i));
547 //                              goto not_this_one;
548                                 continue;
549                         }
550                         rle = resource_list_add(rl, SYS_RES_MEMORY,
551                             rid, rman_get_start(r), rman_get_end(r), len);
552                         if (rle == NULL)
553                                 panic("Cannot add resource rid %d MEM", rid);
554                         rle->res = r;
555                         spaces++;
556                 }
557                 if (spaces == 0) {
558                         DEVPRINTF((bus, "Neither memory nor I/O mapped\n"));
559                         goto not_this_one;
560                 }
561                 if (cfe->irqmask) {
562                         rid = 0;
563                         r = bus_alloc_resource_any(bus, SYS_RES_IRQ, &rid,
564                             RF_SHAREABLE);
565                         if (r == NULL) {
566                                 DEVPRINTF((bus, "IRQ rid %d failed\n", rid));
567                                 goto not_this_one;
568                         }
569                         rle = resource_list_add(rl, SYS_RES_IRQ, rid,
570                             rman_get_start(r), rman_get_end(r), 1);
571                         if (rle == NULL)
572                                 panic("Cannot add resource rid %d IRQ", rid);
573                         rle->res = r;
574                 }
575                 /* If we get to here, we've allocated all we need */
576                 pf->cfe = cfe;
577                 break;
578             not_this_one:;
579                 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
580                     cfe->number));
581                 resource_list_purge(rl);
582         }
583 }
584
585 /*
586  * Free resources allocated by pccard_function_init(), May be called as long
587  * as the function is disabled.
588  *
589  * NOTE: This function should be unnecessary.  pccard_function_init should
590  * never keep resources initialized.
591  */
592 static void
593 pccard_function_free(struct pccard_function *pf)
594 {
595         struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
596         struct resource_list_entry *rle;
597
598         if (pf->pf_flags & PFF_ENABLED) {
599                 printf("pccard_function_free: function is enabled");
600                 return;
601         }
602
603         STAILQ_FOREACH(rle, &devi->resources, link) {
604                 if (rle->res) {
605                         if (rman_get_device(rle->res) != pf->sc->dev)
606                                 device_printf(pf->sc->dev,
607                                     "function_free: Resource still owned by "
608                                     "child, oops. "
609                                     "(type=%d, rid=%d, addr=%#jx)\n",
610                                     rle->type, rle->rid,
611                                     rman_get_start(rle->res));
612                         BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
613                             pf->sc->dev, rle->type, rle->rid, rle->res);
614                         rle->res = NULL;
615                 }
616         }
617         resource_list_free(&devi->resources);
618 }
619
620 static void
621 pccard_mfc_adjust_iobase(struct pccard_function *pf, rman_res_t addr,
622     rman_res_t offset, rman_res_t size)
623 {
624         bus_size_t iosize, tmp;
625
626         if (addr != 0) {
627                 if (pf->pf_mfc_iomax == 0) {
628                         pf->pf_mfc_iobase = addr + offset;
629                         pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
630                 } else {
631                         /* this makes the assumption that nothing overlaps */
632                         if (pf->pf_mfc_iobase > addr + offset)
633                                 pf->pf_mfc_iobase = addr + offset;
634                         if (pf->pf_mfc_iomax < addr + offset + size)
635                                 pf->pf_mfc_iomax = addr + offset + size;
636                 }
637         }
638
639         tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
640         /* round up to nearest (2^n)-1 */
641         for (iosize = 1; iosize < tmp; iosize <<= 1)
642                 ;
643         iosize--;
644
645         DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n",
646             (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1)));
647         pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
648             pf->pf_mfc_iobase & 0xff);
649         pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
650             (pf->pf_mfc_iobase >> 8) & 0xff);
651         pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
652         pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
653         pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
654 }
655
656 /* Enable a PCCARD function */
657 static int
658 pccard_function_enable(struct pccard_function *pf)
659 {
660         struct pccard_function *tmp;
661         int reg;
662         device_t dev = pf->sc->dev;
663
664         if (pf->cfe == NULL) {
665                 DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
666                 return (ENOMEM);
667         }
668
669         if (pf->pf_flags & PFF_ENABLED)
670                 return (0);
671         pf->sc->sc_enabled_count++;
672
673         /*
674          * it's possible for different functions' CCRs to be in the same
675          * underlying page.  Check for that.
676          */
677         STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
678                 if ((tmp->pf_flags & PFF_ENABLED) &&
679                     (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
680                     ((pf->ccr_base + PCCARD_CCR_SIZE) <=
681                     (tmp->ccr_base - tmp->pf_ccr_offset +
682                     tmp->pf_ccr_realsize))) {
683                         pf->pf_ccrt = tmp->pf_ccrt;
684                         pf->pf_ccrh = tmp->pf_ccrh;
685                         pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
686
687                         /*
688                          * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
689                          * tmp->ccr_base) + pf->ccr_base;
690                          */
691                         /* pf->pf_ccr_offset =
692                             (tmp->pf_ccr_offset + pf->ccr_base) -
693                             tmp->ccr_base; */
694                         pf->pf_ccr_window = tmp->pf_ccr_window;
695                         break;
696                 }
697         }
698         if (tmp == NULL) {
699                 pf->ccr_rid = 0;
700                 pf->ccr_res = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY,
701                     &pf->ccr_rid, PCCARD_MEM_PAGE_SIZE, RF_ACTIVE);
702                 if (!pf->ccr_res)
703                         goto bad;
704                 DEVPRINTF((dev, "ccr_res == %#jx-%#jx, base=%#x\n",
705                     rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
706                     pf->ccr_base));
707                 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
708                     pf->ccr_rid, PCCARD_A_MEM_ATTR);
709                 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
710                     pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
711                 pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
712                 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
713                 pf->pf_ccr_realsize = 1;
714         }
715
716         reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
717         reg |= PCCARD_CCR_OPTION_LEVIREQ;
718         if (pccard_mfc(pf->sc)) {
719                 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
720                         PCCARD_CCR_OPTION_ADDR_DECODE);
721                 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
722         }
723         pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
724
725         reg = 0;
726         if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
727                 reg |= PCCARD_CCR_STATUS_IOIS8;
728         if (pf->cfe->flags & PCCARD_CFE_AUDIO)
729                 reg |= PCCARD_CCR_STATUS_AUDIO;
730         pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
731
732         pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
733
734         if (pccard_mfc(pf->sc))
735                 pccard_mfc_adjust_iobase(pf, 0, 0, 0);
736
737 #ifdef PCCARDDEBUG
738         if (pccard_debug) {
739                 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
740                         device_printf(tmp->sc->dev,
741                             "function %d CCR at %d offset %#x: "
742                             "%#x %#x %#x %#x, %#x %#x %#x %#x, %#x\n",
743                             tmp->number, tmp->pf_ccr_window,
744                             tmp->pf_ccr_offset,
745                             pccard_ccr_read(tmp, 0x00),
746                             pccard_ccr_read(tmp, 0x02),
747                             pccard_ccr_read(tmp, 0x04),
748                             pccard_ccr_read(tmp, 0x06),
749                             pccard_ccr_read(tmp, 0x0A),
750                             pccard_ccr_read(tmp, 0x0C),
751                             pccard_ccr_read(tmp, 0x0E),
752                             pccard_ccr_read(tmp, 0x10),
753                             pccard_ccr_read(tmp, 0x12));
754                 }
755         }
756 #endif
757         pf->pf_flags |= PFF_ENABLED;
758         return (0);
759
760  bad:
761         /*
762          * Decrement the reference count, and power down the socket, if
763          * necessary.
764          */
765         pf->sc->sc_enabled_count--;
766         DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
767
768         return (1);
769 }
770
771 /* Disable PCCARD function. */
772 static void
773 pccard_function_disable(struct pccard_function *pf)
774 {
775         struct pccard_function *tmp;
776         device_t dev = pf->sc->dev;
777
778         if (pf->cfe == NULL)
779                 panic("pccard_function_disable: function not initialized");
780
781         if ((pf->pf_flags & PFF_ENABLED) == 0)
782                 return;
783         if (pf->intr_handler != NULL) {
784                 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
785                 struct resource_list_entry *rle =
786                     resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
787                 if (rle == NULL)
788                         panic("Can't disable an interrupt with no IRQ res\n");
789                 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
790                     pf->intr_handler_cookie);
791         }
792
793         /*
794          * it's possible for different functions' CCRs to be in the same
795          * underlying page.  Check for that.  Note we mark us as disabled
796          * first to avoid matching ourself.
797          */
798
799         pf->pf_flags &= ~PFF_ENABLED;
800         STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
801                 if ((tmp->pf_flags & PFF_ENABLED) &&
802                     (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
803                     ((pf->ccr_base + PCCARD_CCR_SIZE) <=
804                     (tmp->ccr_base - tmp->pf_ccr_offset +
805                     tmp->pf_ccr_realsize)))
806                         break;
807         }
808
809         /* Not used by anyone else; unmap the CCR. */
810         if (tmp == NULL) {
811                 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
812                     pf->ccr_res);
813                 pf->ccr_res = NULL;
814         }
815
816         /*
817          * Decrement the reference count, and power down the socket, if
818          * necessary.
819          */
820         pf->sc->sc_enabled_count--;
821 }
822
823 #define PCCARD_NPORT    2
824 #define PCCARD_NMEM     5
825 #define PCCARD_NIRQ     1
826 #define PCCARD_NDRQ     0
827
828 static int
829 pccard_probe(device_t dev)
830 {
831         device_set_desc(dev, "16-bit PCCard bus");
832         return (0);
833 }
834
835 static int
836 pccard_attach(device_t dev)
837 {
838         struct pccard_softc *sc = PCCARD_SOFTC(dev);
839         int err;
840
841         sc->dev = dev;
842         sc->sc_enabled_count = 0;
843         if ((err = pccard_device_create(sc)) != 0)
844                 return  (err);
845         STAILQ_INIT(&sc->card.pf_head);
846         return (bus_generic_attach(dev));
847 }
848
849 static int
850 pccard_detach(device_t dev)
851 {
852         pccard_detach_card(dev);
853         pccard_device_destroy(device_get_softc(dev));
854         return (0);
855 }
856
857 static int
858 pccard_suspend(device_t self)
859 {
860         pccard_detach_card(self);
861         return (0);
862 }
863
864 static
865 int
866 pccard_resume(device_t self)
867 {
868         return (0);
869 }
870
871 static void
872 pccard_print_resources(struct resource_list *rl, const char *name, int type,
873     int count, const char *format)
874 {
875         struct resource_list_entry *rle;
876         int printed;
877         int i;
878
879         printed = 0;
880         for (i = 0; i < count; i++) {
881                 rle = resource_list_find(rl, type, i);
882                 if (rle != NULL) {
883                         if (printed == 0)
884                                 printf(" %s ", name);
885                         else if (printed > 0)
886                                 printf(",");
887                         printed++;
888                         printf(format, rle->start);
889                         if (rle->count > 1) {
890                                 printf("-");
891                                 printf(format, rle->start + rle->count - 1);
892                         }
893                 } else if (i > 3) {
894                         /* check the first few regardless */
895                         break;
896                 }
897         }
898 }
899
900 static int
901 pccard_print_child(device_t dev, device_t child)
902 {
903         struct pccard_ivar *devi = PCCARD_IVAR(child);
904         struct resource_list *rl = &devi->resources;
905         int retval = 0;
906
907         retval += bus_print_child_header(dev, child);
908         retval += printf(" at");
909
910         if (devi != NULL) {
911                 pccard_print_resources(rl, "port", SYS_RES_IOPORT,
912                     PCCARD_NPORT, "%#lx");
913                 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
914                     PCCARD_NMEM, "%#lx");
915                 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
916                     "%ld");
917                 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
918                     "%ld");
919                 retval += printf(" function %d config %d", devi->pf->number,
920                     devi->pf->cfe->number);
921         }
922
923         retval += bus_print_child_footer(dev, child);
924
925         return (retval);
926 }
927
928 static int
929 pccard_set_resource(device_t dev, device_t child, int type, int rid,
930     rman_res_t start, rman_res_t count)
931 {
932         struct pccard_ivar *devi = PCCARD_IVAR(child);
933         struct resource_list *rl = &devi->resources;
934
935         if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
936             && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
937                 return (EINVAL);
938         if (rid < 0)
939                 return (EINVAL);
940         if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
941                 return (EINVAL);
942         if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
943                 return (EINVAL);
944         if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
945                 return (EINVAL);
946         if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
947                 return (EINVAL);
948
949         resource_list_add(rl, type, rid, start, start + count - 1, count);
950         if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
951             type, &rid, start, start + count - 1, count, 0))
952                 return 0;
953         else
954                 return ENOMEM;
955 }
956
957 static int
958 pccard_get_resource(device_t dev, device_t child, int type, int rid,
959     rman_res_t *startp, rman_res_t *countp)
960 {
961         struct pccard_ivar *devi = PCCARD_IVAR(child);
962         struct resource_list *rl = &devi->resources;
963         struct resource_list_entry *rle;
964
965         rle = resource_list_find(rl, type, rid);
966         if (rle == NULL)
967                 return (ENOENT);
968
969         if (startp != NULL)
970                 *startp = rle->start;
971         if (countp != NULL)
972                 *countp = rle->count;
973
974         return (0);
975 }
976
977 static void
978 pccard_delete_resource(device_t dev, device_t child, int type, int rid)
979 {
980         struct pccard_ivar *devi = PCCARD_IVAR(child);
981         struct resource_list *rl = &devi->resources;
982         resource_list_delete(rl, type, rid);
983 }
984
985 static int
986 pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
987     u_long flags)
988 {
989         return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
990             rid, flags));
991 }
992
993 static int
994 pccard_set_memory_offset(device_t dev, device_t child, int rid,
995     uint32_t offset, uint32_t *deltap)
996
997 {
998         return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
999             offset, deltap));
1000 }
1001
1002 static void
1003 pccard_probe_nomatch(device_t bus, device_t child)
1004 {
1005         struct pccard_ivar *devi = PCCARD_IVAR(child);
1006         struct pccard_function *pf = devi->pf;
1007         struct pccard_softc *sc = PCCARD_SOFTC(bus);
1008         int i;
1009
1010         device_printf(bus, "<unknown card>");
1011         printf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) "
1012             "at function %d\n", sc->card.manufacturer, sc->card.product,
1013             pf->function, pf->number);
1014         device_printf(bus, "   CIS info: ");
1015         for (i = 0; sc->card.cis1_info[i] != NULL && i < 4; i++)
1016                 printf("%s%s", i > 0 ? ", " : "", sc->card.cis1_info[i]);
1017         printf("\n");
1018         return;
1019 }
1020
1021 static int
1022 pccard_child_location_str(device_t bus, device_t child, char *buf,
1023     size_t buflen)
1024 {
1025         struct pccard_ivar *devi = PCCARD_IVAR(child);
1026         struct pccard_function *pf = devi->pf;
1027
1028         snprintf(buf, buflen, "function=%d", pf->number);
1029         return (0);
1030 }
1031
1032 static int
1033 pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
1034     size_t buflen)
1035 {
1036         struct pccard_ivar *devi = PCCARD_IVAR(child);
1037         struct pccard_function *pf = devi->pf;
1038         struct pccard_softc *sc = PCCARD_SOFTC(bus);
1039         char cis0[128], cis1[128];
1040
1041         devctl_safe_quote(cis0, sc->card.cis1_info[0], sizeof(cis0));
1042         devctl_safe_quote(cis1, sc->card.cis1_info[1], sizeof(cis1));
1043         snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
1044             "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
1045             sc->card.manufacturer, sc->card.product, cis0, cis1, pf->function);
1046         return (0);
1047 }
1048
1049 static int
1050 pccard_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1051 {
1052         struct pccard_ivar *devi = PCCARD_IVAR(child);
1053         struct pccard_function *pf = devi->pf;
1054         struct pccard_softc *sc = PCCARD_SOFTC(bus);
1055
1056         if (!pf)
1057                 panic("No pccard function pointer");
1058         switch (which) {
1059         default:
1060                 return (EINVAL);
1061         case PCCARD_IVAR_FUNCE_DISK:
1062                 *(uint16_t *)result = pf->pf_funce_disk_interface |
1063                     (pf->pf_funce_disk_power << 8);
1064                 break;
1065         case PCCARD_IVAR_ETHADDR:
1066                 bcopy(pf->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
1067                 break;
1068         case PCCARD_IVAR_VENDOR:
1069                 *(uint32_t *)result = sc->card.manufacturer;
1070                 break;
1071         case PCCARD_IVAR_PRODUCT:
1072                 *(uint32_t *)result = sc->card.product;
1073                 break;
1074         case PCCARD_IVAR_PRODEXT:
1075                 *(uint16_t *)result = sc->card.prodext;
1076                 break;
1077         case PCCARD_IVAR_FUNCTION:
1078                 *(uint32_t *)result = pf->function;
1079                 break;
1080         case PCCARD_IVAR_FUNCTION_NUMBER:
1081                 *(uint32_t *)result = pf->number;
1082                 break;
1083         case PCCARD_IVAR_VENDOR_STR:
1084                 *(const char **)result = sc->card.cis1_info[0];
1085                 break;
1086         case PCCARD_IVAR_PRODUCT_STR:
1087                 *(const char **)result = sc->card.cis1_info[1];
1088                 break;
1089         case PCCARD_IVAR_CIS3_STR:
1090                 *(const char **)result = sc->card.cis1_info[2];
1091                 break;
1092         case PCCARD_IVAR_CIS4_STR:
1093                 *(const char **)result = sc->card.cis1_info[3];
1094                 break;
1095         }
1096         return (0);
1097 }
1098
1099 static void
1100 pccard_driver_added(device_t dev, driver_t *driver)
1101 {
1102         struct pccard_softc *sc = PCCARD_SOFTC(dev);
1103         struct pccard_function *pf;
1104         device_t child;
1105
1106         STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
1107                 if (STAILQ_EMPTY(&pf->cfe_head))
1108                         continue;
1109                 child = pf->dev;
1110                 if (device_get_state(child) != DS_NOTPRESENT)
1111                         continue;
1112                 pccard_probe_and_attach_child(dev, child, pf);
1113         }
1114         return;
1115 }
1116
1117 static struct resource *
1118 pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
1119     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1120 {
1121         struct pccard_ivar *dinfo;
1122         struct resource_list_entry *rle = NULL;
1123         int passthrough = (device_get_parent(child) != dev);
1124         int isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1);
1125         struct resource *r = NULL;
1126
1127         /* XXX I'm no longer sure this is right */
1128         if (passthrough) {
1129                 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
1130                     type, rid, start, end, count, flags));
1131         }
1132
1133         dinfo = device_get_ivars(child);
1134         rle = resource_list_find(&dinfo->resources, type, *rid);
1135
1136         if (rle == NULL && isdefault)
1137                 return (NULL);  /* no resource of that type/rid */
1138         if (rle == NULL || rle->res == NULL) {
1139                 /* XXX Need to adjust flags */
1140                 r = bus_alloc_resource(dev, type, rid, start, end,
1141                   count, flags);
1142                 if (r == NULL)
1143                     goto bad;
1144                 resource_list_add(&dinfo->resources, type, *rid,
1145                   rman_get_start(r), rman_get_end(r), count);
1146                 rle = resource_list_find(&dinfo->resources, type, *rid);
1147                 if (!rle)
1148                     goto bad;
1149                 rle->res = r;
1150         }
1151         /*
1152          * If dev doesn't own the device, then we can't give this device
1153          * out.
1154          */
1155         if (rman_get_device(rle->res) != dev)
1156                 return (NULL);
1157         rman_set_device(rle->res, child);
1158         if (flags & RF_ACTIVE)
1159                 BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, rle->res);
1160         return (rle->res);
1161 bad:;
1162         device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1163         return (NULL);
1164 }
1165
1166 static int
1167 pccard_release_resource(device_t dev, device_t child, int type, int rid,
1168     struct resource *r)
1169 {
1170         struct pccard_ivar *dinfo;
1171         int passthrough = (device_get_parent(child) != dev);
1172         struct resource_list_entry *rle = NULL;
1173
1174         if (passthrough)
1175                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1176                     type, rid, r);
1177
1178         dinfo = device_get_ivars(child);
1179
1180         rle = resource_list_find(&dinfo->resources, type, rid);
1181
1182         if (!rle) {
1183                 device_printf(dev, "Allocated resource not found, "
1184                     "%d %#x %#jx %#jx\n",
1185                     type, rid, rman_get_start(r), rman_get_size(r));
1186                 return ENOENT;
1187         }
1188         if (!rle->res) {
1189                 device_printf(dev, "Allocated resource not recorded\n");
1190                 return ENOENT;
1191         }
1192         /*
1193          * Deactivate the resource (since it is being released), and
1194          * assign it to the bus.
1195          */
1196         BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, rle->res);
1197         rman_set_device(rle->res, dev);
1198         return (0);
1199 }
1200
1201 static void
1202 pccard_child_detached(device_t parent, device_t dev)
1203 {
1204         struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1205         struct pccard_function *pf = ivar->pf;
1206
1207         pccard_function_disable(pf);
1208 }
1209
1210 static int
1211 pccard_filter(void *arg)
1212 {
1213         struct pccard_function *pf = (struct pccard_function*) arg;
1214         int reg;
1215         int doisr = 1;
1216
1217         /*
1218          * MFC cards know if they interrupted, so we have to ack the
1219          * interrupt and call the ISR.  Non-MFC cards don't have these
1220          * bits, so they always get called.  Many non-MFC cards have
1221          * this bit set always upon read, but some do not.
1222          *
1223          * We always ack the interrupt, even if there's no ISR
1224          * for the card.  This is done on the theory that acking
1225          * the interrupt will pacify the card enough to keep an
1226          * interrupt storm from happening.  Of course this won't
1227          * help in the non-MFC case.
1228          *
1229          * This has no impact for MPSAFEness of the client drivers.
1230          * We register this with whatever flags the intr_handler
1231          * was registered with.  All these functions are MPSAFE.
1232          */
1233         if (pccard_mfc(pf->sc)) {
1234                 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1235                 if (reg & PCCARD_CCR_STATUS_INTR)
1236                         pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1237                             reg & ~PCCARD_CCR_STATUS_INTR);
1238                 else
1239                         doisr = 0;
1240         }
1241         if (doisr) {
1242                 if (pf->intr_filter != NULL)
1243                         return (pf->intr_filter(pf->intr_handler_arg));
1244                 return (FILTER_SCHEDULE_THREAD);
1245         }
1246         return (FILTER_STRAY);
1247 }
1248
1249 static void
1250 pccard_intr(void *arg)
1251 {
1252         struct pccard_function *pf = (struct pccard_function*) arg;
1253         
1254         pf->intr_handler(pf->intr_handler_arg); 
1255 }
1256
1257 static int
1258 pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1259     int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg, 
1260     void **cookiep)
1261 {
1262         struct pccard_softc *sc = PCCARD_SOFTC(dev);
1263         struct pccard_ivar *ivar = PCCARD_IVAR(child);
1264         struct pccard_function *pf = ivar->pf;
1265         int err;
1266
1267         if (pf->intr_filter != NULL || pf->intr_handler != NULL)
1268                 panic("Only one interrupt handler per function allowed");
1269         err = bus_generic_setup_intr(dev, child, irq, flags, pccard_filter, 
1270             intr ? pccard_intr : NULL, pf, cookiep);
1271         if (err != 0)
1272                 return (err);
1273         pf->intr_filter = filt;
1274         pf->intr_handler = intr;
1275         pf->intr_handler_arg = arg;
1276         pf->intr_handler_cookie = *cookiep;
1277         if (pccard_mfc(sc)) {
1278                 pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1279                     pccard_ccr_read(pf, PCCARD_CCR_OPTION) |
1280                     PCCARD_CCR_OPTION_IREQ_ENABLE);
1281         }
1282         return (0);
1283 }
1284
1285 static int
1286 pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
1287     void *cookie)
1288 {
1289         struct pccard_softc *sc = PCCARD_SOFTC(dev);
1290         struct pccard_ivar *ivar = PCCARD_IVAR(child);
1291         struct pccard_function *pf = ivar->pf;
1292         int ret;
1293
1294         if (pccard_mfc(sc)) {
1295                 pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1296                     pccard_ccr_read(pf, PCCARD_CCR_OPTION) &
1297                     ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1298         }
1299         ret = bus_generic_teardown_intr(dev, child, r, cookie);
1300         if (ret == 0) {
1301                 pf->intr_handler = NULL;
1302                 pf->intr_handler_arg = NULL;
1303                 pf->intr_handler_cookie = NULL;
1304         }
1305
1306         return (ret);
1307 }
1308
1309 static int
1310 pccard_activate_resource(device_t brdev, device_t child, int type, int rid,
1311     struct resource *r)
1312 {
1313         struct pccard_ivar *ivar = PCCARD_IVAR(child);
1314         struct pccard_function *pf = ivar->pf;
1315
1316         switch(type) {
1317         case SYS_RES_IOPORT:
1318                 /*
1319                  * We need to adjust IOBASE[01] and IOSIZE if we're an MFC
1320                  * card.
1321                  */
1322                 if (pccard_mfc(pf->sc))
1323                         pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0,
1324                             rman_get_size(r));
1325                 break;
1326         default:
1327                 break;
1328         }
1329         return (bus_generic_activate_resource(brdev, child, type, rid, r));
1330 }
1331
1332 static int
1333 pccard_deactivate_resource(device_t brdev, device_t child, int type,
1334     int rid, struct resource *r)
1335 {
1336         /* XXX undo pccard_activate_resource? XXX */
1337         return (bus_generic_deactivate_resource(brdev, child, type, rid, r));
1338 }
1339
1340 static int
1341 pccard_attr_read_impl(device_t brdev, device_t child, uint32_t offset,
1342     uint8_t *val)
1343 {
1344         struct pccard_ivar *devi = PCCARD_IVAR(child);
1345         struct pccard_function *pf = devi->pf;
1346
1347         /*
1348          * Optimization.  Most of the time, devices want to access
1349          * the same page of the attribute memory that the CCR is in.
1350          * We take advantage of this fact here.
1351          */
1352         if (offset / PCCARD_MEM_PAGE_SIZE ==
1353             pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1354                 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
1355                     offset % PCCARD_MEM_PAGE_SIZE);
1356         else {
1357                 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1358                     &offset);
1359                 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, offset);
1360                 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1361                     &offset);
1362         }
1363         return 0;
1364 }
1365
1366 static int
1367 pccard_attr_write_impl(device_t brdev, device_t child, uint32_t offset,
1368     uint8_t val)
1369 {
1370         struct pccard_ivar *devi = PCCARD_IVAR(child);
1371         struct pccard_function *pf = devi->pf;
1372
1373         /*
1374          * Optimization.  Most of the time, devices want to access
1375          * the same page of the attribute memory that the CCR is in.
1376          * We take advantage of this fact here.
1377          */
1378         if (offset / PCCARD_MEM_PAGE_SIZE ==
1379             pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1380                 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
1381                     offset % PCCARD_MEM_PAGE_SIZE, val);
1382         else {
1383                 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1384                     &offset);
1385                 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, offset, val);
1386                 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1387                     &offset);
1388         }
1389
1390         return 0;
1391 }
1392
1393 static int
1394 pccard_ccr_read_impl(device_t brdev, device_t child, uint32_t offset,
1395     uint8_t *val)
1396 {
1397         struct pccard_ivar *devi = PCCARD_IVAR(child);
1398
1399         *val = pccard_ccr_read(devi->pf, offset);
1400         DEVPRINTF((child, "ccr_read of %#x (%#x) is %#x\n", offset,
1401           devi->pf->pf_ccr_offset, *val));
1402         return 0;
1403 }
1404
1405 static int
1406 pccard_ccr_write_impl(device_t brdev, device_t child, uint32_t offset,
1407     uint8_t val)
1408 {
1409         struct pccard_ivar *devi = PCCARD_IVAR(child);
1410         struct pccard_function *pf = devi->pf;
1411
1412         /*
1413          * Can't use pccard_ccr_write since client drivers may access
1414          * registers not contained in the 'mask' if they are non-standard.
1415          */
1416         DEVPRINTF((child, "ccr_write of %#x to %#x (%#x)\n", val, offset,
1417           devi->pf->pf_ccr_offset));
1418         bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, pf->pf_ccr_offset + offset,
1419             val);
1420         return 0;
1421 }
1422
1423
1424 static device_method_t pccard_methods[] = {
1425         /* Device interface */
1426         DEVMETHOD(device_probe,         pccard_probe),
1427         DEVMETHOD(device_attach,        pccard_attach),
1428         DEVMETHOD(device_detach,        pccard_detach),
1429         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
1430         DEVMETHOD(device_suspend,       pccard_suspend),
1431         DEVMETHOD(device_resume,        pccard_resume),
1432
1433         /* Bus interface */
1434         DEVMETHOD(bus_print_child,      pccard_print_child),
1435         DEVMETHOD(bus_driver_added,     pccard_driver_added),
1436         DEVMETHOD(bus_child_detached,   pccard_child_detached),
1437         DEVMETHOD(bus_alloc_resource,   pccard_alloc_resource),
1438         DEVMETHOD(bus_release_resource, pccard_release_resource),
1439         DEVMETHOD(bus_activate_resource, pccard_activate_resource),
1440         DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
1441         DEVMETHOD(bus_setup_intr,       pccard_setup_intr),
1442         DEVMETHOD(bus_teardown_intr,    pccard_teardown_intr),
1443         DEVMETHOD(bus_set_resource,     pccard_set_resource),
1444         DEVMETHOD(bus_get_resource,     pccard_get_resource),
1445         DEVMETHOD(bus_delete_resource,  pccard_delete_resource),
1446         DEVMETHOD(bus_probe_nomatch,    pccard_probe_nomatch),
1447         DEVMETHOD(bus_read_ivar,        pccard_read_ivar),
1448         DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1449         DEVMETHOD(bus_child_location_str, pccard_child_location_str),
1450
1451         /* Card Interface */
1452         DEVMETHOD(card_set_res_flags,   pccard_set_res_flags),
1453         DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
1454         DEVMETHOD(card_attach_card,     pccard_attach_card),
1455         DEVMETHOD(card_detach_card,     pccard_detach_card),
1456         DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
1457         DEVMETHOD(card_cis_scan,        pccard_scan_cis),
1458         DEVMETHOD(card_attr_read,       pccard_attr_read_impl),
1459         DEVMETHOD(card_attr_write,      pccard_attr_write_impl),
1460         DEVMETHOD(card_ccr_read,        pccard_ccr_read_impl),
1461         DEVMETHOD(card_ccr_write,       pccard_ccr_write_impl),
1462
1463         { 0, 0 }
1464 };
1465
1466 static driver_t pccard_driver = {
1467         "pccard",
1468         pccard_methods,
1469         sizeof(struct pccard_softc)
1470 };
1471
1472 devclass_t      pccard_devclass;
1473
1474 /* Maybe we need to have a slot device? */
1475 DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1476 DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
1477 MODULE_VERSION(pccard, 1);