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