The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/eisa/ahb.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: ahb.c,v 1.42 2005/02/21 00:29:07 thorpej Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
    9  * Simulation Facility, NASA Ames Research Center.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. All advertising materials mentioning features or use of this software
   20  *    must display the following acknowledgement:
   21  *      This product includes software developed by the NetBSD
   22  *      Foundation, Inc. and its contributors.
   23  * 4. Neither the name of The NetBSD Foundation nor the names of its
   24  *    contributors may be used to endorse or promote products derived
   25  *    from this software without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   37  * POSSIBILITY OF SUCH DAMAGE.
   38  */
   39 
   40 /*
   41  * Originally written by Julian Elischer (julian@tfs.com)
   42  * for TRW Financial Systems for use under the MACH(2.5) operating system.
   43  *
   44  * TRW Financial Systems, in accordance with their agreement with Carnegie
   45  * Mellon University, makes this software available to CMU to distribute
   46  * or use in any manner that they see fit as long as this message is kept with
   47  * the software. For this reason TFS also grants any other persons or
   48  * organisations permission to use or modify this software.
   49  *
   50  * TFS supplies this software to be publicly redistributed
   51  * on the understanding that TFS is not responsible for the correct
   52  * functioning of this software in any circumstances.
   53  */
   54 
   55 #include <sys/cdefs.h>
   56 __KERNEL_RCSID(0, "$NetBSD: ahb.c,v 1.42 2005/02/21 00:29:07 thorpej Exp $");
   57 
   58 #include "opt_ddb.h"
   59 
   60 #undef  AHBDEBUG
   61 
   62 #include <sys/param.h>
   63 #include <sys/systm.h>
   64 #include <sys/kernel.h>
   65 #include <sys/errno.h>
   66 #include <sys/ioctl.h>
   67 #include <sys/device.h>
   68 #include <sys/malloc.h>
   69 #include <sys/buf.h>
   70 #include <sys/proc.h>
   71 #include <sys/user.h>
   72 
   73 #include <uvm/uvm_extern.h>
   74 
   75 #include <machine/bus.h>
   76 #include <machine/intr.h>
   77 
   78 #include <dev/scsipi/scsi_all.h>
   79 #include <dev/scsipi/scsipi_all.h>
   80 #include <dev/scsipi/scsiconf.h>
   81 
   82 #include <dev/eisa/eisareg.h>
   83 #include <dev/eisa/eisavar.h>
   84 #include <dev/eisa/eisadevs.h>
   85 #include <dev/eisa/ahbreg.h>
   86 
   87 #ifndef DDB
   88 #define Debugger() panic("should call debugger here (aha1742.c)")
   89 #endif /* ! DDB */
   90 
   91 #define AHB_ECB_MAX     32      /* store up to 32 ECBs at one time */
   92 #define ECB_HASH_SIZE   32      /* hash table size for phystokv */
   93 #define ECB_HASH_SHIFT  9
   94 #define ECB_HASH(x)     ((((long)(x))>>ECB_HASH_SHIFT) & (ECB_HASH_SIZE - 1))
   95 
   96 #define AHB_MAXXFER     ((AHB_NSEG - 1) << PGSHIFT)
   97 
   98 struct ahb_softc {
   99         struct device sc_dev;
  100 
  101         bus_space_tag_t sc_iot;
  102         bus_space_handle_t sc_ioh;
  103         bus_dma_tag_t sc_dmat;
  104         void *sc_ih;
  105 
  106         bus_dmamap_t sc_dmamap_ecb;     /* maps the ecbs */
  107         struct ahb_ecb *sc_ecbs;        /* all our ecbs */
  108 
  109         struct ahb_ecb *sc_ecbhash[ECB_HASH_SIZE];
  110         TAILQ_HEAD(, ahb_ecb) sc_free_ecb;
  111         struct ahb_ecb *sc_immed_ecb;   /* an outstanding immediete command */
  112         int sc_numecbs;
  113 
  114         struct scsipi_adapter sc_adapter;
  115         struct scsipi_channel sc_channel;
  116 };
  117 
  118 /*
  119  * Offset of an ECB from the beginning of the ECB DMA mapping.
  120  */
  121 #define AHB_ECB_OFF(e)  (((u_long)(e)) - ((u_long)&sc->sc_ecbs[0]))
  122 
  123 struct ahb_probe_data {
  124         int sc_irq;
  125         int sc_scsi_dev;
  126 };
  127 
  128 static void     ahb_send_mbox(struct ahb_softc *, int, struct ahb_ecb *);
  129 static void     ahb_send_immed(struct ahb_softc *, u_int32_t, struct ahb_ecb *);
  130 static int      ahbintr(void *);
  131 static void     ahb_free_ecb(struct ahb_softc *, struct ahb_ecb *);
  132 static struct   ahb_ecb *ahb_get_ecb(struct ahb_softc *);
  133 static struct   ahb_ecb *ahb_ecb_phys_kv(struct ahb_softc *, physaddr);
  134 static void     ahb_done(struct ahb_softc *, struct ahb_ecb *);
  135 static int      ahb_find(bus_space_tag_t, bus_space_handle_t,
  136                     struct ahb_probe_data *);
  137 static int      ahb_init(struct ahb_softc *);
  138 static void     ahbminphys(struct buf *);
  139 static void     ahb_scsipi_request(struct scsipi_channel *,
  140                     scsipi_adapter_req_t, void *);
  141 static int      ahb_poll(struct ahb_softc *, struct scsipi_xfer *, int);
  142 static void     ahb_timeout(void *);
  143 static int      ahb_create_ecbs(struct ahb_softc *, struct ahb_ecb *, int);
  144 
  145 static int      ahb_init_ecb(struct ahb_softc *, struct ahb_ecb *);
  146 
  147 static int      ahbmatch(struct device *, struct cfdata *, void *);
  148 static void     ahbattach(struct device *, struct device *, void *);
  149 
  150 CFATTACH_DECL(ahb, sizeof(struct ahb_softc),
  151     ahbmatch, ahbattach, NULL, NULL);
  152 
  153 #define AHB_ABORT_TIMEOUT       2000    /* time to wait for abort (mSec) */
  154 
  155 /*
  156  * Check the slots looking for a board we recognise
  157  * If we find one, note it's address (slot) and call
  158  * the actual probe routine to check it out.
  159  */
  160 static int
  161 ahbmatch(struct device *parent, struct cfdata *match, void *aux)
  162 {
  163         struct eisa_attach_args *ea = aux;
  164         bus_space_tag_t iot = ea->ea_iot;
  165         bus_space_handle_t ioh;
  166         int rv;
  167 
  168         /* must match one of our known ID strings */
  169         if (strcmp(ea->ea_idstring, "ADP0000") &&
  170             strcmp(ea->ea_idstring, "ADP0001") &&
  171             strcmp(ea->ea_idstring, "ADP0002") &&
  172             strcmp(ea->ea_idstring, "ADP0400"))
  173                 return (0);
  174 
  175         if (bus_space_map(iot,
  176             EISA_SLOT_ADDR(ea->ea_slot) + AHB_EISA_SLOT_OFFSET, AHB_EISA_IOSIZE,
  177             0, &ioh))
  178                 return (0);
  179 
  180         rv = !ahb_find(iot, ioh, NULL);
  181 
  182         bus_space_unmap(iot, ioh, AHB_EISA_IOSIZE);
  183 
  184         return (rv);
  185 }
  186 
  187 /*
  188  * Attach all the sub-devices we can find
  189  */
  190 static void
  191 ahbattach(struct device *parent, struct device *self, void *aux)
  192 {
  193         struct eisa_attach_args *ea = aux;
  194         struct ahb_softc *sc = (void *)self;
  195         bus_space_tag_t iot = ea->ea_iot;
  196         bus_space_handle_t ioh;
  197         eisa_chipset_tag_t ec = ea->ea_ec;
  198         eisa_intr_handle_t ih;
  199         const char *model, *intrstr;
  200         struct ahb_probe_data apd;
  201         struct scsipi_adapter *adapt = &sc->sc_adapter;
  202         struct scsipi_channel *chan = &sc->sc_channel;
  203 
  204         if (!strcmp(ea->ea_idstring, "ADP0000"))
  205                 model = EISA_PRODUCT_ADP0000;
  206         else if (!strcmp(ea->ea_idstring, "ADP0001"))
  207                 model = EISA_PRODUCT_ADP0001;
  208         else if (!strcmp(ea->ea_idstring, "ADP0002"))
  209                 model = EISA_PRODUCT_ADP0002;
  210         else if (!strcmp(ea->ea_idstring, "ADP0400"))
  211                 model = EISA_PRODUCT_ADP0400;
  212         else
  213                 model = "unknown model!";
  214         printf(": %s\n", model);
  215 
  216         if (bus_space_map(iot,
  217             EISA_SLOT_ADDR(ea->ea_slot) + AHB_EISA_SLOT_OFFSET, AHB_EISA_IOSIZE,
  218             0, &ioh))
  219                 panic("ahbattach: could not map I/O addresses");
  220 
  221         sc->sc_iot = iot;
  222         sc->sc_ioh = ioh;
  223         sc->sc_dmat = ea->ea_dmat;
  224         if (ahb_find(iot, ioh, &apd))
  225                 panic("ahbattach: ahb_find failed!");
  226 
  227         TAILQ_INIT(&sc->sc_free_ecb);
  228 
  229         /*
  230          * Fill in the scsipi_adapter.
  231          */
  232         memset(adapt, 0, sizeof(*adapt));
  233         adapt->adapt_dev = &sc->sc_dev;
  234         adapt->adapt_nchannels = 1;
  235         /* adapt_openings initialized below */
  236         adapt->adapt_max_periph = 4;            /* XXX arbitrary? */
  237         adapt->adapt_request = ahb_scsipi_request;
  238         adapt->adapt_minphys = ahbminphys;
  239 
  240         /*
  241          * Fill in the scsipi_channel.
  242          */
  243         memset(chan, 0, sizeof(*chan));
  244         chan->chan_adapter = adapt;
  245         chan->chan_bustype = &scsi_bustype;
  246         chan->chan_channel = 0;
  247         chan->chan_ntargets = 8;
  248         chan->chan_nluns = 8;
  249         chan->chan_id = apd.sc_scsi_dev;
  250 
  251         if (ahb_init(sc) != 0) {
  252                 /* Error during initialization! */
  253                 return;
  254         }
  255 
  256         if (eisa_intr_map(ec, apd.sc_irq, &ih)) {
  257                 printf("%s: couldn't map interrupt (%d)\n",
  258                     sc->sc_dev.dv_xname, apd.sc_irq);
  259                 return;
  260         }
  261         intrstr = eisa_intr_string(ec, ih);
  262         sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
  263             ahbintr, sc);
  264         if (sc->sc_ih == NULL) {
  265                 printf("%s: couldn't establish interrupt",
  266                     sc->sc_dev.dv_xname);
  267                 if (intrstr != NULL)
  268                         printf(" at %s", intrstr);
  269                 printf("\n");
  270                 return;
  271         }
  272         if (intrstr != NULL)
  273                 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
  274                     intrstr);
  275 
  276         /*
  277          * ask the adapter what subunits are present
  278          */
  279         config_found(self, &sc->sc_channel, scsiprint);
  280 }
  281 
  282 /*
  283  * Function to send a command out through a mailbox
  284  */
  285 static void
  286 ahb_send_mbox(struct ahb_softc *sc, int opcode, struct ahb_ecb *ecb)
  287 {
  288         bus_space_tag_t iot = sc->sc_iot;
  289         bus_space_handle_t ioh = sc->sc_ioh;
  290         int wait = 300; /* 1ms should be enough */
  291 
  292         while (--wait) {
  293                 if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
  294                     == (G2STAT_MBOX_EMPTY))
  295                         break;
  296                 delay(10);
  297         }
  298         if (!wait) {
  299                 printf("%s: board not responding\n", sc->sc_dev.dv_xname);
  300                 Debugger();
  301         }
  302 
  303         /*
  304          * don't know if this will work.
  305          * XXX WHAT DOES THIS COMMENT MEAN?!  --thorpej
  306          */
  307         bus_space_write_4(iot, ioh, MBOXOUT0,
  308             sc->sc_dmamap_ecb->dm_segs[0].ds_addr + AHB_ECB_OFF(ecb));
  309         bus_space_write_1(iot, ioh, ATTN, opcode |
  310                 ecb->xs->xs_periph->periph_target);
  311 
  312         if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
  313                 callout_reset(&ecb->xs->xs_callout,
  314                     mstohz(ecb->timeout), ahb_timeout, ecb);
  315 }
  316 
  317 /*
  318  * Function to  send an immediate type command to the adapter
  319  */
  320 static void
  321 ahb_send_immed(struct ahb_softc *sc, u_int32_t cmd, struct ahb_ecb *ecb)
  322 {
  323         bus_space_tag_t iot = sc->sc_iot;
  324         bus_space_handle_t ioh = sc->sc_ioh;
  325         int wait = 100; /* 1 ms enough? */
  326 
  327         while (--wait) {
  328                 if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
  329                     == (G2STAT_MBOX_EMPTY))
  330                         break;
  331                 delay(10);
  332         }
  333         if (!wait) {
  334                 printf("%s: board not responding\n", sc->sc_dev.dv_xname);
  335                 Debugger();
  336         }
  337 
  338         bus_space_write_4(iot, ioh, MBOXOUT0, cmd);     /* don't know this will work */
  339         bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_SET_HOST_READY);
  340         bus_space_write_1(iot, ioh, ATTN, OP_IMMED |
  341                 ecb->xs->xs_periph->periph_target);
  342 
  343         if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
  344                 callout_reset(&ecb->xs->xs_callout,
  345                     mstohz(ecb->timeout), ahb_timeout, ecb);
  346 }
  347 
  348 /*
  349  * Catch an interrupt from the adaptor
  350  */
  351 static int
  352 ahbintr(void *arg)
  353 {
  354         struct ahb_softc *sc = arg;
  355         bus_space_tag_t iot = sc->sc_iot;
  356         bus_space_handle_t ioh = sc->sc_ioh;
  357         struct ahb_ecb *ecb;
  358         u_char ahbstat;
  359         u_int32_t mboxval;
  360 
  361 #ifdef  AHBDEBUG
  362         printf("%s: ahbintr ", sc->sc_dev.dv_xname);
  363 #endif /* AHBDEBUG */
  364 
  365         if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
  366                 return 0;
  367 
  368         for (;;) {
  369                 /*
  370                  * First get all the information and then
  371                  * acknowledge the interrupt
  372                  */
  373                 ahbstat = bus_space_read_1(iot, ioh, G2INTST);
  374                 mboxval = bus_space_read_4(iot, ioh, MBOXIN0);
  375                 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
  376 
  377 #ifdef  AHBDEBUG
  378                 printf("status = 0x%x ", ahbstat);
  379 #endif /* AHBDEBUG */
  380 
  381                 /*
  382                  * Process the completed operation
  383                  */
  384                 switch (ahbstat & G2INTST_INT_STAT) {
  385                 case AHB_ECB_OK:
  386                 case AHB_ECB_RECOVERED:
  387                 case AHB_ECB_ERR:
  388                         ecb = ahb_ecb_phys_kv(sc, mboxval);
  389                         if (!ecb) {
  390                                 printf("%s: BAD ECB RETURNED!\n",
  391                                     sc->sc_dev.dv_xname);
  392                                 goto next;      /* whatever it was, it'll timeout */
  393                         }
  394                         break;
  395 
  396                 case AHB_IMMED_ERR:
  397                         ecb = sc->sc_immed_ecb;
  398                         sc->sc_immed_ecb = 0;
  399                         ecb->flags |= ECB_IMMED_FAIL;
  400                         break;
  401 
  402                 case AHB_IMMED_OK:
  403                         ecb = sc->sc_immed_ecb;
  404                         sc->sc_immed_ecb = 0;
  405                         break;
  406 
  407                 default:
  408                         printf("%s: unexpected interrupt %x\n",
  409                             sc->sc_dev.dv_xname, ahbstat);
  410                         goto next;
  411                 }
  412 
  413                 callout_stop(&ecb->xs->xs_callout);
  414                 ahb_done(sc, ecb);
  415 
  416         next:
  417                 if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
  418                         return 1;
  419         }
  420 }
  421 
  422 static __inline void
  423 ahb_reset_ecb(struct ahb_softc *sc, struct ahb_ecb *ecb)
  424 {
  425 
  426         ecb->flags = 0;
  427 }
  428 
  429 /*
  430  * A ecb (and hence a mbx-out is put onto the
  431  * free list.
  432  */
  433 static void
  434 ahb_free_ecb(struct ahb_softc *sc, struct ahb_ecb *ecb)
  435 {
  436         int s;
  437 
  438         s = splbio();
  439         ahb_reset_ecb(sc, ecb);
  440         TAILQ_INSERT_HEAD(&sc->sc_free_ecb, ecb, chain);
  441         splx(s);
  442 }
  443 
  444 /*
  445  * Create a set of ecbs and add them to the free list.
  446  */
  447 static int
  448 ahb_init_ecb(struct ahb_softc *sc, struct ahb_ecb *ecb)
  449 {
  450         bus_dma_tag_t dmat = sc->sc_dmat;
  451         int hashnum, error;
  452 
  453         /*
  454          * Create the DMA map for this ECB.
  455          */
  456         error = bus_dmamap_create(dmat, AHB_MAXXFER, AHB_NSEG, AHB_MAXXFER,
  457             0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &ecb->dmamap_xfer);
  458         if (error) {
  459                 printf("%s: can't create ecb dmamap_xfer\n",
  460                     sc->sc_dev.dv_xname);
  461                 return (error);
  462         }
  463 
  464         /*
  465          * put in the phystokv hash table
  466          * Never gets taken out.
  467          */
  468         ecb->hashkey = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
  469             AHB_ECB_OFF(ecb);
  470         hashnum = ECB_HASH(ecb->hashkey);
  471         ecb->nexthash = sc->sc_ecbhash[hashnum];
  472         sc->sc_ecbhash[hashnum] = ecb;
  473         ahb_reset_ecb(sc, ecb);
  474         return (0);
  475 }
  476 
  477 static int
  478 ahb_create_ecbs(struct ahb_softc *sc, struct ahb_ecb *ecbstore, int count)
  479 {
  480         struct ahb_ecb *ecb;
  481         int i, error;
  482 
  483         bzero(ecbstore, sizeof(struct ahb_ecb) * count);
  484         for (i = 0; i < count; i++) {
  485                 ecb = &ecbstore[i];
  486                 if ((error = ahb_init_ecb(sc, ecb)) != 0) {
  487                         printf("%s: unable to initialize ecb, error = %d\n",
  488                             sc->sc_dev.dv_xname, error);
  489                         goto out;
  490                 }
  491                 TAILQ_INSERT_TAIL(&sc->sc_free_ecb, ecb, chain);
  492         }
  493  out:
  494         return (i);
  495 }
  496 
  497 /*
  498  * Get a free ecb
  499  *
  500  * If there are none, see if we can allocate a new one. If so, put it in the
  501  * hash table too otherwise either return an error or sleep.
  502  */
  503 static struct ahb_ecb *
  504 ahb_get_ecb(struct ahb_softc *sc)
  505 {
  506         struct ahb_ecb *ecb;
  507         int s;
  508 
  509         s = splbio();
  510         ecb = TAILQ_FIRST(&sc->sc_free_ecb);
  511         if (ecb != NULL) {
  512                 TAILQ_REMOVE(&sc->sc_free_ecb, ecb, chain);
  513                 ecb->flags |= ECB_ALLOC;
  514         }
  515         splx(s);
  516         return (ecb);
  517 }
  518 
  519 /*
  520  * given a physical address, find the ecb that it corresponds to.
  521  */
  522 static struct ahb_ecb *
  523 ahb_ecb_phys_kv(struct ahb_softc *sc, physaddr ecb_phys)
  524 {
  525         int hashnum = ECB_HASH(ecb_phys);
  526         struct ahb_ecb *ecb = sc->sc_ecbhash[hashnum];
  527 
  528         while (ecb) {
  529                 if (ecb->hashkey == ecb_phys)
  530                         break;
  531                 ecb = ecb->nexthash;
  532         }
  533         return ecb;
  534 }
  535 
  536 /*
  537  * We have a ecb which has been processed by the adaptor, now we look to see
  538  * how the operation went.
  539  */
  540 static void
  541 ahb_done(struct ahb_softc *sc, struct ahb_ecb *ecb)
  542 {
  543         bus_dma_tag_t dmat = sc->sc_dmat;
  544         struct scsi_sense_data *s1, *s2;
  545         struct scsipi_xfer *xs = ecb->xs;
  546 
  547         SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("ahb_done\n"));
  548 
  549         bus_dmamap_sync(dmat, sc->sc_dmamap_ecb,
  550             AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
  551             BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
  552 
  553         /*
  554          * If we were a data transfer, unload the map that described
  555          * the data buffer.
  556          */
  557         if (xs->datalen) {
  558                 bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
  559                     ecb->dmamap_xfer->dm_mapsize,
  560                     (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
  561                     BUS_DMASYNC_POSTWRITE);
  562                 bus_dmamap_unload(dmat, ecb->dmamap_xfer);
  563         }
  564 
  565         /*
  566          * Otherwise, put the results of the operation
  567          * into the xfer and call whoever started it
  568          */
  569         if ((ecb->flags & ECB_ALLOC) == 0) {
  570                 printf("%s: exiting ecb not allocated!\n", sc->sc_dev.dv_xname);
  571                 Debugger();
  572         }
  573         if (ecb->flags & ECB_IMMED) {
  574                 if (ecb->flags & ECB_IMMED_FAIL)
  575                         xs->error = XS_DRIVER_STUFFUP;
  576                 goto done;
  577         }
  578         if (xs->error == XS_NOERROR) {
  579                 if (ecb->ecb_status.host_stat != HS_OK) {
  580                         switch (ecb->ecb_status.host_stat) {
  581                         case HS_TIMED_OUT:      /* No response */
  582                                 xs->error = XS_SELTIMEOUT;
  583                                 break;
  584                         default:        /* Other scsi protocol messes */
  585                                 printf("%s: host_stat %x\n",
  586                                     sc->sc_dev.dv_xname, ecb->ecb_status.host_stat);
  587                                 xs->error = XS_DRIVER_STUFFUP;
  588                         }
  589                 } else if (ecb->ecb_status.target_stat != SCSI_OK) {
  590                         switch (ecb->ecb_status.target_stat) {
  591                         case SCSI_CHECK:
  592                                 s1 = &ecb->ecb_sense;
  593                                 s2 = &xs->sense.scsi_sense;
  594                                 *s2 = *s1;
  595                                 xs->error = XS_SENSE;
  596                                 break;
  597                         case SCSI_BUSY:
  598                                 xs->error = XS_BUSY;
  599                                 break;
  600                         default:
  601                                 printf("%s: target_stat %x\n",
  602                                     sc->sc_dev.dv_xname, ecb->ecb_status.target_stat);
  603                                 xs->error = XS_DRIVER_STUFFUP;
  604                         }
  605                 } else
  606                         xs->resid = 0;
  607         }
  608 done:
  609         ahb_free_ecb(sc, ecb);
  610         scsipi_done(xs);
  611 }
  612 
  613 /*
  614  * Start the board, ready for normal operation
  615  */
  616 static int
  617 ahb_find(bus_space_tag_t iot, bus_space_handle_t ioh, struct ahb_probe_data *sc)
  618 {
  619         u_char intdef;
  620         int i, irq, busid;
  621         int wait = 1000;        /* 1 sec enough? */
  622 
  623         bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
  624 
  625 #define NO_NO 1
  626 #ifdef NO_NO
  627         /*
  628          * reset board, If it doesn't respond, assume
  629          * that it's not there.. good for the probe
  630          */
  631         bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_HARD_RESET);
  632         delay(1000);
  633         bus_space_write_1(iot, ioh, G2CNTRL, 0);
  634         delay(10000);
  635         while (--wait) {
  636                 if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_BUSY) == 0)
  637                         break;
  638                 delay(1000);
  639         }
  640         if (!wait) {
  641 #ifdef  AHBDEBUG
  642                 printf("ahb_find: No answer from aha1742 board\n");
  643 #endif /* AHBDEBUG */
  644                 return ENXIO;
  645         }
  646         i = bus_space_read_1(iot, ioh, MBOXIN0);
  647         if (i) {
  648                 printf("self test failed, val = 0x%x\n", i);
  649                 return EIO;
  650         }
  651 
  652         /* Set it again, just to be sure. */
  653         bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
  654 #endif
  655 
  656         while (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) {
  657                 printf(".");
  658                 bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
  659                 delay(10000);
  660         }
  661 
  662         intdef = bus_space_read_1(iot, ioh, INTDEF);
  663         switch (intdef & 0x07) {
  664         case INT9:
  665                 irq = 9;
  666                 break;
  667         case INT10:
  668                 irq = 10;
  669                 break;
  670         case INT11:
  671                 irq = 11;
  672                 break;
  673         case INT12:
  674                 irq = 12;
  675                 break;
  676         case INT14:
  677                 irq = 14;
  678                 break;
  679         case INT15:
  680                 irq = 15;
  681                 break;
  682         default:
  683                 printf("illegal int setting %x\n", intdef);
  684                 return EIO;
  685         }
  686 
  687         bus_space_write_1(iot, ioh, INTDEF, (intdef | INTEN));  /* make sure we can interrupt */
  688 
  689         /* who are we on the scsi bus? */
  690         busid = (bus_space_read_1(iot, ioh, SCSIDEF) & HSCSIID);
  691 
  692         /* if we want to return data, do so now */
  693         if (sc) {
  694                 sc->sc_irq = irq;
  695                 sc->sc_scsi_dev = busid;
  696         }
  697 
  698         /*
  699          * Note that we are going and return (to probe)
  700          */
  701         return 0;
  702 }
  703 
  704 static int
  705 ahb_init(struct ahb_softc *sc)
  706 {
  707         bus_dma_segment_t seg;
  708         int i, error, rseg;
  709 
  710 #define ECBSIZE         (AHB_ECB_MAX * sizeof(struct ahb_ecb))
  711 
  712         /*
  713          * Allocate the ECBs.
  714          */
  715         if ((error = bus_dmamem_alloc(sc->sc_dmat, ECBSIZE,
  716             PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
  717                 printf("%s: unable to allocate ecbs, error = %d\n",
  718                     sc->sc_dev.dv_xname, error);
  719                 return (error);
  720         }
  721         if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
  722             ECBSIZE, (caddr_t *)&sc->sc_ecbs,
  723             BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
  724                 printf("%s: unable to map ecbs, error = %d\n",
  725                     sc->sc_dev.dv_xname, error);
  726                 return (error);
  727         }
  728 
  729         /*
  730          * Create and load the DMA map used for the ecbs.
  731          */
  732         if ((error = bus_dmamap_create(sc->sc_dmat, ECBSIZE,
  733             1, ECBSIZE, 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_ecb)) != 0) {
  734                 printf("%s: unable to create ecb DMA map, error = %d\n",
  735                     sc->sc_dev.dv_xname, error);
  736                 return (error);
  737         }
  738         if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_ecb,
  739             sc->sc_ecbs, ECBSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
  740                 printf("%s: unable to load ecb DMA map, error = %d\n",
  741                     sc->sc_dev.dv_xname, error);
  742                 return (error);
  743         }
  744 
  745 #undef ECBSIZE
  746 
  747         /*
  748          * Initialize the ecbs.
  749          */
  750         i = ahb_create_ecbs(sc, sc->sc_ecbs, AHB_ECB_MAX);
  751         if (i == 0) {
  752                 printf("%s: unable to create ecbs\n",
  753                     sc->sc_dev.dv_xname);
  754                 return (ENOMEM);
  755         } else if (i != AHB_ECB_MAX) {
  756                 printf("%s: WARNING: only %d of %d ecbs created\n",
  757                     sc->sc_dev.dv_xname, i, AHB_ECB_MAX);
  758         }
  759 
  760         sc->sc_adapter.adapt_openings = i;
  761 
  762         return (0);
  763 }
  764 
  765 static void
  766 ahbminphys(struct buf *bp)
  767 {
  768 
  769         if (bp->b_bcount > AHB_MAXXFER)
  770                 bp->b_bcount = AHB_MAXXFER;
  771         minphys(bp);
  772 }
  773 
  774 /*
  775  * start a scsi operation given the command and the data address.  Also needs
  776  * the unit, target and lu.
  777  */
  778 static void
  779 ahb_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
  780     void *arg)
  781 {
  782         struct scsipi_xfer *xs;
  783         struct scsipi_periph *periph;
  784         struct ahb_softc *sc = (void *)chan->chan_adapter->adapt_dev;
  785         bus_dma_tag_t dmat = sc->sc_dmat;
  786         struct ahb_ecb *ecb;
  787         int error, seg, flags, s;
  788 
  789         switch (req) {
  790         case ADAPTER_REQ_RUN_XFER:
  791                 xs = arg;
  792                 periph = xs->xs_periph;
  793                 flags = xs->xs_control;
  794 
  795                 SC_DEBUG(periph, SCSIPI_DB2, ("ahb_scsipi_request\n"));
  796 
  797                 /* Get an ECB to use. */
  798                 ecb = ahb_get_ecb(sc);
  799 #ifdef DIAGNOSTIC
  800                 /*
  801                  * This should never happen as we track the resources
  802                  * in the mid-layer.
  803                  */
  804                 if (ecb == NULL) {
  805                         scsipi_printaddr(periph);
  806                         printf("unable to allocate ecb\n");
  807                         panic("ahb_scsipi_request");
  808                 }
  809 #endif
  810 
  811                 ecb->xs = xs;
  812                 ecb->timeout = xs->timeout;
  813 
  814                 /*
  815                  * If it's a reset, we need to do an 'immediate'
  816                  * command, and store its ecb for later
  817                  * if there is already an immediate waiting,
  818                  * then WE must wait
  819                  */
  820                 if (flags & XS_CTL_RESET) {
  821                         ecb->flags |= ECB_IMMED;
  822                         if (sc->sc_immed_ecb) {
  823                                 ahb_free_ecb(sc, ecb);
  824                                 xs->error = XS_BUSY;
  825                                 scsipi_done(xs);
  826                                 return;
  827                         }
  828                         sc->sc_immed_ecb = ecb;
  829 
  830                         s = splbio();
  831                         ahb_send_immed(sc, AHB_TARG_RESET, ecb);
  832                         splx(s);
  833 
  834                         if ((flags & XS_CTL_POLL) == 0)
  835                                 return;
  836 
  837                         /*
  838                          * If we can't use interrupts, poll on completion
  839                          */
  840                         if (ahb_poll(sc, xs, ecb->timeout))
  841                                 ahb_timeout(ecb);
  842                         return;
  843                 }
  844 
  845                 /*
  846                  * Put all the arguments for the xfer in the ecb
  847                  */
  848                 if (xs->cmdlen > sizeof(ecb->scsi_cmd)) {
  849                         printf("%s: cmdlen %d too large for ECB\n",
  850                             sc->sc_dev.dv_xname, xs->cmdlen);
  851                         xs->error = XS_DRIVER_STUFFUP;
  852                         goto out_bad;
  853                 }
  854                 ecb->opcode = ECB_SCSI_OP;
  855                 ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
  856                 ecb->opt2 = periph->periph_lun | ECB_NRB;
  857                 bcopy(xs->cmd, &ecb->scsi_cmd,
  858                     ecb->scsi_cmd_length = xs->cmdlen);
  859                 ecb->sense_ptr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
  860                     AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_sense);
  861                 ecb->req_sense_length = sizeof(ecb->ecb_sense);
  862                 ecb->status = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
  863                     AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_status);
  864                 ecb->ecb_status.host_stat = 0x00;
  865                 ecb->ecb_status.target_stat = 0x00;
  866 
  867                 if (xs->datalen) {
  868                         /*
  869                          * Map the DMA transfer.
  870                          */
  871 #ifdef TFS
  872                         if (flags & XS_CTL_DATA_UIO) {
  873                                 error = bus_dmamap_load_uio(sc->sc_dmat,
  874                                     ecb->dmamap_xfer, (struct uio *)xs->data,
  875                                     BUS_DMA_NOWAIT);
  876                         } else
  877 #endif /* TFS */
  878                         {
  879                                 error = bus_dmamap_load(sc->sc_dmat,
  880                                     ecb->dmamap_xfer, xs->data, xs->datalen,
  881                                     NULL, BUS_DMA_NOWAIT);
  882                         }
  883 
  884                         switch (error) {
  885                         case 0:
  886                                 break;
  887 
  888                         case ENOMEM:
  889                         case EAGAIN:
  890                                 xs->error = XS_RESOURCE_SHORTAGE;
  891                                 goto out_bad;
  892 
  893                         default:
  894                                 xs->error = XS_DRIVER_STUFFUP;
  895                                 printf("%s: error %d loading DMA map\n",
  896                                     sc->sc_dev.dv_xname, error);
  897  out_bad:
  898                                 ahb_free_ecb(sc, ecb);
  899                                 scsipi_done(xs);
  900                                 return;
  901                         }
  902 
  903                         bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
  904                             ecb->dmamap_xfer->dm_mapsize,
  905                             (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
  906                             BUS_DMASYNC_PREWRITE);
  907 
  908                         /*
  909                          * Load the hardware scatter/gather map with the
  910                          * contents of the DMA map.
  911                          */
  912                         for (seg = 0; seg < ecb->dmamap_xfer->dm_nsegs; seg++) {
  913                                 ecb->ahb_dma[seg].seg_addr =
  914                                     ecb->dmamap_xfer->dm_segs[seg].ds_addr;
  915                                 ecb->ahb_dma[seg].seg_len =
  916                                     ecb->dmamap_xfer->dm_segs[seg].ds_len;
  917                         }
  918 
  919                         ecb->data_addr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
  920                             AHB_ECB_OFF(ecb) +
  921                             offsetof(struct ahb_ecb, ahb_dma);
  922                         ecb->data_length = ecb->dmamap_xfer->dm_nsegs *
  923                             sizeof(struct ahb_dma_seg);
  924                         ecb->opt1 |= ECB_S_G;
  925                 } else {        /* No data xfer, use non S/G values */
  926                         ecb->data_addr = (physaddr)0;
  927                         ecb->data_length = 0;
  928                 }
  929                 ecb->link_addr = (physaddr)0;
  930 
  931                 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ecb,
  932                     AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
  933                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  934 
  935                 s = splbio();
  936                 ahb_send_mbox(sc, OP_START_ECB, ecb);
  937                 splx(s);
  938 
  939                 if ((flags & XS_CTL_POLL) == 0)
  940                         return;
  941 
  942                 /*
  943                  * If we can't use interrupts, poll on completion
  944                  */
  945                 if (ahb_poll(sc, xs, ecb->timeout)) {
  946                         ahb_timeout(ecb);
  947                         if (ahb_poll(sc, xs, ecb->timeout))
  948                                 ahb_timeout(ecb);
  949                 }
  950                 return;
  951 
  952         case ADAPTER_REQ_GROW_RESOURCES:
  953                 /* XXX Not supported. */
  954                 return;
  955 
  956         case ADAPTER_REQ_SET_XFER_MODE:
  957                 /* XXX How do we do this? */
  958                 return;
  959         }
  960 }
  961 
  962 /*
  963  * Function to poll for command completion when in poll mode
  964  */
  965 static int
  966 ahb_poll(struct ahb_softc *sc, struct scsipi_xfer *xs, int count)
  967 {                               /* in msec  */
  968         bus_space_tag_t iot = sc->sc_iot;
  969         bus_space_handle_t ioh = sc->sc_ioh;
  970 
  971         while (count) {
  972                 /*
  973                  * If we had interrupts enabled, would we
  974                  * have got an interrupt?
  975                  */
  976                 if (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND)
  977                         ahbintr(sc);
  978                 if (xs->xs_status & XS_STS_DONE)
  979                         return 0;
  980                 delay(1000);
  981                 count--;
  982         }
  983         return 1;
  984 }
  985 
  986 static void
  987 ahb_timeout(void *arg)
  988 {
  989         struct ahb_ecb *ecb = arg;
  990         struct scsipi_xfer *xs = ecb->xs;
  991         struct scsipi_periph *periph = xs->xs_periph;
  992         struct ahb_softc *sc =
  993             (void *)periph->periph_channel->chan_adapter->adapt_dev;
  994         int s;
  995 
  996         scsipi_printaddr(periph);
  997         printf("timed out");
  998 
  999         s = splbio();
 1000 
 1001         if (ecb->flags & ECB_IMMED) {
 1002                 printf("\n");
 1003                 ecb->flags |= ECB_IMMED_FAIL;
 1004                 /* XXX Must reset! */
 1005         } else
 1006 
 1007         /*
 1008          * If it has been through before, then
 1009          * a previous abort has failed, don't
 1010          * try abort again
 1011          */
 1012         if (ecb->flags & ECB_ABORT) {
 1013                 /* abort timed out */
 1014                 printf(" AGAIN\n");
 1015                 /* XXX Must reset! */
 1016         } else {
 1017                 /* abort the operation that has timed out */
 1018                 printf("\n");
 1019                 ecb->xs->error = XS_TIMEOUT;
 1020                 ecb->timeout = AHB_ABORT_TIMEOUT;
 1021                 ecb->flags |= ECB_ABORT;
 1022                 ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
 1023         }
 1024 
 1025         splx(s);
 1026 }

Cache object: 14e895b30231c235362e8ef7fb02a0e6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.