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-amigaos.c 1.6 04/01/14 Copyright 1997,2000-2003 J. Schilling */
15 * Interface for the AmigaOS generic SCSI implementation.
17 * Warning: you may change this source, but if you do that
18 * you need to change the _usal_version and _usal_auth* string below.
19 * You may not return "schily" for an SCG_AUTHOR request anymore.
20 * Choose your name instead of "schily" and make clear that the version
21 * string is related to a modified source.
23 * Copyright (c) 1997, 2000-2003 J. Schilling
24 * AmigaOS support code written by T. Langer
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License version 2
29 * as published by the Free Software Foundation.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License along with
37 * this program; see the file COPYING. If not, write to the Free Software
38 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 #include <exec/ports.h>
46 #include <exec/errors.h>
47 #include <devices/scsidisk.h>
48 #include <devices/timer.h>
49 #include <exec/semaphores.h>
50 #include <exec/memory.h>
51 #include <exec/execbase.h>
52 #include <clib/exec_protos.h>
53 #include <clib/alib_protos.h>
56 * Warning: you may change this source, but if you do that
57 * you need to change the _usal_version and _usal_auth* string below.
58 * You may not return "schily" for an SCG_AUTHOR request anymore.
59 * Choose your name instead of "schily" and make clear that the version
60 * string is related to a modified source.
62 static char _usal_trans_version[] = "scsi-amigaos.c-1.6"; /* The version for this transport */
63 static char _usal_auth[] = "T. Langer";
65 #define MAX_SCG 8 /* Max # of SCSI controllers */
68 #define MAX_DEV MAX_SCG*MAX_TGT*MAX_LUN
71 int usalfiles[MAX_SCG][MAX_TGT][MAX_LUN];
74 #define usallocal(p) ((struct usal_local*)((p)->local))
76 #define MAX_DMA_AMIGAOS (64*1024)
79 struct IOStdReq *ioReq;
83 static char *devs[MAX_SCG];
84 static struct MsgPort *ioMsgPort = NULL;
85 static struct timerequest *timer_io = NULL;
86 static struct MsgPort *timerMsgPort = NULL;
87 static int initialized = 0;
88 static int last_bus = -1;
89 /* my private var: for debug purpose only */
90 static int ami_debug = 0;
92 extern struct ExecBase *SysBase;
93 #define IOERR_TIMEOUT (-8)
94 #define CHECK_CONDITION 0x02
95 #define DUNIT(b, t, l) (100 * b) + (10 * (l < 0 ? 0:l)) + t
97 static void amiga_init(void);
98 static int amiga_open_scsi(int bus, int tgt, int lun, SCSI *usalp);
99 static void amiga_close_scsi(int fd);
100 static void amiga_close_scsi_all(void);
101 static void amiga_scan_devices(void);
102 static int amiga_find_device(char *device);
103 static int amiga_open_timer(void);
104 static void amiga_close_timer(void);
105 static int amiga_get_scsi_bus(char *device);
108 * Return version information for the low level SCSI transport code.
109 * This has been introduced to make it easier to trace down problems
113 usalo_version(SCSI *usalp, int what)
115 if (usalp != (SCSI *)0) {
119 return (_usal_trans_version);
121 * If you changed this source, you are not allowed to
122 * return "schily" for the SCG_AUTHOR request.
125 /* return (_usal_auth_cdrkit);*/
135 usalo_help(SCSI *usalp, FILE *f)
137 __usal_help(f, "Amiga SCSI", "Generic SCSI",
138 "", "bus,target,lun or xxx.device:b,t,l", "1,2,0 or scsi.device:1,2,0", TRUE, FALSE);
143 usalo_open(SCSI *usalp, char *device)
145 int busno = usal_scsibus(usalp);
146 int tgt = usal_target(usalp);
147 int tlun = usal_lun(usalp);
152 register int nopen = 0;
154 if (initialized == 0) {
159 if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
162 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
163 "Illegal value for busno, target or lun '%d,%d,%d'",
168 if (usalp->local == NULL) {
169 usalp->local = malloc(sizeof (struct usal_local));
170 if (usalp->local == NULL)
173 for (b = 0; b < MAX_SCG; b++) {
174 for (t = 0; t < MAX_TGT; t++) {
175 for (l = 0; l < MAX_LUN; l++) {
176 usallocal(usalp)->usalfiles[b][t][l] = -1;
182 if (device == NULL || *device == '\0') {
184 if (last_bus == -1) {
185 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
186 "No scsi device found");
189 if (busno < 0 && tgt < 0 && tlun < 0) {
190 /* cdrecord -scanbus */
191 for (b = 0; b <= last_bus; b++) {
192 for (t = 0; t < MAX_TGT; t++) {
193 for (l = 0; l < MAX_LUN; l++) {
194 f = amiga_open_scsi(b, t, l, usalp);
196 usallocal(usalp)->usalfiles[b][t][l] = f;
203 /* cdrecord [-scanbus] dev=b,t,l */
204 f = amiga_open_scsi(busno, tgt, tlun, usalp);
206 usallocal(usalp)->usalfiles[busno][tgt][tlun] = f;
211 if (busno < 0 && tgt < 0 && tlun < 0) {
212 /* cdrecord -scanbus dev=xxx.device */
213 b = amiga_get_scsi_bus(device);
215 for (t = 0; t < MAX_TGT; t++) {
216 for (l = 0; l < MAX_LUN; l++) {
217 f = amiga_open_scsi(b, t, l, usalp);
219 usallocal(usalp)->usalfiles[b][t][l] = f;
225 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
226 "Scsi device %s not found", device);
229 /* cdrecord [-scanbus] dev=xxx.device:b,t,l */
231 * this is a special case, in which the scsi device is accessed just by
232 * name, bus parameter from the command is ignored.
234 b = amiga_get_scsi_bus(device);
236 f = amiga_open_scsi(b, tgt, tlun, usalp);
238 usallocal(usalp)->usalfiles[busno][tgt][tlun] = f;
242 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
243 "Scsi device %s not found", device);
252 usalo_close(SCSI *usalp)
258 if (usalp->local == NULL)
261 for (b = 0; b < MAX_SCG; b++) {
262 for (t = 0; t < MAX_TGT; t++) {
263 for (l = 0; l < MAX_LUN; l++) {
264 if (usallocal(usalp)->usalfiles[b][t][l] >= 0)
265 amiga_close_scsi(usallocal(usalp)->usalfiles[b][t][l]);
266 usallocal(usalp)->usalfiles[b][t][l] = -1;
274 usalo_maxdma(SCSI *usalp, long amt)
276 return (MAX_DMA_AMIGAOS);
280 usalo_getbuf(SCSI *usalp, long amt)
282 if (usalp->debug > 0) {
283 fprintf((FILE *)usalp->errfile,
284 "usalo_getbuf: %ld bytes\n", amt);
286 usalp->bufbase = valloc((size_t)(amt));
287 return (usalp->bufbase);
291 usalo_freebuf(SCSI *usalp)
294 free(usalp->bufbase);
295 usalp->bufbase = NULL;
299 usalo_havebus(SCSI *usalp, int busno)
304 if (busno < 0 || busno >= MAX_SCG)
307 if (usalp->local == NULL)
310 for (t = 0; t < MAX_TGT; t++) {
311 for (l = 0; l < MAX_LUN; l++)
312 if (usallocal(usalp)->usalfiles[busno][t][l] != -1)
319 usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
321 if (busno < 0 || busno >= MAX_SCG ||
322 tgt < 0 || tgt >= MAX_TGT ||
323 tlun < 0 || tlun >= MAX_LUN)
326 if (usalp->local == NULL)
329 return ((int)usallocal(usalp)->usalfiles[busno][tgt][tlun]);
333 usalo_initiator_id(SCSI *usalp)
339 usalo_isatapi(SCSI *usalp)
345 usalo_reset(SCSI *usalp, int what)
347 /* XXX Is there really no reset function on AmigaOS? */
353 usalo_send(SCSI *usalp)
355 register struct IOStdReq *ioreq = NULL;
358 struct usal_cmd *sp = usalp->scmd;
360 sp->error = SCG_NO_ERROR;
362 sp->u_scb.cmd_scb[0] = 0;
366 sp->error = SCG_FATAL;
369 ioreq = request[usalp->fd].ioReq;
371 sp->error = SCG_FATAL;
374 ioreq->io_Length = sizeof (struct SCSICmd);
375 ioreq->io_Data = &Cmd;
376 ioreq->io_Command = HD_SCSICMD;
378 Cmd.scsi_Flags = SCSIF_AUTOSENSE; /* We set the SCSI cmd len */
379 if (sp->flags & SCG_RECV_DATA)
380 Cmd.scsi_Flags |= SCSIF_READ;
381 else if (sp->size > 0)
382 Cmd.scsi_Flags |= SCSIF_WRITE;
384 Cmd.scsi_Command = sp->cdb.cmd_cdb;
385 Cmd.scsi_CmdLength = sp->cdb_len;
386 Cmd.scsi_Data = (UWORD *) sp->addr;
387 Cmd.scsi_Length = sp->size;
390 Cmd.scsi_SenseData = sp->u_sense.cmd_sense;
391 Cmd.scsi_SenseLength = sp->sense_len;
392 Cmd.scsi_SenseActual = 0;
395 do_scsi_cmd(ioreq, sp->timeout);
397 fillbytes(&sp->scb, sizeof (sp->scb), '\0');
398 sp->resid = Cmd.scsi_Length - Cmd.scsi_Actual;
402 sp->sense_count = Cmd.scsi_SenseActual;
403 if (sp->sense_count < 0)
406 if (sp->sense_count > SCG_MAX_SENSE)
407 sp->sense_count = SCG_MAX_SENSE;
408 sp->u_scb.cmd_scb[0] = Cmd.scsi_Status;
414 printf("ioreq->io_Error: %ld; status %ld\n", ioreq->io_Error, Cmd.scsi_Status);
416 switch (ioreq->io_Error) {
418 sp->error = SCG_NO_ERROR;
421 sp->error = SCG_TIMEOUT;
427 case HFERR_BadStatus:
428 if (Cmd.scsi_Status == CHECK_CONDITION) {
429 sp->error = SCG_NO_ERROR;
431 sp->error = SCG_RETRYABLE;
435 /* XXX was kann sonst noch passieren? */
436 sp->error = SCG_FATAL;
444 do_scsi_cmd(struct IOStdReq *scsi_io, int timeout)
447 ULONG timer_flag = 0;
452 scsi_flag = 1L<<scsi_io->io_Message.mn_ReplyPort->mp_SigBit;
453 wait_sigs = scsi_flag;
455 SetSignal(0L, scsi_flag);
456 SendIO((struct IORequest *)scsi_io);
458 timer_flag = 1L<<timerMsgPort->mp_SigBit;
459 wait_sigs |= timer_flag;
460 timer_io->tr_time.tv_secs = timeout;
461 SetSignal(0L, timer_flag);
462 SendIO((struct IORequest *)timer_io);
465 wait_ret = Wait(wait_sigs);
467 if (wait_ret & scsi_flag) {
468 WaitIO((struct IORequest *)scsi_io);
470 if (!CheckIO((struct IORequest *) timer_io)) {
471 AbortIO((struct IORequest *)timer_io);
472 WaitIO((struct IORequest *)timer_io);
475 } else if (wait_ret & timer_flag) {
476 WaitIO((struct IORequest *)timer_io);
477 if (!CheckIO((struct IORequest *) scsi_io)) {
478 AbortIO((struct IORequest *)scsi_io);
479 if (scsi_io->io_Error == IOERR_ABORTED) {
480 WaitIO((struct IORequest *)scsi_io);
483 scsi_io->io_Error = IOERR_TIMEOUT;
486 return (scsi_io->io_Error);
489 /*--------------------------------------------------------------------------*/
491 /* strlwr: seems not to be implemented in ixemul */
497 s1 = (unsigned char *)s;
499 if ((*s1 > ('A'-1)) && (*s1 < ('Z'+1)))
507 * amiga specific functions
512 memset(request, 0, sizeof (request));
513 amiga_scan_devices();
515 printf("scanning bus. %ld device(s) found\n", last_bus + 1);
517 atexit(amiga_close_scsi_all);
524 * searching all known scsi devices
525 * for first there is only a (full) support for scsi.device and amithlon.device.
526 * This are devices i tested.
527 * All the other devices have to be specified by name (i.e. cdrecord dev=blabla.device:0,0,0)
528 * but didn't appear in the scanbus list. Later they should/may be also added to the
529 * main_dev_list (after testing).
531 const char *main_dev_list[] = { "scsi.device", "amithlon.device", NULL };
536 main_dev = (char **)main_dev_list;
538 if (last_bus == MAX_SCG - 1)
540 if (amiga_find_device(*main_dev)) {
542 devs[last_bus] = strdup(*main_dev);
543 for (i = 2; i < MAX_SCG; i++) {
545 if (last_bus == MAX_SCG - 1)
548 sprintf(tmp, "2nd.%s", *main_dev);
550 sprintf(tmp, "3rd.%s", *main_dev);
552 sprintf(tmp, "%ldth.%s", i, *main_dev);
554 if (amiga_find_device(tmp)) {
556 devs[last_bus] = strdup(tmp);
567 amiga_close_scsi(int fd)
569 if (request[fd].ref_count > 0) {
570 request[fd].ref_count--;
571 if (request[fd].ref_count == 0) {
572 CloseDevice((struct IORequest *) request[fd].ioReq);
573 DeleteStdIO(request[fd].ioReq);
574 request[fd].ioReq = NULL;
576 printf("closing device fd %ld\n", fd);
583 amiga_close_scsi_all()
587 for (i = 0; i < MAX_DEV; i++) {
588 if (request[i].ioReq != NULL) {
590 printf("closing device fd %ld\n", i);
592 CloseDevice((struct IORequest *) request[i].ioReq);
593 DeleteStdIO(request[i].ioReq);
594 request[i].ioReq = NULL;
599 DeletePort(ioMsgPort);
604 for (i = 0; i < MAX_SCG; i++) {
610 amiga_open_scsi(int bus, int tgt, int lun, SCSI *usalp)
612 int fd = bus * MAX_TGT * MAX_LUN + tgt * MAX_LUN + lun;
613 int unit = DUNIT(bus, tgt, lun);
614 char *dev = devs[bus];
618 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
619 "No scsi device found at bus %ld\n", bus);
624 if (ioMsgPort == NULL) {
625 ioMsgPort = CreatePort(NULL, 0);
628 if (ioMsgPort != NULL) {
629 if (request[fd].ioReq == NULL) {
630 request[fd].ioReq = CreateStdIO(ioMsgPort);
633 if (request[fd].ioReq != NULL) {
635 printf("trying %s, unit %ld\n", dev, unit);
636 if (OpenDevice(dev, unit, (struct IORequest *)request[fd].ioReq, 0L)) {
638 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
639 "Cannot open %s\n", dev);
641 if (request[fd].ref_count == 0) {
642 DeleteStdIO(request[fd].ioReq);
643 request[fd].ioReq = NULL;
647 request[fd].ref_count++;
649 printf("opening %s, unit %ld as fd %ld count %ld\n", dev, unit, fd, request[fd].ref_count);
653 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
654 "Cannot create IOReq");
659 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
660 "Cannot open Message Port");
672 /* we need the timer to catch scsi timeouts */
673 if (timer_io == NULL) {
675 printf("opening timer\n");
677 timerMsgPort = CreatePort(NULL, 0);
679 timer_io = (struct timerequest *)CreateExtIO(timerMsgPort, sizeof (struct timerequest));
681 if (OpenDevice("timer.device", UNIT_MICROHZ, (struct IORequest *)timer_io, 0)) {
682 printf("Warning: can't open timer device\n");
683 DeleteExtIO((struct IORequest *) timer_io);
684 DeletePort(timerMsgPort);
689 timer_io->tr_node.io_Command = TR_ADDREQUEST;
690 timer_io->tr_time.tv_micro = 0;
693 printf("Warning: can't create timer request\n");
694 DeletePort(timerMsgPort);
699 printf("Warning: can't create timer port\n");
711 printf("closing timer\n");
713 AbortIO((struct IORequest *) timer_io);
714 WaitIO((struct IORequest *) timer_io);
715 CloseDevice((struct IORequest *)timer_io);
716 DeleteExtIO((struct IORequest *) timer_io);
717 DeletePort(timerMsgPort);
724 amiga_get_scsi_bus(char *device)
727 char *tmp = strdup(device);
730 for (i = 0; i <= last_bus; i++) {
731 if (strcmp(devs[i], tmp) == 0) {
736 if (amiga_find_device(tmp)) {
746 amiga_find_device(char *device)
749 struct Node *DeviceLibNode = SysBase->DeviceList.lh_Head;
752 printf("looking for %s ", device);
755 while (DeviceLibNode->ln_Succ) {
756 DeviceLibNode = DeviceLibNode->ln_Succ;
757 strcpy(tmp, DeviceLibNode->ln_Name);
759 if (strcmp(tmp, device) == 0) {
761 printf(" ... found\n");
768 printf(" ... not found\n");