Home | History | Annotate | Line # | Download | only in pci
ld_twa.c revision 1.6
      1 /*	$wasabi: ld_twa.c,v 1.9 2006/02/14 18:44:37 jordanr Exp $	*/
      2 /*	$NetBSD: ld_twa.c,v 1.6 2007/07/21 19:06:21 ad Exp $ */
      3 
      4 /*-
      5  * Copyright (c) 2000, 2001, 2002, 2003, 2004 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Andrew Doran, and by Jason R. Thorpe and Jordan Rhody of Wasabi
     10  * Systems, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * 3ware "Apache" RAID controller front-end for ld(4) driver.
     43  */
     44 
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: ld_twa.c,v 1.6 2007/07/21 19:06:21 ad Exp $");
     47 
     48 #include "rnd.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/kernel.h>
     53 #include <sys/device.h>
     54 #include <sys/buf.h>
     55 #include <sys/bufq.h>
     56 #include <sys/endian.h>
     57 #include <sys/dkio.h>
     58 #include <sys/disk.h>
     59 #include <sys/proc.h>
     60 #if NRND > 0
     61 #include <sys/rnd.h>
     62 #endif
     63 
     64 #include <machine/bus.h>
     65 
     66 #include <uvm/uvm_extern.h>
     67 
     68 #include <dev/ldvar.h>
     69 
     70 #include <dev/scsipi/scsipi_all.h>
     71 #include <dev/scsipi/scsipi_disk.h>
     72 #include <dev/scsipi/scsipiconf.h>
     73 #include <dev/scsipi/scsi_disk.h>
     74 
     75 
     76 #include <dev/pci/pcireg.h>
     77 #include <dev/pci/pcivar.h>
     78 #include <dev/pci/twareg.h>
     79 #include <dev/pci/twavar.h>
     80 
     81 struct ld_twa_softc {
     82 	struct	ld_softc sc_ld;
     83 	int	sc_hwunit;
     84 };
     85 
     86 static void	ld_twa_attach(struct device *, struct device *, void *);
     87 static int	ld_twa_detach(struct device *, int);
     88 static int	ld_twa_dobio(struct ld_twa_softc *, void *, size_t, daddr_t,
     89 			     struct buf *);
     90 static int	ld_twa_dump(struct ld_softc *, void *, int, int);
     91 static int	ld_twa_flush(struct ld_softc *);
     92 static void	ld_twa_handler(struct twa_request *);
     93 static int	ld_twa_match(struct device *, struct cfdata *, void *);
     94 static int	ld_twa_start(struct ld_softc *, struct buf *);
     95 
     96 static void	ld_twa_adjqparam(struct device *, int);
     97 
     98 static int ld_twa_scsicmd(struct ld_twa_softc *,
     99 	struct twa_request *, struct buf *);
    100 
    101 CFATTACH_DECL(ld_twa, sizeof(struct ld_twa_softc),
    102     ld_twa_match, ld_twa_attach, ld_twa_detach, NULL);
    103 
    104 static const struct twa_callbacks ld_twa_callbacks = {
    105 	ld_twa_adjqparam,
    106 };
    107 
    108 static int
    109 ld_twa_match(struct device *parent, struct cfdata *match,
    110     void *aux)
    111 {
    112 
    113 	return (1);
    114 }
    115 
    116 static void
    117 ld_twa_attach(struct device *parent, struct device *self, void *aux)
    118 {
    119 	struct twa_attach_args *twa_args;
    120 	struct ld_twa_softc *sc;
    121 	struct ld_softc *ld;
    122 	struct twa_softc *twa;
    123 
    124 	sc = (struct ld_twa_softc *)self;
    125 	ld = &sc->sc_ld;
    126 	twa = (struct twa_softc *)parent;
    127 	twa_args = aux;
    128 
    129 	twa_register_callbacks(twa, twa_args->twaa_unit, &ld_twa_callbacks);
    130 
    131 	sc->sc_hwunit = twa_args->twaa_unit;
    132 	ld->sc_maxxfer = twa_get_maxxfer(twa_get_maxsegs());
    133 	ld->sc_secperunit = twa->sc_units[sc->sc_hwunit].td_size;
    134 	ld->sc_flags = LDF_ENABLED;
    135 	ld->sc_secsize = TWA_SECTOR_SIZE;
    136 	ld->sc_maxqueuecnt = twa->sc_openings;
    137 	ld->sc_start = ld_twa_start;
    138 	ld->sc_dump = ld_twa_dump;
    139 	ld->sc_flush = ld_twa_flush;
    140 	ldattach(ld);
    141 }
    142 
    143 static int
    144 ld_twa_detach(struct device *self, int flags)
    145 {
    146 	int error;
    147 
    148 	if ((error = ldbegindetach((struct ld_softc *)self, flags)) != 0)
    149 		return (error);
    150 	ldenddetach((struct ld_softc *)self);
    151 
    152 	return (0);
    153 }
    154 
    155 static int
    156 ld_twa_dobio(struct ld_twa_softc *sc, void *data, size_t datasize,
    157 	     daddr_t blkno, struct buf *bp)
    158 {
    159 	int rv;
    160 	struct twa_request	*tr;
    161 	struct twa_softc *twa;
    162 
    163 	twa = (struct twa_softc *)sc->sc_ld.sc_dv.dv_parent;
    164 
    165 	if ((tr = twa_get_request(twa, 0)) == NULL) {
    166 		return (EAGAIN);
    167 	}
    168 	if (bp->b_flags & B_READ) {
    169 		tr->tr_flags = TWA_CMD_DATA_OUT;
    170 	} else {
    171 		tr->tr_flags = TWA_CMD_DATA_IN;
    172 	}
    173 
    174 	tr->tr_data = data;
    175 	tr->tr_length = datasize;
    176 	tr->tr_cmd_pkt_type =
    177 		(TWA_CMD_PKT_TYPE_9K | TWA_CMD_PKT_TYPE_EXTERNAL);
    178 
    179 	tr->tr_command->cmd_hdr.header_desc.size_header = 128;
    180 
    181 	tr->tr_command->command.cmd_pkt_9k.command.opcode =
    182 		TWA_OP_EXECUTE_SCSI_COMMAND;
    183 	tr->tr_command->command.cmd_pkt_9k.unit =
    184 		sc->sc_hwunit;
    185 	tr->tr_command->command.cmd_pkt_9k.request_id =
    186 		tr->tr_request_id;
    187 	tr->tr_command->command.cmd_pkt_9k.status = 0;
    188 	tr->tr_command->command.cmd_pkt_9k.sgl_entries = 1;
    189 	tr->tr_command->command.cmd_pkt_9k.sgl_offset = 16;
    190 
    191 	/* offset from end of hdr = max cdb len */
    192 	ld_twa_scsicmd(sc, tr, bp);
    193 
    194 	tr->tr_callback = ld_twa_handler;
    195 	tr->tr_ld_sc = sc;
    196 
    197 	tr->bp = bp;
    198 
    199 	rv = twa_map_request(tr);
    200 
    201 	return (rv);
    202 }
    203 
    204 static int
    205 ld_twa_start(struct ld_softc *ld, struct buf *bp)
    206 {
    207 
    208 	return (ld_twa_dobio((struct ld_twa_softc *)ld, bp->b_data,
    209 	    bp->b_bcount, bp->b_rawblkno, bp));
    210 }
    211 
    212 static void
    213 ld_twa_handler(struct twa_request *tr)
    214 {
    215 	uint8_t	status;
    216 	struct buf *bp;
    217 	struct ld_twa_softc *sc;
    218 	struct twa_softc *twa;
    219 
    220 	bp = tr->bp;
    221 	sc = (struct ld_twa_softc *)tr->tr_ld_sc;
    222 	twa = (struct twa_softc *)sc->sc_ld.sc_dv.dv_parent;
    223 
    224 	status = tr->tr_command->command.cmd_pkt_9k.status;
    225 
    226 	if (status != 0) {
    227 		bp->b_flags |= B_ERROR;
    228 		bp->b_error = EIO;
    229 		bp->b_resid = bp->b_bcount;
    230 	} else {
    231 		bp->b_resid = 0;
    232 		bp->b_error = 0;
    233 	}
    234 	twa_release_request(tr);
    235 
    236 	lddone(&sc->sc_ld, bp);
    237 }
    238 
    239 static int
    240 ld_twa_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    241 {
    242 
    243 #if 0
    244 	/* XXX Unsafe right now. */
    245 	return (ld_twa_dobio((struct ld_twa_softc *)ld, data,
    246 	    blkcnt * ld->sc_secsize, blkno, NULL));
    247 #else
    248 	return EIO;
    249 #endif
    250 
    251 }
    252 
    253 
    254 static int
    255 ld_twa_flush(struct ld_softc *ld)
    256 {
    257 	int s, rv = 0;
    258 	struct twa_request *tr;
    259 	struct twa_softc *twa = (void *)ld->sc_dv.dv_parent;
    260 	struct ld_twa_softc *sc = (void *)ld;
    261 	struct twa_command_generic *generic_cmd;
    262 
    263 	/* Get a request packet. */
    264 	tr = twa_get_request_wait(twa, 0);
    265 	KASSERT(tr != NULL);
    266 
    267 	tr->tr_cmd_pkt_type =
    268 		(TWA_CMD_PKT_TYPE_9K | TWA_CMD_PKT_TYPE_EXTERNAL);
    269 
    270 	tr->tr_callback = twa_request_wait_handler;
    271 	tr->tr_ld_sc = sc;
    272 
    273 	tr->tr_command->cmd_hdr.header_desc.size_header = 128;
    274 
    275 	generic_cmd = &(tr->tr_command->command.cmd_pkt_7k.generic);
    276 	generic_cmd->opcode = TWA_OP_FLUSH;
    277 	generic_cmd->size = 2;
    278 	generic_cmd->unit = sc->sc_hwunit;
    279 	generic_cmd->request_id = tr->tr_request_id;
    280 	generic_cmd->sgl_offset = 0;
    281 	generic_cmd->host_id = 0;
    282 	generic_cmd->status = 0;
    283 	generic_cmd->flags = 0;
    284 	generic_cmd->count = 0;
    285 	rv = twa_map_request(tr);
    286 	s = splbio();
    287 	while (tr->tr_status != TWA_CMD_COMPLETE)
    288 		if ((rv = tsleep(tr, PRIBIO, "twaflush", 60 * hz)) != 0)
    289 			break;
    290 	twa_release_request(tr);
    291 	splx(s);
    292 
    293 	return (rv);
    294 }
    295 
    296 static void
    297 ld_twa_adjqparam(struct device *self, int openings)
    298 {
    299 
    300 	ldadjqparam((struct ld_softc *)self, openings);
    301 }
    302 
    303 
    304 static int
    305 ld_twa_scsicmd(struct ld_twa_softc *sc,
    306 	struct twa_request *tr, struct buf *bp)
    307 {
    308 	if (tr->tr_flags == TWA_CMD_DATA_IN) {
    309 		tr->tr_command->command.cmd_pkt_9k.cdb[0] = WRITE_16;
    310 	} else {
    311 		tr->tr_command->command.cmd_pkt_9k.cdb[0] = READ_16;
    312 	}
    313 	tr->tr_command->command.cmd_pkt_9k.cdb[1] =
    314 		(sc->sc_hwunit << 5);			/* lun for CDB */
    315 
    316 	_lto8b(htole64(bp->b_rawblkno),
    317 		&tr->tr_command->command.cmd_pkt_9k.cdb[2]);
    318 	_lto4b(htole32((bp->b_bcount / TWA_SECTOR_SIZE)),
    319 		&tr->tr_command->command.cmd_pkt_9k.cdb[10]);
    320 
    321 	tr->tr_command->command.cmd_pkt_9k.cdb[14] = 0;
    322 	tr->tr_command->command.cmd_pkt_9k.cdb[15] = 0;
    323 
    324 	return (0);
    325 }
    326