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