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