Home | History | Annotate | Line # | Download | only in ic
mpt_netbsd.c revision 1.16.2.3
      1  1.16.2.3      yamt /*	$NetBSD: mpt_netbsd.c,v 1.16.2.3 2014/05/22 11:40:22 yamt 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.16.2.3      yamt __KERNEL_RCSID(0, "$NetBSD: mpt_netbsd.c,v 1.16.2.3 2014/05/22 11:40:22 yamt Exp $");
     81       1.1   thorpej 
     82       1.1   thorpej #include <dev/ic/mpt.h>			/* pulls in all headers */
     83  1.16.2.3      yamt #include <sys/scsiio.h>
     84       1.1   thorpej 
     85       1.1   thorpej static int	mpt_poll(mpt_softc_t *, struct scsipi_xfer *, int);
     86       1.1   thorpej static void	mpt_timeout(void *);
     87  1.16.2.3      yamt static void	mpt_restart(mpt_softc_t *, request_t *);
     88       1.1   thorpej static void	mpt_done(mpt_softc_t *, uint32_t);
     89  1.16.2.3      yamt static int	mpt_drain_queue(mpt_softc_t *);
     90       1.1   thorpej static void	mpt_run_xfer(mpt_softc_t *, struct scsipi_xfer *);
     91       1.1   thorpej static void	mpt_set_xfer_mode(mpt_softc_t *, struct scsipi_xfer_mode *);
     92       1.1   thorpej static void	mpt_get_xfer_mode(mpt_softc_t *, struct scsipi_periph *);
     93       1.1   thorpej static void	mpt_ctlop(mpt_softc_t *, void *vmsg, uint32_t);
     94       1.1   thorpej static void	mpt_event_notify_reply(mpt_softc_t *, MSG_EVENT_NOTIFY_REPLY *);
     95  1.16.2.3      yamt static void  mpt_bus_reset(mpt_softc_t *);
     96       1.1   thorpej 
     97       1.1   thorpej static void	mpt_scsipi_request(struct scsipi_channel *,
     98       1.1   thorpej 		    scsipi_adapter_req_t, void *);
     99       1.1   thorpej static void	mpt_minphys(struct buf *);
    100  1.16.2.3      yamt static int 	mpt_ioctl(struct scsipi_channel *, u_long, void *, int,
    101  1.16.2.3      yamt 	struct proc *);
    102       1.1   thorpej 
    103  1.16.2.1      yamt /*
    104  1.16.2.1      yamt  * XXX - this assumes the device_private() of the attachement starts with
    105  1.16.2.1      yamt  * a struct mpt_softc, so we can use the return value of device_private()
    106  1.16.2.1      yamt  * straight without any offset.
    107  1.16.2.1      yamt  */
    108  1.16.2.1      yamt #define DEV_TO_MPT(DEV)	device_private(DEV)
    109  1.16.2.1      yamt 
    110       1.1   thorpej void
    111       1.1   thorpej mpt_scsipi_attach(mpt_softc_t *mpt)
    112       1.1   thorpej {
    113       1.1   thorpej 	struct scsipi_adapter *adapt = &mpt->sc_adapter;
    114       1.1   thorpej 	struct scsipi_channel *chan = &mpt->sc_channel;
    115       1.1   thorpej 	int maxq;
    116       1.1   thorpej 
    117       1.1   thorpej 	mpt->bus = 0;		/* XXX ?? */
    118       1.1   thorpej 
    119       1.1   thorpej 	maxq = (mpt->mpt_global_credits < MPT_MAX_REQUESTS(mpt)) ?
    120       1.1   thorpej 	    mpt->mpt_global_credits : MPT_MAX_REQUESTS(mpt);
    121       1.1   thorpej 
    122       1.1   thorpej 	/* Fill in the scsipi_adapter. */
    123       1.1   thorpej 	memset(adapt, 0, sizeof(*adapt));
    124  1.16.2.1      yamt 	adapt->adapt_dev = mpt->sc_dev;
    125       1.1   thorpej 	adapt->adapt_nchannels = 1;
    126  1.16.2.1      yamt 	adapt->adapt_openings = maxq - 2;	/* Reserve 2 for driver use*/
    127  1.16.2.1      yamt 	adapt->adapt_max_periph = maxq - 2;
    128       1.1   thorpej 	adapt->adapt_request = mpt_scsipi_request;
    129       1.1   thorpej 	adapt->adapt_minphys = mpt_minphys;
    130  1.16.2.3      yamt 	adapt->adapt_ioctl = mpt_ioctl;
    131       1.1   thorpej 
    132       1.1   thorpej 	/* Fill in the scsipi_channel. */
    133       1.1   thorpej 	memset(chan, 0, sizeof(*chan));
    134       1.1   thorpej 	chan->chan_adapter = adapt;
    135  1.16.2.2      yamt 	if (mpt->is_sas) {
    136  1.16.2.2      yamt 		chan->chan_bustype = &scsi_sas_bustype;
    137  1.16.2.2      yamt 	} else if (mpt->is_fc) {
    138  1.16.2.2      yamt 		chan->chan_bustype = &scsi_fc_bustype;
    139  1.16.2.2      yamt 	} else {
    140  1.16.2.2      yamt 		chan->chan_bustype = &scsi_bustype;
    141  1.16.2.2      yamt 	}
    142       1.1   thorpej 	chan->chan_channel = 0;
    143       1.1   thorpej 	chan->chan_flags = 0;
    144       1.1   thorpej 	chan->chan_nluns = 8;
    145      1.12      tron 	chan->chan_ntargets = mpt->mpt_max_devices;
    146      1.12      tron 	chan->chan_id = mpt->mpt_ini_id;
    147       1.1   thorpej 
    148  1.16.2.3      yamt 	/*
    149  1.16.2.3      yamt 	* Save the output of the config so we can rescan the bus in case of
    150  1.16.2.3      yamt 	* errors
    151  1.16.2.3      yamt 	*/
    152  1.16.2.3      yamt 	mpt->sc_scsibus_dv = config_found(mpt->sc_dev, &mpt->sc_channel,
    153  1.16.2.3      yamt 	scsiprint);
    154       1.1   thorpej }
    155       1.1   thorpej 
    156       1.1   thorpej int
    157       1.1   thorpej mpt_dma_mem_alloc(mpt_softc_t *mpt)
    158       1.1   thorpej {
    159       1.1   thorpej 	bus_dma_segment_t reply_seg, request_seg;
    160       1.1   thorpej 	int reply_rseg, request_rseg;
    161       1.1   thorpej 	bus_addr_t pptr, end;
    162      1.11  christos 	char *vptr;
    163       1.1   thorpej 	size_t len;
    164       1.1   thorpej 	int error, i;
    165       1.1   thorpej 
    166       1.1   thorpej 	/* Check if we have already allocated the reply memory. */
    167       1.1   thorpej 	if (mpt->reply != NULL)
    168       1.1   thorpej 		return (0);
    169       1.1   thorpej 
    170       1.1   thorpej 	/*
    171       1.1   thorpej 	 * Allocate the request pool.  This isn't really DMA'd memory,
    172       1.1   thorpej 	 * but it's a convenient place to do it.
    173       1.1   thorpej 	 */
    174       1.1   thorpej 	len = sizeof(request_t) * MPT_MAX_REQUESTS(mpt);
    175       1.1   thorpej 	mpt->request_pool = malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
    176       1.1   thorpej 	if (mpt->request_pool == NULL) {
    177  1.16.2.1      yamt 		aprint_error_dev(mpt->sc_dev, "unable to allocate request pool\n");
    178       1.1   thorpej 		return (ENOMEM);
    179       1.1   thorpej 	}
    180       1.1   thorpej 
    181       1.1   thorpej 	/*
    182       1.1   thorpej 	 * Allocate DMA resources for reply buffers.
    183       1.1   thorpej 	 */
    184       1.1   thorpej 	error = bus_dmamem_alloc(mpt->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0,
    185       1.1   thorpej 	    &reply_seg, 1, &reply_rseg, 0);
    186       1.1   thorpej 	if (error) {
    187  1.16.2.1      yamt 		aprint_error_dev(mpt->sc_dev, "unable to allocate reply area, error = %d\n",
    188      1.14    cegger 		    error);
    189       1.1   thorpej 		goto fail_0;
    190       1.1   thorpej 	}
    191       1.1   thorpej 
    192       1.1   thorpej 	error = bus_dmamem_map(mpt->sc_dmat, &reply_seg, reply_rseg, PAGE_SIZE,
    193      1.11  christos 	    (void **) &mpt->reply, BUS_DMA_COHERENT/*XXX*/);
    194       1.1   thorpej 	if (error) {
    195  1.16.2.1      yamt 		aprint_error_dev(mpt->sc_dev, "unable to map reply area, error = %d\n",
    196      1.14    cegger 		    error);
    197       1.1   thorpej 		goto fail_1;
    198       1.1   thorpej 	}
    199       1.1   thorpej 
    200       1.1   thorpej 	error = bus_dmamap_create(mpt->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
    201       1.1   thorpej 	    0, 0, &mpt->reply_dmap);
    202       1.1   thorpej 	if (error) {
    203  1.16.2.1      yamt 		aprint_error_dev(mpt->sc_dev, "unable to create reply DMA map, error = %d\n",
    204      1.14    cegger 		    error);
    205       1.1   thorpej 		goto fail_2;
    206       1.1   thorpej 	}
    207       1.1   thorpej 
    208       1.1   thorpej 	error = bus_dmamap_load(mpt->sc_dmat, mpt->reply_dmap, mpt->reply,
    209       1.1   thorpej 	    PAGE_SIZE, NULL, 0);
    210       1.1   thorpej 	if (error) {
    211  1.16.2.1      yamt 		aprint_error_dev(mpt->sc_dev, "unable to load reply DMA map, error = %d\n",
    212      1.14    cegger 		    error);
    213       1.1   thorpej 		goto fail_3;
    214       1.1   thorpej 	}
    215       1.1   thorpej 	mpt->reply_phys = mpt->reply_dmap->dm_segs[0].ds_addr;
    216       1.1   thorpej 
    217       1.1   thorpej 	/*
    218       1.1   thorpej 	 * Allocate DMA resources for request buffers.
    219       1.1   thorpej 	 */
    220       1.1   thorpej 	error = bus_dmamem_alloc(mpt->sc_dmat, MPT_REQ_MEM_SIZE(mpt),
    221       1.1   thorpej 	    PAGE_SIZE, 0, &request_seg, 1, &request_rseg, 0);
    222       1.1   thorpej 	if (error) {
    223  1.16.2.1      yamt 		aprint_error_dev(mpt->sc_dev, "unable to allocate request area, "
    224      1.14    cegger 		    "error = %d\n", error);
    225       1.1   thorpej 		goto fail_4;
    226       1.1   thorpej 	}
    227       1.1   thorpej 
    228       1.1   thorpej 	error = bus_dmamem_map(mpt->sc_dmat, &request_seg, request_rseg,
    229      1.11  christos 	    MPT_REQ_MEM_SIZE(mpt), (void **) &mpt->request, 0);
    230       1.1   thorpej 	if (error) {
    231  1.16.2.1      yamt 		aprint_error_dev(mpt->sc_dev, "unable to map request area, error = %d\n",
    232      1.14    cegger 		    error);
    233       1.1   thorpej 		goto fail_5;
    234       1.1   thorpej 	}
    235       1.1   thorpej 
    236       1.1   thorpej 	error = bus_dmamap_create(mpt->sc_dmat, MPT_REQ_MEM_SIZE(mpt), 1,
    237       1.1   thorpej 	    MPT_REQ_MEM_SIZE(mpt), 0, 0, &mpt->request_dmap);
    238       1.1   thorpej 	if (error) {
    239  1.16.2.1      yamt 		aprint_error_dev(mpt->sc_dev, "unable to create request DMA map, "
    240      1.14    cegger 		    "error = %d\n", error);
    241       1.1   thorpej 		goto fail_6;
    242       1.1   thorpej 	}
    243       1.1   thorpej 
    244       1.1   thorpej 	error = bus_dmamap_load(mpt->sc_dmat, mpt->request_dmap, mpt->request,
    245       1.1   thorpej 	    MPT_REQ_MEM_SIZE(mpt), NULL, 0);
    246       1.1   thorpej 	if (error) {
    247  1.16.2.1      yamt 		aprint_error_dev(mpt->sc_dev, "unable to load request DMA map, error = %d\n",
    248      1.14    cegger 		    error);
    249       1.1   thorpej 		goto fail_7;
    250       1.1   thorpej 	}
    251       1.1   thorpej 	mpt->request_phys = mpt->request_dmap->dm_segs[0].ds_addr;
    252       1.1   thorpej 
    253       1.1   thorpej 	pptr = mpt->request_phys;
    254      1.11  christos 	vptr = (void *) mpt->request;
    255       1.1   thorpej 	end = pptr + MPT_REQ_MEM_SIZE(mpt);
    256       1.1   thorpej 
    257       1.1   thorpej 	for (i = 0; pptr < end; i++) {
    258       1.1   thorpej 		request_t *req = &mpt->request_pool[i];
    259       1.1   thorpej 		req->index = i;
    260       1.1   thorpej 
    261       1.1   thorpej 		/* Store location of Request Data */
    262       1.1   thorpej 		req->req_pbuf = pptr;
    263       1.1   thorpej 		req->req_vbuf = vptr;
    264       1.1   thorpej 
    265       1.1   thorpej 		pptr += MPT_REQUEST_AREA;
    266       1.1   thorpej 		vptr += MPT_REQUEST_AREA;
    267       1.1   thorpej 
    268       1.1   thorpej 		req->sense_pbuf = (pptr - MPT_SENSE_SIZE);
    269       1.1   thorpej 		req->sense_vbuf = (vptr - MPT_SENSE_SIZE);
    270       1.1   thorpej 
    271       1.5       tls 		error = bus_dmamap_create(mpt->sc_dmat, MAXPHYS,
    272       1.5       tls 		    MPT_SGL_MAX, MAXPHYS, 0, 0, &req->dmap);
    273       1.1   thorpej 		if (error) {
    274  1.16.2.1      yamt 			aprint_error_dev(mpt->sc_dev, "unable to create req %d DMA map, "
    275      1.14    cegger 			    "error = %d\n", i, error);
    276       1.1   thorpej 			goto fail_8;
    277       1.1   thorpej 		}
    278       1.1   thorpej 	}
    279       1.1   thorpej 
    280       1.1   thorpej 	return (0);
    281       1.1   thorpej 
    282       1.1   thorpej  fail_8:
    283       1.1   thorpej 	for (--i; i >= 0; i--) {
    284       1.1   thorpej 		request_t *req = &mpt->request_pool[i];
    285       1.3   thorpej 		if (req->dmap != NULL)
    286       1.3   thorpej 			bus_dmamap_destroy(mpt->sc_dmat, req->dmap);
    287       1.1   thorpej 	}
    288       1.1   thorpej 	bus_dmamap_unload(mpt->sc_dmat, mpt->request_dmap);
    289       1.1   thorpej  fail_7:
    290       1.1   thorpej 	bus_dmamap_destroy(mpt->sc_dmat, mpt->request_dmap);
    291       1.1   thorpej  fail_6:
    292      1.11  christos 	bus_dmamem_unmap(mpt->sc_dmat, (void *)mpt->request, PAGE_SIZE);
    293       1.1   thorpej  fail_5:
    294       1.1   thorpej 	bus_dmamem_free(mpt->sc_dmat, &request_seg, request_rseg);
    295       1.1   thorpej  fail_4:
    296       1.1   thorpej 	bus_dmamap_unload(mpt->sc_dmat, mpt->reply_dmap);
    297       1.1   thorpej  fail_3:
    298       1.1   thorpej 	bus_dmamap_destroy(mpt->sc_dmat, mpt->reply_dmap);
    299       1.1   thorpej  fail_2:
    300      1.11  christos 	bus_dmamem_unmap(mpt->sc_dmat, (void *)mpt->reply, PAGE_SIZE);
    301       1.1   thorpej  fail_1:
    302       1.1   thorpej 	bus_dmamem_free(mpt->sc_dmat, &reply_seg, reply_rseg);
    303       1.1   thorpej  fail_0:
    304       1.1   thorpej 	free(mpt->request_pool, M_DEVBUF);
    305       1.1   thorpej 
    306       1.1   thorpej 	mpt->reply = NULL;
    307       1.1   thorpej 	mpt->request = NULL;
    308       1.1   thorpej 	mpt->request_pool = NULL;
    309       1.1   thorpej 
    310       1.1   thorpej 	return (error);
    311       1.1   thorpej }
    312       1.1   thorpej 
    313       1.1   thorpej int
    314       1.1   thorpej mpt_intr(void *arg)
    315       1.1   thorpej {
    316       1.1   thorpej 	mpt_softc_t *mpt = arg;
    317       1.1   thorpej 	int nrepl = 0;
    318       1.1   thorpej 
    319       1.1   thorpej 	if ((mpt_read(mpt, MPT_OFFSET_INTR_STATUS) & MPT_INTR_REPLY_READY) == 0)
    320       1.1   thorpej 		return (0);
    321       1.1   thorpej 
    322  1.16.2.3      yamt 	nrepl = mpt_drain_queue(mpt);
    323       1.1   thorpej 	return (nrepl != 0);
    324       1.1   thorpej }
    325       1.1   thorpej 
    326       1.1   thorpej void
    327       1.1   thorpej mpt_prt(mpt_softc_t *mpt, const char *fmt, ...)
    328       1.1   thorpej {
    329       1.1   thorpej 	va_list ap;
    330       1.1   thorpej 
    331  1.16.2.1      yamt 	printf("%s: ", device_xname(mpt->sc_dev));
    332       1.1   thorpej 	va_start(ap, fmt);
    333       1.1   thorpej 	vprintf(fmt, ap);
    334       1.1   thorpej 	va_end(ap);
    335       1.1   thorpej 	printf("\n");
    336       1.1   thorpej }
    337       1.1   thorpej 
    338       1.1   thorpej static int
    339       1.1   thorpej mpt_poll(mpt_softc_t *mpt, struct scsipi_xfer *xs, int count)
    340       1.1   thorpej {
    341       1.1   thorpej 
    342       1.1   thorpej 	/* Timeouts are in msec, so we loop in 1000usec cycles */
    343       1.1   thorpej 	while (count) {
    344       1.1   thorpej 		mpt_intr(mpt);
    345       1.1   thorpej 		if (xs->xs_status & XS_STS_DONE)
    346       1.1   thorpej 			return (0);
    347       1.1   thorpej 		delay(1000);		/* only happens in boot, so ok */
    348       1.1   thorpej 		count--;
    349       1.1   thorpej 	}
    350       1.1   thorpej 	return (1);
    351       1.1   thorpej }
    352       1.1   thorpej 
    353       1.1   thorpej static void
    354       1.1   thorpej mpt_timeout(void *arg)
    355       1.1   thorpej {
    356       1.1   thorpej 	request_t *req = arg;
    357  1.16.2.3      yamt 	struct scsipi_xfer *xs;
    358  1.16.2.3      yamt 	struct scsipi_periph *periph;
    359  1.16.2.3      yamt 	mpt_softc_t *mpt;
    360  1.16.2.3      yamt  	uint32_t oseq;
    361  1.16.2.3      yamt 	int s, nrepl = 0;
    362  1.16.2.3      yamt 
    363  1.16.2.3      yamt 	if (req->xfer  == NULL) {
    364  1.16.2.3      yamt 		printf("mpt_timeout: NULL xfer for request index 0x%x, sequenc 0x%x\n",
    365  1.16.2.3      yamt 		req->index, req->sequence);
    366  1.16.2.3      yamt 		return;
    367  1.16.2.3      yamt 	}
    368  1.16.2.3      yamt 	xs = req->xfer;
    369  1.16.2.3      yamt 	periph = xs->xs_periph;
    370  1.16.2.3      yamt 	mpt = (void *) periph->periph_channel->chan_adapter->adapt_dev;
    371       1.1   thorpej 	scsipi_printaddr(periph);
    372       1.1   thorpej 	printf("command timeout\n");
    373       1.1   thorpej 
    374       1.1   thorpej 	s = splbio();
    375       1.1   thorpej 
    376       1.1   thorpej 	oseq = req->sequence;
    377       1.1   thorpej 	mpt->timeouts++;
    378       1.1   thorpej 	if (mpt_intr(mpt)) {
    379       1.1   thorpej 		if (req->sequence != oseq) {
    380  1.16.2.3      yamt 			mpt->success++;
    381       1.1   thorpej 			mpt_prt(mpt, "recovered from command timeout");
    382       1.1   thorpej 			splx(s);
    383       1.1   thorpej 			return;
    384       1.1   thorpej 		}
    385       1.1   thorpej 	}
    386  1.16.2.3      yamt 
    387  1.16.2.3      yamt 	/*
    388  1.16.2.3      yamt 	 * Ensure the IOC is really done giving us data since it appears it can
    389  1.16.2.3      yamt 	 * sometimes fail to give us interrupts under heavy load.
    390  1.16.2.3      yamt 	 */
    391  1.16.2.3      yamt 	nrepl = mpt_drain_queue(mpt);
    392  1.16.2.3      yamt 	if (nrepl ) {
    393  1.16.2.3      yamt 		mpt_prt(mpt, "mpt_timeout: recovered %d commands",nrepl);
    394  1.16.2.3      yamt 	}
    395  1.16.2.3      yamt 
    396  1.16.2.3      yamt 	if (req->sequence != oseq) {
    397  1.16.2.3      yamt 		mpt->success++;
    398  1.16.2.3      yamt 		splx(s);
    399  1.16.2.3      yamt 		return;
    400  1.16.2.3      yamt 	}
    401  1.16.2.3      yamt 
    402       1.1   thorpej 	mpt_prt(mpt,
    403       1.1   thorpej 	    "timeout on request index = 0x%x, seq = 0x%08x",
    404       1.1   thorpej 	    req->index, req->sequence);
    405       1.1   thorpej 	mpt_check_doorbell(mpt);
    406       1.1   thorpej 	mpt_prt(mpt, "Status 0x%08x, Mask 0x%08x, Doorbell 0x%08x",
    407       1.1   thorpej 	    mpt_read(mpt, MPT_OFFSET_INTR_STATUS),
    408       1.1   thorpej 	    mpt_read(mpt, MPT_OFFSET_INTR_MASK),
    409       1.1   thorpej 	    mpt_read(mpt, MPT_OFFSET_DOORBELL));
    410       1.1   thorpej 	mpt_prt(mpt, "request state: %s", mpt_req_state(req->debug));
    411       1.1   thorpej 	if (mpt->verbose > 1)
    412       1.1   thorpej 		mpt_print_scsi_io_request((MSG_SCSI_IO_REQUEST *)req->req_vbuf);
    413       1.1   thorpej 
    414       1.6   thorpej 	xs->error = XS_TIMEOUT;
    415  1.16.2.3      yamt 	splx(s);
    416  1.16.2.3      yamt 	mpt_restart(mpt, req);
    417  1.16.2.3      yamt }
    418  1.16.2.3      yamt 
    419  1.16.2.3      yamt static void
    420  1.16.2.3      yamt mpt_restart(mpt_softc_t *mpt, request_t *req0)
    421  1.16.2.3      yamt {
    422  1.16.2.3      yamt 	int i, s, nreq;
    423  1.16.2.3      yamt 	request_t *req;
    424  1.16.2.3      yamt 	struct scsipi_xfer *xs;
    425  1.16.2.3      yamt 
    426  1.16.2.3      yamt 	/* first, reset the IOC, leaving stopped so all requests are idle */
    427  1.16.2.3      yamt 	if (mpt_soft_reset(mpt) != MPT_OK) {
    428  1.16.2.3      yamt 		mpt_prt(mpt, "soft reset failed");
    429  1.16.2.3      yamt 		/*
    430  1.16.2.3      yamt 		* Don't try a hard reset since this mangles the PCI
    431  1.16.2.3      yamt 		* configuration registers.
    432  1.16.2.3      yamt 		*/
    433  1.16.2.3      yamt 		return;
    434  1.16.2.3      yamt 	}
    435       1.6   thorpej 
    436  1.16.2.3      yamt 	/* Freeze the channel so scsipi doesn't queue more commands. */
    437  1.16.2.3      yamt 	scsipi_channel_freeze(&mpt->sc_channel, 1);
    438  1.16.2.3      yamt 
    439  1.16.2.3      yamt 	/* Return all pending requests to scsipi and de-allocate them. */
    440  1.16.2.3      yamt 	s = splbio();
    441  1.16.2.3      yamt 	nreq = 0;
    442  1.16.2.3      yamt 	for (i = 0; i < MPT_MAX_REQUESTS(mpt); i++) {
    443  1.16.2.3      yamt 		req = &mpt->request_pool[i];
    444  1.16.2.3      yamt 		xs = req->xfer;
    445  1.16.2.3      yamt 		if (xs != NULL) {
    446  1.16.2.3      yamt 			if (xs->datalen != 0)
    447  1.16.2.3      yamt 				bus_dmamap_unload(mpt->sc_dmat, req->dmap);
    448  1.16.2.3      yamt 			req->xfer = NULL;
    449  1.16.2.3      yamt 			callout_stop(&xs->xs_callout);
    450  1.16.2.3      yamt 			if (req != req0) {
    451  1.16.2.3      yamt 				nreq++;
    452  1.16.2.3      yamt 				xs->error = XS_REQUEUE;
    453  1.16.2.3      yamt 			}
    454  1.16.2.3      yamt 			scsipi_done(xs);
    455  1.16.2.3      yamt 			/*
    456  1.16.2.3      yamt 			* Don't need to mpt_free_request() since mpt_init()
    457  1.16.2.3      yamt 			* below will free all requests anyway.
    458  1.16.2.3      yamt 			*/
    459  1.16.2.3      yamt 			mpt_free_request(mpt, req);
    460  1.16.2.3      yamt 		}
    461  1.16.2.3      yamt 	}
    462       1.1   thorpej 	splx(s);
    463  1.16.2.3      yamt 	if (nreq > 0)
    464  1.16.2.3      yamt 		mpt_prt(mpt, "re-queued %d requests", nreq);
    465  1.16.2.3      yamt 
    466  1.16.2.3      yamt 	/* Re-initialize the IOC (which restarts it). */
    467  1.16.2.3      yamt 	if (mpt_init(mpt, MPT_DB_INIT_HOST) == 0)
    468  1.16.2.3      yamt 		mpt_prt(mpt, "restart succeeded");
    469  1.16.2.3      yamt 	/* else error message already printed */
    470  1.16.2.3      yamt 
    471  1.16.2.3      yamt 	/* Thaw the channel, causing scsipi to re-queue the commands. */
    472  1.16.2.3      yamt 	scsipi_channel_thaw(&mpt->sc_channel, 1);
    473  1.16.2.3      yamt }
    474  1.16.2.3      yamt 
    475  1.16.2.3      yamt static int
    476  1.16.2.3      yamt mpt_drain_queue(mpt_softc_t *mpt)
    477  1.16.2.3      yamt {
    478  1.16.2.3      yamt 	int nrepl = 0;
    479  1.16.2.3      yamt 	uint32_t reply;
    480  1.16.2.3      yamt 
    481  1.16.2.3      yamt 	reply = mpt_pop_reply_queue(mpt);
    482  1.16.2.3      yamt 	while (reply != MPT_REPLY_EMPTY) {
    483  1.16.2.3      yamt 		nrepl++;
    484  1.16.2.3      yamt 		if (mpt->verbose > 1) {
    485  1.16.2.3      yamt 			if ((reply & MPT_CONTEXT_REPLY) != 0) {
    486  1.16.2.3      yamt 				/* Address reply; IOC has something to say */
    487  1.16.2.3      yamt 				mpt_print_reply(MPT_REPLY_PTOV(mpt, reply));
    488  1.16.2.3      yamt 			} else {
    489  1.16.2.3      yamt 				/* Context reply; all went well */
    490  1.16.2.3      yamt 				mpt_prt(mpt, "context %u reply OK", reply);
    491  1.16.2.3      yamt 			}
    492  1.16.2.3      yamt 		}
    493  1.16.2.3      yamt 		mpt_done(mpt, reply);
    494  1.16.2.3      yamt 		reply = mpt_pop_reply_queue(mpt);
    495  1.16.2.3      yamt 	}
    496  1.16.2.3      yamt 	return (nrepl);
    497       1.1   thorpej }
    498       1.1   thorpej 
    499       1.1   thorpej static void
    500       1.1   thorpej mpt_done(mpt_softc_t *mpt, uint32_t reply)
    501       1.1   thorpej {
    502       1.6   thorpej 	struct scsipi_xfer *xs = NULL;
    503       1.1   thorpej 	struct scsipi_periph *periph;
    504       1.1   thorpej 	int index;
    505       1.1   thorpej 	request_t *req;
    506       1.1   thorpej 	MSG_REQUEST_HEADER *mpt_req;
    507       1.1   thorpej 	MSG_SCSI_IO_REPLY *mpt_reply;
    508  1.16.2.3      yamt 	int restart = 0; /* nonzero if we need to restart the IOC*/
    509       1.1   thorpej 
    510       1.1   thorpej 	if (__predict_true((reply & MPT_CONTEXT_REPLY) == 0)) {
    511       1.1   thorpej 		/* context reply (ok) */
    512       1.1   thorpej 		mpt_reply = NULL;
    513       1.1   thorpej 		index = reply & MPT_CONTEXT_MASK;
    514       1.1   thorpej 	} else {
    515       1.1   thorpej 		/* address reply (error) */
    516       1.1   thorpej 
    517       1.1   thorpej 		/* XXX BUS_DMASYNC_POSTREAD XXX */
    518       1.1   thorpej 		mpt_reply = MPT_REPLY_PTOV(mpt, reply);
    519  1.16.2.3      yamt 		if (mpt_reply != NULL) {
    520  1.16.2.3      yamt 			if (mpt->verbose > 1) {
    521  1.16.2.3      yamt 				uint32_t *pReply = (uint32_t *) mpt_reply;
    522       1.9     perry 
    523  1.16.2.3      yamt 				mpt_prt(mpt, "Address Reply (index %u):",
    524  1.16.2.3      yamt 				    le32toh(mpt_reply->MsgContext) & 0xffff);
    525  1.16.2.3      yamt 				mpt_prt(mpt, "%08x %08x %08x %08x", pReply[0],
    526  1.16.2.3      yamt 				    pReply[1], pReply[2], pReply[3]);
    527  1.16.2.3      yamt 				mpt_prt(mpt, "%08x %08x %08x %08x", pReply[4],
    528  1.16.2.3      yamt 				    pReply[5], pReply[6], pReply[7]);
    529  1.16.2.3      yamt 				mpt_prt(mpt, "%08x %08x %08x %08x", pReply[8],
    530  1.16.2.3      yamt 				    pReply[9], pReply[10], pReply[11]);
    531  1.16.2.3      yamt 			}
    532  1.16.2.3      yamt 			index = le32toh(mpt_reply->MsgContext);
    533  1.16.2.3      yamt 		} else
    534  1.16.2.3      yamt 			index = reply & MPT_CONTEXT_MASK;
    535       1.1   thorpej 	}
    536       1.1   thorpej 
    537       1.1   thorpej 	/*
    538       1.1   thorpej 	 * Address reply with MessageContext high bit set.
    539       1.1   thorpej 	 * This is most likely a notify message, so we try
    540       1.1   thorpej 	 * to process it, then free it.
    541       1.1   thorpej 	 */
    542       1.1   thorpej 	if (__predict_false((index & 0x80000000) != 0)) {
    543       1.1   thorpej 		if (mpt_reply != NULL)
    544       1.1   thorpej 			mpt_ctlop(mpt, mpt_reply, reply);
    545       1.1   thorpej 		else
    546  1.16.2.3      yamt 			mpt_prt(mpt, "%s: index 0x%x, NULL reply", __func__,
    547  1.16.2.3      yamt 			    index);
    548       1.1   thorpej 		return;
    549       1.1   thorpej 	}
    550       1.1   thorpej 
    551       1.1   thorpej 	/* Did we end up with a valid index into the table? */
    552       1.1   thorpej 	if (__predict_false(index < 0 || index >= MPT_MAX_REQUESTS(mpt))) {
    553  1.16.2.3      yamt 		mpt_prt(mpt, "%s: invalid index (0x%x) in reply", __func__,
    554  1.16.2.3      yamt 		    index);
    555       1.1   thorpej 		return;
    556       1.1   thorpej 	}
    557       1.1   thorpej 
    558       1.1   thorpej 	req = &mpt->request_pool[index];
    559       1.1   thorpej 
    560       1.1   thorpej 	/* Make sure memory hasn't been trashed. */
    561       1.1   thorpej 	if (__predict_false(req->index != index)) {
    562  1.16.2.3      yamt 		mpt_prt(mpt, "%s: corrupted request_t (0x%x)", __func__,
    563  1.16.2.3      yamt 		    index);
    564       1.1   thorpej 		return;
    565       1.1   thorpej 	}
    566       1.1   thorpej 
    567       1.2   thorpej 	MPT_SYNC_REQ(mpt, req, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    568       1.1   thorpej 	mpt_req = req->req_vbuf;
    569       1.1   thorpej 
    570       1.1   thorpej 	/* Short cut for task management replies; nothing more for us to do. */
    571       1.1   thorpej 	if (__predict_false(mpt_req->Function == MPI_FUNCTION_SCSI_TASK_MGMT)) {
    572       1.1   thorpej 		if (mpt->verbose > 1)
    573  1.16.2.3      yamt 			mpt_prt(mpt, "%s: TASK MGMT", __func__);
    574  1.16.2.3      yamt 		KASSERT(req == mpt->mngt_req);
    575  1.16.2.3      yamt 		mpt->mngt_req = NULL;
    576       1.1   thorpej 		goto done;
    577       1.1   thorpej 	}
    578       1.1   thorpej 
    579       1.1   thorpej 	if (__predict_false(mpt_req->Function == MPI_FUNCTION_PORT_ENABLE))
    580       1.1   thorpej 		goto done;
    581       1.1   thorpej 
    582       1.1   thorpej 	/*
    583       1.1   thorpej 	 * At this point, it had better be a SCSI I/O command, but don't
    584       1.1   thorpej 	 * crash if it isn't.
    585       1.1   thorpej 	 */
    586       1.1   thorpej 	if (__predict_false(mpt_req->Function !=
    587       1.1   thorpej 			    MPI_FUNCTION_SCSI_IO_REQUEST)) {
    588       1.1   thorpej 		if (mpt->verbose > 1)
    589  1.16.2.3      yamt 			mpt_prt(mpt, "%s: unknown Function 0x%x (0x%x)",
    590  1.16.2.3      yamt 			    __func__, mpt_req->Function, index);
    591       1.1   thorpej 		goto done;
    592       1.1   thorpej 	}
    593       1.1   thorpej 
    594       1.1   thorpej 	/* Recover scsipi_xfer from the request structure. */
    595       1.1   thorpej 	xs = req->xfer;
    596       1.1   thorpej 
    597       1.1   thorpej 	/* Can't have a SCSI command without a scsipi_xfer. */
    598       1.1   thorpej 	if (__predict_false(xs == NULL)) {
    599       1.1   thorpej 		mpt_prt(mpt,
    600  1.16.2.3      yamt 		    "%s: no scsipi_xfer, index = 0x%x, seq = 0x%08x", __func__,
    601       1.1   thorpej 		    req->index, req->sequence);
    602       1.1   thorpej 		mpt_prt(mpt, "request state: %s", mpt_req_state(req->debug));
    603       1.1   thorpej 		mpt_prt(mpt, "mpt_request:");
    604       1.1   thorpej 		mpt_print_scsi_io_request((MSG_SCSI_IO_REQUEST *)req->req_vbuf);
    605       1.1   thorpej 
    606       1.1   thorpej 		if (mpt_reply != NULL) {
    607       1.1   thorpej 			mpt_prt(mpt, "mpt_reply:");
    608       1.1   thorpej 			mpt_print_reply(mpt_reply);
    609       1.1   thorpej 		} else {
    610       1.1   thorpej 			mpt_prt(mpt, "context reply: 0x%08x", reply);
    611       1.1   thorpej 		}
    612       1.1   thorpej 		goto done;
    613       1.1   thorpej 	}
    614       1.1   thorpej 
    615       1.1   thorpej 	callout_stop(&xs->xs_callout);
    616       1.1   thorpej 
    617       1.1   thorpej 	periph = xs->xs_periph;
    618       1.1   thorpej 
    619       1.1   thorpej 	/*
    620       1.1   thorpej 	 * If we were a data transfer, unload the map that described
    621       1.1   thorpej 	 * the data buffer.
    622       1.1   thorpej 	 */
    623       1.1   thorpej 	if (__predict_true(xs->datalen != 0)) {
    624       1.1   thorpej 		bus_dmamap_sync(mpt->sc_dmat, req->dmap, 0,
    625       1.1   thorpej 		    req->dmap->dm_mapsize,
    626       1.1   thorpej 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD
    627       1.1   thorpej 						      : BUS_DMASYNC_POSTWRITE);
    628       1.1   thorpej 		bus_dmamap_unload(mpt->sc_dmat, req->dmap);
    629       1.1   thorpej 	}
    630       1.1   thorpej 
    631       1.1   thorpej 	if (__predict_true(mpt_reply == NULL)) {
    632       1.1   thorpej 		/*
    633       1.1   thorpej 		 * Context reply; report that the command was
    634       1.1   thorpej 		 * successful!
    635       1.1   thorpej 		 *
    636       1.1   thorpej 		 * Also report the xfer mode, if necessary.
    637       1.1   thorpej 		 */
    638       1.1   thorpej 		if (__predict_false(mpt->mpt_report_xfer_mode != 0)) {
    639       1.1   thorpej 			if ((mpt->mpt_report_xfer_mode &
    640       1.1   thorpej 			     (1 << periph->periph_target)) != 0)
    641       1.1   thorpej 				mpt_get_xfer_mode(mpt, periph);
    642       1.1   thorpej 		}
    643       1.1   thorpej 		xs->error = XS_NOERROR;
    644       1.1   thorpej 		xs->status = SCSI_OK;
    645       1.1   thorpej 		xs->resid = 0;
    646       1.6   thorpej 		mpt_free_request(mpt, req);
    647       1.1   thorpej 		scsipi_done(xs);
    648       1.6   thorpej 		return;
    649       1.1   thorpej 	}
    650       1.1   thorpej 
    651       1.1   thorpej 	xs->status = mpt_reply->SCSIStatus;
    652  1.16.2.3      yamt 	switch (le16toh(mpt_reply->IOCStatus) & MPI_IOCSTATUS_MASK) {
    653       1.1   thorpej 	case MPI_IOCSTATUS_SCSI_DATA_OVERRUN:
    654       1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
    655  1.16.2.3      yamt 		mpt_prt(mpt, "%s: IOC overrun!", __func__);
    656       1.1   thorpej 		break;
    657       1.1   thorpej 
    658       1.1   thorpej 	case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN:
    659       1.1   thorpej 		/*
    660       1.1   thorpej 		 * Yikes!  Tagged queue full comes through this path!
    661       1.1   thorpej 		 *
    662       1.1   thorpej 		 * So we'll change it to a status error and anything
    663       1.1   thorpej 		 * that returns status should probably be a status
    664       1.1   thorpej 		 * error as well.
    665       1.1   thorpej 		 */
    666      1.15       chs 		xs->resid = xs->datalen - le32toh(mpt_reply->TransferCount);
    667       1.1   thorpej 		if (mpt_reply->SCSIState &
    668       1.1   thorpej 		    MPI_SCSI_STATE_NO_SCSI_STATUS) {
    669       1.1   thorpej 			xs->error = XS_DRIVER_STUFFUP;
    670       1.1   thorpej 			break;
    671       1.1   thorpej 		}
    672       1.1   thorpej 		/* FALLTHROUGH */
    673       1.1   thorpej 	case MPI_IOCSTATUS_SUCCESS:
    674       1.1   thorpej 	case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR:
    675       1.1   thorpej 		switch (xs->status) {
    676       1.1   thorpej 		case SCSI_OK:
    677       1.1   thorpej 			/* Report the xfer mode, if necessary. */
    678       1.1   thorpej 			if ((mpt->mpt_report_xfer_mode &
    679       1.1   thorpej 			     (1 << periph->periph_target)) != 0)
    680       1.1   thorpej 				mpt_get_xfer_mode(mpt, periph);
    681       1.1   thorpej 			xs->resid = 0;
    682       1.1   thorpej 			break;
    683       1.1   thorpej 
    684       1.1   thorpej 		case SCSI_CHECK:
    685       1.1   thorpej 			xs->error = XS_SENSE;
    686       1.1   thorpej 			break;
    687       1.1   thorpej 
    688       1.1   thorpej 		case SCSI_BUSY:
    689       1.1   thorpej 		case SCSI_QUEUE_FULL:
    690       1.1   thorpej 			xs->error = XS_BUSY;
    691       1.1   thorpej 			break;
    692       1.1   thorpej 
    693       1.1   thorpej 		default:
    694       1.1   thorpej 			scsipi_printaddr(periph);
    695       1.1   thorpej 			printf("invalid status code %d\n", xs->status);
    696       1.1   thorpej 			xs->error = XS_DRIVER_STUFFUP;
    697       1.1   thorpej 			break;
    698       1.1   thorpej 		}
    699       1.1   thorpej 		break;
    700       1.1   thorpej 
    701       1.1   thorpej 	case MPI_IOCSTATUS_BUSY:
    702       1.1   thorpej 	case MPI_IOCSTATUS_INSUFFICIENT_RESOURCES:
    703       1.1   thorpej 		xs->error = XS_RESOURCE_SHORTAGE;
    704       1.1   thorpej 		break;
    705       1.1   thorpej 
    706       1.1   thorpej 	case MPI_IOCSTATUS_SCSI_INVALID_BUS:
    707       1.1   thorpej 	case MPI_IOCSTATUS_SCSI_INVALID_TARGETID:
    708       1.1   thorpej 	case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
    709       1.1   thorpej 		xs->error = XS_SELTIMEOUT;
    710       1.1   thorpej 		break;
    711       1.1   thorpej 
    712       1.1   thorpej 	case MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
    713       1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
    714  1.16.2.3      yamt 		mpt_prt(mpt, "%s: IOC SCSI residual mismatch!", __func__);
    715  1.16.2.3      yamt 		restart = 1;
    716       1.1   thorpej 		break;
    717       1.1   thorpej 
    718       1.1   thorpej 	case MPI_IOCSTATUS_SCSI_TASK_TERMINATED:
    719       1.1   thorpej 		/* XXX What should we do here? */
    720  1.16.2.3      yamt 		mpt_prt(mpt, "%s: IOC SCSI task terminated!", __func__);
    721  1.16.2.3      yamt 		restart = 1;
    722       1.1   thorpej 		break;
    723       1.1   thorpej 
    724       1.1   thorpej 	case MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
    725       1.1   thorpej 		/* XXX */
    726       1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
    727  1.16.2.3      yamt 		mpt_prt(mpt, "%s: IOC SCSI task failed!", __func__);
    728  1.16.2.3      yamt 		restart = 1;
    729       1.1   thorpej 		break;
    730       1.1   thorpej 
    731       1.1   thorpej 	case MPI_IOCSTATUS_SCSI_IOC_TERMINATED:
    732       1.1   thorpej 		/* XXX */
    733       1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
    734  1.16.2.3      yamt 		mpt_prt(mpt, "%s: IOC task terminated!", __func__);
    735  1.16.2.3      yamt 		restart = 1;
    736       1.1   thorpej 		break;
    737       1.1   thorpej 
    738       1.1   thorpej 	case MPI_IOCSTATUS_SCSI_EXT_TERMINATED:
    739       1.1   thorpej 		/* XXX This is a bus-reset */
    740       1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
    741  1.16.2.3      yamt 		mpt_prt(mpt, "%s: IOC SCSI bus reset!", __func__);
    742  1.16.2.3      yamt 		restart = 1;
    743  1.16.2.3      yamt 		break;
    744  1.16.2.3      yamt 
    745  1.16.2.3      yamt 	case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR:
    746  1.16.2.3      yamt 		/*
    747  1.16.2.3      yamt 		 * FreeBSD and Linux indicate this is a phase error between
    748  1.16.2.3      yamt 		 * the IOC and the drive itself. When this happens, the IOC
    749  1.16.2.3      yamt 		 * becomes unhappy and stops processing all transactions.
    750  1.16.2.3      yamt 		 * Call mpt_timeout which knows how to get the IOC back
    751  1.16.2.3      yamt 		 * on its feet.
    752  1.16.2.3      yamt 		 */
    753  1.16.2.3      yamt 		 mpt_prt(mpt, "%s: IOC indicates protocol error -- "
    754  1.16.2.3      yamt 		     "recovering...", __func__);
    755  1.16.2.3      yamt 		xs->error = XS_TIMEOUT;
    756  1.16.2.3      yamt 		restart = 1;
    757  1.16.2.3      yamt 
    758       1.1   thorpej 		break;
    759       1.1   thorpej 
    760       1.1   thorpej 	default:
    761       1.1   thorpej 		/* XXX unrecognized HBA error */
    762       1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
    763  1.16.2.3      yamt 		mpt_prt(mpt, "%s: IOC returned unknown code: 0x%x", __func__,
    764  1.16.2.3      yamt 		    le16toh(mpt_reply->IOCStatus));
    765  1.16.2.3      yamt 		restart = 1;
    766       1.1   thorpej 		break;
    767       1.1   thorpej 	}
    768       1.9     perry 
    769  1.16.2.3      yamt 	if (mpt_reply != NULL) {
    770  1.16.2.3      yamt 		if (mpt_reply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_VALID) {
    771  1.16.2.3      yamt 			memcpy(&xs->sense.scsi_sense, req->sense_vbuf,
    772  1.16.2.3      yamt 			    sizeof(xs->sense.scsi_sense));
    773  1.16.2.3      yamt 		} else if (mpt_reply->SCSIState &
    774  1.16.2.3      yamt 		    MPI_SCSI_STATE_AUTOSENSE_FAILED) {
    775  1.16.2.3      yamt 			/*
    776  1.16.2.3      yamt 			 * This will cause the scsipi layer to issue
    777  1.16.2.3      yamt 			 * a REQUEST SENSE.
    778  1.16.2.3      yamt 			 */
    779  1.16.2.3      yamt 			if (xs->status == SCSI_CHECK)
    780  1.16.2.3      yamt 				xs->error = XS_BUSY;
    781  1.16.2.3      yamt 		}
    782       1.1   thorpej 	}
    783       1.1   thorpej 
    784       1.1   thorpej  done:
    785  1.16.2.3      yamt 	if (mpt_reply != NULL && le16toh(mpt_reply->IOCStatus) &
    786  1.16.2.3      yamt 	MPI_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
    787  1.16.2.3      yamt 		mpt_prt(mpt, "%s: IOC has error - logging...\n", __func__);
    788  1.16.2.3      yamt 		mpt_ctlop(mpt, mpt_reply, reply);
    789  1.16.2.3      yamt 	}
    790  1.16.2.3      yamt 
    791  1.16.2.3      yamt 	/* If IOC done with this request, free it up. */
    792       1.1   thorpej 	if (mpt_reply == NULL || (mpt_reply->MsgFlags & 0x80) == 0)
    793       1.1   thorpej 		mpt_free_request(mpt, req);
    794       1.1   thorpej 
    795       1.1   thorpej 	/* If address reply, give the buffer back to the IOC. */
    796       1.1   thorpej 	if (mpt_reply != NULL)
    797       1.1   thorpej 		mpt_free_reply(mpt, (reply << 1));
    798       1.6   thorpej 
    799       1.6   thorpej 	if (xs != NULL)
    800       1.6   thorpej 		scsipi_done(xs);
    801  1.16.2.3      yamt 
    802  1.16.2.3      yamt 	if (restart) {
    803  1.16.2.3      yamt 		mpt_prt(mpt, "%s: IOC fatal error: restarting...", __func__);
    804  1.16.2.3      yamt 		mpt_restart(mpt, NULL);
    805  1.16.2.3      yamt 	}
    806       1.1   thorpej }
    807       1.1   thorpej 
    808       1.1   thorpej static void
    809       1.1   thorpej mpt_run_xfer(mpt_softc_t *mpt, struct scsipi_xfer *xs)
    810       1.1   thorpej {
    811       1.1   thorpej 	struct scsipi_periph *periph = xs->xs_periph;
    812       1.1   thorpej 	request_t *req;
    813       1.1   thorpej 	MSG_SCSI_IO_REQUEST *mpt_req;
    814       1.1   thorpej 	int error, s;
    815       1.1   thorpej 
    816       1.1   thorpej 	s = splbio();
    817       1.1   thorpej 	req = mpt_get_request(mpt);
    818       1.1   thorpej 	if (__predict_false(req == NULL)) {
    819       1.1   thorpej 		/* This should happen very infrequently. */
    820       1.1   thorpej 		xs->error = XS_RESOURCE_SHORTAGE;
    821       1.1   thorpej 		scsipi_done(xs);
    822       1.1   thorpej 		splx(s);
    823       1.1   thorpej 		return;
    824       1.1   thorpej 	}
    825       1.1   thorpej 	splx(s);
    826       1.1   thorpej 
    827       1.1   thorpej 	/* Link the req and the scsipi_xfer. */
    828       1.1   thorpej 	req->xfer = xs;
    829       1.1   thorpej 
    830       1.1   thorpej 	/* Now we build the command for the IOC */
    831       1.1   thorpej 	mpt_req = req->req_vbuf;
    832       1.1   thorpej 	memset(mpt_req, 0, sizeof(*mpt_req));
    833       1.1   thorpej 
    834       1.1   thorpej 	mpt_req->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
    835       1.1   thorpej 	mpt_req->Bus = mpt->bus;
    836       1.1   thorpej 
    837       1.1   thorpej 	mpt_req->SenseBufferLength =
    838       1.1   thorpej 	    (sizeof(xs->sense.scsi_sense) < MPT_SENSE_SIZE) ?
    839       1.1   thorpej 	    sizeof(xs->sense.scsi_sense) : MPT_SENSE_SIZE;
    840       1.1   thorpej 
    841       1.1   thorpej 	/*
    842       1.1   thorpej 	 * We use the message context to find the request structure when
    843       1.1   thorpej 	 * we get the command completion interrupt from the IOC.
    844       1.1   thorpej 	 */
    845      1.15       chs 	mpt_req->MsgContext = htole32(req->index);
    846       1.1   thorpej 
    847       1.1   thorpej 	/* Which physical device to do the I/O on. */
    848       1.1   thorpej 	mpt_req->TargetID = periph->periph_target;
    849       1.1   thorpej 	mpt_req->LUN[1] = periph->periph_lun;
    850       1.1   thorpej 
    851       1.1   thorpej 	/* Set the direction of the transfer. */
    852       1.1   thorpej 	if (xs->xs_control & XS_CTL_DATA_IN)
    853       1.1   thorpej 		mpt_req->Control = MPI_SCSIIO_CONTROL_READ;
    854       1.1   thorpej 	else if (xs->xs_control & XS_CTL_DATA_OUT)
    855       1.1   thorpej 		mpt_req->Control = MPI_SCSIIO_CONTROL_WRITE;
    856       1.1   thorpej 	else
    857       1.1   thorpej 		mpt_req->Control = MPI_SCSIIO_CONTROL_NODATATRANSFER;
    858       1.1   thorpej 
    859       1.1   thorpej 	/* Set the queue behavior. */
    860      1.12      tron 	if (__predict_true((!mpt->is_scsi) ||
    861       1.1   thorpej 			   (mpt->mpt_tag_enable &
    862       1.1   thorpej 			    (1 << periph->periph_target)))) {
    863       1.1   thorpej 		switch (XS_CTL_TAGTYPE(xs)) {
    864       1.1   thorpej 		case XS_CTL_HEAD_TAG:
    865       1.1   thorpej 			mpt_req->Control |= MPI_SCSIIO_CONTROL_HEADOFQ;
    866       1.1   thorpej 			break;
    867       1.1   thorpej 
    868       1.1   thorpej #if 0	/* XXX */
    869       1.1   thorpej 		case XS_CTL_ACA_TAG:
    870       1.1   thorpej 			mpt_req->Control |= MPI_SCSIIO_CONTROL_ACAQ;
    871       1.1   thorpej 			break;
    872       1.1   thorpej #endif
    873       1.1   thorpej 
    874       1.1   thorpej 		case XS_CTL_ORDERED_TAG:
    875       1.1   thorpej 			mpt_req->Control |= MPI_SCSIIO_CONTROL_ORDEREDQ;
    876       1.1   thorpej 			break;
    877       1.1   thorpej 
    878       1.1   thorpej 		case XS_CTL_SIMPLE_TAG:
    879       1.1   thorpej 			mpt_req->Control |= MPI_SCSIIO_CONTROL_SIMPLEQ;
    880       1.1   thorpej 			break;
    881       1.1   thorpej 
    882       1.1   thorpej 		default:
    883      1.12      tron 			if (mpt->is_scsi)
    884      1.12      tron 				mpt_req->Control |= MPI_SCSIIO_CONTROL_UNTAGGED;
    885      1.12      tron 			else
    886       1.1   thorpej 				mpt_req->Control |= MPI_SCSIIO_CONTROL_SIMPLEQ;
    887       1.1   thorpej 			break;
    888       1.1   thorpej 		}
    889       1.1   thorpej 	} else
    890       1.1   thorpej 		mpt_req->Control |= MPI_SCSIIO_CONTROL_UNTAGGED;
    891       1.1   thorpej 
    892      1.12      tron 	if (__predict_false(mpt->is_scsi &&
    893       1.1   thorpej 			    (mpt->mpt_disc_enable &
    894       1.1   thorpej 			     (1 << periph->periph_target)) == 0))
    895       1.1   thorpej 		mpt_req->Control |= MPI_SCSIIO_CONTROL_NO_DISCONNECT;
    896       1.1   thorpej 
    897      1.15       chs 	mpt_req->Control = htole32(mpt_req->Control);
    898      1.15       chs 
    899       1.1   thorpej 	/* Copy the SCSI command block into place. */
    900       1.1   thorpej 	memcpy(mpt_req->CDB, xs->cmd, xs->cmdlen);
    901       1.1   thorpej 
    902       1.1   thorpej 	mpt_req->CDBLength = xs->cmdlen;
    903      1.15       chs 	mpt_req->DataLength = htole32(xs->datalen);
    904      1.15       chs 	mpt_req->SenseBufferLowAddr = htole32(req->sense_pbuf);
    905       1.1   thorpej 
    906       1.1   thorpej 	/*
    907       1.1   thorpej 	 * Map the DMA transfer.
    908       1.1   thorpej 	 */
    909       1.1   thorpej 	if (xs->datalen) {
    910       1.1   thorpej 		SGE_SIMPLE32 *se;
    911       1.1   thorpej 
    912       1.1   thorpej 		error = bus_dmamap_load(mpt->sc_dmat, req->dmap, xs->data,
    913       1.1   thorpej 		    xs->datalen, NULL,
    914       1.1   thorpej 		    ((xs->xs_control & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT
    915       1.1   thorpej 						       : BUS_DMA_WAITOK) |
    916       1.1   thorpej 		    BUS_DMA_STREAMING |
    917       1.1   thorpej 		    ((xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMA_READ
    918       1.1   thorpej 						       : BUS_DMA_WRITE));
    919       1.1   thorpej 		switch (error) {
    920       1.1   thorpej 		case 0:
    921       1.1   thorpej 			break;
    922       1.1   thorpej 
    923       1.1   thorpej 		case ENOMEM:
    924       1.1   thorpej 		case EAGAIN:
    925       1.1   thorpej 			xs->error = XS_RESOURCE_SHORTAGE;
    926       1.1   thorpej 			goto out_bad;
    927       1.1   thorpej 
    928       1.1   thorpej 		default:
    929       1.1   thorpej 			xs->error = XS_DRIVER_STUFFUP;
    930       1.1   thorpej 			mpt_prt(mpt, "error %d loading DMA map", error);
    931       1.1   thorpej  out_bad:
    932       1.1   thorpej 			s = splbio();
    933       1.1   thorpej 			mpt_free_request(mpt, req);
    934       1.1   thorpej 			scsipi_done(xs);
    935       1.1   thorpej 			splx(s);
    936       1.1   thorpej 			return;
    937       1.1   thorpej 		}
    938       1.1   thorpej 
    939       1.1   thorpej 		if (req->dmap->dm_nsegs > MPT_NSGL_FIRST(mpt)) {
    940       1.1   thorpej 			int seg, i, nleft = req->dmap->dm_nsegs;
    941       1.1   thorpej 			uint32_t flags;
    942       1.1   thorpej 			SGE_CHAIN32 *ce;
    943       1.1   thorpej 
    944       1.1   thorpej 			seg = 0;
    945       1.1   thorpej 			flags = MPI_SGE_FLAGS_SIMPLE_ELEMENT;
    946       1.1   thorpej 			if (xs->xs_control & XS_CTL_DATA_OUT)
    947       1.1   thorpej 				flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
    948       1.1   thorpej 
    949       1.1   thorpej 			se = (SGE_SIMPLE32 *) &mpt_req->SGL;
    950       1.1   thorpej 			for (i = 0; i < MPT_NSGL_FIRST(mpt) - 1;
    951       1.1   thorpej 			     i++, se++, seg++) {
    952       1.1   thorpej 				uint32_t tf;
    953       1.1   thorpej 
    954       1.1   thorpej 				memset(se, 0, sizeof(*se));
    955      1.15       chs 				se->Address =
    956      1.15       chs 				    htole32(req->dmap->dm_segs[seg].ds_addr);
    957       1.1   thorpej 				MPI_pSGE_SET_LENGTH(se,
    958       1.1   thorpej 				    req->dmap->dm_segs[seg].ds_len);
    959       1.1   thorpej 				tf = flags;
    960       1.1   thorpej 				if (i == MPT_NSGL_FIRST(mpt) - 2)
    961       1.1   thorpej 					tf |= MPI_SGE_FLAGS_LAST_ELEMENT;
    962       1.1   thorpej 				MPI_pSGE_SET_FLAGS(se, tf);
    963      1.15       chs 				se->FlagsLength = htole32(se->FlagsLength);
    964       1.1   thorpej 				nleft--;
    965       1.1   thorpej 			}
    966       1.1   thorpej 
    967       1.1   thorpej 			/*
    968       1.1   thorpej 			 * Tell the IOC where to find the first chain element.
    969       1.1   thorpej 			 */
    970       1.1   thorpej 			mpt_req->ChainOffset =
    971       1.1   thorpej 			    ((char *)se - (char *)mpt_req) >> 2;
    972       1.1   thorpej 
    973       1.1   thorpej 			/*
    974       1.1   thorpej 			 * Until we're finished with all segments...
    975       1.1   thorpej 			 */
    976       1.1   thorpej 			while (nleft) {
    977       1.1   thorpej 				int ntodo;
    978       1.1   thorpej 
    979       1.1   thorpej 				/*
    980       1.1   thorpej 				 * Construct the chain element that points to
    981       1.1   thorpej 				 * the next segment.
    982       1.1   thorpej 				 */
    983       1.1   thorpej 				ce = (SGE_CHAIN32 *) se++;
    984       1.1   thorpej 				if (nleft > MPT_NSGL(mpt)) {
    985       1.1   thorpej 					ntodo = MPT_NSGL(mpt) - 1;
    986       1.1   thorpej 					ce->NextChainOffset = (MPT_RQSL(mpt) -
    987       1.1   thorpej 					    sizeof(SGE_SIMPLE32)) >> 2;
    988      1.15       chs 					ce->Length = htole16(MPT_NSGL(mpt)
    989      1.15       chs 						* sizeof(SGE_SIMPLE32));
    990       1.1   thorpej 				} else {
    991       1.1   thorpej 					ntodo = nleft;
    992       1.1   thorpej 					ce->NextChainOffset = 0;
    993      1.15       chs 					ce->Length = htole16(ntodo
    994      1.15       chs 						* sizeof(SGE_SIMPLE32));
    995       1.1   thorpej 				}
    996      1.15       chs 				ce->Address = htole32(req->req_pbuf +
    997      1.15       chs 				    ((char *)se - (char *)mpt_req));
    998       1.1   thorpej 				ce->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT;
    999       1.1   thorpej 				for (i = 0; i < ntodo; i++, se++, seg++) {
   1000       1.1   thorpej 					uint32_t tf;
   1001       1.1   thorpej 
   1002       1.1   thorpej 					memset(se, 0, sizeof(*se));
   1003      1.15       chs 					se->Address = htole32(
   1004      1.15       chs 					    req->dmap->dm_segs[seg].ds_addr);
   1005       1.1   thorpej 					MPI_pSGE_SET_LENGTH(se,
   1006       1.1   thorpej 					    req->dmap->dm_segs[seg].ds_len);
   1007       1.1   thorpej 					tf = flags;
   1008       1.1   thorpej 					if (i == ntodo - 1) {
   1009       1.1   thorpej 						tf |=
   1010       1.1   thorpej 						    MPI_SGE_FLAGS_LAST_ELEMENT;
   1011       1.1   thorpej 						if (ce->NextChainOffset == 0) {
   1012       1.1   thorpej 							tf |=
   1013       1.1   thorpej 						    MPI_SGE_FLAGS_END_OF_LIST |
   1014       1.1   thorpej 						    MPI_SGE_FLAGS_END_OF_BUFFER;
   1015       1.1   thorpej 						}
   1016       1.1   thorpej 					}
   1017       1.1   thorpej 					MPI_pSGE_SET_FLAGS(se, tf);
   1018      1.15       chs 					se->FlagsLength =
   1019      1.15       chs 					    htole32(se->FlagsLength);
   1020       1.1   thorpej 					nleft--;
   1021       1.1   thorpej 				}
   1022       1.1   thorpej 			}
   1023       1.1   thorpej 			bus_dmamap_sync(mpt->sc_dmat, req->dmap, 0,
   1024       1.1   thorpej 			    req->dmap->dm_mapsize,
   1025       1.1   thorpej 			    (xs->xs_control & XS_CTL_DATA_IN) ?
   1026       1.1   thorpej 			    				BUS_DMASYNC_PREREAD
   1027       1.1   thorpej 						      : BUS_DMASYNC_PREWRITE);
   1028       1.1   thorpej 		} else {
   1029       1.1   thorpej 			int i;
   1030       1.1   thorpej 			uint32_t flags;
   1031       1.1   thorpej 
   1032       1.1   thorpej 			flags = MPI_SGE_FLAGS_SIMPLE_ELEMENT;
   1033       1.1   thorpej 			if (xs->xs_control & XS_CTL_DATA_OUT)
   1034       1.1   thorpej 				flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
   1035       1.1   thorpej 
   1036       1.1   thorpej 			/* Copy the segments into our SG list. */
   1037       1.1   thorpej 			se = (SGE_SIMPLE32 *) &mpt_req->SGL;
   1038       1.1   thorpej 			for (i = 0; i < req->dmap->dm_nsegs;
   1039       1.1   thorpej 			     i++, se++) {
   1040       1.1   thorpej 				uint32_t tf;
   1041       1.1   thorpej 
   1042       1.1   thorpej 				memset(se, 0, sizeof(*se));
   1043      1.15       chs 				se->Address =
   1044      1.15       chs 				    htole32(req->dmap->dm_segs[i].ds_addr);
   1045       1.1   thorpej 				MPI_pSGE_SET_LENGTH(se,
   1046       1.1   thorpej 				    req->dmap->dm_segs[i].ds_len);
   1047       1.1   thorpej 				tf = flags;
   1048       1.1   thorpej 				if (i == req->dmap->dm_nsegs - 1) {
   1049       1.1   thorpej 					tf |=
   1050       1.1   thorpej 					    MPI_SGE_FLAGS_LAST_ELEMENT |
   1051       1.1   thorpej 					    MPI_SGE_FLAGS_END_OF_BUFFER |
   1052       1.1   thorpej 					    MPI_SGE_FLAGS_END_OF_LIST;
   1053       1.1   thorpej 				}
   1054       1.1   thorpej 				MPI_pSGE_SET_FLAGS(se, tf);
   1055      1.15       chs 				se->FlagsLength = htole32(se->FlagsLength);
   1056       1.1   thorpej 			}
   1057       1.1   thorpej 			bus_dmamap_sync(mpt->sc_dmat, req->dmap, 0,
   1058       1.1   thorpej 			    req->dmap->dm_mapsize,
   1059       1.1   thorpej 			    (xs->xs_control & XS_CTL_DATA_IN) ?
   1060       1.1   thorpej 			    				BUS_DMASYNC_PREREAD
   1061       1.1   thorpej 						      : BUS_DMASYNC_PREWRITE);
   1062       1.1   thorpej 		}
   1063       1.1   thorpej 	} else {
   1064       1.1   thorpej 		/*
   1065       1.1   thorpej 		 * No data to transfer; just make a single simple SGL
   1066       1.1   thorpej 		 * with zero length.
   1067       1.1   thorpej 		 */
   1068       1.1   thorpej 		SGE_SIMPLE32 *se = (SGE_SIMPLE32 *) &mpt_req->SGL;
   1069       1.1   thorpej 		memset(se, 0, sizeof(*se));
   1070       1.1   thorpej 		MPI_pSGE_SET_FLAGS(se,
   1071       1.1   thorpej 		    (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
   1072       1.1   thorpej 		     MPI_SGE_FLAGS_SIMPLE_ELEMENT | MPI_SGE_FLAGS_END_OF_LIST));
   1073      1.15       chs 		se->FlagsLength = htole32(se->FlagsLength);
   1074       1.1   thorpej 	}
   1075       1.1   thorpej 
   1076       1.1   thorpej 	if (mpt->verbose > 1)
   1077       1.1   thorpej 		mpt_print_scsi_io_request(mpt_req);
   1078       1.1   thorpej 
   1079  1.16.2.3      yamt 		if (xs->timeout == 0) {
   1080  1.16.2.3      yamt 			mpt_prt(mpt, "mpt_run_xfer: no timeout specified for request: 0x%x\n",
   1081  1.16.2.3      yamt 			req->index);
   1082  1.16.2.3      yamt 			xs->timeout = 500;
   1083  1.16.2.3      yamt 		}
   1084  1.16.2.3      yamt 
   1085       1.1   thorpej 	s = splbio();
   1086       1.1   thorpej 	if (__predict_true((xs->xs_control & XS_CTL_POLL) == 0))
   1087       1.1   thorpej 		callout_reset(&xs->xs_callout,
   1088       1.1   thorpej 		    mstohz(xs->timeout), mpt_timeout, req);
   1089       1.1   thorpej 	mpt_send_cmd(mpt, req);
   1090       1.1   thorpej 	splx(s);
   1091       1.1   thorpej 
   1092       1.1   thorpej 	if (__predict_true((xs->xs_control & XS_CTL_POLL) == 0))
   1093       1.1   thorpej 		return;
   1094       1.1   thorpej 
   1095       1.1   thorpej 	/*
   1096       1.1   thorpej 	 * If we can't use interrupts, poll on completion.
   1097       1.1   thorpej 	 */
   1098       1.4   thorpej 	if (mpt_poll(mpt, xs, xs->timeout))
   1099       1.1   thorpej 		mpt_timeout(req);
   1100       1.1   thorpej }
   1101       1.1   thorpej 
   1102       1.1   thorpej static void
   1103       1.1   thorpej mpt_set_xfer_mode(mpt_softc_t *mpt, struct scsipi_xfer_mode *xm)
   1104       1.1   thorpej {
   1105       1.1   thorpej 	fCONFIG_PAGE_SCSI_DEVICE_1 tmp;
   1106       1.1   thorpej 
   1107       1.1   thorpej 	/*
   1108       1.1   thorpej 	 * Always allow disconnect; we don't have a way to disable
   1109       1.1   thorpej 	 * it right now, in any case.
   1110       1.1   thorpej 	 */
   1111       1.1   thorpej 	mpt->mpt_disc_enable |= (1 << xm->xm_target);
   1112       1.1   thorpej 
   1113       1.1   thorpej 	if (xm->xm_mode & PERIPH_CAP_TQING)
   1114       1.1   thorpej 		mpt->mpt_tag_enable |= (1 << xm->xm_target);
   1115       1.1   thorpej 	else
   1116       1.1   thorpej 		mpt->mpt_tag_enable &= ~(1 << xm->xm_target);
   1117       1.1   thorpej 
   1118  1.16.2.1      yamt 	if (mpt->is_scsi) {
   1119  1.16.2.1      yamt 		/*
   1120  1.16.2.1      yamt 		 * SCSI transport settings only make any sense for
   1121  1.16.2.1      yamt 		 * SCSI
   1122  1.16.2.1      yamt 		 */
   1123       1.1   thorpej 
   1124  1.16.2.1      yamt 		tmp = mpt->mpt_dev_page1[xm->xm_target];
   1125       1.1   thorpej 
   1126  1.16.2.1      yamt 		/*
   1127  1.16.2.1      yamt 		 * Set the wide/narrow parameter for the target.
   1128  1.16.2.1      yamt 		 */
   1129  1.16.2.1      yamt 		if (xm->xm_mode & PERIPH_CAP_WIDE16)
   1130  1.16.2.1      yamt 			tmp.RequestedParameters |= MPI_SCSIDEVPAGE1_RP_WIDE;
   1131  1.16.2.1      yamt 		else
   1132  1.16.2.1      yamt 			tmp.RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_WIDE;
   1133  1.16.2.1      yamt 
   1134  1.16.2.1      yamt 		/*
   1135  1.16.2.1      yamt 		 * Set the synchronous parameters for the target.
   1136  1.16.2.1      yamt 		 *
   1137  1.16.2.1      yamt 		 * XXX If we request sync transfers, we just go ahead and
   1138  1.16.2.1      yamt 		 * XXX request the maximum available.  We need finer control
   1139  1.16.2.1      yamt 		 * XXX in order to implement Domain Validation.
   1140  1.16.2.1      yamt 		 */
   1141  1.16.2.1      yamt 		tmp.RequestedParameters &= ~(MPI_SCSIDEVPAGE1_RP_MIN_SYNC_PERIOD_MASK |
   1142  1.16.2.1      yamt 		    MPI_SCSIDEVPAGE1_RP_MAX_SYNC_OFFSET_MASK |
   1143  1.16.2.1      yamt 		    MPI_SCSIDEVPAGE1_RP_DT | MPI_SCSIDEVPAGE1_RP_QAS |
   1144  1.16.2.1      yamt 		    MPI_SCSIDEVPAGE1_RP_IU);
   1145  1.16.2.1      yamt 		if (xm->xm_mode & PERIPH_CAP_SYNC) {
   1146  1.16.2.1      yamt 			int factor, offset, np;
   1147  1.16.2.1      yamt 
   1148  1.16.2.1      yamt 			factor = (mpt->mpt_port_page0.Capabilities >> 8) & 0xff;
   1149  1.16.2.1      yamt 			offset = (mpt->mpt_port_page0.Capabilities >> 16) & 0xff;
   1150  1.16.2.1      yamt 			np = 0;
   1151  1.16.2.1      yamt 			if (factor < 0x9) {
   1152  1.16.2.1      yamt 				/* Ultra320 */
   1153  1.16.2.1      yamt 				np |= MPI_SCSIDEVPAGE1_RP_QAS | MPI_SCSIDEVPAGE1_RP_IU;
   1154  1.16.2.1      yamt 			}
   1155  1.16.2.1      yamt 			if (factor < 0xa) {
   1156  1.16.2.1      yamt 				/* at least Ultra160 */
   1157  1.16.2.1      yamt 				np |= MPI_SCSIDEVPAGE1_RP_DT;
   1158  1.16.2.1      yamt 			}
   1159  1.16.2.1      yamt 			np |= (factor << 8) | (offset << 16);
   1160  1.16.2.1      yamt 			tmp.RequestedParameters |= np;
   1161       1.1   thorpej 		}
   1162       1.1   thorpej 
   1163  1.16.2.1      yamt 		host2mpt_config_page_scsi_device_1(&tmp);
   1164  1.16.2.1      yamt 		if (mpt_write_cfg_page(mpt, xm->xm_target, &tmp.Header)) {
   1165  1.16.2.1      yamt 			mpt_prt(mpt, "unable to write Device Page 1");
   1166  1.16.2.1      yamt 			return;
   1167  1.16.2.1      yamt 		}
   1168       1.1   thorpej 
   1169  1.16.2.1      yamt 		if (mpt_read_cfg_page(mpt, xm->xm_target, &tmp.Header)) {
   1170  1.16.2.1      yamt 			mpt_prt(mpt, "unable to read back Device Page 1");
   1171  1.16.2.1      yamt 			return;
   1172  1.16.2.1      yamt 		}
   1173       1.1   thorpej 
   1174  1.16.2.1      yamt 		mpt2host_config_page_scsi_device_1(&tmp);
   1175  1.16.2.1      yamt 		mpt->mpt_dev_page1[xm->xm_target] = tmp;
   1176  1.16.2.1      yamt 		if (mpt->verbose > 1) {
   1177  1.16.2.1      yamt 			mpt_prt(mpt,
   1178  1.16.2.1      yamt 			    "SPI Target %d Page 1: RequestedParameters %x Config %x",
   1179  1.16.2.1      yamt 			    xm->xm_target,
   1180  1.16.2.1      yamt 			    mpt->mpt_dev_page1[xm->xm_target].RequestedParameters,
   1181  1.16.2.1      yamt 			    mpt->mpt_dev_page1[xm->xm_target].Configuration);
   1182  1.16.2.1      yamt 		}
   1183       1.1   thorpej 	}
   1184       1.1   thorpej 
   1185       1.1   thorpej 	/*
   1186       1.1   thorpej 	 * Make a note that we should perform an async callback at the
   1187       1.1   thorpej 	 * end of the next successful command completion to report the
   1188       1.1   thorpej 	 * negotiated transfer mode.
   1189       1.1   thorpej 	 */
   1190       1.1   thorpej 	mpt->mpt_report_xfer_mode |= (1 << xm->xm_target);
   1191       1.1   thorpej }
   1192       1.1   thorpej 
   1193       1.1   thorpej static void
   1194       1.1   thorpej mpt_get_xfer_mode(mpt_softc_t *mpt, struct scsipi_periph *periph)
   1195       1.1   thorpej {
   1196       1.1   thorpej 	fCONFIG_PAGE_SCSI_DEVICE_0 tmp;
   1197       1.1   thorpej 	struct scsipi_xfer_mode xm;
   1198       1.1   thorpej 	int period, offset;
   1199       1.1   thorpej 
   1200       1.1   thorpej 	tmp = mpt->mpt_dev_page0[periph->periph_target];
   1201      1.15       chs 	host2mpt_config_page_scsi_device_0(&tmp);
   1202       1.1   thorpej 	if (mpt_read_cfg_page(mpt, periph->periph_target, &tmp.Header)) {
   1203       1.1   thorpej 		mpt_prt(mpt, "unable to read Device Page 0");
   1204       1.1   thorpej 		return;
   1205       1.1   thorpej 	}
   1206      1.15       chs 	mpt2host_config_page_scsi_device_0(&tmp);
   1207       1.1   thorpej 
   1208       1.1   thorpej 	if (mpt->verbose > 1) {
   1209       1.1   thorpej 		mpt_prt(mpt,
   1210       1.1   thorpej 		    "SPI Tgt %d Page 0: NParms %x Information %x",
   1211       1.1   thorpej 		    periph->periph_target,
   1212       1.1   thorpej 		    tmp.NegotiatedParameters, tmp.Information);
   1213       1.1   thorpej 	}
   1214       1.1   thorpej 
   1215       1.1   thorpej 	xm.xm_target = periph->periph_target;
   1216       1.1   thorpej 	xm.xm_mode = 0;
   1217       1.1   thorpej 
   1218       1.1   thorpej 	if (tmp.NegotiatedParameters & MPI_SCSIDEVPAGE0_NP_WIDE)
   1219       1.1   thorpej 		xm.xm_mode |= PERIPH_CAP_WIDE16;
   1220       1.1   thorpej 
   1221       1.1   thorpej 	period = (tmp.NegotiatedParameters >> 8) & 0xff;
   1222       1.1   thorpej 	offset = (tmp.NegotiatedParameters >> 16) & 0xff;
   1223       1.1   thorpej 	if (offset) {
   1224       1.1   thorpej 		xm.xm_period = period;
   1225       1.1   thorpej 		xm.xm_offset = offset;
   1226       1.1   thorpej 		xm.xm_mode |= PERIPH_CAP_SYNC;
   1227       1.1   thorpej 	}
   1228       1.1   thorpej 
   1229       1.1   thorpej 	/*
   1230       1.1   thorpej 	 * Tagged queueing is all controlled by us; there is no
   1231       1.1   thorpej 	 * other setting to query.
   1232       1.1   thorpej 	 */
   1233       1.1   thorpej 	if (mpt->mpt_tag_enable & (1 << periph->periph_target))
   1234       1.1   thorpej 		xm.xm_mode |= PERIPH_CAP_TQING;
   1235       1.1   thorpej 
   1236       1.1   thorpej 	/*
   1237       1.1   thorpej 	 * We're going to deliver the async event, so clear the marker.
   1238       1.1   thorpej 	 */
   1239       1.1   thorpej 	mpt->mpt_report_xfer_mode &= ~(1 << periph->periph_target);
   1240       1.1   thorpej 
   1241       1.1   thorpej 	scsipi_async_event(&mpt->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
   1242       1.1   thorpej }
   1243       1.1   thorpej 
   1244       1.1   thorpej static void
   1245       1.1   thorpej mpt_ctlop(mpt_softc_t *mpt, void *vmsg, uint32_t reply)
   1246       1.1   thorpej {
   1247       1.1   thorpej 	MSG_DEFAULT_REPLY *dmsg = vmsg;
   1248       1.1   thorpej 
   1249       1.1   thorpej 	switch (dmsg->Function) {
   1250       1.1   thorpej 	case MPI_FUNCTION_EVENT_NOTIFICATION:
   1251       1.1   thorpej 		mpt_event_notify_reply(mpt, vmsg);
   1252       1.1   thorpej 		mpt_free_reply(mpt, (reply << 1));
   1253       1.1   thorpej 		break;
   1254       1.1   thorpej 
   1255       1.1   thorpej 	case MPI_FUNCTION_EVENT_ACK:
   1256       1.1   thorpej 		mpt_free_reply(mpt, (reply << 1));
   1257       1.1   thorpej 		break;
   1258       1.1   thorpej 
   1259       1.1   thorpej 	case MPI_FUNCTION_PORT_ENABLE:
   1260       1.1   thorpej 	    {
   1261       1.1   thorpej 		MSG_PORT_ENABLE_REPLY *msg = vmsg;
   1262      1.15       chs 		int index = le32toh(msg->MsgContext) & ~0x80000000;
   1263       1.1   thorpej 		if (mpt->verbose > 1)
   1264       1.1   thorpej 			mpt_prt(mpt, "enable port reply index %d", index);
   1265       1.1   thorpej 		if (index >= 0 && index < MPT_MAX_REQUESTS(mpt)) {
   1266       1.1   thorpej 			request_t *req = &mpt->request_pool[index];
   1267       1.1   thorpej 			req->debug = REQ_DONE;
   1268       1.1   thorpej 		}
   1269       1.1   thorpej 		mpt_free_reply(mpt, (reply << 1));
   1270       1.1   thorpej 		break;
   1271       1.1   thorpej 	    }
   1272       1.1   thorpej 
   1273       1.1   thorpej 	case MPI_FUNCTION_CONFIG:
   1274       1.1   thorpej 	    {
   1275       1.1   thorpej 		MSG_CONFIG_REPLY *msg = vmsg;
   1276      1.15       chs 		int index = le32toh(msg->MsgContext) & ~0x80000000;
   1277       1.1   thorpej 		if (index >= 0 && index < MPT_MAX_REQUESTS(mpt)) {
   1278       1.1   thorpej 			request_t *req = &mpt->request_pool[index];
   1279       1.1   thorpej 			req->debug = REQ_DONE;
   1280       1.1   thorpej 			req->sequence = reply;
   1281       1.1   thorpej 		} else
   1282       1.1   thorpej 			mpt_free_reply(mpt, (reply << 1));
   1283       1.1   thorpej 		break;
   1284       1.1   thorpej 	    }
   1285       1.1   thorpej 
   1286       1.1   thorpej 	default:
   1287       1.1   thorpej 		mpt_prt(mpt, "unknown ctlop: 0x%x", dmsg->Function);
   1288       1.1   thorpej 	}
   1289       1.1   thorpej }
   1290       1.1   thorpej 
   1291       1.1   thorpej static void
   1292       1.1   thorpej mpt_event_notify_reply(mpt_softc_t *mpt, MSG_EVENT_NOTIFY_REPLY *msg)
   1293       1.1   thorpej {
   1294       1.1   thorpej 
   1295      1.15       chs 	switch (le32toh(msg->Event)) {
   1296       1.1   thorpej 	case MPI_EVENT_LOG_DATA:
   1297       1.1   thorpej 	    {
   1298       1.1   thorpej 		int i;
   1299       1.1   thorpej 
   1300       1.1   thorpej 		/* Some error occurrerd that the Fusion wants logged. */
   1301       1.1   thorpej 		mpt_prt(mpt, "EvtLogData: IOCLogInfo: 0x%08x", msg->IOCLogInfo);
   1302       1.1   thorpej 		mpt_prt(mpt, "EvtLogData: Event Data:");
   1303       1.1   thorpej 		for (i = 0; i < msg->EventDataLength; i++) {
   1304       1.1   thorpej 			if ((i % 4) == 0)
   1305  1.16.2.1      yamt 				printf("%s:\t", device_xname(mpt->sc_dev));
   1306       1.1   thorpej 			printf("0x%08x%c", msg->Data[i],
   1307       1.1   thorpej 			    ((i % 4) == 3) ? '\n' : ' ');
   1308       1.1   thorpej 		}
   1309       1.1   thorpej 		if ((i % 4) != 0)
   1310       1.1   thorpej 			printf("\n");
   1311       1.1   thorpej 		break;
   1312       1.1   thorpej 	    }
   1313       1.1   thorpej 
   1314       1.1   thorpej 	case MPI_EVENT_UNIT_ATTENTION:
   1315       1.1   thorpej 		mpt_prt(mpt, "Unit Attn: Bus 0x%02x Target 0x%02x",
   1316       1.1   thorpej 		    (msg->Data[0] >> 8) & 0xff, msg->Data[0] & 0xff);
   1317       1.1   thorpej 		break;
   1318       1.1   thorpej 
   1319       1.1   thorpej 	case MPI_EVENT_IOC_BUS_RESET:
   1320       1.1   thorpej 		/* We generated a bus reset. */
   1321       1.1   thorpej 		mpt_prt(mpt, "IOC Bus Reset Port %d",
   1322       1.1   thorpej 		    (msg->Data[0] >> 8) & 0xff);
   1323       1.1   thorpej 		break;
   1324       1.1   thorpej 
   1325       1.1   thorpej 	case MPI_EVENT_EXT_BUS_RESET:
   1326       1.1   thorpej 		/* Someone else generated a bus reset. */
   1327       1.1   thorpej 		mpt_prt(mpt, "External Bus Reset");
   1328       1.1   thorpej 		/*
   1329       1.1   thorpej 		 * These replies don't return EventData like the MPI
   1330       1.1   thorpej 		 * spec says they do.
   1331       1.1   thorpej 		 */
   1332       1.1   thorpej 		/* XXX Send an async event? */
   1333       1.1   thorpej 		break;
   1334       1.1   thorpej 
   1335       1.1   thorpej 	case MPI_EVENT_RESCAN:
   1336       1.1   thorpej 		/*
   1337       1.1   thorpej 		 * In general, thise means a device has been added
   1338       1.1   thorpej 		 * to the loop.
   1339       1.1   thorpej 		 */
   1340       1.1   thorpej 		mpt_prt(mpt, "Rescan Port %d", (msg->Data[0] >> 8) & 0xff);
   1341       1.1   thorpej 		/* XXX Send an async event? */
   1342       1.1   thorpej 		break;
   1343       1.1   thorpej 
   1344       1.1   thorpej 	case MPI_EVENT_LINK_STATUS_CHANGE:
   1345       1.1   thorpej 		mpt_prt(mpt, "Port %d: Link state %s",
   1346       1.1   thorpej 		    (msg->Data[1] >> 8) & 0xff,
   1347       1.1   thorpej 		    (msg->Data[0] & 0xff) == 0 ? "Failed" : "Active");
   1348       1.1   thorpej 		break;
   1349       1.1   thorpej 
   1350       1.1   thorpej 	case MPI_EVENT_LOOP_STATE_CHANGE:
   1351       1.1   thorpej 		switch ((msg->Data[0] >> 16) & 0xff) {
   1352       1.1   thorpej 		case 0x01:
   1353       1.1   thorpej 			mpt_prt(mpt,
   1354       1.1   thorpej 			    "Port %d: FC Link Event: LIP(%02x,%02x) "
   1355       1.1   thorpej 			    "(Loop Initialization)",
   1356       1.1   thorpej 			    (msg->Data[1] >> 8) & 0xff,
   1357       1.1   thorpej 			    (msg->Data[0] >> 8) & 0xff,
   1358       1.1   thorpej 			    (msg->Data[0]     ) & 0xff);
   1359       1.1   thorpej 			switch ((msg->Data[0] >> 8) & 0xff) {
   1360       1.1   thorpej 			case 0xf7:
   1361       1.1   thorpej 				if ((msg->Data[0] & 0xff) == 0xf7)
   1362       1.1   thorpej 					mpt_prt(mpt, "\tDevice needs AL_PA");
   1363       1.1   thorpej 				else
   1364       1.1   thorpej 					mpt_prt(mpt, "\tDevice %02x doesn't "
   1365       1.1   thorpej 					    "like FC performance",
   1366       1.1   thorpej 					    msg->Data[0] & 0xff);
   1367       1.1   thorpej 				break;
   1368       1.1   thorpej 
   1369       1.1   thorpej 			case 0xf8:
   1370       1.1   thorpej 				if ((msg->Data[0] & 0xff) == 0xf7)
   1371       1.1   thorpej 					mpt_prt(mpt, "\tDevice detected loop "
   1372       1.1   thorpej 					    "failure before acquiring AL_PA");
   1373       1.1   thorpej 				else
   1374       1.1   thorpej 					mpt_prt(mpt, "\tDevice %02x detected "
   1375       1.1   thorpej 					    "loop failure",
   1376       1.1   thorpej 					    msg->Data[0] & 0xff);
   1377       1.1   thorpej 				break;
   1378       1.1   thorpej 
   1379       1.1   thorpej 			default:
   1380       1.1   thorpej 				mpt_prt(mpt, "\tDevice %02x requests that "
   1381       1.1   thorpej 				    "device %02x reset itself",
   1382       1.1   thorpej 				    msg->Data[0] & 0xff,
   1383       1.1   thorpej 				    (msg->Data[0] >> 8) & 0xff);
   1384       1.1   thorpej 				break;
   1385       1.1   thorpej 			}
   1386       1.1   thorpej 			break;
   1387       1.1   thorpej 
   1388       1.1   thorpej 		case 0x02:
   1389       1.1   thorpej 			mpt_prt(mpt, "Port %d: FC Link Event: LPE(%02x,%02x) "
   1390       1.1   thorpej 			    "(Loop Port Enable)",
   1391       1.1   thorpej 			    (msg->Data[1] >> 8) & 0xff,
   1392       1.1   thorpej 			    (msg->Data[0] >> 8) & 0xff,
   1393       1.1   thorpej 			    (msg->Data[0]     ) & 0xff);
   1394       1.1   thorpej 			break;
   1395       1.1   thorpej 
   1396       1.1   thorpej 		case 0x03:
   1397       1.1   thorpej 			mpt_prt(mpt, "Port %d: FC Link Event: LPB(%02x,%02x) "
   1398       1.1   thorpej 			    "(Loop Port Bypass)",
   1399       1.1   thorpej 			    (msg->Data[1] >> 8) & 0xff,
   1400       1.1   thorpej 			    (msg->Data[0] >> 8) & 0xff,
   1401       1.1   thorpej 			    (msg->Data[0]     ) & 0xff);
   1402       1.1   thorpej 			break;
   1403       1.1   thorpej 
   1404       1.1   thorpej 		default:
   1405       1.1   thorpej 			mpt_prt(mpt, "Port %d: FC Link Event: "
   1406       1.1   thorpej 			    "Unknown event (%02x %02x %02x)",
   1407       1.1   thorpej 			    (msg->Data[1] >>  8) & 0xff,
   1408       1.1   thorpej 			    (msg->Data[0] >> 16) & 0xff,
   1409       1.1   thorpej 			    (msg->Data[0] >>  8) & 0xff,
   1410       1.1   thorpej 			    (msg->Data[0]      ) & 0xff);
   1411       1.1   thorpej 			break;
   1412       1.1   thorpej 		}
   1413       1.1   thorpej 		break;
   1414       1.1   thorpej 
   1415       1.1   thorpej 	case MPI_EVENT_LOGOUT:
   1416       1.1   thorpej 		mpt_prt(mpt, "Port %d: FC Logout: N_PortID: %02x",
   1417       1.1   thorpej 		    (msg->Data[1] >> 8) & 0xff, msg->Data[0]);
   1418       1.1   thorpej 		break;
   1419       1.1   thorpej 
   1420       1.1   thorpej 	case MPI_EVENT_EVENT_CHANGE:
   1421       1.1   thorpej 		/*
   1422       1.1   thorpej 		 * This is just an acknowledgement of our
   1423       1.1   thorpej 		 * mpt_send_event_request().
   1424       1.1   thorpej 		 */
   1425       1.1   thorpej 		break;
   1426       1.1   thorpej 
   1427      1.12      tron 	case MPI_EVENT_SAS_PHY_LINK_STATUS:
   1428      1.13      tron 		switch ((msg->Data[0] >> 12) & 0x0f) {
   1429      1.12      tron 		case 0x00:
   1430      1.12      tron 			mpt_prt(mpt, "Phy %d: Link Status Unknown",
   1431      1.12      tron 			    msg->Data[0] & 0xff);
   1432      1.12      tron 			break;
   1433      1.12      tron 		case 0x01:
   1434      1.12      tron 			mpt_prt(mpt, "Phy %d: Link Disabled",
   1435      1.12      tron 			    msg->Data[0] & 0xff);
   1436      1.12      tron 			break;
   1437      1.12      tron 		case 0x02:
   1438      1.12      tron 			mpt_prt(mpt, "Phy %d: Failed Speed Negotiation",
   1439      1.12      tron 			    msg->Data[0] & 0xff);
   1440      1.12      tron 			break;
   1441      1.12      tron 		case 0x03:
   1442      1.12      tron 			mpt_prt(mpt, "Phy %d: SATA OOB Complete",
   1443      1.12      tron 			    msg->Data[0] & 0xff);
   1444      1.12      tron 			break;
   1445      1.12      tron 		case 0x08:
   1446      1.12      tron 			mpt_prt(mpt, "Phy %d: Link Rate 1.5 Gbps",
   1447      1.12      tron 			    msg->Data[0] & 0xff);
   1448      1.12      tron 			break;
   1449      1.12      tron 		case 0x09:
   1450      1.12      tron 			mpt_prt(mpt, "Phy %d: Link Rate 3.0 Gbps",
   1451      1.12      tron 			    msg->Data[0] & 0xff);
   1452      1.12      tron 			break;
   1453      1.13      tron 		default:
   1454      1.12      tron 			mpt_prt(mpt, "Phy %d: SAS Phy Link Status Event: "
   1455      1.12      tron 			    "Unknown event (%0x)",
   1456      1.12      tron 			    msg->Data[0] & 0xff, (msg->Data[0] >> 8) & 0xff);
   1457      1.12      tron 		}
   1458      1.12      tron 		break;
   1459      1.12      tron 
   1460      1.12      tron 	case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
   1461      1.12      tron 	case MPI_EVENT_SAS_DISCOVERY:
   1462      1.12      tron 		/* ignore these events for now */
   1463      1.12      tron 		break;
   1464      1.12      tron 
   1465  1.16.2.1      yamt 	case MPI_EVENT_QUEUE_FULL:
   1466  1.16.2.1      yamt 		/* This can get a little chatty */
   1467  1.16.2.1      yamt 		if (mpt->verbose > 0)
   1468  1.16.2.1      yamt 			mpt_prt(mpt, "Queue Full Event");
   1469  1.16.2.1      yamt 		break;
   1470  1.16.2.1      yamt 
   1471       1.1   thorpej 	default:
   1472       1.1   thorpej 		mpt_prt(mpt, "Unknown async event: 0x%x", msg->Event);
   1473       1.1   thorpej 		break;
   1474       1.1   thorpej 	}
   1475       1.9     perry 
   1476       1.1   thorpej 	if (msg->AckRequired) {
   1477       1.1   thorpej 		MSG_EVENT_ACK *ackp;
   1478       1.1   thorpej 		request_t *req;
   1479       1.1   thorpej 
   1480       1.1   thorpej 		if ((req = mpt_get_request(mpt)) == NULL) {
   1481       1.1   thorpej 			/* XXX XXX XXX XXXJRT */
   1482       1.1   thorpej 			panic("mpt_event_notify_reply: unable to allocate "
   1483       1.1   thorpej 			    "request structure");
   1484       1.1   thorpej 		}
   1485       1.1   thorpej 
   1486       1.1   thorpej 		ackp = (MSG_EVENT_ACK *) req->req_vbuf;
   1487       1.1   thorpej 		memset(ackp, 0, sizeof(*ackp));
   1488       1.1   thorpej 		ackp->Function = MPI_FUNCTION_EVENT_ACK;
   1489       1.1   thorpej 		ackp->Event = msg->Event;
   1490       1.1   thorpej 		ackp->EventContext = msg->EventContext;
   1491      1.15       chs 		ackp->MsgContext = htole32(req->index | 0x80000000);
   1492       1.1   thorpej 		mpt_check_doorbell(mpt);
   1493       1.1   thorpej 		mpt_send_cmd(mpt, req);
   1494       1.1   thorpej 	}
   1495       1.1   thorpej }
   1496       1.1   thorpej 
   1497  1.16.2.3      yamt static void
   1498  1.16.2.3      yamt mpt_bus_reset(mpt_softc_t *mpt)
   1499  1.16.2.3      yamt {
   1500  1.16.2.3      yamt 	request_t *req;
   1501  1.16.2.3      yamt 	MSG_SCSI_TASK_MGMT *mngt_req;
   1502  1.16.2.3      yamt 	int s;
   1503  1.16.2.3      yamt 
   1504  1.16.2.3      yamt 	s = splbio();
   1505  1.16.2.3      yamt 	if (mpt->mngt_req) {
   1506  1.16.2.3      yamt 		/* request already queued; can't do more */
   1507  1.16.2.3      yamt 		splx(s);
   1508  1.16.2.3      yamt 		return;
   1509  1.16.2.3      yamt 	}
   1510  1.16.2.3      yamt 	req = mpt_get_request(mpt);
   1511  1.16.2.3      yamt 	if (__predict_false(req == NULL)) {
   1512  1.16.2.3      yamt 		mpt_prt(mpt, "no mngt request\n");
   1513  1.16.2.3      yamt 		splx(s);
   1514  1.16.2.3      yamt 		return;
   1515  1.16.2.3      yamt 	}
   1516  1.16.2.3      yamt 	mpt->mngt_req = req;
   1517  1.16.2.3      yamt 	splx(s);
   1518  1.16.2.3      yamt 	mngt_req = req->req_vbuf;
   1519  1.16.2.3      yamt 	memset(mngt_req, 0, sizeof(*mngt_req));
   1520  1.16.2.3      yamt 	mngt_req->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
   1521  1.16.2.3      yamt 	mngt_req->Bus = mpt->bus;
   1522  1.16.2.3      yamt 	mngt_req->TargetID = 0;
   1523  1.16.2.3      yamt 	mngt_req->ChainOffset = 0;
   1524  1.16.2.3      yamt 	mngt_req->TaskType = MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS;
   1525  1.16.2.3      yamt 	mngt_req->Reserved1 = 0;
   1526  1.16.2.3      yamt 	mngt_req->MsgFlags =
   1527  1.16.2.3      yamt 	    mpt->is_fc ? MPI_SCSITASKMGMT_MSGFLAGS_LIP_RESET_OPTION : 0;
   1528  1.16.2.3      yamt 	mngt_req->MsgContext = req->index;
   1529  1.16.2.3      yamt 	mngt_req->TaskMsgContext = 0;
   1530  1.16.2.3      yamt 	s = splbio();
   1531  1.16.2.3      yamt 	mpt_send_handshake_cmd(mpt, sizeof(*mngt_req), mngt_req);
   1532  1.16.2.3      yamt 	splx(s);
   1533  1.16.2.3      yamt }
   1534       1.1   thorpej 
   1535       1.1   thorpej /*****************************************************************************
   1536       1.1   thorpej  * SCSI interface routines
   1537       1.1   thorpej  *****************************************************************************/
   1538       1.1   thorpej 
   1539       1.1   thorpej static void
   1540       1.1   thorpej mpt_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
   1541       1.1   thorpej     void *arg)
   1542       1.1   thorpej {
   1543       1.1   thorpej 	struct scsipi_adapter *adapt = chan->chan_adapter;
   1544  1.16.2.1      yamt 	mpt_softc_t *mpt = DEV_TO_MPT(adapt->adapt_dev);
   1545       1.1   thorpej 
   1546       1.1   thorpej 	switch (req) {
   1547       1.1   thorpej 	case ADAPTER_REQ_RUN_XFER:
   1548       1.1   thorpej 		mpt_run_xfer(mpt, (struct scsipi_xfer *) arg);
   1549       1.1   thorpej 		return;
   1550       1.1   thorpej 
   1551       1.1   thorpej 	case ADAPTER_REQ_GROW_RESOURCES:
   1552       1.1   thorpej 		/* Not supported. */
   1553       1.1   thorpej 		return;
   1554       1.1   thorpej 
   1555       1.1   thorpej 	case ADAPTER_REQ_SET_XFER_MODE:
   1556       1.1   thorpej 		mpt_set_xfer_mode(mpt, (struct scsipi_xfer_mode *) arg);
   1557       1.1   thorpej 		return;
   1558       1.1   thorpej 	}
   1559       1.1   thorpej }
   1560       1.1   thorpej 
   1561       1.1   thorpej static void
   1562       1.1   thorpej mpt_minphys(struct buf *bp)
   1563       1.1   thorpej {
   1564       1.1   thorpej 
   1565       1.1   thorpej /*
   1566       1.1   thorpej  * Subtract one from the SGL limit, since we need an extra one to handle
   1567       1.1   thorpej  * an non-page-aligned transfer.
   1568       1.1   thorpej  */
   1569       1.1   thorpej #define	MPT_MAX_XFER	((MPT_SGL_MAX - 1) * PAGE_SIZE)
   1570       1.1   thorpej 
   1571       1.1   thorpej 	if (bp->b_bcount > MPT_MAX_XFER)
   1572       1.1   thorpej 		bp->b_bcount = MPT_MAX_XFER;
   1573       1.1   thorpej 	minphys(bp);
   1574       1.1   thorpej }
   1575  1.16.2.3      yamt 
   1576  1.16.2.3      yamt static int
   1577  1.16.2.3      yamt mpt_ioctl(struct scsipi_channel *chan, u_long cmd, void *arg,
   1578  1.16.2.3      yamt     int flag, struct proc *p)
   1579  1.16.2.3      yamt {
   1580  1.16.2.3      yamt 	mpt_softc_t *mpt;
   1581  1.16.2.3      yamt 	int s;
   1582  1.16.2.3      yamt 
   1583  1.16.2.3      yamt 	mpt = device_private(chan->chan_adapter->adapt_dev);
   1584  1.16.2.3      yamt 	switch (cmd) {
   1585  1.16.2.3      yamt 	case SCBUSIORESET:
   1586  1.16.2.3      yamt 		mpt_bus_reset(mpt);
   1587  1.16.2.3      yamt 		s = splbio();
   1588  1.16.2.3      yamt 		mpt_intr(mpt);
   1589  1.16.2.3      yamt 		splx(s);
   1590  1.16.2.3      yamt 		return(0);
   1591  1.16.2.3      yamt 	default:
   1592  1.16.2.3      yamt 		return (ENOTTY);
   1593  1.16.2.3      yamt 	}
   1594  1.16.2.3      yamt }
   1595