scsipi_base.c revision 1.1.2.1 1 1.1.2.1 bouyer /* $NetBSD: scsipi_base.c,v 1.1.2.1 1997/07/01 16:52:34 bouyer Exp $ */
2 1.1.2.1 bouyer
3 1.1.2.1 bouyer /*
4 1.1.2.1 bouyer * Copyright (c) 1994, 1995, 1997 Charles M. Hannum. All rights reserved.
5 1.1.2.1 bouyer *
6 1.1.2.1 bouyer * Redistribution and use in source and binary forms, with or without
7 1.1.2.1 bouyer * modification, are permitted provided that the following conditions
8 1.1.2.1 bouyer * are met:
9 1.1.2.1 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.1.2.1 bouyer * notice, this list of conditions and the following disclaimer.
11 1.1.2.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.2.1 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.1.2.1 bouyer * documentation and/or other materials provided with the distribution.
14 1.1.2.1 bouyer * 3. All advertising materials mentioning features or use of this software
15 1.1.2.1 bouyer * must display the following acknowledgement:
16 1.1.2.1 bouyer * This product includes software developed by Charles M. Hannum.
17 1.1.2.1 bouyer * 4. The name of the author may not be used to endorse or promote products
18 1.1.2.1 bouyer * derived from this software without specific prior written permission.
19 1.1.2.1 bouyer *
20 1.1.2.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1.2.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1.2.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1.2.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1.2.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1.2.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1.2.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1.2.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1.2.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1.2.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 bouyer */
31 1.1.2.1 bouyer
32 1.1.2.1 bouyer /*
33 1.1.2.1 bouyer * Originally written by Julian Elischer (julian (at) dialix.oz.au)
34 1.1.2.1 bouyer */
35 1.1.2.1 bouyer
36 1.1.2.1 bouyer #include <sys/types.h>
37 1.1.2.1 bouyer #include <sys/param.h>
38 1.1.2.1 bouyer #include <sys/systm.h>
39 1.1.2.1 bouyer #include <sys/kernel.h>
40 1.1.2.1 bouyer #include <sys/buf.h>
41 1.1.2.1 bouyer #include <sys/uio.h>
42 1.1.2.1 bouyer #include <sys/malloc.h>
43 1.1.2.1 bouyer #include <sys/errno.h>
44 1.1.2.1 bouyer #include <sys/device.h>
45 1.1.2.1 bouyer #include <sys/proc.h>
46 1.1.2.1 bouyer
47 1.1.2.1 bouyer #include <dev/scsipi/scsipi_all.h>
48 1.1.2.1 bouyer #include <dev/scsipi/scsi_all.h>
49 1.1.2.1 bouyer #include <dev/scsipi/scsipi_disk.h>
50 1.1.2.1 bouyer #include <dev/scsipi/scsipiconf.h>
51 1.1.2.1 bouyer #include <dev/scsipi/scsipi_base.h>
52 1.1.2.1 bouyer
53 1.1.2.1 bouyer struct xs_free_list xs_free_list;
54 1.1.2.1 bouyer int sc_err1 __P((struct scsipi_xfer *, int));
55 1.1.2.1 bouyer
56 1.1.2.1 bouyer /*
57 1.1.2.1 bouyer * Get a scsipi transfer structure for the caller. Charge the structure
58 1.1.2.1 bouyer * to the device that is referenced by the sc_link structure. If the
59 1.1.2.1 bouyer * sc_link structure has no 'credits' then the device already has the
60 1.1.2.1 bouyer * maximum number or outstanding operations under way. In this stage,
61 1.1.2.1 bouyer * wait on the structure so that when one is freed, we are awoken again
62 1.1.2.1 bouyer * If the SCSI_NOSLEEP flag is set, then do not wait, but rather, return
63 1.1.2.1 bouyer * a NULL pointer, signifying that no slots were available
64 1.1.2.1 bouyer * Note in the link structure, that we are waiting on it.
65 1.1.2.1 bouyer */
66 1.1.2.1 bouyer
67 1.1.2.1 bouyer struct scsipi_xfer *
68 1.1.2.1 bouyer scsipi_get_xs(sc_link, flags)
69 1.1.2.1 bouyer struct scsipi_link *sc_link; /* who to charge the xs to */
70 1.1.2.1 bouyer int flags; /* if this call can sleep */
71 1.1.2.1 bouyer {
72 1.1.2.1 bouyer struct scsipi_xfer *xs;
73 1.1.2.1 bouyer int s;
74 1.1.2.1 bouyer
75 1.1.2.1 bouyer SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_get_xs\n"));
76 1.1.2.1 bouyer s = splbio();
77 1.1.2.1 bouyer while (sc_link->openings <= 0) {
78 1.1.2.1 bouyer SC_DEBUG(sc_link, SDEV_DB3, ("sleeping\n"));
79 1.1.2.1 bouyer if ((flags & SCSI_NOSLEEP) != 0) {
80 1.1.2.1 bouyer splx(s);
81 1.1.2.1 bouyer return 0;
82 1.1.2.1 bouyer }
83 1.1.2.1 bouyer sc_link->flags |= SDEV_WAITING;
84 1.1.2.1 bouyer (void) tsleep(sc_link, PRIBIO, "getxs", 0);
85 1.1.2.1 bouyer }
86 1.1.2.1 bouyer sc_link->openings--;
87 1.1.2.1 bouyer if ((xs = xs_free_list.lh_first) != NULL) {
88 1.1.2.1 bouyer LIST_REMOVE(xs, free_list);
89 1.1.2.1 bouyer splx(s);
90 1.1.2.1 bouyer } else {
91 1.1.2.1 bouyer splx(s);
92 1.1.2.1 bouyer SC_DEBUG(sc_link, SDEV_DB3, ("making\n"));
93 1.1.2.1 bouyer xs = malloc(sizeof(*xs), M_DEVBUF,
94 1.1.2.1 bouyer ((flags & SCSI_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
95 1.1.2.1 bouyer if (!xs) {
96 1.1.2.1 bouyer sc_link->sc_print_addr(sc_link);
97 1.1.2.1 bouyer printf("cannot allocate scsipi xs\n");
98 1.1.2.1 bouyer return 0;
99 1.1.2.1 bouyer }
100 1.1.2.1 bouyer }
101 1.1.2.1 bouyer
102 1.1.2.1 bouyer SC_DEBUG(sc_link, SDEV_DB3, ("returning\n"));
103 1.1.2.1 bouyer /* zero's out the command, as ATAPI may use longer commands than SCSI */
104 1.1.2.1 bouyer bzero(&xs->cmdstore, sizeof(xs->cmdstore));
105 1.1.2.1 bouyer xs->flags = INUSE | flags;
106 1.1.2.1 bouyer return xs;
107 1.1.2.1 bouyer }
108 1.1.2.1 bouyer
109 1.1.2.1 bouyer /*
110 1.1.2.1 bouyer * Given a scsipi_xfer struct, and a device (referenced through sc_link)
111 1.1.2.1 bouyer * return the struct to the free pool and credit the device with it
112 1.1.2.1 bouyer * If another process is waiting for an xs, do a wakeup, let it proceed
113 1.1.2.1 bouyer */
114 1.1.2.1 bouyer void
115 1.1.2.1 bouyer scsipi_free_xs(xs, flags)
116 1.1.2.1 bouyer struct scsipi_xfer *xs;
117 1.1.2.1 bouyer int flags;
118 1.1.2.1 bouyer {
119 1.1.2.1 bouyer struct scsipi_link *sc_link = xs->sc_link;
120 1.1.2.1 bouyer
121 1.1.2.1 bouyer xs->flags &= ~INUSE;
122 1.1.2.1 bouyer LIST_INSERT_HEAD(&xs_free_list, xs, free_list);
123 1.1.2.1 bouyer
124 1.1.2.1 bouyer SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_free_xs\n"));
125 1.1.2.1 bouyer /* if was 0 and someone waits, wake them up */
126 1.1.2.1 bouyer sc_link->openings++;
127 1.1.2.1 bouyer if ((sc_link->flags & SDEV_WAITING) != 0) {
128 1.1.2.1 bouyer sc_link->flags &= ~SDEV_WAITING;
129 1.1.2.1 bouyer wakeup(sc_link);
130 1.1.2.1 bouyer } else {
131 1.1.2.1 bouyer if (sc_link->device->start) {
132 1.1.2.1 bouyer SC_DEBUG(sc_link, SDEV_DB2, ("calling private start()\n"));
133 1.1.2.1 bouyer (*(sc_link->device->start)) (sc_link->device_softc);
134 1.1.2.1 bouyer }
135 1.1.2.1 bouyer }
136 1.1.2.1 bouyer }
137 1.1.2.1 bouyer
138 1.1.2.1 bouyer /*
139 1.1.2.1 bouyer * Find out from the device what its capacity is.
140 1.1.2.1 bouyer */
141 1.1.2.1 bouyer u_long
142 1.1.2.1 bouyer scsipi_size(sc_link, flags)
143 1.1.2.1 bouyer struct scsipi_link *sc_link;
144 1.1.2.1 bouyer int flags;
145 1.1.2.1 bouyer {
146 1.1.2.1 bouyer struct scsipi_read_cap_data rdcap;
147 1.1.2.1 bouyer struct scsipi_read_capacity scsipi_cmd;
148 1.1.2.1 bouyer
149 1.1.2.1 bouyer /*
150 1.1.2.1 bouyer * make up a scsipi command and ask the scsipi driver to do
151 1.1.2.1 bouyer * it for you.
152 1.1.2.1 bouyer */
153 1.1.2.1 bouyer bzero(&scsipi_cmd, sizeof(scsipi_cmd));
154 1.1.2.1 bouyer scsipi_cmd.opcode = READ_CAPACITY;
155 1.1.2.1 bouyer
156 1.1.2.1 bouyer /*
157 1.1.2.1 bouyer * If the command works, interpret the result as a 4 byte
158 1.1.2.1 bouyer * number of blocks
159 1.1.2.1 bouyer */
160 1.1.2.1 bouyer if (sc_link->scsipi_cmd(sc_link, (struct scsipi_generic *)&scsipi_cmd,
161 1.1.2.1 bouyer sizeof(scsipi_cmd), (u_char *)&rdcap, sizeof(rdcap),
162 1.1.2.1 bouyer 2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
163 1.1.2.1 bouyer sc_link->sc_print_addr(sc_link);
164 1.1.2.1 bouyer printf("could not get size\n");
165 1.1.2.1 bouyer return 0;
166 1.1.2.1 bouyer }
167 1.1.2.1 bouyer
168 1.1.2.1 bouyer return _4btol(rdcap.addr) + 1;
169 1.1.2.1 bouyer }
170 1.1.2.1 bouyer
171 1.1.2.1 bouyer /*
172 1.1.2.1 bouyer * Get scsipi driver to send a "are you ready?" command
173 1.1.2.1 bouyer */
174 1.1.2.1 bouyer int
175 1.1.2.1 bouyer scsipi_test_unit_ready(sc_link, flags)
176 1.1.2.1 bouyer struct scsipi_link *sc_link;
177 1.1.2.1 bouyer int flags;
178 1.1.2.1 bouyer {
179 1.1.2.1 bouyer struct scsipi_test_unit_ready scsipi_cmd;
180 1.1.2.1 bouyer
181 1.1.2.1 bouyer /* some ATAPI drives don't support TEST_UNIT_READY. Sigh */
182 1.1.2.1 bouyer if (sc_link->quirks & ADEV_NOTUR)
183 1.1.2.1 bouyer return 0;
184 1.1.2.1 bouyer
185 1.1.2.1 bouyer bzero(&scsipi_cmd, sizeof(scsipi_cmd));
186 1.1.2.1 bouyer scsipi_cmd.opcode = TEST_UNIT_READY;
187 1.1.2.1 bouyer
188 1.1.2.1 bouyer return sc_link->scsipi_cmd(sc_link, (struct scsipi_generic *) &scsipi_cmd,
189 1.1.2.1 bouyer sizeof(scsipi_cmd), 0, 0, 2, 10000, NULL, flags);
190 1.1.2.1 bouyer }
191 1.1.2.1 bouyer
192 1.1.2.1 bouyer /*
193 1.1.2.1 bouyer * Do a scsipi operation asking a device what it is
194 1.1.2.1 bouyer * Use the scsipi_cmd routine in the switch table.
195 1.1.2.1 bouyer * XXX actually this is only used for scsi devices, because I have the feeling
196 1.1.2.1 bouyer * that some atapi CDROM may not implement it, althouh it marked as mandatory
197 1.1.2.1 bouyer * in the atapi specs.
198 1.1.2.1 bouyer */
199 1.1.2.1 bouyer int
200 1.1.2.1 bouyer scsipi_inquire(sc_link, inqbuf, flags)
201 1.1.2.1 bouyer struct scsipi_link *sc_link;
202 1.1.2.1 bouyer struct scsipi_inquiry_data *inqbuf;
203 1.1.2.1 bouyer int flags;
204 1.1.2.1 bouyer {
205 1.1.2.1 bouyer struct scsipi_inquiry scsipi_cmd;
206 1.1.2.1 bouyer
207 1.1.2.1 bouyer bzero(&scsipi_cmd, sizeof(scsipi_cmd));
208 1.1.2.1 bouyer scsipi_cmd.opcode = INQUIRY;
209 1.1.2.1 bouyer scsipi_cmd.length = sizeof(struct scsipi_inquiry_data);
210 1.1.2.1 bouyer
211 1.1.2.1 bouyer return sc_link->scsipi_cmd(sc_link, (struct scsipi_generic *) &scsipi_cmd,
212 1.1.2.1 bouyer sizeof(scsipi_cmd), (u_char *) inqbuf,
213 1.1.2.1 bouyer sizeof(struct scsipi_inquiry_data), 2, 10000, NULL,
214 1.1.2.1 bouyer SCSI_DATA_IN | flags);
215 1.1.2.1 bouyer }
216 1.1.2.1 bouyer
217 1.1.2.1 bouyer /*
218 1.1.2.1 bouyer * Prevent or allow the user to remove the media
219 1.1.2.1 bouyer */
220 1.1.2.1 bouyer int
221 1.1.2.1 bouyer scsipi_prevent(sc_link, type, flags)
222 1.1.2.1 bouyer struct scsipi_link *sc_link;
223 1.1.2.1 bouyer int type, flags;
224 1.1.2.1 bouyer {
225 1.1.2.1 bouyer struct scsipi_prevent scsipi_cmd;
226 1.1.2.1 bouyer
227 1.1.2.1 bouyer if (sc_link->quirks & ADEV_NODOORLOCK)
228 1.1.2.1 bouyer return 0;
229 1.1.2.1 bouyer
230 1.1.2.1 bouyer bzero(&scsipi_cmd, sizeof(scsipi_cmd));
231 1.1.2.1 bouyer scsipi_cmd.opcode = PREVENT_ALLOW;
232 1.1.2.1 bouyer scsipi_cmd.how = type;
233 1.1.2.1 bouyer return sc_link->scsipi_cmd(sc_link, (struct scsipi_generic *) &scsipi_cmd,
234 1.1.2.1 bouyer sizeof(scsipi_cmd), 0, 0, 2, 5000, NULL, flags);
235 1.1.2.1 bouyer }
236 1.1.2.1 bouyer
237 1.1.2.1 bouyer /*
238 1.1.2.1 bouyer * Get scsipi driver to send a "start up" command
239 1.1.2.1 bouyer */
240 1.1.2.1 bouyer int
241 1.1.2.1 bouyer scsipi_start(sc_link, type, flags)
242 1.1.2.1 bouyer struct scsipi_link *sc_link;
243 1.1.2.1 bouyer int type, flags;
244 1.1.2.1 bouyer {
245 1.1.2.1 bouyer struct scsipi_start_stop scsipi_cmd;
246 1.1.2.1 bouyer
247 1.1.2.1 bouyer bzero(&scsipi_cmd, sizeof(scsipi_cmd));
248 1.1.2.1 bouyer scsipi_cmd.opcode = START_STOP;
249 1.1.2.1 bouyer scsipi_cmd.byte2 = 0x00;
250 1.1.2.1 bouyer scsipi_cmd.how = type;
251 1.1.2.1 bouyer return sc_link->scsipi_cmd(sc_link, (struct scsipi_generic *) &scsipi_cmd,
252 1.1.2.1 bouyer sizeof(scsipi_cmd), 0, 0, 2,
253 1.1.2.1 bouyer type == SSS_START ? 30000 : 10000, NULL, flags);
254 1.1.2.1 bouyer }
255 1.1.2.1 bouyer
256 1.1.2.1 bouyer /*
257 1.1.2.1 bouyer * This routine is called by the scsipi interrupt when the transfer is complete.
258 1.1.2.1 bouyer */
259 1.1.2.1 bouyer void
260 1.1.2.1 bouyer scsipi_done(xs)
261 1.1.2.1 bouyer struct scsipi_xfer *xs;
262 1.1.2.1 bouyer {
263 1.1.2.1 bouyer struct scsipi_link *sc_link = xs->sc_link;
264 1.1.2.1 bouyer struct buf *bp;
265 1.1.2.1 bouyer int error;
266 1.1.2.1 bouyer
267 1.1.2.1 bouyer SC_DEBUG(sc_link, SDEV_DB2, ("scsipi_done\n"));
268 1.1.2.1 bouyer #ifdef SCSIDEBUG
269 1.1.2.1 bouyer if ((sc_link->flags & SDEV_DB1) != 0)
270 1.1.2.1 bouyer show_scsipi_cmd(xs);
271 1.1.2.1 bouyer #endif /* SCSIDEBUG */
272 1.1.2.1 bouyer
273 1.1.2.1 bouyer /*
274 1.1.2.1 bouyer * If it's a user level request, bypass all usual completion processing,
275 1.1.2.1 bouyer * let the user work it out.. We take reponsibility for freeing the
276 1.1.2.1 bouyer * xs when the user returns. (and restarting the device's queue).
277 1.1.2.1 bouyer */
278 1.1.2.1 bouyer if ((xs->flags & SCSI_USER) != 0) {
279 1.1.2.1 bouyer SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
280 1.1.2.1 bouyer scsipi_user_done(xs); /* to take a copy of the sense etc. */
281 1.1.2.1 bouyer SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
282 1.1.2.1 bouyer
283 1.1.2.1 bouyer scsipi_free_xs(xs, SCSI_NOSLEEP); /* restarts queue too */
284 1.1.2.1 bouyer SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
285 1.1.2.1 bouyer return;
286 1.1.2.1 bouyer }
287 1.1.2.1 bouyer
288 1.1.2.1 bouyer if (!((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)) {
289 1.1.2.1 bouyer /*
290 1.1.2.1 bouyer * if it's a normal upper level request, then ask
291 1.1.2.1 bouyer * the upper level code to handle error checking
292 1.1.2.1 bouyer * rather than doing it here at interrupt time
293 1.1.2.1 bouyer */
294 1.1.2.1 bouyer wakeup(xs);
295 1.1.2.1 bouyer return;
296 1.1.2.1 bouyer }
297 1.1.2.1 bouyer
298 1.1.2.1 bouyer /*
299 1.1.2.1 bouyer * Go and handle errors now.
300 1.1.2.1 bouyer * If it returns ERESTART then we should RETRY
301 1.1.2.1 bouyer */
302 1.1.2.1 bouyer retry:
303 1.1.2.1 bouyer error = sc_err1(xs, 1);
304 1.1.2.1 bouyer if (error == ERESTART) {
305 1.1.2.1 bouyer switch ((*(sc_link->adapter->scsipi_cmd)) (xs)) {
306 1.1.2.1 bouyer case SUCCESSFULLY_QUEUED:
307 1.1.2.1 bouyer return;
308 1.1.2.1 bouyer
309 1.1.2.1 bouyer case TRY_AGAIN_LATER:
310 1.1.2.1 bouyer xs->error = XS_BUSY;
311 1.1.2.1 bouyer case COMPLETE:
312 1.1.2.1 bouyer goto retry;
313 1.1.2.1 bouyer }
314 1.1.2.1 bouyer }
315 1.1.2.1 bouyer
316 1.1.2.1 bouyer bp = xs->bp;
317 1.1.2.1 bouyer if (bp) {
318 1.1.2.1 bouyer if (error) {
319 1.1.2.1 bouyer bp->b_error = error;
320 1.1.2.1 bouyer bp->b_flags |= B_ERROR;
321 1.1.2.1 bouyer bp->b_resid = bp->b_bcount;
322 1.1.2.1 bouyer } else {
323 1.1.2.1 bouyer bp->b_error = 0;
324 1.1.2.1 bouyer bp->b_resid = xs->resid;
325 1.1.2.1 bouyer }
326 1.1.2.1 bouyer }
327 1.1.2.1 bouyer if (sc_link->device->done) {
328 1.1.2.1 bouyer /*
329 1.1.2.1 bouyer * Tell the device the operation is actually complete.
330 1.1.2.1 bouyer * No more will happen with this xfer. This for
331 1.1.2.1 bouyer * notification of the upper-level driver only; they
332 1.1.2.1 bouyer * won't be returning any meaningful information to us.
333 1.1.2.1 bouyer */
334 1.1.2.1 bouyer (*sc_link->device->done)(xs);
335 1.1.2.1 bouyer }
336 1.1.2.1 bouyer scsipi_free_xs(xs, SCSI_NOSLEEP);
337 1.1.2.1 bouyer if (bp)
338 1.1.2.1 bouyer biodone(bp);
339 1.1.2.1 bouyer }
340 1.1.2.1 bouyer
341 1.1.2.1 bouyer int
342 1.1.2.1 bouyer scsipi_execute_xs(xs)
343 1.1.2.1 bouyer struct scsipi_xfer *xs;
344 1.1.2.1 bouyer {
345 1.1.2.1 bouyer int error;
346 1.1.2.1 bouyer int s;
347 1.1.2.1 bouyer
348 1.1.2.1 bouyer xs->flags &= ~ITSDONE;
349 1.1.2.1 bouyer xs->error = XS_NOERROR;
350 1.1.2.1 bouyer xs->resid = xs->datalen;
351 1.1.2.1 bouyer
352 1.1.2.1 bouyer retry:
353 1.1.2.1 bouyer /*
354 1.1.2.1 bouyer * Do the transfer. If we are polling we will return:
355 1.1.2.1 bouyer * COMPLETE, Was poll, and scsipi_done has been called
356 1.1.2.1 bouyer * TRY_AGAIN_LATER, Adapter short resources, try again
357 1.1.2.1 bouyer *
358 1.1.2.1 bouyer * if under full steam (interrupts) it will return:
359 1.1.2.1 bouyer * SUCCESSFULLY_QUEUED, will do a wakeup when complete
360 1.1.2.1 bouyer * TRY_AGAIN_LATER, (as for polling)
361 1.1.2.1 bouyer * After the wakeup, we must still check if it succeeded
362 1.1.2.1 bouyer *
363 1.1.2.1 bouyer * If we have a SCSI_NOSLEEP (typically because we have a buf)
364 1.1.2.1 bouyer * we just return. All the error proccessing and the buffer
365 1.1.2.1 bouyer * code both expect us to return straight to them, so as soon
366 1.1.2.1 bouyer * as the command is queued, return.
367 1.1.2.1 bouyer */
368 1.1.2.1 bouyer #ifdef SCSIDEBUG
369 1.1.2.1 bouyer if (xs->sc_link->flags & SDEV_DB3) {
370 1.1.2.1 bouyer printf("scsipi_exec_cmd: ");
371 1.1.2.1 bouyer show_scsipi_xs(xs);
372 1.1.2.1 bouyer printf("\n");
373 1.1.2.1 bouyer }
374 1.1.2.1 bouyer #endif
375 1.1.2.1 bouyer switch ((*(xs->sc_link->adapter->scsipi_cmd)) (xs)) {
376 1.1.2.1 bouyer case SUCCESSFULLY_QUEUED:
377 1.1.2.1 bouyer if ((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)
378 1.1.2.1 bouyer return EJUSTRETURN;
379 1.1.2.1 bouyer #ifdef DIAGNOSTIC
380 1.1.2.1 bouyer if (xs->flags & SCSI_NOSLEEP)
381 1.1.2.1 bouyer panic("scsipi_execute_xs: NOSLEEP and POLL");
382 1.1.2.1 bouyer #endif
383 1.1.2.1 bouyer s = splbio();
384 1.1.2.1 bouyer while ((xs->flags & ITSDONE) == 0)
385 1.1.2.1 bouyer tsleep(xs, PRIBIO + 1, "scsipi_cmd", 0);
386 1.1.2.1 bouyer splx(s);
387 1.1.2.1 bouyer case COMPLETE: /* Polling command completed ok */
388 1.1.2.1 bouyer if (xs->bp)
389 1.1.2.1 bouyer return EJUSTRETURN;
390 1.1.2.1 bouyer doit:
391 1.1.2.1 bouyer SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
392 1.1.2.1 bouyer if ((error = sc_err1(xs, 0)) != ERESTART)
393 1.1.2.1 bouyer return error;
394 1.1.2.1 bouyer goto retry;
395 1.1.2.1 bouyer
396 1.1.2.1 bouyer case TRY_AGAIN_LATER: /* adapter resource shortage */
397 1.1.2.1 bouyer xs->error = XS_BUSY;
398 1.1.2.1 bouyer goto doit;
399 1.1.2.1 bouyer
400 1.1.2.1 bouyer default:
401 1.1.2.1 bouyer panic("scsipi_execute_xs: invalid return code");
402 1.1.2.1 bouyer }
403 1.1.2.1 bouyer
404 1.1.2.1 bouyer #ifdef DIAGNOSTIC
405 1.1.2.1 bouyer panic("scsipi_execute_xs: impossible");
406 1.1.2.1 bouyer #endif
407 1.1.2.1 bouyer return EINVAL;
408 1.1.2.1 bouyer }
409 1.1.2.1 bouyer
410 1.1.2.1 bouyer int
411 1.1.2.1 bouyer sc_err1(xs, async)
412 1.1.2.1 bouyer struct scsipi_xfer *xs;
413 1.1.2.1 bouyer int async;
414 1.1.2.1 bouyer {
415 1.1.2.1 bouyer int error;
416 1.1.2.1 bouyer
417 1.1.2.1 bouyer SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
418 1.1.2.1 bouyer
419 1.1.2.1 bouyer /*
420 1.1.2.1 bouyer * If it has a buf, we might be working with
421 1.1.2.1 bouyer * a request from the buffer cache or some other
422 1.1.2.1 bouyer * piece of code that requires us to process
423 1.1.2.1 bouyer * errors at inetrrupt time. We have probably
424 1.1.2.1 bouyer * been called by scsipi_done()
425 1.1.2.1 bouyer */
426 1.1.2.1 bouyer switch (xs->error) {
427 1.1.2.1 bouyer case XS_NOERROR: /* nearly always hit this one */
428 1.1.2.1 bouyer error = 0;
429 1.1.2.1 bouyer break;
430 1.1.2.1 bouyer
431 1.1.2.1 bouyer case XS_SENSE:
432 1.1.2.1 bouyer if ((error = xs->sc_link->scsipi_interpret_sense(xs)) == ERESTART)
433 1.1.2.1 bouyer goto retry;
434 1.1.2.1 bouyer SC_DEBUG(xs->sc_link, SDEV_DB3,
435 1.1.2.1 bouyer ("scsipi_interpret_sense returned %d\n", error));
436 1.1.2.1 bouyer break;
437 1.1.2.1 bouyer
438 1.1.2.1 bouyer case XS_BUSY:
439 1.1.2.1 bouyer if (xs->retries) {
440 1.1.2.1 bouyer if ((xs->flags & SCSI_POLL) != 0)
441 1.1.2.1 bouyer delay(1000000);
442 1.1.2.1 bouyer else if ((xs->flags & SCSI_NOSLEEP) == 0)
443 1.1.2.1 bouyer tsleep(&lbolt, PRIBIO, "scbusy", 0);
444 1.1.2.1 bouyer else
445 1.1.2.1 bouyer #if 0
446 1.1.2.1 bouyer timeout(scsipi_requeue, xs, hz);
447 1.1.2.1 bouyer #else
448 1.1.2.1 bouyer goto lose;
449 1.1.2.1 bouyer #endif
450 1.1.2.1 bouyer }
451 1.1.2.1 bouyer case XS_TIMEOUT:
452 1.1.2.1 bouyer retry:
453 1.1.2.1 bouyer if (xs->retries--) {
454 1.1.2.1 bouyer xs->error = XS_NOERROR;
455 1.1.2.1 bouyer xs->flags &= ~ITSDONE;
456 1.1.2.1 bouyer return ERESTART;
457 1.1.2.1 bouyer }
458 1.1.2.1 bouyer case XS_DRIVER_STUFFUP:
459 1.1.2.1 bouyer lose:
460 1.1.2.1 bouyer error = EIO;
461 1.1.2.1 bouyer break;
462 1.1.2.1 bouyer
463 1.1.2.1 bouyer case XS_SELTIMEOUT:
464 1.1.2.1 bouyer /* XXX Disable device? */
465 1.1.2.1 bouyer error = EIO;
466 1.1.2.1 bouyer break;
467 1.1.2.1 bouyer
468 1.1.2.1 bouyer default:
469 1.1.2.1 bouyer xs->sc_link->sc_print_addr(xs->sc_link);
470 1.1.2.1 bouyer printf("unknown error category from scsipi driver\n");
471 1.1.2.1 bouyer error = EIO;
472 1.1.2.1 bouyer break;
473 1.1.2.1 bouyer }
474 1.1.2.1 bouyer
475 1.1.2.1 bouyer return error;
476 1.1.2.1 bouyer }
477 1.1.2.1 bouyer
478 1.1.2.1 bouyer #ifdef SCSIDEBUG
479 1.1.2.1 bouyer /*
480 1.1.2.1 bouyer * Given a scsipi_xfer, dump the request, in all it's glory
481 1.1.2.1 bouyer */
482 1.1.2.1 bouyer void
483 1.1.2.1 bouyer show_scsipi_xs(xs)
484 1.1.2.1 bouyer struct scsipi_xfer *xs;
485 1.1.2.1 bouyer {
486 1.1.2.1 bouyer printf("xs(%p): ", xs);
487 1.1.2.1 bouyer printf("flg(0x%x)", xs->flags);
488 1.1.2.1 bouyer printf("sc_link(%p)", xs->sc_link);
489 1.1.2.1 bouyer printf("retr(0x%x)", xs->retries);
490 1.1.2.1 bouyer printf("timo(0x%x)", xs->timeout);
491 1.1.2.1 bouyer printf("cmd(%p)", xs->cmd);
492 1.1.2.1 bouyer printf("len(0x%x)", xs->cmdlen);
493 1.1.2.1 bouyer printf("data(%p)", xs->data);
494 1.1.2.1 bouyer printf("len(0x%x)", xs->datalen);
495 1.1.2.1 bouyer printf("res(0x%x)", xs->resid);
496 1.1.2.1 bouyer printf("err(0x%x)", xs->error);
497 1.1.2.1 bouyer printf("bp(%p)", xs->bp);
498 1.1.2.1 bouyer show_scsipi_cmd(xs);
499 1.1.2.1 bouyer }
500 1.1.2.1 bouyer
501 1.1.2.1 bouyer void
502 1.1.2.1 bouyer show_scsipi_cmd(xs)
503 1.1.2.1 bouyer struct scsipi_xfer *xs;
504 1.1.2.1 bouyer {
505 1.1.2.1 bouyer u_char *b = (u_char *) xs->cmd;
506 1.1.2.1 bouyer int i = 0;
507 1.1.2.1 bouyer
508 1.1.2.1 bouyer xs->sc_link->sc_print_addr(xs->sc_link);
509 1.1.2.1 bouyer printf("command: ");
510 1.1.2.1 bouyer
511 1.1.2.1 bouyer if ((xs->flags & SCSI_RESET) == 0) {
512 1.1.2.1 bouyer while (i < xs->cmdlen) {
513 1.1.2.1 bouyer if (i)
514 1.1.2.1 bouyer printf(",");
515 1.1.2.1 bouyer printf("%x", b[i++]);
516 1.1.2.1 bouyer }
517 1.1.2.1 bouyer printf("-[%d bytes]\n", xs->datalen);
518 1.1.2.1 bouyer if (xs->datalen)
519 1.1.2.1 bouyer show_mem(xs->data, min(64, xs->datalen));
520 1.1.2.1 bouyer } else
521 1.1.2.1 bouyer printf("-RESET-\n");
522 1.1.2.1 bouyer }
523 1.1.2.1 bouyer
524 1.1.2.1 bouyer void
525 1.1.2.1 bouyer show_mem(address, num)
526 1.1.2.1 bouyer u_char *address;
527 1.1.2.1 bouyer int num;
528 1.1.2.1 bouyer {
529 1.1.2.1 bouyer int x;
530 1.1.2.1 bouyer
531 1.1.2.1 bouyer printf("------------------------------");
532 1.1.2.1 bouyer for (x = 0; x < num; x++) {
533 1.1.2.1 bouyer if ((x % 16) == 0)
534 1.1.2.1 bouyer printf("\n%03d: ", x);
535 1.1.2.1 bouyer printf("%02x ", *address++);
536 1.1.2.1 bouyer }
537 1.1.2.1 bouyer printf("\n------------------------------\n");
538 1.1.2.1 bouyer }
539 1.1.2.1 bouyer #endif /*SCSIDEBUG */
540 1.1.2.1 bouyer
541