iopsp.c revision 1.2.2.7 1 1.2.2.7 bouyer /* $NetBSD: iopsp.c,v 1.2.2.7 2001/03/27 15:31:52 bouyer Exp $ */
2 1.2.2.2 bouyer
3 1.2.2.2 bouyer /*-
4 1.2.2.7 bouyer * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
5 1.2.2.2 bouyer * All rights reserved.
6 1.2.2.2 bouyer *
7 1.2.2.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 bouyer * by Andrew Doran.
9 1.2.2.2 bouyer *
10 1.2.2.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 bouyer * modification, are permitted provided that the following conditions
12 1.2.2.2 bouyer * are met:
13 1.2.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.2.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.2.2.2 bouyer * must display the following acknowledgement:
20 1.2.2.2 bouyer * This product includes software developed by the NetBSD
21 1.2.2.2 bouyer * Foundation, Inc. and its contributors.
22 1.2.2.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2.2.2 bouyer * contributors may be used to endorse or promote products derived
24 1.2.2.2 bouyer * from this software without specific prior written permission.
25 1.2.2.2 bouyer *
26 1.2.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.2.2.2 bouyer */
38 1.2.2.2 bouyer
39 1.2.2.2 bouyer /*
40 1.2.2.7 bouyer * Raw SCSI device support for I2O. IOPs present SCSI devices individually;
41 1.2.2.7 bouyer * we group them by controlling port.
42 1.2.2.2 bouyer */
43 1.2.2.2 bouyer
44 1.2.2.2 bouyer #include "opt_i2o.h"
45 1.2.2.2 bouyer
46 1.2.2.2 bouyer #include <sys/param.h>
47 1.2.2.2 bouyer #include <sys/systm.h>
48 1.2.2.2 bouyer #include <sys/kernel.h>
49 1.2.2.2 bouyer #include <sys/device.h>
50 1.2.2.2 bouyer #include <sys/queue.h>
51 1.2.2.2 bouyer #include <sys/proc.h>
52 1.2.2.2 bouyer #include <sys/buf.h>
53 1.2.2.2 bouyer #include <sys/endian.h>
54 1.2.2.2 bouyer #include <sys/malloc.h>
55 1.2.2.2 bouyer #include <sys/scsiio.h>
56 1.2.2.3 bouyer #include <sys/lock.h>
57 1.2.2.2 bouyer
58 1.2.2.2 bouyer #include <machine/bswap.h>
59 1.2.2.2 bouyer #include <machine/bus.h>
60 1.2.2.2 bouyer
61 1.2.2.2 bouyer #include <dev/scsipi/scsi_all.h>
62 1.2.2.2 bouyer #include <dev/scsipi/scsi_disk.h>
63 1.2.2.2 bouyer #include <dev/scsipi/scsipi_all.h>
64 1.2.2.2 bouyer #include <dev/scsipi/scsiconf.h>
65 1.2.2.5 bouyer #include <dev/scsipi/scsi_message.h>
66 1.2.2.2 bouyer
67 1.2.2.2 bouyer #include <dev/i2o/i2o.h>
68 1.2.2.7 bouyer #include <dev/i2o/iopio.h>
69 1.2.2.2 bouyer #include <dev/i2o/iopvar.h>
70 1.2.2.2 bouyer #include <dev/i2o/iopspvar.h>
71 1.2.2.2 bouyer
72 1.2.2.7 bouyer static void iopsp_adjqparam(struct device *, int);
73 1.2.2.2 bouyer static void iopsp_attach(struct device *, struct device *, void *);
74 1.2.2.2 bouyer static void iopsp_intr(struct device *, struct iop_msg *, void *);
75 1.2.2.5 bouyer static int iopsp_ioctl(struct scsipi_channel *, u_long,
76 1.2.2.5 bouyer caddr_t, int, struct proc *);
77 1.2.2.2 bouyer static int iopsp_match(struct device *, struct cfdata *, void *);
78 1.2.2.2 bouyer static int iopsp_rescan(struct iopsp_softc *);
79 1.2.2.3 bouyer static int iopsp_reconfig(struct device *);
80 1.2.2.3 bouyer static int iopsp_scsi_abort(struct iopsp_softc *, int, struct iop_msg *);
81 1.2.2.5 bouyer static void iopsp_scsipi_request(struct scsipi_channel *,
82 1.2.2.5 bouyer scsipi_adapter_req_t, void *);
83 1.2.2.2 bouyer
84 1.2.2.2 bouyer struct cfattach iopsp_ca = {
85 1.2.2.2 bouyer sizeof(struct iopsp_softc), iopsp_match, iopsp_attach
86 1.2.2.2 bouyer };
87 1.2.2.2 bouyer
88 1.2.2.2 bouyer /*
89 1.2.2.7 bouyer * Match a supported device.
90 1.2.2.2 bouyer */
91 1.2.2.2 bouyer static int
92 1.2.2.2 bouyer iopsp_match(struct device *parent, struct cfdata *match, void *aux)
93 1.2.2.2 bouyer {
94 1.2.2.2 bouyer struct iop_attach_args *ia;
95 1.2.2.2 bouyer struct {
96 1.2.2.2 bouyer struct i2o_param_op_results pr;
97 1.2.2.2 bouyer struct i2o_param_read_results prr;
98 1.2.2.2 bouyer struct i2o_param_hba_ctlr_info ci;
99 1.2.2.2 bouyer } __attribute__ ((__packed__)) param;
100 1.2.2.2 bouyer
101 1.2.2.2 bouyer ia = aux;
102 1.2.2.2 bouyer
103 1.2.2.2 bouyer if (ia->ia_class != I2O_CLASS_BUS_ADAPTER_PORT)
104 1.2.2.2 bouyer return (0);
105 1.2.2.2 bouyer
106 1.2.2.7 bouyer if (iop_param_op((struct iop_softc *)parent, ia->ia_tid, NULL, 0,
107 1.2.2.2 bouyer I2O_PARAM_HBA_CTLR_INFO, ¶m, sizeof(param)) != 0)
108 1.2.2.2 bouyer return (0);
109 1.2.2.2 bouyer
110 1.2.2.2 bouyer return (param.ci.bustype == I2O_HBA_BUS_SCSI ||
111 1.2.2.2 bouyer param.ci.bustype == I2O_HBA_BUS_FCA);
112 1.2.2.2 bouyer }
113 1.2.2.2 bouyer
114 1.2.2.2 bouyer /*
115 1.2.2.2 bouyer * Attach a supported device.
116 1.2.2.2 bouyer */
117 1.2.2.2 bouyer static void
118 1.2.2.2 bouyer iopsp_attach(struct device *parent, struct device *self, void *aux)
119 1.2.2.2 bouyer {
120 1.2.2.2 bouyer struct iop_attach_args *ia;
121 1.2.2.2 bouyer struct iopsp_softc *sc;
122 1.2.2.2 bouyer struct iop_softc *iop;
123 1.2.2.2 bouyer struct {
124 1.2.2.2 bouyer struct i2o_param_op_results pr;
125 1.2.2.2 bouyer struct i2o_param_read_results prr;
126 1.2.2.2 bouyer union {
127 1.2.2.2 bouyer struct i2o_param_hba_ctlr_info ci;
128 1.2.2.2 bouyer struct i2o_param_hba_scsi_ctlr_info sci;
129 1.2.2.3 bouyer struct i2o_param_hba_scsi_port_info spi;
130 1.2.2.2 bouyer } p;
131 1.2.2.2 bouyer } __attribute__ ((__packed__)) param;
132 1.2.2.3 bouyer int fcal, rv;
133 1.2.2.2 bouyer #ifdef I2OVERBOSE
134 1.2.2.2 bouyer int size;
135 1.2.2.2 bouyer #endif
136 1.2.2.2 bouyer
137 1.2.2.2 bouyer ia = (struct iop_attach_args *)aux;
138 1.2.2.2 bouyer sc = (struct iopsp_softc *)self;
139 1.2.2.2 bouyer iop = (struct iop_softc *)parent;
140 1.2.2.2 bouyer
141 1.2.2.2 bouyer /* Register us as an initiator. */
142 1.2.2.2 bouyer sc->sc_ii.ii_dv = self;
143 1.2.2.2 bouyer sc->sc_ii.ii_intr = iopsp_intr;
144 1.2.2.2 bouyer sc->sc_ii.ii_flags = 0;
145 1.2.2.3 bouyer sc->sc_ii.ii_tid = ia->ia_tid;
146 1.2.2.3 bouyer sc->sc_ii.ii_reconfig = iopsp_reconfig;
147 1.2.2.7 bouyer sc->sc_ii.ii_adjqparam = iopsp_adjqparam;
148 1.2.2.7 bouyer iop_initiator_register(iop, &sc->sc_ii);
149 1.2.2.2 bouyer
150 1.2.2.7 bouyer rv = iop_param_op(iop, ia->ia_tid, NULL, 0, I2O_PARAM_HBA_CTLR_INFO,
151 1.2.2.7 bouyer ¶m, sizeof(param));
152 1.2.2.3 bouyer if (rv != 0) {
153 1.2.2.3 bouyer printf("%s: unable to get parameters (0x%04x; %d)\n",
154 1.2.2.3 bouyer sc->sc_dv.dv_xname, I2O_PARAM_HBA_CTLR_INFO, rv);
155 1.2.2.2 bouyer goto bad;
156 1.2.2.2 bouyer }
157 1.2.2.2 bouyer
158 1.2.2.2 bouyer fcal = (param.p.ci.bustype == I2O_HBA_BUS_FCA); /* XXX */
159 1.2.2.2 bouyer
160 1.2.2.2 bouyer /*
161 1.2.2.2 bouyer * Say what the device is. If we can find out what the controling
162 1.2.2.2 bouyer * device is, say what that is too.
163 1.2.2.2 bouyer */
164 1.2.2.7 bouyer printf(": SCSI port");
165 1.2.2.7 bouyer iop_print_ident(iop, ia->ia_tid);
166 1.2.2.2 bouyer printf("\n");
167 1.2.2.2 bouyer
168 1.2.2.7 bouyer rv = iop_param_op(iop, ia->ia_tid, NULL, 0,
169 1.2.2.7 bouyer I2O_PARAM_HBA_SCSI_CTLR_INFO, ¶m, sizeof(param));
170 1.2.2.3 bouyer if (rv != 0) {
171 1.2.2.3 bouyer printf("%s: unable to get parameters (0x%04x; %d)\n",
172 1.2.2.3 bouyer sc->sc_dv.dv_xname, I2O_PARAM_HBA_SCSI_CTLR_INFO, rv);
173 1.2.2.2 bouyer goto bad;
174 1.2.2.2 bouyer }
175 1.2.2.2 bouyer
176 1.2.2.2 bouyer #ifdef I2OVERBOSE
177 1.2.2.2 bouyer printf("%s: %d-bit, max sync rate %dMHz, initiator ID %d\n",
178 1.2.2.2 bouyer sc->sc_dv.dv_xname, param.p.sci.maxdatawidth,
179 1.2.2.2 bouyer (u_int32_t)le64toh(param.p.sci.maxsyncrate) / 1000,
180 1.2.2.2 bouyer le32toh(param.p.sci.initiatorid));
181 1.2.2.2 bouyer #endif
182 1.2.2.2 bouyer
183 1.2.2.5 bouyer sc->sc_adapter.adapt_dev = &sc->sc_dv;
184 1.2.2.5 bouyer sc->sc_adapter.adapt_nchannels = 1;
185 1.2.2.7 bouyer sc->sc_adapter.adapt_openings = 1;
186 1.2.2.7 bouyer sc->sc_adapter.adapt_max_periph = 1;
187 1.2.2.5 bouyer sc->sc_adapter.adapt_ioctl = iopsp_ioctl;
188 1.2.2.5 bouyer sc->sc_adapter.adapt_minphys = minphys;
189 1.2.2.5 bouyer sc->sc_adapter.adapt_request = iopsp_scsipi_request;
190 1.2.2.5 bouyer
191 1.2.2.5 bouyer memset(&sc->sc_channel, 0, sizeof(sc->sc_channel));
192 1.2.2.5 bouyer sc->sc_channel.chan_adapter = &sc->sc_adapter;
193 1.2.2.5 bouyer sc->sc_channel.chan_bustype = &scsi_bustype;
194 1.2.2.5 bouyer sc->sc_channel.chan_channel = 0;
195 1.2.2.5 bouyer sc->sc_channel.chan_ntargets = fcal ?
196 1.2.2.5 bouyer IOPSP_MAX_FCAL_TARGET : param.p.sci.maxdatawidth;
197 1.2.2.5 bouyer sc->sc_channel.chan_nluns = IOPSP_MAX_LUN;
198 1.2.2.5 bouyer sc->sc_channel.chan_id = le32toh(param.p.sci.initiatorid);
199 1.2.2.2 bouyer
200 1.2.2.2 bouyer #ifdef I2OVERBOSE
201 1.2.2.2 bouyer /*
202 1.2.2.2 bouyer * Allocate the target map. Currently used for informational
203 1.2.2.2 bouyer * purposes only.
204 1.2.2.2 bouyer */
205 1.2.2.5 bouyer size = sc->sc_channel.chan_ntargets * sizeof(struct iopsp_target);
206 1.2.2.2 bouyer sc->sc_targetmap = malloc(size, M_DEVBUF, M_NOWAIT);
207 1.2.2.2 bouyer memset(sc->sc_targetmap, 0, size);
208 1.2.2.2 bouyer #endif
209 1.2.2.2 bouyer
210 1.2.2.3 bouyer /* Build the two maps, and attach to scsipi. */
211 1.2.2.3 bouyer if (iopsp_reconfig(self) != 0) {
212 1.2.2.7 bouyer printf("%s: configure failed\n", sc->sc_dv.dv_xname);
213 1.2.2.3 bouyer goto bad;
214 1.2.2.3 bouyer }
215 1.2.2.5 bouyer config_found(self, &sc->sc_channel, scsiprint);
216 1.2.2.2 bouyer return;
217 1.2.2.2 bouyer
218 1.2.2.7 bouyer bad:
219 1.2.2.2 bouyer iop_initiator_unregister(iop, &sc->sc_ii);
220 1.2.2.2 bouyer }
221 1.2.2.2 bouyer
222 1.2.2.2 bouyer /*
223 1.2.2.3 bouyer * Scan the LCT to determine which devices we control, and enter them into
224 1.2.2.3 bouyer * the maps.
225 1.2.2.2 bouyer */
226 1.2.2.3 bouyer static int
227 1.2.2.3 bouyer iopsp_reconfig(struct device *dv)
228 1.2.2.2 bouyer {
229 1.2.2.3 bouyer struct iopsp_softc *sc;
230 1.2.2.2 bouyer struct iop_softc *iop;
231 1.2.2.2 bouyer struct i2o_lct_entry *le;
232 1.2.2.5 bouyer struct scsipi_channel *sc_chan;
233 1.2.2.2 bouyer struct {
234 1.2.2.2 bouyer struct i2o_param_op_results pr;
235 1.2.2.2 bouyer struct i2o_param_read_results prr;
236 1.2.2.2 bouyer struct i2o_param_scsi_device_info sdi;
237 1.2.2.2 bouyer } __attribute__ ((__packed__)) param;
238 1.2.2.7 bouyer u_int tid, nent, i, targ, lun, size, s, rv, bptid;
239 1.2.2.2 bouyer u_short *tidmap;
240 1.2.2.2 bouyer #ifdef I2OVERBOSE
241 1.2.2.2 bouyer struct iopsp_target *it;
242 1.2.2.2 bouyer int syncrate;
243 1.2.2.2 bouyer #endif
244 1.2.2.2 bouyer
245 1.2.2.3 bouyer sc = (struct iopsp_softc *)dv;
246 1.2.2.2 bouyer iop = (struct iop_softc *)sc->sc_dv.dv_parent;
247 1.2.2.5 bouyer sc_chan = &sc->sc_channel;
248 1.2.2.2 bouyer
249 1.2.2.3 bouyer /* Anything to do? */
250 1.2.2.7 bouyer if (iop->sc_chgind == sc->sc_chgind)
251 1.2.2.3 bouyer return (0);
252 1.2.2.3 bouyer
253 1.2.2.2 bouyer /*
254 1.2.2.2 bouyer * Allocate memory for the target/LUN -> TID map. Use zero to
255 1.2.2.2 bouyer * denote absent targets (zero is the TID of the I2O executive,
256 1.2.2.2 bouyer * and we never address that here).
257 1.2.2.2 bouyer */
258 1.2.2.5 bouyer size = sc_chan->chan_ntargets * (IOPSP_MAX_LUN) * sizeof(u_short);
259 1.2.2.3 bouyer if ((tidmap = malloc(size, M_DEVBUF, M_WAITOK)) == NULL)
260 1.2.2.3 bouyer return (ENOMEM);
261 1.2.2.3 bouyer memset(tidmap, 0, size);
262 1.2.2.2 bouyer
263 1.2.2.2 bouyer #ifdef I2OVERBOSE
264 1.2.2.5 bouyer for (i = 0; i < sc_chan->chan_ntargets; i++)
265 1.2.2.2 bouyer sc->sc_targetmap[i].it_flags &= ~IT_PRESENT;
266 1.2.2.2 bouyer #endif
267 1.2.2.2 bouyer
268 1.2.2.7 bouyer /*
269 1.2.2.7 bouyer * A quick hack to handle Intel's stacked bus port arrangement.
270 1.2.2.7 bouyer */
271 1.2.2.7 bouyer bptid = sc->sc_ii.ii_tid;
272 1.2.2.2 bouyer nent = iop->sc_nlctent;
273 1.2.2.7 bouyer for (le = iop->sc_lct->entry; nent != 0; nent--, le++)
274 1.2.2.7 bouyer if ((le16toh(le->classid) & 4095) ==
275 1.2.2.7 bouyer I2O_CLASS_BUS_ADAPTER_PORT &&
276 1.2.2.7 bouyer (le32toh(le->usertid) & 4095) == bptid) {
277 1.2.2.7 bouyer bptid = le32toh(le->localtid) & 4095;
278 1.2.2.2 bouyer break;
279 1.2.2.2 bouyer }
280 1.2.2.2 bouyer
281 1.2.2.7 bouyer nent = iop->sc_nlctent;
282 1.2.2.7 bouyer for (i = 0, le = iop->sc_lct->entry; i < nent; i++, le++) {
283 1.2.2.7 bouyer if ((le16toh(le->classid) & 4095) != I2O_CLASS_SCSI_PERIPHERAL)
284 1.2.2.7 bouyer continue;
285 1.2.2.7 bouyer if (((le32toh(le->usertid) >> 12) & 4095) != bptid)
286 1.2.2.7 bouyer continue;
287 1.2.2.2 bouyer tid = le32toh(le->localtid) & 4095;
288 1.2.2.2 bouyer
289 1.2.2.7 bouyer rv = iop_param_op(iop, tid, NULL, 0, I2O_PARAM_SCSI_DEVICE_INFO,
290 1.2.2.3 bouyer ¶m, sizeof(param));
291 1.2.2.3 bouyer if (rv != 0) {
292 1.2.2.3 bouyer printf("%s: unable to get parameters (0x%04x; %d)\n",
293 1.2.2.3 bouyer sc->sc_dv.dv_xname, I2O_PARAM_SCSI_DEVICE_INFO,
294 1.2.2.3 bouyer rv);
295 1.2.2.2 bouyer continue;
296 1.2.2.2 bouyer }
297 1.2.2.2 bouyer targ = le32toh(param.sdi.identifier);
298 1.2.2.2 bouyer lun = param.sdi.luninfo[1];
299 1.2.2.7 bouyer #if defined(DIAGNOSTIC) || defined(I2ODEBUG)
300 1.2.2.7 bouyer if (targ >= sc_chan->chan_ntargets ||
301 1.2.2.7 bouyer lun >= sc_chan->chan_nluns) {
302 1.2.2.7 bouyer printf("%s: target %d,%d (tid %d): bad target/LUN\n",
303 1.2.2.7 bouyer sc->sc_dv.dv_xname, targ, lun, tid);
304 1.2.2.2 bouyer continue;
305 1.2.2.2 bouyer }
306 1.2.2.7 bouyer #endif
307 1.2.2.2 bouyer
308 1.2.2.2 bouyer #ifdef I2OVERBOSE
309 1.2.2.2 bouyer /*
310 1.2.2.2 bouyer * If we've already described this target, and nothing has
311 1.2.2.2 bouyer * changed, then don't describe it again.
312 1.2.2.2 bouyer */
313 1.2.2.2 bouyer it = &sc->sc_targetmap[targ];
314 1.2.2.2 bouyer it->it_flags |= IT_PRESENT;
315 1.2.2.2 bouyer syncrate = ((int)le64toh(param.sdi.negsyncrate) + 500) / 1000;
316 1.2.2.2 bouyer if (it->it_width == param.sdi.negdatawidth &&
317 1.2.2.2 bouyer it->it_offset == param.sdi.negoffset &&
318 1.2.2.2 bouyer it->it_syncrate == syncrate)
319 1.2.2.2 bouyer continue;
320 1.2.2.2 bouyer
321 1.2.2.2 bouyer it->it_width = param.sdi.negdatawidth;
322 1.2.2.2 bouyer it->it_offset = param.sdi.negoffset;
323 1.2.2.2 bouyer it->it_syncrate = syncrate;
324 1.2.2.2 bouyer
325 1.2.2.2 bouyer printf("%s: target %d (tid %d): %d-bit, ", sc->sc_dv.dv_xname,
326 1.2.2.2 bouyer targ, tid, it->it_width);
327 1.2.2.2 bouyer if (it->it_syncrate == 0)
328 1.2.2.2 bouyer printf("asynchronous\n");
329 1.2.2.2 bouyer else
330 1.2.2.2 bouyer printf("synchronous at %dMHz, offset 0x%x\n",
331 1.2.2.2 bouyer it->it_syncrate, it->it_offset);
332 1.2.2.2 bouyer #endif
333 1.2.2.7 bouyer
334 1.2.2.7 bouyer /* Ignore the device if it's in use by somebody else. */
335 1.2.2.7 bouyer if ((le32toh(le->usertid) & 4095) != I2O_TID_NONE) {
336 1.2.2.7 bouyer #ifdef I2OVERBOSE
337 1.2.2.7 bouyer if (sc->sc_tidmap == NULL ||
338 1.2.2.7 bouyer IOPSP_TIDMAP(sc->sc_tidmap, targ, lun) !=
339 1.2.2.7 bouyer IOPSP_TID_INUSE)
340 1.2.2.7 bouyer printf("%s: target %d,%d (tid %d): in use by"
341 1.2.2.7 bouyer " tid %d\n", sc->sc_dv.dv_xname,
342 1.2.2.7 bouyer targ, lun, tid,
343 1.2.2.7 bouyer le32toh(le->usertid) & 4095);
344 1.2.2.7 bouyer #endif
345 1.2.2.7 bouyer IOPSP_TIDMAP(tidmap, targ, lun) = IOPSP_TID_INUSE;
346 1.2.2.7 bouyer } else
347 1.2.2.7 bouyer IOPSP_TIDMAP(tidmap, targ, lun) = (u_short)tid;
348 1.2.2.2 bouyer }
349 1.2.2.2 bouyer
350 1.2.2.2 bouyer #ifdef I2OVERBOSE
351 1.2.2.5 bouyer for (i = 0; i < sc_chan->chan_ntargets; i++)
352 1.2.2.2 bouyer if ((sc->sc_targetmap[i].it_flags & IT_PRESENT) == 0)
353 1.2.2.2 bouyer sc->sc_targetmap[i].it_width = 0;
354 1.2.2.2 bouyer #endif
355 1.2.2.2 bouyer
356 1.2.2.2 bouyer /* Swap in the new map and return. */
357 1.2.2.3 bouyer s = splbio();
358 1.2.2.2 bouyer if (sc->sc_tidmap != NULL)
359 1.2.2.2 bouyer free(sc->sc_tidmap, M_DEVBUF);
360 1.2.2.2 bouyer sc->sc_tidmap = tidmap;
361 1.2.2.3 bouyer splx(s);
362 1.2.2.7 bouyer sc->sc_chgind = iop->sc_chgind;
363 1.2.2.3 bouyer return (0);
364 1.2.2.2 bouyer }
365 1.2.2.2 bouyer
366 1.2.2.2 bouyer /*
367 1.2.2.3 bouyer * Re-scan the bus; to be called from a higher level (e.g. scsipi).
368 1.2.2.2 bouyer */
369 1.2.2.2 bouyer static int
370 1.2.2.2 bouyer iopsp_rescan(struct iopsp_softc *sc)
371 1.2.2.2 bouyer {
372 1.2.2.2 bouyer struct iop_softc *iop;
373 1.2.2.2 bouyer struct iop_msg *im;
374 1.2.2.7 bouyer struct i2o_hba_bus_scan mf;
375 1.2.2.2 bouyer int rv;
376 1.2.2.2 bouyer
377 1.2.2.2 bouyer iop = (struct iop_softc *)sc->sc_dv.dv_parent;
378 1.2.2.2 bouyer
379 1.2.2.7 bouyer rv = lockmgr(&iop->sc_conflock, LK_EXCLUSIVE, NULL);
380 1.2.2.3 bouyer if (rv != 0) {
381 1.2.2.3 bouyer #ifdef I2ODEBUG
382 1.2.2.3 bouyer printf("iopsp_rescan: unable to acquire lock\n");
383 1.2.2.3 bouyer #endif
384 1.2.2.2 bouyer return (rv);
385 1.2.2.3 bouyer }
386 1.2.2.2 bouyer
387 1.2.2.7 bouyer im = iop_msg_alloc(iop, &sc->sc_ii, IM_WAIT);
388 1.2.2.3 bouyer
389 1.2.2.7 bouyer mf.msgflags = I2O_MSGFLAGS(i2o_hba_bus_scan);
390 1.2.2.7 bouyer mf.msgfunc = I2O_MSGFUNC(sc->sc_ii.ii_tid, I2O_HBA_BUS_SCAN);
391 1.2.2.7 bouyer mf.msgictx = sc->sc_ii.ii_ictx;
392 1.2.2.7 bouyer mf.msgtctx = im->im_tctx;
393 1.2.2.7 bouyer
394 1.2.2.7 bouyer rv = iop_msg_post(iop, im, &mf, 5*60*1000);
395 1.2.2.7 bouyer iop_msg_free(iop, im);
396 1.2.2.7 bouyer if (rv != 0)
397 1.2.2.7 bouyer printf("%s: bus rescan failed (error %d)\n",
398 1.2.2.7 bouyer sc->sc_dv.dv_xname, rv);
399 1.2.2.7 bouyer
400 1.2.2.7 bouyer if ((rv = iop_lct_get(iop)) != 0)
401 1.2.2.7 bouyer goto done;
402 1.2.2.3 bouyer
403 1.2.2.3 bouyer /* Rebuild the target/LUN -> TID map, release lock, and return. */
404 1.2.2.3 bouyer rv = iopsp_reconfig(&sc->sc_dv);
405 1.2.2.7 bouyer done:
406 1.2.2.3 bouyer lockmgr(&iop->sc_conflock, LK_RELEASE, NULL);
407 1.2.2.3 bouyer return (rv);
408 1.2.2.2 bouyer }
409 1.2.2.2 bouyer
410 1.2.2.2 bouyer /*
411 1.2.2.2 bouyer * Start a SCSI command.
412 1.2.2.2 bouyer */
413 1.2.2.5 bouyer static void
414 1.2.2.5 bouyer iopsp_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
415 1.2.2.5 bouyer void *arg)
416 1.2.2.2 bouyer {
417 1.2.2.5 bouyer struct scsipi_xfer *xs;
418 1.2.2.5 bouyer struct scsipi_periph *periph;
419 1.2.2.5 bouyer struct iopsp_softc *sc = (void *)chan->chan_adapter->adapt_dev;
420 1.2.2.2 bouyer struct iop_msg *im;
421 1.2.2.5 bouyer struct iop_softc *iop = (struct iop_softc *)sc->sc_dv.dv_parent;
422 1.2.2.7 bouyer struct i2o_scsi_scb_exec *mf;
423 1.2.2.7 bouyer int error, flags, tid, imf;
424 1.2.2.7 bouyer u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
425 1.2.2.2 bouyer
426 1.2.2.5 bouyer switch (req) {
427 1.2.2.5 bouyer case ADAPTER_REQ_RUN_XFER:
428 1.2.2.5 bouyer xs = arg;
429 1.2.2.5 bouyer periph = xs->xs_periph;
430 1.2.2.5 bouyer flags = xs->xs_control;
431 1.2.2.5 bouyer
432 1.2.2.5 bouyer tid = IOPSP_TIDMAP(sc->sc_tidmap, periph->periph_target,
433 1.2.2.5 bouyer periph->periph_lun);
434 1.2.2.5 bouyer if (tid == IOPSP_TID_ABSENT || tid == IOPSP_TID_INUSE) {
435 1.2.2.5 bouyer xs->error = XS_SELTIMEOUT;
436 1.2.2.5 bouyer scsipi_done(xs);
437 1.2.2.5 bouyer return;
438 1.2.2.5 bouyer }
439 1.2.2.2 bouyer
440 1.2.2.5 bouyer SC_DEBUG(periph, SDEV_DB2, ("iopsp_scsi_request run_xfer\n"));
441 1.2.2.2 bouyer
442 1.2.2.5 bouyer /* Need to reset the target? */
443 1.2.2.5 bouyer if ((flags & XS_CTL_RESET) != 0) {
444 1.2.2.5 bouyer if (iop_simple_cmd(iop, tid, I2O_SCSI_DEVICE_RESET,
445 1.2.2.5 bouyer sc->sc_ii.ii_ictx, 1, 10*1000) != 0) {
446 1.2.2.2 bouyer #ifdef I2ODEBUG
447 1.2.2.5 bouyer printf("%s: reset failed\n",
448 1.2.2.5 bouyer sc->sc_dv.dv_xname);
449 1.2.2.2 bouyer #endif
450 1.2.2.5 bouyer xs->error = XS_DRIVER_STUFFUP;
451 1.2.2.5 bouyer } else
452 1.2.2.5 bouyer xs->error = XS_NOERROR;
453 1.2.2.5 bouyer
454 1.2.2.5 bouyer scsipi_done(xs);
455 1.2.2.5 bouyer return;
456 1.2.2.2 bouyer }
457 1.2.2.2 bouyer
458 1.2.2.2 bouyer #if defined(I2ODEBUG) || defined(SCSIDEBUG)
459 1.2.2.7 bouyer if (xs->cmdlen > sizeof(mf->cdb))
460 1.2.2.5 bouyer panic("%s: CDB too large\n", sc->sc_dv.dv_xname);
461 1.2.2.2 bouyer #endif
462 1.2.2.2 bouyer
463 1.2.2.7 bouyer imf = (flags & (XS_CTL_POLL | XS_CTL_NOSLEEP)) != 0 ?
464 1.2.2.7 bouyer IM_POLL : 0; im = iop_msg_alloc(iop, &sc->sc_ii, imf);
465 1.2.2.5 bouyer im->im_dvcontext = xs;
466 1.2.2.2 bouyer
467 1.2.2.7 bouyer mf = (struct i2o_scsi_scb_exec *)mb;
468 1.2.2.7 bouyer mf->msgflags = I2O_MSGFLAGS(i2o_scsi_scb_exec);
469 1.2.2.7 bouyer mf->msgfunc = I2O_MSGFUNC(tid, I2O_SCSI_SCB_EXEC);
470 1.2.2.7 bouyer mf->msgictx = sc->sc_ii.ii_ictx;
471 1.2.2.7 bouyer mf->msgtctx = im->im_tctx;
472 1.2.2.7 bouyer mf->flags = xs->cmdlen | I2O_SCB_FLAG_ENABLE_DISCONNECT |
473 1.2.2.5 bouyer I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
474 1.2.2.7 bouyer mf->datalen = xs->datalen;
475 1.2.2.7 bouyer memcpy(mf->cdb, xs->cmd, xs->cmdlen);
476 1.2.2.5 bouyer
477 1.2.2.5 bouyer switch(xs->xs_tag_type) {
478 1.2.2.5 bouyer case MSG_ORDERED_Q_TAG:
479 1.2.2.7 bouyer mf->flags |= I2O_SCB_FLAG_ORDERED_QUEUE_TAG;
480 1.2.2.5 bouyer break;
481 1.2.2.5 bouyer case MSG_SIMPLE_Q_TAG:
482 1.2.2.7 bouyer mf->flags |= I2O_SCB_FLAG_SIMPLE_QUEUE_TAG;
483 1.2.2.5 bouyer break;
484 1.2.2.5 bouyer case MSG_HEAD_OF_Q_TAG:
485 1.2.2.7 bouyer mf->flags |= I2O_SCB_FLAG_HEAD_QUEUE_TAG;
486 1.2.2.5 bouyer break;
487 1.2.2.5 bouyer default:
488 1.2.2.5 bouyer break;
489 1.2.2.5 bouyer }
490 1.2.2.5 bouyer if (xs->datalen != 0) {
491 1.2.2.7 bouyer error = iop_msg_map_bio(iop, im, mb, xs->data,
492 1.2.2.7 bouyer xs->datalen, (flags & XS_CTL_DATA_OUT) == 0);
493 1.2.2.5 bouyer if (error) {
494 1.2.2.2 bouyer #ifdef I2ODEBUG
495 1.2.2.5 bouyer printf("%s: error %d mapping xfer\n",
496 1.2.2.5 bouyer sc->sc_dv.dv_xname, error);
497 1.2.2.2 bouyer #endif
498 1.2.2.5 bouyer xs->error = XS_DRIVER_STUFFUP;
499 1.2.2.7 bouyer iop_msg_free(iop, im);
500 1.2.2.5 bouyer scsipi_done(xs);
501 1.2.2.5 bouyer return;
502 1.2.2.5 bouyer }
503 1.2.2.5 bouyer if ((flags & XS_CTL_DATA_IN) == 0)
504 1.2.2.7 bouyer mf->flags |= I2O_SCB_FLAG_XFER_TO_DEVICE;
505 1.2.2.5 bouyer else
506 1.2.2.7 bouyer mf->flags |= I2O_SCB_FLAG_XFER_FROM_DEVICE;
507 1.2.2.2 bouyer }
508 1.2.2.2 bouyer
509 1.2.2.5 bouyer /*
510 1.2.2.5 bouyer * If the command is allowed to execute asynchronously,
511 1.2.2.5 bouyer * enqueue it with the IOP.
512 1.2.2.5 bouyer */
513 1.2.2.5 bouyer if ((flags & XS_CTL_POLL) == 0) {
514 1.2.2.7 bouyer if (iop_msg_post(iop, im, mb, 0)) {
515 1.2.2.7 bouyer if (xs->datalen != 0)
516 1.2.2.7 bouyer iop_msg_unmap(iop, im);
517 1.2.2.7 bouyer iop_msg_free(iop, im);
518 1.2.2.7 bouyer xs->error = XS_DRIVER_STUFFUP;
519 1.2.2.7 bouyer scsipi_done(xs);
520 1.2.2.7 bouyer }
521 1.2.2.5 bouyer return;
522 1.2.2.5 bouyer }
523 1.2.2.2 bouyer
524 1.2.2.7 bouyer if (iop_msg_post(iop, im, mb, xs->timeout)) {
525 1.2.2.5 bouyer scsipi_printaddr(xs->xs_periph);
526 1.2.2.5 bouyer printf("timeout; aborting command\n");
527 1.2.2.5 bouyer if (iopsp_scsi_abort(sc, tid, im)) {
528 1.2.2.5 bouyer scsipi_printaddr(xs->xs_periph);
529 1.2.2.5 bouyer printf("abort failed\n");
530 1.2.2.5 bouyer }
531 1.2.2.5 bouyer xs->error = XS_TIMEOUT;
532 1.2.2.2 bouyer }
533 1.2.2.5 bouyer scsipi_done(xs);
534 1.2.2.5 bouyer return;
535 1.2.2.5 bouyer case ADAPTER_REQ_GROW_RESOURCES:
536 1.2.2.5 bouyer /* XXX Not supported. */
537 1.2.2.5 bouyer return;
538 1.2.2.5 bouyer case ADAPTER_REQ_SET_XFER_MODE:
539 1.2.2.5 bouyer /* XXX Not supported. */
540 1.2.2.5 bouyer return;
541 1.2.2.2 bouyer }
542 1.2.2.2 bouyer }
543 1.2.2.2 bouyer
544 1.2.2.2 bouyer /*
545 1.2.2.2 bouyer * Abort the specified I2O_SCSI_SCB_EXEC message and its associated SCB.
546 1.2.2.2 bouyer */
547 1.2.2.2 bouyer static int
548 1.2.2.3 bouyer iopsp_scsi_abort(struct iopsp_softc *sc, int atid, struct iop_msg *aim)
549 1.2.2.2 bouyer {
550 1.2.2.2 bouyer struct iop_msg *im;
551 1.2.2.7 bouyer struct i2o_scsi_scb_abort mf;
552 1.2.2.2 bouyer struct iop_softc *iop;
553 1.2.2.7 bouyer int rv, s;
554 1.2.2.2 bouyer
555 1.2.2.2 bouyer iop = (struct iop_softc *)sc->sc_dv.dv_parent;
556 1.2.2.7 bouyer im = iop_msg_alloc(iop, &sc->sc_ii, IM_POLL);
557 1.2.2.2 bouyer
558 1.2.2.7 bouyer mf.msgflags = I2O_MSGFLAGS(i2o_scsi_scb_abort);
559 1.2.2.7 bouyer mf.msgfunc = I2O_MSGFUNC(atid, I2O_SCSI_SCB_ABORT);
560 1.2.2.7 bouyer mf.msgictx = sc->sc_ii.ii_ictx;
561 1.2.2.7 bouyer mf.msgtctx = im->im_tctx;
562 1.2.2.7 bouyer mf.tctxabort = aim->im_tctx;
563 1.2.2.2 bouyer
564 1.2.2.7 bouyer s = splbio();
565 1.2.2.7 bouyer rv = iop_msg_post(iop, im, &mf, 30000);
566 1.2.2.7 bouyer splx(s);
567 1.2.2.7 bouyer iop_msg_free(iop, im);
568 1.2.2.2 bouyer return (rv);
569 1.2.2.2 bouyer }
570 1.2.2.2 bouyer
571 1.2.2.2 bouyer /*
572 1.2.2.2 bouyer * We have a message which has been processed and replied to by the IOP -
573 1.2.2.2 bouyer * deal with it.
574 1.2.2.2 bouyer */
575 1.2.2.2 bouyer static void
576 1.2.2.2 bouyer iopsp_intr(struct device *dv, struct iop_msg *im, void *reply)
577 1.2.2.2 bouyer {
578 1.2.2.2 bouyer struct scsipi_xfer *xs;
579 1.2.2.2 bouyer struct iopsp_softc *sc;
580 1.2.2.2 bouyer struct i2o_scsi_reply *rb;
581 1.2.2.7 bouyer struct iop_softc *iop;
582 1.2.2.7 bouyer u_int sl;
583 1.2.2.2 bouyer
584 1.2.2.2 bouyer sc = (struct iopsp_softc *)dv;
585 1.2.2.2 bouyer xs = (struct scsipi_xfer *)im->im_dvcontext;
586 1.2.2.2 bouyer iop = (struct iop_softc *)dv->dv_parent;
587 1.2.2.2 bouyer
588 1.2.2.5 bouyer SC_DEBUG(xs->xs_periph, SDEV_DB2, ("iopsp_intr\n"));
589 1.2.2.2 bouyer
590 1.2.2.7 bouyer if ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0) {
591 1.2.2.7 bouyer xs->error = XS_DRIVER_STUFFUP;
592 1.2.2.7 bouyer xs->resid = xs->datalen;
593 1.2.2.7 bouyer } else {
594 1.2.2.2 bouyer rb = reply;
595 1.2.2.2 bouyer
596 1.2.2.7 bouyer if (rb->hbastatus != I2O_SCSI_DSC_SUCCESS) {
597 1.2.2.7 bouyer switch (rb->hbastatus) {
598 1.2.2.2 bouyer case I2O_SCSI_DSC_ADAPTER_BUSY:
599 1.2.2.2 bouyer case I2O_SCSI_DSC_SCSI_BUS_RESET:
600 1.2.2.2 bouyer case I2O_SCSI_DSC_BUS_BUSY:
601 1.2.2.2 bouyer xs->error = XS_BUSY;
602 1.2.2.2 bouyer break;
603 1.2.2.2 bouyer case I2O_SCSI_DSC_SELECTION_TIMEOUT:
604 1.2.2.2 bouyer xs->error = XS_SELTIMEOUT;
605 1.2.2.2 bouyer break;
606 1.2.2.2 bouyer case I2O_SCSI_DSC_COMMAND_TIMEOUT:
607 1.2.2.2 bouyer case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
608 1.2.2.2 bouyer case I2O_SCSI_DSC_LUN_INVALID:
609 1.2.2.2 bouyer case I2O_SCSI_DSC_SCSI_TID_INVALID:
610 1.2.2.2 bouyer xs->error = XS_TIMEOUT;
611 1.2.2.2 bouyer break;
612 1.2.2.2 bouyer default:
613 1.2.2.2 bouyer xs->error = XS_DRIVER_STUFFUP;
614 1.2.2.2 bouyer break;
615 1.2.2.2 bouyer }
616 1.2.2.2 bouyer printf("%s: HBA status 0x%02x\n", sc->sc_dv.dv_xname,
617 1.2.2.7 bouyer rb->hbastatus);
618 1.2.2.7 bouyer } else if (rb->scsistatus != SCSI_OK) {
619 1.2.2.7 bouyer switch (rb->scsistatus) {
620 1.2.2.2 bouyer case SCSI_CHECK:
621 1.2.2.2 bouyer xs->error = XS_SENSE;
622 1.2.2.2 bouyer sl = le32toh(rb->senselen);
623 1.2.2.2 bouyer if (sl > sizeof(xs->sense.scsi_sense))
624 1.2.2.4 bouyer sl = sizeof(xs->sense.scsi_sense);
625 1.2.2.2 bouyer memcpy(&xs->sense.scsi_sense, rb->sense, sl);
626 1.2.2.2 bouyer break;
627 1.2.2.5 bouyer case SCSI_QUEUE_FULL:
628 1.2.2.2 bouyer case SCSI_BUSY:
629 1.2.2.2 bouyer xs->error = XS_BUSY;
630 1.2.2.2 bouyer break;
631 1.2.2.2 bouyer default:
632 1.2.2.2 bouyer xs->error = XS_DRIVER_STUFFUP;
633 1.2.2.2 bouyer break;
634 1.2.2.2 bouyer }
635 1.2.2.2 bouyer } else
636 1.2.2.2 bouyer xs->error = XS_NOERROR;
637 1.2.2.2 bouyer
638 1.2.2.7 bouyer xs->resid = xs->datalen - le32toh(rb->datalen);
639 1.2.2.7 bouyer xs->status = rb->scsistatus;
640 1.2.2.2 bouyer }
641 1.2.2.2 bouyer
642 1.2.2.2 bouyer /* Free the message wrapper and pass the news to scsipi. */
643 1.2.2.7 bouyer if (xs->datalen != 0)
644 1.2.2.7 bouyer iop_msg_unmap(iop, im);
645 1.2.2.7 bouyer iop_msg_free(iop, im);
646 1.2.2.2 bouyer scsipi_done(xs);
647 1.2.2.2 bouyer }
648 1.2.2.2 bouyer
649 1.2.2.2 bouyer /*
650 1.2.2.2 bouyer * ioctl hook; used here only to initiate low-level rescans.
651 1.2.2.2 bouyer */
652 1.2.2.2 bouyer static int
653 1.2.2.5 bouyer iopsp_ioctl(struct scsipi_channel *chan, u_long cmd, caddr_t data, int flag,
654 1.2.2.2 bouyer struct proc *p)
655 1.2.2.2 bouyer {
656 1.2.2.2 bouyer int rv;
657 1.2.2.2 bouyer
658 1.2.2.2 bouyer switch (cmd) {
659 1.2.2.2 bouyer case SCBUSIOLLSCAN:
660 1.2.2.5 bouyer rv = iopsp_rescan(
661 1.2.2.5 bouyer (struct iopsp_softc *)chan->chan_adapter->adapt_dev);
662 1.2.2.7 bouyer /*
663 1.2.2.7 bouyer * If it's boot time, the bus will have been scanned and the
664 1.2.2.7 bouyer * maps built. Locking would stop re-configuration, but we
665 1.2.2.7 bouyer * want to fake success.
666 1.2.2.7 bouyer */
667 1.2.2.7 bouyer if (p != &proc0)
668 1.2.2.7 bouyer rv = iopsp_rescan(
669 1.2.2.7 bouyer (struct iopsp_softc *)chan->chan_adapter->adapt_dev);
670 1.2.2.7 bouyer else
671 1.2.2.7 bouyer rv = 0;
672 1.2.2.2 bouyer break;
673 1.2.2.7 bouyer
674 1.2.2.2 bouyer default:
675 1.2.2.7 bouyer rv = ENOTTY;
676 1.2.2.2 bouyer break;
677 1.2.2.2 bouyer }
678 1.2.2.2 bouyer
679 1.2.2.2 bouyer return (rv);
680 1.2.2.2 bouyer }
681 1.2.2.7 bouyer
682 1.2.2.7 bouyer /*
683 1.2.2.7 bouyer * The number of openings available to us has changed, so inform scsipi.
684 1.2.2.7 bouyer */
685 1.2.2.7 bouyer static void
686 1.2.2.7 bouyer iopsp_adjqparam(struct device *dv, int mpi)
687 1.2.2.7 bouyer {
688 1.2.2.7 bouyer int s;
689 1.2.2.7 bouyer
690 1.2.2.7 bouyer /* XXX */
691 1.2.2.7 bouyer s = splbio();
692 1.2.2.7 bouyer ((struct iopsp_softc *)dv)->sc_adapter.adapt_openings = mpi;
693 1.2.2.7 bouyer splx(s);
694 1.2.2.7 bouyer }
695