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