scsipi_base.c revision 1.9 1 /* $NetBSD: scsipi_base.c,v 1.9 1998/09/14 05:49:21 scottr 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 /*
315 * If this was an asynchronous operation (i.e. adapter
316 * returned SUCCESSFULLY_QUEUED when the command was
317 * submitted), we need to free the scsipi_xfer here.
318 */
319 if (xs->flags & SCSI_ASYNCREQ)
320 scsipi_free_xs(xs, SCSI_NOSLEEP);
321 SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
322 return;
323 }
324
325 if (!((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)) {
326 /*
327 * if it's a normal upper level request, then ask
328 * the upper level code to handle error checking
329 * rather than doing it here at interrupt time
330 */
331 wakeup(xs);
332 return;
333 }
334
335 /*
336 * Go and handle errors now.
337 * If it returns ERESTART then we should RETRY
338 */
339 retry:
340 error = sc_err1(xs, 1);
341 if (error == ERESTART)
342 switch (scsipi_command_direct(xs)) {
343 case SUCCESSFULLY_QUEUED:
344 return;
345
346 case TRY_AGAIN_LATER:
347 xs->error = XS_BUSY;
348 case COMPLETE:
349 goto retry;
350 }
351
352 bp = xs->bp;
353 if (bp) {
354 if (error) {
355 bp->b_error = error;
356 bp->b_flags |= B_ERROR;
357 bp->b_resid = bp->b_bcount;
358 } else {
359 bp->b_error = 0;
360 bp->b_resid = xs->resid;
361 }
362 }
363 if (sc_link->device->done) {
364 /*
365 * Tell the device the operation is actually complete.
366 * No more will happen with this xfer. This for
367 * notification of the upper-level driver only; they
368 * won't be returning any meaningful information to us.
369 */
370 (*sc_link->device->done)(xs);
371 }
372 /*
373 * If this was an asynchronous operation (i.e. adapter
374 * returned SUCCESSFULLY_QUEUED when the command was
375 * submitted), we need to free the scsipi_xfer here.
376 */
377 if (xs->flags & SCSI_ASYNCREQ)
378 scsipi_free_xs(xs, SCSI_NOSLEEP);
379 if (bp)
380 biodone(bp);
381 }
382
383 int
384 scsipi_execute_xs(xs)
385 struct scsipi_xfer *xs;
386 {
387 int error;
388 int s;
389
390 xs->flags &= ~(ITSDONE|SCSI_ASYNCREQ);
391 xs->error = XS_NOERROR;
392 xs->resid = xs->datalen;
393 xs->status = 0;
394
395 retry:
396 /*
397 * Do the transfer. If we are polling we will return:
398 * COMPLETE, Was poll, and scsipi_done has been called
399 * TRY_AGAIN_LATER, Adapter short resources, try again
400 *
401 * if under full steam (interrupts) it will return:
402 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
403 * TRY_AGAIN_LATER, (as for polling)
404 * After the wakeup, we must still check if it succeeded
405 *
406 * If we have a SCSI_NOSLEEP (typically because we have a buf)
407 * we just return. All the error proccessing and the buffer
408 * code both expect us to return straight to them, so as soon
409 * as the command is queued, return.
410 */
411 #ifdef SCSIDEBUG
412 if (xs->sc_link->flags & SDEV_DB3) {
413 printf("scsipi_exec_cmd: ");
414 show_scsipi_xs(xs);
415 printf("\n");
416 }
417 #endif
418 switch (scsipi_command_direct(xs)) {
419 case SUCCESSFULLY_QUEUED:
420 if ((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP) {
421 /*
422 * The request will complete asynchronously. In this
423 * case, we need scsipi_done() to free the scsipi_xfer.
424 */
425 xs->flags |= SCSI_ASYNCREQ;
426 return (EJUSTRETURN);
427 }
428 #ifdef DIAGNOSTIC
429 if (xs->flags & SCSI_NOSLEEP)
430 panic("scsipi_execute_xs: NOSLEEP and POLL");
431 #endif
432 s = splbio();
433 while ((xs->flags & ITSDONE) == 0)
434 tsleep(xs, PRIBIO + 1, "scsipi_cmd", 0);
435 splx(s);
436 case COMPLETE: /* Polling command completed ok */
437 if (xs->bp)
438 return (0);
439 doit:
440 SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
441 if ((error = sc_err1(xs, 0)) != ERESTART)
442 return (error);
443 goto retry;
444
445 case TRY_AGAIN_LATER: /* adapter resource shortage */
446 xs->error = XS_BUSY;
447 goto doit;
448
449 default:
450 panic("scsipi_execute_xs: invalid return code");
451 }
452
453 #ifdef DIAGNOSTIC
454 panic("scsipi_execute_xs: impossible");
455 #endif
456 return (EINVAL);
457 }
458
459 int
460 sc_err1(xs, async)
461 struct scsipi_xfer *xs;
462 int async;
463 {
464 int error;
465
466 SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
467
468 /*
469 * If it has a buf, we might be working with
470 * a request from the buffer cache or some other
471 * piece of code that requires us to process
472 * errors at inetrrupt time. We have probably
473 * been called by scsipi_done()
474 */
475 switch (xs->error) {
476 case XS_NOERROR: /* nearly always hit this one */
477 error = 0;
478 break;
479
480 case XS_SENSE:
481 if ((error = (*xs->sc_link->scsipi_interpret_sense)(xs)) ==
482 ERESTART)
483 goto retry;
484 SC_DEBUG(xs->sc_link, SDEV_DB3,
485 ("scsipi_interpret_sense returned %d\n", error));
486 break;
487
488 case XS_BUSY:
489 if (xs->retries) {
490 if ((xs->flags & SCSI_POLL) != 0)
491 delay(1000000);
492 else if ((xs->flags & SCSI_NOSLEEP) == 0)
493 tsleep(&lbolt, PRIBIO, "scbusy", 0);
494 else
495 #if 0
496 timeout(scsipi_requeue, xs, hz);
497 #else
498 goto lose;
499 #endif
500 }
501 case XS_TIMEOUT:
502 retry:
503 if (xs->retries--) {
504 xs->error = XS_NOERROR;
505 xs->flags &= ~ITSDONE;
506 return (ERESTART);
507 }
508 case XS_DRIVER_STUFFUP:
509 lose:
510 error = EIO;
511 break;
512
513 case XS_SELTIMEOUT:
514 /* XXX Disable device? */
515 error = EIO;
516 break;
517
518 default:
519 (*xs->sc_link->sc_print_addr)(xs->sc_link);
520 printf("unknown error category from scsipi driver\n");
521 error = EIO;
522 break;
523 }
524
525 return (error);
526 }
527
528 #ifdef SCSIDEBUG
529 /*
530 * Given a scsipi_xfer, dump the request, in all it's glory
531 */
532 void
533 show_scsipi_xs(xs)
534 struct scsipi_xfer *xs;
535 {
536
537 printf("xs(%p): ", xs);
538 printf("flg(0x%x)", xs->flags);
539 printf("sc_link(%p)", xs->sc_link);
540 printf("retr(0x%x)", xs->retries);
541 printf("timo(0x%x)", xs->timeout);
542 printf("cmd(%p)", xs->cmd);
543 printf("len(0x%x)", xs->cmdlen);
544 printf("data(%p)", xs->data);
545 printf("len(0x%x)", xs->datalen);
546 printf("res(0x%x)", xs->resid);
547 printf("err(0x%x)", xs->error);
548 printf("bp(%p)", xs->bp);
549 show_scsipi_cmd(xs);
550 }
551
552 void
553 show_scsipi_cmd(xs)
554 struct scsipi_xfer *xs;
555 {
556 u_char *b = (u_char *) xs->cmd;
557 int i = 0;
558
559 (*xs->sc_link->sc_print_addr)(xs->sc_link);
560 printf("command: ");
561
562 if ((xs->flags & SCSI_RESET) == 0) {
563 while (i < xs->cmdlen) {
564 if (i)
565 printf(",");
566 printf("0x%x", b[i++]);
567 }
568 printf("-[%d bytes]\n", xs->datalen);
569 if (xs->datalen)
570 show_mem(xs->data, min(64, xs->datalen));
571 } else
572 printf("-RESET-\n");
573 }
574
575 void
576 show_mem(address, num)
577 u_char *address;
578 int num;
579 {
580 int x;
581
582 printf("------------------------------");
583 for (x = 0; x < num; x++) {
584 if ((x % 16) == 0)
585 printf("\n%03d: ", x);
586 printf("%02x ", *address++);
587 }
588 printf("\n------------------------------\n");
589 }
590 #endif /*SCSIDEBUG */
591
592