scsi_base.c revision 1.36 1 1.36 christos /* $NetBSD: scsi_base.c,v 1.36 1996/05/03 19:48:20 christos Exp $ */
2 1.14 cgd
3 1.1 mycroft /*
4 1.28 mycroft * Copyright (c) 1994, 1995 Charles Hannum. All rights reserved.
5 1.9 mycroft *
6 1.9 mycroft * Redistribution and use in source and binary forms, with or without
7 1.9 mycroft * modification, are permitted provided that the following conditions
8 1.9 mycroft * are met:
9 1.9 mycroft * 1. Redistributions of source code must retain the above copyright
10 1.9 mycroft * notice, this list of conditions and the following disclaimer.
11 1.9 mycroft * 2. Redistributions in binary form must reproduce the above copyright
12 1.9 mycroft * notice, this list of conditions and the following disclaimer in the
13 1.9 mycroft * documentation and/or other materials provided with the distribution.
14 1.9 mycroft * 3. All advertising materials mentioning features or use of this software
15 1.9 mycroft * must display the following acknowledgement:
16 1.9 mycroft * This product includes software developed by Charles Hannum.
17 1.9 mycroft * 4. The name of the author may not be used to endorse or promote products
18 1.9 mycroft * derived from this software without specific prior written permission.
19 1.9 mycroft *
20 1.9 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.9 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.9 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.9 mycroft * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.9 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.9 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.9 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.9 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.9 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.9 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.9 mycroft */
31 1.9 mycroft
32 1.9 mycroft /*
33 1.9 mycroft * Originally written by Julian Elischer (julian (at) dialix.oz.au)
34 1.1 mycroft */
35 1.1 mycroft
36 1.1 mycroft #include <sys/types.h>
37 1.1 mycroft #include <sys/param.h>
38 1.30 thorpej #include <sys/systm.h>
39 1.11 mycroft #include <sys/kernel.h>
40 1.1 mycroft #include <sys/buf.h>
41 1.1 mycroft #include <sys/uio.h>
42 1.1 mycroft #include <sys/malloc.h>
43 1.1 mycroft #include <sys/errno.h>
44 1.1 mycroft #include <sys/device.h>
45 1.33 christos #include <sys/proc.h>
46 1.2 mycroft
47 1.1 mycroft #include <scsi/scsi_all.h>
48 1.1 mycroft #include <scsi/scsi_disk.h>
49 1.1 mycroft #include <scsi/scsiconf.h>
50 1.1 mycroft
51 1.22 mycroft void scsi_error __P((struct scsi_xfer *, int));
52 1.22 mycroft
53 1.12 mycroft LIST_HEAD(xs_free_list, scsi_xfer) xs_free_list;
54 1.1 mycroft
55 1.33 christos static __inline struct scsi_xfer *scsi_make_xs __P((struct scsi_link *,
56 1.33 christos struct scsi_generic *,
57 1.33 christos int cmdlen,
58 1.33 christos u_char *data_addr,
59 1.33 christos int datalen,
60 1.33 christos int retries,
61 1.33 christos int timeout,
62 1.33 christos struct buf *,
63 1.33 christos int flags));
64 1.33 christos
65 1.33 christos int sc_err1 __P((struct scsi_xfer *, int));
66 1.33 christos int scsi_interpret_sense __P((struct scsi_xfer *));
67 1.33 christos
68 1.1 mycroft /*
69 1.1 mycroft * Get a scsi transfer structure for the caller. Charge the structure
70 1.1 mycroft * to the device that is referenced by the sc_link structure. If the
71 1.1 mycroft * sc_link structure has no 'credits' then the device already has the
72 1.1 mycroft * maximum number or outstanding operations under way. In this stage,
73 1.1 mycroft * wait on the structure so that when one is freed, we are awoken again
74 1.1 mycroft * If the SCSI_NOSLEEP flag is set, then do not wait, but rather, return
75 1.1 mycroft * a NULL pointer, signifying that no slots were available
76 1.1 mycroft * Note in the link structure, that we are waiting on it.
77 1.1 mycroft */
78 1.1 mycroft
79 1.1 mycroft struct scsi_xfer *
80 1.22 mycroft scsi_get_xs(sc_link, flags)
81 1.2 mycroft struct scsi_link *sc_link; /* who to charge the xs to */
82 1.11 mycroft int flags; /* if this call can sleep */
83 1.1 mycroft {
84 1.2 mycroft struct scsi_xfer *xs;
85 1.11 mycroft int s;
86 1.1 mycroft
87 1.22 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("scsi_get_xs\n"));
88 1.1 mycroft s = splbio();
89 1.22 mycroft while (sc_link->openings <= 0) {
90 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("sleeping\n"));
91 1.22 mycroft if ((flags & SCSI_NOSLEEP) != 0) {
92 1.1 mycroft splx(s);
93 1.1 mycroft return 0;
94 1.1 mycroft }
95 1.1 mycroft sc_link->flags |= SDEV_WAITING;
96 1.12 mycroft (void) tsleep(sc_link, PRIBIO, "getxs", 0);
97 1.1 mycroft }
98 1.22 mycroft sc_link->openings--;
99 1.33 christos if ((xs = xs_free_list.lh_first) != NULL) {
100 1.12 mycroft LIST_REMOVE(xs, free_list);
101 1.1 mycroft splx(s);
102 1.1 mycroft } else {
103 1.1 mycroft splx(s);
104 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("making\n"));
105 1.12 mycroft xs = malloc(sizeof(*xs), M_DEVBUF,
106 1.22 mycroft ((flags & SCSI_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
107 1.12 mycroft if (!xs) {
108 1.1 mycroft sc_print_addr(sc_link);
109 1.1 mycroft printf("cannot allocate scsi xs\n");
110 1.12 mycroft return 0;
111 1.1 mycroft }
112 1.1 mycroft }
113 1.22 mycroft
114 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("returning\n"));
115 1.22 mycroft xs->flags = INUSE | flags;
116 1.11 mycroft return xs;
117 1.1 mycroft }
118 1.1 mycroft
119 1.1 mycroft /*
120 1.1 mycroft * Given a scsi_xfer struct, and a device (referenced through sc_link)
121 1.1 mycroft * return the struct to the free pool and credit the device with it
122 1.1 mycroft * If another process is waiting for an xs, do a wakeup, let it proceed
123 1.1 mycroft */
124 1.1 mycroft void
125 1.22 mycroft scsi_free_xs(xs, flags)
126 1.1 mycroft struct scsi_xfer *xs;
127 1.11 mycroft int flags;
128 1.1 mycroft {
129 1.22 mycroft struct scsi_link *sc_link = xs->sc_link;
130 1.22 mycroft
131 1.22 mycroft xs->flags &= ~INUSE;
132 1.12 mycroft LIST_INSERT_HEAD(&xs_free_list, xs, free_list);
133 1.1 mycroft
134 1.22 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("scsi_free_xs\n"));
135 1.1 mycroft /* if was 0 and someone waits, wake them up */
136 1.22 mycroft sc_link->openings++;
137 1.22 mycroft if ((sc_link->flags & SDEV_WAITING) != 0) {
138 1.3 mycroft sc_link->flags &= ~SDEV_WAITING;
139 1.22 mycroft wakeup(sc_link);
140 1.3 mycroft } else {
141 1.1 mycroft if (sc_link->device->start) {
142 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("calling private start()\n"));
143 1.17 mycroft (*(sc_link->device->start)) (sc_link->device_softc);
144 1.1 mycroft }
145 1.3 mycroft }
146 1.1 mycroft }
147 1.1 mycroft
148 1.1 mycroft /*
149 1.22 mycroft * Make a scsi_xfer, and return a pointer to it.
150 1.22 mycroft */
151 1.22 mycroft static __inline struct scsi_xfer *
152 1.22 mycroft scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
153 1.33 christos retries, timeout, bp, flags)
154 1.22 mycroft struct scsi_link *sc_link;
155 1.22 mycroft struct scsi_generic *scsi_cmd;
156 1.22 mycroft int cmdlen;
157 1.22 mycroft u_char *data_addr;
158 1.22 mycroft int datalen;
159 1.22 mycroft int retries;
160 1.22 mycroft int timeout;
161 1.22 mycroft struct buf *bp;
162 1.22 mycroft int flags;
163 1.22 mycroft {
164 1.22 mycroft struct scsi_xfer *xs;
165 1.22 mycroft
166 1.22 mycroft if ((xs = scsi_get_xs(sc_link, flags)) == NULL)
167 1.22 mycroft return NULL;
168 1.22 mycroft
169 1.22 mycroft /*
170 1.22 mycroft * Fill out the scsi_xfer structure. We don't know whose context
171 1.22 mycroft * the cmd is in, so copy it.
172 1.22 mycroft */
173 1.22 mycroft xs->sc_link = sc_link;
174 1.22 mycroft bcopy(scsi_cmd, &xs->cmdstore, cmdlen);
175 1.22 mycroft xs->cmd = &xs->cmdstore;
176 1.22 mycroft xs->cmdlen = cmdlen;
177 1.22 mycroft xs->data = data_addr;
178 1.22 mycroft xs->datalen = datalen;
179 1.22 mycroft xs->retries = retries;
180 1.22 mycroft xs->timeout = timeout;
181 1.22 mycroft xs->bp = bp;
182 1.22 mycroft
183 1.22 mycroft return xs;
184 1.22 mycroft }
185 1.22 mycroft
186 1.22 mycroft /*
187 1.1 mycroft * Find out from the device what its capacity is.
188 1.1 mycroft */
189 1.22 mycroft u_long
190 1.1 mycroft scsi_size(sc_link, flags)
191 1.1 mycroft struct scsi_link *sc_link;
192 1.11 mycroft int flags;
193 1.1 mycroft {
194 1.1 mycroft struct scsi_read_cap_data rdcap;
195 1.1 mycroft struct scsi_read_capacity scsi_cmd;
196 1.1 mycroft
197 1.1 mycroft /*
198 1.1 mycroft * make up a scsi command and ask the scsi driver to do
199 1.1 mycroft * it for you.
200 1.1 mycroft */
201 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
202 1.22 mycroft scsi_cmd.opcode = READ_CAPACITY;
203 1.1 mycroft
204 1.1 mycroft /*
205 1.1 mycroft * If the command works, interpret the result as a 4 byte
206 1.1 mycroft * number of blocks
207 1.1 mycroft */
208 1.22 mycroft if (scsi_scsi_cmd(sc_link, (struct scsi_generic *)&scsi_cmd,
209 1.22 mycroft sizeof(scsi_cmd), (u_char *)&rdcap, sizeof(rdcap),
210 1.2 mycroft 2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
211 1.1 mycroft sc_print_addr(sc_link);
212 1.1 mycroft printf("could not get size\n");
213 1.11 mycroft return 0;
214 1.1 mycroft }
215 1.34 mycroft
216 1.34 mycroft return _4btol(rdcap.addr) + 1;
217 1.1 mycroft }
218 1.1 mycroft
219 1.1 mycroft /*
220 1.1 mycroft * Get scsi driver to send a "are you ready?" command
221 1.1 mycroft */
222 1.2 mycroft int
223 1.1 mycroft scsi_test_unit_ready(sc_link, flags)
224 1.1 mycroft struct scsi_link *sc_link;
225 1.11 mycroft int flags;
226 1.1 mycroft {
227 1.1 mycroft struct scsi_test_unit_ready scsi_cmd;
228 1.1 mycroft
229 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
230 1.22 mycroft scsi_cmd.opcode = TEST_UNIT_READY;
231 1.1 mycroft
232 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
233 1.22 mycroft sizeof(scsi_cmd), 0, 0, 2, 10000, NULL, flags);
234 1.1 mycroft }
235 1.1 mycroft
236 1.1 mycroft /*
237 1.1 mycroft * Do a scsi operation, asking a device to run as SCSI-II if it can.
238 1.1 mycroft */
239 1.2 mycroft int
240 1.1 mycroft scsi_change_def(sc_link, flags)
241 1.1 mycroft struct scsi_link *sc_link;
242 1.11 mycroft int flags;
243 1.1 mycroft {
244 1.1 mycroft struct scsi_changedef scsi_cmd;
245 1.1 mycroft
246 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
247 1.22 mycroft scsi_cmd.opcode = CHANGE_DEFINITION;
248 1.1 mycroft scsi_cmd.how = SC_SCSI_2;
249 1.1 mycroft
250 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
251 1.2 mycroft sizeof(scsi_cmd), 0, 0, 2, 100000, NULL, flags);
252 1.1 mycroft }
253 1.1 mycroft
254 1.1 mycroft /*
255 1.1 mycroft * Do a scsi operation asking a device what it is
256 1.1 mycroft * Use the scsi_cmd routine in the switch table.
257 1.1 mycroft */
258 1.2 mycroft int
259 1.1 mycroft scsi_inquire(sc_link, inqbuf, flags)
260 1.1 mycroft struct scsi_link *sc_link;
261 1.1 mycroft struct scsi_inquiry_data *inqbuf;
262 1.11 mycroft int flags;
263 1.1 mycroft {
264 1.1 mycroft struct scsi_inquiry scsi_cmd;
265 1.1 mycroft
266 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
267 1.22 mycroft scsi_cmd.opcode = INQUIRY;
268 1.1 mycroft scsi_cmd.length = sizeof(struct scsi_inquiry_data);
269 1.1 mycroft
270 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
271 1.2 mycroft sizeof(scsi_cmd), (u_char *) inqbuf,
272 1.22 mycroft sizeof(struct scsi_inquiry_data), 2, 10000, NULL,
273 1.2 mycroft SCSI_DATA_IN | flags);
274 1.1 mycroft }
275 1.1 mycroft
276 1.1 mycroft /*
277 1.1 mycroft * Prevent or allow the user to remove the media
278 1.1 mycroft */
279 1.2 mycroft int
280 1.1 mycroft scsi_prevent(sc_link, type, flags)
281 1.1 mycroft struct scsi_link *sc_link;
282 1.11 mycroft int type, flags;
283 1.1 mycroft {
284 1.1 mycroft struct scsi_prevent scsi_cmd;
285 1.1 mycroft
286 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
287 1.22 mycroft scsi_cmd.opcode = PREVENT_ALLOW;
288 1.1 mycroft scsi_cmd.how = type;
289 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
290 1.2 mycroft sizeof(scsi_cmd), 0, 0, 2, 5000, NULL, flags);
291 1.1 mycroft }
292 1.1 mycroft
293 1.1 mycroft /*
294 1.1 mycroft * Get scsi driver to send a "start up" command
295 1.1 mycroft */
296 1.2 mycroft int
297 1.10 mycroft scsi_start(sc_link, type, flags)
298 1.1 mycroft struct scsi_link *sc_link;
299 1.11 mycroft int type, flags;
300 1.1 mycroft {
301 1.1 mycroft struct scsi_start_stop scsi_cmd;
302 1.1 mycroft
303 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
304 1.22 mycroft scsi_cmd.opcode = START_STOP;
305 1.22 mycroft scsi_cmd.byte2 = 0x00;
306 1.10 mycroft scsi_cmd.how = type;
307 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
308 1.10 mycroft sizeof(scsi_cmd), 0, 0, 2,
309 1.22 mycroft type == SSS_START ? 30000 : 10000, NULL, flags);
310 1.1 mycroft }
311 1.1 mycroft
312 1.1 mycroft /*
313 1.1 mycroft * This routine is called by the scsi interrupt when the transfer is complete.
314 1.1 mycroft */
315 1.1 mycroft void
316 1.1 mycroft scsi_done(xs)
317 1.1 mycroft struct scsi_xfer *xs;
318 1.1 mycroft {
319 1.1 mycroft struct scsi_link *sc_link = xs->sc_link;
320 1.11 mycroft int error;
321 1.1 mycroft
322 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("scsi_done\n"));
323 1.1 mycroft #ifdef SCSIDEBUG
324 1.22 mycroft if ((sc_link->flags & SDEV_DB1) != 0)
325 1.1 mycroft show_scsi_cmd(xs);
326 1.22 mycroft #endif /* SCSIDEBUG */
327 1.22 mycroft
328 1.1 mycroft /*
329 1.1 mycroft * If it's a user level request, bypass all usual completion processing,
330 1.1 mycroft * let the user work it out.. We take reponsibility for freeing the
331 1.1 mycroft * xs when the user returns. (and restarting the device's queue).
332 1.1 mycroft */
333 1.22 mycroft if ((xs->flags & SCSI_USER) != 0) {
334 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
335 1.1 mycroft scsi_user_done(xs); /* to take a copy of the sense etc. */
336 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
337 1.19 mycroft
338 1.22 mycroft scsi_free_xs(xs, SCSI_NOSLEEP); /* restarts queue too */
339 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
340 1.1 mycroft return;
341 1.1 mycroft }
342 1.22 mycroft
343 1.1 mycroft /*
344 1.1 mycroft * If the device has it's own done routine, call it first.
345 1.1 mycroft * If it returns a legit error value, return that, otherwise
346 1.1 mycroft * it wants us to continue with normal processing.
347 1.31 thorpej *
348 1.31 thorpej * Make sure the upper-level driver knows that this might not
349 1.31 thorpej * actually be the last time they hear from us. We need to get
350 1.31 thorpej * status back.
351 1.1 mycroft */
352 1.1 mycroft if (sc_link->device->done) {
353 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("calling private done()\n"));
354 1.31 thorpej error = (*sc_link->device->done)(xs, 0);
355 1.22 mycroft if (error == EJUSTRETURN)
356 1.22 mycroft goto done;
357 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("continuing with generic done()\n"));
358 1.1 mycroft }
359 1.22 mycroft if (xs->bp == NULL) {
360 1.1 mycroft /*
361 1.1 mycroft * if it's a normal upper level request, then ask
362 1.1 mycroft * the upper level code to handle error checking
363 1.1 mycroft * rather than doing it here at interrupt time
364 1.1 mycroft */
365 1.1 mycroft wakeup(xs);
366 1.1 mycroft return;
367 1.1 mycroft }
368 1.1 mycroft /*
369 1.1 mycroft * Go and handle errors now.
370 1.22 mycroft * If it returns ERESTART then we should RETRY
371 1.1 mycroft */
372 1.22 mycroft retry:
373 1.22 mycroft if (sc_err1(xs, 1) == ERESTART) {
374 1.22 mycroft switch ((*(sc_link->adapter->scsi_cmd)) (xs)) {
375 1.22 mycroft case SUCCESSFULLY_QUEUED:
376 1.1 mycroft return;
377 1.22 mycroft
378 1.22 mycroft case TRY_AGAIN_LATER:
379 1.22 mycroft xs->error = XS_BUSY;
380 1.22 mycroft case COMPLETE:
381 1.22 mycroft goto retry;
382 1.22 mycroft }
383 1.1 mycroft }
384 1.22 mycroft done:
385 1.31 thorpej if (sc_link->device->done) {
386 1.31 thorpej /*
387 1.31 thorpej * Tell the device the operation is actually complete.
388 1.31 thorpej * No more will happen with this xfer. This for
389 1.31 thorpej * notification of the upper-level driver only; they
390 1.31 thorpej * won't be returning any meaningful information to us.
391 1.31 thorpej */
392 1.31 thorpej (void)(*sc_link->device->done)(xs, 1);
393 1.31 thorpej }
394 1.22 mycroft scsi_free_xs(xs, SCSI_NOSLEEP);
395 1.1 mycroft }
396 1.1 mycroft
397 1.22 mycroft int
398 1.22 mycroft scsi_execute_xs(xs)
399 1.22 mycroft struct scsi_xfer *xs;
400 1.1 mycroft {
401 1.11 mycroft int error;
402 1.11 mycroft int s;
403 1.1 mycroft
404 1.22 mycroft xs->flags &= ~ITSDONE;
405 1.22 mycroft xs->error = XS_NOERROR;
406 1.22 mycroft xs->resid = xs->datalen;
407 1.1 mycroft
408 1.1 mycroft retry:
409 1.1 mycroft /*
410 1.1 mycroft * Do the transfer. If we are polling we will return:
411 1.1 mycroft * COMPLETE, Was poll, and scsi_done has been called
412 1.1 mycroft * TRY_AGAIN_LATER, Adapter short resources, try again
413 1.1 mycroft *
414 1.1 mycroft * if under full steam (interrupts) it will return:
415 1.1 mycroft * SUCCESSFULLY_QUEUED, will do a wakeup when complete
416 1.1 mycroft * TRY_AGAIN_LATER, (as for polling)
417 1.1 mycroft * After the wakeup, we must still check if it succeeded
418 1.1 mycroft *
419 1.1 mycroft * If we have a bp however, all the error proccessing
420 1.1 mycroft * and the buffer code both expect us to return straight
421 1.1 mycroft * to them, so as soon as the command is queued, return
422 1.1 mycroft */
423 1.22 mycroft switch ((*(xs->sc_link->adapter->scsi_cmd)) (xs)) {
424 1.1 mycroft case SUCCESSFULLY_QUEUED:
425 1.22 mycroft if (xs->bp)
426 1.22 mycroft return EJUSTRETURN;
427 1.1 mycroft s = splbio();
428 1.22 mycroft while ((xs->flags & ITSDONE) == 0)
429 1.4 mycroft tsleep(xs, PRIBIO + 1, "scsi_scsi_cmd", 0);
430 1.1 mycroft splx(s);
431 1.1 mycroft case COMPLETE: /* Polling command completed ok */
432 1.22 mycroft if (xs->bp)
433 1.22 mycroft return EJUSTRETURN;
434 1.22 mycroft doit:
435 1.24 mycroft SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
436 1.22 mycroft if ((error = sc_err1(xs, 0)) != ERESTART)
437 1.22 mycroft return error;
438 1.22 mycroft goto retry;
439 1.1 mycroft
440 1.1 mycroft case TRY_AGAIN_LATER: /* adapter resource shortage */
441 1.22 mycroft xs->error = XS_BUSY;
442 1.22 mycroft goto doit;
443 1.22 mycroft
444 1.1 mycroft default:
445 1.22 mycroft panic("scsi_execute_xs: invalid return code");
446 1.1 mycroft }
447 1.22 mycroft
448 1.22 mycroft #ifdef DIAGNOSTIC
449 1.22 mycroft panic("scsi_execute_xs: impossible");
450 1.22 mycroft #endif
451 1.33 christos return EINVAL;
452 1.22 mycroft }
453 1.22 mycroft
454 1.22 mycroft /*
455 1.22 mycroft * ask the scsi driver to perform a command for us.
456 1.22 mycroft * tell it where to read/write the data, and how
457 1.22 mycroft * long the data is supposed to be. If we have a buf
458 1.22 mycroft * to associate with the transfer, we need that too.
459 1.22 mycroft */
460 1.22 mycroft int
461 1.22 mycroft scsi_scsi_cmd(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
462 1.22 mycroft retries, timeout, bp, flags)
463 1.22 mycroft struct scsi_link *sc_link;
464 1.22 mycroft struct scsi_generic *scsi_cmd;
465 1.22 mycroft int cmdlen;
466 1.22 mycroft u_char *data_addr;
467 1.22 mycroft int datalen;
468 1.22 mycroft int retries;
469 1.22 mycroft int timeout;
470 1.22 mycroft struct buf *bp;
471 1.22 mycroft int flags;
472 1.22 mycroft {
473 1.22 mycroft struct scsi_xfer *xs;
474 1.22 mycroft int error;
475 1.22 mycroft
476 1.22 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("scsi_cmd\n"));
477 1.22 mycroft
478 1.25 mycroft #ifdef DIAGNOSTIC
479 1.25 mycroft if (bp != 0 && (flags & SCSI_NOSLEEP) == 0)
480 1.25 mycroft panic("scsi_scsi_cmd: buffer without nosleep");
481 1.25 mycroft #endif
482 1.25 mycroft
483 1.22 mycroft if ((xs = scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
484 1.22 mycroft retries, timeout, bp, flags)) == NULL)
485 1.22 mycroft return ENOMEM;
486 1.22 mycroft
487 1.22 mycroft if ((error = scsi_execute_xs(xs)) == EJUSTRETURN)
488 1.22 mycroft return 0;
489 1.22 mycroft
490 1.1 mycroft /*
491 1.1 mycroft * we have finished with the xfer stuct, free it and
492 1.1 mycroft * check if anyone else needs to be started up.
493 1.1 mycroft */
494 1.22 mycroft scsi_free_xs(xs, flags);
495 1.11 mycroft return error;
496 1.1 mycroft }
497 1.1 mycroft
498 1.2 mycroft int
499 1.22 mycroft sc_err1(xs, async)
500 1.1 mycroft struct scsi_xfer *xs;
501 1.22 mycroft int async;
502 1.1 mycroft {
503 1.11 mycroft int error;
504 1.1 mycroft
505 1.1 mycroft SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
506 1.22 mycroft
507 1.1 mycroft /*
508 1.1 mycroft * If it has a buf, we might be working with
509 1.1 mycroft * a request from the buffer cache or some other
510 1.1 mycroft * piece of code that requires us to process
511 1.1 mycroft * errors at inetrrupt time. We have probably
512 1.1 mycroft * been called by scsi_done()
513 1.1 mycroft */
514 1.1 mycroft switch (xs->error) {
515 1.1 mycroft case XS_NOERROR: /* nearly always hit this one */
516 1.11 mycroft error = 0;
517 1.1 mycroft break;
518 1.1 mycroft
519 1.1 mycroft case XS_SENSE:
520 1.22 mycroft if ((error = scsi_interpret_sense(xs)) == ERESTART)
521 1.22 mycroft goto retry;
522 1.21 mycroft SC_DEBUG(xs->sc_link, SDEV_DB3,
523 1.21 mycroft ("scsi_interpret_sense returned %d\n", error));
524 1.1 mycroft break;
525 1.1 mycroft
526 1.1 mycroft case XS_BUSY:
527 1.22 mycroft if (xs->retries) {
528 1.22 mycroft if ((xs->flags & SCSI_POLL) != 0)
529 1.22 mycroft delay(1000000);
530 1.22 mycroft else if ((xs->flags & SCSI_NOSLEEP) == 0)
531 1.22 mycroft tsleep(&lbolt, PRIBIO, "scbusy", 0);
532 1.22 mycroft else
533 1.22 mycroft #if 0
534 1.22 mycroft timeout(scsi_requeue, xs, hz);
535 1.22 mycroft #else
536 1.22 mycroft goto lose;
537 1.22 mycroft #endif
538 1.22 mycroft }
539 1.1 mycroft case XS_TIMEOUT:
540 1.22 mycroft retry:
541 1.1 mycroft if (xs->retries--) {
542 1.1 mycroft xs->error = XS_NOERROR;
543 1.1 mycroft xs->flags &= ~ITSDONE;
544 1.22 mycroft return ERESTART;
545 1.1 mycroft }
546 1.1 mycroft case XS_DRIVER_STUFFUP:
547 1.22 mycroft lose:
548 1.22 mycroft error = EIO;
549 1.22 mycroft break;
550 1.22 mycroft
551 1.22 mycroft case XS_SELTIMEOUT:
552 1.22 mycroft /* XXX Disable device? */
553 1.11 mycroft error = EIO;
554 1.1 mycroft break;
555 1.21 mycroft
556 1.1 mycroft default:
557 1.1 mycroft sc_print_addr(xs->sc_link);
558 1.1 mycroft printf("unknown error category from scsi driver\n");
559 1.21 mycroft error = EIO;
560 1.21 mycroft break;
561 1.21 mycroft }
562 1.21 mycroft
563 1.22 mycroft scsi_error(xs, error);
564 1.22 mycroft return error;
565 1.22 mycroft }
566 1.22 mycroft
567 1.22 mycroft void
568 1.22 mycroft scsi_error(xs, error)
569 1.22 mycroft struct scsi_xfer *xs;
570 1.22 mycroft int error;
571 1.22 mycroft {
572 1.22 mycroft struct buf *bp = xs->bp;
573 1.22 mycroft
574 1.21 mycroft if (bp) {
575 1.21 mycroft if (error) {
576 1.22 mycroft bp->b_error = error;
577 1.21 mycroft bp->b_flags |= B_ERROR;
578 1.21 mycroft bp->b_resid = bp->b_bcount;
579 1.21 mycroft } else {
580 1.21 mycroft bp->b_error = 0;
581 1.22 mycroft bp->b_resid = xs->resid;
582 1.21 mycroft }
583 1.22 mycroft biodone(bp);
584 1.1 mycroft }
585 1.1 mycroft }
586 1.1 mycroft
587 1.1 mycroft /*
588 1.1 mycroft * Look at the returned sense and act on the error, determining
589 1.1 mycroft * the unix error number to pass back. (0 = report no error)
590 1.1 mycroft *
591 1.1 mycroft * THIS IS THE DEFAULT ERROR HANDLER
592 1.1 mycroft */
593 1.2 mycroft int
594 1.1 mycroft scsi_interpret_sense(xs)
595 1.1 mycroft struct scsi_xfer *xs;
596 1.1 mycroft {
597 1.1 mycroft struct scsi_sense_data *sense;
598 1.1 mycroft struct scsi_link *sc_link = xs->sc_link;
599 1.22 mycroft u_int8_t key;
600 1.22 mycroft u_int32_t info;
601 1.2 mycroft int error;
602 1.1 mycroft
603 1.22 mycroft static char *error_mes[] = {
604 1.22 mycroft "soft error (corrected)",
605 1.22 mycroft "not ready", "medium error",
606 1.22 mycroft "non-media hardware failure", "illegal request",
607 1.22 mycroft "unit attention", "readonly device",
608 1.22 mycroft "no data found", "vendor unique",
609 1.22 mycroft "copy aborted", "command aborted",
610 1.22 mycroft "search returned equal", "volume overflow",
611 1.22 mycroft "verify miscompare", "unknown error key"
612 1.1 mycroft };
613 1.1 mycroft
614 1.11 mycroft sense = &xs->sense;
615 1.1 mycroft #ifdef SCSIDEBUG
616 1.22 mycroft if ((sc_link->flags & SDEV_DB1) != 0) {
617 1.22 mycroft int count;
618 1.1 mycroft printf("code%x valid%x ",
619 1.1 mycroft sense->error_code & SSD_ERRCODE,
620 1.1 mycroft sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
621 1.1 mycroft printf("seg%x key%x ili%x eom%x fmark%x\n",
622 1.34 mycroft sense->segment,
623 1.34 mycroft sense->flags & SSD_KEY,
624 1.34 mycroft sense->flags & SSD_ILI ? 1 : 0,
625 1.34 mycroft sense->flags & SSD_EOM ? 1 : 0,
626 1.34 mycroft sense->flags & SSD_FILEMARK ? 1 : 0);
627 1.1 mycroft printf("info: %x %x %x %x followed by %d extra bytes\n",
628 1.34 mycroft sense->info[0],
629 1.34 mycroft sense->info[1],
630 1.34 mycroft sense->info[2],
631 1.34 mycroft sense->info[3],
632 1.34 mycroft sense->extra_len);
633 1.1 mycroft printf("extra: ");
634 1.34 mycroft for (count = 0; count < sense->extra_len; count++)
635 1.34 mycroft printf("%x ", sense->extra_bytes[count]);
636 1.1 mycroft printf("\n");
637 1.1 mycroft }
638 1.1 mycroft #endif /*SCSIDEBUG */
639 1.1 mycroft /*
640 1.1 mycroft * If the device has it's own error handler, call it first.
641 1.1 mycroft * If it returns a legit error value, return that, otherwise
642 1.1 mycroft * it wants us to continue with normal error processing.
643 1.1 mycroft */
644 1.1 mycroft if (sc_link->device->err_handler) {
645 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("calling private err_handler()\n"));
646 1.2 mycroft error = (*sc_link->device->err_handler) (xs);
647 1.2 mycroft if (error != -1)
648 1.2 mycroft return error; /* error >= 0 better ? */
649 1.1 mycroft }
650 1.1 mycroft /* otherwise use the default */
651 1.1 mycroft switch (sense->error_code & SSD_ERRCODE) {
652 1.1 mycroft /*
653 1.1 mycroft * If it's code 70, use the extended stuff and interpret the key
654 1.1 mycroft */
655 1.1 mycroft case 0x71: /* delayed error */
656 1.1 mycroft sc_print_addr(sc_link);
657 1.34 mycroft key = sense->flags & SSD_KEY;
658 1.1 mycroft printf(" DELAYED ERROR, key = 0x%x\n", key);
659 1.1 mycroft case 0x70:
660 1.34 mycroft if ((sense->error_code & SSD_ERRCODE_VALID) != 0)
661 1.34 mycroft info = _4btol(sense->info);
662 1.34 mycroft else
663 1.1 mycroft info = 0;
664 1.34 mycroft key = sense->flags & SSD_KEY;
665 1.1 mycroft
666 1.22 mycroft switch (key) {
667 1.23 mycroft case 0x0: /* NO SENSE */
668 1.22 mycroft case 0x1: /* RECOVERED ERROR */
669 1.22 mycroft if (xs->resid == xs->datalen)
670 1.22 mycroft xs->resid = 0; /* not short read */
671 1.22 mycroft case 0xc: /* EQUAL */
672 1.22 mycroft error = 0;
673 1.22 mycroft break;
674 1.22 mycroft case 0x2: /* NOT READY */
675 1.22 mycroft if ((sc_link->flags & SDEV_REMOVABLE) != 0)
676 1.22 mycroft sc_link->flags &= ~SDEV_MEDIA_LOADED;
677 1.22 mycroft if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
678 1.22 mycroft return 0;
679 1.22 mycroft if ((xs->flags & SCSI_SILENT) != 0)
680 1.22 mycroft return EIO;
681 1.22 mycroft error = EIO;
682 1.22 mycroft break;
683 1.22 mycroft case 0x5: /* ILLEGAL REQUEST */
684 1.22 mycroft if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
685 1.22 mycroft return 0;
686 1.22 mycroft error = EINVAL;
687 1.22 mycroft break;
688 1.22 mycroft case 0x6: /* UNIT ATTENTION */
689 1.22 mycroft if ((sc_link->flags & SDEV_REMOVABLE) != 0)
690 1.22 mycroft sc_link->flags &= ~SDEV_MEDIA_LOADED;
691 1.22 mycroft if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
692 1.22 mycroft /* XXX Should reupload any transient state. */
693 1.22 mycroft (sc_link->flags & SDEV_REMOVABLE) == 0)
694 1.22 mycroft return ERESTART;
695 1.22 mycroft if ((xs->flags & SCSI_SILENT) != 0)
696 1.22 mycroft return EIO;
697 1.22 mycroft error = EIO;
698 1.22 mycroft break;
699 1.22 mycroft case 0x7: /* DATA PROTECT */
700 1.22 mycroft error = EACCES;
701 1.22 mycroft break;
702 1.22 mycroft case 0x8: /* BLANK CHECK */
703 1.22 mycroft error = 0;
704 1.22 mycroft break;
705 1.32 briggs case 0xb: /* COMMAND ABORTED */
706 1.32 briggs error = ERESTART;
707 1.32 briggs break;
708 1.22 mycroft case 0xd: /* VOLUME OVERFLOW */
709 1.22 mycroft error = ENOSPC;
710 1.22 mycroft break;
711 1.22 mycroft default:
712 1.22 mycroft error = EIO;
713 1.22 mycroft break;
714 1.22 mycroft }
715 1.22 mycroft
716 1.22 mycroft if (key) {
717 1.1 mycroft sc_print_addr(sc_link);
718 1.1 mycroft printf("%s", error_mes[key - 1]);
719 1.22 mycroft if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
720 1.1 mycroft switch (key) {
721 1.23 mycroft case 0x2: /* NOT READY */
722 1.23 mycroft case 0x5: /* ILLEGAL REQUEST */
723 1.23 mycroft case 0x6: /* UNIT ATTENTION */
724 1.1 mycroft case 0x7: /* DATA PROTECT */
725 1.1 mycroft break;
726 1.1 mycroft case 0x8: /* BLANK CHECK */
727 1.1 mycroft printf(", requested size: %d (decimal)",
728 1.1 mycroft info);
729 1.32 briggs break;
730 1.32 briggs case 0xb:
731 1.32 briggs if (xs->retries)
732 1.32 briggs printf(", retrying");
733 1.32 briggs printf(", cmd 0x%x, info 0x%x",
734 1.32 briggs xs->cmd->opcode, info);
735 1.1 mycroft break;
736 1.1 mycroft default:
737 1.1 mycroft printf(", info = %d (decimal)", info);
738 1.1 mycroft }
739 1.25 mycroft }
740 1.34 mycroft if (sense->extra_len != 0) {
741 1.25 mycroft int n;
742 1.25 mycroft printf(", data =");
743 1.34 mycroft for (n = 0; n < sense->extra_len; n++)
744 1.34 mycroft printf(" %02x", sense->extra_bytes[n]);
745 1.1 mycroft }
746 1.1 mycroft printf("\n");
747 1.1 mycroft }
748 1.22 mycroft return error;
749 1.22 mycroft
750 1.1 mycroft /*
751 1.1 mycroft * Not code 70, just report it
752 1.1 mycroft */
753 1.1 mycroft default:
754 1.22 mycroft sc_print_addr(sc_link);
755 1.22 mycroft printf("error code %d",
756 1.22 mycroft sense->error_code & SSD_ERRCODE);
757 1.22 mycroft if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
758 1.34 mycroft struct scsi_sense_data_unextended *usense =
759 1.34 mycroft (struct scsi_sense_data_unextended *)sense;
760 1.22 mycroft printf(" at block no. %d (decimal)",
761 1.34 mycroft _3btol(usense->block));
762 1.1 mycroft }
763 1.22 mycroft printf("\n");
764 1.11 mycroft return EIO;
765 1.1 mycroft }
766 1.1 mycroft }
767 1.1 mycroft
768 1.1 mycroft /*
769 1.1 mycroft * Utility routines often used in SCSI stuff
770 1.1 mycroft */
771 1.1 mycroft
772 1.1 mycroft
773 1.1 mycroft /*
774 1.1 mycroft * Print out the scsi_link structure's address info.
775 1.1 mycroft */
776 1.1 mycroft void
777 1.1 mycroft sc_print_addr(sc_link)
778 1.22 mycroft struct scsi_link *sc_link;
779 1.1 mycroft {
780 1.1 mycroft
781 1.17 mycroft printf("%s(%s:%d:%d): ",
782 1.22 mycroft sc_link->device_softc ?
783 1.22 mycroft ((struct device *)sc_link->device_softc)->dv_xname : "probe",
784 1.22 mycroft ((struct device *)sc_link->adapter_softc)->dv_xname,
785 1.22 mycroft sc_link->target, sc_link->lun);
786 1.1 mycroft }
787 1.1 mycroft
788 1.1 mycroft #ifdef SCSIDEBUG
789 1.1 mycroft /*
790 1.1 mycroft * Given a scsi_xfer, dump the request, in all it's glory
791 1.1 mycroft */
792 1.1 mycroft void
793 1.1 mycroft show_scsi_xs(xs)
794 1.1 mycroft struct scsi_xfer *xs;
795 1.1 mycroft {
796 1.36 christos printf("xs(%p): ", xs);
797 1.1 mycroft printf("flg(0x%x)", xs->flags);
798 1.36 christos printf("sc_link(%p)", xs->sc_link);
799 1.1 mycroft printf("retr(0x%x)", xs->retries);
800 1.1 mycroft printf("timo(0x%x)", xs->timeout);
801 1.36 christos printf("cmd(%p)", xs->cmd);
802 1.1 mycroft printf("len(0x%x)", xs->cmdlen);
803 1.36 christos printf("data(%p)", xs->data);
804 1.1 mycroft printf("len(0x%x)", xs->datalen);
805 1.1 mycroft printf("res(0x%x)", xs->resid);
806 1.1 mycroft printf("err(0x%x)", xs->error);
807 1.36 christos printf("bp(%p)", xs->bp);
808 1.1 mycroft show_scsi_cmd(xs);
809 1.1 mycroft }
810 1.1 mycroft
811 1.1 mycroft void
812 1.15 deraadt show_scsi_cmd(xs)
813 1.15 deraadt struct scsi_xfer *xs;
814 1.1 mycroft {
815 1.1 mycroft u_char *b = (u_char *) xs->cmd;
816 1.1 mycroft int i = 0;
817 1.1 mycroft
818 1.1 mycroft sc_print_addr(xs->sc_link);
819 1.1 mycroft printf("command: ");
820 1.1 mycroft
821 1.22 mycroft if ((xs->flags & SCSI_RESET) == 0) {
822 1.1 mycroft while (i < xs->cmdlen) {
823 1.1 mycroft if (i)
824 1.1 mycroft printf(",");
825 1.1 mycroft printf("%x", b[i++]);
826 1.1 mycroft }
827 1.1 mycroft printf("-[%d bytes]\n", xs->datalen);
828 1.1 mycroft if (xs->datalen)
829 1.1 mycroft show_mem(xs->data, min(64, xs->datalen));
830 1.2 mycroft } else
831 1.1 mycroft printf("-RESET-\n");
832 1.1 mycroft }
833 1.1 mycroft
834 1.1 mycroft void
835 1.1 mycroft show_mem(address, num)
836 1.24 mycroft u_char *address;
837 1.24 mycroft int num;
838 1.1 mycroft {
839 1.24 mycroft int x;
840 1.24 mycroft
841 1.1 mycroft printf("------------------------------");
842 1.24 mycroft for (x = 0; x < num; x++) {
843 1.24 mycroft if ((x % 16) == 0)
844 1.24 mycroft printf("\n%03d: ", x);
845 1.1 mycroft printf("%02x ", *address++);
846 1.1 mycroft }
847 1.1 mycroft printf("\n------------------------------\n");
848 1.1 mycroft }
849 1.1 mycroft #endif /*SCSIDEBUG */
850