iopsp.c revision 1.2.2.2 1 1.2.2.2 bouyer /* $NetBSD: iopsp.c,v 1.2.2.2 2000/11/22 17:34:20 bouyer Exp $ */
2 1.2.2.2 bouyer
3 1.2.2.2 bouyer /*-
4 1.2.2.2 bouyer * Copyright (c) 2000 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.2 bouyer * Raw SCSI/FC-AL device support for I2O. I2O presents SCSI devices
41 1.2.2.2 bouyer * individually; we group them by controller 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.2 bouyer
57 1.2.2.2 bouyer #include <machine/bswap.h>
58 1.2.2.2 bouyer #include <machine/bus.h>
59 1.2.2.2 bouyer
60 1.2.2.2 bouyer #include <dev/scsipi/scsi_all.h>
61 1.2.2.2 bouyer #include <dev/scsipi/scsi_disk.h>
62 1.2.2.2 bouyer #include <dev/scsipi/scsipi_all.h>
63 1.2.2.2 bouyer #include <dev/scsipi/scsiconf.h>
64 1.2.2.2 bouyer
65 1.2.2.2 bouyer #include <dev/i2o/i2o.h>
66 1.2.2.2 bouyer #include <dev/i2o/iopreg.h>
67 1.2.2.2 bouyer #include <dev/i2o/iopvar.h>
68 1.2.2.2 bouyer #include <dev/i2o/iopspvar.h>
69 1.2.2.2 bouyer
70 1.2.2.2 bouyer static void iopsp_attach(struct device *, struct device *, void *);
71 1.2.2.2 bouyer static void iopsp_intr(struct device *, struct iop_msg *, void *);
72 1.2.2.2 bouyer static int iopsp_ioctl(struct scsipi_link *, u_long, caddr_t, int,
73 1.2.2.2 bouyer struct proc *);
74 1.2.2.2 bouyer static int iopsp_match(struct device *, struct cfdata *, void *);
75 1.2.2.2 bouyer static void iopsp_minphys(struct buf *);
76 1.2.2.2 bouyer static int iopsp_rescan(struct iopsp_softc *);
77 1.2.2.2 bouyer static void iopsp_scan(struct iopsp_softc *);
78 1.2.2.2 bouyer static int iopsp_scsi_abort(struct iopsp_softc *, struct iop_msg *);
79 1.2.2.2 bouyer static int iopsp_scsi_cmd(struct scsipi_xfer *);
80 1.2.2.2 bouyer
81 1.2.2.2 bouyer static struct scsipi_device iopsp_dev = {
82 1.2.2.2 bouyer NULL, /* Use default error handler */
83 1.2.2.2 bouyer NULL, /* have a queue, served by this */
84 1.2.2.2 bouyer NULL, /* have no async handler */
85 1.2.2.2 bouyer NULL, /* Use default 'done' routine */
86 1.2.2.2 bouyer };
87 1.2.2.2 bouyer
88 1.2.2.2 bouyer struct cfattach iopsp_ca = {
89 1.2.2.2 bouyer sizeof(struct iopsp_softc), iopsp_match, iopsp_attach
90 1.2.2.2 bouyer };
91 1.2.2.2 bouyer
92 1.2.2.2 bouyer /*
93 1.2.2.2 bouyer * Match SCSI and fibre channel ports.
94 1.2.2.2 bouyer */
95 1.2.2.2 bouyer static int
96 1.2.2.2 bouyer iopsp_match(struct device *parent, struct cfdata *match, void *aux)
97 1.2.2.2 bouyer {
98 1.2.2.2 bouyer struct iop_attach_args *ia;
99 1.2.2.2 bouyer struct {
100 1.2.2.2 bouyer struct i2o_param_op_results pr;
101 1.2.2.2 bouyer struct i2o_param_read_results prr;
102 1.2.2.2 bouyer struct i2o_param_hba_ctlr_info ci;
103 1.2.2.2 bouyer } __attribute__ ((__packed__)) param;
104 1.2.2.2 bouyer
105 1.2.2.2 bouyer ia = aux;
106 1.2.2.2 bouyer
107 1.2.2.2 bouyer if (ia->ia_class != I2O_CLASS_BUS_ADAPTER_PORT)
108 1.2.2.2 bouyer return (0);
109 1.2.2.2 bouyer
110 1.2.2.2 bouyer if (iop_params_get((struct iop_softc *)parent, ia->ia_tid,
111 1.2.2.2 bouyer I2O_PARAM_HBA_CTLR_INFO, ¶m, sizeof(param)) != 0)
112 1.2.2.2 bouyer return (0);
113 1.2.2.2 bouyer
114 1.2.2.2 bouyer /* XXX */
115 1.2.2.2 bouyer return (param.ci.bustype == I2O_HBA_BUS_SCSI ||
116 1.2.2.2 bouyer param.ci.bustype == I2O_HBA_BUS_FCA);
117 1.2.2.2 bouyer }
118 1.2.2.2 bouyer
119 1.2.2.2 bouyer /*
120 1.2.2.2 bouyer * Attach a supported device.
121 1.2.2.2 bouyer */
122 1.2.2.2 bouyer static void
123 1.2.2.2 bouyer iopsp_attach(struct device *parent, struct device *self, void *aux)
124 1.2.2.2 bouyer {
125 1.2.2.2 bouyer struct iop_attach_args *ia;
126 1.2.2.2 bouyer struct iopsp_softc *sc;
127 1.2.2.2 bouyer struct scsipi_link *sc_link;
128 1.2.2.2 bouyer struct iop_softc *iop;
129 1.2.2.2 bouyer struct {
130 1.2.2.2 bouyer struct i2o_param_op_results pr;
131 1.2.2.2 bouyer struct i2o_param_read_results prr;
132 1.2.2.2 bouyer union {
133 1.2.2.2 bouyer struct i2o_param_device_identity di;
134 1.2.2.2 bouyer struct i2o_param_hba_ctlr_info ci;
135 1.2.2.2 bouyer struct i2o_param_hba_scsi_ctlr_info sci;
136 1.2.2.2 bouyer } p;
137 1.2.2.2 bouyer } __attribute__ ((__packed__)) param;
138 1.2.2.2 bouyer char ident[64];
139 1.2.2.2 bouyer int fcal;
140 1.2.2.2 bouyer #ifdef I2OVERBOSE
141 1.2.2.2 bouyer int size;
142 1.2.2.2 bouyer #endif
143 1.2.2.2 bouyer
144 1.2.2.2 bouyer ia = (struct iop_attach_args *)aux;
145 1.2.2.2 bouyer sc = (struct iopsp_softc *)self;
146 1.2.2.2 bouyer iop = (struct iop_softc *)parent;
147 1.2.2.2 bouyer sc->sc_tid = ia->ia_tid;
148 1.2.2.2 bouyer
149 1.2.2.2 bouyer /* Register us as an initiator. */
150 1.2.2.2 bouyer sc->sc_ii.ii_dv = self;
151 1.2.2.2 bouyer sc->sc_ii.ii_intr = iopsp_intr;
152 1.2.2.2 bouyer sc->sc_ii.ii_flags = 0;
153 1.2.2.2 bouyer if (iop_initiator_register(iop, &sc->sc_ii) != 0) {
154 1.2.2.2 bouyer printf("%s: unable to register as an initiator",
155 1.2.2.2 bouyer sc->sc_dv.dv_xname);
156 1.2.2.2 bouyer return;
157 1.2.2.2 bouyer }
158 1.2.2.2 bouyer
159 1.2.2.2 bouyer if (iop_params_get(iop, ia->ia_tid, I2O_PARAM_HBA_CTLR_INFO, ¶m,
160 1.2.2.2 bouyer sizeof(param)) != 0) {
161 1.2.2.2 bouyer printf("%s: unable to retrieve parameters\n",
162 1.2.2.2 bouyer sc->sc_dv.dv_xname);
163 1.2.2.2 bouyer goto bad;
164 1.2.2.2 bouyer }
165 1.2.2.2 bouyer
166 1.2.2.2 bouyer fcal = (param.p.ci.bustype == I2O_HBA_BUS_FCA); /* XXX */
167 1.2.2.2 bouyer
168 1.2.2.2 bouyer /*
169 1.2.2.2 bouyer * Say what the device is. If we can find out what the controling
170 1.2.2.2 bouyer * device is, say what that is too.
171 1.2.2.2 bouyer */
172 1.2.2.2 bouyer printf(": %s port", fcal ? "FC-AL" : "SCSI");
173 1.2.2.2 bouyer if (iop_params_get(iop, ia->ia_tid, I2O_PARAM_DEVICE_IDENTITY, ¶m,
174 1.2.2.2 bouyer sizeof(param)) == 0) {
175 1.2.2.2 bouyer iop_strvis(param.p.di.vendorinfo,
176 1.2.2.2 bouyer sizeof(param.p.di.vendorinfo), ident, sizeof(ident));
177 1.2.2.2 bouyer printf(" <%s, ", ident);
178 1.2.2.2 bouyer iop_strvis(param.p.di.productinfo,
179 1.2.2.2 bouyer sizeof(param.p.di.productinfo), ident, sizeof(ident));
180 1.2.2.2 bouyer printf("%s, ", ident);
181 1.2.2.2 bouyer iop_strvis(param.p.di.revlevel,
182 1.2.2.2 bouyer sizeof(param.p.di.revlevel), ident, sizeof(ident));
183 1.2.2.2 bouyer printf("%s> ", ident);
184 1.2.2.2 bouyer }
185 1.2.2.2 bouyer printf("\n");
186 1.2.2.2 bouyer
187 1.2.2.2 bouyer if (iop_params_get(iop, ia->ia_tid, I2O_PARAM_HBA_SCSI_CTLR_INFO,
188 1.2.2.2 bouyer ¶m, sizeof(param)) != 0) {
189 1.2.2.2 bouyer printf("%s: unable to retrieve controller parameters\n",
190 1.2.2.2 bouyer sc->sc_dv.dv_xname);
191 1.2.2.2 bouyer goto bad;
192 1.2.2.2 bouyer }
193 1.2.2.2 bouyer
194 1.2.2.2 bouyer #ifdef I2OVERBOSE
195 1.2.2.2 bouyer printf("%s: %d-bit, max sync rate %dMHz, initiator ID %d\n",
196 1.2.2.2 bouyer sc->sc_dv.dv_xname, param.p.sci.maxdatawidth,
197 1.2.2.2 bouyer (u_int32_t)le64toh(param.p.sci.maxsyncrate) / 1000,
198 1.2.2.2 bouyer le32toh(param.p.sci.initiatorid));
199 1.2.2.2 bouyer #endif
200 1.2.2.2 bouyer
201 1.2.2.2 bouyer sc->sc_adapter.scsipi_cmd = iopsp_scsi_cmd;
202 1.2.2.2 bouyer sc->sc_adapter.scsipi_minphys = iopsp_minphys;
203 1.2.2.2 bouyer sc->sc_adapter.scsipi_ioctl = iopsp_ioctl;
204 1.2.2.2 bouyer
205 1.2.2.2 bouyer sc_link = &sc->sc_link;
206 1.2.2.2 bouyer sc_link->type = BUS_SCSI;
207 1.2.2.2 bouyer sc_link->device = &iopsp_dev;
208 1.2.2.2 bouyer sc_link->adapter = &sc->sc_adapter;
209 1.2.2.2 bouyer sc_link->adapter_softc = sc;
210 1.2.2.2 bouyer sc_link->scsipi_scsi.channel = 0;
211 1.2.2.2 bouyer sc_link->scsipi_scsi.adapter_target = le32toh(param.p.sci.initiatorid);
212 1.2.2.2 bouyer sc_link->scsipi_scsi.max_target =
213 1.2.2.2 bouyer fcal ? IOPSP_MAX_FCAL_TARGET : param.p.sci.maxdatawidth - 1;
214 1.2.2.2 bouyer sc_link->scsipi_scsi.max_lun = IOPSP_MAX_LUN;
215 1.2.2.2 bouyer sc_link->openings = iop->sc_maxqueuecnt; /* XXX */
216 1.2.2.2 bouyer
217 1.2.2.2 bouyer #ifdef I2OVERBOSE
218 1.2.2.2 bouyer /*
219 1.2.2.2 bouyer * Allocate the target map. Currently used for informational
220 1.2.2.2 bouyer * purposes only.
221 1.2.2.2 bouyer */
222 1.2.2.2 bouyer size = (sc_link->scsipi_scsi.max_target + 1) *
223 1.2.2.2 bouyer sizeof(struct iopsp_target);
224 1.2.2.2 bouyer sc->sc_targetmap = malloc(size, M_DEVBUF, M_NOWAIT);
225 1.2.2.2 bouyer memset(sc->sc_targetmap, 0, size);
226 1.2.2.2 bouyer #endif
227 1.2.2.2 bouyer
228 1.2.2.2 bouyer /* Build the two maps, and attach to scsipi. */
229 1.2.2.2 bouyer iopsp_scan(sc);
230 1.2.2.2 bouyer config_found(self, sc_link, scsiprint);
231 1.2.2.2 bouyer return;
232 1.2.2.2 bouyer
233 1.2.2.2 bouyer bad:
234 1.2.2.2 bouyer iop_initiator_unregister(iop, &sc->sc_ii);
235 1.2.2.2 bouyer }
236 1.2.2.2 bouyer
237 1.2.2.2 bouyer /*
238 1.2.2.2 bouyer * Determine which devices we control, and enter them into the maps.
239 1.2.2.2 bouyer */
240 1.2.2.2 bouyer static void
241 1.2.2.2 bouyer iopsp_scan(struct iopsp_softc *sc)
242 1.2.2.2 bouyer {
243 1.2.2.2 bouyer struct iop_softc *iop;
244 1.2.2.2 bouyer struct i2o_lct_entry *le;
245 1.2.2.2 bouyer struct scsipi_link *sc_link;
246 1.2.2.2 bouyer struct {
247 1.2.2.2 bouyer struct i2o_param_op_results pr;
248 1.2.2.2 bouyer struct i2o_param_read_results prr;
249 1.2.2.2 bouyer struct i2o_param_scsi_device_info sdi;
250 1.2.2.2 bouyer } __attribute__ ((__packed__)) param;
251 1.2.2.2 bouyer int tid, nent, i, targ, lun, size;
252 1.2.2.2 bouyer u_short *tidmap;
253 1.2.2.2 bouyer #ifdef I2OVERBOSE
254 1.2.2.2 bouyer struct iopsp_target *it;
255 1.2.2.2 bouyer int syncrate;
256 1.2.2.2 bouyer #endif
257 1.2.2.2 bouyer
258 1.2.2.2 bouyer iop = (struct iop_softc *)sc->sc_dv.dv_parent;
259 1.2.2.2 bouyer sc_link = &sc->sc_link;
260 1.2.2.2 bouyer
261 1.2.2.2 bouyer /*
262 1.2.2.2 bouyer * Allocate memory for the target/LUN -> TID map. Use zero to
263 1.2.2.2 bouyer * denote absent targets (zero is the TID of the I2O executive,
264 1.2.2.2 bouyer * and we never address that here).
265 1.2.2.2 bouyer */
266 1.2.2.2 bouyer size = (sc_link->scsipi_scsi.max_target + 1) *
267 1.2.2.2 bouyer (IOPSP_MAX_LUN + 1) * sizeof(u_short);
268 1.2.2.2 bouyer tidmap = malloc(size, M_DEVBUF, M_NOWAIT);
269 1.2.2.2 bouyer memset(tidmap, 0, size); /* XXX */
270 1.2.2.2 bouyer
271 1.2.2.2 bouyer #ifdef I2OVERBOSE
272 1.2.2.2 bouyer for (i = 0; i <= sc_link->scsipi_scsi.max_target; i++)
273 1.2.2.2 bouyer sc->sc_targetmap[i].it_flags &= ~IT_PRESENT;
274 1.2.2.2 bouyer #endif
275 1.2.2.2 bouyer
276 1.2.2.2 bouyer if (iop_lct_lock(iop) != 0)
277 1.2.2.2 bouyer return;
278 1.2.2.2 bouyer
279 1.2.2.2 bouyer nent = iop->sc_nlctent;
280 1.2.2.2 bouyer for (i = 0, le = iop->sc_lct->entry; i < nent; i++, le++) {
281 1.2.2.2 bouyer switch (le16toh(le->classid) & 4095) {
282 1.2.2.2 bouyer case I2O_CLASS_SCSI_PERIPHERAL:
283 1.2.2.2 bouyer case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
284 1.2.2.2 bouyer break;
285 1.2.2.2 bouyer default:
286 1.2.2.2 bouyer continue;
287 1.2.2.2 bouyer }
288 1.2.2.2 bouyer if (((le32toh(le->usertid) >> 12) & 4095) != sc->sc_tid)
289 1.2.2.2 bouyer continue;
290 1.2.2.2 bouyer
291 1.2.2.2 bouyer tid = le32toh(le->localtid) & 4095;
292 1.2.2.2 bouyer
293 1.2.2.2 bouyer if (iop_params_get(iop, tid, I2O_PARAM_SCSI_DEVICE_INFO, ¶m,
294 1.2.2.2 bouyer sizeof(param)) != 0) {
295 1.2.2.2 bouyer printf("%s: unable to retrieve device parameters\n",
296 1.2.2.2 bouyer sc->sc_dv.dv_xname);
297 1.2.2.2 bouyer continue;
298 1.2.2.2 bouyer }
299 1.2.2.2 bouyer targ = le32toh(param.sdi.identifier);
300 1.2.2.2 bouyer lun = param.sdi.luninfo[1];
301 1.2.2.2 bouyer
302 1.2.2.2 bouyer /* If the device is in use by another module, ignore it. */
303 1.2.2.2 bouyer if (!iop_tid_inuse(iop, tid)) {
304 1.2.2.2 bouyer #ifdef I2OVERBOSE
305 1.2.2.2 bouyer if (sc->sc_tidmap == NULL ||
306 1.2.2.2 bouyer IOPSP_TIDMAP(sc->sc_tidmap, targ, lun) !=
307 1.2.2.2 bouyer IOPSP_TID_INUSE)
308 1.2.2.2 bouyer printf("%s: target %d,%d (tid %d): in use by"
309 1.2.2.2 bouyer " another module\n", sc->sc_dv.dv_xname,
310 1.2.2.2 bouyer targ, lun, tid);
311 1.2.2.2 bouyer #endif
312 1.2.2.2 bouyer IOPSP_TIDMAP(tidmap, targ, lun) = IOPSP_TID_INUSE;
313 1.2.2.2 bouyer continue;
314 1.2.2.2 bouyer }
315 1.2.2.2 bouyer IOPSP_TIDMAP(tidmap, targ, lun) = (u_short)tid;
316 1.2.2.2 bouyer
317 1.2.2.2 bouyer #ifdef I2OVERBOSE
318 1.2.2.2 bouyer /*
319 1.2.2.2 bouyer * If we've already described this target, and nothing has
320 1.2.2.2 bouyer * changed, then don't describe it again.
321 1.2.2.2 bouyer */
322 1.2.2.2 bouyer it = &sc->sc_targetmap[targ];
323 1.2.2.2 bouyer it->it_flags |= IT_PRESENT;
324 1.2.2.2 bouyer syncrate = ((int)le64toh(param.sdi.negsyncrate) + 500) / 1000;
325 1.2.2.2 bouyer if (it->it_width == param.sdi.negdatawidth &&
326 1.2.2.2 bouyer it->it_offset == param.sdi.negoffset &&
327 1.2.2.2 bouyer it->it_syncrate == syncrate)
328 1.2.2.2 bouyer continue;
329 1.2.2.2 bouyer
330 1.2.2.2 bouyer it->it_width = param.sdi.negdatawidth;
331 1.2.2.2 bouyer it->it_offset = param.sdi.negoffset;
332 1.2.2.2 bouyer it->it_syncrate = syncrate;
333 1.2.2.2 bouyer
334 1.2.2.2 bouyer printf("%s: target %d (tid %d): %d-bit, ", sc->sc_dv.dv_xname,
335 1.2.2.2 bouyer targ, tid, it->it_width);
336 1.2.2.2 bouyer if (it->it_syncrate == 0)
337 1.2.2.2 bouyer printf("asynchronous\n");
338 1.2.2.2 bouyer else
339 1.2.2.2 bouyer printf("synchronous at %dMHz, offset 0x%x\n",
340 1.2.2.2 bouyer it->it_syncrate, it->it_offset);
341 1.2.2.2 bouyer #endif
342 1.2.2.2 bouyer }
343 1.2.2.2 bouyer
344 1.2.2.2 bouyer iop_lct_unlock(iop);
345 1.2.2.2 bouyer
346 1.2.2.2 bouyer #ifdef I2OVERBOSE
347 1.2.2.2 bouyer for (i = 0; i <= sc_link->scsipi_scsi.max_target; i++)
348 1.2.2.2 bouyer if ((sc->sc_targetmap[i].it_flags & IT_PRESENT) == 0)
349 1.2.2.2 bouyer sc->sc_targetmap[i].it_width = 0;
350 1.2.2.2 bouyer #endif
351 1.2.2.2 bouyer
352 1.2.2.2 bouyer /* Swap in the new map and return. */
353 1.2.2.2 bouyer if (sc->sc_tidmap != NULL)
354 1.2.2.2 bouyer free(sc->sc_tidmap, M_DEVBUF);
355 1.2.2.2 bouyer sc->sc_tidmap = tidmap;
356 1.2.2.2 bouyer sc->sc_chgindicator = iop->sc_lct->changeindicator;
357 1.2.2.2 bouyer }
358 1.2.2.2 bouyer
359 1.2.2.2 bouyer /*
360 1.2.2.2 bouyer * Adjust the size an I/O request.
361 1.2.2.2 bouyer */
362 1.2.2.2 bouyer static void
363 1.2.2.2 bouyer iopsp_minphys(struct buf *bp)
364 1.2.2.2 bouyer {
365 1.2.2.2 bouyer
366 1.2.2.2 bouyer if (bp->b_bcount > IOP_MAX_XFER)
367 1.2.2.2 bouyer bp->b_bcount = IOP_MAX_XFER;
368 1.2.2.2 bouyer minphys(bp);
369 1.2.2.2 bouyer }
370 1.2.2.2 bouyer
371 1.2.2.2 bouyer /*
372 1.2.2.2 bouyer * Ask the bus port to perform a rescan. XXX It's unclear whether or not
373 1.2.2.2 bouyer * TIDs for established targets will change.
374 1.2.2.2 bouyer */
375 1.2.2.2 bouyer static int
376 1.2.2.2 bouyer iopsp_rescan(struct iopsp_softc *sc)
377 1.2.2.2 bouyer {
378 1.2.2.2 bouyer struct iop_softc *iop;
379 1.2.2.2 bouyer struct iop_msg *im;
380 1.2.2.2 bouyer struct i2o_hba_bus_scan *mb;
381 1.2.2.2 bouyer int rv;
382 1.2.2.2 bouyer
383 1.2.2.2 bouyer iop = (struct iop_softc *)sc->sc_dv.dv_parent;
384 1.2.2.2 bouyer
385 1.2.2.2 bouyer if (iop_msg_alloc(iop, &sc->sc_ii, &im, IM_NOINTR) != 0)
386 1.2.2.2 bouyer return (-1);
387 1.2.2.2 bouyer
388 1.2.2.2 bouyer mb = (struct i2o_hba_bus_scan *)im->im_msg;
389 1.2.2.2 bouyer mb->msgflags = I2O_MSGFLAGS(i2o_hba_bus_scan);
390 1.2.2.2 bouyer mb->msgfunc = I2O_MSGFUNC(sc->sc_tid, I2O_HBA_BUS_SCAN);
391 1.2.2.2 bouyer mb->msgictx = sc->sc_ii.ii_ictx;
392 1.2.2.2 bouyer mb->msgtctx = im->im_tctx;
393 1.2.2.2 bouyer
394 1.2.2.2 bouyer if (iop_msg_enqueue(iop, im) != 0)
395 1.2.2.2 bouyer rv = -1;
396 1.2.2.2 bouyer else if(iop_msg_wait(iop, im, 1000) != 0)
397 1.2.2.2 bouyer rv = -1;
398 1.2.2.2 bouyer else
399 1.2.2.2 bouyer rv = 0;
400 1.2.2.2 bouyer iop_msg_free(iop, &sc->sc_ii, im);
401 1.2.2.2 bouyer if (rv != 0)
402 1.2.2.2 bouyer return (rv);
403 1.2.2.2 bouyer
404 1.2.2.2 bouyer /*
405 1.2.2.2 bouyer * Re-read the LCT and determine if it has changed. XXX This should
406 1.2.2.2 bouyer * probably be accomplished by noticing a CHANGED_LCT event from the
407 1.2.2.2 bouyer * executive.
408 1.2.2.2 bouyer */
409 1.2.2.2 bouyer if (iop_lct_get(iop))
410 1.2.2.2 bouyer return (-1);
411 1.2.2.2 bouyer if (iop->sc_lct->changeindicator == sc->sc_chgindicator)
412 1.2.2.2 bouyer return (0);
413 1.2.2.2 bouyer
414 1.2.2.2 bouyer /* Rebuild the target/LUN -> TID map, and return. */
415 1.2.2.2 bouyer iopsp_scan(sc);
416 1.2.2.2 bouyer return (0);
417 1.2.2.2 bouyer }
418 1.2.2.2 bouyer
419 1.2.2.2 bouyer /*
420 1.2.2.2 bouyer * Start a SCSI command.
421 1.2.2.2 bouyer */
422 1.2.2.2 bouyer static int
423 1.2.2.2 bouyer iopsp_scsi_cmd(struct scsipi_xfer *xs)
424 1.2.2.2 bouyer {
425 1.2.2.2 bouyer struct scsipi_link *sc_link;
426 1.2.2.2 bouyer struct iopsp_softc *sc;
427 1.2.2.2 bouyer struct iop_msg *im;
428 1.2.2.2 bouyer struct iop_softc *iop;
429 1.2.2.2 bouyer struct i2o_scsi_scb_exec *mb;
430 1.2.2.2 bouyer int error, flags, tid;
431 1.2.2.2 bouyer
432 1.2.2.2 bouyer sc_link = xs->sc_link;
433 1.2.2.2 bouyer flags = xs->xs_control;
434 1.2.2.2 bouyer sc = sc_link->adapter_softc;
435 1.2.2.2 bouyer iop = (struct iop_softc *)sc->sc_dv.dv_parent;
436 1.2.2.2 bouyer
437 1.2.2.2 bouyer tid = IOPSP_TIDMAP(sc->sc_tidmap, sc_link->scsipi_scsi.target,
438 1.2.2.2 bouyer sc_link->scsipi_scsi.lun);
439 1.2.2.2 bouyer if (tid == IOPSP_TID_ABSENT || tid == IOPSP_TID_INUSE) {
440 1.2.2.2 bouyer xs->error = XS_SELTIMEOUT;
441 1.2.2.2 bouyer return (COMPLETE);
442 1.2.2.2 bouyer }
443 1.2.2.2 bouyer
444 1.2.2.2 bouyer SC_DEBUG(sc_link, SDEV_DB2, ("iopsp_scsi_cmd\n"));
445 1.2.2.2 bouyer
446 1.2.2.2 bouyer /* Need to reset the target? */
447 1.2.2.2 bouyer if ((flags & XS_CTL_RESET) != 0) {
448 1.2.2.2 bouyer if (iop_simple_cmd(iop, tid, I2O_SCSI_DEVICE_RESET,
449 1.2.2.2 bouyer sc->sc_ii.ii_ictx) != 0) {
450 1.2.2.2 bouyer #ifdef I2ODEBUG
451 1.2.2.2 bouyer printf("%s: reset failed\n", sc->sc_dv.dv_xname);
452 1.2.2.2 bouyer #endif
453 1.2.2.2 bouyer xs->error = XS_DRIVER_STUFFUP;
454 1.2.2.2 bouyer }
455 1.2.2.2 bouyer return (COMPLETE);
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.2 bouyer if (xs->cmdlen > 16) {
460 1.2.2.2 bouyer printf("%s: CDB too large\n", sc->sc_dv.dv_xname);
461 1.2.2.2 bouyer xs->error = XS_DRIVER_STUFFUP;
462 1.2.2.2 bouyer return (COMPLETE);
463 1.2.2.2 bouyer }
464 1.2.2.2 bouyer #endif
465 1.2.2.2 bouyer
466 1.2.2.2 bouyer if (iop_msg_alloc(iop, &sc->sc_ii, &im,
467 1.2.2.2 bouyer (flags & (XS_CTL_POLL | XS_CTL_NOSLEEP)) != 0 ? IM_NOWAIT : 0)) {
468 1.2.2.2 bouyer xs->error = XS_DRIVER_STUFFUP;
469 1.2.2.2 bouyer return (TRY_AGAIN_LATER);
470 1.2.2.2 bouyer }
471 1.2.2.2 bouyer im->im_dvcontext = xs;
472 1.2.2.2 bouyer
473 1.2.2.2 bouyer mb = (struct i2o_scsi_scb_exec *)im->im_msg;
474 1.2.2.2 bouyer mb->msgflags = I2O_MSGFLAGS(i2o_scsi_scb_exec);
475 1.2.2.2 bouyer mb->msgfunc = I2O_MSGFUNC(tid, I2O_SCSI_SCB_EXEC);
476 1.2.2.2 bouyer mb->msgictx = sc->sc_ii.ii_ictx;
477 1.2.2.2 bouyer mb->msgtctx = im->im_tctx;
478 1.2.2.2 bouyer mb->flags = xs->cmdlen | I2O_SCB_FLAG_ENABLE_DISCONNECT |
479 1.2.2.2 bouyer I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
480 1.2.2.2 bouyer memset(mb->cdb, 0, sizeof(mb->cdb));
481 1.2.2.2 bouyer memcpy(mb->cdb, xs->cmd, xs->cmdlen);
482 1.2.2.2 bouyer mb->datalen = xs->datalen;
483 1.2.2.2 bouyer
484 1.2.2.2 bouyer /* XXX */
485 1.2.2.2 bouyer if ((xs->sc_link->quirks & SDEV_NOTAG) == 0 &&
486 1.2.2.2 bouyer xs->cmd->opcode != INQUIRY &&
487 1.2.2.2 bouyer xs->cmd->opcode != TEST_UNIT_READY &&
488 1.2.2.2 bouyer xs->cmd->opcode != REQUEST_SENSE) {
489 1.2.2.2 bouyer if (xs->bp != NULL && (xs->bp->b_flags & B_ASYNC) != 0)
490 1.2.2.2 bouyer mb->flags |= I2O_SCB_FLAG_ORDERED_QUEUE_TAG;
491 1.2.2.2 bouyer else
492 1.2.2.2 bouyer mb->flags |= I2O_SCB_FLAG_SIMPLE_QUEUE_TAG;
493 1.2.2.2 bouyer }
494 1.2.2.2 bouyer
495 1.2.2.2 bouyer if (xs->datalen != 0) {
496 1.2.2.2 bouyer error = iop_msg_map(iop, im, xs->data, xs->datalen,
497 1.2.2.2 bouyer (flags & XS_CTL_DATA_OUT) == 0);
498 1.2.2.2 bouyer if (error) {
499 1.2.2.2 bouyer #ifdef I2ODEBUG
500 1.2.2.2 bouyer printf("%s: error %d mapping xfer\n",
501 1.2.2.2 bouyer sc->sc_dv.dv_xname, error);
502 1.2.2.2 bouyer #endif
503 1.2.2.2 bouyer xs->error = XS_DRIVER_STUFFUP;
504 1.2.2.2 bouyer iop_msg_free(iop, &sc->sc_ii, im);
505 1.2.2.2 bouyer return (COMPLETE);
506 1.2.2.2 bouyer }
507 1.2.2.2 bouyer if ((flags & XS_CTL_DATA_IN) == 0)
508 1.2.2.2 bouyer mb->flags |= I2O_SCB_FLAG_XFER_TO_DEVICE;
509 1.2.2.2 bouyer else
510 1.2.2.2 bouyer mb->flags |= I2O_SCB_FLAG_XFER_FROM_DEVICE;
511 1.2.2.2 bouyer }
512 1.2.2.2 bouyer
513 1.2.2.2 bouyer /*
514 1.2.2.2 bouyer * If the command is allowed to execute asynchronously, enqueue it
515 1.2.2.2 bouyer * with the IOP. XXX Time out async commands?
516 1.2.2.2 bouyer */
517 1.2.2.2 bouyer if ((flags & XS_CTL_POLL) == 0) {
518 1.2.2.2 bouyer if (iop_msg_enqueue(iop, im)) {
519 1.2.2.2 bouyer iop_msg_unmap(iop, im);
520 1.2.2.2 bouyer iop_msg_free(iop, &sc->sc_ii, im);
521 1.2.2.2 bouyer #ifdef I2ODEBUG
522 1.2.2.2 bouyer printf("%s: can't enqueue msg\n", sc->sc_dv.dv_xname);
523 1.2.2.2 bouyer #endif
524 1.2.2.2 bouyer xs->error = XS_DRIVER_STUFFUP;
525 1.2.2.2 bouyer return (COMPLETE);
526 1.2.2.2 bouyer }
527 1.2.2.2 bouyer return (SUCCESSFULLY_QUEUED);
528 1.2.2.2 bouyer }
529 1.2.2.2 bouyer
530 1.2.2.2 bouyer if (iop_msg_send(iop, im, xs->timeout)) {
531 1.2.2.2 bouyer scsi_print_addr(xs->sc_link);
532 1.2.2.2 bouyer printf("timeout; aborting command\n");
533 1.2.2.2 bouyer if (iopsp_scsi_abort(sc, im)) {
534 1.2.2.2 bouyer scsi_print_addr(xs->sc_link);
535 1.2.2.2 bouyer printf("abort failed\n");
536 1.2.2.2 bouyer }
537 1.2.2.2 bouyer xs->error = XS_DRIVER_STUFFUP;
538 1.2.2.2 bouyer }
539 1.2.2.2 bouyer return (COMPLETE);
540 1.2.2.2 bouyer }
541 1.2.2.2 bouyer
542 1.2.2.2 bouyer /*
543 1.2.2.2 bouyer * Abort the specified I2O_SCSI_SCB_EXEC message and its associated SCB.
544 1.2.2.2 bouyer */
545 1.2.2.2 bouyer static int
546 1.2.2.2 bouyer iopsp_scsi_abort(struct iopsp_softc *sc, struct iop_msg *aim)
547 1.2.2.2 bouyer {
548 1.2.2.2 bouyer struct iop_msg *im;
549 1.2.2.2 bouyer struct i2o_scsi_scb_abort *mb;
550 1.2.2.2 bouyer struct iop_softc *iop;
551 1.2.2.2 bouyer int rv;
552 1.2.2.2 bouyer
553 1.2.2.2 bouyer iop = (struct iop_softc *)sc->sc_dv.dv_parent;
554 1.2.2.2 bouyer
555 1.2.2.2 bouyer rv = iop_msg_alloc(iop, &sc->sc_ii, &im, IM_NOWAIT | IM_NOINTR);
556 1.2.2.2 bouyer if (rv != 0)
557 1.2.2.2 bouyer return (rv);
558 1.2.2.2 bouyer
559 1.2.2.2 bouyer mb = (struct i2o_scsi_scb_abort *)im->im_msg;
560 1.2.2.2 bouyer mb->msgflags = I2O_MSGFLAGS(i2o_scsi_scb_abort);
561 1.2.2.2 bouyer mb->msgfunc = I2O_MSGFUNC(aim->im_tid, I2O_SCSI_SCB_ABORT);
562 1.2.2.2 bouyer mb->msgictx = sc->sc_ii.ii_ictx;
563 1.2.2.2 bouyer mb->msgtctx = im->im_tctx;
564 1.2.2.2 bouyer mb->tctxabort = aim->im_tctx;
565 1.2.2.2 bouyer
566 1.2.2.2 bouyer rv = iop_msg_send(iop, im, 1000);
567 1.2.2.2 bouyer iop_msg_free(iop, &sc->sc_ii, 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.2 bouyer struct iop_softc *iop;
582 1.2.2.2 bouyer u_int hba_status, scsi_status, detail;
583 1.2.2.2 bouyer int sl;
584 1.2.2.2 bouyer
585 1.2.2.2 bouyer sc = (struct iopsp_softc *)dv;
586 1.2.2.2 bouyer xs = (struct scsipi_xfer *)im->im_dvcontext;
587 1.2.2.2 bouyer iop = (struct iop_softc *)dv->dv_parent;
588 1.2.2.2 bouyer
589 1.2.2.2 bouyer SC_DEBUG(xs->sc_link, SDEV_DB2, ("iopsp_intr\n"));
590 1.2.2.2 bouyer
591 1.2.2.2 bouyer if (xs->error == XS_NOERROR) {
592 1.2.2.2 bouyer rb = reply;
593 1.2.2.2 bouyer detail = le16toh(rb->detail);
594 1.2.2.2 bouyer hba_status = (detail >> 8) & 0xff;
595 1.2.2.2 bouyer scsi_status = detail & 0xff;
596 1.2.2.2 bouyer
597 1.2.2.2 bouyer if (hba_status != I2O_SCSI_DSC_SUCCESS) {
598 1.2.2.2 bouyer switch (hba_status) {
599 1.2.2.2 bouyer case I2O_SCSI_DSC_ADAPTER_BUSY:
600 1.2.2.2 bouyer case I2O_SCSI_DSC_SCSI_BUS_RESET:
601 1.2.2.2 bouyer case I2O_SCSI_DSC_BUS_BUSY:
602 1.2.2.2 bouyer xs->error = XS_BUSY;
603 1.2.2.2 bouyer break;
604 1.2.2.2 bouyer case I2O_SCSI_DSC_SELECTION_TIMEOUT:
605 1.2.2.2 bouyer xs->error = XS_SELTIMEOUT;
606 1.2.2.2 bouyer break;
607 1.2.2.2 bouyer case I2O_SCSI_DSC_COMMAND_TIMEOUT:
608 1.2.2.2 bouyer case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
609 1.2.2.2 bouyer case I2O_SCSI_DSC_LUN_INVALID:
610 1.2.2.2 bouyer case I2O_SCSI_DSC_SCSI_TID_INVALID:
611 1.2.2.2 bouyer xs->error = XS_TIMEOUT;
612 1.2.2.2 bouyer break;
613 1.2.2.2 bouyer default:
614 1.2.2.2 bouyer xs->error = XS_DRIVER_STUFFUP;
615 1.2.2.2 bouyer break;
616 1.2.2.2 bouyer }
617 1.2.2.2 bouyer #ifdef I2ODEBUG
618 1.2.2.2 bouyer printf("%s: HBA status 0x%02x\n", sc->sc_dv.dv_xname,
619 1.2.2.2 bouyer hba_status);
620 1.2.2.2 bouyer #endif
621 1.2.2.2 bouyer } else if (scsi_status != SCSI_OK) {
622 1.2.2.2 bouyer switch (scsi_status) {
623 1.2.2.2 bouyer case SCSI_CHECK:
624 1.2.2.2 bouyer xs->error = XS_SENSE;
625 1.2.2.2 bouyer sl = le32toh(rb->senselen);
626 1.2.2.2 bouyer if (xs->req_sense_length != 0 &&
627 1.2.2.2 bouyer xs->req_sense_length < sl)
628 1.2.2.2 bouyer sl = xs->req_sense_length;
629 1.2.2.2 bouyer if (sl > sizeof(xs->sense.scsi_sense))
630 1.2.2.2 bouyer sl = le32toh(rb->senselen);
631 1.2.2.2 bouyer memcpy(&xs->sense.scsi_sense, rb->sense, sl);
632 1.2.2.2 bouyer break;
633 1.2.2.2 bouyer case SCSI_BUSY:
634 1.2.2.2 bouyer xs->error = XS_BUSY;
635 1.2.2.2 bouyer break;
636 1.2.2.2 bouyer default:
637 1.2.2.2 bouyer xs->error = XS_DRIVER_STUFFUP;
638 1.2.2.2 bouyer break;
639 1.2.2.2 bouyer }
640 1.2.2.2 bouyer } else
641 1.2.2.2 bouyer xs->error = XS_NOERROR;
642 1.2.2.2 bouyer
643 1.2.2.2 bouyer xs->resid = le32toh(rb->datalen) - xs->datalen;
644 1.2.2.2 bouyer xs->status = scsi_status;
645 1.2.2.2 bouyer }
646 1.2.2.2 bouyer
647 1.2.2.2 bouyer /* Free the message wrapper and pass the news to scsipi. */
648 1.2.2.2 bouyer iop_msg_unmap(iop, im);
649 1.2.2.2 bouyer iop_msg_free(iop, &sc->sc_ii, im);
650 1.2.2.2 bouyer xs->xs_status |= XS_STS_DONE;
651 1.2.2.2 bouyer scsipi_done(xs);
652 1.2.2.2 bouyer }
653 1.2.2.2 bouyer
654 1.2.2.2 bouyer /*
655 1.2.2.2 bouyer * ioctl hook; used here only to initiate low-level rescans.
656 1.2.2.2 bouyer */
657 1.2.2.2 bouyer static int
658 1.2.2.2 bouyer iopsp_ioctl(struct scsipi_link *sc_link, u_long cmd, caddr_t data, int flag,
659 1.2.2.2 bouyer struct proc *p)
660 1.2.2.2 bouyer {
661 1.2.2.2 bouyer int rv;
662 1.2.2.2 bouyer
663 1.2.2.2 bouyer switch (cmd) {
664 1.2.2.2 bouyer case SCBUSIOLLSCAN:
665 1.2.2.2 bouyer rv = iopsp_rescan(sc_link->adapter_softc);
666 1.2.2.2 bouyer break;
667 1.2.2.2 bouyer default:
668 1.2.2.2 bouyer rv = ENXIO;
669 1.2.2.2 bouyer break;
670 1.2.2.2 bouyer }
671 1.2.2.2 bouyer
672 1.2.2.2 bouyer return (rv);
673 1.2.2.2 bouyer }
674