/[debburn]/nonameyet/trunk/libscg/scsi-os2.c
ViewVC logotype

Contents of /nonameyet/trunk/libscg/scsi-os2.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Aug 18 17:34:13 2006 UTC (6 years, 10 months ago) by blade
File MIME type: text/plain
File size: 16927 byte(s)
Imported the resorted version a08
1 /* @(#)scsi-os2.c 1.25 04/01/15 Copyright 1998 J. Schilling, C. Wohlgemuth */
2 #ifndef lint
3 static char __sccsid[] =
4 "@(#)scsi-os2.c 1.25 04/01/15 Copyright 1998 J. Schilling, C. Wohlgemuth";
5 #endif
6 /*
7 * Interface for the OS/2 ASPI-Router ASPIROUT.SYS ((c) D. Dorau).
8 * This additional driver is a prerequisite for using cdrecord.
9 * Get it from HOBBES or LEO.
10 *
11 * Warning: you may change this source, but if you do that
12 * you need to change the _scg_version and _scg_auth* string below.
13 * You may not return "schily" for an SCG_AUTHOR request anymore.
14 * Choose your name instead of "schily" and make clear that the version
15 * string is related to a modified source.
16 *
17 * XXX it currently uses static SRB and for this reason is not reentrant
18 *
19 * Copyright (c) 1998 J. Schilling
20 * Copyright (c) 1998 C. Wohlgemuth for this interface.
21 */
22 /*
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License version 2
25 * as published by the Free Software Foundation.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License along with
33 * this program; see the file COPYING. If not, write to the Free Software
34 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 */
36
37 #undef sense
38
39 /*#define DEBUG*/
40
41 /* For AspiRouter */
42 #include "scg/srb_os2.h"
43
44 /*
45 * Warning: you may change this source, but if you do that
46 * you need to change the _scg_version and _scg_auth* string below.
47 * You may not return "schily" for an SCG_AUTHOR request anymore.
48 * Choose your name instead of "schily" and make clear that the version
49 * string is related to a modified source.
50 */
51 LOCAL char _scg_trans_version[] = "scsi-os2.c-1.25"; /* The version for this transport*/
52
53 #define FILE_OPEN 0x0001
54 #define OPEN_SHARE_DENYREADWRITE 0x0010
55 #define OPEN_ACCESS_READWRITE 0x0002
56 #define DC_SEM_SHARED 0x01
57 #define OBJ_TILE 0x0040
58 #define PAG_READ 0x0001
59 #define PAG_WRITE 0x0002
60 #define PAG_COMMIT 0x0010
61
62 typedef unsigned long LHANDLE;
63 typedef unsigned long ULONG;
64 typedef unsigned char *PSZ;
65 typedef unsigned short USHORT;
66 typedef unsigned char UCHAR;
67
68 typedef LHANDLE HFILE;
69 typedef ULONG HEV;
70
71 #define MAX_SCG 16 /* Max # of SCSI controllers */
72 #define MAX_TGT 16
73 #define MAX_LUN 8
74
75 struct scg_local {
76 int dummy;
77 };
78 #define scglocal(p) ((struct scg_local *)((p)->local))
79
80 #define MAX_DMA_OS2 (63*1024) /* ASPI-Router allows up to 64k */
81
82 LOCAL void *buffer = NULL;
83 LOCAL HFILE driver_handle = 0;
84 LOCAL HEV postSema = 0;
85
86 LOCAL BOOL open_driver __PR((SCSI *scgp));
87 LOCAL BOOL close_driver __PR((void));
88 LOCAL ULONG wait_post __PR((ULONG ulTimeOut));
89 LOCAL BOOL init_buffer __PR((void* mem));
90 LOCAL void exit_func __PR((void));
91 LOCAL void set_error __PR((SRB *srb, struct scg_cmd *sp));
92
93
94 LOCAL void
95 exit_func()
96 {
97 if (!close_driver())
98 js_fprintf(stderr, "Cannot close OS/2-ASPI-Router!\n");
99 }
100
101 /*
102 * Return version information for the low level SCSI transport code.
103 * This has been introduced to make it easier to trace down problems
104 * in applications.
105 */
106 LOCAL char *
107 scgo_version(scgp, what)
108 SCSI *scgp;
109 int what;
110 {
111 if (scgp != (SCSI *)0) {
112 switch (what) {
113
114 case SCG_VERSION:
115 return (_scg_trans_version);
116 /*
117 * If you changed this source, you are not allowed to
118 * return "schily" for the SCG_AUTHOR request.
119 */
120 case SCG_AUTHOR:
121 return (_scg_auth_schily);
122 case SCG_SCCS_ID:
123 return (__sccsid);
124 }
125 }
126 return ((char *)0);
127 }
128
129 LOCAL int
130 scgo_help(scgp, f)
131 SCSI *scgp;
132 FILE *f;
133 {
134 __scg_help(f, "ASPI", "Generic transport independent SCSI",
135 "", "bus,target,lun", "1,2,0", TRUE, FALSE);
136 return (0);
137 }
138
139 LOCAL int
140 scgo_open(scgp, device)
141 SCSI *scgp;
142 char *device;
143 {
144 int busno = scg_scsibus(scgp);
145 int tgt = scg_target(scgp);
146 int tlun = scg_lun(scgp);
147
148 if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
149 errno = EINVAL;
150 if (scgp->errstr)
151 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
152 "Illegal value for busno, target or lun '%d,%d,%d'",
153 busno, tgt, tlun);
154 return (-1);
155 }
156
157 if ((device != NULL && *device != '\0') || (busno == -2 && tgt == -2)) {
158 errno = EINVAL;
159 if (scgp->errstr)
160 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
161 "Open by 'devname' not supported on this OS");
162 return (-1);
163 }
164
165 if (scgp->local == NULL) {
166 scgp->local = malloc(sizeof (struct scg_local));
167 if (scgp->local == NULL)
168 return (0);
169 }
170
171 if (!open_driver(scgp)) /* Try to open ASPI-Router */
172 return (-1);
173 atexit(exit_func); /* Install Exit Function which closes the ASPI-Router */
174
175 /*
176 * Success after all
177 */
178 return (1);
179 }
180
181 LOCAL int
182 scgo_close(scgp)
183 SCSI *scgp;
184 {
185 exit_func();
186 return (0);
187 }
188
189 LOCAL long
190 scgo_maxdma(scgp, amt)
191 SCSI *scgp;
192 long amt;
193 {
194 long maxdma = MAX_DMA_OS2;
195 return (maxdma);
196 }
197
198 LOCAL void *
199 scgo_getbuf(scgp, amt)
200 SCSI *scgp;
201 long amt;
202 {
203 ULONG rc;
204
205 #ifdef DEBUG
206 js_fprintf((FILE *)scgp->errfile, "scgo_getbuf: %ld bytes\n", amt);
207 #endif
208 rc = DosAllocMem(&buffer, amt, OBJ_TILE | PAG_READ | PAG_WRITE | PAG_COMMIT);
209
210 if (rc) {
211 js_fprintf((FILE *)scgp->errfile, "Cannot allocate buffer.\n");
212 return ((void *)0);
213 }
214 scgp->bufbase = buffer;
215
216 #ifdef DEBUG
217 js_fprintf((FILE *)scgp->errfile, "Buffer allocated at: 0x%x\n", scgp->bufbase);
218 #endif
219
220 /* Lock memory */
221 if (init_buffer(scgp->bufbase))
222 return (scgp->bufbase);
223
224 js_fprintf((FILE *)scgp->errfile, "Cannot lock memory buffer.\n");
225 return ((void *)0); /* Error */
226 }
227
228 LOCAL void
229 scgo_freebuf(scgp)
230 SCSI *scgp;
231 {
232 if (scgp->bufbase && DosFreeMem(scgp->bufbase)) {
233 js_fprintf((FILE *)scgp->errfile,
234 "Cannot free buffer memory for ASPI-Router!\n"); /* Free our memory buffer if not already done */
235 }
236 if (buffer == scgp->bufbase)
237 buffer = NULL;
238 scgp->bufbase = NULL;
239 }
240
241 LOCAL BOOL
242 scgo_havebus(scgp, busno)
243 SCSI *scgp;
244 int busno;
245 {
246 register int t;
247 register int l;
248
249 if (busno < 0 || busno >= MAX_SCG)
250 return (FALSE);
251
252 return (TRUE);
253 }
254
255 LOCAL int
256 scgo_fileno(scgp, busno, tgt, tlun)
257 SCSI *scgp;
258 int busno;
259 int tgt;
260 int tlun;
261 {
262 if (busno < 0 || busno >= MAX_SCG ||
263 tgt < 0 || tgt >= MAX_TGT ||
264 tlun < 0 || tlun >= MAX_LUN)
265 return (-1);
266
267 /*
268 * Return fake
269 */
270 return (1);
271 }
272
273
274 LOCAL int
275 scgo_initiator_id(scgp)
276 SCSI *scgp;
277 {
278 return (-1);
279 }
280
281 LOCAL int
282 scgo_isatapi(scgp)
283 SCSI *scgp;
284 {
285 return (FALSE);
286 }
287
288
289 LOCAL int
290 scgo_reset(scgp, what)
291 SCSI *scgp;
292 int what;
293 {
294 ULONG rc; /* return value */
295 ULONG cbreturn;
296 ULONG cbParam;
297 BOOL success;
298 static SRB SRBlock; /* XXX makes it non reentrant */
299
300 if (what == SCG_RESET_NOP)
301 return (0);
302 if (what != SCG_RESET_BUS) {
303 errno = EINVAL;
304 return (-1);
305 }
306 /*
307 * XXX Does this reset TGT or BUS ???
308 */
309 SRBlock.cmd = SRB_Reset; /* reset device */
310 SRBlock.ha_num = scg_scsibus(scgp); /* host adapter number */
311 SRBlock.flags = SRB_Post; /* posting enabled */
312 SRBlock.u.res.target = scg_target(scgp); /* target id */
313 SRBlock.u.res.lun = scg_lun(scgp); /* target LUN */
314
315 rc = DosDevIOCtl(driver_handle, 0x92, 0x02, (void*) &SRBlock, sizeof (SRB), &cbParam,
316 (void*) &SRBlock, sizeof (SRB), &cbreturn);
317 if (rc) {
318 js_fprintf((FILE *)scgp->errfile,
319 "DosDevIOCtl() failed in resetDevice.\n");
320 return (1); /* DosDevIOCtl failed */
321 } else {
322 success = wait_post(40000); /** wait for SRB being processed */
323 if (success)
324 return (2);
325 }
326 if (SRBlock.status != SRB_Done)
327 return (3);
328 #ifdef DEBUG
329 js_fprintf((FILE *)scgp->errfile,
330 "resetDevice of host: %d target: %d lun: %d successful.\n", scg_scsibus(scgp), scg_target(scgp), scg_lun(scgp));
331 js_fprintf((FILE *)scgp->errfile,
332 "SRBlock.ha_status: 0x%x, SRBlock.target_status: 0x%x, SRBlock.satus: 0x%x\n",
333 SRBlock.u.cmd.ha_status, SRBlock.u.cmd.target_status, SRBlock.status);
334 #endif
335 return (0);
336 }
337
338 /*
339 * Set error flags
340 */
341 LOCAL void
342 set_error(srb, sp)
343 SRB *srb;
344 struct scg_cmd *sp;
345 {
346 switch (srb->status) {
347
348 case SRB_InvalidCmd: /* 0x80 Invalid SCSI request */
349 case SRB_InvalidHA: /* 0x81 Invalid host adapter number */
350 case SRB_BadDevice: /* 0x82 SCSI device not installed */
351 sp->error = SCG_FATAL;
352 sp->ux_errno = EINVAL; /* Should we ever return != EIO */
353 sp->ux_errno = EIO;
354 break;
355
356
357 case SRB_Busy: /* 0x00 SCSI request in progress */
358 case SRB_Aborted: /* 0x02 SCSI aborted by host */
359 case SRB_BadAbort: /* 0x03 Unable to abort SCSI request */
360 case SRB_Error: /* 0x04 SCSI request completed with error */
361 default:
362 sp->error = SCG_RETRYABLE;
363 sp->ux_errno = EIO;
364 break;
365 }
366 }
367
368 LOCAL int
369 scgo_send(scgp)
370 SCSI *scgp;
371 {
372 struct scg_cmd *sp = scgp->scmd;
373 ULONG rc; /* return value */
374 static SRB SRBlock; /* XXX makes it non reentrant */
375 Ulong cbreturn;
376 Ulong cbParam;
377 UCHAR* ptr;
378
379 if (scgp->fd < 0) { /* Set in scgo_open() */
380 sp->error = SCG_FATAL;
381 return (0);
382 }
383
384 if (sp->cdb_len > sizeof (SRBlock.u.cmd.cdb_st)) { /* commandsize too big */
385 sp->error = SCG_FATAL;
386 sp->ux_errno = EINVAL;
387 js_fprintf((FILE *)scgp->errfile,
388 "sp->cdb_len > SRBlock.u.cmd.cdb_st. Fatal error in scgo_send, exiting...\n");
389 return (-1);
390 }
391
392 /* clear command block */
393 fillbytes((caddr_t)&SRBlock.u.cmd.cdb_st, sizeof (SRBlock.u.cmd.cdb_st), '\0');
394 /* copy cdrecord command into SRB */
395 movebytes(&sp->cdb, &SRBlock.u.cmd.cdb_st, sp->cdb_len);
396
397 /* Build SRB command block */
398 SRBlock.cmd = SRB_Command;
399 SRBlock.ha_num = scg_scsibus(scgp); /* host adapter number */
400
401 SRBlock.flags = SRB_Post; /* flags */
402
403 SRBlock.u.cmd.target = scg_target(scgp); /* Target SCSI ID */
404 SRBlock.u.cmd.lun = scg_lun(scgp); /* Target SCSI LUN */
405 SRBlock.u.cmd.data_len = sp->size; /* # of bytes transferred */
406 SRBlock.u.cmd.data_ptr = 0; /* pointer to data buffer */
407 SRBlock.u.cmd.sense_len = sp->sense_len; /* length of sense buffer */
408
409 SRBlock.u.cmd.link_ptr = 0; /* pointer to next SRB */
410 SRBlock.u.cmd.cdb_len = sp->cdb_len; /* SCSI command length */
411
412 /* Specify direction */
413 if (sp->flags & SCG_RECV_DATA) {
414 SRBlock.flags |= SRB_Read;
415 } else {
416 if (sp->size > 0) {
417 SRBlock.flags |= SRB_Write;
418 if (scgp->bufbase != sp->addr) { /* Copy only if data not in ASPI-Mem */
419 movebytes(sp->addr, scgp->bufbase, sp->size);
420 }
421 } else {
422 SRBlock.flags |= SRB_NoTransfer;
423 }
424 }
425 sp->error = SCG_NO_ERROR;
426 sp->sense_count = 0;
427 sp->u_scb.cmd_scb[0] = 0;
428 sp->resid = 0;
429
430 /* execute SCSI command */
431 rc = DosDevIOCtl(driver_handle, 0x92, 0x02,
432 (void*) &SRBlock, sizeof (SRB), &cbParam,
433 (void*) &SRBlock, sizeof (SRB), &cbreturn);
434
435 if (rc) { /* An error occured */
436 js_fprintf((FILE *)scgp->errfile,
437 "DosDevIOCtl() in sendCommand failed.\n");
438 sp->error = SCG_FATAL;
439 sp->ux_errno = EIO; /* Später vielleicht errno einsetzen */
440 return (rc);
441 } else {
442 /* Wait until the command is processed */
443 rc = wait_post(sp->timeout*1000);
444 if (rc) { /* An error occured */
445 if (rc == 640) {
446 /* Timeout */
447 sp->error = SCG_TIMEOUT;
448 sp->ux_errno = EIO;
449 js_fprintf((FILE *)scgp->errfile,
450 "Timeout during SCSI-Command.\n");
451 return (1);
452 }
453 sp->error = SCG_FATAL;
454 sp->ux_errno = EIO;
455 js_fprintf((FILE *)scgp->errfile,
456 "Fatal Error during DosWaitEventSem().\n");
457 return (1);
458 }
459
460 /* The command is processed */
461 if (SRBlock.status == SRB_Done) { /* succesful completion */
462 #ifdef DEBUG
463 js_fprintf((FILE *)scgp->errfile,
464 "Command successful finished. SRBlock.status=0x%x\n\n", SRBlock.status);
465 #endif
466 sp->sense_count = 0;
467 sp->resid = 0;
468 if (sp->flags & SCG_RECV_DATA) { /* We read data */
469 if (sp->addr && sp->size) {
470 if (scgp->bufbase != sp->addr) /* Copy only if data not in ASPI-Mem */
471 movebytes(scgp->bufbase, sp->addr, SRBlock.u.cmd.data_len);
472 ptr = (UCHAR*)sp->addr;
473 sp->resid = sp->size - SRBlock.u.cmd.data_len; /*nicht übertragene bytes. Korrekt berechnet???*/
474 }
475 } /* end of if (sp->flags & SCG_RECV_DATA) */
476 if (SRBlock.u.cmd.target_status == SRB_CheckStatus) { /* Sense data valid */
477 sp->sense_count = (int)SRBlock.u.cmd.sense_len;
478 if (sp->sense_count > sp->sense_len)
479 sp->sense_count = sp->sense_len;
480
481 ptr = (UCHAR*)&SRBlock.u.cmd.cdb_st;
482 ptr += SRBlock.u.cmd.cdb_len;
483
484 fillbytes(&sp->u_sense.Sense, sizeof (sp->u_sense.Sense), '\0');
485 movebytes(ptr, &sp->u_sense.Sense, sp->sense_len);
486
487 sp->u_scb.cmd_scb[0] = SRBlock.u.cmd.target_status;
488 sp->ux_errno = EIO; /* Später differenzieren? */
489 }
490 return (0);
491 }
492 /* SCSI-Error occured */
493 set_error(&SRBlock, sp);
494
495 if (SRBlock.u.cmd.target_status == SRB_CheckStatus) { /* Sense data valid */
496 sp->sense_count = (int)SRBlock.u.cmd.sense_len;
497 if (sp->sense_count > sp->sense_len)
498 sp->sense_count = sp->sense_len;
499
500 ptr = (UCHAR*)&SRBlock.u.cmd.cdb_st;
501 ptr += SRBlock.u.cmd.cdb_len;
502
503 fillbytes(&sp->u_sense.Sense, sizeof (sp->u_sense.Sense), '\0');
504 movebytes(ptr, &sp->u_sense.Sense, sp->sense_len);
505
506 sp->u_scb.cmd_scb[0] = SRBlock.u.cmd.target_status;
507 }
508 if (sp->flags & SCG_RECV_DATA) {
509 if (sp->addr && sp->size) {
510 if (scgp->bufbase != sp->addr) /* Copy only if data not in ASPI-Mem */
511 movebytes(scgp->bufbase, sp->addr, SRBlock.u.cmd.data_len);
512 }
513 }
514 #ifdef really
515 sp->resid = SRBlock.u.cmd.data_len; /* XXXXX Got no Data ????? */
516 #else
517 sp->resid = sp->size - SRBlock.u.cmd.data_len;
518 #endif
519 return (1);
520 }
521 }
522
523 /***************************************************************************
524 * *
525 * BOOL open_driver() *
526 * *
527 * Opens the ASPI Router device driver and sets device_handle. *
528 * Returns: *
529 * TRUE - Success *
530 * FALSE - Unsuccessful opening of device driver *
531 * *
532 * Preconditions: ASPI Router driver has be loaded *
533 * *
534 ***************************************************************************/
535 LOCAL BOOL
536 open_driver(scgp)
537 SCSI *scgp;
538 {
539 ULONG rc; /* return value */
540 ULONG ActionTaken; /* return value */
541 USHORT openSemaReturn; /* return value */
542 ULONG cbreturn;
543 ULONG cbParam;
544
545 if (driver_handle) /* ASPI-Router already opened */
546 return (TRUE);
547
548 rc = DosOpen((PSZ) "aspirou$", /* open driver*/
549 &driver_handle,
550 &ActionTaken,
551 0,
552 0,
553 FILE_OPEN,
554 OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE,
555 NULL);
556 if (rc) {
557 js_fprintf((FILE *)scgp->errfile,
558 "Cannot open ASPI-Router!\n");
559
560 return (FALSE); /* opening failed -> return false*/
561 }
562
563 /* Init semaphore */
564 if (DosCreateEventSem(NULL, &postSema, /* create event semaphore */
565 DC_SEM_SHARED, 0)) {
566 DosClose(driver_handle);
567 js_fprintf((FILE *)scgp->errfile,
568 "Cannot create event semaphore!\n");
569
570 return (FALSE);
571 }
572 rc = DosDevIOCtl(driver_handle, 0x92, 0x03, /* pass semaphore handle */
573 (void*) &postSema, sizeof (HEV), /* to driver */
574 &cbParam, (void*) &openSemaReturn,
575 sizeof (USHORT), &cbreturn);
576
577 if (rc||openSemaReturn) { /* Error */
578 DosCloseEventSem(postSema);
579 DosClose(driver_handle);
580 return (FALSE);
581 }
582 return (TRUE);
583 }
584
585 /***************************************************************************
586 * *
587 * BOOL close_driver() *
588 * *
589 * Closes the device driver *
590 * Returns: *
591 * TRUE - Success *
592 * FALSE - Unsuccessful closing of device driver *
593 * *
594 * Preconditions: ASPI Router driver has be opened with open_driver *
595 * *
596 ***************************************************************************/
597 LOCAL BOOL
598 close_driver()
599 {
600 ULONG rc; /* return value */
601
602 if (driver_handle) {
603 rc = DosClose(driver_handle);
604 if (rc)
605 return (FALSE); /* closing failed -> return false */
606 driver_handle = 0;
607 if (DosCloseEventSem(postSema))
608 js_fprintf(stderr, "Cannot close event semaphore!\n");
609 if (buffer && DosFreeMem(buffer)) {
610 js_fprintf(stderr,
611 "Cannot free buffer memory for ASPI-Router!\n"); /* Free our memory buffer if not already done */
612 }
613 buffer = NULL;
614 }
615 return (TRUE);
616 }
617
618 LOCAL ULONG
619 wait_post(ULONG ulTimeOut)
620 {
621 ULONG count = 0;
622 ULONG rc; /* return value */
623
624 /* rc = DosWaitEventSem(postSema, -1);*/ /* wait forever*/
625 rc = DosWaitEventSem(postSema, ulTimeOut);
626 DosResetEventSem(postSema, &count);
627 return (rc);
628 }
629
630 LOCAL BOOL
631 init_buffer(mem)
632 void *mem;
633 {
634 ULONG rc; /* return value */
635 USHORT lockSegmentReturn; /* return value */
636 Ulong cbreturn;
637 Ulong cbParam;
638
639 rc = DosDevIOCtl(driver_handle, 0x92, 0x04, /* pass buffers pointer */
640 (void*) mem, sizeof (void*), /* to driver */
641 &cbParam, (void*) &lockSegmentReturn,
642 sizeof (USHORT), &cbreturn);
643 if (rc)
644 return (FALSE); /* DosDevIOCtl failed */
645 if (lockSegmentReturn)
646 return (FALSE); /* Driver could not lock segment */
647 return (TRUE);
648 }
649 #define sense u_sense.Sense

  ViewVC Help
Powered by ViewVC 1.1.5