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