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