xref: /netbsd-src/sys/dev/scsipi/scsipi_base.c (revision 93f9db1b75d415b78f73ed629beeb86235153473)
1 /*	$NetBSD: scsipi_base.c,v 1.12 1998/10/10 03:42:53 thorpej 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 (SCSIPI_XFER_ASYNC(xs))
320 			scsipi_free_xs(xs, SCSI_NOSLEEP);
321 		SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
322 		return;
323 	}
324 
325 	if (!SCSIPI_XFER_ASYNC(xs)) {
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 (SCSIPI_XFER_ASYNC(xs))
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 async;
388 	int error;
389 	int s;
390 
391 	xs->flags &= ~ITSDONE;
392 	xs->error = XS_NOERROR;
393 	xs->resid = xs->datalen;
394 	xs->status = 0;
395 
396 retry:
397 	/*
398 	 * Do the transfer. If we are polling we will return:
399 	 * COMPLETE,  Was poll, and scsipi_done has been called
400 	 * TRY_AGAIN_LATER, Adapter short resources, try again
401 	 *
402 	 * if under full steam (interrupts) it will return:
403 	 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
404 	 * TRY_AGAIN_LATER, (as for polling)
405 	 * After the wakeup, we must still check if it succeeded
406 	 *
407 	 * If we have a SCSI_NOSLEEP (typically because we have a buf)
408 	 * we just return.  All the error proccessing and the buffer
409 	 * code both expect us to return straight to them, so as soon
410 	 * as the command is queued, return.
411 	 */
412 #ifdef SCSIDEBUG
413 	if (xs->sc_link->flags & SDEV_DB3) {
414 		printf("scsipi_exec_cmd: ");
415 		show_scsipi_xs(xs);
416 		printf("\n");
417 	}
418 #endif
419 	async = SCSIPI_XFER_ASYNC(xs);
420 	switch (scsipi_command_direct(xs)) {
421 	case SUCCESSFULLY_QUEUED:
422 		if (async) {
423 			/* scsipi_done() will free the scsipi_xfer. */
424 			return (EJUSTRETURN);
425 		}
426 #ifdef DIAGNOSTIC
427 		if (xs->flags & SCSI_NOSLEEP)
428 			panic("scsipi_execute_xs: NOSLEEP and POLL");
429 #endif
430 		s = splbio();
431 		while ((xs->flags & ITSDONE) == 0)
432 			tsleep(xs, PRIBIO + 1, "scsipi_cmd", 0);
433 		splx(s);
434 	case COMPLETE:		/* Polling command completed ok */
435 		if (xs->bp)
436 			return (0);
437 	doit:
438 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
439 		if ((error = sc_err1(xs, 0)) != ERESTART)
440 			return (error);
441 		goto retry;
442 
443 	case TRY_AGAIN_LATER:	/* adapter resource shortage */
444 		xs->error = XS_BUSY;
445 		goto doit;
446 
447 	default:
448 		panic("scsipi_execute_xs: invalid return code");
449 	}
450 
451 #ifdef DIAGNOSTIC
452 	panic("scsipi_execute_xs: impossible");
453 #endif
454 	return (EINVAL);
455 }
456 
457 int
458 sc_err1(xs, async)
459 	struct scsipi_xfer *xs;
460 	int async;
461 {
462 	int error;
463 
464 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
465 
466 	/*
467 	 * If it has a buf, we might be working with
468 	 * a request from the buffer cache or some other
469 	 * piece of code that requires us to process
470 	 * errors at inetrrupt time. We have probably
471 	 * been called by scsipi_done()
472 	 */
473 	switch (xs->error) {
474 	case XS_NOERROR:	/* nearly always hit this one */
475 		error = 0;
476 		break;
477 
478 	case XS_SENSE:
479 		if ((error = (*xs->sc_link->scsipi_interpret_sense)(xs)) ==
480 		    ERESTART)
481 			goto retry;
482 		SC_DEBUG(xs->sc_link, SDEV_DB3,
483 		    ("scsipi_interpret_sense returned %d\n", error));
484 		break;
485 
486 	case XS_BUSY:
487 		if (xs->retries) {
488 			if ((xs->flags & SCSI_POLL) != 0)
489 				delay(1000000);
490 			else if ((xs->flags & SCSI_NOSLEEP) == 0)
491 				tsleep(&lbolt, PRIBIO, "scbusy", 0);
492 			else
493 #if 0
494 				timeout(scsipi_requeue, xs, hz);
495 #else
496 				goto lose;
497 #endif
498 		}
499 	case XS_TIMEOUT:
500 	retry:
501 		if (xs->retries) {
502 			xs->retries--;
503 			xs->error = XS_NOERROR;
504 			xs->flags &= ~ITSDONE;
505 			return (ERESTART);
506 		}
507 	case XS_DRIVER_STUFFUP:
508 	lose:
509 		error = EIO;
510 		break;
511 
512 	case XS_SELTIMEOUT:
513 		/* XXX Disable device? */
514 		error = EIO;
515 		break;
516 
517 	case XS_RESET:
518 		if (xs->retries) {
519 			SC_DEBUG(xs->sc_link, SDEV_DB3,
520 			    ("restarting command destroyed by reset\n"));
521 			goto retry;
522 		}
523 		error = EIO;
524 		break;
525 
526 	default:
527 		(*xs->sc_link->sc_print_addr)(xs->sc_link);
528 		printf("unknown error category from scsipi driver\n");
529 		error = EIO;
530 		break;
531 	}
532 
533 	return (error);
534 }
535 
536 #ifdef	SCSIDEBUG
537 /*
538  * Given a scsipi_xfer, dump the request, in all it's glory
539  */
540 void
541 show_scsipi_xs(xs)
542 	struct scsipi_xfer *xs;
543 {
544 
545 	printf("xs(%p): ", xs);
546 	printf("flg(0x%x)", xs->flags);
547 	printf("sc_link(%p)", xs->sc_link);
548 	printf("retr(0x%x)", xs->retries);
549 	printf("timo(0x%x)", xs->timeout);
550 	printf("cmd(%p)", xs->cmd);
551 	printf("len(0x%x)", xs->cmdlen);
552 	printf("data(%p)", xs->data);
553 	printf("len(0x%x)", xs->datalen);
554 	printf("res(0x%x)", xs->resid);
555 	printf("err(0x%x)", xs->error);
556 	printf("bp(%p)", xs->bp);
557 	show_scsipi_cmd(xs);
558 }
559 
560 void
561 show_scsipi_cmd(xs)
562 	struct scsipi_xfer *xs;
563 {
564 	u_char *b = (u_char *) xs->cmd;
565 	int i = 0;
566 
567 	(*xs->sc_link->sc_print_addr)(xs->sc_link);
568 	printf("command: ");
569 
570 	if ((xs->flags & SCSI_RESET) == 0) {
571 		while (i < xs->cmdlen) {
572 			if (i)
573 				printf(",");
574 			printf("0x%x", b[i++]);
575 		}
576 		printf("-[%d bytes]\n", xs->datalen);
577 		if (xs->datalen)
578 			show_mem(xs->data, min(64, xs->datalen));
579 	} else
580 		printf("-RESET-\n");
581 }
582 
583 void
584 show_mem(address, num)
585 	u_char *address;
586 	int num;
587 {
588 	int x;
589 
590 	printf("------------------------------");
591 	for (x = 0; x < num; x++) {
592 		if ((x % 16) == 0)
593 			printf("\n%03d: ", x);
594 		printf("%02x ", *address++);
595 	}
596 	printf("\n------------------------------\n");
597 }
598 #endif /*SCSIDEBUG */
599 
600