2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 /* @(#)scsi-apollo.c 1.5 04/01/14 Copyright 1997,2000 J. Schilling */
15 * Code to support Apollo Domain/OS 10.4.1
17 * Copyright (c) 1997,2000 J. Schilling
18 * Apollo support code written by Paul Walker.
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License version 2
23 * as published by the Free Software Foundation.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License along with
31 * this program; see the file COPYING. If not, write to the Free Software
32 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #include <apollo/base.h>
36 #include <apollo/scsi.h>
38 #define DomainScsiTimeout 100000
41 * Warning: you may change this source, but if you do that
42 * you need to change the _usal_version and _usal_auth* string below.
43 * You may not return "schily" for an SCG_AUTHOR request anymore.
44 * Choose your name instead of "schily" and make clear that the version
45 * string is related to a modified source.
47 static char _usal_trans_version[] = "scsi-apollo.c-1.5"; /* The version for this transport */
50 #define MAX_SCG 1 /* Max # of SCSI controllers */
51 #define MAX_TGT 1 /* Max # of SCSI targets */
52 #define MAX_LUN 1 /* Max # of SCSI logical units */
55 scsi_$handle_t handle;
56 unsigned char *DomainSensePointer;
57 short usalfiles[MAX_SCG][MAX_TGT][MAX_LUN];
60 #define usallocal(p) ((struct usal_local *)((p)->local))
63 #define SG_MAX_SENSE 16 /* Too small for CCS / SCSI-2 */
64 #endif /* But cannot be changed */
67 * Return version information for the low level SCSI transport code.
68 * This has been introduced to make it easier to trace down problems
72 usalo_version(SCSI *usalp, int what)
74 if (usalp != (SCSI *)0) {
78 return (_usal_trans_version);
80 * If you changed this source, you are not allowed to
81 * return "schily" for the SCG_AUTHOR request.
84 return ("Paul Walker");
93 usalo_help(SCSI *usalp, FILE *f)
95 __usal_help(f, "scsi_$do_command_2", "SCSI transport from Apollo DomainOS drivers",
96 "", "DomainOS driver name", "A DomainOS device node name", FALSE, TRUE);
101 usalo_open(SCSI *usalp, char *device)
103 register int nopen = 0;
106 if (usalp->debug > 1)
107 printf("Entered scsi_open, usalp=%p, device='%s'\n", usalp, device);
108 if (usalp->local == NULL) {
109 usalp->local = malloc(sizeof (struct usal_local));
110 if (usalp->local == NULL)
113 if (device == NULL || *device == '\0') {
114 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE, "Must give device name");
118 scsi_$acquire(device, strlen(device), &usallocal(usalp)->handle, &status);
121 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE, "Cannot open '%s', status %08x", device, status.all);
125 * Allocate the sense buffer
127 usallocal(usalp)->DomainSensePointer = (Uchar *)valloc((size_t) (SCG_MAX_SENSE + getpagesize()));
128 assert(status.all == 0);
130 * Wire the sense buffer
132 scsi_$wire(usallocal(usalp)->handle, (caddr_t)(usallocal(usalp)->DomainSensePointer), SCG_MAX_SENSE, &status);
133 assert(status.all == 0);
135 if (usallocal(usalp)->usalfiles[0][0][0] == -1)
136 usallocal(usalp)->usalfiles[0][0][0] = 1;
137 usal_settarget(usalp, 0, 0, 0);
142 usalo_close(SCSI *usalp)
146 if (usalp->debug > 1)
147 printf("Entering scsi_close\n");
148 scsi_$release(usallocal(usalp)->handle, &status);
150 * should also unwire the sense buffer
157 usalo_maxdma(SCSI *usalp, long amt)
162 scsi_$get_info(usallocal(usalp)->handle, sizeof (info), &info, &status);
165 * Should have some better error handling here
167 printf("scsi_$get_info returned %08x\n", status.all);
170 return (info.max_xfer);
175 usalo_getbuf(SCSI *usalp, long amt)
179 if (usalp->debug > 1)
180 printf("scsi_getbuf: %ld bytes\n", amt);
181 ret = valloc((size_t)amt);
184 usalp->bufbase = ret;
189 usalo_freebuf(SCSI *usalp)
191 if (usalp->debug > 1)
192 printf("Entering scsi_freebuf\n");
195 free(usalp->bufbase);
196 usalp->bufbase = NULL;
200 usalo_havebus(SCSI *usalp, int busno)
205 if (usalp->debug > 1)
206 printf("Entered scsi_havebus: usalp=%p, busno=%d\n", usalp, busno);
208 if (busno < 0 || busno >= MAX_SCG)
211 if (usalp->local == NULL)
214 for (t = 0; t < MAX_TGT; t++) {
215 for (l = 0; l < MAX_LUN; l++)
216 if (usallocal(usalp)->usalfiles[busno][t][l] >= 0)
223 usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
225 if (usalp->debug > 1)
226 printf("Entered scsi_fileno: usalp=%p, busno=%d, tgt=%d, tlun=%d\n", usalp, busno, tgt, tlun);
227 if (busno < 0 || busno >= MAX_SCG ||
228 tgt < 0 || tgt >= MAX_TGT ||
229 tlun < 0 || tlun >= MAX_LUN)
232 if (usalp->local == NULL)
234 if (usalp->debug > 1)
235 printf("exiting scsi_fileno, returning %d\n", usallocal(usalp)->usalfiles[busno][tgt][tlun]);
236 return ((int) usallocal(usalp)->usalfiles[busno][tgt][tlun]);
240 usalo_initiator_id(SCSI *usalp)
242 if (usalp->debug > 1)
243 printf("Entering scsi_initiator\n");
249 usalo_isatapi(SCSI *usalp)
255 usalo_reset(SCSI *usalp, int what)
259 if (usalp->debug > 0)
260 printf("Entering scsi_reset\n");
262 if (what == SCG_RESET_NOP)
265 if (what == SCG_RESET_TGT) {
266 scsi_$reset_device(usallocal(usalp)->handle, &status);
268 printf("Error - scsi_$reset_device failed, status is 0x%08x\n", status.all);
277 scsi_do_sense(SCSI *usalp, struct usal_cmd *sp)
279 scsi_$op_status_t op_status;
280 static scsi_$cdb_t sense_cdb;
281 static linteger sense_cdb_size;
282 static linteger sense_buffer_size;
283 static scsi_$operation_id_t sense_op_id;
284 static status_$t sense_status;
285 static pinteger sense_return_count;
289 * Issue the sense command (wire, issue, wait, unwire
291 sense_buffer_size = sp->sense_len;
292 sense_cdb_size = SC_G0_CDBLEN;
293 memset(sense_cdb.all, 0, sense_cdb_size); /* Assuming Apollo sense */
294 /* structure is correct */
296 sense_cdb.g0.cmd = SC_REQUEST_SENSE;
297 sense_cdb.g0.lun = sp->cdb.g0_cdb.lun;
298 sense_cdb.g0.len = sp->sense_len;
299 scsi_$do_command_2(usallocal(usalp)->handle, sense_cdb, sense_cdb_size, (caddr_t) (usallocal(usalp)->DomainSensePointer), sense_buffer_size, scsi_read, &sense_op_id, &sense_status);
300 if (sense_status.all) {
301 printf("Error executing sense command, status is 0x%08x\n", sense_status.all);
303 scsi_$wait(usallocal(usalp)->handle, DomainScsiTimeout, true, sense_op_id, 1, &op_status, &sense_return_count, &sense_status);
305 * Print the sense information if debug is on, or if the information is
308 if (usalp->debug > 0 ||
310 * I don't prinqqt info for sense codes 0, 2, 5, 6 because
311 * they aren't interesting
313 (((u_char *) usallocal(usalp)->DomainSensePointer)[2] == 1) ||
314 (((u_char *) usallocal(usalp)->DomainSensePointer)[2] == 3) ||
315 (((u_char *) usallocal(usalp)->DomainSensePointer)[2] == 4) ||
316 (((u_char *) usallocal(usalp)->DomainSensePointer)[2] >= 7)) {
317 printf(" Sense dump:\n");
318 for (i = 0; i < sp->sense_len; i++)
319 printf(" %02x", ((u_char *) usallocal(usalp)->DomainSensePointer)[i]);
322 if (((u_char *) usallocal(usalp)->DomainSensePointer)[2] == 5) {
326 printf("Illegal command detected, ASC=0x%02x, ASQ=0x%02x\n", ((u_char *) usallocal(usalp)->DomainSensePointer)[12], ((u_char *) usallocal(usalp)->DomainSensePointer)[13]);
329 * Copy the sense information to the driver
331 memcpy(sp->u_sense.cmd_sense, usallocal(usalp)->DomainSensePointer, sp->sense_len);
332 sp->sense_count = sp->sense_len;
337 usalo_send(SCSI *usalp)
339 linteger buffer_length;
341 scsi_$operation_id_t operation;
342 scsi_$wait_index_t wait_index;
343 scsi_$op_status_t op_status;
344 pinteger return_count;
346 char *ascii_wait_status;
348 struct usal_cmd *sp = usalp->scmd;
351 sp->error = SCG_FATAL;
355 if (usalp->debug > 0) {
356 printf("Entered usalo_send, usalp=%p, sp=%p\n", usalp, sp);
357 printf("usalcmd dump:\n");
358 printf(" addr=%p\n", sp->addr);
359 printf(" size=0x%x\n", sp->size);
360 printf(" flags=0x%x\n", sp->flags);
361 printf(" cdb_len=%d\n", sp->cdb_len);
362 printf(" sense_len=%d\n", sp->sense_len);
363 printf(" timeout=%d\n", sp->timeout);
364 printf(" kdebug=%d\n", sp->kdebug);
366 for (i = 0; i < sp->cdb_len; i++)
367 printf(" %02x", sp->cdb.cmd_cdb[i]);
372 * Assume complete transfer, so residual count = 0
375 buffer_length = sp->size;
377 if (usalp->debug > 0)
378 printf(" wiring 0x%x bytes at 0x%x\n", buffer_length, sp->addr);
379 scsi_$wire(usallocal(usalp)->handle, sp->addr, buffer_length, &status);
382 * Need better error handling
384 printf("scsi_$wire failed, 0x%08x\n", status.all);
387 cdb_len = sp->cdb_len;
388 scsi_$do_command_2(usallocal(usalp)->handle, /* device handle*/
389 *(scsi_$cdb_t *) &(sp->cdb.cmd_cdb[0]), /* SCSI CDB */
390 cdb_len, /* CDB len */
391 sp->addr, /* DMA buf */
392 buffer_length, /* DMA len */
393 (sp->flags & SCG_RECV_DATA) ? scsi_read : scsi_write,
394 &operation, /* OP ID */
395 &status); /* Status ret */
399 * Need better error handling
401 printf("scsi_$do_command failed, 0x%08x\n", status.all);
402 sp->error = SCG_FATAL;
405 } else if (usalp->debug > 0) {
406 printf("Command submitted, operation=0x%x\n", operation);
408 wait_index = scsi_$wait(usallocal(usalp)->handle, /* device handle*/
409 sp->timeout * 1000, /* timeout */
410 0, /* async enable */
411 operation, /* OP ID */
413 &op_status, /* status list */
414 &return_count, /* count ret */
415 &status); /* Status ret */
418 * Need better error handling
420 printf("scsi_$wait failed, 0x%08x\n", status.all);
421 sp->error = SCG_FATAL;
425 if (usalp->debug > 0) {
426 printf("wait_index=%d, return_count=%d, op_status: op=0x%x, cmd_status=0x%x, op_status=0x%x\n",
427 wait_index, return_count, op_status.op, op_status.cmd_status, op_status.op_status);
429 switch (wait_index) {
431 case scsi_device_advance:
432 ascii_wait_status = "scsi_device_advance";
435 ascii_wait_status = "scsi_timeout";
437 case scsi_async_fault:
438 ascii_wait_status = "scsi_async_fault";
441 ascii_wait_status = "unknown";
445 * See if the scsi_$wait terminated "abnormally"
447 if (wait_index != scsi_device_advance) {
448 printf("scsi_$wait terminated abnormally, status='%s'\n", ascii_wait_status);
449 sp->error = SCG_FATAL;
454 * Normal termination, what's the scoop?
456 assert(return_count == 1);
457 switch (op_status.cmd_status.all) {
460 switch (op_status.op_status) {
462 case scsi_good_status:
463 sp->error = SCG_NO_ERROR;
467 sp->error = SCG_NO_ERROR;
470 case scsi_check_condition:
471 if (usalp->debug > 0)
472 printf("SCSI ERROR - CheckCondition\n");
473 scsi_do_sense(usalp, sp);
475 * If this was a media error, then call it retryable,
476 * instead of no error
478 if ((((u_char *) usallocal(usalp)->DomainSensePointer)[0] == 0xf0) &&
479 (((u_char *) usallocal(usalp)->DomainSensePointer)[2] == 0x03)) {
480 if (usalp->debug > 0)
481 printf(" (retryable)\n");
482 sp->error = SCG_RETRYABLE;
485 /* printf(" (no error)\n"); */
486 sp->error = SCG_NO_ERROR;
492 * I fault out in this case because I want to know
493 * about this error, and this guarantees that it will
496 printf("Unhandled Domain/OS op_status error: status=%08x\n",
497 op_status.op_status);
502 * Handle recognized error conditions by copying the error
505 case scsi_$operation_timeout:
506 printf("SCSI ERROR - Timeout\n");
507 scsi_do_sense(usalp, sp);
508 sp->error = SCG_TIMEOUT;
511 case scsi_$dma_underrun:
513 * This condition seems to occur occasionaly. I no longer
514 * complain because it doesn't seem to matter.
516 if (usalp->debug > 0)
517 printf("SCSI ERROR - Underrun\n");
518 scsi_do_sense(usalp, sp);
519 sp->resid = sp->size; /* We don't have the right number */
520 sp->error = SCG_RETRYABLE;
523 case scsi_$hdwr_failure: /* received when both scanners were active */
524 printf("SCSI ERROR - Hardware Failure\n");
525 scsi_do_sense(usalp, sp);
526 sp->error = SCG_RETRYABLE;
530 printf("\nUnhandled Domain/OS cmd_status error: status=%08x\n", op_status.cmd_status.all);
531 error_$print(op_status.cmd_status);
536 if (usalp->debug > 0)
537 printf(" unwiring buffer\n");
538 scsi_$unwire(usallocal(usalp)->handle, sp->addr, buffer_length, sp->flags & SCG_RECV_DATA, &status);
541 * Need better error handling
543 printf("scsi_$unwire failed, 0x%08x\n", status.all);
544 sp->error = SCG_FATAL;