Home | History | Annotate | Line # | Download | only in scsipi
scsi_base.c revision 1.90.16.1
      1  1.90.16.1     skrll /*	$NetBSD: scsi_base.c,v 1.90.16.1 2016/12/05 10:55:17 skrll Exp $	*/
      2       1.14       cgd 
      3       1.65   mycroft /*-
      4       1.82   mycroft  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      5       1.65   mycroft  * All rights reserved.
      6       1.65   mycroft  *
      7       1.65   mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8       1.65   mycroft  * by Charles M. Hannum.
      9        1.9   mycroft  *
     10        1.9   mycroft  * Redistribution and use in source and binary forms, with or without
     11        1.9   mycroft  * modification, are permitted provided that the following conditions
     12        1.9   mycroft  * are met:
     13        1.9   mycroft  * 1. Redistributions of source code must retain the above copyright
     14        1.9   mycroft  *    notice, this list of conditions and the following disclaimer.
     15        1.9   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.9   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17        1.9   mycroft  *    documentation and/or other materials provided with the distribution.
     18        1.9   mycroft  *
     19       1.65   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.65   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.65   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.65   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.65   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.65   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.65   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.65   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.65   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.65   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.65   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1   mycroft  */
     31       1.76     lukem 
     32       1.76     lukem #include <sys/cdefs.h>
     33  1.90.16.1     skrll __KERNEL_RCSID(0, "$NetBSD: scsi_base.c,v 1.90.16.1 2016/12/05 10:55:17 skrll Exp $");
     34       1.49     enami 
     35        1.1   mycroft #include <sys/param.h>
     36       1.30   thorpej #include <sys/systm.h>
     37       1.11   mycroft #include <sys/kernel.h>
     38        1.1   mycroft #include <sys/buf.h>
     39        1.1   mycroft #include <sys/uio.h>
     40        1.1   mycroft #include <sys/malloc.h>
     41        1.1   mycroft #include <sys/errno.h>
     42        1.1   mycroft #include <sys/device.h>
     43       1.33  christos #include <sys/proc.h>
     44        1.2   mycroft 
     45       1.47    bouyer #include <dev/scsipi/scsipi_all.h>
     46       1.47    bouyer #include <dev/scsipi/scsi_all.h>
     47       1.47    bouyer #include <dev/scsipi/scsi_disk.h>
     48       1.47    bouyer #include <dev/scsipi/scsiconf.h>
     49       1.47    bouyer #include <dev/scsipi/scsipi_base.h>
     50       1.47    bouyer 
     51       1.90    bouyer static void scsi_print_xfer_mode(struct scsipi_periph *);
     52        1.1   mycroft /*
     53        1.1   mycroft  * Do a scsi operation, asking a device to run as SCSI-II if it can.
     54        1.1   mycroft  */
     55       1.50     enami int
     56       1.79   thorpej scsi_change_def(struct scsipi_periph *periph, int flags)
     57        1.1   mycroft {
     58       1.84   mycroft 	struct scsi_changedef cmd;
     59        1.1   mycroft 
     60       1.84   mycroft 	memset(&cmd, 0, sizeof(cmd));
     61       1.84   mycroft 	cmd.opcode = SCSI_CHANGE_DEFINITION;
     62       1.84   mycroft 	cmd.how = SC_SCSI_2;
     63       1.84   mycroft 
     64       1.84   mycroft 	return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
     65       1.84   mycroft 	    SCSIPIRETRIES, 100000, NULL, flags));
     66       1.22   mycroft }
     67       1.22   mycroft 
     68       1.22   mycroft /*
     69       1.22   mycroft  * ask the scsi driver to perform a command for us.
     70       1.22   mycroft  * tell it where to read/write the data, and how
     71       1.22   mycroft  * long the data is supposed to be. If we have  a buf
     72       1.22   mycroft  * to associate with the transfer, we need that too.
     73       1.22   mycroft  */
     74       1.82   mycroft void
     75       1.82   mycroft scsi_scsipi_cmd(struct scsipi_xfer *xs)
     76       1.22   mycroft {
     77       1.82   mycroft 	struct scsipi_periph *periph = xs->xs_periph;
     78       1.22   mycroft 
     79       1.74    bouyer 	SC_DEBUG(periph, SCSIPI_DB2, ("scsi_scsipi_cmd\n"));
     80       1.22   mycroft 
     81       1.58       cgd 	/*
     82       1.58       cgd 	 * Set the LUN in the CDB if we have an older device.  We also
     83       1.74    bouyer 	 * set it for more modern SCSI-2 devices "just in case".
     84       1.58       cgd 	 */
     85       1.74    bouyer 	if (periph->periph_version <= 2)
     86       1.58       cgd 		xs->cmd->bytes[0] |=
     87       1.74    bouyer 		    ((periph->periph_lun << SCSI_CMD_LUN_SHIFT) &
     88       1.58       cgd 			SCSI_CMD_LUN_MASK);
     89        1.1   mycroft }
     90        1.1   mycroft 
     91        1.1   mycroft /*
     92        1.1   mycroft  * Utility routines often used in SCSI stuff
     93        1.1   mycroft  */
     94        1.1   mycroft 
     95        1.1   mycroft /*
     96       1.74    bouyer  * Print out the periph's address info.
     97        1.1   mycroft  */
     98        1.1   mycroft void
     99       1.79   thorpej scsi_print_addr(struct scsipi_periph *periph)
    100        1.1   mycroft {
    101       1.74    bouyer 	struct scsipi_channel *chan = periph->periph_channel;
    102       1.74    bouyer 	struct scsipi_adapter *adapt = chan->chan_adapter;
    103        1.1   mycroft 
    104       1.74    bouyer 	printf("%s(%s:%d:%d:%d): ", periph->periph_dev != NULL ?
    105       1.88    cegger 	    device_xname(periph->periph_dev) : "probe",
    106       1.88    cegger 	    device_xname(adapt->adapt_dev),
    107       1.74    bouyer 	    chan->chan_channel, periph->periph_target,
    108       1.74    bouyer 	    periph->periph_lun);
    109       1.70     enami }
    110       1.70     enami 
    111       1.70     enami /*
    112       1.74    bouyer  * Kill off all pending xfers for a periph.
    113       1.70     enami  *
    114  1.90.16.1     skrll  * Must be called with channel lock held
    115       1.70     enami  */
    116       1.70     enami void
    117       1.87  christos scsi_kill_pending(struct scsipi_periph *periph)
    118       1.70     enami {
    119        1.1   mycroft }
    120       1.90    bouyer 
    121       1.90    bouyer /*
    122       1.90    bouyer  * scsi_print_xfer_mode:
    123       1.90    bouyer  *
    124       1.90    bouyer  *	Print a parallel SCSI periph's capabilities.
    125       1.90    bouyer  */
    126       1.90    bouyer static void
    127       1.90    bouyer scsi_print_xfer_mode(struct scsipi_periph *periph)
    128       1.90    bouyer {
    129       1.90    bouyer 	int period, freq, speed, mbs;
    130       1.90    bouyer 
    131       1.90    bouyer 	aprint_normal_dev(periph->periph_dev, "");
    132       1.90    bouyer 	if (periph->periph_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_DT)) {
    133       1.90    bouyer 		period = scsipi_sync_factor_to_period(periph->periph_period);
    134       1.90    bouyer 		aprint_normal("sync (%d.%02dns offset %d)",
    135       1.90    bouyer 		    period / 100, period % 100, periph->periph_offset);
    136       1.90    bouyer 	} else
    137       1.90    bouyer 		aprint_normal("async");
    138       1.90    bouyer 
    139       1.90    bouyer 	if (periph->periph_mode & PERIPH_CAP_WIDE32)
    140       1.90    bouyer 		aprint_normal(", 32-bit");
    141       1.90    bouyer 	else if (periph->periph_mode & (PERIPH_CAP_WIDE16 | PERIPH_CAP_DT))
    142       1.90    bouyer 		aprint_normal(", 16-bit");
    143       1.90    bouyer 	else
    144       1.90    bouyer 		aprint_normal(", 8-bit");
    145       1.90    bouyer 
    146       1.90    bouyer 	if (periph->periph_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_DT)) {
    147       1.90    bouyer 		freq = scsipi_sync_factor_to_freq(periph->periph_period);
    148       1.90    bouyer 		speed = freq;
    149       1.90    bouyer 		if (periph->periph_mode & PERIPH_CAP_WIDE32)
    150       1.90    bouyer 			speed *= 4;
    151       1.90    bouyer 		else if (periph->periph_mode &
    152       1.90    bouyer 		    (PERIPH_CAP_WIDE16 | PERIPH_CAP_DT))
    153       1.90    bouyer 			speed *= 2;
    154       1.90    bouyer 		mbs = speed / 1000;
    155       1.90    bouyer 		if (mbs > 0) {
    156       1.90    bouyer 			aprint_normal(" (%d.%03dMB/s)", mbs,
    157       1.90    bouyer 			    speed % 1000);
    158       1.90    bouyer 		} else
    159       1.90    bouyer 			aprint_normal(" (%dKB/s)", speed % 1000);
    160       1.90    bouyer 	}
    161       1.90    bouyer 
    162       1.90    bouyer 	aprint_normal(" transfers");
    163       1.90    bouyer 
    164       1.90    bouyer 	if (periph->periph_mode & PERIPH_CAP_TQING)
    165       1.90    bouyer 		aprint_normal(", tagged queueing");
    166       1.90    bouyer 
    167       1.90    bouyer 	aprint_normal("\n");
    168       1.90    bouyer }
    169       1.90    bouyer 
    170       1.90    bouyer /*
    171       1.90    bouyer  * scsi_async_event_xfer_mode:
    172       1.90    bouyer  *
    173       1.90    bouyer  *	Update the xfer mode for all parallel SCSI periphs sharing the
    174       1.90    bouyer  *	specified I_T Nexus.
    175       1.90    bouyer  */
    176       1.90    bouyer void
    177       1.90    bouyer scsi_async_event_xfer_mode(struct scsipi_channel *chan, void *arg)
    178       1.90    bouyer {
    179       1.90    bouyer 	struct scsipi_xfer_mode *xm = arg;
    180       1.90    bouyer 	struct scsipi_periph *periph;
    181       1.90    bouyer 	int lun, announce, mode, period, offset;
    182       1.90    bouyer 
    183       1.90    bouyer 	for (lun = 0; lun < chan->chan_nluns; lun++) {
    184  1.90.16.1     skrll 		periph = scsipi_lookup_periph_locked(chan, xm->xm_target, lun);
    185       1.90    bouyer 		if (periph == NULL)
    186       1.90    bouyer 			continue;
    187       1.90    bouyer 		announce = 0;
    188       1.90    bouyer 
    189       1.90    bouyer 		/*
    190       1.90    bouyer 		 * Clamp the xfer mode down to this periph's capabilities.
    191       1.90    bouyer 		 */
    192       1.90    bouyer 		mode = xm->xm_mode & periph->periph_cap;
    193       1.90    bouyer 		if (mode & PERIPH_CAP_SYNC) {
    194       1.90    bouyer 			period = xm->xm_period;
    195       1.90    bouyer 			offset = xm->xm_offset;
    196       1.90    bouyer 		} else {
    197       1.90    bouyer 			period = 0;
    198       1.90    bouyer 			offset = 0;
    199       1.90    bouyer 		}
    200       1.90    bouyer 
    201       1.90    bouyer 		/*
    202       1.90    bouyer 		 * If we do not have a valid xfer mode yet, or the parameters
    203       1.90    bouyer 		 * are different, announce them.
    204       1.90    bouyer 		 */
    205       1.90    bouyer 		if ((periph->periph_flags & PERIPH_MODE_VALID) == 0 ||
    206       1.90    bouyer 		    periph->periph_mode != mode ||
    207       1.90    bouyer 		    periph->periph_period != period ||
    208       1.90    bouyer 		    periph->periph_offset != offset)
    209       1.90    bouyer 			announce = 1;
    210       1.90    bouyer 
    211       1.90    bouyer 		periph->periph_mode = mode;
    212       1.90    bouyer 		periph->periph_period = period;
    213       1.90    bouyer 		periph->periph_offset = offset;
    214       1.90    bouyer 		periph->periph_flags |= PERIPH_MODE_VALID;
    215       1.90    bouyer 
    216       1.90    bouyer 		if (announce)
    217       1.90    bouyer 			scsi_print_xfer_mode(periph);
    218       1.90    bouyer 	}
    219       1.90    bouyer }
    220       1.90    bouyer 
    221       1.90    bouyer /*
    222       1.90    bouyer  * scsipi_async_event_xfer_mode:
    223       1.90    bouyer  *
    224       1.90    bouyer  *	Update the xfer mode for all SAS/FC periphs sharing the
    225       1.90    bouyer  *	specified I_T Nexus.
    226       1.90    bouyer  */
    227       1.90    bouyer void
    228       1.90    bouyer scsi_fc_sas_async_event_xfer_mode(struct scsipi_channel *chan, void *arg)
    229       1.90    bouyer {
    230       1.90    bouyer 	struct scsipi_xfer_mode *xm = arg;
    231       1.90    bouyer 	struct scsipi_periph *periph;
    232       1.90    bouyer 	int lun, announce, mode;
    233       1.90    bouyer 
    234       1.90    bouyer 	for (lun = 0; lun < chan->chan_nluns; lun++) {
    235  1.90.16.1     skrll 		periph = scsipi_lookup_periph_locked(chan, xm->xm_target, lun);
    236       1.90    bouyer 		if (periph == NULL)
    237       1.90    bouyer 			continue;
    238       1.90    bouyer 		announce = 0;
    239       1.90    bouyer 
    240       1.90    bouyer 		/*
    241       1.90    bouyer 		 * Clamp the xfer mode down to this periph's capabilities.
    242       1.90    bouyer 		 */
    243       1.90    bouyer 		mode = xm->xm_mode & periph->periph_cap;
    244       1.90    bouyer 		/*
    245       1.90    bouyer 		 * If we do not have a valid xfer mode yet, or the parameters
    246       1.90    bouyer 		 * are different, announce them.
    247       1.90    bouyer 		 */
    248       1.90    bouyer 		if ((periph->periph_flags & PERIPH_MODE_VALID) == 0 ||
    249       1.90    bouyer 		    periph->periph_mode != mode)
    250       1.90    bouyer 			announce = 1;
    251       1.90    bouyer 
    252       1.90    bouyer 		periph->periph_mode = mode;
    253       1.90    bouyer 		periph->periph_flags |= PERIPH_MODE_VALID;
    254       1.90    bouyer 
    255       1.90    bouyer 		if (announce &&
    256       1.90    bouyer 		    (periph->periph_mode & PERIPH_CAP_TQING) != 0) {
    257       1.90    bouyer 			aprint_normal_dev(periph->periph_dev,
    258       1.90    bouyer 			    "tagged queueing\n");
    259       1.90    bouyer 		}
    260       1.90    bouyer 	}
    261       1.90    bouyer }
    262