Home | History | Annotate | Line # | Download | only in ic
mpt_netbsd.c revision 1.14.14.2
      1  1.14.14.2    bouyer /*	$NetBSD: mpt_netbsd.c,v 1.14.14.2 2012/02/04 17:05:56 bouyer Exp $	*/
      2        1.1   thorpej 
      3        1.1   thorpej /*
      4        1.1   thorpej  * Copyright (c) 2003 Wasabi Systems, Inc.
      5        1.1   thorpej  * All rights reserved.
      6        1.1   thorpej  *
      7        1.1   thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8        1.1   thorpej  *
      9        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     10        1.1   thorpej  * modification, are permitted provided that the following conditions
     11        1.1   thorpej  * are met:
     12        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     13        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     14        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     17        1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     18        1.1   thorpej  *    must display the following acknowledgement:
     19        1.1   thorpej  *	This product includes software developed for the NetBSD Project by
     20        1.1   thorpej  *	Wasabi Systems, Inc.
     21        1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22        1.1   thorpej  *    or promote products derived from this software without specific prior
     23        1.1   thorpej  *    written permission.
     24        1.1   thorpej  *
     25        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26        1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27        1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28        1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29        1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30        1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31        1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32        1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33        1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34        1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35        1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36        1.1   thorpej  */
     37        1.1   thorpej 
     38        1.1   thorpej /*
     39        1.1   thorpej  * Copyright (c) 2000, 2001 by Greg Ansley
     40        1.1   thorpej  * Partially derived from Matt Jacob's ISP driver.
     41        1.1   thorpej  *
     42        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     43        1.1   thorpej  * modification, are permitted provided that the following conditions
     44        1.1   thorpej  * are met:
     45        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     46        1.1   thorpej  *    notice immediately at the beginning of the file, without modification,
     47        1.1   thorpej  *    this list of conditions, and the following disclaimer.
     48        1.1   thorpej  * 2. The name of the author may not be used to endorse or promote products
     49        1.1   thorpej  *    derived from this software without specific prior written permission.
     50        1.1   thorpej  *
     51        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     52        1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53        1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54        1.1   thorpej  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     55        1.1   thorpej  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56        1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57        1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58        1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59        1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60        1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61        1.1   thorpej  * SUCH DAMAGE.
     62        1.1   thorpej  */
     63        1.1   thorpej /*
     64        1.1   thorpej  * Additional Copyright (c) 2002 by Matthew Jacob under same license.
     65        1.1   thorpej  */
     66        1.1   thorpej 
     67        1.1   thorpej /*
     68        1.1   thorpej  * mpt_netbsd.c:
     69        1.1   thorpej  *
     70        1.1   thorpej  * NetBSD-specific routines for LSI Fusion adapters.  Includes some
     71        1.1   thorpej  * bus_dma glue, and SCSIPI glue.
     72        1.1   thorpej  *
     73        1.1   thorpej  * Adapted from the FreeBSD "mpt" driver by Jason R. Thorpe for
     74        1.1   thorpej  * Wasabi Systems, Inc.
     75       1.12      tron  *
     76       1.12      tron  * Additional contributions by Garrett D'Amore on behalf of TELES AG.
     77        1.1   thorpej  */
     78        1.7     lukem 
     79        1.7     lukem #include <sys/cdefs.h>
     80  1.14.14.2    bouyer __KERNEL_RCSID(0, "$NetBSD: mpt_netbsd.c,v 1.14.14.2 2012/02/04 17:05:56 bouyer Exp $");
     81        1.1   thorpej 
     82        1.1   thorpej #include <dev/ic/mpt.h>			/* pulls in all headers */
     83        1.1   thorpej 
     84        1.1   thorpej #include <machine/stdarg.h>		/* for mpt_prt() */
     85        1.1   thorpej 
     86        1.1   thorpej static int	mpt_poll(mpt_softc_t *, struct scsipi_xfer *, int);
     87        1.1   thorpej static void	mpt_timeout(void *);
     88        1.1   thorpej static void	mpt_done(mpt_softc_t *, uint32_t);
     89        1.1   thorpej static void	mpt_run_xfer(mpt_softc_t *, struct scsipi_xfer *);
     90        1.1   thorpej static void	mpt_set_xfer_mode(mpt_softc_t *, struct scsipi_xfer_mode *);
     91        1.1   thorpej static void	mpt_get_xfer_mode(mpt_softc_t *, struct scsipi_periph *);
     92        1.1   thorpej static void	mpt_ctlop(mpt_softc_t *, void *vmsg, uint32_t);
     93        1.1   thorpej static void	mpt_event_notify_reply(mpt_softc_t *, MSG_EVENT_NOTIFY_REPLY *);
     94        1.1   thorpej 
     95        1.1   thorpej static void	mpt_scsipi_request(struct scsipi_channel *,
     96        1.1   thorpej 		    scsipi_adapter_req_t, void *);
     97        1.1   thorpej static void	mpt_minphys(struct buf *);
     98        1.1   thorpej 
     99        1.1   thorpej void
    100        1.1   thorpej mpt_scsipi_attach(mpt_softc_t *mpt)
    101        1.1   thorpej {
    102        1.1   thorpej 	struct scsipi_adapter *adapt = &mpt->sc_adapter;
    103        1.1   thorpej 	struct scsipi_channel *chan = &mpt->sc_channel;
    104        1.1   thorpej 	int maxq;
    105        1.1   thorpej 
    106        1.1   thorpej 	mpt->bus = 0;		/* XXX ?? */
    107        1.1   thorpej 
    108        1.1   thorpej 	maxq = (mpt->mpt_global_credits < MPT_MAX_REQUESTS(mpt)) ?
    109        1.1   thorpej 	    mpt->mpt_global_credits : MPT_MAX_REQUESTS(mpt);
    110        1.1   thorpej 
    111        1.1   thorpej 	/* Fill in the scsipi_adapter. */
    112        1.1   thorpej 	memset(adapt, 0, sizeof(*adapt));
    113        1.1   thorpej 	adapt->adapt_dev = &mpt->sc_dev;
    114        1.1   thorpej 	adapt->adapt_nchannels = 1;
    115  1.14.14.2    bouyer 	adapt->adapt_openings = maxq - 2;	/* Reserve 2 for driver use*/
    116  1.14.14.2    bouyer 	adapt->adapt_max_periph = maxq - 2;
    117        1.1   thorpej 	adapt->adapt_request = mpt_scsipi_request;
    118        1.1   thorpej 	adapt->adapt_minphys = mpt_minphys;
    119        1.1   thorpej 
    120        1.1   thorpej 	/* Fill in the scsipi_channel. */
    121        1.1   thorpej 	memset(chan, 0, sizeof(*chan));
    122        1.1   thorpej 	chan->chan_adapter = adapt;
    123        1.1   thorpej 	chan->chan_bustype = &scsi_bustype;
    124        1.1   thorpej 	chan->chan_channel = 0;
    125        1.1   thorpej 	chan->chan_flags = 0;
    126        1.1   thorpej 	chan->chan_nluns = 8;
    127       1.12      tron 	chan->chan_ntargets = mpt->mpt_max_devices;
    128       1.12      tron 	chan->chan_id = mpt->mpt_ini_id;
    129        1.1   thorpej 
    130        1.1   thorpej 	(void) config_found(&mpt->sc_dev, &mpt->sc_channel, scsiprint);
    131        1.1   thorpej }
    132        1.1   thorpej 
    133        1.1   thorpej int
    134        1.1   thorpej mpt_dma_mem_alloc(mpt_softc_t *mpt)
    135        1.1   thorpej {
    136        1.1   thorpej 	bus_dma_segment_t reply_seg, request_seg;
    137        1.1   thorpej 	int reply_rseg, request_rseg;
    138        1.1   thorpej 	bus_addr_t pptr, end;
    139       1.11  christos 	char *vptr;
    140        1.1   thorpej 	size_t len;
    141        1.1   thorpej 	int error, i;
    142        1.1   thorpej 
    143        1.1   thorpej 	/* Check if we have already allocated the reply memory. */
    144        1.1   thorpej 	if (mpt->reply != NULL)
    145        1.1   thorpej 		return (0);
    146        1.1   thorpej 
    147        1.1   thorpej 	/*
    148        1.1   thorpej 	 * Allocate the request pool.  This isn't really DMA'd memory,
    149        1.1   thorpej 	 * but it's a convenient place to do it.
    150        1.1   thorpej 	 */
    151        1.1   thorpej 	len = sizeof(request_t) * MPT_MAX_REQUESTS(mpt);
    152        1.1   thorpej 	mpt->request_pool = malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
    153        1.1   thorpej 	if (mpt->request_pool == NULL) {
    154       1.14    cegger 		aprint_error_dev(&mpt->sc_dev, "unable to allocate request pool\n");
    155        1.1   thorpej 		return (ENOMEM);
    156        1.1   thorpej 	}
    157        1.1   thorpej 
    158        1.1   thorpej 	/*
    159        1.1   thorpej 	 * Allocate DMA resources for reply buffers.
    160        1.1   thorpej 	 */
    161        1.1   thorpej 	error = bus_dmamem_alloc(mpt->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0,
    162        1.1   thorpej 	    &reply_seg, 1, &reply_rseg, 0);
    163        1.1   thorpej 	if (error) {
    164       1.14    cegger 		aprint_error_dev(&mpt->sc_dev, "unable to allocate reply area, error = %d\n",
    165       1.14    cegger 		    error);
    166        1.1   thorpej 		goto fail_0;
    167        1.1   thorpej 	}
    168        1.1   thorpej 
    169        1.1   thorpej 	error = bus_dmamem_map(mpt->sc_dmat, &reply_seg, reply_rseg, PAGE_SIZE,
    170       1.11  christos 	    (void **) &mpt->reply, BUS_DMA_COHERENT/*XXX*/);
    171        1.1   thorpej 	if (error) {
    172       1.14    cegger 		aprint_error_dev(&mpt->sc_dev, "unable to map reply area, error = %d\n",
    173       1.14    cegger 		    error);
    174        1.1   thorpej 		goto fail_1;
    175        1.1   thorpej 	}
    176        1.1   thorpej 
    177        1.1   thorpej 	error = bus_dmamap_create(mpt->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
    178        1.1   thorpej 	    0, 0, &mpt->reply_dmap);
    179        1.1   thorpej 	if (error) {
    180       1.14    cegger 		aprint_error_dev(&mpt->sc_dev, "unable to create reply DMA map, error = %d\n",
    181       1.14    cegger 		    error);
    182        1.1   thorpej 		goto fail_2;
    183        1.1   thorpej 	}
    184        1.1   thorpej 
    185        1.1   thorpej 	error = bus_dmamap_load(mpt->sc_dmat, mpt->reply_dmap, mpt->reply,
    186        1.1   thorpej 	    PAGE_SIZE, NULL, 0);
    187        1.1   thorpej 	if (error) {
    188       1.14    cegger 		aprint_error_dev(&mpt->sc_dev, "unable to load reply DMA map, error = %d\n",
    189       1.14    cegger 		    error);
    190        1.1   thorpej 		goto fail_3;
    191        1.1   thorpej 	}
    192        1.1   thorpej 	mpt->reply_phys = mpt->reply_dmap->dm_segs[0].ds_addr;
    193        1.1   thorpej 
    194        1.1   thorpej 	/*
    195        1.1   thorpej 	 * Allocate DMA resources for request buffers.
    196        1.1   thorpej 	 */
    197        1.1   thorpej 	error = bus_dmamem_alloc(mpt->sc_dmat, MPT_REQ_MEM_SIZE(mpt),
    198        1.1   thorpej 	    PAGE_SIZE, 0, &request_seg, 1, &request_rseg, 0);
    199        1.1   thorpej 	if (error) {
    200       1.14    cegger 		aprint_error_dev(&mpt->sc_dev, "unable to allocate request area, "
    201       1.14    cegger 		    "error = %d\n", error);
    202        1.1   thorpej 		goto fail_4;
    203        1.1   thorpej 	}
    204        1.1   thorpej 
    205        1.1   thorpej 	error = bus_dmamem_map(mpt->sc_dmat, &request_seg, request_rseg,
    206       1.11  christos 	    MPT_REQ_MEM_SIZE(mpt), (void **) &mpt->request, 0);
    207        1.1   thorpej 	if (error) {
    208       1.14    cegger 		aprint_error_dev(&mpt->sc_dev, "unable to map request area, error = %d\n",
    209       1.14    cegger 		    error);
    210        1.1   thorpej 		goto fail_5;
    211        1.1   thorpej 	}
    212        1.1   thorpej 
    213        1.1   thorpej 	error = bus_dmamap_create(mpt->sc_dmat, MPT_REQ_MEM_SIZE(mpt), 1,
    214        1.1   thorpej 	    MPT_REQ_MEM_SIZE(mpt), 0, 0, &mpt->request_dmap);
    215        1.1   thorpej 	if (error) {
    216       1.14    cegger 		aprint_error_dev(&mpt->sc_dev, "unable to create request DMA map, "
    217       1.14    cegger 		    "error = %d\n", error);
    218        1.1   thorpej 		goto fail_6;
    219        1.1   thorpej 	}
    220        1.1   thorpej 
    221        1.1   thorpej 	error = bus_dmamap_load(mpt->sc_dmat, mpt->request_dmap, mpt->request,
    222        1.1   thorpej 	    MPT_REQ_MEM_SIZE(mpt), NULL, 0);
    223        1.1   thorpej 	if (error) {
    224       1.14    cegger 		aprint_error_dev(&mpt->sc_dev, "unable to load request DMA map, error = %d\n",
    225       1.14    cegger 		    error);
    226        1.1   thorpej 		goto fail_7;
    227        1.1   thorpej 	}
    228        1.1   thorpej 	mpt->request_phys = mpt->request_dmap->dm_segs[0].ds_addr;
    229        1.1   thorpej 
    230        1.1   thorpej 	pptr = mpt->request_phys;
    231       1.11  christos 	vptr = (void *) mpt->request;
    232        1.1   thorpej 	end = pptr + MPT_REQ_MEM_SIZE(mpt);
    233        1.1   thorpej 
    234        1.1   thorpej 	for (i = 0; pptr < end; i++) {
    235        1.1   thorpej 		request_t *req = &mpt->request_pool[i];
    236        1.1   thorpej 		req->index = i;
    237        1.1   thorpej 
    238        1.1   thorpej 		/* Store location of Request Data */
    239        1.1   thorpej 		req->req_pbuf = pptr;
    240        1.1   thorpej 		req->req_vbuf = vptr;
    241        1.1   thorpej 
    242        1.1   thorpej 		pptr += MPT_REQUEST_AREA;
    243        1.1   thorpej 		vptr += MPT_REQUEST_AREA;
    244        1.1   thorpej 
    245        1.1   thorpej 		req->sense_pbuf = (pptr - MPT_SENSE_SIZE);
    246        1.1   thorpej 		req->sense_vbuf = (vptr - MPT_SENSE_SIZE);
    247        1.1   thorpej 
    248        1.5       tls 		error = bus_dmamap_create(mpt->sc_dmat, MAXPHYS,
    249        1.5       tls 		    MPT_SGL_MAX, MAXPHYS, 0, 0, &req->dmap);
    250        1.1   thorpej 		if (error) {
    251       1.14    cegger 			aprint_error_dev(&mpt->sc_dev, "unable to create req %d DMA map, "
    252       1.14    cegger 			    "error = %d\n", i, error);
    253        1.1   thorpej 			goto fail_8;
    254        1.1   thorpej 		}
    255        1.1   thorpej 	}
    256        1.1   thorpej 
    257        1.1   thorpej 	return (0);
    258        1.1   thorpej 
    259        1.1   thorpej  fail_8:
    260        1.1   thorpej 	for (--i; i >= 0; i--) {
    261        1.1   thorpej 		request_t *req = &mpt->request_pool[i];
    262        1.3   thorpej 		if (req->dmap != NULL)
    263        1.3   thorpej 			bus_dmamap_destroy(mpt->sc_dmat, req->dmap);
    264        1.1   thorpej 	}
    265        1.1   thorpej 	bus_dmamap_unload(mpt->sc_dmat, mpt->request_dmap);
    266        1.1   thorpej  fail_7:
    267        1.1   thorpej 	bus_dmamap_destroy(mpt->sc_dmat, mpt->request_dmap);
    268        1.1   thorpej  fail_6:
    269       1.11  christos 	bus_dmamem_unmap(mpt->sc_dmat, (void *)mpt->request, PAGE_SIZE);
    270        1.1   thorpej  fail_5:
    271        1.1   thorpej 	bus_dmamem_free(mpt->sc_dmat, &request_seg, request_rseg);
    272        1.1   thorpej  fail_4:
    273        1.1   thorpej 	bus_dmamap_unload(mpt->sc_dmat, mpt->reply_dmap);
    274        1.1   thorpej  fail_3:
    275        1.1   thorpej 	bus_dmamap_destroy(mpt->sc_dmat, mpt->reply_dmap);
    276        1.1   thorpej  fail_2:
    277       1.11  christos 	bus_dmamem_unmap(mpt->sc_dmat, (void *)mpt->reply, PAGE_SIZE);
    278        1.1   thorpej  fail_1:
    279        1.1   thorpej 	bus_dmamem_free(mpt->sc_dmat, &reply_seg, reply_rseg);
    280        1.1   thorpej  fail_0:
    281        1.1   thorpej 	free(mpt->request_pool, M_DEVBUF);
    282        1.1   thorpej 
    283        1.1   thorpej 	mpt->reply = NULL;
    284        1.1   thorpej 	mpt->request = NULL;
    285        1.1   thorpej 	mpt->request_pool = NULL;
    286        1.1   thorpej 
    287        1.1   thorpej 	return (error);
    288        1.1   thorpej }
    289        1.1   thorpej 
    290        1.1   thorpej int
    291        1.1   thorpej mpt_intr(void *arg)
    292        1.1   thorpej {
    293        1.1   thorpej 	mpt_softc_t *mpt = arg;
    294        1.1   thorpej 	int nrepl = 0;
    295        1.1   thorpej 	uint32_t reply;
    296        1.1   thorpej 
    297        1.1   thorpej 	if ((mpt_read(mpt, MPT_OFFSET_INTR_STATUS) & MPT_INTR_REPLY_READY) == 0)
    298        1.1   thorpej 		return (0);
    299        1.1   thorpej 
    300        1.1   thorpej 	reply = mpt_pop_reply_queue(mpt);
    301        1.1   thorpej 	while (reply != MPT_REPLY_EMPTY) {
    302        1.1   thorpej 		nrepl++;
    303        1.1   thorpej 		if (mpt->verbose > 1) {
    304        1.1   thorpej 			if ((reply & MPT_CONTEXT_REPLY) != 0) {
    305        1.1   thorpej 				/* Address reply; IOC has something to say */
    306        1.1   thorpej 				mpt_print_reply(MPT_REPLY_PTOV(mpt, reply));
    307        1.1   thorpej 			} else {
    308        1.1   thorpej 				/* Context reply; all went well */
    309        1.1   thorpej 				mpt_prt(mpt, "context %u reply OK", reply);
    310        1.1   thorpej 			}
    311        1.1   thorpej 		}
    312        1.1   thorpej 		mpt_done(mpt, reply);
    313        1.1   thorpej 		reply = mpt_pop_reply_queue(mpt);
    314        1.1   thorpej 	}
    315        1.1   thorpej 	return (nrepl != 0);
    316        1.1   thorpej }
    317        1.1   thorpej 
    318        1.1   thorpej void
    319        1.1   thorpej mpt_prt(mpt_softc_t *mpt, const char *fmt, ...)
    320        1.1   thorpej {
    321        1.1   thorpej 	va_list ap;
    322        1.1   thorpej 
    323       1.14    cegger 	printf("%s: ", device_xname(&mpt->sc_dev));
    324        1.1   thorpej 	va_start(ap, fmt);
    325        1.1   thorpej 	vprintf(fmt, ap);
    326        1.1   thorpej 	va_end(ap);
    327        1.1   thorpej 	printf("\n");
    328        1.1   thorpej }
    329        1.1   thorpej 
    330        1.1   thorpej static int
    331        1.1   thorpej mpt_poll(mpt_softc_t *mpt, struct scsipi_xfer *xs, int count)
    332        1.1   thorpej {
    333        1.1   thorpej 
    334        1.1   thorpej 	/* Timeouts are in msec, so we loop in 1000usec cycles */
    335        1.1   thorpej 	while (count) {
    336        1.1   thorpej 		mpt_intr(mpt);
    337        1.1   thorpej 		if (xs->xs_status & XS_STS_DONE)
    338        1.1   thorpej 			return (0);
    339        1.1   thorpej 		delay(1000);		/* only happens in boot, so ok */
    340        1.1   thorpej 		count--;
    341        1.1   thorpej 	}
    342        1.1   thorpej 	return (1);
    343        1.1   thorpej }
    344        1.1   thorpej 
    345        1.1   thorpej static void
    346        1.1   thorpej mpt_timeout(void *arg)
    347        1.1   thorpej {
    348        1.1   thorpej 	request_t *req = arg;
    349        1.1   thorpej 	struct scsipi_xfer *xs = req->xfer;
    350        1.1   thorpej 	struct scsipi_periph *periph = xs->xs_periph;
    351        1.9     perry 	mpt_softc_t *mpt =
    352        1.1   thorpej 	    (void *) periph->periph_channel->chan_adapter->adapt_dev;
    353        1.1   thorpej 	uint32_t oseq;
    354        1.1   thorpej 	int s;
    355        1.1   thorpej 
    356        1.1   thorpej 	scsipi_printaddr(periph);
    357        1.1   thorpej 	printf("command timeout\n");
    358        1.1   thorpej 
    359        1.1   thorpej 	s = splbio();
    360        1.1   thorpej 
    361        1.1   thorpej 	oseq = req->sequence;
    362        1.1   thorpej 	mpt->timeouts++;
    363        1.1   thorpej 	if (mpt_intr(mpt)) {
    364        1.1   thorpej 		if (req->sequence != oseq) {
    365        1.1   thorpej 			mpt_prt(mpt, "recovered from command timeout");
    366        1.1   thorpej 			splx(s);
    367        1.1   thorpej 			return;
    368        1.1   thorpej 		}
    369        1.1   thorpej 	}
    370        1.1   thorpej 	mpt_prt(mpt,
    371        1.1   thorpej 	    "timeout on request index = 0x%x, seq = 0x%08x",
    372        1.1   thorpej 	    req->index, req->sequence);
    373        1.1   thorpej 	mpt_check_doorbell(mpt);
    374        1.1   thorpej 	mpt_prt(mpt, "Status 0x%08x, Mask 0x%08x, Doorbell 0x%08x",
    375        1.1   thorpej 	    mpt_read(mpt, MPT_OFFSET_INTR_STATUS),
    376        1.1   thorpej 	    mpt_read(mpt, MPT_OFFSET_INTR_MASK),
    377        1.1   thorpej 	    mpt_read(mpt, MPT_OFFSET_DOORBELL));
    378        1.1   thorpej 	mpt_prt(mpt, "request state: %s", mpt_req_state(req->debug));
    379        1.1   thorpej 	if (mpt->verbose > 1)
    380        1.1   thorpej 		mpt_print_scsi_io_request((MSG_SCSI_IO_REQUEST *)req->req_vbuf);
    381        1.1   thorpej 
    382        1.1   thorpej 	/* XXX WHAT IF THE IOC IS STILL USING IT?? */
    383        1.1   thorpej 	req->xfer = NULL;
    384        1.1   thorpej 	mpt_free_request(mpt, req);
    385        1.1   thorpej 
    386        1.6   thorpej 	xs->error = XS_TIMEOUT;
    387        1.6   thorpej 	scsipi_done(xs);
    388        1.6   thorpej 
    389        1.1   thorpej 	splx(s);
    390        1.1   thorpej }
    391        1.1   thorpej 
    392        1.1   thorpej static void
    393        1.1   thorpej mpt_done(mpt_softc_t *mpt, uint32_t reply)
    394        1.1   thorpej {
    395        1.6   thorpej 	struct scsipi_xfer *xs = NULL;
    396        1.1   thorpej 	struct scsipi_periph *periph;
    397        1.1   thorpej 	int index;
    398        1.1   thorpej 	request_t *req;
    399        1.1   thorpej 	MSG_REQUEST_HEADER *mpt_req;
    400        1.1   thorpej 	MSG_SCSI_IO_REPLY *mpt_reply;
    401        1.1   thorpej 
    402        1.1   thorpej 	if (__predict_true((reply & MPT_CONTEXT_REPLY) == 0)) {
    403        1.1   thorpej 		/* context reply (ok) */
    404        1.1   thorpej 		mpt_reply = NULL;
    405        1.1   thorpej 		index = reply & MPT_CONTEXT_MASK;
    406        1.1   thorpej 	} else {
    407        1.1   thorpej 		/* address reply (error) */
    408        1.1   thorpej 
    409        1.1   thorpej 		/* XXX BUS_DMASYNC_POSTREAD XXX */
    410        1.1   thorpej 		mpt_reply = MPT_REPLY_PTOV(mpt, reply);
    411        1.1   thorpej 		if (mpt->verbose > 1) {
    412        1.1   thorpej 			uint32_t *pReply = (uint32_t *) mpt_reply;
    413        1.9     perry 
    414        1.1   thorpej 			mpt_prt(mpt, "Address Reply (index %u):",
    415  1.14.14.1       snj 			    le32toh(mpt_reply->MsgContext) & 0xffff);
    416        1.1   thorpej 			mpt_prt(mpt, "%08x %08x %08x %08x",
    417        1.1   thorpej 			    pReply[0], pReply[1], pReply[2], pReply[3]);
    418        1.1   thorpej 			mpt_prt(mpt, "%08x %08x %08x %08x",
    419        1.1   thorpej 			    pReply[4], pReply[5], pReply[6], pReply[7]);
    420        1.1   thorpej 			mpt_prt(mpt, "%08x %08x %08x %08x",
    421        1.1   thorpej 			    pReply[8], pReply[9], pReply[10], pReply[11]);
    422        1.1   thorpej 		}
    423  1.14.14.1       snj 		index = le32toh(mpt_reply->MsgContext);
    424        1.1   thorpej 	}
    425        1.1   thorpej 
    426        1.1   thorpej 	/*
    427        1.1   thorpej 	 * Address reply with MessageContext high bit set.
    428        1.1   thorpej 	 * This is most likely a notify message, so we try
    429        1.1   thorpej 	 * to process it, then free it.
    430        1.1   thorpej 	 */
    431        1.1   thorpej 	if (__predict_false((index & 0x80000000) != 0)) {
    432        1.1   thorpej 		if (mpt_reply != NULL)
    433        1.1   thorpej 			mpt_ctlop(mpt, mpt_reply, reply);
    434        1.1   thorpej 		else
    435        1.1   thorpej 			mpt_prt(mpt, "mpt_done: index 0x%x, NULL reply", index);
    436        1.1   thorpej 		return;
    437        1.1   thorpej 	}
    438        1.1   thorpej 
    439        1.1   thorpej 	/* Did we end up with a valid index into the table? */
    440        1.1   thorpej 	if (__predict_false(index < 0 || index >= MPT_MAX_REQUESTS(mpt))) {
    441        1.1   thorpej 		mpt_prt(mpt, "mpt_done: invalid index (0x%x) in reply", index);
    442        1.1   thorpej 		return;
    443        1.1   thorpej 	}
    444        1.1   thorpej 
    445        1.1   thorpej 	req = &mpt->request_pool[index];
    446        1.1   thorpej 
    447        1.1   thorpej 	/* Make sure memory hasn't been trashed. */
    448        1.1   thorpej 	if (__predict_false(req->index != index)) {
    449        1.1   thorpej 		mpt_prt(mpt, "mpt_done: corrupted request_t (0x%x)", index);
    450        1.1   thorpej 		return;
    451        1.1   thorpej 	}
    452        1.1   thorpej 
    453        1.2   thorpej 	MPT_SYNC_REQ(mpt, req, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    454        1.1   thorpej 	mpt_req = req->req_vbuf;
    455        1.1   thorpej 
    456        1.1   thorpej 	/* Short cut for task management replies; nothing more for us to do. */
    457        1.1   thorpej 	if (__predict_false(mpt_req->Function == MPI_FUNCTION_SCSI_TASK_MGMT)) {
    458        1.1   thorpej 		if (mpt->verbose > 1)
    459        1.1   thorpej 			mpt_prt(mpt, "mpt_done: TASK MGMT");
    460        1.1   thorpej 		goto done;
    461        1.1   thorpej 	}
    462        1.1   thorpej 
    463        1.1   thorpej 	if (__predict_false(mpt_req->Function == MPI_FUNCTION_PORT_ENABLE))
    464        1.1   thorpej 		goto done;
    465        1.1   thorpej 
    466        1.1   thorpej 	/*
    467        1.1   thorpej 	 * At this point, it had better be a SCSI I/O command, but don't
    468        1.1   thorpej 	 * crash if it isn't.
    469        1.1   thorpej 	 */
    470        1.1   thorpej 	if (__predict_false(mpt_req->Function !=
    471        1.1   thorpej 			    MPI_FUNCTION_SCSI_IO_REQUEST)) {
    472        1.1   thorpej 		if (mpt->verbose > 1)
    473        1.1   thorpej 			mpt_prt(mpt, "mpt_done: unknown Function 0x%x (0x%x)",
    474        1.1   thorpej 			    mpt_req->Function, index);
    475        1.1   thorpej 		goto done;
    476        1.1   thorpej 	}
    477        1.1   thorpej 
    478        1.1   thorpej 	/* Recover scsipi_xfer from the request structure. */
    479        1.1   thorpej 	xs = req->xfer;
    480        1.1   thorpej 
    481        1.1   thorpej 	/* Can't have a SCSI command without a scsipi_xfer. */
    482        1.1   thorpej 	if (__predict_false(xs == NULL)) {
    483        1.1   thorpej 		mpt_prt(mpt,
    484        1.1   thorpej 		    "mpt_done: no scsipi_xfer, index = 0x%x, seq = 0x%08x",
    485        1.1   thorpej 		    req->index, req->sequence);
    486        1.1   thorpej 		mpt_prt(mpt, "request state: %s", mpt_req_state(req->debug));
    487        1.1   thorpej 		mpt_prt(mpt, "mpt_request:");
    488        1.1   thorpej 		mpt_print_scsi_io_request((MSG_SCSI_IO_REQUEST *)req->req_vbuf);
    489        1.1   thorpej 
    490        1.1   thorpej 		if (mpt_reply != NULL) {
    491        1.1   thorpej 			mpt_prt(mpt, "mpt_reply:");
    492        1.1   thorpej 			mpt_print_reply(mpt_reply);
    493        1.1   thorpej 		} else {
    494        1.1   thorpej 			mpt_prt(mpt, "context reply: 0x%08x", reply);
    495        1.1   thorpej 		}
    496        1.1   thorpej 		goto done;
    497        1.1   thorpej 	}
    498        1.1   thorpej 
    499        1.1   thorpej 	callout_stop(&xs->xs_callout);
    500        1.1   thorpej 
    501        1.1   thorpej 	periph = xs->xs_periph;
    502        1.1   thorpej 
    503        1.1   thorpej 	/*
    504        1.1   thorpej 	 * If we were a data transfer, unload the map that described
    505        1.1   thorpej 	 * the data buffer.
    506        1.1   thorpej 	 */
    507        1.1   thorpej 	if (__predict_true(xs->datalen != 0)) {
    508        1.1   thorpej 		bus_dmamap_sync(mpt->sc_dmat, req->dmap, 0,
    509        1.1   thorpej 		    req->dmap->dm_mapsize,
    510        1.1   thorpej 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD
    511        1.1   thorpej 						      : BUS_DMASYNC_POSTWRITE);
    512        1.1   thorpej 		bus_dmamap_unload(mpt->sc_dmat, req->dmap);
    513        1.1   thorpej 	}
    514        1.1   thorpej 
    515        1.1   thorpej 	if (__predict_true(mpt_reply == NULL)) {
    516        1.1   thorpej 		/*
    517        1.1   thorpej 		 * Context reply; report that the command was
    518        1.1   thorpej 		 * successful!
    519        1.1   thorpej 		 *
    520        1.1   thorpej 		 * Also report the xfer mode, if necessary.
    521        1.1   thorpej 		 */
    522        1.1   thorpej 		if (__predict_false(mpt->mpt_report_xfer_mode != 0)) {
    523        1.1   thorpej 			if ((mpt->mpt_report_xfer_mode &
    524        1.1   thorpej 			     (1 << periph->periph_target)) != 0)
    525        1.1   thorpej 				mpt_get_xfer_mode(mpt, periph);
    526        1.1   thorpej 		}
    527        1.1   thorpej 		xs->error = XS_NOERROR;
    528        1.1   thorpej 		xs->status = SCSI_OK;
    529        1.1   thorpej 		xs->resid = 0;
    530        1.6   thorpej 		mpt_free_request(mpt, req);
    531        1.1   thorpej 		scsipi_done(xs);
    532        1.6   thorpej 		return;
    533        1.1   thorpej 	}
    534        1.1   thorpej 
    535        1.1   thorpej 	xs->status = mpt_reply->SCSIStatus;
    536  1.14.14.1       snj 	switch (le16toh(mpt_reply->IOCStatus)) {
    537        1.1   thorpej 	case MPI_IOCSTATUS_SCSI_DATA_OVERRUN:
    538        1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
    539        1.1   thorpej 		break;
    540        1.1   thorpej 
    541        1.1   thorpej 	case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN:
    542        1.1   thorpej 		/*
    543        1.1   thorpej 		 * Yikes!  Tagged queue full comes through this path!
    544        1.1   thorpej 		 *
    545        1.1   thorpej 		 * So we'll change it to a status error and anything
    546        1.1   thorpej 		 * that returns status should probably be a status
    547        1.1   thorpej 		 * error as well.
    548        1.1   thorpej 		 */
    549  1.14.14.1       snj 		xs->resid = xs->datalen - le32toh(mpt_reply->TransferCount);
    550        1.1   thorpej 		if (mpt_reply->SCSIState &
    551        1.1   thorpej 		    MPI_SCSI_STATE_NO_SCSI_STATUS) {
    552        1.1   thorpej 			xs->error = XS_DRIVER_STUFFUP;
    553        1.1   thorpej 			break;
    554        1.1   thorpej 		}
    555        1.1   thorpej 		/* FALLTHROUGH */
    556        1.1   thorpej 	case MPI_IOCSTATUS_SUCCESS:
    557        1.1   thorpej 	case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR:
    558        1.1   thorpej 		switch (xs->status) {
    559        1.1   thorpej 		case SCSI_OK:
    560        1.1   thorpej 			/* Report the xfer mode, if necessary. */
    561        1.1   thorpej 			if ((mpt->mpt_report_xfer_mode &
    562        1.1   thorpej 			     (1 << periph->periph_target)) != 0)
    563        1.1   thorpej 				mpt_get_xfer_mode(mpt, periph);
    564        1.1   thorpej 			xs->resid = 0;
    565        1.1   thorpej 			break;
    566        1.1   thorpej 
    567        1.1   thorpej 		case SCSI_CHECK:
    568        1.1   thorpej 			xs->error = XS_SENSE;
    569        1.1   thorpej 			break;
    570        1.1   thorpej 
    571        1.1   thorpej 		case SCSI_BUSY:
    572        1.1   thorpej 		case SCSI_QUEUE_FULL:
    573        1.1   thorpej 			xs->error = XS_BUSY;
    574        1.1   thorpej 			break;
    575        1.1   thorpej 
    576        1.1   thorpej 		default:
    577        1.1   thorpej 			scsipi_printaddr(periph);
    578        1.1   thorpej 			printf("invalid status code %d\n", xs->status);
    579        1.1   thorpej 			xs->error = XS_DRIVER_STUFFUP;
    580        1.1   thorpej 			break;
    581        1.1   thorpej 		}
    582        1.1   thorpej 		break;
    583        1.1   thorpej 
    584        1.1   thorpej 	case MPI_IOCSTATUS_BUSY:
    585        1.1   thorpej 	case MPI_IOCSTATUS_INSUFFICIENT_RESOURCES:
    586        1.1   thorpej 		xs->error = XS_RESOURCE_SHORTAGE;
    587        1.1   thorpej 		break;
    588        1.1   thorpej 
    589        1.1   thorpej 	case MPI_IOCSTATUS_SCSI_INVALID_BUS:
    590        1.1   thorpej 	case MPI_IOCSTATUS_SCSI_INVALID_TARGETID:
    591        1.1   thorpej 	case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
    592        1.1   thorpej 		xs->error = XS_SELTIMEOUT;
    593        1.1   thorpej 		break;
    594        1.1   thorpej 
    595        1.1   thorpej 	case MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
    596        1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
    597        1.1   thorpej 		break;
    598        1.1   thorpej 
    599        1.1   thorpej 	case MPI_IOCSTATUS_SCSI_TASK_TERMINATED:
    600        1.1   thorpej 		/* XXX What should we do here? */
    601        1.1   thorpej 		break;
    602        1.1   thorpej 
    603        1.1   thorpej 	case MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
    604        1.1   thorpej 		/* XXX */
    605        1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
    606        1.1   thorpej 		break;
    607        1.1   thorpej 
    608        1.1   thorpej 	case MPI_IOCSTATUS_SCSI_IOC_TERMINATED:
    609        1.1   thorpej 		/* XXX */
    610        1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
    611        1.1   thorpej 		break;
    612        1.1   thorpej 
    613        1.1   thorpej 	case MPI_IOCSTATUS_SCSI_EXT_TERMINATED:
    614        1.1   thorpej 		/* XXX This is a bus-reset */
    615        1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
    616        1.1   thorpej 		break;
    617        1.1   thorpej 
    618        1.1   thorpej 	default:
    619        1.1   thorpej 		/* XXX unrecognized HBA error */
    620        1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
    621        1.1   thorpej 		break;
    622        1.1   thorpej 	}
    623        1.9     perry 
    624        1.1   thorpej 	if (mpt_reply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_VALID) {
    625        1.1   thorpej 		memcpy(&xs->sense.scsi_sense, req->sense_vbuf,
    626        1.1   thorpej 		    sizeof(xs->sense.scsi_sense));
    627        1.1   thorpej 	} else if (mpt_reply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_FAILED) {
    628        1.1   thorpej 		/*
    629        1.1   thorpej 		 * This will cause the scsipi layer to issue
    630        1.1   thorpej 		 * a REQUEST SENSE.
    631        1.1   thorpej 		 */
    632        1.1   thorpej 		if (xs->status == SCSI_CHECK)
    633        1.1   thorpej 			xs->error = XS_BUSY;
    634        1.1   thorpej 	}
    635        1.1   thorpej 
    636        1.1   thorpej  done:
    637        1.1   thorpej 	/* If IOC done with this requeset, free it up. */
    638        1.1   thorpej 	if (mpt_reply == NULL || (mpt_reply->MsgFlags & 0x80) == 0)
    639        1.1   thorpej 		mpt_free_request(mpt, req);
    640        1.1   thorpej 
    641        1.1   thorpej 	/* If address reply, give the buffer back to the IOC. */
    642        1.1   thorpej 	if (mpt_reply != NULL)
    643        1.1   thorpej 		mpt_free_reply(mpt, (reply << 1));
    644        1.6   thorpej 
    645        1.6   thorpej 	if (xs != NULL)
    646        1.6   thorpej 		scsipi_done(xs);
    647        1.1   thorpej }
    648        1.1   thorpej 
    649        1.1   thorpej static void
    650        1.1   thorpej mpt_run_xfer(mpt_softc_t *mpt, struct scsipi_xfer *xs)
    651        1.1   thorpej {
    652        1.1   thorpej 	struct scsipi_periph *periph = xs->xs_periph;
    653        1.1   thorpej 	request_t *req;
    654        1.1   thorpej 	MSG_SCSI_IO_REQUEST *mpt_req;
    655        1.1   thorpej 	int error, s;
    656        1.1   thorpej 
    657        1.1   thorpej 	s = splbio();
    658        1.1   thorpej 	req = mpt_get_request(mpt);
    659        1.1   thorpej 	if (__predict_false(req == NULL)) {
    660        1.1   thorpej 		/* This should happen very infrequently. */
    661        1.1   thorpej 		xs->error = XS_RESOURCE_SHORTAGE;
    662        1.1   thorpej 		scsipi_done(xs);
    663        1.1   thorpej 		splx(s);
    664        1.1   thorpej 		return;
    665        1.1   thorpej 	}
    666        1.1   thorpej 	splx(s);
    667        1.1   thorpej 
    668        1.1   thorpej 	/* Link the req and the scsipi_xfer. */
    669        1.1   thorpej 	req->xfer = xs;
    670        1.1   thorpej 
    671        1.1   thorpej 	/* Now we build the command for the IOC */
    672        1.1   thorpej 	mpt_req = req->req_vbuf;
    673        1.1   thorpej 	memset(mpt_req, 0, sizeof(*mpt_req));
    674        1.1   thorpej 
    675        1.1   thorpej 	mpt_req->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
    676        1.1   thorpej 	mpt_req->Bus = mpt->bus;
    677        1.1   thorpej 
    678        1.1   thorpej 	mpt_req->SenseBufferLength =
    679        1.1   thorpej 	    (sizeof(xs->sense.scsi_sense) < MPT_SENSE_SIZE) ?
    680        1.1   thorpej 	    sizeof(xs->sense.scsi_sense) : MPT_SENSE_SIZE;
    681        1.1   thorpej 
    682        1.1   thorpej 	/*
    683        1.1   thorpej 	 * We use the message context to find the request structure when
    684        1.1   thorpej 	 * we get the command completion interrupt from the IOC.
    685        1.1   thorpej 	 */
    686  1.14.14.1       snj 	mpt_req->MsgContext = htole32(req->index);
    687        1.1   thorpej 
    688        1.1   thorpej 	/* Which physical device to do the I/O on. */
    689        1.1   thorpej 	mpt_req->TargetID = periph->periph_target;
    690        1.1   thorpej 	mpt_req->LUN[1] = periph->periph_lun;
    691        1.1   thorpej 
    692        1.1   thorpej 	/* Set the direction of the transfer. */
    693        1.1   thorpej 	if (xs->xs_control & XS_CTL_DATA_IN)
    694        1.1   thorpej 		mpt_req->Control = MPI_SCSIIO_CONTROL_READ;
    695        1.1   thorpej 	else if (xs->xs_control & XS_CTL_DATA_OUT)
    696        1.1   thorpej 		mpt_req->Control = MPI_SCSIIO_CONTROL_WRITE;
    697        1.1   thorpej 	else
    698        1.1   thorpej 		mpt_req->Control = MPI_SCSIIO_CONTROL_NODATATRANSFER;
    699        1.1   thorpej 
    700        1.1   thorpej 	/* Set the queue behavior. */
    701       1.12      tron 	if (__predict_true((!mpt->is_scsi) ||
    702        1.1   thorpej 			   (mpt->mpt_tag_enable &
    703        1.1   thorpej 			    (1 << periph->periph_target)))) {
    704        1.1   thorpej 		switch (XS_CTL_TAGTYPE(xs)) {
    705        1.1   thorpej 		case XS_CTL_HEAD_TAG:
    706        1.1   thorpej 			mpt_req->Control |= MPI_SCSIIO_CONTROL_HEADOFQ;
    707        1.1   thorpej 			break;
    708        1.1   thorpej 
    709        1.1   thorpej #if 0	/* XXX */
    710        1.1   thorpej 		case XS_CTL_ACA_TAG:
    711        1.1   thorpej 			mpt_req->Control |= MPI_SCSIIO_CONTROL_ACAQ;
    712        1.1   thorpej 			break;
    713        1.1   thorpej #endif
    714        1.1   thorpej 
    715        1.1   thorpej 		case XS_CTL_ORDERED_TAG:
    716        1.1   thorpej 			mpt_req->Control |= MPI_SCSIIO_CONTROL_ORDEREDQ;
    717        1.1   thorpej 			break;
    718        1.1   thorpej 
    719        1.1   thorpej 		case XS_CTL_SIMPLE_TAG:
    720        1.1   thorpej 			mpt_req->Control |= MPI_SCSIIO_CONTROL_SIMPLEQ;
    721        1.1   thorpej 			break;
    722        1.1   thorpej 
    723        1.1   thorpej 		default:
    724       1.12      tron 			if (mpt->is_scsi)
    725       1.12      tron 				mpt_req->Control |= MPI_SCSIIO_CONTROL_UNTAGGED;
    726       1.12      tron 			else
    727        1.1   thorpej 				mpt_req->Control |= MPI_SCSIIO_CONTROL_SIMPLEQ;
    728        1.1   thorpej 			break;
    729        1.1   thorpej 		}
    730        1.1   thorpej 	} else
    731        1.1   thorpej 		mpt_req->Control |= MPI_SCSIIO_CONTROL_UNTAGGED;
    732        1.1   thorpej 
    733       1.12      tron 	if (__predict_false(mpt->is_scsi &&
    734        1.1   thorpej 			    (mpt->mpt_disc_enable &
    735        1.1   thorpej 			     (1 << periph->periph_target)) == 0))
    736        1.1   thorpej 		mpt_req->Control |= MPI_SCSIIO_CONTROL_NO_DISCONNECT;
    737        1.1   thorpej 
    738  1.14.14.1       snj 	mpt_req->Control = htole32(mpt_req->Control);
    739  1.14.14.1       snj 
    740        1.1   thorpej 	/* Copy the SCSI command block into place. */
    741        1.1   thorpej 	memcpy(mpt_req->CDB, xs->cmd, xs->cmdlen);
    742        1.1   thorpej 
    743        1.1   thorpej 	mpt_req->CDBLength = xs->cmdlen;
    744  1.14.14.1       snj 	mpt_req->DataLength = htole32(xs->datalen);
    745  1.14.14.1       snj 	mpt_req->SenseBufferLowAddr = htole32(req->sense_pbuf);
    746        1.1   thorpej 
    747        1.1   thorpej 	/*
    748        1.1   thorpej 	 * Map the DMA transfer.
    749        1.1   thorpej 	 */
    750        1.1   thorpej 	if (xs->datalen) {
    751        1.1   thorpej 		SGE_SIMPLE32 *se;
    752        1.1   thorpej 
    753        1.1   thorpej 		error = bus_dmamap_load(mpt->sc_dmat, req->dmap, xs->data,
    754        1.1   thorpej 		    xs->datalen, NULL,
    755        1.1   thorpej 		    ((xs->xs_control & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT
    756        1.1   thorpej 						       : BUS_DMA_WAITOK) |
    757        1.1   thorpej 		    BUS_DMA_STREAMING |
    758        1.1   thorpej 		    ((xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMA_READ
    759        1.1   thorpej 						       : BUS_DMA_WRITE));
    760        1.1   thorpej 		switch (error) {
    761        1.1   thorpej 		case 0:
    762        1.1   thorpej 			break;
    763        1.1   thorpej 
    764        1.1   thorpej 		case ENOMEM:
    765        1.1   thorpej 		case EAGAIN:
    766        1.1   thorpej 			xs->error = XS_RESOURCE_SHORTAGE;
    767        1.1   thorpej 			goto out_bad;
    768        1.1   thorpej 
    769        1.1   thorpej 		default:
    770        1.1   thorpej 			xs->error = XS_DRIVER_STUFFUP;
    771        1.1   thorpej 			mpt_prt(mpt, "error %d loading DMA map", error);
    772        1.1   thorpej  out_bad:
    773        1.1   thorpej 			s = splbio();
    774        1.1   thorpej 			mpt_free_request(mpt, req);
    775        1.1   thorpej 			scsipi_done(xs);
    776        1.1   thorpej 			splx(s);
    777        1.1   thorpej 			return;
    778        1.1   thorpej 		}
    779        1.1   thorpej 
    780        1.1   thorpej 		if (req->dmap->dm_nsegs > MPT_NSGL_FIRST(mpt)) {
    781        1.1   thorpej 			int seg, i, nleft = req->dmap->dm_nsegs;
    782        1.1   thorpej 			uint32_t flags;
    783        1.1   thorpej 			SGE_CHAIN32 *ce;
    784        1.1   thorpej 
    785        1.1   thorpej 			seg = 0;
    786        1.1   thorpej 			flags = MPI_SGE_FLAGS_SIMPLE_ELEMENT;
    787        1.1   thorpej 			if (xs->xs_control & XS_CTL_DATA_OUT)
    788        1.1   thorpej 				flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
    789        1.1   thorpej 
    790        1.1   thorpej 			se = (SGE_SIMPLE32 *) &mpt_req->SGL;
    791        1.1   thorpej 			for (i = 0; i < MPT_NSGL_FIRST(mpt) - 1;
    792        1.1   thorpej 			     i++, se++, seg++) {
    793        1.1   thorpej 				uint32_t tf;
    794        1.1   thorpej 
    795        1.1   thorpej 				memset(se, 0, sizeof(*se));
    796  1.14.14.1       snj 				se->Address =
    797  1.14.14.1       snj 				    htole32(req->dmap->dm_segs[seg].ds_addr);
    798        1.1   thorpej 				MPI_pSGE_SET_LENGTH(se,
    799        1.1   thorpej 				    req->dmap->dm_segs[seg].ds_len);
    800        1.1   thorpej 				tf = flags;
    801        1.1   thorpej 				if (i == MPT_NSGL_FIRST(mpt) - 2)
    802        1.1   thorpej 					tf |= MPI_SGE_FLAGS_LAST_ELEMENT;
    803        1.1   thorpej 				MPI_pSGE_SET_FLAGS(se, tf);
    804  1.14.14.1       snj 				se->FlagsLength = htole32(se->FlagsLength);
    805        1.1   thorpej 				nleft--;
    806        1.1   thorpej 			}
    807        1.1   thorpej 
    808        1.1   thorpej 			/*
    809        1.1   thorpej 			 * Tell the IOC where to find the first chain element.
    810        1.1   thorpej 			 */
    811        1.1   thorpej 			mpt_req->ChainOffset =
    812        1.1   thorpej 			    ((char *)se - (char *)mpt_req) >> 2;
    813        1.1   thorpej 
    814        1.1   thorpej 			/*
    815        1.1   thorpej 			 * Until we're finished with all segments...
    816        1.1   thorpej 			 */
    817        1.1   thorpej 			while (nleft) {
    818        1.1   thorpej 				int ntodo;
    819        1.1   thorpej 
    820        1.1   thorpej 				/*
    821        1.1   thorpej 				 * Construct the chain element that points to
    822        1.1   thorpej 				 * the next segment.
    823        1.1   thorpej 				 */
    824        1.1   thorpej 				ce = (SGE_CHAIN32 *) se++;
    825        1.1   thorpej 				if (nleft > MPT_NSGL(mpt)) {
    826        1.1   thorpej 					ntodo = MPT_NSGL(mpt) - 1;
    827        1.1   thorpej 					ce->NextChainOffset = (MPT_RQSL(mpt) -
    828        1.1   thorpej 					    sizeof(SGE_SIMPLE32)) >> 2;
    829  1.14.14.1       snj 					ce->Length = htole16(MPT_NSGL(mpt)
    830  1.14.14.1       snj 						* sizeof(SGE_SIMPLE32));
    831        1.1   thorpej 				} else {
    832        1.1   thorpej 					ntodo = nleft;
    833        1.1   thorpej 					ce->NextChainOffset = 0;
    834  1.14.14.1       snj 					ce->Length = htole16(ntodo
    835  1.14.14.1       snj 						* sizeof(SGE_SIMPLE32));
    836        1.1   thorpej 				}
    837  1.14.14.1       snj 				ce->Address = htole32(req->req_pbuf +
    838  1.14.14.1       snj 				    ((char *)se - (char *)mpt_req));
    839        1.1   thorpej 				ce->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT;
    840        1.1   thorpej 				for (i = 0; i < ntodo; i++, se++, seg++) {
    841        1.1   thorpej 					uint32_t tf;
    842        1.1   thorpej 
    843        1.1   thorpej 					memset(se, 0, sizeof(*se));
    844  1.14.14.1       snj 					se->Address = htole32(
    845  1.14.14.1       snj 					    req->dmap->dm_segs[seg].ds_addr);
    846        1.1   thorpej 					MPI_pSGE_SET_LENGTH(se,
    847        1.1   thorpej 					    req->dmap->dm_segs[seg].ds_len);
    848        1.1   thorpej 					tf = flags;
    849        1.1   thorpej 					if (i == ntodo - 1) {
    850        1.1   thorpej 						tf |=
    851        1.1   thorpej 						    MPI_SGE_FLAGS_LAST_ELEMENT;
    852        1.1   thorpej 						if (ce->NextChainOffset == 0) {
    853        1.1   thorpej 							tf |=
    854        1.1   thorpej 						    MPI_SGE_FLAGS_END_OF_LIST |
    855        1.1   thorpej 						    MPI_SGE_FLAGS_END_OF_BUFFER;
    856        1.1   thorpej 						}
    857        1.1   thorpej 					}
    858        1.1   thorpej 					MPI_pSGE_SET_FLAGS(se, tf);
    859  1.14.14.1       snj 					se->FlagsLength =
    860  1.14.14.1       snj 					    htole32(se->FlagsLength);
    861        1.1   thorpej 					nleft--;
    862        1.1   thorpej 				}
    863        1.1   thorpej 			}
    864        1.1   thorpej 			bus_dmamap_sync(mpt->sc_dmat, req->dmap, 0,
    865        1.1   thorpej 			    req->dmap->dm_mapsize,
    866        1.1   thorpej 			    (xs->xs_control & XS_CTL_DATA_IN) ?
    867        1.1   thorpej 			    				BUS_DMASYNC_PREREAD
    868        1.1   thorpej 						      : BUS_DMASYNC_PREWRITE);
    869        1.1   thorpej 		} else {
    870        1.1   thorpej 			int i;
    871        1.1   thorpej 			uint32_t flags;
    872        1.1   thorpej 
    873        1.1   thorpej 			flags = MPI_SGE_FLAGS_SIMPLE_ELEMENT;
    874        1.1   thorpej 			if (xs->xs_control & XS_CTL_DATA_OUT)
    875        1.1   thorpej 				flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
    876        1.1   thorpej 
    877        1.1   thorpej 			/* Copy the segments into our SG list. */
    878        1.1   thorpej 			se = (SGE_SIMPLE32 *) &mpt_req->SGL;
    879        1.1   thorpej 			for (i = 0; i < req->dmap->dm_nsegs;
    880        1.1   thorpej 			     i++, se++) {
    881        1.1   thorpej 				uint32_t tf;
    882        1.1   thorpej 
    883        1.1   thorpej 				memset(se, 0, sizeof(*se));
    884  1.14.14.1       snj 				se->Address =
    885  1.14.14.1       snj 				    htole32(req->dmap->dm_segs[i].ds_addr);
    886        1.1   thorpej 				MPI_pSGE_SET_LENGTH(se,
    887        1.1   thorpej 				    req->dmap->dm_segs[i].ds_len);
    888        1.1   thorpej 				tf = flags;
    889        1.1   thorpej 				if (i == req->dmap->dm_nsegs - 1) {
    890        1.1   thorpej 					tf |=
    891        1.1   thorpej 					    MPI_SGE_FLAGS_LAST_ELEMENT |
    892        1.1   thorpej 					    MPI_SGE_FLAGS_END_OF_BUFFER |
    893        1.1   thorpej 					    MPI_SGE_FLAGS_END_OF_LIST;
    894        1.1   thorpej 				}
    895        1.1   thorpej 				MPI_pSGE_SET_FLAGS(se, tf);
    896  1.14.14.1       snj 				se->FlagsLength = htole32(se->FlagsLength);
    897        1.1   thorpej 			}
    898        1.1   thorpej 			bus_dmamap_sync(mpt->sc_dmat, req->dmap, 0,
    899        1.1   thorpej 			    req->dmap->dm_mapsize,
    900        1.1   thorpej 			    (xs->xs_control & XS_CTL_DATA_IN) ?
    901        1.1   thorpej 			    				BUS_DMASYNC_PREREAD
    902        1.1   thorpej 						      : BUS_DMASYNC_PREWRITE);
    903        1.1   thorpej 		}
    904        1.1   thorpej 	} else {
    905        1.1   thorpej 		/*
    906        1.1   thorpej 		 * No data to transfer; just make a single simple SGL
    907        1.1   thorpej 		 * with zero length.
    908        1.1   thorpej 		 */
    909        1.1   thorpej 		SGE_SIMPLE32 *se = (SGE_SIMPLE32 *) &mpt_req->SGL;
    910        1.1   thorpej 		memset(se, 0, sizeof(*se));
    911        1.1   thorpej 		MPI_pSGE_SET_FLAGS(se,
    912        1.1   thorpej 		    (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
    913        1.1   thorpej 		     MPI_SGE_FLAGS_SIMPLE_ELEMENT | MPI_SGE_FLAGS_END_OF_LIST));
    914  1.14.14.1       snj 		se->FlagsLength = htole32(se->FlagsLength);
    915        1.1   thorpej 	}
    916        1.1   thorpej 
    917        1.1   thorpej 	if (mpt->verbose > 1)
    918        1.1   thorpej 		mpt_print_scsi_io_request(mpt_req);
    919        1.1   thorpej 
    920        1.1   thorpej 	s = splbio();
    921        1.1   thorpej 	if (__predict_true((xs->xs_control & XS_CTL_POLL) == 0))
    922        1.1   thorpej 		callout_reset(&xs->xs_callout,
    923        1.1   thorpej 		    mstohz(xs->timeout), mpt_timeout, req);
    924        1.1   thorpej 	mpt_send_cmd(mpt, req);
    925        1.1   thorpej 	splx(s);
    926        1.1   thorpej 
    927        1.1   thorpej 	if (__predict_true((xs->xs_control & XS_CTL_POLL) == 0))
    928        1.1   thorpej 		return;
    929        1.1   thorpej 
    930        1.1   thorpej 	/*
    931        1.1   thorpej 	 * If we can't use interrupts, poll on completion.
    932        1.1   thorpej 	 */
    933        1.4   thorpej 	if (mpt_poll(mpt, xs, xs->timeout))
    934        1.1   thorpej 		mpt_timeout(req);
    935        1.1   thorpej }
    936        1.1   thorpej 
    937        1.1   thorpej static void
    938        1.1   thorpej mpt_set_xfer_mode(mpt_softc_t *mpt, struct scsipi_xfer_mode *xm)
    939        1.1   thorpej {
    940        1.1   thorpej 	fCONFIG_PAGE_SCSI_DEVICE_1 tmp;
    941        1.1   thorpej 
    942        1.1   thorpej 	/*
    943        1.1   thorpej 	 * Always allow disconnect; we don't have a way to disable
    944        1.1   thorpej 	 * it right now, in any case.
    945        1.1   thorpej 	 */
    946        1.1   thorpej 	mpt->mpt_disc_enable |= (1 << xm->xm_target);
    947        1.1   thorpej 
    948        1.1   thorpej 	if (xm->xm_mode & PERIPH_CAP_TQING)
    949        1.1   thorpej 		mpt->mpt_tag_enable |= (1 << xm->xm_target);
    950        1.1   thorpej 	else
    951        1.1   thorpej 		mpt->mpt_tag_enable &= ~(1 << xm->xm_target);
    952        1.1   thorpej 
    953  1.14.14.2    bouyer 	if (mpt->is_scsi) {
    954  1.14.14.2    bouyer 		/*
    955  1.14.14.2    bouyer 		 * SCSI transport settings only make any sense for
    956  1.14.14.2    bouyer 		 * SCSI
    957  1.14.14.2    bouyer 		 */
    958        1.1   thorpej 
    959  1.14.14.2    bouyer 		tmp = mpt->mpt_dev_page1[xm->xm_target];
    960        1.1   thorpej 
    961  1.14.14.2    bouyer 		/*
    962  1.14.14.2    bouyer 		 * Set the wide/narrow parameter for the target.
    963  1.14.14.2    bouyer 		 */
    964  1.14.14.2    bouyer 		if (xm->xm_mode & PERIPH_CAP_WIDE16)
    965  1.14.14.2    bouyer 			tmp.RequestedParameters |= MPI_SCSIDEVPAGE1_RP_WIDE;
    966  1.14.14.2    bouyer 		else
    967  1.14.14.2    bouyer 			tmp.RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_WIDE;
    968  1.14.14.2    bouyer 
    969  1.14.14.2    bouyer 		/*
    970  1.14.14.2    bouyer 		 * Set the synchronous parameters for the target.
    971  1.14.14.2    bouyer 		 *
    972  1.14.14.2    bouyer 		 * XXX If we request sync transfers, we just go ahead and
    973  1.14.14.2    bouyer 		 * XXX request the maximum available.  We need finer control
    974  1.14.14.2    bouyer 		 * XXX in order to implement Domain Validation.
    975  1.14.14.2    bouyer 		 */
    976  1.14.14.2    bouyer 		tmp.RequestedParameters &= ~(MPI_SCSIDEVPAGE1_RP_MIN_SYNC_PERIOD_MASK |
    977  1.14.14.2    bouyer 		    MPI_SCSIDEVPAGE1_RP_MAX_SYNC_OFFSET_MASK |
    978  1.14.14.2    bouyer 		    MPI_SCSIDEVPAGE1_RP_DT | MPI_SCSIDEVPAGE1_RP_QAS |
    979  1.14.14.2    bouyer 		    MPI_SCSIDEVPAGE1_RP_IU);
    980  1.14.14.2    bouyer 		if (xm->xm_mode & PERIPH_CAP_SYNC) {
    981  1.14.14.2    bouyer 			int factor, offset, np;
    982  1.14.14.2    bouyer 
    983  1.14.14.2    bouyer 			factor = (mpt->mpt_port_page0.Capabilities >> 8) & 0xff;
    984  1.14.14.2    bouyer 			offset = (mpt->mpt_port_page0.Capabilities >> 16) & 0xff;
    985  1.14.14.2    bouyer 			np = 0;
    986  1.14.14.2    bouyer 			if (factor < 0x9) {
    987  1.14.14.2    bouyer 				/* Ultra320 */
    988  1.14.14.2    bouyer 				np |= MPI_SCSIDEVPAGE1_RP_QAS | MPI_SCSIDEVPAGE1_RP_IU;
    989  1.14.14.2    bouyer 			}
    990  1.14.14.2    bouyer 			if (factor < 0xa) {
    991  1.14.14.2    bouyer 				/* at least Ultra160 */
    992  1.14.14.2    bouyer 				np |= MPI_SCSIDEVPAGE1_RP_DT;
    993  1.14.14.2    bouyer 			}
    994  1.14.14.2    bouyer 			np |= (factor << 8) | (offset << 16);
    995  1.14.14.2    bouyer 			tmp.RequestedParameters |= np;
    996        1.1   thorpej 		}
    997        1.1   thorpej 
    998  1.14.14.2    bouyer 		host2mpt_config_page_scsi_device_1(&tmp);
    999  1.14.14.2    bouyer 		if (mpt_write_cfg_page(mpt, xm->xm_target, &tmp.Header)) {
   1000  1.14.14.2    bouyer 			mpt_prt(mpt, "unable to write Device Page 1");
   1001  1.14.14.2    bouyer 			return;
   1002  1.14.14.2    bouyer 		}
   1003        1.1   thorpej 
   1004  1.14.14.2    bouyer 		if (mpt_read_cfg_page(mpt, xm->xm_target, &tmp.Header)) {
   1005  1.14.14.2    bouyer 			mpt_prt(mpt, "unable to read back Device Page 1");
   1006  1.14.14.2    bouyer 			return;
   1007  1.14.14.2    bouyer 		}
   1008        1.1   thorpej 
   1009  1.14.14.2    bouyer 		mpt2host_config_page_scsi_device_1(&tmp);
   1010  1.14.14.2    bouyer 		mpt->mpt_dev_page1[xm->xm_target] = tmp;
   1011  1.14.14.2    bouyer 		if (mpt->verbose > 1) {
   1012  1.14.14.2    bouyer 			mpt_prt(mpt,
   1013  1.14.14.2    bouyer 			    "SPI Target %d Page 1: RequestedParameters %x Config %x",
   1014  1.14.14.2    bouyer 			    xm->xm_target,
   1015  1.14.14.2    bouyer 			    mpt->mpt_dev_page1[xm->xm_target].RequestedParameters,
   1016  1.14.14.2    bouyer 			    mpt->mpt_dev_page1[xm->xm_target].Configuration);
   1017  1.14.14.2    bouyer 		}
   1018        1.1   thorpej 	}
   1019        1.1   thorpej 
   1020        1.1   thorpej 	/*
   1021        1.1   thorpej 	 * Make a note that we should perform an async callback at the
   1022        1.1   thorpej 	 * end of the next successful command completion to report the
   1023        1.1   thorpej 	 * negotiated transfer mode.
   1024        1.1   thorpej 	 */
   1025        1.1   thorpej 	mpt->mpt_report_xfer_mode |= (1 << xm->xm_target);
   1026        1.1   thorpej }
   1027        1.1   thorpej 
   1028        1.1   thorpej static void
   1029        1.1   thorpej mpt_get_xfer_mode(mpt_softc_t *mpt, struct scsipi_periph *periph)
   1030        1.1   thorpej {
   1031        1.1   thorpej 	fCONFIG_PAGE_SCSI_DEVICE_0 tmp;
   1032        1.1   thorpej 	struct scsipi_xfer_mode xm;
   1033        1.1   thorpej 	int period, offset;
   1034        1.1   thorpej 
   1035        1.1   thorpej 	tmp = mpt->mpt_dev_page0[periph->periph_target];
   1036  1.14.14.1       snj 	host2mpt_config_page_scsi_device_0(&tmp);
   1037        1.1   thorpej 	if (mpt_read_cfg_page(mpt, periph->periph_target, &tmp.Header)) {
   1038        1.1   thorpej 		mpt_prt(mpt, "unable to read Device Page 0");
   1039        1.1   thorpej 		return;
   1040        1.1   thorpej 	}
   1041  1.14.14.1       snj 	mpt2host_config_page_scsi_device_0(&tmp);
   1042        1.1   thorpej 
   1043        1.1   thorpej 	if (mpt->verbose > 1) {
   1044        1.1   thorpej 		mpt_prt(mpt,
   1045        1.1   thorpej 		    "SPI Tgt %d Page 0: NParms %x Information %x",
   1046        1.1   thorpej 		    periph->periph_target,
   1047        1.1   thorpej 		    tmp.NegotiatedParameters, tmp.Information);
   1048        1.1   thorpej 	}
   1049        1.1   thorpej 
   1050        1.1   thorpej 	xm.xm_target = periph->periph_target;
   1051        1.1   thorpej 	xm.xm_mode = 0;
   1052        1.1   thorpej 
   1053        1.1   thorpej 	if (tmp.NegotiatedParameters & MPI_SCSIDEVPAGE0_NP_WIDE)
   1054        1.1   thorpej 		xm.xm_mode |= PERIPH_CAP_WIDE16;
   1055        1.1   thorpej 
   1056        1.1   thorpej 	period = (tmp.NegotiatedParameters >> 8) & 0xff;
   1057        1.1   thorpej 	offset = (tmp.NegotiatedParameters >> 16) & 0xff;
   1058        1.1   thorpej 	if (offset) {
   1059        1.1   thorpej 		xm.xm_period = period;
   1060        1.1   thorpej 		xm.xm_offset = offset;
   1061        1.1   thorpej 		xm.xm_mode |= PERIPH_CAP_SYNC;
   1062        1.1   thorpej 	}
   1063        1.1   thorpej 
   1064        1.1   thorpej 	/*
   1065        1.1   thorpej 	 * Tagged queueing is all controlled by us; there is no
   1066        1.1   thorpej 	 * other setting to query.
   1067        1.1   thorpej 	 */
   1068        1.1   thorpej 	if (mpt->mpt_tag_enable & (1 << periph->periph_target))
   1069        1.1   thorpej 		xm.xm_mode |= PERIPH_CAP_TQING;
   1070        1.1   thorpej 
   1071        1.1   thorpej 	/*
   1072        1.1   thorpej 	 * We're going to deliver the async event, so clear the marker.
   1073        1.1   thorpej 	 */
   1074        1.1   thorpej 	mpt->mpt_report_xfer_mode &= ~(1 << periph->periph_target);
   1075        1.1   thorpej 
   1076        1.1   thorpej 	scsipi_async_event(&mpt->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
   1077        1.1   thorpej }
   1078        1.1   thorpej 
   1079        1.1   thorpej static void
   1080        1.1   thorpej mpt_ctlop(mpt_softc_t *mpt, void *vmsg, uint32_t reply)
   1081        1.1   thorpej {
   1082        1.1   thorpej 	MSG_DEFAULT_REPLY *dmsg = vmsg;
   1083        1.1   thorpej 
   1084        1.1   thorpej 	switch (dmsg->Function) {
   1085        1.1   thorpej 	case MPI_FUNCTION_EVENT_NOTIFICATION:
   1086        1.1   thorpej 		mpt_event_notify_reply(mpt, vmsg);
   1087        1.1   thorpej 		mpt_free_reply(mpt, (reply << 1));
   1088        1.1   thorpej 		break;
   1089        1.1   thorpej 
   1090        1.1   thorpej 	case MPI_FUNCTION_EVENT_ACK:
   1091        1.1   thorpej 		mpt_free_reply(mpt, (reply << 1));
   1092        1.1   thorpej 		break;
   1093        1.1   thorpej 
   1094        1.1   thorpej 	case MPI_FUNCTION_PORT_ENABLE:
   1095        1.1   thorpej 	    {
   1096        1.1   thorpej 		MSG_PORT_ENABLE_REPLY *msg = vmsg;
   1097  1.14.14.1       snj 		int index = le32toh(msg->MsgContext) & ~0x80000000;
   1098        1.1   thorpej 		if (mpt->verbose > 1)
   1099        1.1   thorpej 			mpt_prt(mpt, "enable port reply index %d", index);
   1100        1.1   thorpej 		if (index >= 0 && index < MPT_MAX_REQUESTS(mpt)) {
   1101        1.1   thorpej 			request_t *req = &mpt->request_pool[index];
   1102        1.1   thorpej 			req->debug = REQ_DONE;
   1103        1.1   thorpej 		}
   1104        1.1   thorpej 		mpt_free_reply(mpt, (reply << 1));
   1105        1.1   thorpej 		break;
   1106        1.1   thorpej 	    }
   1107        1.1   thorpej 
   1108        1.1   thorpej 	case MPI_FUNCTION_CONFIG:
   1109        1.1   thorpej 	    {
   1110        1.1   thorpej 		MSG_CONFIG_REPLY *msg = vmsg;
   1111  1.14.14.1       snj 		int index = le32toh(msg->MsgContext) & ~0x80000000;
   1112        1.1   thorpej 		if (index >= 0 && index < MPT_MAX_REQUESTS(mpt)) {
   1113        1.1   thorpej 			request_t *req = &mpt->request_pool[index];
   1114        1.1   thorpej 			req->debug = REQ_DONE;
   1115        1.1   thorpej 			req->sequence = reply;
   1116        1.1   thorpej 		} else
   1117        1.1   thorpej 			mpt_free_reply(mpt, (reply << 1));
   1118        1.1   thorpej 		break;
   1119        1.1   thorpej 	    }
   1120        1.1   thorpej 
   1121        1.1   thorpej 	default:
   1122        1.1   thorpej 		mpt_prt(mpt, "unknown ctlop: 0x%x", dmsg->Function);
   1123        1.1   thorpej 	}
   1124        1.1   thorpej }
   1125        1.1   thorpej 
   1126        1.1   thorpej static void
   1127        1.1   thorpej mpt_event_notify_reply(mpt_softc_t *mpt, MSG_EVENT_NOTIFY_REPLY *msg)
   1128        1.1   thorpej {
   1129        1.1   thorpej 
   1130  1.14.14.1       snj 	switch (le32toh(msg->Event)) {
   1131        1.1   thorpej 	case MPI_EVENT_LOG_DATA:
   1132        1.1   thorpej 	    {
   1133        1.1   thorpej 		int i;
   1134        1.1   thorpej 
   1135        1.1   thorpej 		/* Some error occurrerd that the Fusion wants logged. */
   1136        1.1   thorpej 		mpt_prt(mpt, "EvtLogData: IOCLogInfo: 0x%08x", msg->IOCLogInfo);
   1137        1.1   thorpej 		mpt_prt(mpt, "EvtLogData: Event Data:");
   1138        1.1   thorpej 		for (i = 0; i < msg->EventDataLength; i++) {
   1139        1.1   thorpej 			if ((i % 4) == 0)
   1140       1.14    cegger 				printf("%s:\t", device_xname(&mpt->sc_dev));
   1141        1.1   thorpej 			printf("0x%08x%c", msg->Data[i],
   1142        1.1   thorpej 			    ((i % 4) == 3) ? '\n' : ' ');
   1143        1.1   thorpej 		}
   1144        1.1   thorpej 		if ((i % 4) != 0)
   1145        1.1   thorpej 			printf("\n");
   1146        1.1   thorpej 		break;
   1147        1.1   thorpej 	    }
   1148        1.1   thorpej 
   1149        1.1   thorpej 	case MPI_EVENT_UNIT_ATTENTION:
   1150        1.1   thorpej 		mpt_prt(mpt, "Unit Attn: Bus 0x%02x Target 0x%02x",
   1151        1.1   thorpej 		    (msg->Data[0] >> 8) & 0xff, msg->Data[0] & 0xff);
   1152        1.1   thorpej 		break;
   1153        1.1   thorpej 
   1154        1.1   thorpej 	case MPI_EVENT_IOC_BUS_RESET:
   1155        1.1   thorpej 		/* We generated a bus reset. */
   1156        1.1   thorpej 		mpt_prt(mpt, "IOC Bus Reset Port %d",
   1157        1.1   thorpej 		    (msg->Data[0] >> 8) & 0xff);
   1158        1.1   thorpej 		break;
   1159        1.1   thorpej 
   1160        1.1   thorpej 	case MPI_EVENT_EXT_BUS_RESET:
   1161        1.1   thorpej 		/* Someone else generated a bus reset. */
   1162        1.1   thorpej 		mpt_prt(mpt, "External Bus Reset");
   1163        1.1   thorpej 		/*
   1164        1.1   thorpej 		 * These replies don't return EventData like the MPI
   1165        1.1   thorpej 		 * spec says they do.
   1166        1.1   thorpej 		 */
   1167        1.1   thorpej 		/* XXX Send an async event? */
   1168        1.1   thorpej 		break;
   1169        1.1   thorpej 
   1170        1.1   thorpej 	case MPI_EVENT_RESCAN:
   1171        1.1   thorpej 		/*
   1172        1.1   thorpej 		 * In general, thise means a device has been added
   1173        1.1   thorpej 		 * to the loop.
   1174        1.1   thorpej 		 */
   1175        1.1   thorpej 		mpt_prt(mpt, "Rescan Port %d", (msg->Data[0] >> 8) & 0xff);
   1176        1.1   thorpej 		/* XXX Send an async event? */
   1177        1.1   thorpej 		break;
   1178        1.1   thorpej 
   1179        1.1   thorpej 	case MPI_EVENT_LINK_STATUS_CHANGE:
   1180        1.1   thorpej 		mpt_prt(mpt, "Port %d: Link state %s",
   1181        1.1   thorpej 		    (msg->Data[1] >> 8) & 0xff,
   1182        1.1   thorpej 		    (msg->Data[0] & 0xff) == 0 ? "Failed" : "Active");
   1183        1.1   thorpej 		break;
   1184        1.1   thorpej 
   1185        1.1   thorpej 	case MPI_EVENT_LOOP_STATE_CHANGE:
   1186        1.1   thorpej 		switch ((msg->Data[0] >> 16) & 0xff) {
   1187        1.1   thorpej 		case 0x01:
   1188        1.1   thorpej 			mpt_prt(mpt,
   1189        1.1   thorpej 			    "Port %d: FC Link Event: LIP(%02x,%02x) "
   1190        1.1   thorpej 			    "(Loop Initialization)",
   1191        1.1   thorpej 			    (msg->Data[1] >> 8) & 0xff,
   1192        1.1   thorpej 			    (msg->Data[0] >> 8) & 0xff,
   1193        1.1   thorpej 			    (msg->Data[0]     ) & 0xff);
   1194        1.1   thorpej 			switch ((msg->Data[0] >> 8) & 0xff) {
   1195        1.1   thorpej 			case 0xf7:
   1196        1.1   thorpej 				if ((msg->Data[0] & 0xff) == 0xf7)
   1197        1.1   thorpej 					mpt_prt(mpt, "\tDevice needs AL_PA");
   1198        1.1   thorpej 				else
   1199        1.1   thorpej 					mpt_prt(mpt, "\tDevice %02x doesn't "
   1200        1.1   thorpej 					    "like FC performance",
   1201        1.1   thorpej 					    msg->Data[0] & 0xff);
   1202        1.1   thorpej 				break;
   1203        1.1   thorpej 
   1204        1.1   thorpej 			case 0xf8:
   1205        1.1   thorpej 				if ((msg->Data[0] & 0xff) == 0xf7)
   1206        1.1   thorpej 					mpt_prt(mpt, "\tDevice detected loop "
   1207        1.1   thorpej 					    "failure before acquiring AL_PA");
   1208        1.1   thorpej 				else
   1209        1.1   thorpej 					mpt_prt(mpt, "\tDevice %02x detected "
   1210        1.1   thorpej 					    "loop failure",
   1211        1.1   thorpej 					    msg->Data[0] & 0xff);
   1212        1.1   thorpej 				break;
   1213        1.1   thorpej 
   1214        1.1   thorpej 			default:
   1215        1.1   thorpej 				mpt_prt(mpt, "\tDevice %02x requests that "
   1216        1.1   thorpej 				    "device %02x reset itself",
   1217        1.1   thorpej 				    msg->Data[0] & 0xff,
   1218        1.1   thorpej 				    (msg->Data[0] >> 8) & 0xff);
   1219        1.1   thorpej 				break;
   1220        1.1   thorpej 			}
   1221        1.1   thorpej 			break;
   1222        1.1   thorpej 
   1223        1.1   thorpej 		case 0x02:
   1224        1.1   thorpej 			mpt_prt(mpt, "Port %d: FC Link Event: LPE(%02x,%02x) "
   1225        1.1   thorpej 			    "(Loop Port Enable)",
   1226        1.1   thorpej 			    (msg->Data[1] >> 8) & 0xff,
   1227        1.1   thorpej 			    (msg->Data[0] >> 8) & 0xff,
   1228        1.1   thorpej 			    (msg->Data[0]     ) & 0xff);
   1229        1.1   thorpej 			break;
   1230        1.1   thorpej 
   1231        1.1   thorpej 		case 0x03:
   1232        1.1   thorpej 			mpt_prt(mpt, "Port %d: FC Link Event: LPB(%02x,%02x) "
   1233        1.1   thorpej 			    "(Loop Port Bypass)",
   1234        1.1   thorpej 			    (msg->Data[1] >> 8) & 0xff,
   1235        1.1   thorpej 			    (msg->Data[0] >> 8) & 0xff,
   1236        1.1   thorpej 			    (msg->Data[0]     ) & 0xff);
   1237        1.1   thorpej 			break;
   1238        1.1   thorpej 
   1239        1.1   thorpej 		default:
   1240        1.1   thorpej 			mpt_prt(mpt, "Port %d: FC Link Event: "
   1241        1.1   thorpej 			    "Unknown event (%02x %02x %02x)",
   1242        1.1   thorpej 			    (msg->Data[1] >>  8) & 0xff,
   1243        1.1   thorpej 			    (msg->Data[0] >> 16) & 0xff,
   1244        1.1   thorpej 			    (msg->Data[0] >>  8) & 0xff,
   1245        1.1   thorpej 			    (msg->Data[0]      ) & 0xff);
   1246        1.1   thorpej 			break;
   1247        1.1   thorpej 		}
   1248        1.1   thorpej 		break;
   1249        1.1   thorpej 
   1250        1.1   thorpej 	case MPI_EVENT_LOGOUT:
   1251        1.1   thorpej 		mpt_prt(mpt, "Port %d: FC Logout: N_PortID: %02x",
   1252        1.1   thorpej 		    (msg->Data[1] >> 8) & 0xff, msg->Data[0]);
   1253        1.1   thorpej 		break;
   1254        1.1   thorpej 
   1255        1.1   thorpej 	case MPI_EVENT_EVENT_CHANGE:
   1256        1.1   thorpej 		/*
   1257        1.1   thorpej 		 * This is just an acknowledgement of our
   1258        1.1   thorpej 		 * mpt_send_event_request().
   1259        1.1   thorpej 		 */
   1260        1.1   thorpej 		break;
   1261        1.1   thorpej 
   1262       1.12      tron 	case MPI_EVENT_SAS_PHY_LINK_STATUS:
   1263       1.13      tron 		switch ((msg->Data[0] >> 12) & 0x0f) {
   1264       1.12      tron 		case 0x00:
   1265       1.12      tron 			mpt_prt(mpt, "Phy %d: Link Status Unknown",
   1266       1.12      tron 			    msg->Data[0] & 0xff);
   1267       1.12      tron 			break;
   1268       1.12      tron 		case 0x01:
   1269       1.12      tron 			mpt_prt(mpt, "Phy %d: Link Disabled",
   1270       1.12      tron 			    msg->Data[0] & 0xff);
   1271       1.12      tron 			break;
   1272       1.12      tron 		case 0x02:
   1273       1.12      tron 			mpt_prt(mpt, "Phy %d: Failed Speed Negotiation",
   1274       1.12      tron 			    msg->Data[0] & 0xff);
   1275       1.12      tron 			break;
   1276       1.12      tron 		case 0x03:
   1277       1.12      tron 			mpt_prt(mpt, "Phy %d: SATA OOB Complete",
   1278       1.12      tron 			    msg->Data[0] & 0xff);
   1279       1.12      tron 			break;
   1280       1.12      tron 		case 0x08:
   1281       1.12      tron 			mpt_prt(mpt, "Phy %d: Link Rate 1.5 Gbps",
   1282       1.12      tron 			    msg->Data[0] & 0xff);
   1283       1.12      tron 			break;
   1284       1.12      tron 		case 0x09:
   1285       1.12      tron 			mpt_prt(mpt, "Phy %d: Link Rate 3.0 Gbps",
   1286       1.12      tron 			    msg->Data[0] & 0xff);
   1287       1.12      tron 			break;
   1288       1.13      tron 		default:
   1289       1.12      tron 			mpt_prt(mpt, "Phy %d: SAS Phy Link Status Event: "
   1290       1.12      tron 			    "Unknown event (%0x)",
   1291       1.12      tron 			    msg->Data[0] & 0xff, (msg->Data[0] >> 8) & 0xff);
   1292       1.12      tron 		}
   1293       1.12      tron 		break;
   1294       1.12      tron 
   1295       1.12      tron 	case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
   1296       1.12      tron 	case MPI_EVENT_SAS_DISCOVERY:
   1297       1.12      tron 		/* ignore these events for now */
   1298       1.12      tron 		break;
   1299       1.12      tron 
   1300  1.14.14.2    bouyer 	case MPI_EVENT_QUEUE_FULL:
   1301  1.14.14.2    bouyer 		/* This can get a little chatty */
   1302  1.14.14.2    bouyer 		if (mpt->verbose > 0)
   1303  1.14.14.2    bouyer 			mpt_prt(mpt, "Queue Full Event");
   1304  1.14.14.2    bouyer 		break;
   1305  1.14.14.2    bouyer 
   1306        1.1   thorpej 	default:
   1307        1.1   thorpej 		mpt_prt(mpt, "Unknown async event: 0x%x", msg->Event);
   1308        1.1   thorpej 		break;
   1309        1.1   thorpej 	}
   1310        1.9     perry 
   1311        1.1   thorpej 	if (msg->AckRequired) {
   1312        1.1   thorpej 		MSG_EVENT_ACK *ackp;
   1313        1.1   thorpej 		request_t *req;
   1314        1.1   thorpej 
   1315        1.1   thorpej 		if ((req = mpt_get_request(mpt)) == NULL) {
   1316        1.1   thorpej 			/* XXX XXX XXX XXXJRT */
   1317        1.1   thorpej 			panic("mpt_event_notify_reply: unable to allocate "
   1318        1.1   thorpej 			    "request structure");
   1319        1.1   thorpej 		}
   1320        1.1   thorpej 
   1321        1.1   thorpej 		ackp = (MSG_EVENT_ACK *) req->req_vbuf;
   1322        1.1   thorpej 		memset(ackp, 0, sizeof(*ackp));
   1323        1.1   thorpej 		ackp->Function = MPI_FUNCTION_EVENT_ACK;
   1324        1.1   thorpej 		ackp->Event = msg->Event;
   1325        1.1   thorpej 		ackp->EventContext = msg->EventContext;
   1326  1.14.14.1       snj 		ackp->MsgContext = htole32(req->index | 0x80000000);
   1327        1.1   thorpej 		mpt_check_doorbell(mpt);
   1328        1.1   thorpej 		mpt_send_cmd(mpt, req);
   1329        1.1   thorpej 	}
   1330        1.1   thorpej }
   1331        1.1   thorpej 
   1332        1.1   thorpej /* XXXJRT mpt_bus_reset() */
   1333        1.1   thorpej 
   1334        1.1   thorpej /*****************************************************************************
   1335        1.1   thorpej  * SCSI interface routines
   1336        1.1   thorpej  *****************************************************************************/
   1337        1.1   thorpej 
   1338        1.1   thorpej static void
   1339        1.1   thorpej mpt_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
   1340        1.1   thorpej     void *arg)
   1341        1.1   thorpej {
   1342        1.1   thorpej 	struct scsipi_adapter *adapt = chan->chan_adapter;
   1343        1.1   thorpej 	mpt_softc_t *mpt = (void *) adapt->adapt_dev;
   1344        1.1   thorpej 
   1345        1.1   thorpej 	switch (req) {
   1346        1.1   thorpej 	case ADAPTER_REQ_RUN_XFER:
   1347        1.1   thorpej 		mpt_run_xfer(mpt, (struct scsipi_xfer *) arg);
   1348        1.1   thorpej 		return;
   1349        1.1   thorpej 
   1350        1.1   thorpej 	case ADAPTER_REQ_GROW_RESOURCES:
   1351        1.1   thorpej 		/* Not supported. */
   1352        1.1   thorpej 		return;
   1353        1.1   thorpej 
   1354        1.1   thorpej 	case ADAPTER_REQ_SET_XFER_MODE:
   1355        1.1   thorpej 		mpt_set_xfer_mode(mpt, (struct scsipi_xfer_mode *) arg);
   1356        1.1   thorpej 		return;
   1357        1.1   thorpej 	}
   1358        1.1   thorpej }
   1359        1.1   thorpej 
   1360        1.1   thorpej static void
   1361        1.1   thorpej mpt_minphys(struct buf *bp)
   1362        1.1   thorpej {
   1363        1.1   thorpej 
   1364        1.1   thorpej /*
   1365        1.1   thorpej  * Subtract one from the SGL limit, since we need an extra one to handle
   1366        1.1   thorpej  * an non-page-aligned transfer.
   1367        1.1   thorpej  */
   1368        1.1   thorpej #define	MPT_MAX_XFER	((MPT_SGL_MAX - 1) * PAGE_SIZE)
   1369        1.1   thorpej 
   1370        1.1   thorpej 	if (bp->b_bcount > MPT_MAX_XFER)
   1371        1.1   thorpej 		bp->b_bcount = MPT_MAX_XFER;
   1372        1.1   thorpej 	minphys(bp);
   1373        1.1   thorpej }
   1374